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 int zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
25 unsigned long addr, pte_t *ptep)
28 struct page *page = NULL;
30 if (pte_present(pte)) {
31 flush_cache_page(vma, addr, pte_pfn(pte));
32 pte = ptep_clear_flush(vma, addr, ptep);
33 page = vm_normal_page(vma, addr, pte);
37 page_remove_rmap(page);
38 page_cache_release(page);
42 free_swap_and_cache(pte_to_swp_entry(pte));
43 pte_clear(mm, addr, ptep);
49 * Install a file page to a given virtual memory address, release any
50 * previously existing mapping.
52 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
53 unsigned long addr, struct page *page, pgprot_t prot)
62 pte = get_locked_pte(mm, addr, &ptl);
67 * This page may have been truncated. Tell the
72 inode = vma->vm_file->f_mapping->host;
73 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
74 if (!page->mapping || page->index >= size)
77 if (page_mapcount(page) > INT_MAX/2)
81 if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
82 inc_mm_counter(mm, file_rss);
84 flush_icache_page(vma, page);
85 set_pte_at(mm, addr, pte, mk_pte(page, prot));
86 page_add_file_rmap(page);
88 update_mmu_cache(vma, addr, pte_val);
91 pte_unmap_unlock(pte, ptl);
95 EXPORT_SYMBOL(install_page);
98 * Install a file pte to a given virtual memory address, release any
99 * previously existing mapping.
101 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
102 unsigned long addr, unsigned long pgoff, pgprot_t prot)
109 pte = get_locked_pte(mm, addr, &ptl);
113 if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
114 update_hiwater_rss(mm);
115 dec_mm_counter(mm, file_rss);
118 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
120 update_mmu_cache(vma, addr, pte_val);
121 pte_unmap_unlock(pte, ptl);
128 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
129 * file within an existing vma.
130 * @start: start of the remapped virtual memory range
131 * @size: size of the remapped virtual memory range
132 * @prot: new protection bits of the range
133 * @pgoff: to be mapped page of the backing store file
134 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
136 * this syscall works purely via pagetables, so it's the most efficient
137 * way to map the same (large) file into a given virtual window. Unlike
138 * mmap()/mremap() it does not create any new vmas. The new mappings are
139 * also safe across swapout.
141 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
142 * protection is used. Arbitrary protections might be implemented in the
145 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
146 unsigned long __prot, unsigned long pgoff, unsigned long flags)
148 struct mm_struct *mm = current->mm;
149 struct address_space *mapping;
150 unsigned long end = start + size;
151 struct vm_area_struct *vma;
153 int has_write_lock = 0;
158 * Sanitize the syscall parameters:
160 start = start & PAGE_MASK;
161 size = size & PAGE_MASK;
163 /* Does the address range wrap, or is the span zero-sized? */
164 if (start + size <= start)
167 /* Can we represent this offset inside this architecture's pte's? */
168 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
169 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
173 /* We need down_write() to change vma->vm_flags. */
174 down_read(&mm->mmap_sem);
176 vma = find_vma(mm, start);
179 * Make sure the vma is shared, that it supports prefaulting,
180 * and that the remapped range is valid and fully within
181 * the single existing vma. vm_private_data is used as a
182 * swapout cursor in a VM_NONLINEAR vma.
184 if (vma && (vma->vm_flags & VM_SHARED) &&
185 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
186 vma->vm_ops && vma->vm_ops->populate &&
187 end > start && start >= vma->vm_start &&
188 end <= vma->vm_end) {
190 /* Must set VM_NONLINEAR before any pages are populated. */
191 if (pgoff != linear_page_index(vma, start) &&
192 !(vma->vm_flags & VM_NONLINEAR)) {
193 if (!has_write_lock) {
194 up_read(&mm->mmap_sem);
195 down_write(&mm->mmap_sem);
199 mapping = vma->vm_file->f_mapping;
200 spin_lock(&mapping->i_mmap_lock);
201 flush_dcache_mmap_lock(mapping);
202 vma->vm_flags |= VM_NONLINEAR;
203 vma_prio_tree_remove(vma, &mapping->i_mmap);
204 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
205 flush_dcache_mmap_unlock(mapping);
206 spin_unlock(&mapping->i_mmap_lock);
209 err = vma->vm_ops->populate(vma, start, size,
211 pgoff, flags & MAP_NONBLOCK);
214 * We can't clear VM_NONLINEAR because we'd have to do
215 * it after ->populate completes, and that would prevent
216 * downgrading the lock. (Locks can't be upgraded).
219 if (likely(!has_write_lock))
220 up_read(&mm->mmap_sem);
222 up_write(&mm->mmap_sem);