]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/mm/fremap.c | |
3 | * | |
4 | * Explicit pagetable population and nonlinear (random) mappings support. | |
5 | * | |
6 | * started by Ingo Molnar, Copyright (C) 2002, 2003 | |
7 | */ | |
0b173bc4 | 8 | #include <linux/export.h> |
4af3c9cc | 9 | #include <linux/backing-dev.h> |
1da177e4 LT |
10 | #include <linux/mm.h> |
11 | #include <linux/swap.h> | |
12 | #include <linux/file.h> | |
13 | #include <linux/mman.h> | |
14 | #include <linux/pagemap.h> | |
15 | #include <linux/swapops.h> | |
16 | #include <linux/rmap.h> | |
1da177e4 | 17 | #include <linux/syscalls.h> |
cddb8a5c | 18 | #include <linux/mmu_notifier.h> |
1da177e4 LT |
19 | |
20 | #include <asm/mmu_context.h> | |
21 | #include <asm/cacheflush.h> | |
22 | #include <asm/tlbflush.h> | |
23 | ||
ba470de4 RR |
24 | #include "internal.h" |
25 | ||
d0217ac0 | 26 | static void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma, |
1da177e4 LT |
27 | unsigned long addr, pte_t *ptep) |
28 | { | |
29 | pte_t pte = *ptep; | |
30 | ||
1da177e4 | 31 | if (pte_present(pte)) { |
d0217ac0 NP |
32 | struct page *page; |
33 | ||
6aab341e | 34 | flush_cache_page(vma, addr, pte_pfn(pte)); |
1da177e4 | 35 | pte = ptep_clear_flush(vma, addr, ptep); |
6aab341e LT |
36 | page = vm_normal_page(vma, addr, pte); |
37 | if (page) { | |
38 | if (pte_dirty(pte)) | |
39 | set_page_dirty(page); | |
edc315fd | 40 | page_remove_rmap(page); |
6aab341e | 41 | page_cache_release(page); |
d0217ac0 | 42 | update_hiwater_rss(mm); |
d559db08 | 43 | dec_mm_counter(mm, MM_FILEPAGES); |
1da177e4 LT |
44 | } |
45 | } else { | |
46 | if (!pte_file(pte)) | |
47 | free_swap_and_cache(pte_to_swp_entry(pte)); | |
9888a1ca | 48 | pte_clear_not_present_full(mm, addr, ptep, 0); |
1da177e4 LT |
49 | } |
50 | } | |
51 | ||
1da177e4 LT |
52 | /* |
53 | * Install a file pte to a given virtual memory address, release any | |
54 | * previously existing mapping. | |
55 | */ | |
d0217ac0 | 56 | static int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, |
1da177e4 LT |
57 | unsigned long addr, unsigned long pgoff, pgprot_t prot) |
58 | { | |
59 | int err = -ENOMEM; | |
41bb3476 | 60 | pte_t *pte, ptfile; |
c74df32c | 61 | spinlock_t *ptl; |
1da177e4 | 62 | |
c9cfcddf | 63 | pte = get_locked_pte(mm, addr, &ptl); |
1da177e4 | 64 | if (!pte) |
c74df32c | 65 | goto out; |
1da177e4 | 66 | |
41bb3476 CG |
67 | ptfile = pgoff_to_pte(pgoff); |
68 | ||
69 | if (!pte_none(*pte)) { | |
70 | if (pte_present(*pte) && pte_soft_dirty(*pte)) | |
71 | pte_file_mksoft_dirty(ptfile); | |
d0217ac0 | 72 | zap_pte(mm, vma, addr, pte); |
41bb3476 | 73 | } |
1da177e4 | 74 | |
41bb3476 | 75 | set_pte_at(mm, addr, pte, ptfile); |
668e0d8f HD |
76 | /* |
77 | * We don't need to run update_mmu_cache() here because the "file pte" | |
78 | * being installed by install_file_pte() is not a real pte - it's a | |
79 | * non-present entry (like a swap entry), noting what file offset should | |
80 | * be mapped there when there's a fault (in a non-linear vma where | |
81 | * that's not obvious). | |
82 | */ | |
c74df32c HD |
83 | pte_unmap_unlock(pte, ptl); |
84 | err = 0; | |
85 | out: | |
1da177e4 LT |
86 | return err; |
87 | } | |
88 | ||
0b173bc4 KK |
89 | int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr, |
90 | unsigned long size, pgoff_t pgoff) | |
54cb8821 | 91 | { |
0b173bc4 | 92 | struct mm_struct *mm = vma->vm_mm; |
54cb8821 NP |
93 | int err; |
94 | ||
95 | do { | |
96 | err = install_file_pte(mm, vma, addr, pgoff, vma->vm_page_prot); | |
97 | if (err) | |
98 | return err; | |
99 | ||
100 | size -= PAGE_SIZE; | |
101 | addr += PAGE_SIZE; | |
102 | pgoff++; | |
103 | } while (size); | |
104 | ||
0b173bc4 | 105 | return 0; |
54cb8821 | 106 | } |
0b173bc4 | 107 | EXPORT_SYMBOL(generic_file_remap_pages); |
54cb8821 | 108 | |
8d63494f RD |
109 | /** |
110 | * sys_remap_file_pages - remap arbitrary pages of an existing VM_SHARED vma | |
1da177e4 LT |
111 | * @start: start of the remapped virtual memory range |
112 | * @size: size of the remapped virtual memory range | |
8d63494f RD |
113 | * @prot: new protection bits of the range (see NOTE) |
114 | * @pgoff: to-be-mapped page of the backing store file | |
1da177e4 LT |
115 | * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO. |
116 | * | |
8d63494f RD |
117 | * sys_remap_file_pages remaps arbitrary pages of an existing VM_SHARED vma |
118 | * (shared backing store file). | |
119 | * | |
120 | * This syscall works purely via pagetables, so it's the most efficient | |
1da177e4 LT |
121 | * way to map the same (large) file into a given virtual window. Unlike |
122 | * mmap()/mremap() it does not create any new vmas. The new mappings are | |
123 | * also safe across swapout. | |
124 | * | |
7682486b | 125 | * NOTE: the @prot parameter right now is ignored (but must be zero), |
8d63494f RD |
126 | * and the vma's default protection is used. Arbitrary protections |
127 | * might be implemented in the future. | |
1da177e4 | 128 | */ |
6a6160a7 HC |
129 | SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, |
130 | unsigned long, prot, unsigned long, pgoff, unsigned long, flags) | |
1da177e4 LT |
131 | { |
132 | struct mm_struct *mm = current->mm; | |
133 | struct address_space *mapping; | |
1da177e4 LT |
134 | struct vm_area_struct *vma; |
135 | int err = -EINVAL; | |
136 | int has_write_lock = 0; | |
a2362d24 | 137 | vm_flags_t vm_flags = 0; |
1da177e4 | 138 | |
8d63494f | 139 | if (prot) |
1da177e4 LT |
140 | return err; |
141 | /* | |
142 | * Sanitize the syscall parameters: | |
143 | */ | |
144 | start = start & PAGE_MASK; | |
145 | size = size & PAGE_MASK; | |
146 | ||
147 | /* Does the address range wrap, or is the span zero-sized? */ | |
148 | if (start + size <= start) | |
149 | return err; | |
150 | ||
5ec1055a LW |
151 | /* Does pgoff wrap? */ |
152 | if (pgoff + (size >> PAGE_SHIFT) < pgoff) | |
153 | return err; | |
154 | ||
1da177e4 LT |
155 | /* Can we represent this offset inside this architecture's pte's? */ |
156 | #if PTE_FILE_MAX_BITS < BITS_PER_LONG | |
157 | if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS)) | |
158 | return err; | |
159 | #endif | |
160 | ||
161 | /* We need down_write() to change vma->vm_flags. */ | |
162 | down_read(&mm->mmap_sem); | |
163 | retry: | |
164 | vma = find_vma(mm, start); | |
165 | ||
166 | /* | |
167 | * Make sure the vma is shared, that it supports prefaulting, | |
168 | * and that the remapped range is valid and fully within | |
940e7da5 | 169 | * the single existing vma. |
1da177e4 | 170 | */ |
a2362d24 | 171 | if (!vma || !(vma->vm_flags & VM_SHARED)) |
54cb8821 NP |
172 | goto out; |
173 | ||
deb521c4 | 174 | if (!vma->vm_ops || !vma->vm_ops->remap_pages) |
54cb8821 NP |
175 | goto out; |
176 | ||
e92b05de | 177 | if (start < vma->vm_start || start + size > vma->vm_end) |
54cb8821 NP |
178 | goto out; |
179 | ||
180 | /* Must set VM_NONLINEAR before any pages are populated. */ | |
181 | if (!(vma->vm_flags & VM_NONLINEAR)) { | |
940e7da5 ML |
182 | /* |
183 | * vm_private_data is used as a swapout cursor | |
184 | * in a VM_NONLINEAR vma. | |
185 | */ | |
186 | if (vma->vm_private_data) | |
187 | goto out; | |
188 | ||
54cb8821 NP |
189 | /* Don't need a nonlinear mapping, exit success */ |
190 | if (pgoff == linear_page_index(vma, start)) { | |
191 | err = 0; | |
192 | goto out; | |
193 | } | |
194 | ||
195 | if (!has_write_lock) { | |
940e7da5 | 196 | get_write_lock: |
54cb8821 NP |
197 | up_read(&mm->mmap_sem); |
198 | down_write(&mm->mmap_sem); | |
199 | has_write_lock = 1; | |
200 | goto retry; | |
201 | } | |
202 | mapping = vma->vm_file->f_mapping; | |
3ee6dafc MS |
203 | /* |
204 | * page_mkclean doesn't work on nonlinear vmas, so if | |
205 | * dirty pages need to be accounted, emulate with linear | |
206 | * vmas. | |
207 | */ | |
208 | if (mapping_cap_account_dirty(mapping)) { | |
209 | unsigned long addr; | |
cb0942b8 | 210 | struct file *file = get_file(vma->vm_file); |
4eb91982 RR |
211 | /* mmap_region may free vma; grab the info now */ |
212 | vm_flags = vma->vm_flags; | |
3ee6dafc | 213 | |
4eb91982 | 214 | addr = mmap_region(file, start, size, vm_flags, pgoff); |
8a459e44 | 215 | fput(file); |
3ee6dafc MS |
216 | if (IS_ERR_VALUE(addr)) { |
217 | err = addr; | |
218 | } else { | |
219 | BUG_ON(addr != start); | |
220 | err = 0; | |
221 | } | |
4eb91982 | 222 | goto out_freed; |
3ee6dafc | 223 | } |
3d48ae45 | 224 | mutex_lock(&mapping->i_mmap_mutex); |
54cb8821 NP |
225 | flush_dcache_mmap_lock(mapping); |
226 | vma->vm_flags |= VM_NONLINEAR; | |
6b2dbba8 | 227 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
54cb8821 NP |
228 | vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear); |
229 | flush_dcache_mmap_unlock(mapping); | |
3d48ae45 | 230 | mutex_unlock(&mapping->i_mmap_mutex); |
54cb8821 NP |
231 | } |
232 | ||
ba470de4 RR |
233 | if (vma->vm_flags & VM_LOCKED) { |
234 | /* | |
235 | * drop PG_Mlocked flag for over-mapped range | |
236 | */ | |
940e7da5 ML |
237 | if (!has_write_lock) |
238 | goto get_write_lock; | |
a1ea9549 | 239 | vm_flags = vma->vm_flags; |
ba470de4 | 240 | munlock_vma_pages_range(vma, start, start + size); |
a1ea9549 | 241 | vma->vm_flags = vm_flags; |
ba470de4 RR |
242 | } |
243 | ||
cddb8a5c | 244 | mmu_notifier_invalidate_range_start(mm, start, start + size); |
0b173bc4 | 245 | err = vma->vm_ops->remap_pages(vma, start, size, pgoff); |
cddb8a5c | 246 | mmu_notifier_invalidate_range_end(mm, start, start + size); |
1da177e4 | 247 | |
54cb8821 NP |
248 | /* |
249 | * We can't clear VM_NONLINEAR because we'd have to do | |
250 | * it after ->populate completes, and that would prevent | |
251 | * downgrading the lock. (Locks can't be upgraded). | |
252 | */ | |
1da177e4 | 253 | |
54cb8821 | 254 | out: |
6d7825b1 AM |
255 | if (vma) |
256 | vm_flags = vma->vm_flags; | |
4eb91982 | 257 | out_freed: |
1da177e4 LT |
258 | if (likely(!has_write_lock)) |
259 | up_read(&mm->mmap_sem); | |
260 | else | |
261 | up_write(&mm->mmap_sem); | |
a1ea9549 ML |
262 | if (!err && ((vm_flags & VM_LOCKED) || !(flags & MAP_NONBLOCK))) |
263 | mm_populate(start, size); | |
1da177e4 LT |
264 | |
265 | return err; | |
266 | } |