mremap.c 11 KB

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