mremap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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/hugetlb.h>
  12. #include <linux/shm.h>
  13. #include <linux/ksm.h>
  14. #include <linux/mman.h>
  15. #include <linux/swap.h>
  16. #include <linux/capability.h>
  17. #include <linux/fs.h>
  18. #include <linux/swapops.h>
  19. #include <linux/highmem.h>
  20. #include <linux/security.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/mmu_notifier.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/mm-arch-hooks.h>
  25. #include <linux/userfaultfd_k.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/tlbflush.h>
  28. #include "internal.h"
  29. static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
  30. {
  31. pgd_t *pgd;
  32. p4d_t *p4d;
  33. pud_t *pud;
  34. pmd_t *pmd;
  35. pgd = pgd_offset(mm, addr);
  36. if (pgd_none_or_clear_bad(pgd))
  37. return NULL;
  38. p4d = p4d_offset(pgd, addr);
  39. if (p4d_none_or_clear_bad(p4d))
  40. return NULL;
  41. pud = pud_offset(p4d, addr);
  42. if (pud_none_or_clear_bad(pud))
  43. return NULL;
  44. pmd = pmd_offset(pud, addr);
  45. if (pmd_none(*pmd))
  46. return NULL;
  47. return pmd;
  48. }
  49. static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
  50. unsigned long addr)
  51. {
  52. pgd_t *pgd;
  53. p4d_t *p4d;
  54. pud_t *pud;
  55. pmd_t *pmd;
  56. pgd = pgd_offset(mm, addr);
  57. p4d = p4d_alloc(mm, pgd, addr);
  58. if (!p4d)
  59. return NULL;
  60. pud = pud_alloc(mm, p4d, addr);
  61. if (!pud)
  62. return NULL;
  63. pmd = pmd_alloc(mm, pud, addr);
  64. if (!pmd)
  65. return NULL;
  66. VM_BUG_ON(pmd_trans_huge(*pmd));
  67. return pmd;
  68. }
  69. static void take_rmap_locks(struct vm_area_struct *vma)
  70. {
  71. if (vma->vm_file)
  72. i_mmap_lock_write(vma->vm_file->f_mapping);
  73. if (vma->anon_vma)
  74. anon_vma_lock_write(vma->anon_vma);
  75. }
  76. static void drop_rmap_locks(struct vm_area_struct *vma)
  77. {
  78. if (vma->anon_vma)
  79. anon_vma_unlock_write(vma->anon_vma);
  80. if (vma->vm_file)
  81. i_mmap_unlock_write(vma->vm_file->f_mapping);
  82. }
  83. static pte_t move_soft_dirty_pte(pte_t pte)
  84. {
  85. /*
  86. * Set soft dirty bit so we can notice
  87. * in userspace the ptes were moved.
  88. */
  89. #ifdef CONFIG_MEM_SOFT_DIRTY
  90. if (pte_present(pte))
  91. pte = pte_mksoft_dirty(pte);
  92. else if (is_swap_pte(pte))
  93. pte = pte_swp_mksoft_dirty(pte);
  94. #endif
  95. return pte;
  96. }
  97. static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
  98. unsigned long old_addr, unsigned long old_end,
  99. struct vm_area_struct *new_vma, pmd_t *new_pmd,
  100. unsigned long new_addr, bool need_rmap_locks, bool *need_flush)
  101. {
  102. struct mm_struct *mm = vma->vm_mm;
  103. pte_t *old_pte, *new_pte, pte;
  104. spinlock_t *old_ptl, *new_ptl;
  105. bool force_flush = false;
  106. unsigned long len = old_end - old_addr;
  107. /*
  108. * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
  109. * locks to ensure that rmap will always observe either the old or the
  110. * new ptes. This is the easiest way to avoid races with
  111. * truncate_pagecache(), page migration, etc...
  112. *
  113. * When need_rmap_locks is false, we use other ways to avoid
  114. * such races:
  115. *
  116. * - During exec() shift_arg_pages(), we use a specially tagged vma
  117. * which rmap call sites look for using is_vma_temporary_stack().
  118. *
  119. * - During mremap(), new_vma is often known to be placed after vma
  120. * in rmap traversal order. This ensures rmap will always observe
  121. * either the old pte, or the new pte, or both (the page table locks
  122. * serialize access to individual ptes, but only rmap traversal
  123. * order guarantees that we won't miss both the old and new ptes).
  124. */
  125. if (need_rmap_locks)
  126. take_rmap_locks(vma);
  127. /*
  128. * We don't have to worry about the ordering of src and dst
  129. * pte locks because exclusive mmap_sem prevents deadlock.
  130. */
  131. old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
  132. new_pte = pte_offset_map(new_pmd, new_addr);
  133. new_ptl = pte_lockptr(mm, new_pmd);
  134. if (new_ptl != old_ptl)
  135. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  136. flush_tlb_batched_pending(vma->vm_mm);
  137. arch_enter_lazy_mmu_mode();
  138. for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
  139. new_pte++, new_addr += PAGE_SIZE) {
  140. if (pte_none(*old_pte))
  141. continue;
  142. pte = ptep_get_and_clear(mm, old_addr, old_pte);
  143. /*
  144. * If we are remapping a dirty PTE, make sure
  145. * to flush TLB before we drop the PTL for the
  146. * old PTE or we may race with page_mkclean().
  147. *
  148. * This check has to be done after we removed the
  149. * old PTE from page tables or another thread may
  150. * dirty it after the check and before the removal.
  151. */
  152. if (pte_present(pte) && pte_dirty(pte))
  153. force_flush = true;
  154. pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
  155. pte = move_soft_dirty_pte(pte);
  156. set_pte_at(mm, new_addr, new_pte, pte);
  157. }
  158. arch_leave_lazy_mmu_mode();
  159. if (new_ptl != old_ptl)
  160. spin_unlock(new_ptl);
  161. pte_unmap(new_pte - 1);
  162. if (force_flush)
  163. flush_tlb_range(vma, old_end - len, old_end);
  164. else
  165. *need_flush = true;
  166. pte_unmap_unlock(old_pte - 1, old_ptl);
  167. if (need_rmap_locks)
  168. drop_rmap_locks(vma);
  169. }
  170. unsigned long move_page_tables(struct vm_area_struct *vma,
  171. unsigned long old_addr, struct vm_area_struct *new_vma,
  172. unsigned long new_addr, unsigned long len,
  173. bool need_rmap_locks)
  174. {
  175. unsigned long extent, next, old_end;
  176. pmd_t *old_pmd, *new_pmd;
  177. bool need_flush = false;
  178. unsigned long mmun_start; /* For mmu_notifiers */
  179. unsigned long mmun_end; /* For mmu_notifiers */
  180. old_end = old_addr + len;
  181. flush_cache_range(vma, old_addr, old_end);
  182. mmun_start = old_addr;
  183. mmun_end = old_end;
  184. mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
  185. for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
  186. cond_resched();
  187. next = (old_addr + PMD_SIZE) & PMD_MASK;
  188. /* even if next overflowed, extent below will be ok */
  189. extent = next - old_addr;
  190. if (extent > old_end - old_addr)
  191. extent = old_end - old_addr;
  192. old_pmd = get_old_pmd(vma->vm_mm, old_addr);
  193. if (!old_pmd)
  194. continue;
  195. new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
  196. if (!new_pmd)
  197. break;
  198. if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd)) {
  199. if (extent == HPAGE_PMD_SIZE) {
  200. bool moved;
  201. /* See comment in move_ptes() */
  202. if (need_rmap_locks)
  203. take_rmap_locks(vma);
  204. moved = move_huge_pmd(vma, old_addr, new_addr,
  205. old_end, old_pmd, new_pmd,
  206. &need_flush);
  207. if (need_rmap_locks)
  208. drop_rmap_locks(vma);
  209. if (moved)
  210. continue;
  211. }
  212. split_huge_pmd(vma, old_pmd, old_addr);
  213. if (pmd_trans_unstable(old_pmd))
  214. continue;
  215. }
  216. if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
  217. break;
  218. next = (new_addr + PMD_SIZE) & PMD_MASK;
  219. if (extent > next - new_addr)
  220. extent = next - new_addr;
  221. move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
  222. new_pmd, new_addr, need_rmap_locks, &need_flush);
  223. }
  224. if (need_flush)
  225. flush_tlb_range(vma, old_end-len, old_addr);
  226. mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
  227. return len + old_addr - old_end; /* how much done */
  228. }
  229. static unsigned long move_vma(struct vm_area_struct *vma,
  230. unsigned long old_addr, unsigned long old_len,
  231. unsigned long new_len, unsigned long new_addr,
  232. bool *locked, struct vm_userfaultfd_ctx *uf,
  233. struct list_head *uf_unmap)
  234. {
  235. struct mm_struct *mm = vma->vm_mm;
  236. struct vm_area_struct *new_vma;
  237. unsigned long vm_flags = vma->vm_flags;
  238. unsigned long new_pgoff;
  239. unsigned long moved_len;
  240. unsigned long excess = 0;
  241. unsigned long hiwater_vm;
  242. int split = 0;
  243. int err;
  244. bool need_rmap_locks;
  245. /*
  246. * We'd prefer to avoid failure later on in do_munmap:
  247. * which may split one vma into three before unmapping.
  248. */
  249. if (mm->map_count >= sysctl_max_map_count - 3)
  250. return -ENOMEM;
  251. /*
  252. * Advise KSM to break any KSM pages in the area to be moved:
  253. * it would be confusing if they were to turn up at the new
  254. * location, where they happen to coincide with different KSM
  255. * pages recently unmapped. But leave vma->vm_flags as it was,
  256. * so KSM can come around to merge on vma and new_vma afterwards.
  257. */
  258. err = ksm_madvise(vma, old_addr, old_addr + old_len,
  259. MADV_UNMERGEABLE, &vm_flags);
  260. if (err)
  261. return err;
  262. new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
  263. new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
  264. &need_rmap_locks);
  265. if (!new_vma)
  266. return -ENOMEM;
  267. moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
  268. need_rmap_locks);
  269. if (moved_len < old_len) {
  270. err = -ENOMEM;
  271. } else if (vma->vm_ops && vma->vm_ops->mremap) {
  272. err = vma->vm_ops->mremap(new_vma);
  273. }
  274. if (unlikely(err)) {
  275. /*
  276. * On error, move entries back from new area to old,
  277. * which will succeed since page tables still there,
  278. * and then proceed to unmap new area instead of old.
  279. */
  280. move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
  281. true);
  282. vma = new_vma;
  283. old_len = new_len;
  284. old_addr = new_addr;
  285. new_addr = err;
  286. } else {
  287. mremap_userfaultfd_prep(new_vma, uf);
  288. arch_remap(mm, old_addr, old_addr + old_len,
  289. new_addr, new_addr + new_len);
  290. }
  291. /* Conceal VM_ACCOUNT so old reservation is not undone */
  292. if (vm_flags & VM_ACCOUNT) {
  293. vma->vm_flags &= ~VM_ACCOUNT;
  294. excess = vma->vm_end - vma->vm_start - old_len;
  295. if (old_addr > vma->vm_start &&
  296. old_addr + old_len < vma->vm_end)
  297. split = 1;
  298. }
  299. /*
  300. * If we failed to move page tables we still do total_vm increment
  301. * since do_munmap() will decrement it by old_len == new_len.
  302. *
  303. * Since total_vm is about to be raised artificially high for a
  304. * moment, we need to restore high watermark afterwards: if stats
  305. * are taken meanwhile, total_vm and hiwater_vm appear too high.
  306. * If this were a serious issue, we'd add a flag to do_munmap().
  307. */
  308. hiwater_vm = mm->hiwater_vm;
  309. vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
  310. /* Tell pfnmap has moved from this vma */
  311. if (unlikely(vma->vm_flags & VM_PFNMAP))
  312. untrack_pfn_moved(vma);
  313. if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
  314. /* OOM: unable to split vma, just get accounts right */
  315. vm_unacct_memory(excess >> PAGE_SHIFT);
  316. excess = 0;
  317. }
  318. mm->hiwater_vm = hiwater_vm;
  319. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  320. if (excess) {
  321. vma->vm_flags |= VM_ACCOUNT;
  322. if (split)
  323. vma->vm_next->vm_flags |= VM_ACCOUNT;
  324. }
  325. if (vm_flags & VM_LOCKED) {
  326. mm->locked_vm += new_len >> PAGE_SHIFT;
  327. *locked = true;
  328. }
  329. return new_addr;
  330. }
  331. static struct vm_area_struct *vma_to_resize(unsigned long addr,
  332. unsigned long old_len, unsigned long new_len, unsigned long *p)
  333. {
  334. struct mm_struct *mm = current->mm;
  335. struct vm_area_struct *vma = find_vma(mm, addr);
  336. unsigned long pgoff;
  337. if (!vma || vma->vm_start > addr)
  338. return ERR_PTR(-EFAULT);
  339. /*
  340. * !old_len is a special case where an attempt is made to 'duplicate'
  341. * a mapping. This makes no sense for private mappings as it will
  342. * instead create a fresh/new mapping unrelated to the original. This
  343. * is contrary to the basic idea of mremap which creates new mappings
  344. * based on the original. There are no known use cases for this
  345. * behavior. As a result, fail such attempts.
  346. */
  347. if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
  348. pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid);
  349. return ERR_PTR(-EINVAL);
  350. }
  351. if (is_vm_hugetlb_page(vma))
  352. return ERR_PTR(-EINVAL);
  353. /* We can't remap across vm area boundaries */
  354. if (old_len > vma->vm_end - addr)
  355. return ERR_PTR(-EFAULT);
  356. if (new_len == old_len)
  357. return vma;
  358. /* Need to be careful about a growing mapping */
  359. pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
  360. pgoff += vma->vm_pgoff;
  361. if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
  362. return ERR_PTR(-EINVAL);
  363. if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
  364. return ERR_PTR(-EFAULT);
  365. if (vma->vm_flags & VM_LOCKED) {
  366. unsigned long locked, lock_limit;
  367. locked = mm->locked_vm << PAGE_SHIFT;
  368. lock_limit = rlimit(RLIMIT_MEMLOCK);
  369. locked += new_len - old_len;
  370. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  371. return ERR_PTR(-EAGAIN);
  372. }
  373. if (!may_expand_vm(mm, vma->vm_flags,
  374. (new_len - old_len) >> PAGE_SHIFT))
  375. return ERR_PTR(-ENOMEM);
  376. if (vma->vm_flags & VM_ACCOUNT) {
  377. unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
  378. if (security_vm_enough_memory_mm(mm, charged))
  379. return ERR_PTR(-ENOMEM);
  380. *p = charged;
  381. }
  382. return vma;
  383. }
  384. static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
  385. unsigned long new_addr, unsigned long new_len, bool *locked,
  386. struct vm_userfaultfd_ctx *uf,
  387. struct list_head *uf_unmap_early,
  388. struct list_head *uf_unmap)
  389. {
  390. struct mm_struct *mm = current->mm;
  391. struct vm_area_struct *vma;
  392. unsigned long ret = -EINVAL;
  393. unsigned long charged = 0;
  394. unsigned long map_flags;
  395. if (offset_in_page(new_addr))
  396. goto out;
  397. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  398. goto out;
  399. /* Ensure the old/new locations do not overlap */
  400. if (addr + old_len > new_addr && new_addr + new_len > addr)
  401. goto out;
  402. ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
  403. if (ret)
  404. goto out;
  405. if (old_len >= new_len) {
  406. ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
  407. if (ret && old_len != new_len)
  408. goto out;
  409. old_len = new_len;
  410. }
  411. vma = vma_to_resize(addr, old_len, new_len, &charged);
  412. if (IS_ERR(vma)) {
  413. ret = PTR_ERR(vma);
  414. goto out;
  415. }
  416. map_flags = MAP_FIXED;
  417. if (vma->vm_flags & VM_MAYSHARE)
  418. map_flags |= MAP_SHARED;
  419. ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
  420. ((addr - vma->vm_start) >> PAGE_SHIFT),
  421. map_flags);
  422. if (offset_in_page(ret))
  423. goto out1;
  424. ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf,
  425. uf_unmap);
  426. if (!(offset_in_page(ret)))
  427. goto out;
  428. out1:
  429. vm_unacct_memory(charged);
  430. out:
  431. return ret;
  432. }
  433. static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
  434. {
  435. unsigned long end = vma->vm_end + delta;
  436. if (end < vma->vm_end) /* overflow */
  437. return 0;
  438. if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
  439. return 0;
  440. if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
  441. 0, MAP_FIXED) & ~PAGE_MASK)
  442. return 0;
  443. return 1;
  444. }
  445. /*
  446. * Expand (or shrink) an existing mapping, potentially moving it at the
  447. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  448. *
  449. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  450. * This option implies MREMAP_MAYMOVE.
  451. */
  452. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  453. unsigned long, new_len, unsigned long, flags,
  454. unsigned long, new_addr)
  455. {
  456. struct mm_struct *mm = current->mm;
  457. struct vm_area_struct *vma;
  458. unsigned long ret = -EINVAL;
  459. unsigned long charged = 0;
  460. bool locked = false;
  461. struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
  462. LIST_HEAD(uf_unmap_early);
  463. LIST_HEAD(uf_unmap);
  464. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
  465. return ret;
  466. if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
  467. return ret;
  468. if (offset_in_page(addr))
  469. return ret;
  470. old_len = PAGE_ALIGN(old_len);
  471. new_len = PAGE_ALIGN(new_len);
  472. /*
  473. * We allow a zero old-len as a special case
  474. * for DOS-emu "duplicate shm area" thing. But
  475. * a zero new-len is nonsensical.
  476. */
  477. if (!new_len)
  478. return ret;
  479. if (down_write_killable(&current->mm->mmap_sem))
  480. return -EINTR;
  481. if (flags & MREMAP_FIXED) {
  482. ret = mremap_to(addr, old_len, new_addr, new_len,
  483. &locked, &uf, &uf_unmap_early, &uf_unmap);
  484. goto out;
  485. }
  486. /*
  487. * Always allow a shrinking remap: that just unmaps
  488. * the unnecessary pages..
  489. * do_munmap does all the needed commit accounting
  490. */
  491. if (old_len >= new_len) {
  492. ret = do_munmap(mm, addr+new_len, old_len - new_len, &uf_unmap);
  493. if (ret && old_len != new_len)
  494. goto out;
  495. ret = addr;
  496. goto out;
  497. }
  498. /*
  499. * Ok, we need to grow..
  500. */
  501. vma = vma_to_resize(addr, old_len, new_len, &charged);
  502. if (IS_ERR(vma)) {
  503. ret = PTR_ERR(vma);
  504. goto out;
  505. }
  506. /* old_len exactly to the end of the area..
  507. */
  508. if (old_len == vma->vm_end - addr) {
  509. /* can we just expand the current mapping? */
  510. if (vma_expandable(vma, new_len - old_len)) {
  511. int pages = (new_len - old_len) >> PAGE_SHIFT;
  512. if (vma_adjust(vma, vma->vm_start, addr + new_len,
  513. vma->vm_pgoff, NULL)) {
  514. ret = -ENOMEM;
  515. goto out;
  516. }
  517. vm_stat_account(mm, vma->vm_flags, pages);
  518. if (vma->vm_flags & VM_LOCKED) {
  519. mm->locked_vm += pages;
  520. locked = true;
  521. new_addr = addr;
  522. }
  523. ret = addr;
  524. goto out;
  525. }
  526. }
  527. /*
  528. * We weren't able to just expand or shrink the area,
  529. * we need to create a new one and move it..
  530. */
  531. ret = -ENOMEM;
  532. if (flags & MREMAP_MAYMOVE) {
  533. unsigned long map_flags = 0;
  534. if (vma->vm_flags & VM_MAYSHARE)
  535. map_flags |= MAP_SHARED;
  536. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  537. vma->vm_pgoff +
  538. ((addr - vma->vm_start) >> PAGE_SHIFT),
  539. map_flags);
  540. if (offset_in_page(new_addr)) {
  541. ret = new_addr;
  542. goto out;
  543. }
  544. ret = move_vma(vma, addr, old_len, new_len, new_addr,
  545. &locked, &uf, &uf_unmap);
  546. }
  547. out:
  548. if (offset_in_page(ret)) {
  549. vm_unacct_memory(charged);
  550. locked = 0;
  551. }
  552. up_write(&current->mm->mmap_sem);
  553. if (locked && new_len > old_len)
  554. mm_populate(new_addr + old_len, new_len - old_len);
  555. userfaultfd_unmap_complete(mm, &uf_unmap_early);
  556. mremap_userfaultfd_complete(&uf, addr, new_addr, old_len);
  557. userfaultfd_unmap_complete(mm, &uf_unmap);
  558. return ret;
  559. }