userfaultfd.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. /*
  2. * fs/userfaultfd.c
  3. *
  4. * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
  5. * Copyright (C) 2008-2009 Red Hat, Inc.
  6. * Copyright (C) 2015 Red Hat, Inc.
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2. See
  9. * the COPYING file in the top-level directory.
  10. *
  11. * Some part derived from fs/eventfd.c (anon inode setup) and
  12. * mm/ksm.c (mm hashing).
  13. */
  14. #include <linux/list.h>
  15. #include <linux/hashtable.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/sched/mm.h>
  18. #include <linux/mm.h>
  19. #include <linux/poll.h>
  20. #include <linux/slab.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/file.h>
  23. #include <linux/bug.h>
  24. #include <linux/anon_inodes.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/userfaultfd_k.h>
  27. #include <linux/mempolicy.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/security.h>
  30. #include <linux/hugetlb.h>
  31. static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
  32. enum userfaultfd_state {
  33. UFFD_STATE_WAIT_API,
  34. UFFD_STATE_RUNNING,
  35. };
  36. /*
  37. * Start with fault_pending_wqh and fault_wqh so they're more likely
  38. * to be in the same cacheline.
  39. */
  40. struct userfaultfd_ctx {
  41. /* waitqueue head for the pending (i.e. not read) userfaults */
  42. wait_queue_head_t fault_pending_wqh;
  43. /* waitqueue head for the userfaults */
  44. wait_queue_head_t fault_wqh;
  45. /* waitqueue head for the pseudo fd to wakeup poll/read */
  46. wait_queue_head_t fd_wqh;
  47. /* waitqueue head for events */
  48. wait_queue_head_t event_wqh;
  49. /* a refile sequence protected by fault_pending_wqh lock */
  50. struct seqcount refile_seq;
  51. /* pseudo fd refcounting */
  52. atomic_t refcount;
  53. /* userfaultfd syscall flags */
  54. unsigned int flags;
  55. /* features requested from the userspace */
  56. unsigned int features;
  57. /* state machine */
  58. enum userfaultfd_state state;
  59. /* released */
  60. bool released;
  61. /* mm with one ore more vmas attached to this userfaultfd_ctx */
  62. struct mm_struct *mm;
  63. };
  64. struct userfaultfd_fork_ctx {
  65. struct userfaultfd_ctx *orig;
  66. struct userfaultfd_ctx *new;
  67. struct list_head list;
  68. };
  69. struct userfaultfd_unmap_ctx {
  70. struct userfaultfd_ctx *ctx;
  71. unsigned long start;
  72. unsigned long end;
  73. struct list_head list;
  74. };
  75. struct userfaultfd_wait_queue {
  76. struct uffd_msg msg;
  77. wait_queue_entry_t wq;
  78. struct userfaultfd_ctx *ctx;
  79. bool waken;
  80. };
  81. struct userfaultfd_wake_range {
  82. unsigned long start;
  83. unsigned long len;
  84. };
  85. static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode,
  86. int wake_flags, void *key)
  87. {
  88. struct userfaultfd_wake_range *range = key;
  89. int ret;
  90. struct userfaultfd_wait_queue *uwq;
  91. unsigned long start, len;
  92. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  93. ret = 0;
  94. /* len == 0 means wake all */
  95. start = range->start;
  96. len = range->len;
  97. if (len && (start > uwq->msg.arg.pagefault.address ||
  98. start + len <= uwq->msg.arg.pagefault.address))
  99. goto out;
  100. WRITE_ONCE(uwq->waken, true);
  101. /*
  102. * The Program-Order guarantees provided by the scheduler
  103. * ensure uwq->waken is visible before the task is woken.
  104. */
  105. ret = wake_up_state(wq->private, mode);
  106. if (ret) {
  107. /*
  108. * Wake only once, autoremove behavior.
  109. *
  110. * After the effect of list_del_init is visible to the other
  111. * CPUs, the waitqueue may disappear from under us, see the
  112. * !list_empty_careful() in handle_userfault().
  113. *
  114. * try_to_wake_up() has an implicit smp_mb(), and the
  115. * wq->private is read before calling the extern function
  116. * "wake_up_state" (which in turns calls try_to_wake_up).
  117. */
  118. list_del_init(&wq->entry);
  119. }
  120. out:
  121. return ret;
  122. }
  123. /**
  124. * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
  125. * context.
  126. * @ctx: [in] Pointer to the userfaultfd context.
  127. */
  128. static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
  129. {
  130. if (!atomic_inc_not_zero(&ctx->refcount))
  131. BUG();
  132. }
  133. /**
  134. * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
  135. * context.
  136. * @ctx: [in] Pointer to userfaultfd context.
  137. *
  138. * The userfaultfd context reference must have been previously acquired either
  139. * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
  140. */
  141. static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
  142. {
  143. if (atomic_dec_and_test(&ctx->refcount)) {
  144. VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
  145. VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
  146. VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
  147. VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
  148. VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
  149. VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
  150. VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
  151. VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
  152. mmdrop(ctx->mm);
  153. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  154. }
  155. }
  156. static inline void msg_init(struct uffd_msg *msg)
  157. {
  158. BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
  159. /*
  160. * Must use memset to zero out the paddings or kernel data is
  161. * leaked to userland.
  162. */
  163. memset(msg, 0, sizeof(struct uffd_msg));
  164. }
  165. static inline struct uffd_msg userfault_msg(unsigned long address,
  166. unsigned int flags,
  167. unsigned long reason,
  168. unsigned int features)
  169. {
  170. struct uffd_msg msg;
  171. msg_init(&msg);
  172. msg.event = UFFD_EVENT_PAGEFAULT;
  173. msg.arg.pagefault.address = address;
  174. if (flags & FAULT_FLAG_WRITE)
  175. /*
  176. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  177. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
  178. * was not set in a UFFD_EVENT_PAGEFAULT, it means it
  179. * was a read fault, otherwise if set it means it's
  180. * a write fault.
  181. */
  182. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
  183. if (reason & VM_UFFD_WP)
  184. /*
  185. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  186. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
  187. * not set in a UFFD_EVENT_PAGEFAULT, it means it was
  188. * a missing fault, otherwise if set it means it's a
  189. * write protect fault.
  190. */
  191. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
  192. if (features & UFFD_FEATURE_THREAD_ID)
  193. msg.arg.pagefault.feat.ptid = task_pid_vnr(current);
  194. return msg;
  195. }
  196. #ifdef CONFIG_HUGETLB_PAGE
  197. /*
  198. * Same functionality as userfaultfd_must_wait below with modifications for
  199. * hugepmd ranges.
  200. */
  201. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  202. struct vm_area_struct *vma,
  203. unsigned long address,
  204. unsigned long flags,
  205. unsigned long reason)
  206. {
  207. struct mm_struct *mm = ctx->mm;
  208. pte_t *pte;
  209. bool ret = true;
  210. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  211. pte = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
  212. if (!pte)
  213. goto out;
  214. ret = false;
  215. /*
  216. * Lockless access: we're in a wait_event so it's ok if it
  217. * changes under us.
  218. */
  219. if (huge_pte_none(*pte))
  220. ret = true;
  221. if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
  222. ret = true;
  223. out:
  224. return ret;
  225. }
  226. #else
  227. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  228. struct vm_area_struct *vma,
  229. unsigned long address,
  230. unsigned long flags,
  231. unsigned long reason)
  232. {
  233. return false; /* should never get here */
  234. }
  235. #endif /* CONFIG_HUGETLB_PAGE */
  236. /*
  237. * Verify the pagetables are still not ok after having reigstered into
  238. * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
  239. * userfault that has already been resolved, if userfaultfd_read and
  240. * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
  241. * threads.
  242. */
  243. static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
  244. unsigned long address,
  245. unsigned long flags,
  246. unsigned long reason)
  247. {
  248. struct mm_struct *mm = ctx->mm;
  249. pgd_t *pgd;
  250. p4d_t *p4d;
  251. pud_t *pud;
  252. pmd_t *pmd, _pmd;
  253. pte_t *pte;
  254. bool ret = true;
  255. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  256. pgd = pgd_offset(mm, address);
  257. if (!pgd_present(*pgd))
  258. goto out;
  259. p4d = p4d_offset(pgd, address);
  260. if (!p4d_present(*p4d))
  261. goto out;
  262. pud = pud_offset(p4d, address);
  263. if (!pud_present(*pud))
  264. goto out;
  265. pmd = pmd_offset(pud, address);
  266. /*
  267. * READ_ONCE must function as a barrier with narrower scope
  268. * and it must be equivalent to:
  269. * _pmd = *pmd; barrier();
  270. *
  271. * This is to deal with the instability (as in
  272. * pmd_trans_unstable) of the pmd.
  273. */
  274. _pmd = READ_ONCE(*pmd);
  275. if (!pmd_present(_pmd))
  276. goto out;
  277. ret = false;
  278. if (pmd_trans_huge(_pmd))
  279. goto out;
  280. /*
  281. * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
  282. * and use the standard pte_offset_map() instead of parsing _pmd.
  283. */
  284. pte = pte_offset_map(pmd, address);
  285. /*
  286. * Lockless access: we're in a wait_event so it's ok if it
  287. * changes under us.
  288. */
  289. if (pte_none(*pte))
  290. ret = true;
  291. pte_unmap(pte);
  292. out:
  293. return ret;
  294. }
  295. /*
  296. * The locking rules involved in returning VM_FAULT_RETRY depending on
  297. * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
  298. * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
  299. * recommendation in __lock_page_or_retry is not an understatement.
  300. *
  301. * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
  302. * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
  303. * not set.
  304. *
  305. * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
  306. * set, VM_FAULT_RETRY can still be returned if and only if there are
  307. * fatal_signal_pending()s, and the mmap_sem must be released before
  308. * returning it.
  309. */
  310. int handle_userfault(struct vm_fault *vmf, unsigned long reason)
  311. {
  312. struct mm_struct *mm = vmf->vma->vm_mm;
  313. struct userfaultfd_ctx *ctx;
  314. struct userfaultfd_wait_queue uwq;
  315. int ret;
  316. bool must_wait, return_to_userland;
  317. long blocking_state;
  318. ret = VM_FAULT_SIGBUS;
  319. /*
  320. * We don't do userfault handling for the final child pid update.
  321. *
  322. * We also don't do userfault handling during
  323. * coredumping. hugetlbfs has the special
  324. * follow_hugetlb_page() to skip missing pages in the
  325. * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
  326. * the no_page_table() helper in follow_page_mask(), but the
  327. * shmem_vm_ops->fault method is invoked even during
  328. * coredumping without mmap_sem and it ends up here.
  329. */
  330. if (current->flags & (PF_EXITING|PF_DUMPCORE))
  331. goto out;
  332. /*
  333. * Coredumping runs without mmap_sem so we can only check that
  334. * the mmap_sem is held, if PF_DUMPCORE was not set.
  335. */
  336. WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem));
  337. ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
  338. if (!ctx)
  339. goto out;
  340. BUG_ON(ctx->mm != mm);
  341. VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
  342. VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
  343. if (ctx->features & UFFD_FEATURE_SIGBUS)
  344. goto out;
  345. /*
  346. * If it's already released don't get it. This avoids to loop
  347. * in __get_user_pages if userfaultfd_release waits on the
  348. * caller of handle_userfault to release the mmap_sem.
  349. */
  350. if (unlikely(ACCESS_ONCE(ctx->released))) {
  351. /*
  352. * Don't return VM_FAULT_SIGBUS in this case, so a non
  353. * cooperative manager can close the uffd after the
  354. * last UFFDIO_COPY, without risking to trigger an
  355. * involuntary SIGBUS if the process was starting the
  356. * userfaultfd while the userfaultfd was still armed
  357. * (but after the last UFFDIO_COPY). If the uffd
  358. * wasn't already closed when the userfault reached
  359. * this point, that would normally be solved by
  360. * userfaultfd_must_wait returning 'false'.
  361. *
  362. * If we were to return VM_FAULT_SIGBUS here, the non
  363. * cooperative manager would be instead forced to
  364. * always call UFFDIO_UNREGISTER before it can safely
  365. * close the uffd.
  366. */
  367. ret = VM_FAULT_NOPAGE;
  368. goto out;
  369. }
  370. /*
  371. * Check that we can return VM_FAULT_RETRY.
  372. *
  373. * NOTE: it should become possible to return VM_FAULT_RETRY
  374. * even if FAULT_FLAG_TRIED is set without leading to gup()
  375. * -EBUSY failures, if the userfaultfd is to be extended for
  376. * VM_UFFD_WP tracking and we intend to arm the userfault
  377. * without first stopping userland access to the memory. For
  378. * VM_UFFD_MISSING userfaults this is enough for now.
  379. */
  380. if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
  381. /*
  382. * Validate the invariant that nowait must allow retry
  383. * to be sure not to return SIGBUS erroneously on
  384. * nowait invocations.
  385. */
  386. BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
  387. #ifdef CONFIG_DEBUG_VM
  388. if (printk_ratelimit()) {
  389. printk(KERN_WARNING
  390. "FAULT_FLAG_ALLOW_RETRY missing %x\n",
  391. vmf->flags);
  392. dump_stack();
  393. }
  394. #endif
  395. goto out;
  396. }
  397. /*
  398. * Handle nowait, not much to do other than tell it to retry
  399. * and wait.
  400. */
  401. ret = VM_FAULT_RETRY;
  402. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  403. goto out;
  404. /* take the reference before dropping the mmap_sem */
  405. userfaultfd_ctx_get(ctx);
  406. init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
  407. uwq.wq.private = current;
  408. uwq.msg = userfault_msg(vmf->address, vmf->flags, reason,
  409. ctx->features);
  410. uwq.ctx = ctx;
  411. uwq.waken = false;
  412. return_to_userland =
  413. (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
  414. (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
  415. blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
  416. TASK_KILLABLE;
  417. spin_lock(&ctx->fault_pending_wqh.lock);
  418. /*
  419. * After the __add_wait_queue the uwq is visible to userland
  420. * through poll/read().
  421. */
  422. __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
  423. /*
  424. * The smp_mb() after __set_current_state prevents the reads
  425. * following the spin_unlock to happen before the list_add in
  426. * __add_wait_queue.
  427. */
  428. set_current_state(blocking_state);
  429. spin_unlock(&ctx->fault_pending_wqh.lock);
  430. if (!is_vm_hugetlb_page(vmf->vma))
  431. must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
  432. reason);
  433. else
  434. must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
  435. vmf->address,
  436. vmf->flags, reason);
  437. up_read(&mm->mmap_sem);
  438. if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
  439. (return_to_userland ? !signal_pending(current) :
  440. !fatal_signal_pending(current)))) {
  441. wake_up_poll(&ctx->fd_wqh, POLLIN);
  442. schedule();
  443. ret |= VM_FAULT_MAJOR;
  444. /*
  445. * False wakeups can orginate even from rwsem before
  446. * up_read() however userfaults will wait either for a
  447. * targeted wakeup on the specific uwq waitqueue from
  448. * wake_userfault() or for signals or for uffd
  449. * release.
  450. */
  451. while (!READ_ONCE(uwq.waken)) {
  452. /*
  453. * This needs the full smp_store_mb()
  454. * guarantee as the state write must be
  455. * visible to other CPUs before reading
  456. * uwq.waken from other CPUs.
  457. */
  458. set_current_state(blocking_state);
  459. if (READ_ONCE(uwq.waken) ||
  460. READ_ONCE(ctx->released) ||
  461. (return_to_userland ? signal_pending(current) :
  462. fatal_signal_pending(current)))
  463. break;
  464. schedule();
  465. }
  466. }
  467. __set_current_state(TASK_RUNNING);
  468. if (return_to_userland) {
  469. if (signal_pending(current) &&
  470. !fatal_signal_pending(current)) {
  471. /*
  472. * If we got a SIGSTOP or SIGCONT and this is
  473. * a normal userland page fault, just let
  474. * userland return so the signal will be
  475. * handled and gdb debugging works. The page
  476. * fault code immediately after we return from
  477. * this function is going to release the
  478. * mmap_sem and it's not depending on it
  479. * (unlike gup would if we were not to return
  480. * VM_FAULT_RETRY).
  481. *
  482. * If a fatal signal is pending we still take
  483. * the streamlined VM_FAULT_RETRY failure path
  484. * and there's no need to retake the mmap_sem
  485. * in such case.
  486. */
  487. down_read(&mm->mmap_sem);
  488. ret = VM_FAULT_NOPAGE;
  489. }
  490. }
  491. /*
  492. * Here we race with the list_del; list_add in
  493. * userfaultfd_ctx_read(), however because we don't ever run
  494. * list_del_init() to refile across the two lists, the prev
  495. * and next pointers will never point to self. list_add also
  496. * would never let any of the two pointers to point to
  497. * self. So list_empty_careful won't risk to see both pointers
  498. * pointing to self at any time during the list refile. The
  499. * only case where list_del_init() is called is the full
  500. * removal in the wake function and there we don't re-list_add
  501. * and it's fine not to block on the spinlock. The uwq on this
  502. * kernel stack can be released after the list_del_init.
  503. */
  504. if (!list_empty_careful(&uwq.wq.entry)) {
  505. spin_lock(&ctx->fault_pending_wqh.lock);
  506. /*
  507. * No need of list_del_init(), the uwq on the stack
  508. * will be freed shortly anyway.
  509. */
  510. list_del(&uwq.wq.entry);
  511. spin_unlock(&ctx->fault_pending_wqh.lock);
  512. }
  513. /*
  514. * ctx may go away after this if the userfault pseudo fd is
  515. * already released.
  516. */
  517. userfaultfd_ctx_put(ctx);
  518. out:
  519. return ret;
  520. }
  521. static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
  522. struct userfaultfd_wait_queue *ewq)
  523. {
  524. if (WARN_ON_ONCE(current->flags & PF_EXITING))
  525. goto out;
  526. ewq->ctx = ctx;
  527. init_waitqueue_entry(&ewq->wq, current);
  528. spin_lock(&ctx->event_wqh.lock);
  529. /*
  530. * After the __add_wait_queue the uwq is visible to userland
  531. * through poll/read().
  532. */
  533. __add_wait_queue(&ctx->event_wqh, &ewq->wq);
  534. for (;;) {
  535. set_current_state(TASK_KILLABLE);
  536. if (ewq->msg.event == 0)
  537. break;
  538. if (ACCESS_ONCE(ctx->released) ||
  539. fatal_signal_pending(current)) {
  540. __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
  541. if (ewq->msg.event == UFFD_EVENT_FORK) {
  542. struct userfaultfd_ctx *new;
  543. new = (struct userfaultfd_ctx *)
  544. (unsigned long)
  545. ewq->msg.arg.reserved.reserved1;
  546. userfaultfd_ctx_put(new);
  547. }
  548. break;
  549. }
  550. spin_unlock(&ctx->event_wqh.lock);
  551. wake_up_poll(&ctx->fd_wqh, POLLIN);
  552. schedule();
  553. spin_lock(&ctx->event_wqh.lock);
  554. }
  555. __set_current_state(TASK_RUNNING);
  556. spin_unlock(&ctx->event_wqh.lock);
  557. /*
  558. * ctx may go away after this if the userfault pseudo fd is
  559. * already released.
  560. */
  561. out:
  562. userfaultfd_ctx_put(ctx);
  563. }
  564. static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
  565. struct userfaultfd_wait_queue *ewq)
  566. {
  567. ewq->msg.event = 0;
  568. wake_up_locked(&ctx->event_wqh);
  569. __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
  570. }
  571. int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
  572. {
  573. struct userfaultfd_ctx *ctx = NULL, *octx;
  574. struct userfaultfd_fork_ctx *fctx;
  575. octx = vma->vm_userfaultfd_ctx.ctx;
  576. if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
  577. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  578. vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
  579. return 0;
  580. }
  581. list_for_each_entry(fctx, fcs, list)
  582. if (fctx->orig == octx) {
  583. ctx = fctx->new;
  584. break;
  585. }
  586. if (!ctx) {
  587. fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
  588. if (!fctx)
  589. return -ENOMEM;
  590. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  591. if (!ctx) {
  592. kfree(fctx);
  593. return -ENOMEM;
  594. }
  595. atomic_set(&ctx->refcount, 1);
  596. ctx->flags = octx->flags;
  597. ctx->state = UFFD_STATE_RUNNING;
  598. ctx->features = octx->features;
  599. ctx->released = false;
  600. ctx->mm = vma->vm_mm;
  601. atomic_inc(&ctx->mm->mm_count);
  602. userfaultfd_ctx_get(octx);
  603. fctx->orig = octx;
  604. fctx->new = ctx;
  605. list_add_tail(&fctx->list, fcs);
  606. }
  607. vma->vm_userfaultfd_ctx.ctx = ctx;
  608. return 0;
  609. }
  610. static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
  611. {
  612. struct userfaultfd_ctx *ctx = fctx->orig;
  613. struct userfaultfd_wait_queue ewq;
  614. msg_init(&ewq.msg);
  615. ewq.msg.event = UFFD_EVENT_FORK;
  616. ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
  617. userfaultfd_event_wait_completion(ctx, &ewq);
  618. }
  619. void dup_userfaultfd_complete(struct list_head *fcs)
  620. {
  621. struct userfaultfd_fork_ctx *fctx, *n;
  622. list_for_each_entry_safe(fctx, n, fcs, list) {
  623. dup_fctx(fctx);
  624. list_del(&fctx->list);
  625. kfree(fctx);
  626. }
  627. }
  628. void mremap_userfaultfd_prep(struct vm_area_struct *vma,
  629. struct vm_userfaultfd_ctx *vm_ctx)
  630. {
  631. struct userfaultfd_ctx *ctx;
  632. ctx = vma->vm_userfaultfd_ctx.ctx;
  633. if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
  634. vm_ctx->ctx = ctx;
  635. userfaultfd_ctx_get(ctx);
  636. }
  637. }
  638. void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
  639. unsigned long from, unsigned long to,
  640. unsigned long len)
  641. {
  642. struct userfaultfd_ctx *ctx = vm_ctx->ctx;
  643. struct userfaultfd_wait_queue ewq;
  644. if (!ctx)
  645. return;
  646. if (to & ~PAGE_MASK) {
  647. userfaultfd_ctx_put(ctx);
  648. return;
  649. }
  650. msg_init(&ewq.msg);
  651. ewq.msg.event = UFFD_EVENT_REMAP;
  652. ewq.msg.arg.remap.from = from;
  653. ewq.msg.arg.remap.to = to;
  654. ewq.msg.arg.remap.len = len;
  655. userfaultfd_event_wait_completion(ctx, &ewq);
  656. }
  657. bool userfaultfd_remove(struct vm_area_struct *vma,
  658. unsigned long start, unsigned long end)
  659. {
  660. struct mm_struct *mm = vma->vm_mm;
  661. struct userfaultfd_ctx *ctx;
  662. struct userfaultfd_wait_queue ewq;
  663. ctx = vma->vm_userfaultfd_ctx.ctx;
  664. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
  665. return true;
  666. userfaultfd_ctx_get(ctx);
  667. up_read(&mm->mmap_sem);
  668. msg_init(&ewq.msg);
  669. ewq.msg.event = UFFD_EVENT_REMOVE;
  670. ewq.msg.arg.remove.start = start;
  671. ewq.msg.arg.remove.end = end;
  672. userfaultfd_event_wait_completion(ctx, &ewq);
  673. return false;
  674. }
  675. static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
  676. unsigned long start, unsigned long end)
  677. {
  678. struct userfaultfd_unmap_ctx *unmap_ctx;
  679. list_for_each_entry(unmap_ctx, unmaps, list)
  680. if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
  681. unmap_ctx->end == end)
  682. return true;
  683. return false;
  684. }
  685. int userfaultfd_unmap_prep(struct vm_area_struct *vma,
  686. unsigned long start, unsigned long end,
  687. struct list_head *unmaps)
  688. {
  689. for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
  690. struct userfaultfd_unmap_ctx *unmap_ctx;
  691. struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
  692. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
  693. has_unmap_ctx(ctx, unmaps, start, end))
  694. continue;
  695. unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
  696. if (!unmap_ctx)
  697. return -ENOMEM;
  698. userfaultfd_ctx_get(ctx);
  699. unmap_ctx->ctx = ctx;
  700. unmap_ctx->start = start;
  701. unmap_ctx->end = end;
  702. list_add_tail(&unmap_ctx->list, unmaps);
  703. }
  704. return 0;
  705. }
  706. void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
  707. {
  708. struct userfaultfd_unmap_ctx *ctx, *n;
  709. struct userfaultfd_wait_queue ewq;
  710. list_for_each_entry_safe(ctx, n, uf, list) {
  711. msg_init(&ewq.msg);
  712. ewq.msg.event = UFFD_EVENT_UNMAP;
  713. ewq.msg.arg.remove.start = ctx->start;
  714. ewq.msg.arg.remove.end = ctx->end;
  715. userfaultfd_event_wait_completion(ctx->ctx, &ewq);
  716. list_del(&ctx->list);
  717. kfree(ctx);
  718. }
  719. }
  720. static int userfaultfd_release(struct inode *inode, struct file *file)
  721. {
  722. struct userfaultfd_ctx *ctx = file->private_data;
  723. struct mm_struct *mm = ctx->mm;
  724. struct vm_area_struct *vma, *prev;
  725. /* len == 0 means wake all */
  726. struct userfaultfd_wake_range range = { .len = 0, };
  727. unsigned long new_flags;
  728. ACCESS_ONCE(ctx->released) = true;
  729. if (!mmget_not_zero(mm))
  730. goto wakeup;
  731. /*
  732. * Flush page faults out of all CPUs. NOTE: all page faults
  733. * must be retried without returning VM_FAULT_SIGBUS if
  734. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  735. * changes while handle_userfault released the mmap_sem. So
  736. * it's critical that released is set to true (above), before
  737. * taking the mmap_sem for writing.
  738. */
  739. down_write(&mm->mmap_sem);
  740. prev = NULL;
  741. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  742. cond_resched();
  743. BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
  744. !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  745. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  746. prev = vma;
  747. continue;
  748. }
  749. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  750. prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
  751. new_flags, vma->anon_vma,
  752. vma->vm_file, vma->vm_pgoff,
  753. vma_policy(vma),
  754. NULL_VM_UFFD_CTX);
  755. if (prev)
  756. vma = prev;
  757. else
  758. prev = vma;
  759. vma->vm_flags = new_flags;
  760. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  761. }
  762. up_write(&mm->mmap_sem);
  763. mmput(mm);
  764. wakeup:
  765. /*
  766. * After no new page faults can wait on this fault_*wqh, flush
  767. * the last page faults that may have been already waiting on
  768. * the fault_*wqh.
  769. */
  770. spin_lock(&ctx->fault_pending_wqh.lock);
  771. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
  772. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
  773. spin_unlock(&ctx->fault_pending_wqh.lock);
  774. /* Flush pending events that may still wait on event_wqh */
  775. wake_up_all(&ctx->event_wqh);
  776. wake_up_poll(&ctx->fd_wqh, POLLHUP);
  777. userfaultfd_ctx_put(ctx);
  778. return 0;
  779. }
  780. /* fault_pending_wqh.lock must be hold by the caller */
  781. static inline struct userfaultfd_wait_queue *find_userfault_in(
  782. wait_queue_head_t *wqh)
  783. {
  784. wait_queue_entry_t *wq;
  785. struct userfaultfd_wait_queue *uwq;
  786. VM_BUG_ON(!spin_is_locked(&wqh->lock));
  787. uwq = NULL;
  788. if (!waitqueue_active(wqh))
  789. goto out;
  790. /* walk in reverse to provide FIFO behavior to read userfaults */
  791. wq = list_last_entry(&wqh->head, typeof(*wq), entry);
  792. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  793. out:
  794. return uwq;
  795. }
  796. static inline struct userfaultfd_wait_queue *find_userfault(
  797. struct userfaultfd_ctx *ctx)
  798. {
  799. return find_userfault_in(&ctx->fault_pending_wqh);
  800. }
  801. static inline struct userfaultfd_wait_queue *find_userfault_evt(
  802. struct userfaultfd_ctx *ctx)
  803. {
  804. return find_userfault_in(&ctx->event_wqh);
  805. }
  806. static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
  807. {
  808. struct userfaultfd_ctx *ctx = file->private_data;
  809. unsigned int ret;
  810. poll_wait(file, &ctx->fd_wqh, wait);
  811. switch (ctx->state) {
  812. case UFFD_STATE_WAIT_API:
  813. return POLLERR;
  814. case UFFD_STATE_RUNNING:
  815. /*
  816. * poll() never guarantees that read won't block.
  817. * userfaults can be waken before they're read().
  818. */
  819. if (unlikely(!(file->f_flags & O_NONBLOCK)))
  820. return POLLERR;
  821. /*
  822. * lockless access to see if there are pending faults
  823. * __pollwait last action is the add_wait_queue but
  824. * the spin_unlock would allow the waitqueue_active to
  825. * pass above the actual list_add inside
  826. * add_wait_queue critical section. So use a full
  827. * memory barrier to serialize the list_add write of
  828. * add_wait_queue() with the waitqueue_active read
  829. * below.
  830. */
  831. ret = 0;
  832. smp_mb();
  833. if (waitqueue_active(&ctx->fault_pending_wqh))
  834. ret = POLLIN;
  835. else if (waitqueue_active(&ctx->event_wqh))
  836. ret = POLLIN;
  837. return ret;
  838. default:
  839. WARN_ON_ONCE(1);
  840. return POLLERR;
  841. }
  842. }
  843. static const struct file_operations userfaultfd_fops;
  844. static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
  845. struct userfaultfd_ctx *new,
  846. struct uffd_msg *msg)
  847. {
  848. int fd;
  849. struct file *file;
  850. unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
  851. fd = get_unused_fd_flags(flags);
  852. if (fd < 0)
  853. return fd;
  854. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
  855. O_RDWR | flags);
  856. if (IS_ERR(file)) {
  857. put_unused_fd(fd);
  858. return PTR_ERR(file);
  859. }
  860. fd_install(fd, file);
  861. msg->arg.reserved.reserved1 = 0;
  862. msg->arg.fork.ufd = fd;
  863. return 0;
  864. }
  865. static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
  866. struct uffd_msg *msg)
  867. {
  868. ssize_t ret;
  869. DECLARE_WAITQUEUE(wait, current);
  870. struct userfaultfd_wait_queue *uwq;
  871. /*
  872. * Handling fork event requires sleeping operations, so
  873. * we drop the event_wqh lock, then do these ops, then
  874. * lock it back and wake up the waiter. While the lock is
  875. * dropped the ewq may go away so we keep track of it
  876. * carefully.
  877. */
  878. LIST_HEAD(fork_event);
  879. struct userfaultfd_ctx *fork_nctx = NULL;
  880. /* always take the fd_wqh lock before the fault_pending_wqh lock */
  881. spin_lock(&ctx->fd_wqh.lock);
  882. __add_wait_queue(&ctx->fd_wqh, &wait);
  883. for (;;) {
  884. set_current_state(TASK_INTERRUPTIBLE);
  885. spin_lock(&ctx->fault_pending_wqh.lock);
  886. uwq = find_userfault(ctx);
  887. if (uwq) {
  888. /*
  889. * Use a seqcount to repeat the lockless check
  890. * in wake_userfault() to avoid missing
  891. * wakeups because during the refile both
  892. * waitqueue could become empty if this is the
  893. * only userfault.
  894. */
  895. write_seqcount_begin(&ctx->refile_seq);
  896. /*
  897. * The fault_pending_wqh.lock prevents the uwq
  898. * to disappear from under us.
  899. *
  900. * Refile this userfault from
  901. * fault_pending_wqh to fault_wqh, it's not
  902. * pending anymore after we read it.
  903. *
  904. * Use list_del() by hand (as
  905. * userfaultfd_wake_function also uses
  906. * list_del_init() by hand) to be sure nobody
  907. * changes __remove_wait_queue() to use
  908. * list_del_init() in turn breaking the
  909. * !list_empty_careful() check in
  910. * handle_userfault(). The uwq->wq.head list
  911. * must never be empty at any time during the
  912. * refile, or the waitqueue could disappear
  913. * from under us. The "wait_queue_head_t"
  914. * parameter of __remove_wait_queue() is unused
  915. * anyway.
  916. */
  917. list_del(&uwq->wq.entry);
  918. __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
  919. write_seqcount_end(&ctx->refile_seq);
  920. /* careful to always initialize msg if ret == 0 */
  921. *msg = uwq->msg;
  922. spin_unlock(&ctx->fault_pending_wqh.lock);
  923. ret = 0;
  924. break;
  925. }
  926. spin_unlock(&ctx->fault_pending_wqh.lock);
  927. spin_lock(&ctx->event_wqh.lock);
  928. uwq = find_userfault_evt(ctx);
  929. if (uwq) {
  930. *msg = uwq->msg;
  931. if (uwq->msg.event == UFFD_EVENT_FORK) {
  932. fork_nctx = (struct userfaultfd_ctx *)
  933. (unsigned long)
  934. uwq->msg.arg.reserved.reserved1;
  935. list_move(&uwq->wq.entry, &fork_event);
  936. spin_unlock(&ctx->event_wqh.lock);
  937. ret = 0;
  938. break;
  939. }
  940. userfaultfd_event_complete(ctx, uwq);
  941. spin_unlock(&ctx->event_wqh.lock);
  942. ret = 0;
  943. break;
  944. }
  945. spin_unlock(&ctx->event_wqh.lock);
  946. if (signal_pending(current)) {
  947. ret = -ERESTARTSYS;
  948. break;
  949. }
  950. if (no_wait) {
  951. ret = -EAGAIN;
  952. break;
  953. }
  954. spin_unlock(&ctx->fd_wqh.lock);
  955. schedule();
  956. spin_lock(&ctx->fd_wqh.lock);
  957. }
  958. __remove_wait_queue(&ctx->fd_wqh, &wait);
  959. __set_current_state(TASK_RUNNING);
  960. spin_unlock(&ctx->fd_wqh.lock);
  961. if (!ret && msg->event == UFFD_EVENT_FORK) {
  962. ret = resolve_userfault_fork(ctx, fork_nctx, msg);
  963. if (!ret) {
  964. spin_lock(&ctx->event_wqh.lock);
  965. if (!list_empty(&fork_event)) {
  966. uwq = list_first_entry(&fork_event,
  967. typeof(*uwq),
  968. wq.entry);
  969. list_del(&uwq->wq.entry);
  970. __add_wait_queue(&ctx->event_wqh, &uwq->wq);
  971. userfaultfd_event_complete(ctx, uwq);
  972. }
  973. spin_unlock(&ctx->event_wqh.lock);
  974. }
  975. }
  976. return ret;
  977. }
  978. static ssize_t userfaultfd_read(struct file *file, char __user *buf,
  979. size_t count, loff_t *ppos)
  980. {
  981. struct userfaultfd_ctx *ctx = file->private_data;
  982. ssize_t _ret, ret = 0;
  983. struct uffd_msg msg;
  984. int no_wait = file->f_flags & O_NONBLOCK;
  985. if (ctx->state == UFFD_STATE_WAIT_API)
  986. return -EINVAL;
  987. for (;;) {
  988. if (count < sizeof(msg))
  989. return ret ? ret : -EINVAL;
  990. _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
  991. if (_ret < 0)
  992. return ret ? ret : _ret;
  993. if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
  994. return ret ? ret : -EFAULT;
  995. ret += sizeof(msg);
  996. buf += sizeof(msg);
  997. count -= sizeof(msg);
  998. /*
  999. * Allow to read more than one fault at time but only
  1000. * block if waiting for the very first one.
  1001. */
  1002. no_wait = O_NONBLOCK;
  1003. }
  1004. }
  1005. static void __wake_userfault(struct userfaultfd_ctx *ctx,
  1006. struct userfaultfd_wake_range *range)
  1007. {
  1008. spin_lock(&ctx->fault_pending_wqh.lock);
  1009. /* wake all in the range and autoremove */
  1010. if (waitqueue_active(&ctx->fault_pending_wqh))
  1011. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
  1012. range);
  1013. if (waitqueue_active(&ctx->fault_wqh))
  1014. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
  1015. spin_unlock(&ctx->fault_pending_wqh.lock);
  1016. }
  1017. static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
  1018. struct userfaultfd_wake_range *range)
  1019. {
  1020. unsigned seq;
  1021. bool need_wakeup;
  1022. /*
  1023. * To be sure waitqueue_active() is not reordered by the CPU
  1024. * before the pagetable update, use an explicit SMP memory
  1025. * barrier here. PT lock release or up_read(mmap_sem) still
  1026. * have release semantics that can allow the
  1027. * waitqueue_active() to be reordered before the pte update.
  1028. */
  1029. smp_mb();
  1030. /*
  1031. * Use waitqueue_active because it's very frequent to
  1032. * change the address space atomically even if there are no
  1033. * userfaults yet. So we take the spinlock only when we're
  1034. * sure we've userfaults to wake.
  1035. */
  1036. do {
  1037. seq = read_seqcount_begin(&ctx->refile_seq);
  1038. need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
  1039. waitqueue_active(&ctx->fault_wqh);
  1040. cond_resched();
  1041. } while (read_seqcount_retry(&ctx->refile_seq, seq));
  1042. if (need_wakeup)
  1043. __wake_userfault(ctx, range);
  1044. }
  1045. static __always_inline int validate_range(struct mm_struct *mm,
  1046. __u64 start, __u64 len)
  1047. {
  1048. __u64 task_size = mm->task_size;
  1049. if (start & ~PAGE_MASK)
  1050. return -EINVAL;
  1051. if (len & ~PAGE_MASK)
  1052. return -EINVAL;
  1053. if (!len)
  1054. return -EINVAL;
  1055. if (start < mmap_min_addr)
  1056. return -EINVAL;
  1057. if (start >= task_size)
  1058. return -EINVAL;
  1059. if (len > task_size - start)
  1060. return -EINVAL;
  1061. return 0;
  1062. }
  1063. static inline bool vma_can_userfault(struct vm_area_struct *vma)
  1064. {
  1065. return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
  1066. vma_is_shmem(vma);
  1067. }
  1068. static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  1069. unsigned long arg)
  1070. {
  1071. struct mm_struct *mm = ctx->mm;
  1072. struct vm_area_struct *vma, *prev, *cur;
  1073. int ret;
  1074. struct uffdio_register uffdio_register;
  1075. struct uffdio_register __user *user_uffdio_register;
  1076. unsigned long vm_flags, new_flags;
  1077. bool found;
  1078. bool basic_ioctls;
  1079. unsigned long start, end, vma_end;
  1080. user_uffdio_register = (struct uffdio_register __user *) arg;
  1081. ret = -EFAULT;
  1082. if (copy_from_user(&uffdio_register, user_uffdio_register,
  1083. sizeof(uffdio_register)-sizeof(__u64)))
  1084. goto out;
  1085. ret = -EINVAL;
  1086. if (!uffdio_register.mode)
  1087. goto out;
  1088. if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
  1089. UFFDIO_REGISTER_MODE_WP))
  1090. goto out;
  1091. vm_flags = 0;
  1092. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  1093. vm_flags |= VM_UFFD_MISSING;
  1094. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
  1095. vm_flags |= VM_UFFD_WP;
  1096. /*
  1097. * FIXME: remove the below error constraint by
  1098. * implementing the wprotect tracking mode.
  1099. */
  1100. ret = -EINVAL;
  1101. goto out;
  1102. }
  1103. ret = validate_range(mm, uffdio_register.range.start,
  1104. uffdio_register.range.len);
  1105. if (ret)
  1106. goto out;
  1107. start = uffdio_register.range.start;
  1108. end = start + uffdio_register.range.len;
  1109. ret = -ENOMEM;
  1110. if (!mmget_not_zero(mm))
  1111. goto out;
  1112. down_write(&mm->mmap_sem);
  1113. vma = find_vma_prev(mm, start, &prev);
  1114. if (!vma)
  1115. goto out_unlock;
  1116. /* check that there's at least one vma in the range */
  1117. ret = -EINVAL;
  1118. if (vma->vm_start >= end)
  1119. goto out_unlock;
  1120. /*
  1121. * If the first vma contains huge pages, make sure start address
  1122. * is aligned to huge page size.
  1123. */
  1124. if (is_vm_hugetlb_page(vma)) {
  1125. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1126. if (start & (vma_hpagesize - 1))
  1127. goto out_unlock;
  1128. }
  1129. /*
  1130. * Search for not compatible vmas.
  1131. */
  1132. found = false;
  1133. basic_ioctls = false;
  1134. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1135. cond_resched();
  1136. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1137. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1138. /* check not compatible vmas */
  1139. ret = -EINVAL;
  1140. if (!vma_can_userfault(cur))
  1141. goto out_unlock;
  1142. /*
  1143. * If this vma contains ending address, and huge pages
  1144. * check alignment.
  1145. */
  1146. if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
  1147. end > cur->vm_start) {
  1148. unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
  1149. ret = -EINVAL;
  1150. if (end & (vma_hpagesize - 1))
  1151. goto out_unlock;
  1152. }
  1153. /*
  1154. * Check that this vma isn't already owned by a
  1155. * different userfaultfd. We can't allow more than one
  1156. * userfaultfd to own a single vma simultaneously or we
  1157. * wouldn't know which one to deliver the userfaults to.
  1158. */
  1159. ret = -EBUSY;
  1160. if (cur->vm_userfaultfd_ctx.ctx &&
  1161. cur->vm_userfaultfd_ctx.ctx != ctx)
  1162. goto out_unlock;
  1163. /*
  1164. * Note vmas containing huge pages
  1165. */
  1166. if (is_vm_hugetlb_page(cur))
  1167. basic_ioctls = true;
  1168. found = true;
  1169. }
  1170. BUG_ON(!found);
  1171. if (vma->vm_start < start)
  1172. prev = vma;
  1173. ret = 0;
  1174. do {
  1175. cond_resched();
  1176. BUG_ON(!vma_can_userfault(vma));
  1177. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  1178. vma->vm_userfaultfd_ctx.ctx != ctx);
  1179. /*
  1180. * Nothing to do: this vma is already registered into this
  1181. * userfaultfd and with the right tracking mode too.
  1182. */
  1183. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  1184. (vma->vm_flags & vm_flags) == vm_flags)
  1185. goto skip;
  1186. if (vma->vm_start > start)
  1187. start = vma->vm_start;
  1188. vma_end = min(end, vma->vm_end);
  1189. new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
  1190. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1191. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1192. vma_policy(vma),
  1193. ((struct vm_userfaultfd_ctx){ ctx }));
  1194. if (prev) {
  1195. vma = prev;
  1196. goto next;
  1197. }
  1198. if (vma->vm_start < start) {
  1199. ret = split_vma(mm, vma, start, 1);
  1200. if (ret)
  1201. break;
  1202. }
  1203. if (vma->vm_end > end) {
  1204. ret = split_vma(mm, vma, end, 0);
  1205. if (ret)
  1206. break;
  1207. }
  1208. next:
  1209. /*
  1210. * In the vma_merge() successful mprotect-like case 8:
  1211. * the next vma was merged into the current one and
  1212. * the current one has not been updated yet.
  1213. */
  1214. vma->vm_flags = new_flags;
  1215. vma->vm_userfaultfd_ctx.ctx = ctx;
  1216. skip:
  1217. prev = vma;
  1218. start = vma->vm_end;
  1219. vma = vma->vm_next;
  1220. } while (vma && vma->vm_start < end);
  1221. out_unlock:
  1222. up_write(&mm->mmap_sem);
  1223. mmput(mm);
  1224. if (!ret) {
  1225. /*
  1226. * Now that we scanned all vmas we can already tell
  1227. * userland which ioctls methods are guaranteed to
  1228. * succeed on this range.
  1229. */
  1230. if (put_user(basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
  1231. UFFD_API_RANGE_IOCTLS,
  1232. &user_uffdio_register->ioctls))
  1233. ret = -EFAULT;
  1234. }
  1235. out:
  1236. return ret;
  1237. }
  1238. static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
  1239. unsigned long arg)
  1240. {
  1241. struct mm_struct *mm = ctx->mm;
  1242. struct vm_area_struct *vma, *prev, *cur;
  1243. int ret;
  1244. struct uffdio_range uffdio_unregister;
  1245. unsigned long new_flags;
  1246. bool found;
  1247. unsigned long start, end, vma_end;
  1248. const void __user *buf = (void __user *)arg;
  1249. ret = -EFAULT;
  1250. if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
  1251. goto out;
  1252. ret = validate_range(mm, uffdio_unregister.start,
  1253. uffdio_unregister.len);
  1254. if (ret)
  1255. goto out;
  1256. start = uffdio_unregister.start;
  1257. end = start + uffdio_unregister.len;
  1258. ret = -ENOMEM;
  1259. if (!mmget_not_zero(mm))
  1260. goto out;
  1261. down_write(&mm->mmap_sem);
  1262. vma = find_vma_prev(mm, start, &prev);
  1263. if (!vma)
  1264. goto out_unlock;
  1265. /* check that there's at least one vma in the range */
  1266. ret = -EINVAL;
  1267. if (vma->vm_start >= end)
  1268. goto out_unlock;
  1269. /*
  1270. * If the first vma contains huge pages, make sure start address
  1271. * is aligned to huge page size.
  1272. */
  1273. if (is_vm_hugetlb_page(vma)) {
  1274. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1275. if (start & (vma_hpagesize - 1))
  1276. goto out_unlock;
  1277. }
  1278. /*
  1279. * Search for not compatible vmas.
  1280. */
  1281. found = false;
  1282. ret = -EINVAL;
  1283. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1284. cond_resched();
  1285. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1286. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1287. /*
  1288. * Check not compatible vmas, not strictly required
  1289. * here as not compatible vmas cannot have an
  1290. * userfaultfd_ctx registered on them, but this
  1291. * provides for more strict behavior to notice
  1292. * unregistration errors.
  1293. */
  1294. if (!vma_can_userfault(cur))
  1295. goto out_unlock;
  1296. found = true;
  1297. }
  1298. BUG_ON(!found);
  1299. if (vma->vm_start < start)
  1300. prev = vma;
  1301. ret = 0;
  1302. do {
  1303. cond_resched();
  1304. BUG_ON(!vma_can_userfault(vma));
  1305. /*
  1306. * Nothing to do: this vma is already registered into this
  1307. * userfaultfd and with the right tracking mode too.
  1308. */
  1309. if (!vma->vm_userfaultfd_ctx.ctx)
  1310. goto skip;
  1311. if (vma->vm_start > start)
  1312. start = vma->vm_start;
  1313. vma_end = min(end, vma->vm_end);
  1314. if (userfaultfd_missing(vma)) {
  1315. /*
  1316. * Wake any concurrent pending userfault while
  1317. * we unregister, so they will not hang
  1318. * permanently and it avoids userland to call
  1319. * UFFDIO_WAKE explicitly.
  1320. */
  1321. struct userfaultfd_wake_range range;
  1322. range.start = start;
  1323. range.len = vma_end - start;
  1324. wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
  1325. }
  1326. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  1327. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1328. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1329. vma_policy(vma),
  1330. NULL_VM_UFFD_CTX);
  1331. if (prev) {
  1332. vma = prev;
  1333. goto next;
  1334. }
  1335. if (vma->vm_start < start) {
  1336. ret = split_vma(mm, vma, start, 1);
  1337. if (ret)
  1338. break;
  1339. }
  1340. if (vma->vm_end > end) {
  1341. ret = split_vma(mm, vma, end, 0);
  1342. if (ret)
  1343. break;
  1344. }
  1345. next:
  1346. /*
  1347. * In the vma_merge() successful mprotect-like case 8:
  1348. * the next vma was merged into the current one and
  1349. * the current one has not been updated yet.
  1350. */
  1351. vma->vm_flags = new_flags;
  1352. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  1353. skip:
  1354. prev = vma;
  1355. start = vma->vm_end;
  1356. vma = vma->vm_next;
  1357. } while (vma && vma->vm_start < end);
  1358. out_unlock:
  1359. up_write(&mm->mmap_sem);
  1360. mmput(mm);
  1361. out:
  1362. return ret;
  1363. }
  1364. /*
  1365. * userfaultfd_wake may be used in combination with the
  1366. * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
  1367. */
  1368. static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
  1369. unsigned long arg)
  1370. {
  1371. int ret;
  1372. struct uffdio_range uffdio_wake;
  1373. struct userfaultfd_wake_range range;
  1374. const void __user *buf = (void __user *)arg;
  1375. ret = -EFAULT;
  1376. if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
  1377. goto out;
  1378. ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
  1379. if (ret)
  1380. goto out;
  1381. range.start = uffdio_wake.start;
  1382. range.len = uffdio_wake.len;
  1383. /*
  1384. * len == 0 means wake all and we don't want to wake all here,
  1385. * so check it again to be sure.
  1386. */
  1387. VM_BUG_ON(!range.len);
  1388. wake_userfault(ctx, &range);
  1389. ret = 0;
  1390. out:
  1391. return ret;
  1392. }
  1393. static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
  1394. unsigned long arg)
  1395. {
  1396. __s64 ret;
  1397. struct uffdio_copy uffdio_copy;
  1398. struct uffdio_copy __user *user_uffdio_copy;
  1399. struct userfaultfd_wake_range range;
  1400. user_uffdio_copy = (struct uffdio_copy __user *) arg;
  1401. ret = -EFAULT;
  1402. if (copy_from_user(&uffdio_copy, user_uffdio_copy,
  1403. /* don't copy "copy" last field */
  1404. sizeof(uffdio_copy)-sizeof(__s64)))
  1405. goto out;
  1406. ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
  1407. if (ret)
  1408. goto out;
  1409. /*
  1410. * double check for wraparound just in case. copy_from_user()
  1411. * will later check uffdio_copy.src + uffdio_copy.len to fit
  1412. * in the userland range.
  1413. */
  1414. ret = -EINVAL;
  1415. if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
  1416. goto out;
  1417. if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
  1418. goto out;
  1419. if (mmget_not_zero(ctx->mm)) {
  1420. ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
  1421. uffdio_copy.len);
  1422. mmput(ctx->mm);
  1423. } else {
  1424. return -ESRCH;
  1425. }
  1426. if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
  1427. return -EFAULT;
  1428. if (ret < 0)
  1429. goto out;
  1430. BUG_ON(!ret);
  1431. /* len == 0 would wake all */
  1432. range.len = ret;
  1433. if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
  1434. range.start = uffdio_copy.dst;
  1435. wake_userfault(ctx, &range);
  1436. }
  1437. ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
  1438. out:
  1439. return ret;
  1440. }
  1441. static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
  1442. unsigned long arg)
  1443. {
  1444. __s64 ret;
  1445. struct uffdio_zeropage uffdio_zeropage;
  1446. struct uffdio_zeropage __user *user_uffdio_zeropage;
  1447. struct userfaultfd_wake_range range;
  1448. user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
  1449. ret = -EFAULT;
  1450. if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
  1451. /* don't copy "zeropage" last field */
  1452. sizeof(uffdio_zeropage)-sizeof(__s64)))
  1453. goto out;
  1454. ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
  1455. uffdio_zeropage.range.len);
  1456. if (ret)
  1457. goto out;
  1458. ret = -EINVAL;
  1459. if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
  1460. goto out;
  1461. if (mmget_not_zero(ctx->mm)) {
  1462. ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
  1463. uffdio_zeropage.range.len);
  1464. mmput(ctx->mm);
  1465. } else {
  1466. return -ESRCH;
  1467. }
  1468. if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
  1469. return -EFAULT;
  1470. if (ret < 0)
  1471. goto out;
  1472. /* len == 0 would wake all */
  1473. BUG_ON(!ret);
  1474. range.len = ret;
  1475. if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
  1476. range.start = uffdio_zeropage.range.start;
  1477. wake_userfault(ctx, &range);
  1478. }
  1479. ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
  1480. out:
  1481. return ret;
  1482. }
  1483. static inline unsigned int uffd_ctx_features(__u64 user_features)
  1484. {
  1485. /*
  1486. * For the current set of features the bits just coincide
  1487. */
  1488. return (unsigned int)user_features;
  1489. }
  1490. /*
  1491. * userland asks for a certain API version and we return which bits
  1492. * and ioctl commands are implemented in this kernel for such API
  1493. * version or -EINVAL if unknown.
  1494. */
  1495. static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  1496. unsigned long arg)
  1497. {
  1498. struct uffdio_api uffdio_api;
  1499. void __user *buf = (void __user *)arg;
  1500. int ret;
  1501. __u64 features;
  1502. ret = -EINVAL;
  1503. if (ctx->state != UFFD_STATE_WAIT_API)
  1504. goto out;
  1505. ret = -EFAULT;
  1506. if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
  1507. goto out;
  1508. features = uffdio_api.features;
  1509. if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
  1510. memset(&uffdio_api, 0, sizeof(uffdio_api));
  1511. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1512. goto out;
  1513. ret = -EINVAL;
  1514. goto out;
  1515. }
  1516. /* report all available features and ioctls to userland */
  1517. uffdio_api.features = UFFD_API_FEATURES;
  1518. uffdio_api.ioctls = UFFD_API_IOCTLS;
  1519. ret = -EFAULT;
  1520. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1521. goto out;
  1522. ctx->state = UFFD_STATE_RUNNING;
  1523. /* only enable the requested features for this uffd context */
  1524. ctx->features = uffd_ctx_features(features);
  1525. ret = 0;
  1526. out:
  1527. return ret;
  1528. }
  1529. static long userfaultfd_ioctl(struct file *file, unsigned cmd,
  1530. unsigned long arg)
  1531. {
  1532. int ret = -EINVAL;
  1533. struct userfaultfd_ctx *ctx = file->private_data;
  1534. if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
  1535. return -EINVAL;
  1536. switch(cmd) {
  1537. case UFFDIO_API:
  1538. ret = userfaultfd_api(ctx, arg);
  1539. break;
  1540. case UFFDIO_REGISTER:
  1541. ret = userfaultfd_register(ctx, arg);
  1542. break;
  1543. case UFFDIO_UNREGISTER:
  1544. ret = userfaultfd_unregister(ctx, arg);
  1545. break;
  1546. case UFFDIO_WAKE:
  1547. ret = userfaultfd_wake(ctx, arg);
  1548. break;
  1549. case UFFDIO_COPY:
  1550. ret = userfaultfd_copy(ctx, arg);
  1551. break;
  1552. case UFFDIO_ZEROPAGE:
  1553. ret = userfaultfd_zeropage(ctx, arg);
  1554. break;
  1555. }
  1556. return ret;
  1557. }
  1558. #ifdef CONFIG_PROC_FS
  1559. static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
  1560. {
  1561. struct userfaultfd_ctx *ctx = f->private_data;
  1562. wait_queue_entry_t *wq;
  1563. struct userfaultfd_wait_queue *uwq;
  1564. unsigned long pending = 0, total = 0;
  1565. spin_lock(&ctx->fault_pending_wqh.lock);
  1566. list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
  1567. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1568. pending++;
  1569. total++;
  1570. }
  1571. list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
  1572. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1573. total++;
  1574. }
  1575. spin_unlock(&ctx->fault_pending_wqh.lock);
  1576. /*
  1577. * If more protocols will be added, there will be all shown
  1578. * separated by a space. Like this:
  1579. * protocols: aa:... bb:...
  1580. */
  1581. seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
  1582. pending, total, UFFD_API, ctx->features,
  1583. UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
  1584. }
  1585. #endif
  1586. static const struct file_operations userfaultfd_fops = {
  1587. #ifdef CONFIG_PROC_FS
  1588. .show_fdinfo = userfaultfd_show_fdinfo,
  1589. #endif
  1590. .release = userfaultfd_release,
  1591. .poll = userfaultfd_poll,
  1592. .read = userfaultfd_read,
  1593. .unlocked_ioctl = userfaultfd_ioctl,
  1594. .compat_ioctl = userfaultfd_ioctl,
  1595. .llseek = noop_llseek,
  1596. };
  1597. static void init_once_userfaultfd_ctx(void *mem)
  1598. {
  1599. struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
  1600. init_waitqueue_head(&ctx->fault_pending_wqh);
  1601. init_waitqueue_head(&ctx->fault_wqh);
  1602. init_waitqueue_head(&ctx->event_wqh);
  1603. init_waitqueue_head(&ctx->fd_wqh);
  1604. seqcount_init(&ctx->refile_seq);
  1605. }
  1606. /**
  1607. * userfaultfd_file_create - Creates a userfaultfd file pointer.
  1608. * @flags: Flags for the userfaultfd file.
  1609. *
  1610. * This function creates a userfaultfd file pointer, w/out installing
  1611. * it into the fd table. This is useful when the userfaultfd file is
  1612. * used during the initialization of data structures that require
  1613. * extra setup after the userfaultfd creation. So the userfaultfd
  1614. * creation is split into the file pointer creation phase, and the
  1615. * file descriptor installation phase. In this way races with
  1616. * userspace closing the newly installed file descriptor can be
  1617. * avoided. Returns a userfaultfd file pointer, or a proper error
  1618. * pointer.
  1619. */
  1620. static struct file *userfaultfd_file_create(int flags)
  1621. {
  1622. struct file *file;
  1623. struct userfaultfd_ctx *ctx;
  1624. BUG_ON(!current->mm);
  1625. /* Check the UFFD_* constants for consistency. */
  1626. BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
  1627. BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
  1628. file = ERR_PTR(-EINVAL);
  1629. if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
  1630. goto out;
  1631. file = ERR_PTR(-ENOMEM);
  1632. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  1633. if (!ctx)
  1634. goto out;
  1635. atomic_set(&ctx->refcount, 1);
  1636. ctx->flags = flags;
  1637. ctx->features = 0;
  1638. ctx->state = UFFD_STATE_WAIT_API;
  1639. ctx->released = false;
  1640. ctx->mm = current->mm;
  1641. /* prevent the mm struct to be freed */
  1642. mmgrab(ctx->mm);
  1643. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
  1644. O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
  1645. if (IS_ERR(file)) {
  1646. mmdrop(ctx->mm);
  1647. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  1648. }
  1649. out:
  1650. return file;
  1651. }
  1652. SYSCALL_DEFINE1(userfaultfd, int, flags)
  1653. {
  1654. int fd, error;
  1655. struct file *file;
  1656. error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
  1657. if (error < 0)
  1658. return error;
  1659. fd = error;
  1660. file = userfaultfd_file_create(flags);
  1661. if (IS_ERR(file)) {
  1662. error = PTR_ERR(file);
  1663. goto err_put_unused_fd;
  1664. }
  1665. fd_install(fd, file);
  1666. return fd;
  1667. err_put_unused_fd:
  1668. put_unused_fd(fd);
  1669. return error;
  1670. }
  1671. static int __init userfaultfd_init(void)
  1672. {
  1673. userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
  1674. sizeof(struct userfaultfd_ctx),
  1675. 0,
  1676. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1677. init_once_userfaultfd_ctx);
  1678. return 0;
  1679. }
  1680. __initcall(userfaultfd_init);