mremap.c 17 KB

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