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. if (pfn_valid(pte_pfn(pte)) &&
  128. pte_page(pte) == ZERO_PAGE(old_addr))
  129. pte = pte_wrprotect(mk_pte(ZERO_PAGE(new_addr), new_vma->vm_page_prot));
  130. set_pte_at(mm, new_addr, dst, pte);
  131. } else
  132. error = -ENOMEM;
  133. pte_unmap_nested(src);
  134. }
  135. if (dst)
  136. pte_unmap(dst);
  137. }
  138. spin_unlock(&mm->page_table_lock);
  139. if (mapping)
  140. spin_unlock(&mapping->i_mmap_lock);
  141. return error;
  142. }
  143. static unsigned long move_page_tables(struct vm_area_struct *vma,
  144. unsigned long old_addr, struct vm_area_struct *new_vma,
  145. unsigned long new_addr, unsigned long len)
  146. {
  147. unsigned long offset;
  148. flush_cache_range(vma, old_addr, old_addr + len);
  149. /*
  150. * This is not the clever way to do this, but we're taking the
  151. * easy way out on the assumption that most remappings will be
  152. * only a few pages.. This also makes error recovery easier.
  153. */
  154. for (offset = 0; offset < len; offset += PAGE_SIZE) {
  155. if (move_one_page(vma, old_addr + offset,
  156. new_vma, new_addr + offset) < 0)
  157. break;
  158. cond_resched();
  159. }
  160. return offset;
  161. }
  162. static unsigned long move_vma(struct vm_area_struct *vma,
  163. unsigned long old_addr, unsigned long old_len,
  164. unsigned long new_len, unsigned long new_addr)
  165. {
  166. struct mm_struct *mm = vma->vm_mm;
  167. struct vm_area_struct *new_vma;
  168. unsigned long vm_flags = vma->vm_flags;
  169. unsigned long new_pgoff;
  170. unsigned long moved_len;
  171. unsigned long excess = 0;
  172. int split = 0;
  173. /*
  174. * We'd prefer to avoid failure later on in do_munmap:
  175. * which may split one vma into three before unmapping.
  176. */
  177. if (mm->map_count >= sysctl_max_map_count - 3)
  178. return -ENOMEM;
  179. new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
  180. new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
  181. if (!new_vma)
  182. return -ENOMEM;
  183. moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
  184. if (moved_len < old_len) {
  185. /*
  186. * On error, move entries back from new area to old,
  187. * which will succeed since page tables still there,
  188. * and then proceed to unmap new area instead of old.
  189. */
  190. move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
  191. vma = new_vma;
  192. old_len = new_len;
  193. old_addr = new_addr;
  194. new_addr = -ENOMEM;
  195. }
  196. /* Conceal VM_ACCOUNT so old reservation is not undone */
  197. if (vm_flags & VM_ACCOUNT) {
  198. vma->vm_flags &= ~VM_ACCOUNT;
  199. excess = vma->vm_end - vma->vm_start - old_len;
  200. if (old_addr > vma->vm_start &&
  201. old_addr + old_len < vma->vm_end)
  202. split = 1;
  203. }
  204. /*
  205. * if we failed to move page tables we still do total_vm increment
  206. * since do_munmap() will decrement it by old_len == new_len
  207. */
  208. mm->total_vm += new_len >> PAGE_SHIFT;
  209. __vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
  210. if (do_munmap(mm, old_addr, old_len) < 0) {
  211. /* OOM: unable to split vma, just get accounts right */
  212. vm_unacct_memory(excess >> PAGE_SHIFT);
  213. excess = 0;
  214. }
  215. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  216. if (excess) {
  217. vma->vm_flags |= VM_ACCOUNT;
  218. if (split)
  219. vma->vm_next->vm_flags |= VM_ACCOUNT;
  220. }
  221. if (vm_flags & VM_LOCKED) {
  222. mm->locked_vm += new_len >> PAGE_SHIFT;
  223. if (new_len > old_len)
  224. make_pages_present(new_addr + old_len,
  225. new_addr + new_len);
  226. }
  227. return new_addr;
  228. }
  229. /*
  230. * Expand (or shrink) an existing mapping, potentially moving it at the
  231. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  232. *
  233. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  234. * This option implies MREMAP_MAYMOVE.
  235. */
  236. unsigned long do_mremap(unsigned long addr,
  237. unsigned long old_len, unsigned long new_len,
  238. unsigned long flags, unsigned long new_addr)
  239. {
  240. struct vm_area_struct *vma;
  241. unsigned long ret = -EINVAL;
  242. unsigned long charged = 0;
  243. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
  244. goto out;
  245. if (addr & ~PAGE_MASK)
  246. goto out;
  247. old_len = PAGE_ALIGN(old_len);
  248. new_len = PAGE_ALIGN(new_len);
  249. /*
  250. * We allow a zero old-len as a special case
  251. * for DOS-emu "duplicate shm area" thing. But
  252. * a zero new-len is nonsensical.
  253. */
  254. if (!new_len)
  255. goto out;
  256. /* new_addr is only valid if MREMAP_FIXED is specified */
  257. if (flags & MREMAP_FIXED) {
  258. if (new_addr & ~PAGE_MASK)
  259. goto out;
  260. if (!(flags & MREMAP_MAYMOVE))
  261. goto out;
  262. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  263. goto out;
  264. /* Check if the location we're moving into overlaps the
  265. * old location at all, and fail if it does.
  266. */
  267. if ((new_addr <= addr) && (new_addr+new_len) > addr)
  268. goto out;
  269. if ((addr <= new_addr) && (addr+old_len) > new_addr)
  270. goto out;
  271. ret = do_munmap(current->mm, new_addr, new_len);
  272. if (ret)
  273. goto out;
  274. }
  275. /*
  276. * Always allow a shrinking remap: that just unmaps
  277. * the unnecessary pages..
  278. * do_munmap does all the needed commit accounting
  279. */
  280. if (old_len >= new_len) {
  281. ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
  282. if (ret && old_len != new_len)
  283. goto out;
  284. ret = addr;
  285. if (!(flags & MREMAP_FIXED) || (new_addr == addr))
  286. goto out;
  287. old_len = new_len;
  288. }
  289. /*
  290. * Ok, we need to grow.. or relocate.
  291. */
  292. ret = -EFAULT;
  293. vma = find_vma(current->mm, addr);
  294. if (!vma || vma->vm_start > addr)
  295. goto out;
  296. if (is_vm_hugetlb_page(vma)) {
  297. ret = -EINVAL;
  298. goto out;
  299. }
  300. /* We can't remap across vm area boundaries */
  301. if (old_len > vma->vm_end - addr)
  302. goto out;
  303. if (vma->vm_flags & VM_DONTEXPAND) {
  304. if (new_len > old_len)
  305. goto out;
  306. }
  307. if (vma->vm_flags & VM_LOCKED) {
  308. unsigned long locked, lock_limit;
  309. locked = current->mm->locked_vm << PAGE_SHIFT;
  310. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  311. locked += new_len - old_len;
  312. ret = -EAGAIN;
  313. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  314. goto out;
  315. }
  316. if (!may_expand_vm(current->mm, (new_len - old_len) >> PAGE_SHIFT)) {
  317. ret = -ENOMEM;
  318. goto out;
  319. }
  320. if (vma->vm_flags & VM_ACCOUNT) {
  321. charged = (new_len - old_len) >> PAGE_SHIFT;
  322. if (security_vm_enough_memory(charged))
  323. goto out_nc;
  324. }
  325. /* old_len exactly to the end of the area..
  326. * And we're not relocating the area.
  327. */
  328. if (old_len == vma->vm_end - addr &&
  329. !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
  330. (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
  331. unsigned long max_addr = TASK_SIZE;
  332. if (vma->vm_next)
  333. max_addr = vma->vm_next->vm_start;
  334. /* can we just expand the current mapping? */
  335. if (max_addr - addr >= new_len) {
  336. int pages = (new_len - old_len) >> PAGE_SHIFT;
  337. vma_adjust(vma, vma->vm_start,
  338. addr + new_len, vma->vm_pgoff, NULL);
  339. current->mm->total_vm += pages;
  340. __vm_stat_account(vma->vm_mm, vma->vm_flags,
  341. vma->vm_file, pages);
  342. if (vma->vm_flags & VM_LOCKED) {
  343. current->mm->locked_vm += pages;
  344. make_pages_present(addr + old_len,
  345. addr + new_len);
  346. }
  347. ret = addr;
  348. goto out;
  349. }
  350. }
  351. /*
  352. * We weren't able to just expand or shrink the area,
  353. * we need to create a new one and move it..
  354. */
  355. ret = -ENOMEM;
  356. if (flags & MREMAP_MAYMOVE) {
  357. if (!(flags & MREMAP_FIXED)) {
  358. unsigned long map_flags = 0;
  359. if (vma->vm_flags & VM_MAYSHARE)
  360. map_flags |= MAP_SHARED;
  361. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  362. vma->vm_pgoff, map_flags);
  363. ret = new_addr;
  364. if (new_addr & ~PAGE_MASK)
  365. goto out;
  366. }
  367. ret = move_vma(vma, addr, old_len, new_len, new_addr);
  368. }
  369. out:
  370. if (ret & ~PAGE_MASK)
  371. vm_unacct_memory(charged);
  372. out_nc:
  373. return ret;
  374. }
  375. asmlinkage unsigned long sys_mremap(unsigned long addr,
  376. unsigned long old_len, unsigned long new_len,
  377. unsigned long flags, unsigned long new_addr)
  378. {
  379. unsigned long ret;
  380. down_write(&current->mm->mmap_sem);
  381. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  382. up_write(&current->mm->mmap_sem);
  383. return ret;
  384. }