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, vma);
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)
79 if (!vx_rsspages_avail(mm, 1))
83 if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
84 inc_mm_counter(mm, file_rss);
86 flush_icache_page(vma, page);
87 pte_val = mk_pte(page, prot);
88 set_pte_at(mm, addr, pte, pte_val);
89 page_add_file_rmap(page);
90 update_mmu_cache(vma, addr, pte_val);
91 lazy_mmu_prot_update(pte_val);
94 pte_unmap_unlock(pte, ptl);
98 EXPORT_SYMBOL(install_page);
101 * Install a file pte to a given virtual memory address, release any
102 * previously existing mapping.
104 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
105 unsigned long addr, unsigned long pgoff, pgprot_t prot)
112 pte = get_locked_pte(mm, addr, &ptl);
116 if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
117 update_hiwater_rss(mm);
118 dec_mm_counter(mm, file_rss);
121 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
124 * We don't need to run update_mmu_cache() here because the "file pte"
125 * being installed by install_file_pte() is not a real pte - it's a
126 * non-present entry (like a swap entry), noting what file offset should
127 * be mapped there when there's a fault (in a non-linear vma where
128 * that's not obvious).
130 pte_unmap_unlock(pte, ptl);
137 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
138 * file within an existing vma.
139 * @start: start of the remapped virtual memory range
140 * @size: size of the remapped virtual memory range
141 * @prot: new protection bits of the range
142 * @pgoff: to be mapped page of the backing store file
143 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
145 * this syscall works purely via pagetables, so it's the most efficient
146 * way to map the same (large) file into a given virtual window. Unlike
147 * mmap()/mremap() it does not create any new vmas. The new mappings are
148 * also safe across swapout.
150 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
151 * protection is used. Arbitrary protections might be implemented in the
154 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
155 unsigned long __prot, unsigned long pgoff, unsigned long flags)
157 struct mm_struct *mm = current->mm;
158 struct address_space *mapping;
159 unsigned long end = start + size;
160 struct vm_area_struct *vma;
162 int has_write_lock = 0;
167 * Sanitize the syscall parameters:
169 start = start & PAGE_MASK;
170 size = size & PAGE_MASK;
172 /* Does the address range wrap, or is the span zero-sized? */
173 if (start + size <= start)
176 /* Can we represent this offset inside this architecture's pte's? */
177 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
178 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
182 /* We need down_write() to change vma->vm_flags. */
183 down_read(&mm->mmap_sem);
185 vma = find_vma(mm, start);
188 * Make sure the vma is shared, that it supports prefaulting,
189 * and that the remapped range is valid and fully within
190 * the single existing vma. vm_private_data is used as a
191 * swapout cursor in a VM_NONLINEAR vma.
193 if (vma && (vma->vm_flags & VM_SHARED) &&
194 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
195 vma->vm_ops && vma->vm_ops->populate &&
196 end > start && start >= vma->vm_start &&
197 end <= vma->vm_end) {
199 /* Must set VM_NONLINEAR before any pages are populated. */
200 if (pgoff != linear_page_index(vma, start) &&
201 !(vma->vm_flags & VM_NONLINEAR)) {
202 if (!has_write_lock) {
203 up_read(&mm->mmap_sem);
204 down_write(&mm->mmap_sem);
208 mapping = vma->vm_file->f_mapping;
209 spin_lock(&mapping->i_mmap_lock);
210 flush_dcache_mmap_lock(mapping);
211 vma->vm_flags |= VM_NONLINEAR;
212 vma_prio_tree_remove(vma, &mapping->i_mmap);
213 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
214 flush_dcache_mmap_unlock(mapping);
215 spin_unlock(&mapping->i_mmap_lock);
218 err = vma->vm_ops->populate(vma, start, size,
220 pgoff, flags & MAP_NONBLOCK);
223 * We can't clear VM_NONLINEAR because we'd have to do
224 * it after ->populate completes, and that would prevent
225 * downgrading the lock. (Locks can't be upgraded).
228 if (likely(!has_write_lock))
229 up_read(&mm->mmap_sem);
231 up_write(&mm->mmap_sem);