userfaultfd.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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 && (ctx->features & UFFD_FEATURE_EVENT_REMAP)) {
  662. vm_ctx->ctx = ctx;
  663. userfaultfd_ctx_get(ctx);
  664. WRITE_ONCE(ctx->mmap_changing, true);
  665. }
  666. }
  667. void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *vm_ctx,
  668. unsigned long from, unsigned long to,
  669. unsigned long len)
  670. {
  671. struct userfaultfd_ctx *ctx = vm_ctx->ctx;
  672. struct userfaultfd_wait_queue ewq;
  673. if (!ctx)
  674. return;
  675. if (to & ~PAGE_MASK) {
  676. userfaultfd_ctx_put(ctx);
  677. return;
  678. }
  679. msg_init(&ewq.msg);
  680. ewq.msg.event = UFFD_EVENT_REMAP;
  681. ewq.msg.arg.remap.from = from;
  682. ewq.msg.arg.remap.to = to;
  683. ewq.msg.arg.remap.len = len;
  684. userfaultfd_event_wait_completion(ctx, &ewq);
  685. }
  686. bool userfaultfd_remove(struct vm_area_struct *vma,
  687. unsigned long start, unsigned long end)
  688. {
  689. struct mm_struct *mm = vma->vm_mm;
  690. struct userfaultfd_ctx *ctx;
  691. struct userfaultfd_wait_queue ewq;
  692. ctx = vma->vm_userfaultfd_ctx.ctx;
  693. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_REMOVE))
  694. return true;
  695. userfaultfd_ctx_get(ctx);
  696. WRITE_ONCE(ctx->mmap_changing, true);
  697. up_read(&mm->mmap_sem);
  698. msg_init(&ewq.msg);
  699. ewq.msg.event = UFFD_EVENT_REMOVE;
  700. ewq.msg.arg.remove.start = start;
  701. ewq.msg.arg.remove.end = end;
  702. userfaultfd_event_wait_completion(ctx, &ewq);
  703. return false;
  704. }
  705. static bool has_unmap_ctx(struct userfaultfd_ctx *ctx, struct list_head *unmaps,
  706. unsigned long start, unsigned long end)
  707. {
  708. struct userfaultfd_unmap_ctx *unmap_ctx;
  709. list_for_each_entry(unmap_ctx, unmaps, list)
  710. if (unmap_ctx->ctx == ctx && unmap_ctx->start == start &&
  711. unmap_ctx->end == end)
  712. return true;
  713. return false;
  714. }
  715. int userfaultfd_unmap_prep(struct vm_area_struct *vma,
  716. unsigned long start, unsigned long end,
  717. struct list_head *unmaps)
  718. {
  719. for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
  720. struct userfaultfd_unmap_ctx *unmap_ctx;
  721. struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
  722. if (!ctx || !(ctx->features & UFFD_FEATURE_EVENT_UNMAP) ||
  723. has_unmap_ctx(ctx, unmaps, start, end))
  724. continue;
  725. unmap_ctx = kzalloc(sizeof(*unmap_ctx), GFP_KERNEL);
  726. if (!unmap_ctx)
  727. return -ENOMEM;
  728. userfaultfd_ctx_get(ctx);
  729. WRITE_ONCE(ctx->mmap_changing, true);
  730. unmap_ctx->ctx = ctx;
  731. unmap_ctx->start = start;
  732. unmap_ctx->end = end;
  733. list_add_tail(&unmap_ctx->list, unmaps);
  734. }
  735. return 0;
  736. }
  737. void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf)
  738. {
  739. struct userfaultfd_unmap_ctx *ctx, *n;
  740. struct userfaultfd_wait_queue ewq;
  741. list_for_each_entry_safe(ctx, n, uf, list) {
  742. msg_init(&ewq.msg);
  743. ewq.msg.event = UFFD_EVENT_UNMAP;
  744. ewq.msg.arg.remove.start = ctx->start;
  745. ewq.msg.arg.remove.end = ctx->end;
  746. userfaultfd_event_wait_completion(ctx->ctx, &ewq);
  747. list_del(&ctx->list);
  748. kfree(ctx);
  749. }
  750. }
  751. static int userfaultfd_release(struct inode *inode, struct file *file)
  752. {
  753. struct userfaultfd_ctx *ctx = file->private_data;
  754. struct mm_struct *mm = ctx->mm;
  755. struct vm_area_struct *vma, *prev;
  756. /* len == 0 means wake all */
  757. struct userfaultfd_wake_range range = { .len = 0, };
  758. unsigned long new_flags;
  759. WRITE_ONCE(ctx->released, true);
  760. if (!mmget_not_zero(mm))
  761. goto wakeup;
  762. /*
  763. * Flush page faults out of all CPUs. NOTE: all page faults
  764. * must be retried without returning VM_FAULT_SIGBUS if
  765. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  766. * changes while handle_userfault released the mmap_sem. So
  767. * it's critical that released is set to true (above), before
  768. * taking the mmap_sem for writing.
  769. */
  770. down_write(&mm->mmap_sem);
  771. prev = NULL;
  772. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  773. cond_resched();
  774. BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
  775. !!(vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  776. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  777. prev = vma;
  778. continue;
  779. }
  780. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  781. prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
  782. new_flags, vma->anon_vma,
  783. vma->vm_file, vma->vm_pgoff,
  784. vma_policy(vma),
  785. NULL_VM_UFFD_CTX);
  786. if (prev)
  787. vma = prev;
  788. else
  789. prev = vma;
  790. vma->vm_flags = new_flags;
  791. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  792. }
  793. up_write(&mm->mmap_sem);
  794. mmput(mm);
  795. wakeup:
  796. /*
  797. * After no new page faults can wait on this fault_*wqh, flush
  798. * the last page faults that may have been already waiting on
  799. * the fault_*wqh.
  800. */
  801. spin_lock(&ctx->fault_pending_wqh.lock);
  802. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
  803. __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
  804. spin_unlock(&ctx->fault_pending_wqh.lock);
  805. /* Flush pending events that may still wait on event_wqh */
  806. wake_up_all(&ctx->event_wqh);
  807. wake_up_poll(&ctx->fd_wqh, EPOLLHUP);
  808. userfaultfd_ctx_put(ctx);
  809. return 0;
  810. }
  811. /* fault_pending_wqh.lock must be hold by the caller */
  812. static inline struct userfaultfd_wait_queue *find_userfault_in(
  813. wait_queue_head_t *wqh)
  814. {
  815. wait_queue_entry_t *wq;
  816. struct userfaultfd_wait_queue *uwq;
  817. VM_BUG_ON(!spin_is_locked(&wqh->lock));
  818. uwq = NULL;
  819. if (!waitqueue_active(wqh))
  820. goto out;
  821. /* walk in reverse to provide FIFO behavior to read userfaults */
  822. wq = list_last_entry(&wqh->head, typeof(*wq), entry);
  823. uwq = container_of(wq, struct userfaultfd_wait_queue, wq);
  824. out:
  825. return uwq;
  826. }
  827. static inline struct userfaultfd_wait_queue *find_userfault(
  828. struct userfaultfd_ctx *ctx)
  829. {
  830. return find_userfault_in(&ctx->fault_pending_wqh);
  831. }
  832. static inline struct userfaultfd_wait_queue *find_userfault_evt(
  833. struct userfaultfd_ctx *ctx)
  834. {
  835. return find_userfault_in(&ctx->event_wqh);
  836. }
  837. static __poll_t userfaultfd_poll(struct file *file, poll_table *wait)
  838. {
  839. struct userfaultfd_ctx *ctx = file->private_data;
  840. __poll_t ret;
  841. poll_wait(file, &ctx->fd_wqh, wait);
  842. switch (ctx->state) {
  843. case UFFD_STATE_WAIT_API:
  844. return EPOLLERR;
  845. case UFFD_STATE_RUNNING:
  846. /*
  847. * poll() never guarantees that read won't block.
  848. * userfaults can be waken before they're read().
  849. */
  850. if (unlikely(!(file->f_flags & O_NONBLOCK)))
  851. return EPOLLERR;
  852. /*
  853. * lockless access to see if there are pending faults
  854. * __pollwait last action is the add_wait_queue but
  855. * the spin_unlock would allow the waitqueue_active to
  856. * pass above the actual list_add inside
  857. * add_wait_queue critical section. So use a full
  858. * memory barrier to serialize the list_add write of
  859. * add_wait_queue() with the waitqueue_active read
  860. * below.
  861. */
  862. ret = 0;
  863. smp_mb();
  864. if (waitqueue_active(&ctx->fault_pending_wqh))
  865. ret = EPOLLIN;
  866. else if (waitqueue_active(&ctx->event_wqh))
  867. ret = EPOLLIN;
  868. return ret;
  869. default:
  870. WARN_ON_ONCE(1);
  871. return EPOLLERR;
  872. }
  873. }
  874. static const struct file_operations userfaultfd_fops;
  875. static int resolve_userfault_fork(struct userfaultfd_ctx *ctx,
  876. struct userfaultfd_ctx *new,
  877. struct uffd_msg *msg)
  878. {
  879. int fd;
  880. fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new,
  881. O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS));
  882. if (fd < 0)
  883. return fd;
  884. msg->arg.reserved.reserved1 = 0;
  885. msg->arg.fork.ufd = fd;
  886. return 0;
  887. }
  888. static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait,
  889. struct uffd_msg *msg)
  890. {
  891. ssize_t ret;
  892. DECLARE_WAITQUEUE(wait, current);
  893. struct userfaultfd_wait_queue *uwq;
  894. /*
  895. * Handling fork event requires sleeping operations, so
  896. * we drop the event_wqh lock, then do these ops, then
  897. * lock it back and wake up the waiter. While the lock is
  898. * dropped the ewq may go away so we keep track of it
  899. * carefully.
  900. */
  901. LIST_HEAD(fork_event);
  902. struct userfaultfd_ctx *fork_nctx = NULL;
  903. /* always take the fd_wqh lock before the fault_pending_wqh lock */
  904. spin_lock_irq(&ctx->fd_wqh.lock);
  905. __add_wait_queue(&ctx->fd_wqh, &wait);
  906. for (;;) {
  907. set_current_state(TASK_INTERRUPTIBLE);
  908. spin_lock(&ctx->fault_pending_wqh.lock);
  909. uwq = find_userfault(ctx);
  910. if (uwq) {
  911. /*
  912. * Use a seqcount to repeat the lockless check
  913. * in wake_userfault() to avoid missing
  914. * wakeups because during the refile both
  915. * waitqueue could become empty if this is the
  916. * only userfault.
  917. */
  918. write_seqcount_begin(&ctx->refile_seq);
  919. /*
  920. * The fault_pending_wqh.lock prevents the uwq
  921. * to disappear from under us.
  922. *
  923. * Refile this userfault from
  924. * fault_pending_wqh to fault_wqh, it's not
  925. * pending anymore after we read it.
  926. *
  927. * Use list_del() by hand (as
  928. * userfaultfd_wake_function also uses
  929. * list_del_init() by hand) to be sure nobody
  930. * changes __remove_wait_queue() to use
  931. * list_del_init() in turn breaking the
  932. * !list_empty_careful() check in
  933. * handle_userfault(). The uwq->wq.head list
  934. * must never be empty at any time during the
  935. * refile, or the waitqueue could disappear
  936. * from under us. The "wait_queue_head_t"
  937. * parameter of __remove_wait_queue() is unused
  938. * anyway.
  939. */
  940. list_del(&uwq->wq.entry);
  941. add_wait_queue(&ctx->fault_wqh, &uwq->wq);
  942. write_seqcount_end(&ctx->refile_seq);
  943. /* careful to always initialize msg if ret == 0 */
  944. *msg = uwq->msg;
  945. spin_unlock(&ctx->fault_pending_wqh.lock);
  946. ret = 0;
  947. break;
  948. }
  949. spin_unlock(&ctx->fault_pending_wqh.lock);
  950. spin_lock(&ctx->event_wqh.lock);
  951. uwq = find_userfault_evt(ctx);
  952. if (uwq) {
  953. *msg = uwq->msg;
  954. if (uwq->msg.event == UFFD_EVENT_FORK) {
  955. fork_nctx = (struct userfaultfd_ctx *)
  956. (unsigned long)
  957. uwq->msg.arg.reserved.reserved1;
  958. list_move(&uwq->wq.entry, &fork_event);
  959. /*
  960. * fork_nctx can be freed as soon as
  961. * we drop the lock, unless we take a
  962. * reference on it.
  963. */
  964. userfaultfd_ctx_get(fork_nctx);
  965. spin_unlock(&ctx->event_wqh.lock);
  966. ret = 0;
  967. break;
  968. }
  969. userfaultfd_event_complete(ctx, uwq);
  970. spin_unlock(&ctx->event_wqh.lock);
  971. ret = 0;
  972. break;
  973. }
  974. spin_unlock(&ctx->event_wqh.lock);
  975. if (signal_pending(current)) {
  976. ret = -ERESTARTSYS;
  977. break;
  978. }
  979. if (no_wait) {
  980. ret = -EAGAIN;
  981. break;
  982. }
  983. spin_unlock_irq(&ctx->fd_wqh.lock);
  984. schedule();
  985. spin_lock_irq(&ctx->fd_wqh.lock);
  986. }
  987. __remove_wait_queue(&ctx->fd_wqh, &wait);
  988. __set_current_state(TASK_RUNNING);
  989. spin_unlock_irq(&ctx->fd_wqh.lock);
  990. if (!ret && msg->event == UFFD_EVENT_FORK) {
  991. ret = resolve_userfault_fork(ctx, fork_nctx, msg);
  992. spin_lock(&ctx->event_wqh.lock);
  993. if (!list_empty(&fork_event)) {
  994. /*
  995. * The fork thread didn't abort, so we can
  996. * drop the temporary refcount.
  997. */
  998. userfaultfd_ctx_put(fork_nctx);
  999. uwq = list_first_entry(&fork_event,
  1000. typeof(*uwq),
  1001. wq.entry);
  1002. /*
  1003. * If fork_event list wasn't empty and in turn
  1004. * the event wasn't already released by fork
  1005. * (the event is allocated on fork kernel
  1006. * stack), put the event back to its place in
  1007. * the event_wq. fork_event head will be freed
  1008. * as soon as we return so the event cannot
  1009. * stay queued there no matter the current
  1010. * "ret" value.
  1011. */
  1012. list_del(&uwq->wq.entry);
  1013. __add_wait_queue(&ctx->event_wqh, &uwq->wq);
  1014. /*
  1015. * Leave the event in the waitqueue and report
  1016. * error to userland if we failed to resolve
  1017. * the userfault fork.
  1018. */
  1019. if (likely(!ret))
  1020. userfaultfd_event_complete(ctx, uwq);
  1021. } else {
  1022. /*
  1023. * Here the fork thread aborted and the
  1024. * refcount from the fork thread on fork_nctx
  1025. * has already been released. We still hold
  1026. * the reference we took before releasing the
  1027. * lock above. If resolve_userfault_fork
  1028. * failed we've to drop it because the
  1029. * fork_nctx has to be freed in such case. If
  1030. * it succeeded we'll hold it because the new
  1031. * uffd references it.
  1032. */
  1033. if (ret)
  1034. userfaultfd_ctx_put(fork_nctx);
  1035. }
  1036. spin_unlock(&ctx->event_wqh.lock);
  1037. }
  1038. return ret;
  1039. }
  1040. static ssize_t userfaultfd_read(struct file *file, char __user *buf,
  1041. size_t count, loff_t *ppos)
  1042. {
  1043. struct userfaultfd_ctx *ctx = file->private_data;
  1044. ssize_t _ret, ret = 0;
  1045. struct uffd_msg msg;
  1046. int no_wait = file->f_flags & O_NONBLOCK;
  1047. if (ctx->state == UFFD_STATE_WAIT_API)
  1048. return -EINVAL;
  1049. for (;;) {
  1050. if (count < sizeof(msg))
  1051. return ret ? ret : -EINVAL;
  1052. _ret = userfaultfd_ctx_read(ctx, no_wait, &msg);
  1053. if (_ret < 0)
  1054. return ret ? ret : _ret;
  1055. if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg)))
  1056. return ret ? ret : -EFAULT;
  1057. ret += sizeof(msg);
  1058. buf += sizeof(msg);
  1059. count -= sizeof(msg);
  1060. /*
  1061. * Allow to read more than one fault at time but only
  1062. * block if waiting for the very first one.
  1063. */
  1064. no_wait = O_NONBLOCK;
  1065. }
  1066. }
  1067. static void __wake_userfault(struct userfaultfd_ctx *ctx,
  1068. struct userfaultfd_wake_range *range)
  1069. {
  1070. spin_lock(&ctx->fault_pending_wqh.lock);
  1071. /* wake all in the range and autoremove */
  1072. if (waitqueue_active(&ctx->fault_pending_wqh))
  1073. __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
  1074. range);
  1075. if (waitqueue_active(&ctx->fault_wqh))
  1076. __wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
  1077. spin_unlock(&ctx->fault_pending_wqh.lock);
  1078. }
  1079. static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
  1080. struct userfaultfd_wake_range *range)
  1081. {
  1082. unsigned seq;
  1083. bool need_wakeup;
  1084. /*
  1085. * To be sure waitqueue_active() is not reordered by the CPU
  1086. * before the pagetable update, use an explicit SMP memory
  1087. * barrier here. PT lock release or up_read(mmap_sem) still
  1088. * have release semantics that can allow the
  1089. * waitqueue_active() to be reordered before the pte update.
  1090. */
  1091. smp_mb();
  1092. /*
  1093. * Use waitqueue_active because it's very frequent to
  1094. * change the address space atomically even if there are no
  1095. * userfaults yet. So we take the spinlock only when we're
  1096. * sure we've userfaults to wake.
  1097. */
  1098. do {
  1099. seq = read_seqcount_begin(&ctx->refile_seq);
  1100. need_wakeup = waitqueue_active(&ctx->fault_pending_wqh) ||
  1101. waitqueue_active(&ctx->fault_wqh);
  1102. cond_resched();
  1103. } while (read_seqcount_retry(&ctx->refile_seq, seq));
  1104. if (need_wakeup)
  1105. __wake_userfault(ctx, range);
  1106. }
  1107. static __always_inline int validate_range(struct mm_struct *mm,
  1108. __u64 start, __u64 len)
  1109. {
  1110. __u64 task_size = mm->task_size;
  1111. if (start & ~PAGE_MASK)
  1112. return -EINVAL;
  1113. if (len & ~PAGE_MASK)
  1114. return -EINVAL;
  1115. if (!len)
  1116. return -EINVAL;
  1117. if (start < mmap_min_addr)
  1118. return -EINVAL;
  1119. if (start >= task_size)
  1120. return -EINVAL;
  1121. if (len > task_size - start)
  1122. return -EINVAL;
  1123. return 0;
  1124. }
  1125. static inline bool vma_can_userfault(struct vm_area_struct *vma)
  1126. {
  1127. return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
  1128. vma_is_shmem(vma);
  1129. }
  1130. static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  1131. unsigned long arg)
  1132. {
  1133. struct mm_struct *mm = ctx->mm;
  1134. struct vm_area_struct *vma, *prev, *cur;
  1135. int ret;
  1136. struct uffdio_register uffdio_register;
  1137. struct uffdio_register __user *user_uffdio_register;
  1138. unsigned long vm_flags, new_flags;
  1139. bool found;
  1140. bool basic_ioctls;
  1141. unsigned long start, end, vma_end;
  1142. user_uffdio_register = (struct uffdio_register __user *) arg;
  1143. ret = -EFAULT;
  1144. if (copy_from_user(&uffdio_register, user_uffdio_register,
  1145. sizeof(uffdio_register)-sizeof(__u64)))
  1146. goto out;
  1147. ret = -EINVAL;
  1148. if (!uffdio_register.mode)
  1149. goto out;
  1150. if (uffdio_register.mode & ~(UFFDIO_REGISTER_MODE_MISSING|
  1151. UFFDIO_REGISTER_MODE_WP))
  1152. goto out;
  1153. vm_flags = 0;
  1154. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  1155. vm_flags |= VM_UFFD_MISSING;
  1156. if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
  1157. vm_flags |= VM_UFFD_WP;
  1158. /*
  1159. * FIXME: remove the below error constraint by
  1160. * implementing the wprotect tracking mode.
  1161. */
  1162. ret = -EINVAL;
  1163. goto out;
  1164. }
  1165. ret = validate_range(mm, uffdio_register.range.start,
  1166. uffdio_register.range.len);
  1167. if (ret)
  1168. goto out;
  1169. start = uffdio_register.range.start;
  1170. end = start + uffdio_register.range.len;
  1171. ret = -ENOMEM;
  1172. if (!mmget_not_zero(mm))
  1173. goto out;
  1174. down_write(&mm->mmap_sem);
  1175. vma = find_vma_prev(mm, start, &prev);
  1176. if (!vma)
  1177. goto out_unlock;
  1178. /* check that there's at least one vma in the range */
  1179. ret = -EINVAL;
  1180. if (vma->vm_start >= end)
  1181. goto out_unlock;
  1182. /*
  1183. * If the first vma contains huge pages, make sure start address
  1184. * is aligned to huge page size.
  1185. */
  1186. if (is_vm_hugetlb_page(vma)) {
  1187. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1188. if (start & (vma_hpagesize - 1))
  1189. goto out_unlock;
  1190. }
  1191. /*
  1192. * Search for not compatible vmas.
  1193. */
  1194. found = false;
  1195. basic_ioctls = false;
  1196. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1197. cond_resched();
  1198. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1199. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1200. /* check not compatible vmas */
  1201. ret = -EINVAL;
  1202. if (!vma_can_userfault(cur))
  1203. goto out_unlock;
  1204. /*
  1205. * UFFDIO_COPY will fill file holes even without
  1206. * PROT_WRITE. This check enforces that if this is a
  1207. * MAP_SHARED, the process has write permission to the backing
  1208. * file. If VM_MAYWRITE is set it also enforces that on a
  1209. * MAP_SHARED vma: there is no F_WRITE_SEAL and no further
  1210. * F_WRITE_SEAL can be taken until the vma is destroyed.
  1211. */
  1212. ret = -EPERM;
  1213. if (unlikely(!(cur->vm_flags & VM_MAYWRITE)))
  1214. goto out_unlock;
  1215. /*
  1216. * If this vma contains ending address, and huge pages
  1217. * check alignment.
  1218. */
  1219. if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
  1220. end > cur->vm_start) {
  1221. unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
  1222. ret = -EINVAL;
  1223. if (end & (vma_hpagesize - 1))
  1224. goto out_unlock;
  1225. }
  1226. /*
  1227. * Check that this vma isn't already owned by a
  1228. * different userfaultfd. We can't allow more than one
  1229. * userfaultfd to own a single vma simultaneously or we
  1230. * wouldn't know which one to deliver the userfaults to.
  1231. */
  1232. ret = -EBUSY;
  1233. if (cur->vm_userfaultfd_ctx.ctx &&
  1234. cur->vm_userfaultfd_ctx.ctx != ctx)
  1235. goto out_unlock;
  1236. /*
  1237. * Note vmas containing huge pages
  1238. */
  1239. if (is_vm_hugetlb_page(cur))
  1240. basic_ioctls = true;
  1241. found = true;
  1242. }
  1243. BUG_ON(!found);
  1244. if (vma->vm_start < start)
  1245. prev = vma;
  1246. ret = 0;
  1247. do {
  1248. cond_resched();
  1249. BUG_ON(!vma_can_userfault(vma));
  1250. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  1251. vma->vm_userfaultfd_ctx.ctx != ctx);
  1252. WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
  1253. /*
  1254. * Nothing to do: this vma is already registered into this
  1255. * userfaultfd and with the right tracking mode too.
  1256. */
  1257. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  1258. (vma->vm_flags & vm_flags) == vm_flags)
  1259. goto skip;
  1260. if (vma->vm_start > start)
  1261. start = vma->vm_start;
  1262. vma_end = min(end, vma->vm_end);
  1263. new_flags = (vma->vm_flags & ~vm_flags) | vm_flags;
  1264. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1265. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1266. vma_policy(vma),
  1267. ((struct vm_userfaultfd_ctx){ ctx }));
  1268. if (prev) {
  1269. vma = prev;
  1270. goto next;
  1271. }
  1272. if (vma->vm_start < start) {
  1273. ret = split_vma(mm, vma, start, 1);
  1274. if (ret)
  1275. break;
  1276. }
  1277. if (vma->vm_end > end) {
  1278. ret = split_vma(mm, vma, end, 0);
  1279. if (ret)
  1280. break;
  1281. }
  1282. next:
  1283. /*
  1284. * In the vma_merge() successful mprotect-like case 8:
  1285. * the next vma was merged into the current one and
  1286. * the current one has not been updated yet.
  1287. */
  1288. vma->vm_flags = new_flags;
  1289. vma->vm_userfaultfd_ctx.ctx = ctx;
  1290. skip:
  1291. prev = vma;
  1292. start = vma->vm_end;
  1293. vma = vma->vm_next;
  1294. } while (vma && vma->vm_start < end);
  1295. out_unlock:
  1296. up_write(&mm->mmap_sem);
  1297. mmput(mm);
  1298. if (!ret) {
  1299. /*
  1300. * Now that we scanned all vmas we can already tell
  1301. * userland which ioctls methods are guaranteed to
  1302. * succeed on this range.
  1303. */
  1304. if (put_user(basic_ioctls ? UFFD_API_RANGE_IOCTLS_BASIC :
  1305. UFFD_API_RANGE_IOCTLS,
  1306. &user_uffdio_register->ioctls))
  1307. ret = -EFAULT;
  1308. }
  1309. out:
  1310. return ret;
  1311. }
  1312. static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
  1313. unsigned long arg)
  1314. {
  1315. struct mm_struct *mm = ctx->mm;
  1316. struct vm_area_struct *vma, *prev, *cur;
  1317. int ret;
  1318. struct uffdio_range uffdio_unregister;
  1319. unsigned long new_flags;
  1320. bool found;
  1321. unsigned long start, end, vma_end;
  1322. const void __user *buf = (void __user *)arg;
  1323. ret = -EFAULT;
  1324. if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
  1325. goto out;
  1326. ret = validate_range(mm, uffdio_unregister.start,
  1327. uffdio_unregister.len);
  1328. if (ret)
  1329. goto out;
  1330. start = uffdio_unregister.start;
  1331. end = start + uffdio_unregister.len;
  1332. ret = -ENOMEM;
  1333. if (!mmget_not_zero(mm))
  1334. goto out;
  1335. down_write(&mm->mmap_sem);
  1336. vma = find_vma_prev(mm, start, &prev);
  1337. if (!vma)
  1338. goto out_unlock;
  1339. /* check that there's at least one vma in the range */
  1340. ret = -EINVAL;
  1341. if (vma->vm_start >= end)
  1342. goto out_unlock;
  1343. /*
  1344. * If the first vma contains huge pages, make sure start address
  1345. * is aligned to huge page size.
  1346. */
  1347. if (is_vm_hugetlb_page(vma)) {
  1348. unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
  1349. if (start & (vma_hpagesize - 1))
  1350. goto out_unlock;
  1351. }
  1352. /*
  1353. * Search for not compatible vmas.
  1354. */
  1355. found = false;
  1356. ret = -EINVAL;
  1357. for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
  1358. cond_resched();
  1359. BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
  1360. !!(cur->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP)));
  1361. /*
  1362. * Check not compatible vmas, not strictly required
  1363. * here as not compatible vmas cannot have an
  1364. * userfaultfd_ctx registered on them, but this
  1365. * provides for more strict behavior to notice
  1366. * unregistration errors.
  1367. */
  1368. if (!vma_can_userfault(cur))
  1369. goto out_unlock;
  1370. found = true;
  1371. }
  1372. BUG_ON(!found);
  1373. if (vma->vm_start < start)
  1374. prev = vma;
  1375. ret = 0;
  1376. do {
  1377. cond_resched();
  1378. BUG_ON(!vma_can_userfault(vma));
  1379. /*
  1380. * Nothing to do: this vma is already registered into this
  1381. * userfaultfd and with the right tracking mode too.
  1382. */
  1383. if (!vma->vm_userfaultfd_ctx.ctx)
  1384. goto skip;
  1385. WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
  1386. if (vma->vm_start > start)
  1387. start = vma->vm_start;
  1388. vma_end = min(end, vma->vm_end);
  1389. if (userfaultfd_missing(vma)) {
  1390. /*
  1391. * Wake any concurrent pending userfault while
  1392. * we unregister, so they will not hang
  1393. * permanently and it avoids userland to call
  1394. * UFFDIO_WAKE explicitly.
  1395. */
  1396. struct userfaultfd_wake_range range;
  1397. range.start = start;
  1398. range.len = vma_end - start;
  1399. wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range);
  1400. }
  1401. new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
  1402. prev = vma_merge(mm, prev, start, vma_end, new_flags,
  1403. vma->anon_vma, vma->vm_file, vma->vm_pgoff,
  1404. vma_policy(vma),
  1405. NULL_VM_UFFD_CTX);
  1406. if (prev) {
  1407. vma = prev;
  1408. goto next;
  1409. }
  1410. if (vma->vm_start < start) {
  1411. ret = split_vma(mm, vma, start, 1);
  1412. if (ret)
  1413. break;
  1414. }
  1415. if (vma->vm_end > end) {
  1416. ret = split_vma(mm, vma, end, 0);
  1417. if (ret)
  1418. break;
  1419. }
  1420. next:
  1421. /*
  1422. * In the vma_merge() successful mprotect-like case 8:
  1423. * the next vma was merged into the current one and
  1424. * the current one has not been updated yet.
  1425. */
  1426. vma->vm_flags = new_flags;
  1427. vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
  1428. skip:
  1429. prev = vma;
  1430. start = vma->vm_end;
  1431. vma = vma->vm_next;
  1432. } while (vma && vma->vm_start < end);
  1433. out_unlock:
  1434. up_write(&mm->mmap_sem);
  1435. mmput(mm);
  1436. out:
  1437. return ret;
  1438. }
  1439. /*
  1440. * userfaultfd_wake may be used in combination with the
  1441. * UFFDIO_*_MODE_DONTWAKE to wakeup userfaults in batches.
  1442. */
  1443. static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
  1444. unsigned long arg)
  1445. {
  1446. int ret;
  1447. struct uffdio_range uffdio_wake;
  1448. struct userfaultfd_wake_range range;
  1449. const void __user *buf = (void __user *)arg;
  1450. ret = -EFAULT;
  1451. if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
  1452. goto out;
  1453. ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
  1454. if (ret)
  1455. goto out;
  1456. range.start = uffdio_wake.start;
  1457. range.len = uffdio_wake.len;
  1458. /*
  1459. * len == 0 means wake all and we don't want to wake all here,
  1460. * so check it again to be sure.
  1461. */
  1462. VM_BUG_ON(!range.len);
  1463. wake_userfault(ctx, &range);
  1464. ret = 0;
  1465. out:
  1466. return ret;
  1467. }
  1468. static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
  1469. unsigned long arg)
  1470. {
  1471. __s64 ret;
  1472. struct uffdio_copy uffdio_copy;
  1473. struct uffdio_copy __user *user_uffdio_copy;
  1474. struct userfaultfd_wake_range range;
  1475. user_uffdio_copy = (struct uffdio_copy __user *) arg;
  1476. ret = -EAGAIN;
  1477. if (READ_ONCE(ctx->mmap_changing))
  1478. goto out;
  1479. ret = -EFAULT;
  1480. if (copy_from_user(&uffdio_copy, user_uffdio_copy,
  1481. /* don't copy "copy" last field */
  1482. sizeof(uffdio_copy)-sizeof(__s64)))
  1483. goto out;
  1484. ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
  1485. if (ret)
  1486. goto out;
  1487. /*
  1488. * double check for wraparound just in case. copy_from_user()
  1489. * will later check uffdio_copy.src + uffdio_copy.len to fit
  1490. * in the userland range.
  1491. */
  1492. ret = -EINVAL;
  1493. if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src)
  1494. goto out;
  1495. if (uffdio_copy.mode & ~UFFDIO_COPY_MODE_DONTWAKE)
  1496. goto out;
  1497. if (mmget_not_zero(ctx->mm)) {
  1498. ret = mcopy_atomic(ctx->mm, uffdio_copy.dst, uffdio_copy.src,
  1499. uffdio_copy.len, &ctx->mmap_changing);
  1500. mmput(ctx->mm);
  1501. } else {
  1502. return -ESRCH;
  1503. }
  1504. if (unlikely(put_user(ret, &user_uffdio_copy->copy)))
  1505. return -EFAULT;
  1506. if (ret < 0)
  1507. goto out;
  1508. BUG_ON(!ret);
  1509. /* len == 0 would wake all */
  1510. range.len = ret;
  1511. if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) {
  1512. range.start = uffdio_copy.dst;
  1513. wake_userfault(ctx, &range);
  1514. }
  1515. ret = range.len == uffdio_copy.len ? 0 : -EAGAIN;
  1516. out:
  1517. return ret;
  1518. }
  1519. static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
  1520. unsigned long arg)
  1521. {
  1522. __s64 ret;
  1523. struct uffdio_zeropage uffdio_zeropage;
  1524. struct uffdio_zeropage __user *user_uffdio_zeropage;
  1525. struct userfaultfd_wake_range range;
  1526. user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg;
  1527. ret = -EAGAIN;
  1528. if (READ_ONCE(ctx->mmap_changing))
  1529. goto out;
  1530. ret = -EFAULT;
  1531. if (copy_from_user(&uffdio_zeropage, user_uffdio_zeropage,
  1532. /* don't copy "zeropage" last field */
  1533. sizeof(uffdio_zeropage)-sizeof(__s64)))
  1534. goto out;
  1535. ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
  1536. uffdio_zeropage.range.len);
  1537. if (ret)
  1538. goto out;
  1539. ret = -EINVAL;
  1540. if (uffdio_zeropage.mode & ~UFFDIO_ZEROPAGE_MODE_DONTWAKE)
  1541. goto out;
  1542. if (mmget_not_zero(ctx->mm)) {
  1543. ret = mfill_zeropage(ctx->mm, uffdio_zeropage.range.start,
  1544. uffdio_zeropage.range.len,
  1545. &ctx->mmap_changing);
  1546. mmput(ctx->mm);
  1547. } else {
  1548. return -ESRCH;
  1549. }
  1550. if (unlikely(put_user(ret, &user_uffdio_zeropage->zeropage)))
  1551. return -EFAULT;
  1552. if (ret < 0)
  1553. goto out;
  1554. /* len == 0 would wake all */
  1555. BUG_ON(!ret);
  1556. range.len = ret;
  1557. if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) {
  1558. range.start = uffdio_zeropage.range.start;
  1559. wake_userfault(ctx, &range);
  1560. }
  1561. ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN;
  1562. out:
  1563. return ret;
  1564. }
  1565. static inline unsigned int uffd_ctx_features(__u64 user_features)
  1566. {
  1567. /*
  1568. * For the current set of features the bits just coincide
  1569. */
  1570. return (unsigned int)user_features;
  1571. }
  1572. /*
  1573. * userland asks for a certain API version and we return which bits
  1574. * and ioctl commands are implemented in this kernel for such API
  1575. * version or -EINVAL if unknown.
  1576. */
  1577. static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  1578. unsigned long arg)
  1579. {
  1580. struct uffdio_api uffdio_api;
  1581. void __user *buf = (void __user *)arg;
  1582. int ret;
  1583. __u64 features;
  1584. ret = -EINVAL;
  1585. if (ctx->state != UFFD_STATE_WAIT_API)
  1586. goto out;
  1587. ret = -EFAULT;
  1588. if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api)))
  1589. goto out;
  1590. features = uffdio_api.features;
  1591. if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) {
  1592. memset(&uffdio_api, 0, sizeof(uffdio_api));
  1593. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1594. goto out;
  1595. ret = -EINVAL;
  1596. goto out;
  1597. }
  1598. /* report all available features and ioctls to userland */
  1599. uffdio_api.features = UFFD_API_FEATURES;
  1600. uffdio_api.ioctls = UFFD_API_IOCTLS;
  1601. ret = -EFAULT;
  1602. if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api)))
  1603. goto out;
  1604. ctx->state = UFFD_STATE_RUNNING;
  1605. /* only enable the requested features for this uffd context */
  1606. ctx->features = uffd_ctx_features(features);
  1607. ret = 0;
  1608. out:
  1609. return ret;
  1610. }
  1611. static long userfaultfd_ioctl(struct file *file, unsigned cmd,
  1612. unsigned long arg)
  1613. {
  1614. int ret = -EINVAL;
  1615. struct userfaultfd_ctx *ctx = file->private_data;
  1616. if (cmd != UFFDIO_API && ctx->state == UFFD_STATE_WAIT_API)
  1617. return -EINVAL;
  1618. switch(cmd) {
  1619. case UFFDIO_API:
  1620. ret = userfaultfd_api(ctx, arg);
  1621. break;
  1622. case UFFDIO_REGISTER:
  1623. ret = userfaultfd_register(ctx, arg);
  1624. break;
  1625. case UFFDIO_UNREGISTER:
  1626. ret = userfaultfd_unregister(ctx, arg);
  1627. break;
  1628. case UFFDIO_WAKE:
  1629. ret = userfaultfd_wake(ctx, arg);
  1630. break;
  1631. case UFFDIO_COPY:
  1632. ret = userfaultfd_copy(ctx, arg);
  1633. break;
  1634. case UFFDIO_ZEROPAGE:
  1635. ret = userfaultfd_zeropage(ctx, arg);
  1636. break;
  1637. }
  1638. return ret;
  1639. }
  1640. #ifdef CONFIG_PROC_FS
  1641. static void userfaultfd_show_fdinfo(struct seq_file *m, struct file *f)
  1642. {
  1643. struct userfaultfd_ctx *ctx = f->private_data;
  1644. wait_queue_entry_t *wq;
  1645. unsigned long pending = 0, total = 0;
  1646. spin_lock(&ctx->fault_pending_wqh.lock);
  1647. list_for_each_entry(wq, &ctx->fault_pending_wqh.head, entry) {
  1648. pending++;
  1649. total++;
  1650. }
  1651. list_for_each_entry(wq, &ctx->fault_wqh.head, entry) {
  1652. total++;
  1653. }
  1654. spin_unlock(&ctx->fault_pending_wqh.lock);
  1655. /*
  1656. * If more protocols will be added, there will be all shown
  1657. * separated by a space. Like this:
  1658. * protocols: aa:... bb:...
  1659. */
  1660. seq_printf(m, "pending:\t%lu\ntotal:\t%lu\nAPI:\t%Lx:%x:%Lx\n",
  1661. pending, total, UFFD_API, ctx->features,
  1662. UFFD_API_IOCTLS|UFFD_API_RANGE_IOCTLS);
  1663. }
  1664. #endif
  1665. static const struct file_operations userfaultfd_fops = {
  1666. #ifdef CONFIG_PROC_FS
  1667. .show_fdinfo = userfaultfd_show_fdinfo,
  1668. #endif
  1669. .release = userfaultfd_release,
  1670. .poll = userfaultfd_poll,
  1671. .read = userfaultfd_read,
  1672. .unlocked_ioctl = userfaultfd_ioctl,
  1673. .compat_ioctl = userfaultfd_ioctl,
  1674. .llseek = noop_llseek,
  1675. };
  1676. static void init_once_userfaultfd_ctx(void *mem)
  1677. {
  1678. struct userfaultfd_ctx *ctx = (struct userfaultfd_ctx *) mem;
  1679. init_waitqueue_head(&ctx->fault_pending_wqh);
  1680. init_waitqueue_head(&ctx->fault_wqh);
  1681. init_waitqueue_head(&ctx->event_wqh);
  1682. init_waitqueue_head(&ctx->fd_wqh);
  1683. seqcount_init(&ctx->refile_seq);
  1684. }
  1685. SYSCALL_DEFINE1(userfaultfd, int, flags)
  1686. {
  1687. struct userfaultfd_ctx *ctx;
  1688. int fd;
  1689. BUG_ON(!current->mm);
  1690. /* Check the UFFD_* constants for consistency. */
  1691. BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC);
  1692. BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK);
  1693. if (flags & ~UFFD_SHARED_FCNTL_FLAGS)
  1694. return -EINVAL;
  1695. ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL);
  1696. if (!ctx)
  1697. return -ENOMEM;
  1698. atomic_set(&ctx->refcount, 1);
  1699. ctx->flags = flags;
  1700. ctx->features = 0;
  1701. ctx->state = UFFD_STATE_WAIT_API;
  1702. ctx->released = false;
  1703. ctx->mmap_changing = false;
  1704. ctx->mm = current->mm;
  1705. /* prevent the mm struct to be freed */
  1706. mmgrab(ctx->mm);
  1707. fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
  1708. O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));
  1709. if (fd < 0) {
  1710. mmdrop(ctx->mm);
  1711. kmem_cache_free(userfaultfd_ctx_cachep, ctx);
  1712. }
  1713. return fd;
  1714. }
  1715. static int __init userfaultfd_init(void)
  1716. {
  1717. userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache",
  1718. sizeof(struct userfaultfd_ctx),
  1719. 0,
  1720. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1721. init_once_userfaultfd_ctx);
  1722. return 0;
  1723. }
  1724. __initcall(userfaultfd_init);