4 * Explicit pagetable population and nonlinear (random) mappings support.
6 * started by Ingo Molnar, Copyright (C) 2002, 2003
8 #include <linux/backing-dev.h>
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>
19 #include <asm/mmu_context.h>
20 #include <asm/cacheflush.h>
21 #include <asm/tlbflush.h>
23 static void zap_pte(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
24 unsigned long addr
, pte_t
*ptep
)
28 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
);
39 update_hiwater_rss(mm
);
40 dec_mm_counter(mm
, file_rss
);
44 free_swap_and_cache(pte_to_swp_entry(pte
));
45 pte_clear_not_present_full(mm
, addr
, ptep
, 0);
50 * Install a file pte to a given virtual memory address, release any
51 * previously existing mapping.
53 static int install_file_pte(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
54 unsigned long addr
, unsigned long pgoff
, pgprot_t prot
)
60 pte
= get_locked_pte(mm
, addr
, &ptl
);
65 zap_pte(mm
, vma
, addr
, pte
);
67 set_pte_at(mm
, addr
, pte
, pgoff_to_pte(pgoff
));
69 * We don't need to run update_mmu_cache() here because the "file pte"
70 * being installed by install_file_pte() is not a real pte - it's a
71 * non-present entry (like a swap entry), noting what file offset should
72 * be mapped there when there's a fault (in a non-linear vma where
73 * that's not obvious).
75 pte_unmap_unlock(pte
, ptl
);
81 static int populate_range(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
82 unsigned long addr
, unsigned long size
, pgoff_t pgoff
)
87 err
= install_file_pte(mm
, vma
, addr
, pgoff
, vma
->vm_page_prot
);
101 * sys_remap_file_pages - remap arbitrary pages of an existing VM_SHARED vma
102 * @start: start of the remapped virtual memory range
103 * @size: size of the remapped virtual memory range
104 * @prot: new protection bits of the range (see NOTE)
105 * @pgoff: to-be-mapped page of the backing store file
106 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
108 * sys_remap_file_pages remaps arbitrary pages of an existing VM_SHARED vma
109 * (shared backing store file).
111 * This syscall works purely via pagetables, so it's the most efficient
112 * way to map the same (large) file into a given virtual window. Unlike
113 * mmap()/mremap() it does not create any new vmas. The new mappings are
114 * also safe across swapout.
116 * NOTE: the @prot parameter right now is ignored (but must be zero),
117 * and the vma's default protection is used. Arbitrary protections
118 * might be implemented in the future.
120 asmlinkage
long sys_remap_file_pages(unsigned long start
, unsigned long size
,
121 unsigned long prot
, unsigned long pgoff
, unsigned long flags
)
123 struct mm_struct
*mm
= current
->mm
;
124 struct address_space
*mapping
;
125 unsigned long end
= start
+ size
;
126 struct vm_area_struct
*vma
;
128 int has_write_lock
= 0;
133 * Sanitize the syscall parameters:
135 start
= start
& PAGE_MASK
;
136 size
= size
& PAGE_MASK
;
138 /* Does the address range wrap, or is the span zero-sized? */
139 if (start
+ size
<= start
)
142 /* Can we represent this offset inside this architecture's pte's? */
143 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
144 if (pgoff
+ (size
>> PAGE_SHIFT
) >= (1UL << PTE_FILE_MAX_BITS
))
148 /* We need down_write() to change vma->vm_flags. */
149 down_read(&mm
->mmap_sem
);
151 vma
= find_vma(mm
, start
);
154 * Make sure the vma is shared, that it supports prefaulting,
155 * and that the remapped range is valid and fully within
156 * the single existing vma. vm_private_data is used as a
157 * swapout cursor in a VM_NONLINEAR vma.
159 if (!vma
|| !(vma
->vm_flags
& VM_SHARED
))
162 if (vma
->vm_private_data
&& !(vma
->vm_flags
& VM_NONLINEAR
))
165 if (!(vma
->vm_flags
& VM_CAN_NONLINEAR
))
168 if (end
<= start
|| start
< vma
->vm_start
|| end
> vma
->vm_end
)
171 /* Must set VM_NONLINEAR before any pages are populated. */
172 if (!(vma
->vm_flags
& VM_NONLINEAR
)) {
173 /* Don't need a nonlinear mapping, exit success */
174 if (pgoff
== linear_page_index(vma
, start
)) {
179 if (!has_write_lock
) {
180 up_read(&mm
->mmap_sem
);
181 down_write(&mm
->mmap_sem
);
185 mapping
= vma
->vm_file
->f_mapping
;
187 * page_mkclean doesn't work on nonlinear vmas, so if
188 * dirty pages need to be accounted, emulate with linear
191 if (mapping_cap_account_dirty(mapping
)) {
193 struct file
*file
= vma
->vm_file
;
195 flags
&= MAP_NONBLOCK
;
197 addr
= mmap_region(file
, start
, size
,
198 flags
, vma
->vm_flags
, pgoff
, 1);
200 if (IS_ERR_VALUE(addr
)) {
203 BUG_ON(addr
!= start
);
208 spin_lock(&mapping
->i_mmap_lock
);
209 flush_dcache_mmap_lock(mapping
);
210 vma
->vm_flags
|= VM_NONLINEAR
;
211 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
212 vma_nonlinear_insert(vma
, &mapping
->i_mmap_nonlinear
);
213 flush_dcache_mmap_unlock(mapping
);
214 spin_unlock(&mapping
->i_mmap_lock
);
217 err
= populate_range(mm
, vma
, start
, size
, pgoff
);
218 if (!err
&& !(flags
& MAP_NONBLOCK
)) {
219 if (unlikely(has_write_lock
)) {
220 downgrade_write(&mm
->mmap_sem
);
223 make_pages_present(start
, start
+size
);
227 * We can't clear VM_NONLINEAR because we'd have to do
228 * it after ->populate completes, and that would prevent
229 * downgrading the lock. (Locks can't be upgraded).
233 if (likely(!has_write_lock
))
234 up_read(&mm
->mmap_sem
);
236 up_write(&mm
->mmap_sem
);
This page took 0.035465 seconds and 5 git commands to generate.