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 Program-Order guarantees provided by the scheduler
  103. * ensure uwq->waken is visible before the task is woken.
  104. */
  105. ret = wake_up_state(wq->private, mode);
  106. if (ret) {
  107. /*
  108. * Wake only once, autoremove behavior.
  109. *
  110. * After the effect of list_del_init is visible to the other
  111. * CPUs, the waitqueue may disappear from under us, see the
  112. * !list_empty_careful() in handle_userfault().
  113. *
  114. * try_to_wake_up() has an implicit smp_mb(), and the
  115. * wq->private is read before calling the extern function
  116. * "wake_up_state" (which in turns calls try_to_wake_up).
  117. */
  118. list_del_init(&wq->entry);
  119. }
  120. out:
  121. return ret;
  122. }
  123. /**
  124. * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
  125. * context.
  126. * @ctx: [in] Pointer to the userfaultfd context.
  127. */
  128. static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
  129. {
  130. if (!atomic_inc_not_zero(&ctx->refcount))
  131. BUG();
  132. }
  133. /**
  134. * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
  135. * context.
  136. * @ctx: [in] Pointer to userfaultfd context.
  137. *
  138. * The userfaultfd context reference must have been previously acquired either
  139. * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
  140. */
  141. static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
  142. {
  143. if (atomic_dec_and_test(&ctx->refcount)) {
  144. VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
  145. VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
  146. VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
  147. VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
  148. VM_BUG_ON(spin_is_locked(&ctx->event_wqh.lock));
  149. VM_BUG_ON(waitqueue_active(&ctx->event_wqh));
  150. VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
  151. VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
  152. mmdrop(ctx->mm);
  153. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  154. }
  155. }
  156. static inline void msg_init(struct uffd_msg *msg)
  157. {
  158. BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
  159. /*
  160. * Must use memset to zero out the paddings or kernel data is
  161. * leaked to userland.
  162. */
  163. memset(msg, 0, sizeof(struct uffd_msg));
  164. }
  165. static inline struct uffd_msg userfault_msg(unsigned long address,
  166. unsigned int flags,
  167. unsigned long reason)
  168. {
  169. struct uffd_msg msg;
  170. msg_init(&msg);
  171. msg.event = UFFD_EVENT_PAGEFAULT;
  172. msg.arg.pagefault.address = address;
  173. if (flags & FAULT_FLAG_WRITE)
  174. /*
  175. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  176. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
  177. * was not set in a UFFD_EVENT_PAGEFAULT, it means it
  178. * was a read fault, otherwise if set it means it's
  179. * a write fault.
  180. */
  181. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
  182. if (reason & VM_UFFD_WP)
  183. /*
  184. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  185. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
  186. * not set in a UFFD_EVENT_PAGEFAULT, it means it was
  187. * a missing fault, otherwise if set it means it's a
  188. * write protect fault.
  189. */
  190. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
  191. return msg;
  192. }
  193. #ifdef CONFIG_HUGETLB_PAGE
  194. /*
  195. * Same functionality as userfaultfd_must_wait below with modifications for
  196. * hugepmd ranges.
  197. */
  198. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  199. struct vm_area_struct *vma,
  200. unsigned long address,
  201. unsigned long flags,
  202. unsigned long reason)
  203. {
  204. struct mm_struct *mm = ctx->mm;
  205. pte_t *pte;
  206. bool ret = true;
  207. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  208. pte = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
  209. if (!pte)
  210. goto out;
  211. ret = false;
  212. /*
  213. * Lockless access: we're in a wait_event so it's ok if it
  214. * changes under us.
  215. */
  216. if (huge_pte_none(*pte))
  217. ret = true;
  218. if (!huge_pte_write(*pte) && (reason & VM_UFFD_WP))
  219. ret = true;
  220. out:
  221. return ret;
  222. }
  223. #else
  224. static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,
  225. struct vm_area_struct *vma,
  226. unsigned long address,
  227. unsigned long flags,
  228. unsigned long reason)
  229. {
  230. return false; /* should never get here */
  231. }
  232. #endif /* CONFIG_HUGETLB_PAGE */
  233. /*
  234. * Verify the pagetables are still not ok after having reigstered into
  235. * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
  236. * userfault that has already been resolved, if userfaultfd_read and
  237. * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
  238. * threads.
  239. */
  240. static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
  241. unsigned long address,
  242. unsigned long flags,
  243. unsigned long reason)
  244. {
  245. struct mm_struct *mm = ctx->mm;
  246. pgd_t *pgd;
  247. p4d_t *p4d;
  248. pud_t *pud;
  249. pmd_t *pmd, _pmd;
  250. pte_t *pte;
  251. bool ret = true;
  252. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  253. pgd = pgd_offset(mm, address);
  254. if (!pgd_present(*pgd))
  255. goto out;
  256. p4d = p4d_offset(pgd, address);
  257. if (!p4d_present(*p4d))
  258. goto out;
  259. pud = pud_offset(p4d, address);
  260. if (!pud_present(*pud))
  261. goto out;
  262. pmd = pmd_offset(pud, address);
  263. /*
  264. * READ_ONCE must function as a barrier with narrower scope
  265. * and it must be equivalent to:
  266. * _pmd = *pmd; barrier();
  267. *
  268. * This is to deal with the instability (as in
  269. * pmd_trans_unstable) of the pmd.
  270. */
  271. _pmd = READ_ONCE(*pmd);
  272. if (!pmd_present(_pmd))
  273. goto out;
  274. ret = false;
  275. if (pmd_trans_huge(_pmd))
  276. goto out;
  277. /*
  278. * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
  279. * and use the standard pte_offset_map() instead of parsing _pmd.
  280. */
  281. pte = pte_offset_map(pmd, address);
  282. /*
  283. * Lockless access: we're in a wait_event so it's ok if it
  284. * changes under us.
  285. */
  286. if (pte_none(*pte))
  287. ret = true;
  288. pte_unmap(pte);
  289. out:
  290. return ret;
  291. }
  292. /*
  293. * The locking rules involved in returning VM_FAULT_RETRY depending on
  294. * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
  295. * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
  296. * recommendation in __lock_page_or_retry is not an understatement.
  297. *
  298. * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
  299. * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
  300. * not set.
  301. *
  302. * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
  303. * set, VM_FAULT_RETRY can still be returned if and only if there are
  304. * fatal_signal_pending()s, and the mmap_sem must be released before
  305. * returning it.
  306. */
  307. int handle_userfault(struct vm_fault *vmf, unsigned long reason)
  308. {
  309. struct mm_struct *mm = vmf->vma->vm_mm;
  310. struct userfaultfd_ctx *ctx;
  311. struct userfaultfd_wait_queue uwq;
  312. int ret;
  313. bool must_wait, return_to_userland;
  314. long blocking_state;
  315. ret = VM_FAULT_SIGBUS;
  316. /*
  317. * We don't do userfault handling for the final child pid update.
  318. *
  319. * We also don't do userfault handling during
  320. * coredumping. hugetlbfs has the special
  321. * follow_hugetlb_page() to skip missing pages in the
  322. * FOLL_DUMP case, anon memory also checks for FOLL_DUMP with
  323. * the no_page_table() helper in follow_page_mask(), but the
  324. * shmem_vm_ops->fault method is invoked even during
  325. * coredumping without mmap_sem and it ends up here.
  326. */
  327. if (current->flags & (PF_EXITING|PF_DUMPCORE))
  328. goto out;
  329. /*
  330. * Coredumping runs without mmap_sem so we can only check that
  331. * the mmap_sem is held, if PF_DUMPCORE was not set.
  332. */
  333. WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem));
  334. ctx = vmf->vma->vm_userfaultfd_ctx.ctx;
  335. if (!ctx)
  336. goto out;
  337. BUG_ON(ctx->mm != mm);
  338. VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
  339. VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
  340. /*
  341. * If it's already released don't get it. This avoids to loop
  342. * in __get_user_pages if userfaultfd_release waits on the
  343. * caller of handle_userfault to release the mmap_sem.
  344. */
  345. if (unlikely(ACCESS_ONCE(ctx->released)))
  346. goto out;
  347. /*
  348. * Check that we can return VM_FAULT_RETRY.
  349. *
  350. * NOTE: it should become possible to return VM_FAULT_RETRY
  351. * even if FAULT_FLAG_TRIED is set without leading to gup()
  352. * -EBUSY failures, if the userfaultfd is to be extended for
  353. * VM_UFFD_WP tracking and we intend to arm the userfault
  354. * without first stopping userland access to the memory. For
  355. * VM_UFFD_MISSING userfaults this is enough for now.
  356. */
  357. if (unlikely(!(vmf->flags & FAULT_FLAG_ALLOW_RETRY))) {
  358. /*
  359. * Validate the invariant that nowait must allow retry
  360. * to be sure not to return SIGBUS erroneously on
  361. * nowait invocations.
  362. */
  363. BUG_ON(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);
  364. #ifdef CONFIG_DEBUG_VM
  365. if (printk_ratelimit()) {
  366. printk(KERN_WARNING
  367. "FAULT_FLAG_ALLOW_RETRY missing %x\n",
  368. vmf->flags);
  369. dump_stack();
  370. }
  371. #endif
  372. goto out;
  373. }
  374. /*
  375. * Handle nowait, not much to do other than tell it to retry
  376. * and wait.
  377. */
  378. ret = VM_FAULT_RETRY;
  379. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  380. goto out;
  381. /* take the reference before dropping the mmap_sem */
  382. userfaultfd_ctx_get(ctx);
  383. init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
  384. uwq.wq.private = current;
  385. uwq.msg = userfault_msg(vmf->address, vmf->flags, reason);
  386. uwq.ctx = ctx;
  387. uwq.waken = false;
  388. return_to_userland =
  389. (vmf->flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
  390. (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
  391. blocking_state = return_to_userland ? TASK_INTERRUPTIBLE :
  392. TASK_KILLABLE;
  393. spin_lock(&ctx->fault_pending_wqh.lock);
  394. /*
  395. * After the __add_wait_queue the uwq is visible to userland
  396. * through poll/read().
  397. */
  398. __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
  399. /*
  400. * The smp_mb() after __set_current_state prevents the reads
  401. * following the spin_unlock to happen before the list_add in
  402. * __add_wait_queue.
  403. */
  404. set_current_state(blocking_state);
  405. spin_unlock(&ctx->fault_pending_wqh.lock);
  406. if (!is_vm_hugetlb_page(vmf->vma))
  407. must_wait = userfaultfd_must_wait(ctx, vmf->address, vmf->flags,
  408. reason);
  409. else
  410. must_wait = userfaultfd_huge_must_wait(ctx, vmf->vma,
  411. 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.entry)) {
  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.entry);
  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. /* Flush pending events that may still wait on event_wqh */
  751. wake_up_all(&ctx->event_wqh);
  752. wake_up_poll(&ctx->fd_wqh, POLLHUP);
  753. userfaultfd_ctx_put(ctx);
  754. return 0;
  755. }
  756. /* fault_pending_wqh.lock must be hold by the caller */
  757. static inline struct userfaultfd_wait_queue *find_userfault_in(
  758. wait_queue_head_t *wqh)
  759. {
  760. wait_queue_entry_t *wq;
  761. struct userfaultfd_wait_queue *uwq;
  762. VM_BUG_ON(!spin_is_locked(&wqh->lock));
  763. uwq = NULL;
  764. if (!waitqueue_active(wqh))
  765. goto out;
  766. /* walk in reverse to provide FIFO behavior to read userfaults */
  767. wq = list_last_entry(&wqh->head, typeof(*wq), entry);
  768. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  769. out:
  770. return uwq;
  771. }
  772. static inline struct userfaultfd_wait_queue *find_userfault(
  773. struct userfaultfd_ctx *ctx)
  774. {
  775. return find_userfault_in(&ctx->fault_pending_wqh);
  776. }
  777. static inline struct userfaultfd_wait_queue *find_userfault_evt(
  778. struct userfaultfd_ctx *ctx)
  779. {
  780. return find_userfault_in(&ctx->event_wqh);
  781. }
  782. static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
  783. {
  784. struct userfaultfd_ctx *ctx = file->private_data;
  785. unsigned int ret;
  786. poll_wait(file, &ctx->fd_wqh, wait);
  787. switch (ctx->state) {
  788. case UFFD_STATE_WAIT_API:
  789. return POLLERR;
  790. case UFFD_STATE_RUNNING:
  791. /*
  792. * poll() never guarantees that read won't block.
  793. * userfaults can be waken before they're read().
  794. */
  795. if (unlikely(!(file->f_flags & O_NONBLOCK)))
  796. return POLLERR;
  797. /*
  798. * lockless access to see if there are pending faults
  799. * __pollwait last action is the add_wait_queue but
  800. * the spin_unlock would allow the waitqueue_active to
  801. * pass above the actual list_add inside
  802. * add_wait_queue critical section. So use a full
  803. * memory barrier to serialize the list_add write of
  804. * add_wait_queue() with the waitqueue_active read
  805. * below.
  806. */
  807. ret = 0;
  808. smp_mb();
  809. if (waitqueue_active(&ctx->fault_pending_wqh))
  810. ret = POLLIN;
  811. else if (waitqueue_active(&ctx->event_wqh))
  812. ret = POLLIN;
  813. return ret;
  814. default:
  815. WARN_ON_ONCE(1);
  816. return POLLERR;
  817. }
  818. }
  819. static const struct file_operations userfaultfd_fops;
  820. static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
  821. struct userfaultfd_ctx *new,
  822. struct uffd_msg *msg)
  823. {
  824. int fd;
  825. struct file *file;
  826. unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS;
  827. fd = get_unused_fd_flags(flags);
  828. if (fd < 0)
  829. return fd;
  830. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new,
  831. O_RDWR | flags);
  832. if (IS_ERR(file)) {
  833. put_unused_fd(fd);
  834. return PTR_ERR(file);
  835. }
  836. fd_install(fd, file);
  837. msg->arg.reserved.reserved1 = 0;
  838. msg->arg.fork.ufd = fd;
  839. return 0;
  840. }
  841. static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
  842. struct uffd_msg *msg)
  843. {
  844. ssize_t ret;
  845. DECLARE_WAITQUEUE(wait, current);
  846. struct userfaultfd_wait_queue *uwq;
  847. /*
  848. * Handling fork event requires sleeping operations, so
  849. * we drop the event_wqh lock, then do these ops, then
  850. * lock it back and wake up the waiter. While the lock is
  851. * dropped the ewq may go away so we keep track of it
  852. * carefully.
  853. */
  854. LIST_HEAD(fork_event);
  855. struct userfaultfd_ctx *fork_nctx = NULL;
  856. /* always take the fd_wqh lock before the fault_pending_wqh lock */
  857. spin_lock(&ctx->fd_wqh.lock);
  858. __add_wait_queue(&ctx->fd_wqh, &wait);
  859. for (;;) {
  860. set_current_state(TASK_INTERRUPTIBLE);
  861. spin_lock(&ctx->fault_pending_wqh.lock);
  862. uwq = find_userfault(ctx);
  863. if (uwq) {
  864. /*
  865. * Use a seqcount to repeat the lockless check
  866. * in wake_userfault() to avoid missing
  867. * wakeups because during the refile both
  868. * waitqueue could become empty if this is the
  869. * only userfault.
  870. */
  871. write_seqcount_begin(&ctx->refile_seq);
  872. /*
  873. * The fault_pending_wqh.lock prevents the uwq
  874. * to disappear from under us.
  875. *
  876. * Refile this userfault from
  877. * fault_pending_wqh to fault_wqh, it's not
  878. * pending anymore after we read it.
  879. *
  880. * Use list_del() by hand (as
  881. * userfaultfd_wake_function also uses
  882. * list_del_init() by hand) to be sure nobody
  883. * changes __remove_wait_queue() to use
  884. * list_del_init() in turn breaking the
  885. * !list_empty_careful() check in
  886. * handle_userfault(). The uwq->wq.head list
  887. * must never be empty at any time during the
  888. * refile, or the waitqueue could disappear
  889. * from under us. The "wait_queue_head_t"
  890. * parameter of __remove_wait_queue() is unused
  891. * anyway.
  892. */
  893. list_del(&uwq->wq.entry);
  894. __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
  895. write_seqcount_end(&ctx->refile_seq);
  896. /* careful to always initialize msg if ret == 0 */
  897. *msg = uwq->msg;
  898. spin_unlock(&ctx->fault_pending_wqh.lock);
  899. ret = 0;
  900. break;
  901. }
  902. spin_unlock(&ctx->fault_pending_wqh.lock);
  903. spin_lock(&ctx->event_wqh.lock);
  904. uwq = find_userfault_evt(ctx);
  905. if (uwq) {
  906. *msg = uwq->msg;
  907. if (uwq->msg.event == UFFD_EVENT_FORK) {
  908. fork_nctx = (struct userfaultfd_ctx *)
  909. (unsigned long)
  910. uwq->msg.arg.reserved.reserved1;
  911. list_move(&uwq->wq.entry, &fork_event);
  912. spin_unlock(&ctx->event_wqh.lock);
  913. ret = 0;
  914. break;
  915. }
  916. userfaultfd_event_complete(ctx, uwq);
  917. spin_unlock(&ctx->event_wqh.lock);
  918. ret = 0;
  919. break;
  920. }
  921. spin_unlock(&ctx->event_wqh.lock);
  922. if (signal_pending(current)) {
  923. ret = -ERESTARTSYS;
  924. break;
  925. }
  926. if (no_wait) {
  927. ret = -EAGAIN;
  928. break;
  929. }
  930. spin_unlock(&ctx->fd_wqh.lock);
  931. schedule();
  932. spin_lock(&ctx->fd_wqh.lock);
  933. }
  934. __remove_wait_queue(&ctx->fd_wqh, &wait);
  935. __set_current_state(TASK_RUNNING);
  936. spin_unlock(&ctx->fd_wqh.lock);
  937. if (!ret && msg->event == UFFD_EVENT_FORK) {
  938. ret = resolve_userfault_fork(ctx, fork_nctx, msg);
  939. if (!ret) {
  940. spin_lock(&ctx->event_wqh.lock);
  941. if (!list_empty(&fork_event)) {
  942. uwq = list_first_entry(&fork_event,
  943. typeof(*uwq),
  944. wq.entry);
  945. list_del(&uwq->wq.entry);
  946. __add_wait_queue(&ctx->event_wqh, &uwq->wq);
  947. userfaultfd_event_complete(ctx, uwq);
  948. }
  949. spin_unlock(&ctx->event_wqh.lock);
  950. }
  951. }
  952. return ret;
  953. }
  954. static ssize_t userfaultfd_read(struct file *file, char __user *buf,
  955. size_t count, loff_t *ppos)
  956. {
  957. struct userfaultfd_ctx *ctx = file->private_data;
  958. ssize_t _ret, ret = 0;
  959. struct uffd_msg msg;
  960. int no_wait = file->f_flags & O_NONBLOCK;
  961. if (ctx->state == UFFD_STATE_WAIT_API)
  962. return -EINVAL;
  963. for (;;) {
  964. if (count < sizeof(msg))
  965. return ret ? ret : -EINVAL;
  966. _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
  967. if (_ret < 0)
  968. return ret ? ret : _ret;
  969. if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
  970. return ret ? ret : -EFAULT;
  971. ret += sizeof(msg);
  972. buf += sizeof(msg);
  973. count -= sizeof(msg);
  974. /*
  975. * Allow to read more than one fault at time but only
  976. * block if waiting for the very first one.
  977. */
  978. no_wait = O_NONBLOCK;
  979. }
  980. }
  981. static void __wake_userfault(struct userfaultfd_ctx *ctx,
  982. struct userfaultfd_wake_range *range)
  983. {
  984. spin_lock(&ctx->fault_pending_wqh.lock);
  985. /* wake all in the range and autoremove */
  986. if (waitqueue_active(&ctx->fault_pending_wqh))
  987. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
  988. range);
  989. if (waitqueue_active(&ctx->fault_wqh))
  990. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
  991. spin_unlock(&ctx->fault_pending_wqh.lock);
  992. }
  993. static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
  994. struct userfaultfd_wake_range *range)
  995. {
  996. unsigned seq;
  997. bool need_wakeup;
  998. /*
  999. * To be sure waitqueue_active() is not reordered by the CPU
  1000. * before the pagetable update, use an explicit SMP memory
  1001. * barrier here. PT lock release or up_read(mmap_sem) still
  1002. * have release semantics that can allow the
  1003. * waitqueue_active() to be reordered before the pte update.
  1004. */
  1005. smp_mb();
  1006. /*
  1007. * Use waitqueue_active because it's very frequent to
  1008. * change the address space atomically even if there are no
  1009. * userfaults yet. So we take the spinlock only when we're
  1010. * sure we've userfaults to wake.
  1011. */
  1012. do {
  1013. seq = read_seqcount_begin(&ctx->refile_seq);
  1014. need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
  1015. waitqueue_active(&ctx->fault_wqh);
  1016. cond_resched();
  1017. } while (read_seqcount_retry(&ctx->refile_seq, seq));
  1018. if (need_wakeup)
  1019. __wake_userfault(ctx, range);
  1020. }
  1021. static __always_inline int validate_range(struct mm_struct *mm,
  1022. __u64 start, __u64 len)
  1023. {
  1024. __u64 task_size = mm->task_size;
  1025. if (start & ~PAGE_MASK)
  1026. return -EINVAL;
  1027. if (len & ~PAGE_MASK)
  1028. return -EINVAL;
  1029. if (!len)
  1030. return -EINVAL;
  1031. if (start < mmap_min_addr)
  1032. return -EINVAL;
  1033. if (start >= task_size)
  1034. return -EINVAL;
  1035. if (len > task_size - start)
  1036. return -EINVAL;
  1037. return 0;
  1038. }
  1039. static inline bool vma_can_userfault(struct vm_area_struct *vma)
  1040. {
  1041. return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
  1042. vma_is_shmem(vma);
  1043. }
  1044. static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  1045. unsigned long arg)
  1046. {
  1047. struct mm_struct *mm = ctx->mm;
  1048. struct vm_area_struct *vma, *prev, *cur;
  1049. int ret;
  1050. struct uffdio_register uffdio_register;
  1051. struct uffdio_register __user *user_uffdio_register;
  1052. unsigned long vm_flags, new_flags;
  1053. bool found;
  1054. bool non_anon_pages;
  1055. unsigned long start, end, vma_end;
  1056. user_uffdio_register = (struct uffdio_register __user *) arg;
  1057. ret = -EFAULT;
  1058. if (copy_from_user(&uffdio_register, user_uffdio_register,
  1059. sizeof(uffdio_register)-sizeof(__u64)))
  1060. goto out;
  1061. ret = -EINVAL;
  1062. if (!uffdio_register.mode)
  1063. goto out;
  1064. if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
  1065. UFFDIO_REGISTER_MODE_WP))
  1066. goto out;
  1067. vm_flags = 0;
  1068. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  1069. vm_flags |= VM_UFFD_MISSING;
  1070. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
  1071. vm_flags |= VM_UFFD_WP;
  1072. /*
  1073. * FIXME: remove the below error constraint by
  1074. * implementing the wprotect tracking mode.
  1075. */
  1076. ret = -EINVAL;
  1077. goto out;
  1078. }
  1079. ret = validate_range(mm, uffdio_register.range.start,
  1080. uffdio_register.range.len);
  1081. if (ret)
  1082. goto out;
  1083. start = uffdio_register.range.start;
  1084. end = start + uffdio_register.range.len;
  1085. ret = -ENOMEM;
  1086. if (!mmget_not_zero(mm))
  1087. goto out;
  1088. down_write(&mm->mmap_sem);
  1089. vma = find_vma_prev(mm, start, &prev);
  1090. if (!vma)
  1091. goto out_unlock;
  1092. /* check that there's at least one vma in the range */
  1093. ret = -EINVAL;
  1094. if (vma->vm_start >= end)
  1095. goto out_unlock;
  1096. /*
  1097. * If the first vma contains huge pages, make sure start address
  1098. * is aligned to huge page size.
  1099. */
  1100. if (is_vm_hugetlb_page(vma)) {
  1101. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1102. if (start & (vma_hpagesize - 1))
  1103. goto out_unlock;
  1104. }
  1105. /*
  1106. * Search for not compatible vmas.
  1107. */
  1108. found = false;
  1109. non_anon_pages = false;
  1110. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1111. cond_resched();
  1112. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1113. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1114. /* check not compatible vmas */
  1115. ret = -EINVAL;
  1116. if (!vma_can_userfault(cur))
  1117. goto out_unlock;
  1118. /*
  1119. * If this vma contains ending address, and huge pages
  1120. * check alignment.
  1121. */
  1122. if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
  1123. end > cur->vm_start) {
  1124. unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
  1125. ret = -EINVAL;
  1126. if (end & (vma_hpagesize - 1))
  1127. goto out_unlock;
  1128. }
  1129. /*
  1130. * Check that this vma isn't already owned by a
  1131. * different userfaultfd. We can't allow more than one
  1132. * userfaultfd to own a single vma simultaneously or we
  1133. * wouldn't know which one to deliver the userfaults to.
  1134. */
  1135. ret = -EBUSY;
  1136. if (cur->vm_userfaultfd_ctx.ctx &&
  1137. cur->vm_userfaultfd_ctx.ctx != ctx)
  1138. goto out_unlock;
  1139. /*
  1140. * Note vmas containing huge pages
  1141. */
  1142. if (is_vm_hugetlb_page(cur) || vma_is_shmem(cur))
  1143. non_anon_pages = true;
  1144. found = true;
  1145. }
  1146. BUG_ON(!found);
  1147. if (vma->vm_start < start)
  1148. prev = vma;
  1149. ret = 0;
  1150. do {
  1151. cond_resched();
  1152. BUG_ON(!vma_can_userfault(vma));
  1153. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  1154. vma->vm_userfaultfd_ctx.ctx != ctx);
  1155. /*
  1156. * Nothing to do: this vma is already registered into this
  1157. * userfaultfd and with the right tracking mode too.
  1158. */
  1159. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  1160. (vma->vm_flags & vm_flags) == vm_flags)
  1161. goto skip;
  1162. if (vma->vm_start > start)
  1163. start = vma->vm_start;
  1164. vma_end = min(end, vma->vm_end);
  1165. new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
  1166. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1167. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1168. vma_policy(vma),
  1169. ((struct vm_userfaultfd_ctx){ ctx }));
  1170. if (prev) {
  1171. vma = prev;
  1172. goto next;
  1173. }
  1174. if (vma->vm_start < start) {
  1175. ret = split_vma(mm, vma, start, 1);
  1176. if (ret)
  1177. break;
  1178. }
  1179. if (vma->vm_end > end) {
  1180. ret = split_vma(mm, vma, end, 0);
  1181. if (ret)
  1182. break;
  1183. }
  1184. next:
  1185. /*
  1186. * In the vma_merge() successful mprotect-like case 8:
  1187. * the next vma was merged into the current one and
  1188. * the current one has not been updated yet.
  1189. */
  1190. vma->vm_flags = new_flags;
  1191. vma->vm_userfaultfd_ctx.ctx = ctx;
  1192. skip:
  1193. prev = vma;
  1194. start = vma->vm_end;
  1195. vma = vma->vm_next;
  1196. } while (vma && vma->vm_start < end);
  1197. out_unlock:
  1198. up_write(&mm->mmap_sem);
  1199. mmput(mm);
  1200. if (!ret) {
  1201. /*
  1202. * Now that we scanned all vmas we can already tell
  1203. * userland which ioctls methods are guaranteed to
  1204. * succeed on this range.
  1205. */
  1206. if (put_user(non_anon_pages ? UFFD_API_RANGE_IOCTLS_BASIC :
  1207. UFFD_API_RANGE_IOCTLS,
  1208. &user_uffdio_register->ioctls))
  1209. ret = -EFAULT;
  1210. }
  1211. out:
  1212. return ret;
  1213. }
  1214. static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
  1215. unsigned long arg)
  1216. {
  1217. struct mm_struct *mm = ctx->mm;
  1218. struct vm_area_struct *vma, *prev, *cur;
  1219. int ret;
  1220. struct uffdio_range uffdio_unregister;
  1221. unsigned long new_flags;
  1222. bool found;
  1223. unsigned long start, end, vma_end;
  1224. const void __user *buf = (void __user *)arg;
  1225. ret = -EFAULT;
  1226. if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
  1227. goto out;
  1228. ret = validate_range(mm, uffdio_unregister.start,
  1229. uffdio_unregister.len);
  1230. if (ret)
  1231. goto out;
  1232. start = uffdio_unregister.start;
  1233. end = start + uffdio_unregister.len;
  1234. ret = -ENOMEM;
  1235. if (!mmget_not_zero(mm))
  1236. goto out;
  1237. down_write(&mm->mmap_sem);
  1238. vma = find_vma_prev(mm, start, &prev);
  1239. if (!vma)
  1240. goto out_unlock;
  1241. /* check that there's at least one vma in the range */
  1242. ret = -EINVAL;
  1243. if (vma->vm_start >= end)
  1244. goto out_unlock;
  1245. /*
  1246. * If the first vma contains huge pages, make sure start address
  1247. * is aligned to huge page size.
  1248. */
  1249. if (is_vm_hugetlb_page(vma)) {
  1250. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1251. if (start & (vma_hpagesize - 1))
  1252. goto out_unlock;
  1253. }
  1254. /*
  1255. * Search for not compatible vmas.
  1256. */
  1257. found = false;
  1258. ret = -EINVAL;
  1259. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1260. cond_resched();
  1261. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1262. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1263. /*
  1264. * Check not compatible vmas, not strictly required
  1265. * here as not compatible vmas cannot have an
  1266. * userfaultfd_ctx registered on them, but this
  1267. * provides for more strict behavior to notice
  1268. * unregistration errors.
  1269. */
  1270. if (!vma_can_userfault(cur))
  1271. goto out_unlock;
  1272. found = true;
  1273. }
  1274. BUG_ON(!found);
  1275. if (vma->vm_start < start)
  1276. prev = vma;
  1277. ret = 0;
  1278. do {
  1279. cond_resched();
  1280. BUG_ON(!vma_can_userfault(vma));
  1281. /*
  1282. * Nothing to do: this vma is already registered into this
  1283. * userfaultfd and with the right tracking mode too.
  1284. */
  1285. if (!vma->vm_userfaultfd_ctx.ctx)
  1286. goto skip;
  1287. if (vma->vm_start > start)
  1288. start = vma->vm_start;
  1289. vma_end = min(end, vma->vm_end);
  1290. if (userfaultfd_missing(vma)) {
  1291. /*
  1292. * Wake any concurrent pending userfault while
  1293. * we unregister, so they will not hang
  1294. * permanently and it avoids userland to call
  1295. * UFFDIO_WAKE explicitly.
  1296. */
  1297. struct userfaultfd_wake_range range;
  1298. range.start = start;
  1299. range.len = vma_end - start;
  1300. wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
  1301. }
  1302. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  1303. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1304. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1305. vma_policy(vma),
  1306. NULL_VM_UFFD_CTX);
  1307. if (prev) {
  1308. vma = prev;
  1309. goto next;
  1310. }
  1311. if (vma->vm_start < start) {
  1312. ret = split_vma(mm, vma, start, 1);
  1313. if (ret)
  1314. break;
  1315. }
  1316. if (vma->vm_end > end) {
  1317. ret = split_vma(mm, vma, end, 0);
  1318. if (ret)
  1319. break;
  1320. }
  1321. next:
  1322. /*
  1323. * In the vma_merge() successful mprotect-like case 8:
  1324. * the next vma was merged into the current one and
  1325. * the current one has not been updated yet.
  1326. */
  1327. vma->vm_flags = new_flags;
  1328. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  1329. skip:
  1330. prev = vma;
  1331. start = vma->vm_end;
  1332. vma = vma->vm_next;
  1333. } while (vma && vma->vm_start < end);
  1334. out_unlock:
  1335. up_write(&mm->mmap_sem);
  1336. mmput(mm);
  1337. out:
  1338. return ret;
  1339. }
  1340. /*
  1341. * userfaultfd_wake may be used in combination with the
  1342. * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
  1343. */
  1344. static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
  1345. unsigned long arg)
  1346. {
  1347. int ret;
  1348. struct uffdio_range uffdio_wake;
  1349. struct userfaultfd_wake_range range;
  1350. const void __user *buf = (void __user *)arg;
  1351. ret = -EFAULT;
  1352. if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
  1353. goto out;
  1354. ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
  1355. if (ret)
  1356. goto out;
  1357. range.start = uffdio_wake.start;
  1358. range.len = uffdio_wake.len;
  1359. /*
  1360. * len == 0 means wake all and we don't want to wake all here,
  1361. * so check it again to be sure.
  1362. */
  1363. VM_BUG_ON(!range.len);
  1364. wake_userfault(ctx, &range);
  1365. ret = 0;
  1366. out:
  1367. return ret;
  1368. }
  1369. static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
  1370. unsigned long arg)
  1371. {
  1372. __s64 ret;
  1373. struct uffdio_copy uffdio_copy;
  1374. struct uffdio_copy __user *user_uffdio_copy;
  1375. struct userfaultfd_wake_range range;
  1376. user_uffdio_copy = (struct uffdio_copy __user *) arg;
  1377. ret = -EFAULT;
  1378. if (copy_from_user(&uffdio_copy, user_uffdio_copy,
  1379. /* don't copy "copy" last field */
  1380. sizeof(uffdio_copy)-sizeof(__s64)))
  1381. goto out;
  1382. ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
  1383. if (ret)
  1384. goto out;
  1385. /*
  1386. * double check for wraparound just in case. copy_from_user()
  1387. * will later check uffdio_copy.src + uffdio_copy.len to fit
  1388. * in the userland range.
  1389. */
  1390. ret = -EINVAL;
  1391. if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
  1392. goto out;
  1393. if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
  1394. goto out;
  1395. if (mmget_not_zero(ctx->mm)) {
  1396. ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
  1397. uffdio_copy.len);
  1398. mmput(ctx->mm);
  1399. } else {
  1400. return -ENOSPC;
  1401. }
  1402. if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
  1403. return -EFAULT;
  1404. if (ret < 0)
  1405. goto out;
  1406. BUG_ON(!ret);
  1407. /* len == 0 would wake all */
  1408. range.len = ret;
  1409. if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
  1410. range.start = uffdio_copy.dst;
  1411. wake_userfault(ctx, &range);
  1412. }
  1413. ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
  1414. out:
  1415. return ret;
  1416. }
  1417. static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
  1418. unsigned long arg)
  1419. {
  1420. __s64 ret;
  1421. struct uffdio_zeropage uffdio_zeropage;
  1422. struct uffdio_zeropage __user *user_uffdio_zeropage;
  1423. struct userfaultfd_wake_range range;
  1424. user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
  1425. ret = -EFAULT;
  1426. if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
  1427. /* don't copy "zeropage" last field */
  1428. sizeof(uffdio_zeropage)-sizeof(__s64)))
  1429. goto out;
  1430. ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
  1431. uffdio_zeropage.range.len);
  1432. if (ret)
  1433. goto out;
  1434. ret = -EINVAL;
  1435. if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
  1436. goto out;
  1437. if (mmget_not_zero(ctx->mm)) {
  1438. ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
  1439. uffdio_zeropage.range.len);
  1440. mmput(ctx->mm);
  1441. } else {
  1442. return -ENOSPC;
  1443. }
  1444. if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
  1445. return -EFAULT;
  1446. if (ret < 0)
  1447. goto out;
  1448. /* len == 0 would wake all */
  1449. BUG_ON(!ret);
  1450. range.len = ret;
  1451. if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
  1452. range.start = uffdio_zeropage.range.start;
  1453. wake_userfault(ctx, &range);
  1454. }
  1455. ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
  1456. out:
  1457. return ret;
  1458. }
  1459. static inline unsigned int uffd_ctx_features(__u64 user_features)
  1460. {
  1461. /*
  1462. * For the current set of features the bits just coincide
  1463. */
  1464. return (unsigned int)user_features;
  1465. }
  1466. /*
  1467. * userland asks for a certain API version and we return which bits
  1468. * and ioctl commands are implemented in this kernel for such API
  1469. * version or -EINVAL if unknown.
  1470. */
  1471. static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  1472. unsigned long arg)
  1473. {
  1474. struct uffdio_api uffdio_api;
  1475. void __user *buf = (void __user *)arg;
  1476. int ret;
  1477. __u64 features;
  1478. ret = -EINVAL;
  1479. if (ctx->state != UFFD_STATE_WAIT_API)
  1480. goto out;
  1481. ret = -EFAULT;
  1482. if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
  1483. goto out;
  1484. features = uffdio_api.features;
  1485. if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
  1486. memset(&uffdio_api, 0, sizeof(uffdio_api));
  1487. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1488. goto out;
  1489. ret = -EINVAL;
  1490. goto out;
  1491. }
  1492. /* report all available features and ioctls to userland */
  1493. uffdio_api.features = UFFD_API_FEATURES;
  1494. uffdio_api.ioctls = UFFD_API_IOCTLS;
  1495. ret = -EFAULT;
  1496. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1497. goto out;
  1498. ctx->state = UFFD_STATE_RUNNING;
  1499. /* only enable the requested features for this uffd context */
  1500. ctx->features = uffd_ctx_features(features);
  1501. ret = 0;
  1502. out:
  1503. return ret;
  1504. }
  1505. static long userfaultfd_ioctl(struct file *file, unsigned cmd,
  1506. unsigned long arg)
  1507. {
  1508. int ret = -EINVAL;
  1509. struct userfaultfd_ctx *ctx = file->private_data;
  1510. if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
  1511. return -EINVAL;
  1512. switch(cmd) {
  1513. case UFFDIO_API:
  1514. ret = userfaultfd_api(ctx, arg);
  1515. break;
  1516. case UFFDIO_REGISTER:
  1517. ret = userfaultfd_register(ctx, arg);
  1518. break;
  1519. case UFFDIO_UNREGISTER:
  1520. ret = userfaultfd_unregister(ctx, arg);
  1521. break;
  1522. case UFFDIO_WAKE:
  1523. ret = userfaultfd_wake(ctx, arg);
  1524. break;
  1525. case UFFDIO_COPY:
  1526. ret = userfaultfd_copy(ctx, arg);
  1527. break;
  1528. case UFFDIO_ZEROPAGE:
  1529. ret = userfaultfd_zeropage(ctx, arg);
  1530. break;
  1531. }
  1532. return ret;
  1533. }
  1534. #ifdef CONFIG_PROC_FS
  1535. static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
  1536. {
  1537. struct userfaultfd_ctx *ctx = f->private_data;
  1538. wait_queue_entry_t *wq;
  1539. struct userfaultfd_wait_queue *uwq;
  1540. unsigned long pending = 0, total = 0;
  1541. spin_lock(&ctx->fault_pending_wqh.lock);
  1542. list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
  1543. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1544. pending++;
  1545. total++;
  1546. }
  1547. list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
  1548. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1549. total++;
  1550. }
  1551. spin_unlock(&ctx->fault_pending_wqh.lock);
  1552. /*
  1553. * If more protocols will be added, there will be all shown
  1554. * separated by a space. Like this:
  1555. * protocols: aa:... bb:...
  1556. */
  1557. seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
  1558. pending, total, UFFD_API, ctx->features,
  1559. UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
  1560. }
  1561. #endif
  1562. static const struct file_operations userfaultfd_fops = {
  1563. #ifdef CONFIG_PROC_FS
  1564. .show_fdinfo = userfaultfd_show_fdinfo,
  1565. #endif
  1566. .release = userfaultfd_release,
  1567. .poll = userfaultfd_poll,
  1568. .read = userfaultfd_read,
  1569. .unlocked_ioctl = userfaultfd_ioctl,
  1570. .compat_ioctl = userfaultfd_ioctl,
  1571. .llseek = noop_llseek,
  1572. };
  1573. static void init_once_userfaultfd_ctx(void *mem)
  1574. {
  1575. struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
  1576. init_waitqueue_head(&ctx->fault_pending_wqh);
  1577. init_waitqueue_head(&ctx->fault_wqh);
  1578. init_waitqueue_head(&ctx->event_wqh);
  1579. init_waitqueue_head(&ctx->fd_wqh);
  1580. seqcount_init(&ctx->refile_seq);
  1581. }
  1582. /**
  1583. * userfaultfd_file_create - Creates a userfaultfd file pointer.
  1584. * @flags: Flags for the userfaultfd file.
  1585. *
  1586. * This function creates a userfaultfd file pointer, w/out installing
  1587. * it into the fd table. This is useful when the userfaultfd file is
  1588. * used during the initialization of data structures that require
  1589. * extra setup after the userfaultfd creation. So the userfaultfd
  1590. * creation is split into the file pointer creation phase, and the
  1591. * file descriptor installation phase. In this way races with
  1592. * userspace closing the newly installed file descriptor can be
  1593. * avoided. Returns a userfaultfd file pointer, or a proper error
  1594. * pointer.
  1595. */
  1596. static struct file *userfaultfd_file_create(int flags)
  1597. {
  1598. struct file *file;
  1599. struct userfaultfd_ctx *ctx;
  1600. BUG_ON(!current->mm);
  1601. /* Check the UFFD_* constants for consistency. */
  1602. BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
  1603. BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
  1604. file = ERR_PTR(-EINVAL);
  1605. if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
  1606. goto out;
  1607. file = ERR_PTR(-ENOMEM);
  1608. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  1609. if (!ctx)
  1610. goto out;
  1611. atomic_set(&ctx->refcount, 1);
  1612. ctx->flags = flags;
  1613. ctx->features = 0;
  1614. ctx->state = UFFD_STATE_WAIT_API;
  1615. ctx->released = false;
  1616. ctx->mm = current->mm;
  1617. /* prevent the mm struct to be freed */
  1618. mmgrab(ctx->mm);
  1619. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
  1620. O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
  1621. if (IS_ERR(file)) {
  1622. mmdrop(ctx->mm);
  1623. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  1624. }
  1625. out:
  1626. return file;
  1627. }
  1628. SYSCALL_DEFINE1(userfaultfd, int, flags)
  1629. {
  1630. int fd, error;
  1631. struct file *file;
  1632. error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
  1633. if (error < 0)
  1634. return error;
  1635. fd = error;
  1636. file = userfaultfd_file_create(flags);
  1637. if (IS_ERR(file)) {
  1638. error = PTR_ERR(file);
  1639. goto err_put_unused_fd;
  1640. }
  1641. fd_install(fd, file);
  1642. return fd;
  1643. err_put_unused_fd:
  1644. put_unused_fd(fd);
  1645. return error;
  1646. }
  1647. static int __init userfaultfd_init(void)
  1648. {
  1649. userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
  1650. sizeof(struct userfaultfd_ctx),
  1651. 0,
  1652. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1653. init_once_userfaultfd_ctx);
  1654. return 0;
  1655. }
  1656. __initcall(userfaultfd_init);