patch-2.6.6-vs1.9.0
[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/rmap.h>
19 #include <linux/security.h>
20
21 #include <asm/uaccess.h>
22 #include <asm/pgalloc.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 inline int page_table_present(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 0;
67         pmd = pmd_offset(pgd, addr);
68         return pmd_present(*pmd);
69 }
70
71 static inline pte_t *alloc_one_pte_map(struct mm_struct *mm, unsigned long addr)
72 {
73         pmd_t *pmd;
74         pte_t *pte = NULL;
75
76         pmd = pmd_alloc(mm, pgd_offset(mm, addr), addr);
77         if (pmd)
78                 pte = pte_alloc_map(mm, pmd, addr);
79         return pte;
80 }
81
82 static void
83 copy_one_pte(struct vm_area_struct *vma, unsigned long old_addr,
84              pte_t *src, pte_t *dst, struct pte_chain **pte_chainp)
85 {
86         pte_t pte = ptep_clear_flush(vma, old_addr, src);
87         set_pte(dst, pte);
88
89         if (pte_present(pte)) {
90                 unsigned long pfn = pte_pfn(pte);
91                 if (pfn_valid(pfn)) {
92                         struct page *page = pfn_to_page(pfn);
93                         page_remove_rmap(page, src);
94                         *pte_chainp = page_add_rmap(page, dst, *pte_chainp);
95                 }
96         }
97 }
98
99 static int
100 move_one_page(struct vm_area_struct *vma, unsigned long old_addr,
101                 unsigned long new_addr)
102 {
103         struct mm_struct *mm = vma->vm_mm;
104         int error = 0;
105         pte_t *src, *dst;
106         struct pte_chain *pte_chain;
107
108         pte_chain = pte_chain_alloc(GFP_KERNEL);
109         if (!pte_chain) {
110                 error = -ENOMEM;
111                 goto out;
112         }
113         spin_lock(&mm->page_table_lock);
114         src = get_one_pte_map_nested(mm, old_addr);
115         if (src) {
116                 /*
117                  * Look to see whether alloc_one_pte_map needs to perform a
118                  * memory allocation.  If it does then we need to drop the
119                  * atomic kmap
120                  */
121                 if (!page_table_present(mm, new_addr)) {
122                         pte_unmap_nested(src);
123                         src = NULL;
124                 }
125                 dst = alloc_one_pte_map(mm, new_addr);
126                 if (src == NULL)
127                         src = get_one_pte_map_nested(mm, old_addr);
128                 /*
129                  * Since alloc_one_pte_map can drop and re-acquire
130                  * page_table_lock, we should re-check the src entry...
131                  */
132                 if (src) {
133                         if (dst)
134                                 copy_one_pte(vma, old_addr, src,
135                                                 dst, &pte_chain);
136                         else
137                                 error = -ENOMEM;
138                         pte_unmap_nested(src);
139                 }
140                 if (dst)
141                         pte_unmap(dst);
142         }
143         spin_unlock(&mm->page_table_lock);
144         pte_chain_free(pte_chain);
145 out:
146         return error;
147 }
148
149 static unsigned long move_page_tables(struct vm_area_struct *vma,
150         unsigned long new_addr, unsigned long old_addr, 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         }
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 address_space *mapping = NULL;
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         if (vma->vm_file) {
194                 /*
195                  * Subtle point from Rajesh Venkatasubramanian: before
196                  * moving file-based ptes, we must lock vmtruncate out,
197                  * since it might clean the dst vma before the src vma,
198                  * and we propagate stale pages into the dst afterward.
199                  */
200                 mapping = vma->vm_file->f_mapping;
201                 down(&mapping->i_shared_sem);
202         }
203         moved_len = move_page_tables(vma, new_addr, old_addr, old_len);
204         if (moved_len < old_len) {
205                 /*
206                  * On error, move entries back from new area to old,
207                  * which will succeed since page tables still there,
208                  * and then proceed to unmap new area instead of old.
209                  */
210                 move_page_tables(new_vma, old_addr, new_addr, moved_len);
211                 vma = new_vma;
212                 old_len = new_len;
213                 old_addr = new_addr;
214                 new_addr = -ENOMEM;
215         }
216         if (mapping)
217                 up(&mapping->i_shared_sem);
218
219         /* Conceal VM_ACCOUNT so old reservation is not undone */
220         if (vm_flags & VM_ACCOUNT) {
221                 vma->vm_flags &= ~VM_ACCOUNT;
222                 excess = vma->vm_end - vma->vm_start - old_len;
223                 if (old_addr > vma->vm_start &&
224                     old_addr + old_len < vma->vm_end)
225                         split = 1;
226         }
227
228         if (do_munmap(mm, old_addr, old_len) < 0) {
229                 /* OOM: unable to split vma, just get accounts right */
230                 vm_unacct_memory(excess >> PAGE_SHIFT);
231                 excess = 0;
232         }
233
234         /* Restore VM_ACCOUNT if one or two pieces of vma left */
235         if (excess) {
236                 vma->vm_flags |= VM_ACCOUNT;
237                 if (split)
238                         vma->vm_next->vm_flags |= VM_ACCOUNT;
239         }
240
241         // mm->total_vm += new_len >> PAGE_SHIFT;
242         vx_vmpages_add(mm, new_len >> PAGE_SHIFT);
243         if (vm_flags & VM_LOCKED) {
244                 // mm->locked_vm += new_len >> PAGE_SHIFT;
245                 vx_vmlocked_add(mm, new_len >> PAGE_SHIFT);
246                 if (new_len > old_len)
247                         make_pages_present(new_addr + old_len,
248                                            new_addr + new_len);
249         }
250
251         return new_addr;
252 }
253
254 /*
255  * Expand (or shrink) an existing mapping, potentially moving it at the
256  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
257  *
258  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
259  * This option implies MREMAP_MAYMOVE.
260  */
261 unsigned long do_mremap(unsigned long addr,
262         unsigned long old_len, unsigned long new_len,
263         unsigned long flags, unsigned long new_addr)
264 {
265         struct vm_area_struct *vma;
266         unsigned long ret = -EINVAL;
267         unsigned long charged = 0;
268
269         if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
270                 goto out;
271
272         if (addr & ~PAGE_MASK)
273                 goto out;
274
275         old_len = PAGE_ALIGN(old_len);
276         new_len = PAGE_ALIGN(new_len);
277
278         /*
279          * We allow a zero old-len as a special case
280          * for DOS-emu "duplicate shm area" thing. But
281          * a zero new-len is nonsensical.
282          */
283         if (!new_len)
284                 goto out;
285
286         /* new_addr is only valid if MREMAP_FIXED is specified */
287         if (flags & MREMAP_FIXED) {
288                 if (new_addr & ~PAGE_MASK)
289                         goto out;
290                 if (!(flags & MREMAP_MAYMOVE))
291                         goto out;
292
293                 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
294                         goto out;
295
296                 /* Check if the location we're moving into overlaps the
297                  * old location at all, and fail if it does.
298                  */
299                 if ((new_addr <= addr) && (new_addr+new_len) > addr)
300                         goto out;
301
302                 if ((addr <= new_addr) && (addr+old_len) > new_addr)
303                         goto out;
304
305                 ret = do_munmap(current->mm, new_addr, new_len);
306                 if (ret)
307                         goto out;
308         }
309
310         /*
311          * Always allow a shrinking remap: that just unmaps
312          * the unnecessary pages..
313          * do_munmap does all the needed commit accounting
314          */
315         if (old_len >= new_len) {
316                 ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
317                 if (ret && old_len != new_len)
318                         goto out;
319                 ret = addr;
320                 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
321                         goto out;
322                 old_len = new_len;
323         }
324
325         /*
326          * Ok, we need to grow..  or relocate.
327          */
328         ret = -EFAULT;
329         vma = find_vma(current->mm, addr);
330         if (!vma || vma->vm_start > addr)
331                 goto out;
332         if (is_vm_hugetlb_page(vma)) {
333                 ret = -EINVAL;
334                 goto out;
335         }
336         /* We can't remap across vm area boundaries */
337         if (old_len > vma->vm_end - addr)
338                 goto out;
339         if (vma->vm_flags & VM_DONTEXPAND) {
340                 if (new_len > old_len)
341                         goto out;
342         }
343         if (vma->vm_flags & VM_LOCKED) {
344                 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
345                 locked += new_len - old_len;
346                 ret = -EAGAIN;
347                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
348                         goto out;
349         }
350         ret = -ENOMEM;
351         if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
352             > current->rlim[RLIMIT_AS].rlim_cur)
353                 goto out;
354         /* check context space, maybe only Private writable mapping? */
355         if (!vx_vmpages_avail(current->mm, (new_len - old_len) >> PAGE_SHIFT))
356                 goto out;
357
358         if (vma->vm_flags & VM_ACCOUNT) {
359                 charged = (new_len - old_len) >> PAGE_SHIFT;
360                 if (security_vm_enough_memory(charged))
361                         goto out_nc;
362         }
363
364         /* old_len exactly to the end of the area..
365          * And we're not relocating the area.
366          */
367         if (old_len == vma->vm_end - addr &&
368             !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
369             (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
370                 unsigned long max_addr = TASK_SIZE;
371                 if (vma->vm_next)
372                         max_addr = vma->vm_next->vm_start;
373                 /* can we just expand the current mapping? */
374                 if (max_addr - addr >= new_len) {
375                         int pages = (new_len - old_len) >> PAGE_SHIFT;
376                         spin_lock(&vma->vm_mm->page_table_lock);
377                         vma->vm_end = addr + new_len;
378                         spin_unlock(&vma->vm_mm->page_table_lock);
379                         // current->mm->total_vm += pages;
380                         vx_vmpages_add(current->mm, pages);
381                         if (vma->vm_flags & VM_LOCKED) {
382                                 // current->mm->locked_vm += pages;
383                                 vx_vmlocked_add(current->mm, pages);
384                                 make_pages_present(addr + old_len,
385                                                    addr + new_len);
386                         }
387                         ret = addr;
388                         goto out;
389                 }
390         }
391
392         /*
393          * We weren't able to just expand or shrink the area,
394          * we need to create a new one and move it..
395          */
396         ret = -ENOMEM;
397         if (flags & MREMAP_MAYMOVE) {
398                 if (!(flags & MREMAP_FIXED)) {
399                         unsigned long map_flags = 0;
400                         if (vma->vm_flags & VM_MAYSHARE)
401                                 map_flags |= MAP_SHARED;
402
403                         new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
404                                                 vma->vm_pgoff, map_flags);
405                         ret = new_addr;
406                         if (new_addr & ~PAGE_MASK)
407                                 goto out;
408                 }
409                 ret = move_vma(vma, addr, old_len, new_len, new_addr);
410         }
411 out:
412         if (ret & ~PAGE_MASK)
413                 vm_unacct_memory(charged);
414 out_nc:
415         return ret;
416 }
417
418 asmlinkage unsigned long sys_mremap(unsigned long addr,
419         unsigned long old_len, unsigned long new_len,
420         unsigned long flags, unsigned long new_addr)
421 {
422         unsigned long ret;
423
424         down_write(&current->mm->mmap_sem);
425         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
426         up_write(&current->mm->mmap_sem);
427         return ret;
428 }