4 * Explicit pagetable population and nonlinear (random) mappings support.
6 * started by Ingo Molnar, Copyright (C) 2002, 2003
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>
19 #include <asm/mmu_context.h>
20 #include <asm/cacheflush.h>
21 #include <asm/tlbflush.h>
23 static inline void zap_pte(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
24 unsigned long addr
, pte_t
*ptep
)
30 if (pte_present(pte
)) {
31 unsigned long pfn
= pte_pfn(pte
);
33 flush_cache_page(vma
, addr
, pfn
);
34 pte
= ptep_clear_flush(vma
, addr
, ptep
);
36 struct page
*page
= pfn_to_page(pfn
);
37 if (!PageReserved(page
)) {
40 page_remove_rmap(page
);
41 page_cache_release(page
);
42 dec_mm_counter(mm
, rss
);
47 free_swap_and_cache(pte_to_swp_entry(pte
));
48 pte_clear(mm
, addr
, ptep
);
53 * Install a file page to a given virtual memory address, release any
54 * previously existing mapping.
56 int install_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
57 unsigned long addr
, struct page
*page
, pgprot_t prot
)
68 pgd
= pgd_offset(mm
, addr
);
69 spin_lock(&mm
->page_table_lock
);
71 pud
= pud_alloc(mm
, pgd
, addr
);
75 pmd
= pmd_alloc(mm
, pud
, addr
);
79 pte
= pte_alloc_map(mm
, pmd
, addr
);
84 * This page may have been truncated. Tell the
88 inode
= vma
->vm_file
->f_mapping
->host
;
89 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
90 if (!page
->mapping
|| page
->index
>= size
)
93 if (page_mapcount(page
) > INT_MAX
/2)
96 zap_pte(mm
, vma
, addr
, pte
);
98 inc_mm_counter(mm
,rss
);
99 flush_icache_page(vma
, page
);
100 set_pte_at(mm
, addr
, pte
, mk_pte(page
, prot
));
101 page_add_file_rmap(page
);
104 update_mmu_cache(vma
, addr
, pte_val
);
108 spin_unlock(&mm
->page_table_lock
);
111 EXPORT_SYMBOL(install_page
);
115 * Install a file pte to a given virtual memory address, release any
116 * previously existing mapping.
118 int install_file_pte(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
119 unsigned long addr
, unsigned long pgoff
, pgprot_t prot
)
128 pgd
= pgd_offset(mm
, addr
);
129 spin_lock(&mm
->page_table_lock
);
131 pud
= pud_alloc(mm
, pgd
, addr
);
135 pmd
= pmd_alloc(mm
, pud
, addr
);
139 pte
= pte_alloc_map(mm
, pmd
, addr
);
143 zap_pte(mm
, vma
, addr
, pte
);
145 set_pte_at(mm
, addr
, pte
, pgoff_to_pte(pgoff
));
148 update_mmu_cache(vma
, addr
, pte_val
);
149 spin_unlock(&mm
->page_table_lock
);
153 spin_unlock(&mm
->page_table_lock
);
159 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
160 * file within an existing vma.
161 * @start: start of the remapped virtual memory range
162 * @size: size of the remapped virtual memory range
163 * @prot: new protection bits of the range
164 * @pgoff: to be mapped page of the backing store file
165 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
167 * this syscall works purely via pagetables, so it's the most efficient
168 * way to map the same (large) file into a given virtual window. Unlike
169 * mmap()/mremap() it does not create any new vmas. The new mappings are
170 * also safe across swapout.
172 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
173 * protection is used. Arbitrary protections might be implemented in the
176 asmlinkage
long sys_remap_file_pages(unsigned long start
, unsigned long size
,
177 unsigned long __prot
, unsigned long pgoff
, unsigned long flags
)
179 struct mm_struct
*mm
= current
->mm
;
180 struct address_space
*mapping
;
181 unsigned long end
= start
+ size
;
182 struct vm_area_struct
*vma
;
184 int has_write_lock
= 0;
189 * Sanitize the syscall parameters:
191 start
= start
& PAGE_MASK
;
192 size
= size
& PAGE_MASK
;
194 /* Does the address range wrap, or is the span zero-sized? */
195 if (start
+ size
<= start
)
198 /* Can we represent this offset inside this architecture's pte's? */
199 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
200 if (pgoff
+ (size
>> PAGE_SHIFT
) >= (1UL << PTE_FILE_MAX_BITS
))
204 /* We need down_write() to change vma->vm_flags. */
205 down_read(&mm
->mmap_sem
);
207 vma
= find_vma(mm
, start
);
210 * Make sure the vma is shared, that it supports prefaulting,
211 * and that the remapped range is valid and fully within
212 * the single existing vma. vm_private_data is used as a
213 * swapout cursor in a VM_NONLINEAR vma (unless VM_RESERVED
214 * or VM_LOCKED, but VM_LOCKED could be revoked later on).
216 if (vma
&& (vma
->vm_flags
& VM_SHARED
) &&
217 (!vma
->vm_private_data
||
218 (vma
->vm_flags
& (VM_NONLINEAR
|VM_RESERVED
))) &&
219 vma
->vm_ops
&& vma
->vm_ops
->populate
&&
220 end
> start
&& start
>= vma
->vm_start
&&
221 end
<= vma
->vm_end
) {
223 /* Must set VM_NONLINEAR before any pages are populated. */
224 if (pgoff
!= linear_page_index(vma
, start
) &&
225 !(vma
->vm_flags
& VM_NONLINEAR
)) {
226 if (!has_write_lock
) {
227 up_read(&mm
->mmap_sem
);
228 down_write(&mm
->mmap_sem
);
232 mapping
= vma
->vm_file
->f_mapping
;
233 spin_lock(&mapping
->i_mmap_lock
);
234 flush_dcache_mmap_lock(mapping
);
235 vma
->vm_flags
|= VM_NONLINEAR
;
236 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
237 vma_nonlinear_insert(vma
, &mapping
->i_mmap_nonlinear
);
238 flush_dcache_mmap_unlock(mapping
);
239 spin_unlock(&mapping
->i_mmap_lock
);
242 err
= vma
->vm_ops
->populate(vma
, start
, size
,
244 pgoff
, flags
& MAP_NONBLOCK
);
247 * We can't clear VM_NONLINEAR because we'd have to do
248 * it after ->populate completes, and that would prevent
249 * downgrading the lock. (Locks can't be upgraded).
252 if (likely(!has_write_lock
))
253 up_read(&mm
->mmap_sem
);
255 up_write(&mm
->mmap_sem
);