Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[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         }
80         err = -ENOMEM;
81         if (!vx_rsspages_avail(mm, 1))
82                 goto unlock;
83
84         if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
85                 inc_mm_counter(mm, file_rss);
86
87         flush_icache_page(vma, page);
88         pte_val = mk_pte(page, prot);
89         set_pte_at(mm, addr, pte, pte_val);
90         page_add_file_rmap(page);
91         update_mmu_cache(vma, addr, pte_val);
92         lazy_mmu_prot_update(pte_val);
93         err = 0;
94 unlock:
95         pte_unmap_unlock(pte, ptl);
96 out:
97         return err;
98 }
99 EXPORT_SYMBOL(install_page);
100
101 /*
102  * Install a file pte to a given virtual memory address, release any
103  * previously existing mapping.
104  */
105 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
106                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
107 {
108         int err = -ENOMEM;
109         pte_t *pte;
110         pte_t pte_val;
111         spinlock_t *ptl;
112
113         pte = get_locked_pte(mm, addr, &ptl);
114         if (!pte)
115                 goto out;
116
117         if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
118                 update_hiwater_rss(mm);
119                 dec_mm_counter(mm, file_rss);
120         }
121
122         set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
123         pte_val = *pte;
124         /*
125          * We don't need to run update_mmu_cache() here because the "file pte"
126          * being installed by install_file_pte() is not a real pte - it's a
127          * non-present entry (like a swap entry), noting what file offset should
128          * be mapped there when there's a fault (in a non-linear vma where
129          * that's not obvious).
130          */
131         pte_unmap_unlock(pte, ptl);
132         err = 0;
133 out:
134         return err;
135 }
136
137 /***
138  * sys_remap_file_pages - remap arbitrary pages of a shared backing store
139  *                        file within an existing vma.
140  * @start: start of the remapped virtual memory range
141  * @size: size of the remapped virtual memory range
142  * @prot: new protection bits of the range
143  * @pgoff: to be mapped page of the backing store file
144  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
145  *
146  * this syscall works purely via pagetables, so it's the most efficient
147  * way to map the same (large) file into a given virtual window. Unlike
148  * mmap()/mremap() it does not create any new vmas. The new mappings are
149  * also safe across swapout.
150  *
151  * NOTE: the 'prot' parameter right now is ignored, and the vma's default
152  * protection is used. Arbitrary protections might be implemented in the
153  * future.
154  */
155 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
156         unsigned long __prot, unsigned long pgoff, unsigned long flags)
157 {
158         struct mm_struct *mm = current->mm;
159         struct address_space *mapping;
160         unsigned long end = start + size;
161         struct vm_area_struct *vma;
162         int err = -EINVAL;
163         int has_write_lock = 0;
164
165         if (__prot)
166                 return err;
167         /*
168          * Sanitize the syscall parameters:
169          */
170         start = start & PAGE_MASK;
171         size = size & PAGE_MASK;
172
173         /* Does the address range wrap, or is the span zero-sized? */
174         if (start + size <= start)
175                 return err;
176
177         /* Can we represent this offset inside this architecture's pte's? */
178 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
179         if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
180                 return err;
181 #endif
182
183         /* We need down_write() to change vma->vm_flags. */
184         down_read(&mm->mmap_sem);
185  retry:
186         vma = find_vma(mm, start);
187
188         /*
189          * Make sure the vma is shared, that it supports prefaulting,
190          * and that the remapped range is valid and fully within
191          * the single existing vma.  vm_private_data is used as a
192          * swapout cursor in a VM_NONLINEAR vma.
193          */
194         if (vma && (vma->vm_flags & VM_SHARED) &&
195                 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
196                 vma->vm_ops && vma->vm_ops->populate &&
197                         end > start && start >= vma->vm_start &&
198                                 end <= vma->vm_end) {
199
200                 /* Must set VM_NONLINEAR before any pages are populated. */
201                 if (pgoff != linear_page_index(vma, start) &&
202                     !(vma->vm_flags & VM_NONLINEAR)) {
203                         if (!has_write_lock) {
204                                 up_read(&mm->mmap_sem);
205                                 down_write(&mm->mmap_sem);
206                                 has_write_lock = 1;
207                                 goto retry;
208                         }
209                         mapping = vma->vm_file->f_mapping;
210                         spin_lock(&mapping->i_mmap_lock);
211                         flush_dcache_mmap_lock(mapping);
212                         vma->vm_flags |= VM_NONLINEAR;
213                         vma_prio_tree_remove(vma, &mapping->i_mmap);
214                         vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
215                         flush_dcache_mmap_unlock(mapping);
216                         spin_unlock(&mapping->i_mmap_lock);
217                 }
218
219                 err = vma->vm_ops->populate(vma, start, size,
220                                             vma->vm_page_prot,
221                                             pgoff, flags & MAP_NONBLOCK);
222
223                 /*
224                  * We can't clear VM_NONLINEAR because we'd have to do
225                  * it after ->populate completes, and that would prevent
226                  * downgrading the lock.  (Locks can't be upgraded).
227                  */
228         }
229         if (likely(!has_write_lock))
230                 up_read(&mm->mmap_sem);
231         else
232                 up_write(&mm->mmap_sem);
233
234         return err;
235 }
236