mremap.c 15 KB

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