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