userfaultfd.c 35 KB

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