mremap.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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 <[email protected]>
  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 void 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. struct mm_struct *mm = vma->vm_mm;
  118. pte_t *old_pte, *new_pte, pte;
  119. spinlock_t *old_ptl, *new_ptl;
  120. bool force_flush = false;
  121. unsigned long len = old_end - old_addr;
  122. /*
  123. * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
  124. * locks to ensure that rmap will always observe either the old or the
  125. * new ptes. This is the easiest way to avoid races with
  126. * truncate_pagecache(), page migration, etc...
  127. *
  128. * When need_rmap_locks is false, we use other ways to avoid
  129. * such races:
  130. *
  131. * - During exec() shift_arg_pages(), we use a specially tagged vma
  132. * which rmap call sites look for using vma_is_temporary_stack().
  133. *
  134. * - During mremap(), new_vma is often known to be placed after vma
  135. * in rmap traversal order. This ensures rmap will always observe
  136. * either the old pte, or the new pte, or both (the page table locks
  137. * serialize access to individual ptes, but only rmap traversal
  138. * order guarantees that we won't miss both the old and new ptes).
  139. */
  140. if (need_rmap_locks)
  141. take_rmap_locks(vma);
  142. /*
  143. * We don't have to worry about the ordering of src and dst
  144. * pte locks because exclusive mmap_lock prevents deadlock.
  145. */
  146. old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
  147. new_pte = pte_offset_map(new_pmd, new_addr);
  148. new_ptl = pte_lockptr(mm, new_pmd);
  149. if (new_ptl != old_ptl)
  150. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  151. flush_tlb_batched_pending(vma->vm_mm);
  152. arch_enter_lazy_mmu_mode();
  153. for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
  154. new_pte++, new_addr += PAGE_SIZE) {
  155. if (pte_none(*old_pte))
  156. continue;
  157. pte = ptep_get_and_clear(mm, old_addr, old_pte);
  158. /*
  159. * If we are remapping a valid PTE, make sure
  160. * to flush TLB before we drop the PTL for the
  161. * PTE.
  162. *
  163. * NOTE! Both old and new PTL matter: the old one
  164. * for racing with page_mkclean(), the new one to
  165. * make sure the physical page stays valid until
  166. * the TLB entry for the old mapping has been
  167. * flushed.
  168. */
  169. if (pte_present(pte))
  170. force_flush = true;
  171. pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
  172. pte = move_soft_dirty_pte(pte);
  173. set_pte_at(mm, new_addr, new_pte, pte);
  174. }
  175. arch_leave_lazy_mmu_mode();
  176. if (force_flush)
  177. flush_tlb_range(vma, old_end - len, old_end);
  178. if (new_ptl != old_ptl)
  179. spin_unlock(new_ptl);
  180. pte_unmap(new_pte - 1);
  181. pte_unmap_unlock(old_pte - 1, old_ptl);
  182. if (need_rmap_locks)
  183. drop_rmap_locks(vma);
  184. }
  185. #ifndef arch_supports_page_table_move
  186. #define arch_supports_page_table_move arch_supports_page_table_move
  187. static inline bool arch_supports_page_table_move(void)
  188. {
  189. return IS_ENABLED(CONFIG_HAVE_MOVE_PMD) ||
  190. IS_ENABLED(CONFIG_HAVE_MOVE_PUD);
  191. }
  192. #endif
  193. #ifdef CONFIG_HAVE_MOVE_PMD
  194. static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
  195. unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
  196. {
  197. spinlock_t *old_ptl, *new_ptl;
  198. struct mm_struct *mm = vma->vm_mm;
  199. pmd_t pmd;
  200. if (!arch_supports_page_table_move())
  201. return false;
  202. /*
  203. * The destination pmd shouldn't be established, free_pgtables()
  204. * should have released it.
  205. *
  206. * However, there's a case during execve() where we use mremap
  207. * to move the initial stack, and in that case the target area
  208. * may overlap the source area (always moving down).
  209. *
  210. * If everything is PMD-aligned, that works fine, as moving
  211. * each pmd down will clear the source pmd. But if we first
  212. * have a few 4kB-only pages that get moved down, and then
  213. * hit the "now the rest is PMD-aligned, let's do everything
  214. * one pmd at a time", we will still have the old (now empty
  215. * of any 4kB pages, but still there) PMD in the page table
  216. * tree.
  217. *
  218. * Warn on it once - because we really should try to figure
  219. * out how to do this better - but then say "I won't move
  220. * this pmd".
  221. *
  222. * One alternative might be to just unmap the target pmd at
  223. * this point, and verify that it really is empty. We'll see.
  224. */
  225. if (WARN_ON_ONCE(!pmd_none(*new_pmd)))
  226. return false;
  227. /*
  228. * We don't have to worry about the ordering of src and dst
  229. * ptlocks because exclusive mmap_lock prevents deadlock.
  230. */
  231. old_ptl = pmd_lock(vma->vm_mm, old_pmd);
  232. new_ptl = pmd_lockptr(mm, new_pmd);
  233. if (new_ptl != old_ptl)
  234. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  235. /* Clear the pmd */
  236. pmd = *old_pmd;
  237. pmd_clear(old_pmd);
  238. VM_BUG_ON(!pmd_none(*new_pmd));
  239. pmd_populate(mm, new_pmd, pmd_pgtable(pmd));
  240. flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
  241. if (new_ptl != old_ptl)
  242. spin_unlock(new_ptl);
  243. spin_unlock(old_ptl);
  244. return true;
  245. }
  246. #else
  247. static inline bool move_normal_pmd(struct vm_area_struct *vma,
  248. unsigned long old_addr, unsigned long new_addr, pmd_t *old_pmd,
  249. pmd_t *new_pmd)
  250. {
  251. return false;
  252. }
  253. #endif
  254. #if CONFIG_PGTABLE_LEVELS > 2 && defined(CONFIG_HAVE_MOVE_PUD)
  255. static bool move_normal_pud(struct vm_area_struct *vma, unsigned long old_addr,
  256. unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
  257. {
  258. spinlock_t *old_ptl, *new_ptl;
  259. struct mm_struct *mm = vma->vm_mm;
  260. pud_t pud;
  261. if (!arch_supports_page_table_move())
  262. return false;
  263. /*
  264. * The destination pud shouldn't be established, free_pgtables()
  265. * should have released it.
  266. */
  267. if (WARN_ON_ONCE(!pud_none(*new_pud)))
  268. return false;
  269. /*
  270. * We don't have to worry about the ordering of src and dst
  271. * ptlocks because exclusive mmap_lock prevents deadlock.
  272. */
  273. old_ptl = pud_lock(vma->vm_mm, old_pud);
  274. new_ptl = pud_lockptr(mm, new_pud);
  275. if (new_ptl != old_ptl)
  276. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  277. /* Clear the pud */
  278. pud = *old_pud;
  279. pud_clear(old_pud);
  280. VM_BUG_ON(!pud_none(*new_pud));
  281. pud_populate(mm, new_pud, pud_pgtable(pud));
  282. flush_tlb_range(vma, old_addr, old_addr + PUD_SIZE);
  283. if (new_ptl != old_ptl)
  284. spin_unlock(new_ptl);
  285. spin_unlock(old_ptl);
  286. return true;
  287. }
  288. #else
  289. static inline bool move_normal_pud(struct vm_area_struct *vma,
  290. unsigned long old_addr, unsigned long new_addr, pud_t *old_pud,
  291. pud_t *new_pud)
  292. {
  293. return false;
  294. }
  295. #endif
  296. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  297. static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
  298. unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
  299. {
  300. spinlock_t *old_ptl, *new_ptl;
  301. struct mm_struct *mm = vma->vm_mm;
  302. pud_t pud;
  303. /*
  304. * The destination pud shouldn't be established, free_pgtables()
  305. * should have released it.
  306. */
  307. if (WARN_ON_ONCE(!pud_none(*new_pud)))
  308. return false;
  309. /*
  310. * We don't have to worry about the ordering of src and dst
  311. * ptlocks because exclusive mmap_lock prevents deadlock.
  312. */
  313. old_ptl = pud_lock(vma->vm_mm, old_pud);
  314. new_ptl = pud_lockptr(mm, new_pud);
  315. if (new_ptl != old_ptl)
  316. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  317. /* Clear the pud */
  318. pud = *old_pud;
  319. pud_clear(old_pud);
  320. VM_BUG_ON(!pud_none(*new_pud));
  321. /* Set the new pud */
  322. /* mark soft_ditry when we add pud level soft dirty support */
  323. set_pud_at(mm, new_addr, new_pud, pud);
  324. flush_pud_tlb_range(vma, old_addr, old_addr + HPAGE_PUD_SIZE);
  325. if (new_ptl != old_ptl)
  326. spin_unlock(new_ptl);
  327. spin_unlock(old_ptl);
  328. return true;
  329. }
  330. #else
  331. static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
  332. unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
  333. {
  334. WARN_ON_ONCE(1);
  335. return false;
  336. }
  337. #endif
  338. enum pgt_entry {
  339. NORMAL_PMD,
  340. HPAGE_PMD,
  341. NORMAL_PUD,
  342. HPAGE_PUD,
  343. };
  344. /*
  345. * Returns an extent of the corresponding size for the pgt_entry specified if
  346. * valid. Else returns a smaller extent bounded by the end of the source and
  347. * destination pgt_entry.
  348. */
  349. static __always_inline unsigned long get_extent(enum pgt_entry entry,
  350. unsigned long old_addr, unsigned long old_end,
  351. unsigned long new_addr)
  352. {
  353. unsigned long next, extent, mask, size;
  354. switch (entry) {
  355. case HPAGE_PMD:
  356. case NORMAL_PMD:
  357. mask = PMD_MASK;
  358. size = PMD_SIZE;
  359. break;
  360. case HPAGE_PUD:
  361. case NORMAL_PUD:
  362. mask = PUD_MASK;
  363. size = PUD_SIZE;
  364. break;
  365. default:
  366. BUILD_BUG();
  367. break;
  368. }
  369. next = (old_addr + size) & mask;
  370. /* even if next overflowed, extent below will be ok */
  371. extent = next - old_addr;
  372. if (extent > old_end - old_addr)
  373. extent = old_end - old_addr;
  374. next = (new_addr + size) & mask;
  375. if (extent > next - new_addr)
  376. extent = next - new_addr;
  377. return extent;
  378. }
  379. /*
  380. * Attempts to speedup the move by moving entry at the level corresponding to
  381. * pgt_entry. Returns true if the move was successful, else false.
  382. */
  383. static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma,
  384. unsigned long old_addr, unsigned long new_addr,
  385. void *old_entry, void *new_entry, bool need_rmap_locks)
  386. {
  387. bool moved = false;
  388. /* See comment in move_ptes() */
  389. if (need_rmap_locks)
  390. take_rmap_locks(vma);
  391. switch (entry) {
  392. case NORMAL_PMD:
  393. moved = move_normal_pmd(vma, old_addr, new_addr, old_entry,
  394. new_entry);
  395. break;
  396. case NORMAL_PUD:
  397. moved = move_normal_pud(vma, old_addr, new_addr, old_entry,
  398. new_entry);
  399. break;
  400. case HPAGE_PMD:
  401. moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  402. move_huge_pmd(vma, old_addr, new_addr, old_entry,
  403. new_entry);
  404. break;
  405. case HPAGE_PUD:
  406. moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
  407. move_huge_pud(vma, old_addr, new_addr, old_entry,
  408. new_entry);
  409. break;
  410. default:
  411. WARN_ON_ONCE(1);
  412. break;
  413. }
  414. if (need_rmap_locks)
  415. drop_rmap_locks(vma);
  416. return moved;
  417. }
  418. unsigned long move_page_tables(struct vm_area_struct *vma,
  419. unsigned long old_addr, struct vm_area_struct *new_vma,
  420. unsigned long new_addr, unsigned long len,
  421. bool need_rmap_locks)
  422. {
  423. unsigned long extent, old_end;
  424. struct mmu_notifier_range range;
  425. pmd_t *old_pmd, *new_pmd;
  426. pud_t *old_pud, *new_pud;
  427. if (!len)
  428. return 0;
  429. old_end = old_addr + len;
  430. if (is_vm_hugetlb_page(vma))
  431. return move_hugetlb_page_tables(vma, new_vma, old_addr,
  432. new_addr, len);
  433. flush_cache_range(vma, old_addr, old_end);
  434. mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
  435. old_addr, old_end);
  436. mmu_notifier_invalidate_range_start(&range);
  437. for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
  438. cond_resched();
  439. /*
  440. * If extent is PUD-sized try to speed up the move by moving at the
  441. * PUD level if possible.
  442. */
  443. extent = get_extent(NORMAL_PUD, old_addr, old_end, new_addr);
  444. old_pud = get_old_pud(vma->vm_mm, old_addr);
  445. if (!old_pud)
  446. continue;
  447. new_pud = alloc_new_pud(vma->vm_mm, vma, new_addr);
  448. if (!new_pud)
  449. break;
  450. if (pud_trans_huge(*old_pud) || pud_devmap(*old_pud)) {
  451. if (extent == HPAGE_PUD_SIZE) {
  452. move_pgt_entry(HPAGE_PUD, vma, old_addr, new_addr,
  453. old_pud, new_pud, need_rmap_locks);
  454. /* We ignore and continue on error? */
  455. continue;
  456. }
  457. } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) {
  458. if (move_pgt_entry(NORMAL_PUD, vma, old_addr, new_addr,
  459. old_pud, new_pud, true))
  460. continue;
  461. }
  462. extent = get_extent(NORMAL_PMD, old_addr, old_end, new_addr);
  463. old_pmd = get_old_pmd(vma->vm_mm, old_addr);
  464. if (!old_pmd)
  465. continue;
  466. new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
  467. if (!new_pmd)
  468. break;
  469. if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) ||
  470. pmd_devmap(*old_pmd)) {
  471. if (extent == HPAGE_PMD_SIZE &&
  472. move_pgt_entry(HPAGE_PMD, vma, old_addr, new_addr,
  473. old_pmd, new_pmd, need_rmap_locks))
  474. continue;
  475. split_huge_pmd(vma, old_pmd, old_addr);
  476. if (pmd_trans_unstable(old_pmd))
  477. continue;
  478. } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PMD) &&
  479. extent == PMD_SIZE) {
  480. /*
  481. * If the extent is PMD-sized, try to speed the move by
  482. * moving at the PMD level if possible.
  483. */
  484. if (move_pgt_entry(NORMAL_PMD, vma, old_addr, new_addr,
  485. old_pmd, new_pmd, true))
  486. continue;
  487. }
  488. if (pte_alloc(new_vma->vm_mm, new_pmd))
  489. break;
  490. move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
  491. new_pmd, new_addr, need_rmap_locks);
  492. }
  493. mmu_notifier_invalidate_range_end(&range);
  494. return len + old_addr - old_end; /* how much done */
  495. }
  496. static unsigned long move_vma(struct vm_area_struct *vma,
  497. unsigned long old_addr, unsigned long old_len,
  498. unsigned long new_len, unsigned long new_addr,
  499. bool *locked, unsigned long flags,
  500. struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap)
  501. {
  502. long to_account = new_len - old_len;
  503. struct mm_struct *mm = vma->vm_mm;
  504. struct vm_area_struct *new_vma;
  505. unsigned long vm_flags = vma->vm_flags;
  506. unsigned long new_pgoff;
  507. unsigned long moved_len;
  508. unsigned long excess = 0;
  509. unsigned long hiwater_vm;
  510. int split = 0;
  511. int err = 0;
  512. bool need_rmap_locks;
  513. /*
  514. * We'd prefer to avoid failure later on in do_munmap:
  515. * which may split one vma into three before unmapping.
  516. */
  517. if (mm->map_count >= sysctl_max_map_count - 3)
  518. return -ENOMEM;
  519. if (unlikely(flags & MREMAP_DONTUNMAP))
  520. to_account = new_len;
  521. if (vma->vm_ops && vma->vm_ops->may_split) {
  522. if (vma->vm_start != old_addr)
  523. err = vma->vm_ops->may_split(vma, old_addr);
  524. if (!err && vma->vm_end != old_addr + old_len)
  525. err = vma->vm_ops->may_split(vma, old_addr + old_len);
  526. if (err)
  527. return err;
  528. }
  529. /*
  530. * Advise KSM to break any KSM pages in the area to be moved:
  531. * it would be confusing if they were to turn up at the new
  532. * location, where they happen to coincide with different KSM
  533. * pages recently unmapped. But leave vma->vm_flags as it was,
  534. * so KSM can come around to merge on vma and new_vma afterwards.
  535. */
  536. err = ksm_madvise(vma, old_addr, old_addr + old_len,
  537. MADV_UNMERGEABLE, &vm_flags);
  538. if (err)
  539. return err;
  540. if (vm_flags & VM_ACCOUNT) {
  541. if (security_vm_enough_memory_mm(mm, to_account >> PAGE_SHIFT))
  542. return -ENOMEM;
  543. }
  544. vma_start_write(vma);
  545. new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
  546. new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
  547. &need_rmap_locks);
  548. if (!new_vma) {
  549. if (vm_flags & VM_ACCOUNT)
  550. vm_unacct_memory(to_account >> PAGE_SHIFT);
  551. return -ENOMEM;
  552. }
  553. moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
  554. need_rmap_locks);
  555. if (moved_len < old_len) {
  556. err = -ENOMEM;
  557. } else if (vma->vm_ops && vma->vm_ops->mremap) {
  558. err = vma->vm_ops->mremap(new_vma);
  559. }
  560. if (unlikely(err)) {
  561. /*
  562. * On error, move entries back from new area to old,
  563. * which will succeed since page tables still there,
  564. * and then proceed to unmap new area instead of old.
  565. */
  566. move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
  567. true);
  568. vma = new_vma;
  569. old_len = new_len;
  570. old_addr = new_addr;
  571. new_addr = err;
  572. } else {
  573. mremap_userfaultfd_prep(new_vma, uf);
  574. }
  575. if (is_vm_hugetlb_page(vma)) {
  576. clear_vma_resv_huge_pages(vma);
  577. }
  578. /* Conceal VM_ACCOUNT so old reservation is not undone */
  579. if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) {
  580. vm_flags_clear(vma, VM_ACCOUNT);
  581. excess = vma->vm_end - vma->vm_start - old_len;
  582. if (old_addr > vma->vm_start &&
  583. old_addr + old_len < vma->vm_end)
  584. split = 1;
  585. }
  586. /*
  587. * If we failed to move page tables we still do total_vm increment
  588. * since do_munmap() will decrement it by old_len == new_len.
  589. *
  590. * Since total_vm is about to be raised artificially high for a
  591. * moment, we need to restore high watermark afterwards: if stats
  592. * are taken meanwhile, total_vm and hiwater_vm appear too high.
  593. * If this were a serious issue, we'd add a flag to do_munmap().
  594. */
  595. hiwater_vm = mm->hiwater_vm;
  596. vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
  597. /* Tell pfnmap has moved from this vma */
  598. if (unlikely(vma->vm_flags & VM_PFNMAP))
  599. untrack_pfn_moved(vma);
  600. if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) {
  601. /* We always clear VM_LOCKED[ONFAULT] on the old vma */
  602. vm_flags_clear(vma, VM_LOCKED_MASK);
  603. /*
  604. * anon_vma links of the old vma is no longer needed after its page
  605. * table has been moved.
  606. */
  607. if (new_vma != vma && vma->vm_start == old_addr &&
  608. vma->vm_end == (old_addr + old_len))
  609. unlink_anon_vmas(vma);
  610. /* Because we won't unmap we don't need to touch locked_vm */
  611. return new_addr;
  612. }
  613. if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
  614. /* OOM: unable to split vma, just get accounts right */
  615. if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP))
  616. vm_acct_memory(old_len >> PAGE_SHIFT);
  617. excess = 0;
  618. }
  619. if (vm_flags & VM_LOCKED) {
  620. mm->locked_vm += new_len >> PAGE_SHIFT;
  621. *locked = true;
  622. }
  623. mm->hiwater_vm = hiwater_vm;
  624. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  625. if (excess) {
  626. vm_flags_set(vma, VM_ACCOUNT);
  627. if (split)
  628. vm_flags_set(find_vma(mm, vma->vm_end), VM_ACCOUNT);
  629. }
  630. return new_addr;
  631. }
  632. static struct vm_area_struct *vma_to_resize(unsigned long addr,
  633. unsigned long old_len, unsigned long new_len, unsigned long flags)
  634. {
  635. struct mm_struct *mm = current->mm;
  636. struct vm_area_struct *vma;
  637. unsigned long pgoff;
  638. vma = vma_lookup(mm, addr);
  639. if (!vma)
  640. return ERR_PTR(-EFAULT);
  641. /*
  642. * !old_len is a special case where an attempt is made to 'duplicate'
  643. * a mapping. This makes no sense for private mappings as it will
  644. * instead create a fresh/new mapping unrelated to the original. This
  645. * is contrary to the basic idea of mremap which creates new mappings
  646. * based on the original. There are no known use cases for this
  647. * behavior. As a result, fail such attempts.
  648. */
  649. if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
  650. pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid);
  651. return ERR_PTR(-EINVAL);
  652. }
  653. if ((flags & MREMAP_DONTUNMAP) &&
  654. (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)))
  655. return ERR_PTR(-EINVAL);
  656. /* We can't remap across vm area boundaries */
  657. if (old_len > vma->vm_end - addr)
  658. return ERR_PTR(-EFAULT);
  659. if (new_len == old_len)
  660. return vma;
  661. /* Need to be careful about a growing mapping */
  662. pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
  663. pgoff += vma->vm_pgoff;
  664. if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
  665. return ERR_PTR(-EINVAL);
  666. if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
  667. return ERR_PTR(-EFAULT);
  668. if (mlock_future_check(mm, vma->vm_flags, new_len - old_len))
  669. return ERR_PTR(-EAGAIN);
  670. if (!may_expand_vm(mm, vma->vm_flags,
  671. (new_len - old_len) >> PAGE_SHIFT))
  672. return ERR_PTR(-ENOMEM);
  673. return vma;
  674. }
  675. static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
  676. unsigned long new_addr, unsigned long new_len, bool *locked,
  677. unsigned long flags, struct vm_userfaultfd_ctx *uf,
  678. struct list_head *uf_unmap_early,
  679. struct list_head *uf_unmap)
  680. {
  681. struct mm_struct *mm = current->mm;
  682. struct vm_area_struct *vma;
  683. unsigned long ret = -EINVAL;
  684. unsigned long map_flags = 0;
  685. if (offset_in_page(new_addr))
  686. goto out;
  687. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  688. goto out;
  689. /* Ensure the old/new locations do not overlap */
  690. if (addr + old_len > new_addr && new_addr + new_len > addr)
  691. goto out;
  692. /*
  693. * move_vma() need us to stay 4 maps below the threshold, otherwise
  694. * it will bail out at the very beginning.
  695. * That is a problem if we have already unmaped the regions here
  696. * (new_addr, and old_addr), because userspace will not know the
  697. * state of the vma's after it gets -ENOMEM.
  698. * So, to avoid such scenario we can pre-compute if the whole
  699. * operation has high chances to success map-wise.
  700. * Worst-scenario case is when both vma's (new_addr and old_addr) get
  701. * split in 3 before unmapping it.
  702. * That means 2 more maps (1 for each) to the ones we already hold.
  703. * Check whether current map count plus 2 still leads us to 4 maps below
  704. * the threshold, otherwise return -ENOMEM here to be more safe.
  705. */
  706. if ((mm->map_count + 2) >= sysctl_max_map_count - 3)
  707. return -ENOMEM;
  708. if (flags & MREMAP_FIXED) {
  709. ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
  710. if (ret)
  711. goto out;
  712. }
  713. if (old_len > new_len) {
  714. ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
  715. if (ret)
  716. goto out;
  717. old_len = new_len;
  718. }
  719. vma = vma_to_resize(addr, old_len, new_len, flags);
  720. if (IS_ERR(vma)) {
  721. ret = PTR_ERR(vma);
  722. goto out;
  723. }
  724. /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */
  725. if (flags & MREMAP_DONTUNMAP &&
  726. !may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) {
  727. ret = -ENOMEM;
  728. goto out;
  729. }
  730. if (flags & MREMAP_FIXED)
  731. map_flags |= MAP_FIXED;
  732. if (vma->vm_flags & VM_MAYSHARE)
  733. map_flags |= MAP_SHARED;
  734. ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
  735. ((addr - vma->vm_start) >> PAGE_SHIFT),
  736. map_flags);
  737. if (IS_ERR_VALUE(ret))
  738. goto out;
  739. /* We got a new mapping */
  740. if (!(flags & MREMAP_FIXED))
  741. new_addr = ret;
  742. ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf,
  743. uf_unmap);
  744. out:
  745. return ret;
  746. }
  747. static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
  748. {
  749. unsigned long end = vma->vm_end + delta;
  750. if (end < vma->vm_end) /* overflow */
  751. return 0;
  752. if (find_vma_intersection(vma->vm_mm, vma->vm_end, end))
  753. return 0;
  754. if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
  755. 0, MAP_FIXED) & ~PAGE_MASK)
  756. return 0;
  757. return 1;
  758. }
  759. /*
  760. * Expand (or shrink) an existing mapping, potentially moving it at the
  761. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  762. *
  763. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  764. * This option implies MREMAP_MAYMOVE.
  765. */
  766. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  767. unsigned long, new_len, unsigned long, flags,
  768. unsigned long, new_addr)
  769. {
  770. struct mm_struct *mm = current->mm;
  771. struct vm_area_struct *vma;
  772. unsigned long ret = -EINVAL;
  773. bool locked = false;
  774. bool downgraded = false;
  775. struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
  776. LIST_HEAD(uf_unmap_early);
  777. LIST_HEAD(uf_unmap);
  778. /*
  779. * There is a deliberate asymmetry here: we strip the pointer tag
  780. * from the old address but leave the new address alone. This is
  781. * for consistency with mmap(), where we prevent the creation of
  782. * aliasing mappings in userspace by leaving the tag bits of the
  783. * mapping address intact. A non-zero tag will cause the subsequent
  784. * range checks to reject the address as invalid.
  785. *
  786. * See Documentation/arm64/tagged-address-abi.rst for more information.
  787. */
  788. addr = untagged_addr(addr);
  789. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP))
  790. return ret;
  791. if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
  792. return ret;
  793. /*
  794. * MREMAP_DONTUNMAP is always a move and it does not allow resizing
  795. * in the process.
  796. */
  797. if (flags & MREMAP_DONTUNMAP &&
  798. (!(flags & MREMAP_MAYMOVE) || old_len != new_len))
  799. return ret;
  800. if (offset_in_page(addr))
  801. return ret;
  802. old_len = PAGE_ALIGN(old_len);
  803. new_len = PAGE_ALIGN(new_len);
  804. /*
  805. * We allow a zero old-len as a special case
  806. * for DOS-emu "duplicate shm area" thing. But
  807. * a zero new-len is nonsensical.
  808. */
  809. if (!new_len)
  810. return ret;
  811. if (mmap_write_lock_killable(current->mm))
  812. return -EINTR;
  813. vma = vma_lookup(mm, addr);
  814. if (!vma) {
  815. ret = -EFAULT;
  816. goto out;
  817. }
  818. if (is_vm_hugetlb_page(vma)) {
  819. struct hstate *h __maybe_unused = hstate_vma(vma);
  820. old_len = ALIGN(old_len, huge_page_size(h));
  821. new_len = ALIGN(new_len, huge_page_size(h));
  822. /* addrs must be huge page aligned */
  823. if (addr & ~huge_page_mask(h))
  824. goto out;
  825. if (new_addr & ~huge_page_mask(h))
  826. goto out;
  827. /*
  828. * Don't allow remap expansion, because the underlying hugetlb
  829. * reservation is not yet capable to handle split reservation.
  830. */
  831. if (new_len > old_len)
  832. goto out;
  833. }
  834. if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) {
  835. ret = mremap_to(addr, old_len, new_addr, new_len,
  836. &locked, flags, &uf, &uf_unmap_early,
  837. &uf_unmap);
  838. goto out;
  839. }
  840. /*
  841. * Always allow a shrinking remap: that just unmaps
  842. * the unnecessary pages..
  843. * do_mas_munmap does all the needed commit accounting, and
  844. * downgrades mmap_lock to read if so directed.
  845. */
  846. if (old_len >= new_len) {
  847. int retval;
  848. MA_STATE(mas, &mm->mm_mt, addr + new_len, addr + new_len);
  849. retval = do_mas_munmap(&mas, mm, addr + new_len,
  850. old_len - new_len, &uf_unmap, true);
  851. /* Returning 1 indicates mmap_lock is downgraded to read. */
  852. if (retval == 1) {
  853. downgraded = true;
  854. } else if (retval < 0 && old_len != new_len) {
  855. ret = retval;
  856. goto out;
  857. }
  858. ret = addr;
  859. goto out;
  860. }
  861. /*
  862. * Ok, we need to grow..
  863. */
  864. vma = vma_to_resize(addr, old_len, new_len, flags);
  865. if (IS_ERR(vma)) {
  866. ret = PTR_ERR(vma);
  867. goto out;
  868. }
  869. /* old_len exactly to the end of the area..
  870. */
  871. if (old_len == vma->vm_end - addr) {
  872. /* can we just expand the current mapping? */
  873. if (vma_expandable(vma, new_len - old_len)) {
  874. long pages = (new_len - old_len) >> PAGE_SHIFT;
  875. unsigned long extension_start = addr + old_len;
  876. unsigned long extension_end = addr + new_len;
  877. pgoff_t extension_pgoff = vma->vm_pgoff +
  878. ((extension_start - vma->vm_start) >> PAGE_SHIFT);
  879. if (vma->vm_flags & VM_ACCOUNT) {
  880. if (security_vm_enough_memory_mm(mm, pages)) {
  881. ret = -ENOMEM;
  882. goto out;
  883. }
  884. }
  885. /*
  886. * Function vma_merge() is called on the extension we
  887. * are adding to the already existing vma, vma_merge()
  888. * will merge this extension with the already existing
  889. * vma (expand operation itself) and possibly also with
  890. * the next vma if it becomes adjacent to the expanded
  891. * vma and otherwise compatible.
  892. *
  893. * However, vma_merge() can currently fail due to
  894. * is_mergeable_vma() check for vm_ops->close (see the
  895. * comment there). Yet this should not prevent vma
  896. * expanding, so perform a simple expand for such vma.
  897. * Ideally the check for close op should be only done
  898. * when a vma would be actually removed due to a merge.
  899. */
  900. if (!vma->vm_ops || !vma->vm_ops->close) {
  901. vma = vma_merge(mm, vma, extension_start, extension_end,
  902. vma->vm_flags, vma->anon_vma, vma->vm_file,
  903. extension_pgoff, vma_policy(vma),
  904. vma->vm_userfaultfd_ctx, anon_vma_name(vma));
  905. } else if (vma_adjust(vma, vma->vm_start, addr + new_len,
  906. vma->vm_pgoff, NULL)) {
  907. vma = NULL;
  908. }
  909. if (!vma) {
  910. vm_unacct_memory(pages);
  911. ret = -ENOMEM;
  912. goto out;
  913. }
  914. vm_stat_account(mm, vma->vm_flags, pages);
  915. if (vma->vm_flags & VM_LOCKED) {
  916. mm->locked_vm += pages;
  917. locked = true;
  918. new_addr = addr;
  919. }
  920. ret = addr;
  921. goto out;
  922. }
  923. }
  924. /*
  925. * We weren't able to just expand or shrink the area,
  926. * we need to create a new one and move it..
  927. */
  928. ret = -ENOMEM;
  929. if (flags & MREMAP_MAYMOVE) {
  930. unsigned long map_flags = 0;
  931. if (vma->vm_flags & VM_MAYSHARE)
  932. map_flags |= MAP_SHARED;
  933. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  934. vma->vm_pgoff +
  935. ((addr - vma->vm_start) >> PAGE_SHIFT),
  936. map_flags);
  937. if (IS_ERR_VALUE(new_addr)) {
  938. ret = new_addr;
  939. goto out;
  940. }
  941. ret = move_vma(vma, addr, old_len, new_len, new_addr,
  942. &locked, flags, &uf, &uf_unmap);
  943. }
  944. out:
  945. if (offset_in_page(ret))
  946. locked = false;
  947. if (downgraded)
  948. mmap_read_unlock(current->mm);
  949. else
  950. mmap_write_unlock(current->mm);
  951. if (locked && new_len > old_len)
  952. mm_populate(new_addr + old_len, new_len - old_len);
  953. userfaultfd_unmap_complete(mm, &uf_unmap_early);
  954. mremap_userfaultfd_complete(&uf, addr, ret, old_len);
  955. userfaultfd_unmap_complete(mm, &uf_unmap);
  956. return ret;
  957. }