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