mremap.c 15 KB

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