userfaultfd.c 51 KB

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