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