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