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