userfaultfd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. mmdrop(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 fault_env *fe, unsigned long reason)
  242. {
  243. struct mm_struct *mm = fe->vma->vm_mm;
  244. struct userfaultfd_ctx *ctx;
  245. struct userfaultfd_wait_queue uwq;
  246. int ret;
  247. bool must_wait, return_to_userland;
  248. BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  249. ret = VM_FAULT_SIGBUS;
  250. ctx = fe->vma->vm_userfaultfd_ctx.ctx;
  251. if (!ctx)
  252. goto out;
  253. BUG_ON(ctx->mm != mm);
  254. VM_BUG_ON(reason & ~(VM_UFFD_MISSING|VM_UFFD_WP));
  255. VM_BUG_ON(!(reason & VM_UFFD_MISSING) ^ !!(reason & VM_UFFD_WP));
  256. /*
  257. * If it's already released don't get it. This avoids to loop
  258. * in __get_user_pages if userfaultfd_release waits on the
  259. * caller of handle_userfault to release the mmap_sem.
  260. */
  261. if (unlikely(ACCESS_ONCE(ctx->released)))
  262. goto out;
  263. /*
  264. * We don't do userfault handling for the final child pid update.
  265. */
  266. if (current->flags & PF_EXITING)
  267. goto out;
  268. /*
  269. * Check that we can return VM_FAULT_RETRY.
  270. *
  271. * NOTE: it should become possible to return VM_FAULT_RETRY
  272. * even if FAULT_FLAG_TRIED is set without leading to gup()
  273. * -EBUSY failures, if the userfaultfd is to be extended for
  274. * VM_UFFD_WP tracking and we intend to arm the userfault
  275. * without first stopping userland access to the memory. For
  276. * VM_UFFD_MISSING userfaults this is enough for now.
  277. */
  278. if (unlikely(!(fe->flags & FAULT_FLAG_ALLOW_RETRY))) {
  279. /*
  280. * Validate the invariant that nowait must allow retry
  281. * to be sure not to return SIGBUS erroneously on
  282. * nowait invocations.
  283. */
  284. BUG_ON(fe->flags & FAULT_FLAG_RETRY_NOWAIT);
  285. #ifdef CONFIG_DEBUG_VM
  286. if (printk_ratelimit()) {
  287. printk(KERN_WARNING
  288. "FAULT_FLAG_ALLOW_RETRY missing %x\n", fe->flags);
  289. dump_stack();
  290. }
  291. #endif
  292. goto out;
  293. }
  294. /*
  295. * Handle nowait, not much to do other than tell it to retry
  296. * and wait.
  297. */
  298. ret = VM_FAULT_RETRY;
  299. if (fe->flags & FAULT_FLAG_RETRY_NOWAIT)
  300. goto out;
  301. /* take the reference before dropping the mmap_sem */
  302. userfaultfd_ctx_get(ctx);
  303. init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function);
  304. uwq.wq.private = current;
  305. uwq.msg = userfault_msg(fe->address, fe->flags, reason);
  306. uwq.ctx = ctx;
  307. return_to_userland =
  308. (fe->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, fe->address, fe->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 = 0;
  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. if (!mmget_not_zero(mm))
  397. goto wakeup;
  398. /*
  399. * Flush page faults out of all CPUs. NOTE: all page faults
  400. * must be retried without returning VM_FAULT_SIGBUS if
  401. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  402. * changes while handle_userfault released the mmap_sem. So
  403. * it's critical that released is set to true (above), before
  404. * taking the mmap_sem for writing.
  405. */
  406. down_write(&mm->mmap_sem);
  407. prev = NULL;
  408. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  409. cond_resched();
  410. BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
  411. !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  412. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  413. prev = vma;
  414. continue;
  415. }
  416. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  417. prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
  418. new_flags, vma->anon_vma,
  419. vma->vm_file, vma->vm_pgoff,
  420. vma_policy(vma),
  421. NULL_VM_UFFD_CTX);
  422. if (prev)
  423. vma = prev;
  424. else
  425. prev = vma;
  426. vma->vm_flags = new_flags;
  427. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  428. }
  429. up_write(&mm->mmap_sem);
  430. mmput(mm);
  431. wakeup:
  432. /*
  433. * After no new page faults can wait on this fault_*wqh, flush
  434. * the last page faults that may have been already waiting on
  435. * the fault_*wqh.
  436. */
  437. spin_lock(&ctx->fault_pending_wqh.lock);
  438. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
  439. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range);
  440. spin_unlock(&ctx->fault_pending_wqh.lock);
  441. wake_up_poll(&ctx->fd_wqh, POLLHUP);
  442. userfaultfd_ctx_put(ctx);
  443. return 0;
  444. }
  445. /* fault_pending_wqh.lock must be hold by the caller */
  446. static inline struct userfaultfd_wait_queue *find_userfault(
  447. struct userfaultfd_ctx *ctx)
  448. {
  449. wait_queue_t *wq;
  450. struct userfaultfd_wait_queue *uwq;
  451. VM_BUG_ON(!spin_is_locked(&ctx->fault_pending_wqh.lock));
  452. uwq = NULL;
  453. if (!waitqueue_active(&ctx->fault_pending_wqh))
  454. goto out;
  455. /* walk in reverse to provide FIFO behavior to read userfaults */
  456. wq = list_last_entry(&ctx->fault_pending_wqh.task_list,
  457. typeof(*wq), task_list);
  458. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  459. out:
  460. return uwq;
  461. }
  462. static unsigned int userfaultfd_poll(struct file *file, poll_table *wait)
  463. {
  464. struct userfaultfd_ctx *ctx = file->private_data;
  465. unsigned int ret;
  466. poll_wait(file, &ctx->fd_wqh, wait);
  467. switch (ctx->state) {
  468. case UFFD_STATE_WAIT_API:
  469. return POLLERR;
  470. case UFFD_STATE_RUNNING:
  471. /*
  472. * poll() never guarantees that read won't block.
  473. * userfaults can be waken before they're read().
  474. */
  475. if (unlikely(!(file->f_flags & O_NONBLOCK)))
  476. return POLLERR;
  477. /*
  478. * lockless access to see if there are pending faults
  479. * __pollwait last action is the add_wait_queue but
  480. * the spin_unlock would allow the waitqueue_active to
  481. * pass above the actual list_add inside
  482. * add_wait_queue critical section. So use a full
  483. * memory barrier to serialize the list_add write of
  484. * add_wait_queue() with the waitqueue_active read
  485. * below.
  486. */
  487. ret = 0;
  488. smp_mb();
  489. if (waitqueue_active(&ctx->fault_pending_wqh))
  490. ret = POLLIN;
  491. return ret;
  492. default:
  493. BUG();
  494. }
  495. }
  496. static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
  497. struct uffd_msg *msg)
  498. {
  499. ssize_t ret;
  500. DECLARE_WAITQUEUE(wait, current);
  501. struct userfaultfd_wait_queue *uwq;
  502. /* always take the fd_wqh lock before the fault_pending_wqh lock */
  503. spin_lock(&ctx->fd_wqh.lock);
  504. __add_wait_queue(&ctx->fd_wqh, &wait);
  505. for (;;) {
  506. set_current_state(TASK_INTERRUPTIBLE);
  507. spin_lock(&ctx->fault_pending_wqh.lock);
  508. uwq = find_userfault(ctx);
  509. if (uwq) {
  510. /*
  511. * Use a seqcount to repeat the lockless check
  512. * in wake_userfault() to avoid missing
  513. * wakeups because during the refile both
  514. * waitqueue could become empty if this is the
  515. * only userfault.
  516. */
  517. write_seqcount_begin(&ctx->refile_seq);
  518. /*
  519. * The fault_pending_wqh.lock prevents the uwq
  520. * to disappear from under us.
  521. *
  522. * Refile this userfault from
  523. * fault_pending_wqh to fault_wqh, it's not
  524. * pending anymore after we read it.
  525. *
  526. * Use list_del() by hand (as
  527. * userfaultfd_wake_function also uses
  528. * list_del_init() by hand) to be sure nobody
  529. * changes __remove_wait_queue() to use
  530. * list_del_init() in turn breaking the
  531. * !list_empty_careful() check in
  532. * handle_userfault(). The uwq->wq.task_list
  533. * must never be empty at any time during the
  534. * refile, or the waitqueue could disappear
  535. * from under us. The "wait_queue_head_t"
  536. * parameter of __remove_wait_queue() is unused
  537. * anyway.
  538. */
  539. list_del(&uwq->wq.task_list);
  540. __add_wait_queue(&ctx->fault_wqh, &uwq->wq);
  541. write_seqcount_end(&ctx->refile_seq);
  542. /* careful to always initialize msg if ret == 0 */
  543. *msg = uwq->msg;
  544. spin_unlock(&ctx->fault_pending_wqh.lock);
  545. ret = 0;
  546. break;
  547. }
  548. spin_unlock(&ctx->fault_pending_wqh.lock);
  549. if (signal_pending(current)) {
  550. ret = -ERESTARTSYS;
  551. break;
  552. }
  553. if (no_wait) {
  554. ret = -EAGAIN;
  555. break;
  556. }
  557. spin_unlock(&ctx->fd_wqh.lock);
  558. schedule();
  559. spin_lock(&ctx->fd_wqh.lock);
  560. }
  561. __remove_wait_queue(&ctx->fd_wqh, &wait);
  562. __set_current_state(TASK_RUNNING);
  563. spin_unlock(&ctx->fd_wqh.lock);
  564. return ret;
  565. }
  566. static ssize_t userfaultfd_read(struct file *file, char __user *buf,
  567. size_t count, loff_t *ppos)
  568. {
  569. struct userfaultfd_ctx *ctx = file->private_data;
  570. ssize_t _ret, ret = 0;
  571. struct uffd_msg msg;
  572. int no_wait = file->f_flags & O_NONBLOCK;
  573. if (ctx->state == UFFD_STATE_WAIT_API)
  574. return -EINVAL;
  575. for (;;) {
  576. if (count < sizeof(msg))
  577. return ret ? ret : -EINVAL;
  578. _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
  579. if (_ret < 0)
  580. return ret ? ret : _ret;
  581. if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
  582. return ret ? ret : -EFAULT;
  583. ret += sizeof(msg);
  584. buf += sizeof(msg);
  585. count -= sizeof(msg);
  586. /*
  587. * Allow to read more than one fault at time but only
  588. * block if waiting for the very first one.
  589. */
  590. no_wait = O_NONBLOCK;
  591. }
  592. }
  593. static void __wake_userfault(struct userfaultfd_ctx *ctx,
  594. struct userfaultfd_wake_range *range)
  595. {
  596. unsigned long start, end;
  597. start = range->start;
  598. end = range->start + range->len;
  599. spin_lock(&ctx->fault_pending_wqh.lock);
  600. /* wake all in the range and autoremove */
  601. if (waitqueue_active(&ctx->fault_pending_wqh))
  602. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
  603. range);
  604. if (waitqueue_active(&ctx->fault_wqh))
  605. __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range);
  606. spin_unlock(&ctx->fault_pending_wqh.lock);
  607. }
  608. static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
  609. struct userfaultfd_wake_range *range)
  610. {
  611. unsigned seq;
  612. bool need_wakeup;
  613. /*
  614. * To be sure waitqueue_active() is not reordered by the CPU
  615. * before the pagetable update, use an explicit SMP memory
  616. * barrier here. PT lock release or up_read(mmap_sem) still
  617. * have release semantics that can allow the
  618. * waitqueue_active() to be reordered before the pte update.
  619. */
  620. smp_mb();
  621. /*
  622. * Use waitqueue_active because it's very frequent to
  623. * change the address space atomically even if there are no
  624. * userfaults yet. So we take the spinlock only when we're
  625. * sure we've userfaults to wake.
  626. */
  627. do {
  628. seq = read_seqcount_begin(&ctx->refile_seq);
  629. need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
  630. waitqueue_active(&ctx->fault_wqh);
  631. cond_resched();
  632. } while (read_seqcount_retry(&ctx->refile_seq, seq));
  633. if (need_wakeup)
  634. __wake_userfault(ctx, range);
  635. }
  636. static __always_inline int validate_range(struct mm_struct *mm,
  637. __u64 start, __u64 len)
  638. {
  639. __u64 task_size = mm->task_size;
  640. if (start & ~PAGE_MASK)
  641. return -EINVAL;
  642. if (len & ~PAGE_MASK)
  643. return -EINVAL;
  644. if (!len)
  645. return -EINVAL;
  646. if (start < mmap_min_addr)
  647. return -EINVAL;
  648. if (start >= task_size)
  649. return -EINVAL;
  650. if (len > task_size - start)
  651. return -EINVAL;
  652. return 0;
  653. }
  654. static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  655. unsigned long arg)
  656. {
  657. struct mm_struct *mm = ctx->mm;
  658. struct vm_area_struct *vma, *prev, *cur;
  659. int ret;
  660. struct uffdio_register uffdio_register;
  661. struct uffdio_register __user *user_uffdio_register;
  662. unsigned long vm_flags, new_flags;
  663. bool found;
  664. unsigned long start, end, vma_end;
  665. user_uffdio_register = (struct uffdio_register __user *) arg;
  666. ret = -EFAULT;
  667. if (copy_from_user(&uffdio_register, user_uffdio_register,
  668. sizeof(uffdio_register)-sizeof(__u64)))
  669. goto out;
  670. ret = -EINVAL;
  671. if (!uffdio_register.mode)
  672. goto out;
  673. if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
  674. UFFDIO_REGISTER_MODE_WP))
  675. goto out;
  676. vm_flags = 0;
  677. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  678. vm_flags |= VM_UFFD_MISSING;
  679. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
  680. vm_flags |= VM_UFFD_WP;
  681. /*
  682. * FIXME: remove the below error constraint by
  683. * implementing the wprotect tracking mode.
  684. */
  685. ret = -EINVAL;
  686. goto out;
  687. }
  688. ret = validate_range(mm, uffdio_register.range.start,
  689. uffdio_register.range.len);
  690. if (ret)
  691. goto out;
  692. start = uffdio_register.range.start;
  693. end = start + uffdio_register.range.len;
  694. ret = -ENOMEM;
  695. if (!mmget_not_zero(mm))
  696. goto out;
  697. down_write(&mm->mmap_sem);
  698. vma = find_vma_prev(mm, start, &prev);
  699. if (!vma)
  700. goto out_unlock;
  701. /* check that there's at least one vma in the range */
  702. ret = -EINVAL;
  703. if (vma->vm_start >= end)
  704. goto out_unlock;
  705. /*
  706. * Search for not compatible vmas.
  707. *
  708. * FIXME: this shall be relaxed later so that it doesn't fail
  709. * on tmpfs backed vmas (in addition to the current allowance
  710. * on anonymous vmas).
  711. */
  712. found = false;
  713. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  714. cond_resched();
  715. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  716. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  717. /* check not compatible vmas */
  718. ret = -EINVAL;
  719. if (cur->vm_ops)
  720. goto out_unlock;
  721. /*
  722. * Check that this vma isn't already owned by a
  723. * different userfaultfd. We can't allow more than one
  724. * userfaultfd to own a single vma simultaneously or we
  725. * wouldn't know which one to deliver the userfaults to.
  726. */
  727. ret = -EBUSY;
  728. if (cur->vm_userfaultfd_ctx.ctx &&
  729. cur->vm_userfaultfd_ctx.ctx != ctx)
  730. goto out_unlock;
  731. found = true;
  732. }
  733. BUG_ON(!found);
  734. if (vma->vm_start < start)
  735. prev = vma;
  736. ret = 0;
  737. do {
  738. cond_resched();
  739. BUG_ON(vma->vm_ops);
  740. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  741. vma->vm_userfaultfd_ctx.ctx != ctx);
  742. /*
  743. * Nothing to do: this vma is already registered into this
  744. * userfaultfd and with the right tracking mode too.
  745. */
  746. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  747. (vma->vm_flags & vm_flags) == vm_flags)
  748. goto skip;
  749. if (vma->vm_start > start)
  750. start = vma->vm_start;
  751. vma_end = min(end, vma->vm_end);
  752. new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
  753. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  754. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  755. vma_policy(vma),
  756. ((struct vm_userfaultfd_ctx){ ctx }));
  757. if (prev) {
  758. vma = prev;
  759. goto next;
  760. }
  761. if (vma->vm_start < start) {
  762. ret = split_vma(mm, vma, start, 1);
  763. if (ret)
  764. break;
  765. }
  766. if (vma->vm_end > end) {
  767. ret = split_vma(mm, vma, end, 0);
  768. if (ret)
  769. break;
  770. }
  771. next:
  772. /*
  773. * In the vma_merge() successful mprotect-like case 8:
  774. * the next vma was merged into the current one and
  775. * the current one has not been updated yet.
  776. */
  777. vma->vm_flags = new_flags;
  778. vma->vm_userfaultfd_ctx.ctx = ctx;
  779. skip:
  780. prev = vma;
  781. start = vma->vm_end;
  782. vma = vma->vm_next;
  783. } while (vma && vma->vm_start < end);
  784. out_unlock:
  785. up_write(&mm->mmap_sem);
  786. mmput(mm);
  787. if (!ret) {
  788. /*
  789. * Now that we scanned all vmas we can already tell
  790. * userland which ioctls methods are guaranteed to
  791. * succeed on this range.
  792. */
  793. if (put_user(UFFD_API_RANGE_IOCTLS,
  794. &user_uffdio_register->ioctls))
  795. ret = -EFAULT;
  796. }
  797. out:
  798. return ret;
  799. }
  800. static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
  801. unsigned long arg)
  802. {
  803. struct mm_struct *mm = ctx->mm;
  804. struct vm_area_struct *vma, *prev, *cur;
  805. int ret;
  806. struct uffdio_range uffdio_unregister;
  807. unsigned long new_flags;
  808. bool found;
  809. unsigned long start, end, vma_end;
  810. const void __user *buf = (void __user *)arg;
  811. ret = -EFAULT;
  812. if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
  813. goto out;
  814. ret = validate_range(mm, uffdio_unregister.start,
  815. uffdio_unregister.len);
  816. if (ret)
  817. goto out;
  818. start = uffdio_unregister.start;
  819. end = start + uffdio_unregister.len;
  820. ret = -ENOMEM;
  821. if (!mmget_not_zero(mm))
  822. goto out;
  823. down_write(&mm->mmap_sem);
  824. vma = find_vma_prev(mm, start, &prev);
  825. if (!vma)
  826. goto out_unlock;
  827. /* check that there's at least one vma in the range */
  828. ret = -EINVAL;
  829. if (vma->vm_start >= end)
  830. goto out_unlock;
  831. /*
  832. * Search for not compatible vmas.
  833. *
  834. * FIXME: this shall be relaxed later so that it doesn't fail
  835. * on tmpfs backed vmas (in addition to the current allowance
  836. * on anonymous vmas).
  837. */
  838. found = false;
  839. ret = -EINVAL;
  840. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  841. cond_resched();
  842. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  843. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  844. /*
  845. * Check not compatible vmas, not strictly required
  846. * here as not compatible vmas cannot have an
  847. * userfaultfd_ctx registered on them, but this
  848. * provides for more strict behavior to notice
  849. * unregistration errors.
  850. */
  851. if (cur->vm_ops)
  852. goto out_unlock;
  853. found = true;
  854. }
  855. BUG_ON(!found);
  856. if (vma->vm_start < start)
  857. prev = vma;
  858. ret = 0;
  859. do {
  860. cond_resched();
  861. BUG_ON(vma->vm_ops);
  862. /*
  863. * Nothing to do: this vma is already registered into this
  864. * userfaultfd and with the right tracking mode too.
  865. */
  866. if (!vma->vm_userfaultfd_ctx.ctx)
  867. goto skip;
  868. if (vma->vm_start > start)
  869. start = vma->vm_start;
  870. vma_end = min(end, vma->vm_end);
  871. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  872. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  873. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  874. vma_policy(vma),
  875. NULL_VM_UFFD_CTX);
  876. if (prev) {
  877. vma = prev;
  878. goto next;
  879. }
  880. if (vma->vm_start < start) {
  881. ret = split_vma(mm, vma, start, 1);
  882. if (ret)
  883. break;
  884. }
  885. if (vma->vm_end > end) {
  886. ret = split_vma(mm, vma, end, 0);
  887. if (ret)
  888. break;
  889. }
  890. next:
  891. /*
  892. * In the vma_merge() successful mprotect-like case 8:
  893. * the next vma was merged into the current one and
  894. * the current one has not been updated yet.
  895. */
  896. vma->vm_flags = new_flags;
  897. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  898. skip:
  899. prev = vma;
  900. start = vma->vm_end;
  901. vma = vma->vm_next;
  902. } while (vma && vma->vm_start < end);
  903. out_unlock:
  904. up_write(&mm->mmap_sem);
  905. mmput(mm);
  906. out:
  907. return ret;
  908. }
  909. /*
  910. * userfaultfd_wake may be used in combination with the
  911. * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
  912. */
  913. static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
  914. unsigned long arg)
  915. {
  916. int ret;
  917. struct uffdio_range uffdio_wake;
  918. struct userfaultfd_wake_range range;
  919. const void __user *buf = (void __user *)arg;
  920. ret = -EFAULT;
  921. if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
  922. goto out;
  923. ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
  924. if (ret)
  925. goto out;
  926. range.start = uffdio_wake.start;
  927. range.len = uffdio_wake.len;
  928. /*
  929. * len == 0 means wake all and we don't want to wake all here,
  930. * so check it again to be sure.
  931. */
  932. VM_BUG_ON(!range.len);
  933. wake_userfault(ctx, &range);
  934. ret = 0;
  935. out:
  936. return ret;
  937. }
  938. static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
  939. unsigned long arg)
  940. {
  941. __s64 ret;
  942. struct uffdio_copy uffdio_copy;
  943. struct uffdio_copy __user *user_uffdio_copy;
  944. struct userfaultfd_wake_range range;
  945. user_uffdio_copy = (struct uffdio_copy __user *) arg;
  946. ret = -EFAULT;
  947. if (copy_from_user(&uffdio_copy, user_uffdio_copy,
  948. /* don't copy "copy" last field */
  949. sizeof(uffdio_copy)-sizeof(__s64)))
  950. goto out;
  951. ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
  952. if (ret)
  953. goto out;
  954. /*
  955. * double check for wraparound just in case. copy_from_user()
  956. * will later check uffdio_copy.src + uffdio_copy.len to fit
  957. * in the userland range.
  958. */
  959. ret = -EINVAL;
  960. if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
  961. goto out;
  962. if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
  963. goto out;
  964. if (mmget_not_zero(ctx->mm)) {
  965. ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
  966. uffdio_copy.len);
  967. mmput(ctx->mm);
  968. }
  969. if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
  970. return -EFAULT;
  971. if (ret < 0)
  972. goto out;
  973. BUG_ON(!ret);
  974. /* len == 0 would wake all */
  975. range.len = ret;
  976. if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
  977. range.start = uffdio_copy.dst;
  978. wake_userfault(ctx, &range);
  979. }
  980. ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
  981. out:
  982. return ret;
  983. }
  984. static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
  985. unsigned long arg)
  986. {
  987. __s64 ret;
  988. struct uffdio_zeropage uffdio_zeropage;
  989. struct uffdio_zeropage __user *user_uffdio_zeropage;
  990. struct userfaultfd_wake_range range;
  991. user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
  992. ret = -EFAULT;
  993. if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
  994. /* don't copy "zeropage" last field */
  995. sizeof(uffdio_zeropage)-sizeof(__s64)))
  996. goto out;
  997. ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
  998. uffdio_zeropage.range.len);
  999. if (ret)
  1000. goto out;
  1001. ret = -EINVAL;
  1002. if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
  1003. goto out;
  1004. if (mmget_not_zero(ctx->mm)) {
  1005. ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
  1006. uffdio_zeropage.range.len);
  1007. mmput(ctx->mm);
  1008. }
  1009. if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
  1010. return -EFAULT;
  1011. if (ret < 0)
  1012. goto out;
  1013. /* len == 0 would wake all */
  1014. BUG_ON(!ret);
  1015. range.len = ret;
  1016. if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
  1017. range.start = uffdio_zeropage.range.start;
  1018. wake_userfault(ctx, &range);
  1019. }
  1020. ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
  1021. out:
  1022. return ret;
  1023. }
  1024. /*
  1025. * userland asks for a certain API version and we return which bits
  1026. * and ioctl commands are implemented in this kernel for such API
  1027. * version or -EINVAL if unknown.
  1028. */
  1029. static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  1030. unsigned long arg)
  1031. {
  1032. struct uffdio_api uffdio_api;
  1033. void __user *buf = (void __user *)arg;
  1034. int ret;
  1035. ret = -EINVAL;
  1036. if (ctx->state != UFFD_STATE_WAIT_API)
  1037. goto out;
  1038. ret = -EFAULT;
  1039. if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
  1040. goto out;
  1041. if (uffdio_api.api != UFFD_API || uffdio_api.features) {
  1042. memset(&uffdio_api, 0, sizeof(uffdio_api));
  1043. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1044. goto out;
  1045. ret = -EINVAL;
  1046. goto out;
  1047. }
  1048. uffdio_api.features = UFFD_API_FEATURES;
  1049. uffdio_api.ioctls = UFFD_API_IOCTLS;
  1050. ret = -EFAULT;
  1051. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1052. goto out;
  1053. ctx->state = UFFD_STATE_RUNNING;
  1054. ret = 0;
  1055. out:
  1056. return ret;
  1057. }
  1058. static long userfaultfd_ioctl(struct file *file, unsigned cmd,
  1059. unsigned long arg)
  1060. {
  1061. int ret = -EINVAL;
  1062. struct userfaultfd_ctx *ctx = file->private_data;
  1063. if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
  1064. return -EINVAL;
  1065. switch(cmd) {
  1066. case UFFDIO_API:
  1067. ret = userfaultfd_api(ctx, arg);
  1068. break;
  1069. case UFFDIO_REGISTER:
  1070. ret = userfaultfd_register(ctx, arg);
  1071. break;
  1072. case UFFDIO_UNREGISTER:
  1073. ret = userfaultfd_unregister(ctx, arg);
  1074. break;
  1075. case UFFDIO_WAKE:
  1076. ret = userfaultfd_wake(ctx, arg);
  1077. break;
  1078. case UFFDIO_COPY:
  1079. ret = userfaultfd_copy(ctx, arg);
  1080. break;
  1081. case UFFDIO_ZEROPAGE:
  1082. ret = userfaultfd_zeropage(ctx, arg);
  1083. break;
  1084. }
  1085. return ret;
  1086. }
  1087. #ifdef CONFIG_PROC_FS
  1088. static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
  1089. {
  1090. struct userfaultfd_ctx *ctx = f->private_data;
  1091. wait_queue_t *wq;
  1092. struct userfaultfd_wait_queue *uwq;
  1093. unsigned long pending = 0, total = 0;
  1094. spin_lock(&ctx->fault_pending_wqh.lock);
  1095. list_for_each_entry(wq, &ctx->fault_pending_wqh.task_list, task_list) {
  1096. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1097. pending++;
  1098. total++;
  1099. }
  1100. list_for_each_entry(wq, &ctx->fault_wqh.task_list, task_list) {
  1101. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  1102. total++;
  1103. }
  1104. spin_unlock(&ctx->fault_pending_wqh.lock);
  1105. /*
  1106. * If more protocols will be added, there will be all shown
  1107. * separated by a space. Like this:
  1108. * protocols: aa:... bb:...
  1109. */
  1110. seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
  1111. pending, total, UFFD_API, UFFD_API_FEATURES,
  1112. UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
  1113. }
  1114. #endif
  1115. static const struct file_operations userfaultfd_fops = {
  1116. #ifdef CONFIG_PROC_FS
  1117. .show_fdinfo = userfaultfd_show_fdinfo,
  1118. #endif
  1119. .release = userfaultfd_release,
  1120. .poll = userfaultfd_poll,
  1121. .read = userfaultfd_read,
  1122. .unlocked_ioctl = userfaultfd_ioctl,
  1123. .compat_ioctl = userfaultfd_ioctl,
  1124. .llseek = noop_llseek,
  1125. };
  1126. static void init_once_userfaultfd_ctx(void *mem)
  1127. {
  1128. struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
  1129. init_waitqueue_head(&ctx->fault_pending_wqh);
  1130. init_waitqueue_head(&ctx->fault_wqh);
  1131. init_waitqueue_head(&ctx->fd_wqh);
  1132. seqcount_init(&ctx->refile_seq);
  1133. }
  1134. /**
  1135. * userfaultfd_file_create - Creates an userfaultfd file pointer.
  1136. * @flags: Flags for the userfaultfd file.
  1137. *
  1138. * This function creates an userfaultfd file pointer, w/out installing
  1139. * it into the fd table. This is useful when the userfaultfd file is
  1140. * used during the initialization of data structures that require
  1141. * extra setup after the userfaultfd creation. So the userfaultfd
  1142. * creation is split into the file pointer creation phase, and the
  1143. * file descriptor installation phase. In this way races with
  1144. * userspace closing the newly installed file descriptor can be
  1145. * avoided. Returns an userfaultfd file pointer, or a proper error
  1146. * pointer.
  1147. */
  1148. static struct file *userfaultfd_file_create(int flags)
  1149. {
  1150. struct file *file;
  1151. struct userfaultfd_ctx *ctx;
  1152. BUG_ON(!current->mm);
  1153. /* Check the UFFD_* constants for consistency. */
  1154. BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
  1155. BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
  1156. file = ERR_PTR(-EINVAL);
  1157. if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
  1158. goto out;
  1159. file = ERR_PTR(-ENOMEM);
  1160. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  1161. if (!ctx)
  1162. goto out;
  1163. atomic_set(&ctx->refcount, 1);
  1164. ctx->flags = flags;
  1165. ctx->state = UFFD_STATE_WAIT_API;
  1166. ctx->released = false;
  1167. ctx->mm = current->mm;
  1168. /* prevent the mm struct to be freed */
  1169. atomic_inc(&ctx->mm->mm_count);
  1170. file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
  1171. O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
  1172. if (IS_ERR(file)) {
  1173. mmdrop(ctx->mm);
  1174. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  1175. }
  1176. out:
  1177. return file;
  1178. }
  1179. SYSCALL_DEFINE1(userfaultfd, int, flags)
  1180. {
  1181. int fd, error;
  1182. struct file *file;
  1183. error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS);
  1184. if (error < 0)
  1185. return error;
  1186. fd = error;
  1187. file = userfaultfd_file_create(flags);
  1188. if (IS_ERR(file)) {
  1189. error = PTR_ERR(file);
  1190. goto err_put_unused_fd;
  1191. }
  1192. fd_install(fd, file);
  1193. return fd;
  1194. err_put_unused_fd:
  1195. put_unused_fd(fd);
  1196. return error;
  1197. }
  1198. static int __init userfaultfd_init(void)
  1199. {
  1200. userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
  1201. sizeof(struct userfaultfd_ctx),
  1202. 0,
  1203. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1204. init_once_userfaultfd_ctx);
  1205. return 0;
  1206. }
  1207. __initcall(userfaultfd_init);