mremap.c 11 KB

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