userfaultfd.c 48 KB

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