mremap.c 16 KB

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