mremap.c 14 KB

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