userfaultfd.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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.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_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_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->task_list);
  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. * Returns: In case of success, returns not zero.
  132. */
  133. static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
  134. {
  135. if (!atomic_inc_not_zero(&ctx->refcount))
  136. BUG();
  137. }
  138. /**
  139. * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
  140. * context.
  141. * @ctx: [in] Pointer to userfaultfd context.
  142. *
  143. * The userfaultfd context reference must have been previously acquired either
  144. * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
  145. */
  146. static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
  147. {
  148. if (atomic_dec_and_test(&ctx->refcount)) {
  149. VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
  150. VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
  151. VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
  152. VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
  153. VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
  154. VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
  155. VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
  156. VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
  157. mmdrop(ctx->mm);
  158. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  159. }
  160. }
  161. static inline void msg_init(struct uffd_msg *msg)
  162. {
  163. BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
  164. /*
  165. * Must use memset to zero out the paddings or kernel data is
  166. * leaked to userland.
  167. */
  168. memset(msg, 0, sizeof(struct uffd_msg));
  169. }
  170. static inline struct uffd_msg userfault_msg(unsigned long address,
  171. unsigned int flags,
  172. unsigned long reason)
  173. {
  174. struct uffd_msg msg;
  175. msg_init(&msg);
  176. msg.event = UFFD_EVENT_PAGEFAULT;
  177. msg.arg.pagefault.address = address;
  178. if (flags & FAULT_FLAG_WRITE)
  179. /*
  180. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  181. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
  182. * was not set in a UFFD_EVENT_PAGEFAULT, it means it
  183. * was a read fault, otherwise if set it means it's
  184. * a write fault.
  185. */
  186. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
  187. if (reason & VM_UFFD_WP)
  188. /*
  189. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  190. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
  191. * not set in a UFFD_EVENT_PAGEFAULT, it means it was
  192. * a missing fault, otherwise if set it means it's a
  193. * write protect fault.
  194. */
  195. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
  196. return msg;
  197. }
  198. #ifdef CONFIG_HUGETLB_PAGE
  199. /*
  200. * Same functionality as userfaultfd_must_wait below with modifications for
  201. * hugepmd ranges.
  202. */
  203. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  204. unsigned long address,
  205. unsigned long flags,
  206. unsigned long reason)
  207. {
  208. struct mm_struct *mm = ctx->mm;
  209. pte_t *pte;
  210. bool ret = true;
  211. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  212. pte = huge_pte_offset(mm, address);
  213. if (!pte)
  214. goto out;
  215. ret = false;
  216. /*
  217. * Lockless access: we're in a wait_event so it's ok if it
  218. * changes under us.
  219. */
  220. if (huge_pte_none(*pte))
  221. ret = true;
  222. if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
  223. ret = true;
  224. out:
  225. return ret;
  226. }
  227. #else
  228. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  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. pud_t *pud;
  251. pmd_t *pmd, _pmd;
  252. pte_t *pte;
  253. bool ret = true;
  254. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  255. pgd = pgd_offset(mm, address);
  256. if (!pgd_present(*pgd))
  257. goto out;
  258. pud = pud_offset(pgd, address);
  259. if (!pud_present(*pud))
  260. goto out;
  261. pmd = pmd_offset(pud, address);
  262. /*
  263. * READ_ONCE must function as a barrier with narrower scope
  264. * and it must be equivalent to:
  265. * _pmd = *pmd; barrier();
  266. *
  267. * This is to deal with the instability (as in
  268. * pmd_trans_unstable) of the pmd.
  269. */
  270. _pmd = READ_ONCE(*pmd);
  271. if (!pmd_present(_pmd))
  272. goto out;
  273. ret = false;
  274. if (pmd_trans_huge(_pmd))
  275. goto out;
  276. /*
  277. * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
  278. * and use the standard pte_offset_map() instead of parsing _pmd.
  279. */
  280. pte = pte_offset_map(pmd, address);
  281. /*
  282. * Lockless access: we're in a wait_event so it's ok if it
  283. * changes under us.
  284. */
  285. if (pte_none(*pte))
  286. ret = true;
  287. pte_unmap(pte);
  288. out:
  289. return ret;
  290. }
  291. /*
  292. * The locking rules involved in returning VM_FAULT_RETRY depending on
  293. * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
  294. * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
  295. * recommendation in __lock_page_or_retry is not an understatement.
  296. *
  297. * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
  298. * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
  299. * not set.
  300. *
  301. * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
  302. * set, VM_FAULT_RETRY can still be returned if and only if there are
  303. * fatal_signal_pending()s, and the mmap_sem must be released before
  304. * returning it.
  305. */
  306. int handle_userfault(struct vm_fault *vmf, unsigned long reason)
  307. {
  308. struct mm_struct *mm = vmf->vma->vm_mm;
  309. struct userfaultfd_ctx *ctx;
  310. struct userfaultfd_wait_queue uwq;
  311. int ret;
  312. bool must_wait, return_to_userland;
  313. long blocking_state;
  314. BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  315. ret = VM_FAULT_SIGBUS;
  316. ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
  317. if (!ctx)
  318. goto out;
  319. BUG_ON(ctx->mm != mm);
  320. VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
  321. VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
  322. /*
  323. * If it's already released don't get it. This avoids to loop
  324. * in __get_user_pages if userfaultfd_release waits on the
  325. * caller of handle_userfault to release the mmap_sem.
  326. */
  327. if (unlikely(ACCESS_ONCE(ctx->released)))
  328. goto out;
  329. /*
  330. * We don't do userfault handling for the final child pid update.
  331. */
  332. if (current->flags & PF_EXITING)
  333. goto out;
  334. /*
  335. * Check that we can return VM_FAULT_RETRY.
  336. *
  337. * NOTE: it should become possible to return VM_FAULT_RETRY
  338. * even if FAULT_FLAG_TRIED is set without leading to gup()
  339. * -EBUSY failures, if the userfaultfd is to be extended for
  340. * VM_UFFD_WP tracking and we intend to arm the userfault
  341. * without first stopping userland access to the memory. For
  342. * VM_UFFD_MISSING userfaults this is enough for now.
  343. */
  344. if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
  345. /*
  346. * Validate the invariant that nowait must allow retry
  347. * to be sure not to return SIGBUS erroneously on
  348. * nowait invocations.
  349. */
  350. BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
  351. #ifdef CONFIG_DEBUG_VM
  352. if (printk_ratelimit()) {
  353. printk(KERN_WARNING
  354. "FAULT_FLAG_ALLOW_RETRY missing %x\n",
  355. vmf->flags);
  356. dump_stack();
  357. }
  358. #endif
  359. goto out;
  360. }
  361. /*
  362. * Handle nowait, not much to do other than tell it to retry
  363. * and wait.
  364. */
  365. ret = VM_FAULT_RETRY;
  366. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  367. goto out;
  368. /* take the reference before dropping the mmap_sem */
  369. userfaultfd_ctx_get(ctx);
  370. init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
  371. uwq.wq.private = current;
  372. uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
  373. uwq.ctx = ctx;
  374. uwq.waken = false;
  375. return_to_userland =
  376. (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
  377. (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
  378. blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
  379. TASK_KILLABLE;
  380. spin_lock(&ctx->fault_pending_wqh.lock);
  381. /*
  382. * After the __add_wait_queue the uwq is visible to userland
  383. * through poll/read().
  384. */
  385. __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
  386. /*
  387. * The smp_mb() after __set_current_state prevents the reads
  388. * following the spin_unlock to happen before the list_add in
  389. * __add_wait_queue.
  390. */
  391. set_current_state(blocking_state);
  392. spin_unlock(&ctx->fault_pending_wqh.lock);
  393. if (!is_vm_hugetlb_page(vmf->vma))
  394. must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
  395. reason);
  396. else
  397. must_wait = userfaultfd_huge_must_wait(ctx, vmf->address,
  398. vmf->flags, reason);
  399. up_read(&mm->mmap_sem);
  400. if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
  401. (return_to_userland ? !signal_pending(current) :
  402. !fatal_signal_pending(current)))) {
  403. wake_up_poll(&ctx->fd_wqh, POLLIN);
  404. schedule();
  405. ret |= VM_FAULT_MAJOR;
  406. /*
  407. * False wakeups can orginate even from rwsem before
  408. * up_read() however userfaults will wait either for a
  409. * targeted wakeup on the specific uwq waitqueue from
  410. * wake_userfault() or for signals or for uffd
  411. * release.
  412. */
  413. while (!READ_ONCE(uwq.waken)) {
  414. /*
  415. * This needs the full smp_store_mb()
  416. * guarantee as the state write must be
  417. * visible to other CPUs before reading
  418. * uwq.waken from other CPUs.
  419. */
  420. set_current_state(blocking_state);
  421. if (READ_ONCE(uwq.waken) ||
  422. READ_ONCE(ctx->released) ||
  423. (return_to_userland ? signal_pending(current) :
  424. fatal_signal_pending(current)))
  425. break;
  426. schedule();
  427. }
  428. }
  429. __set_current_state(TASK_RUNNING);
  430. if (return_to_userland) {
  431. if (signal_pending(current) &&
  432. !fatal_signal_pending(current)) {
  433. /*
  434. * If we got a SIGSTOP or SIGCONT and this is
  435. * a normal userland page fault, just let
  436. * userland return so the signal will be
  437. * handled and gdb debugging works. The page
  438. * fault code immediately after we return from
  439. * this function is going to release the
  440. * mmap_sem and it's not depending on it
  441. * (unlike gup would if we were not to return
  442. * VM_FAULT_RETRY).
  443. *
  444. * If a fatal signal is pending we still take
  445. * the streamlined VM_FAULT_RETRY failure path
  446. * and there's no need to retake the mmap_sem
  447. * in such case.
  448. */
  449. down_read(&mm->mmap_sem);
  450. ret = 0;
  451. }
  452. }
  453. /*
  454. * Here we race with the list_del; list_add in
  455. * userfaultfd_ctx_read(), however because we don't ever run
  456. * list_del_init() to refile across the two lists, the prev
  457. * and next pointers will never point to self. list_add also
  458. * would never let any of the two pointers to point to
  459. * self. So list_empty_careful won't risk to see both pointers
  460. * pointing to self at any time during the list refile. The
  461. * only case where list_del_init() is called is the full
  462. * removal in the wake function and there we don't re-list_add
  463. * and it's fine not to block on the spinlock. The uwq on this
  464. * kernel stack can be released after the list_del_init.
  465. */
  466. if (!list_empty_careful(&uwq.wq.task_list)) {
  467. spin_lock(&ctx->fault_pending_wqh.lock);
  468. /*
  469. * No need of list_del_init(), the uwq on the stack
  470. * will be freed shortly anyway.
  471. */
  472. list_del(&uwq.wq.task_list);
  473. spin_unlock(&ctx->fault_pending_wqh.lock);
  474. }
  475. /*
  476. * ctx may go away after this if the userfault pseudo fd is
  477. * already released.
  478. */
  479. userfaultfd_ctx_put(ctx);
  480. out:
  481. return ret;
  482. }
  483. static int userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
  484. struct userfaultfd_wait_queue *ewq)
  485. {
  486. int ret = 0;
  487. ewq->ctx = ctx;
  488. init_waitqueue_entry(&ewq->wq, current);
  489. spin_lock(&ctx->event_wqh.lock);
  490. /*
  491. * After the __add_wait_queue the uwq is visible to userland
  492. * through poll/read().
  493. */
  494. __add_wait_queue(&ctx->event_wqh, &ewq->wq);
  495. for (;;) {
  496. set_current_state(TASK_KILLABLE);
  497. if (ewq->msg.event == 0)
  498. break;
  499. if (ACCESS_ONCE(ctx->released) ||
  500. fatal_signal_pending(current)) {
  501. ret = -1;
  502. __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
  503. break;
  504. }
  505. spin_unlock(&ctx->event_wqh.lock);
  506. wake_up_poll(&ctx->fd_wqh, POLLIN);
  507. schedule();
  508. spin_lock(&ctx->event_wqh.lock);
  509. }
  510. __set_current_state(TASK_RUNNING);
  511. spin_unlock(&ctx->event_wqh.lock);
  512. /*
  513. * ctx may go away after this if the userfault pseudo fd is
  514. * already released.
  515. */
  516. userfaultfd_ctx_put(ctx);
  517. return ret;
  518. }
  519. static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
  520. struct userfaultfd_wait_queue *ewq)
  521. {
  522. ewq->msg.event = 0;
  523. wake_up_locked(&ctx->event_wqh);
  524. __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
  525. }
  526. int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
  527. {
  528. struct userfaultfd_ctx *ctx = NULL, *octx;
  529. struct userfaultfd_fork_ctx *fctx;
  530. octx = vma->vm_userfaultfd_ctx.ctx;
  531. if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
  532. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  533. vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
  534. return 0;
  535. }
  536. list_for_each_entry(fctx, fcs, list)
  537. if (fctx->orig == octx) {
  538. ctx = fctx->new;
  539. break;
  540. }
  541. if (!ctx) {
  542. fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
  543. if (!fctx)
  544. return -ENOMEM;
  545. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  546. if (!ctx) {
  547. kfree(fctx);
  548. return -ENOMEM;
  549. }
  550. atomic_set(&ctx->refcount, 1);
  551. ctx->flags = octx->flags;
  552. ctx->state = UFFD_STATE_RUNNING;
  553. ctx->features = octx->features;
  554. ctx->released = false;
  555. ctx->mm = vma->vm_mm;
  556. atomic_inc(&ctx->mm->mm_count);
  557. userfaultfd_ctx_get(octx);
  558. fctx->orig = octx;
  559. fctx->new = ctx;
  560. list_add_tail(&fctx->list, fcs);
  561. }
  562. vma->vm_userfaultfd_ctx.ctx = ctx;
  563. return 0;
  564. }
  565. static int dup_fctx(struct userfaultfd_fork_ctx *fctx)
  566. {
  567. struct userfaultfd_ctx *ctx = fctx->orig;
  568. struct userfaultfd_wait_queue ewq;
  569. msg_init(&ewq.msg);
  570. ewq.msg.event = UFFD_EVENT_FORK;
  571. ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
  572. return userfaultfd_event_wait_completion(ctx, &ewq);
  573. }
  574. void dup_userfaultfd_complete(struct list_head *fcs)
  575. {
  576. int ret = 0;
  577. struct userfaultfd_fork_ctx *fctx, *n;
  578. list_for_each_entry_safe(fctx, n, fcs, list) {
  579. if (!ret)
  580. ret = dup_fctx(fctx);
  581. list_del(&fctx->list);
  582. kfree(fctx);
  583. }
  584. }
  585. void mremap_userfaultfd_prep(struct vm_area_struct *vma,
  586. struct vm_userfaultfd_ctx *vm_ctx)
  587. {
  588. struct userfaultfd_ctx *ctx;
  589. ctx = vma->vm_userfaultfd_ctx.ctx;
  590. if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
  591. vm_ctx->ctx = ctx;
  592. userfaultfd_ctx_get(ctx);
  593. }
  594. }
  595. void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
  596. unsigned long from, unsigned long to,
  597. unsigned long len)
  598. {
  599. struct userfaultfd_ctx *ctx = vm_ctx->ctx;
  600. struct userfaultfd_wait_queue ewq;
  601. if (!ctx)
  602. return;
  603. if (to & ~PAGE_MASK) {
  604. userfaultfd_ctx_put(ctx);
  605. return;
  606. }
  607. msg_init(&ewq.msg);
  608. ewq.msg.event = UFFD_EVENT_REMAP;
  609. ewq.msg.arg.remap.from = from;
  610. ewq.msg.arg.remap.to = to;
  611. ewq.msg.arg.remap.len = len;
  612. userfaultfd_event_wait_completion(ctx, &ewq);
  613. }
  614. void userfaultfd_remove(struct vm_area_struct *vma,
  615. struct vm_area_struct **prev,
  616. unsigned long start, unsigned long end)
  617. {
  618. struct mm_struct *mm = vma->vm_mm;
  619. struct userfaultfd_ctx *ctx;
  620. struct userfaultfd_wait_queue ewq;
  621. ctx = vma->vm_userfaultfd_ctx.ctx;
  622. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
  623. return;
  624. userfaultfd_ctx_get(ctx);
  625. up_read(&mm->mmap_sem);
  626. *prev = NULL; /* We wait for ACK w/o the mmap semaphore */
  627. msg_init(&ewq.msg);
  628. ewq.msg.event = UFFD_EVENT_REMOVE;
  629. ewq.msg.arg.remove.start = start;
  630. ewq.msg.arg.remove.end = end;
  631. userfaultfd_event_wait_completion(ctx, &ewq);
  632. down_read(&mm->mmap_sem);
  633. }
  634. static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
  635. unsigned long start, unsigned long end)
  636. {
  637. struct userfaultfd_unmap_ctx *unmap_ctx;
  638. list_for_each_entry(unmap_ctx, unmaps, list)
  639. if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
  640. unmap_ctx->end == end)
  641. return true;
  642. return false;
  643. }
  644. int userfaultfd_unmap_prep(struct vm_area_struct *vma,
  645. unsigned long start, unsigned long end,
  646. struct list_head *unmaps)
  647. {
  648. for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
  649. struct userfaultfd_unmap_ctx *unmap_ctx;
  650. struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
  651. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
  652. has_unmap_ctx(ctx, unmaps, start, end))
  653. continue;
  654. unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
  655. if (!unmap_ctx)
  656. return -ENOMEM;
  657. userfaultfd_ctx_get(ctx);
  658. unmap_ctx->ctx = ctx;
  659. unmap_ctx->start = start;
  660. unmap_ctx->end = end;
  661. list_add_tail(&unmap_ctx->list, unmaps);
  662. }
  663. return 0;
  664. }
  665. void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
  666. {
  667. struct userfaultfd_unmap_ctx *ctx, *n;
  668. struct userfaultfd_wait_queue ewq;
  669. list_for_each_entry_safe(ctx, n, uf, list) {
  670. msg_init(&ewq.msg);
  671. ewq.msg.event = UFFD_EVENT_UNMAP;
  672. ewq.msg.arg.remove.start = ctx->start;
  673. ewq.msg.arg.remove.end = ctx->end;
  674. userfaultfd_event_wait_completion(ctx->ctx, &ewq);
  675. list_del(&ctx->list);
  676. kfree(ctx);
  677. }
  678. }
  679. void userfaultfd_exit(struct mm_struct *mm)
  680. {
  681. struct vm_area_struct *vma = mm->mmap;
  682. /*
  683. * We can do the vma walk without locking because the caller
  684. * (exit_mm) knows it now has exclusive access
  685. */
  686. while (vma) {
  687. struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
  688. if (ctx && (ctx->features & UFFD_FEATURE_EVENT_EXIT)) {
  689. struct userfaultfd_wait_queue ewq;
  690. userfaultfd_ctx_get(ctx);
  691. msg_init(&ewq.msg);
  692. ewq.msg.event = UFFD_EVENT_EXIT;
  693. userfaultfd_event_wait_completion(ctx, &ewq);
  694. ctx->features &= ~UFFD_FEATURE_EVENT_EXIT;
  695. }
  696. vma = vma->vm_next;
  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_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->task_list, typeof(*wq), task_list);
  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.task_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.task_list);
  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.task_list, &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.task_list);
  946. list_del(&uwq->wq.task_list);
  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. unsigned long start, end;
  986. start = range->start;
  987. end = range->start + range->len;
  988. spin_lock(&ctx->fault_pending_wqh.lock);
  989. /* wake all in the range and autoremove */
  990. if (waitqueue_active(&ctx->fault_pending_wqh))
  991. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
  992. range);
  993. if (waitqueue_active(&ctx->fault_wqh))
  994. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
  995. spin_unlock(&ctx->fault_pending_wqh.lock);
  996. }
  997. static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
  998. struct userfaultfd_wake_range *range)
  999. {
  1000. unsigned seq;
  1001. bool need_wakeup;
  1002. /*
  1003. * To be sure waitqueue_active() is not reordered by the CPU
  1004. * before the pagetable update, use an explicit SMP memory
  1005. * barrier here. PT lock release or up_read(mmap_sem) still
  1006. * have release semantics that can allow the
  1007. * waitqueue_active() to be reordered before the pte update.
  1008. */
  1009. smp_mb();
  1010. /*
  1011. * Use waitqueue_active because it's very frequent to
  1012. * change the address space atomically even if there are no
  1013. * userfaults yet. So we take the spinlock only when we're
  1014. * sure we've userfaults to wake.
  1015. */
  1016. do {
  1017. seq = read_seqcount_begin(&ctx->refile_seq);
  1018. need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
  1019. waitqueue_active(&ctx->fault_wqh);
  1020. cond_resched();
  1021. } while (read_seqcount_retry(&ctx->refile_seq, seq));
  1022. if (need_wakeup)
  1023. __wake_userfault(ctx, range);
  1024. }
  1025. static __always_inline int validate_range(struct mm_struct *mm,
  1026. __u64 start, __u64 len)
  1027. {
  1028. __u64 task_size = mm->task_size;
  1029. if (start & ~PAGE_MASK)
  1030. return -EINVAL;
  1031. if (len & ~PAGE_MASK)
  1032. return -EINVAL;
  1033. if (!len)
  1034. return -EINVAL;
  1035. if (start < mmap_min_addr)
  1036. return -EINVAL;
  1037. if (start >= task_size)
  1038. return -EINVAL;
  1039. if (len > task_size - start)
  1040. return -EINVAL;
  1041. return 0;
  1042. }
  1043. static inline bool vma_can_userfault(struct vm_area_struct *vma)
  1044. {
  1045. return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
  1046. vma_is_shmem(vma);
  1047. }
  1048. static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  1049. unsigned long arg)
  1050. {
  1051. struct mm_struct *mm = ctx->mm;
  1052. struct vm_area_struct *vma, *prev, *cur;
  1053. int ret;
  1054. struct uffdio_register uffdio_register;
  1055. struct uffdio_register __user *user_uffdio_register;
  1056. unsigned long vm_flags, new_flags;
  1057. bool found;
  1058. bool non_anon_pages;
  1059. unsigned long start, end, vma_end;
  1060. user_uffdio_register = (struct uffdio_register __user *) arg;
  1061. ret = -EFAULT;
  1062. if (copy_from_user(&uffdio_register, user_uffdio_register,
  1063. sizeof(uffdio_register)-sizeof(__u64)))
  1064. goto out;
  1065. ret = -EINVAL;
  1066. if (!uffdio_register.mode)
  1067. goto out;
  1068. if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
  1069. UFFDIO_REGISTER_MODE_WP))
  1070. goto out;
  1071. vm_flags = 0;
  1072. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  1073. vm_flags |= VM_UFFD_MISSING;
  1074. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
  1075. vm_flags |= VM_UFFD_WP;
  1076. /*
  1077. * FIXME: remove the below error constraint by
  1078. * implementing the wprotect tracking mode.
  1079. */
  1080. ret = -EINVAL;
  1081. goto out;
  1082. }
  1083. ret = validate_range(mm, uffdio_register.range.start,
  1084. uffdio_register.range.len);
  1085. if (ret)
  1086. goto out;
  1087. start = uffdio_register.range.start;
  1088. end = start + uffdio_register.range.len;
  1089. ret = -ENOMEM;
  1090. if (!mmget_not_zero(mm))
  1091. goto out;
  1092. down_write(&mm->mmap_sem);
  1093. vma = find_vma_prev(mm, start, &prev);
  1094. if (!vma)
  1095. goto out_unlock;
  1096. /* check that there's at least one vma in the range */
  1097. ret = -EINVAL;
  1098. if (vma->vm_start >= end)
  1099. goto out_unlock;
  1100. /*
  1101. * If the first vma contains huge pages, make sure start address
  1102. * is aligned to huge page size.
  1103. */
  1104. if (is_vm_hugetlb_page(vma)) {
  1105. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1106. if (start & (vma_hpagesize - 1))
  1107. goto out_unlock;
  1108. }
  1109. /*
  1110. * Search for not compatible vmas.
  1111. */
  1112. found = false;
  1113. non_anon_pages = false;
  1114. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1115. cond_resched();
  1116. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1117. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1118. /* check not compatible vmas */
  1119. ret = -EINVAL;
  1120. if (!vma_can_userfault(cur))
  1121. goto out_unlock;
  1122. /*
  1123. * If this vma contains ending address, and huge pages
  1124. * check alignment.
  1125. */
  1126. if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
  1127. end > cur->vm_start) {
  1128. unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
  1129. ret = -EINVAL;
  1130. if (end & (vma_hpagesize - 1))
  1131. goto out_unlock;
  1132. }
  1133. /*
  1134. * Check that this vma isn't already owned by a
  1135. * different userfaultfd. We can't allow more than one
  1136. * userfaultfd to own a single vma simultaneously or we
  1137. * wouldn't know which one to deliver the userfaults to.
  1138. */
  1139. ret = -EBUSY;
  1140. if (cur->vm_userfaultfd_ctx.ctx &&
  1141. cur->vm_userfaultfd_ctx.ctx != ctx)
  1142. goto out_unlock;
  1143. /*
  1144. * Note vmas containing huge pages
  1145. */
  1146. if (is_vm_hugetlb_page(cur) || vma_is_shmem(cur))
  1147. non_anon_pages = true;
  1148. found = true;
  1149. }
  1150. BUG_ON(!found);
  1151. if (vma->vm_start < start)
  1152. prev = vma;
  1153. ret = 0;
  1154. do {
  1155. cond_resched();
  1156. BUG_ON(!vma_can_userfault(vma));
  1157. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  1158. vma->vm_userfaultfd_ctx.ctx != ctx);
  1159. /*
  1160. * Nothing to do: this vma is already registered into this
  1161. * userfaultfd and with the right tracking mode too.
  1162. */
  1163. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  1164. (vma->vm_flags & vm_flags) == vm_flags)
  1165. goto skip;
  1166. if (vma->vm_start > start)
  1167. start = vma->vm_start;
  1168. vma_end = min(end, vma->vm_end);
  1169. new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
  1170. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1171. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1172. vma_policy(vma),
  1173. ((struct vm_userfaultfd_ctx){ ctx }));
  1174. if (prev) {
  1175. vma = prev;
  1176. goto next;
  1177. }
  1178. if (vma->vm_start < start) {
  1179. ret = split_vma(mm, vma, start, 1);
  1180. if (ret)
  1181. break;
  1182. }
  1183. if (vma->vm_end > end) {
  1184. ret = split_vma(mm, vma, end, 0);
  1185. if (ret)
  1186. break;
  1187. }
  1188. next:
  1189. /*
  1190. * In the vma_merge() successful mprotect-like case 8:
  1191. * the next vma was merged into the current one and
  1192. * the current one has not been updated yet.
  1193. */
  1194. vma->vm_flags = new_flags;
  1195. vma->vm_userfaultfd_ctx.ctx = ctx;
  1196. skip:
  1197. prev = vma;
  1198. start = vma->vm_end;
  1199. vma = vma->vm_next;
  1200. } while (vma && vma->vm_start < end);
  1201. out_unlock:
  1202. up_write(&mm->mmap_sem);
  1203. mmput(mm);
  1204. if (!ret) {
  1205. /*
  1206. * Now that we scanned all vmas we can already tell
  1207. * userland which ioctls methods are guaranteed to
  1208. * succeed on this range.
  1209. */
  1210. if (put_user(non_anon_pages ? UFFD_API_RANGE_IOCTLS_BASIC :
  1211. UFFD_API_RANGE_IOCTLS,
  1212. &user_uffdio_register->ioctls))
  1213. ret = -EFAULT;
  1214. }
  1215. out:
  1216. return ret;
  1217. }
  1218. static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
  1219. unsigned long arg)
  1220. {
  1221. struct mm_struct *mm = ctx->mm;
  1222. struct vm_area_struct *vma, *prev, *cur;
  1223. int ret;
  1224. struct uffdio_range uffdio_unregister;
  1225. unsigned long new_flags;
  1226. bool found;
  1227. unsigned long start, end, vma_end;
  1228. const void __user *buf = (void __user *)arg;
  1229. ret = -EFAULT;
  1230. if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
  1231. goto out;
  1232. ret = validate_range(mm, uffdio_unregister.start,
  1233. uffdio_unregister.len);
  1234. if (ret)
  1235. goto out;
  1236. start = uffdio_unregister.start;
  1237. end = start + uffdio_unregister.len;
  1238. ret = -ENOMEM;
  1239. if (!mmget_not_zero(mm))
  1240. goto out;
  1241. down_write(&mm->mmap_sem);
  1242. vma = find_vma_prev(mm, start, &prev);
  1243. if (!vma)
  1244. goto out_unlock;
  1245. /* check that there's at least one vma in the range */
  1246. ret = -EINVAL;
  1247. if (vma->vm_start >= end)
  1248. goto out_unlock;
  1249. /*
  1250. * If the first vma contains huge pages, make sure start address
  1251. * is aligned to huge page size.
  1252. */
  1253. if (is_vm_hugetlb_page(vma)) {
  1254. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1255. if (start & (vma_hpagesize - 1))
  1256. goto out_unlock;
  1257. }
  1258. /*
  1259. * Search for not compatible vmas.
  1260. */
  1261. found = false;
  1262. ret = -EINVAL;
  1263. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1264. cond_resched();
  1265. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1266. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1267. /*
  1268. * Check not compatible vmas, not strictly required
  1269. * here as not compatible vmas cannot have an
  1270. * userfaultfd_ctx registered on them, but this
  1271. * provides for more strict behavior to notice
  1272. * unregistration errors.
  1273. */
  1274. if (!vma_can_userfault(cur))
  1275. goto out_unlock;
  1276. found = true;
  1277. }
  1278. BUG_ON(!found);
  1279. if (vma->vm_start < start)
  1280. prev = vma;
  1281. ret = 0;
  1282. do {
  1283. cond_resched();
  1284. BUG_ON(!vma_can_userfault(vma));
  1285. /*
  1286. * Nothing to do: this vma is already registered into this
  1287. * userfaultfd and with the right tracking mode too.
  1288. */
  1289. if (!vma->vm_userfaultfd_ctx.ctx)
  1290. goto skip;
  1291. if (vma->vm_start > start)
  1292. start = vma->vm_start;
  1293. vma_end = min(end, vma->vm_end);
  1294. if (userfaultfd_missing(vma)) {
  1295. /*
  1296. * Wake any concurrent pending userfault while
  1297. * we unregister, so they will not hang
  1298. * permanently and it avoids userland to call
  1299. * UFFDIO_WAKE explicitly.
  1300. */
  1301. struct userfaultfd_wake_range range;
  1302. range.start = start;
  1303. range.len = vma_end - start;
  1304. wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
  1305. }
  1306. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  1307. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1308. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1309. vma_policy(vma),
  1310. NULL_VM_UFFD_CTX);
  1311. if (prev) {
  1312. vma = prev;
  1313. goto next;
  1314. }
  1315. if (vma->vm_start < start) {
  1316. ret = split_vma(mm, vma, start, 1);
  1317. if (ret)
  1318. break;
  1319. }
  1320. if (vma->vm_end > end) {
  1321. ret = split_vma(mm, vma, end, 0);
  1322. if (ret)
  1323. break;
  1324. }
  1325. next:
  1326. /*
  1327. * In the vma_merge() successful mprotect-like case 8:
  1328. * the next vma was merged into the current one and
  1329. * the current one has not been updated yet.
  1330. */
  1331. vma->vm_flags = new_flags;
  1332. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  1333. skip:
  1334. prev = vma;
  1335. start = vma->vm_end;
  1336. vma = vma->vm_next;
  1337. } while (vma && vma->vm_start < end);
  1338. out_unlock:
  1339. up_write(&mm->mmap_sem);
  1340. mmput(mm);
  1341. out:
  1342. return ret;
  1343. }
  1344. /*
  1345. * userfaultfd_wake may be used in combination with the
  1346. * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
  1347. */
  1348. static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
  1349. unsigned long arg)
  1350. {
  1351. int ret;
  1352. struct uffdio_range uffdio_wake;
  1353. struct userfaultfd_wake_range range;
  1354. const void __user *buf = (void __user *)arg;
  1355. ret = -EFAULT;
  1356. if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
  1357. goto out;
  1358. ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
  1359. if (ret)
  1360. goto out;
  1361. range.start = uffdio_wake.start;
  1362. range.len = uffdio_wake.len;
  1363. /*
  1364. * len == 0 means wake all and we don't want to wake all here,
  1365. * so check it again to be sure.
  1366. */
  1367. VM_BUG_ON(!range.len);
  1368. wake_userfault(ctx, &range);
  1369. ret = 0;
  1370. out:
  1371. return ret;
  1372. }
  1373. static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
  1374. unsigned long arg)
  1375. {
  1376. __s64 ret;
  1377. struct uffdio_copy uffdio_copy;
  1378. struct uffdio_copy __user *user_uffdio_copy;
  1379. struct userfaultfd_wake_range range;
  1380. user_uffdio_copy = (struct uffdio_copy __user *) arg;
  1381. ret = -EFAULT;
  1382. if (copy_from_user(&uffdio_copy, user_uffdio_copy,
  1383. /* don't copy "copy" last field */
  1384. sizeof(uffdio_copy)-sizeof(__s64)))
  1385. goto out;
  1386. ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
  1387. if (ret)
  1388. goto out;
  1389. /*
  1390. * double check for wraparound just in case. copy_from_user()
  1391. * will later check uffdio_copy.src + uffdio_copy.len to fit
  1392. * in the userland range.
  1393. */
  1394. ret = -EINVAL;
  1395. if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
  1396. goto out;
  1397. if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
  1398. goto out;
  1399. if (mmget_not_zero(ctx->mm)) {
  1400. ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
  1401. uffdio_copy.len);
  1402. mmput(ctx->mm);
  1403. } else {
  1404. return -ENOSPC;
  1405. }
  1406. if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
  1407. return -EFAULT;
  1408. if (ret < 0)
  1409. goto out;
  1410. BUG_ON(!ret);
  1411. /* len == 0 would wake all */
  1412. range.len = ret;
  1413. if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
  1414. range.start = uffdio_copy.dst;
  1415. wake_userfault(ctx, &range);
  1416. }
  1417. ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
  1418. out:
  1419. return ret;
  1420. }
  1421. static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
  1422. unsigned long arg)
  1423. {
  1424. __s64 ret;
  1425. struct uffdio_zeropage uffdio_zeropage;
  1426. struct uffdio_zeropage __user *user_uffdio_zeropage;
  1427. struct userfaultfd_wake_range range;
  1428. user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
  1429. ret = -EFAULT;
  1430. if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
  1431. /* don't copy "zeropage" last field */
  1432. sizeof(uffdio_zeropage)-sizeof(__s64)))
  1433. goto out;
  1434. ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
  1435. uffdio_zeropage.range.len);
  1436. if (ret)
  1437. goto out;
  1438. ret = -EINVAL;
  1439. if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
  1440. goto out;
  1441. if (mmget_not_zero(ctx->mm)) {
  1442. ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
  1443. uffdio_zeropage.range.len);
  1444. mmput(ctx->mm);
  1445. }
  1446. if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
  1447. return -EFAULT;
  1448. if (ret < 0)
  1449. goto out;
  1450. /* len == 0 would wake all */
  1451. BUG_ON(!ret);
  1452. range.len = ret;
  1453. if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
  1454. range.start = uffdio_zeropage.range.start;
  1455. wake_userfault(ctx, &range);
  1456. }
  1457. ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
  1458. out:
  1459. return ret;
  1460. }
  1461. static inline unsigned int uffd_ctx_features(__u64 user_features)
  1462. {
  1463. /*
  1464. * For the current set of features the bits just coincide
  1465. */
  1466. return (unsigned int)user_features;
  1467. }
  1468. /*
  1469. * userland asks for a certain API version and we return which bits
  1470. * and ioctl commands are implemented in this kernel for such API
  1471. * version or -EINVAL if unknown.
  1472. */
  1473. static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  1474. unsigned long arg)
  1475. {
  1476. struct uffdio_api uffdio_api;
  1477. void __user *buf = (void __user *)arg;
  1478. int ret;
  1479. __u64 features;
  1480. ret = -EINVAL;
  1481. if (ctx->state != UFFD_STATE_WAIT_API)
  1482. goto out;
  1483. ret = -EFAULT;
  1484. if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
  1485. goto out;
  1486. features = uffdio_api.features;
  1487. if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
  1488. memset(&uffdio_api, 0, sizeof(uffdio_api));
  1489. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1490. goto out;
  1491. ret = -EINVAL;
  1492. goto out;
  1493. }
  1494. /* report all available features and ioctls to userland */
  1495. uffdio_api.features = UFFD_API_FEATURES;
  1496. uffdio_api.ioctls = UFFD_API_IOCTLS;
  1497. ret = -EFAULT;
  1498. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1499. goto out;
  1500. ctx->state = UFFD_STATE_RUNNING;
  1501. /* only enable the requested features for this uffd context */
  1502. ctx->features = uffd_ctx_features(features);
  1503. ret = 0;
  1504. out:
  1505. return ret;
  1506. }
  1507. static long userfaultfd_ioctl(struct file *file, unsigned cmd,
  1508. unsigned long arg)
  1509. {
  1510. int ret = -EINVAL;
  1511. struct userfaultfd_ctx *ctx = file->private_data;
  1512. if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
  1513. return -EINVAL;
  1514. switch(cmd) {
  1515. case UFFDIO_API:
  1516. ret = userfaultfd_api(ctx, arg);
  1517. break;
  1518. case UFFDIO_REGISTER:
  1519. ret = userfaultfd_register(ctx, arg);
  1520. break;
  1521. case UFFDIO_UNREGISTER:
  1522. ret = userfaultfd_unregister(ctx, arg);
  1523. break;
  1524. case UFFDIO_WAKE:
  1525. ret = userfaultfd_wake(ctx, arg);
  1526. break;
  1527. case UFFDIO_COPY:
  1528. ret = userfaultfd_copy(ctx, arg);
  1529. break;
  1530. case UFFDIO_ZEROPAGE:
  1531. ret = userfaultfd_zeropage(ctx, arg);
  1532. break;
  1533. }
  1534. return ret;
  1535. }
  1536. #ifdef CONFIG_PROC_FS
  1537. static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
  1538. {
  1539. struct userfaultfd_ctx *ctx = f->private_data;
  1540. wait_queue_t *wq;
  1541. struct userfaultfd_wait_queue *uwq;
  1542. unsigned long pending = 0, total = 0;
  1543. spin_lock(&ctx->fault_pending_wqh.lock);
  1544. list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
  1545. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1546. pending++;
  1547. total++;
  1548. }
  1549. list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
  1550. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1551. total++;
  1552. }
  1553. spin_unlock(&ctx->fault_pending_wqh.lock);
  1554. /*
  1555. * If more protocols will be added, there will be all shown
  1556. * separated by a space. Like this:
  1557. * protocols: aa:... bb:...
  1558. */
  1559. seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
  1560. pending, total, UFFD_API, UFFD_API_FEATURES,
  1561. UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
  1562. }
  1563. #endif
  1564. static const struct file_operations userfaultfd_fops = {
  1565. #ifdef CONFIG_PROC_FS
  1566. .show_fdinfo = userfaultfd_show_fdinfo,
  1567. #endif
  1568. .release = userfaultfd_release,
  1569. .poll = userfaultfd_poll,
  1570. .read = userfaultfd_read,
  1571. .unlocked_ioctl = userfaultfd_ioctl,
  1572. .compat_ioctl = userfaultfd_ioctl,
  1573. .llseek = noop_llseek,
  1574. };
  1575. static void init_once_userfaultfd_ctx(void *mem)
  1576. {
  1577. struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
  1578. init_waitqueue_head(&ctx->fault_pending_wqh);
  1579. init_waitqueue_head(&ctx->fault_wqh);
  1580. init_waitqueue_head(&ctx->event_wqh);
  1581. init_waitqueue_head(&ctx->fd_wqh);
  1582. seqcount_init(&ctx->refile_seq);
  1583. }
  1584. /**
  1585. * userfaultfd_file_create - Creates a userfaultfd file pointer.
  1586. * @flags: Flags for the userfaultfd file.
  1587. *
  1588. * This function creates a userfaultfd file pointer, w/out installing
  1589. * it into the fd table. This is useful when the userfaultfd file is
  1590. * used during the initialization of data structures that require
  1591. * extra setup after the userfaultfd creation. So the userfaultfd
  1592. * creation is split into the file pointer creation phase, and the
  1593. * file descriptor installation phase. In this way races with
  1594. * userspace closing the newly installed file descriptor can be
  1595. * avoided. Returns a userfaultfd file pointer, or a proper error
  1596. * pointer.
  1597. */
  1598. static struct file *userfaultfd_file_create(int flags)
  1599. {
  1600. struct file *file;
  1601. struct userfaultfd_ctx *ctx;
  1602. BUG_ON(!current->mm);
  1603. /* Check the UFFD_* constants for consistency. */
  1604. BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
  1605. BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
  1606. file = ERR_PTR(-EINVAL);
  1607. if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
  1608. goto out;
  1609. file = ERR_PTR(-ENOMEM);
  1610. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  1611. if (!ctx)
  1612. goto out;
  1613. atomic_set(&ctx->refcount, 1);
  1614. ctx->flags = flags;
  1615. ctx->features = 0;
  1616. ctx->state = UFFD_STATE_WAIT_API;
  1617. ctx->released = false;
  1618. ctx->mm = current->mm;
  1619. /* prevent the mm struct to be freed */
  1620. mmgrab(ctx->mm);
  1621. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
  1622. O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
  1623. if (IS_ERR(file)) {
  1624. mmdrop(ctx->mm);
  1625. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  1626. }
  1627. out:
  1628. return file;
  1629. }
  1630. SYSCALL_DEFINE1(userfaultfd, int, flags)
  1631. {
  1632. int fd, error;
  1633. struct file *file;
  1634. error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
  1635. if (error < 0)
  1636. return error;
  1637. fd = error;
  1638. file = userfaultfd_file_create(flags);
  1639. if (IS_ERR(file)) {
  1640. error = PTR_ERR(file);
  1641. goto err_put_unused_fd;
  1642. }
  1643. fd_install(fd, file);
  1644. return fd;
  1645. err_put_unused_fd:
  1646. put_unused_fd(fd);
  1647. return error;
  1648. }
  1649. static int __init userfaultfd_init(void)
  1650. {
  1651. userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
  1652. sizeof(struct userfaultfd_ctx),
  1653. 0,
  1654. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1655. init_once_userfaultfd_ctx);
  1656. return 0;
  1657. }
  1658. __initcall(userfaultfd_init);