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 zap_pte(mm, vma, addr, pte);
95 inc_mm_counter(mm,rss);
96 flush_icache_page(vma, page);
97 set_pte_at(mm, addr, pte, mk_pte(page, prot));
98 page_add_file_rmap(page);
101 update_mmu_cache(vma, addr, pte_val);
105 spin_unlock(&mm->page_table_lock);
108 EXPORT_SYMBOL(install_page);
112 * Install a file pte to a given virtual memory address, release any
113 * previously existing mapping.
115 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
116 unsigned long addr, unsigned long pgoff, pgprot_t prot)
125 pgd = pgd_offset(mm, addr);
126 spin_lock(&mm->page_table_lock);
128 pud = pud_alloc(mm, pgd, addr);
132 pmd = pmd_alloc(mm, pud, addr);
136 pte = pte_alloc_map(mm, pmd, addr);
140 zap_pte(mm, vma, addr, pte);
142 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
145 update_mmu_cache(vma, addr, pte_val);
146 spin_unlock(&mm->page_table_lock);
150 spin_unlock(&mm->page_table_lock);
156 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
157 * file within an existing vma.
158 * @start: start of the remapped virtual memory range
159 * @size: size of the remapped virtual memory range
160 * @prot: new protection bits of the range
161 * @pgoff: to be mapped page of the backing store file
162 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
164 * this syscall works purely via pagetables, so it's the most efficient
165 * way to map the same (large) file into a given virtual window. Unlike
166 * mmap()/mremap() it does not create any new vmas. The new mappings are
167 * also safe across swapout.
169 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
170 * protection is used. Arbitrary protections might be implemented in the
173 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
174 unsigned long __prot, unsigned long pgoff, unsigned long flags)
176 struct mm_struct *mm = current->mm;
177 struct address_space *mapping;
178 unsigned long end = start + size;
179 struct vm_area_struct *vma;
181 int has_write_lock = 0;
186 * Sanitize the syscall parameters:
188 start = start & PAGE_MASK;
189 size = size & PAGE_MASK;
191 /* Does the address range wrap, or is the span zero-sized? */
192 if (start + size <= start)
195 /* Can we represent this offset inside this architecture's pte's? */
196 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
197 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
201 /* We need down_write() to change vma->vm_flags. */
202 down_read(&mm->mmap_sem);
204 vma = find_vma(mm, start);
207 * Make sure the vma is shared, that it supports prefaulting,
208 * and that the remapped range is valid and fully within
209 * the single existing vma. vm_private_data is used as a
210 * swapout cursor in a VM_NONLINEAR vma (unless VM_RESERVED
211 * or VM_LOCKED, but VM_LOCKED could be revoked later on).
213 if (vma && (vma->vm_flags & VM_SHARED) &&
214 (!vma->vm_private_data ||
215 (vma->vm_flags & (VM_NONLINEAR|VM_RESERVED))) &&
216 vma->vm_ops && vma->vm_ops->populate &&
217 end > start && start >= vma->vm_start &&
218 end <= vma->vm_end) {
220 /* Must set VM_NONLINEAR before any pages are populated. */
221 if (pgoff != linear_page_index(vma, start) &&
222 !(vma->vm_flags & VM_NONLINEAR)) {
223 if (!has_write_lock) {
224 up_read(&mm->mmap_sem);
225 down_write(&mm->mmap_sem);
229 mapping = vma->vm_file->f_mapping;
230 spin_lock(&mapping->i_mmap_lock);
231 flush_dcache_mmap_lock(mapping);
232 vma->vm_flags |= VM_NONLINEAR;
233 vma_prio_tree_remove(vma, &mapping->i_mmap);
234 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
235 flush_dcache_mmap_unlock(mapping);
236 spin_unlock(&mapping->i_mmap_lock);
239 err = vma->vm_ops->populate(vma, start, size,
241 pgoff, flags & MAP_NONBLOCK);
244 * We can't clear VM_NONLINEAR because we'd have to do
245 * it after ->populate completes, and that would prevent
246 * downgrading the lock. (Locks can't be upgraded).
249 if (likely(!has_write_lock))
250 up_read(&mm->mmap_sem);
252 up_write(&mm->mmap_sem);