mremap.c 11 KB

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