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