mremap.c 11 KB

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