~ [ 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-6.8.12)


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

~ [ 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