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