mremap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/slab.h>
  12. #include <linux/shm.h>
  13. #include <linux/ksm.h>
  14. #include <linux/mman.h>
  15. #include <linux/swap.h>
  16. #include <linux/capability.h>
  17. #include <linux/fs.h>
  18. #include <linux/highmem.h>
  19. #include <linux/security.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/mmu_notifier.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlbflush.h>
  25. #include "internal.h"
  26. static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
  27. {
  28. pgd_t *pgd;
  29. pud_t *pud;
  30. pmd_t *pmd;
  31. pgd = pgd_offset(mm, addr);
  32. if (pgd_none_or_clear_bad(pgd))
  33. return NULL;
  34. pud = pud_offset(pgd, addr);
  35. if (pud_none_or_clear_bad(pud))
  36. return NULL;
  37. pmd = pmd_offset(pud, addr);
  38. if (pmd_none_or_clear_bad(pmd))
  39. return NULL;
  40. return pmd;
  41. }
  42. static pmd_t *alloc_new_pmd(struct mm_struct *mm, 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. if (!pmd_present(*pmd) && __pte_alloc(mm, pmd, addr))
  55. return NULL;
  56. return pmd;
  57. }
  58. static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
  59. unsigned long old_addr, unsigned long old_end,
  60. struct vm_area_struct *new_vma, pmd_t *new_pmd,
  61. unsigned long new_addr)
  62. {
  63. struct address_space *mapping = 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. unsigned long old_start;
  68. old_start = old_addr;
  69. mmu_notifier_invalidate_range_start(vma->vm_mm,
  70. old_start, old_end);
  71. if (vma->vm_file) {
  72. /*
  73. * Subtle point from Rajesh Venkatasubramanian: before
  74. * moving file-based ptes, we must lock truncate_pagecache
  75. * out, since it might clean the dst vma before the src vma,
  76. * and we propagate stale pages into the dst afterward.
  77. */
  78. mapping = vma->vm_file->f_mapping;
  79. spin_lock(&mapping->i_mmap_lock);
  80. if (new_vma->vm_truncate_count &&
  81. new_vma->vm_truncate_count != vma->vm_truncate_count)
  82. new_vma->vm_truncate_count = 0;
  83. }
  84. /*
  85. * We don't have to worry about the ordering of src and dst
  86. * pte locks because exclusive mmap_sem prevents deadlock.
  87. */
  88. old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
  89. new_pte = pte_offset_map_nested(new_pmd, new_addr);
  90. new_ptl = pte_lockptr(mm, new_pmd);
  91. if (new_ptl != old_ptl)
  92. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  93. arch_enter_lazy_mmu_mode();
  94. for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
  95. new_pte++, new_addr += PAGE_SIZE) {
  96. if (pte_none(*old_pte))
  97. continue;
  98. pte = ptep_clear_flush(vma, old_addr, old_pte);
  99. pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
  100. set_pte_at(mm, new_addr, new_pte, pte);
  101. }
  102. arch_leave_lazy_mmu_mode();
  103. if (new_ptl != old_ptl)
  104. spin_unlock(new_ptl);
  105. pte_unmap_nested(new_pte - 1);
  106. pte_unmap_unlock(old_pte - 1, old_ptl);
  107. if (mapping)
  108. spin_unlock(&mapping->i_mmap_lock);
  109. mmu_notifier_invalidate_range_end(vma->vm_mm, old_start, old_end);
  110. }
  111. #define LATENCY_LIMIT (64 * PAGE_SIZE)
  112. unsigned long move_page_tables(struct vm_area_struct *vma,
  113. unsigned long old_addr, struct vm_area_struct *new_vma,
  114. unsigned long new_addr, unsigned long len)
  115. {
  116. unsigned long extent, next, old_end;
  117. pmd_t *old_pmd, *new_pmd;
  118. old_end = old_addr + len;
  119. flush_cache_range(vma, old_addr, old_end);
  120. for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
  121. cond_resched();
  122. next = (old_addr + PMD_SIZE) & PMD_MASK;
  123. if (next - 1 > old_end)
  124. next = old_end;
  125. extent = next - old_addr;
  126. old_pmd = get_old_pmd(vma->vm_mm, old_addr);
  127. if (!old_pmd)
  128. continue;
  129. new_pmd = alloc_new_pmd(vma->vm_mm, new_addr);
  130. if (!new_pmd)
  131. break;
  132. next = (new_addr + PMD_SIZE) & PMD_MASK;
  133. if (extent > next - new_addr)
  134. extent = next - new_addr;
  135. if (extent > LATENCY_LIMIT)
  136. extent = LATENCY_LIMIT;
  137. move_ptes(vma, old_pmd, old_addr, old_addr + extent,
  138. new_vma, new_pmd, new_addr);
  139. }
  140. return len + old_addr - old_end; /* how much done */
  141. }
  142. static unsigned long move_vma(struct vm_area_struct *vma,
  143. unsigned long old_addr, unsigned long old_len,
  144. unsigned long new_len, unsigned long new_addr)
  145. {
  146. struct mm_struct *mm = vma->vm_mm;
  147. struct vm_area_struct *new_vma;
  148. unsigned long vm_flags = vma->vm_flags;
  149. unsigned long new_pgoff;
  150. unsigned long moved_len;
  151. unsigned long excess = 0;
  152. unsigned long hiwater_vm;
  153. int split = 0;
  154. int err;
  155. /*
  156. * We'd prefer to avoid failure later on in do_munmap:
  157. * which may split one vma into three before unmapping.
  158. */
  159. if (mm->map_count >= sysctl_max_map_count - 3)
  160. return -ENOMEM;
  161. /*
  162. * Advise KSM to break any KSM pages in the area to be moved:
  163. * it would be confusing if they were to turn up at the new
  164. * location, where they happen to coincide with different KSM
  165. * pages recently unmapped. But leave vma->vm_flags as it was,
  166. * so KSM can come around to merge on vma and new_vma afterwards.
  167. */
  168. err = ksm_madvise(vma, old_addr, old_addr + old_len,
  169. MADV_UNMERGEABLE, &vm_flags);
  170. if (err)
  171. return err;
  172. new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
  173. new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
  174. if (!new_vma)
  175. return -ENOMEM;
  176. moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
  177. if (moved_len < old_len) {
  178. /*
  179. * On error, move entries back from new area to old,
  180. * which will succeed since page tables still there,
  181. * and then proceed to unmap new area instead of old.
  182. */
  183. move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
  184. vma = new_vma;
  185. old_len = new_len;
  186. old_addr = new_addr;
  187. new_addr = -ENOMEM;
  188. }
  189. /* Conceal VM_ACCOUNT so old reservation is not undone */
  190. if (vm_flags & VM_ACCOUNT) {
  191. vma->vm_flags &= ~VM_ACCOUNT;
  192. excess = vma->vm_end - vma->vm_start - old_len;
  193. if (old_addr > vma->vm_start &&
  194. old_addr + old_len < vma->vm_end)
  195. split = 1;
  196. }
  197. /*
  198. * If we failed to move page tables we still do total_vm increment
  199. * since do_munmap() will decrement it by old_len == new_len.
  200. *
  201. * Since total_vm is about to be raised artificially high for a
  202. * moment, we need to restore high watermark afterwards: if stats
  203. * are taken meanwhile, total_vm and hiwater_vm appear too high.
  204. * If this were a serious issue, we'd add a flag to do_munmap().
  205. */
  206. hiwater_vm = mm->hiwater_vm;
  207. mm->total_vm += new_len >> PAGE_SHIFT;
  208. vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
  209. if (do_munmap(mm, old_addr, old_len) < 0) {
  210. /* OOM: unable to split vma, just get accounts right */
  211. vm_unacct_memory(excess >> PAGE_SHIFT);
  212. excess = 0;
  213. }
  214. mm->hiwater_vm = hiwater_vm;
  215. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  216. if (excess) {
  217. vma->vm_flags |= VM_ACCOUNT;
  218. if (split)
  219. vma->vm_next->vm_flags |= VM_ACCOUNT;
  220. }
  221. if (vm_flags & VM_LOCKED) {
  222. mm->locked_vm += new_len >> PAGE_SHIFT;
  223. if (new_len > old_len)
  224. mlock_vma_pages_range(new_vma, new_addr + old_len,
  225. new_addr + new_len);
  226. }
  227. return new_addr;
  228. }
  229. /*
  230. * Expand (or shrink) an existing mapping, potentially moving it at the
  231. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  232. *
  233. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  234. * This option implies MREMAP_MAYMOVE.
  235. */
  236. unsigned long do_mremap(unsigned long addr,
  237. unsigned long old_len, unsigned long new_len,
  238. unsigned long flags, unsigned long new_addr)
  239. {
  240. struct mm_struct *mm = current->mm;
  241. struct vm_area_struct *vma;
  242. unsigned long ret = -EINVAL;
  243. unsigned long charged = 0;
  244. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
  245. goto out;
  246. if (addr & ~PAGE_MASK)
  247. goto out;
  248. old_len = PAGE_ALIGN(old_len);
  249. new_len = PAGE_ALIGN(new_len);
  250. /*
  251. * We allow a zero old-len as a special case
  252. * for DOS-emu "duplicate shm area" thing. But
  253. * a zero new-len is nonsensical.
  254. */
  255. if (!new_len)
  256. goto out;
  257. /* new_addr is only valid if MREMAP_FIXED is specified */
  258. if (flags & MREMAP_FIXED) {
  259. if (new_addr & ~PAGE_MASK)
  260. goto out;
  261. if (!(flags & MREMAP_MAYMOVE))
  262. goto out;
  263. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  264. goto out;
  265. /* Check if the location we're moving into overlaps the
  266. * old location at all, and fail if it does.
  267. */
  268. if ((new_addr <= addr) && (new_addr+new_len) > addr)
  269. goto out;
  270. if ((addr <= new_addr) && (addr+old_len) > new_addr)
  271. goto out;
  272. ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
  273. if (ret)
  274. goto out;
  275. ret = do_munmap(mm, new_addr, new_len);
  276. if (ret)
  277. goto out;
  278. }
  279. /*
  280. * Always allow a shrinking remap: that just unmaps
  281. * the unnecessary pages..
  282. * do_munmap does all the needed commit accounting
  283. */
  284. if (old_len >= new_len) {
  285. ret = do_munmap(mm, addr+new_len, old_len - new_len);
  286. if (ret && old_len != new_len)
  287. goto out;
  288. ret = addr;
  289. if (!(flags & MREMAP_FIXED) || (new_addr == addr))
  290. goto out;
  291. old_len = new_len;
  292. }
  293. /*
  294. * Ok, we need to grow.. or relocate.
  295. */
  296. ret = -EFAULT;
  297. vma = find_vma(mm, addr);
  298. if (!vma || vma->vm_start > addr)
  299. goto out;
  300. if (is_vm_hugetlb_page(vma)) {
  301. ret = -EINVAL;
  302. goto out;
  303. }
  304. /* We can't remap across vm area boundaries */
  305. if (old_len > vma->vm_end - addr)
  306. goto out;
  307. if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) {
  308. if (new_len > old_len)
  309. goto out;
  310. }
  311. if (vma->vm_flags & VM_LOCKED) {
  312. unsigned long locked, lock_limit;
  313. locked = mm->locked_vm << PAGE_SHIFT;
  314. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  315. locked += new_len - old_len;
  316. ret = -EAGAIN;
  317. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  318. goto out;
  319. }
  320. if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT)) {
  321. ret = -ENOMEM;
  322. goto out;
  323. }
  324. if (vma->vm_flags & VM_ACCOUNT) {
  325. charged = (new_len - old_len) >> PAGE_SHIFT;
  326. if (security_vm_enough_memory(charged))
  327. goto out_nc;
  328. }
  329. /* old_len exactly to the end of the area..
  330. * And we're not relocating the area.
  331. */
  332. if (old_len == vma->vm_end - addr &&
  333. !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
  334. (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
  335. unsigned long max_addr = TASK_SIZE;
  336. if (vma->vm_next)
  337. max_addr = vma->vm_next->vm_start;
  338. /* can we just expand the current mapping? */
  339. if (max_addr - addr >= new_len) {
  340. int pages = (new_len - old_len) >> PAGE_SHIFT;
  341. vma_adjust(vma, vma->vm_start,
  342. addr + new_len, vma->vm_pgoff, NULL);
  343. mm->total_vm += pages;
  344. vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
  345. if (vma->vm_flags & VM_LOCKED) {
  346. mm->locked_vm += pages;
  347. mlock_vma_pages_range(vma, addr + old_len,
  348. addr + new_len);
  349. }
  350. ret = addr;
  351. goto out;
  352. }
  353. }
  354. /*
  355. * We weren't able to just expand or shrink the area,
  356. * we need to create a new one and move it..
  357. */
  358. ret = -ENOMEM;
  359. if (flags & MREMAP_MAYMOVE) {
  360. if (!(flags & MREMAP_FIXED)) {
  361. unsigned long map_flags = 0;
  362. if (vma->vm_flags & VM_MAYSHARE)
  363. map_flags |= MAP_SHARED;
  364. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  365. vma->vm_pgoff, map_flags);
  366. if (new_addr & ~PAGE_MASK) {
  367. ret = new_addr;
  368. goto out;
  369. }
  370. ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
  371. if (ret)
  372. goto out;
  373. }
  374. ret = move_vma(vma, addr, old_len, new_len, new_addr);
  375. }
  376. out:
  377. if (ret & ~PAGE_MASK)
  378. vm_unacct_memory(charged);
  379. out_nc:
  380. return ret;
  381. }
  382. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  383. unsigned long, new_len, unsigned long, flags,
  384. unsigned long, new_addr)
  385. {
  386. unsigned long ret;
  387. down_write(&current->mm->mmap_sem);
  388. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  389. up_write(&current->mm->mmap_sem);
  390. return ret;
  391. }