mremap.c 11 KB

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