userfaultfd.c 49 KB

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