userfaultfd.c 56 KB

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