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