Fedora Core 2 - 1.492
[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
18 #include <asm/mmu_context.h>
19 #include <asm/cacheflush.h>
20 #include <asm/tlbflush.h>
21
22 static inline void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
23                         unsigned long addr, pte_t *ptep)
24 {
25         pte_t pte = *ptep;
26
27         if (pte_none(pte))
28                 return;
29         if (pte_present(pte)) {
30                 unsigned long pfn = pte_pfn(pte);
31
32                 flush_cache_page(vma, addr);
33                 pte = ptep_clear_flush(vma, addr, ptep);
34                 if (pfn_valid(pfn)) {
35                         struct page *page = pfn_to_page(pfn);
36                         if (!PageReserved(page)) {
37                                 if (pte_dirty(pte))
38                                         set_page_dirty(page);
39                                 page_remove_rmap(page);
40                                 page_cache_release(page);
41                                 mm->rss--;
42                         }
43                 }
44         } else {
45                 if (!pte_file(pte))
46                         free_swap_and_cache(pte_to_swp_entry(pte));
47                 pte_clear(ptep);
48         }
49 }
50
51 /*
52  * Install a file page to a given virtual memory address, release any
53  * previously existing mapping.
54  */
55 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
56                 unsigned long addr, struct page *page, pgprot_t prot)
57 {
58         int err = -ENOMEM;
59         pte_t *pte;
60         pgd_t *pgd;
61         pmd_t *pmd;
62         pte_t pte_val;
63
64         pgd = pgd_offset(mm, addr);
65         spin_lock(&mm->page_table_lock);
66
67         pmd = pmd_alloc(mm, pgd, addr);
68         if (!pmd)
69                 goto err_unlock;
70
71         pte = pte_alloc_map(mm, pmd, addr);
72         if (!pte)
73                 goto err_unlock;
74
75         zap_pte(mm, vma, addr, pte);
76
77         mm->rss++;
78         flush_icache_page(vma, page);
79         set_pte(pte, mk_pte(page, prot));
80         page_add_file_rmap(page);
81         pte_val = *pte;
82         pte_unmap(pte);
83         update_mmu_cache(vma, addr, pte_val);
84
85         err = 0;
86 err_unlock:
87         spin_unlock(&mm->page_table_lock);
88         return err;
89 }
90 EXPORT_SYMBOL(install_page);
91
92
93 /*
94  * Install a file pte to a given virtual memory address, release any
95  * previously existing mapping.
96  */
97 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
98                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
99 {
100         int err = -ENOMEM;
101         pte_t *pte;
102         pgd_t *pgd;
103         pmd_t *pmd;
104         pte_t pte_val;
105
106         pgd = pgd_offset(mm, addr);
107         spin_lock(&mm->page_table_lock);
108
109         pmd = pmd_alloc(mm, pgd, addr);
110         if (!pmd)
111                 goto err_unlock;
112
113         pte = pte_alloc_map(mm, pmd, addr);
114         if (!pte)
115                 goto err_unlock;
116
117         zap_pte(mm, vma, addr, pte);
118
119         set_pte(pte, pgoff_to_pte(pgoff));
120         pte_val = *pte;
121         pte_unmap(pte);
122         update_mmu_cache(vma, addr, pte_val);
123         spin_unlock(&mm->page_table_lock);
124         return 0;
125
126 err_unlock:
127         spin_unlock(&mm->page_table_lock);
128         return err;
129 }
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 (unless VM_RESERVED
188          * or VM_LOCKED, but VM_LOCKED could be revoked later on).
189          */
190         if (vma && (vma->vm_flags & VM_SHARED) &&
191                 (!vma->vm_private_data ||
192                         (vma->vm_flags & (VM_NONLINEAR|VM_RESERVED))) &&
193                 vma->vm_ops && vma->vm_ops->populate &&
194                         end > start && start >= vma->vm_start &&
195                                 end <= vma->vm_end) {
196
197                 /* Must set VM_NONLINEAR before any pages are populated. */
198                 if (pgoff != linear_page_index(vma, start) &&
199                     !(vma->vm_flags & VM_NONLINEAR)) {
200                         if (!has_write_lock) {
201                                 up_read(&mm->mmap_sem);
202                                 down_write(&mm->mmap_sem);
203                                 has_write_lock = 1;
204                                 goto retry;
205                         }
206                         mapping = vma->vm_file->f_mapping;
207                         spin_lock(&mapping->i_mmap_lock);
208                         flush_dcache_mmap_lock(mapping);
209                         vma->vm_flags |= VM_NONLINEAR;
210                         vma_prio_tree_remove(vma, &mapping->i_mmap);
211                         vma_prio_tree_init(vma);
212                         list_add_tail(&vma->shared.vm_set.list,
213                                         &mapping->i_mmap_nonlinear);
214                         flush_dcache_mmap_unlock(mapping);
215                         spin_unlock(&mapping->i_mmap_lock);
216                 }
217
218                 err = vma->vm_ops->populate(vma, start, size,
219                                             vma->vm_page_prot,
220                                             pgoff, flags & MAP_NONBLOCK);
221
222                 /*
223                  * We can't clear VM_NONLINEAR because we'd have to do
224                  * it after ->populate completes, and that would prevent
225                  * downgrading the lock.  (Locks can't be upgraded).
226                  */
227         }
228         if (likely(!has_write_lock))
229                 up_read(&mm->mmap_sem);
230         else
231                 up_write(&mm->mmap_sem);
232
233         return err;
234 }
235