Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[linux-2.6.git] / mm / fremap.c
1 /*
2  *   linux/mm/fremap.c
3  * 
4  * Explicit pagetable population and nonlinear (random) mappings support.
5  *
6  * started by Ingo Molnar, Copyright (C) 2002, 2003
7  */
8
9 #include <linux/mm.h>
10 #include <linux/swap.h>
11 #include <linux/file.h>
12 #include <linux/mman.h>
13 #include <linux/pagemap.h>
14 #include <linux/swapops.h>
15 #include <linux/rmap.h>
16 #include <linux/module.h>
17 #include <linux/syscalls.h>
18 #include <linux/vs_memory.h>
19
20 #include <asm/mmu_context.h>
21 #include <asm/cacheflush.h>
22 #include <asm/tlbflush.h>
23
24 static int zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
25                         unsigned long addr, pte_t *ptep)
26 {
27         pte_t pte = *ptep;
28         struct page *page = NULL;
29
30         if (pte_present(pte)) {
31                 flush_cache_page(vma, addr, pte_pfn(pte));
32                 pte = ptep_clear_flush(vma, addr, ptep);
33                 page = vm_normal_page(vma, addr, pte);
34                 if (page) {
35                         if (pte_dirty(pte))
36                                 set_page_dirty(page);
37                         page_remove_rmap(page, vma);
38                         page_cache_release(page);
39                 }
40         } else {
41                 if (!pte_file(pte))
42                         free_swap_and_cache(pte_to_swp_entry(pte));
43                 pte_clear(mm, addr, ptep);
44         }
45         return !!page;
46 }
47
48 /*
49  * Install a file page to a given virtual memory address, release any
50  * previously existing mapping.
51  */
52 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
53                 unsigned long addr, struct page *page, pgprot_t prot)
54 {
55         struct inode *inode;
56         pgoff_t size;
57         int err = -ENOMEM;
58         pte_t *pte;
59         pte_t pte_val;
60         spinlock_t *ptl;
61
62         pte = get_locked_pte(mm, addr, &ptl);
63         if (!pte)
64                 goto out;
65
66         /*
67          * This page may have been truncated. Tell the
68          * caller about it.
69          */
70         err = -EINVAL;
71         if (vma->vm_file) {
72                 inode = vma->vm_file->f_mapping->host;
73                 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
74                 if (!page->mapping || page->index >= size)
75                         goto unlock;
76                 err = -ENOMEM;
77                 if (page_mapcount(page) > INT_MAX/2)
78                         goto unlock;
79                 if (!vx_rsspages_avail(mm, 1))
80                         goto unlock;
81         }
82
83         if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
84                 inc_mm_counter(mm, file_rss);
85
86         flush_icache_page(vma, page);
87         pte_val = mk_pte(page, prot);
88         set_pte_at(mm, addr, pte, pte_val);
89         page_add_file_rmap(page);
90         update_mmu_cache(vma, addr, pte_val);
91         lazy_mmu_prot_update(pte_val);
92         err = 0;
93 unlock:
94         pte_unmap_unlock(pte, ptl);
95 out:
96         return err;
97 }
98 EXPORT_SYMBOL(install_page);
99
100 /*
101  * Install a file pte to a given virtual memory address, release any
102  * previously existing mapping.
103  */
104 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
105                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
106 {
107         int err = -ENOMEM;
108         pte_t *pte;
109         pte_t pte_val;
110         spinlock_t *ptl;
111
112         pte = get_locked_pte(mm, addr, &ptl);
113         if (!pte)
114                 goto out;
115
116         if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
117                 update_hiwater_rss(mm);
118                 dec_mm_counter(mm, file_rss);
119         }
120
121         set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
122         pte_val = *pte;
123         /*
124          * We don't need to run update_mmu_cache() here because the "file pte"
125          * being installed by install_file_pte() is not a real pte - it's a
126          * non-present entry (like a swap entry), noting what file offset should
127          * be mapped there when there's a fault (in a non-linear vma where
128          * that's not obvious).
129          */
130         pte_unmap_unlock(pte, ptl);
131         err = 0;
132 out:
133         return err;
134 }
135
136 /***
137  * sys_remap_file_pages - remap arbitrary pages of a shared backing store
138  *                        file within an existing vma.
139  * @start: start of the remapped virtual memory range
140  * @size: size of the remapped virtual memory range
141  * @prot: new protection bits of the range
142  * @pgoff: to be mapped page of the backing store file
143  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
144  *
145  * this syscall works purely via pagetables, so it's the most efficient
146  * way to map the same (large) file into a given virtual window. Unlike
147  * mmap()/mremap() it does not create any new vmas. The new mappings are
148  * also safe across swapout.
149  *
150  * NOTE: the 'prot' parameter right now is ignored, and the vma's default
151  * protection is used. Arbitrary protections might be implemented in the
152  * future.
153  */
154 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
155         unsigned long __prot, unsigned long pgoff, unsigned long flags)
156 {
157         struct mm_struct *mm = current->mm;
158         struct address_space *mapping;
159         unsigned long end = start + size;
160         struct vm_area_struct *vma;
161         int err = -EINVAL;
162         int has_write_lock = 0;
163
164         if (__prot)
165                 return err;
166         /*
167          * Sanitize the syscall parameters:
168          */
169         start = start & PAGE_MASK;
170         size = size & PAGE_MASK;
171
172         /* Does the address range wrap, or is the span zero-sized? */
173         if (start + size <= start)
174                 return err;
175
176         /* Can we represent this offset inside this architecture's pte's? */
177 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
178         if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
179                 return err;
180 #endif
181
182         /* We need down_write() to change vma->vm_flags. */
183         down_read(&mm->mmap_sem);
184  retry:
185         vma = find_vma(mm, start);
186
187         /*
188          * Make sure the vma is shared, that it supports prefaulting,
189          * and that the remapped range is valid and fully within
190          * the single existing vma.  vm_private_data is used as a
191          * swapout cursor in a VM_NONLINEAR vma.
192          */
193         if (vma && (vma->vm_flags & VM_SHARED) &&
194                 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
195                 vma->vm_ops && vma->vm_ops->populate &&
196                         end > start && start >= vma->vm_start &&
197                                 end <= vma->vm_end) {
198
199                 /* Must set VM_NONLINEAR before any pages are populated. */
200                 if (pgoff != linear_page_index(vma, start) &&
201                     !(vma->vm_flags & VM_NONLINEAR)) {
202                         if (!has_write_lock) {
203                                 up_read(&mm->mmap_sem);
204                                 down_write(&mm->mmap_sem);
205                                 has_write_lock = 1;
206                                 goto retry;
207                         }
208                         mapping = vma->vm_file->f_mapping;
209                         spin_lock(&mapping->i_mmap_lock);
210                         flush_dcache_mmap_lock(mapping);
211                         vma->vm_flags |= VM_NONLINEAR;
212                         vma_prio_tree_remove(vma, &mapping->i_mmap);
213                         vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
214                         flush_dcache_mmap_unlock(mapping);
215                         spin_unlock(&mapping->i_mmap_lock);
216                 }
217
218                 err = vma->vm_ops->populate(vma, start, size,
219                                             vma->vm_page_prot,
220                                             pgoff, flags & MAP_NONBLOCK);
221
222                 /*
223                  * We can't clear VM_NONLINEAR because we'd have to do
224                  * it after ->populate completes, and that would prevent
225                  * downgrading the lock.  (Locks can't be upgraded).
226                  */
227         }
228         if (likely(!has_write_lock))
229                 up_read(&mm->mmap_sem);
230         else
231                 up_write(&mm->mmap_sem);
232
233         return err;
234 }
235