mremap.c 11 KB

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