userfaultfd.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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->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. 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. unsigned long address,
  203. unsigned long flags,
  204. unsigned long reason)
  205. {
  206. struct mm_struct *mm = ctx->mm;
  207. pte_t *pte;
  208. bool ret = true;
  209. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  210. pte = huge_pte_offset(mm, address);
  211. if (!pte)
  212. goto out;
  213. ret = false;
  214. /*
  215. * Lockless access: we're in a wait_event so it's ok if it
  216. * changes under us.
  217. */
  218. if (huge_pte_none(*pte))
  219. ret = true;
  220. if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
  221. ret = true;
  222. out:
  223. return ret;
  224. }
  225. #else
  226. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  227. unsigned long address,
  228. unsigned long flags,
  229. unsigned long reason)
  230. {
  231. return false; /* should never get here */
  232. }
  233. #endif /* CONFIG_HUGETLB_PAGE */
  234. /*
  235. * Verify the pagetables are still not ok after having reigstered into
  236. * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
  237. * userfault that has already been resolved, if userfaultfd_read and
  238. * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
  239. * threads.
  240. */
  241. static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
  242. unsigned long address,
  243. unsigned long flags,
  244. unsigned long reason)
  245. {
  246. struct mm_struct *mm = ctx->mm;
  247. pgd_t *pgd;
  248. p4d_t *p4d;
  249. pud_t *pud;
  250. pmd_t *pmd, _pmd;
  251. pte_t *pte;
  252. bool ret = true;
  253. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  254. pgd = pgd_offset(mm, address);
  255. if (!pgd_present(*pgd))
  256. goto out;
  257. p4d = p4d_offset(pgd, address);
  258. if (!p4d_present(*p4d))
  259. goto out;
  260. pud = pud_offset(p4d, address);
  261. if (!pud_present(*pud))
  262. goto out;
  263. pmd = pmd_offset(pud, address);
  264. /*
  265. * READ_ONCE must function as a barrier with narrower scope
  266. * and it must be equivalent to:
  267. * _pmd = *pmd; barrier();
  268. *
  269. * This is to deal with the instability (as in
  270. * pmd_trans_unstable) of the pmd.
  271. */
  272. _pmd = READ_ONCE(*pmd);
  273. if (!pmd_present(_pmd))
  274. goto out;
  275. ret = false;
  276. if (pmd_trans_huge(_pmd))
  277. goto out;
  278. /*
  279. * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
  280. * and use the standard pte_offset_map() instead of parsing _pmd.
  281. */
  282. pte = pte_offset_map(pmd, address);
  283. /*
  284. * Lockless access: we're in a wait_event so it's ok if it
  285. * changes under us.
  286. */
  287. if (pte_none(*pte))
  288. ret = true;
  289. pte_unmap(pte);
  290. out:
  291. return ret;
  292. }
  293. /*
  294. * The locking rules involved in returning VM_FAULT_RETRY depending on
  295. * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
  296. * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
  297. * recommendation in __lock_page_or_retry is not an understatement.
  298. *
  299. * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
  300. * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
  301. * not set.
  302. *
  303. * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
  304. * set, VM_FAULT_RETRY can still be returned if and only if there are
  305. * fatal_signal_pending()s, and the mmap_sem must be released before
  306. * returning it.
  307. */
  308. int handle_userfault(struct vm_fault *vmf, unsigned long reason)
  309. {
  310. struct mm_struct *mm = vmf->vma->vm_mm;
  311. struct userfaultfd_ctx *ctx;
  312. struct userfaultfd_wait_queue uwq;
  313. int ret;
  314. bool must_wait, return_to_userland;
  315. long blocking_state;
  316. ret = VM_FAULT_SIGBUS;
  317. /*
  318. * We don't do userfault handling for the final child pid update.
  319. *
  320. * We also don't do userfault handling during
  321. * coredumping. hugetlbfs has the special
  322. * follow_hugetlb_page() to skip missing pages in the
  323. * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
  324. * the no_page_table() helper in follow_page_mask(), but the
  325. * shmem_vm_ops->fault method is invoked even during
  326. * coredumping without mmap_sem and it ends up here.
  327. */
  328. if (current->flags & (PF_EXITING|PF_DUMPCORE))
  329. goto out;
  330. /*
  331. * Coredumping runs without mmap_sem so we can only check that
  332. * the mmap_sem is held, if PF_DUMPCORE was not set.
  333. */
  334. WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem));
  335. ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
  336. if (!ctx)
  337. goto out;
  338. BUG_ON(ctx->mm != mm);
  339. VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
  340. VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
  341. /*
  342. * If it's already released don't get it. This avoids to loop
  343. * in __get_user_pages if userfaultfd_release waits on the
  344. * caller of handle_userfault to release the mmap_sem.
  345. */
  346. if (unlikely(ACCESS_ONCE(ctx->released)))
  347. goto out;
  348. /*
  349. * Check that we can return VM_FAULT_RETRY.
  350. *
  351. * NOTE: it should become possible to return VM_FAULT_RETRY
  352. * even if FAULT_FLAG_TRIED is set without leading to gup()
  353. * -EBUSY failures, if the userfaultfd is to be extended for
  354. * VM_UFFD_WP tracking and we intend to arm the userfault
  355. * without first stopping userland access to the memory. For
  356. * VM_UFFD_MISSING userfaults this is enough for now.
  357. */
  358. if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
  359. /*
  360. * Validate the invariant that nowait must allow retry
  361. * to be sure not to return SIGBUS erroneously on
  362. * nowait invocations.
  363. */
  364. BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
  365. #ifdef CONFIG_DEBUG_VM
  366. if (printk_ratelimit()) {
  367. printk(KERN_WARNING
  368. "FAULT_FLAG_ALLOW_RETRY missing %x\n",
  369. vmf->flags);
  370. dump_stack();
  371. }
  372. #endif
  373. goto out;
  374. }
  375. /*
  376. * Handle nowait, not much to do other than tell it to retry
  377. * and wait.
  378. */
  379. ret = VM_FAULT_RETRY;
  380. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  381. goto out;
  382. /* take the reference before dropping the mmap_sem */
  383. userfaultfd_ctx_get(ctx);
  384. init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
  385. uwq.wq.private = current;
  386. uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
  387. uwq.ctx = ctx;
  388. uwq.waken = false;
  389. return_to_userland =
  390. (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
  391. (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
  392. blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
  393. TASK_KILLABLE;
  394. spin_lock(&ctx->fault_pending_wqh.lock);
  395. /*
  396. * After the __add_wait_queue the uwq is visible to userland
  397. * through poll/read().
  398. */
  399. __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
  400. /*
  401. * The smp_mb() after __set_current_state prevents the reads
  402. * following the spin_unlock to happen before the list_add in
  403. * __add_wait_queue.
  404. */
  405. set_current_state(blocking_state);
  406. spin_unlock(&ctx->fault_pending_wqh.lock);
  407. if (!is_vm_hugetlb_page(vmf->vma))
  408. must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
  409. reason);
  410. else
  411. must_wait = userfaultfd_huge_must_wait(ctx, vmf->address,
  412. vmf->flags, reason);
  413. up_read(&mm->mmap_sem);
  414. if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
  415. (return_to_userland ? !signal_pending(current) :
  416. !fatal_signal_pending(current)))) {
  417. wake_up_poll(&ctx->fd_wqh, POLLIN);
  418. schedule();
  419. ret |= VM_FAULT_MAJOR;
  420. /*
  421. * False wakeups can orginate even from rwsem before
  422. * up_read() however userfaults will wait either for a
  423. * targeted wakeup on the specific uwq waitqueue from
  424. * wake_userfault() or for signals or for uffd
  425. * release.
  426. */
  427. while (!READ_ONCE(uwq.waken)) {
  428. /*
  429. * This needs the full smp_store_mb()
  430. * guarantee as the state write must be
  431. * visible to other CPUs before reading
  432. * uwq.waken from other CPUs.
  433. */
  434. set_current_state(blocking_state);
  435. if (READ_ONCE(uwq.waken) ||
  436. READ_ONCE(ctx->released) ||
  437. (return_to_userland ? signal_pending(current) :
  438. fatal_signal_pending(current)))
  439. break;
  440. schedule();
  441. }
  442. }
  443. __set_current_state(TASK_RUNNING);
  444. if (return_to_userland) {
  445. if (signal_pending(current) &&
  446. !fatal_signal_pending(current)) {
  447. /*
  448. * If we got a SIGSTOP or SIGCONT and this is
  449. * a normal userland page fault, just let
  450. * userland return so the signal will be
  451. * handled and gdb debugging works. The page
  452. * fault code immediately after we return from
  453. * this function is going to release the
  454. * mmap_sem and it's not depending on it
  455. * (unlike gup would if we were not to return
  456. * VM_FAULT_RETRY).
  457. *
  458. * If a fatal signal is pending we still take
  459. * the streamlined VM_FAULT_RETRY failure path
  460. * and there's no need to retake the mmap_sem
  461. * in such case.
  462. */
  463. down_read(&mm->mmap_sem);
  464. ret = VM_FAULT_NOPAGE;
  465. }
  466. }
  467. /*
  468. * Here we race with the list_del; list_add in
  469. * userfaultfd_ctx_read(), however because we don't ever run
  470. * list_del_init() to refile across the two lists, the prev
  471. * and next pointers will never point to self. list_add also
  472. * would never let any of the two pointers to point to
  473. * self. So list_empty_careful won't risk to see both pointers
  474. * pointing to self at any time during the list refile. The
  475. * only case where list_del_init() is called is the full
  476. * removal in the wake function and there we don't re-list_add
  477. * and it's fine not to block on the spinlock. The uwq on this
  478. * kernel stack can be released after the list_del_init.
  479. */
  480. if (!list_empty_careful(&uwq.wq.task_list)) {
  481. spin_lock(&ctx->fault_pending_wqh.lock);
  482. /*
  483. * No need of list_del_init(), the uwq on the stack
  484. * will be freed shortly anyway.
  485. */
  486. list_del(&uwq.wq.task_list);
  487. spin_unlock(&ctx->fault_pending_wqh.lock);
  488. }
  489. /*
  490. * ctx may go away after this if the userfault pseudo fd is
  491. * already released.
  492. */
  493. userfaultfd_ctx_put(ctx);
  494. out:
  495. return ret;
  496. }
  497. static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
  498. struct userfaultfd_wait_queue *ewq)
  499. {
  500. if (WARN_ON_ONCE(current->flags & PF_EXITING))
  501. goto out;
  502. ewq->ctx = ctx;
  503. init_waitqueue_entry(&ewq->wq, current);
  504. spin_lock(&ctx->event_wqh.lock);
  505. /*
  506. * After the __add_wait_queue the uwq is visible to userland
  507. * through poll/read().
  508. */
  509. __add_wait_queue(&ctx->event_wqh, &ewq->wq);
  510. for (;;) {
  511. set_current_state(TASK_KILLABLE);
  512. if (ewq->msg.event == 0)
  513. break;
  514. if (ACCESS_ONCE(ctx->released) ||
  515. fatal_signal_pending(current)) {
  516. __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
  517. if (ewq->msg.event == UFFD_EVENT_FORK) {
  518. struct userfaultfd_ctx *new;
  519. new = (struct userfaultfd_ctx *)
  520. (unsigned long)
  521. ewq->msg.arg.reserved.reserved1;
  522. userfaultfd_ctx_put(new);
  523. }
  524. break;
  525. }
  526. spin_unlock(&ctx->event_wqh.lock);
  527. wake_up_poll(&ctx->fd_wqh, POLLIN);
  528. schedule();
  529. spin_lock(&ctx->event_wqh.lock);
  530. }
  531. __set_current_state(TASK_RUNNING);
  532. spin_unlock(&ctx->event_wqh.lock);
  533. /*
  534. * ctx may go away after this if the userfault pseudo fd is
  535. * already released.
  536. */
  537. out:
  538. userfaultfd_ctx_put(ctx);
  539. }
  540. static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx,
  541. struct userfaultfd_wait_queue *ewq)
  542. {
  543. ewq->msg.event = 0;
  544. wake_up_locked(&ctx->event_wqh);
  545. __remove_wait_queue(&ctx->event_wqh, &ewq->wq);
  546. }
  547. int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs)
  548. {
  549. struct userfaultfd_ctx *ctx = NULL, *octx;
  550. struct userfaultfd_fork_ctx *fctx;
  551. octx = vma->vm_userfaultfd_ctx.ctx;
  552. if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) {
  553. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  554. vma->vm_flags &= ~(VM_UFFD_WP | VM_UFFD_MISSING);
  555. return 0;
  556. }
  557. list_for_each_entry(fctx, fcs, list)
  558. if (fctx->orig == octx) {
  559. ctx = fctx->new;
  560. break;
  561. }
  562. if (!ctx) {
  563. fctx = kmalloc(sizeof(*fctx), GFP_KERNEL);
  564. if (!fctx)
  565. return -ENOMEM;
  566. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  567. if (!ctx) {
  568. kfree(fctx);
  569. return -ENOMEM;
  570. }
  571. atomic_set(&ctx->refcount, 1);
  572. ctx->flags = octx->flags;
  573. ctx->state = UFFD_STATE_RUNNING;
  574. ctx->features = octx->features;
  575. ctx->released = false;
  576. ctx->mm = vma->vm_mm;
  577. atomic_inc(&ctx->mm->mm_count);
  578. userfaultfd_ctx_get(octx);
  579. fctx->orig = octx;
  580. fctx->new = ctx;
  581. list_add_tail(&fctx->list, fcs);
  582. }
  583. vma->vm_userfaultfd_ctx.ctx = ctx;
  584. return 0;
  585. }
  586. static void dup_fctx(struct userfaultfd_fork_ctx *fctx)
  587. {
  588. struct userfaultfd_ctx *ctx = fctx->orig;
  589. struct userfaultfd_wait_queue ewq;
  590. msg_init(&ewq.msg);
  591. ewq.msg.event = UFFD_EVENT_FORK;
  592. ewq.msg.arg.reserved.reserved1 = (unsigned long)fctx->new;
  593. userfaultfd_event_wait_completion(ctx, &ewq);
  594. }
  595. void dup_userfaultfd_complete(struct list_head *fcs)
  596. {
  597. struct userfaultfd_fork_ctx *fctx, *n;
  598. list_for_each_entry_safe(fctx, n, fcs, list) {
  599. dup_fctx(fctx);
  600. list_del(&fctx->list);
  601. kfree(fctx);
  602. }
  603. }
  604. void mremap_userfaultfd_prep(struct vm_area_struct *vma,
  605. struct vm_userfaultfd_ctx *vm_ctx)
  606. {
  607. struct userfaultfd_ctx *ctx;
  608. ctx = vma->vm_userfaultfd_ctx.ctx;
  609. if (ctx && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
  610. vm_ctx->ctx = ctx;
  611. userfaultfd_ctx_get(ctx);
  612. }
  613. }
  614. void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
  615. unsigned long from, unsigned long to,
  616. unsigned long len)
  617. {
  618. struct userfaultfd_ctx *ctx = vm_ctx->ctx;
  619. struct userfaultfd_wait_queue ewq;
  620. if (!ctx)
  621. return;
  622. if (to & ~PAGE_MASK) {
  623. userfaultfd_ctx_put(ctx);
  624. return;
  625. }
  626. msg_init(&ewq.msg);
  627. ewq.msg.event = UFFD_EVENT_REMAP;
  628. ewq.msg.arg.remap.from = from;
  629. ewq.msg.arg.remap.to = to;
  630. ewq.msg.arg.remap.len = len;
  631. userfaultfd_event_wait_completion(ctx, &ewq);
  632. }
  633. bool userfaultfd_remove(struct vm_area_struct *vma,
  634. unsigned long start, unsigned long end)
  635. {
  636. struct mm_struct *mm = vma->vm_mm;
  637. struct userfaultfd_ctx *ctx;
  638. struct userfaultfd_wait_queue ewq;
  639. ctx = vma->vm_userfaultfd_ctx.ctx;
  640. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
  641. return true;
  642. userfaultfd_ctx_get(ctx);
  643. up_read(&mm->mmap_sem);
  644. msg_init(&ewq.msg);
  645. ewq.msg.event = UFFD_EVENT_REMOVE;
  646. ewq.msg.arg.remove.start = start;
  647. ewq.msg.arg.remove.end = end;
  648. userfaultfd_event_wait_completion(ctx, &ewq);
  649. return false;
  650. }
  651. static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
  652. unsigned long start, unsigned long end)
  653. {
  654. struct userfaultfd_unmap_ctx *unmap_ctx;
  655. list_for_each_entry(unmap_ctx, unmaps, list)
  656. if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
  657. unmap_ctx->end == end)
  658. return true;
  659. return false;
  660. }
  661. int userfaultfd_unmap_prep(struct vm_area_struct *vma,
  662. unsigned long start, unsigned long end,
  663. struct list_head *unmaps)
  664. {
  665. for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
  666. struct userfaultfd_unmap_ctx *unmap_ctx;
  667. struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
  668. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
  669. has_unmap_ctx(ctx, unmaps, start, end))
  670. continue;
  671. unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
  672. if (!unmap_ctx)
  673. return -ENOMEM;
  674. userfaultfd_ctx_get(ctx);
  675. unmap_ctx->ctx = ctx;
  676. unmap_ctx->start = start;
  677. unmap_ctx->end = end;
  678. list_add_tail(&unmap_ctx->list, unmaps);
  679. }
  680. return 0;
  681. }
  682. void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
  683. {
  684. struct userfaultfd_unmap_ctx *ctx, *n;
  685. struct userfaultfd_wait_queue ewq;
  686. list_for_each_entry_safe(ctx, n, uf, list) {
  687. msg_init(&ewq.msg);
  688. ewq.msg.event = UFFD_EVENT_UNMAP;
  689. ewq.msg.arg.remove.start = ctx->start;
  690. ewq.msg.arg.remove.end = ctx->end;
  691. userfaultfd_event_wait_completion(ctx->ctx, &ewq);
  692. list_del(&ctx->list);
  693. kfree(ctx);
  694. }
  695. }
  696. static int userfaultfd_release(struct inode *inode, struct file *file)
  697. {
  698. struct userfaultfd_ctx *ctx = file->private_data;
  699. struct mm_struct *mm = ctx->mm;
  700. struct vm_area_struct *vma, *prev;
  701. /* len == 0 means wake all */
  702. struct userfaultfd_wake_range range = { .len = 0, };
  703. unsigned long new_flags;
  704. ACCESS_ONCE(ctx->released) = true;
  705. if (!mmget_not_zero(mm))
  706. goto wakeup;
  707. /*
  708. * Flush page faults out of all CPUs. NOTE: all page faults
  709. * must be retried without returning VM_FAULT_SIGBUS if
  710. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  711. * changes while handle_userfault released the mmap_sem. So
  712. * it's critical that released is set to true (above), before
  713. * taking the mmap_sem for writing.
  714. */
  715. down_write(&mm->mmap_sem);
  716. prev = NULL;
  717. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  718. cond_resched();
  719. BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
  720. !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  721. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  722. prev = vma;
  723. continue;
  724. }
  725. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  726. prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
  727. new_flags, vma->anon_vma,
  728. vma->vm_file, vma->vm_pgoff,
  729. vma_policy(vma),
  730. NULL_VM_UFFD_CTX);
  731. if (prev)
  732. vma = prev;
  733. else
  734. prev = vma;
  735. vma->vm_flags = new_flags;
  736. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  737. }
  738. up_write(&mm->mmap_sem);
  739. mmput(mm);
  740. wakeup:
  741. /*
  742. * After no new page faults can wait on this fault_*wqh, flush
  743. * the last page faults that may have been already waiting on
  744. * the fault_*wqh.
  745. */
  746. spin_lock(&ctx->fault_pending_wqh.lock);
  747. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
  748. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
  749. spin_unlock(&ctx->fault_pending_wqh.lock);
  750. wake_up_poll(&ctx->fd_wqh, POLLHUP);
  751. userfaultfd_ctx_put(ctx);
  752. return 0;
  753. }
  754. /* fault_pending_wqh.lock must be hold by the caller */
  755. static inline struct userfaultfd_wait_queue *find_userfault_in(
  756. wait_queue_head_t *wqh)
  757. {
  758. wait_queue_entry_t *wq;
  759. struct userfaultfd_wait_queue *uwq;
  760. VM_BUG_ON(!spin_is_locked(&wqh->lock));
  761. uwq = NULL;
  762. if (!waitqueue_active(wqh))
  763. goto out;
  764. /* walk in reverse to provide FIFO behavior to read userfaults */
  765. wq = list_last_entry(&wqh->task_list, typeof(*wq), task_list);
  766. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  767. out:
  768. return uwq;
  769. }
  770. static inline struct userfaultfd_wait_queue *find_userfault(
  771. struct userfaultfd_ctx *ctx)
  772. {
  773. return find_userfault_in(&ctx->fault_pending_wqh);
  774. }
  775. static inline struct userfaultfd_wait_queue *find_userfault_evt(
  776. struct userfaultfd_ctx *ctx)
  777. {
  778. return find_userfault_in(&ctx->event_wqh);
  779. }
  780. static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
  781. {
  782. struct userfaultfd_ctx *ctx = file->private_data;
  783. unsigned int ret;
  784. poll_wait(file, &ctx->fd_wqh, wait);
  785. switch (ctx->state) {
  786. case UFFD_STATE_WAIT_API:
  787. return POLLERR;
  788. case UFFD_STATE_RUNNING:
  789. /*
  790. * poll() never guarantees that read won't block.
  791. * userfaults can be waken before they're read().
  792. */
  793. if (unlikely(!(file->f_flags & O_NONBLOCK)))
  794. return POLLERR;
  795. /*
  796. * lockless access to see if there are pending faults
  797. * __pollwait last action is the add_wait_queue but
  798. * the spin_unlock would allow the waitqueue_active to
  799. * pass above the actual list_add inside
  800. * add_wait_queue critical section. So use a full
  801. * memory barrier to serialize the list_add write of
  802. * add_wait_queue() with the waitqueue_active read
  803. * below.
  804. */
  805. ret = 0;
  806. smp_mb();
  807. if (waitqueue_active(&ctx->fault_pending_wqh))
  808. ret = POLLIN;
  809. else if (waitqueue_active(&ctx->event_wqh))
  810. ret = POLLIN;
  811. return ret;
  812. default:
  813. WARN_ON_ONCE(1);
  814. return POLLERR;
  815. }
  816. }
  817. static const struct file_operations userfaultfd_fops;
  818. static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
  819. struct userfaultfd_ctx *new,
  820. struct uffd_msg *msg)
  821. {
  822. int fd;
  823. struct file *file;
  824. unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
  825. fd = get_unused_fd_flags(flags);
  826. if (fd < 0)
  827. return fd;
  828. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
  829. O_RDWR | flags);
  830. if (IS_ERR(file)) {
  831. put_unused_fd(fd);
  832. return PTR_ERR(file);
  833. }
  834. fd_install(fd, file);
  835. msg->arg.reserved.reserved1 = 0;
  836. msg->arg.fork.ufd = fd;
  837. return 0;
  838. }
  839. static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
  840. struct uffd_msg *msg)
  841. {
  842. ssize_t ret;
  843. DECLARE_WAITQUEUE(wait, current);
  844. struct userfaultfd_wait_queue *uwq;
  845. /*
  846. * Handling fork event requires sleeping operations, so
  847. * we drop the event_wqh lock, then do these ops, then
  848. * lock it back and wake up the waiter. While the lock is
  849. * dropped the ewq may go away so we keep track of it
  850. * carefully.
  851. */
  852. LIST_HEAD(fork_event);
  853. struct userfaultfd_ctx *fork_nctx = NULL;
  854. /* always take the fd_wqh lock before the fault_pending_wqh lock */
  855. spin_lock(&ctx->fd_wqh.lock);
  856. __add_wait_queue(&ctx->fd_wqh, &wait);
  857. for (;;) {
  858. set_current_state(TASK_INTERRUPTIBLE);
  859. spin_lock(&ctx->fault_pending_wqh.lock);
  860. uwq = find_userfault(ctx);
  861. if (uwq) {
  862. /*
  863. * Use a seqcount to repeat the lockless check
  864. * in wake_userfault() to avoid missing
  865. * wakeups because during the refile both
  866. * waitqueue could become empty if this is the
  867. * only userfault.
  868. */
  869. write_seqcount_begin(&ctx->refile_seq);
  870. /*
  871. * The fault_pending_wqh.lock prevents the uwq
  872. * to disappear from under us.
  873. *
  874. * Refile this userfault from
  875. * fault_pending_wqh to fault_wqh, it's not
  876. * pending anymore after we read it.
  877. *
  878. * Use list_del() by hand (as
  879. * userfaultfd_wake_function also uses
  880. * list_del_init() by hand) to be sure nobody
  881. * changes __remove_wait_queue() to use
  882. * list_del_init() in turn breaking the
  883. * !list_empty_careful() check in
  884. * handle_userfault(). The uwq->wq.task_list
  885. * must never be empty at any time during the
  886. * refile, or the waitqueue could disappear
  887. * from under us. The "wait_queue_head_t"
  888. * parameter of __remove_wait_queue() is unused
  889. * anyway.
  890. */
  891. list_del(&uwq->wq.task_list);
  892. __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
  893. write_seqcount_end(&ctx->refile_seq);
  894. /* careful to always initialize msg if ret == 0 */
  895. *msg = uwq->msg;
  896. spin_unlock(&ctx->fault_pending_wqh.lock);
  897. ret = 0;
  898. break;
  899. }
  900. spin_unlock(&ctx->fault_pending_wqh.lock);
  901. spin_lock(&ctx->event_wqh.lock);
  902. uwq = find_userfault_evt(ctx);
  903. if (uwq) {
  904. *msg = uwq->msg;
  905. if (uwq->msg.event == UFFD_EVENT_FORK) {
  906. fork_nctx = (struct userfaultfd_ctx *)
  907. (unsigned long)
  908. uwq->msg.arg.reserved.reserved1;
  909. list_move(&uwq->wq.task_list, &fork_event);
  910. spin_unlock(&ctx->event_wqh.lock);
  911. ret = 0;
  912. break;
  913. }
  914. userfaultfd_event_complete(ctx, uwq);
  915. spin_unlock(&ctx->event_wqh.lock);
  916. ret = 0;
  917. break;
  918. }
  919. spin_unlock(&ctx->event_wqh.lock);
  920. if (signal_pending(current)) {
  921. ret = -ERESTARTSYS;
  922. break;
  923. }
  924. if (no_wait) {
  925. ret = -EAGAIN;
  926. break;
  927. }
  928. spin_unlock(&ctx->fd_wqh.lock);
  929. schedule();
  930. spin_lock(&ctx->fd_wqh.lock);
  931. }
  932. __remove_wait_queue(&ctx->fd_wqh, &wait);
  933. __set_current_state(TASK_RUNNING);
  934. spin_unlock(&ctx->fd_wqh.lock);
  935. if (!ret && msg->event == UFFD_EVENT_FORK) {
  936. ret = resolve_userfault_fork(ctx, fork_nctx, msg);
  937. if (!ret) {
  938. spin_lock(&ctx->event_wqh.lock);
  939. if (!list_empty(&fork_event)) {
  940. uwq = list_first_entry(&fork_event,
  941. typeof(*uwq),
  942. wq.task_list);
  943. list_del(&uwq->wq.task_list);
  944. __add_wait_queue(&ctx->event_wqh, &uwq->wq);
  945. userfaultfd_event_complete(ctx, uwq);
  946. }
  947. spin_unlock(&ctx->event_wqh.lock);
  948. }
  949. }
  950. return ret;
  951. }
  952. static ssize_t userfaultfd_read(struct file *file, char __user *buf,
  953. size_t count, loff_t *ppos)
  954. {
  955. struct userfaultfd_ctx *ctx = file->private_data;
  956. ssize_t _ret, ret = 0;
  957. struct uffd_msg msg;
  958. int no_wait = file->f_flags & O_NONBLOCK;
  959. if (ctx->state == UFFD_STATE_WAIT_API)
  960. return -EINVAL;
  961. for (;;) {
  962. if (count < sizeof(msg))
  963. return ret ? ret : -EINVAL;
  964. _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
  965. if (_ret < 0)
  966. return ret ? ret : _ret;
  967. if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
  968. return ret ? ret : -EFAULT;
  969. ret += sizeof(msg);
  970. buf += sizeof(msg);
  971. count -= sizeof(msg);
  972. /*
  973. * Allow to read more than one fault at time but only
  974. * block if waiting for the very first one.
  975. */
  976. no_wait = O_NONBLOCK;
  977. }
  978. }
  979. static void __wake_userfault(struct userfaultfd_ctx *ctx,
  980. struct userfaultfd_wake_range *range)
  981. {
  982. unsigned long start, end;
  983. start = range->start;
  984. end = range->start + range->len;
  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.task_list, task_list) {
  1542. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1543. pending++;
  1544. total++;
  1545. }
  1546. list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
  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);