userfaultfd.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mm/userfaultfd.c
  4. *
  5. * Copyright (C) 2015 Red Hat, Inc.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/rmap.h>
  11. #include <linux/swap.h>
  12. #include <linux/swapops.h>
  13. #include <linux/userfaultfd_k.h>
  14. #include <linux/mmu_notifier.h>
  15. #include <linux/hugetlb.h>
  16. #include <linux/shmem_fs.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/tlb.h>
  19. #include "internal.h"
  20. static __always_inline
  21. bool validate_dst_vma(struct vm_area_struct *dst_vma, unsigned long dst_end)
  22. {
  23. /* Make sure that the dst range is fully within dst_vma. */
  24. if (dst_end > dst_vma->vm_end)
  25. return false;
  26. /*
  27. * Check the vma is registered in uffd, this is required to
  28. * enforce the VM_MAYWRITE check done at uffd registration
  29. * time.
  30. */
  31. if (!dst_vma->vm_userfaultfd_ctx.ctx)
  32. return false;
  33. return true;
  34. }
  35. static __always_inline
  36. struct vm_area_struct *find_vma_and_prepare_anon(struct mm_struct *mm,
  37. unsigned long addr)
  38. {
  39. struct vm_area_struct *vma;
  40. mmap_assert_locked(mm);
  41. vma = vma_lookup(mm, addr);
  42. if (!vma)
  43. vma = ERR_PTR(-ENOENT);
  44. else if (!(vma->vm_flags & VM_SHARED) &&
  45. unlikely(anon_vma_prepare(vma)))
  46. vma = ERR_PTR(-ENOMEM);
  47. return vma;
  48. }
  49. #ifdef CONFIG_PER_VMA_LOCK
  50. /*
  51. * uffd_lock_vma() - Lookup and lock vma corresponding to @address.
  52. * @mm: mm to search vma in.
  53. * @address: address that the vma should contain.
  54. *
  55. * Should be called without holding mmap_lock.
  56. *
  57. * Return: A locked vma containing @address, -ENOENT if no vma is found, or
  58. * -ENOMEM if anon_vma couldn't be allocated.
  59. */
  60. static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,
  61. unsigned long address)
  62. {
  63. struct vm_area_struct *vma;
  64. vma = lock_vma_under_rcu(mm, address);
  65. if (vma) {
  66. /*
  67. * We know we're going to need to use anon_vma, so check
  68. * that early.
  69. */
  70. if (!(vma->vm_flags & VM_SHARED) && unlikely(!vma->anon_vma))
  71. vma_end_read(vma);
  72. else
  73. return vma;
  74. }
  75. mmap_read_lock(mm);
  76. vma = find_vma_and_prepare_anon(mm, address);
  77. if (!IS_ERR(vma)) {
  78. /*
  79. * We cannot use vma_start_read() as it may fail due to
  80. * false locked (see comment in vma_start_read()). We
  81. * can avoid that by directly locking vm_lock under
  82. * mmap_lock, which guarantees that nobody can lock the
  83. * vma for write (vma_start_write()) under us.
  84. */
  85. down_read(&vma->vm_lock->lock);
  86. }
  87. mmap_read_unlock(mm);
  88. return vma;
  89. }
  90. static struct vm_area_struct *uffd_mfill_lock(struct mm_struct *dst_mm,
  91. unsigned long dst_start,
  92. unsigned long len)
  93. {
  94. struct vm_area_struct *dst_vma;
  95. dst_vma = uffd_lock_vma(dst_mm, dst_start);
  96. if (IS_ERR(dst_vma) || validate_dst_vma(dst_vma, dst_start + len))
  97. return dst_vma;
  98. vma_end_read(dst_vma);
  99. return ERR_PTR(-ENOENT);
  100. }
  101. static void uffd_mfill_unlock(struct vm_area_struct *vma)
  102. {
  103. vma_end_read(vma);
  104. }
  105. #else
  106. static struct vm_area_struct *uffd_mfill_lock(struct mm_struct *dst_mm,
  107. unsigned long dst_start,
  108. unsigned long len)
  109. {
  110. struct vm_area_struct *dst_vma;
  111. mmap_read_lock(dst_mm);
  112. dst_vma = find_vma_and_prepare_anon(dst_mm, dst_start);
  113. if (IS_ERR(dst_vma))
  114. goto out_unlock;
  115. if (validate_dst_vma(dst_vma, dst_start + len))
  116. return dst_vma;
  117. dst_vma = ERR_PTR(-ENOENT);
  118. out_unlock:
  119. mmap_read_unlock(dst_mm);
  120. return dst_vma;
  121. }
  122. static void uffd_mfill_unlock(struct vm_area_struct *vma)
  123. {
  124. mmap_read_unlock(vma->vm_mm);
  125. }
  126. #endif
  127. /* Check if dst_addr is outside of file's size. Must be called with ptl held. */
  128. static bool mfill_file_over_size(struct vm_area_struct *dst_vma,
  129. unsigned long dst_addr)
  130. {
  131. struct inode *inode;
  132. pgoff_t offset, max_off;
  133. if (!dst_vma->vm_file)
  134. return false;
  135. inode = dst_vma->vm_file->f_inode;
  136. offset = linear_page_index(dst_vma, dst_addr);
  137. max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  138. return offset >= max_off;
  139. }
  140. /*
  141. * Install PTEs, to map dst_addr (within dst_vma) to page.
  142. *
  143. * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
  144. * and anon, and for both shared and private VMAs.
  145. */
  146. int mfill_atomic_install_pte(pmd_t *dst_pmd,
  147. struct vm_area_struct *dst_vma,
  148. unsigned long dst_addr, struct page *page,
  149. bool newly_allocated, uffd_flags_t flags)
  150. {
  151. int ret;
  152. struct mm_struct *dst_mm = dst_vma->vm_mm;
  153. pte_t _dst_pte, *dst_pte;
  154. bool writable = dst_vma->vm_flags & VM_WRITE;
  155. bool vm_shared = dst_vma->vm_flags & VM_SHARED;
  156. spinlock_t *ptl;
  157. struct folio *folio = page_folio(page);
  158. bool page_in_cache = folio_mapping(folio);
  159. _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
  160. _dst_pte = pte_mkdirty(_dst_pte);
  161. if (page_in_cache && !vm_shared)
  162. writable = false;
  163. if (writable)
  164. _dst_pte = pte_mkwrite(_dst_pte, dst_vma);
  165. if (flags & MFILL_ATOMIC_WP)
  166. _dst_pte = pte_mkuffd_wp(_dst_pte);
  167. ret = -EAGAIN;
  168. dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
  169. if (!dst_pte)
  170. goto out;
  171. if (mfill_file_over_size(dst_vma, dst_addr)) {
  172. ret = -EFAULT;
  173. goto out_unlock;
  174. }
  175. ret = -EEXIST;
  176. /*
  177. * We allow to overwrite a pte marker: consider when both MISSING|WP
  178. * registered, we firstly wr-protect a none pte which has no page cache
  179. * page backing it, then access the page.
  180. */
  181. if (!pte_none_mostly(ptep_get(dst_pte)))
  182. goto out_unlock;
  183. if (page_in_cache) {
  184. /* Usually, cache pages are already added to LRU */
  185. if (newly_allocated)
  186. folio_add_lru(folio);
  187. folio_add_file_rmap_pte(folio, page, dst_vma);
  188. } else {
  189. folio_add_new_anon_rmap(folio, dst_vma, dst_addr, RMAP_EXCLUSIVE);
  190. folio_add_lru_vma(folio, dst_vma);
  191. }
  192. /*
  193. * Must happen after rmap, as mm_counter() checks mapping (via
  194. * PageAnon()), which is set by __page_set_anon_rmap().
  195. */
  196. inc_mm_counter(dst_mm, mm_counter(folio));
  197. set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  198. /* No need to invalidate - it was non-present before */
  199. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  200. ret = 0;
  201. out_unlock:
  202. pte_unmap_unlock(dst_pte, ptl);
  203. out:
  204. return ret;
  205. }
  206. static int mfill_atomic_pte_copy(pmd_t *dst_pmd,
  207. struct vm_area_struct *dst_vma,
  208. unsigned long dst_addr,
  209. unsigned long src_addr,
  210. uffd_flags_t flags,
  211. struct folio **foliop)
  212. {
  213. void *kaddr;
  214. int ret;
  215. struct folio *folio;
  216. if (!*foliop) {
  217. ret = -ENOMEM;
  218. folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma,
  219. dst_addr, false);
  220. if (!folio)
  221. goto out;
  222. kaddr = kmap_local_folio(folio, 0);
  223. /*
  224. * The read mmap_lock is held here. Despite the
  225. * mmap_lock being read recursive a deadlock is still
  226. * possible if a writer has taken a lock. For example:
  227. *
  228. * process A thread 1 takes read lock on own mmap_lock
  229. * process A thread 2 calls mmap, blocks taking write lock
  230. * process B thread 1 takes page fault, read lock on own mmap lock
  231. * process B thread 2 calls mmap, blocks taking write lock
  232. * process A thread 1 blocks taking read lock on process B
  233. * process B thread 1 blocks taking read lock on process A
  234. *
  235. * Disable page faults to prevent potential deadlock
  236. * and retry the copy outside the mmap_lock.
  237. */
  238. pagefault_disable();
  239. ret = copy_from_user(kaddr, (const void __user *) src_addr,
  240. PAGE_SIZE);
  241. pagefault_enable();
  242. kunmap_local(kaddr);
  243. /* fallback to copy_from_user outside mmap_lock */
  244. if (unlikely(ret)) {
  245. ret = -ENOENT;
  246. *foliop = folio;
  247. /* don't free the page */
  248. goto out;
  249. }
  250. flush_dcache_folio(folio);
  251. } else {
  252. folio = *foliop;
  253. *foliop = NULL;
  254. }
  255. /*
  256. * The memory barrier inside __folio_mark_uptodate makes sure that
  257. * preceding stores to the page contents become visible before
  258. * the set_pte_at() write.
  259. */
  260. __folio_mark_uptodate(folio);
  261. ret = -ENOMEM;
  262. if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL))
  263. goto out_release;
  264. ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
  265. &folio->page, true, flags);
  266. if (ret)
  267. goto out_release;
  268. out:
  269. return ret;
  270. out_release:
  271. folio_put(folio);
  272. goto out;
  273. }
  274. static int mfill_atomic_pte_zeroed_folio(pmd_t *dst_pmd,
  275. struct vm_area_struct *dst_vma,
  276. unsigned long dst_addr)
  277. {
  278. struct folio *folio;
  279. int ret = -ENOMEM;
  280. folio = vma_alloc_zeroed_movable_folio(dst_vma, dst_addr);
  281. if (!folio)
  282. return ret;
  283. if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL))
  284. goto out_put;
  285. /*
  286. * The memory barrier inside __folio_mark_uptodate makes sure that
  287. * zeroing out the folio become visible before mapping the page
  288. * using set_pte_at(). See do_anonymous_page().
  289. */
  290. __folio_mark_uptodate(folio);
  291. ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
  292. &folio->page, true, 0);
  293. if (ret)
  294. goto out_put;
  295. return 0;
  296. out_put:
  297. folio_put(folio);
  298. return ret;
  299. }
  300. static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,
  301. struct vm_area_struct *dst_vma,
  302. unsigned long dst_addr)
  303. {
  304. pte_t _dst_pte, *dst_pte;
  305. spinlock_t *ptl;
  306. int ret;
  307. if (mm_forbids_zeropage(dst_vma->vm_mm))
  308. return mfill_atomic_pte_zeroed_folio(dst_pmd, dst_vma, dst_addr);
  309. _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
  310. dst_vma->vm_page_prot));
  311. ret = -EAGAIN;
  312. dst_pte = pte_offset_map_lock(dst_vma->vm_mm, dst_pmd, dst_addr, &ptl);
  313. if (!dst_pte)
  314. goto out;
  315. if (mfill_file_over_size(dst_vma, dst_addr)) {
  316. ret = -EFAULT;
  317. goto out_unlock;
  318. }
  319. ret = -EEXIST;
  320. if (!pte_none(ptep_get(dst_pte)))
  321. goto out_unlock;
  322. set_pte_at(dst_vma->vm_mm, dst_addr, dst_pte, _dst_pte);
  323. /* No need to invalidate - it was non-present before */
  324. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  325. ret = 0;
  326. out_unlock:
  327. pte_unmap_unlock(dst_pte, ptl);
  328. out:
  329. return ret;
  330. }
  331. /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
  332. static int mfill_atomic_pte_continue(pmd_t *dst_pmd,
  333. struct vm_area_struct *dst_vma,
  334. unsigned long dst_addr,
  335. uffd_flags_t flags)
  336. {
  337. struct inode *inode = file_inode(dst_vma->vm_file);
  338. pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
  339. struct folio *folio;
  340. struct page *page;
  341. int ret;
  342. ret = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
  343. /* Our caller expects us to return -EFAULT if we failed to find folio */
  344. if (ret == -ENOENT)
  345. ret = -EFAULT;
  346. if (ret)
  347. goto out;
  348. if (!folio) {
  349. ret = -EFAULT;
  350. goto out;
  351. }
  352. page = folio_file_page(folio, pgoff);
  353. if (PageHWPoison(page)) {
  354. ret = -EIO;
  355. goto out_release;
  356. }
  357. ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
  358. page, false, flags);
  359. if (ret)
  360. goto out_release;
  361. folio_unlock(folio);
  362. ret = 0;
  363. out:
  364. return ret;
  365. out_release:
  366. folio_unlock(folio);
  367. folio_put(folio);
  368. goto out;
  369. }
  370. /* Handles UFFDIO_POISON for all non-hugetlb VMAs. */
  371. static int mfill_atomic_pte_poison(pmd_t *dst_pmd,
  372. struct vm_area_struct *dst_vma,
  373. unsigned long dst_addr,
  374. uffd_flags_t flags)
  375. {
  376. int ret;
  377. struct mm_struct *dst_mm = dst_vma->vm_mm;
  378. pte_t _dst_pte, *dst_pte;
  379. spinlock_t *ptl;
  380. _dst_pte = make_pte_marker(PTE_MARKER_POISONED);
  381. ret = -EAGAIN;
  382. dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
  383. if (!dst_pte)
  384. goto out;
  385. if (mfill_file_over_size(dst_vma, dst_addr)) {
  386. ret = -EFAULT;
  387. goto out_unlock;
  388. }
  389. ret = -EEXIST;
  390. /* Refuse to overwrite any PTE, even a PTE marker (e.g. UFFD WP). */
  391. if (!pte_none(ptep_get(dst_pte)))
  392. goto out_unlock;
  393. set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  394. /* No need to invalidate - it was non-present before */
  395. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  396. ret = 0;
  397. out_unlock:
  398. pte_unmap_unlock(dst_pte, ptl);
  399. out:
  400. return ret;
  401. }
  402. static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
  403. {
  404. pgd_t *pgd;
  405. p4d_t *p4d;
  406. pud_t *pud;
  407. pgd = pgd_offset(mm, address);
  408. p4d = p4d_alloc(mm, pgd, address);
  409. if (!p4d)
  410. return NULL;
  411. pud = pud_alloc(mm, p4d, address);
  412. if (!pud)
  413. return NULL;
  414. /*
  415. * Note that we didn't run this because the pmd was
  416. * missing, the *pmd may be already established and in
  417. * turn it may also be a trans_huge_pmd.
  418. */
  419. return pmd_alloc(mm, pud, address);
  420. }
  421. #ifdef CONFIG_HUGETLB_PAGE
  422. /*
  423. * mfill_atomic processing for HUGETLB vmas. Note that this routine is
  424. * called with either vma-lock or mmap_lock held, it will release the lock
  425. * before returning.
  426. */
  427. static __always_inline ssize_t mfill_atomic_hugetlb(
  428. struct userfaultfd_ctx *ctx,
  429. struct vm_area_struct *dst_vma,
  430. unsigned long dst_start,
  431. unsigned long src_start,
  432. unsigned long len,
  433. uffd_flags_t flags)
  434. {
  435. struct mm_struct *dst_mm = dst_vma->vm_mm;
  436. ssize_t err;
  437. pte_t *dst_pte;
  438. unsigned long src_addr, dst_addr;
  439. long copied;
  440. struct folio *folio;
  441. unsigned long vma_hpagesize;
  442. pgoff_t idx;
  443. u32 hash;
  444. struct address_space *mapping;
  445. /*
  446. * There is no default zero huge page for all huge page sizes as
  447. * supported by hugetlb. A PMD_SIZE huge pages may exist as used
  448. * by THP. Since we can not reliably insert a zero page, this
  449. * feature is not supported.
  450. */
  451. if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) {
  452. up_read(&ctx->map_changing_lock);
  453. uffd_mfill_unlock(dst_vma);
  454. return -EINVAL;
  455. }
  456. src_addr = src_start;
  457. dst_addr = dst_start;
  458. copied = 0;
  459. folio = NULL;
  460. vma_hpagesize = vma_kernel_pagesize(dst_vma);
  461. /*
  462. * Validate alignment based on huge page size
  463. */
  464. err = -EINVAL;
  465. if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
  466. goto out_unlock;
  467. retry:
  468. /*
  469. * On routine entry dst_vma is set. If we had to drop mmap_lock and
  470. * retry, dst_vma will be set to NULL and we must lookup again.
  471. */
  472. if (!dst_vma) {
  473. dst_vma = uffd_mfill_lock(dst_mm, dst_start, len);
  474. if (IS_ERR(dst_vma)) {
  475. err = PTR_ERR(dst_vma);
  476. goto out;
  477. }
  478. err = -ENOENT;
  479. if (!is_vm_hugetlb_page(dst_vma))
  480. goto out_unlock_vma;
  481. err = -EINVAL;
  482. if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
  483. goto out_unlock_vma;
  484. /*
  485. * If memory mappings are changing because of non-cooperative
  486. * operation (e.g. mremap) running in parallel, bail out and
  487. * request the user to retry later
  488. */
  489. down_read(&ctx->map_changing_lock);
  490. err = -EAGAIN;
  491. if (atomic_read(&ctx->mmap_changing))
  492. goto out_unlock;
  493. }
  494. while (src_addr < src_start + len) {
  495. BUG_ON(dst_addr >= dst_start + len);
  496. /*
  497. * Serialize via vma_lock and hugetlb_fault_mutex.
  498. * vma_lock ensures the dst_pte remains valid even
  499. * in the case of shared pmds. fault mutex prevents
  500. * races with other faulting threads.
  501. */
  502. idx = linear_page_index(dst_vma, dst_addr);
  503. mapping = dst_vma->vm_file->f_mapping;
  504. hash = hugetlb_fault_mutex_hash(mapping, idx);
  505. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  506. hugetlb_vma_lock_read(dst_vma);
  507. err = -ENOMEM;
  508. dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
  509. if (!dst_pte) {
  510. hugetlb_vma_unlock_read(dst_vma);
  511. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  512. goto out_unlock;
  513. }
  514. if (!uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE) &&
  515. !huge_pte_none_mostly(huge_ptep_get(dst_mm, dst_addr, dst_pte))) {
  516. err = -EEXIST;
  517. hugetlb_vma_unlock_read(dst_vma);
  518. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  519. goto out_unlock;
  520. }
  521. err = hugetlb_mfill_atomic_pte(dst_pte, dst_vma, dst_addr,
  522. src_addr, flags, &folio);
  523. hugetlb_vma_unlock_read(dst_vma);
  524. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  525. cond_resched();
  526. if (unlikely(err == -ENOENT)) {
  527. up_read(&ctx->map_changing_lock);
  528. uffd_mfill_unlock(dst_vma);
  529. BUG_ON(!folio);
  530. err = copy_folio_from_user(folio,
  531. (const void __user *)src_addr, true);
  532. if (unlikely(err)) {
  533. err = -EFAULT;
  534. goto out;
  535. }
  536. dst_vma = NULL;
  537. goto retry;
  538. } else
  539. BUG_ON(folio);
  540. if (!err) {
  541. dst_addr += vma_hpagesize;
  542. src_addr += vma_hpagesize;
  543. copied += vma_hpagesize;
  544. if (fatal_signal_pending(current))
  545. err = -EINTR;
  546. }
  547. if (err)
  548. break;
  549. }
  550. out_unlock:
  551. up_read(&ctx->map_changing_lock);
  552. out_unlock_vma:
  553. uffd_mfill_unlock(dst_vma);
  554. out:
  555. if (folio)
  556. folio_put(folio);
  557. BUG_ON(copied < 0);
  558. BUG_ON(err > 0);
  559. BUG_ON(!copied && !err);
  560. return copied ? copied : err;
  561. }
  562. #else /* !CONFIG_HUGETLB_PAGE */
  563. /* fail at build time if gcc attempts to use this */
  564. extern ssize_t mfill_atomic_hugetlb(struct userfaultfd_ctx *ctx,
  565. struct vm_area_struct *dst_vma,
  566. unsigned long dst_start,
  567. unsigned long src_start,
  568. unsigned long len,
  569. uffd_flags_t flags);
  570. #endif /* CONFIG_HUGETLB_PAGE */
  571. static __always_inline ssize_t mfill_atomic_pte(pmd_t *dst_pmd,
  572. struct vm_area_struct *dst_vma,
  573. unsigned long dst_addr,
  574. unsigned long src_addr,
  575. uffd_flags_t flags,
  576. struct folio **foliop)
  577. {
  578. ssize_t err;
  579. if (uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) {
  580. return mfill_atomic_pte_continue(dst_pmd, dst_vma,
  581. dst_addr, flags);
  582. } else if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) {
  583. return mfill_atomic_pte_poison(dst_pmd, dst_vma,
  584. dst_addr, flags);
  585. }
  586. /*
  587. * The normal page fault path for a shmem will invoke the
  588. * fault, fill the hole in the file and COW it right away. The
  589. * result generates plain anonymous memory. So when we are
  590. * asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
  591. * generate anonymous memory directly without actually filling
  592. * the hole. For the MAP_PRIVATE case the robustness check
  593. * only happens in the pagetable (to verify it's still none)
  594. * and not in the radix tree.
  595. */
  596. if (!(dst_vma->vm_flags & VM_SHARED)) {
  597. if (uffd_flags_mode_is(flags, MFILL_ATOMIC_COPY))
  598. err = mfill_atomic_pte_copy(dst_pmd, dst_vma,
  599. dst_addr, src_addr,
  600. flags, foliop);
  601. else
  602. err = mfill_atomic_pte_zeropage(dst_pmd,
  603. dst_vma, dst_addr);
  604. } else {
  605. err = shmem_mfill_atomic_pte(dst_pmd, dst_vma,
  606. dst_addr, src_addr,
  607. flags, foliop);
  608. }
  609. return err;
  610. }
  611. static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx,
  612. unsigned long dst_start,
  613. unsigned long src_start,
  614. unsigned long len,
  615. uffd_flags_t flags)
  616. {
  617. struct mm_struct *dst_mm = ctx->mm;
  618. struct vm_area_struct *dst_vma;
  619. ssize_t err;
  620. pmd_t *dst_pmd;
  621. unsigned long src_addr, dst_addr;
  622. long copied;
  623. struct folio *folio;
  624. /*
  625. * Sanitize the command parameters:
  626. */
  627. BUG_ON(dst_start & ~PAGE_MASK);
  628. BUG_ON(len & ~PAGE_MASK);
  629. /* Does the address range wrap, or is the span zero-sized? */
  630. BUG_ON(src_start + len <= src_start);
  631. BUG_ON(dst_start + len <= dst_start);
  632. src_addr = src_start;
  633. dst_addr = dst_start;
  634. copied = 0;
  635. folio = NULL;
  636. retry:
  637. /*
  638. * Make sure the vma is not shared, that the dst range is
  639. * both valid and fully within a single existing vma.
  640. */
  641. dst_vma = uffd_mfill_lock(dst_mm, dst_start, len);
  642. if (IS_ERR(dst_vma)) {
  643. err = PTR_ERR(dst_vma);
  644. goto out;
  645. }
  646. /*
  647. * If memory mappings are changing because of non-cooperative
  648. * operation (e.g. mremap) running in parallel, bail out and
  649. * request the user to retry later
  650. */
  651. down_read(&ctx->map_changing_lock);
  652. err = -EAGAIN;
  653. if (atomic_read(&ctx->mmap_changing))
  654. goto out_unlock;
  655. err = -EINVAL;
  656. /*
  657. * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but
  658. * it will overwrite vm_ops, so vma_is_anonymous must return false.
  659. */
  660. if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) &&
  661. dst_vma->vm_flags & VM_SHARED))
  662. goto out_unlock;
  663. /*
  664. * validate 'mode' now that we know the dst_vma: don't allow
  665. * a wrprotect copy if the userfaultfd didn't register as WP.
  666. */
  667. if ((flags & MFILL_ATOMIC_WP) && !(dst_vma->vm_flags & VM_UFFD_WP))
  668. goto out_unlock;
  669. /*
  670. * If this is a HUGETLB vma, pass off to appropriate routine
  671. */
  672. if (is_vm_hugetlb_page(dst_vma))
  673. return mfill_atomic_hugetlb(ctx, dst_vma, dst_start,
  674. src_start, len, flags);
  675. if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma))
  676. goto out_unlock;
  677. if (!vma_is_shmem(dst_vma) &&
  678. uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE))
  679. goto out_unlock;
  680. while (src_addr < src_start + len) {
  681. pmd_t dst_pmdval;
  682. BUG_ON(dst_addr >= dst_start + len);
  683. dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
  684. if (unlikely(!dst_pmd)) {
  685. err = -ENOMEM;
  686. break;
  687. }
  688. dst_pmdval = pmdp_get_lockless(dst_pmd);
  689. if (unlikely(pmd_none(dst_pmdval)) &&
  690. unlikely(__pte_alloc(dst_mm, dst_pmd))) {
  691. err = -ENOMEM;
  692. break;
  693. }
  694. dst_pmdval = pmdp_get_lockless(dst_pmd);
  695. /*
  696. * If the dst_pmd is THP don't override it and just be strict.
  697. * (This includes the case where the PMD used to be THP and
  698. * changed back to none after __pte_alloc().)
  699. */
  700. if (unlikely(!pmd_present(dst_pmdval) || pmd_trans_huge(dst_pmdval) ||
  701. pmd_devmap(dst_pmdval))) {
  702. err = -EEXIST;
  703. break;
  704. }
  705. if (unlikely(pmd_bad(dst_pmdval))) {
  706. err = -EFAULT;
  707. break;
  708. }
  709. /*
  710. * For shmem mappings, khugepaged is allowed to remove page
  711. * tables under us; pte_offset_map_lock() will deal with that.
  712. */
  713. err = mfill_atomic_pte(dst_pmd, dst_vma, dst_addr,
  714. src_addr, flags, &folio);
  715. cond_resched();
  716. if (unlikely(err == -ENOENT)) {
  717. void *kaddr;
  718. up_read(&ctx->map_changing_lock);
  719. uffd_mfill_unlock(dst_vma);
  720. BUG_ON(!folio);
  721. kaddr = kmap_local_folio(folio, 0);
  722. err = copy_from_user(kaddr,
  723. (const void __user *) src_addr,
  724. PAGE_SIZE);
  725. kunmap_local(kaddr);
  726. if (unlikely(err)) {
  727. err = -EFAULT;
  728. goto out;
  729. }
  730. flush_dcache_folio(folio);
  731. goto retry;
  732. } else
  733. BUG_ON(folio);
  734. if (!err) {
  735. dst_addr += PAGE_SIZE;
  736. src_addr += PAGE_SIZE;
  737. copied += PAGE_SIZE;
  738. if (fatal_signal_pending(current))
  739. err = -EINTR;
  740. }
  741. if (err)
  742. break;
  743. }
  744. out_unlock:
  745. up_read(&ctx->map_changing_lock);
  746. uffd_mfill_unlock(dst_vma);
  747. out:
  748. if (folio)
  749. folio_put(folio);
  750. BUG_ON(copied < 0);
  751. BUG_ON(err > 0);
  752. BUG_ON(!copied && !err);
  753. return copied ? copied : err;
  754. }
  755. ssize_t mfill_atomic_copy(struct userfaultfd_ctx *ctx, unsigned long dst_start,
  756. unsigned long src_start, unsigned long len,
  757. uffd_flags_t flags)
  758. {
  759. return mfill_atomic(ctx, dst_start, src_start, len,
  760. uffd_flags_set_mode(flags, MFILL_ATOMIC_COPY));
  761. }
  762. ssize_t mfill_atomic_zeropage(struct userfaultfd_ctx *ctx,
  763. unsigned long start,
  764. unsigned long len)
  765. {
  766. return mfill_atomic(ctx, start, 0, len,
  767. uffd_flags_set_mode(0, MFILL_ATOMIC_ZEROPAGE));
  768. }
  769. ssize_t mfill_atomic_continue(struct userfaultfd_ctx *ctx, unsigned long start,
  770. unsigned long len, uffd_flags_t flags)
  771. {
  772. /*
  773. * A caller might reasonably assume that UFFDIO_CONTINUE contains an
  774. * smp_wmb() to ensure that any writes to the about-to-be-mapped page by
  775. * the thread doing the UFFDIO_CONTINUE are guaranteed to be visible to
  776. * subsequent loads from the page through the newly mapped address range.
  777. */
  778. smp_wmb();
  779. return mfill_atomic(ctx, start, 0, len,
  780. uffd_flags_set_mode(flags, MFILL_ATOMIC_CONTINUE));
  781. }
  782. ssize_t mfill_atomic_poison(struct userfaultfd_ctx *ctx, unsigned long start,
  783. unsigned long len, uffd_flags_t flags)
  784. {
  785. return mfill_atomic(ctx, start, 0, len,
  786. uffd_flags_set_mode(flags, MFILL_ATOMIC_POISON));
  787. }
  788. long uffd_wp_range(struct vm_area_struct *dst_vma,
  789. unsigned long start, unsigned long len, bool enable_wp)
  790. {
  791. unsigned int mm_cp_flags;
  792. struct mmu_gather tlb;
  793. long ret;
  794. VM_WARN_ONCE(start < dst_vma->vm_start || start + len > dst_vma->vm_end,
  795. "The address range exceeds VMA boundary.\n");
  796. if (enable_wp)
  797. mm_cp_flags = MM_CP_UFFD_WP;
  798. else
  799. mm_cp_flags = MM_CP_UFFD_WP_RESOLVE;
  800. /*
  801. * vma->vm_page_prot already reflects that uffd-wp is enabled for this
  802. * VMA (see userfaultfd_set_vm_flags()) and that all PTEs are supposed
  803. * to be write-protected as default whenever protection changes.
  804. * Try upgrading write permissions manually.
  805. */
  806. if (!enable_wp && vma_wants_manual_pte_write_upgrade(dst_vma))
  807. mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
  808. tlb_gather_mmu(&tlb, dst_vma->vm_mm);
  809. ret = change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
  810. tlb_finish_mmu(&tlb);
  811. return ret;
  812. }
  813. int mwriteprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
  814. unsigned long len, bool enable_wp)
  815. {
  816. struct mm_struct *dst_mm = ctx->mm;
  817. unsigned long end = start + len;
  818. unsigned long _start, _end;
  819. struct vm_area_struct *dst_vma;
  820. unsigned long page_mask;
  821. long err;
  822. VMA_ITERATOR(vmi, dst_mm, start);
  823. /*
  824. * Sanitize the command parameters:
  825. */
  826. BUG_ON(start & ~PAGE_MASK);
  827. BUG_ON(len & ~PAGE_MASK);
  828. /* Does the address range wrap, or is the span zero-sized? */
  829. BUG_ON(start + len <= start);
  830. mmap_read_lock(dst_mm);
  831. /*
  832. * If memory mappings are changing because of non-cooperative
  833. * operation (e.g. mremap) running in parallel, bail out and
  834. * request the user to retry later
  835. */
  836. down_read(&ctx->map_changing_lock);
  837. err = -EAGAIN;
  838. if (atomic_read(&ctx->mmap_changing))
  839. goto out_unlock;
  840. err = -ENOENT;
  841. for_each_vma_range(vmi, dst_vma, end) {
  842. if (!userfaultfd_wp(dst_vma)) {
  843. err = -ENOENT;
  844. break;
  845. }
  846. if (is_vm_hugetlb_page(dst_vma)) {
  847. err = -EINVAL;
  848. page_mask = vma_kernel_pagesize(dst_vma) - 1;
  849. if ((start & page_mask) || (len & page_mask))
  850. break;
  851. }
  852. _start = max(dst_vma->vm_start, start);
  853. _end = min(dst_vma->vm_end, end);
  854. err = uffd_wp_range(dst_vma, _start, _end - _start, enable_wp);
  855. /* Return 0 on success, <0 on failures */
  856. if (err < 0)
  857. break;
  858. err = 0;
  859. }
  860. out_unlock:
  861. up_read(&ctx->map_changing_lock);
  862. mmap_read_unlock(dst_mm);
  863. return err;
  864. }
  865. void double_pt_lock(spinlock_t *ptl1,
  866. spinlock_t *ptl2)
  867. __acquires(ptl1)
  868. __acquires(ptl2)
  869. {
  870. if (ptl1 > ptl2)
  871. swap(ptl1, ptl2);
  872. /* lock in virtual address order to avoid lock inversion */
  873. spin_lock(ptl1);
  874. if (ptl1 != ptl2)
  875. spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);
  876. else
  877. __acquire(ptl2);
  878. }
  879. void double_pt_unlock(spinlock_t *ptl1,
  880. spinlock_t *ptl2)
  881. __releases(ptl1)
  882. __releases(ptl2)
  883. {
  884. spin_unlock(ptl1);
  885. if (ptl1 != ptl2)
  886. spin_unlock(ptl2);
  887. else
  888. __release(ptl2);
  889. }
  890. static int move_present_pte(struct mm_struct *mm,
  891. struct vm_area_struct *dst_vma,
  892. struct vm_area_struct *src_vma,
  893. unsigned long dst_addr, unsigned long src_addr,
  894. pte_t *dst_pte, pte_t *src_pte,
  895. pte_t orig_dst_pte, pte_t orig_src_pte,
  896. spinlock_t *dst_ptl, spinlock_t *src_ptl,
  897. struct folio *src_folio)
  898. {
  899. int err = 0;
  900. double_pt_lock(dst_ptl, src_ptl);
  901. if (!pte_same(ptep_get(src_pte), orig_src_pte) ||
  902. !pte_same(ptep_get(dst_pte), orig_dst_pte)) {
  903. err = -EAGAIN;
  904. goto out;
  905. }
  906. if (folio_test_large(src_folio) ||
  907. folio_maybe_dma_pinned(src_folio) ||
  908. !PageAnonExclusive(&src_folio->page)) {
  909. err = -EBUSY;
  910. goto out;
  911. }
  912. orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte);
  913. /* Folio got pinned from under us. Put it back and fail the move. */
  914. if (folio_maybe_dma_pinned(src_folio)) {
  915. set_pte_at(mm, src_addr, src_pte, orig_src_pte);
  916. err = -EBUSY;
  917. goto out;
  918. }
  919. folio_move_anon_rmap(src_folio, dst_vma);
  920. src_folio->index = linear_page_index(dst_vma, dst_addr);
  921. orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot);
  922. /* Follow mremap() behavior and treat the entry dirty after the move */
  923. orig_dst_pte = pte_mkwrite(pte_mkdirty(orig_dst_pte), dst_vma);
  924. set_pte_at(mm, dst_addr, dst_pte, orig_dst_pte);
  925. out:
  926. double_pt_unlock(dst_ptl, src_ptl);
  927. return err;
  928. }
  929. static int move_swap_pte(struct mm_struct *mm,
  930. unsigned long dst_addr, unsigned long src_addr,
  931. pte_t *dst_pte, pte_t *src_pte,
  932. pte_t orig_dst_pte, pte_t orig_src_pte,
  933. spinlock_t *dst_ptl, spinlock_t *src_ptl)
  934. {
  935. if (!pte_swp_exclusive(orig_src_pte))
  936. return -EBUSY;
  937. double_pt_lock(dst_ptl, src_ptl);
  938. if (!pte_same(ptep_get(src_pte), orig_src_pte) ||
  939. !pte_same(ptep_get(dst_pte), orig_dst_pte)) {
  940. double_pt_unlock(dst_ptl, src_ptl);
  941. return -EAGAIN;
  942. }
  943. orig_src_pte = ptep_get_and_clear(mm, src_addr, src_pte);
  944. set_pte_at(mm, dst_addr, dst_pte, orig_src_pte);
  945. double_pt_unlock(dst_ptl, src_ptl);
  946. return 0;
  947. }
  948. static int move_zeropage_pte(struct mm_struct *mm,
  949. struct vm_area_struct *dst_vma,
  950. struct vm_area_struct *src_vma,
  951. unsigned long dst_addr, unsigned long src_addr,
  952. pte_t *dst_pte, pte_t *src_pte,
  953. pte_t orig_dst_pte, pte_t orig_src_pte,
  954. spinlock_t *dst_ptl, spinlock_t *src_ptl)
  955. {
  956. pte_t zero_pte;
  957. double_pt_lock(dst_ptl, src_ptl);
  958. if (!pte_same(ptep_get(src_pte), orig_src_pte) ||
  959. !pte_same(ptep_get(dst_pte), orig_dst_pte)) {
  960. double_pt_unlock(dst_ptl, src_ptl);
  961. return -EAGAIN;
  962. }
  963. zero_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
  964. dst_vma->vm_page_prot));
  965. ptep_clear_flush(src_vma, src_addr, src_pte);
  966. set_pte_at(mm, dst_addr, dst_pte, zero_pte);
  967. double_pt_unlock(dst_ptl, src_ptl);
  968. return 0;
  969. }
  970. /*
  971. * The mmap_lock for reading is held by the caller. Just move the page
  972. * from src_pmd to dst_pmd if possible, and return true if succeeded
  973. * in moving the page.
  974. */
  975. static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
  976. struct vm_area_struct *dst_vma,
  977. struct vm_area_struct *src_vma,
  978. unsigned long dst_addr, unsigned long src_addr,
  979. __u64 mode)
  980. {
  981. swp_entry_t entry;
  982. pte_t orig_src_pte, orig_dst_pte;
  983. pte_t src_folio_pte;
  984. spinlock_t *src_ptl, *dst_ptl;
  985. pte_t *src_pte = NULL;
  986. pte_t *dst_pte = NULL;
  987. struct folio *src_folio = NULL;
  988. struct anon_vma *src_anon_vma = NULL;
  989. struct mmu_notifier_range range;
  990. int err = 0;
  991. flush_cache_range(src_vma, src_addr, src_addr + PAGE_SIZE);
  992. mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
  993. src_addr, src_addr + PAGE_SIZE);
  994. mmu_notifier_invalidate_range_start(&range);
  995. retry:
  996. dst_pte = pte_offset_map_nolock(mm, dst_pmd, dst_addr, &dst_ptl);
  997. /* Retry if a huge pmd materialized from under us */
  998. if (unlikely(!dst_pte)) {
  999. err = -EAGAIN;
  1000. goto out;
  1001. }
  1002. src_pte = pte_offset_map_nolock(mm, src_pmd, src_addr, &src_ptl);
  1003. /*
  1004. * We held the mmap_lock for reading so MADV_DONTNEED
  1005. * can zap transparent huge pages under us, or the
  1006. * transparent huge page fault can establish new
  1007. * transparent huge pages under us.
  1008. */
  1009. if (unlikely(!src_pte)) {
  1010. err = -EAGAIN;
  1011. goto out;
  1012. }
  1013. /* Sanity checks before the operation */
  1014. if (WARN_ON_ONCE(pmd_none(*dst_pmd)) || WARN_ON_ONCE(pmd_none(*src_pmd)) ||
  1015. WARN_ON_ONCE(pmd_trans_huge(*dst_pmd)) || WARN_ON_ONCE(pmd_trans_huge(*src_pmd))) {
  1016. err = -EINVAL;
  1017. goto out;
  1018. }
  1019. spin_lock(dst_ptl);
  1020. orig_dst_pte = ptep_get(dst_pte);
  1021. spin_unlock(dst_ptl);
  1022. if (!pte_none(orig_dst_pte)) {
  1023. err = -EEXIST;
  1024. goto out;
  1025. }
  1026. spin_lock(src_ptl);
  1027. orig_src_pte = ptep_get(src_pte);
  1028. spin_unlock(src_ptl);
  1029. if (pte_none(orig_src_pte)) {
  1030. if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES))
  1031. err = -ENOENT;
  1032. else /* nothing to do to move a hole */
  1033. err = 0;
  1034. goto out;
  1035. }
  1036. /* If PTE changed after we locked the folio them start over */
  1037. if (src_folio && unlikely(!pte_same(src_folio_pte, orig_src_pte))) {
  1038. err = -EAGAIN;
  1039. goto out;
  1040. }
  1041. if (pte_present(orig_src_pte)) {
  1042. if (is_zero_pfn(pte_pfn(orig_src_pte))) {
  1043. err = move_zeropage_pte(mm, dst_vma, src_vma,
  1044. dst_addr, src_addr, dst_pte, src_pte,
  1045. orig_dst_pte, orig_src_pte,
  1046. dst_ptl, src_ptl);
  1047. goto out;
  1048. }
  1049. /*
  1050. * Pin and lock both source folio and anon_vma. Since we are in
  1051. * RCU read section, we can't block, so on contention have to
  1052. * unmap the ptes, obtain the lock and retry.
  1053. */
  1054. if (!src_folio) {
  1055. struct folio *folio;
  1056. /*
  1057. * Pin the page while holding the lock to be sure the
  1058. * page isn't freed under us
  1059. */
  1060. spin_lock(src_ptl);
  1061. if (!pte_same(orig_src_pte, ptep_get(src_pte))) {
  1062. spin_unlock(src_ptl);
  1063. err = -EAGAIN;
  1064. goto out;
  1065. }
  1066. folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
  1067. if (!folio || !PageAnonExclusive(&folio->page)) {
  1068. spin_unlock(src_ptl);
  1069. err = -EBUSY;
  1070. goto out;
  1071. }
  1072. folio_get(folio);
  1073. src_folio = folio;
  1074. src_folio_pte = orig_src_pte;
  1075. spin_unlock(src_ptl);
  1076. if (!folio_trylock(src_folio)) {
  1077. pte_unmap(&orig_src_pte);
  1078. pte_unmap(&orig_dst_pte);
  1079. src_pte = dst_pte = NULL;
  1080. /* now we can block and wait */
  1081. folio_lock(src_folio);
  1082. goto retry;
  1083. }
  1084. if (WARN_ON_ONCE(!folio_test_anon(src_folio))) {
  1085. err = -EBUSY;
  1086. goto out;
  1087. }
  1088. }
  1089. /* at this point we have src_folio locked */
  1090. if (folio_test_large(src_folio)) {
  1091. /* split_folio() can block */
  1092. pte_unmap(&orig_src_pte);
  1093. pte_unmap(&orig_dst_pte);
  1094. src_pte = dst_pte = NULL;
  1095. err = split_folio(src_folio);
  1096. if (err)
  1097. goto out;
  1098. /* have to reacquire the folio after it got split */
  1099. folio_unlock(src_folio);
  1100. folio_put(src_folio);
  1101. src_folio = NULL;
  1102. goto retry;
  1103. }
  1104. if (!src_anon_vma) {
  1105. /*
  1106. * folio_referenced walks the anon_vma chain
  1107. * without the folio lock. Serialize against it with
  1108. * the anon_vma lock, the folio lock is not enough.
  1109. */
  1110. src_anon_vma = folio_get_anon_vma(src_folio);
  1111. if (!src_anon_vma) {
  1112. /* page was unmapped from under us */
  1113. err = -EAGAIN;
  1114. goto out;
  1115. }
  1116. if (!anon_vma_trylock_write(src_anon_vma)) {
  1117. pte_unmap(&orig_src_pte);
  1118. pte_unmap(&orig_dst_pte);
  1119. src_pte = dst_pte = NULL;
  1120. /* now we can block and wait */
  1121. anon_vma_lock_write(src_anon_vma);
  1122. goto retry;
  1123. }
  1124. }
  1125. err = move_present_pte(mm, dst_vma, src_vma,
  1126. dst_addr, src_addr, dst_pte, src_pte,
  1127. orig_dst_pte, orig_src_pte,
  1128. dst_ptl, src_ptl, src_folio);
  1129. } else {
  1130. entry = pte_to_swp_entry(orig_src_pte);
  1131. if (non_swap_entry(entry)) {
  1132. if (is_migration_entry(entry)) {
  1133. pte_unmap(&orig_src_pte);
  1134. pte_unmap(&orig_dst_pte);
  1135. src_pte = dst_pte = NULL;
  1136. migration_entry_wait(mm, src_pmd, src_addr);
  1137. err = -EAGAIN;
  1138. } else
  1139. err = -EFAULT;
  1140. goto out;
  1141. }
  1142. err = move_swap_pte(mm, dst_addr, src_addr,
  1143. dst_pte, src_pte,
  1144. orig_dst_pte, orig_src_pte,
  1145. dst_ptl, src_ptl);
  1146. }
  1147. out:
  1148. if (src_anon_vma) {
  1149. anon_vma_unlock_write(src_anon_vma);
  1150. put_anon_vma(src_anon_vma);
  1151. }
  1152. if (src_folio) {
  1153. folio_unlock(src_folio);
  1154. folio_put(src_folio);
  1155. }
  1156. if (dst_pte)
  1157. pte_unmap(dst_pte);
  1158. if (src_pte)
  1159. pte_unmap(src_pte);
  1160. mmu_notifier_invalidate_range_end(&range);
  1161. return err;
  1162. }
  1163. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1164. static inline bool move_splits_huge_pmd(unsigned long dst_addr,
  1165. unsigned long src_addr,
  1166. unsigned long src_end)
  1167. {
  1168. return (src_addr & ~HPAGE_PMD_MASK) || (dst_addr & ~HPAGE_PMD_MASK) ||
  1169. src_end - src_addr < HPAGE_PMD_SIZE;
  1170. }
  1171. #else
  1172. static inline bool move_splits_huge_pmd(unsigned long dst_addr,
  1173. unsigned long src_addr,
  1174. unsigned long src_end)
  1175. {
  1176. /* This is unreachable anyway, just to avoid warnings when HPAGE_PMD_SIZE==0 */
  1177. return false;
  1178. }
  1179. #endif
  1180. static inline bool vma_move_compatible(struct vm_area_struct *vma)
  1181. {
  1182. return !(vma->vm_flags & (VM_PFNMAP | VM_IO | VM_HUGETLB |
  1183. VM_MIXEDMAP | VM_SHADOW_STACK));
  1184. }
  1185. static int validate_move_areas(struct userfaultfd_ctx *ctx,
  1186. struct vm_area_struct *src_vma,
  1187. struct vm_area_struct *dst_vma)
  1188. {
  1189. /* Only allow moving if both have the same access and protection */
  1190. if ((src_vma->vm_flags & VM_ACCESS_FLAGS) != (dst_vma->vm_flags & VM_ACCESS_FLAGS) ||
  1191. pgprot_val(src_vma->vm_page_prot) != pgprot_val(dst_vma->vm_page_prot))
  1192. return -EINVAL;
  1193. /* Only allow moving if both are mlocked or both aren't */
  1194. if ((src_vma->vm_flags & VM_LOCKED) != (dst_vma->vm_flags & VM_LOCKED))
  1195. return -EINVAL;
  1196. /*
  1197. * For now, we keep it simple and only move between writable VMAs.
  1198. * Access flags are equal, therefore cheching only the source is enough.
  1199. */
  1200. if (!(src_vma->vm_flags & VM_WRITE))
  1201. return -EINVAL;
  1202. /* Check if vma flags indicate content which can be moved */
  1203. if (!vma_move_compatible(src_vma) || !vma_move_compatible(dst_vma))
  1204. return -EINVAL;
  1205. /* Ensure dst_vma is registered in uffd we are operating on */
  1206. if (!dst_vma->vm_userfaultfd_ctx.ctx ||
  1207. dst_vma->vm_userfaultfd_ctx.ctx != ctx)
  1208. return -EINVAL;
  1209. /* Only allow moving across anonymous vmas */
  1210. if (!vma_is_anonymous(src_vma) || !vma_is_anonymous(dst_vma))
  1211. return -EINVAL;
  1212. return 0;
  1213. }
  1214. static __always_inline
  1215. int find_vmas_mm_locked(struct mm_struct *mm,
  1216. unsigned long dst_start,
  1217. unsigned long src_start,
  1218. struct vm_area_struct **dst_vmap,
  1219. struct vm_area_struct **src_vmap)
  1220. {
  1221. struct vm_area_struct *vma;
  1222. mmap_assert_locked(mm);
  1223. vma = find_vma_and_prepare_anon(mm, dst_start);
  1224. if (IS_ERR(vma))
  1225. return PTR_ERR(vma);
  1226. *dst_vmap = vma;
  1227. /* Skip finding src_vma if src_start is in dst_vma */
  1228. if (src_start >= vma->vm_start && src_start < vma->vm_end)
  1229. goto out_success;
  1230. vma = vma_lookup(mm, src_start);
  1231. if (!vma)
  1232. return -ENOENT;
  1233. out_success:
  1234. *src_vmap = vma;
  1235. return 0;
  1236. }
  1237. #ifdef CONFIG_PER_VMA_LOCK
  1238. static int uffd_move_lock(struct mm_struct *mm,
  1239. unsigned long dst_start,
  1240. unsigned long src_start,
  1241. struct vm_area_struct **dst_vmap,
  1242. struct vm_area_struct **src_vmap)
  1243. {
  1244. struct vm_area_struct *vma;
  1245. int err;
  1246. vma = uffd_lock_vma(mm, dst_start);
  1247. if (IS_ERR(vma))
  1248. return PTR_ERR(vma);
  1249. *dst_vmap = vma;
  1250. /*
  1251. * Skip finding src_vma if src_start is in dst_vma. This also ensures
  1252. * that we don't lock the same vma twice.
  1253. */
  1254. if (src_start >= vma->vm_start && src_start < vma->vm_end) {
  1255. *src_vmap = vma;
  1256. return 0;
  1257. }
  1258. /*
  1259. * Using uffd_lock_vma() to get src_vma can lead to following deadlock:
  1260. *
  1261. * Thread1 Thread2
  1262. * ------- -------
  1263. * vma_start_read(dst_vma)
  1264. * mmap_write_lock(mm)
  1265. * vma_start_write(src_vma)
  1266. * vma_start_read(src_vma)
  1267. * mmap_read_lock(mm)
  1268. * vma_start_write(dst_vma)
  1269. */
  1270. *src_vmap = lock_vma_under_rcu(mm, src_start);
  1271. if (likely(*src_vmap))
  1272. return 0;
  1273. /* Undo any locking and retry in mmap_lock critical section */
  1274. vma_end_read(*dst_vmap);
  1275. mmap_read_lock(mm);
  1276. err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap);
  1277. if (!err) {
  1278. /*
  1279. * See comment in uffd_lock_vma() as to why not using
  1280. * vma_start_read() here.
  1281. */
  1282. down_read(&(*dst_vmap)->vm_lock->lock);
  1283. if (*dst_vmap != *src_vmap)
  1284. down_read_nested(&(*src_vmap)->vm_lock->lock,
  1285. SINGLE_DEPTH_NESTING);
  1286. }
  1287. mmap_read_unlock(mm);
  1288. return err;
  1289. }
  1290. static void uffd_move_unlock(struct vm_area_struct *dst_vma,
  1291. struct vm_area_struct *src_vma)
  1292. {
  1293. vma_end_read(src_vma);
  1294. if (src_vma != dst_vma)
  1295. vma_end_read(dst_vma);
  1296. }
  1297. #else
  1298. static int uffd_move_lock(struct mm_struct *mm,
  1299. unsigned long dst_start,
  1300. unsigned long src_start,
  1301. struct vm_area_struct **dst_vmap,
  1302. struct vm_area_struct **src_vmap)
  1303. {
  1304. int err;
  1305. mmap_read_lock(mm);
  1306. err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap);
  1307. if (err)
  1308. mmap_read_unlock(mm);
  1309. return err;
  1310. }
  1311. static void uffd_move_unlock(struct vm_area_struct *dst_vma,
  1312. struct vm_area_struct *src_vma)
  1313. {
  1314. mmap_assert_locked(src_vma->vm_mm);
  1315. mmap_read_unlock(dst_vma->vm_mm);
  1316. }
  1317. #endif
  1318. /**
  1319. * move_pages - move arbitrary anonymous pages of an existing vma
  1320. * @ctx: pointer to the userfaultfd context
  1321. * @dst_start: start of the destination virtual memory range
  1322. * @src_start: start of the source virtual memory range
  1323. * @len: length of the virtual memory range
  1324. * @mode: flags from uffdio_move.mode
  1325. *
  1326. * It will either use the mmap_lock in read mode or per-vma locks
  1327. *
  1328. * move_pages() remaps arbitrary anonymous pages atomically in zero
  1329. * copy. It only works on non shared anonymous pages because those can
  1330. * be relocated without generating non linear anon_vmas in the rmap
  1331. * code.
  1332. *
  1333. * It provides a zero copy mechanism to handle userspace page faults.
  1334. * The source vma pages should have mapcount == 1, which can be
  1335. * enforced by using madvise(MADV_DONTFORK) on src vma.
  1336. *
  1337. * The thread receiving the page during the userland page fault
  1338. * will receive the faulting page in the source vma through the network,
  1339. * storage or any other I/O device (MADV_DONTFORK in the source vma
  1340. * avoids move_pages() to fail with -EBUSY if the process forks before
  1341. * move_pages() is called), then it will call move_pages() to map the
  1342. * page in the faulting address in the destination vma.
  1343. *
  1344. * This userfaultfd command works purely via pagetables, so it's the
  1345. * most efficient way to move physical non shared anonymous pages
  1346. * across different virtual addresses. Unlike mremap()/mmap()/munmap()
  1347. * it does not create any new vmas. The mapping in the destination
  1348. * address is atomic.
  1349. *
  1350. * It only works if the vma protection bits are identical from the
  1351. * source and destination vma.
  1352. *
  1353. * It can remap non shared anonymous pages within the same vma too.
  1354. *
  1355. * If the source virtual memory range has any unmapped holes, or if
  1356. * the destination virtual memory range is not a whole unmapped hole,
  1357. * move_pages() will fail respectively with -ENOENT or -EEXIST. This
  1358. * provides a very strict behavior to avoid any chance of memory
  1359. * corruption going unnoticed if there are userland race conditions.
  1360. * Only one thread should resolve the userland page fault at any given
  1361. * time for any given faulting address. This means that if two threads
  1362. * try to both call move_pages() on the same destination address at the
  1363. * same time, the second thread will get an explicit error from this
  1364. * command.
  1365. *
  1366. * The command retval will return "len" is successful. The command
  1367. * however can be interrupted by fatal signals or errors. If
  1368. * interrupted it will return the number of bytes successfully
  1369. * remapped before the interruption if any, or the negative error if
  1370. * none. It will never return zero. Either it will return an error or
  1371. * an amount of bytes successfully moved. If the retval reports a
  1372. * "short" remap, the move_pages() command should be repeated by
  1373. * userland with src+retval, dst+reval, len-retval if it wants to know
  1374. * about the error that interrupted it.
  1375. *
  1376. * The UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES flag can be specified to
  1377. * prevent -ENOENT errors to materialize if there are holes in the
  1378. * source virtual range that is being remapped. The holes will be
  1379. * accounted as successfully remapped in the retval of the
  1380. * command. This is mostly useful to remap hugepage naturally aligned
  1381. * virtual regions without knowing if there are transparent hugepage
  1382. * in the regions or not, but preventing the risk of having to split
  1383. * the hugepmd during the remap.
  1384. *
  1385. * If there's any rmap walk that is taking the anon_vma locks without
  1386. * first obtaining the folio lock (the only current instance is
  1387. * folio_referenced), they will have to verify if the folio->mapping
  1388. * has changed after taking the anon_vma lock. If it changed they
  1389. * should release the lock and retry obtaining a new anon_vma, because
  1390. * it means the anon_vma was changed by move_pages() before the lock
  1391. * could be obtained. This is the only additional complexity added to
  1392. * the rmap code to provide this anonymous page remapping functionality.
  1393. */
  1394. ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
  1395. unsigned long src_start, unsigned long len, __u64 mode)
  1396. {
  1397. struct mm_struct *mm = ctx->mm;
  1398. struct vm_area_struct *src_vma, *dst_vma;
  1399. unsigned long src_addr, dst_addr;
  1400. pmd_t *src_pmd, *dst_pmd;
  1401. long err = -EINVAL;
  1402. ssize_t moved = 0;
  1403. /* Sanitize the command parameters. */
  1404. if (WARN_ON_ONCE(src_start & ~PAGE_MASK) ||
  1405. WARN_ON_ONCE(dst_start & ~PAGE_MASK) ||
  1406. WARN_ON_ONCE(len & ~PAGE_MASK))
  1407. goto out;
  1408. /* Does the address range wrap, or is the span zero-sized? */
  1409. if (WARN_ON_ONCE(src_start + len <= src_start) ||
  1410. WARN_ON_ONCE(dst_start + len <= dst_start))
  1411. goto out;
  1412. err = uffd_move_lock(mm, dst_start, src_start, &dst_vma, &src_vma);
  1413. if (err)
  1414. goto out;
  1415. /* Re-check after taking map_changing_lock */
  1416. err = -EAGAIN;
  1417. down_read(&ctx->map_changing_lock);
  1418. if (likely(atomic_read(&ctx->mmap_changing)))
  1419. goto out_unlock;
  1420. /*
  1421. * Make sure the vma is not shared, that the src and dst remap
  1422. * ranges are both valid and fully within a single existing
  1423. * vma.
  1424. */
  1425. err = -EINVAL;
  1426. if (src_vma->vm_flags & VM_SHARED)
  1427. goto out_unlock;
  1428. if (src_start + len > src_vma->vm_end)
  1429. goto out_unlock;
  1430. if (dst_vma->vm_flags & VM_SHARED)
  1431. goto out_unlock;
  1432. if (dst_start + len > dst_vma->vm_end)
  1433. goto out_unlock;
  1434. err = validate_move_areas(ctx, src_vma, dst_vma);
  1435. if (err)
  1436. goto out_unlock;
  1437. for (src_addr = src_start, dst_addr = dst_start;
  1438. src_addr < src_start + len;) {
  1439. spinlock_t *ptl;
  1440. pmd_t dst_pmdval;
  1441. unsigned long step_size;
  1442. /*
  1443. * Below works because anonymous area would not have a
  1444. * transparent huge PUD. If file-backed support is added,
  1445. * that case would need to be handled here.
  1446. */
  1447. src_pmd = mm_find_pmd(mm, src_addr);
  1448. if (unlikely(!src_pmd)) {
  1449. if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) {
  1450. err = -ENOENT;
  1451. break;
  1452. }
  1453. src_pmd = mm_alloc_pmd(mm, src_addr);
  1454. if (unlikely(!src_pmd)) {
  1455. err = -ENOMEM;
  1456. break;
  1457. }
  1458. }
  1459. dst_pmd = mm_alloc_pmd(mm, dst_addr);
  1460. if (unlikely(!dst_pmd)) {
  1461. err = -ENOMEM;
  1462. break;
  1463. }
  1464. dst_pmdval = pmdp_get_lockless(dst_pmd);
  1465. /*
  1466. * If the dst_pmd is mapped as THP don't override it and just
  1467. * be strict. If dst_pmd changes into TPH after this check, the
  1468. * move_pages_huge_pmd() will detect the change and retry
  1469. * while move_pages_pte() will detect the change and fail.
  1470. */
  1471. if (unlikely(pmd_trans_huge(dst_pmdval))) {
  1472. err = -EEXIST;
  1473. break;
  1474. }
  1475. ptl = pmd_trans_huge_lock(src_pmd, src_vma);
  1476. if (ptl) {
  1477. if (pmd_devmap(*src_pmd)) {
  1478. spin_unlock(ptl);
  1479. err = -ENOENT;
  1480. break;
  1481. }
  1482. /* Check if we can move the pmd without splitting it. */
  1483. if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
  1484. !pmd_none(dst_pmdval)) {
  1485. struct folio *folio = pmd_folio(*src_pmd);
  1486. if (!folio || (!is_huge_zero_folio(folio) &&
  1487. !PageAnonExclusive(&folio->page))) {
  1488. spin_unlock(ptl);
  1489. err = -EBUSY;
  1490. break;
  1491. }
  1492. spin_unlock(ptl);
  1493. split_huge_pmd(src_vma, src_pmd, src_addr);
  1494. /* The folio will be split by move_pages_pte() */
  1495. continue;
  1496. }
  1497. err = move_pages_huge_pmd(mm, dst_pmd, src_pmd,
  1498. dst_pmdval, dst_vma, src_vma,
  1499. dst_addr, src_addr);
  1500. step_size = HPAGE_PMD_SIZE;
  1501. } else {
  1502. if (pmd_none(*src_pmd)) {
  1503. if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) {
  1504. err = -ENOENT;
  1505. break;
  1506. }
  1507. if (unlikely(__pte_alloc(mm, src_pmd))) {
  1508. err = -ENOMEM;
  1509. break;
  1510. }
  1511. }
  1512. if (unlikely(pte_alloc(mm, dst_pmd))) {
  1513. err = -ENOMEM;
  1514. break;
  1515. }
  1516. err = move_pages_pte(mm, dst_pmd, src_pmd,
  1517. dst_vma, src_vma,
  1518. dst_addr, src_addr, mode);
  1519. step_size = PAGE_SIZE;
  1520. }
  1521. cond_resched();
  1522. if (fatal_signal_pending(current)) {
  1523. /* Do not override an error */
  1524. if (!err || err == -EAGAIN)
  1525. err = -EINTR;
  1526. break;
  1527. }
  1528. if (err) {
  1529. if (err == -EAGAIN)
  1530. continue;
  1531. break;
  1532. }
  1533. /* Proceed to the next page */
  1534. dst_addr += step_size;
  1535. src_addr += step_size;
  1536. moved += step_size;
  1537. }
  1538. out_unlock:
  1539. up_read(&ctx->map_changing_lock);
  1540. uffd_move_unlock(dst_vma, src_vma);
  1541. out:
  1542. VM_WARN_ON(moved < 0);
  1543. VM_WARN_ON(err > 0);
  1544. VM_WARN_ON(!moved && !err);
  1545. return moved ? moved : err;
  1546. }
  1547. static void userfaultfd_set_vm_flags(struct vm_area_struct *vma,
  1548. vm_flags_t flags)
  1549. {
  1550. const bool uffd_wp_changed = (vma->vm_flags ^ flags) & VM_UFFD_WP;
  1551. vm_flags_reset(vma, flags);
  1552. /*
  1553. * For shared mappings, we want to enable writenotify while
  1554. * userfaultfd-wp is enabled (see vma_wants_writenotify()). We'll simply
  1555. * recalculate vma->vm_page_prot whenever userfaultfd-wp changes.
  1556. */
  1557. if ((vma->vm_flags & VM_SHARED) && uffd_wp_changed)
  1558. vma_set_page_prot(vma);
  1559. }
  1560. static void userfaultfd_set_ctx(struct vm_area_struct *vma,
  1561. struct userfaultfd_ctx *ctx,
  1562. unsigned long flags)
  1563. {
  1564. vma_start_write(vma);
  1565. vma->vm_userfaultfd_ctx = (struct vm_userfaultfd_ctx){ctx};
  1566. userfaultfd_set_vm_flags(vma,
  1567. (vma->vm_flags & ~__VM_UFFD_FLAGS) | flags);
  1568. }
  1569. void userfaultfd_reset_ctx(struct vm_area_struct *vma)
  1570. {
  1571. userfaultfd_set_ctx(vma, NULL, 0);
  1572. }
  1573. struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
  1574. struct vm_area_struct *prev,
  1575. struct vm_area_struct *vma,
  1576. unsigned long start,
  1577. unsigned long end)
  1578. {
  1579. struct vm_area_struct *ret;
  1580. /* Reset ptes for the whole vma range if wr-protected */
  1581. if (userfaultfd_wp(vma))
  1582. uffd_wp_range(vma, start, end - start, false);
  1583. ret = vma_modify_flags_uffd(vmi, prev, vma, start, end,
  1584. vma->vm_flags & ~__VM_UFFD_FLAGS,
  1585. NULL_VM_UFFD_CTX);
  1586. /*
  1587. * In the vma_merge() successful mprotect-like case 8:
  1588. * the next vma was merged into the current one and
  1589. * the current one has not been updated yet.
  1590. */
  1591. if (!IS_ERR(ret))
  1592. userfaultfd_reset_ctx(ret);
  1593. return ret;
  1594. }
  1595. /* Assumes mmap write lock taken, and mm_struct pinned. */
  1596. int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
  1597. struct vm_area_struct *vma,
  1598. unsigned long vm_flags,
  1599. unsigned long start, unsigned long end,
  1600. bool wp_async)
  1601. {
  1602. VMA_ITERATOR(vmi, ctx->mm, start);
  1603. struct vm_area_struct *prev = vma_prev(&vmi);
  1604. unsigned long vma_end;
  1605. unsigned long new_flags;
  1606. if (vma->vm_start < start)
  1607. prev = vma;
  1608. for_each_vma_range(vmi, vma, end) {
  1609. cond_resched();
  1610. BUG_ON(!vma_can_userfault(vma, vm_flags, wp_async));
  1611. BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
  1612. vma->vm_userfaultfd_ctx.ctx != ctx);
  1613. WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
  1614. /*
  1615. * Nothing to do: this vma is already registered into this
  1616. * userfaultfd and with the right tracking mode too.
  1617. */
  1618. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  1619. (vma->vm_flags & vm_flags) == vm_flags)
  1620. goto skip;
  1621. if (vma->vm_start > start)
  1622. start = vma->vm_start;
  1623. vma_end = min(end, vma->vm_end);
  1624. new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags;
  1625. vma = vma_modify_flags_uffd(&vmi, prev, vma, start, vma_end,
  1626. new_flags,
  1627. (struct vm_userfaultfd_ctx){ctx});
  1628. if (IS_ERR(vma))
  1629. return PTR_ERR(vma);
  1630. /*
  1631. * In the vma_merge() successful mprotect-like case 8:
  1632. * the next vma was merged into the current one and
  1633. * the current one has not been updated yet.
  1634. */
  1635. userfaultfd_set_ctx(vma, ctx, vm_flags);
  1636. if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma))
  1637. hugetlb_unshare_all_pmds(vma);
  1638. skip:
  1639. prev = vma;
  1640. start = vma->vm_end;
  1641. }
  1642. return 0;
  1643. }
  1644. void userfaultfd_release_new(struct userfaultfd_ctx *ctx)
  1645. {
  1646. struct mm_struct *mm = ctx->mm;
  1647. struct vm_area_struct *vma;
  1648. VMA_ITERATOR(vmi, mm, 0);
  1649. /* the various vma->vm_userfaultfd_ctx still points to it */
  1650. mmap_write_lock(mm);
  1651. for_each_vma(vmi, vma) {
  1652. if (vma->vm_userfaultfd_ctx.ctx == ctx)
  1653. userfaultfd_reset_ctx(vma);
  1654. }
  1655. mmap_write_unlock(mm);
  1656. }
  1657. void userfaultfd_release_all(struct mm_struct *mm,
  1658. struct userfaultfd_ctx *ctx)
  1659. {
  1660. struct vm_area_struct *vma, *prev;
  1661. VMA_ITERATOR(vmi, mm, 0);
  1662. if (!mmget_not_zero(mm))
  1663. return;
  1664. /*
  1665. * Flush page faults out of all CPUs. NOTE: all page faults
  1666. * must be retried without returning VM_FAULT_SIGBUS if
  1667. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  1668. * changes while handle_userfault released the mmap_lock. So
  1669. * it's critical that released is set to true (above), before
  1670. * taking the mmap_lock for writing.
  1671. */
  1672. mmap_write_lock(mm);
  1673. prev = NULL;
  1674. for_each_vma(vmi, vma) {
  1675. cond_resched();
  1676. BUG_ON(!!vma->vm_userfaultfd_ctx.ctx ^
  1677. !!(vma->vm_flags & __VM_UFFD_FLAGS));
  1678. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  1679. prev = vma;
  1680. continue;
  1681. }
  1682. vma = userfaultfd_clear_vma(&vmi, prev, vma,
  1683. vma->vm_start, vma->vm_end);
  1684. prev = vma;
  1685. }
  1686. mmap_write_unlock(mm);
  1687. mmput(mm);
  1688. }