mremap.c 14 KB

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