mremap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. #define LATENCY_LIMIT (64 * PAGE_SIZE)
  171. unsigned long move_page_tables(struct vm_area_struct *vma,
  172. unsigned long old_addr, struct vm_area_struct *new_vma,
  173. unsigned long new_addr, unsigned long len,
  174. bool need_rmap_locks)
  175. {
  176. unsigned long extent, next, old_end;
  177. pmd_t *old_pmd, *new_pmd;
  178. bool need_flush = false;
  179. unsigned long mmun_start; /* For mmu_notifiers */
  180. unsigned long mmun_end; /* For mmu_notifiers */
  181. old_end = old_addr + len;
  182. flush_cache_range(vma, old_addr, old_end);
  183. mmun_start = old_addr;
  184. mmun_end = old_end;
  185. mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
  186. for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
  187. cond_resched();
  188. next = (old_addr + PMD_SIZE) & PMD_MASK;
  189. /* even if next overflowed, extent below will be ok */
  190. extent = next - old_addr;
  191. if (extent > old_end - old_addr)
  192. extent = old_end - old_addr;
  193. old_pmd = get_old_pmd(vma->vm_mm, old_addr);
  194. if (!old_pmd)
  195. continue;
  196. new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
  197. if (!new_pmd)
  198. break;
  199. if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd)) {
  200. if (extent == HPAGE_PMD_SIZE) {
  201. bool moved;
  202. /* See comment in move_ptes() */
  203. if (need_rmap_locks)
  204. take_rmap_locks(vma);
  205. moved = move_huge_pmd(vma, old_addr, new_addr,
  206. old_end, old_pmd, new_pmd,
  207. &need_flush);
  208. if (need_rmap_locks)
  209. drop_rmap_locks(vma);
  210. if (moved)
  211. continue;
  212. }
  213. split_huge_pmd(vma, old_pmd, old_addr);
  214. if (pmd_trans_unstable(old_pmd))
  215. continue;
  216. }
  217. if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
  218. break;
  219. next = (new_addr + PMD_SIZE) & PMD_MASK;
  220. if (extent > next - new_addr)
  221. extent = next - new_addr;
  222. if (extent > LATENCY_LIMIT)
  223. extent = LATENCY_LIMIT;
  224. move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
  225. new_pmd, new_addr, need_rmap_locks, &need_flush);
  226. }
  227. if (need_flush)
  228. flush_tlb_range(vma, old_end-len, old_addr);
  229. mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
  230. return len + old_addr - old_end; /* how much done */
  231. }
  232. static unsigned long move_vma(struct vm_area_struct *vma,
  233. unsigned long old_addr, unsigned long old_len,
  234. unsigned long new_len, unsigned long new_addr,
  235. bool *locked, struct vm_userfaultfd_ctx *uf,
  236. struct list_head *uf_unmap)
  237. {
  238. struct mm_struct *mm = vma->vm_mm;
  239. struct vm_area_struct *new_vma;
  240. unsigned long vm_flags = vma->vm_flags;
  241. unsigned long new_pgoff;
  242. unsigned long moved_len;
  243. unsigned long excess = 0;
  244. unsigned long hiwater_vm;
  245. int split = 0;
  246. int err;
  247. bool need_rmap_locks;
  248. /*
  249. * We'd prefer to avoid failure later on in do_munmap:
  250. * which may split one vma into three before unmapping.
  251. */
  252. if (mm->map_count >= sysctl_max_map_count - 3)
  253. return -ENOMEM;
  254. /*
  255. * Advise KSM to break any KSM pages in the area to be moved:
  256. * it would be confusing if they were to turn up at the new
  257. * location, where they happen to coincide with different KSM
  258. * pages recently unmapped. But leave vma->vm_flags as it was,
  259. * so KSM can come around to merge on vma and new_vma afterwards.
  260. */
  261. err = ksm_madvise(vma, old_addr, old_addr + old_len,
  262. MADV_UNMERGEABLE, &vm_flags);
  263. if (err)
  264. return err;
  265. new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
  266. new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
  267. &need_rmap_locks);
  268. if (!new_vma)
  269. return -ENOMEM;
  270. moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
  271. need_rmap_locks);
  272. if (moved_len < old_len) {
  273. err = -ENOMEM;
  274. } else if (vma->vm_ops && vma->vm_ops->mremap) {
  275. err = vma->vm_ops->mremap(new_vma);
  276. }
  277. if (unlikely(err)) {
  278. /*
  279. * On error, move entries back from new area to old,
  280. * which will succeed since page tables still there,
  281. * and then proceed to unmap new area instead of old.
  282. */
  283. move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
  284. true);
  285. vma = new_vma;
  286. old_len = new_len;
  287. old_addr = new_addr;
  288. new_addr = err;
  289. } else {
  290. mremap_userfaultfd_prep(new_vma, uf);
  291. arch_remap(mm, old_addr, old_addr + old_len,
  292. new_addr, new_addr + new_len);
  293. }
  294. /* Conceal VM_ACCOUNT so old reservation is not undone */
  295. if (vm_flags & VM_ACCOUNT) {
  296. vma->vm_flags &= ~VM_ACCOUNT;
  297. excess = vma->vm_end - vma->vm_start - old_len;
  298. if (old_addr > vma->vm_start &&
  299. old_addr + old_len < vma->vm_end)
  300. split = 1;
  301. }
  302. /*
  303. * If we failed to move page tables we still do total_vm increment
  304. * since do_munmap() will decrement it by old_len == new_len.
  305. *
  306. * Since total_vm is about to be raised artificially high for a
  307. * moment, we need to restore high watermark afterwards: if stats
  308. * are taken meanwhile, total_vm and hiwater_vm appear too high.
  309. * If this were a serious issue, we'd add a flag to do_munmap().
  310. */
  311. hiwater_vm = mm->hiwater_vm;
  312. vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
  313. /* Tell pfnmap has moved from this vma */
  314. if (unlikely(vma->vm_flags & VM_PFNMAP))
  315. untrack_pfn_moved(vma);
  316. if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
  317. /* OOM: unable to split vma, just get accounts right */
  318. vm_unacct_memory(excess >> PAGE_SHIFT);
  319. excess = 0;
  320. }
  321. mm->hiwater_vm = hiwater_vm;
  322. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  323. if (excess) {
  324. vma->vm_flags |= VM_ACCOUNT;
  325. if (split)
  326. vma->vm_next->vm_flags |= VM_ACCOUNT;
  327. }
  328. if (vm_flags & VM_LOCKED) {
  329. mm->locked_vm += new_len >> PAGE_SHIFT;
  330. *locked = true;
  331. }
  332. return new_addr;
  333. }
  334. static struct vm_area_struct *vma_to_resize(unsigned long addr,
  335. unsigned long old_len, unsigned long new_len, unsigned long *p)
  336. {
  337. struct mm_struct *mm = current->mm;
  338. struct vm_area_struct *vma = find_vma(mm, addr);
  339. unsigned long pgoff;
  340. if (!vma || vma->vm_start > addr)
  341. return ERR_PTR(-EFAULT);
  342. /*
  343. * !old_len is a special case where an attempt is made to 'duplicate'
  344. * a mapping. This makes no sense for private mappings as it will
  345. * instead create a fresh/new mapping unrelated to the original. This
  346. * is contrary to the basic idea of mremap which creates new mappings
  347. * based on the original. There are no known use cases for this
  348. * behavior. As a result, fail such attempts.
  349. */
  350. if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
  351. pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid);
  352. return ERR_PTR(-EINVAL);
  353. }
  354. if (is_vm_hugetlb_page(vma))
  355. return ERR_PTR(-EINVAL);
  356. /* We can't remap across vm area boundaries */
  357. if (old_len > vma->vm_end - addr)
  358. return ERR_PTR(-EFAULT);
  359. if (new_len == old_len)
  360. return vma;
  361. /* Need to be careful about a growing mapping */
  362. pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
  363. pgoff += vma->vm_pgoff;
  364. if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
  365. return ERR_PTR(-EINVAL);
  366. if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
  367. return ERR_PTR(-EFAULT);
  368. if (vma->vm_flags & VM_LOCKED) {
  369. unsigned long locked, lock_limit;
  370. locked = mm->locked_vm << PAGE_SHIFT;
  371. lock_limit = rlimit(RLIMIT_MEMLOCK);
  372. locked += new_len - old_len;
  373. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  374. return ERR_PTR(-EAGAIN);
  375. }
  376. if (!may_expand_vm(mm, vma->vm_flags,
  377. (new_len - old_len) >> PAGE_SHIFT))
  378. return ERR_PTR(-ENOMEM);
  379. if (vma->vm_flags & VM_ACCOUNT) {
  380. unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
  381. if (security_vm_enough_memory_mm(mm, charged))
  382. return ERR_PTR(-ENOMEM);
  383. *p = charged;
  384. }
  385. return vma;
  386. }
  387. static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
  388. unsigned long new_addr, unsigned long new_len, bool *locked,
  389. struct vm_userfaultfd_ctx *uf,
  390. struct list_head *uf_unmap_early,
  391. struct list_head *uf_unmap)
  392. {
  393. struct mm_struct *mm = current->mm;
  394. struct vm_area_struct *vma;
  395. unsigned long ret = -EINVAL;
  396. unsigned long charged = 0;
  397. unsigned long map_flags;
  398. if (offset_in_page(new_addr))
  399. goto out;
  400. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  401. goto out;
  402. /* Ensure the old/new locations do not overlap */
  403. if (addr + old_len > new_addr && new_addr + new_len > addr)
  404. goto out;
  405. ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
  406. if (ret)
  407. goto out;
  408. if (old_len >= new_len) {
  409. ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
  410. if (ret && old_len != new_len)
  411. goto out;
  412. old_len = new_len;
  413. }
  414. vma = vma_to_resize(addr, old_len, new_len, &charged);
  415. if (IS_ERR(vma)) {
  416. ret = PTR_ERR(vma);
  417. goto out;
  418. }
  419. map_flags = MAP_FIXED;
  420. if (vma->vm_flags & VM_MAYSHARE)
  421. map_flags |= MAP_SHARED;
  422. ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
  423. ((addr - vma->vm_start) >> PAGE_SHIFT),
  424. map_flags);
  425. if (offset_in_page(ret))
  426. goto out1;
  427. ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf,
  428. uf_unmap);
  429. if (!(offset_in_page(ret)))
  430. goto out;
  431. out1:
  432. vm_unacct_memory(charged);
  433. out:
  434. return ret;
  435. }
  436. static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
  437. {
  438. unsigned long end = vma->vm_end + delta;
  439. if (end < vma->vm_end) /* overflow */
  440. return 0;
  441. if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
  442. return 0;
  443. if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
  444. 0, MAP_FIXED) & ~PAGE_MASK)
  445. return 0;
  446. return 1;
  447. }
  448. /*
  449. * Expand (or shrink) an existing mapping, potentially moving it at the
  450. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  451. *
  452. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  453. * This option implies MREMAP_MAYMOVE.
  454. */
  455. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  456. unsigned long, new_len, unsigned long, flags,
  457. unsigned long, new_addr)
  458. {
  459. struct mm_struct *mm = current->mm;
  460. struct vm_area_struct *vma;
  461. unsigned long ret = -EINVAL;
  462. unsigned long charged = 0;
  463. bool locked = false;
  464. struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
  465. LIST_HEAD(uf_unmap_early);
  466. LIST_HEAD(uf_unmap);
  467. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
  468. return ret;
  469. if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
  470. return ret;
  471. if (offset_in_page(addr))
  472. return ret;
  473. old_len = PAGE_ALIGN(old_len);
  474. new_len = PAGE_ALIGN(new_len);
  475. /*
  476. * We allow a zero old-len as a special case
  477. * for DOS-emu "duplicate shm area" thing. But
  478. * a zero new-len is nonsensical.
  479. */
  480. if (!new_len)
  481. return ret;
  482. if (down_write_killable(&current->mm->mmap_sem))
  483. return -EINTR;
  484. if (flags & MREMAP_FIXED) {
  485. ret = mremap_to(addr, old_len, new_addr, new_len,
  486. &locked, &uf, &uf_unmap_early, &uf_unmap);
  487. goto out;
  488. }
  489. /*
  490. * Always allow a shrinking remap: that just unmaps
  491. * the unnecessary pages..
  492. * do_munmap does all the needed commit accounting
  493. */
  494. if (old_len >= new_len) {
  495. ret = do_munmap(mm, addr+new_len, old_len - new_len, &uf_unmap);
  496. if (ret && old_len != new_len)
  497. goto out;
  498. ret = addr;
  499. goto out;
  500. }
  501. /*
  502. * Ok, we need to grow..
  503. */
  504. vma = vma_to_resize(addr, old_len, new_len, &charged);
  505. if (IS_ERR(vma)) {
  506. ret = PTR_ERR(vma);
  507. goto out;
  508. }
  509. /* old_len exactly to the end of the area..
  510. */
  511. if (old_len == vma->vm_end - addr) {
  512. /* can we just expand the current mapping? */
  513. if (vma_expandable(vma, new_len - old_len)) {
  514. int pages = (new_len - old_len) >> PAGE_SHIFT;
  515. if (vma_adjust(vma, vma->vm_start, addr + new_len,
  516. vma->vm_pgoff, NULL)) {
  517. ret = -ENOMEM;
  518. goto out;
  519. }
  520. vm_stat_account(mm, vma->vm_flags, pages);
  521. if (vma->vm_flags & VM_LOCKED) {
  522. mm->locked_vm += pages;
  523. locked = true;
  524. new_addr = addr;
  525. }
  526. ret = addr;
  527. goto out;
  528. }
  529. }
  530. /*
  531. * We weren't able to just expand or shrink the area,
  532. * we need to create a new one and move it..
  533. */
  534. ret = -ENOMEM;
  535. if (flags & MREMAP_MAYMOVE) {
  536. unsigned long map_flags = 0;
  537. if (vma->vm_flags & VM_MAYSHARE)
  538. map_flags |= MAP_SHARED;
  539. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  540. vma->vm_pgoff +
  541. ((addr - vma->vm_start) >> PAGE_SHIFT),
  542. map_flags);
  543. if (offset_in_page(new_addr)) {
  544. ret = new_addr;
  545. goto out;
  546. }
  547. ret = move_vma(vma, addr, old_len, new_len, new_addr,
  548. &locked, &uf, &uf_unmap);
  549. }
  550. out:
  551. if (offset_in_page(ret)) {
  552. vm_unacct_memory(charged);
  553. locked = 0;
  554. }
  555. up_write(&current->mm->mmap_sem);
  556. if (locked && new_len > old_len)
  557. mm_populate(new_addr + old_len, new_len - old_len);
  558. userfaultfd_unmap_complete(mm, &uf_unmap_early);
  559. mremap_userfaultfd_complete(&uf, addr, new_addr, old_len);
  560. userfaultfd_unmap_complete(mm, &uf_unmap);
  561. return ret;
  562. }