userfaultfd.c 47 KB

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