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>
18 #include <linux/vs_memory.h>
20 #include <asm/mmu_context.h>
21 #include <asm/cacheflush.h>
22 #include <asm/tlbflush.h>
24 static inline void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
25 unsigned long addr, pte_t *ptep)
31 if (pte_present(pte)) {
32 unsigned long pfn = pte_pfn(pte);
34 flush_cache_page(vma, addr);
35 pte = ptep_clear_flush(vma, addr, ptep);
37 struct page *page = pfn_to_page(pfn);
38 if (!PageReserved(page)) {
41 page_remove_rmap(page);
42 page_cache_release(page);
48 free_swap_and_cache(pte_to_swp_entry(pte));
54 * Install a file page to a given virtual memory address, release any
55 * previously existing mapping.
57 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
58 unsigned long addr, struct page *page, pgprot_t prot)
69 pgd = pgd_offset(mm, addr);
70 spin_lock(&mm->page_table_lock);
72 if (!vx_rsspages_avail(mm, 1))
75 pud = pud_alloc(mm, pgd, addr);
79 pmd = pmd_alloc(mm, pud, addr);
83 pte = pte_alloc_map(mm, pmd, addr);
88 * This page may have been truncated. Tell the
92 inode = vma->vm_file->f_mapping->host;
93 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
94 if (!page->mapping || page->index >= size)
97 zap_pte(mm, vma, addr, pte);
100 flush_icache_page(vma, page);
101 set_pte(pte, mk_pte(page, prot));
102 page_add_file_rmap(page);
105 update_mmu_cache(vma, addr, pte_val);
109 spin_unlock(&mm->page_table_lock);
112 EXPORT_SYMBOL(install_page);
116 * Install a file pte to a given virtual memory address, release any
117 * previously existing mapping.
119 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
120 unsigned long addr, unsigned long pgoff, pgprot_t prot)
129 pgd = pgd_offset(mm, addr);
130 spin_lock(&mm->page_table_lock);
132 pud = pud_alloc(mm, pgd, addr);
136 pmd = pmd_alloc(mm, pud, addr);
140 pte = pte_alloc_map(mm, pmd, addr);
144 zap_pte(mm, vma, addr, pte);
146 set_pte(pte, pgoff_to_pte(pgoff));
149 update_mmu_cache(vma, addr, pte_val);
150 spin_unlock(&mm->page_table_lock);
154 spin_unlock(&mm->page_table_lock);
160 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
161 * file within an existing vma.
162 * @start: start of the remapped virtual memory range
163 * @size: size of the remapped virtual memory range
164 * @prot: new protection bits of the range
165 * @pgoff: to be mapped page of the backing store file
166 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
168 * this syscall works purely via pagetables, so it's the most efficient
169 * way to map the same (large) file into a given virtual window. Unlike
170 * mmap()/mremap() it does not create any new vmas. The new mappings are
171 * also safe across swapout.
173 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
174 * protection is used. Arbitrary protections might be implemented in the
177 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
178 unsigned long __prot, unsigned long pgoff, unsigned long flags)
180 struct mm_struct *mm = current->mm;
181 struct address_space *mapping;
182 unsigned long end = start + size;
183 struct vm_area_struct *vma;
185 int has_write_lock = 0;
190 * Sanitize the syscall parameters:
192 start = start & PAGE_MASK;
193 size = size & PAGE_MASK;
195 /* Does the address range wrap, or is the span zero-sized? */
196 if (start + size <= start)
199 /* Can we represent this offset inside this architecture's pte's? */
200 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
201 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
205 /* We need down_write() to change vma->vm_flags. */
206 down_read(&mm->mmap_sem);
208 vma = find_vma(mm, start);
211 * Make sure the vma is shared, that it supports prefaulting,
212 * and that the remapped range is valid and fully within
213 * the single existing vma. vm_private_data is used as a
214 * swapout cursor in a VM_NONLINEAR vma (unless VM_RESERVED
215 * or VM_LOCKED, but VM_LOCKED could be revoked later on).
217 if (vma && (vma->vm_flags & VM_SHARED) &&
218 (!vma->vm_private_data ||
219 (vma->vm_flags & (VM_NONLINEAR|VM_RESERVED))) &&
220 vma->vm_ops && vma->vm_ops->populate &&
221 end > start && start >= vma->vm_start &&
222 end <= vma->vm_end) {
224 /* Must set VM_NONLINEAR before any pages are populated. */
225 if (pgoff != linear_page_index(vma, start) &&
226 !(vma->vm_flags & VM_NONLINEAR)) {
227 if (!has_write_lock) {
228 up_read(&mm->mmap_sem);
229 down_write(&mm->mmap_sem);
233 mapping = vma->vm_file->f_mapping;
234 spin_lock(&mapping->i_mmap_lock);
235 flush_dcache_mmap_lock(mapping);
236 vma->vm_flags |= VM_NONLINEAR;
237 vma_prio_tree_remove(vma, &mapping->i_mmap);
238 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
239 flush_dcache_mmap_unlock(mapping);
240 spin_unlock(&mapping->i_mmap_lock);
243 err = vma->vm_ops->populate(vma, start, size,
245 pgoff, flags & MAP_NONBLOCK);
248 * We can't clear VM_NONLINEAR because we'd have to do
249 * it after ->populate completes, and that would prevent
250 * downgrading the lock. (Locks can't be upgraded).
253 if (likely(!has_write_lock))
254 up_read(&mm->mmap_sem);
256 up_write(&mm->mmap_sem);