userfaultfd.c 51 KB

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