ACPI: thinkpad-acpi: preserve radio state across shutdown
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / mremap.c
bloba27c00798c2624496e6b56657be9d39a5e87e41a
1 /*
2 * mm/mremap.c
4 * (C) Copyright 1996 Linus Torvalds
6 * Address space accounting code <alan@redhat.com>
7 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
8 */
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/capability.h>
17 #include <linux/fs.h>
18 #include <linux/highmem.h>
19 #include <linux/security.h>
20 #include <linux/syscalls.h>
21 #include <linux/mmu_notifier.h>
23 #include <asm/uaccess.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
27 static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
29 pgd_t *pgd;
30 pud_t *pud;
31 pmd_t *pmd;
33 pgd = pgd_offset(mm, addr);
34 if (pgd_none_or_clear_bad(pgd))
35 return NULL;
37 pud = pud_offset(pgd, addr);
38 if (pud_none_or_clear_bad(pud))
39 return NULL;
41 pmd = pmd_offset(pud, addr);
42 if (pmd_none_or_clear_bad(pmd))
43 return NULL;
45 return pmd;
48 static pmd_t *alloc_new_pmd(struct mm_struct *mm, unsigned long addr)
50 pgd_t *pgd;
51 pud_t *pud;
52 pmd_t *pmd;
54 pgd = pgd_offset(mm, addr);
55 pud = pud_alloc(mm, pgd, addr);
56 if (!pud)
57 return NULL;
59 pmd = pmd_alloc(mm, pud, addr);
60 if (!pmd)
61 return NULL;
63 if (!pmd_present(*pmd) && __pte_alloc(mm, pmd, addr))
64 return NULL;
66 return pmd;
69 static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
70 unsigned long old_addr, unsigned long old_end,
71 struct vm_area_struct *new_vma, pmd_t *new_pmd,
72 unsigned long new_addr)
74 struct address_space *mapping = NULL;
75 struct mm_struct *mm = vma->vm_mm;
76 pte_t *old_pte, *new_pte, pte;
77 spinlock_t *old_ptl, *new_ptl;
78 unsigned long old_start;
80 old_start = old_addr;
81 mmu_notifier_invalidate_range_start(vma->vm_mm,
82 old_start, old_end);
83 if (vma->vm_file) {
85 * Subtle point from Rajesh Venkatasubramanian: before
86 * moving file-based ptes, we must lock vmtruncate out,
87 * since it might clean the dst vma before the src vma,
88 * and we propagate stale pages into the dst afterward.
90 mapping = vma->vm_file->f_mapping;
91 spin_lock(&mapping->i_mmap_lock);
92 new_vma->vm_truncate_count = 0;
96 * We don't have to worry about the ordering of src and dst
97 * pte locks because exclusive mmap_sem prevents deadlock.
99 old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
100 new_pte = pte_offset_map_nested(new_pmd, new_addr);
101 new_ptl = pte_lockptr(mm, new_pmd);
102 if (new_ptl != old_ptl)
103 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
104 arch_enter_lazy_mmu_mode();
106 for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
107 new_pte++, new_addr += PAGE_SIZE) {
108 if (pte_none(*old_pte))
109 continue;
110 pte = ptep_clear_flush(vma, old_addr, old_pte);
111 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
112 set_pte_at(mm, new_addr, new_pte, pte);
115 arch_leave_lazy_mmu_mode();
116 if (new_ptl != old_ptl)
117 spin_unlock(new_ptl);
118 pte_unmap_nested(new_pte - 1);
119 pte_unmap_unlock(old_pte - 1, old_ptl);
120 if (mapping)
121 spin_unlock(&mapping->i_mmap_lock);
122 mmu_notifier_invalidate_range_end(vma->vm_mm, old_start, old_end);
125 #define LATENCY_LIMIT (64 * PAGE_SIZE)
127 unsigned long move_page_tables(struct vm_area_struct *vma,
128 unsigned long old_addr, struct vm_area_struct *new_vma,
129 unsigned long new_addr, unsigned long len)
131 unsigned long extent, next, old_end;
132 pmd_t *old_pmd, *new_pmd;
134 old_end = old_addr + len;
135 flush_cache_range(vma, old_addr, old_end);
137 for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
138 cond_resched();
139 next = (old_addr + PMD_SIZE) & PMD_MASK;
140 if (next - 1 > old_end)
141 next = old_end;
142 extent = next - old_addr;
143 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
144 if (!old_pmd)
145 continue;
146 new_pmd = alloc_new_pmd(vma->vm_mm, new_addr);
147 if (!new_pmd)
148 break;
149 next = (new_addr + PMD_SIZE) & PMD_MASK;
150 if (extent > next - new_addr)
151 extent = next - new_addr;
152 if (extent > LATENCY_LIMIT)
153 extent = LATENCY_LIMIT;
154 move_ptes(vma, old_pmd, old_addr, old_addr + extent,
155 new_vma, new_pmd, new_addr);
158 return len + old_addr - old_end; /* how much done */
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)
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 unsigned long hiwater_vm;
172 int split = 0;
175 * We'd prefer to avoid failure later on in do_munmap:
176 * which may split one vma into three before unmapping.
178 if (mm->map_count >= sysctl_max_map_count - 3)
179 return -ENOMEM;
181 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
182 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
183 if (!new_vma)
184 return -ENOMEM;
186 moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
187 if (moved_len < old_len) {
189 * On error, move entries back from new area to old,
190 * which will succeed since page tables still there,
191 * and then proceed to unmap new area instead of old.
193 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
194 vma = new_vma;
195 old_len = new_len;
196 old_addr = new_addr;
197 new_addr = -ENOMEM;
200 /* Conceal VM_ACCOUNT so old reservation is not undone */
201 if (vm_flags & VM_ACCOUNT) {
202 vma->vm_flags &= ~VM_ACCOUNT;
203 excess = vma->vm_end - vma->vm_start - old_len;
204 if (old_addr > vma->vm_start &&
205 old_addr + old_len < vma->vm_end)
206 split = 1;
210 * If we failed to move page tables we still do total_vm increment
211 * since do_munmap() will decrement it by old_len == new_len.
213 * Since total_vm is about to be raised artificially high for a
214 * moment, we need to restore high watermark afterwards: if stats
215 * are taken meanwhile, total_vm and hiwater_vm appear too high.
216 * If this were a serious issue, we'd add a flag to do_munmap().
218 hiwater_vm = mm->hiwater_vm;
219 mm->total_vm += new_len >> PAGE_SHIFT;
220 vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
222 if (do_munmap(mm, old_addr, old_len) < 0) {
223 /* OOM: unable to split vma, just get accounts right */
224 vm_unacct_memory(excess >> PAGE_SHIFT);
225 excess = 0;
227 mm->hiwater_vm = hiwater_vm;
229 /* Restore VM_ACCOUNT if one or two pieces of vma left */
230 if (excess) {
231 vma->vm_flags |= VM_ACCOUNT;
232 if (split)
233 vma->vm_next->vm_flags |= VM_ACCOUNT;
236 if (vm_flags & VM_LOCKED) {
237 mm->locked_vm += new_len >> PAGE_SHIFT;
238 if (new_len > old_len)
239 make_pages_present(new_addr + old_len,
240 new_addr + new_len);
243 return new_addr;
247 * Expand (or shrink) an existing mapping, potentially moving it at the
248 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
250 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
251 * This option implies MREMAP_MAYMOVE.
253 unsigned long do_mremap(unsigned long addr,
254 unsigned long old_len, unsigned long new_len,
255 unsigned long flags, unsigned long new_addr)
257 struct mm_struct *mm = current->mm;
258 struct vm_area_struct *vma;
259 unsigned long ret = -EINVAL;
260 unsigned long charged = 0;
262 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
263 goto out;
265 if (addr & ~PAGE_MASK)
266 goto out;
268 old_len = PAGE_ALIGN(old_len);
269 new_len = PAGE_ALIGN(new_len);
272 * We allow a zero old-len as a special case
273 * for DOS-emu "duplicate shm area" thing. But
274 * a zero new-len is nonsensical.
276 if (!new_len)
277 goto out;
279 /* new_addr is only valid if MREMAP_FIXED is specified */
280 if (flags & MREMAP_FIXED) {
281 if (new_addr & ~PAGE_MASK)
282 goto out;
283 if (!(flags & MREMAP_MAYMOVE))
284 goto out;
286 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
287 goto out;
289 /* Check if the location we're moving into overlaps the
290 * old location at all, and fail if it does.
292 if ((new_addr <= addr) && (new_addr+new_len) > addr)
293 goto out;
295 if ((addr <= new_addr) && (addr+old_len) > new_addr)
296 goto out;
298 ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
299 if (ret)
300 goto out;
302 ret = do_munmap(mm, new_addr, new_len);
303 if (ret)
304 goto out;
308 * Always allow a shrinking remap: that just unmaps
309 * the unnecessary pages..
310 * do_munmap does all the needed commit accounting
312 if (old_len >= new_len) {
313 ret = do_munmap(mm, addr+new_len, old_len - new_len);
314 if (ret && old_len != new_len)
315 goto out;
316 ret = addr;
317 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
318 goto out;
319 old_len = new_len;
323 * Ok, we need to grow.. or relocate.
325 ret = -EFAULT;
326 vma = find_vma(mm, addr);
327 if (!vma || vma->vm_start > addr)
328 goto out;
329 if (is_vm_hugetlb_page(vma)) {
330 ret = -EINVAL;
331 goto out;
333 /* We can't remap across vm area boundaries */
334 if (old_len > vma->vm_end - addr)
335 goto out;
337 /* Need to be careful about a growing mapping */
338 if (new_len > old_len) {
339 unsigned long pgoff;
341 if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
342 goto out;
343 pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
344 pgoff += vma->vm_pgoff;
345 if (pgoff + (new_len >> PAGE_SHIFT) < pgoff) {
346 ret = -EINVAL;
347 goto out;
351 if (vma->vm_flags & VM_LOCKED) {
352 unsigned long locked, lock_limit;
353 locked = mm->locked_vm << PAGE_SHIFT;
354 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
355 locked += new_len - old_len;
356 ret = -EAGAIN;
357 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
358 goto out;
360 if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT)) {
361 ret = -ENOMEM;
362 goto out;
365 if (vma->vm_flags & VM_ACCOUNT) {
366 charged = (new_len - old_len) >> PAGE_SHIFT;
367 if (security_vm_enough_memory(charged))
368 goto out_nc;
371 /* old_len exactly to the end of the area..
372 * And we're not relocating the area.
374 if (old_len == vma->vm_end - addr &&
375 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
376 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
377 unsigned long max_addr = TASK_SIZE;
378 if (vma->vm_next)
379 max_addr = vma->vm_next->vm_start;
380 /* can we just expand the current mapping? */
381 if (max_addr - addr >= new_len) {
382 int pages = (new_len - old_len) >> PAGE_SHIFT;
384 vma_adjust(vma, vma->vm_start,
385 addr + new_len, vma->vm_pgoff, NULL);
387 mm->total_vm += pages;
388 vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
389 if (vma->vm_flags & VM_LOCKED) {
390 mm->locked_vm += pages;
391 make_pages_present(addr + old_len,
392 addr + new_len);
394 ret = addr;
395 goto out;
400 * We weren't able to just expand or shrink the area,
401 * we need to create a new one and move it..
403 ret = -ENOMEM;
404 if (flags & MREMAP_MAYMOVE) {
405 if (!(flags & MREMAP_FIXED)) {
406 unsigned long map_flags = 0;
407 if (vma->vm_flags & VM_MAYSHARE)
408 map_flags |= MAP_SHARED;
410 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
411 vma->vm_pgoff, map_flags);
412 if (new_addr & ~PAGE_MASK) {
413 ret = new_addr;
414 goto out;
417 ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
418 if (ret)
419 goto out;
421 ret = move_vma(vma, addr, old_len, new_len, new_addr);
423 out:
424 if (ret & ~PAGE_MASK)
425 vm_unacct_memory(charged);
426 out_nc:
427 return ret;
430 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
431 unsigned long, new_len, unsigned long, flags,
432 unsigned long, new_addr)
434 unsigned long ret;
436 down_write(&current->mm->mmap_sem);
437 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
438 up_write(&current->mm->mmap_sem);
439 return ret;