mremap.c 16 KB

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