userfaultfd.c 48 KB

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