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_base.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 int zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
26 unsigned long addr, pte_t *ptep)
29 struct page *page = NULL;
31 if (pte_present(pte)) {
32 flush_cache_page(vma, addr, pte_pfn(pte));
33 pte = ptep_clear_flush(vma, addr, ptep);
34 page = vm_normal_page(vma, addr, pte);
38 page_remove_rmap(page, vma);
39 page_cache_release(page);
43 free_swap_and_cache(pte_to_swp_entry(pte));
44 pte_clear(mm, addr, ptep);
50 * Install a file page to a given virtual memory address, release any
51 * previously existing mapping.
53 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
54 unsigned long addr, struct page *page, pgprot_t prot)
63 pte = get_locked_pte(mm, addr, &ptl);
68 * This page may have been truncated. Tell the
73 inode = vma->vm_file->f_mapping->host;
74 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
75 if (!page->mapping || page->index >= size)
78 if (page_mapcount(page) > INT_MAX/2)
80 if (!vx_rsspages_avail(mm, 1))
84 if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
85 inc_mm_counter(mm, file_rss);
87 flush_icache_page(vma, page);
88 pte_val = mk_pte(page, prot);
89 set_pte_at(mm, addr, pte, pte_val);
90 page_add_file_rmap(page);
91 update_mmu_cache(vma, addr, pte_val);
92 lazy_mmu_prot_update(pte_val);
95 pte_unmap_unlock(pte, ptl);
99 EXPORT_SYMBOL(install_page);
102 * Install a file pte to a given virtual memory address, release any
103 * previously existing mapping.
105 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
106 unsigned long addr, unsigned long pgoff, pgprot_t prot)
113 pte = get_locked_pte(mm, addr, &ptl);
117 if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
118 update_hiwater_rss(mm);
119 dec_mm_counter(mm, file_rss);
122 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
125 * We don't need to run update_mmu_cache() here because the "file pte"
126 * being installed by install_file_pte() is not a real pte - it's a
127 * non-present entry (like a swap entry), noting what file offset should
128 * be mapped there when there's a fault (in a non-linear vma where
129 * that's not obvious).
131 pte_unmap_unlock(pte, ptl);
138 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
139 * file within an existing vma.
140 * @start: start of the remapped virtual memory range
141 * @size: size of the remapped virtual memory range
142 * @prot: new protection bits of the range
143 * @pgoff: to be mapped page of the backing store file
144 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
146 * this syscall works purely via pagetables, so it's the most efficient
147 * way to map the same (large) file into a given virtual window. Unlike
148 * mmap()/mremap() it does not create any new vmas. The new mappings are
149 * also safe across swapout.
151 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
152 * protection is used. Arbitrary protections might be implemented in the
155 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
156 unsigned long __prot, unsigned long pgoff, unsigned long flags)
158 struct mm_struct *mm = current->mm;
159 struct address_space *mapping;
160 unsigned long end = start + size;
161 struct vm_area_struct *vma;
163 int has_write_lock = 0;
168 * Sanitize the syscall parameters:
170 start = start & PAGE_MASK;
171 size = size & PAGE_MASK;
173 /* Does the address range wrap, or is the span zero-sized? */
174 if (start + size <= start)
177 /* Can we represent this offset inside this architecture's pte's? */
178 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
179 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
183 /* We need down_write() to change vma->vm_flags. */
184 down_read(&mm->mmap_sem);
186 vma = find_vma(mm, start);
189 * Make sure the vma is shared, that it supports prefaulting,
190 * and that the remapped range is valid and fully within
191 * the single existing vma. vm_private_data is used as a
192 * swapout cursor in a VM_NONLINEAR vma.
194 if (vma && (vma->vm_flags & VM_SHARED) &&
195 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
196 vma->vm_ops && vma->vm_ops->populate &&
197 end > start && start >= vma->vm_start &&
198 end <= vma->vm_end) {
200 /* Must set VM_NONLINEAR before any pages are populated. */
201 if (pgoff != linear_page_index(vma, start) &&
202 !(vma->vm_flags & VM_NONLINEAR)) {
203 if (!has_write_lock) {
204 up_read(&mm->mmap_sem);
205 down_write(&mm->mmap_sem);
209 mapping = vma->vm_file->f_mapping;
210 spin_lock(&mapping->i_mmap_lock);
211 flush_dcache_mmap_lock(mapping);
212 vma->vm_flags |= VM_NONLINEAR;
213 vma_prio_tree_remove(vma, &mapping->i_mmap);
214 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
215 flush_dcache_mmap_unlock(mapping);
216 spin_unlock(&mapping->i_mmap_lock);
219 err = vma->vm_ops->populate(vma, start, size,
221 pgoff, flags & MAP_NONBLOCK);
224 * We can't clear VM_NONLINEAR because we'd have to do
225 * it after ->populate completes, and that would prevent
226 * downgrading the lock. (Locks can't be upgraded).
229 if (likely(!has_write_lock))
230 up_read(&mm->mmap_sem);
232 up_write(&mm->mmap_sem);