mremap.c 10 KB

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