userfaultfd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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/hashtable.h>
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/file.h>
  21. #include <linux/bug.h>
  22. #include <linux/anon_inodes.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/userfaultfd_k.h>
  25. #include <linux/mempolicy.h>
  26. #include <linux/ioctl.h>
  27. #include <linux/security.h>
  28. static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
  29. enum userfaultfd_state {
  30. UFFD_STATE_WAIT_API,
  31. UFFD_STATE_RUNNING,
  32. };
  33. /*
  34. * Start with fault_pending_wqh and fault_wqh so they're more likely
  35. * to be in the same cacheline.
  36. */
  37. struct userfaultfd_ctx {
  38. /* waitqueue head for the pending (i.e. not read) userfaults */
  39. wait_queue_head_t fault_pending_wqh;
  40. /* waitqueue head for the userfaults */
  41. wait_queue_head_t fault_wqh;
  42. /* waitqueue head for the pseudo fd to wakeup poll/read */
  43. wait_queue_head_t fd_wqh;
  44. /* a refile sequence protected by fault_pending_wqh lock */
  45. struct seqcount refile_seq;
  46. /* pseudo fd refcounting */
  47. atomic_t refcount;
  48. /* userfaultfd syscall flags */
  49. unsigned int flags;
  50. /* state machine */
  51. enum userfaultfd_state state;
  52. /* released */
  53. bool released;
  54. /* mm with one ore more vmas attached to this userfaultfd_ctx */
  55. struct mm_struct *mm;
  56. };
  57. struct userfaultfd_wait_queue {
  58. struct uffd_msg msg;
  59. wait_queue_t wq;
  60. struct userfaultfd_ctx *ctx;
  61. };
  62. struct userfaultfd_wake_range {
  63. unsigned long start;
  64. unsigned long len;
  65. };
  66. static int userfaultfd_wake_function(wait_queue_t *wq, unsigned mode,
  67. int wake_flags, void *key)
  68. {
  69. struct userfaultfd_wake_range *range = key;
  70. int ret;
  71. struct userfaultfd_wait_queue *uwq;
  72. unsigned long start, len;
  73. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  74. ret = 0;
  75. /* len == 0 means wake all */
  76. start = range->start;
  77. len = range->len;
  78. if (len && (start > uwq->msg.arg.pagefault.address ||
  79. start + len <= uwq->msg.arg.pagefault.address))
  80. goto out;
  81. ret = wake_up_state(wq->private, mode);
  82. if (ret)
  83. /*
  84. * Wake only once, autoremove behavior.
  85. *
  86. * After the effect of list_del_init is visible to the
  87. * other CPUs, the waitqueue may disappear from under
  88. * us, see the !list_empty_careful() in
  89. * handle_userfault(). try_to_wake_up() has an
  90. * implicit smp_mb__before_spinlock, and the
  91. * wq->private is read before calling the extern
  92. * function "wake_up_state" (which in turns calls
  93. * try_to_wake_up). While the spin_lock;spin_unlock;
  94. * wouldn't be enough, the smp_mb__before_spinlock is
  95. * enough to avoid an explicit smp_mb() here.
  96. */
  97. list_del_init(&wq->task_list);
  98. out:
  99. return ret;
  100. }
  101. /**
  102. * userfaultfd_ctx_get - Acquires a reference to the internal userfaultfd
  103. * context.
  104. * @ctx: [in] Pointer to the userfaultfd context.
  105. *
  106. * Returns: In case of success, returns not zero.
  107. */
  108. static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx)
  109. {
  110. if (!atomic_inc_not_zero(&ctx->refcount))
  111. BUG();
  112. }
  113. /**
  114. * userfaultfd_ctx_put - Releases a reference to the internal userfaultfd
  115. * context.
  116. * @ctx: [in] Pointer to userfaultfd context.
  117. *
  118. * The userfaultfd context reference must have been previously acquired either
  119. * with userfaultfd_ctx_get() or userfaultfd_ctx_fdget().
  120. */
  121. static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx)
  122. {
  123. if (atomic_dec_and_test(&ctx->refcount)) {
  124. VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock));
  125. VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh));
  126. VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock));
  127. VM_BUG_ON(waitqueue_active(&ctx->fault_wqh));
  128. VM_BUG_ON(spin_is_locked(&ctx->fd_wqh.lock));
  129. VM_BUG_ON(waitqueue_active(&ctx->fd_wqh));
  130. mmput(ctx->mm);
  131. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  132. }
  133. }
  134. static inline void msg_init(struct uffd_msg *msg)
  135. {
  136. BUILD_BUG_ON(sizeof(struct uffd_msg) != 32);
  137. /*
  138. * Must use memset to zero out the paddings or kernel data is
  139. * leaked to userland.
  140. */
  141. memset(msg, 0, sizeof(struct uffd_msg));
  142. }
  143. static inline struct uffd_msg userfault_msg(unsigned long address,
  144. unsigned int flags,
  145. unsigned long reason)
  146. {
  147. struct uffd_msg msg;
  148. msg_init(&msg);
  149. msg.event = UFFD_EVENT_PAGEFAULT;
  150. msg.arg.pagefault.address = address;
  151. if (flags & FAULT_FLAG_WRITE)
  152. /*
  153. * If UFFD_FEATURE_PAGEFAULT_FLAG_WRITE was set in the
  154. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WRITE
  155. * was not set in a UFFD_EVENT_PAGEFAULT, it means it
  156. * was a read fault, otherwise if set it means it's
  157. * a write fault.
  158. */
  159. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WRITE;
  160. if (reason & VM_UFFD_WP)
  161. /*
  162. * If UFFD_FEATURE_PAGEFAULT_FLAG_WP was set in the
  163. * uffdio_api.features and UFFD_PAGEFAULT_FLAG_WP was
  164. * not set in a UFFD_EVENT_PAGEFAULT, it means it was
  165. * a missing fault, otherwise if set it means it's a
  166. * write protect fault.
  167. */
  168. msg.arg.pagefault.flags |= UFFD_PAGEFAULT_FLAG_WP;
  169. return msg;
  170. }
  171. /*
  172. * Verify the pagetables are still not ok after having reigstered into
  173. * the fault_pending_wqh to avoid userland having to UFFDIO_WAKE any
  174. * userfault that has already been resolved, if userfaultfd_read and
  175. * UFFDIO_COPY|ZEROPAGE are being run simultaneously on two different
  176. * threads.
  177. */
  178. static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,
  179. unsigned long address,
  180. unsigned long flags,
  181. unsigned long reason)
  182. {
  183. struct mm_struct *mm = ctx->mm;
  184. pgd_t *pgd;
  185. pud_t *pud;
  186. pmd_t *pmd, _pmd;
  187. pte_t *pte;
  188. bool ret = true;
  189. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  190. pgd = pgd_offset(mm, address);
  191. if (!pgd_present(*pgd))
  192. goto out;
  193. pud = pud_offset(pgd, address);
  194. if (!pud_present(*pud))
  195. goto out;
  196. pmd = pmd_offset(pud, address);
  197. /*
  198. * READ_ONCE must function as a barrier with narrower scope
  199. * and it must be equivalent to:
  200. * _pmd = *pmd; barrier();
  201. *
  202. * This is to deal with the instability (as in
  203. * pmd_trans_unstable) of the pmd.
  204. */
  205. _pmd = READ_ONCE(*pmd);
  206. if (!pmd_present(_pmd))
  207. goto out;
  208. ret = false;
  209. if (pmd_trans_huge(_pmd))
  210. goto out;
  211. /*
  212. * the pmd is stable (as in !pmd_trans_unstable) so we can re-read it
  213. * and use the standard pte_offset_map() instead of parsing _pmd.
  214. */
  215. pte = pte_offset_map(pmd, address);
  216. /*
  217. * Lockless access: we're in a wait_event so it's ok if it
  218. * changes under us.
  219. */
  220. if (pte_none(*pte))
  221. ret = true;
  222. pte_unmap(pte);
  223. out:
  224. return ret;
  225. }
  226. /*
  227. * The locking rules involved in returning VM_FAULT_RETRY depending on
  228. * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and
  229. * FAULT_FLAG_KILLABLE are not straightforward. The "Caution"
  230. * recommendation in __lock_page_or_retry is not an understatement.
  231. *
  232. * If FAULT_FLAG_ALLOW_RETRY is set, the mmap_sem must be released
  233. * before returning VM_FAULT_RETRY only if FAULT_FLAG_RETRY_NOWAIT is
  234. * not set.
  235. *
  236. * If FAULT_FLAG_ALLOW_RETRY is set but FAULT_FLAG_KILLABLE is not
  237. * set, VM_FAULT_RETRY can still be returned if and only if there are
  238. * fatal_signal_pending()s, and the mmap_sem must be released before
  239. * returning it.
  240. */
  241. int handle_userfault(struct vm_area_struct *vma, unsigned long address,
  242. unsigned int flags, unsigned long reason)
  243. {
  244. struct mm_struct *mm = vma->vm_mm;
  245. struct userfaultfd_ctx *ctx;
  246. struct userfaultfd_wait_queue uwq;
  247. int ret;
  248. bool must_wait, return_to_userland;
  249. BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  250. ret = VM_FAULT_SIGBUS;
  251. ctx = vma->vm_userfaultfd_ctx.ctx;
  252. if (!ctx)
  253. goto out;
  254. BUG_ON(ctx->mm != mm);
  255. VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
  256. VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
  257. /*
  258. * If it's already released don't get it. This avoids to loop
  259. * in __get_user_pages if userfaultfd_release waits on the
  260. * caller of handle_userfault to release the mmap_sem.
  261. */
  262. if (unlikely(ACCESS_ONCE(ctx->released)))
  263. goto out;
  264. /*
  265. * We don't do userfault handling for the final child pid update.
  266. */
  267. if (current->flags & PF_EXITING)
  268. goto out;
  269. /*
  270. * Check that we can return VM_FAULT_RETRY.
  271. *
  272. * NOTE: it should become possible to return VM_FAULT_RETRY
  273. * even if FAULT_FLAG_TRIED is set without leading to gup()
  274. * -EBUSY failures, if the userfaultfd is to be extended for
  275. * VM_UFFD_WP tracking and we intend to arm the userfault
  276. * without first stopping userland access to the memory. For
  277. * VM_UFFD_MISSING userfaults this is enough for now.
  278. */
  279. if (unlikely(!(flags & FAULT_FLAG_ALLOW_RETRY))) {
  280. /*
  281. * Validate the invariant that nowait must allow retry
  282. * to be sure not to return SIGBUS erroneously on
  283. * nowait invocations.
  284. */
  285. BUG_ON(flags & FAULT_FLAG_RETRY_NOWAIT);
  286. #ifdef CONFIG_DEBUG_VM
  287. if (printk_ratelimit()) {
  288. printk(KERN_WARNING
  289. "FAULT_FLAG_ALLOW_RETRY missing %x\n", flags);
  290. dump_stack();
  291. }
  292. #endif
  293. goto out;
  294. }
  295. /*
  296. * Handle nowait, not much to do other than tell it to retry
  297. * and wait.
  298. */
  299. ret = VM_FAULT_RETRY;
  300. if (flags & FAULT_FLAG_RETRY_NOWAIT)
  301. goto out;
  302. /* take the reference before dropping the mmap_sem */
  303. userfaultfd_ctx_get(ctx);
  304. init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
  305. uwq.wq.private = current;
  306. uwq.msg = userfault_msg(address, flags, reason);
  307. uwq.ctx = ctx;
  308. return_to_userland = (flags & (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE)) ==
  309. (FAULT_FLAG_USER|FAULT_FLAG_KILLABLE);
  310. spin_lock(&ctx->fault_pending_wqh.lock);
  311. /*
  312. * After the __add_wait_queue the uwq is visible to userland
  313. * through poll/read().
  314. */
  315. __add_wait_queue(&ctx->fault_pending_wqh, &uwq.wq);
  316. /*
  317. * The smp_mb() after __set_current_state prevents the reads
  318. * following the spin_unlock to happen before the list_add in
  319. * __add_wait_queue.
  320. */
  321. set_current_state(return_to_userland ? TASK_INTERRUPTIBLE :
  322. TASK_KILLABLE);
  323. spin_unlock(&ctx->fault_pending_wqh.lock);
  324. must_wait = userfaultfd_must_wait(ctx, address, flags, reason);
  325. up_read(&mm->mmap_sem);
  326. if (likely(must_wait && !ACCESS_ONCE(ctx->released) &&
  327. (return_to_userland ? !signal_pending(current) :
  328. !fatal_signal_pending(current)))) {
  329. wake_up_poll(&ctx->fd_wqh, POLLIN);
  330. schedule();
  331. ret |= VM_FAULT_MAJOR;
  332. }
  333. __set_current_state(TASK_RUNNING);
  334. if (return_to_userland) {
  335. if (signal_pending(current) &&
  336. !fatal_signal_pending(current)) {
  337. /*
  338. * If we got a SIGSTOP or SIGCONT and this is
  339. * a normal userland page fault, just let
  340. * userland return so the signal will be
  341. * handled and gdb debugging works. The page
  342. * fault code immediately after we return from
  343. * this function is going to release the
  344. * mmap_sem and it's not depending on it
  345. * (unlike gup would if we were not to return
  346. * VM_FAULT_RETRY).
  347. *
  348. * If a fatal signal is pending we still take
  349. * the streamlined VM_FAULT_RETRY failure path
  350. * and there's no need to retake the mmap_sem
  351. * in such case.
  352. */
  353. down_read(&mm->mmap_sem);
  354. ret = VM_FAULT_NOPAGE;
  355. }
  356. }
  357. /*
  358. * Here we race with the list_del; list_add in
  359. * userfaultfd_ctx_read(), however because we don't ever run
  360. * list_del_init() to refile across the two lists, the prev
  361. * and next pointers will never point to self. list_add also
  362. * would never let any of the two pointers to point to
  363. * self. So list_empty_careful won't risk to see both pointers
  364. * pointing to self at any time during the list refile. The
  365. * only case where list_del_init() is called is the full
  366. * removal in the wake function and there we don't re-list_add
  367. * and it's fine not to block on the spinlock. The uwq on this
  368. * kernel stack can be released after the list_del_init.
  369. */
  370. if (!list_empty_careful(&uwq.wq.task_list)) {
  371. spin_lock(&ctx->fault_pending_wqh.lock);
  372. /*
  373. * No need of list_del_init(), the uwq on the stack
  374. * will be freed shortly anyway.
  375. */
  376. list_del(&uwq.wq.task_list);
  377. spin_unlock(&ctx->fault_pending_wqh.lock);
  378. }
  379. /*
  380. * ctx may go away after this if the userfault pseudo fd is
  381. * already released.
  382. */
  383. userfaultfd_ctx_put(ctx);
  384. out:
  385. return ret;
  386. }
  387. static int userfaultfd_release(struct inode *inode, struct file *file)
  388. {
  389. struct userfaultfd_ctx *ctx = file->private_data;
  390. struct mm_struct *mm = ctx->mm;
  391. struct vm_area_struct *vma, *prev;
  392. /* len == 0 means wake all */
  393. struct userfaultfd_wake_range range = { .len = 0, };
  394. unsigned long new_flags;
  395. ACCESS_ONCE(ctx->released) = true;
  396. /*
  397. * Flush page faults out of all CPUs. NOTE: all page faults
  398. * must be retried without returning VM_FAULT_SIGBUS if
  399. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  400. * changes while handle_userfault released the mmap_sem. So
  401. * it's critical that released is set to true (above), before
  402. * taking the mmap_sem for writing.
  403. */
  404. down_write(&mm->mmap_sem);
  405. prev = NULL;
  406. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  407. cond_resched();
  408. BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
  409. !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  410. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  411. prev = vma;
  412. continue;
  413. }
  414. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  415. prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
  416. new_flags, vma->anon_vma,
  417. vma->vm_file, vma->vm_pgoff,
  418. vma_policy(vma),
  419. NULL_VM_UFFD_CTX);
  420. if (prev)
  421. vma = prev;
  422. else
  423. prev = vma;
  424. vma->vm_flags = new_flags;
  425. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  426. }
  427. up_write(&mm->mmap_sem);
  428. /*
  429. * After no new page faults can wait on this fault_*wqh, flush
  430. * the last page faults that may have been already waiting on
  431. * the fault_*wqh.
  432. */
  433. spin_lock(&ctx->fault_pending_wqh.lock);
  434. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
  435. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
  436. spin_unlock(&ctx->fault_pending_wqh.lock);
  437. wake_up_poll(&ctx->fd_wqh, POLLHUP);
  438. userfaultfd_ctx_put(ctx);
  439. return 0;
  440. }
  441. /* fault_pending_wqh.lock must be hold by the caller */
  442. static inline struct userfaultfd_wait_queue *find_userfault(
  443. struct userfaultfd_ctx *ctx)
  444. {
  445. wait_queue_t *wq;
  446. struct userfaultfd_wait_queue *uwq;
  447. VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
  448. uwq = NULL;
  449. if (!waitqueue_active(&ctx->fault_pending_wqh))
  450. goto out;
  451. /* walk in reverse to provide FIFO behavior to read userfaults */
  452. wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
  453. typeof(*wq), task_list);
  454. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  455. out:
  456. return uwq;
  457. }
  458. static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
  459. {
  460. struct userfaultfd_ctx *ctx = file->private_data;
  461. unsigned int ret;
  462. poll_wait(file, &ctx->fd_wqh, wait);
  463. switch (ctx->state) {
  464. case UFFD_STATE_WAIT_API:
  465. return POLLERR;
  466. case UFFD_STATE_RUNNING:
  467. /*
  468. * poll() never guarantees that read won't block.
  469. * userfaults can be waken before they're read().
  470. */
  471. if (unlikely(!(file->f_flags & O_NONBLOCK)))
  472. return POLLERR;
  473. /*
  474. * lockless access to see if there are pending faults
  475. * __pollwait last action is the add_wait_queue but
  476. * the spin_unlock would allow the waitqueue_active to
  477. * pass above the actual list_add inside
  478. * add_wait_queue critical section. So use a full
  479. * memory barrier to serialize the list_add write of
  480. * add_wait_queue() with the waitqueue_active read
  481. * below.
  482. */
  483. ret = 0;
  484. smp_mb();
  485. if (waitqueue_active(&ctx->fault_pending_wqh))
  486. ret = POLLIN;
  487. return ret;
  488. default:
  489. BUG();
  490. }
  491. }
  492. static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
  493. struct uffd_msg *msg)
  494. {
  495. ssize_t ret;
  496. DECLARE_WAITQUEUE(wait, current);
  497. struct userfaultfd_wait_queue *uwq;
  498. /* always take the fd_wqh lock before the fault_pending_wqh lock */
  499. spin_lock(&ctx->fd_wqh.lock);
  500. __add_wait_queue(&ctx->fd_wqh, &wait);
  501. for (;;) {
  502. set_current_state(TASK_INTERRUPTIBLE);
  503. spin_lock(&ctx->fault_pending_wqh.lock);
  504. uwq = find_userfault(ctx);
  505. if (uwq) {
  506. /*
  507. * Use a seqcount to repeat the lockless check
  508. * in wake_userfault() to avoid missing
  509. * wakeups because during the refile both
  510. * waitqueue could become empty if this is the
  511. * only userfault.
  512. */
  513. write_seqcount_begin(&ctx->refile_seq);
  514. /*
  515. * The fault_pending_wqh.lock prevents the uwq
  516. * to disappear from under us.
  517. *
  518. * Refile this userfault from
  519. * fault_pending_wqh to fault_wqh, it's not
  520. * pending anymore after we read it.
  521. *
  522. * Use list_del() by hand (as
  523. * userfaultfd_wake_function also uses
  524. * list_del_init() by hand) to be sure nobody
  525. * changes __remove_wait_queue() to use
  526. * list_del_init() in turn breaking the
  527. * !list_empty_careful() check in
  528. * handle_userfault(). The uwq->wq.task_list
  529. * must never be empty at any time during the
  530. * refile, or the waitqueue could disappear
  531. * from under us. The "wait_queue_head_t"
  532. * parameter of __remove_wait_queue() is unused
  533. * anyway.
  534. */
  535. list_del(&uwq->wq.task_list);
  536. __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
  537. write_seqcount_end(&ctx->refile_seq);
  538. /* careful to always initialize msg if ret == 0 */
  539. *msg = uwq->msg;
  540. spin_unlock(&ctx->fault_pending_wqh.lock);
  541. ret = 0;
  542. break;
  543. }
  544. spin_unlock(&ctx->fault_pending_wqh.lock);
  545. if (signal_pending(current)) {
  546. ret = -ERESTARTSYS;
  547. break;
  548. }
  549. if (no_wait) {
  550. ret = -EAGAIN;
  551. break;
  552. }
  553. spin_unlock(&ctx->fd_wqh.lock);
  554. schedule();
  555. spin_lock(&ctx->fd_wqh.lock);
  556. }
  557. __remove_wait_queue(&ctx->fd_wqh, &wait);
  558. __set_current_state(TASK_RUNNING);
  559. spin_unlock(&ctx->fd_wqh.lock);
  560. return ret;
  561. }
  562. static ssize_t userfaultfd_read(struct file *file, char __user *buf,
  563. size_t count, loff_t *ppos)
  564. {
  565. struct userfaultfd_ctx *ctx = file->private_data;
  566. ssize_t _ret, ret = 0;
  567. struct uffd_msg msg;
  568. int no_wait = file->f_flags & O_NONBLOCK;
  569. if (ctx->state == UFFD_STATE_WAIT_API)
  570. return -EINVAL;
  571. for (;;) {
  572. if (count < sizeof(msg))
  573. return ret ? ret : -EINVAL;
  574. _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
  575. if (_ret < 0)
  576. return ret ? ret : _ret;
  577. if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
  578. return ret ? ret : -EFAULT;
  579. ret += sizeof(msg);
  580. buf += sizeof(msg);
  581. count -= sizeof(msg);
  582. /*
  583. * Allow to read more than one fault at time but only
  584. * block if waiting for the very first one.
  585. */
  586. no_wait = O_NONBLOCK;
  587. }
  588. }
  589. static void __wake_userfault(struct userfaultfd_ctx *ctx,
  590. struct userfaultfd_wake_range *range)
  591. {
  592. unsigned long start, end;
  593. start = range->start;
  594. end = range->start + range->len;
  595. spin_lock(&ctx->fault_pending_wqh.lock);
  596. /* wake all in the range and autoremove */
  597. if (waitqueue_active(&ctx->fault_pending_wqh))
  598. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
  599. range);
  600. if (waitqueue_active(&ctx->fault_wqh))
  601. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
  602. spin_unlock(&ctx->fault_pending_wqh.lock);
  603. }
  604. static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
  605. struct userfaultfd_wake_range *range)
  606. {
  607. unsigned seq;
  608. bool need_wakeup;
  609. /*
  610. * To be sure waitqueue_active() is not reordered by the CPU
  611. * before the pagetable update, use an explicit SMP memory
  612. * barrier here. PT lock release or up_read(mmap_sem) still
  613. * have release semantics that can allow the
  614. * waitqueue_active() to be reordered before the pte update.
  615. */
  616. smp_mb();
  617. /*
  618. * Use waitqueue_active because it's very frequent to
  619. * change the address space atomically even if there are no
  620. * userfaults yet. So we take the spinlock only when we're
  621. * sure we've userfaults to wake.
  622. */
  623. do {
  624. seq = read_seqcount_begin(&ctx->refile_seq);
  625. need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
  626. waitqueue_active(&ctx->fault_wqh);
  627. cond_resched();
  628. } while (read_seqcount_retry(&ctx->refile_seq, seq));
  629. if (need_wakeup)
  630. __wake_userfault(ctx, range);
  631. }
  632. static __always_inline int validate_range(struct mm_struct *mm,
  633. __u64 start, __u64 len)
  634. {
  635. __u64 task_size = mm->task_size;
  636. if (start & ~PAGE_MASK)
  637. return -EINVAL;
  638. if (len & ~PAGE_MASK)
  639. return -EINVAL;
  640. if (!len)
  641. return -EINVAL;
  642. if (start < mmap_min_addr)
  643. return -EINVAL;
  644. if (start >= task_size)
  645. return -EINVAL;
  646. if (len > task_size - start)
  647. return -EINVAL;
  648. return 0;
  649. }
  650. static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  651. unsigned long arg)
  652. {
  653. struct mm_struct *mm = ctx->mm;
  654. struct vm_area_struct *vma, *prev, *cur;
  655. int ret;
  656. struct uffdio_register uffdio_register;
  657. struct uffdio_register __user *user_uffdio_register;
  658. unsigned long vm_flags, new_flags;
  659. bool found;
  660. unsigned long start, end, vma_end;
  661. user_uffdio_register = (struct uffdio_register __user *) arg;
  662. ret = -EFAULT;
  663. if (copy_from_user(&uffdio_register, user_uffdio_register,
  664. sizeof(uffdio_register)-sizeof(__u64)))
  665. goto out;
  666. ret = -EINVAL;
  667. if (!uffdio_register.mode)
  668. goto out;
  669. if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
  670. UFFDIO_REGISTER_MODE_WP))
  671. goto out;
  672. vm_flags = 0;
  673. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  674. vm_flags |= VM_UFFD_MISSING;
  675. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
  676. vm_flags |= VM_UFFD_WP;
  677. /*
  678. * FIXME: remove the below error constraint by
  679. * implementing the wprotect tracking mode.
  680. */
  681. ret = -EINVAL;
  682. goto out;
  683. }
  684. ret = validate_range(mm, uffdio_register.range.start,
  685. uffdio_register.range.len);
  686. if (ret)
  687. goto out;
  688. start = uffdio_register.range.start;
  689. end = start + uffdio_register.range.len;
  690. down_write(&mm->mmap_sem);
  691. vma = find_vma_prev(mm, start, &prev);
  692. ret = -ENOMEM;
  693. if (!vma)
  694. goto out_unlock;
  695. /* check that there's at least one vma in the range */
  696. ret = -EINVAL;
  697. if (vma->vm_start >= end)
  698. goto out_unlock;
  699. /*
  700. * Search for not compatible vmas.
  701. *
  702. * FIXME: this shall be relaxed later so that it doesn't fail
  703. * on tmpfs backed vmas (in addition to the current allowance
  704. * on anonymous vmas).
  705. */
  706. found = false;
  707. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  708. cond_resched();
  709. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  710. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  711. /* check not compatible vmas */
  712. ret = -EINVAL;
  713. if (cur->vm_ops)
  714. goto out_unlock;
  715. /*
  716. * Check that this vma isn't already owned by a
  717. * different userfaultfd. We can't allow more than one
  718. * userfaultfd to own a single vma simultaneously or we
  719. * wouldn't know which one to deliver the userfaults to.
  720. */
  721. ret = -EBUSY;
  722. if (cur->vm_userfaultfd_ctx.ctx &&
  723. cur->vm_userfaultfd_ctx.ctx != ctx)
  724. goto out_unlock;
  725. found = true;
  726. }
  727. BUG_ON(!found);
  728. if (vma->vm_start < start)
  729. prev = vma;
  730. ret = 0;
  731. do {
  732. cond_resched();
  733. BUG_ON(vma->vm_ops);
  734. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  735. vma->vm_userfaultfd_ctx.ctx != ctx);
  736. /*
  737. * Nothing to do: this vma is already registered into this
  738. * userfaultfd and with the right tracking mode too.
  739. */
  740. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  741. (vma->vm_flags & vm_flags) == vm_flags)
  742. goto skip;
  743. if (vma->vm_start > start)
  744. start = vma->vm_start;
  745. vma_end = min(end, vma->vm_end);
  746. new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
  747. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  748. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  749. vma_policy(vma),
  750. ((struct vm_userfaultfd_ctx){ ctx }));
  751. if (prev) {
  752. vma = prev;
  753. goto next;
  754. }
  755. if (vma->vm_start < start) {
  756. ret = split_vma(mm, vma, start, 1);
  757. if (ret)
  758. break;
  759. }
  760. if (vma->vm_end > end) {
  761. ret = split_vma(mm, vma, end, 0);
  762. if (ret)
  763. break;
  764. }
  765. next:
  766. /*
  767. * In the vma_merge() successful mprotect-like case 8:
  768. * the next vma was merged into the current one and
  769. * the current one has not been updated yet.
  770. */
  771. vma->vm_flags = new_flags;
  772. vma->vm_userfaultfd_ctx.ctx = ctx;
  773. skip:
  774. prev = vma;
  775. start = vma->vm_end;
  776. vma = vma->vm_next;
  777. } while (vma && vma->vm_start < end);
  778. out_unlock:
  779. up_write(&mm->mmap_sem);
  780. if (!ret) {
  781. /*
  782. * Now that we scanned all vmas we can already tell
  783. * userland which ioctls methods are guaranteed to
  784. * succeed on this range.
  785. */
  786. if (put_user(UFFD_API_RANGE_IOCTLS,
  787. &user_uffdio_register->ioctls))
  788. ret = -EFAULT;
  789. }
  790. out:
  791. return ret;
  792. }
  793. static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
  794. unsigned long arg)
  795. {
  796. struct mm_struct *mm = ctx->mm;
  797. struct vm_area_struct *vma, *prev, *cur;
  798. int ret;
  799. struct uffdio_range uffdio_unregister;
  800. unsigned long new_flags;
  801. bool found;
  802. unsigned long start, end, vma_end;
  803. const void __user *buf = (void __user *)arg;
  804. ret = -EFAULT;
  805. if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
  806. goto out;
  807. ret = validate_range(mm, uffdio_unregister.start,
  808. uffdio_unregister.len);
  809. if (ret)
  810. goto out;
  811. start = uffdio_unregister.start;
  812. end = start + uffdio_unregister.len;
  813. down_write(&mm->mmap_sem);
  814. vma = find_vma_prev(mm, start, &prev);
  815. ret = -ENOMEM;
  816. if (!vma)
  817. goto out_unlock;
  818. /* check that there's at least one vma in the range */
  819. ret = -EINVAL;
  820. if (vma->vm_start >= end)
  821. goto out_unlock;
  822. /*
  823. * Search for not compatible vmas.
  824. *
  825. * FIXME: this shall be relaxed later so that it doesn't fail
  826. * on tmpfs backed vmas (in addition to the current allowance
  827. * on anonymous vmas).
  828. */
  829. found = false;
  830. ret = -EINVAL;
  831. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  832. cond_resched();
  833. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  834. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  835. /*
  836. * Check not compatible vmas, not strictly required
  837. * here as not compatible vmas cannot have an
  838. * userfaultfd_ctx registered on them, but this
  839. * provides for more strict behavior to notice
  840. * unregistration errors.
  841. */
  842. if (cur->vm_ops)
  843. goto out_unlock;
  844. found = true;
  845. }
  846. BUG_ON(!found);
  847. if (vma->vm_start < start)
  848. prev = vma;
  849. ret = 0;
  850. do {
  851. cond_resched();
  852. BUG_ON(vma->vm_ops);
  853. /*
  854. * Nothing to do: this vma is already registered into this
  855. * userfaultfd and with the right tracking mode too.
  856. */
  857. if (!vma->vm_userfaultfd_ctx.ctx)
  858. goto skip;
  859. if (vma->vm_start > start)
  860. start = vma->vm_start;
  861. vma_end = min(end, vma->vm_end);
  862. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  863. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  864. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  865. vma_policy(vma),
  866. NULL_VM_UFFD_CTX);
  867. if (prev) {
  868. vma = prev;
  869. goto next;
  870. }
  871. if (vma->vm_start < start) {
  872. ret = split_vma(mm, vma, start, 1);
  873. if (ret)
  874. break;
  875. }
  876. if (vma->vm_end > end) {
  877. ret = split_vma(mm, vma, end, 0);
  878. if (ret)
  879. break;
  880. }
  881. next:
  882. /*
  883. * In the vma_merge() successful mprotect-like case 8:
  884. * the next vma was merged into the current one and
  885. * the current one has not been updated yet.
  886. */
  887. vma->vm_flags = new_flags;
  888. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  889. skip:
  890. prev = vma;
  891. start = vma->vm_end;
  892. vma = vma->vm_next;
  893. } while (vma && vma->vm_start < end);
  894. out_unlock:
  895. up_write(&mm->mmap_sem);
  896. out:
  897. return ret;
  898. }
  899. /*
  900. * userfaultfd_wake may be used in combination with the
  901. * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
  902. */
  903. static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
  904. unsigned long arg)
  905. {
  906. int ret;
  907. struct uffdio_range uffdio_wake;
  908. struct userfaultfd_wake_range range;
  909. const void __user *buf = (void __user *)arg;
  910. ret = -EFAULT;
  911. if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
  912. goto out;
  913. ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
  914. if (ret)
  915. goto out;
  916. range.start = uffdio_wake.start;
  917. range.len = uffdio_wake.len;
  918. /*
  919. * len == 0 means wake all and we don't want to wake all here,
  920. * so check it again to be sure.
  921. */
  922. VM_BUG_ON(!range.len);
  923. wake_userfault(ctx, &range);
  924. ret = 0;
  925. out:
  926. return ret;
  927. }
  928. static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
  929. unsigned long arg)
  930. {
  931. __s64 ret;
  932. struct uffdio_copy uffdio_copy;
  933. struct uffdio_copy __user *user_uffdio_copy;
  934. struct userfaultfd_wake_range range;
  935. user_uffdio_copy = (struct uffdio_copy __user *) arg;
  936. ret = -EFAULT;
  937. if (copy_from_user(&uffdio_copy, user_uffdio_copy,
  938. /* don't copy "copy" last field */
  939. sizeof(uffdio_copy)-sizeof(__s64)))
  940. goto out;
  941. ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
  942. if (ret)
  943. goto out;
  944. /*
  945. * double check for wraparound just in case. copy_from_user()
  946. * will later check uffdio_copy.src + uffdio_copy.len to fit
  947. * in the userland range.
  948. */
  949. ret = -EINVAL;
  950. if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
  951. goto out;
  952. if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
  953. goto out;
  954. ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
  955. uffdio_copy.len);
  956. if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
  957. return -EFAULT;
  958. if (ret < 0)
  959. goto out;
  960. BUG_ON(!ret);
  961. /* len == 0 would wake all */
  962. range.len = ret;
  963. if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
  964. range.start = uffdio_copy.dst;
  965. wake_userfault(ctx, &range);
  966. }
  967. ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
  968. out:
  969. return ret;
  970. }
  971. static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
  972. unsigned long arg)
  973. {
  974. __s64 ret;
  975. struct uffdio_zeropage uffdio_zeropage;
  976. struct uffdio_zeropage __user *user_uffdio_zeropage;
  977. struct userfaultfd_wake_range range;
  978. user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
  979. ret = -EFAULT;
  980. if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
  981. /* don't copy "zeropage" last field */
  982. sizeof(uffdio_zeropage)-sizeof(__s64)))
  983. goto out;
  984. ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
  985. uffdio_zeropage.range.len);
  986. if (ret)
  987. goto out;
  988. ret = -EINVAL;
  989. if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
  990. goto out;
  991. ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
  992. uffdio_zeropage.range.len);
  993. if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
  994. return -EFAULT;
  995. if (ret < 0)
  996. goto out;
  997. /* len == 0 would wake all */
  998. BUG_ON(!ret);
  999. range.len = ret;
  1000. if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
  1001. range.start = uffdio_zeropage.range.start;
  1002. wake_userfault(ctx, &range);
  1003. }
  1004. ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
  1005. out:
  1006. return ret;
  1007. }
  1008. /*
  1009. * userland asks for a certain API version and we return which bits
  1010. * and ioctl commands are implemented in this kernel for such API
  1011. * version or -EINVAL if unknown.
  1012. */
  1013. static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  1014. unsigned long arg)
  1015. {
  1016. struct uffdio_api uffdio_api;
  1017. void __user *buf = (void __user *)arg;
  1018. int ret;
  1019. ret = -EINVAL;
  1020. if (ctx->state != UFFD_STATE_WAIT_API)
  1021. goto out;
  1022. ret = -EFAULT;
  1023. if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
  1024. goto out;
  1025. if (uffdio_api.api != UFFD_API || uffdio_api.features) {
  1026. memset(&uffdio_api, 0, sizeof(uffdio_api));
  1027. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1028. goto out;
  1029. ret = -EINVAL;
  1030. goto out;
  1031. }
  1032. uffdio_api.features = UFFD_API_FEATURES;
  1033. uffdio_api.ioctls = UFFD_API_IOCTLS;
  1034. ret = -EFAULT;
  1035. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1036. goto out;
  1037. ctx->state = UFFD_STATE_RUNNING;
  1038. ret = 0;
  1039. out:
  1040. return ret;
  1041. }
  1042. static long userfaultfd_ioctl(struct file *file, unsigned cmd,
  1043. unsigned long arg)
  1044. {
  1045. int ret = -EINVAL;
  1046. struct userfaultfd_ctx *ctx = file->private_data;
  1047. if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
  1048. return -EINVAL;
  1049. switch(cmd) {
  1050. case UFFDIO_API:
  1051. ret = userfaultfd_api(ctx, arg);
  1052. break;
  1053. case UFFDIO_REGISTER:
  1054. ret = userfaultfd_register(ctx, arg);
  1055. break;
  1056. case UFFDIO_UNREGISTER:
  1057. ret = userfaultfd_unregister(ctx, arg);
  1058. break;
  1059. case UFFDIO_WAKE:
  1060. ret = userfaultfd_wake(ctx, arg);
  1061. break;
  1062. case UFFDIO_COPY:
  1063. ret = userfaultfd_copy(ctx, arg);
  1064. break;
  1065. case UFFDIO_ZEROPAGE:
  1066. ret = userfaultfd_zeropage(ctx, arg);
  1067. break;
  1068. }
  1069. return ret;
  1070. }
  1071. #ifdef CONFIG_PROC_FS
  1072. static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
  1073. {
  1074. struct userfaultfd_ctx *ctx = f->private_data;
  1075. wait_queue_t *wq;
  1076. struct userfaultfd_wait_queue *uwq;
  1077. unsigned long pending = 0, total = 0;
  1078. spin_lock(&ctx->fault_pending_wqh.lock);
  1079. list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
  1080. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1081. pending++;
  1082. total++;
  1083. }
  1084. list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
  1085. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1086. total++;
  1087. }
  1088. spin_unlock(&ctx->fault_pending_wqh.lock);
  1089. /*
  1090. * If more protocols will be added, there will be all shown
  1091. * separated by a space. Like this:
  1092. * protocols: aa:... bb:...
  1093. */
  1094. seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
  1095. pending, total, UFFD_API, UFFD_API_FEATURES,
  1096. UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
  1097. }
  1098. #endif
  1099. static const struct file_operations userfaultfd_fops = {
  1100. #ifdef CONFIG_PROC_FS
  1101. .show_fdinfo = userfaultfd_show_fdinfo,
  1102. #endif
  1103. .release = userfaultfd_release,
  1104. .poll = userfaultfd_poll,
  1105. .read = userfaultfd_read,
  1106. .unlocked_ioctl = userfaultfd_ioctl,
  1107. .compat_ioctl = userfaultfd_ioctl,
  1108. .llseek = noop_llseek,
  1109. };
  1110. static void init_once_userfaultfd_ctx(void *mem)
  1111. {
  1112. struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
  1113. init_waitqueue_head(&ctx->fault_pending_wqh);
  1114. init_waitqueue_head(&ctx->fault_wqh);
  1115. init_waitqueue_head(&ctx->fd_wqh);
  1116. seqcount_init(&ctx->refile_seq);
  1117. }
  1118. /**
  1119. * userfaultfd_file_create - Creates an userfaultfd file pointer.
  1120. * @flags: Flags for the userfaultfd file.
  1121. *
  1122. * This function creates an userfaultfd file pointer, w/out installing
  1123. * it into the fd table. This is useful when the userfaultfd file is
  1124. * used during the initialization of data structures that require
  1125. * extra setup after the userfaultfd creation. So the userfaultfd
  1126. * creation is split into the file pointer creation phase, and the
  1127. * file descriptor installation phase. In this way races with
  1128. * userspace closing the newly installed file descriptor can be
  1129. * avoided. Returns an userfaultfd file pointer, or a proper error
  1130. * pointer.
  1131. */
  1132. static struct file *userfaultfd_file_create(int flags)
  1133. {
  1134. struct file *file;
  1135. struct userfaultfd_ctx *ctx;
  1136. BUG_ON(!current->mm);
  1137. /* Check the UFFD_* constants for consistency. */
  1138. BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
  1139. BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
  1140. file = ERR_PTR(-EINVAL);
  1141. if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
  1142. goto out;
  1143. file = ERR_PTR(-ENOMEM);
  1144. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  1145. if (!ctx)
  1146. goto out;
  1147. atomic_set(&ctx->refcount, 1);
  1148. ctx->flags = flags;
  1149. ctx->state = UFFD_STATE_WAIT_API;
  1150. ctx->released = false;
  1151. ctx->mm = current->mm;
  1152. /* prevent the mm struct to be freed */
  1153. atomic_inc(&ctx->mm->mm_users);
  1154. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
  1155. O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
  1156. if (IS_ERR(file)) {
  1157. mmput(ctx->mm);
  1158. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  1159. }
  1160. out:
  1161. return file;
  1162. }
  1163. SYSCALL_DEFINE1(userfaultfd, int, flags)
  1164. {
  1165. int fd, error;
  1166. struct file *file;
  1167. error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
  1168. if (error < 0)
  1169. return error;
  1170. fd = error;
  1171. file = userfaultfd_file_create(flags);
  1172. if (IS_ERR(file)) {
  1173. error = PTR_ERR(file);
  1174. goto err_put_unused_fd;
  1175. }
  1176. fd_install(fd, file);
  1177. return fd;
  1178. err_put_unused_fd:
  1179. put_unused_fd(fd);
  1180. return error;
  1181. }
  1182. static int __init userfaultfd_init(void)
  1183. {
  1184. userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
  1185. sizeof(struct userfaultfd_ctx),
  1186. 0,
  1187. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1188. init_once_userfaultfd_ctx);
  1189. return 0;
  1190. }
  1191. __initcall(userfaultfd_init);