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