userfaultfd.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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/leafops.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. #include "swap.h"
  21. static __always_inline
  22. bool validate_dst_vma(struct vm_area_struct *dst_vma, unsigned long dst_end)
  23. {
  24. /* Make sure that the dst range is fully within dst_vma. */
  25. if (dst_end > dst_vma->vm_end)
  26. return false;
  27. /*
  28. * Check the vma is registered in uffd, this is required to
  29. * enforce the VM_MAYWRITE check done at uffd registration
  30. * time.
  31. */
  32. if (!dst_vma->vm_userfaultfd_ctx.ctx)
  33. return false;
  34. return true;
  35. }
  36. static __always_inline
  37. struct vm_area_struct *find_vma_and_prepare_anon(struct mm_struct *mm,
  38. unsigned long addr)
  39. {
  40. struct vm_area_struct *vma;
  41. mmap_assert_locked(mm);
  42. vma = vma_lookup(mm, addr);
  43. if (!vma)
  44. vma = ERR_PTR(-ENOENT);
  45. else if (!(vma->vm_flags & VM_SHARED) &&
  46. unlikely(anon_vma_prepare(vma)))
  47. vma = ERR_PTR(-ENOMEM);
  48. return vma;
  49. }
  50. #ifdef CONFIG_PER_VMA_LOCK
  51. /*
  52. * uffd_lock_vma() - Lookup and lock vma corresponding to @address.
  53. * @mm: mm to search vma in.
  54. * @address: address that the vma should contain.
  55. *
  56. * Should be called without holding mmap_lock.
  57. *
  58. * Return: A locked vma containing @address, -ENOENT if no vma is found, or
  59. * -ENOMEM if anon_vma couldn't be allocated.
  60. */
  61. static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,
  62. unsigned long address)
  63. {
  64. struct vm_area_struct *vma;
  65. vma = lock_vma_under_rcu(mm, address);
  66. if (vma) {
  67. /*
  68. * We know we're going to need to use anon_vma, so check
  69. * that early.
  70. */
  71. if (!(vma->vm_flags & VM_SHARED) && unlikely(!vma->anon_vma))
  72. vma_end_read(vma);
  73. else
  74. return vma;
  75. }
  76. mmap_read_lock(mm);
  77. vma = find_vma_and_prepare_anon(mm, address);
  78. if (!IS_ERR(vma)) {
  79. bool locked = vma_start_read_locked(vma);
  80. if (!locked)
  81. vma = ERR_PTR(-EAGAIN);
  82. }
  83. mmap_read_unlock(mm);
  84. return vma;
  85. }
  86. static struct vm_area_struct *uffd_mfill_lock(struct mm_struct *dst_mm,
  87. unsigned long dst_start,
  88. unsigned long len)
  89. {
  90. struct vm_area_struct *dst_vma;
  91. dst_vma = uffd_lock_vma(dst_mm, dst_start);
  92. if (IS_ERR(dst_vma) || validate_dst_vma(dst_vma, dst_start + len))
  93. return dst_vma;
  94. vma_end_read(dst_vma);
  95. return ERR_PTR(-ENOENT);
  96. }
  97. static void uffd_mfill_unlock(struct vm_area_struct *vma)
  98. {
  99. vma_end_read(vma);
  100. }
  101. #else
  102. static struct vm_area_struct *uffd_mfill_lock(struct mm_struct *dst_mm,
  103. unsigned long dst_start,
  104. unsigned long len)
  105. {
  106. struct vm_area_struct *dst_vma;
  107. mmap_read_lock(dst_mm);
  108. dst_vma = find_vma_and_prepare_anon(dst_mm, dst_start);
  109. if (IS_ERR(dst_vma))
  110. goto out_unlock;
  111. if (validate_dst_vma(dst_vma, dst_start + len))
  112. return dst_vma;
  113. dst_vma = ERR_PTR(-ENOENT);
  114. out_unlock:
  115. mmap_read_unlock(dst_mm);
  116. return dst_vma;
  117. }
  118. static void uffd_mfill_unlock(struct vm_area_struct *vma)
  119. {
  120. mmap_read_unlock(vma->vm_mm);
  121. }
  122. #endif
  123. /* Check if dst_addr is outside of file's size. Must be called with ptl held. */
  124. static bool mfill_file_over_size(struct vm_area_struct *dst_vma,
  125. unsigned long dst_addr)
  126. {
  127. struct inode *inode;
  128. pgoff_t offset, max_off;
  129. if (!dst_vma->vm_file)
  130. return false;
  131. inode = dst_vma->vm_file->f_inode;
  132. offset = linear_page_index(dst_vma, dst_addr);
  133. max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  134. return offset >= max_off;
  135. }
  136. /*
  137. * Install PTEs, to map dst_addr (within dst_vma) to page.
  138. *
  139. * This function handles both MCOPY_ATOMIC_NORMAL and _CONTINUE for both shmem
  140. * and anon, and for both shared and private VMAs.
  141. */
  142. int mfill_atomic_install_pte(pmd_t *dst_pmd,
  143. struct vm_area_struct *dst_vma,
  144. unsigned long dst_addr, struct page *page,
  145. bool newly_allocated, uffd_flags_t flags)
  146. {
  147. int ret;
  148. struct mm_struct *dst_mm = dst_vma->vm_mm;
  149. pte_t _dst_pte, *dst_pte;
  150. bool writable = dst_vma->vm_flags & VM_WRITE;
  151. bool vm_shared = dst_vma->vm_flags & VM_SHARED;
  152. spinlock_t *ptl;
  153. struct folio *folio = page_folio(page);
  154. bool page_in_cache = folio_mapping(folio);
  155. pte_t dst_ptep;
  156. _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
  157. _dst_pte = pte_mkdirty(_dst_pte);
  158. if (page_in_cache && !vm_shared)
  159. writable = false;
  160. if (writable)
  161. _dst_pte = pte_mkwrite(_dst_pte, dst_vma);
  162. if (flags & MFILL_ATOMIC_WP)
  163. _dst_pte = pte_mkuffd_wp(_dst_pte);
  164. ret = -EAGAIN;
  165. dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
  166. if (!dst_pte)
  167. goto out;
  168. if (mfill_file_over_size(dst_vma, dst_addr)) {
  169. ret = -EFAULT;
  170. goto out_unlock;
  171. }
  172. ret = -EEXIST;
  173. dst_ptep = ptep_get(dst_pte);
  174. /*
  175. * We are allowed to overwrite a UFFD pte marker: consider when both
  176. * MISSING|WP registered, we firstly wr-protect a none pte which has no
  177. * page cache page backing it, then access the page.
  178. */
  179. if (!pte_none(dst_ptep) && !pte_is_uffd_marker(dst_ptep))
  180. goto out_unlock;
  181. if (page_in_cache) {
  182. /* Usually, cache pages are already added to LRU */
  183. if (newly_allocated)
  184. folio_add_lru(folio);
  185. folio_add_file_rmap_pte(folio, page, dst_vma);
  186. } else {
  187. folio_add_new_anon_rmap(folio, dst_vma, dst_addr, RMAP_EXCLUSIVE);
  188. folio_add_lru_vma(folio, dst_vma);
  189. }
  190. /*
  191. * Must happen after rmap, as mm_counter() checks mapping (via
  192. * PageAnon()), which is set by __page_set_anon_rmap().
  193. */
  194. inc_mm_counter(dst_mm, mm_counter(folio));
  195. set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  196. /* No need to invalidate - it was non-present before */
  197. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  198. ret = 0;
  199. out_unlock:
  200. pte_unmap_unlock(dst_pte, ptl);
  201. out:
  202. return ret;
  203. }
  204. static int mfill_atomic_pte_copy(pmd_t *dst_pmd,
  205. struct vm_area_struct *dst_vma,
  206. unsigned long dst_addr,
  207. unsigned long src_addr,
  208. uffd_flags_t flags,
  209. struct folio **foliop)
  210. {
  211. void *kaddr;
  212. int ret;
  213. struct folio *folio;
  214. if (!*foliop) {
  215. ret = -ENOMEM;
  216. folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma,
  217. dst_addr);
  218. if (!folio)
  219. goto out;
  220. kaddr = kmap_local_folio(folio, 0);
  221. /*
  222. * The read mmap_lock is held here. Despite the
  223. * mmap_lock being read recursive a deadlock is still
  224. * possible if a writer has taken a lock. For example:
  225. *
  226. * process A thread 1 takes read lock on own mmap_lock
  227. * process A thread 2 calls mmap, blocks taking write lock
  228. * process B thread 1 takes page fault, read lock on own mmap lock
  229. * process B thread 2 calls mmap, blocks taking write lock
  230. * process A thread 1 blocks taking read lock on process B
  231. * process B thread 1 blocks taking read lock on process A
  232. *
  233. * Disable page faults to prevent potential deadlock
  234. * and retry the copy outside the mmap_lock.
  235. */
  236. pagefault_disable();
  237. ret = copy_from_user(kaddr, (const void __user *) src_addr,
  238. PAGE_SIZE);
  239. pagefault_enable();
  240. kunmap_local(kaddr);
  241. /* fallback to copy_from_user outside mmap_lock */
  242. if (unlikely(ret)) {
  243. ret = -ENOENT;
  244. *foliop = folio;
  245. /* don't free the page */
  246. goto out;
  247. }
  248. flush_dcache_folio(folio);
  249. } else {
  250. folio = *foliop;
  251. *foliop = NULL;
  252. }
  253. /*
  254. * The memory barrier inside __folio_mark_uptodate makes sure that
  255. * preceding stores to the page contents become visible before
  256. * the set_pte_at() write.
  257. */
  258. __folio_mark_uptodate(folio);
  259. ret = -ENOMEM;
  260. if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL))
  261. goto out_release;
  262. ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
  263. &folio->page, true, flags);
  264. if (ret)
  265. goto out_release;
  266. out:
  267. return ret;
  268. out_release:
  269. folio_put(folio);
  270. goto out;
  271. }
  272. static int mfill_atomic_pte_zeroed_folio(pmd_t *dst_pmd,
  273. struct vm_area_struct *dst_vma,
  274. unsigned long dst_addr)
  275. {
  276. struct folio *folio;
  277. int ret = -ENOMEM;
  278. folio = vma_alloc_zeroed_movable_folio(dst_vma, dst_addr);
  279. if (!folio)
  280. return ret;
  281. if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL))
  282. goto out_put;
  283. /*
  284. * The memory barrier inside __folio_mark_uptodate makes sure that
  285. * zeroing out the folio become visible before mapping the page
  286. * using set_pte_at(). See do_anonymous_page().
  287. */
  288. __folio_mark_uptodate(folio);
  289. ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
  290. &folio->page, true, 0);
  291. if (ret)
  292. goto out_put;
  293. return 0;
  294. out_put:
  295. folio_put(folio);
  296. return ret;
  297. }
  298. static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd,
  299. struct vm_area_struct *dst_vma,
  300. unsigned long dst_addr)
  301. {
  302. pte_t _dst_pte, *dst_pte;
  303. spinlock_t *ptl;
  304. int ret;
  305. if (mm_forbids_zeropage(dst_vma->vm_mm))
  306. return mfill_atomic_pte_zeroed_folio(dst_pmd, dst_vma, dst_addr);
  307. _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
  308. dst_vma->vm_page_prot));
  309. ret = -EAGAIN;
  310. dst_pte = pte_offset_map_lock(dst_vma->vm_mm, dst_pmd, dst_addr, &ptl);
  311. if (!dst_pte)
  312. goto out;
  313. if (mfill_file_over_size(dst_vma, dst_addr)) {
  314. ret = -EFAULT;
  315. goto out_unlock;
  316. }
  317. ret = -EEXIST;
  318. if (!pte_none(ptep_get(dst_pte)))
  319. goto out_unlock;
  320. set_pte_at(dst_vma->vm_mm, dst_addr, dst_pte, _dst_pte);
  321. /* No need to invalidate - it was non-present before */
  322. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  323. ret = 0;
  324. out_unlock:
  325. pte_unmap_unlock(dst_pte, ptl);
  326. out:
  327. return ret;
  328. }
  329. /* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */
  330. static int mfill_atomic_pte_continue(pmd_t *dst_pmd,
  331. struct vm_area_struct *dst_vma,
  332. unsigned long dst_addr,
  333. uffd_flags_t flags)
  334. {
  335. struct inode *inode = file_inode(dst_vma->vm_file);
  336. pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
  337. struct folio *folio;
  338. struct page *page;
  339. int ret;
  340. ret = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC);
  341. /* Our caller expects us to return -EFAULT if we failed to find folio */
  342. if (ret == -ENOENT)
  343. ret = -EFAULT;
  344. if (ret)
  345. goto out;
  346. if (!folio) {
  347. ret = -EFAULT;
  348. goto out;
  349. }
  350. page = folio_file_page(folio, pgoff);
  351. if (PageHWPoison(page)) {
  352. ret = -EIO;
  353. goto out_release;
  354. }
  355. ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr,
  356. page, false, flags);
  357. if (ret)
  358. goto out_release;
  359. folio_unlock(folio);
  360. ret = 0;
  361. out:
  362. return ret;
  363. out_release:
  364. folio_unlock(folio);
  365. folio_put(folio);
  366. goto out;
  367. }
  368. /* Handles UFFDIO_POISON for all non-hugetlb VMAs. */
  369. static int mfill_atomic_pte_poison(pmd_t *dst_pmd,
  370. struct vm_area_struct *dst_vma,
  371. unsigned long dst_addr,
  372. uffd_flags_t flags)
  373. {
  374. int ret;
  375. struct mm_struct *dst_mm = dst_vma->vm_mm;
  376. pte_t _dst_pte, *dst_pte;
  377. spinlock_t *ptl;
  378. _dst_pte = make_pte_marker(PTE_MARKER_POISONED);
  379. ret = -EAGAIN;
  380. dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
  381. if (!dst_pte)
  382. goto out;
  383. if (mfill_file_over_size(dst_vma, dst_addr)) {
  384. ret = -EFAULT;
  385. goto out_unlock;
  386. }
  387. ret = -EEXIST;
  388. /* Refuse to overwrite any PTE, even a PTE marker (e.g. UFFD WP). */
  389. if (!pte_none(ptep_get(dst_pte)))
  390. goto out_unlock;
  391. set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  392. /* No need to invalidate - it was non-present before */
  393. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  394. ret = 0;
  395. out_unlock:
  396. pte_unmap_unlock(dst_pte, ptl);
  397. out:
  398. return ret;
  399. }
  400. static pmd_t *mm_alloc_pmd(struct mm_struct *mm, unsigned long address)
  401. {
  402. pgd_t *pgd;
  403. p4d_t *p4d;
  404. pud_t *pud;
  405. pgd = pgd_offset(mm, address);
  406. p4d = p4d_alloc(mm, pgd, address);
  407. if (!p4d)
  408. return NULL;
  409. pud = pud_alloc(mm, p4d, address);
  410. if (!pud)
  411. return NULL;
  412. /*
  413. * Note that we didn't run this because the pmd was
  414. * missing, the *pmd may be already established and in
  415. * turn it may also be a trans_huge_pmd.
  416. */
  417. return pmd_alloc(mm, pud, address);
  418. }
  419. #ifdef CONFIG_HUGETLB_PAGE
  420. /*
  421. * mfill_atomic processing for HUGETLB vmas. Note that this routine is
  422. * called with either vma-lock or mmap_lock held, it will release the lock
  423. * before returning.
  424. */
  425. static __always_inline ssize_t mfill_atomic_hugetlb(
  426. struct userfaultfd_ctx *ctx,
  427. struct vm_area_struct *dst_vma,
  428. unsigned long dst_start,
  429. unsigned long src_start,
  430. unsigned long len,
  431. uffd_flags_t flags)
  432. {
  433. struct mm_struct *dst_mm = dst_vma->vm_mm;
  434. ssize_t err;
  435. pte_t *dst_pte;
  436. unsigned long src_addr, dst_addr;
  437. long copied;
  438. struct folio *folio;
  439. unsigned long vma_hpagesize;
  440. pgoff_t idx;
  441. u32 hash;
  442. struct address_space *mapping;
  443. /*
  444. * There is no default zero huge page for all huge page sizes as
  445. * supported by hugetlb. A PMD_SIZE huge pages may exist as used
  446. * by THP. Since we can not reliably insert a zero page, this
  447. * feature is not supported.
  448. */
  449. if (uffd_flags_mode_is(flags, MFILL_ATOMIC_ZEROPAGE)) {
  450. up_read(&ctx->map_changing_lock);
  451. uffd_mfill_unlock(dst_vma);
  452. return -EINVAL;
  453. }
  454. src_addr = src_start;
  455. dst_addr = dst_start;
  456. copied = 0;
  457. folio = NULL;
  458. vma_hpagesize = vma_kernel_pagesize(dst_vma);
  459. /*
  460. * Validate alignment based on huge page size
  461. */
  462. err = -EINVAL;
  463. if (dst_start & (vma_hpagesize - 1) || len & (vma_hpagesize - 1))
  464. goto out_unlock;
  465. retry:
  466. /*
  467. * On routine entry dst_vma is set. If we had to drop mmap_lock and
  468. * retry, dst_vma will be set to NULL and we must lookup again.
  469. */
  470. if (!dst_vma) {
  471. dst_vma = uffd_mfill_lock(dst_mm, dst_start, len);
  472. if (IS_ERR(dst_vma)) {
  473. err = PTR_ERR(dst_vma);
  474. goto out;
  475. }
  476. err = -ENOENT;
  477. if (!is_vm_hugetlb_page(dst_vma))
  478. goto out_unlock_vma;
  479. err = -EINVAL;
  480. if (vma_hpagesize != vma_kernel_pagesize(dst_vma))
  481. goto out_unlock_vma;
  482. /*
  483. * If memory mappings are changing because of non-cooperative
  484. * operation (e.g. mremap) running in parallel, bail out and
  485. * request the user to retry later
  486. */
  487. down_read(&ctx->map_changing_lock);
  488. err = -EAGAIN;
  489. if (atomic_read(&ctx->mmap_changing))
  490. goto out_unlock;
  491. }
  492. while (src_addr < src_start + len) {
  493. VM_WARN_ON_ONCE(dst_addr >= dst_start + len);
  494. /*
  495. * Serialize via vma_lock and hugetlb_fault_mutex.
  496. * vma_lock ensures the dst_pte remains valid even
  497. * in the case of shared pmds. fault mutex prevents
  498. * races with other faulting threads.
  499. */
  500. idx = linear_page_index(dst_vma, dst_addr);
  501. mapping = dst_vma->vm_file->f_mapping;
  502. hash = hugetlb_fault_mutex_hash(mapping, idx);
  503. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  504. hugetlb_vma_lock_read(dst_vma);
  505. err = -ENOMEM;
  506. dst_pte = huge_pte_alloc(dst_mm, dst_vma, dst_addr, vma_hpagesize);
  507. if (!dst_pte) {
  508. hugetlb_vma_unlock_read(dst_vma);
  509. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  510. goto out_unlock;
  511. }
  512. if (!uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) {
  513. const pte_t ptep = huge_ptep_get(dst_mm, dst_addr, dst_pte);
  514. if (!huge_pte_none(ptep) && !pte_is_uffd_marker(ptep)) {
  515. err = -EEXIST;
  516. hugetlb_vma_unlock_read(dst_vma);
  517. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  518. goto out_unlock;
  519. }
  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. VM_WARN_ON_ONCE(!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. VM_WARN_ON_ONCE(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. VM_WARN_ON_ONCE(copied < 0);
  558. VM_WARN_ON_ONCE(err > 0);
  559. VM_WARN_ON_ONCE(!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. VM_WARN_ON_ONCE(dst_start & ~PAGE_MASK);
  628. VM_WARN_ON_ONCE(len & ~PAGE_MASK);
  629. /* Does the address range wrap, or is the span zero-sized? */
  630. VM_WARN_ON_ONCE(src_start + len <= src_start);
  631. VM_WARN_ON_ONCE(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. VM_WARN_ON_ONCE(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) ||
  701. pmd_trans_huge(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. VM_WARN_ON_ONCE(!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. VM_WARN_ON_ONCE(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. VM_WARN_ON_ONCE(copied < 0);
  751. VM_WARN_ON_ONCE(err > 0);
  752. VM_WARN_ON_ONCE(!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. VM_WARN_ON_ONCE(start & ~PAGE_MASK);
  827. VM_WARN_ON_ONCE(len & ~PAGE_MASK);
  828. /* Does the address range wrap, or is the span zero-sized? */
  829. VM_WARN_ON_ONCE(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 inline bool is_pte_pages_stable(pte_t *dst_pte, pte_t *src_pte,
  891. pte_t orig_dst_pte, pte_t orig_src_pte,
  892. pmd_t *dst_pmd, pmd_t dst_pmdval)
  893. {
  894. return pte_same(ptep_get(src_pte), orig_src_pte) &&
  895. pte_same(ptep_get(dst_pte), orig_dst_pte) &&
  896. pmd_same(dst_pmdval, pmdp_get_lockless(dst_pmd));
  897. }
  898. /*
  899. * Checks if the two ptes and the corresponding folio are eligible for batched
  900. * move. If so, then returns pointer to the locked folio. Otherwise, returns NULL.
  901. *
  902. * NOTE: folio's reference is not required as the whole operation is within
  903. * PTL's critical section.
  904. */
  905. static struct folio *check_ptes_for_batched_move(struct vm_area_struct *src_vma,
  906. unsigned long src_addr,
  907. pte_t *src_pte, pte_t *dst_pte)
  908. {
  909. pte_t orig_dst_pte, orig_src_pte;
  910. struct folio *folio;
  911. orig_dst_pte = ptep_get(dst_pte);
  912. if (!pte_none(orig_dst_pte))
  913. return NULL;
  914. orig_src_pte = ptep_get(src_pte);
  915. if (!pte_present(orig_src_pte) || is_zero_pfn(pte_pfn(orig_src_pte)))
  916. return NULL;
  917. folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
  918. if (!folio || !folio_trylock(folio))
  919. return NULL;
  920. if (!PageAnonExclusive(&folio->page) || folio_test_large(folio)) {
  921. folio_unlock(folio);
  922. return NULL;
  923. }
  924. return folio;
  925. }
  926. /*
  927. * Moves src folios to dst in a batch as long as they are not large, and can
  928. * successfully take the lock via folio_trylock().
  929. */
  930. static long move_present_ptes(struct mm_struct *mm,
  931. struct vm_area_struct *dst_vma,
  932. struct vm_area_struct *src_vma,
  933. unsigned long dst_addr, unsigned long src_addr,
  934. pte_t *dst_pte, pte_t *src_pte,
  935. pte_t orig_dst_pte, pte_t orig_src_pte,
  936. pmd_t *dst_pmd, pmd_t dst_pmdval,
  937. spinlock_t *dst_ptl, spinlock_t *src_ptl,
  938. struct folio **first_src_folio, unsigned long len)
  939. {
  940. int err = 0;
  941. struct folio *src_folio = *first_src_folio;
  942. unsigned long src_start = src_addr;
  943. unsigned long src_end;
  944. len = pmd_addr_end(dst_addr, dst_addr + len) - dst_addr;
  945. src_end = pmd_addr_end(src_addr, src_addr + len);
  946. flush_cache_range(src_vma, src_addr, src_end);
  947. double_pt_lock(dst_ptl, src_ptl);
  948. if (!is_pte_pages_stable(dst_pte, src_pte, orig_dst_pte, orig_src_pte,
  949. dst_pmd, dst_pmdval)) {
  950. err = -EAGAIN;
  951. goto out;
  952. }
  953. if (folio_test_large(src_folio) ||
  954. folio_maybe_dma_pinned(src_folio) ||
  955. !PageAnonExclusive(&src_folio->page)) {
  956. err = -EBUSY;
  957. goto out;
  958. }
  959. /* It's safe to drop the reference now as the page-table is holding one. */
  960. folio_put(*first_src_folio);
  961. *first_src_folio = NULL;
  962. lazy_mmu_mode_enable();
  963. while (true) {
  964. orig_src_pte = ptep_get_and_clear(mm, src_addr, src_pte);
  965. /* Folio got pinned from under us. Put it back and fail the move. */
  966. if (folio_maybe_dma_pinned(src_folio)) {
  967. set_pte_at(mm, src_addr, src_pte, orig_src_pte);
  968. err = -EBUSY;
  969. break;
  970. }
  971. folio_move_anon_rmap(src_folio, dst_vma);
  972. src_folio->index = linear_page_index(dst_vma, dst_addr);
  973. orig_dst_pte = folio_mk_pte(src_folio, dst_vma->vm_page_prot);
  974. /* Set soft dirty bit so userspace can notice the pte was moved */
  975. if (pgtable_supports_soft_dirty())
  976. orig_dst_pte = pte_mksoft_dirty(orig_dst_pte);
  977. if (pte_dirty(orig_src_pte))
  978. orig_dst_pte = pte_mkdirty(orig_dst_pte);
  979. orig_dst_pte = pte_mkwrite(orig_dst_pte, dst_vma);
  980. set_pte_at(mm, dst_addr, dst_pte, orig_dst_pte);
  981. src_addr += PAGE_SIZE;
  982. if (src_addr == src_end)
  983. break;
  984. dst_addr += PAGE_SIZE;
  985. dst_pte++;
  986. src_pte++;
  987. folio_unlock(src_folio);
  988. src_folio = check_ptes_for_batched_move(src_vma, src_addr,
  989. src_pte, dst_pte);
  990. if (!src_folio)
  991. break;
  992. }
  993. lazy_mmu_mode_disable();
  994. if (src_addr > src_start)
  995. flush_tlb_range(src_vma, src_start, src_addr);
  996. if (src_folio)
  997. folio_unlock(src_folio);
  998. out:
  999. double_pt_unlock(dst_ptl, src_ptl);
  1000. return src_addr > src_start ? src_addr - src_start : err;
  1001. }
  1002. static int move_swap_pte(struct mm_struct *mm, struct vm_area_struct *dst_vma,
  1003. unsigned long dst_addr, unsigned long src_addr,
  1004. pte_t *dst_pte, pte_t *src_pte,
  1005. pte_t orig_dst_pte, pte_t orig_src_pte,
  1006. pmd_t *dst_pmd, pmd_t dst_pmdval,
  1007. spinlock_t *dst_ptl, spinlock_t *src_ptl,
  1008. struct folio *src_folio,
  1009. struct swap_info_struct *si, swp_entry_t entry)
  1010. {
  1011. /*
  1012. * Check if the folio still belongs to the target swap entry after
  1013. * acquiring the lock. Folio can be freed in the swap cache while
  1014. * not locked.
  1015. */
  1016. if (src_folio && unlikely(!folio_test_swapcache(src_folio) ||
  1017. entry.val != src_folio->swap.val))
  1018. return -EAGAIN;
  1019. double_pt_lock(dst_ptl, src_ptl);
  1020. if (!is_pte_pages_stable(dst_pte, src_pte, orig_dst_pte, orig_src_pte,
  1021. dst_pmd, dst_pmdval)) {
  1022. double_pt_unlock(dst_ptl, src_ptl);
  1023. return -EAGAIN;
  1024. }
  1025. /*
  1026. * The src_folio resides in the swapcache, requiring an update to its
  1027. * index and mapping to align with the dst_vma, where a swap-in may
  1028. * occur and hit the swapcache after moving the PTE.
  1029. */
  1030. if (src_folio) {
  1031. folio_move_anon_rmap(src_folio, dst_vma);
  1032. src_folio->index = linear_page_index(dst_vma, dst_addr);
  1033. } else {
  1034. /*
  1035. * Check if the swap entry is cached after acquiring the src_pte
  1036. * lock. Otherwise, we might miss a newly loaded swap cache folio.
  1037. *
  1038. * We are trying to catch newly added swap cache, the only possible case is
  1039. * when a folio is swapped in and out again staying in swap cache, using the
  1040. * same entry before the PTE check above. The PTL is acquired and released
  1041. * twice, each time after updating the swap table. So holding
  1042. * the PTL here ensures we see the updated value.
  1043. */
  1044. if (swap_cache_has_folio(entry)) {
  1045. double_pt_unlock(dst_ptl, src_ptl);
  1046. return -EAGAIN;
  1047. }
  1048. }
  1049. orig_src_pte = ptep_get_and_clear(mm, src_addr, src_pte);
  1050. if (pgtable_supports_soft_dirty())
  1051. orig_src_pte = pte_swp_mksoft_dirty(orig_src_pte);
  1052. set_pte_at(mm, dst_addr, dst_pte, orig_src_pte);
  1053. double_pt_unlock(dst_ptl, src_ptl);
  1054. return PAGE_SIZE;
  1055. }
  1056. static int move_zeropage_pte(struct mm_struct *mm,
  1057. struct vm_area_struct *dst_vma,
  1058. struct vm_area_struct *src_vma,
  1059. unsigned long dst_addr, unsigned long src_addr,
  1060. pte_t *dst_pte, pte_t *src_pte,
  1061. pte_t orig_dst_pte, pte_t orig_src_pte,
  1062. pmd_t *dst_pmd, pmd_t dst_pmdval,
  1063. spinlock_t *dst_ptl, spinlock_t *src_ptl)
  1064. {
  1065. pte_t zero_pte;
  1066. double_pt_lock(dst_ptl, src_ptl);
  1067. if (!is_pte_pages_stable(dst_pte, src_pte, orig_dst_pte, orig_src_pte,
  1068. dst_pmd, dst_pmdval)) {
  1069. double_pt_unlock(dst_ptl, src_ptl);
  1070. return -EAGAIN;
  1071. }
  1072. zero_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
  1073. dst_vma->vm_page_prot));
  1074. ptep_clear_flush(src_vma, src_addr, src_pte);
  1075. set_pte_at(mm, dst_addr, dst_pte, zero_pte);
  1076. double_pt_unlock(dst_ptl, src_ptl);
  1077. return PAGE_SIZE;
  1078. }
  1079. /*
  1080. * The mmap_lock for reading is held by the caller. Just move the page(s)
  1081. * from src_pmd to dst_pmd if possible, and return number of bytes moved.
  1082. * On failure, an error code is returned.
  1083. */
  1084. static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
  1085. struct vm_area_struct *dst_vma,
  1086. struct vm_area_struct *src_vma,
  1087. unsigned long dst_addr, unsigned long src_addr,
  1088. unsigned long len, __u64 mode)
  1089. {
  1090. struct swap_info_struct *si = NULL;
  1091. pte_t orig_src_pte, orig_dst_pte;
  1092. pte_t src_folio_pte;
  1093. spinlock_t *src_ptl, *dst_ptl;
  1094. pte_t *src_pte = NULL;
  1095. pte_t *dst_pte = NULL;
  1096. pmd_t dummy_pmdval;
  1097. pmd_t dst_pmdval;
  1098. struct folio *src_folio = NULL;
  1099. struct mmu_notifier_range range;
  1100. long ret = 0;
  1101. mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
  1102. src_addr, src_addr + len);
  1103. mmu_notifier_invalidate_range_start(&range);
  1104. retry:
  1105. /*
  1106. * Use the maywrite version to indicate that dst_pte will be modified,
  1107. * since dst_pte needs to be none, the subsequent pte_same() check
  1108. * cannot prevent the dst_pte page from being freed concurrently, so we
  1109. * also need to obtain dst_pmdval and recheck pmd_same() later.
  1110. */
  1111. dst_pte = pte_offset_map_rw_nolock(mm, dst_pmd, dst_addr, &dst_pmdval,
  1112. &dst_ptl);
  1113. /* Retry if a huge pmd materialized from under us */
  1114. if (unlikely(!dst_pte)) {
  1115. ret = -EAGAIN;
  1116. goto out;
  1117. }
  1118. /*
  1119. * Unlike dst_pte, the subsequent pte_same() check can ensure the
  1120. * stability of the src_pte page, so there is no need to get pmdval,
  1121. * just pass a dummy variable to it.
  1122. */
  1123. src_pte = pte_offset_map_rw_nolock(mm, src_pmd, src_addr, &dummy_pmdval,
  1124. &src_ptl);
  1125. /*
  1126. * We held the mmap_lock for reading so MADV_DONTNEED
  1127. * can zap transparent huge pages under us, or the
  1128. * transparent huge page fault can establish new
  1129. * transparent huge pages under us.
  1130. */
  1131. if (unlikely(!src_pte)) {
  1132. ret = -EAGAIN;
  1133. goto out;
  1134. }
  1135. /* Sanity checks before the operation */
  1136. if (pmd_none(*dst_pmd) || pmd_none(*src_pmd) ||
  1137. pmd_trans_huge(*dst_pmd) || pmd_trans_huge(*src_pmd)) {
  1138. ret = -EINVAL;
  1139. goto out;
  1140. }
  1141. spin_lock(dst_ptl);
  1142. orig_dst_pte = ptep_get(dst_pte);
  1143. spin_unlock(dst_ptl);
  1144. if (!pte_none(orig_dst_pte)) {
  1145. ret = -EEXIST;
  1146. goto out;
  1147. }
  1148. spin_lock(src_ptl);
  1149. orig_src_pte = ptep_get(src_pte);
  1150. spin_unlock(src_ptl);
  1151. if (pte_none(orig_src_pte)) {
  1152. if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES))
  1153. ret = -ENOENT;
  1154. else /* nothing to do to move a hole */
  1155. ret = PAGE_SIZE;
  1156. goto out;
  1157. }
  1158. /* If PTE changed after we locked the folio then start over */
  1159. if (src_folio && unlikely(!pte_same(src_folio_pte, orig_src_pte))) {
  1160. ret = -EAGAIN;
  1161. goto out;
  1162. }
  1163. if (pte_present(orig_src_pte)) {
  1164. if (is_zero_pfn(pte_pfn(orig_src_pte))) {
  1165. ret = move_zeropage_pte(mm, dst_vma, src_vma,
  1166. dst_addr, src_addr, dst_pte, src_pte,
  1167. orig_dst_pte, orig_src_pte,
  1168. dst_pmd, dst_pmdval, dst_ptl, src_ptl);
  1169. goto out;
  1170. }
  1171. /*
  1172. * Pin and lock source folio. Since we are in RCU read section,
  1173. * we can't block, so on contention have to unmap the ptes,
  1174. * obtain the lock and retry.
  1175. */
  1176. if (!src_folio) {
  1177. struct folio *folio;
  1178. bool locked;
  1179. /*
  1180. * Pin the page while holding the lock to be sure the
  1181. * page isn't freed under us
  1182. */
  1183. spin_lock(src_ptl);
  1184. if (!pte_same(orig_src_pte, ptep_get(src_pte))) {
  1185. spin_unlock(src_ptl);
  1186. ret = -EAGAIN;
  1187. goto out;
  1188. }
  1189. folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
  1190. if (!folio || !PageAnonExclusive(&folio->page)) {
  1191. spin_unlock(src_ptl);
  1192. ret = -EBUSY;
  1193. goto out;
  1194. }
  1195. locked = folio_trylock(folio);
  1196. /*
  1197. * We avoid waiting for folio lock with a raised
  1198. * refcount for large folios because extra refcounts
  1199. * will result in split_folio() failing later and
  1200. * retrying. If multiple tasks are trying to move a
  1201. * large folio we can end up livelocking.
  1202. */
  1203. if (!locked && folio_test_large(folio)) {
  1204. spin_unlock(src_ptl);
  1205. ret = -EAGAIN;
  1206. goto out;
  1207. }
  1208. folio_get(folio);
  1209. src_folio = folio;
  1210. src_folio_pte = orig_src_pte;
  1211. spin_unlock(src_ptl);
  1212. if (!locked) {
  1213. pte_unmap(src_pte);
  1214. pte_unmap(dst_pte);
  1215. src_pte = dst_pte = NULL;
  1216. /* now we can block and wait */
  1217. folio_lock(src_folio);
  1218. goto retry;
  1219. }
  1220. if (WARN_ON_ONCE(!folio_test_anon(src_folio))) {
  1221. ret = -EBUSY;
  1222. goto out;
  1223. }
  1224. }
  1225. /* at this point we have src_folio locked */
  1226. if (folio_test_large(src_folio)) {
  1227. /* split_folio() can block */
  1228. pte_unmap(src_pte);
  1229. pte_unmap(dst_pte);
  1230. src_pte = dst_pte = NULL;
  1231. ret = split_folio(src_folio);
  1232. if (ret)
  1233. goto out;
  1234. /* have to reacquire the folio after it got split */
  1235. folio_unlock(src_folio);
  1236. folio_put(src_folio);
  1237. src_folio = NULL;
  1238. goto retry;
  1239. }
  1240. ret = move_present_ptes(mm, dst_vma, src_vma,
  1241. dst_addr, src_addr, dst_pte, src_pte,
  1242. orig_dst_pte, orig_src_pte, dst_pmd,
  1243. dst_pmdval, dst_ptl, src_ptl, &src_folio,
  1244. len);
  1245. } else { /* !pte_present() */
  1246. struct folio *folio = NULL;
  1247. const softleaf_t entry = softleaf_from_pte(orig_src_pte);
  1248. if (softleaf_is_migration(entry)) {
  1249. pte_unmap(src_pte);
  1250. pte_unmap(dst_pte);
  1251. src_pte = dst_pte = NULL;
  1252. migration_entry_wait(mm, src_pmd, src_addr);
  1253. ret = -EAGAIN;
  1254. goto out;
  1255. } else if (!softleaf_is_swap(entry)) {
  1256. ret = -EFAULT;
  1257. goto out;
  1258. }
  1259. if (!pte_swp_exclusive(orig_src_pte)) {
  1260. ret = -EBUSY;
  1261. goto out;
  1262. }
  1263. si = get_swap_device(entry);
  1264. if (unlikely(!si)) {
  1265. ret = -EAGAIN;
  1266. goto out;
  1267. }
  1268. /*
  1269. * Verify the existence of the swapcache. If present, the folio's
  1270. * index and mapping must be updated even when the PTE is a swap
  1271. * entry. The anon_vma lock is not taken during this process since
  1272. * the folio has already been unmapped, and the swap entry is
  1273. * exclusive, preventing rmap walks.
  1274. *
  1275. * For large folios, return -EBUSY immediately, as split_folio()
  1276. * also returns -EBUSY when attempting to split unmapped large
  1277. * folios in the swapcache. This issue needs to be resolved
  1278. * separately to allow proper handling.
  1279. */
  1280. if (!src_folio)
  1281. folio = swap_cache_get_folio(entry);
  1282. if (folio) {
  1283. if (folio_test_large(folio)) {
  1284. ret = -EBUSY;
  1285. folio_put(folio);
  1286. goto out;
  1287. }
  1288. src_folio = folio;
  1289. src_folio_pte = orig_src_pte;
  1290. if (!folio_trylock(src_folio)) {
  1291. pte_unmap(src_pte);
  1292. pte_unmap(dst_pte);
  1293. src_pte = dst_pte = NULL;
  1294. put_swap_device(si);
  1295. si = NULL;
  1296. /* now we can block and wait */
  1297. folio_lock(src_folio);
  1298. goto retry;
  1299. }
  1300. }
  1301. ret = move_swap_pte(mm, dst_vma, dst_addr, src_addr, dst_pte, src_pte,
  1302. orig_dst_pte, orig_src_pte, dst_pmd, dst_pmdval,
  1303. dst_ptl, src_ptl, src_folio, si, entry);
  1304. }
  1305. out:
  1306. if (src_folio) {
  1307. folio_unlock(src_folio);
  1308. folio_put(src_folio);
  1309. }
  1310. /*
  1311. * Unmap in reverse order (LIFO) to maintain proper kmap_local
  1312. * index ordering when CONFIG_HIGHPTE is enabled. We mapped dst_pte
  1313. * first, then src_pte, so we must unmap src_pte first, then dst_pte.
  1314. */
  1315. if (src_pte)
  1316. pte_unmap(src_pte);
  1317. if (dst_pte)
  1318. pte_unmap(dst_pte);
  1319. mmu_notifier_invalidate_range_end(&range);
  1320. if (si)
  1321. put_swap_device(si);
  1322. return ret;
  1323. }
  1324. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1325. static inline bool move_splits_huge_pmd(unsigned long dst_addr,
  1326. unsigned long src_addr,
  1327. unsigned long src_end)
  1328. {
  1329. return (src_addr & ~HPAGE_PMD_MASK) || (dst_addr & ~HPAGE_PMD_MASK) ||
  1330. src_end - src_addr < HPAGE_PMD_SIZE;
  1331. }
  1332. #else
  1333. static inline bool move_splits_huge_pmd(unsigned long dst_addr,
  1334. unsigned long src_addr,
  1335. unsigned long src_end)
  1336. {
  1337. /* This is unreachable anyway, just to avoid warnings when HPAGE_PMD_SIZE==0 */
  1338. return false;
  1339. }
  1340. #endif
  1341. static inline bool vma_move_compatible(struct vm_area_struct *vma)
  1342. {
  1343. return !(vma->vm_flags & (VM_PFNMAP | VM_IO | VM_HUGETLB |
  1344. VM_MIXEDMAP | VM_SHADOW_STACK));
  1345. }
  1346. static int validate_move_areas(struct userfaultfd_ctx *ctx,
  1347. struct vm_area_struct *src_vma,
  1348. struct vm_area_struct *dst_vma)
  1349. {
  1350. /* Only allow moving if both have the same access and protection */
  1351. if ((src_vma->vm_flags & VM_ACCESS_FLAGS) != (dst_vma->vm_flags & VM_ACCESS_FLAGS) ||
  1352. pgprot_val(src_vma->vm_page_prot) != pgprot_val(dst_vma->vm_page_prot))
  1353. return -EINVAL;
  1354. /* Only allow moving if both are mlocked or both aren't */
  1355. if ((src_vma->vm_flags & VM_LOCKED) != (dst_vma->vm_flags & VM_LOCKED))
  1356. return -EINVAL;
  1357. /*
  1358. * For now, we keep it simple and only move between writable VMAs.
  1359. * Access flags are equal, therefore checking only the source is enough.
  1360. */
  1361. if (!(src_vma->vm_flags & VM_WRITE))
  1362. return -EINVAL;
  1363. /* Check if vma flags indicate content which can be moved */
  1364. if (!vma_move_compatible(src_vma) || !vma_move_compatible(dst_vma))
  1365. return -EINVAL;
  1366. /* Ensure dst_vma is registered in uffd we are operating on */
  1367. if (!dst_vma->vm_userfaultfd_ctx.ctx ||
  1368. dst_vma->vm_userfaultfd_ctx.ctx != ctx)
  1369. return -EINVAL;
  1370. /* Only allow moving across anonymous vmas */
  1371. if (!vma_is_anonymous(src_vma) || !vma_is_anonymous(dst_vma))
  1372. return -EINVAL;
  1373. return 0;
  1374. }
  1375. static __always_inline
  1376. int find_vmas_mm_locked(struct mm_struct *mm,
  1377. unsigned long dst_start,
  1378. unsigned long src_start,
  1379. struct vm_area_struct **dst_vmap,
  1380. struct vm_area_struct **src_vmap)
  1381. {
  1382. struct vm_area_struct *vma;
  1383. mmap_assert_locked(mm);
  1384. vma = find_vma_and_prepare_anon(mm, dst_start);
  1385. if (IS_ERR(vma))
  1386. return PTR_ERR(vma);
  1387. *dst_vmap = vma;
  1388. /* Skip finding src_vma if src_start is in dst_vma */
  1389. if (src_start >= vma->vm_start && src_start < vma->vm_end)
  1390. goto out_success;
  1391. vma = vma_lookup(mm, src_start);
  1392. if (!vma)
  1393. return -ENOENT;
  1394. out_success:
  1395. *src_vmap = vma;
  1396. return 0;
  1397. }
  1398. #ifdef CONFIG_PER_VMA_LOCK
  1399. static int uffd_move_lock(struct mm_struct *mm,
  1400. unsigned long dst_start,
  1401. unsigned long src_start,
  1402. struct vm_area_struct **dst_vmap,
  1403. struct vm_area_struct **src_vmap)
  1404. {
  1405. struct vm_area_struct *vma;
  1406. int err;
  1407. vma = uffd_lock_vma(mm, dst_start);
  1408. if (IS_ERR(vma))
  1409. return PTR_ERR(vma);
  1410. *dst_vmap = vma;
  1411. /*
  1412. * Skip finding src_vma if src_start is in dst_vma. This also ensures
  1413. * that we don't lock the same vma twice.
  1414. */
  1415. if (src_start >= vma->vm_start && src_start < vma->vm_end) {
  1416. *src_vmap = vma;
  1417. return 0;
  1418. }
  1419. /*
  1420. * Using uffd_lock_vma() to get src_vma can lead to following deadlock:
  1421. *
  1422. * Thread1 Thread2
  1423. * ------- -------
  1424. * vma_start_read(dst_vma)
  1425. * mmap_write_lock(mm)
  1426. * vma_start_write(src_vma)
  1427. * vma_start_read(src_vma)
  1428. * mmap_read_lock(mm)
  1429. * vma_start_write(dst_vma)
  1430. */
  1431. *src_vmap = lock_vma_under_rcu(mm, src_start);
  1432. if (likely(*src_vmap))
  1433. return 0;
  1434. /* Undo any locking and retry in mmap_lock critical section */
  1435. vma_end_read(*dst_vmap);
  1436. mmap_read_lock(mm);
  1437. err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap);
  1438. if (err)
  1439. goto out;
  1440. if (!vma_start_read_locked(*dst_vmap)) {
  1441. err = -EAGAIN;
  1442. goto out;
  1443. }
  1444. /* Nothing further to do if both vmas are locked. */
  1445. if (*dst_vmap == *src_vmap)
  1446. goto out;
  1447. if (!vma_start_read_locked_nested(*src_vmap, SINGLE_DEPTH_NESTING)) {
  1448. /* Undo dst_vmap locking if src_vmap failed to lock */
  1449. vma_end_read(*dst_vmap);
  1450. err = -EAGAIN;
  1451. }
  1452. out:
  1453. mmap_read_unlock(mm);
  1454. return err;
  1455. }
  1456. static void uffd_move_unlock(struct vm_area_struct *dst_vma,
  1457. struct vm_area_struct *src_vma)
  1458. {
  1459. vma_end_read(src_vma);
  1460. if (src_vma != dst_vma)
  1461. vma_end_read(dst_vma);
  1462. }
  1463. #else
  1464. static int uffd_move_lock(struct mm_struct *mm,
  1465. unsigned long dst_start,
  1466. unsigned long src_start,
  1467. struct vm_area_struct **dst_vmap,
  1468. struct vm_area_struct **src_vmap)
  1469. {
  1470. int err;
  1471. mmap_read_lock(mm);
  1472. err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap);
  1473. if (err)
  1474. mmap_read_unlock(mm);
  1475. return err;
  1476. }
  1477. static void uffd_move_unlock(struct vm_area_struct *dst_vma,
  1478. struct vm_area_struct *src_vma)
  1479. {
  1480. mmap_assert_locked(src_vma->vm_mm);
  1481. mmap_read_unlock(dst_vma->vm_mm);
  1482. }
  1483. #endif
  1484. /**
  1485. * move_pages - move arbitrary anonymous pages of an existing vma
  1486. * @ctx: pointer to the userfaultfd context
  1487. * @dst_start: start of the destination virtual memory range
  1488. * @src_start: start of the source virtual memory range
  1489. * @len: length of the virtual memory range
  1490. * @mode: flags from uffdio_move.mode
  1491. *
  1492. * It will either use the mmap_lock in read mode or per-vma locks
  1493. *
  1494. * move_pages() remaps arbitrary anonymous pages atomically in zero
  1495. * copy. It only works on non shared anonymous pages because those can
  1496. * be relocated without generating non linear anon_vmas in the rmap
  1497. * code.
  1498. *
  1499. * It provides a zero copy mechanism to handle userspace page faults.
  1500. * The source vma pages should have mapcount == 1, which can be
  1501. * enforced by using madvise(MADV_DONTFORK) on src vma.
  1502. *
  1503. * The thread receiving the page during the userland page fault
  1504. * will receive the faulting page in the source vma through the network,
  1505. * storage or any other I/O device (MADV_DONTFORK in the source vma
  1506. * avoids move_pages() to fail with -EBUSY if the process forks before
  1507. * move_pages() is called), then it will call move_pages() to map the
  1508. * page in the faulting address in the destination vma.
  1509. *
  1510. * This userfaultfd command works purely via pagetables, so it's the
  1511. * most efficient way to move physical non shared anonymous pages
  1512. * across different virtual addresses. Unlike mremap()/mmap()/munmap()
  1513. * it does not create any new vmas. The mapping in the destination
  1514. * address is atomic.
  1515. *
  1516. * It only works if the vma protection bits are identical from the
  1517. * source and destination vma.
  1518. *
  1519. * It can remap non shared anonymous pages within the same vma too.
  1520. *
  1521. * If the source virtual memory range has any unmapped holes, or if
  1522. * the destination virtual memory range is not a whole unmapped hole,
  1523. * move_pages() will fail respectively with -ENOENT or -EEXIST. This
  1524. * provides a very strict behavior to avoid any chance of memory
  1525. * corruption going unnoticed if there are userland race conditions.
  1526. * Only one thread should resolve the userland page fault at any given
  1527. * time for any given faulting address. This means that if two threads
  1528. * try to both call move_pages() on the same destination address at the
  1529. * same time, the second thread will get an explicit error from this
  1530. * command.
  1531. *
  1532. * The command retval will return "len" is successful. The command
  1533. * however can be interrupted by fatal signals or errors. If
  1534. * interrupted it will return the number of bytes successfully
  1535. * remapped before the interruption if any, or the negative error if
  1536. * none. It will never return zero. Either it will return an error or
  1537. * an amount of bytes successfully moved. If the retval reports a
  1538. * "short" remap, the move_pages() command should be repeated by
  1539. * userland with src+retval, dst+reval, len-retval if it wants to know
  1540. * about the error that interrupted it.
  1541. *
  1542. * The UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES flag can be specified to
  1543. * prevent -ENOENT errors to materialize if there are holes in the
  1544. * source virtual range that is being remapped. The holes will be
  1545. * accounted as successfully remapped in the retval of the
  1546. * command. This is mostly useful to remap hugepage naturally aligned
  1547. * virtual regions without knowing if there are transparent hugepage
  1548. * in the regions or not, but preventing the risk of having to split
  1549. * the hugepmd during the remap.
  1550. */
  1551. ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
  1552. unsigned long src_start, unsigned long len, __u64 mode)
  1553. {
  1554. struct mm_struct *mm = ctx->mm;
  1555. struct vm_area_struct *src_vma, *dst_vma;
  1556. unsigned long src_addr, dst_addr, src_end;
  1557. pmd_t *src_pmd, *dst_pmd;
  1558. long err = -EINVAL;
  1559. ssize_t moved = 0;
  1560. /* Sanitize the command parameters. */
  1561. VM_WARN_ON_ONCE(src_start & ~PAGE_MASK);
  1562. VM_WARN_ON_ONCE(dst_start & ~PAGE_MASK);
  1563. VM_WARN_ON_ONCE(len & ~PAGE_MASK);
  1564. /* Does the address range wrap, or is the span zero-sized? */
  1565. VM_WARN_ON_ONCE(src_start + len < src_start);
  1566. VM_WARN_ON_ONCE(dst_start + len < dst_start);
  1567. err = uffd_move_lock(mm, dst_start, src_start, &dst_vma, &src_vma);
  1568. if (err)
  1569. goto out;
  1570. /* Re-check after taking map_changing_lock */
  1571. err = -EAGAIN;
  1572. down_read(&ctx->map_changing_lock);
  1573. if (likely(atomic_read(&ctx->mmap_changing)))
  1574. goto out_unlock;
  1575. /*
  1576. * Make sure the vma is not shared, that the src and dst remap
  1577. * ranges are both valid and fully within a single existing
  1578. * vma.
  1579. */
  1580. err = -EINVAL;
  1581. if (src_vma->vm_flags & VM_SHARED)
  1582. goto out_unlock;
  1583. if (src_start + len > src_vma->vm_end)
  1584. goto out_unlock;
  1585. if (dst_vma->vm_flags & VM_SHARED)
  1586. goto out_unlock;
  1587. if (dst_start + len > dst_vma->vm_end)
  1588. goto out_unlock;
  1589. err = validate_move_areas(ctx, src_vma, dst_vma);
  1590. if (err)
  1591. goto out_unlock;
  1592. for (src_addr = src_start, dst_addr = dst_start, src_end = src_start + len;
  1593. src_addr < src_end;) {
  1594. spinlock_t *ptl;
  1595. pmd_t dst_pmdval;
  1596. unsigned long step_size;
  1597. /*
  1598. * Below works because anonymous area would not have a
  1599. * transparent huge PUD. If file-backed support is added,
  1600. * that case would need to be handled here.
  1601. */
  1602. src_pmd = mm_find_pmd(mm, src_addr);
  1603. if (unlikely(!src_pmd)) {
  1604. if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) {
  1605. err = -ENOENT;
  1606. break;
  1607. }
  1608. src_pmd = mm_alloc_pmd(mm, src_addr);
  1609. if (unlikely(!src_pmd)) {
  1610. err = -ENOMEM;
  1611. break;
  1612. }
  1613. }
  1614. dst_pmd = mm_alloc_pmd(mm, dst_addr);
  1615. if (unlikely(!dst_pmd)) {
  1616. err = -ENOMEM;
  1617. break;
  1618. }
  1619. dst_pmdval = pmdp_get_lockless(dst_pmd);
  1620. /*
  1621. * If the dst_pmd is mapped as THP don't override it and just
  1622. * be strict. If dst_pmd changes into TPH after this check, the
  1623. * move_pages_huge_pmd() will detect the change and retry
  1624. * while move_pages_pte() will detect the change and fail.
  1625. */
  1626. if (unlikely(pmd_trans_huge(dst_pmdval))) {
  1627. err = -EEXIST;
  1628. break;
  1629. }
  1630. ptl = pmd_trans_huge_lock(src_pmd, src_vma);
  1631. if (ptl) {
  1632. /* Check if we can move the pmd without splitting it. */
  1633. if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
  1634. !pmd_none(dst_pmdval)) {
  1635. /* Can be a migration entry */
  1636. if (pmd_present(*src_pmd)) {
  1637. struct folio *folio = pmd_folio(*src_pmd);
  1638. if (!is_huge_zero_folio(folio) &&
  1639. !PageAnonExclusive(&folio->page)) {
  1640. spin_unlock(ptl);
  1641. err = -EBUSY;
  1642. break;
  1643. }
  1644. }
  1645. spin_unlock(ptl);
  1646. split_huge_pmd(src_vma, src_pmd, src_addr);
  1647. /* The folio will be split by move_pages_pte() */
  1648. continue;
  1649. }
  1650. err = move_pages_huge_pmd(mm, dst_pmd, src_pmd,
  1651. dst_pmdval, dst_vma, src_vma,
  1652. dst_addr, src_addr);
  1653. step_size = HPAGE_PMD_SIZE;
  1654. } else {
  1655. long ret;
  1656. if (pmd_none(*src_pmd)) {
  1657. if (!(mode & UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES)) {
  1658. err = -ENOENT;
  1659. break;
  1660. }
  1661. if (unlikely(__pte_alloc(mm, src_pmd))) {
  1662. err = -ENOMEM;
  1663. break;
  1664. }
  1665. }
  1666. if (unlikely(pte_alloc(mm, dst_pmd))) {
  1667. err = -ENOMEM;
  1668. break;
  1669. }
  1670. ret = move_pages_ptes(mm, dst_pmd, src_pmd,
  1671. dst_vma, src_vma, dst_addr,
  1672. src_addr, src_end - src_addr, mode);
  1673. if (ret < 0)
  1674. err = ret;
  1675. else
  1676. step_size = ret;
  1677. }
  1678. cond_resched();
  1679. if (fatal_signal_pending(current)) {
  1680. /* Do not override an error */
  1681. if (!err || err == -EAGAIN)
  1682. err = -EINTR;
  1683. break;
  1684. }
  1685. if (err) {
  1686. if (err == -EAGAIN)
  1687. continue;
  1688. break;
  1689. }
  1690. /* Proceed to the next page */
  1691. dst_addr += step_size;
  1692. src_addr += step_size;
  1693. moved += step_size;
  1694. }
  1695. out_unlock:
  1696. up_read(&ctx->map_changing_lock);
  1697. uffd_move_unlock(dst_vma, src_vma);
  1698. out:
  1699. VM_WARN_ON_ONCE(moved < 0);
  1700. VM_WARN_ON_ONCE(err > 0);
  1701. VM_WARN_ON_ONCE(!moved && !err);
  1702. return moved ? moved : err;
  1703. }
  1704. static void userfaultfd_set_vm_flags(struct vm_area_struct *vma,
  1705. vm_flags_t vm_flags)
  1706. {
  1707. const bool uffd_wp_changed = (vma->vm_flags ^ vm_flags) & VM_UFFD_WP;
  1708. vm_flags_reset(vma, vm_flags);
  1709. /*
  1710. * For shared mappings, we want to enable writenotify while
  1711. * userfaultfd-wp is enabled (see vma_wants_writenotify()). We'll simply
  1712. * recalculate vma->vm_page_prot whenever userfaultfd-wp changes.
  1713. */
  1714. if ((vma->vm_flags & VM_SHARED) && uffd_wp_changed)
  1715. vma_set_page_prot(vma);
  1716. }
  1717. static void userfaultfd_set_ctx(struct vm_area_struct *vma,
  1718. struct userfaultfd_ctx *ctx,
  1719. vm_flags_t vm_flags)
  1720. {
  1721. vma_start_write(vma);
  1722. vma->vm_userfaultfd_ctx = (struct vm_userfaultfd_ctx){ctx};
  1723. userfaultfd_set_vm_flags(vma,
  1724. (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags);
  1725. }
  1726. void userfaultfd_reset_ctx(struct vm_area_struct *vma)
  1727. {
  1728. userfaultfd_set_ctx(vma, NULL, 0);
  1729. }
  1730. struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
  1731. struct vm_area_struct *prev,
  1732. struct vm_area_struct *vma,
  1733. unsigned long start,
  1734. unsigned long end)
  1735. {
  1736. struct vm_area_struct *ret;
  1737. bool give_up_on_oom = false;
  1738. /*
  1739. * If we are modifying only and not splitting, just give up on the merge
  1740. * if OOM prevents us from merging successfully.
  1741. */
  1742. if (start == vma->vm_start && end == vma->vm_end)
  1743. give_up_on_oom = true;
  1744. /* Reset ptes for the whole vma range if wr-protected */
  1745. if (userfaultfd_wp(vma))
  1746. uffd_wp_range(vma, start, end - start, false);
  1747. ret = vma_modify_flags_uffd(vmi, prev, vma, start, end,
  1748. vma->vm_flags & ~__VM_UFFD_FLAGS,
  1749. NULL_VM_UFFD_CTX, give_up_on_oom);
  1750. /*
  1751. * In the vma_merge() successful mprotect-like case 8:
  1752. * the next vma was merged into the current one and
  1753. * the current one has not been updated yet.
  1754. */
  1755. if (!IS_ERR(ret))
  1756. userfaultfd_reset_ctx(ret);
  1757. return ret;
  1758. }
  1759. /* Assumes mmap write lock taken, and mm_struct pinned. */
  1760. int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
  1761. struct vm_area_struct *vma,
  1762. vm_flags_t vm_flags,
  1763. unsigned long start, unsigned long end,
  1764. bool wp_async)
  1765. {
  1766. VMA_ITERATOR(vmi, ctx->mm, start);
  1767. struct vm_area_struct *prev = vma_prev(&vmi);
  1768. unsigned long vma_end;
  1769. vm_flags_t new_flags;
  1770. if (vma->vm_start < start)
  1771. prev = vma;
  1772. for_each_vma_range(vmi, vma, end) {
  1773. cond_resched();
  1774. VM_WARN_ON_ONCE(!vma_can_userfault(vma, vm_flags, wp_async));
  1775. VM_WARN_ON_ONCE(vma->vm_userfaultfd_ctx.ctx &&
  1776. vma->vm_userfaultfd_ctx.ctx != ctx);
  1777. VM_WARN_ON_ONCE(!(vma->vm_flags & VM_MAYWRITE));
  1778. /*
  1779. * Nothing to do: this vma is already registered into this
  1780. * userfaultfd and with the right tracking mode too.
  1781. */
  1782. if (vma->vm_userfaultfd_ctx.ctx == ctx &&
  1783. (vma->vm_flags & vm_flags) == vm_flags)
  1784. goto skip;
  1785. if (vma->vm_start > start)
  1786. start = vma->vm_start;
  1787. vma_end = min(end, vma->vm_end);
  1788. new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags;
  1789. vma = vma_modify_flags_uffd(&vmi, prev, vma, start, vma_end,
  1790. new_flags,
  1791. (struct vm_userfaultfd_ctx){ctx},
  1792. /* give_up_on_oom = */false);
  1793. if (IS_ERR(vma))
  1794. return PTR_ERR(vma);
  1795. /*
  1796. * In the vma_merge() successful mprotect-like case 8:
  1797. * the next vma was merged into the current one and
  1798. * the current one has not been updated yet.
  1799. */
  1800. userfaultfd_set_ctx(vma, ctx, vm_flags);
  1801. if (is_vm_hugetlb_page(vma) && uffd_disable_huge_pmd_share(vma))
  1802. hugetlb_unshare_all_pmds(vma);
  1803. skip:
  1804. prev = vma;
  1805. start = vma->vm_end;
  1806. }
  1807. return 0;
  1808. }
  1809. void userfaultfd_release_new(struct userfaultfd_ctx *ctx)
  1810. {
  1811. struct mm_struct *mm = ctx->mm;
  1812. struct vm_area_struct *vma;
  1813. VMA_ITERATOR(vmi, mm, 0);
  1814. /* the various vma->vm_userfaultfd_ctx still points to it */
  1815. mmap_write_lock(mm);
  1816. for_each_vma(vmi, vma) {
  1817. if (vma->vm_userfaultfd_ctx.ctx == ctx)
  1818. userfaultfd_reset_ctx(vma);
  1819. }
  1820. mmap_write_unlock(mm);
  1821. }
  1822. void userfaultfd_release_all(struct mm_struct *mm,
  1823. struct userfaultfd_ctx *ctx)
  1824. {
  1825. struct vm_area_struct *vma, *prev;
  1826. VMA_ITERATOR(vmi, mm, 0);
  1827. if (!mmget_not_zero(mm))
  1828. return;
  1829. /*
  1830. * Flush page faults out of all CPUs. NOTE: all page faults
  1831. * must be retried without returning VM_FAULT_SIGBUS if
  1832. * userfaultfd_ctx_get() succeeds but vma->vma_userfault_ctx
  1833. * changes while handle_userfault released the mmap_lock. So
  1834. * it's critical that released is set to true (above), before
  1835. * taking the mmap_lock for writing.
  1836. */
  1837. mmap_write_lock(mm);
  1838. prev = NULL;
  1839. for_each_vma(vmi, vma) {
  1840. cond_resched();
  1841. VM_WARN_ON_ONCE(!!vma->vm_userfaultfd_ctx.ctx ^
  1842. !!(vma->vm_flags & __VM_UFFD_FLAGS));
  1843. if (vma->vm_userfaultfd_ctx.ctx != ctx) {
  1844. prev = vma;
  1845. continue;
  1846. }
  1847. vma = userfaultfd_clear_vma(&vmi, prev, vma,
  1848. vma->vm_start, vma->vm_end);
  1849. prev = vma;
  1850. }
  1851. mmap_write_unlock(mm);
  1852. mmput(mm);
  1853. }