mremap.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/mremap.c
  4. *
  5. * (C) Copyright 1996 Linus Torvalds
  6. *
  7. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  8. * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/mm_inline.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/shm.h>
  14. #include <linux/ksm.h>
  15. #include <linux/mman.h>
  16. #include <linux/swap.h>
  17. #include <linux/capability.h>
  18. #include <linux/fs.h>
  19. #include <linux/swapops.h>
  20. #include <linux/highmem.h>
  21. #include <linux/security.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/mmu_notifier.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/userfaultfd_k.h>
  26. #include <linux/mempolicy.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/tlb.h>
  29. #include <asm/pgalloc.h>
  30. #include "internal.h"
  31. static pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr)
  32. {
  33. pgd_t *pgd;
  34. p4d_t *p4d;
  35. pud_t *pud;
  36. pgd = pgd_offset(mm, addr);
  37. if (pgd_none_or_clear_bad(pgd))
  38. return NULL;
  39. p4d = p4d_offset(pgd, addr);
  40. if (p4d_none_or_clear_bad(p4d))
  41. return NULL;
  42. pud = pud_offset(p4d, addr);
  43. if (pud_none_or_clear_bad(pud))
  44. return NULL;
  45. return pud;
  46. }
  47. static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
  48. {
  49. pud_t *pud;
  50. pmd_t *pmd;
  51. pud = get_old_pud(mm, addr);
  52. if (!pud)
  53. return NULL;
  54. pmd = pmd_offset(pud, addr);
  55. if (pmd_none(*pmd))
  56. return NULL;
  57. return pmd;
  58. }
  59. static pud_t *alloc_new_pud(struct mm_struct *mm, struct vm_area_struct *vma,
  60. unsigned long addr)
  61. {
  62. pgd_t *pgd;
  63. p4d_t *p4d;
  64. pgd = pgd_offset(mm, addr);
  65. p4d = p4d_alloc(mm, pgd, addr);
  66. if (!p4d)
  67. return NULL;
  68. return pud_alloc(mm, p4d, addr);
  69. }
  70. static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
  71. unsigned long addr)
  72. {
  73. pud_t *pud;
  74. pmd_t *pmd;
  75. pud = alloc_new_pud(mm, vma, addr);
  76. if (!pud)
  77. return NULL;
  78. pmd = pmd_alloc(mm, pud, addr);
  79. if (!pmd)
  80. return NULL;
  81. VM_BUG_ON(pmd_trans_huge(*pmd));
  82. return pmd;
  83. }
  84. static void take_rmap_locks(struct vm_area_struct *vma)
  85. {
  86. if (vma->vm_file)
  87. i_mmap_lock_write(vma->vm_file->f_mapping);
  88. if (vma->anon_vma)
  89. anon_vma_lock_write(vma->anon_vma);
  90. }
  91. static void drop_rmap_locks(struct vm_area_struct *vma)
  92. {
  93. if (vma->anon_vma)
  94. anon_vma_unlock_write(vma->anon_vma);
  95. if (vma->vm_file)
  96. i_mmap_unlock_write(vma->vm_file->f_mapping);
  97. }
  98. static pte_t move_soft_dirty_pte(pte_t pte)
  99. {
  100. /*
  101. * Set soft dirty bit so we can notice
  102. * in userspace the ptes were moved.
  103. */
  104. #ifdef CONFIG_MEM_SOFT_DIRTY
  105. if (pte_present(pte))
  106. pte = pte_mksoft_dirty(pte);
  107. else if (is_swap_pte(pte))
  108. pte = pte_swp_mksoft_dirty(pte);
  109. #endif
  110. return pte;
  111. }
  112. static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
  113. unsigned long old_addr, unsigned long old_end,
  114. struct vm_area_struct *new_vma, pmd_t *new_pmd,
  115. unsigned long new_addr, bool need_rmap_locks)
  116. {
  117. bool need_clear_uffd_wp = vma_has_uffd_without_event_remap(vma);
  118. struct mm_struct *mm = vma->vm_mm;
  119. pte_t *old_pte, *new_pte, pte;
  120. spinlock_t *old_ptl, *new_ptl;
  121. bool force_flush = false;
  122. unsigned long len = old_end - old_addr;
  123. int err = 0;
  124. /*
  125. * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
  126. * locks to ensure that rmap will always observe either the old or the
  127. * new ptes. This is the easiest way to avoid races with
  128. * truncate_pagecache(), page migration, etc...
  129. *
  130. * When need_rmap_locks is false, we use other ways to avoid
  131. * such races:
  132. *
  133. * - During exec() shift_arg_pages(), we use a specially tagged vma
  134. * which rmap call sites look for using vma_is_temporary_stack().
  135. *
  136. * - During mremap(), new_vma is often known to be placed after vma
  137. * in rmap traversal order. This ensures rmap will always observe
  138. * either the old pte, or the new pte, or both (the page table locks
  139. * serialize access to individual ptes, but only rmap traversal
  140. * order guarantees that we won't miss both the old and new ptes).
  141. */
  142. if (need_rmap_locks)
  143. take_rmap_locks(vma);
  144. /*
  145. * We don't have to worry about the ordering of src and dst
  146. * pte locks because exclusive mmap_lock prevents deadlock.
  147. */
  148. old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
  149. if (!old_pte) {
  150. err = -EAGAIN;
  151. goto out;
  152. }
  153. new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl);
  154. if (!new_pte) {
  155. pte_unmap_unlock(old_pte, old_ptl);
  156. err = -EAGAIN;
  157. goto out;
  158. }
  159. if (new_ptl != old_ptl)
  160. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  161. flush_tlb_batched_pending(vma->vm_mm);
  162. arch_enter_lazy_mmu_mode();
  163. for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
  164. new_pte++, new_addr += PAGE_SIZE) {
  165. if (pte_none(ptep_get(old_pte)))
  166. continue;
  167. pte = ptep_get_and_clear(mm, old_addr, old_pte);
  168. /*
  169. * If we are remapping a valid PTE, make sure
  170. * to flush TLB before we drop the PTL for the
  171. * PTE.
  172. *
  173. * NOTE! Both old and new PTL matter: the old one
  174. * for racing with folio_mkclean(), the new one to
  175. * make sure the physical page stays valid until
  176. * the TLB entry for the old mapping has been
  177. * flushed.
  178. */
  179. if (pte_present(pte))
  180. force_flush = true;
  181. pte = move_pte(pte, old_addr, new_addr);
  182. pte = move_soft_dirty_pte(pte);
  183. if (need_clear_uffd_wp && pte_marker_uffd_wp(pte))
  184. pte_clear(mm, new_addr, new_pte);
  185. else {
  186. if (need_clear_uffd_wp) {
  187. if (pte_present(pte))
  188. pte = pte_clear_uffd_wp(pte);
  189. else if (is_swap_pte(pte))
  190. pte = pte_swp_clear_uffd_wp(pte);
  191. }
  192. set_pte_at(mm, new_addr, new_pte, pte);
  193. }
  194. }
  195. arch_leave_lazy_mmu_mode();
  196. if (force_flush)
  197. flush_tlb_range(vma, old_end - len, old_end);
  198. if (new_ptl != old_ptl)
  199. spin_unlock(new_ptl);
  200. pte_unmap(new_pte - 1);
  201. pte_unmap_unlock(old_pte - 1, old_ptl);
  202. out:
  203. if (need_rmap_locks)
  204. drop_rmap_locks(vma);
  205. return err;
  206. }
  207. #ifndef arch_supports_page_table_move
  208. #define arch_supports_page_table_move arch_supports_page_table_move
  209. static inline bool arch_supports_page_table_move(void)
  210. {
  211. return IS_ENABLED(CONFIG_HAVE_MOVE_PMD) ||
  212. IS_ENABLED(CONFIG_HAVE_MOVE_PUD);
  213. }
  214. #endif
  215. #ifdef CONFIG_HAVE_MOVE_PMD
  216. static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
  217. unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
  218. {
  219. spinlock_t *old_ptl, *new_ptl;
  220. struct mm_struct *mm = vma->vm_mm;
  221. bool res = false;
  222. pmd_t pmd;
  223. if (!arch_supports_page_table_move())
  224. return false;
  225. /*
  226. * The destination pmd shouldn't be established, free_pgtables()
  227. * should have released it.
  228. *
  229. * However, there's a case during execve() where we use mremap
  230. * to move the initial stack, and in that case the target area
  231. * may overlap the source area (always moving down).
  232. *
  233. * If everything is PMD-aligned, that works fine, as moving
  234. * each pmd down will clear the source pmd. But if we first
  235. * have a few 4kB-only pages that get moved down, and then
  236. * hit the "now the rest is PMD-aligned, let's do everything
  237. * one pmd at a time", we will still have the old (now empty
  238. * of any 4kB pages, but still there) PMD in the page table
  239. * tree.
  240. *
  241. * Warn on it once - because we really should try to figure
  242. * out how to do this better - but then say "I won't move
  243. * this pmd".
  244. *
  245. * One alternative might be to just unmap the target pmd at
  246. * this point, and verify that it really is empty. We'll see.
  247. */
  248. if (WARN_ON_ONCE(!pmd_none(*new_pmd)))
  249. return false;
  250. /* If this pmd belongs to a uffd vma with remap events disabled, we need
  251. * to ensure that the uffd-wp state is cleared from all pgtables. This
  252. * means recursing into lower page tables in move_page_tables(), and we
  253. * can reuse the existing code if we simply treat the entry as "not
  254. * moved".
  255. */
  256. if (vma_has_uffd_without_event_remap(vma))
  257. return false;
  258. /*
  259. * We don't have to worry about the ordering of src and dst
  260. * ptlocks because exclusive mmap_lock prevents deadlock.
  261. */
  262. old_ptl = pmd_lock(vma->vm_mm, old_pmd);
  263. new_ptl = pmd_lockptr(mm, new_pmd);
  264. if (new_ptl != old_ptl)
  265. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  266. pmd = *old_pmd;
  267. /* Racing with collapse? */
  268. if (unlikely(!pmd_present(pmd) || pmd_leaf(pmd)))
  269. goto out_unlock;
  270. /* Clear the pmd */
  271. pmd_clear(old_pmd);
  272. res = true;
  273. VM_BUG_ON(!pmd_none(*new_pmd));
  274. pmd_populate(mm, new_pmd, pmd_pgtable(pmd));
  275. flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
  276. out_unlock:
  277. if (new_ptl != old_ptl)
  278. spin_unlock(new_ptl);
  279. spin_unlock(old_ptl);
  280. return res;
  281. }
  282. #else
  283. static inline bool move_normal_pmd(struct vm_area_struct *vma,
  284. unsigned long old_addr, unsigned long new_addr, pmd_t *old_pmd,
  285. pmd_t *new_pmd)
  286. {
  287. return false;
  288. }
  289. #endif
  290. #if CONFIG_PGTABLE_LEVELS > 2 && defined(CONFIG_HAVE_MOVE_PUD)
  291. static bool move_normal_pud(struct vm_area_struct *vma, unsigned long old_addr,
  292. unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
  293. {
  294. spinlock_t *old_ptl, *new_ptl;
  295. struct mm_struct *mm = vma->vm_mm;
  296. pud_t pud;
  297. if (!arch_supports_page_table_move())
  298. return false;
  299. /*
  300. * The destination pud shouldn't be established, free_pgtables()
  301. * should have released it.
  302. */
  303. if (WARN_ON_ONCE(!pud_none(*new_pud)))
  304. return false;
  305. /* If this pud belongs to a uffd vma with remap events disabled, we need
  306. * to ensure that the uffd-wp state is cleared from all pgtables. This
  307. * means recursing into lower page tables in move_page_tables(), and we
  308. * can reuse the existing code if we simply treat the entry as "not
  309. * moved".
  310. */
  311. if (vma_has_uffd_without_event_remap(vma))
  312. return false;
  313. /*
  314. * We don't have to worry about the ordering of src and dst
  315. * ptlocks because exclusive mmap_lock prevents deadlock.
  316. */
  317. old_ptl = pud_lock(vma->vm_mm, old_pud);
  318. new_ptl = pud_lockptr(mm, new_pud);
  319. if (new_ptl != old_ptl)
  320. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  321. /* Clear the pud */
  322. pud = *old_pud;
  323. pud_clear(old_pud);
  324. VM_BUG_ON(!pud_none(*new_pud));
  325. pud_populate(mm, new_pud, pud_pgtable(pud));
  326. flush_tlb_range(vma, old_addr, old_addr + PUD_SIZE);
  327. if (new_ptl != old_ptl)
  328. spin_unlock(new_ptl);
  329. spin_unlock(old_ptl);
  330. return true;
  331. }
  332. #else
  333. static inline bool move_normal_pud(struct vm_area_struct *vma,
  334. unsigned long old_addr, unsigned long new_addr, pud_t *old_pud,
  335. pud_t *new_pud)
  336. {
  337. return false;
  338. }
  339. #endif
  340. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
  341. static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
  342. unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
  343. {
  344. spinlock_t *old_ptl, *new_ptl;
  345. struct mm_struct *mm = vma->vm_mm;
  346. pud_t pud;
  347. /*
  348. * The destination pud shouldn't be established, free_pgtables()
  349. * should have released it.
  350. */
  351. if (WARN_ON_ONCE(!pud_none(*new_pud)))
  352. return false;
  353. /*
  354. * We don't have to worry about the ordering of src and dst
  355. * ptlocks because exclusive mmap_lock prevents deadlock.
  356. */
  357. old_ptl = pud_lock(vma->vm_mm, old_pud);
  358. new_ptl = pud_lockptr(mm, new_pud);
  359. if (new_ptl != old_ptl)
  360. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  361. /* Clear the pud */
  362. pud = *old_pud;
  363. pud_clear(old_pud);
  364. VM_BUG_ON(!pud_none(*new_pud));
  365. /* Set the new pud */
  366. /* mark soft_ditry when we add pud level soft dirty support */
  367. set_pud_at(mm, new_addr, new_pud, pud);
  368. flush_pud_tlb_range(vma, old_addr, old_addr + HPAGE_PUD_SIZE);
  369. if (new_ptl != old_ptl)
  370. spin_unlock(new_ptl);
  371. spin_unlock(old_ptl);
  372. return true;
  373. }
  374. #else
  375. static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
  376. unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
  377. {
  378. WARN_ON_ONCE(1);
  379. return false;
  380. }
  381. #endif
  382. enum pgt_entry {
  383. NORMAL_PMD,
  384. HPAGE_PMD,
  385. NORMAL_PUD,
  386. HPAGE_PUD,
  387. };
  388. /*
  389. * Returns an extent of the corresponding size for the pgt_entry specified if
  390. * valid. Else returns a smaller extent bounded by the end of the source and
  391. * destination pgt_entry.
  392. */
  393. static __always_inline unsigned long get_extent(enum pgt_entry entry,
  394. unsigned long old_addr, unsigned long old_end,
  395. unsigned long new_addr)
  396. {
  397. unsigned long next, extent, mask, size;
  398. switch (entry) {
  399. case HPAGE_PMD:
  400. case NORMAL_PMD:
  401. mask = PMD_MASK;
  402. size = PMD_SIZE;
  403. break;
  404. case HPAGE_PUD:
  405. case NORMAL_PUD:
  406. mask = PUD_MASK;
  407. size = PUD_SIZE;
  408. break;
  409. default:
  410. BUILD_BUG();
  411. break;
  412. }
  413. next = (old_addr + size) & mask;
  414. /* even if next overflowed, extent below will be ok */
  415. extent = next - old_addr;
  416. if (extent > old_end - old_addr)
  417. extent = old_end - old_addr;
  418. next = (new_addr + size) & mask;
  419. if (extent > next - new_addr)
  420. extent = next - new_addr;
  421. return extent;
  422. }
  423. /*
  424. * Attempts to speedup the move by moving entry at the level corresponding to
  425. * pgt_entry. Returns true if the move was successful, else false.
  426. */
  427. static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma,
  428. unsigned long old_addr, unsigned long new_addr,
  429. void *old_entry, void *new_entry, bool need_rmap_locks)
  430. {
  431. bool moved = false;
  432. /* See comment in move_ptes() */
  433. if (need_rmap_locks)
  434. take_rmap_locks(vma);
  435. switch (entry) {
  436. case NORMAL_PMD:
  437. moved = move_normal_pmd(vma, old_addr, new_addr, old_entry,
  438. new_entry);
  439. break;
  440. case NORMAL_PUD:
  441. moved = move_normal_pud(vma, old_addr, new_addr, old_entry,
  442. new_entry);
  443. break;
  444. case HPAGE_PMD:
  445. moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  446. move_huge_pmd(vma, old_addr, new_addr, old_entry,
  447. new_entry);
  448. break;
  449. case HPAGE_PUD:
  450. moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  451. move_huge_pud(vma, old_addr, new_addr, old_entry,
  452. new_entry);
  453. break;
  454. default:
  455. WARN_ON_ONCE(1);
  456. break;
  457. }
  458. if (need_rmap_locks)
  459. drop_rmap_locks(vma);
  460. return moved;
  461. }
  462. /*
  463. * A helper to check if aligning down is OK. The aligned address should fall
  464. * on *no mapping*. For the stack moving down, that's a special move within
  465. * the VMA that is created to span the source and destination of the move,
  466. * so we make an exception for it.
  467. */
  468. static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_align,
  469. unsigned long mask, bool for_stack)
  470. {
  471. unsigned long addr_masked = addr_to_align & mask;
  472. /*
  473. * If @addr_to_align of either source or destination is not the beginning
  474. * of the corresponding VMA, we can't align down or we will destroy part
  475. * of the current mapping.
  476. */
  477. if (!for_stack && vma->vm_start != addr_to_align)
  478. return false;
  479. /* In the stack case we explicitly permit in-VMA alignment. */
  480. if (for_stack && addr_masked >= vma->vm_start)
  481. return true;
  482. /*
  483. * Make sure the realignment doesn't cause the address to fall on an
  484. * existing mapping.
  485. */
  486. return find_vma_intersection(vma->vm_mm, addr_masked, vma->vm_start) == NULL;
  487. }
  488. /* Opportunistically realign to specified boundary for faster copy. */
  489. static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old_vma,
  490. unsigned long *new_addr, struct vm_area_struct *new_vma,
  491. unsigned long mask, bool for_stack)
  492. {
  493. /* Skip if the addresses are already aligned. */
  494. if ((*old_addr & ~mask) == 0)
  495. return;
  496. /* Only realign if the new and old addresses are mutually aligned. */
  497. if ((*old_addr & ~mask) != (*new_addr & ~mask))
  498. return;
  499. /* Ensure realignment doesn't cause overlap with existing mappings. */
  500. if (!can_align_down(old_vma, *old_addr, mask, for_stack) ||
  501. !can_align_down(new_vma, *new_addr, mask, for_stack))
  502. return;
  503. *old_addr = *old_addr & mask;
  504. *new_addr = *new_addr & mask;
  505. }
  506. unsigned long move_page_tables(struct vm_area_struct *vma,
  507. unsigned long old_addr, struct vm_area_struct *new_vma,
  508. unsigned long new_addr, unsigned long len,
  509. bool need_rmap_locks, bool for_stack)
  510. {
  511. unsigned long extent, old_end;
  512. struct mmu_notifier_range range;
  513. pmd_t *old_pmd, *new_pmd;
  514. pud_t *old_pud, *new_pud;
  515. if (!len)
  516. return 0;
  517. old_end = old_addr + len;
  518. if (is_vm_hugetlb_page(vma))
  519. return move_hugetlb_page_tables(vma, new_vma, old_addr,
  520. new_addr, len);
  521. /*
  522. * If possible, realign addresses to PMD boundary for faster copy.
  523. * Only realign if the mremap copying hits a PMD boundary.
  524. */
  525. if (len >= PMD_SIZE - (old_addr & ~PMD_MASK))
  526. try_realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK,
  527. for_stack);
  528. flush_cache_range(vma, old_addr, old_end);
  529. mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm,
  530. old_addr, old_end);
  531. mmu_notifier_invalidate_range_start(&range);
  532. for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
  533. cond_resched();
  534. /*
  535. * If extent is PUD-sized try to speed up the move by moving at the
  536. * PUD level if possible.
  537. */
  538. extent = get_extent(NORMAL_PUD, old_addr, old_end, new_addr);
  539. old_pud = get_old_pud(vma->vm_mm, old_addr);
  540. if (!old_pud)
  541. continue;
  542. new_pud = alloc_new_pud(vma->vm_mm, vma, new_addr);
  543. if (!new_pud)
  544. break;
  545. if (pud_trans_huge(*old_pud) || pud_devmap(*old_pud)) {
  546. if (extent == HPAGE_PUD_SIZE) {
  547. move_pgt_entry(HPAGE_PUD, vma, old_addr, new_addr,
  548. old_pud, new_pud, need_rmap_locks);
  549. /* We ignore and continue on error? */
  550. continue;
  551. }
  552. } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) {
  553. if (move_pgt_entry(NORMAL_PUD, vma, old_addr, new_addr,
  554. old_pud, new_pud, true))
  555. continue;
  556. }
  557. extent = get_extent(NORMAL_PMD, old_addr, old_end, new_addr);
  558. old_pmd = get_old_pmd(vma->vm_mm, old_addr);
  559. if (!old_pmd)
  560. continue;
  561. new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
  562. if (!new_pmd)
  563. break;
  564. again:
  565. if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) ||
  566. pmd_devmap(*old_pmd)) {
  567. if (extent == HPAGE_PMD_SIZE &&
  568. move_pgt_entry(HPAGE_PMD, vma, old_addr, new_addr,
  569. old_pmd, new_pmd, need_rmap_locks))
  570. continue;
  571. split_huge_pmd(vma, old_pmd, old_addr);
  572. } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PMD) &&
  573. extent == PMD_SIZE) {
  574. /*
  575. * If the extent is PMD-sized, try to speed the move by
  576. * moving at the PMD level if possible.
  577. */
  578. if (move_pgt_entry(NORMAL_PMD, vma, old_addr, new_addr,
  579. old_pmd, new_pmd, true))
  580. continue;
  581. }
  582. if (pmd_none(*old_pmd))
  583. continue;
  584. if (pte_alloc(new_vma->vm_mm, new_pmd))
  585. break;
  586. if (move_ptes(vma, old_pmd, old_addr, old_addr + extent,
  587. new_vma, new_pmd, new_addr, need_rmap_locks) < 0)
  588. goto again;
  589. }
  590. mmu_notifier_invalidate_range_end(&range);
  591. /*
  592. * Prevent negative return values when {old,new}_addr was realigned
  593. * but we broke out of the above loop for the first PMD itself.
  594. */
  595. if (old_addr < old_end - len)
  596. return 0;
  597. return len + old_addr - old_end; /* how much done */
  598. }
  599. static unsigned long move_vma(struct vm_area_struct *vma,
  600. unsigned long old_addr, unsigned long old_len,
  601. unsigned long new_len, unsigned long new_addr,
  602. bool *locked, unsigned long flags,
  603. struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap)
  604. {
  605. long to_account = new_len - old_len;
  606. struct mm_struct *mm = vma->vm_mm;
  607. struct vm_area_struct *new_vma;
  608. unsigned long vm_flags = vma->vm_flags;
  609. unsigned long new_pgoff;
  610. unsigned long moved_len;
  611. unsigned long account_start = 0;
  612. unsigned long account_end = 0;
  613. unsigned long hiwater_vm;
  614. int err = 0;
  615. bool need_rmap_locks;
  616. struct vma_iterator vmi;
  617. /*
  618. * We'd prefer to avoid failure later on in do_munmap:
  619. * which may split one vma into three before unmapping.
  620. */
  621. if (mm->map_count >= sysctl_max_map_count - 3)
  622. return -ENOMEM;
  623. if (unlikely(flags & MREMAP_DONTUNMAP))
  624. to_account = new_len;
  625. if (vma->vm_ops && vma->vm_ops->may_split) {
  626. if (vma->vm_start != old_addr)
  627. err = vma->vm_ops->may_split(vma, old_addr);
  628. if (!err && vma->vm_end != old_addr + old_len)
  629. err = vma->vm_ops->may_split(vma, old_addr + old_len);
  630. if (err)
  631. return err;
  632. }
  633. /*
  634. * Advise KSM to break any KSM pages in the area to be moved:
  635. * it would be confusing if they were to turn up at the new
  636. * location, where they happen to coincide with different KSM
  637. * pages recently unmapped. But leave vma->vm_flags as it was,
  638. * so KSM can come around to merge on vma and new_vma afterwards.
  639. */
  640. err = ksm_madvise(vma, old_addr, old_addr + old_len,
  641. MADV_UNMERGEABLE, &vm_flags);
  642. if (err)
  643. return err;
  644. if (vm_flags & VM_ACCOUNT) {
  645. if (security_vm_enough_memory_mm(mm, to_account >> PAGE_SHIFT))
  646. return -ENOMEM;
  647. }
  648. vma_start_write(vma);
  649. new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
  650. new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
  651. &need_rmap_locks);
  652. if (!new_vma) {
  653. if (vm_flags & VM_ACCOUNT)
  654. vm_unacct_memory(to_account >> PAGE_SHIFT);
  655. return -ENOMEM;
  656. }
  657. moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
  658. need_rmap_locks, false);
  659. if (moved_len < old_len) {
  660. err = -ENOMEM;
  661. } else if (vma->vm_ops && vma->vm_ops->mremap) {
  662. err = vma->vm_ops->mremap(new_vma);
  663. }
  664. if (unlikely(err)) {
  665. /*
  666. * On error, move entries back from new area to old,
  667. * which will succeed since page tables still there,
  668. * and then proceed to unmap new area instead of old.
  669. */
  670. move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
  671. true, false);
  672. vma = new_vma;
  673. old_len = new_len;
  674. old_addr = new_addr;
  675. new_addr = err;
  676. } else {
  677. mremap_userfaultfd_prep(new_vma, uf);
  678. }
  679. if (is_vm_hugetlb_page(vma)) {
  680. clear_vma_resv_huge_pages(vma);
  681. }
  682. /* Conceal VM_ACCOUNT so old reservation is not undone */
  683. if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) {
  684. vm_flags_clear(vma, VM_ACCOUNT);
  685. if (vma->vm_start < old_addr)
  686. account_start = vma->vm_start;
  687. if (vma->vm_end > old_addr + old_len)
  688. account_end = vma->vm_end;
  689. }
  690. /*
  691. * If we failed to move page tables we still do total_vm increment
  692. * since do_munmap() will decrement it by old_len == new_len.
  693. *
  694. * Since total_vm is about to be raised artificially high for a
  695. * moment, we need to restore high watermark afterwards: if stats
  696. * are taken meanwhile, total_vm and hiwater_vm appear too high.
  697. * If this were a serious issue, we'd add a flag to do_munmap().
  698. */
  699. hiwater_vm = mm->hiwater_vm;
  700. vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
  701. /* Tell pfnmap has moved from this vma */
  702. if (unlikely(vma->vm_flags & VM_PFNMAP))
  703. untrack_pfn_clear(vma);
  704. if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) {
  705. /* We always clear VM_LOCKED[ONFAULT] on the old vma */
  706. vm_flags_clear(vma, VM_LOCKED_MASK);
  707. /*
  708. * anon_vma links of the old vma is no longer needed after its page
  709. * table has been moved.
  710. */
  711. if (new_vma != vma && vma->vm_start == old_addr &&
  712. vma->vm_end == (old_addr + old_len))
  713. unlink_anon_vmas(vma);
  714. /* Because we won't unmap we don't need to touch locked_vm */
  715. return new_addr;
  716. }
  717. vma_iter_init(&vmi, mm, old_addr);
  718. if (do_vmi_munmap(&vmi, mm, old_addr, old_len, uf_unmap, false) < 0) {
  719. /* OOM: unable to split vma, just get accounts right */
  720. if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP))
  721. vm_acct_memory(old_len >> PAGE_SHIFT);
  722. account_start = account_end = 0;
  723. }
  724. if (vm_flags & VM_LOCKED) {
  725. mm->locked_vm += new_len >> PAGE_SHIFT;
  726. *locked = true;
  727. }
  728. mm->hiwater_vm = hiwater_vm;
  729. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  730. if (account_start) {
  731. vma = vma_prev(&vmi);
  732. vm_flags_set(vma, VM_ACCOUNT);
  733. }
  734. if (account_end) {
  735. vma = vma_next(&vmi);
  736. vm_flags_set(vma, VM_ACCOUNT);
  737. }
  738. return new_addr;
  739. }
  740. static struct vm_area_struct *vma_to_resize(unsigned long addr,
  741. unsigned long old_len, unsigned long new_len, unsigned long flags)
  742. {
  743. struct mm_struct *mm = current->mm;
  744. struct vm_area_struct *vma;
  745. unsigned long pgoff;
  746. vma = vma_lookup(mm, addr);
  747. if (!vma)
  748. return ERR_PTR(-EFAULT);
  749. /*
  750. * !old_len is a special case where an attempt is made to 'duplicate'
  751. * a mapping. This makes no sense for private mappings as it will
  752. * instead create a fresh/new mapping unrelated to the original. This
  753. * is contrary to the basic idea of mremap which creates new mappings
  754. * based on the original. There are no known use cases for this
  755. * behavior. As a result, fail such attempts.
  756. */
  757. if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
  758. pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid);
  759. return ERR_PTR(-EINVAL);
  760. }
  761. if ((flags & MREMAP_DONTUNMAP) &&
  762. (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)))
  763. return ERR_PTR(-EINVAL);
  764. /* We can't remap across vm area boundaries */
  765. if (old_len > vma->vm_end - addr)
  766. return ERR_PTR(-EFAULT);
  767. if (new_len == old_len)
  768. return vma;
  769. /* Need to be careful about a growing mapping */
  770. pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
  771. pgoff += vma->vm_pgoff;
  772. if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
  773. return ERR_PTR(-EINVAL);
  774. if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
  775. return ERR_PTR(-EFAULT);
  776. if (!mlock_future_ok(mm, vma->vm_flags, new_len - old_len))
  777. return ERR_PTR(-EAGAIN);
  778. if (!may_expand_vm(mm, vma->vm_flags,
  779. (new_len - old_len) >> PAGE_SHIFT))
  780. return ERR_PTR(-ENOMEM);
  781. return vma;
  782. }
  783. static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
  784. unsigned long new_addr, unsigned long new_len, bool *locked,
  785. unsigned long flags, struct vm_userfaultfd_ctx *uf,
  786. struct list_head *uf_unmap_early,
  787. struct list_head *uf_unmap)
  788. {
  789. struct mm_struct *mm = current->mm;
  790. struct vm_area_struct *vma;
  791. unsigned long ret = -EINVAL;
  792. unsigned long map_flags = 0;
  793. if (offset_in_page(new_addr))
  794. goto out;
  795. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  796. goto out;
  797. /* Ensure the old/new locations do not overlap */
  798. if (addr + old_len > new_addr && new_addr + new_len > addr)
  799. goto out;
  800. /*
  801. * move_vma() need us to stay 4 maps below the threshold, otherwise
  802. * it will bail out at the very beginning.
  803. * That is a problem if we have already unmaped the regions here
  804. * (new_addr, and old_addr), because userspace will not know the
  805. * state of the vma's after it gets -ENOMEM.
  806. * So, to avoid such scenario we can pre-compute if the whole
  807. * operation has high chances to success map-wise.
  808. * Worst-scenario case is when both vma's (new_addr and old_addr) get
  809. * split in 3 before unmapping it.
  810. * That means 2 more maps (1 for each) to the ones we already hold.
  811. * Check whether current map count plus 2 still leads us to 4 maps below
  812. * the threshold, otherwise return -ENOMEM here to be more safe.
  813. */
  814. if ((mm->map_count + 2) >= sysctl_max_map_count - 3)
  815. return -ENOMEM;
  816. if (flags & MREMAP_FIXED) {
  817. /*
  818. * In mremap_to().
  819. * VMA is moved to dst address, and munmap dst first.
  820. * do_munmap will check if dst is sealed.
  821. */
  822. ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
  823. if (ret)
  824. goto out;
  825. }
  826. if (old_len > new_len) {
  827. ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
  828. if (ret)
  829. goto out;
  830. old_len = new_len;
  831. }
  832. vma = vma_to_resize(addr, old_len, new_len, flags);
  833. if (IS_ERR(vma)) {
  834. ret = PTR_ERR(vma);
  835. goto out;
  836. }
  837. /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */
  838. if (flags & MREMAP_DONTUNMAP &&
  839. !may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) {
  840. ret = -ENOMEM;
  841. goto out;
  842. }
  843. if (flags & MREMAP_FIXED)
  844. map_flags |= MAP_FIXED;
  845. if (vma->vm_flags & VM_MAYSHARE)
  846. map_flags |= MAP_SHARED;
  847. ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
  848. ((addr - vma->vm_start) >> PAGE_SHIFT),
  849. map_flags);
  850. if (IS_ERR_VALUE(ret))
  851. goto out;
  852. /* We got a new mapping */
  853. if (!(flags & MREMAP_FIXED))
  854. new_addr = ret;
  855. ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf,
  856. uf_unmap);
  857. out:
  858. return ret;
  859. }
  860. static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
  861. {
  862. unsigned long end = vma->vm_end + delta;
  863. if (end < vma->vm_end) /* overflow */
  864. return 0;
  865. if (find_vma_intersection(vma->vm_mm, vma->vm_end, end))
  866. return 0;
  867. if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
  868. 0, MAP_FIXED) & ~PAGE_MASK)
  869. return 0;
  870. return 1;
  871. }
  872. /*
  873. * Expand (or shrink) an existing mapping, potentially moving it at the
  874. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  875. *
  876. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  877. * This option implies MREMAP_MAYMOVE.
  878. */
  879. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  880. unsigned long, new_len, unsigned long, flags,
  881. unsigned long, new_addr)
  882. {
  883. struct mm_struct *mm = current->mm;
  884. struct vm_area_struct *vma;
  885. unsigned long ret = -EINVAL;
  886. bool locked = false;
  887. struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
  888. LIST_HEAD(uf_unmap_early);
  889. LIST_HEAD(uf_unmap);
  890. /*
  891. * There is a deliberate asymmetry here: we strip the pointer tag
  892. * from the old address but leave the new address alone. This is
  893. * for consistency with mmap(), where we prevent the creation of
  894. * aliasing mappings in userspace by leaving the tag bits of the
  895. * mapping address intact. A non-zero tag will cause the subsequent
  896. * range checks to reject the address as invalid.
  897. *
  898. * See Documentation/arch/arm64/tagged-address-abi.rst for more
  899. * information.
  900. */
  901. addr = untagged_addr(addr);
  902. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP))
  903. return ret;
  904. if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
  905. return ret;
  906. /*
  907. * MREMAP_DONTUNMAP is always a move and it does not allow resizing
  908. * in the process.
  909. */
  910. if (flags & MREMAP_DONTUNMAP &&
  911. (!(flags & MREMAP_MAYMOVE) || old_len != new_len))
  912. return ret;
  913. if (offset_in_page(addr))
  914. return ret;
  915. old_len = PAGE_ALIGN(old_len);
  916. new_len = PAGE_ALIGN(new_len);
  917. /*
  918. * We allow a zero old-len as a special case
  919. * for DOS-emu "duplicate shm area" thing. But
  920. * a zero new-len is nonsensical.
  921. */
  922. if (!new_len)
  923. return ret;
  924. if (mmap_write_lock_killable(current->mm))
  925. return -EINTR;
  926. vma = vma_lookup(mm, addr);
  927. if (!vma) {
  928. ret = -EFAULT;
  929. goto out;
  930. }
  931. /* Don't allow remapping vmas when they have already been sealed */
  932. if (!can_modify_vma(vma)) {
  933. ret = -EPERM;
  934. goto out;
  935. }
  936. if (is_vm_hugetlb_page(vma)) {
  937. struct hstate *h __maybe_unused = hstate_vma(vma);
  938. old_len = ALIGN(old_len, huge_page_size(h));
  939. new_len = ALIGN(new_len, huge_page_size(h));
  940. /* addrs must be huge page aligned */
  941. if (addr & ~huge_page_mask(h))
  942. goto out;
  943. if (new_addr & ~huge_page_mask(h))
  944. goto out;
  945. /*
  946. * Don't allow remap expansion, because the underlying hugetlb
  947. * reservation is not yet capable to handle split reservation.
  948. */
  949. if (new_len > old_len)
  950. goto out;
  951. }
  952. if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) {
  953. ret = mremap_to(addr, old_len, new_addr, new_len,
  954. &locked, flags, &uf, &uf_unmap_early,
  955. &uf_unmap);
  956. goto out;
  957. }
  958. /*
  959. * Always allow a shrinking remap: that just unmaps
  960. * the unnecessary pages..
  961. * do_vmi_munmap does all the needed commit accounting, and
  962. * unlocks the mmap_lock if so directed.
  963. */
  964. if (old_len >= new_len) {
  965. VMA_ITERATOR(vmi, mm, addr + new_len);
  966. if (old_len == new_len) {
  967. ret = addr;
  968. goto out;
  969. }
  970. ret = do_vmi_munmap(&vmi, mm, addr + new_len, old_len - new_len,
  971. &uf_unmap, true);
  972. if (ret)
  973. goto out;
  974. ret = addr;
  975. goto out_unlocked;
  976. }
  977. /*
  978. * Ok, we need to grow..
  979. */
  980. vma = vma_to_resize(addr, old_len, new_len, flags);
  981. if (IS_ERR(vma)) {
  982. ret = PTR_ERR(vma);
  983. goto out;
  984. }
  985. /* old_len exactly to the end of the area..
  986. */
  987. if (old_len == vma->vm_end - addr) {
  988. unsigned long delta = new_len - old_len;
  989. /* can we just expand the current mapping? */
  990. if (vma_expandable(vma, delta)) {
  991. long pages = delta >> PAGE_SHIFT;
  992. VMA_ITERATOR(vmi, mm, vma->vm_end);
  993. long charged = 0;
  994. if (vma->vm_flags & VM_ACCOUNT) {
  995. if (security_vm_enough_memory_mm(mm, pages)) {
  996. ret = -ENOMEM;
  997. goto out;
  998. }
  999. charged = pages;
  1000. }
  1001. /*
  1002. * Function vma_merge_extend() is called on the
  1003. * extension we are adding to the already existing vma,
  1004. * vma_merge_extend() will merge this extension with the
  1005. * already existing vma (expand operation itself) and
  1006. * possibly also with the next vma if it becomes
  1007. * adjacent to the expanded vma and otherwise
  1008. * compatible.
  1009. */
  1010. vma = vma_merge_extend(&vmi, vma, delta);
  1011. if (!vma) {
  1012. vm_unacct_memory(charged);
  1013. ret = -ENOMEM;
  1014. goto out;
  1015. }
  1016. vm_stat_account(mm, vma->vm_flags, pages);
  1017. if (vma->vm_flags & VM_LOCKED) {
  1018. mm->locked_vm += pages;
  1019. locked = true;
  1020. new_addr = addr;
  1021. }
  1022. ret = addr;
  1023. goto out;
  1024. }
  1025. }
  1026. /*
  1027. * We weren't able to just expand or shrink the area,
  1028. * we need to create a new one and move it..
  1029. */
  1030. ret = -ENOMEM;
  1031. if (flags & MREMAP_MAYMOVE) {
  1032. unsigned long map_flags = 0;
  1033. if (vma->vm_flags & VM_MAYSHARE)
  1034. map_flags |= MAP_SHARED;
  1035. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  1036. vma->vm_pgoff +
  1037. ((addr - vma->vm_start) >> PAGE_SHIFT),
  1038. map_flags);
  1039. if (IS_ERR_VALUE(new_addr)) {
  1040. ret = new_addr;
  1041. goto out;
  1042. }
  1043. ret = move_vma(vma, addr, old_len, new_len, new_addr,
  1044. &locked, flags, &uf, &uf_unmap);
  1045. }
  1046. out:
  1047. if (offset_in_page(ret))
  1048. locked = false;
  1049. mmap_write_unlock(current->mm);
  1050. if (locked && new_len > old_len)
  1051. mm_populate(new_addr + old_len, new_len - old_len);
  1052. out_unlocked:
  1053. userfaultfd_unmap_complete(mm, &uf_unmap_early);
  1054. mremap_userfaultfd_complete(&uf, addr, ret, old_len);
  1055. userfaultfd_unmap_complete(mm, &uf_unmap);
  1056. return ret;
  1057. }