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