mremap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. if (do_munmap(mm, old_addr, old_len) < 0) {
  201. /* OOM: unable to split vma, just get accounts right */
  202. vm_unacct_memory(excess >> PAGE_SHIFT);
  203. excess = 0;
  204. }
  205. /* Restore VM_ACCOUNT if one or two pieces of vma left */
  206. if (excess) {
  207. vma->vm_flags |= VM_ACCOUNT;
  208. if (split)
  209. vma->vm_next->vm_flags |= VM_ACCOUNT;
  210. }
  211. mm->total_vm += new_len >> PAGE_SHIFT;
  212. __vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
  213. if (vm_flags & VM_LOCKED) {
  214. mm->locked_vm += new_len >> PAGE_SHIFT;
  215. if (new_len > old_len)
  216. make_pages_present(new_addr + old_len,
  217. new_addr + new_len);
  218. }
  219. return new_addr;
  220. }
  221. /*
  222. * Expand (or shrink) an existing mapping, potentially moving it at the
  223. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  224. *
  225. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  226. * This option implies MREMAP_MAYMOVE.
  227. */
  228. unsigned long do_mremap(unsigned long addr,
  229. unsigned long old_len, unsigned long new_len,
  230. unsigned long flags, unsigned long new_addr)
  231. {
  232. struct vm_area_struct *vma;
  233. unsigned long ret = -EINVAL;
  234. unsigned long charged = 0;
  235. if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
  236. goto out;
  237. if (addr & ~PAGE_MASK)
  238. goto out;
  239. old_len = PAGE_ALIGN(old_len);
  240. new_len = PAGE_ALIGN(new_len);
  241. /*
  242. * We allow a zero old-len as a special case
  243. * for DOS-emu "duplicate shm area" thing. But
  244. * a zero new-len is nonsensical.
  245. */
  246. if (!new_len)
  247. goto out;
  248. /* new_addr is only valid if MREMAP_FIXED is specified */
  249. if (flags & MREMAP_FIXED) {
  250. if (new_addr & ~PAGE_MASK)
  251. goto out;
  252. if (!(flags & MREMAP_MAYMOVE))
  253. goto out;
  254. if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  255. goto out;
  256. /* Check if the location we're moving into overlaps the
  257. * old location at all, and fail if it does.
  258. */
  259. if ((new_addr <= addr) && (new_addr+new_len) > addr)
  260. goto out;
  261. if ((addr <= new_addr) && (addr+old_len) > new_addr)
  262. goto out;
  263. ret = do_munmap(current->mm, new_addr, new_len);
  264. if (ret)
  265. goto out;
  266. }
  267. /*
  268. * Always allow a shrinking remap: that just unmaps
  269. * the unnecessary pages..
  270. * do_munmap does all the needed commit accounting
  271. */
  272. if (old_len >= new_len) {
  273. ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
  274. if (ret && old_len != new_len)
  275. goto out;
  276. ret = addr;
  277. if (!(flags & MREMAP_FIXED) || (new_addr == addr))
  278. goto out;
  279. old_len = new_len;
  280. }
  281. /*
  282. * Ok, we need to grow.. or relocate.
  283. */
  284. ret = -EFAULT;
  285. vma = find_vma(current->mm, addr);
  286. if (!vma || vma->vm_start > addr)
  287. goto out;
  288. if (is_vm_hugetlb_page(vma)) {
  289. ret = -EINVAL;
  290. goto out;
  291. }
  292. /* We can't remap across vm area boundaries */
  293. if (old_len > vma->vm_end - addr)
  294. goto out;
  295. if (vma->vm_flags & VM_DONTEXPAND) {
  296. if (new_len > old_len)
  297. goto out;
  298. }
  299. if (vma->vm_flags & VM_LOCKED) {
  300. unsigned long locked, lock_limit;
  301. locked = current->mm->locked_vm << PAGE_SHIFT;
  302. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  303. locked += new_len - old_len;
  304. ret = -EAGAIN;
  305. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  306. goto out;
  307. }
  308. if (!may_expand_vm(current->mm, (new_len - old_len) >> PAGE_SHIFT)) {
  309. ret = -ENOMEM;
  310. goto out;
  311. }
  312. if (vma->vm_flags & VM_ACCOUNT) {
  313. charged = (new_len - old_len) >> PAGE_SHIFT;
  314. if (security_vm_enough_memory(charged))
  315. goto out_nc;
  316. }
  317. /* old_len exactly to the end of the area..
  318. * And we're not relocating the area.
  319. */
  320. if (old_len == vma->vm_end - addr &&
  321. !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
  322. (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
  323. unsigned long max_addr = TASK_SIZE;
  324. if (vma->vm_next)
  325. max_addr = vma->vm_next->vm_start;
  326. /* can we just expand the current mapping? */
  327. if (max_addr - addr >= new_len) {
  328. int pages = (new_len - old_len) >> PAGE_SHIFT;
  329. vma_adjust(vma, vma->vm_start,
  330. addr + new_len, vma->vm_pgoff, NULL);
  331. current->mm->total_vm += pages;
  332. __vm_stat_account(vma->vm_mm, vma->vm_flags,
  333. vma->vm_file, pages);
  334. if (vma->vm_flags & VM_LOCKED) {
  335. current->mm->locked_vm += pages;
  336. make_pages_present(addr + old_len,
  337. addr + new_len);
  338. }
  339. ret = addr;
  340. goto out;
  341. }
  342. }
  343. /*
  344. * We weren't able to just expand or shrink the area,
  345. * we need to create a new one and move it..
  346. */
  347. ret = -ENOMEM;
  348. if (flags & MREMAP_MAYMOVE) {
  349. if (!(flags & MREMAP_FIXED)) {
  350. unsigned long map_flags = 0;
  351. if (vma->vm_flags & VM_MAYSHARE)
  352. map_flags |= MAP_SHARED;
  353. new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
  354. vma->vm_pgoff, map_flags);
  355. ret = new_addr;
  356. if (new_addr & ~PAGE_MASK)
  357. goto out;
  358. }
  359. ret = move_vma(vma, addr, old_len, new_len, new_addr);
  360. }
  361. out:
  362. if (ret & ~PAGE_MASK)
  363. vm_unacct_memory(charged);
  364. out_nc:
  365. return ret;
  366. }
  367. asmlinkage unsigned long sys_mremap(unsigned long addr,
  368. unsigned long old_len, unsigned long new_len,
  369. unsigned long flags, unsigned long new_addr)
  370. {
  371. unsigned long ret;
  372. down_write(&current->mm->mmap_sem);
  373. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  374. up_write(&current->mm->mmap_sem);
  375. return ret;
  376. }