vserver 2.0 rc7
[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 inline void 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
29         if (pte_none(pte))
30                 return;
31         if (pte_present(pte)) {
32                 unsigned long pfn = pte_pfn(pte);
33
34                 flush_cache_page(vma, addr, pfn);
35                 pte = ptep_clear_flush(vma, addr, ptep);
36                 if (pfn_valid(pfn)) {
37                         struct page *page = pfn_to_page(pfn);
38                         if (!PageReserved(page)) {
39                                 if (pte_dirty(pte))
40                                         set_page_dirty(page);
41                                 page_remove_rmap(page);
42                                 page_cache_release(page);
43                                 dec_mm_counter(mm, rss);
44                         }
45                 }
46         } else {
47                 if (!pte_file(pte))
48                         free_swap_and_cache(pte_to_swp_entry(pte));
49                 pte_clear(mm, addr, ptep);
50         }
51 }
52
53 /*
54  * Install a file page to a given virtual memory address, release any
55  * previously existing mapping.
56  */
57 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
58                 unsigned long addr, struct page *page, pgprot_t prot)
59 {
60         struct inode *inode;
61         pgoff_t size;
62         int err = -ENOMEM;
63         pte_t *pte;
64         pmd_t *pmd;
65         pud_t *pud;
66         pgd_t *pgd;
67         pte_t pte_val;
68
69         pgd = pgd_offset(mm, addr);
70         spin_lock(&mm->page_table_lock);
71
72         if (!vx_rsspages_avail(mm, 1))
73                 goto err_unlock;
74         
75         pud = pud_alloc(mm, pgd, addr);
76         if (!pud)
77                 goto err_unlock;
78
79         pmd = pmd_alloc(mm, pud, addr);
80         if (!pmd)
81                 goto err_unlock;
82
83         pte = pte_alloc_map(mm, pmd, addr);
84         if (!pte)
85                 goto err_unlock;
86
87         /*
88          * This page may have been truncated. Tell the
89          * caller about it.
90          */
91         err = -EINVAL;
92         inode = vma->vm_file->f_mapping->host;
93         size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
94         if (!page->mapping || page->index >= size)
95                 goto err_unlock;
96
97         zap_pte(mm, vma, addr, pte);
98
99         inc_mm_counter(mm,rss);
100         flush_icache_page(vma, page);
101         set_pte_at(mm, addr, pte, mk_pte(page, prot));
102         page_add_file_rmap(page);
103         pte_val = *pte;
104         pte_unmap(pte);
105         update_mmu_cache(vma, addr, pte_val);
106
107         err = 0;
108 err_unlock:
109         spin_unlock(&mm->page_table_lock);
110         return err;
111 }
112 EXPORT_SYMBOL(install_page);
113
114
115 /*
116  * Install a file pte to a given virtual memory address, release any
117  * previously existing mapping.
118  */
119 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
120                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
121 {
122         int err = -ENOMEM;
123         pte_t *pte;
124         pmd_t *pmd;
125         pud_t *pud;
126         pgd_t *pgd;
127         pte_t pte_val;
128
129         pgd = pgd_offset(mm, addr);
130         spin_lock(&mm->page_table_lock);
131         
132         pud = pud_alloc(mm, pgd, addr);
133         if (!pud)
134                 goto err_unlock;
135
136         pmd = pmd_alloc(mm, pud, addr);
137         if (!pmd)
138                 goto err_unlock;
139
140         pte = pte_alloc_map(mm, pmd, addr);
141         if (!pte)
142                 goto err_unlock;
143
144         zap_pte(mm, vma, addr, pte);
145
146         set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
147         pte_val = *pte;
148         pte_unmap(pte);
149         update_mmu_cache(vma, addr, pte_val);
150         spin_unlock(&mm->page_table_lock);
151         return 0;
152
153 err_unlock:
154         spin_unlock(&mm->page_table_lock);
155         return err;
156 }
157
158
159 /***
160  * sys_remap_file_pages - remap arbitrary pages of a shared backing store
161  *                        file within an existing vma.
162  * @start: start of the remapped virtual memory range
163  * @size: size of the remapped virtual memory range
164  * @prot: new protection bits of the range
165  * @pgoff: to be mapped page of the backing store file
166  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
167  *
168  * this syscall works purely via pagetables, so it's the most efficient
169  * way to map the same (large) file into a given virtual window. Unlike
170  * mmap()/mremap() it does not create any new vmas. The new mappings are
171  * also safe across swapout.
172  *
173  * NOTE: the 'prot' parameter right now is ignored, and the vma's default
174  * protection is used. Arbitrary protections might be implemented in the
175  * future.
176  */
177 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
178         unsigned long __prot, unsigned long pgoff, unsigned long flags)
179 {
180         struct mm_struct *mm = current->mm;
181         struct address_space *mapping;
182         unsigned long end = start + size;
183         struct vm_area_struct *vma;
184         int err = -EINVAL;
185         int has_write_lock = 0;
186
187         if (__prot)
188                 return err;
189         /*
190          * Sanitize the syscall parameters:
191          */
192         start = start & PAGE_MASK;
193         size = size & PAGE_MASK;
194
195         /* Does the address range wrap, or is the span zero-sized? */
196         if (start + size <= start)
197                 return err;
198
199         /* Can we represent this offset inside this architecture's pte's? */
200 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
201         if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
202                 return err;
203 #endif
204
205         /* We need down_write() to change vma->vm_flags. */
206         down_read(&mm->mmap_sem);
207  retry:
208         vma = find_vma(mm, start);
209
210         /*
211          * Make sure the vma is shared, that it supports prefaulting,
212          * and that the remapped range is valid and fully within
213          * the single existing vma.  vm_private_data is used as a
214          * swapout cursor in a VM_NONLINEAR vma (unless VM_RESERVED
215          * or VM_LOCKED, but VM_LOCKED could be revoked later on).
216          */
217         if (vma && (vma->vm_flags & VM_SHARED) &&
218                 (!vma->vm_private_data ||
219                         (vma->vm_flags & (VM_NONLINEAR|VM_RESERVED))) &&
220                 vma->vm_ops && vma->vm_ops->populate &&
221                         end > start && start >= vma->vm_start &&
222                                 end <= vma->vm_end) {
223
224                 /* Must set VM_NONLINEAR before any pages are populated. */
225                 if (pgoff != linear_page_index(vma, start) &&
226                     !(vma->vm_flags & VM_NONLINEAR)) {
227                         if (!has_write_lock) {
228                                 up_read(&mm->mmap_sem);
229                                 down_write(&mm->mmap_sem);
230                                 has_write_lock = 1;
231                                 goto retry;
232                         }
233                         mapping = vma->vm_file->f_mapping;
234                         spin_lock(&mapping->i_mmap_lock);
235                         flush_dcache_mmap_lock(mapping);
236                         vma->vm_flags |= VM_NONLINEAR;
237                         vma_prio_tree_remove(vma, &mapping->i_mmap);
238                         vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
239                         flush_dcache_mmap_unlock(mapping);
240                         spin_unlock(&mapping->i_mmap_lock);
241                 }
242
243                 err = vma->vm_ops->populate(vma, start, size,
244                                             vma->vm_page_prot,
245                                             pgoff, flags & MAP_NONBLOCK);
246
247                 /*
248                  * We can't clear VM_NONLINEAR because we'd have to do
249                  * it after ->populate completes, and that would prevent
250                  * downgrading the lock.  (Locks can't be upgraded).
251                  */
252         }
253         if (likely(!has_write_lock))
254                 up_read(&mm->mmap_sem);
255         else
256                 up_write(&mm->mmap_sem);
257
258         return err;
259 }
260