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