userfaultfd.c 50 KB

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