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