mremap.c 15 KB

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