mremap.c 13 KB

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