~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/userfaultfd.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/userfaultfd.c (Version linux-6.11.5) and /fs/userfaultfd.c (Version linux-4.9.337)


  1 // SPDX-License-Identifier: GPL-2.0-only       << 
  2 /*                                                  1 /*
  3  *  fs/userfaultfd.c                                2  *  fs/userfaultfd.c
  4  *                                                  3  *
  5  *  Copyright (C) 2007  Davide Libenzi <davide      4  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
  6  *  Copyright (C) 2008-2009 Red Hat, Inc.           5  *  Copyright (C) 2008-2009 Red Hat, Inc.
  7  *  Copyright (C) 2015  Red Hat, Inc.               6  *  Copyright (C) 2015  Red Hat, Inc.
  8  *                                                  7  *
                                                   >>   8  *  This work is licensed under the terms of the GNU GPL, version 2. See
                                                   >>   9  *  the COPYING file in the top-level directory.
                                                   >>  10  *
  9  *  Some part derived from fs/eventfd.c (anon      11  *  Some part derived from fs/eventfd.c (anon inode setup) and
 10  *  mm/ksm.c (mm hashing).                         12  *  mm/ksm.c (mm hashing).
 11  */                                                13  */
 12                                                    14 
 13 #include <linux/list.h>                        << 
 14 #include <linux/hashtable.h>                       15 #include <linux/hashtable.h>
 15 #include <linux/sched/signal.h>                !!  16 #include <linux/sched.h>
 16 #include <linux/sched/mm.h>                    << 
 17 #include <linux/mm.h>                              17 #include <linux/mm.h>
 18 #include <linux/mm_inline.h>                   << 
 19 #include <linux/mmu_notifier.h>                << 
 20 #include <linux/poll.h>                            18 #include <linux/poll.h>
 21 #include <linux/slab.h>                            19 #include <linux/slab.h>
 22 #include <linux/seq_file.h>                        20 #include <linux/seq_file.h>
 23 #include <linux/file.h>                            21 #include <linux/file.h>
 24 #include <linux/bug.h>                             22 #include <linux/bug.h>
 25 #include <linux/anon_inodes.h>                     23 #include <linux/anon_inodes.h>
 26 #include <linux/syscalls.h>                        24 #include <linux/syscalls.h>
 27 #include <linux/userfaultfd_k.h>                   25 #include <linux/userfaultfd_k.h>
 28 #include <linux/mempolicy.h>                       26 #include <linux/mempolicy.h>
 29 #include <linux/ioctl.h>                           27 #include <linux/ioctl.h>
 30 #include <linux/security.h>                        28 #include <linux/security.h>
 31 #include <linux/hugetlb.h>                     << 
 32 #include <linux/swapops.h>                     << 
 33 #include <linux/miscdevice.h>                  << 
 34 #include <linux/uio.h>                         << 
 35                                                << 
 36 static int sysctl_unprivileged_userfaultfd __r << 
 37                                                << 
 38 #ifdef CONFIG_SYSCTL                           << 
 39 static struct ctl_table vm_userfaultfd_table[] << 
 40         {                                      << 
 41                 .procname       = "unprivilege << 
 42                 .data           = &sysctl_unpr << 
 43                 .maxlen         = sizeof(sysct << 
 44                 .mode           = 0644,        << 
 45                 .proc_handler   = proc_dointve << 
 46                 .extra1         = SYSCTL_ZERO, << 
 47                 .extra2         = SYSCTL_ONE,  << 
 48         },                                     << 
 49 };                                             << 
 50 #endif                                         << 
 51                                                    29 
 52 static struct kmem_cache *userfaultfd_ctx_cach !!  30 static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
 53                                                    31 
 54 struct userfaultfd_fork_ctx {                  !!  32 enum userfaultfd_state {
 55         struct userfaultfd_ctx *orig;          !!  33         UFFD_STATE_WAIT_API,
 56         struct userfaultfd_ctx *new;           !!  34         UFFD_STATE_RUNNING,
 57         struct list_head list;                 << 
 58 };                                                 35 };
 59                                                    36 
 60 struct userfaultfd_unmap_ctx {                 !!  37 /*
 61         struct userfaultfd_ctx *ctx;           !!  38  * Start with fault_pending_wqh and fault_wqh so they're more likely
 62         unsigned long start;                   !!  39  * to be in the same cacheline.
 63         unsigned long end;                     !!  40  */
 64         struct list_head list;                 !!  41 struct userfaultfd_ctx {
                                                   >>  42         /* waitqueue head for the pending (i.e. not read) userfaults */
                                                   >>  43         wait_queue_head_t fault_pending_wqh;
                                                   >>  44         /* waitqueue head for the userfaults */
                                                   >>  45         wait_queue_head_t fault_wqh;
                                                   >>  46         /* waitqueue head for the pseudo fd to wakeup poll/read */
                                                   >>  47         wait_queue_head_t fd_wqh;
                                                   >>  48         /* a refile sequence protected by fault_pending_wqh lock */
                                                   >>  49         struct seqcount refile_seq;
                                                   >>  50         /* pseudo fd refcounting */
                                                   >>  51         atomic_t refcount;
                                                   >>  52         /* userfaultfd syscall flags */
                                                   >>  53         unsigned int flags;
                                                   >>  54         /* state machine */
                                                   >>  55         enum userfaultfd_state state;
                                                   >>  56         /* released */
                                                   >>  57         bool released;
                                                   >>  58         /* mm with one ore more vmas attached to this userfaultfd_ctx */
                                                   >>  59         struct mm_struct *mm;
 65 };                                                 60 };
 66                                                    61 
 67 struct userfaultfd_wait_queue {                    62 struct userfaultfd_wait_queue {
 68         struct uffd_msg msg;                       63         struct uffd_msg msg;
 69         wait_queue_entry_t wq;                 !!  64         wait_queue_t wq;
 70         struct userfaultfd_ctx *ctx;               65         struct userfaultfd_ctx *ctx;
 71         bool waken;                                66         bool waken;
 72 };                                                 67 };
 73                                                    68 
 74 struct userfaultfd_wake_range {                    69 struct userfaultfd_wake_range {
 75         unsigned long start;                       70         unsigned long start;
 76         unsigned long len;                         71         unsigned long len;
 77 };                                                 72 };
 78                                                    73 
 79 /* internal indication that UFFD_API ioctl was !!  74 static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
 80 #define UFFD_FEATURE_INITIALIZED               << 
 81                                                << 
 82 static bool userfaultfd_is_initialized(struct  << 
 83 {                                              << 
 84         return ctx->features & UFFD_FEATURE_IN << 
 85 }                                              << 
 86                                                << 
 87 static bool userfaultfd_wp_async_ctx(struct us << 
 88 {                                              << 
 89         return ctx && (ctx->features & UFFD_FE << 
 90 }                                              << 
 91                                                << 
 92 /*                                             << 
 93  * Whether WP_UNPOPULATED is enabled on the uf << 
 94  * meaningful when userfaultfd_wp()==true on t << 
 95  * anonymous.                                  << 
 96  */                                            << 
 97 bool userfaultfd_wp_unpopulated(struct vm_area << 
 98 {                                              << 
 99         struct userfaultfd_ctx *ctx = vma->vm_ << 
100                                                << 
101         if (!ctx)                              << 
102                 return false;                  << 
103                                                << 
104         return ctx->features & UFFD_FEATURE_WP << 
105 }                                              << 
106                                                << 
107 static void userfaultfd_set_vm_flags(struct vm << 
108                                      vm_flags_ << 
109 {                                              << 
110         const bool uffd_wp_changed = (vma->vm_ << 
111                                                << 
112         vm_flags_reset(vma, flags);            << 
113         /*                                     << 
114          * For shared mappings, we want to ena << 
115          * userfaultfd-wp is enabled (see vma_ << 
116          * recalculate vma->vm_page_prot whene << 
117          */                                    << 
118         if ((vma->vm_flags & VM_SHARED) && uff << 
119                 vma_set_page_prot(vma);        << 
120 }                                              << 
121                                                << 
122 static int userfaultfd_wake_function(wait_queu << 
123                                      int wake_     75                                      int wake_flags, void *key)
124 {                                                  76 {
125         struct userfaultfd_wake_range *range =     77         struct userfaultfd_wake_range *range = key;
126         int ret;                                   78         int ret;
127         struct userfaultfd_wait_queue *uwq;        79         struct userfaultfd_wait_queue *uwq;
128         unsigned long start, len;                  80         unsigned long start, len;
129                                                    81 
130         uwq = container_of(wq, struct userfaul     82         uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
131         ret = 0;                                   83         ret = 0;
132         /* len == 0 means wake all */              84         /* len == 0 means wake all */
133         start = range->start;                      85         start = range->start;
134         len = range->len;                          86         len = range->len;
135         if (len && (start > uwq->msg.arg.pagef     87         if (len && (start > uwq->msg.arg.pagefault.address ||
136                     start + len <= uwq->msg.ar     88                     start + len <= uwq->msg.arg.pagefault.address))
137                 goto out;                          89                 goto out;
138         WRITE_ONCE(uwq->waken, true);              90         WRITE_ONCE(uwq->waken, true);
139         /*                                         91         /*
140          * The Program-Order guarantees provid !!  92          * The implicit smp_mb__before_spinlock in try_to_wake_up()
141          * ensure uwq->waken is visible before !!  93          * renders uwq->waken visible to other CPUs before the task is
                                                   >>  94          * waken.
142          */                                        95          */
143         ret = wake_up_state(wq->private, mode)     96         ret = wake_up_state(wq->private, mode);
144         if (ret) {                             !!  97         if (ret)
145                 /*                                 98                 /*
146                  * Wake only once, autoremove      99                  * Wake only once, autoremove behavior.
147                  *                                100                  *
148                  * After the effect of list_de !! 101                  * After the effect of list_del_init is visible to the
149                  * CPUs, the waitqueue may dis !! 102                  * other CPUs, the waitqueue may disappear from under
150                  * !list_empty_careful() in ha !! 103                  * us, see the !list_empty_careful() in
151                  *                             !! 104                  * handle_userfault(). try_to_wake_up() has an
152                  * try_to_wake_up() has an imp !! 105                  * implicit smp_mb__before_spinlock, and the
153                  * wq->private is read before  !! 106                  * wq->private is read before calling the extern
154                  * "wake_up_state" (which in t !! 107                  * function "wake_up_state" (which in turns calls
                                                   >> 108                  * try_to_wake_up). While the spin_lock;spin_unlock;
                                                   >> 109                  * wouldn't be enough, the smp_mb__before_spinlock is
                                                   >> 110                  * enough to avoid an explicit smp_mb() here.
155                  */                               111                  */
156                 list_del_init(&wq->entry);     !! 112                 list_del_init(&wq->task_list);
157         }                                      << 
158 out:                                              113 out:
159         return ret;                               114         return ret;
160 }                                                 115 }
161                                                   116 
162 /**                                               117 /**
163  * userfaultfd_ctx_get - Acquires a reference     118  * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
164  * context.                                       119  * context.
165  * @ctx: [in] Pointer to the userfaultfd conte    120  * @ctx: [in] Pointer to the userfaultfd context.
                                                   >> 121  *
                                                   >> 122  * Returns: In case of success, returns not zero.
166  */                                               123  */
167 static void userfaultfd_ctx_get(struct userfau    124 static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
168 {                                                 125 {
169         refcount_inc(&ctx->refcount);          !! 126         if (!atomic_inc_not_zero(&ctx->refcount))
                                                   >> 127                 BUG();
170 }                                                 128 }
171                                                   129 
172 /**                                               130 /**
173  * userfaultfd_ctx_put - Releases a reference     131  * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
174  * context.                                       132  * context.
175  * @ctx: [in] Pointer to userfaultfd context.     133  * @ctx: [in] Pointer to userfaultfd context.
176  *                                                134  *
177  * The userfaultfd context reference must have    135  * The userfaultfd context reference must have been previously acquired either
178  * with userfaultfd_ctx_get() or userfaultfd_c    136  * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
179  */                                               137  */
180 static void userfaultfd_ctx_put(struct userfau    138 static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
181 {                                                 139 {
182         if (refcount_dec_and_test(&ctx->refcou !! 140         if (atomic_dec_and_test(&ctx->refcount)) {
183                 VM_BUG_ON(spin_is_locked(&ctx-    141                 VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
184                 VM_BUG_ON(waitqueue_active(&ct    142                 VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
185                 VM_BUG_ON(spin_is_locked(&ctx-    143                 VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
186                 VM_BUG_ON(waitqueue_active(&ct    144                 VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
187                 VM_BUG_ON(spin_is_locked(&ctx- << 
188                 VM_BUG_ON(waitqueue_active(&ct << 
189                 VM_BUG_ON(spin_is_locked(&ctx-    145                 VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
190                 VM_BUG_ON(waitqueue_active(&ct    146                 VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
191                 mmdrop(ctx->mm);                  147                 mmdrop(ctx->mm);
192                 kmem_cache_free(userfaultfd_ct    148                 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
193         }                                         149         }
194 }                                                 150 }
195                                                   151 
196 static inline void msg_init(struct uffd_msg *m    152 static inline void msg_init(struct uffd_msg *msg)
197 {                                                 153 {
198         BUILD_BUG_ON(sizeof(struct uffd_msg) !    154         BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
199         /*                                        155         /*
200          * Must use memset to zero out the pad    156          * Must use memset to zero out the paddings or kernel data is
201          * leaked to userland.                    157          * leaked to userland.
202          */                                       158          */
203         memset(msg, 0, sizeof(struct uffd_msg)    159         memset(msg, 0, sizeof(struct uffd_msg));
204 }                                                 160 }
205                                                   161 
206 static inline struct uffd_msg userfault_msg(un    162 static inline struct uffd_msg userfault_msg(unsigned long address,
207                                             un << 
208                                             un    163                                             unsigned int flags,
209                                             un !! 164                                             unsigned long reason)
210                                             un << 
211 {                                                 165 {
212         struct uffd_msg msg;                      166         struct uffd_msg msg;
213                                                << 
214         msg_init(&msg);                           167         msg_init(&msg);
215         msg.event = UFFD_EVENT_PAGEFAULT;         168         msg.event = UFFD_EVENT_PAGEFAULT;
216                                                !! 169         msg.arg.pagefault.address = address;
217         msg.arg.pagefault.address = (features  << 
218                                     real_addre << 
219                                                << 
220         /*                                     << 
221          * These flags indicate why the userfa << 
222          * - UFFD_PAGEFAULT_FLAG_WP indicates  << 
223          * - UFFD_PAGEFAULT_FLAG_MINOR indicat << 
224          * - Neither of these flags being set  << 
225          *                                     << 
226          * Separately, UFFD_PAGEFAULT_FLAG_WRI << 
227          * fault. Otherwise, it was a read fau << 
228          */                                    << 
229         if (flags & FAULT_FLAG_WRITE)             170         if (flags & FAULT_FLAG_WRITE)
                                                   >> 171                 /*
                                                   >> 172                  * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
                                                   >> 173                  * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
                                                   >> 174                  * was not set in a UFFD_EVENT_PAGEFAULT, it means it
                                                   >> 175                  * was a read fault, otherwise if set it means it's
                                                   >> 176                  * a write fault.
                                                   >> 177                  */
230                 msg.arg.pagefault.flags |= UFF    178                 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
231         if (reason & VM_UFFD_WP)                  179         if (reason & VM_UFFD_WP)
                                                   >> 180                 /*
                                                   >> 181                  * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
                                                   >> 182                  * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
                                                   >> 183                  * not set in a UFFD_EVENT_PAGEFAULT, it means it was
                                                   >> 184                  * a missing fault, otherwise if set it means it's a
                                                   >> 185                  * write protect fault.
                                                   >> 186                  */
232                 msg.arg.pagefault.flags |= UFF    187                 msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
233         if (reason & VM_UFFD_MINOR)            << 
234                 msg.arg.pagefault.flags |= UFF << 
235         if (features & UFFD_FEATURE_THREAD_ID) << 
236                 msg.arg.pagefault.feat.ptid =  << 
237         return msg;                               188         return msg;
238 }                                                 189 }
239                                                   190 
240 #ifdef CONFIG_HUGETLB_PAGE                     << 
241 /*                                             << 
242  * Same functionality as userfaultfd_must_wait << 
243  * hugepmd ranges.                             << 
244  */                                            << 
245 static inline bool userfaultfd_huge_must_wait( << 
246                                                << 
247                                                << 
248 {                                              << 
249         struct vm_area_struct *vma = vmf->vma; << 
250         pte_t *ptep, pte;                      << 
251         bool ret = true;                       << 
252                                                << 
253         assert_fault_locked(vmf);              << 
254                                                << 
255         ptep = hugetlb_walk(vma, vmf->address, << 
256         if (!ptep)                             << 
257                 goto out;                      << 
258                                                << 
259         ret = false;                           << 
260         pte = huge_ptep_get(vma->vm_mm, vmf->a << 
261                                                << 
262         /*                                     << 
263          * Lockless access: we're in a wait_ev << 
264          * changes under us.  PTE markers shou << 
265          * ptes here.                          << 
266          */                                    << 
267         if (huge_pte_none_mostly(pte))         << 
268                 ret = true;                    << 
269         if (!huge_pte_write(pte) && (reason &  << 
270                 ret = true;                    << 
271 out:                                           << 
272         return ret;                            << 
273 }                                              << 
274 #else                                          << 
275 static inline bool userfaultfd_huge_must_wait( << 
276                                                << 
277                                                << 
278 {                                              << 
279         return false;   /* should never get he << 
280 }                                              << 
281 #endif /* CONFIG_HUGETLB_PAGE */               << 
282                                                << 
283 /*                                                191 /*
284  * Verify the pagetables are still not ok afte    192  * Verify the pagetables are still not ok after having reigstered into
285  * the fault_pending_wqh to avoid userland hav    193  * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
286  * userfault that has already been resolved, i !! 194  * userfault that has already been resolved, if userfaultfd_read and
287  * UFFDIO_COPY|ZEROPAGE are being run simultan    195  * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
288  * threads.                                       196  * threads.
289  */                                               197  */
290 static inline bool userfaultfd_must_wait(struc    198 static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
291                                          struc !! 199                                          unsigned long address,
                                                   >> 200                                          unsigned long flags,
292                                          unsig    201                                          unsigned long reason)
293 {                                                 202 {
294         struct mm_struct *mm = ctx->mm;           203         struct mm_struct *mm = ctx->mm;
295         unsigned long address = vmf->address;  << 
296         pgd_t *pgd;                               204         pgd_t *pgd;
297         p4d_t *p4d;                            << 
298         pud_t *pud;                               205         pud_t *pud;
299         pmd_t *pmd, _pmd;                         206         pmd_t *pmd, _pmd;
300         pte_t *pte;                               207         pte_t *pte;
301         pte_t ptent;                           << 
302         bool ret = true;                          208         bool ret = true;
303                                                   209 
304         assert_fault_locked(vmf);              !! 210         VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
305                                                   211 
306         pgd = pgd_offset(mm, address);            212         pgd = pgd_offset(mm, address);
307         if (!pgd_present(*pgd))                   213         if (!pgd_present(*pgd))
308                 goto out;                         214                 goto out;
309         p4d = p4d_offset(pgd, address);        !! 215         pud = pud_offset(pgd, address);
310         if (!p4d_present(*p4d))                << 
311                 goto out;                      << 
312         pud = pud_offset(p4d, address);        << 
313         if (!pud_present(*pud))                   216         if (!pud_present(*pud))
314                 goto out;                         217                 goto out;
315         pmd = pmd_offset(pud, address);           218         pmd = pmd_offset(pud, address);
316 again:                                         !! 219         /*
317         _pmd = pmdp_get_lockless(pmd);         !! 220          * READ_ONCE must function as a barrier with narrower scope
318         if (pmd_none(_pmd))                    !! 221          * and it must be equivalent to:
                                                   >> 222          *      _pmd = *pmd; barrier();
                                                   >> 223          *
                                                   >> 224          * This is to deal with the instability (as in
                                                   >> 225          * pmd_trans_unstable) of the pmd.
                                                   >> 226          */
                                                   >> 227         _pmd = READ_ONCE(*pmd);
                                                   >> 228         if (!pmd_present(_pmd))
319                 goto out;                         229                 goto out;
320                                                   230 
321         ret = false;                              231         ret = false;
322         if (!pmd_present(_pmd) || pmd_devmap(_ !! 232         if (pmd_trans_huge(_pmd))
323                 goto out;                         233                 goto out;
324                                                   234 
325         if (pmd_trans_huge(_pmd)) {            !! 235         /*
326                 if (!pmd_write(_pmd) && (reaso !! 236          * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
327                         ret = true;            !! 237          * and use the standard pte_offset_map() instead of parsing _pmd.
328                 goto out;                      !! 238          */
329         }                                      << 
330                                                << 
331         pte = pte_offset_map(pmd, address);       239         pte = pte_offset_map(pmd, address);
332         if (!pte) {                            << 
333                 ret = true;                    << 
334                 goto again;                    << 
335         }                                      << 
336         /*                                        240         /*
337          * Lockless access: we're in a wait_ev    241          * Lockless access: we're in a wait_event so it's ok if it
338          * changes under us.  PTE markers shou !! 242          * changes under us.
339          * ptes here.                          << 
340          */                                       243          */
341         ptent = ptep_get(pte);                 !! 244         if (pte_none(*pte))
342         if (pte_none_mostly(ptent))            << 
343                 ret = true;                    << 
344         if (!pte_write(ptent) && (reason & VM_ << 
345                 ret = true;                       245                 ret = true;
346         pte_unmap(pte);                           246         pte_unmap(pte);
347                                                   247 
348 out:                                              248 out:
349         return ret;                               249         return ret;
350 }                                                 250 }
351                                                   251 
352 static inline unsigned int userfaultfd_get_blo << 
353 {                                              << 
354         if (flags & FAULT_FLAG_INTERRUPTIBLE)  << 
355                 return TASK_INTERRUPTIBLE;     << 
356                                                << 
357         if (flags & FAULT_FLAG_KILLABLE)       << 
358                 return TASK_KILLABLE;          << 
359                                                << 
360         return TASK_UNINTERRUPTIBLE;           << 
361 }                                              << 
362                                                << 
363 /*                                                252 /*
364  * The locking rules involved in returning VM_    253  * The locking rules involved in returning VM_FAULT_RETRY depending on
365  * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NO    254  * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
366  * FAULT_FLAG_KILLABLE are not straightforward    255  * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
367  * recommendation in __lock_page_or_retry is n    256  * recommendation in __lock_page_or_retry is not an understatement.
368  *                                                257  *
369  * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_ !! 258  * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
370  * before returning VM_FAULT_RETRY only if FAU    259  * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
371  * not set.                                       260  * not set.
372  *                                                261  *
373  * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_    262  * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
374  * set, VM_FAULT_RETRY can still be returned i    263  * set, VM_FAULT_RETRY can still be returned if and only if there are
375  * fatal_signal_pending()s, and the mmap_lock  !! 264  * fatal_signal_pending()s, and the mmap_sem must be released before
376  * returning it.                                  265  * returning it.
377  */                                               266  */
378 vm_fault_t handle_userfault(struct vm_fault *v !! 267 int handle_userfault(struct fault_env *fe, unsigned long reason)
379 {                                                 268 {
380         struct vm_area_struct *vma = vmf->vma; !! 269         struct mm_struct *mm = fe->vma->vm_mm;
381         struct mm_struct *mm = vma->vm_mm;     << 
382         struct userfaultfd_ctx *ctx;              270         struct userfaultfd_ctx *ctx;
383         struct userfaultfd_wait_queue uwq;        271         struct userfaultfd_wait_queue uwq;
384         vm_fault_t ret = VM_FAULT_SIGBUS;      !! 272         int ret;
385         bool must_wait;                        !! 273         bool must_wait, return_to_userland;
386         unsigned int blocking_state;           !! 274         long blocking_state;
387                                                << 
388         /*                                     << 
389          * We don't do userfault handling for  << 
390          *                                     << 
391          * We also don't do userfault handling << 
392          * coredumping. hugetlbfs has the spec << 
393          * hugetlb_follow_page_mask() to skip  << 
394          * FOLL_DUMP case, anon memory also ch << 
395          * the no_page_table() helper in follo << 
396          * shmem_vm_ops->fault method is invok << 
397          * coredumping and it ends up here.    << 
398          */                                    << 
399         if (current->flags & (PF_EXITING|PF_DU << 
400                 goto out;                      << 
401                                                   275 
402         assert_fault_locked(vmf);              !! 276         BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
403                                                   277 
404         ctx = vma->vm_userfaultfd_ctx.ctx;     !! 278         ret = VM_FAULT_SIGBUS;
                                                   >> 279         ctx = fe->vma->vm_userfaultfd_ctx.ctx;
405         if (!ctx)                                 280         if (!ctx)
406                 goto out;                         281                 goto out;
407                                                   282 
408         BUG_ON(ctx->mm != mm);                    283         BUG_ON(ctx->mm != mm);
409                                                   284 
410         /* Any unrecognized flag is a bug. */  !! 285         VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
411         VM_BUG_ON(reason & ~__VM_UFFD_FLAGS);  !! 286         VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
412         /* 0 or > 1 flags set is a bug; we exp << 
413         VM_BUG_ON(!reason || (reason & (reason << 
414                                                << 
415         if (ctx->features & UFFD_FEATURE_SIGBU << 
416                 goto out;                      << 
417         if (!(vmf->flags & FAULT_FLAG_USER) && << 
418                 goto out;                      << 
419                                                   287 
420         /*                                        288         /*
421          * If it's already released don't get     289          * If it's already released don't get it. This avoids to loop
422          * in __get_user_pages if userfaultfd_    290          * in __get_user_pages if userfaultfd_release waits on the
423          * caller of handle_userfault to relea !! 291          * caller of handle_userfault to release the mmap_sem.
424          */                                       292          */
425         if (unlikely(READ_ONCE(ctx->released)) !! 293         if (unlikely(ACCESS_ONCE(ctx->released)))
426                 /*                             !! 294                 goto out;
427                  * Don't return VM_FAULT_SIGBU !! 295 
428                  * cooperative manager can clo !! 296         /*
429                  * last UFFDIO_COPY, without r !! 297          * We don't do userfault handling for the final child pid update.
430                  * involuntary SIGBUS if the p !! 298          */
431                  * userfaultfd while the userf !! 299         if (current->flags & PF_EXITING)
432                  * (but after the last UFFDIO_ << 
433                  * wasn't already closed when  << 
434                  * this point, that would norm << 
435                  * userfaultfd_must_wait retur << 
436                  *                             << 
437                  * If we were to return VM_FAU << 
438                  * cooperative manager would b << 
439                  * always call UFFDIO_UNREGIST << 
440                  * close the uffd.             << 
441                  */                            << 
442                 ret = VM_FAULT_NOPAGE;         << 
443                 goto out;                         300                 goto out;
444         }                                      << 
445                                                   301 
446         /*                                        302         /*
447          * Check that we can return VM_FAULT_R    303          * Check that we can return VM_FAULT_RETRY.
448          *                                        304          *
449          * NOTE: it should become possible to     305          * NOTE: it should become possible to return VM_FAULT_RETRY
450          * even if FAULT_FLAG_TRIED is set wit    306          * even if FAULT_FLAG_TRIED is set without leading to gup()
451          * -EBUSY failures, if the userfaultfd    307          * -EBUSY failures, if the userfaultfd is to be extended for
452          * VM_UFFD_WP tracking and we intend t    308          * VM_UFFD_WP tracking and we intend to arm the userfault
453          * without first stopping userland acc    309          * without first stopping userland access to the memory. For
454          * VM_UFFD_MISSING userfaults this is     310          * VM_UFFD_MISSING userfaults this is enough for now.
455          */                                       311          */
456         if (unlikely(!(vmf->flags & FAULT_FLAG !! 312         if (unlikely(!(fe->flags & FAULT_FLAG_ALLOW_RETRY))) {
457                 /*                                313                 /*
458                  * Validate the invariant that    314                  * Validate the invariant that nowait must allow retry
459                  * to be sure not to return SI    315                  * to be sure not to return SIGBUS erroneously on
460                  * nowait invocations.            316                  * nowait invocations.
461                  */                               317                  */
462                 BUG_ON(vmf->flags & FAULT_FLAG !! 318                 BUG_ON(fe->flags & FAULT_FLAG_RETRY_NOWAIT);
463 #ifdef CONFIG_DEBUG_VM                            319 #ifdef CONFIG_DEBUG_VM
464                 if (printk_ratelimit()) {         320                 if (printk_ratelimit()) {
465                         printk(KERN_WARNING       321                         printk(KERN_WARNING
466                                "FAULT_FLAG_ALL !! 322                                "FAULT_FLAG_ALLOW_RETRY missing %x\n", fe->flags);
467                                vmf->flags);    << 
468                         dump_stack();             323                         dump_stack();
469                 }                                 324                 }
470 #endif                                            325 #endif
471                 goto out;                         326                 goto out;
472         }                                         327         }
473                                                   328 
474         /*                                        329         /*
475          * Handle nowait, not much to do other    330          * Handle nowait, not much to do other than tell it to retry
476          * and wait.                              331          * and wait.
477          */                                       332          */
478         ret = VM_FAULT_RETRY;                     333         ret = VM_FAULT_RETRY;
479         if (vmf->flags & FAULT_FLAG_RETRY_NOWA !! 334         if (fe->flags & FAULT_FLAG_RETRY_NOWAIT)
480                 goto out;                         335                 goto out;
481                                                   336 
482         /* take the reference before dropping  !! 337         /* take the reference before dropping the mmap_sem */
483         userfaultfd_ctx_get(ctx);                 338         userfaultfd_ctx_get(ctx);
484                                                   339 
485         init_waitqueue_func_entry(&uwq.wq, use    340         init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
486         uwq.wq.private = current;                 341         uwq.wq.private = current;
487         uwq.msg = userfault_msg(vmf->address,  !! 342         uwq.msg = userfault_msg(fe->address, fe->flags, reason);
488                                 reason, ctx->f << 
489         uwq.ctx = ctx;                            343         uwq.ctx = ctx;
490         uwq.waken = false;                        344         uwq.waken = false;
491                                                   345 
492         blocking_state = userfaultfd_get_block !! 346         return_to_userland =
493                                                !! 347                 (fe->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
494         /*                                     !! 348                 (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
495          * Take the vma lock now, in order to  !! 349         blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
496          * userfaultfd_huge_must_wait() later. !! 350                          TASK_KILLABLE;
497          * (sleepable) vma lock can modify the << 
498          * must be before explicitly calling s << 
499          */                                    << 
500         if (is_vm_hugetlb_page(vma))           << 
501                 hugetlb_vma_lock_read(vma);    << 
502                                                   351 
503         spin_lock_irq(&ctx->fault_pending_wqh. !! 352         spin_lock(&ctx->fault_pending_wqh.lock);
504         /*                                        353         /*
505          * After the __add_wait_queue the uwq     354          * After the __add_wait_queue the uwq is visible to userland
506          * through poll/read().                   355          * through poll/read().
507          */                                       356          */
508         __add_wait_queue(&ctx->fault_pending_w    357         __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
509         /*                                        358         /*
510          * The smp_mb() after __set_current_st    359          * The smp_mb() after __set_current_state prevents the reads
511          * following the spin_unlock to happen    360          * following the spin_unlock to happen before the list_add in
512          * __add_wait_queue.                      361          * __add_wait_queue.
513          */                                       362          */
514         set_current_state(blocking_state);        363         set_current_state(blocking_state);
515         spin_unlock_irq(&ctx->fault_pending_wq !! 364         spin_unlock(&ctx->fault_pending_wqh.lock);
516                                                   365 
517         if (!is_vm_hugetlb_page(vma))          !! 366         must_wait = userfaultfd_must_wait(ctx, fe->address, fe->flags, reason);
518                 must_wait = userfaultfd_must_w !! 367         up_read(&mm->mmap_sem);
519         else                                   << 
520                 must_wait = userfaultfd_huge_m << 
521         if (is_vm_hugetlb_page(vma))           << 
522                 hugetlb_vma_unlock_read(vma);  << 
523         release_fault_lock(vmf);               << 
524                                                   368 
525         if (likely(must_wait && !READ_ONCE(ctx !! 369         if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
526                 wake_up_poll(&ctx->fd_wqh, EPO !! 370                    (return_to_userland ? !signal_pending(current) :
                                                   >> 371                     !fatal_signal_pending(current)))) {
                                                   >> 372                 wake_up_poll(&ctx->fd_wqh, POLLIN);
527                 schedule();                       373                 schedule();
                                                   >> 374                 ret |= VM_FAULT_MAJOR;
                                                   >> 375 
                                                   >> 376                 /*
                                                   >> 377                  * False wakeups can orginate even from rwsem before
                                                   >> 378                  * up_read() however userfaults will wait either for a
                                                   >> 379                  * targeted wakeup on the specific uwq waitqueue from
                                                   >> 380                  * wake_userfault() or for signals or for uffd
                                                   >> 381                  * release.
                                                   >> 382                  */
                                                   >> 383                 while (!READ_ONCE(uwq.waken)) {
                                                   >> 384                         /*
                                                   >> 385                          * This needs the full smp_store_mb()
                                                   >> 386                          * guarantee as the state write must be
                                                   >> 387                          * visible to other CPUs before reading
                                                   >> 388                          * uwq.waken from other CPUs.
                                                   >> 389                          */
                                                   >> 390                         set_current_state(blocking_state);
                                                   >> 391                         if (READ_ONCE(uwq.waken) ||
                                                   >> 392                             READ_ONCE(ctx->released) ||
                                                   >> 393                             (return_to_userland ? signal_pending(current) :
                                                   >> 394                              fatal_signal_pending(current)))
                                                   >> 395                                 break;
                                                   >> 396                         schedule();
                                                   >> 397                 }
528         }                                         398         }
529                                                   399 
530         __set_current_state(TASK_RUNNING);        400         __set_current_state(TASK_RUNNING);
531                                                   401 
                                                   >> 402         if (return_to_userland) {
                                                   >> 403                 if (signal_pending(current) &&
                                                   >> 404                     !fatal_signal_pending(current)) {
                                                   >> 405                         /*
                                                   >> 406                          * If we got a SIGSTOP or SIGCONT and this is
                                                   >> 407                          * a normal userland page fault, just let
                                                   >> 408                          * userland return so the signal will be
                                                   >> 409                          * handled and gdb debugging works.  The page
                                                   >> 410                          * fault code immediately after we return from
                                                   >> 411                          * this function is going to release the
                                                   >> 412                          * mmap_sem and it's not depending on it
                                                   >> 413                          * (unlike gup would if we were not to return
                                                   >> 414                          * VM_FAULT_RETRY).
                                                   >> 415                          *
                                                   >> 416                          * If a fatal signal is pending we still take
                                                   >> 417                          * the streamlined VM_FAULT_RETRY failure path
                                                   >> 418                          * and there's no need to retake the mmap_sem
                                                   >> 419                          * in such case.
                                                   >> 420                          */
                                                   >> 421                         down_read(&mm->mmap_sem);
                                                   >> 422                         ret = VM_FAULT_NOPAGE;
                                                   >> 423                 }
                                                   >> 424         }
                                                   >> 425 
532         /*                                        426         /*
533          * Here we race with the list_del; lis    427          * Here we race with the list_del; list_add in
534          * userfaultfd_ctx_read(), however bec    428          * userfaultfd_ctx_read(), however because we don't ever run
535          * list_del_init() to refile across th    429          * list_del_init() to refile across the two lists, the prev
536          * and next pointers will never point     430          * and next pointers will never point to self. list_add also
537          * would never let any of the two poin    431          * would never let any of the two pointers to point to
538          * self. So list_empty_careful won't r    432          * self. So list_empty_careful won't risk to see both pointers
539          * pointing to self at any time during    433          * pointing to self at any time during the list refile. The
540          * only case where list_del_init() is     434          * only case where list_del_init() is called is the full
541          * removal in the wake function and th    435          * removal in the wake function and there we don't re-list_add
542          * and it's fine not to block on the s    436          * and it's fine not to block on the spinlock. The uwq on this
543          * kernel stack can be released after     437          * kernel stack can be released after the list_del_init.
544          */                                       438          */
545         if (!list_empty_careful(&uwq.wq.entry) !! 439         if (!list_empty_careful(&uwq.wq.task_list)) {
546                 spin_lock_irq(&ctx->fault_pend !! 440                 spin_lock(&ctx->fault_pending_wqh.lock);
547                 /*                                441                 /*
548                  * No need of list_del_init(),    442                  * No need of list_del_init(), the uwq on the stack
549                  * will be freed shortly anywa    443                  * will be freed shortly anyway.
550                  */                               444                  */
551                 list_del(&uwq.wq.entry);       !! 445                 list_del(&uwq.wq.task_list);
552                 spin_unlock_irq(&ctx->fault_pe !! 446                 spin_unlock(&ctx->fault_pending_wqh.lock);
553         }                                         447         }
554                                                   448 
555         /*                                        449         /*
556          * ctx may go away after this if the u    450          * ctx may go away after this if the userfault pseudo fd is
557          * already released.                      451          * already released.
558          */                                       452          */
559         userfaultfd_ctx_put(ctx);                 453         userfaultfd_ctx_put(ctx);
560                                                   454 
561 out:                                              455 out:
562         return ret;                               456         return ret;
563 }                                                 457 }
564                                                   458 
565 static void userfaultfd_event_wait_completion( << 
566                                                << 
567 {                                              << 
568         struct userfaultfd_ctx *release_new_ct << 
569                                                << 
570         if (WARN_ON_ONCE(current->flags & PF_E << 
571                 goto out;                      << 
572                                                << 
573         ewq->ctx = ctx;                        << 
574         init_waitqueue_entry(&ewq->wq, current << 
575         release_new_ctx = NULL;                << 
576                                                << 
577         spin_lock_irq(&ctx->event_wqh.lock);   << 
578         /*                                     << 
579          * After the __add_wait_queue the uwq  << 
580          * through poll/read().                << 
581          */                                    << 
582         __add_wait_queue(&ctx->event_wqh, &ewq << 
583         for (;;) {                             << 
584                 set_current_state(TASK_KILLABL << 
585                 if (ewq->msg.event == 0)       << 
586                         break;                 << 
587                 if (READ_ONCE(ctx->released) | << 
588                     fatal_signal_pending(curre << 
589                         /*                     << 
590                          * &ewq->wq may be que << 
591                          * __remove_wait_queue << 
592                          * parameter. It would << 
593                          * didn't.             << 
594                          */                    << 
595                         __remove_wait_queue(&c << 
596                         if (ewq->msg.event ==  << 
597                                 struct userfau << 
598                                                << 
599                                 new = (struct  << 
600                                         (unsig << 
601                                         ewq->m << 
602                                 release_new_ct << 
603                         }                      << 
604                         break;                 << 
605                 }                              << 
606                                                << 
607                 spin_unlock_irq(&ctx->event_wq << 
608                                                << 
609                 wake_up_poll(&ctx->fd_wqh, EPO << 
610                 schedule();                    << 
611                                                << 
612                 spin_lock_irq(&ctx->event_wqh. << 
613         }                                      << 
614         __set_current_state(TASK_RUNNING);     << 
615         spin_unlock_irq(&ctx->event_wqh.lock); << 
616                                                << 
617         if (release_new_ctx) {                 << 
618                 struct vm_area_struct *vma;    << 
619                 struct mm_struct *mm = release << 
620                 VMA_ITERATOR(vmi, mm, 0);      << 
621                                                << 
622                 /* the various vma->vm_userfau << 
623                 mmap_write_lock(mm);           << 
624                 for_each_vma(vmi, vma) {       << 
625                         if (vma->vm_userfaultf << 
626                                 vma_start_writ << 
627                                 vma->vm_userfa << 
628                                 userfaultfd_se << 
629                                                << 
630                         }                      << 
631                 }                              << 
632                 mmap_write_unlock(mm);         << 
633                                                << 
634                 userfaultfd_ctx_put(release_ne << 
635         }                                      << 
636                                                << 
637         /*                                     << 
638          * ctx may go away after this if the u << 
639          * already released.                   << 
640          */                                    << 
641 out:                                           << 
642         atomic_dec(&ctx->mmap_changing);       << 
643         VM_BUG_ON(atomic_read(&ctx->mmap_chang << 
644         userfaultfd_ctx_put(ctx);              << 
645 }                                              << 
646                                                << 
647 static void userfaultfd_event_complete(struct  << 
648                                        struct  << 
649 {                                              << 
650         ewq->msg.event = 0;                    << 
651         wake_up_locked(&ctx->event_wqh);       << 
652         __remove_wait_queue(&ctx->event_wqh, & << 
653 }                                              << 
654                                                << 
655 int dup_userfaultfd(struct vm_area_struct *vma << 
656 {                                              << 
657         struct userfaultfd_ctx *ctx = NULL, *o << 
658         struct userfaultfd_fork_ctx *fctx;     << 
659                                                << 
660         octx = vma->vm_userfaultfd_ctx.ctx;    << 
661         if (!octx)                             << 
662                 return 0;                      << 
663                                                << 
664         if (!(octx->features & UFFD_FEATURE_EV << 
665                 vma_start_write(vma);          << 
666                 vma->vm_userfaultfd_ctx = NULL << 
667                 userfaultfd_set_vm_flags(vma,  << 
668                 return 0;                      << 
669         }                                      << 
670                                                << 
671         list_for_each_entry(fctx, fcs, list)   << 
672                 if (fctx->orig == octx) {      << 
673                         ctx = fctx->new;       << 
674                         break;                 << 
675                 }                              << 
676                                                << 
677         if (!ctx) {                            << 
678                 fctx = kmalloc(sizeof(*fctx),  << 
679                 if (!fctx)                     << 
680                         return -ENOMEM;        << 
681                                                << 
682                 ctx = kmem_cache_alloc(userfau << 
683                 if (!ctx) {                    << 
684                         kfree(fctx);           << 
685                         return -ENOMEM;        << 
686                 }                              << 
687                                                << 
688                 refcount_set(&ctx->refcount, 1 << 
689                 ctx->flags = octx->flags;      << 
690                 ctx->features = octx->features << 
691                 ctx->released = false;         << 
692                 init_rwsem(&ctx->map_changing_ << 
693                 atomic_set(&ctx->mmap_changing << 
694                 ctx->mm = vma->vm_mm;          << 
695                 mmgrab(ctx->mm);               << 
696                                                << 
697                 userfaultfd_ctx_get(octx);     << 
698                 down_write(&octx->map_changing << 
699                 atomic_inc(&octx->mmap_changin << 
700                 up_write(&octx->map_changing_l << 
701                 fctx->orig = octx;             << 
702                 fctx->new = ctx;               << 
703                 list_add_tail(&fctx->list, fcs << 
704         }                                      << 
705                                                << 
706         vma->vm_userfaultfd_ctx.ctx = ctx;     << 
707         return 0;                              << 
708 }                                              << 
709                                                << 
710 static void dup_fctx(struct userfaultfd_fork_c << 
711 {                                              << 
712         struct userfaultfd_ctx *ctx = fctx->or << 
713         struct userfaultfd_wait_queue ewq;     << 
714                                                << 
715         msg_init(&ewq.msg);                    << 
716                                                << 
717         ewq.msg.event = UFFD_EVENT_FORK;       << 
718         ewq.msg.arg.reserved.reserved1 = (unsi << 
719                                                << 
720         userfaultfd_event_wait_completion(ctx, << 
721 }                                              << 
722                                                << 
723 void dup_userfaultfd_complete(struct list_head << 
724 {                                              << 
725         struct userfaultfd_fork_ctx *fctx, *n; << 
726                                                << 
727         list_for_each_entry_safe(fctx, n, fcs, << 
728                 dup_fctx(fctx);                << 
729                 list_del(&fctx->list);         << 
730                 kfree(fctx);                   << 
731         }                                      << 
732 }                                              << 
733                                                << 
734 void mremap_userfaultfd_prep(struct vm_area_st << 
735                              struct vm_userfau << 
736 {                                              << 
737         struct userfaultfd_ctx *ctx;           << 
738                                                << 
739         ctx = vma->vm_userfaultfd_ctx.ctx;     << 
740                                                << 
741         if (!ctx)                              << 
742                 return;                        << 
743                                                << 
744         if (ctx->features & UFFD_FEATURE_EVENT << 
745                 vm_ctx->ctx = ctx;             << 
746                 userfaultfd_ctx_get(ctx);      << 
747                 down_write(&ctx->map_changing_ << 
748                 atomic_inc(&ctx->mmap_changing << 
749                 up_write(&ctx->map_changing_lo << 
750         } else {                               << 
751                 /* Drop uffd context if remap  << 
752                 vma_start_write(vma);          << 
753                 vma->vm_userfaultfd_ctx = NULL << 
754                 userfaultfd_set_vm_flags(vma,  << 
755         }                                      << 
756 }                                              << 
757                                                << 
758 void mremap_userfaultfd_complete(struct vm_use << 
759                                  unsigned long << 
760                                  unsigned long << 
761 {                                              << 
762         struct userfaultfd_ctx *ctx = vm_ctx-> << 
763         struct userfaultfd_wait_queue ewq;     << 
764                                                << 
765         if (!ctx)                              << 
766                 return;                        << 
767                                                << 
768         if (to & ~PAGE_MASK) {                 << 
769                 userfaultfd_ctx_put(ctx);      << 
770                 return;                        << 
771         }                                      << 
772                                                << 
773         msg_init(&ewq.msg);                    << 
774                                                << 
775         ewq.msg.event = UFFD_EVENT_REMAP;      << 
776         ewq.msg.arg.remap.from = from;         << 
777         ewq.msg.arg.remap.to = to;             << 
778         ewq.msg.arg.remap.len = len;           << 
779                                                << 
780         userfaultfd_event_wait_completion(ctx, << 
781 }                                              << 
782                                                << 
783 bool userfaultfd_remove(struct vm_area_struct  << 
784                         unsigned long start, u << 
785 {                                              << 
786         struct mm_struct *mm = vma->vm_mm;     << 
787         struct userfaultfd_ctx *ctx;           << 
788         struct userfaultfd_wait_queue ewq;     << 
789                                                << 
790         ctx = vma->vm_userfaultfd_ctx.ctx;     << 
791         if (!ctx || !(ctx->features & UFFD_FEA << 
792                 return true;                   << 
793                                                << 
794         userfaultfd_ctx_get(ctx);              << 
795         down_write(&ctx->map_changing_lock);   << 
796         atomic_inc(&ctx->mmap_changing);       << 
797         up_write(&ctx->map_changing_lock);     << 
798         mmap_read_unlock(mm);                  << 
799                                                << 
800         msg_init(&ewq.msg);                    << 
801                                                << 
802         ewq.msg.event = UFFD_EVENT_REMOVE;     << 
803         ewq.msg.arg.remove.start = start;      << 
804         ewq.msg.arg.remove.end = end;          << 
805                                                << 
806         userfaultfd_event_wait_completion(ctx, << 
807                                                << 
808         return false;                          << 
809 }                                              << 
810                                                << 
811 static bool has_unmap_ctx(struct userfaultfd_c << 
812                           unsigned long start, << 
813 {                                              << 
814         struct userfaultfd_unmap_ctx *unmap_ct << 
815                                                << 
816         list_for_each_entry(unmap_ctx, unmaps, << 
817                 if (unmap_ctx->ctx == ctx && u << 
818                     unmap_ctx->end == end)     << 
819                         return true;           << 
820                                                << 
821         return false;                          << 
822 }                                              << 
823                                                << 
824 int userfaultfd_unmap_prep(struct vm_area_stru << 
825                            unsigned long end,  << 
826 {                                              << 
827         struct userfaultfd_unmap_ctx *unmap_ct << 
828         struct userfaultfd_ctx *ctx = vma->vm_ << 
829                                                << 
830         if (!ctx || !(ctx->features & UFFD_FEA << 
831             has_unmap_ctx(ctx, unmaps, start,  << 
832                 return 0;                      << 
833                                                << 
834         unmap_ctx = kzalloc(sizeof(*unmap_ctx) << 
835         if (!unmap_ctx)                        << 
836                 return -ENOMEM;                << 
837                                                << 
838         userfaultfd_ctx_get(ctx);              << 
839         down_write(&ctx->map_changing_lock);   << 
840         atomic_inc(&ctx->mmap_changing);       << 
841         up_write(&ctx->map_changing_lock);     << 
842         unmap_ctx->ctx = ctx;                  << 
843         unmap_ctx->start = start;              << 
844         unmap_ctx->end = end;                  << 
845         list_add_tail(&unmap_ctx->list, unmaps << 
846                                                << 
847         return 0;                              << 
848 }                                              << 
849                                                << 
850 void userfaultfd_unmap_complete(struct mm_stru << 
851 {                                              << 
852         struct userfaultfd_unmap_ctx *ctx, *n; << 
853         struct userfaultfd_wait_queue ewq;     << 
854                                                << 
855         list_for_each_entry_safe(ctx, n, uf, l << 
856                 msg_init(&ewq.msg);            << 
857                                                << 
858                 ewq.msg.event = UFFD_EVENT_UNM << 
859                 ewq.msg.arg.remove.start = ctx << 
860                 ewq.msg.arg.remove.end = ctx-> << 
861                                                << 
862                 userfaultfd_event_wait_complet << 
863                                                << 
864                 list_del(&ctx->list);          << 
865                 kfree(ctx);                    << 
866         }                                      << 
867 }                                              << 
868                                                << 
869 static int userfaultfd_release(struct inode *i    459 static int userfaultfd_release(struct inode *inode, struct file *file)
870 {                                                 460 {
871         struct userfaultfd_ctx *ctx = file->pr    461         struct userfaultfd_ctx *ctx = file->private_data;
872         struct mm_struct *mm = ctx->mm;           462         struct mm_struct *mm = ctx->mm;
873         struct vm_area_struct *vma, *prev;        463         struct vm_area_struct *vma, *prev;
874         /* len == 0 means wake all */             464         /* len == 0 means wake all */
875         struct userfaultfd_wake_range range =     465         struct userfaultfd_wake_range range = { .len = 0, };
876         unsigned long new_flags;                  466         unsigned long new_flags;
877         VMA_ITERATOR(vmi, mm, 0);              !! 467         bool still_valid;
878                                                   468 
879         WRITE_ONCE(ctx->released, true);       !! 469         ACCESS_ONCE(ctx->released) = true;
880                                                   470 
881         if (!mmget_not_zero(mm))                  471         if (!mmget_not_zero(mm))
882                 goto wakeup;                      472                 goto wakeup;
883                                                   473 
884         /*                                        474         /*
885          * Flush page faults out of all CPUs.     475          * Flush page faults out of all CPUs. NOTE: all page faults
886          * must be retried without returning V    476          * must be retried without returning VM_FAULT_SIGBUS if
887          * userfaultfd_ctx_get() succeeds but     477          * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
888          * changes while handle_userfault rele !! 478          * changes while handle_userfault released the mmap_sem. So
889          * it's critical that released is set     479          * it's critical that released is set to true (above), before
890          * taking the mmap_lock for writing.   !! 480          * taking the mmap_sem for writing.
891          */                                       481          */
892         mmap_write_lock(mm);                   !! 482         down_write(&mm->mmap_sem);
                                                   >> 483         still_valid = mmget_still_valid(mm);
893         prev = NULL;                              484         prev = NULL;
894         for_each_vma(vmi, vma) {               !! 485         for (vma = mm->mmap; vma; vma = vma->vm_next) {
895                 cond_resched();                   486                 cond_resched();
896                 BUG_ON(!!vma->vm_userfaultfd_c    487                 BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
897                        !!(vma->vm_flags & __VM !! 488                        !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
898                 if (vma->vm_userfaultfd_ctx.ct    489                 if (vma->vm_userfaultfd_ctx.ctx != ctx) {
899                         prev = vma;               490                         prev = vma;
900                         continue;                 491                         continue;
901                 }                                 492                 }
902                 /* Reset ptes for the whole vm !! 493                 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
903                 if (userfaultfd_wp(vma))       !! 494                 if (still_valid) {
904                         uffd_wp_range(vma, vma !! 495                         prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
905                                       vma->vm_ !! 496                                          new_flags, vma->anon_vma,
906                 new_flags = vma->vm_flags & ~_ !! 497                                          vma->vm_file, vma->vm_pgoff,
907                 vma = vma_modify_flags_uffd(&v !! 498                                          vma_policy(vma),
908                                             vm !! 499                                          NULL_VM_UFFD_CTX);
909                                             NU !! 500                         if (prev)
910                                                !! 501                                 vma = prev;
911                 vma_start_write(vma);          !! 502                         else
912                 userfaultfd_set_vm_flags(vma,  !! 503                                 prev = vma;
                                                   >> 504                 }
                                                   >> 505                 vma->vm_flags = new_flags;
913                 vma->vm_userfaultfd_ctx = NULL    506                 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
914                                                << 
915                 prev = vma;                    << 
916         }                                         507         }
917         mmap_write_unlock(mm);                 !! 508         up_write(&mm->mmap_sem);
918         mmput(mm);                                509         mmput(mm);
919 wakeup:                                           510 wakeup:
920         /*                                        511         /*
921          * After no new page faults can wait o    512          * After no new page faults can wait on this fault_*wqh, flush
922          * the last page faults that may have     513          * the last page faults that may have been already waiting on
923          * the fault_*wqh.                        514          * the fault_*wqh.
924          */                                       515          */
925         spin_lock_irq(&ctx->fault_pending_wqh. !! 516         spin_lock(&ctx->fault_pending_wqh.lock);
926         __wake_up_locked_key(&ctx->fault_pendi    517         __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
927         __wake_up(&ctx->fault_wqh, TASK_NORMAL !! 518         __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
928         spin_unlock_irq(&ctx->fault_pending_wq !! 519         spin_unlock(&ctx->fault_pending_wqh.lock);
929                                                   520 
930         /* Flush pending events that may still !! 521         wake_up_poll(&ctx->fd_wqh, POLLHUP);
931         wake_up_all(&ctx->event_wqh);          << 
932                                                << 
933         wake_up_poll(&ctx->fd_wqh, EPOLLHUP);  << 
934         userfaultfd_ctx_put(ctx);                 522         userfaultfd_ctx_put(ctx);
935         return 0;                                 523         return 0;
936 }                                                 524 }
937                                                   525 
938 /* fault_pending_wqh.lock must be hold by the     526 /* fault_pending_wqh.lock must be hold by the caller */
939 static inline struct userfaultfd_wait_queue *f !! 527 static inline struct userfaultfd_wait_queue *find_userfault(
940                 wait_queue_head_t *wqh)        !! 528         struct userfaultfd_ctx *ctx)
941 {                                                 529 {
942         wait_queue_entry_t *wq;                !! 530         wait_queue_t *wq;
943         struct userfaultfd_wait_queue *uwq;       531         struct userfaultfd_wait_queue *uwq;
944                                                   532 
945         lockdep_assert_held(&wqh->lock);       !! 533         VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
946                                                   534 
947         uwq = NULL;                               535         uwq = NULL;
948         if (!waitqueue_active(wqh))            !! 536         if (!waitqueue_active(&ctx->fault_pending_wqh))
949                 goto out;                         537                 goto out;
950         /* walk in reverse to provide FIFO beh    538         /* walk in reverse to provide FIFO behavior to read userfaults */
951         wq = list_last_entry(&wqh->head, typeo !! 539         wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
                                                   >> 540                              typeof(*wq), task_list);
952         uwq = container_of(wq, struct userfaul    541         uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
953 out:                                              542 out:
954         return uwq;                               543         return uwq;
955 }                                                 544 }
956                                                   545 
957 static inline struct userfaultfd_wait_queue *f !! 546 static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
958                 struct userfaultfd_ctx *ctx)   << 
959 {                                              << 
960         return find_userfault_in(&ctx->fault_p << 
961 }                                              << 
962                                                << 
963 static inline struct userfaultfd_wait_queue *f << 
964                 struct userfaultfd_ctx *ctx)   << 
965 {                                              << 
966         return find_userfault_in(&ctx->event_w << 
967 }                                              << 
968                                                << 
969 static __poll_t userfaultfd_poll(struct file * << 
970 {                                                 547 {
971         struct userfaultfd_ctx *ctx = file->pr    548         struct userfaultfd_ctx *ctx = file->private_data;
972         __poll_t ret;                          !! 549         unsigned int ret;
973                                                   550 
974         poll_wait(file, &ctx->fd_wqh, wait);      551         poll_wait(file, &ctx->fd_wqh, wait);
975                                                   552 
976         if (!userfaultfd_is_initialized(ctx))  !! 553         switch (ctx->state) {
977                 return EPOLLERR;               !! 554         case UFFD_STATE_WAIT_API:
978                                                !! 555                 return POLLERR;
979         /*                                     !! 556         case UFFD_STATE_RUNNING:
980          * poll() never guarantees that read w !! 557                 /*
981          * userfaults can be waken before they !! 558                  * poll() never guarantees that read won't block.
982          */                                    !! 559                  * userfaults can be waken before they're read().
983         if (unlikely(!(file->f_flags & O_NONBL !! 560                  */
984                 return EPOLLERR;               !! 561                 if (unlikely(!(file->f_flags & O_NONBLOCK)))
985         /*                                     !! 562                         return POLLERR;
986          * lockless access to see if there are !! 563                 /*
987          * __pollwait last action is the add_w !! 564                  * lockless access to see if there are pending faults
988          * the spin_unlock would allow the wai !! 565                  * __pollwait last action is the add_wait_queue but
989          * pass above the actual list_add insi !! 566                  * the spin_unlock would allow the waitqueue_active to
990          * add_wait_queue critical section. So !! 567                  * pass above the actual list_add inside
991          * memory barrier to serialize the lis !! 568                  * add_wait_queue critical section. So use a full
992          * add_wait_queue() with the waitqueue !! 569                  * memory barrier to serialize the list_add write of
993          * below.                              !! 570                  * add_wait_queue() with the waitqueue_active read
994          */                                    !! 571                  * below.
995         ret = 0;                               !! 572                  */
996         smp_mb();                              !! 573                 ret = 0;
997         if (waitqueue_active(&ctx->fault_pendi !! 574                 smp_mb();
998                 ret = EPOLLIN;                 !! 575                 if (waitqueue_active(&ctx->fault_pending_wqh))
999         else if (waitqueue_active(&ctx->event_ !! 576                         ret = POLLIN;
1000                 ret = EPOLLIN;                !! 577                 return ret;
1001                                               !! 578         default:
1002         return ret;                           !! 579                 BUG();
1003 }                                             !! 580         }
1004                                               << 
1005 static const struct file_operations userfault << 
1006                                               << 
1007 static int resolve_userfault_fork(struct user << 
1008                                   struct inod << 
1009                                   struct uffd << 
1010 {                                             << 
1011         int fd;                               << 
1012                                               << 
1013         fd = anon_inode_create_getfd("[userfa << 
1014                         O_RDONLY | (new->flag << 
1015         if (fd < 0)                           << 
1016                 return fd;                    << 
1017                                               << 
1018         msg->arg.reserved.reserved1 = 0;      << 
1019         msg->arg.fork.ufd = fd;               << 
1020         return 0;                             << 
1021 }                                                581 }
1022                                                  582 
1023 static ssize_t userfaultfd_ctx_read(struct us    583 static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
1024                                     struct uf !! 584                                     struct uffd_msg *msg)
1025 {                                                585 {
1026         ssize_t ret;                             586         ssize_t ret;
1027         DECLARE_WAITQUEUE(wait, current);        587         DECLARE_WAITQUEUE(wait, current);
1028         struct userfaultfd_wait_queue *uwq;      588         struct userfaultfd_wait_queue *uwq;
1029         /*                                    << 
1030          * Handling fork event requires sleep << 
1031          * we drop the event_wqh lock, then d << 
1032          * lock it back and wake up the waite << 
1033          * dropped the ewq may go away so we  << 
1034          * carefully.                         << 
1035          */                                   << 
1036         LIST_HEAD(fork_event);                << 
1037         struct userfaultfd_ctx *fork_nctx = N << 
1038                                                  589 
1039         /* always take the fd_wqh lock before    590         /* always take the fd_wqh lock before the fault_pending_wqh lock */
1040         spin_lock_irq(&ctx->fd_wqh.lock);     !! 591         spin_lock(&ctx->fd_wqh.lock);
1041         __add_wait_queue(&ctx->fd_wqh, &wait)    592         __add_wait_queue(&ctx->fd_wqh, &wait);
1042         for (;;) {                               593         for (;;) {
1043                 set_current_state(TASK_INTERR    594                 set_current_state(TASK_INTERRUPTIBLE);
1044                 spin_lock(&ctx->fault_pending    595                 spin_lock(&ctx->fault_pending_wqh.lock);
1045                 uwq = find_userfault(ctx);       596                 uwq = find_userfault(ctx);
1046                 if (uwq) {                       597                 if (uwq) {
1047                         /*                       598                         /*
1048                          * Use a seqcount to     599                          * Use a seqcount to repeat the lockless check
1049                          * in wake_userfault(    600                          * in wake_userfault() to avoid missing
1050                          * wakeups because du    601                          * wakeups because during the refile both
1051                          * waitqueue could be    602                          * waitqueue could become empty if this is the
1052                          * only userfault.       603                          * only userfault.
1053                          */                      604                          */
1054                         write_seqcount_begin(    605                         write_seqcount_begin(&ctx->refile_seq);
1055                                                  606 
1056                         /*                       607                         /*
1057                          * The fault_pending_    608                          * The fault_pending_wqh.lock prevents the uwq
1058                          * to disappear from     609                          * to disappear from under us.
1059                          *                       610                          *
1060                          * Refile this userfa    611                          * Refile this userfault from
1061                          * fault_pending_wqh     612                          * fault_pending_wqh to fault_wqh, it's not
1062                          * pending anymore af    613                          * pending anymore after we read it.
1063                          *                       614                          *
1064                          * Use list_del() by     615                          * Use list_del() by hand (as
1065                          * userfaultfd_wake_f    616                          * userfaultfd_wake_function also uses
1066                          * list_del_init() by    617                          * list_del_init() by hand) to be sure nobody
1067                          * changes __remove_w    618                          * changes __remove_wait_queue() to use
1068                          * list_del_init() in    619                          * list_del_init() in turn breaking the
1069                          * !list_empty_carefu    620                          * !list_empty_careful() check in
1070                          * handle_userfault() !! 621                          * handle_userfault(). The uwq->wq.task_list
1071                          * must never be empt    622                          * must never be empty at any time during the
1072                          * refile, or the wai    623                          * refile, or the waitqueue could disappear
1073                          * from under us. The    624                          * from under us. The "wait_queue_head_t"
1074                          * parameter of __rem    625                          * parameter of __remove_wait_queue() is unused
1075                          * anyway.               626                          * anyway.
1076                          */                      627                          */
1077                         list_del(&uwq->wq.ent !! 628                         list_del(&uwq->wq.task_list);
1078                         add_wait_queue(&ctx-> !! 629                         __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
1079                                                  630 
1080                         write_seqcount_end(&c    631                         write_seqcount_end(&ctx->refile_seq);
1081                                                  632 
1082                         /* careful to always     633                         /* careful to always initialize msg if ret == 0 */
1083                         *msg = uwq->msg;         634                         *msg = uwq->msg;
1084                         spin_unlock(&ctx->fau    635                         spin_unlock(&ctx->fault_pending_wqh.lock);
1085                         ret = 0;                 636                         ret = 0;
1086                         break;                   637                         break;
1087                 }                                638                 }
1088                 spin_unlock(&ctx->fault_pendi    639                 spin_unlock(&ctx->fault_pending_wqh.lock);
1089                                               << 
1090                 spin_lock(&ctx->event_wqh.loc << 
1091                 uwq = find_userfault_evt(ctx) << 
1092                 if (uwq) {                    << 
1093                         *msg = uwq->msg;      << 
1094                                               << 
1095                         if (uwq->msg.event == << 
1096                                 fork_nctx = ( << 
1097                                         (unsi << 
1098                                         uwq-> << 
1099                                 list_move(&uw << 
1100                                 /*            << 
1101                                  * fork_nctx  << 
1102                                  * we drop th << 
1103                                  * reference  << 
1104                                  */           << 
1105                                 userfaultfd_c << 
1106                                 spin_unlock(& << 
1107                                 ret = 0;      << 
1108                                 break;        << 
1109                         }                     << 
1110                                               << 
1111                         userfaultfd_event_com << 
1112                         spin_unlock(&ctx->eve << 
1113                         ret = 0;              << 
1114                         break;                << 
1115                 }                             << 
1116                 spin_unlock(&ctx->event_wqh.l << 
1117                                               << 
1118                 if (signal_pending(current))     640                 if (signal_pending(current)) {
1119                         ret = -ERESTARTSYS;      641                         ret = -ERESTARTSYS;
1120                         break;                   642                         break;
1121                 }                                643                 }
1122                 if (no_wait) {                   644                 if (no_wait) {
1123                         ret = -EAGAIN;           645                         ret = -EAGAIN;
1124                         break;                   646                         break;
1125                 }                                647                 }
1126                 spin_unlock_irq(&ctx->fd_wqh. !! 648                 spin_unlock(&ctx->fd_wqh.lock);
1127                 schedule();                      649                 schedule();
1128                 spin_lock_irq(&ctx->fd_wqh.lo !! 650                 spin_lock(&ctx->fd_wqh.lock);
1129         }                                        651         }
1130         __remove_wait_queue(&ctx->fd_wqh, &wa    652         __remove_wait_queue(&ctx->fd_wqh, &wait);
1131         __set_current_state(TASK_RUNNING);       653         __set_current_state(TASK_RUNNING);
1132         spin_unlock_irq(&ctx->fd_wqh.lock);   !! 654         spin_unlock(&ctx->fd_wqh.lock);
1133                                               << 
1134         if (!ret && msg->event == UFFD_EVENT_ << 
1135                 ret = resolve_userfault_fork( << 
1136                 spin_lock_irq(&ctx->event_wqh << 
1137                 if (!list_empty(&fork_event)) << 
1138                         /*                    << 
1139                          * The fork thread di << 
1140                          * drop the temporary << 
1141                          */                   << 
1142                         userfaultfd_ctx_put(f << 
1143                                               << 
1144                         uwq = list_first_entr << 
1145                                               << 
1146                                               << 
1147                         /*                    << 
1148                          * If fork_event list << 
1149                          * the event wasn't a << 
1150                          * (the event is allo << 
1151                          * stack), put the ev << 
1152                          * the event_wq. fork << 
1153                          * as soon as we retu << 
1154                          * stay queued there  << 
1155                          * "ret" value.       << 
1156                          */                   << 
1157                         list_del(&uwq->wq.ent << 
1158                         __add_wait_queue(&ctx << 
1159                                               << 
1160                         /*                    << 
1161                          * Leave the event in << 
1162                          * error to userland  << 
1163                          * the userfault fork << 
1164                          */                   << 
1165                         if (likely(!ret))     << 
1166                                 userfaultfd_e << 
1167                 } else {                      << 
1168                         /*                    << 
1169                          * Here the fork thre << 
1170                          * refcount from the  << 
1171                          * has already been r << 
1172                          * the reference we t << 
1173                          * lock above. If res << 
1174                          * failed we've to dr << 
1175                          * fork_nctx has to b << 
1176                          * it succeeded we'll << 
1177                          * uffd references it << 
1178                          */                   << 
1179                         if (ret)              << 
1180                                 userfaultfd_c << 
1181                 }                             << 
1182                 spin_unlock_irq(&ctx->event_w << 
1183         }                                     << 
1184                                                  655 
1185         return ret;                              656         return ret;
1186 }                                                657 }
1187                                                  658 
1188 static ssize_t userfaultfd_read_iter(struct k !! 659 static ssize_t userfaultfd_read(struct file *file, char __user *buf,
                                                   >> 660                                 size_t count, loff_t *ppos)
1189 {                                                661 {
1190         struct file *file = iocb->ki_filp;    << 
1191         struct userfaultfd_ctx *ctx = file->p    662         struct userfaultfd_ctx *ctx = file->private_data;
1192         ssize_t _ret, ret = 0;                   663         ssize_t _ret, ret = 0;
1193         struct uffd_msg msg;                     664         struct uffd_msg msg;
1194         struct inode *inode = file_inode(file !! 665         int no_wait = file->f_flags & O_NONBLOCK;
1195         bool no_wait;                         << 
1196                                                  666 
1197         if (!userfaultfd_is_initialized(ctx)) !! 667         if (ctx->state == UFFD_STATE_WAIT_API)
1198                 return -EINVAL;                  668                 return -EINVAL;
1199                                                  669 
1200         no_wait = file->f_flags & O_NONBLOCK  << 
1201         for (;;) {                               670         for (;;) {
1202                 if (iov_iter_count(to) < size !! 671                 if (count < sizeof(msg))
1203                         return ret ? ret : -E    672                         return ret ? ret : -EINVAL;
1204                 _ret = userfaultfd_ctx_read(c !! 673                 _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
1205                 if (_ret < 0)                    674                 if (_ret < 0)
1206                         return ret ? ret : _r    675                         return ret ? ret : _ret;
1207                 _ret = !copy_to_iter_full(&ms !! 676                 if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
1208                 if (_ret)                     << 
1209                         return ret ? ret : -E    677                         return ret ? ret : -EFAULT;
1210                 ret += sizeof(msg);              678                 ret += sizeof(msg);
                                                   >> 679                 buf += sizeof(msg);
                                                   >> 680                 count -= sizeof(msg);
1211                 /*                               681                 /*
1212                  * Allow to read more than on    682                  * Allow to read more than one fault at time but only
1213                  * block if waiting for the v    683                  * block if waiting for the very first one.
1214                  */                              684                  */
1215                 no_wait = true;               !! 685                 no_wait = O_NONBLOCK;
1216         }                                        686         }
1217 }                                                687 }
1218                                                  688 
1219 static void __wake_userfault(struct userfault    689 static void __wake_userfault(struct userfaultfd_ctx *ctx,
1220                              struct userfault    690                              struct userfaultfd_wake_range *range)
1221 {                                                691 {
1222         spin_lock_irq(&ctx->fault_pending_wqh !! 692         unsigned long start, end;
                                                   >> 693 
                                                   >> 694         start = range->start;
                                                   >> 695         end = range->start + range->len;
                                                   >> 696 
                                                   >> 697         spin_lock(&ctx->fault_pending_wqh.lock);
1223         /* wake all in the range and autoremo    698         /* wake all in the range and autoremove */
1224         if (waitqueue_active(&ctx->fault_pend    699         if (waitqueue_active(&ctx->fault_pending_wqh))
1225                 __wake_up_locked_key(&ctx->fa    700                 __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
1226                                      range);     701                                      range);
1227         if (waitqueue_active(&ctx->fault_wqh)    702         if (waitqueue_active(&ctx->fault_wqh))
1228                 __wake_up(&ctx->fault_wqh, TA !! 703                 __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
1229         spin_unlock_irq(&ctx->fault_pending_w !! 704         spin_unlock(&ctx->fault_pending_wqh.lock);
1230 }                                                705 }
1231                                                  706 
1232 static __always_inline void wake_userfault(st    707 static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
1233                                            st    708                                            struct userfaultfd_wake_range *range)
1234 {                                                709 {
1235         unsigned seq;                            710         unsigned seq;
1236         bool need_wakeup;                        711         bool need_wakeup;
1237                                                  712 
1238         /*                                       713         /*
1239          * To be sure waitqueue_active() is n    714          * To be sure waitqueue_active() is not reordered by the CPU
1240          * before the pagetable update, use a    715          * before the pagetable update, use an explicit SMP memory
1241          * barrier here. PT lock release or m !! 716          * barrier here. PT lock release or up_read(mmap_sem) still
1242          * have release semantics that can al    717          * have release semantics that can allow the
1243          * waitqueue_active() to be reordered    718          * waitqueue_active() to be reordered before the pte update.
1244          */                                      719          */
1245         smp_mb();                                720         smp_mb();
1246                                                  721 
1247         /*                                       722         /*
1248          * Use waitqueue_active because it's     723          * Use waitqueue_active because it's very frequent to
1249          * change the address space atomicall    724          * change the address space atomically even if there are no
1250          * userfaults yet. So we take the spi    725          * userfaults yet. So we take the spinlock only when we're
1251          * sure we've userfaults to wake.        726          * sure we've userfaults to wake.
1252          */                                      727          */
1253         do {                                     728         do {
1254                 seq = read_seqcount_begin(&ct    729                 seq = read_seqcount_begin(&ctx->refile_seq);
1255                 need_wakeup = waitqueue_activ    730                 need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
1256                         waitqueue_active(&ctx    731                         waitqueue_active(&ctx->fault_wqh);
1257                 cond_resched();                  732                 cond_resched();
1258         } while (read_seqcount_retry(&ctx->re    733         } while (read_seqcount_retry(&ctx->refile_seq, seq));
1259         if (need_wakeup)                         734         if (need_wakeup)
1260                 __wake_userfault(ctx, range);    735                 __wake_userfault(ctx, range);
1261 }                                                736 }
1262                                                  737 
1263 static __always_inline int validate_unaligned !! 738 static __always_inline int validate_range(struct mm_struct *mm,
1264         struct mm_struct *mm, __u64 start, __ !! 739                                           __u64 start, __u64 len)
1265 {                                                740 {
1266         __u64 task_size = mm->task_size;         741         __u64 task_size = mm->task_size;
1267                                                  742 
                                                   >> 743         if (start & ~PAGE_MASK)
                                                   >> 744                 return -EINVAL;
1268         if (len & ~PAGE_MASK)                    745         if (len & ~PAGE_MASK)
1269                 return -EINVAL;                  746                 return -EINVAL;
1270         if (!len)                                747         if (!len)
1271                 return -EINVAL;                  748                 return -EINVAL;
1272         if (start < mmap_min_addr)               749         if (start < mmap_min_addr)
1273                 return -EINVAL;                  750                 return -EINVAL;
1274         if (start >= task_size)                  751         if (start >= task_size)
1275                 return -EINVAL;                  752                 return -EINVAL;
1276         if (len > task_size - start)             753         if (len > task_size - start)
1277                 return -EINVAL;                  754                 return -EINVAL;
1278         if (start + len <= start)             << 
1279                 return -EINVAL;               << 
1280         return 0;                                755         return 0;
1281 }                                                756 }
1282                                                  757 
1283 static __always_inline int validate_range(str << 
1284                                           __u << 
1285 {                                             << 
1286         if (start & ~PAGE_MASK)               << 
1287                 return -EINVAL;               << 
1288                                               << 
1289         return validate_unaligned_range(mm, s << 
1290 }                                             << 
1291                                               << 
1292 static int userfaultfd_register(struct userfa    758 static int userfaultfd_register(struct userfaultfd_ctx *ctx,
1293                                 unsigned long    759                                 unsigned long arg)
1294 {                                                760 {
1295         struct mm_struct *mm = ctx->mm;          761         struct mm_struct *mm = ctx->mm;
1296         struct vm_area_struct *vma, *prev, *c    762         struct vm_area_struct *vma, *prev, *cur;
1297         int ret;                                 763         int ret;
1298         struct uffdio_register uffdio_registe    764         struct uffdio_register uffdio_register;
1299         struct uffdio_register __user *user_u    765         struct uffdio_register __user *user_uffdio_register;
1300         unsigned long vm_flags, new_flags;       766         unsigned long vm_flags, new_flags;
1301         bool found;                              767         bool found;
1302         bool basic_ioctls;                    << 
1303         unsigned long start, end, vma_end;       768         unsigned long start, end, vma_end;
1304         struct vma_iterator vmi;              << 
1305         bool wp_async = userfaultfd_wp_async_ << 
1306                                                  769 
1307         user_uffdio_register = (struct uffdio    770         user_uffdio_register = (struct uffdio_register __user *) arg;
1308                                                  771 
1309         ret = -EFAULT;                           772         ret = -EFAULT;
1310         if (copy_from_user(&uffdio_register,     773         if (copy_from_user(&uffdio_register, user_uffdio_register,
1311                            sizeof(uffdio_regi    774                            sizeof(uffdio_register)-sizeof(__u64)))
1312                 goto out;                        775                 goto out;
1313                                                  776 
1314         ret = -EINVAL;                           777         ret = -EINVAL;
1315         if (!uffdio_register.mode)               778         if (!uffdio_register.mode)
1316                 goto out;                        779                 goto out;
1317         if (uffdio_register.mode & ~UFFD_API_ !! 780         if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
                                                   >> 781                                      UFFDIO_REGISTER_MODE_WP))
1318                 goto out;                        782                 goto out;
1319         vm_flags = 0;                            783         vm_flags = 0;
1320         if (uffdio_register.mode & UFFDIO_REG    784         if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
1321                 vm_flags |= VM_UFFD_MISSING;     785                 vm_flags |= VM_UFFD_MISSING;
1322         if (uffdio_register.mode & UFFDIO_REG    786         if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
1323 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP       << 
1324                 goto out;                     << 
1325 #endif                                        << 
1326                 vm_flags |= VM_UFFD_WP;          787                 vm_flags |= VM_UFFD_WP;
1327         }                                     !! 788                 /*
1328         if (uffdio_register.mode & UFFDIO_REG !! 789                  * FIXME: remove the below error constraint by
1329 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR    !! 790                  * implementing the wprotect tracking mode.
                                                   >> 791                  */
                                                   >> 792                 ret = -EINVAL;
1330                 goto out;                        793                 goto out;
1331 #endif                                        << 
1332                 vm_flags |= VM_UFFD_MINOR;    << 
1333         }                                        794         }
1334                                                  795 
1335         ret = validate_range(mm, uffdio_regis    796         ret = validate_range(mm, uffdio_register.range.start,
1336                              uffdio_register.    797                              uffdio_register.range.len);
1337         if (ret)                                 798         if (ret)
1338                 goto out;                        799                 goto out;
1339                                                  800 
1340         start = uffdio_register.range.start;     801         start = uffdio_register.range.start;
1341         end = start + uffdio_register.range.l    802         end = start + uffdio_register.range.len;
1342                                                  803 
1343         ret = -ENOMEM;                           804         ret = -ENOMEM;
1344         if (!mmget_not_zero(mm))                 805         if (!mmget_not_zero(mm))
1345                 goto out;                        806                 goto out;
1346                                                  807 
1347         ret = -EINVAL;                        !! 808         down_write(&mm->mmap_sem);
1348         mmap_write_lock(mm);                  !! 809         if (!mmget_still_valid(mm))
1349         vma_iter_init(&vmi, mm, start);       << 
1350         vma = vma_find(&vmi, end);            << 
1351         if (!vma)                             << 
1352                 goto out_unlock;                 810                 goto out_unlock;
1353                                                  811 
1354         /*                                    !! 812         vma = find_vma_prev(mm, start, &prev);
1355          * If the first vma contains huge pag !! 813         if (!vma)
1356          * is aligned to huge page size.      !! 814                 goto out_unlock;
1357          */                                   << 
1358         if (is_vm_hugetlb_page(vma)) {        << 
1359                 unsigned long vma_hpagesize = << 
1360                                                  815 
1361                 if (start & (vma_hpagesize -  !! 816         /* check that there's at least one vma in the range */
1362                         goto out_unlock;      !! 817         ret = -EINVAL;
1363         }                                     !! 818         if (vma->vm_start >= end)
                                                   >> 819                 goto out_unlock;
1364                                                  820 
1365         /*                                       821         /*
1366          * Search for not compatible vmas.       822          * Search for not compatible vmas.
                                                   >> 823          *
                                                   >> 824          * FIXME: this shall be relaxed later so that it doesn't fail
                                                   >> 825          * on tmpfs backed vmas (in addition to the current allowance
                                                   >> 826          * on anonymous vmas).
1367          */                                      827          */
1368         found = false;                           828         found = false;
1369         basic_ioctls = false;                 !! 829         for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1370         cur = vma;                            << 
1371         do {                                  << 
1372                 cond_resched();                  830                 cond_resched();
1373                                                  831 
1374                 BUG_ON(!!cur->vm_userfaultfd_    832                 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1375                        !!(cur->vm_flags & __V !! 833                        !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1376                                                  834 
1377                 /* check not compatible vmas     835                 /* check not compatible vmas */
1378                 ret = -EINVAL;                   836                 ret = -EINVAL;
1379                 if (!vma_can_userfault(cur, v !! 837                 if (cur->vm_ops)
1380                         goto out_unlock;      << 
1381                                               << 
1382                 /*                            << 
1383                  * UFFDIO_COPY will fill file << 
1384                  * PROT_WRITE. This check enf << 
1385                  * MAP_SHARED, the process ha << 
1386                  * file. If VM_MAYWRITE is se << 
1387                  * MAP_SHARED vma: there is n << 
1388                  * F_WRITE_SEAL can be taken  << 
1389                  */                           << 
1390                 ret = -EPERM;                 << 
1391                 if (unlikely(!(cur->vm_flags  << 
1392                         goto out_unlock;      << 
1393                                               << 
1394                 /*                            << 
1395                  * If this vma contains endin << 
1396                  * check alignment.           << 
1397                  */                           << 
1398                 if (is_vm_hugetlb_page(cur) & << 
1399                     end > cur->vm_start) {    << 
1400                         unsigned long vma_hpa << 
1401                                               << 
1402                         ret = -EINVAL;        << 
1403                                               << 
1404                         if (end & (vma_hpages << 
1405                                 goto out_unlo << 
1406                 }                             << 
1407                 if ((vm_flags & VM_UFFD_WP) & << 
1408                         goto out_unlock;         838                         goto out_unlock;
1409                                                  839 
1410                 /*                               840                 /*
1411                  * Check that this vma isn't     841                  * Check that this vma isn't already owned by a
1412                  * different userfaultfd. We     842                  * different userfaultfd. We can't allow more than one
1413                  * userfaultfd to own a singl    843                  * userfaultfd to own a single vma simultaneously or we
1414                  * wouldn't know which one to    844                  * wouldn't know which one to deliver the userfaults to.
1415                  */                              845                  */
1416                 ret = -EBUSY;                    846                 ret = -EBUSY;
1417                 if (cur->vm_userfaultfd_ctx.c    847                 if (cur->vm_userfaultfd_ctx.ctx &&
1418                     cur->vm_userfaultfd_ctx.c    848                     cur->vm_userfaultfd_ctx.ctx != ctx)
1419                         goto out_unlock;         849                         goto out_unlock;
1420                                                  850 
1421                 /*                            << 
1422                  * Note vmas containing huge  << 
1423                  */                           << 
1424                 if (is_vm_hugetlb_page(cur))  << 
1425                         basic_ioctls = true;  << 
1426                                               << 
1427                 found = true;                    851                 found = true;
1428         } for_each_vma_range(vmi, cur, end);  !! 852         }
1429         BUG_ON(!found);                          853         BUG_ON(!found);
1430                                                  854 
1431         vma_iter_set(&vmi, start);            << 
1432         prev = vma_prev(&vmi);                << 
1433         if (vma->vm_start < start)               855         if (vma->vm_start < start)
1434                 prev = vma;                      856                 prev = vma;
1435                                                  857 
1436         ret = 0;                                 858         ret = 0;
1437         for_each_vma_range(vmi, vma, end) {   !! 859         do {
1438                 cond_resched();                  860                 cond_resched();
1439                                                  861 
1440                 BUG_ON(!vma_can_userfault(vma !! 862                 BUG_ON(vma->vm_ops);
1441                 BUG_ON(vma->vm_userfaultfd_ct    863                 BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
1442                        vma->vm_userfaultfd_ct    864                        vma->vm_userfaultfd_ctx.ctx != ctx);
1443                 WARN_ON(!(vma->vm_flags & VM_ << 
1444                                                  865 
1445                 /*                               866                 /*
1446                  * Nothing to do: this vma is    867                  * Nothing to do: this vma is already registered into this
1447                  * userfaultfd and with the r    868                  * userfaultfd and with the right tracking mode too.
1448                  */                              869                  */
1449                 if (vma->vm_userfaultfd_ctx.c    870                 if (vma->vm_userfaultfd_ctx.ctx == ctx &&
1450                     (vma->vm_flags & vm_flags    871                     (vma->vm_flags & vm_flags) == vm_flags)
1451                         goto skip;               872                         goto skip;
1452                                                  873 
1453                 if (vma->vm_start > start)       874                 if (vma->vm_start > start)
1454                         start = vma->vm_start    875                         start = vma->vm_start;
1455                 vma_end = min(end, vma->vm_en    876                 vma_end = min(end, vma->vm_end);
1456                                                  877 
1457                 new_flags = (vma->vm_flags &  !! 878                 new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
1458                 vma = vma_modify_flags_uffd(& !! 879                 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1459                                             n !! 880                                  vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1460                                             ( !! 881                                  vma_policy(vma),
1461                 if (IS_ERR(vma)) {            !! 882                                  ((struct vm_userfaultfd_ctx){ ctx }));
1462                         ret = PTR_ERR(vma);   !! 883                 if (prev) {
1463                         break;                !! 884                         vma = prev;
                                                   >> 885                         goto next;
1464                 }                                886                 }
1465                                               !! 887                 if (vma->vm_start < start) {
                                                   >> 888                         ret = split_vma(mm, vma, start, 1);
                                                   >> 889                         if (ret)
                                                   >> 890                                 break;
                                                   >> 891                 }
                                                   >> 892                 if (vma->vm_end > end) {
                                                   >> 893                         ret = split_vma(mm, vma, end, 0);
                                                   >> 894                         if (ret)
                                                   >> 895                                 break;
                                                   >> 896                 }
                                                   >> 897         next:
1466                 /*                               898                 /*
1467                  * In the vma_merge() success    899                  * In the vma_merge() successful mprotect-like case 8:
1468                  * the next vma was merged in    900                  * the next vma was merged into the current one and
1469                  * the current one has not be    901                  * the current one has not been updated yet.
1470                  */                              902                  */
1471                 vma_start_write(vma);         !! 903                 vma->vm_flags = new_flags;
1472                 userfaultfd_set_vm_flags(vma, << 
1473                 vma->vm_userfaultfd_ctx.ctx =    904                 vma->vm_userfaultfd_ctx.ctx = ctx;
1474                                                  905 
1475                 if (is_vm_hugetlb_page(vma) & << 
1476                         hugetlb_unshare_all_p << 
1477                                               << 
1478         skip:                                    906         skip:
1479                 prev = vma;                      907                 prev = vma;
1480                 start = vma->vm_end;             908                 start = vma->vm_end;
1481         }                                     !! 909                 vma = vma->vm_next;
1482                                               !! 910         } while (vma && vma->vm_start < end);
1483 out_unlock:                                      911 out_unlock:
1484         mmap_write_unlock(mm);                !! 912         up_write(&mm->mmap_sem);
1485         mmput(mm);                               913         mmput(mm);
1486         if (!ret) {                              914         if (!ret) {
1487                 __u64 ioctls_out;             << 
1488                                               << 
1489                 ioctls_out = basic_ioctls ? U << 
1490                     UFFD_API_RANGE_IOCTLS;    << 
1491                                               << 
1492                 /*                            << 
1493                  * Declare the WP ioctl only  << 
1494                  * specified and all checks p << 
1495                  */                           << 
1496                 if (!(uffdio_register.mode &  << 
1497                         ioctls_out &= ~((__u6 << 
1498                                               << 
1499                 /* CONTINUE ioctl is only sup << 
1500                 if (!(uffdio_register.mode &  << 
1501                         ioctls_out &= ~((__u6 << 
1502                                               << 
1503                 /*                               915                 /*
1504                  * Now that we scanned all vm    916                  * Now that we scanned all vmas we can already tell
1505                  * userland which ioctls meth    917                  * userland which ioctls methods are guaranteed to
1506                  * succeed on this range.        918                  * succeed on this range.
1507                  */                              919                  */
1508                 if (put_user(ioctls_out, &use !! 920                 if (put_user(UFFD_API_RANGE_IOCTLS,
                                                   >> 921                              &user_uffdio_register->ioctls))
1509                         ret = -EFAULT;           922                         ret = -EFAULT;
1510         }                                        923         }
1511 out:                                             924 out:
1512         return ret;                              925         return ret;
1513 }                                                926 }
1514                                                  927 
1515 static int userfaultfd_unregister(struct user    928 static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
1516                                   unsigned lo    929                                   unsigned long arg)
1517 {                                                930 {
1518         struct mm_struct *mm = ctx->mm;          931         struct mm_struct *mm = ctx->mm;
1519         struct vm_area_struct *vma, *prev, *c    932         struct vm_area_struct *vma, *prev, *cur;
1520         int ret;                                 933         int ret;
1521         struct uffdio_range uffdio_unregister    934         struct uffdio_range uffdio_unregister;
1522         unsigned long new_flags;                 935         unsigned long new_flags;
1523         bool found;                              936         bool found;
1524         unsigned long start, end, vma_end;       937         unsigned long start, end, vma_end;
1525         const void __user *buf = (void __user    938         const void __user *buf = (void __user *)arg;
1526         struct vma_iterator vmi;              << 
1527         bool wp_async = userfaultfd_wp_async_ << 
1528                                                  939 
1529         ret = -EFAULT;                           940         ret = -EFAULT;
1530         if (copy_from_user(&uffdio_unregister    941         if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
1531                 goto out;                        942                 goto out;
1532                                                  943 
1533         ret = validate_range(mm, uffdio_unreg    944         ret = validate_range(mm, uffdio_unregister.start,
1534                              uffdio_unregiste    945                              uffdio_unregister.len);
1535         if (ret)                                 946         if (ret)
1536                 goto out;                        947                 goto out;
1537                                                  948 
1538         start = uffdio_unregister.start;         949         start = uffdio_unregister.start;
1539         end = start + uffdio_unregister.len;     950         end = start + uffdio_unregister.len;
1540                                                  951 
1541         ret = -ENOMEM;                           952         ret = -ENOMEM;
1542         if (!mmget_not_zero(mm))                 953         if (!mmget_not_zero(mm))
1543                 goto out;                        954                 goto out;
1544                                                  955 
1545         mmap_write_lock(mm);                  !! 956         down_write(&mm->mmap_sem);
1546         ret = -EINVAL;                        !! 957         if (!mmget_still_valid(mm))
1547         vma_iter_init(&vmi, mm, start);       << 
1548         vma = vma_find(&vmi, end);            << 
1549         if (!vma)                             << 
1550                 goto out_unlock;                 958                 goto out_unlock;
1551                                                  959 
1552         /*                                    !! 960         vma = find_vma_prev(mm, start, &prev);
1553          * If the first vma contains huge pag !! 961         if (!vma)
1554          * is aligned to huge page size.      !! 962                 goto out_unlock;
1555          */                                   << 
1556         if (is_vm_hugetlb_page(vma)) {        << 
1557                 unsigned long vma_hpagesize = << 
1558                                                  963 
1559                 if (start & (vma_hpagesize -  !! 964         /* check that there's at least one vma in the range */
1560                         goto out_unlock;      !! 965         ret = -EINVAL;
1561         }                                     !! 966         if (vma->vm_start >= end)
                                                   >> 967                 goto out_unlock;
1562                                                  968 
1563         /*                                       969         /*
1564          * Search for not compatible vmas.       970          * Search for not compatible vmas.
                                                   >> 971          *
                                                   >> 972          * FIXME: this shall be relaxed later so that it doesn't fail
                                                   >> 973          * on tmpfs backed vmas (in addition to the current allowance
                                                   >> 974          * on anonymous vmas).
1565          */                                      975          */
1566         found = false;                           976         found = false;
1567         cur = vma;                            !! 977         ret = -EINVAL;
1568         do {                                  !! 978         for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
1569                 cond_resched();                  979                 cond_resched();
1570                                                  980 
1571                 BUG_ON(!!cur->vm_userfaultfd_    981                 BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
1572                        !!(cur->vm_flags & __V !! 982                        !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
1573                                                  983 
1574                 /*                               984                 /*
1575                  * Check not compatible vmas,    985                  * Check not compatible vmas, not strictly required
1576                  * here as not compatible vma    986                  * here as not compatible vmas cannot have an
1577                  * userfaultfd_ctx registered    987                  * userfaultfd_ctx registered on them, but this
1578                  * provides for more strict b    988                  * provides for more strict behavior to notice
1579                  * unregistration errors.        989                  * unregistration errors.
1580                  */                              990                  */
1581                 if (!vma_can_userfault(cur, c !! 991                 if (cur->vm_ops)
1582                         goto out_unlock;         992                         goto out_unlock;
1583                                                  993 
1584                 found = true;                    994                 found = true;
1585         } for_each_vma_range(vmi, cur, end);  !! 995         }
1586         BUG_ON(!found);                          996         BUG_ON(!found);
1587                                                  997 
1588         vma_iter_set(&vmi, start);            << 
1589         prev = vma_prev(&vmi);                << 
1590         if (vma->vm_start < start)               998         if (vma->vm_start < start)
1591                 prev = vma;                      999                 prev = vma;
1592                                                  1000 
1593         ret = 0;                                 1001         ret = 0;
1594         for_each_vma_range(vmi, vma, end) {   !! 1002         do {
1595                 cond_resched();                  1003                 cond_resched();
1596                                                  1004 
1597                 BUG_ON(!vma_can_userfault(vma !! 1005                 BUG_ON(vma->vm_ops);
1598                                                  1006 
1599                 /*                               1007                 /*
1600                  * Nothing to do: this vma is    1008                  * Nothing to do: this vma is already registered into this
1601                  * userfaultfd and with the r    1009                  * userfaultfd and with the right tracking mode too.
1602                  */                              1010                  */
1603                 if (!vma->vm_userfaultfd_ctx.    1011                 if (!vma->vm_userfaultfd_ctx.ctx)
1604                         goto skip;               1012                         goto skip;
1605                                                  1013 
1606                 WARN_ON(!(vma->vm_flags & VM_ << 
1607                                               << 
1608                 if (vma->vm_start > start)       1014                 if (vma->vm_start > start)
1609                         start = vma->vm_start    1015                         start = vma->vm_start;
1610                 vma_end = min(end, vma->vm_en    1016                 vma_end = min(end, vma->vm_end);
1611                                                  1017 
1612                 if (userfaultfd_missing(vma)) !! 1018                 new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
1613                         /*                    !! 1019                 prev = vma_merge(mm, prev, start, vma_end, new_flags,
1614                          * Wake any concurren !! 1020                                  vma->anon_vma, vma->vm_file, vma->vm_pgoff,
1615                          * we unregister, so  !! 1021                                  vma_policy(vma),
1616                          * permanently and it !! 1022                                  NULL_VM_UFFD_CTX);
1617                          * UFFDIO_WAKE explic !! 1023                 if (prev) {
1618                          */                   !! 1024                         vma = prev;
1619                         struct userfaultfd_wa !! 1025                         goto next;
1620                         range.start = start;  << 
1621                         range.len = vma_end - << 
1622                         wake_userfault(vma->v << 
1623                 }                                1026                 }
1624                                               !! 1027                 if (vma->vm_start < start) {
1625                 /* Reset ptes for the whole v !! 1028                         ret = split_vma(mm, vma, start, 1);
1626                 if (userfaultfd_wp(vma))      !! 1029                         if (ret)
1627                         uffd_wp_range(vma, st !! 1030                                 break;
1628                                               << 
1629                 new_flags = vma->vm_flags & ~ << 
1630                 vma = vma_modify_flags_uffd(& << 
1631                                             n << 
1632                 if (IS_ERR(vma)) {            << 
1633                         ret = PTR_ERR(vma);   << 
1634                         break;                << 
1635                 }                                1031                 }
1636                                               !! 1032                 if (vma->vm_end > end) {
                                                   >> 1033                         ret = split_vma(mm, vma, end, 0);
                                                   >> 1034                         if (ret)
                                                   >> 1035                                 break;
                                                   >> 1036                 }
                                                   >> 1037         next:
1637                 /*                               1038                 /*
1638                  * In the vma_merge() success    1039                  * In the vma_merge() successful mprotect-like case 8:
1639                  * the next vma was merged in    1040                  * the next vma was merged into the current one and
1640                  * the current one has not be    1041                  * the current one has not been updated yet.
1641                  */                              1042                  */
1642                 vma_start_write(vma);         !! 1043                 vma->vm_flags = new_flags;
1643                 userfaultfd_set_vm_flags(vma, << 
1644                 vma->vm_userfaultfd_ctx = NUL    1044                 vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
1645                                                  1045 
1646         skip:                                    1046         skip:
1647                 prev = vma;                      1047                 prev = vma;
1648                 start = vma->vm_end;             1048                 start = vma->vm_end;
1649         }                                     !! 1049                 vma = vma->vm_next;
1650                                               !! 1050         } while (vma && vma->vm_start < end);
1651 out_unlock:                                      1051 out_unlock:
1652         mmap_write_unlock(mm);                !! 1052         up_write(&mm->mmap_sem);
1653         mmput(mm);                               1053         mmput(mm);
1654 out:                                             1054 out:
1655         return ret;                              1055         return ret;
1656 }                                                1056 }
1657                                                  1057 
1658 /*                                               1058 /*
1659  * userfaultfd_wake may be used in combinatio    1059  * userfaultfd_wake may be used in combination with the
1660  * UFFDIO_*_MODE_DONTWAKE to wakeup userfault    1060  * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
1661  */                                              1061  */
1662 static int userfaultfd_wake(struct userfaultf    1062 static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
1663                             unsigned long arg    1063                             unsigned long arg)
1664 {                                                1064 {
1665         int ret;                                 1065         int ret;
1666         struct uffdio_range uffdio_wake;         1066         struct uffdio_range uffdio_wake;
1667         struct userfaultfd_wake_range range;     1067         struct userfaultfd_wake_range range;
1668         const void __user *buf = (void __user    1068         const void __user *buf = (void __user *)arg;
1669                                                  1069 
1670         ret = -EFAULT;                           1070         ret = -EFAULT;
1671         if (copy_from_user(&uffdio_wake, buf,    1071         if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
1672                 goto out;                        1072                 goto out;
1673                                                  1073 
1674         ret = validate_range(ctx->mm, uffdio_    1074         ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
1675         if (ret)                                 1075         if (ret)
1676                 goto out;                        1076                 goto out;
1677                                                  1077 
1678         range.start = uffdio_wake.start;         1078         range.start = uffdio_wake.start;
1679         range.len = uffdio_wake.len;             1079         range.len = uffdio_wake.len;
1680                                                  1080 
1681         /*                                       1081         /*
1682          * len == 0 means wake all and we don    1082          * len == 0 means wake all and we don't want to wake all here,
1683          * so check it again to be sure.         1083          * so check it again to be sure.
1684          */                                      1084          */
1685         VM_BUG_ON(!range.len);                   1085         VM_BUG_ON(!range.len);
1686                                                  1086 
1687         wake_userfault(ctx, &range);             1087         wake_userfault(ctx, &range);
1688         ret = 0;                                 1088         ret = 0;
1689                                                  1089 
1690 out:                                             1090 out:
1691         return ret;                              1091         return ret;
1692 }                                                1092 }
1693                                                  1093 
1694 static int userfaultfd_copy(struct userfaultf    1094 static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
1695                             unsigned long arg    1095                             unsigned long arg)
1696 {                                                1096 {
1697         __s64 ret;                               1097         __s64 ret;
1698         struct uffdio_copy uffdio_copy;          1098         struct uffdio_copy uffdio_copy;
1699         struct uffdio_copy __user *user_uffdi    1099         struct uffdio_copy __user *user_uffdio_copy;
1700         struct userfaultfd_wake_range range;     1100         struct userfaultfd_wake_range range;
1701         uffd_flags_t flags = 0;               << 
1702                                                  1101 
1703         user_uffdio_copy = (struct uffdio_cop    1102         user_uffdio_copy = (struct uffdio_copy __user *) arg;
1704                                                  1103 
1705         ret = -EAGAIN;                        << 
1706         if (atomic_read(&ctx->mmap_changing)) << 
1707                 goto out;                     << 
1708                                               << 
1709         ret = -EFAULT;                           1104         ret = -EFAULT;
1710         if (copy_from_user(&uffdio_copy, user    1105         if (copy_from_user(&uffdio_copy, user_uffdio_copy,
1711                            /* don't copy "cop    1106                            /* don't copy "copy" last field */
1712                            sizeof(uffdio_copy    1107                            sizeof(uffdio_copy)-sizeof(__s64)))
1713                 goto out;                        1108                 goto out;
1714                                                  1109 
1715         ret = validate_unaligned_range(ctx->m << 
1716                                        uffdio << 
1717         if (ret)                              << 
1718                 goto out;                     << 
1719         ret = validate_range(ctx->mm, uffdio_    1110         ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
1720         if (ret)                                 1111         if (ret)
1721                 goto out;                        1112                 goto out;
1722                                               !! 1113         /*
                                                   >> 1114          * double check for wraparound just in case. copy_from_user()
                                                   >> 1115          * will later check uffdio_copy.src + uffdio_copy.len to fit
                                                   >> 1116          * in the userland range.
                                                   >> 1117          */
1723         ret = -EINVAL;                           1118         ret = -EINVAL;
1724         if (uffdio_copy.mode & ~(UFFDIO_COPY_ !! 1119         if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
                                                   >> 1120                 goto out;
                                                   >> 1121         if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
1725                 goto out;                        1122                 goto out;
1726         if (uffdio_copy.mode & UFFDIO_COPY_MO << 
1727                 flags |= MFILL_ATOMIC_WP;     << 
1728         if (mmget_not_zero(ctx->mm)) {           1123         if (mmget_not_zero(ctx->mm)) {
1729                 ret = mfill_atomic_copy(ctx,  !! 1124                 ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
1730                                         uffdi !! 1125                                    uffdio_copy.len);
1731                 mmput(ctx->mm);                  1126                 mmput(ctx->mm);
1732         } else {                              << 
1733                 return -ESRCH;                << 
1734         }                                        1127         }
1735         if (unlikely(put_user(ret, &user_uffd    1128         if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
1736                 return -EFAULT;                  1129                 return -EFAULT;
1737         if (ret < 0)                             1130         if (ret < 0)
1738                 goto out;                        1131                 goto out;
1739         BUG_ON(!ret);                            1132         BUG_ON(!ret);
1740         /* len == 0 would wake all */            1133         /* len == 0 would wake all */
1741         range.len = ret;                         1134         range.len = ret;
1742         if (!(uffdio_copy.mode & UFFDIO_COPY_    1135         if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
1743                 range.start = uffdio_copy.dst    1136                 range.start = uffdio_copy.dst;
1744                 wake_userfault(ctx, &range);     1137                 wake_userfault(ctx, &range);
1745         }                                        1138         }
1746         ret = range.len == uffdio_copy.len ?     1139         ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
1747 out:                                             1140 out:
1748         return ret;                              1141         return ret;
1749 }                                                1142 }
1750                                                  1143 
1751 static int userfaultfd_zeropage(struct userfa    1144 static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
1752                                 unsigned long    1145                                 unsigned long arg)
1753 {                                                1146 {
1754         __s64 ret;                               1147         __s64 ret;
1755         struct uffdio_zeropage uffdio_zeropag    1148         struct uffdio_zeropage uffdio_zeropage;
1756         struct uffdio_zeropage __user *user_u    1149         struct uffdio_zeropage __user *user_uffdio_zeropage;
1757         struct userfaultfd_wake_range range;     1150         struct userfaultfd_wake_range range;
1758                                                  1151 
1759         user_uffdio_zeropage = (struct uffdio    1152         user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
1760                                                  1153 
1761         ret = -EAGAIN;                        << 
1762         if (atomic_read(&ctx->mmap_changing)) << 
1763                 goto out;                     << 
1764                                               << 
1765         ret = -EFAULT;                           1154         ret = -EFAULT;
1766         if (copy_from_user(&uffdio_zeropage,     1155         if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
1767                            /* don't copy "zer    1156                            /* don't copy "zeropage" last field */
1768                            sizeof(uffdio_zero    1157                            sizeof(uffdio_zeropage)-sizeof(__s64)))
1769                 goto out;                        1158                 goto out;
1770                                                  1159 
1771         ret = validate_range(ctx->mm, uffdio_    1160         ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
1772                              uffdio_zeropage.    1161                              uffdio_zeropage.range.len);
1773         if (ret)                                 1162         if (ret)
1774                 goto out;                        1163                 goto out;
1775         ret = -EINVAL;                           1164         ret = -EINVAL;
1776         if (uffdio_zeropage.mode & ~UFFDIO_ZE    1165         if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
1777                 goto out;                        1166                 goto out;
1778                                                  1167 
1779         if (mmget_not_zero(ctx->mm)) {           1168         if (mmget_not_zero(ctx->mm)) {
1780                 ret = mfill_atomic_zeropage(c !! 1169                 ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
1781                                            uf !! 1170                                      uffdio_zeropage.range.len);
1782                 mmput(ctx->mm);                  1171                 mmput(ctx->mm);
1783         } else {                              << 
1784                 return -ESRCH;                << 
1785         }                                        1172         }
1786         if (unlikely(put_user(ret, &user_uffd    1173         if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
1787                 return -EFAULT;                  1174                 return -EFAULT;
1788         if (ret < 0)                             1175         if (ret < 0)
1789                 goto out;                        1176                 goto out;
1790         /* len == 0 would wake all */            1177         /* len == 0 would wake all */
1791         BUG_ON(!ret);                            1178         BUG_ON(!ret);
1792         range.len = ret;                         1179         range.len = ret;
1793         if (!(uffdio_zeropage.mode & UFFDIO_Z    1180         if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
1794                 range.start = uffdio_zeropage    1181                 range.start = uffdio_zeropage.range.start;
1795                 wake_userfault(ctx, &range);     1182                 wake_userfault(ctx, &range);
1796         }                                        1183         }
1797         ret = range.len == uffdio_zeropage.ra    1184         ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
1798 out:                                             1185 out:
1799         return ret;                              1186         return ret;
1800 }                                                1187 }
1801                                                  1188 
1802 static int userfaultfd_writeprotect(struct us << 
1803                                     unsigned  << 
1804 {                                             << 
1805         int ret;                              << 
1806         struct uffdio_writeprotect uffdio_wp; << 
1807         struct uffdio_writeprotect __user *us << 
1808         struct userfaultfd_wake_range range;  << 
1809         bool mode_wp, mode_dontwake;          << 
1810                                               << 
1811         if (atomic_read(&ctx->mmap_changing)) << 
1812                 return -EAGAIN;               << 
1813                                               << 
1814         user_uffdio_wp = (struct uffdio_write << 
1815                                               << 
1816         if (copy_from_user(&uffdio_wp, user_u << 
1817                            sizeof(struct uffd << 
1818                 return -EFAULT;               << 
1819                                               << 
1820         ret = validate_range(ctx->mm, uffdio_ << 
1821                              uffdio_wp.range. << 
1822         if (ret)                              << 
1823                 return ret;                   << 
1824                                               << 
1825         if (uffdio_wp.mode & ~(UFFDIO_WRITEPR << 
1826                                UFFDIO_WRITEPR << 
1827                 return -EINVAL;               << 
1828                                               << 
1829         mode_wp = uffdio_wp.mode & UFFDIO_WRI << 
1830         mode_dontwake = uffdio_wp.mode & UFFD << 
1831                                               << 
1832         if (mode_wp && mode_dontwake)         << 
1833                 return -EINVAL;               << 
1834                                               << 
1835         if (mmget_not_zero(ctx->mm)) {        << 
1836                 ret = mwriteprotect_range(ctx << 
1837                                           uff << 
1838                 mmput(ctx->mm);               << 
1839         } else {                              << 
1840                 return -ESRCH;                << 
1841         }                                     << 
1842                                               << 
1843         if (ret)                              << 
1844                 return ret;                   << 
1845                                               << 
1846         if (!mode_wp && !mode_dontwake) {     << 
1847                 range.start = uffdio_wp.range << 
1848                 range.len = uffdio_wp.range.l << 
1849                 wake_userfault(ctx, &range);  << 
1850         }                                     << 
1851         return ret;                           << 
1852 }                                             << 
1853                                               << 
1854 static int userfaultfd_continue(struct userfa << 
1855 {                                             << 
1856         __s64 ret;                            << 
1857         struct uffdio_continue uffdio_continu << 
1858         struct uffdio_continue __user *user_u << 
1859         struct userfaultfd_wake_range range;  << 
1860         uffd_flags_t flags = 0;               << 
1861                                               << 
1862         user_uffdio_continue = (struct uffdio << 
1863                                               << 
1864         ret = -EAGAIN;                        << 
1865         if (atomic_read(&ctx->mmap_changing)) << 
1866                 goto out;                     << 
1867                                               << 
1868         ret = -EFAULT;                        << 
1869         if (copy_from_user(&uffdio_continue,  << 
1870                            /* don't copy the  << 
1871                            sizeof(uffdio_cont << 
1872                 goto out;                     << 
1873                                               << 
1874         ret = validate_range(ctx->mm, uffdio_ << 
1875                              uffdio_continue. << 
1876         if (ret)                              << 
1877                 goto out;                     << 
1878                                               << 
1879         ret = -EINVAL;                        << 
1880         if (uffdio_continue.mode & ~(UFFDIO_C << 
1881                                      UFFDIO_C << 
1882                 goto out;                     << 
1883         if (uffdio_continue.mode & UFFDIO_CON << 
1884                 flags |= MFILL_ATOMIC_WP;     << 
1885                                               << 
1886         if (mmget_not_zero(ctx->mm)) {        << 
1887                 ret = mfill_atomic_continue(c << 
1888                                             u << 
1889                 mmput(ctx->mm);               << 
1890         } else {                              << 
1891                 return -ESRCH;                << 
1892         }                                     << 
1893                                               << 
1894         if (unlikely(put_user(ret, &user_uffd << 
1895                 return -EFAULT;               << 
1896         if (ret < 0)                          << 
1897                 goto out;                     << 
1898                                               << 
1899         /* len == 0 would wake all */         << 
1900         BUG_ON(!ret);                         << 
1901         range.len = ret;                      << 
1902         if (!(uffdio_continue.mode & UFFDIO_C << 
1903                 range.start = uffdio_continue << 
1904                 wake_userfault(ctx, &range);  << 
1905         }                                     << 
1906         ret = range.len == uffdio_continue.ra << 
1907                                               << 
1908 out:                                          << 
1909         return ret;                           << 
1910 }                                             << 
1911                                               << 
1912 static inline int userfaultfd_poison(struct u << 
1913 {                                             << 
1914         __s64 ret;                            << 
1915         struct uffdio_poison uffdio_poison;   << 
1916         struct uffdio_poison __user *user_uff << 
1917         struct userfaultfd_wake_range range;  << 
1918                                               << 
1919         user_uffdio_poison = (struct uffdio_p << 
1920                                               << 
1921         ret = -EAGAIN;                        << 
1922         if (atomic_read(&ctx->mmap_changing)) << 
1923                 goto out;                     << 
1924                                               << 
1925         ret = -EFAULT;                        << 
1926         if (copy_from_user(&uffdio_poison, us << 
1927                            /* don't copy the  << 
1928                            sizeof(uffdio_pois << 
1929                 goto out;                     << 
1930                                               << 
1931         ret = validate_range(ctx->mm, uffdio_ << 
1932                              uffdio_poison.ra << 
1933         if (ret)                              << 
1934                 goto out;                     << 
1935                                               << 
1936         ret = -EINVAL;                        << 
1937         if (uffdio_poison.mode & ~UFFDIO_POIS << 
1938                 goto out;                     << 
1939                                               << 
1940         if (mmget_not_zero(ctx->mm)) {        << 
1941                 ret = mfill_atomic_poison(ctx << 
1942                                           uff << 
1943                 mmput(ctx->mm);               << 
1944         } else {                              << 
1945                 return -ESRCH;                << 
1946         }                                     << 
1947                                               << 
1948         if (unlikely(put_user(ret, &user_uffd << 
1949                 return -EFAULT;               << 
1950         if (ret < 0)                          << 
1951                 goto out;                     << 
1952                                               << 
1953         /* len == 0 would wake all */         << 
1954         BUG_ON(!ret);                         << 
1955         range.len = ret;                      << 
1956         if (!(uffdio_poison.mode & UFFDIO_POI << 
1957                 range.start = uffdio_poison.r << 
1958                 wake_userfault(ctx, &range);  << 
1959         }                                     << 
1960         ret = range.len == uffdio_poison.rang << 
1961                                               << 
1962 out:                                          << 
1963         return ret;                           << 
1964 }                                             << 
1965                                               << 
1966 bool userfaultfd_wp_async(struct vm_area_stru << 
1967 {                                             << 
1968         return userfaultfd_wp_async_ctx(vma-> << 
1969 }                                             << 
1970                                               << 
1971 static inline unsigned int uffd_ctx_features( << 
1972 {                                             << 
1973         /*                                    << 
1974          * For the current set of features th << 
1975          * UFFD_FEATURE_INITIALIZED to mark t << 
1976          */                                   << 
1977         return (unsigned int)user_features |  << 
1978 }                                             << 
1979                                               << 
1980 static int userfaultfd_move(struct userfaultf << 
1981                             unsigned long arg << 
1982 {                                             << 
1983         __s64 ret;                            << 
1984         struct uffdio_move uffdio_move;       << 
1985         struct uffdio_move __user *user_uffdi << 
1986         struct userfaultfd_wake_range range;  << 
1987         struct mm_struct *mm = ctx->mm;       << 
1988                                               << 
1989         user_uffdio_move = (struct uffdio_mov << 
1990                                               << 
1991         if (atomic_read(&ctx->mmap_changing)) << 
1992                 return -EAGAIN;               << 
1993                                               << 
1994         if (copy_from_user(&uffdio_move, user << 
1995                            /* don't copy "mov << 
1996                            sizeof(uffdio_move << 
1997                 return -EFAULT;               << 
1998                                               << 
1999         /* Do not allow cross-mm moves. */    << 
2000         if (mm != current->mm)                << 
2001                 return -EINVAL;               << 
2002                                               << 
2003         ret = validate_range(mm, uffdio_move. << 
2004         if (ret)                              << 
2005                 return ret;                   << 
2006                                               << 
2007         ret = validate_range(mm, uffdio_move. << 
2008         if (ret)                              << 
2009                 return ret;                   << 
2010                                               << 
2011         if (uffdio_move.mode & ~(UFFDIO_MOVE_ << 
2012                                   UFFDIO_MOVE << 
2013                 return -EINVAL;               << 
2014                                               << 
2015         if (mmget_not_zero(mm)) {             << 
2016                 ret = move_pages(ctx, uffdio_ << 
2017                                  uffdio_move. << 
2018                 mmput(mm);                    << 
2019         } else {                              << 
2020                 return -ESRCH;                << 
2021         }                                     << 
2022                                               << 
2023         if (unlikely(put_user(ret, &user_uffd << 
2024                 return -EFAULT;               << 
2025         if (ret < 0)                          << 
2026                 goto out;                     << 
2027                                               << 
2028         /* len == 0 would wake all */         << 
2029         VM_WARN_ON(!ret);                     << 
2030         range.len = ret;                      << 
2031         if (!(uffdio_move.mode & UFFDIO_MOVE_ << 
2032                 range.start = uffdio_move.dst << 
2033                 wake_userfault(ctx, &range);  << 
2034         }                                     << 
2035         ret = range.len == uffdio_move.len ?  << 
2036                                               << 
2037 out:                                          << 
2038         return ret;                           << 
2039 }                                             << 
2040                                               << 
2041 /*                                               1189 /*
2042  * userland asks for a certain API version an    1190  * userland asks for a certain API version and we return which bits
2043  * and ioctl commands are implemented in this    1191  * and ioctl commands are implemented in this kernel for such API
2044  * version or -EINVAL if unknown.                1192  * version or -EINVAL if unknown.
2045  */                                              1193  */
2046 static int userfaultfd_api(struct userfaultfd    1194 static int userfaultfd_api(struct userfaultfd_ctx *ctx,
2047                            unsigned long arg)    1195                            unsigned long arg)
2048 {                                                1196 {
2049         struct uffdio_api uffdio_api;            1197         struct uffdio_api uffdio_api;
2050         void __user *buf = (void __user *)arg    1198         void __user *buf = (void __user *)arg;
2051         unsigned int ctx_features;            << 
2052         int ret;                                 1199         int ret;
2053         __u64 features;                       << 
2054                                                  1200 
                                                   >> 1201         ret = -EINVAL;
                                                   >> 1202         if (ctx->state != UFFD_STATE_WAIT_API)
                                                   >> 1203                 goto out;
2055         ret = -EFAULT;                           1204         ret = -EFAULT;
2056         if (copy_from_user(&uffdio_api, buf,     1205         if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
2057                 goto out;                        1206                 goto out;
2058         features = uffdio_api.features;       !! 1207         if (uffdio_api.api != UFFD_API || uffdio_api.features) {
2059         ret = -EINVAL;                        !! 1208                 memset(&uffdio_api, 0, sizeof(uffdio_api));
2060         if (uffdio_api.api != UFFD_API)       !! 1209                 if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
2061                 goto err_out;                 !! 1210                         goto out;
2062         ret = -EPERM;                         !! 1211                 ret = -EINVAL;
2063         if ((features & UFFD_FEATURE_EVENT_FO !! 1212                 goto out;
2064                 goto err_out;                 !! 1213         }
2065                                               << 
2066         /* WP_ASYNC relies on WP_UNPOPULATED, << 
2067         if (features & UFFD_FEATURE_WP_ASYNC) << 
2068                 features |= UFFD_FEATURE_WP_U << 
2069                                               << 
2070         /* report all available features and  << 
2071         uffdio_api.features = UFFD_API_FEATUR    1214         uffdio_api.features = UFFD_API_FEATURES;
2072 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR    << 
2073         uffdio_api.features &=                << 
2074                 ~(UFFD_FEATURE_MINOR_HUGETLBF << 
2075 #endif                                        << 
2076 #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP       << 
2077         uffdio_api.features &= ~UFFD_FEATURE_ << 
2078 #endif                                        << 
2079 #ifndef CONFIG_PTE_MARKER_UFFD_WP             << 
2080         uffdio_api.features &= ~UFFD_FEATURE_ << 
2081         uffdio_api.features &= ~UFFD_FEATURE_ << 
2082         uffdio_api.features &= ~UFFD_FEATURE_ << 
2083 #endif                                        << 
2084                                               << 
2085         ret = -EINVAL;                        << 
2086         if (features & ~uffdio_api.features)  << 
2087                 goto err_out;                 << 
2088                                               << 
2089         uffdio_api.ioctls = UFFD_API_IOCTLS;     1215         uffdio_api.ioctls = UFFD_API_IOCTLS;
2090         ret = -EFAULT;                           1216         ret = -EFAULT;
2091         if (copy_to_user(buf, &uffdio_api, si    1217         if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
2092                 goto out;                        1218                 goto out;
2093                                               !! 1219         ctx->state = UFFD_STATE_RUNNING;
2094         /* only enable the requested features << 
2095         ctx_features = uffd_ctx_features(feat << 
2096         ret = -EINVAL;                        << 
2097         if (cmpxchg(&ctx->features, 0, ctx_fe << 
2098                 goto err_out;                 << 
2099                                               << 
2100         ret = 0;                                 1220         ret = 0;
2101 out:                                             1221 out:
2102         return ret;                              1222         return ret;
2103 err_out:                                      << 
2104         memset(&uffdio_api, 0, sizeof(uffdio_ << 
2105         if (copy_to_user(buf, &uffdio_api, si << 
2106                 ret = -EFAULT;                << 
2107         goto out;                             << 
2108 }                                                1223 }
2109                                                  1224 
2110 static long userfaultfd_ioctl(struct file *fi    1225 static long userfaultfd_ioctl(struct file *file, unsigned cmd,
2111                               unsigned long a    1226                               unsigned long arg)
2112 {                                                1227 {
2113         int ret = -EINVAL;                       1228         int ret = -EINVAL;
2114         struct userfaultfd_ctx *ctx = file->p    1229         struct userfaultfd_ctx *ctx = file->private_data;
2115                                                  1230 
2116         if (cmd != UFFDIO_API && !userfaultfd !! 1231         if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
2117                 return -EINVAL;                  1232                 return -EINVAL;
2118                                                  1233 
2119         switch(cmd) {                            1234         switch(cmd) {
2120         case UFFDIO_API:                         1235         case UFFDIO_API:
2121                 ret = userfaultfd_api(ctx, ar    1236                 ret = userfaultfd_api(ctx, arg);
2122                 break;                           1237                 break;
2123         case UFFDIO_REGISTER:                    1238         case UFFDIO_REGISTER:
2124                 ret = userfaultfd_register(ct    1239                 ret = userfaultfd_register(ctx, arg);
2125                 break;                           1240                 break;
2126         case UFFDIO_UNREGISTER:                  1241         case UFFDIO_UNREGISTER:
2127                 ret = userfaultfd_unregister(    1242                 ret = userfaultfd_unregister(ctx, arg);
2128                 break;                           1243                 break;
2129         case UFFDIO_WAKE:                        1244         case UFFDIO_WAKE:
2130                 ret = userfaultfd_wake(ctx, a    1245                 ret = userfaultfd_wake(ctx, arg);
2131                 break;                           1246                 break;
2132         case UFFDIO_COPY:                        1247         case UFFDIO_COPY:
2133                 ret = userfaultfd_copy(ctx, a    1248                 ret = userfaultfd_copy(ctx, arg);
2134                 break;                           1249                 break;
2135         case UFFDIO_ZEROPAGE:                    1250         case UFFDIO_ZEROPAGE:
2136                 ret = userfaultfd_zeropage(ct    1251                 ret = userfaultfd_zeropage(ctx, arg);
2137                 break;                           1252                 break;
2138         case UFFDIO_MOVE:                     << 
2139                 ret = userfaultfd_move(ctx, a << 
2140                 break;                        << 
2141         case UFFDIO_WRITEPROTECT:             << 
2142                 ret = userfaultfd_writeprotec << 
2143                 break;                        << 
2144         case UFFDIO_CONTINUE:                 << 
2145                 ret = userfaultfd_continue(ct << 
2146                 break;                        << 
2147         case UFFDIO_POISON:                   << 
2148                 ret = userfaultfd_poison(ctx, << 
2149                 break;                        << 
2150         }                                        1253         }
2151         return ret;                              1254         return ret;
2152 }                                                1255 }
2153                                                  1256 
2154 #ifdef CONFIG_PROC_FS                            1257 #ifdef CONFIG_PROC_FS
2155 static void userfaultfd_show_fdinfo(struct se    1258 static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
2156 {                                                1259 {
2157         struct userfaultfd_ctx *ctx = f->priv    1260         struct userfaultfd_ctx *ctx = f->private_data;
2158         wait_queue_entry_t *wq;               !! 1261         wait_queue_t *wq;
                                                   >> 1262         struct userfaultfd_wait_queue *uwq;
2159         unsigned long pending = 0, total = 0;    1263         unsigned long pending = 0, total = 0;
2160                                                  1264 
2161         spin_lock_irq(&ctx->fault_pending_wqh !! 1265         spin_lock(&ctx->fault_pending_wqh.lock);
2162         list_for_each_entry(wq, &ctx->fault_p !! 1266         list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
                                                   >> 1267                 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
2163                 pending++;                       1268                 pending++;
2164                 total++;                         1269                 total++;
2165         }                                        1270         }
2166         list_for_each_entry(wq, &ctx->fault_w !! 1271         list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
                                                   >> 1272                 uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
2167                 total++;                         1273                 total++;
2168         }                                        1274         }
2169         spin_unlock_irq(&ctx->fault_pending_w !! 1275         spin_unlock(&ctx->fault_pending_wqh.lock);
2170                                                  1276 
2171         /*                                       1277         /*
2172          * If more protocols will be added, t    1278          * If more protocols will be added, there will be all shown
2173          * separated by a space. Like this:      1279          * separated by a space. Like this:
2174          *      protocols: aa:... bb:...         1280          *      protocols: aa:... bb:...
2175          */                                      1281          */
2176         seq_printf(m, "pending:\t%lu\ntotal:\    1282         seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
2177                    pending, total, UFFD_API,  !! 1283                    pending, total, UFFD_API, UFFD_API_FEATURES,
2178                    UFFD_API_IOCTLS|UFFD_API_R    1284                    UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
2179 }                                                1285 }
2180 #endif                                           1286 #endif
2181                                                  1287 
2182 static const struct file_operations userfault    1288 static const struct file_operations userfaultfd_fops = {
2183 #ifdef CONFIG_PROC_FS                            1289 #ifdef CONFIG_PROC_FS
2184         .show_fdinfo    = userfaultfd_show_fd    1290         .show_fdinfo    = userfaultfd_show_fdinfo,
2185 #endif                                           1291 #endif
2186         .release        = userfaultfd_release    1292         .release        = userfaultfd_release,
2187         .poll           = userfaultfd_poll,      1293         .poll           = userfaultfd_poll,
2188         .read_iter      = userfaultfd_read_it !! 1294         .read           = userfaultfd_read,
2189         .unlocked_ioctl = userfaultfd_ioctl,     1295         .unlocked_ioctl = userfaultfd_ioctl,
2190         .compat_ioctl   = compat_ptr_ioctl,   !! 1296         .compat_ioctl   = userfaultfd_ioctl,
2191         .llseek         = noop_llseek,           1297         .llseek         = noop_llseek,
2192 };                                               1298 };
2193                                                  1299 
2194 static void init_once_userfaultfd_ctx(void *m    1300 static void init_once_userfaultfd_ctx(void *mem)
2195 {                                                1301 {
2196         struct userfaultfd_ctx *ctx = (struct    1302         struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
2197                                                  1303 
2198         init_waitqueue_head(&ctx->fault_pendi    1304         init_waitqueue_head(&ctx->fault_pending_wqh);
2199         init_waitqueue_head(&ctx->fault_wqh);    1305         init_waitqueue_head(&ctx->fault_wqh);
2200         init_waitqueue_head(&ctx->event_wqh); << 
2201         init_waitqueue_head(&ctx->fd_wqh);       1306         init_waitqueue_head(&ctx->fd_wqh);
2202         seqcount_spinlock_init(&ctx->refile_s !! 1307         seqcount_init(&ctx->refile_seq);
2203 }                                                1308 }
2204                                                  1309 
2205 static int new_userfaultfd(int flags)         !! 1310 /**
                                                   >> 1311  * userfaultfd_file_create - Creates an userfaultfd file pointer.
                                                   >> 1312  * @flags: Flags for the userfaultfd file.
                                                   >> 1313  *
                                                   >> 1314  * This function creates an userfaultfd file pointer, w/out installing
                                                   >> 1315  * it into the fd table. This is useful when the userfaultfd file is
                                                   >> 1316  * used during the initialization of data structures that require
                                                   >> 1317  * extra setup after the userfaultfd creation. So the userfaultfd
                                                   >> 1318  * creation is split into the file pointer creation phase, and the
                                                   >> 1319  * file descriptor installation phase.  In this way races with
                                                   >> 1320  * userspace closing the newly installed file descriptor can be
                                                   >> 1321  * avoided.  Returns an userfaultfd file pointer, or a proper error
                                                   >> 1322  * pointer.
                                                   >> 1323  */
                                                   >> 1324 static struct file *userfaultfd_file_create(int flags)
2206 {                                                1325 {
2207         struct userfaultfd_ctx *ctx;          << 
2208         struct file *file;                       1326         struct file *file;
2209         int fd;                               !! 1327         struct userfaultfd_ctx *ctx;
2210                                                  1328 
2211         BUG_ON(!current->mm);                    1329         BUG_ON(!current->mm);
2212                                                  1330 
2213         /* Check the UFFD_* constants for con    1331         /* Check the UFFD_* constants for consistency.  */
2214         BUILD_BUG_ON(UFFD_USER_MODE_ONLY & UF << 
2215         BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXE    1332         BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
2216         BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBL    1333         BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
2217                                                  1334 
2218         if (flags & ~(UFFD_SHARED_FCNTL_FLAGS !! 1335         file = ERR_PTR(-EINVAL);
2219                 return -EINVAL;               !! 1336         if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
                                                   >> 1337                 goto out;
2220                                                  1338 
                                                   >> 1339         file = ERR_PTR(-ENOMEM);
2221         ctx = kmem_cache_alloc(userfaultfd_ct    1340         ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
2222         if (!ctx)                                1341         if (!ctx)
2223                 return -ENOMEM;               !! 1342                 goto out;
2224                                                  1343 
2225         refcount_set(&ctx->refcount, 1);      !! 1344         atomic_set(&ctx->refcount, 1);
2226         ctx->flags = flags;                      1345         ctx->flags = flags;
2227         ctx->features = 0;                    !! 1346         ctx->state = UFFD_STATE_WAIT_API;
2228         ctx->released = false;                   1347         ctx->released = false;
2229         init_rwsem(&ctx->map_changing_lock);  << 
2230         atomic_set(&ctx->mmap_changing, 0);   << 
2231         ctx->mm = current->mm;                   1348         ctx->mm = current->mm;
                                                   >> 1349         /* prevent the mm struct to be freed */
                                                   >> 1350         atomic_inc(&ctx->mm->mm_count);
2232                                                  1351 
2233         fd = get_unused_fd_flags(flags & UFFD !! 1352         file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
2234         if (fd < 0)                           !! 1353                                   O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
2235                 goto err_out;                 << 
2236                                               << 
2237         /* Create a new inode so that the LSM << 
2238         file = anon_inode_create_getfile("[us << 
2239                         O_RDONLY | (flags & U << 
2240         if (IS_ERR(file)) {                      1354         if (IS_ERR(file)) {
2241                 put_unused_fd(fd);            !! 1355                 mmdrop(ctx->mm);
2242                 fd = PTR_ERR(file);           !! 1356                 kmem_cache_free(userfaultfd_ctx_cachep, ctx);
2243                 goto err_out;                 << 
2244         }                                        1357         }
2245         /* prevent the mm struct to be freed  !! 1358 out:
2246         mmgrab(ctx->mm);                      !! 1359         return file;
2247         file->f_mode |= FMODE_NOWAIT;         << 
2248         fd_install(fd, file);                 << 
2249         return fd;                            << 
2250 err_out:                                      << 
2251         kmem_cache_free(userfaultfd_ctx_cache << 
2252         return fd;                            << 
2253 }                                             << 
2254                                               << 
2255 static inline bool userfaultfd_syscall_allowe << 
2256 {                                             << 
2257         /* Userspace-only page faults are alw << 
2258         if (flags & UFFD_USER_MODE_ONLY)      << 
2259                 return true;                  << 
2260                                               << 
2261         /*                                    << 
2262          * The user is requesting a userfault << 
2263          * Privileged users are always allowe << 
2264          */                                   << 
2265         if (capable(CAP_SYS_PTRACE))          << 
2266                 return true;                  << 
2267                                               << 
2268         /* Otherwise, access to kernel fault  << 
2269         return sysctl_unprivileged_userfaultf << 
2270 }                                                1360 }
2271                                                  1361 
2272 SYSCALL_DEFINE1(userfaultfd, int, flags)         1362 SYSCALL_DEFINE1(userfaultfd, int, flags)
2273 {                                                1363 {
2274         if (!userfaultfd_syscall_allowed(flag !! 1364         int fd, error;
2275                 return -EPERM;                !! 1365         struct file *file;
2276                                                  1366 
2277         return new_userfaultfd(flags);        !! 1367         error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
2278 }                                             !! 1368         if (error < 0)
                                                   >> 1369                 return error;
                                                   >> 1370         fd = error;
2279                                                  1371 
2280 static long userfaultfd_dev_ioctl(struct file !! 1372         file = userfaultfd_file_create(flags);
2281 {                                             !! 1373         if (IS_ERR(file)) {
2282         if (cmd != USERFAULTFD_IOC_NEW)       !! 1374                 error = PTR_ERR(file);
2283                 return -EINVAL;               !! 1375                 goto err_put_unused_fd;
                                                   >> 1376         }
                                                   >> 1377         fd_install(fd, file);
2284                                                  1378 
2285         return new_userfaultfd(flags);        !! 1379         return fd;
2286 }                                             << 
2287                                                  1380 
2288 static const struct file_operations userfault !! 1381 err_put_unused_fd:
2289         .unlocked_ioctl = userfaultfd_dev_ioc !! 1382         put_unused_fd(fd);
2290         .compat_ioctl = userfaultfd_dev_ioctl << 
2291         .owner = THIS_MODULE,                 << 
2292         .llseek = noop_llseek,                << 
2293 };                                            << 
2294                                                  1383 
2295 static struct miscdevice userfaultfd_misc = { !! 1384         return error;
2296         .minor = MISC_DYNAMIC_MINOR,          !! 1385 }
2297         .name = "userfaultfd",                << 
2298         .fops = &userfaultfd_dev_fops         << 
2299 };                                            << 
2300                                                  1386 
2301 static int __init userfaultfd_init(void)         1387 static int __init userfaultfd_init(void)
2302 {                                                1388 {
2303         int ret;                              << 
2304                                               << 
2305         ret = misc_register(&userfaultfd_misc << 
2306         if (ret)                              << 
2307                 return ret;                   << 
2308                                               << 
2309         userfaultfd_ctx_cachep = kmem_cache_c    1389         userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
2310                                                  1390                                                 sizeof(struct userfaultfd_ctx),
2311                                                  1391                                                 0,
2312                                                  1392                                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2313                                                  1393                                                 init_once_userfaultfd_ctx);
2314 #ifdef CONFIG_SYSCTL                          << 
2315         register_sysctl_init("vm", vm_userfau << 
2316 #endif                                        << 
2317         return 0;                                1394         return 0;
2318 }                                                1395 }
2319 __initcall(userfaultfd_init);                    1396 __initcall(userfaultfd_init);
2320                                                  1397 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php