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