[PATCH] Convert i_shared_sem back to a spinlock
[linux-flexiantxendom0-3.2.10.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 inline int
83 can_move_one_pte(pte_t *src, unsigned long new_addr)
84 {
85         int move = 1;
86         if (pte_present(*src)) {
87                 unsigned long pfn = pte_pfn(*src);
88                 if (pfn_valid(pfn)) {
89                         struct page *page = pfn_to_page(pfn);
90                         if (PageAnon(page))
91                                 move = mremap_move_anon_rmap(page, new_addr);
92                 }
93         }
94         return move;
95 }
96
97 static int
98 move_one_page(struct vm_area_struct *vma, unsigned long old_addr,
99                 unsigned long new_addr)
100 {
101         struct mm_struct *mm = vma->vm_mm;
102         int error = 0;
103         pte_t *src, *dst;
104
105         spin_lock(&mm->page_table_lock);
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                 if (!page_table_present(mm, new_addr)) {
114                         pte_unmap_nested(src);
115                         src = NULL;
116                 }
117                 dst = alloc_one_pte_map(mm, new_addr);
118                 if (src == NULL)
119                         src = get_one_pte_map_nested(mm, old_addr);
120                 /*
121                  * Since alloc_one_pte_map can drop and re-acquire
122                  * page_table_lock, we should re-check the src entry...
123                  */
124                 if (src) {
125                         if (!dst)
126                                 error = -ENOMEM;
127                         else if (!can_move_one_pte(src, new_addr))
128                                 error = -EAGAIN;
129                         else {
130                                 pte_t pte;
131                                 pte = ptep_clear_flush(vma, old_addr, src);
132                                 set_pte(dst, pte);
133                         }
134                         pte_unmap_nested(src);
135                 }
136                 if (dst)
137                         pte_unmap(dst);
138         }
139         spin_unlock(&mm->page_table_lock);
140         return error;
141 }
142
143 static unsigned long move_page_tables(struct vm_area_struct *vma,
144                 unsigned long new_addr, unsigned long old_addr,
145                 unsigned long len, int *cows)
146 {
147         unsigned long offset;
148
149         flush_cache_range(vma, old_addr, old_addr + len);
150
151         /*
152          * This is not the clever way to do this, but we're taking the
153          * easy way out on the assumption that most remappings will be
154          * only a few pages.. This also makes error recovery easier.
155          */
156         for (offset = 0; offset < len; offset += PAGE_SIZE) {
157                 int ret = move_one_page(vma, old_addr+offset, new_addr+offset);
158                 /*
159                  * The anonmm objrmap can only track anon page movements
160                  * if the page is exclusive to one mm.  In the rare case
161                  * when mremap move is applied to a shared page, break
162                  * COW (take a copy of the page) to make it exclusive.
163                  * If shared while on swap, page will be copied when
164                  * brought back in (if it's still shared by then).
165                  */
166                 if (ret == -EAGAIN) {
167                         ret = make_page_exclusive(vma, old_addr+offset);
168                         offset -= PAGE_SIZE;
169                         (*cows)++;
170                 }
171                 if (ret)
172                         break;
173                 cond_resched();
174         }
175         return offset;
176 }
177
178 static unsigned long move_vma(struct vm_area_struct *vma,
179                 unsigned long old_addr, unsigned long old_len,
180                 unsigned long new_len, unsigned long new_addr)
181 {
182         struct mm_struct *mm = vma->vm_mm;
183         struct vm_area_struct *new_vma;
184         unsigned long vm_flags = vma->vm_flags;
185         unsigned long new_pgoff;
186         unsigned long moved_len;
187         unsigned long excess = 0;
188         int split = 0;
189         int cows = 0;
190
191         /*
192          * We'd prefer to avoid failure later on in do_munmap:
193          * which may split one vma into three before unmapping.
194          */
195         if (mm->map_count >= sysctl_max_map_count - 3)
196                 return -ENOMEM;
197
198         new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
199         new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
200         if (!new_vma)
201                 return -ENOMEM;
202
203         moved_len = move_page_tables(vma, new_addr, old_addr, old_len, &cows);
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, &cows);
211                 vma = new_vma;
212                 old_len = new_len;
213                 old_addr = new_addr;
214                 new_addr = -ENOMEM;
215         }
216         if (cows)       /* Downgrade or remove this message later */
217                 printk(KERN_WARNING "%s: mremap moved %d cows\n",
218                                                         current->comm, cows);
219
220         /* Conceal VM_ACCOUNT so old reservation is not undone */
221         if (vm_flags & VM_ACCOUNT) {
222                 vma->vm_flags &= ~VM_ACCOUNT;
223                 excess = vma->vm_end - vma->vm_start - old_len;
224                 if (old_addr > vma->vm_start &&
225                     old_addr + old_len < vma->vm_end)
226                         split = 1;
227         }
228
229         if (do_munmap(mm, old_addr, old_len) < 0) {
230                 /* OOM: unable to split vma, just get accounts right */
231                 vm_unacct_memory(excess >> PAGE_SHIFT);
232                 excess = 0;
233         }
234
235         /* Restore VM_ACCOUNT if one or two pieces of vma left */
236         if (excess) {
237                 vma->vm_flags |= VM_ACCOUNT;
238                 if (split)
239                         vma->vm_next->vm_flags |= VM_ACCOUNT;
240         }
241
242         mm->total_vm += new_len >> PAGE_SHIFT;
243         if (vm_flags & VM_LOCKED) {
244                 mm->locked_vm += new_len >> PAGE_SHIFT;
245                 if (new_len > old_len)
246                         make_pages_present(new_addr + old_len,
247                                            new_addr + new_len);
248         }
249
250         return new_addr;
251 }
252
253 /*
254  * Expand (or shrink) an existing mapping, potentially moving it at the
255  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
256  *
257  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
258  * This option implies MREMAP_MAYMOVE.
259  */
260 unsigned long do_mremap(unsigned long addr,
261         unsigned long old_len, unsigned long new_len,
262         unsigned long flags, unsigned long new_addr)
263 {
264         struct vm_area_struct *vma;
265         unsigned long ret = -EINVAL;
266         unsigned long charged = 0;
267
268         if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
269                 goto out;
270
271         if (addr & ~PAGE_MASK)
272                 goto out;
273
274         old_len = PAGE_ALIGN(old_len);
275         new_len = PAGE_ALIGN(new_len);
276
277         /*
278          * We allow a zero old-len as a special case
279          * for DOS-emu "duplicate shm area" thing. But
280          * a zero new-len is nonsensical.
281          */
282         if (!new_len)
283                 goto out;
284
285         /* new_addr is only valid if MREMAP_FIXED is specified */
286         if (flags & MREMAP_FIXED) {
287                 if (new_addr & ~PAGE_MASK)
288                         goto out;
289                 if (!(flags & MREMAP_MAYMOVE))
290                         goto out;
291
292                 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
293                         goto out;
294
295                 /* Check if the location we're moving into overlaps the
296                  * old location at all, and fail if it does.
297                  */
298                 if ((new_addr <= addr) && (new_addr+new_len) > addr)
299                         goto out;
300
301                 if ((addr <= new_addr) && (addr+old_len) > new_addr)
302                         goto out;
303
304                 ret = do_munmap(current->mm, new_addr, new_len);
305                 if (ret)
306                         goto out;
307         }
308
309         /*
310          * Always allow a shrinking remap: that just unmaps
311          * the unnecessary pages..
312          * do_munmap does all the needed commit accounting
313          */
314         if (old_len >= new_len) {
315                 ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
316                 if (ret && old_len != new_len)
317                         goto out;
318                 ret = addr;
319                 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
320                         goto out;
321                 old_len = new_len;
322         }
323
324         /*
325          * Ok, we need to grow..  or relocate.
326          */
327         ret = -EFAULT;
328         vma = find_vma(current->mm, addr);
329         if (!vma || vma->vm_start > addr)
330                 goto out;
331         if (is_vm_hugetlb_page(vma)) {
332                 ret = -EINVAL;
333                 goto out;
334         }
335         /* We can't remap across vm area boundaries */
336         if (old_len > vma->vm_end - addr)
337                 goto out;
338         if (vma->vm_flags & VM_DONTEXPAND) {
339                 if (new_len > old_len)
340                         goto out;
341         }
342         if (vma->vm_flags & VM_LOCKED) {
343                 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
344                 locked += new_len - old_len;
345                 ret = -EAGAIN;
346                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
347                         goto out;
348         }
349         ret = -ENOMEM;
350         if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
351             > current->rlim[RLIMIT_AS].rlim_cur)
352                 goto out;
353
354         if (vma->vm_flags & VM_ACCOUNT) {
355                 charged = (new_len - old_len) >> PAGE_SHIFT;
356                 if (security_vm_enough_memory(charged))
357                         goto out_nc;
358         }
359
360         /* old_len exactly to the end of the area..
361          * And we're not relocating the area.
362          */
363         if (old_len == vma->vm_end - addr &&
364             !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
365             (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
366                 unsigned long max_addr = TASK_SIZE;
367                 if (vma->vm_next)
368                         max_addr = vma->vm_next->vm_start;
369                 /* can we just expand the current mapping? */
370                 if (max_addr - addr >= new_len) {
371                         int pages = (new_len - old_len) >> PAGE_SHIFT;
372                         spin_lock(&vma->vm_mm->page_table_lock);
373                         vma->vm_end = addr + new_len;
374                         spin_unlock(&vma->vm_mm->page_table_lock);
375                         current->mm->total_vm += pages;
376                         if (vma->vm_flags & VM_LOCKED) {
377                                 current->mm->locked_vm += pages;
378                                 make_pages_present(addr + old_len,
379                                                    addr + new_len);
380                         }
381                         ret = addr;
382                         goto out;
383                 }
384         }
385
386         /*
387          * We weren't able to just expand or shrink the area,
388          * we need to create a new one and move it..
389          */
390         ret = -ENOMEM;
391         if (flags & MREMAP_MAYMOVE) {
392                 if (!(flags & MREMAP_FIXED)) {
393                         unsigned long map_flags = 0;
394                         if (vma->vm_flags & VM_MAYSHARE)
395                                 map_flags |= MAP_SHARED;
396
397                         new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
398                                                 vma->vm_pgoff, map_flags);
399                         ret = new_addr;
400                         if (new_addr & ~PAGE_MASK)
401                                 goto out;
402                 }
403                 ret = move_vma(vma, addr, old_len, new_len, new_addr);
404         }
405 out:
406         if (ret & ~PAGE_MASK)
407                 vm_unacct_memory(charged);
408 out_nc:
409         return ret;
410 }
411
412 asmlinkage unsigned long sys_mremap(unsigned long addr,
413         unsigned long old_len, unsigned long new_len,
414         unsigned long flags, unsigned long new_addr)
415 {
416         unsigned long ret;
417
418         down_write(&current->mm->mmap_sem);
419         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
420         up_write(&current->mm->mmap_sem);
421         return ret;
422 }