mremap.c 10 KB

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