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/vs_memory.h>
18 #include <linux/syscalls.h>
19 #include <linux/vs_memory.h>
21 #include <asm/mmu_context.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
25 static inline void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
26 unsigned long addr, pte_t *ptep)
32 if (pte_present(pte)) {
33 unsigned long pfn = pte_pfn(pte);
35 flush_cache_page(vma, addr, pfn);
36 pte = ptep_clear_flush(vma, addr, ptep);
38 struct page *page = pfn_to_page(pfn);
39 if (!PageReserved(page)) {
42 page_remove_rmap(page);
43 page_cache_release(page);
44 dec_mm_counter(mm, rss);
49 free_swap_and_cache(pte_to_swp_entry(pte));
50 pte_clear(mm, addr, ptep);
55 * Install a file page to a given virtual memory address, release any
56 * previously existing mapping.
58 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
59 unsigned long addr, struct page *page, pgprot_t prot)
70 pgd = pgd_offset(mm, addr);
71 spin_lock(&mm->page_table_lock);
73 if (!vx_rsspages_avail(mm, 1))
76 if (!vx_rsspages_avail(mm, 1))
79 pud = pud_alloc(mm, pgd, addr);
83 pmd = pmd_alloc(mm, pud, addr);
87 pte = pte_alloc_map(mm, pmd, addr);
92 * This page may have been truncated. Tell the
97 inode = vma->vm_file->f_mapping->host;
98 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
99 if (!page->mapping || page->index >= size)
103 zap_pte(mm, vma, addr, pte);
105 inc_mm_counter(mm,rss);
106 flush_icache_page(vma, page);
107 set_pte_at(mm, addr, pte, mk_pte(page, prot));
108 page_add_file_rmap(page);
111 update_mmu_cache(vma, addr, pte_val);
115 spin_unlock(&mm->page_table_lock);
118 EXPORT_SYMBOL(install_page);
122 * Install a file pte to a given virtual memory address, release any
123 * previously existing mapping.
125 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
126 unsigned long addr, unsigned long pgoff, pgprot_t prot)
135 pgd = pgd_offset(mm, addr);
136 spin_lock(&mm->page_table_lock);
138 pud = pud_alloc(mm, pgd, addr);
142 pmd = pmd_alloc(mm, pud, addr);
146 pte = pte_alloc_map(mm, pmd, addr);
150 zap_pte(mm, vma, addr, pte);
152 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
155 update_mmu_cache(vma, addr, pte_val);
156 spin_unlock(&mm->page_table_lock);
160 spin_unlock(&mm->page_table_lock);
166 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
167 * file within an existing vma.
168 * @start: start of the remapped virtual memory range
169 * @size: size of the remapped virtual memory range
170 * @prot: new protection bits of the range
171 * @pgoff: to be mapped page of the backing store file
172 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
174 * this syscall works purely via pagetables, so it's the most efficient
175 * way to map the same (large) file into a given virtual window. Unlike
176 * mmap()/mremap() it does not create any new vmas. The new mappings are
177 * also safe across swapout.
179 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
180 * protection is used. Arbitrary protections might be implemented in the
183 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
184 unsigned long __prot, unsigned long pgoff, unsigned long flags)
186 struct mm_struct *mm = current->mm;
187 struct address_space *mapping;
188 unsigned long end = start + size;
189 struct vm_area_struct *vma;
191 int has_write_lock = 0;
196 * Sanitize the syscall parameters:
198 start = start & PAGE_MASK;
199 size = size & PAGE_MASK;
201 /* Does the address range wrap, or is the span zero-sized? */
202 if (start + size <= start)
205 /* Can we represent this offset inside this architecture's pte's? */
206 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
207 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
211 /* We need down_write() to change vma->vm_flags. */
212 down_read(&mm->mmap_sem);
214 vma = find_vma(mm, start);
217 * Make sure the vma is shared, that it supports prefaulting,
218 * and that the remapped range is valid and fully within
219 * the single existing vma. vm_private_data is used as a
220 * swapout cursor in a VM_NONLINEAR vma (unless VM_RESERVED
221 * or VM_LOCKED, but VM_LOCKED could be revoked later on).
223 if (vma && (vma->vm_flags & VM_SHARED) &&
224 (!vma->vm_private_data ||
225 (vma->vm_flags & (VM_NONLINEAR|VM_RESERVED))) &&
226 vma->vm_ops && vma->vm_ops->populate &&
227 end > start && start >= vma->vm_start &&
228 end <= vma->vm_end) {
230 /* Must set VM_NONLINEAR before any pages are populated. */
231 if (pgoff != linear_page_index(vma, start) &&
232 !(vma->vm_flags & VM_NONLINEAR)) {
233 if (!has_write_lock) {
234 up_read(&mm->mmap_sem);
235 down_write(&mm->mmap_sem);
239 mapping = vma->vm_file->f_mapping;
240 spin_lock(&mapping->i_mmap_lock);
241 flush_dcache_mmap_lock(mapping);
242 vma->vm_flags |= VM_NONLINEAR;
243 vma_prio_tree_remove(vma, &mapping->i_mmap);
244 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
245 flush_dcache_mmap_unlock(mapping);
246 spin_unlock(&mapping->i_mmap_lock);
249 err = vma->vm_ops->populate(vma, start, size,
251 pgoff, flags & MAP_NONBLOCK);
254 * We can't clear VM_NONLINEAR because we'd have to do
255 * it after ->populate completes, and that would prevent
256 * downgrading the lock. (Locks can't be upgraded).
259 if (likely(!has_write_lock))
260 up_read(&mm->mmap_sem);
262 up_write(&mm->mmap_sem);