Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * mm/mmap.c | |
4 | * | |
5 | * Written by obz. | |
6 | * | |
046c6884 | 7 | * Address space accounting code <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
8 | */ |
9 | ||
b1de0d13 MH |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
e8420a8e | 12 | #include <linux/kernel.h> |
1da177e4 | 13 | #include <linux/slab.h> |
4af3c9cc | 14 | #include <linux/backing-dev.h> |
1da177e4 | 15 | #include <linux/mm.h> |
17fca131 | 16 | #include <linux/mm_inline.h> |
1da177e4 LT |
17 | #include <linux/shm.h> |
18 | #include <linux/mman.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/swap.h> | |
21 | #include <linux/syscalls.h> | |
c59ede7b | 22 | #include <linux/capability.h> |
1da177e4 LT |
23 | #include <linux/init.h> |
24 | #include <linux/file.h> | |
25 | #include <linux/fs.h> | |
26 | #include <linux/personality.h> | |
27 | #include <linux/security.h> | |
28 | #include <linux/hugetlb.h> | |
c01d5b30 | 29 | #include <linux/shmem_fs.h> |
1da177e4 | 30 | #include <linux/profile.h> |
b95f1b31 | 31 | #include <linux/export.h> |
1da177e4 LT |
32 | #include <linux/mount.h> |
33 | #include <linux/mempolicy.h> | |
34 | #include <linux/rmap.h> | |
cddb8a5c | 35 | #include <linux/mmu_notifier.h> |
82f71ae4 | 36 | #include <linux/mmdebug.h> |
cdd6c482 | 37 | #include <linux/perf_event.h> |
120a795d | 38 | #include <linux/audit.h> |
b15d00b6 | 39 | #include <linux/khugepaged.h> |
2b144498 | 40 | #include <linux/uprobes.h> |
1640879a AS |
41 | #include <linux/notifier.h> |
42 | #include <linux/memory.h> | |
b1de0d13 | 43 | #include <linux/printk.h> |
19a809af | 44 | #include <linux/userfaultfd_k.h> |
d977d56c | 45 | #include <linux/moduleparam.h> |
62b5f7d0 | 46 | #include <linux/pkeys.h> |
21292580 | 47 | #include <linux/oom.h> |
04f5866e | 48 | #include <linux/sched/mm.h> |
1da177e4 | 49 | |
7c0f6ba6 | 50 | #include <linux/uaccess.h> |
1da177e4 LT |
51 | #include <asm/cacheflush.h> |
52 | #include <asm/tlb.h> | |
d6dd61c8 | 53 | #include <asm/mmu_context.h> |
1da177e4 | 54 | |
df529cab JK |
55 | #define CREATE_TRACE_POINTS |
56 | #include <trace/events/mmap.h> | |
57 | ||
42b77728 JB |
58 | #include "internal.h" |
59 | ||
3a459756 KK |
60 | #ifndef arch_mmap_check |
61 | #define arch_mmap_check(addr, len, flags) (0) | |
62 | #endif | |
63 | ||
d07e2259 DC |
64 | #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS |
65 | const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; | |
66 | const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; | |
67 | int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; | |
68 | #endif | |
69 | #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS | |
70 | const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; | |
71 | const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; | |
72 | int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; | |
73 | #endif | |
74 | ||
f4fcd558 | 75 | static bool ignore_rlimit_data; |
d977d56c | 76 | core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); |
d07e2259 | 77 | |
763ecb03 | 78 | static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, |
e0da382c | 79 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
763ecb03 LH |
80 | struct vm_area_struct *next, unsigned long start, |
81 | unsigned long end); | |
e0da382c | 82 | |
64e45507 PF |
83 | static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) |
84 | { | |
85 | return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); | |
86 | } | |
87 | ||
88 | /* Update vma->vm_page_prot to reflect vma->vm_flags. */ | |
89 | void vma_set_page_prot(struct vm_area_struct *vma) | |
90 | { | |
91 | unsigned long vm_flags = vma->vm_flags; | |
6d2329f8 | 92 | pgprot_t vm_page_prot; |
64e45507 | 93 | |
6d2329f8 AA |
94 | vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); |
95 | if (vma_wants_writenotify(vma, vm_page_prot)) { | |
64e45507 | 96 | vm_flags &= ~VM_SHARED; |
6d2329f8 | 97 | vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); |
64e45507 | 98 | } |
c1e8d7c6 | 99 | /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ |
6d2329f8 | 100 | WRITE_ONCE(vma->vm_page_prot, vm_page_prot); |
64e45507 PF |
101 | } |
102 | ||
1da177e4 | 103 | /* |
c8c06efa | 104 | * Requires inode->i_mapping->i_mmap_rwsem |
1da177e4 LT |
105 | */ |
106 | static void __remove_shared_vm_struct(struct vm_area_struct *vma, | |
107 | struct file *file, struct address_space *mapping) | |
108 | { | |
1da177e4 | 109 | if (vma->vm_flags & VM_SHARED) |
4bb5f5d9 | 110 | mapping_unmap_writable(mapping); |
1da177e4 LT |
111 | |
112 | flush_dcache_mmap_lock(mapping); | |
27ba0644 | 113 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
1da177e4 LT |
114 | flush_dcache_mmap_unlock(mapping); |
115 | } | |
116 | ||
117 | /* | |
6b2dbba8 | 118 | * Unlink a file-based vm structure from its interval tree, to hide |
a8fb5618 | 119 | * vma from rmap and vmtruncate before freeing its page tables. |
1da177e4 | 120 | */ |
a8fb5618 | 121 | void unlink_file_vma(struct vm_area_struct *vma) |
1da177e4 LT |
122 | { |
123 | struct file *file = vma->vm_file; | |
124 | ||
1da177e4 LT |
125 | if (file) { |
126 | struct address_space *mapping = file->f_mapping; | |
83cde9e8 | 127 | i_mmap_lock_write(mapping); |
1da177e4 | 128 | __remove_shared_vm_struct(vma, file, mapping); |
83cde9e8 | 129 | i_mmap_unlock_write(mapping); |
1da177e4 | 130 | } |
a8fb5618 HD |
131 | } |
132 | ||
133 | /* | |
763ecb03 | 134 | * Close a vm structure and free it. |
a8fb5618 | 135 | */ |
763ecb03 | 136 | static void remove_vma(struct vm_area_struct *vma) |
a8fb5618 | 137 | { |
a8fb5618 | 138 | might_sleep(); |
1da177e4 LT |
139 | if (vma->vm_ops && vma->vm_ops->close) |
140 | vma->vm_ops->close(vma); | |
e9714acf | 141 | if (vma->vm_file) |
a8fb5618 | 142 | fput(vma->vm_file); |
f0be3d32 | 143 | mpol_put(vma_policy(vma)); |
3928d4f5 | 144 | vm_area_free(vma); |
1da177e4 LT |
145 | } |
146 | ||
b62b633e LH |
147 | static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, |
148 | unsigned long min) | |
149 | { | |
150 | return mas_prev(&vmi->mas, min); | |
151 | } | |
152 | ||
153 | static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, | |
154 | unsigned long start, unsigned long end, gfp_t gfp) | |
155 | { | |
156 | vmi->mas.index = start; | |
157 | vmi->mas.last = end - 1; | |
158 | mas_store_gfp(&vmi->mas, NULL, gfp); | |
159 | if (unlikely(mas_is_err(&vmi->mas))) | |
160 | return -ENOMEM; | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
2e7ce7d3 LH |
165 | /* |
166 | * check_brk_limits() - Use platform specific check of range & verify mlock | |
167 | * limits. | |
168 | * @addr: The address to check | |
169 | * @len: The size of increase. | |
170 | * | |
171 | * Return: 0 on success. | |
172 | */ | |
173 | static int check_brk_limits(unsigned long addr, unsigned long len) | |
174 | { | |
175 | unsigned long mapped_addr; | |
176 | ||
177 | mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); | |
178 | if (IS_ERR_VALUE(mapped_addr)) | |
179 | return mapped_addr; | |
180 | ||
181 | return mlock_future_check(current->mm, current->mm->def_flags, len); | |
182 | } | |
92fed820 | 183 | static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, |
763ecb03 | 184 | unsigned long addr, unsigned long request, unsigned long flags); |
6a6160a7 | 185 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
1da177e4 | 186 | { |
9bc8039e | 187 | unsigned long newbrk, oldbrk, origbrk; |
1da177e4 | 188 | struct mm_struct *mm = current->mm; |
2e7ce7d3 | 189 | struct vm_area_struct *brkvma, *next = NULL; |
a5b4592c | 190 | unsigned long min_brk; |
128557ff | 191 | bool populate; |
9bc8039e | 192 | bool downgraded = false; |
897ab3e0 | 193 | LIST_HEAD(uf); |
92fed820 | 194 | struct vma_iterator vmi; |
1da177e4 | 195 | |
d8ed45c5 | 196 | if (mmap_write_lock_killable(mm)) |
dc0ef0df | 197 | return -EINTR; |
1da177e4 | 198 | |
9bc8039e YS |
199 | origbrk = mm->brk; |
200 | ||
a5b4592c | 201 | #ifdef CONFIG_COMPAT_BRK |
5520e894 JK |
202 | /* |
203 | * CONFIG_COMPAT_BRK can still be overridden by setting | |
204 | * randomize_va_space to 2, which will still cause mm->start_brk | |
205 | * to be arbitrarily shifted | |
206 | */ | |
4471a675 | 207 | if (current->brk_randomized) |
5520e894 JK |
208 | min_brk = mm->start_brk; |
209 | else | |
210 | min_brk = mm->end_data; | |
a5b4592c JK |
211 | #else |
212 | min_brk = mm->start_brk; | |
213 | #endif | |
214 | if (brk < min_brk) | |
1da177e4 | 215 | goto out; |
1e624196 RG |
216 | |
217 | /* | |
218 | * Check against rlimit here. If this check is done later after the test | |
219 | * of oldbrk with newbrk then it can escape the test and let the data | |
220 | * segment grow beyond its set limit the in case where the limit is | |
221 | * not page aligned -Ram Gupta | |
222 | */ | |
8764b338 CG |
223 | if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, |
224 | mm->end_data, mm->start_data)) | |
1e624196 RG |
225 | goto out; |
226 | ||
1da177e4 LT |
227 | newbrk = PAGE_ALIGN(brk); |
228 | oldbrk = PAGE_ALIGN(mm->brk); | |
9bc8039e YS |
229 | if (oldbrk == newbrk) { |
230 | mm->brk = brk; | |
231 | goto success; | |
232 | } | |
1da177e4 | 233 | |
9bc8039e YS |
234 | /* |
235 | * Always allow shrinking brk. | |
27b26701 | 236 | * do_vma_munmap() may downgrade mmap_lock to read. |
9bc8039e | 237 | */ |
1da177e4 | 238 | if (brk <= mm->brk) { |
9bc8039e YS |
239 | int ret; |
240 | ||
2e7ce7d3 | 241 | /* Search one past newbrk */ |
92fed820 LH |
242 | vma_iter_init(&vmi, mm, newbrk); |
243 | brkvma = vma_find(&vmi, oldbrk); | |
f5ad5083 | 244 | if (!brkvma || brkvma->vm_start >= oldbrk) |
2e7ce7d3 | 245 | goto out; /* mapping intersects with an existing non-brk vma. */ |
9bc8039e | 246 | /* |
2e7ce7d3 | 247 | * mm->brk must be protected by write mmap_lock. |
27b26701 LH |
248 | * do_vma_munmap() may downgrade the lock, so update it |
249 | * before calling do_vma_munmap(). | |
9bc8039e YS |
250 | */ |
251 | mm->brk = brk; | |
27b26701 | 252 | ret = do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true); |
2e7ce7d3 | 253 | if (ret == 1) { |
9bc8039e | 254 | downgraded = true; |
2e7ce7d3 LH |
255 | goto success; |
256 | } else if (!ret) | |
257 | goto success; | |
258 | ||
259 | mm->brk = origbrk; | |
260 | goto out; | |
1da177e4 LT |
261 | } |
262 | ||
2e7ce7d3 LH |
263 | if (check_brk_limits(oldbrk, newbrk - oldbrk)) |
264 | goto out; | |
265 | ||
266 | /* | |
267 | * Only check if the next VMA is within the stack_guard_gap of the | |
268 | * expansion area | |
269 | */ | |
92fed820 LH |
270 | vma_iter_init(&vmi, mm, oldbrk); |
271 | next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap); | |
1be7107f | 272 | if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) |
1da177e4 LT |
273 | goto out; |
274 | ||
92fed820 | 275 | brkvma = vma_prev_limit(&vmi, mm->start_brk); |
1da177e4 | 276 | /* Ok, looks good - let it rip. */ |
92fed820 | 277 | if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) |
1da177e4 | 278 | goto out; |
2e7ce7d3 | 279 | |
1da177e4 | 280 | mm->brk = brk; |
9bc8039e YS |
281 | |
282 | success: | |
128557ff | 283 | populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; |
9bc8039e | 284 | if (downgraded) |
d8ed45c5 | 285 | mmap_read_unlock(mm); |
9bc8039e | 286 | else |
d8ed45c5 | 287 | mmap_write_unlock(mm); |
897ab3e0 | 288 | userfaultfd_unmap_complete(mm, &uf); |
128557ff ML |
289 | if (populate) |
290 | mm_populate(oldbrk, newbrk - oldbrk); | |
291 | return brk; | |
292 | ||
1da177e4 | 293 | out: |
d8ed45c5 | 294 | mmap_write_unlock(mm); |
b7204006 | 295 | return origbrk; |
1da177e4 LT |
296 | } |
297 | ||
d4af56c5 LH |
298 | #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) |
299 | extern void mt_validate(struct maple_tree *mt); | |
300 | extern void mt_dump(const struct maple_tree *mt); | |
1da177e4 | 301 | |
d4af56c5 LH |
302 | /* Validate the maple tree */ |
303 | static void validate_mm_mt(struct mm_struct *mm) | |
304 | { | |
305 | struct maple_tree *mt = &mm->mm_mt; | |
763ecb03 | 306 | struct vm_area_struct *vma_mt; |
d4af56c5 LH |
307 | |
308 | MA_STATE(mas, mt, 0, 0); | |
309 | ||
310 | mt_validate(&mm->mm_mt); | |
311 | mas_for_each(&mas, vma_mt, ULONG_MAX) { | |
763ecb03 LH |
312 | if ((vma_mt->vm_start != mas.index) || |
313 | (vma_mt->vm_end - 1 != mas.last)) { | |
d4af56c5 LH |
314 | pr_emerg("issue in %s\n", current->comm); |
315 | dump_stack(); | |
d4af56c5 | 316 | dump_vma(vma_mt); |
d4af56c5 LH |
317 | pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, |
318 | mas.index, mas.last); | |
319 | pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, | |
320 | vma_mt->vm_start, vma_mt->vm_end); | |
d4af56c5 LH |
321 | |
322 | mt_dump(mas.tree); | |
323 | if (vma_mt->vm_end != mas.last + 1) { | |
324 | pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", | |
325 | mm, vma_mt->vm_start, vma_mt->vm_end, | |
326 | mas.index, mas.last); | |
327 | mt_dump(mas.tree); | |
328 | } | |
329 | VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); | |
330 | if (vma_mt->vm_start != mas.index) { | |
331 | pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", | |
332 | mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); | |
333 | mt_dump(mas.tree); | |
334 | } | |
335 | VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); | |
336 | } | |
d4af56c5 | 337 | } |
d4af56c5 | 338 | } |
1da177e4 | 339 | |
eafd4dc4 | 340 | static void validate_mm(struct mm_struct *mm) |
1da177e4 LT |
341 | { |
342 | int bug = 0; | |
343 | int i = 0; | |
763ecb03 LH |
344 | struct vm_area_struct *vma; |
345 | MA_STATE(mas, &mm->mm_mt, 0, 0); | |
ff26f70f | 346 | |
524e00b3 LH |
347 | validate_mm_mt(mm); |
348 | ||
763ecb03 | 349 | mas_for_each(&mas, vma, ULONG_MAX) { |
524e00b3 | 350 | #ifdef CONFIG_DEBUG_VM_RB |
12352d3c | 351 | struct anon_vma *anon_vma = vma->anon_vma; |
ed8ea815 | 352 | struct anon_vma_chain *avc; |
ff26f70f | 353 | |
12352d3c KK |
354 | if (anon_vma) { |
355 | anon_vma_lock_read(anon_vma); | |
356 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
357 | anon_vma_interval_tree_verify(avc); | |
358 | anon_vma_unlock_read(anon_vma); | |
359 | } | |
524e00b3 | 360 | #endif |
1da177e4 LT |
361 | i++; |
362 | } | |
5a0768f6 | 363 | if (i != mm->map_count) { |
763ecb03 | 364 | pr_emerg("map_count %d mas_for_each %d\n", mm->map_count, i); |
5a0768f6 ML |
365 | bug = 1; |
366 | } | |
96dad67f | 367 | VM_BUG_ON_MM(bug, mm); |
1da177e4 | 368 | } |
524e00b3 LH |
369 | |
370 | #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ | |
d4af56c5 | 371 | #define validate_mm_mt(root) do { } while (0) |
1da177e4 | 372 | #define validate_mm(mm) do { } while (0) |
524e00b3 | 373 | #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ |
8f26e0b1 | 374 | |
bf181b9f ML |
375 | /* |
376 | * vma has some anon_vma assigned, and is already inserted on that | |
377 | * anon_vma's interval trees. | |
378 | * | |
379 | * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the | |
380 | * vma must be removed from the anon_vma's interval trees using | |
381 | * anon_vma_interval_tree_pre_update_vma(). | |
382 | * | |
383 | * After the update, the vma will be reinserted using | |
384 | * anon_vma_interval_tree_post_update_vma(). | |
385 | * | |
c1e8d7c6 | 386 | * The entire update must be protected by exclusive mmap_lock and by |
bf181b9f ML |
387 | * the root anon_vma's mutex. |
388 | */ | |
389 | static inline void | |
390 | anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) | |
391 | { | |
392 | struct anon_vma_chain *avc; | |
393 | ||
394 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
395 | anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); | |
396 | } | |
397 | ||
398 | static inline void | |
399 | anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) | |
400 | { | |
401 | struct anon_vma_chain *avc; | |
402 | ||
403 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
404 | anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); | |
405 | } | |
406 | ||
e8420a8e CH |
407 | static unsigned long count_vma_pages_range(struct mm_struct *mm, |
408 | unsigned long addr, unsigned long end) | |
409 | { | |
2e3af1db | 410 | VMA_ITERATOR(vmi, mm, addr); |
e8420a8e | 411 | struct vm_area_struct *vma; |
2e3af1db | 412 | unsigned long nr_pages = 0; |
e8420a8e | 413 | |
2e3af1db MWO |
414 | for_each_vma_range(vmi, vma, end) { |
415 | unsigned long vm_start = max(addr, vma->vm_start); | |
416 | unsigned long vm_end = min(end, vma->vm_end); | |
e8420a8e | 417 | |
2e3af1db | 418 | nr_pages += PHYS_PFN(vm_end - vm_start); |
e8420a8e CH |
419 | } |
420 | ||
421 | return nr_pages; | |
422 | } | |
423 | ||
c154124f LH |
424 | static void __vma_link_file(struct vm_area_struct *vma, |
425 | struct address_space *mapping) | |
1da177e4 | 426 | { |
c154124f LH |
427 | if (vma->vm_flags & VM_SHARED) |
428 | mapping_allow_writable(mapping); | |
1da177e4 | 429 | |
c154124f LH |
430 | flush_dcache_mmap_lock(mapping); |
431 | vma_interval_tree_insert(vma, &mapping->i_mmap); | |
432 | flush_dcache_mmap_unlock(mapping); | |
1da177e4 LT |
433 | } |
434 | ||
763ecb03 | 435 | static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 436 | { |
79e4f2ca | 437 | VMA_ITERATOR(vmi, mm, 0); |
1da177e4 LT |
438 | struct address_space *mapping = NULL; |
439 | ||
79e4f2ca | 440 | if (vma_iter_prealloc(&vmi)) |
d4af56c5 LH |
441 | return -ENOMEM; |
442 | ||
64ac4940 | 443 | if (vma->vm_file) { |
1da177e4 | 444 | mapping = vma->vm_file->f_mapping; |
83cde9e8 | 445 | i_mmap_lock_write(mapping); |
64ac4940 | 446 | } |
1da177e4 | 447 | |
79e4f2ca | 448 | vma_iter_store(&vmi, vma); |
1da177e4 | 449 | |
c154124f LH |
450 | if (mapping) { |
451 | __vma_link_file(vma, mapping); | |
83cde9e8 | 452 | i_mmap_unlock_write(mapping); |
c154124f | 453 | } |
1da177e4 LT |
454 | |
455 | mm->map_count++; | |
456 | validate_mm(mm); | |
d4af56c5 | 457 | return 0; |
1da177e4 LT |
458 | } |
459 | ||
4dd1b841 LH |
460 | /* |
461 | * vma_expand - Expand an existing VMA | |
462 | * | |
463 | * @mas: The maple state | |
464 | * @vma: The vma to expand | |
465 | * @start: The start of the vma | |
466 | * @end: The exclusive end of the vma | |
467 | * @pgoff: The page offset of vma | |
468 | * @next: The current of next vma. | |
469 | * | |
470 | * Expand @vma to @start and @end. Can expand off the start and end. Will | |
471 | * expand over @next if it's different from @vma and @end == @next->vm_end. | |
472 | * Checking if the @vma can expand and merge with @next needs to be handled by | |
473 | * the caller. | |
474 | * | |
475 | * Returns: 0 on success | |
476 | */ | |
3c441ab7 | 477 | inline int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, |
4dd1b841 LH |
478 | unsigned long start, unsigned long end, pgoff_t pgoff, |
479 | struct vm_area_struct *next) | |
480 | { | |
481 | struct mm_struct *mm = vma->vm_mm; | |
482 | struct address_space *mapping = NULL; | |
483 | struct rb_root_cached *root = NULL; | |
484 | struct anon_vma *anon_vma = vma->anon_vma; | |
485 | struct file *file = vma->vm_file; | |
486 | bool remove_next = false; | |
487 | ||
488 | if (next && (vma != next) && (end == next->vm_end)) { | |
489 | remove_next = true; | |
490 | if (next->anon_vma && !vma->anon_vma) { | |
491 | int error; | |
492 | ||
493 | anon_vma = next->anon_vma; | |
494 | vma->anon_vma = anon_vma; | |
495 | error = anon_vma_clone(vma, next); | |
496 | if (error) | |
497 | return error; | |
498 | } | |
499 | } | |
500 | ||
501 | /* Not merging but overwriting any part of next is not handled. */ | |
502 | VM_BUG_ON(next && !remove_next && next != vma && end > next->vm_start); | |
503 | /* Only handles expanding */ | |
504 | VM_BUG_ON(vma->vm_start < start || vma->vm_end > end); | |
505 | ||
3c441ab7 | 506 | if (vma_iter_prealloc(vmi)) |
4dd1b841 LH |
507 | goto nomem; |
508 | ||
509 | vma_adjust_trans_huge(vma, start, end, 0); | |
510 | ||
511 | if (file) { | |
512 | mapping = file->f_mapping; | |
513 | root = &mapping->i_mmap; | |
514 | uprobe_munmap(vma, vma->vm_start, vma->vm_end); | |
515 | i_mmap_lock_write(mapping); | |
516 | } | |
517 | ||
518 | if (anon_vma) { | |
519 | anon_vma_lock_write(anon_vma); | |
520 | anon_vma_interval_tree_pre_update_vma(vma); | |
521 | } | |
522 | ||
523 | if (file) { | |
524 | flush_dcache_mmap_lock(mapping); | |
525 | vma_interval_tree_remove(vma, root); | |
526 | } | |
527 | ||
528 | vma->vm_start = start; | |
529 | vma->vm_end = end; | |
530 | vma->vm_pgoff = pgoff; | |
3c441ab7 | 531 | vma_iter_store(vmi, vma); |
4dd1b841 LH |
532 | |
533 | if (file) { | |
534 | vma_interval_tree_insert(vma, root); | |
535 | flush_dcache_mmap_unlock(mapping); | |
536 | } | |
537 | ||
538 | /* Expanding over the next vma */ | |
763ecb03 LH |
539 | if (remove_next && file) { |
540 | __remove_shared_vm_struct(next, file, mapping); | |
4dd1b841 LH |
541 | } |
542 | ||
543 | if (anon_vma) { | |
544 | anon_vma_interval_tree_post_update_vma(vma); | |
545 | anon_vma_unlock_write(anon_vma); | |
546 | } | |
547 | ||
548 | if (file) { | |
549 | i_mmap_unlock_write(mapping); | |
550 | uprobe_mmap(vma); | |
551 | } | |
552 | ||
553 | if (remove_next) { | |
554 | if (file) { | |
555 | uprobe_munmap(next, next->vm_start, next->vm_end); | |
556 | fput(file); | |
557 | } | |
558 | if (next->anon_vma) | |
559 | anon_vma_merge(vma, next); | |
560 | mm->map_count--; | |
561 | mpol_put(vma_policy(next)); | |
562 | vm_area_free(next); | |
563 | } | |
564 | ||
565 | validate_mm(mm); | |
566 | return 0; | |
567 | ||
568 | nomem: | |
569 | return -ENOMEM; | |
570 | } | |
571 | ||
1da177e4 LT |
572 | /* |
573 | * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that | |
574 | * is already present in an i_mmap tree without adjusting the tree. | |
575 | * The following helper function should be used when such adjustments | |
576 | * are necessary. The "insert" vma (if any) is to be inserted | |
577 | * before we drop the necessary locks. | |
578 | */ | |
9e560446 LH |
579 | int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, |
580 | unsigned long start, unsigned long end, pgoff_t pgoff, | |
581 | struct vm_area_struct *insert, struct vm_area_struct *expand) | |
1da177e4 LT |
582 | { |
583 | struct mm_struct *mm = vma->vm_mm; | |
1cd916d0 AM |
584 | struct vm_area_struct *next_next = NULL; /* uninit var warning */ |
585 | struct vm_area_struct *next = find_vma(mm, vma->vm_end); | |
524e00b3 | 586 | struct vm_area_struct *orig_vma = vma; |
1da177e4 | 587 | struct address_space *mapping = NULL; |
f808c13f | 588 | struct rb_root_cached *root = NULL; |
012f1800 | 589 | struct anon_vma *anon_vma = NULL; |
1da177e4 | 590 | struct file *file = vma->vm_file; |
524e00b3 | 591 | bool vma_changed = false; |
1da177e4 LT |
592 | long adjust_next = 0; |
593 | int remove_next = 0; | |
d4af56c5 | 594 | struct vm_area_struct *exporter = NULL, *importer = NULL; |
1da177e4 | 595 | |
d4af56c5 | 596 | if (next && !insert) { |
1da177e4 LT |
597 | if (end >= next->vm_end) { |
598 | /* | |
599 | * vma expands, overlapping all the next, and | |
600 | * perhaps the one after too (mprotect case 6). | |
86d12e47 | 601 | * The only other cases that gets here are |
e86f15ee | 602 | * case 1, case 7 and case 8. |
1da177e4 | 603 | */ |
e86f15ee AA |
604 | if (next == expand) { |
605 | /* | |
606 | * The only case where we don't expand "vma" | |
607 | * and we expand "next" instead is case 8. | |
608 | */ | |
609 | VM_WARN_ON(end != next->vm_end); | |
610 | /* | |
611 | * remove_next == 3 means we're | |
612 | * removing "vma" and that to do so we | |
613 | * swapped "vma" and "next". | |
614 | */ | |
615 | remove_next = 3; | |
616 | VM_WARN_ON(file != next->vm_file); | |
617 | swap(vma, next); | |
618 | } else { | |
619 | VM_WARN_ON(expand != vma); | |
620 | /* | |
621 | * case 1, 6, 7, remove_next == 2 is case 6, | |
622 | * remove_next == 1 is case 1 or 7. | |
623 | */ | |
624 | remove_next = 1 + (end > next->vm_end); | |
d4af56c5 LH |
625 | if (remove_next == 2) |
626 | next_next = find_vma(mm, next->vm_end); | |
627 | ||
e86f15ee | 628 | VM_WARN_ON(remove_next == 2 && |
763ecb03 | 629 | end != next_next->vm_end); |
e86f15ee AA |
630 | } |
631 | ||
287d97ac | 632 | exporter = next; |
1da177e4 | 633 | importer = vma; |
734537c9 KS |
634 | |
635 | /* | |
636 | * If next doesn't have anon_vma, import from vma after | |
637 | * next, if the vma overlaps with it. | |
638 | */ | |
97a42cd4 | 639 | if (remove_next == 2 && !next->anon_vma) |
763ecb03 | 640 | exporter = next_next; |
734537c9 | 641 | |
1da177e4 LT |
642 | } else if (end > next->vm_start) { |
643 | /* | |
644 | * vma expands, overlapping part of the next: | |
645 | * mprotect case 5 shifting the boundary up. | |
646 | */ | |
f9d86a60 | 647 | adjust_next = (end - next->vm_start); |
287d97ac | 648 | exporter = next; |
1da177e4 | 649 | importer = vma; |
e86f15ee | 650 | VM_WARN_ON(expand != importer); |
1da177e4 LT |
651 | } else if (end < vma->vm_end) { |
652 | /* | |
653 | * vma shrinks, and !insert tells it's not | |
654 | * split_vma inserting another: so it must be | |
655 | * mprotect case 4 shifting the boundary down. | |
656 | */ | |
f9d86a60 | 657 | adjust_next = -(vma->vm_end - end); |
287d97ac | 658 | exporter = vma; |
1da177e4 | 659 | importer = next; |
e86f15ee | 660 | VM_WARN_ON(expand != importer); |
1da177e4 | 661 | } |
1da177e4 | 662 | |
5beb4930 RR |
663 | /* |
664 | * Easily overlooked: when mprotect shifts the boundary, | |
665 | * make sure the expanding vma has anon_vma set if the | |
666 | * shrinking vma had, to cover any anon pages imported. | |
667 | */ | |
287d97ac | 668 | if (exporter && exporter->anon_vma && !importer->anon_vma) { |
c4ea95d7 DF |
669 | int error; |
670 | ||
b800c91a | 671 | importer->anon_vma = exporter->anon_vma; |
c4ea95d7 | 672 | error = anon_vma_clone(importer, exporter); |
3fe89b3e | 673 | if (error) |
c4ea95d7 | 674 | return error; |
5beb4930 RR |
675 | } |
676 | } | |
37f9f559 | 677 | |
9e560446 | 678 | if (vma_iter_prealloc(vmi)) |
d4af56c5 LH |
679 | return -ENOMEM; |
680 | ||
681 | vma_adjust_trans_huge(orig_vma, start, end, adjust_next); | |
1da177e4 LT |
682 | if (file) { |
683 | mapping = file->f_mapping; | |
27ba0644 KS |
684 | root = &mapping->i_mmap; |
685 | uprobe_munmap(vma, vma->vm_start, vma->vm_end); | |
682968e0 | 686 | |
27ba0644 KS |
687 | if (adjust_next) |
688 | uprobe_munmap(next, next->vm_start, next->vm_end); | |
682968e0 | 689 | |
83cde9e8 | 690 | i_mmap_lock_write(mapping); |
c154124f | 691 | if (insert && insert->vm_file) { |
1da177e4 | 692 | /* |
6b2dbba8 | 693 | * Put into interval tree now, so instantiated pages |
1da177e4 LT |
694 | * are visible to arm/parisc __flush_dcache_page |
695 | * throughout; but we cannot insert into address | |
696 | * space until vma start or end is updated. | |
697 | */ | |
c154124f | 698 | __vma_link_file(insert, insert->vm_file->f_mapping); |
1da177e4 LT |
699 | } |
700 | } | |
701 | ||
bf181b9f ML |
702 | anon_vma = vma->anon_vma; |
703 | if (!anon_vma && adjust_next) | |
704 | anon_vma = next->anon_vma; | |
705 | if (anon_vma) { | |
e86f15ee AA |
706 | VM_WARN_ON(adjust_next && next->anon_vma && |
707 | anon_vma != next->anon_vma); | |
4fc3f1d6 | 708 | anon_vma_lock_write(anon_vma); |
bf181b9f ML |
709 | anon_vma_interval_tree_pre_update_vma(vma); |
710 | if (adjust_next) | |
711 | anon_vma_interval_tree_pre_update_vma(next); | |
712 | } | |
012f1800 | 713 | |
0fc48a6e | 714 | if (file) { |
1da177e4 | 715 | flush_dcache_mmap_lock(mapping); |
6b2dbba8 | 716 | vma_interval_tree_remove(vma, root); |
1da177e4 | 717 | if (adjust_next) |
6b2dbba8 | 718 | vma_interval_tree_remove(next, root); |
1da177e4 LT |
719 | } |
720 | ||
d3737187 | 721 | if (start != vma->vm_start) { |
c465be97 LH |
722 | if (vma->vm_start < start) { |
723 | if (!insert || (insert->vm_end != start)) { | |
724 | vma_iter_clear(vmi, vma->vm_start, start); | |
725 | vma_iter_set(vmi, start); | |
726 | VM_WARN_ON(insert && insert->vm_start > vma->vm_start); | |
727 | } | |
3b0e81a1 LH |
728 | } else { |
729 | vma_changed = true; | |
730 | } | |
d3737187 | 731 | vma->vm_start = start; |
d3737187 ML |
732 | } |
733 | if (end != vma->vm_end) { | |
3b0e81a1 LH |
734 | if (vma->vm_end > end) { |
735 | if (!insert || (insert->vm_start != end)) { | |
9e560446 LH |
736 | vma_iter_clear(vmi, end, vma->vm_end); |
737 | vma_iter_set(vmi, vma->vm_end); | |
3b0e81a1 LH |
738 | VM_WARN_ON(insert && |
739 | insert->vm_end < vma->vm_end); | |
3b0e81a1 LH |
740 | } |
741 | } else { | |
742 | vma_changed = true; | |
743 | } | |
d3737187 | 744 | vma->vm_end = end; |
d3737187 | 745 | } |
d4af56c5 | 746 | |
524e00b3 | 747 | if (vma_changed) |
9e560446 | 748 | vma_iter_store(vmi, vma); |
d4af56c5 | 749 | |
1da177e4 LT |
750 | vma->vm_pgoff = pgoff; |
751 | if (adjust_next) { | |
f9d86a60 WY |
752 | next->vm_start += adjust_next; |
753 | next->vm_pgoff += adjust_next >> PAGE_SHIFT; | |
9e560446 | 754 | vma_iter_store(vmi, next); |
1da177e4 LT |
755 | } |
756 | ||
0fc48a6e | 757 | if (file) { |
1da177e4 | 758 | if (adjust_next) |
6b2dbba8 ML |
759 | vma_interval_tree_insert(next, root); |
760 | vma_interval_tree_insert(vma, root); | |
1da177e4 LT |
761 | flush_dcache_mmap_unlock(mapping); |
762 | } | |
763 | ||
763ecb03 LH |
764 | if (remove_next && file) { |
765 | __remove_shared_vm_struct(next, file, mapping); | |
524e00b3 | 766 | if (remove_next == 2) |
763ecb03 | 767 | __remove_shared_vm_struct(next_next, file, mapping); |
1da177e4 LT |
768 | } else if (insert) { |
769 | /* | |
770 | * split_vma has split insert from vma, and needs | |
771 | * us to insert it before dropping the locks | |
772 | * (it may either follow vma or precede it). | |
773 | */ | |
9e560446 | 774 | vma_iter_store(vmi, insert); |
763ecb03 | 775 | mm->map_count++; |
1da177e4 LT |
776 | } |
777 | ||
bf181b9f ML |
778 | if (anon_vma) { |
779 | anon_vma_interval_tree_post_update_vma(vma); | |
780 | if (adjust_next) | |
781 | anon_vma_interval_tree_post_update_vma(next); | |
08b52706 | 782 | anon_vma_unlock_write(anon_vma); |
bf181b9f | 783 | } |
1da177e4 | 784 | |
0fc48a6e | 785 | if (file) { |
808fbdbe | 786 | i_mmap_unlock_write(mapping); |
7b2d81d4 | 787 | uprobe_mmap(vma); |
2b144498 SD |
788 | |
789 | if (adjust_next) | |
7b2d81d4 | 790 | uprobe_mmap(next); |
2b144498 SD |
791 | } |
792 | ||
1da177e4 | 793 | if (remove_next) { |
d4af56c5 | 794 | again: |
925d1c40 | 795 | if (file) { |
cbc91f71 | 796 | uprobe_munmap(next, next->vm_start, next->vm_end); |
1da177e4 | 797 | fput(file); |
925d1c40 | 798 | } |
5beb4930 RR |
799 | if (next->anon_vma) |
800 | anon_vma_merge(vma, next); | |
1da177e4 | 801 | mm->map_count--; |
3964acd0 | 802 | mpol_put(vma_policy(next)); |
524e00b3 LH |
803 | if (remove_next != 2) |
804 | BUG_ON(vma->vm_end < next->vm_end); | |
3928d4f5 | 805 | vm_area_free(next); |
524e00b3 | 806 | |
1da177e4 LT |
807 | /* |
808 | * In mprotect's case 6 (see comments on vma_merge), | |
763ecb03 | 809 | * we must remove next_next too. |
1da177e4 | 810 | */ |
734537c9 KS |
811 | if (remove_next == 2) { |
812 | remove_next = 1; | |
763ecb03 | 813 | next = next_next; |
1da177e4 | 814 | goto again; |
fb8c41e9 | 815 | } |
1da177e4 | 816 | } |
2b144498 | 817 | if (insert && file) |
7b2d81d4 | 818 | uprobe_mmap(insert); |
1da177e4 | 819 | |
9e560446 | 820 | vma_iter_free(vmi); |
1da177e4 | 821 | validate_mm(mm); |
763ecb03 | 822 | |
5beb4930 | 823 | return 0; |
1da177e4 LT |
824 | } |
825 | ||
826 | /* | |
827 | * If the vma has a ->close operation then the driver probably needs to release | |
828 | * per-vma resources, so we don't attempt to merge those. | |
829 | */ | |
1da177e4 | 830 | static inline int is_mergeable_vma(struct vm_area_struct *vma, |
19a809af | 831 | struct file *file, unsigned long vm_flags, |
9a10064f | 832 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 833 | struct anon_vma_name *anon_name) |
1da177e4 | 834 | { |
34228d47 CG |
835 | /* |
836 | * VM_SOFTDIRTY should not prevent from VMA merging, if we | |
837 | * match the flags but dirty bit -- the caller should mark | |
838 | * merged VMA as dirty. If dirty bit won't be excluded from | |
8bb4e7a2 | 839 | * comparison, we increase pressure on the memory system forcing |
34228d47 CG |
840 | * the kernel to generate new VMAs when old one could be |
841 | * extended instead. | |
842 | */ | |
843 | if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) | |
1da177e4 LT |
844 | return 0; |
845 | if (vma->vm_file != file) | |
846 | return 0; | |
847 | if (vma->vm_ops && vma->vm_ops->close) | |
848 | return 0; | |
19a809af AA |
849 | if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) |
850 | return 0; | |
5c26f6ac | 851 | if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) |
9a10064f | 852 | return 0; |
1da177e4 LT |
853 | return 1; |
854 | } | |
855 | ||
856 | static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, | |
965f55de SL |
857 | struct anon_vma *anon_vma2, |
858 | struct vm_area_struct *vma) | |
1da177e4 | 859 | { |
965f55de SL |
860 | /* |
861 | * The list_is_singular() test is to avoid merging VMA cloned from | |
862 | * parents. This can improve scalability caused by anon_vma lock. | |
863 | */ | |
864 | if ((!anon_vma1 || !anon_vma2) && (!vma || | |
865 | list_is_singular(&vma->anon_vma_chain))) | |
866 | return 1; | |
867 | return anon_vma1 == anon_vma2; | |
1da177e4 LT |
868 | } |
869 | ||
870 | /* | |
871 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) | |
872 | * in front of (at a lower virtual address and file offset than) the vma. | |
873 | * | |
874 | * We cannot merge two vmas if they have differently assigned (non-NULL) | |
875 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. | |
876 | * | |
877 | * We don't check here for the merged mmap wrapping around the end of pagecache | |
45e55300 | 878 | * indices (16TB on ia32) because do_mmap() does not permit mmap's which |
1da177e4 LT |
879 | * wrap, nor mmaps which cover the final page at index -1UL. |
880 | */ | |
881 | static int | |
882 | can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, | |
19a809af AA |
883 | struct anon_vma *anon_vma, struct file *file, |
884 | pgoff_t vm_pgoff, | |
9a10064f | 885 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 886 | struct anon_vma_name *anon_name) |
1da177e4 | 887 | { |
9a10064f | 888 | if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && |
965f55de | 889 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
1da177e4 LT |
890 | if (vma->vm_pgoff == vm_pgoff) |
891 | return 1; | |
892 | } | |
893 | return 0; | |
894 | } | |
895 | ||
896 | /* | |
897 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) | |
898 | * beyond (at a higher virtual address and file offset than) the vma. | |
899 | * | |
900 | * We cannot merge two vmas if they have differently assigned (non-NULL) | |
901 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. | |
902 | */ | |
903 | static int | |
904 | can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, | |
19a809af AA |
905 | struct anon_vma *anon_vma, struct file *file, |
906 | pgoff_t vm_pgoff, | |
9a10064f | 907 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 908 | struct anon_vma_name *anon_name) |
1da177e4 | 909 | { |
9a10064f | 910 | if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && |
965f55de | 911 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
1da177e4 | 912 | pgoff_t vm_pglen; |
d6e93217 | 913 | vm_pglen = vma_pages(vma); |
1da177e4 LT |
914 | if (vma->vm_pgoff + vm_pglen == vm_pgoff) |
915 | return 1; | |
916 | } | |
917 | return 0; | |
918 | } | |
919 | ||
920 | /* | |
9a10064f CC |
921 | * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), |
922 | * figure out whether that can be merged with its predecessor or its | |
923 | * successor. Or both (it neatly fills a hole). | |
1da177e4 LT |
924 | * |
925 | * In most cases - when called for mmap, brk or mremap - [addr,end) is | |
926 | * certain not to be mapped by the time vma_merge is called; but when | |
927 | * called for mprotect, it is certain to be already mapped (either at | |
928 | * an offset within prev, or at the start of next), and the flags of | |
929 | * this area are about to be changed to vm_flags - and the no-change | |
930 | * case has already been eliminated. | |
931 | * | |
932 | * The following mprotect cases have to be considered, where AAAA is | |
933 | * the area passed down from mprotect_fixup, never extending beyond one | |
934 | * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: | |
935 | * | |
5d42ab29 WY |
936 | * AAAA AAAA AAAA |
937 | * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN | |
938 | * cannot merge might become might become | |
939 | * PPNNNNNNNNNN PPPPPPPPPPNN | |
940 | * mmap, brk or case 4 below case 5 below | |
941 | * mremap move: | |
942 | * AAAA AAAA | |
943 | * PPPP NNNN PPPPNNNNXXXX | |
944 | * might become might become | |
945 | * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or | |
946 | * PPPPPPPPNNNN 2 or PPPPPPPPXXXX 7 or | |
947 | * PPPPNNNNNNNN 3 PPPPXXXXXXXX 8 | |
1da177e4 | 948 | * |
8bb4e7a2 | 949 | * It is important for case 8 that the vma NNNN overlapping the |
e86f15ee AA |
950 | * region AAAA is never going to extended over XXXX. Instead XXXX must |
951 | * be extended in region AAAA and NNNN must be removed. This way in | |
952 | * all cases where vma_merge succeeds, the moment vma_adjust drops the | |
953 | * rmap_locks, the properties of the merged vma will be already | |
954 | * correct for the whole merged range. Some of those properties like | |
955 | * vm_page_prot/vm_flags may be accessed by rmap_walks and they must | |
956 | * be correct for the whole merged range immediately after the | |
957 | * rmap_locks are released. Otherwise if XXXX would be removed and | |
958 | * NNNN would be extended over the XXXX range, remove_migration_ptes | |
959 | * or other rmap walkers (if working on addresses beyond the "end" | |
960 | * parameter) may establish ptes with the wrong permissions of NNNN | |
961 | * instead of the right permissions of XXXX. | |
1da177e4 | 962 | */ |
9760ebff | 963 | struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, |
1da177e4 LT |
964 | struct vm_area_struct *prev, unsigned long addr, |
965 | unsigned long end, unsigned long vm_flags, | |
cc71aba3 | 966 | struct anon_vma *anon_vma, struct file *file, |
19a809af | 967 | pgoff_t pgoff, struct mempolicy *policy, |
9a10064f | 968 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 969 | struct anon_vma_name *anon_name) |
1da177e4 LT |
970 | { |
971 | pgoff_t pglen = (end - addr) >> PAGE_SHIFT; | |
9760ebff | 972 | struct vm_area_struct *mid, *next, *res = NULL; |
eef19944 JM |
973 | int err = -1; |
974 | bool merge_prev = false; | |
975 | bool merge_next = false; | |
1da177e4 LT |
976 | |
977 | /* | |
978 | * We later require that vma->vm_flags == vm_flags, | |
979 | * so this tests vma->vm_flags & VM_SPECIAL, too. | |
980 | */ | |
981 | if (vm_flags & VM_SPECIAL) | |
982 | return NULL; | |
983 | ||
763ecb03 | 984 | next = find_vma(mm, prev ? prev->vm_end : 0); |
eef19944 JM |
985 | mid = next; |
986 | if (next && next->vm_end == end) /* cases 6, 7, 8 */ | |
763ecb03 | 987 | next = find_vma(mm, next->vm_end); |
1da177e4 | 988 | |
e86f15ee AA |
989 | /* verify some invariant that must be enforced by the caller */ |
990 | VM_WARN_ON(prev && addr <= prev->vm_start); | |
eef19944 | 991 | VM_WARN_ON(mid && end > mid->vm_end); |
e86f15ee AA |
992 | VM_WARN_ON(addr >= end); |
993 | ||
eef19944 | 994 | /* Can we merge the predecessor? */ |
1da177e4 | 995 | if (prev && prev->vm_end == addr && |
cc71aba3 | 996 | mpol_equal(vma_policy(prev), policy) && |
1da177e4 | 997 | can_vma_merge_after(prev, vm_flags, |
19a809af | 998 | anon_vma, file, pgoff, |
9a10064f | 999 | vm_userfaultfd_ctx, anon_name)) { |
eef19944 | 1000 | merge_prev = true; |
1da177e4 | 1001 | } |
eef19944 | 1002 | /* Can we merge the successor? */ |
1da177e4 | 1003 | if (next && end == next->vm_start && |
cc71aba3 | 1004 | mpol_equal(policy, vma_policy(next)) && |
1da177e4 | 1005 | can_vma_merge_before(next, vm_flags, |
19a809af | 1006 | anon_vma, file, pgoff+pglen, |
9a10064f | 1007 | vm_userfaultfd_ctx, anon_name)) { |
eef19944 JM |
1008 | merge_next = true; |
1009 | } | |
1010 | /* Can we merge both the predecessor and the successor? */ | |
1011 | if (merge_prev && merge_next && | |
1012 | is_mergeable_anon_vma(prev->anon_vma, | |
1013 | next->anon_vma, NULL)) { /* cases 1, 6 */ | |
9e560446 | 1014 | err = __vma_adjust(vmi, prev, prev->vm_start, |
eef19944 JM |
1015 | next->vm_end, prev->vm_pgoff, NULL, |
1016 | prev); | |
1017 | res = prev; | |
1018 | } else if (merge_prev) { /* cases 2, 5, 7 */ | |
9e560446 | 1019 | err = __vma_adjust(vmi, prev, prev->vm_start, |
eef19944 JM |
1020 | end, prev->vm_pgoff, NULL, prev); |
1021 | res = prev; | |
1022 | } else if (merge_next) { | |
1da177e4 | 1023 | if (prev && addr < prev->vm_end) /* case 4 */ |
9e560446 | 1024 | err = __vma_adjust(vmi, prev, prev->vm_start, |
eef19944 JM |
1025 | addr, prev->vm_pgoff, NULL, next); |
1026 | else /* cases 3, 8 */ | |
9e560446 | 1027 | err = __vma_adjust(vmi, mid, addr, next->vm_end, |
eef19944 JM |
1028 | next->vm_pgoff - pglen, NULL, next); |
1029 | res = next; | |
1da177e4 LT |
1030 | } |
1031 | ||
eef19944 JM |
1032 | /* |
1033 | * Cannot merge with predecessor or successor or error in __vma_adjust? | |
1034 | */ | |
1035 | if (err) | |
1036 | return NULL; | |
1037 | khugepaged_enter_vma(res, vm_flags); | |
1da177e4 | 1038 | |
9760ebff | 1039 | if (res) |
f2ebfe43 LH |
1040 | vma_iter_set(vmi, end); |
1041 | ||
9760ebff | 1042 | return res; |
f2ebfe43 LH |
1043 | } |
1044 | ||
d0e9fe17 | 1045 | /* |
b4f315b4 | 1046 | * Rough compatibility check to quickly see if it's even worth looking |
d0e9fe17 LT |
1047 | * at sharing an anon_vma. |
1048 | * | |
1049 | * They need to have the same vm_file, and the flags can only differ | |
1050 | * in things that mprotect may change. | |
1051 | * | |
1052 | * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that | |
1053 | * we can merge the two vma's. For example, we refuse to merge a vma if | |
1054 | * there is a vm_ops->close() function, because that indicates that the | |
1055 | * driver is doing some kind of reference counting. But that doesn't | |
1056 | * really matter for the anon_vma sharing case. | |
1057 | */ | |
1058 | static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) | |
1059 | { | |
1060 | return a->vm_end == b->vm_start && | |
1061 | mpol_equal(vma_policy(a), vma_policy(b)) && | |
1062 | a->vm_file == b->vm_file && | |
6cb4d9a2 | 1063 | !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && |
d0e9fe17 LT |
1064 | b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); |
1065 | } | |
1066 | ||
1067 | /* | |
1068 | * Do some basic sanity checking to see if we can re-use the anon_vma | |
1069 | * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be | |
1070 | * the same as 'old', the other will be the new one that is trying | |
1071 | * to share the anon_vma. | |
1072 | * | |
5b449489 | 1073 | * NOTE! This runs with mmap_lock held for reading, so it is possible that |
d0e9fe17 LT |
1074 | * the anon_vma of 'old' is concurrently in the process of being set up |
1075 | * by another page fault trying to merge _that_. But that's ok: if it | |
1076 | * is being set up, that automatically means that it will be a singleton | |
1077 | * acceptable for merging, so we can do all of this optimistically. But | |
4db0c3c2 | 1078 | * we do that READ_ONCE() to make sure that we never re-load the pointer. |
d0e9fe17 LT |
1079 | * |
1080 | * IOW: that the "list_is_singular()" test on the anon_vma_chain only | |
1081 | * matters for the 'stable anon_vma' case (ie the thing we want to avoid | |
1082 | * is to return an anon_vma that is "complex" due to having gone through | |
1083 | * a fork). | |
1084 | * | |
1085 | * We also make sure that the two vma's are compatible (adjacent, | |
1086 | * and with the same memory policies). That's all stable, even with just | |
5b449489 | 1087 | * a read lock on the mmap_lock. |
d0e9fe17 LT |
1088 | */ |
1089 | static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) | |
1090 | { | |
1091 | if (anon_vma_compatible(a, b)) { | |
4db0c3c2 | 1092 | struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); |
d0e9fe17 LT |
1093 | |
1094 | if (anon_vma && list_is_singular(&old->anon_vma_chain)) | |
1095 | return anon_vma; | |
1096 | } | |
1097 | return NULL; | |
1098 | } | |
1099 | ||
1da177e4 LT |
1100 | /* |
1101 | * find_mergeable_anon_vma is used by anon_vma_prepare, to check | |
1102 | * neighbouring vmas for a suitable anon_vma, before it goes off | |
1103 | * to allocate a new anon_vma. It checks because a repetitive | |
1104 | * sequence of mprotects and faults may otherwise lead to distinct | |
1105 | * anon_vmas being allocated, preventing vma merge in subsequent | |
1106 | * mprotect. | |
1107 | */ | |
1108 | struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) | |
1109 | { | |
763ecb03 | 1110 | MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); |
a67c8caa | 1111 | struct anon_vma *anon_vma = NULL; |
763ecb03 | 1112 | struct vm_area_struct *prev, *next; |
a67c8caa ML |
1113 | |
1114 | /* Try next first. */ | |
763ecb03 LH |
1115 | next = mas_walk(&mas); |
1116 | if (next) { | |
1117 | anon_vma = reusable_anon_vma(next, vma, next); | |
a67c8caa ML |
1118 | if (anon_vma) |
1119 | return anon_vma; | |
1120 | } | |
1121 | ||
763ecb03 LH |
1122 | prev = mas_prev(&mas, 0); |
1123 | VM_BUG_ON_VMA(prev != vma, vma); | |
1124 | prev = mas_prev(&mas, 0); | |
a67c8caa | 1125 | /* Try prev next. */ |
763ecb03 LH |
1126 | if (prev) |
1127 | anon_vma = reusable_anon_vma(prev, prev, vma); | |
a67c8caa | 1128 | |
1da177e4 | 1129 | /* |
a67c8caa ML |
1130 | * We might reach here with anon_vma == NULL if we can't find |
1131 | * any reusable anon_vma. | |
1da177e4 LT |
1132 | * There's no absolute need to look only at touching neighbours: |
1133 | * we could search further afield for "compatible" anon_vmas. | |
1134 | * But it would probably just be a waste of time searching, | |
1135 | * or lead to too many vmas hanging off the same anon_vma. | |
1136 | * We're trying to allow mprotect remerging later on, | |
1137 | * not trying to minimize memory used for anon_vmas. | |
1138 | */ | |
a67c8caa | 1139 | return anon_vma; |
1da177e4 LT |
1140 | } |
1141 | ||
40401530 AV |
1142 | /* |
1143 | * If a hint addr is less than mmap_min_addr change hint to be as | |
1144 | * low as possible but still greater than mmap_min_addr | |
1145 | */ | |
1146 | static inline unsigned long round_hint_to_min(unsigned long hint) | |
1147 | { | |
1148 | hint &= PAGE_MASK; | |
1149 | if (((void *)hint != NULL) && | |
1150 | (hint < mmap_min_addr)) | |
1151 | return PAGE_ALIGN(mmap_min_addr); | |
1152 | return hint; | |
1153 | } | |
1154 | ||
6aeb2542 MR |
1155 | int mlock_future_check(struct mm_struct *mm, unsigned long flags, |
1156 | unsigned long len) | |
363ee17f DB |
1157 | { |
1158 | unsigned long locked, lock_limit; | |
1159 | ||
1160 | /* mlock MCL_FUTURE? */ | |
1161 | if (flags & VM_LOCKED) { | |
1162 | locked = len >> PAGE_SHIFT; | |
1163 | locked += mm->locked_vm; | |
1164 | lock_limit = rlimit(RLIMIT_MEMLOCK); | |
1165 | lock_limit >>= PAGE_SHIFT; | |
1166 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) | |
1167 | return -EAGAIN; | |
1168 | } | |
1169 | return 0; | |
1170 | } | |
1171 | ||
be83bbf8 LT |
1172 | static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) |
1173 | { | |
1174 | if (S_ISREG(inode->i_mode)) | |
423913ad | 1175 | return MAX_LFS_FILESIZE; |
be83bbf8 LT |
1176 | |
1177 | if (S_ISBLK(inode->i_mode)) | |
1178 | return MAX_LFS_FILESIZE; | |
1179 | ||
76f34950 IK |
1180 | if (S_ISSOCK(inode->i_mode)) |
1181 | return MAX_LFS_FILESIZE; | |
1182 | ||
be83bbf8 LT |
1183 | /* Special "we do even unsigned file positions" case */ |
1184 | if (file->f_mode & FMODE_UNSIGNED_OFFSET) | |
1185 | return 0; | |
1186 | ||
1187 | /* Yes, random drivers might want more. But I'm tired of buggy drivers */ | |
1188 | return ULONG_MAX; | |
1189 | } | |
1190 | ||
1191 | static inline bool file_mmap_ok(struct file *file, struct inode *inode, | |
1192 | unsigned long pgoff, unsigned long len) | |
1193 | { | |
1194 | u64 maxsize = file_mmap_size_max(file, inode); | |
1195 | ||
1196 | if (maxsize && len > maxsize) | |
1197 | return false; | |
1198 | maxsize -= len; | |
1199 | if (pgoff > maxsize >> PAGE_SHIFT) | |
1200 | return false; | |
1201 | return true; | |
1202 | } | |
1203 | ||
1da177e4 | 1204 | /* |
3e4e28c5 | 1205 | * The caller must write-lock current->mm->mmap_lock. |
1da177e4 | 1206 | */ |
1fcfd8db | 1207 | unsigned long do_mmap(struct file *file, unsigned long addr, |
1da177e4 | 1208 | unsigned long len, unsigned long prot, |
45e55300 PC |
1209 | unsigned long flags, unsigned long pgoff, |
1210 | unsigned long *populate, struct list_head *uf) | |
1da177e4 | 1211 | { |
cc71aba3 | 1212 | struct mm_struct *mm = current->mm; |
45e55300 | 1213 | vm_flags_t vm_flags; |
62b5f7d0 | 1214 | int pkey = 0; |
1da177e4 | 1215 | |
524e00b3 | 1216 | validate_mm(mm); |
41badc15 | 1217 | *populate = 0; |
bebeb3d6 | 1218 | |
e37609bb PK |
1219 | if (!len) |
1220 | return -EINVAL; | |
1221 | ||
1da177e4 LT |
1222 | /* |
1223 | * Does the application expect PROT_READ to imply PROT_EXEC? | |
1224 | * | |
1225 | * (the exception is when the underlying filesystem is noexec | |
1226 | * mounted, in which case we dont add PROT_EXEC.) | |
1227 | */ | |
1228 | if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) | |
90f8572b | 1229 | if (!(file && path_noexec(&file->f_path))) |
1da177e4 LT |
1230 | prot |= PROT_EXEC; |
1231 | ||
a4ff8e86 MH |
1232 | /* force arch specific MAP_FIXED handling in get_unmapped_area */ |
1233 | if (flags & MAP_FIXED_NOREPLACE) | |
1234 | flags |= MAP_FIXED; | |
1235 | ||
7cd94146 EP |
1236 | if (!(flags & MAP_FIXED)) |
1237 | addr = round_hint_to_min(addr); | |
1238 | ||
1da177e4 LT |
1239 | /* Careful about overflows.. */ |
1240 | len = PAGE_ALIGN(len); | |
9206de95 | 1241 | if (!len) |
1da177e4 LT |
1242 | return -ENOMEM; |
1243 | ||
1244 | /* offset overflow? */ | |
1245 | if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) | |
cc71aba3 | 1246 | return -EOVERFLOW; |
1da177e4 LT |
1247 | |
1248 | /* Too many mappings? */ | |
1249 | if (mm->map_count > sysctl_max_map_count) | |
1250 | return -ENOMEM; | |
1251 | ||
1252 | /* Obtain the address to map to. we verify (or select) it and ensure | |
1253 | * that it represents a valid section of the address space. | |
1254 | */ | |
1255 | addr = get_unmapped_area(file, addr, len, pgoff, flags); | |
ff68dac6 | 1256 | if (IS_ERR_VALUE(addr)) |
1da177e4 LT |
1257 | return addr; |
1258 | ||
a4ff8e86 | 1259 | if (flags & MAP_FIXED_NOREPLACE) { |
35e43c5f | 1260 | if (find_vma_intersection(mm, addr, addr + len)) |
a4ff8e86 MH |
1261 | return -EEXIST; |
1262 | } | |
1263 | ||
62b5f7d0 DH |
1264 | if (prot == PROT_EXEC) { |
1265 | pkey = execute_only_pkey(mm); | |
1266 | if (pkey < 0) | |
1267 | pkey = 0; | |
1268 | } | |
1269 | ||
1da177e4 LT |
1270 | /* Do simple checking here so the lower-level routines won't have |
1271 | * to. we assume access permissions have been handled by the open | |
1272 | * of the memory object, so we don't do any here. | |
1273 | */ | |
45e55300 | 1274 | vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | |
1da177e4 LT |
1275 | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
1276 | ||
cdf7b341 | 1277 | if (flags & MAP_LOCKED) |
1da177e4 LT |
1278 | if (!can_do_mlock()) |
1279 | return -EPERM; | |
ba470de4 | 1280 | |
363ee17f DB |
1281 | if (mlock_future_check(mm, vm_flags, len)) |
1282 | return -EAGAIN; | |
1da177e4 | 1283 | |
1da177e4 | 1284 | if (file) { |
077bf22b | 1285 | struct inode *inode = file_inode(file); |
1c972597 DW |
1286 | unsigned long flags_mask; |
1287 | ||
be83bbf8 LT |
1288 | if (!file_mmap_ok(file, inode, pgoff, len)) |
1289 | return -EOVERFLOW; | |
1290 | ||
1c972597 | 1291 | flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; |
077bf22b | 1292 | |
1da177e4 LT |
1293 | switch (flags & MAP_TYPE) { |
1294 | case MAP_SHARED: | |
1c972597 DW |
1295 | /* |
1296 | * Force use of MAP_SHARED_VALIDATE with non-legacy | |
1297 | * flags. E.g. MAP_SYNC is dangerous to use with | |
1298 | * MAP_SHARED as you don't know which consistency model | |
1299 | * you will get. We silently ignore unsupported flags | |
1300 | * with MAP_SHARED to preserve backward compatibility. | |
1301 | */ | |
1302 | flags &= LEGACY_MAP_MASK; | |
e4a9bc58 | 1303 | fallthrough; |
1c972597 DW |
1304 | case MAP_SHARED_VALIDATE: |
1305 | if (flags & ~flags_mask) | |
1306 | return -EOPNOTSUPP; | |
dc617f29 DW |
1307 | if (prot & PROT_WRITE) { |
1308 | if (!(file->f_mode & FMODE_WRITE)) | |
1309 | return -EACCES; | |
1310 | if (IS_SWAPFILE(file->f_mapping->host)) | |
1311 | return -ETXTBSY; | |
1312 | } | |
1da177e4 LT |
1313 | |
1314 | /* | |
1315 | * Make sure we don't allow writing to an append-only | |
1316 | * file.. | |
1317 | */ | |
1318 | if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) | |
1319 | return -EACCES; | |
1320 | ||
1da177e4 LT |
1321 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
1322 | if (!(file->f_mode & FMODE_WRITE)) | |
1323 | vm_flags &= ~(VM_MAYWRITE | VM_SHARED); | |
e4a9bc58 | 1324 | fallthrough; |
1da177e4 LT |
1325 | case MAP_PRIVATE: |
1326 | if (!(file->f_mode & FMODE_READ)) | |
1327 | return -EACCES; | |
90f8572b | 1328 | if (path_noexec(&file->f_path)) { |
80c5606c LT |
1329 | if (vm_flags & VM_EXEC) |
1330 | return -EPERM; | |
1331 | vm_flags &= ~VM_MAYEXEC; | |
1332 | } | |
80c5606c | 1333 | |
72c2d531 | 1334 | if (!file->f_op->mmap) |
80c5606c | 1335 | return -ENODEV; |
b2c56e4f ON |
1336 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
1337 | return -EINVAL; | |
1da177e4 LT |
1338 | break; |
1339 | ||
1340 | default: | |
1341 | return -EINVAL; | |
1342 | } | |
1343 | } else { | |
1344 | switch (flags & MAP_TYPE) { | |
1345 | case MAP_SHARED: | |
b2c56e4f ON |
1346 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
1347 | return -EINVAL; | |
ce363942 TH |
1348 | /* |
1349 | * Ignore pgoff. | |
1350 | */ | |
1351 | pgoff = 0; | |
1da177e4 LT |
1352 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
1353 | break; | |
1354 | case MAP_PRIVATE: | |
1355 | /* | |
1356 | * Set pgoff according to addr for anon_vma. | |
1357 | */ | |
1358 | pgoff = addr >> PAGE_SHIFT; | |
1359 | break; | |
1360 | default: | |
1361 | return -EINVAL; | |
1362 | } | |
1363 | } | |
1364 | ||
c22c0d63 ML |
1365 | /* |
1366 | * Set 'VM_NORESERVE' if we should not account for the | |
1367 | * memory use of this mapping. | |
1368 | */ | |
1369 | if (flags & MAP_NORESERVE) { | |
1370 | /* We honor MAP_NORESERVE if allowed to overcommit */ | |
1371 | if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) | |
1372 | vm_flags |= VM_NORESERVE; | |
1373 | ||
1374 | /* hugetlb applies strict overcommit unless MAP_NORESERVE */ | |
1375 | if (file && is_file_hugepages(file)) | |
1376 | vm_flags |= VM_NORESERVE; | |
1377 | } | |
1378 | ||
897ab3e0 | 1379 | addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); |
09a9f1d2 ML |
1380 | if (!IS_ERR_VALUE(addr) && |
1381 | ((vm_flags & VM_LOCKED) || | |
1382 | (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) | |
41badc15 | 1383 | *populate = len; |
bebeb3d6 | 1384 | return addr; |
0165ab44 | 1385 | } |
6be5ceb0 | 1386 | |
a90f590a DB |
1387 | unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, |
1388 | unsigned long prot, unsigned long flags, | |
1389 | unsigned long fd, unsigned long pgoff) | |
66f0dc48 HD |
1390 | { |
1391 | struct file *file = NULL; | |
1e3ee14b | 1392 | unsigned long retval; |
66f0dc48 HD |
1393 | |
1394 | if (!(flags & MAP_ANONYMOUS)) { | |
120a795d | 1395 | audit_mmap_fd(fd, flags); |
66f0dc48 HD |
1396 | file = fget(fd); |
1397 | if (!file) | |
1e3ee14b | 1398 | return -EBADF; |
7bba8f0e | 1399 | if (is_file_hugepages(file)) { |
af73e4d9 | 1400 | len = ALIGN(len, huge_page_size(hstate_file(file))); |
7bba8f0e ZL |
1401 | } else if (unlikely(flags & MAP_HUGETLB)) { |
1402 | retval = -EINVAL; | |
493af578 | 1403 | goto out_fput; |
7bba8f0e | 1404 | } |
66f0dc48 | 1405 | } else if (flags & MAP_HUGETLB) { |
c103a4dc | 1406 | struct hstate *hs; |
af73e4d9 | 1407 | |
20ac2893 | 1408 | hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); |
091d0d55 LZ |
1409 | if (!hs) |
1410 | return -EINVAL; | |
1411 | ||
1412 | len = ALIGN(len, huge_page_size(hs)); | |
66f0dc48 HD |
1413 | /* |
1414 | * VM_NORESERVE is used because the reservations will be | |
1415 | * taken when vm_ops->mmap() is called | |
66f0dc48 | 1416 | */ |
af73e4d9 | 1417 | file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, |
42d7395f | 1418 | VM_NORESERVE, |
83c1fd76 | 1419 | HUGETLB_ANONHUGE_INODE, |
42d7395f | 1420 | (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); |
66f0dc48 HD |
1421 | if (IS_ERR(file)) |
1422 | return PTR_ERR(file); | |
1423 | } | |
1424 | ||
9fbeb5ab | 1425 | retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
493af578 | 1426 | out_fput: |
66f0dc48 HD |
1427 | if (file) |
1428 | fput(file); | |
66f0dc48 HD |
1429 | return retval; |
1430 | } | |
1431 | ||
a90f590a DB |
1432 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
1433 | unsigned long, prot, unsigned long, flags, | |
1434 | unsigned long, fd, unsigned long, pgoff) | |
1435 | { | |
1436 | return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); | |
1437 | } | |
1438 | ||
a4679373 CH |
1439 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
1440 | struct mmap_arg_struct { | |
1441 | unsigned long addr; | |
1442 | unsigned long len; | |
1443 | unsigned long prot; | |
1444 | unsigned long flags; | |
1445 | unsigned long fd; | |
1446 | unsigned long offset; | |
1447 | }; | |
1448 | ||
1449 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) | |
1450 | { | |
1451 | struct mmap_arg_struct a; | |
1452 | ||
1453 | if (copy_from_user(&a, arg, sizeof(a))) | |
1454 | return -EFAULT; | |
de1741a1 | 1455 | if (offset_in_page(a.offset)) |
a4679373 CH |
1456 | return -EINVAL; |
1457 | ||
a90f590a DB |
1458 | return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, |
1459 | a.offset >> PAGE_SHIFT); | |
a4679373 CH |
1460 | } |
1461 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ | |
1462 | ||
4e950f6f | 1463 | /* |
8bb4e7a2 | 1464 | * Some shared mappings will want the pages marked read-only |
4e950f6f AD |
1465 | * to track write events. If so, we'll downgrade vm_page_prot |
1466 | * to the private version (using protection_map[] without the | |
1467 | * VM_SHARED bit). | |
1468 | */ | |
6d2329f8 | 1469 | int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) |
4e950f6f | 1470 | { |
ca16d140 | 1471 | vm_flags_t vm_flags = vma->vm_flags; |
8a04446a | 1472 | const struct vm_operations_struct *vm_ops = vma->vm_ops; |
4e950f6f AD |
1473 | |
1474 | /* If it was private or non-writable, the write bit is already clear */ | |
1475 | if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) | |
1476 | return 0; | |
1477 | ||
1478 | /* The backer wishes to know when pages are first written to? */ | |
8a04446a | 1479 | if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) |
4e950f6f AD |
1480 | return 1; |
1481 | ||
64e45507 PF |
1482 | /* The open routine did something to the protections that pgprot_modify |
1483 | * won't preserve? */ | |
6d2329f8 AA |
1484 | if (pgprot_val(vm_page_prot) != |
1485 | pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) | |
4e950f6f AD |
1486 | return 0; |
1487 | ||
f96f7a40 DH |
1488 | /* |
1489 | * Do we need to track softdirty? hugetlb does not support softdirty | |
1490 | * tracking yet. | |
1491 | */ | |
1492 | if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) | |
64e45507 PF |
1493 | return 1; |
1494 | ||
51d3d5eb DH |
1495 | /* Do we need write faults for uffd-wp tracking? */ |
1496 | if (userfaultfd_wp(vma)) | |
1497 | return 1; | |
1498 | ||
4e950f6f | 1499 | /* Specialty mapping? */ |
4b6e1e37 | 1500 | if (vm_flags & VM_PFNMAP) |
4e950f6f AD |
1501 | return 0; |
1502 | ||
1503 | /* Can the mapping track the dirty pages? */ | |
1504 | return vma->vm_file && vma->vm_file->f_mapping && | |
f56753ac | 1505 | mapping_can_writeback(vma->vm_file->f_mapping); |
4e950f6f AD |
1506 | } |
1507 | ||
fc8744ad LT |
1508 | /* |
1509 | * We account for memory if it's a private writeable mapping, | |
5a6fe125 | 1510 | * not hugepages and VM_NORESERVE wasn't set. |
fc8744ad | 1511 | */ |
ca16d140 | 1512 | static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) |
fc8744ad | 1513 | { |
5a6fe125 MG |
1514 | /* |
1515 | * hugetlb has its own accounting separate from the core VM | |
1516 | * VM_HUGETLB may not be set yet so we cannot check for that flag. | |
1517 | */ | |
1518 | if (file && is_file_hugepages(file)) | |
1519 | return 0; | |
1520 | ||
fc8744ad LT |
1521 | return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; |
1522 | } | |
1523 | ||
3499a131 LH |
1524 | /** |
1525 | * unmapped_area() - Find an area between the low_limit and the high_limit with | |
1526 | * the correct alignment and offset, all from @info. Note: current->mm is used | |
1527 | * for the search. | |
1528 | * | |
82b24936 VY |
1529 | * @info: The unmapped area information including the range [low_limit - |
1530 | * high_limit), the alignment offset and mask. | |
3499a131 LH |
1531 | * |
1532 | * Return: A memory address or -ENOMEM. | |
1533 | */ | |
baceaf1c | 1534 | static unsigned long unmapped_area(struct vm_unmapped_area_info *info) |
db4fbfb9 | 1535 | { |
3499a131 | 1536 | unsigned long length, gap; |
db4fbfb9 | 1537 | |
3499a131 | 1538 | MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); |
db4fbfb9 ML |
1539 | |
1540 | /* Adjust search length to account for worst case alignment overhead */ | |
1541 | length = info->length + info->align_mask; | |
1542 | if (length < info->length) | |
1543 | return -ENOMEM; | |
1544 | ||
3499a131 LH |
1545 | if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, |
1546 | length)) | |
db4fbfb9 ML |
1547 | return -ENOMEM; |
1548 | ||
3499a131 LH |
1549 | gap = mas.index; |
1550 | gap += (info->align_offset - gap) & info->align_mask; | |
1551 | return gap; | |
db4fbfb9 ML |
1552 | } |
1553 | ||
3499a131 LH |
1554 | /** |
1555 | * unmapped_area_topdown() - Find an area between the low_limit and the | |
82b24936 | 1556 | * high_limit with the correct alignment and offset at the highest available |
3499a131 LH |
1557 | * address, all from @info. Note: current->mm is used for the search. |
1558 | * | |
82b24936 VY |
1559 | * @info: The unmapped area information including the range [low_limit - |
1560 | * high_limit), the alignment offset and mask. | |
3499a131 LH |
1561 | * |
1562 | * Return: A memory address or -ENOMEM. | |
1563 | */ | |
baceaf1c | 1564 | static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) |
db4fbfb9 | 1565 | { |
3499a131 | 1566 | unsigned long length, gap; |
db4fbfb9 | 1567 | |
3499a131 | 1568 | MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); |
db4fbfb9 ML |
1569 | /* Adjust search length to account for worst case alignment overhead */ |
1570 | length = info->length + info->align_mask; | |
1571 | if (length < info->length) | |
1572 | return -ENOMEM; | |
1573 | ||
3499a131 LH |
1574 | if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, |
1575 | length)) | |
db4fbfb9 | 1576 | return -ENOMEM; |
db4fbfb9 | 1577 | |
3499a131 LH |
1578 | gap = mas.last + 1 - info->length; |
1579 | gap -= (gap - info->align_offset) & info->align_mask; | |
1580 | return gap; | |
db4fbfb9 ML |
1581 | } |
1582 | ||
baceaf1c JK |
1583 | /* |
1584 | * Search for an unmapped address range. | |
1585 | * | |
1586 | * We are looking for a range that: | |
1587 | * - does not intersect with any VMA; | |
1588 | * - is contained within the [low_limit, high_limit) interval; | |
1589 | * - is at least the desired size. | |
1590 | * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) | |
1591 | */ | |
1592 | unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) | |
1593 | { | |
df529cab JK |
1594 | unsigned long addr; |
1595 | ||
baceaf1c | 1596 | if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) |
df529cab | 1597 | addr = unmapped_area_topdown(info); |
baceaf1c | 1598 | else |
df529cab JK |
1599 | addr = unmapped_area(info); |
1600 | ||
1601 | trace_vm_unmapped_area(addr, info); | |
1602 | return addr; | |
baceaf1c | 1603 | } |
f6795053 | 1604 | |
1da177e4 LT |
1605 | /* Get an address range which is currently unmapped. |
1606 | * For shmat() with addr=0. | |
1607 | * | |
1608 | * Ugly calling convention alert: | |
1609 | * Return value with the low bits set means error value, | |
1610 | * ie | |
1611 | * if (ret & ~PAGE_MASK) | |
1612 | * error = ret; | |
1613 | * | |
1614 | * This function "knows" that -ENOMEM has the bits set. | |
1615 | */ | |
1da177e4 | 1616 | unsigned long |
4b439e25 CL |
1617 | generic_get_unmapped_area(struct file *filp, unsigned long addr, |
1618 | unsigned long len, unsigned long pgoff, | |
1619 | unsigned long flags) | |
1da177e4 LT |
1620 | { |
1621 | struct mm_struct *mm = current->mm; | |
1be7107f | 1622 | struct vm_area_struct *vma, *prev; |
db4fbfb9 | 1623 | struct vm_unmapped_area_info info; |
2cb4de08 | 1624 | const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); |
1da177e4 | 1625 | |
f6795053 | 1626 | if (len > mmap_end - mmap_min_addr) |
1da177e4 LT |
1627 | return -ENOMEM; |
1628 | ||
06abdfb4 BH |
1629 | if (flags & MAP_FIXED) |
1630 | return addr; | |
1631 | ||
1da177e4 LT |
1632 | if (addr) { |
1633 | addr = PAGE_ALIGN(addr); | |
1be7107f | 1634 | vma = find_vma_prev(mm, addr, &prev); |
f6795053 | 1635 | if (mmap_end - len >= addr && addr >= mmap_min_addr && |
1be7107f HD |
1636 | (!vma || addr + len <= vm_start_gap(vma)) && |
1637 | (!prev || addr >= vm_end_gap(prev))) | |
1da177e4 LT |
1638 | return addr; |
1639 | } | |
1da177e4 | 1640 | |
db4fbfb9 ML |
1641 | info.flags = 0; |
1642 | info.length = len; | |
4e99b021 | 1643 | info.low_limit = mm->mmap_base; |
f6795053 | 1644 | info.high_limit = mmap_end; |
db4fbfb9 | 1645 | info.align_mask = 0; |
09ef5283 | 1646 | info.align_offset = 0; |
db4fbfb9 | 1647 | return vm_unmapped_area(&info); |
1da177e4 | 1648 | } |
4b439e25 CL |
1649 | |
1650 | #ifndef HAVE_ARCH_UNMAPPED_AREA | |
1651 | unsigned long | |
1652 | arch_get_unmapped_area(struct file *filp, unsigned long addr, | |
1653 | unsigned long len, unsigned long pgoff, | |
1654 | unsigned long flags) | |
1655 | { | |
1656 | return generic_get_unmapped_area(filp, addr, len, pgoff, flags); | |
1657 | } | |
cc71aba3 | 1658 | #endif |
1da177e4 | 1659 | |
1da177e4 LT |
1660 | /* |
1661 | * This mmap-allocator allocates new areas top-down from below the | |
1662 | * stack's low limit (the base): | |
1663 | */ | |
1da177e4 | 1664 | unsigned long |
4b439e25 CL |
1665 | generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, |
1666 | unsigned long len, unsigned long pgoff, | |
1667 | unsigned long flags) | |
1da177e4 | 1668 | { |
1be7107f | 1669 | struct vm_area_struct *vma, *prev; |
1da177e4 | 1670 | struct mm_struct *mm = current->mm; |
db4fbfb9 | 1671 | struct vm_unmapped_area_info info; |
2cb4de08 | 1672 | const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); |
1da177e4 LT |
1673 | |
1674 | /* requested length too big for entire address space */ | |
f6795053 | 1675 | if (len > mmap_end - mmap_min_addr) |
1da177e4 LT |
1676 | return -ENOMEM; |
1677 | ||
06abdfb4 BH |
1678 | if (flags & MAP_FIXED) |
1679 | return addr; | |
1680 | ||
1da177e4 LT |
1681 | /* requesting a specific address */ |
1682 | if (addr) { | |
1683 | addr = PAGE_ALIGN(addr); | |
1be7107f | 1684 | vma = find_vma_prev(mm, addr, &prev); |
f6795053 | 1685 | if (mmap_end - len >= addr && addr >= mmap_min_addr && |
1be7107f HD |
1686 | (!vma || addr + len <= vm_start_gap(vma)) && |
1687 | (!prev || addr >= vm_end_gap(prev))) | |
1da177e4 LT |
1688 | return addr; |
1689 | } | |
1690 | ||
db4fbfb9 ML |
1691 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
1692 | info.length = len; | |
2afc745f | 1693 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
f6795053 | 1694 | info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); |
db4fbfb9 | 1695 | info.align_mask = 0; |
09ef5283 | 1696 | info.align_offset = 0; |
db4fbfb9 | 1697 | addr = vm_unmapped_area(&info); |
b716ad95 | 1698 | |
1da177e4 LT |
1699 | /* |
1700 | * A failed mmap() very likely causes application failure, | |
1701 | * so fall back to the bottom-up function here. This scenario | |
1702 | * can happen with large stack limits and large mmap() | |
1703 | * allocations. | |
1704 | */ | |
de1741a1 | 1705 | if (offset_in_page(addr)) { |
db4fbfb9 ML |
1706 | VM_BUG_ON(addr != -ENOMEM); |
1707 | info.flags = 0; | |
1708 | info.low_limit = TASK_UNMAPPED_BASE; | |
f6795053 | 1709 | info.high_limit = mmap_end; |
db4fbfb9 ML |
1710 | addr = vm_unmapped_area(&info); |
1711 | } | |
1da177e4 LT |
1712 | |
1713 | return addr; | |
1714 | } | |
4b439e25 CL |
1715 | |
1716 | #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN | |
1717 | unsigned long | |
1718 | arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, | |
1719 | unsigned long len, unsigned long pgoff, | |
1720 | unsigned long flags) | |
1721 | { | |
1722 | return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); | |
1723 | } | |
1da177e4 LT |
1724 | #endif |
1725 | ||
1da177e4 LT |
1726 | unsigned long |
1727 | get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, | |
1728 | unsigned long pgoff, unsigned long flags) | |
1729 | { | |
06abdfb4 BH |
1730 | unsigned long (*get_area)(struct file *, unsigned long, |
1731 | unsigned long, unsigned long, unsigned long); | |
1732 | ||
9206de95 AV |
1733 | unsigned long error = arch_mmap_check(addr, len, flags); |
1734 | if (error) | |
1735 | return error; | |
1736 | ||
1737 | /* Careful about overflows.. */ | |
1738 | if (len > TASK_SIZE) | |
1739 | return -ENOMEM; | |
1740 | ||
06abdfb4 | 1741 | get_area = current->mm->get_unmapped_area; |
c01d5b30 HD |
1742 | if (file) { |
1743 | if (file->f_op->get_unmapped_area) | |
1744 | get_area = file->f_op->get_unmapped_area; | |
1745 | } else if (flags & MAP_SHARED) { | |
1746 | /* | |
1747 | * mmap_region() will call shmem_zero_setup() to create a file, | |
1748 | * so use shmem's get_unmapped_area in case it can be huge. | |
45e55300 | 1749 | * do_mmap() will clear pgoff, so match alignment. |
c01d5b30 HD |
1750 | */ |
1751 | pgoff = 0; | |
1752 | get_area = shmem_get_unmapped_area; | |
1753 | } | |
1754 | ||
06abdfb4 BH |
1755 | addr = get_area(file, addr, len, pgoff, flags); |
1756 | if (IS_ERR_VALUE(addr)) | |
1757 | return addr; | |
1da177e4 | 1758 | |
07ab67c8 LT |
1759 | if (addr > TASK_SIZE - len) |
1760 | return -ENOMEM; | |
de1741a1 | 1761 | if (offset_in_page(addr)) |
07ab67c8 | 1762 | return -EINVAL; |
06abdfb4 | 1763 | |
9ac4ed4b AV |
1764 | error = security_mmap_addr(addr); |
1765 | return error ? error : addr; | |
1da177e4 LT |
1766 | } |
1767 | ||
1768 | EXPORT_SYMBOL(get_unmapped_area); | |
1769 | ||
abdba2dd LH |
1770 | /** |
1771 | * find_vma_intersection() - Look up the first VMA which intersects the interval | |
1772 | * @mm: The process address space. | |
1773 | * @start_addr: The inclusive start user address. | |
1774 | * @end_addr: The exclusive end user address. | |
1775 | * | |
1776 | * Returns: The first VMA within the provided range, %NULL otherwise. Assumes | |
1777 | * start_addr < end_addr. | |
1778 | */ | |
1779 | struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, | |
1780 | unsigned long start_addr, | |
1781 | unsigned long end_addr) | |
1782 | { | |
abdba2dd LH |
1783 | unsigned long index = start_addr; |
1784 | ||
1785 | mmap_assert_locked(mm); | |
7964cf8c | 1786 | return mt_find(&mm->mm_mt, &index, end_addr - 1); |
abdba2dd LH |
1787 | } |
1788 | EXPORT_SYMBOL(find_vma_intersection); | |
1789 | ||
be8432e7 LH |
1790 | /** |
1791 | * find_vma() - Find the VMA for a given address, or the next VMA. | |
1792 | * @mm: The mm_struct to check | |
1793 | * @addr: The address | |
1794 | * | |
1795 | * Returns: The VMA associated with addr, or the next VMA. | |
1796 | * May return %NULL in the case of no VMA at addr or above. | |
1797 | */ | |
48aae425 | 1798 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) |
1da177e4 | 1799 | { |
be8432e7 | 1800 | unsigned long index = addr; |
1da177e4 | 1801 | |
5b78ed24 | 1802 | mmap_assert_locked(mm); |
7964cf8c | 1803 | return mt_find(&mm->mm_mt, &index, ULONG_MAX); |
1da177e4 | 1804 | } |
1da177e4 LT |
1805 | EXPORT_SYMBOL(find_vma); |
1806 | ||
7fdbd37d LH |
1807 | /** |
1808 | * find_vma_prev() - Find the VMA for a given address, or the next vma and | |
1809 | * set %pprev to the previous VMA, if any. | |
1810 | * @mm: The mm_struct to check | |
1811 | * @addr: The address | |
1812 | * @pprev: The pointer to set to the previous VMA | |
1813 | * | |
1814 | * Note that RCU lock is missing here since the external mmap_lock() is used | |
1815 | * instead. | |
1816 | * | |
1817 | * Returns: The VMA associated with @addr, or the next vma. | |
1818 | * May return %NULL in the case of no vma at addr or above. | |
6bd4837d | 1819 | */ |
1da177e4 LT |
1820 | struct vm_area_struct * |
1821 | find_vma_prev(struct mm_struct *mm, unsigned long addr, | |
1822 | struct vm_area_struct **pprev) | |
1823 | { | |
6bd4837d | 1824 | struct vm_area_struct *vma; |
7fdbd37d | 1825 | MA_STATE(mas, &mm->mm_mt, addr, addr); |
1da177e4 | 1826 | |
7fdbd37d LH |
1827 | vma = mas_walk(&mas); |
1828 | *pprev = mas_prev(&mas, 0); | |
1829 | if (!vma) | |
1830 | vma = mas_next(&mas, ULONG_MAX); | |
6bd4837d | 1831 | return vma; |
1da177e4 LT |
1832 | } |
1833 | ||
1834 | /* | |
1835 | * Verify that the stack growth is acceptable and | |
1836 | * update accounting. This is shared with both the | |
1837 | * grow-up and grow-down cases. | |
1838 | */ | |
1be7107f HD |
1839 | static int acct_stack_growth(struct vm_area_struct *vma, |
1840 | unsigned long size, unsigned long grow) | |
1da177e4 LT |
1841 | { |
1842 | struct mm_struct *mm = vma->vm_mm; | |
1be7107f | 1843 | unsigned long new_start; |
1da177e4 LT |
1844 | |
1845 | /* address space limit tests */ | |
84638335 | 1846 | if (!may_expand_vm(mm, vma->vm_flags, grow)) |
1da177e4 LT |
1847 | return -ENOMEM; |
1848 | ||
1849 | /* Stack limit test */ | |
24c79d8e | 1850 | if (size > rlimit(RLIMIT_STACK)) |
1da177e4 LT |
1851 | return -ENOMEM; |
1852 | ||
1853 | /* mlock limit tests */ | |
c5d8a364 ML |
1854 | if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) |
1855 | return -ENOMEM; | |
1da177e4 | 1856 | |
0d59a01b AL |
1857 | /* Check to ensure the stack will not grow into a hugetlb-only region */ |
1858 | new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : | |
1859 | vma->vm_end - size; | |
1860 | if (is_hugepage_only_range(vma->vm_mm, new_start, size)) | |
1861 | return -EFAULT; | |
1862 | ||
1da177e4 LT |
1863 | /* |
1864 | * Overcommit.. This must be the final test, as it will | |
1865 | * update security statistics. | |
1866 | */ | |
05fa199d | 1867 | if (security_vm_enough_memory_mm(mm, grow)) |
1da177e4 LT |
1868 | return -ENOMEM; |
1869 | ||
1da177e4 LT |
1870 | return 0; |
1871 | } | |
1872 | ||
46dea3d0 | 1873 | #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) |
1da177e4 | 1874 | /* |
46dea3d0 HD |
1875 | * PA-RISC uses this for its stack; IA64 for its Register Backing Store. |
1876 | * vma is the last one with address > vma->vm_end. Have to extend vma. | |
1da177e4 | 1877 | */ |
46dea3d0 | 1878 | int expand_upwards(struct vm_area_struct *vma, unsigned long address) |
1da177e4 | 1879 | { |
09357814 | 1880 | struct mm_struct *mm = vma->vm_mm; |
1be7107f HD |
1881 | struct vm_area_struct *next; |
1882 | unsigned long gap_addr; | |
12352d3c | 1883 | int error = 0; |
d4af56c5 | 1884 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
1da177e4 LT |
1885 | |
1886 | if (!(vma->vm_flags & VM_GROWSUP)) | |
1887 | return -EFAULT; | |
1888 | ||
bd726c90 | 1889 | /* Guard against exceeding limits of the address space. */ |
1be7107f | 1890 | address &= PAGE_MASK; |
37511fb5 | 1891 | if (address >= (TASK_SIZE & PAGE_MASK)) |
12352d3c | 1892 | return -ENOMEM; |
bd726c90 | 1893 | address += PAGE_SIZE; |
12352d3c | 1894 | |
1be7107f HD |
1895 | /* Enforce stack_guard_gap */ |
1896 | gap_addr = address + stack_guard_gap; | |
bd726c90 HD |
1897 | |
1898 | /* Guard against overflow */ | |
1899 | if (gap_addr < address || gap_addr > TASK_SIZE) | |
1900 | gap_addr = TASK_SIZE; | |
1901 | ||
763ecb03 LH |
1902 | next = find_vma_intersection(mm, vma->vm_end, gap_addr); |
1903 | if (next && vma_is_accessible(next)) { | |
1be7107f HD |
1904 | if (!(next->vm_flags & VM_GROWSUP)) |
1905 | return -ENOMEM; | |
1906 | /* Check that both stack segments have the same anon_vma? */ | |
1907 | } | |
1908 | ||
c5d5546e | 1909 | if (mas_preallocate(&mas, GFP_KERNEL)) |
d4af56c5 LH |
1910 | return -ENOMEM; |
1911 | ||
12352d3c | 1912 | /* We must make sure the anon_vma is allocated. */ |
d4af56c5 LH |
1913 | if (unlikely(anon_vma_prepare(vma))) { |
1914 | mas_destroy(&mas); | |
1da177e4 | 1915 | return -ENOMEM; |
d4af56c5 | 1916 | } |
1da177e4 LT |
1917 | |
1918 | /* | |
1919 | * vma->vm_start/vm_end cannot change under us because the caller | |
c1e8d7c6 | 1920 | * is required to hold the mmap_lock in read mode. We need the |
1da177e4 LT |
1921 | * anon_vma lock to serialize against concurrent expand_stacks. |
1922 | */ | |
12352d3c | 1923 | anon_vma_lock_write(vma->anon_vma); |
1da177e4 LT |
1924 | |
1925 | /* Somebody else might have raced and expanded it already */ | |
1926 | if (address > vma->vm_end) { | |
1927 | unsigned long size, grow; | |
1928 | ||
1929 | size = address - vma->vm_start; | |
1930 | grow = (address - vma->vm_end) >> PAGE_SHIFT; | |
1931 | ||
42c36f63 HD |
1932 | error = -ENOMEM; |
1933 | if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { | |
1934 | error = acct_stack_growth(vma, size, grow); | |
1935 | if (!error) { | |
4128997b | 1936 | /* |
524e00b3 LH |
1937 | * We only hold a shared mmap_lock lock here, so |
1938 | * we need to protect against concurrent vma | |
1939 | * expansions. anon_vma_lock_write() doesn't | |
1940 | * help here, as we don't guarantee that all | |
1941 | * growable vmas in a mm share the same root | |
1942 | * anon vma. So, we reuse mm->page_table_lock | |
1943 | * to guard against concurrent vma expansions. | |
4128997b | 1944 | */ |
09357814 | 1945 | spin_lock(&mm->page_table_lock); |
87e8827b | 1946 | if (vma->vm_flags & VM_LOCKED) |
09357814 | 1947 | mm->locked_vm += grow; |
84638335 | 1948 | vm_stat_account(mm, vma->vm_flags, grow); |
bf181b9f | 1949 | anon_vma_interval_tree_pre_update_vma(vma); |
42c36f63 | 1950 | vma->vm_end = address; |
d4af56c5 | 1951 | /* Overwrite old entry in mtree. */ |
fbcc3104 LH |
1952 | mas_set_range(&mas, vma->vm_start, address - 1); |
1953 | mas_store_prealloc(&mas, vma); | |
bf181b9f | 1954 | anon_vma_interval_tree_post_update_vma(vma); |
09357814 | 1955 | spin_unlock(&mm->page_table_lock); |
4128997b | 1956 | |
42c36f63 HD |
1957 | perf_event_mmap(vma); |
1958 | } | |
3af9e859 | 1959 | } |
1da177e4 | 1960 | } |
12352d3c | 1961 | anon_vma_unlock_write(vma->anon_vma); |
c791576c | 1962 | khugepaged_enter_vma(vma, vma->vm_flags); |
d4af56c5 | 1963 | mas_destroy(&mas); |
1da177e4 LT |
1964 | return error; |
1965 | } | |
46dea3d0 HD |
1966 | #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ |
1967 | ||
1da177e4 LT |
1968 | /* |
1969 | * vma is the first one with address < vma->vm_start. Have to extend vma. | |
1970 | */ | |
524e00b3 | 1971 | int expand_downwards(struct vm_area_struct *vma, unsigned long address) |
1da177e4 | 1972 | { |
09357814 | 1973 | struct mm_struct *mm = vma->vm_mm; |
763ecb03 | 1974 | MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); |
1be7107f | 1975 | struct vm_area_struct *prev; |
0a1d5299 | 1976 | int error = 0; |
1da177e4 | 1977 | |
8869477a | 1978 | address &= PAGE_MASK; |
0a1d5299 JH |
1979 | if (address < mmap_min_addr) |
1980 | return -EPERM; | |
8869477a | 1981 | |
1be7107f | 1982 | /* Enforce stack_guard_gap */ |
763ecb03 | 1983 | prev = mas_prev(&mas, 0); |
32e4e6d5 ON |
1984 | /* Check that both stack segments have the same anon_vma? */ |
1985 | if (prev && !(prev->vm_flags & VM_GROWSDOWN) && | |
3122e80e | 1986 | vma_is_accessible(prev)) { |
32e4e6d5 | 1987 | if (address - prev->vm_end < stack_guard_gap) |
1be7107f | 1988 | return -ENOMEM; |
1be7107f HD |
1989 | } |
1990 | ||
c5d5546e | 1991 | if (mas_preallocate(&mas, GFP_KERNEL)) |
d4af56c5 LH |
1992 | return -ENOMEM; |
1993 | ||
12352d3c | 1994 | /* We must make sure the anon_vma is allocated. */ |
d4af56c5 LH |
1995 | if (unlikely(anon_vma_prepare(vma))) { |
1996 | mas_destroy(&mas); | |
12352d3c | 1997 | return -ENOMEM; |
d4af56c5 | 1998 | } |
1da177e4 LT |
1999 | |
2000 | /* | |
2001 | * vma->vm_start/vm_end cannot change under us because the caller | |
c1e8d7c6 | 2002 | * is required to hold the mmap_lock in read mode. We need the |
1da177e4 LT |
2003 | * anon_vma lock to serialize against concurrent expand_stacks. |
2004 | */ | |
12352d3c | 2005 | anon_vma_lock_write(vma->anon_vma); |
1da177e4 LT |
2006 | |
2007 | /* Somebody else might have raced and expanded it already */ | |
2008 | if (address < vma->vm_start) { | |
2009 | unsigned long size, grow; | |
2010 | ||
2011 | size = vma->vm_end - address; | |
2012 | grow = (vma->vm_start - address) >> PAGE_SHIFT; | |
2013 | ||
a626ca6a LT |
2014 | error = -ENOMEM; |
2015 | if (grow <= vma->vm_pgoff) { | |
2016 | error = acct_stack_growth(vma, size, grow); | |
2017 | if (!error) { | |
4128997b | 2018 | /* |
524e00b3 LH |
2019 | * We only hold a shared mmap_lock lock here, so |
2020 | * we need to protect against concurrent vma | |
2021 | * expansions. anon_vma_lock_write() doesn't | |
2022 | * help here, as we don't guarantee that all | |
2023 | * growable vmas in a mm share the same root | |
2024 | * anon vma. So, we reuse mm->page_table_lock | |
2025 | * to guard against concurrent vma expansions. | |
4128997b | 2026 | */ |
09357814 | 2027 | spin_lock(&mm->page_table_lock); |
87e8827b | 2028 | if (vma->vm_flags & VM_LOCKED) |
09357814 | 2029 | mm->locked_vm += grow; |
84638335 | 2030 | vm_stat_account(mm, vma->vm_flags, grow); |
bf181b9f | 2031 | anon_vma_interval_tree_pre_update_vma(vma); |
a626ca6a LT |
2032 | vma->vm_start = address; |
2033 | vma->vm_pgoff -= grow; | |
d4af56c5 | 2034 | /* Overwrite old entry in mtree. */ |
fbcc3104 LH |
2035 | mas_set_range(&mas, address, vma->vm_end - 1); |
2036 | mas_store_prealloc(&mas, vma); | |
bf181b9f | 2037 | anon_vma_interval_tree_post_update_vma(vma); |
09357814 | 2038 | spin_unlock(&mm->page_table_lock); |
4128997b | 2039 | |
a626ca6a LT |
2040 | perf_event_mmap(vma); |
2041 | } | |
1da177e4 LT |
2042 | } |
2043 | } | |
12352d3c | 2044 | anon_vma_unlock_write(vma->anon_vma); |
c791576c | 2045 | khugepaged_enter_vma(vma, vma->vm_flags); |
d4af56c5 | 2046 | mas_destroy(&mas); |
1da177e4 LT |
2047 | return error; |
2048 | } | |
2049 | ||
1be7107f HD |
2050 | /* enforced gap between the expanding stack and other mappings. */ |
2051 | unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; | |
2052 | ||
2053 | static int __init cmdline_parse_stack_guard_gap(char *p) | |
2054 | { | |
2055 | unsigned long val; | |
2056 | char *endptr; | |
2057 | ||
2058 | val = simple_strtoul(p, &endptr, 10); | |
2059 | if (!*endptr) | |
2060 | stack_guard_gap = val << PAGE_SHIFT; | |
2061 | ||
e6d09493 | 2062 | return 1; |
1be7107f HD |
2063 | } |
2064 | __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); | |
2065 | ||
b6a2fea3 OW |
2066 | #ifdef CONFIG_STACK_GROWSUP |
2067 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
2068 | { | |
2069 | return expand_upwards(vma, address); | |
2070 | } | |
2071 | ||
2072 | struct vm_area_struct * | |
2073 | find_extend_vma(struct mm_struct *mm, unsigned long addr) | |
2074 | { | |
2075 | struct vm_area_struct *vma, *prev; | |
2076 | ||
2077 | addr &= PAGE_MASK; | |
2078 | vma = find_vma_prev(mm, addr, &prev); | |
2079 | if (vma && (vma->vm_start <= addr)) | |
2080 | return vma; | |
4d45e75a | 2081 | if (!prev || expand_stack(prev, addr)) |
b6a2fea3 | 2082 | return NULL; |
cea10a19 | 2083 | if (prev->vm_flags & VM_LOCKED) |
fc05f566 | 2084 | populate_vma_page_range(prev, addr, prev->vm_end, NULL); |
b6a2fea3 OW |
2085 | return prev; |
2086 | } | |
2087 | #else | |
2088 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
2089 | { | |
2090 | return expand_downwards(vma, address); | |
2091 | } | |
2092 | ||
1da177e4 | 2093 | struct vm_area_struct * |
cc71aba3 | 2094 | find_extend_vma(struct mm_struct *mm, unsigned long addr) |
1da177e4 | 2095 | { |
cc71aba3 | 2096 | struct vm_area_struct *vma; |
1da177e4 LT |
2097 | unsigned long start; |
2098 | ||
2099 | addr &= PAGE_MASK; | |
cc71aba3 | 2100 | vma = find_vma(mm, addr); |
1da177e4 LT |
2101 | if (!vma) |
2102 | return NULL; | |
2103 | if (vma->vm_start <= addr) | |
2104 | return vma; | |
2105 | if (!(vma->vm_flags & VM_GROWSDOWN)) | |
2106 | return NULL; | |
2107 | start = vma->vm_start; | |
2108 | if (expand_stack(vma, addr)) | |
2109 | return NULL; | |
cea10a19 | 2110 | if (vma->vm_flags & VM_LOCKED) |
fc05f566 | 2111 | populate_vma_page_range(vma, addr, start, NULL); |
1da177e4 LT |
2112 | return vma; |
2113 | } | |
2114 | #endif | |
2115 | ||
e1d6d01a JB |
2116 | EXPORT_SYMBOL_GPL(find_extend_vma); |
2117 | ||
1da177e4 | 2118 | /* |
763ecb03 LH |
2119 | * Ok - we have the memory areas we should free on a maple tree so release them, |
2120 | * and do the vma updates. | |
2c0b3814 HD |
2121 | * |
2122 | * Called with the mm semaphore held. | |
1da177e4 | 2123 | */ |
763ecb03 | 2124 | static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) |
1da177e4 | 2125 | { |
4f74d2c8 | 2126 | unsigned long nr_accounted = 0; |
763ecb03 | 2127 | struct vm_area_struct *vma; |
4f74d2c8 | 2128 | |
365e9c87 HD |
2129 | /* Update high watermark before we lower total_vm */ |
2130 | update_hiwater_vm(mm); | |
763ecb03 | 2131 | mas_for_each(mas, vma, ULONG_MAX) { |
2c0b3814 HD |
2132 | long nrpages = vma_pages(vma); |
2133 | ||
4f74d2c8 LT |
2134 | if (vma->vm_flags & VM_ACCOUNT) |
2135 | nr_accounted += nrpages; | |
84638335 | 2136 | vm_stat_account(mm, vma->vm_flags, -nrpages); |
763ecb03 LH |
2137 | remove_vma(vma); |
2138 | } | |
4f74d2c8 | 2139 | vm_unacct_memory(nr_accounted); |
1da177e4 LT |
2140 | validate_mm(mm); |
2141 | } | |
2142 | ||
2143 | /* | |
2144 | * Get rid of page table information in the indicated region. | |
2145 | * | |
f10df686 | 2146 | * Called with the mm semaphore held. |
1da177e4 | 2147 | */ |
763ecb03 | 2148 | static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, |
e0da382c | 2149 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
763ecb03 | 2150 | struct vm_area_struct *next, |
e0da382c | 2151 | unsigned long start, unsigned long end) |
1da177e4 | 2152 | { |
d16dfc55 | 2153 | struct mmu_gather tlb; |
1da177e4 LT |
2154 | |
2155 | lru_add_drain(); | |
a72afd87 | 2156 | tlb_gather_mmu(&tlb, mm); |
365e9c87 | 2157 | update_hiwater_rss(mm); |
763ecb03 LH |
2158 | unmap_vmas(&tlb, mt, vma, start, end); |
2159 | free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, | |
6ee8630e | 2160 | next ? next->vm_start : USER_PGTABLES_CEILING); |
ae8eba8b | 2161 | tlb_finish_mmu(&tlb); |
1da177e4 LT |
2162 | } |
2163 | ||
1da177e4 | 2164 | /* |
def5efe0 DR |
2165 | * __split_vma() bypasses sysctl_max_map_count checking. We use this where it |
2166 | * has already been checked or doesn't make sense to fail. | |
1da177e4 | 2167 | */ |
9760ebff | 2168 | int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, |
def5efe0 | 2169 | unsigned long addr, int new_below) |
1da177e4 | 2170 | { |
1da177e4 | 2171 | struct vm_area_struct *new; |
e3975891 | 2172 | int err; |
9760ebff LH |
2173 | unsigned long end = vma->vm_end; |
2174 | ||
2175 | validate_mm_mt(vma->vm_mm); | |
1da177e4 | 2176 | |
dd3b614f DS |
2177 | if (vma->vm_ops && vma->vm_ops->may_split) { |
2178 | err = vma->vm_ops->may_split(vma, addr); | |
31383c68 DW |
2179 | if (err) |
2180 | return err; | |
2181 | } | |
1da177e4 | 2182 | |
3928d4f5 | 2183 | new = vm_area_dup(vma); |
1da177e4 | 2184 | if (!new) |
e3975891 | 2185 | return -ENOMEM; |
1da177e4 | 2186 | |
1da177e4 LT |
2187 | if (new_below) |
2188 | new->vm_end = addr; | |
2189 | else { | |
2190 | new->vm_start = addr; | |
2191 | new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); | |
2192 | } | |
2193 | ||
ef0855d3 ON |
2194 | err = vma_dup_policy(vma, new); |
2195 | if (err) | |
5beb4930 | 2196 | goto out_free_vma; |
1da177e4 | 2197 | |
c4ea95d7 DF |
2198 | err = anon_vma_clone(new, vma); |
2199 | if (err) | |
5beb4930 RR |
2200 | goto out_free_mpol; |
2201 | ||
e9714acf | 2202 | if (new->vm_file) |
1da177e4 LT |
2203 | get_file(new->vm_file); |
2204 | ||
2205 | if (new->vm_ops && new->vm_ops->open) | |
2206 | new->vm_ops->open(new); | |
2207 | ||
2208 | if (new_below) | |
5beb4930 | 2209 | err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + |
1da177e4 LT |
2210 | ((addr - new->vm_start) >> PAGE_SHIFT), new); |
2211 | else | |
5beb4930 | 2212 | err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); |
1da177e4 | 2213 | |
5beb4930 | 2214 | /* Success. */ |
9760ebff LH |
2215 | if (!err) { |
2216 | vma_iter_set(vmi, end); | |
5beb4930 | 2217 | return 0; |
9760ebff | 2218 | } |
5beb4930 | 2219 | |
d4af56c5 LH |
2220 | /* Avoid vm accounting in close() operation */ |
2221 | new->vm_start = new->vm_end; | |
2222 | new->vm_pgoff = 0; | |
5beb4930 | 2223 | /* Clean everything up if vma_adjust failed. */ |
58927533 RR |
2224 | if (new->vm_ops && new->vm_ops->close) |
2225 | new->vm_ops->close(new); | |
e9714acf | 2226 | if (new->vm_file) |
5beb4930 | 2227 | fput(new->vm_file); |
2aeadc30 | 2228 | unlink_anon_vmas(new); |
5beb4930 | 2229 | out_free_mpol: |
ef0855d3 | 2230 | mpol_put(vma_policy(new)); |
5beb4930 | 2231 | out_free_vma: |
3928d4f5 | 2232 | vm_area_free(new); |
9760ebff | 2233 | validate_mm_mt(vma->vm_mm); |
5beb4930 | 2234 | return err; |
1da177e4 LT |
2235 | } |
2236 | ||
659ace58 KM |
2237 | /* |
2238 | * Split a vma into two pieces at address 'addr', a new vma is allocated | |
2239 | * either for the first part or the tail. | |
2240 | */ | |
9760ebff | 2241 | int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, |
659ace58 KM |
2242 | unsigned long addr, int new_below) |
2243 | { | |
9760ebff | 2244 | if (vma->vm_mm->map_count >= sysctl_max_map_count) |
659ace58 KM |
2245 | return -ENOMEM; |
2246 | ||
9760ebff | 2247 | return __split_vma(vmi, vma, addr, new_below); |
f2ebfe43 LH |
2248 | } |
2249 | ||
763ecb03 LH |
2250 | static inline int munmap_sidetree(struct vm_area_struct *vma, |
2251 | struct ma_state *mas_detach) | |
1da177e4 | 2252 | { |
763ecb03 LH |
2253 | mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); |
2254 | if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) | |
2255 | return -ENOMEM; | |
1da177e4 | 2256 | |
763ecb03 LH |
2257 | if (vma->vm_flags & VM_LOCKED) |
2258 | vma->vm_mm->locked_vm -= vma_pages(vma); | |
1da177e4 | 2259 | |
763ecb03 | 2260 | return 0; |
11f9a21a | 2261 | } |
5a28fc94 | 2262 | |
11f9a21a | 2263 | /* |
183654ce LH |
2264 | * do_vmi_align_munmap() - munmap the aligned region from @start to @end. |
2265 | * @vmi: The vma iterator | |
11f9a21a LH |
2266 | * @vma: The starting vm_area_struct |
2267 | * @mm: The mm_struct | |
2268 | * @start: The aligned start address to munmap. | |
2269 | * @end: The aligned end address to munmap. | |
2270 | * @uf: The userfaultfd list_head | |
8651a137 | 2271 | * @downgrade: Set to true to attempt a write downgrade of the mmap_lock |
11f9a21a LH |
2272 | * |
2273 | * If @downgrade is true, check return code for potential release of the lock. | |
2274 | */ | |
2275 | static int | |
183654ce | 2276 | do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, |
11f9a21a LH |
2277 | struct mm_struct *mm, unsigned long start, |
2278 | unsigned long end, struct list_head *uf, bool downgrade) | |
2279 | { | |
763ecb03 LH |
2280 | struct vm_area_struct *prev, *next = NULL; |
2281 | struct maple_tree mt_detach; | |
2282 | int count = 0; | |
11f9a21a | 2283 | int error = -ENOMEM; |
763ecb03 LH |
2284 | MA_STATE(mas_detach, &mt_detach, 0, 0); |
2285 | mt_init_flags(&mt_detach, MT_FLAGS_LOCK_EXTERN); | |
2286 | mt_set_external_lock(&mt_detach, &mm->mmap_lock); | |
d4af56c5 | 2287 | |
1da177e4 LT |
2288 | /* |
2289 | * If we need to split any vma, do it now to save pain later. | |
2290 | * | |
2291 | * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially | |
2292 | * unmapped vm_area_struct will remain in use: so lower split_vma | |
2293 | * places tmp vma above, and higher split_vma places tmp vma below. | |
2294 | */ | |
763ecb03 LH |
2295 | |
2296 | /* Does it split the first one? */ | |
146425a3 | 2297 | if (start > vma->vm_start) { |
659ace58 KM |
2298 | |
2299 | /* | |
2300 | * Make sure that map_count on return from munmap() will | |
2301 | * not exceed its limit; but let map_count go just above | |
2302 | * its limit temporarily, to help free resources as expected. | |
2303 | */ | |
2304 | if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) | |
d4af56c5 | 2305 | goto map_count_exceeded; |
659ace58 | 2306 | |
9760ebff | 2307 | error = __split_vma(vmi, vma, start, 0); |
1da177e4 | 2308 | if (error) |
763ecb03 | 2309 | goto start_split_failed; |
11f9a21a | 2310 | |
183654ce LH |
2311 | vma_iter_set(vmi, start); |
2312 | vma = vma_find(vmi, end); | |
1da177e4 LT |
2313 | } |
2314 | ||
183654ce | 2315 | prev = vma_prev(vmi); |
763ecb03 | 2316 | if (unlikely((!prev))) |
183654ce | 2317 | vma_iter_set(vmi, start); |
763ecb03 LH |
2318 | |
2319 | /* | |
2320 | * Detach a range of VMAs from the mm. Using next as a temp variable as | |
2321 | * it is always overwritten. | |
2322 | */ | |
183654ce | 2323 | for_each_vma_range(*vmi, next, end) { |
763ecb03 LH |
2324 | /* Does it split the end? */ |
2325 | if (next->vm_end > end) { | |
2326 | struct vm_area_struct *split; | |
2327 | ||
9760ebff | 2328 | error = __split_vma(vmi, next, end, 1); |
763ecb03 LH |
2329 | if (error) |
2330 | goto end_split_failed; | |
11f9a21a | 2331 | |
183654ce LH |
2332 | vma_iter_set(vmi, end); |
2333 | split = vma_prev(vmi); | |
763ecb03 LH |
2334 | error = munmap_sidetree(split, &mas_detach); |
2335 | if (error) | |
2336 | goto munmap_sidetree_failed; | |
11f9a21a | 2337 | |
763ecb03 LH |
2338 | count++; |
2339 | if (vma == next) | |
2340 | vma = split; | |
2341 | break; | |
2342 | } | |
2343 | error = munmap_sidetree(next, &mas_detach); | |
1da177e4 | 2344 | if (error) |
763ecb03 | 2345 | goto munmap_sidetree_failed; |
11f9a21a | 2346 | |
763ecb03 LH |
2347 | count++; |
2348 | #ifdef CONFIG_DEBUG_VM_MAPLE_TREE | |
2349 | BUG_ON(next->vm_start < start); | |
2350 | BUG_ON(next->vm_start > end); | |
2351 | #endif | |
1da177e4 | 2352 | } |
1da177e4 | 2353 | |
763ecb03 | 2354 | if (!next) |
183654ce | 2355 | next = vma_next(vmi); |
763ecb03 | 2356 | |
2376dd7c AA |
2357 | if (unlikely(uf)) { |
2358 | /* | |
2359 | * If userfaultfd_unmap_prep returns an error the vmas | |
f0953a1b | 2360 | * will remain split, but userland will get a |
2376dd7c AA |
2361 | * highly unexpected error anyway. This is no |
2362 | * different than the case where the first of the two | |
2363 | * __split_vma fails, but we don't undo the first | |
2364 | * split, despite we could. This is unlikely enough | |
2365 | * failure that it's not worth optimizing it for. | |
2366 | */ | |
69dbe6da | 2367 | error = userfaultfd_unmap_prep(mm, start, end, uf); |
11f9a21a | 2368 | |
2376dd7c | 2369 | if (error) |
d4af56c5 | 2370 | goto userfaultfd_error; |
2376dd7c AA |
2371 | } |
2372 | ||
763ecb03 LH |
2373 | #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) |
2374 | /* Make sure no VMAs are about to be lost. */ | |
2375 | { | |
2376 | MA_STATE(test, &mt_detach, start, end - 1); | |
2377 | struct vm_area_struct *vma_mas, *vma_test; | |
2378 | int test_count = 0; | |
2379 | ||
183654ce | 2380 | vma_iter_set(vmi, start); |
763ecb03 LH |
2381 | rcu_read_lock(); |
2382 | vma_test = mas_find(&test, end - 1); | |
183654ce | 2383 | for_each_vma_range(*vmi, vma_mas, end) { |
763ecb03 LH |
2384 | BUG_ON(vma_mas != vma_test); |
2385 | test_count++; | |
2386 | vma_test = mas_next(&test, end - 1); | |
2387 | } | |
2388 | rcu_read_unlock(); | |
2389 | BUG_ON(count != test_count); | |
763ecb03 LH |
2390 | } |
2391 | #endif | |
0378c0a0 | 2392 | /* Point of no return */ |
183654ce LH |
2393 | vma_iter_set(vmi, start); |
2394 | if (vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL)) | |
0378c0a0 LH |
2395 | return -ENOMEM; |
2396 | ||
763ecb03 | 2397 | mm->map_count -= count; |
11f9a21a LH |
2398 | /* |
2399 | * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or | |
2400 | * VM_GROWSUP VMA. Such VMAs can change their size under | |
2401 | * down_read(mmap_lock) and collide with the VMA we are about to unmap. | |
2402 | */ | |
2403 | if (downgrade) { | |
763ecb03 | 2404 | if (next && (next->vm_flags & VM_GROWSDOWN)) |
11f9a21a LH |
2405 | downgrade = false; |
2406 | else if (prev && (prev->vm_flags & VM_GROWSUP)) | |
2407 | downgrade = false; | |
2408 | else | |
2409 | mmap_write_downgrade(mm); | |
2410 | } | |
dd2283f2 | 2411 | |
763ecb03 LH |
2412 | unmap_region(mm, &mt_detach, vma, prev, next, start, end); |
2413 | /* Statistics and freeing VMAs */ | |
2414 | mas_set(&mas_detach, start); | |
2415 | remove_mt(mm, &mas_detach); | |
2416 | __mt_destroy(&mt_detach); | |
1da177e4 | 2417 | |
524e00b3 LH |
2418 | |
2419 | validate_mm(mm); | |
dd2283f2 | 2420 | return downgrade ? 1 : 0; |
d4af56c5 | 2421 | |
d4af56c5 | 2422 | userfaultfd_error: |
763ecb03 LH |
2423 | munmap_sidetree_failed: |
2424 | end_split_failed: | |
2425 | __mt_destroy(&mt_detach); | |
2426 | start_split_failed: | |
2427 | map_count_exceeded: | |
d4af56c5 | 2428 | return error; |
1da177e4 | 2429 | } |
1da177e4 | 2430 | |
11f9a21a | 2431 | /* |
183654ce LH |
2432 | * do_vmi_munmap() - munmap a given range. |
2433 | * @vmi: The vma iterator | |
11f9a21a LH |
2434 | * @mm: The mm_struct |
2435 | * @start: The start address to munmap | |
2436 | * @len: The length of the range to munmap | |
2437 | * @uf: The userfaultfd list_head | |
2438 | * @downgrade: set to true if the user wants to attempt to write_downgrade the | |
8651a137 | 2439 | * mmap_lock |
11f9a21a LH |
2440 | * |
2441 | * This function takes a @mas that is either pointing to the previous VMA or set | |
2442 | * to MA_START and sets it up to remove the mapping(s). The @len will be | |
2443 | * aligned and any arch_unmap work will be preformed. | |
2444 | * | |
2445 | * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. | |
2446 | */ | |
183654ce | 2447 | int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, |
11f9a21a LH |
2448 | unsigned long start, size_t len, struct list_head *uf, |
2449 | bool downgrade) | |
2450 | { | |
2451 | unsigned long end; | |
2452 | struct vm_area_struct *vma; | |
2453 | ||
2454 | if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) | |
2455 | return -EINVAL; | |
2456 | ||
2457 | end = start + PAGE_ALIGN(len); | |
2458 | if (end == start) | |
2459 | return -EINVAL; | |
2460 | ||
2461 | /* arch_unmap() might do unmaps itself. */ | |
2462 | arch_unmap(mm, start, end); | |
2463 | ||
2464 | /* Find the first overlapping VMA */ | |
183654ce | 2465 | vma = vma_find(vmi, end); |
11f9a21a LH |
2466 | if (!vma) |
2467 | return 0; | |
2468 | ||
183654ce | 2469 | return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); |
11f9a21a LH |
2470 | } |
2471 | ||
2472 | /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. | |
2473 | * @mm: The mm_struct | |
2474 | * @start: The start address to munmap | |
2475 | * @len: The length to be munmapped. | |
2476 | * @uf: The userfaultfd list_head | |
2477 | */ | |
dd2283f2 YS |
2478 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, |
2479 | struct list_head *uf) | |
2480 | { | |
183654ce | 2481 | VMA_ITERATOR(vmi, mm, start); |
11f9a21a | 2482 | |
183654ce | 2483 | return do_vmi_munmap(&vmi, mm, start, len, uf, false); |
dd2283f2 YS |
2484 | } |
2485 | ||
e99668a5 LH |
2486 | unsigned long mmap_region(struct file *file, unsigned long addr, |
2487 | unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, | |
2488 | struct list_head *uf) | |
2489 | { | |
2490 | struct mm_struct *mm = current->mm; | |
2491 | struct vm_area_struct *vma = NULL; | |
2492 | struct vm_area_struct *next, *prev, *merge; | |
2493 | pgoff_t pglen = len >> PAGE_SHIFT; | |
2494 | unsigned long charged = 0; | |
2495 | unsigned long end = addr + len; | |
2496 | unsigned long merge_start = addr, merge_end = end; | |
2497 | pgoff_t vm_pgoff; | |
2498 | int error; | |
183654ce | 2499 | VMA_ITERATOR(vmi, mm, addr); |
e99668a5 LH |
2500 | |
2501 | /* Check against address space limit. */ | |
2502 | if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { | |
2503 | unsigned long nr_pages; | |
2504 | ||
2505 | /* | |
2506 | * MAP_FIXED may remove pages of mappings that intersects with | |
2507 | * requested mapping. Account for the pages it would unmap. | |
2508 | */ | |
2509 | nr_pages = count_vma_pages_range(mm, addr, end); | |
2510 | ||
2511 | if (!may_expand_vm(mm, vm_flags, | |
2512 | (len >> PAGE_SHIFT) - nr_pages)) | |
2513 | return -ENOMEM; | |
2514 | } | |
2515 | ||
2516 | /* Unmap any existing mapping in the area */ | |
183654ce | 2517 | if (do_vmi_munmap(&vmi, mm, addr, len, uf, false)) |
e99668a5 LH |
2518 | return -ENOMEM; |
2519 | ||
2520 | /* | |
2521 | * Private writable mapping: check memory availability | |
2522 | */ | |
2523 | if (accountable_mapping(file, vm_flags)) { | |
2524 | charged = len >> PAGE_SHIFT; | |
2525 | if (security_vm_enough_memory_mm(mm, charged)) | |
2526 | return -ENOMEM; | |
2527 | vm_flags |= VM_ACCOUNT; | |
2528 | } | |
2529 | ||
183654ce LH |
2530 | next = vma_next(&vmi); |
2531 | prev = vma_prev(&vmi); | |
e99668a5 LH |
2532 | if (vm_flags & VM_SPECIAL) |
2533 | goto cannot_expand; | |
2534 | ||
2535 | /* Attempt to expand an old mapping */ | |
2536 | /* Check next */ | |
2537 | if (next && next->vm_start == end && !vma_policy(next) && | |
2538 | can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, | |
2539 | NULL_VM_UFFD_CTX, NULL)) { | |
2540 | merge_end = next->vm_end; | |
2541 | vma = next; | |
2542 | vm_pgoff = next->vm_pgoff - pglen; | |
2543 | } | |
2544 | ||
2545 | /* Check prev */ | |
2546 | if (prev && prev->vm_end == addr && !vma_policy(prev) && | |
2547 | (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, | |
2548 | pgoff, vma->vm_userfaultfd_ctx, NULL) : | |
2549 | can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, | |
2550 | NULL_VM_UFFD_CTX, NULL))) { | |
2551 | merge_start = prev->vm_start; | |
2552 | vma = prev; | |
2553 | vm_pgoff = prev->vm_pgoff; | |
2554 | } | |
2555 | ||
2556 | ||
2557 | /* Actually expand, if possible */ | |
2558 | if (vma && | |
3c441ab7 | 2559 | !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { |
e99668a5 LH |
2560 | khugepaged_enter_vma(vma, vm_flags); |
2561 | goto expanded; | |
2562 | } | |
2563 | ||
e99668a5 LH |
2564 | cannot_expand: |
2565 | /* | |
2566 | * Determine the object being mapped and call the appropriate | |
2567 | * specific mapper. the address has already been validated, but | |
2568 | * not unmapped, but the maps are removed from the list. | |
2569 | */ | |
2570 | vma = vm_area_alloc(mm); | |
2571 | if (!vma) { | |
2572 | error = -ENOMEM; | |
2573 | goto unacct_error; | |
2574 | } | |
2575 | ||
2576 | vma->vm_start = addr; | |
2577 | vma->vm_end = end; | |
2578 | vma->vm_flags = vm_flags; | |
2579 | vma->vm_page_prot = vm_get_page_prot(vm_flags); | |
2580 | vma->vm_pgoff = pgoff; | |
2581 | ||
2582 | if (file) { | |
2583 | if (vm_flags & VM_SHARED) { | |
2584 | error = mapping_map_writable(file->f_mapping); | |
2585 | if (error) | |
2586 | goto free_vma; | |
2587 | } | |
2588 | ||
2589 | vma->vm_file = get_file(file); | |
2590 | error = call_mmap(file, vma); | |
2591 | if (error) | |
2592 | goto unmap_and_free_vma; | |
2593 | ||
a57b7051 LH |
2594 | /* |
2595 | * Expansion is handled above, merging is handled below. | |
2596 | * Drivers should not alter the address of the VMA. | |
e99668a5 | 2597 | */ |
a57b7051 LH |
2598 | if (WARN_ON((addr != vma->vm_start))) { |
2599 | error = -EINVAL; | |
2600 | goto close_and_free_vma; | |
2601 | } | |
183654ce | 2602 | vma_iter_set(&vmi, addr); |
e99668a5 LH |
2603 | |
2604 | /* | |
2605 | * If vm_flags changed after call_mmap(), we should try merge | |
2606 | * vma again as we may succeed this time. | |
2607 | */ | |
2608 | if (unlikely(vm_flags != vma->vm_flags && prev)) { | |
9760ebff LH |
2609 | merge = vma_merge(&vmi, mm, prev, vma->vm_start, |
2610 | vma->vm_end, vma->vm_flags, NULL, | |
2611 | vma->vm_file, vma->vm_pgoff, NULL, | |
2612 | NULL_VM_UFFD_CTX, NULL); | |
e99668a5 LH |
2613 | if (merge) { |
2614 | /* | |
2615 | * ->mmap() can change vma->vm_file and fput | |
2616 | * the original file. So fput the vma->vm_file | |
2617 | * here or we would add an extra fput for file | |
2618 | * and cause general protection fault | |
2619 | * ultimately. | |
2620 | */ | |
2621 | fput(vma->vm_file); | |
2622 | vm_area_free(vma); | |
2623 | vma = merge; | |
2624 | /* Update vm_flags to pick up the change. */ | |
e99668a5 LH |
2625 | vm_flags = vma->vm_flags; |
2626 | goto unmap_writable; | |
2627 | } | |
2628 | } | |
2629 | ||
2630 | vm_flags = vma->vm_flags; | |
2631 | } else if (vm_flags & VM_SHARED) { | |
2632 | error = shmem_zero_setup(vma); | |
2633 | if (error) | |
2634 | goto free_vma; | |
2635 | } else { | |
2636 | vma_set_anonymous(vma); | |
2637 | } | |
2638 | ||
b507808e JG |
2639 | if (map_deny_write_exec(vma, vma->vm_flags)) { |
2640 | error = -EACCES; | |
2641 | if (file) | |
2642 | goto close_and_free_vma; | |
2643 | else if (vma->vm_file) | |
2644 | goto unmap_and_free_vma; | |
2645 | else | |
2646 | goto free_vma; | |
2647 | } | |
2648 | ||
e99668a5 LH |
2649 | /* Allow architectures to sanity-check the vm_flags */ |
2650 | if (!arch_validate_flags(vma->vm_flags)) { | |
2651 | error = -EINVAL; | |
2652 | if (file) | |
deb0f656 | 2653 | goto close_and_free_vma; |
cc674ab3 LZ |
2654 | else if (vma->vm_file) |
2655 | goto unmap_and_free_vma; | |
e99668a5 LH |
2656 | else |
2657 | goto free_vma; | |
2658 | } | |
2659 | ||
183654ce | 2660 | if (vma_iter_prealloc(&vmi)) { |
e99668a5 LH |
2661 | error = -ENOMEM; |
2662 | if (file) | |
5789151e | 2663 | goto close_and_free_vma; |
cc674ab3 LZ |
2664 | else if (vma->vm_file) |
2665 | goto unmap_and_free_vma; | |
e99668a5 LH |
2666 | else |
2667 | goto free_vma; | |
2668 | } | |
2669 | ||
2670 | if (vma->vm_file) | |
2671 | i_mmap_lock_write(vma->vm_file->f_mapping); | |
2672 | ||
183654ce | 2673 | vma_iter_store(&vmi, vma); |
e99668a5 LH |
2674 | mm->map_count++; |
2675 | if (vma->vm_file) { | |
2676 | if (vma->vm_flags & VM_SHARED) | |
2677 | mapping_allow_writable(vma->vm_file->f_mapping); | |
2678 | ||
2679 | flush_dcache_mmap_lock(vma->vm_file->f_mapping); | |
2680 | vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); | |
2681 | flush_dcache_mmap_unlock(vma->vm_file->f_mapping); | |
2682 | i_mmap_unlock_write(vma->vm_file->f_mapping); | |
2683 | } | |
2684 | ||
2685 | /* | |
2686 | * vma_merge() calls khugepaged_enter_vma() either, the below | |
2687 | * call covers the non-merge case. | |
2688 | */ | |
2689 | khugepaged_enter_vma(vma, vma->vm_flags); | |
2690 | ||
2691 | /* Once vma denies write, undo our temporary denial count */ | |
2692 | unmap_writable: | |
2693 | if (file && vm_flags & VM_SHARED) | |
2694 | mapping_unmap_writable(file->f_mapping); | |
2695 | file = vma->vm_file; | |
2696 | expanded: | |
2697 | perf_event_mmap(vma); | |
2698 | ||
2699 | vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); | |
2700 | if (vm_flags & VM_LOCKED) { | |
2701 | if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || | |
2702 | is_vm_hugetlb_page(vma) || | |
2703 | vma == get_gate_vma(current->mm)) | |
2704 | vma->vm_flags &= VM_LOCKED_CLEAR_MASK; | |
2705 | else | |
2706 | mm->locked_vm += (len >> PAGE_SHIFT); | |
2707 | } | |
2708 | ||
2709 | if (file) | |
2710 | uprobe_mmap(vma); | |
2711 | ||
2712 | /* | |
2713 | * New (or expanded) vma always get soft dirty status. | |
2714 | * Otherwise user-space soft-dirty page tracker won't | |
2715 | * be able to distinguish situation when vma area unmapped, | |
2716 | * then new mapped in-place (which must be aimed as | |
2717 | * a completely new data area). | |
2718 | */ | |
2719 | vma->vm_flags |= VM_SOFTDIRTY; | |
2720 | ||
2721 | vma_set_page_prot(vma); | |
2722 | ||
2723 | validate_mm(mm); | |
2724 | return addr; | |
2725 | ||
deb0f656 CL |
2726 | close_and_free_vma: |
2727 | if (vma->vm_ops && vma->vm_ops->close) | |
2728 | vma->vm_ops->close(vma); | |
e99668a5 LH |
2729 | unmap_and_free_vma: |
2730 | fput(vma->vm_file); | |
2731 | vma->vm_file = NULL; | |
2732 | ||
2733 | /* Undo any partial mapping done by a device driver. */ | |
183654ce | 2734 | unmap_region(mm, &mm->mm_mt, vma, prev, next, vma->vm_start, vma->vm_end); |
cc674ab3 | 2735 | if (file && (vm_flags & VM_SHARED)) |
e99668a5 LH |
2736 | mapping_unmap_writable(file->f_mapping); |
2737 | free_vma: | |
2738 | vm_area_free(vma); | |
2739 | unacct_error: | |
2740 | if (charged) | |
2741 | vm_unacct_memory(charged); | |
2742 | validate_mm(mm); | |
2743 | return error; | |
2744 | } | |
2745 | ||
dd2283f2 | 2746 | static int __vm_munmap(unsigned long start, size_t len, bool downgrade) |
1da177e4 LT |
2747 | { |
2748 | int ret; | |
bfce281c | 2749 | struct mm_struct *mm = current->mm; |
897ab3e0 | 2750 | LIST_HEAD(uf); |
183654ce | 2751 | VMA_ITERATOR(vmi, mm, start); |
1da177e4 | 2752 | |
d8ed45c5 | 2753 | if (mmap_write_lock_killable(mm)) |
ae798783 MH |
2754 | return -EINTR; |
2755 | ||
183654ce | 2756 | ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade); |
dd2283f2 | 2757 | /* |
c1e8d7c6 | 2758 | * Returning 1 indicates mmap_lock is downgraded. |
dd2283f2 YS |
2759 | * But 1 is not legal return value of vm_munmap() and munmap(), reset |
2760 | * it to 0 before return. | |
2761 | */ | |
2762 | if (ret == 1) { | |
d8ed45c5 | 2763 | mmap_read_unlock(mm); |
dd2283f2 YS |
2764 | ret = 0; |
2765 | } else | |
d8ed45c5 | 2766 | mmap_write_unlock(mm); |
dd2283f2 | 2767 | |
897ab3e0 | 2768 | userfaultfd_unmap_complete(mm, &uf); |
1da177e4 LT |
2769 | return ret; |
2770 | } | |
dd2283f2 YS |
2771 | |
2772 | int vm_munmap(unsigned long start, size_t len) | |
2773 | { | |
2774 | return __vm_munmap(start, len, false); | |
2775 | } | |
a46ef99d LT |
2776 | EXPORT_SYMBOL(vm_munmap); |
2777 | ||
2778 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) | |
2779 | { | |
ce18d171 | 2780 | addr = untagged_addr(addr); |
dd2283f2 | 2781 | return __vm_munmap(addr, len, true); |
a46ef99d | 2782 | } |
1da177e4 | 2783 | |
c8d78c18 KS |
2784 | |
2785 | /* | |
2786 | * Emulation of deprecated remap_file_pages() syscall. | |
2787 | */ | |
2788 | SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, | |
2789 | unsigned long, prot, unsigned long, pgoff, unsigned long, flags) | |
2790 | { | |
2791 | ||
2792 | struct mm_struct *mm = current->mm; | |
2793 | struct vm_area_struct *vma; | |
2794 | unsigned long populate = 0; | |
2795 | unsigned long ret = -EINVAL; | |
2796 | struct file *file; | |
2797 | ||
ee65728e | 2798 | pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", |
756a025f | 2799 | current->comm, current->pid); |
c8d78c18 KS |
2800 | |
2801 | if (prot) | |
2802 | return ret; | |
2803 | start = start & PAGE_MASK; | |
2804 | size = size & PAGE_MASK; | |
2805 | ||
2806 | if (start + size <= start) | |
2807 | return ret; | |
2808 | ||
2809 | /* Does pgoff wrap? */ | |
2810 | if (pgoff + (size >> PAGE_SHIFT) < pgoff) | |
2811 | return ret; | |
2812 | ||
d8ed45c5 | 2813 | if (mmap_write_lock_killable(mm)) |
dc0ef0df MH |
2814 | return -EINTR; |
2815 | ||
9b593cb2 | 2816 | vma = vma_lookup(mm, start); |
c8d78c18 KS |
2817 | |
2818 | if (!vma || !(vma->vm_flags & VM_SHARED)) | |
2819 | goto out; | |
2820 | ||
48f7df32 | 2821 | if (start + size > vma->vm_end) { |
763ecb03 LH |
2822 | VMA_ITERATOR(vmi, mm, vma->vm_end); |
2823 | struct vm_area_struct *next, *prev = vma; | |
48f7df32 | 2824 | |
763ecb03 | 2825 | for_each_vma_range(vmi, next, start + size) { |
48f7df32 | 2826 | /* hole between vmas ? */ |
763ecb03 | 2827 | if (next->vm_start != prev->vm_end) |
48f7df32 KS |
2828 | goto out; |
2829 | ||
2830 | if (next->vm_file != vma->vm_file) | |
2831 | goto out; | |
2832 | ||
2833 | if (next->vm_flags != vma->vm_flags) | |
2834 | goto out; | |
2835 | ||
1db43d3f LH |
2836 | if (start + size <= next->vm_end) |
2837 | break; | |
2838 | ||
763ecb03 | 2839 | prev = next; |
48f7df32 KS |
2840 | } |
2841 | ||
2842 | if (!next) | |
2843 | goto out; | |
c8d78c18 KS |
2844 | } |
2845 | ||
2846 | prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; | |
2847 | prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; | |
2848 | prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; | |
2849 | ||
2850 | flags &= MAP_NONBLOCK; | |
2851 | flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; | |
fce000b1 | 2852 | if (vma->vm_flags & VM_LOCKED) |
c8d78c18 | 2853 | flags |= MAP_LOCKED; |
48f7df32 | 2854 | |
c8d78c18 | 2855 | file = get_file(vma->vm_file); |
45e55300 | 2856 | ret = do_mmap(vma->vm_file, start, size, |
897ab3e0 | 2857 | prot, flags, pgoff, &populate, NULL); |
c8d78c18 KS |
2858 | fput(file); |
2859 | out: | |
d8ed45c5 | 2860 | mmap_write_unlock(mm); |
c8d78c18 KS |
2861 | if (populate) |
2862 | mm_populate(ret, populate); | |
2863 | if (!IS_ERR_VALUE(ret)) | |
2864 | ret = 0; | |
2865 | return ret; | |
2866 | } | |
2867 | ||
1da177e4 | 2868 | /* |
27b26701 LH |
2869 | * do_vma_munmap() - Unmap a full or partial vma. |
2870 | * @vmi: The vma iterator pointing at the vma | |
2871 | * @vma: The first vma to be munmapped | |
2872 | * @start: the start of the address to unmap | |
2873 | * @end: The end of the address to unmap | |
2e7ce7d3 | 2874 | * @uf: The userfaultfd list_head |
27b26701 | 2875 | * @downgrade: Attempt to downgrade or not |
2e7ce7d3 | 2876 | * |
27b26701 LH |
2877 | * Returns: 0 on success and not downgraded, 1 on success and downgraded. |
2878 | * unmaps a VMA mapping when the vma iterator is already in position. | |
2879 | * Does not handle alignment. | |
1da177e4 | 2880 | */ |
27b26701 LH |
2881 | int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, |
2882 | unsigned long start, unsigned long end, | |
2883 | struct list_head *uf, bool downgrade) | |
1da177e4 | 2884 | { |
2e7ce7d3 LH |
2885 | struct mm_struct *mm = vma->vm_mm; |
2886 | int ret; | |
3a459756 | 2887 | |
27b26701 LH |
2888 | arch_unmap(mm, start, end); |
2889 | ret = do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); | |
2e7ce7d3 LH |
2890 | validate_mm_mt(mm); |
2891 | return ret; | |
2892 | } | |
1da177e4 | 2893 | |
2e7ce7d3 LH |
2894 | /* |
2895 | * do_brk_flags() - Increase the brk vma if the flags match. | |
92fed820 | 2896 | * @vmi: The vma iterator |
2e7ce7d3 LH |
2897 | * @addr: The start address |
2898 | * @len: The length of the increase | |
2899 | * @vma: The vma, | |
2900 | * @flags: The VMA Flags | |
2901 | * | |
2902 | * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags | |
2903 | * do not match then create a new anonymous VMA. Eventually we may be able to | |
2904 | * do some brk-specific accounting here. | |
2905 | */ | |
92fed820 | 2906 | static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, |
763ecb03 | 2907 | unsigned long addr, unsigned long len, unsigned long flags) |
2e7ce7d3 LH |
2908 | { |
2909 | struct mm_struct *mm = current->mm; | |
1da177e4 | 2910 | |
2e7ce7d3 LH |
2911 | validate_mm_mt(mm); |
2912 | /* | |
2913 | * Check against address space limits by the changed size | |
2914 | * Note: This happens *after* clearing old mappings in some code paths. | |
2915 | */ | |
2916 | flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; | |
84638335 | 2917 | if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) |
1da177e4 LT |
2918 | return -ENOMEM; |
2919 | ||
2920 | if (mm->map_count > sysctl_max_map_count) | |
2921 | return -ENOMEM; | |
2922 | ||
191c5424 | 2923 | if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) |
1da177e4 LT |
2924 | return -ENOMEM; |
2925 | ||
1da177e4 | 2926 | /* |
2e7ce7d3 LH |
2927 | * Expand the existing vma if possible; Note that singular lists do not |
2928 | * occur after forking, so the expand will only happen on new VMAs. | |
1da177e4 | 2929 | */ |
6c28ca64 LH |
2930 | if (vma && vma->vm_end == addr && !vma_policy(vma) && |
2931 | can_vma_merge_after(vma, flags, NULL, NULL, | |
2932 | addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) { | |
92fed820 | 2933 | if (vma_iter_prealloc(vmi)) |
675eaca1 | 2934 | goto unacct_fail; |
28c5609f LH |
2935 | |
2936 | vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0); | |
2e7ce7d3 LH |
2937 | if (vma->anon_vma) { |
2938 | anon_vma_lock_write(vma->anon_vma); | |
2939 | anon_vma_interval_tree_pre_update_vma(vma); | |
2940 | } | |
2941 | vma->vm_end = addr + len; | |
2942 | vma->vm_flags |= VM_SOFTDIRTY; | |
92fed820 | 2943 | vma_iter_store(vmi, vma); |
2e7ce7d3 LH |
2944 | |
2945 | if (vma->anon_vma) { | |
2946 | anon_vma_interval_tree_post_update_vma(vma); | |
2947 | anon_vma_unlock_write(vma->anon_vma); | |
2948 | } | |
2949 | khugepaged_enter_vma(vma, flags); | |
2950 | goto out; | |
1da177e4 | 2951 | } |
2e7ce7d3 LH |
2952 | |
2953 | /* create a vma struct for an anonymous mapping */ | |
2954 | vma = vm_area_alloc(mm); | |
2955 | if (!vma) | |
675eaca1 | 2956 | goto unacct_fail; |
1da177e4 | 2957 | |
bfd40eaf | 2958 | vma_set_anonymous(vma); |
1da177e4 LT |
2959 | vma->vm_start = addr; |
2960 | vma->vm_end = addr + len; | |
2e7ce7d3 | 2961 | vma->vm_pgoff = addr >> PAGE_SHIFT; |
1da177e4 | 2962 | vma->vm_flags = flags; |
3ed75eb8 | 2963 | vma->vm_page_prot = vm_get_page_prot(flags); |
92fed820 | 2964 | if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) |
2e7ce7d3 | 2965 | goto mas_store_fail; |
d4af56c5 | 2966 | |
2e7ce7d3 | 2967 | mm->map_count++; |
1da177e4 | 2968 | out: |
3af9e859 | 2969 | perf_event_mmap(vma); |
1da177e4 | 2970 | mm->total_vm += len >> PAGE_SHIFT; |
84638335 | 2971 | mm->data_vm += len >> PAGE_SHIFT; |
128557ff ML |
2972 | if (flags & VM_LOCKED) |
2973 | mm->locked_vm += (len >> PAGE_SHIFT); | |
d9104d1c | 2974 | vma->vm_flags |= VM_SOFTDIRTY; |
763ecb03 | 2975 | validate_mm(mm); |
5d22fc25 | 2976 | return 0; |
d4af56c5 | 2977 | |
2e7ce7d3 | 2978 | mas_store_fail: |
d4af56c5 | 2979 | vm_area_free(vma); |
675eaca1 | 2980 | unacct_fail: |
2e7ce7d3 LH |
2981 | vm_unacct_memory(len >> PAGE_SHIFT); |
2982 | return -ENOMEM; | |
1da177e4 LT |
2983 | } |
2984 | ||
bb177a73 | 2985 | int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) |
e4eb1ff6 LT |
2986 | { |
2987 | struct mm_struct *mm = current->mm; | |
2e7ce7d3 | 2988 | struct vm_area_struct *vma = NULL; |
bb177a73 | 2989 | unsigned long len; |
5d22fc25 | 2990 | int ret; |
128557ff | 2991 | bool populate; |
897ab3e0 | 2992 | LIST_HEAD(uf); |
92fed820 | 2993 | VMA_ITERATOR(vmi, mm, addr); |
e4eb1ff6 | 2994 | |
bb177a73 MH |
2995 | len = PAGE_ALIGN(request); |
2996 | if (len < request) | |
2997 | return -ENOMEM; | |
2998 | if (!len) | |
2999 | return 0; | |
3000 | ||
d8ed45c5 | 3001 | if (mmap_write_lock_killable(mm)) |
2d6c9282 MH |
3002 | return -EINTR; |
3003 | ||
2e7ce7d3 LH |
3004 | /* Until we need other flags, refuse anything except VM_EXEC. */ |
3005 | if ((flags & (~VM_EXEC)) != 0) | |
3006 | return -EINVAL; | |
3007 | ||
3008 | ret = check_brk_limits(addr, len); | |
3009 | if (ret) | |
3010 | goto limits_failed; | |
3011 | ||
183654ce | 3012 | ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0); |
2e7ce7d3 LH |
3013 | if (ret) |
3014 | goto munmap_failed; | |
3015 | ||
92fed820 LH |
3016 | vma = vma_prev(&vmi); |
3017 | ret = do_brk_flags(&vmi, vma, addr, len, flags); | |
128557ff | 3018 | populate = ((mm->def_flags & VM_LOCKED) != 0); |
d8ed45c5 | 3019 | mmap_write_unlock(mm); |
897ab3e0 | 3020 | userfaultfd_unmap_complete(mm, &uf); |
5d22fc25 | 3021 | if (populate && !ret) |
128557ff | 3022 | mm_populate(addr, len); |
e4eb1ff6 | 3023 | return ret; |
2e7ce7d3 LH |
3024 | |
3025 | munmap_failed: | |
3026 | limits_failed: | |
3027 | mmap_write_unlock(mm); | |
3028 | return ret; | |
e4eb1ff6 | 3029 | } |
16e72e9b DV |
3030 | EXPORT_SYMBOL(vm_brk_flags); |
3031 | ||
3032 | int vm_brk(unsigned long addr, unsigned long len) | |
3033 | { | |
3034 | return vm_brk_flags(addr, len, 0); | |
3035 | } | |
e4eb1ff6 | 3036 | EXPORT_SYMBOL(vm_brk); |
1da177e4 LT |
3037 | |
3038 | /* Release all mmaps. */ | |
3039 | void exit_mmap(struct mm_struct *mm) | |
3040 | { | |
d16dfc55 | 3041 | struct mmu_gather tlb; |
ba470de4 | 3042 | struct vm_area_struct *vma; |
1da177e4 | 3043 | unsigned long nr_accounted = 0; |
763ecb03 LH |
3044 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
3045 | int count = 0; | |
1da177e4 | 3046 | |
d6dd61c8 | 3047 | /* mm's last user has gone, and its about to be pulled down */ |
cddb8a5c | 3048 | mmu_notifier_release(mm); |
d6dd61c8 | 3049 | |
bf3980c8 | 3050 | mmap_read_lock(mm); |
9480c53e JF |
3051 | arch_exit_mmap(mm); |
3052 | ||
763ecb03 | 3053 | vma = mas_find(&mas, ULONG_MAX); |
64591e86 SB |
3054 | if (!vma) { |
3055 | /* Can happen if dup_mmap() received an OOM */ | |
bf3980c8 | 3056 | mmap_read_unlock(mm); |
9480c53e | 3057 | return; |
64591e86 | 3058 | } |
9480c53e | 3059 | |
1da177e4 | 3060 | lru_add_drain(); |
1da177e4 | 3061 | flush_cache_mm(mm); |
d8b45053 | 3062 | tlb_gather_mmu_fullmm(&tlb, mm); |
901608d9 | 3063 | /* update_hiwater_rss(mm) here? but nobody should be looking */ |
763ecb03 LH |
3064 | /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ |
3065 | unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX); | |
bf3980c8 SB |
3066 | mmap_read_unlock(mm); |
3067 | ||
3068 | /* | |
3069 | * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper | |
b3541d91 | 3070 | * because the memory has been already freed. |
bf3980c8 SB |
3071 | */ |
3072 | set_bit(MMF_OOM_SKIP, &mm->flags); | |
3073 | mmap_write_lock(mm); | |
763ecb03 LH |
3074 | free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, |
3075 | USER_PGTABLES_CEILING); | |
ae8eba8b | 3076 | tlb_finish_mmu(&tlb); |
1da177e4 | 3077 | |
763ecb03 LH |
3078 | /* |
3079 | * Walk the list again, actually closing and freeing it, with preemption | |
3080 | * enabled, without holding any MM locks besides the unreachable | |
3081 | * mmap_write_lock. | |
3082 | */ | |
3083 | do { | |
4f74d2c8 LT |
3084 | if (vma->vm_flags & VM_ACCOUNT) |
3085 | nr_accounted += vma_pages(vma); | |
763ecb03 LH |
3086 | remove_vma(vma); |
3087 | count++; | |
0a3b3c25 | 3088 | cond_resched(); |
763ecb03 LH |
3089 | } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); |
3090 | ||
3091 | BUG_ON(count != mm->map_count); | |
d4af56c5 LH |
3092 | |
3093 | trace_exit_mmap(mm); | |
3094 | __mt_destroy(&mm->mm_mt); | |
64591e86 | 3095 | mmap_write_unlock(mm); |
4f74d2c8 | 3096 | vm_unacct_memory(nr_accounted); |
1da177e4 LT |
3097 | } |
3098 | ||
3099 | /* Insert vm structure into process list sorted by address | |
3100 | * and into the inode's i_mmap tree. If vm_file is non-NULL | |
c8c06efa | 3101 | * then i_mmap_rwsem is taken here. |
1da177e4 | 3102 | */ |
6597d783 | 3103 | int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 3104 | { |
d4af56c5 | 3105 | unsigned long charged = vma_pages(vma); |
1da177e4 | 3106 | |
d4af56c5 | 3107 | |
d0601a50 | 3108 | if (find_vma_intersection(mm, vma->vm_start, vma->vm_end)) |
c9d13f5f | 3109 | return -ENOMEM; |
d4af56c5 | 3110 | |
c9d13f5f | 3111 | if ((vma->vm_flags & VM_ACCOUNT) && |
d4af56c5 | 3112 | security_vm_enough_memory_mm(mm, charged)) |
c9d13f5f CG |
3113 | return -ENOMEM; |
3114 | ||
1da177e4 LT |
3115 | /* |
3116 | * The vm_pgoff of a purely anonymous vma should be irrelevant | |
3117 | * until its first write fault, when page's anon_vma and index | |
3118 | * are set. But now set the vm_pgoff it will almost certainly | |
3119 | * end up with (unless mremap moves it elsewhere before that | |
3120 | * first wfault), so /proc/pid/maps tells a consistent story. | |
3121 | * | |
3122 | * By setting it to reflect the virtual start address of the | |
3123 | * vma, merges and splits can happen in a seamless way, just | |
3124 | * using the existing file pgoff checks and manipulations. | |
8332326e | 3125 | * Similarly in do_mmap and in do_brk_flags. |
1da177e4 | 3126 | */ |
8a9cc3b5 | 3127 | if (vma_is_anonymous(vma)) { |
1da177e4 LT |
3128 | BUG_ON(vma->anon_vma); |
3129 | vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; | |
3130 | } | |
2b144498 | 3131 | |
763ecb03 | 3132 | if (vma_link(mm, vma)) { |
d4af56c5 LH |
3133 | vm_unacct_memory(charged); |
3134 | return -ENOMEM; | |
3135 | } | |
3136 | ||
1da177e4 LT |
3137 | return 0; |
3138 | } | |
3139 | ||
3140 | /* | |
3141 | * Copy the vma structure to a new location in the same mm, | |
3142 | * prior to moving page table entries, to effect an mremap move. | |
3143 | */ | |
3144 | struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, | |
38a76013 ML |
3145 | unsigned long addr, unsigned long len, pgoff_t pgoff, |
3146 | bool *need_rmap_locks) | |
1da177e4 LT |
3147 | { |
3148 | struct vm_area_struct *vma = *vmap; | |
3149 | unsigned long vma_start = vma->vm_start; | |
3150 | struct mm_struct *mm = vma->vm_mm; | |
3151 | struct vm_area_struct *new_vma, *prev; | |
948f017b | 3152 | bool faulted_in_anon_vma = true; |
076f16bf | 3153 | VMA_ITERATOR(vmi, mm, addr); |
1da177e4 | 3154 | |
d4af56c5 | 3155 | validate_mm_mt(mm); |
1da177e4 LT |
3156 | /* |
3157 | * If anonymous vma has not yet been faulted, update new pgoff | |
3158 | * to match new location, to increase its chance of merging. | |
3159 | */ | |
ce75799b | 3160 | if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { |
1da177e4 | 3161 | pgoff = addr >> PAGE_SHIFT; |
948f017b AA |
3162 | faulted_in_anon_vma = false; |
3163 | } | |
1da177e4 | 3164 | |
763ecb03 LH |
3165 | new_vma = find_vma_prev(mm, addr, &prev); |
3166 | if (new_vma && new_vma->vm_start < addr + len) | |
6597d783 | 3167 | return NULL; /* should never get here */ |
524e00b3 | 3168 | |
9760ebff | 3169 | new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags, |
19a809af | 3170 | vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), |
5c26f6ac | 3171 | vma->vm_userfaultfd_ctx, anon_vma_name(vma)); |
1da177e4 LT |
3172 | if (new_vma) { |
3173 | /* | |
3174 | * Source vma may have been merged into new_vma | |
3175 | */ | |
948f017b AA |
3176 | if (unlikely(vma_start >= new_vma->vm_start && |
3177 | vma_start < new_vma->vm_end)) { | |
3178 | /* | |
3179 | * The only way we can get a vma_merge with | |
3180 | * self during an mremap is if the vma hasn't | |
3181 | * been faulted in yet and we were allowed to | |
3182 | * reset the dst vma->vm_pgoff to the | |
3183 | * destination address of the mremap to allow | |
3184 | * the merge to happen. mremap must change the | |
3185 | * vm_pgoff linearity between src and dst vmas | |
3186 | * (in turn preventing a vma_merge) to be | |
3187 | * safe. It is only safe to keep the vm_pgoff | |
3188 | * linear if there are no pages mapped yet. | |
3189 | */ | |
81d1b09c | 3190 | VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); |
38a76013 | 3191 | *vmap = vma = new_vma; |
108d6642 | 3192 | } |
38a76013 | 3193 | *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); |
1da177e4 | 3194 | } else { |
3928d4f5 | 3195 | new_vma = vm_area_dup(vma); |
e3975891 CG |
3196 | if (!new_vma) |
3197 | goto out; | |
e3975891 CG |
3198 | new_vma->vm_start = addr; |
3199 | new_vma->vm_end = addr + len; | |
3200 | new_vma->vm_pgoff = pgoff; | |
3201 | if (vma_dup_policy(vma, new_vma)) | |
3202 | goto out_free_vma; | |
e3975891 CG |
3203 | if (anon_vma_clone(new_vma, vma)) |
3204 | goto out_free_mempol; | |
3205 | if (new_vma->vm_file) | |
3206 | get_file(new_vma->vm_file); | |
3207 | if (new_vma->vm_ops && new_vma->vm_ops->open) | |
3208 | new_vma->vm_ops->open(new_vma); | |
763ecb03 | 3209 | if (vma_link(mm, new_vma)) |
524e00b3 | 3210 | goto out_vma_link; |
e3975891 | 3211 | *need_rmap_locks = false; |
1da177e4 | 3212 | } |
d4af56c5 | 3213 | validate_mm_mt(mm); |
1da177e4 | 3214 | return new_vma; |
5beb4930 | 3215 | |
524e00b3 LH |
3216 | out_vma_link: |
3217 | if (new_vma->vm_ops && new_vma->vm_ops->close) | |
3218 | new_vma->vm_ops->close(new_vma); | |
92b73996 LH |
3219 | |
3220 | if (new_vma->vm_file) | |
3221 | fput(new_vma->vm_file); | |
3222 | ||
3223 | unlink_anon_vmas(new_vma); | |
e3975891 | 3224 | out_free_mempol: |
ef0855d3 | 3225 | mpol_put(vma_policy(new_vma)); |
e3975891 | 3226 | out_free_vma: |
3928d4f5 | 3227 | vm_area_free(new_vma); |
e3975891 | 3228 | out: |
d4af56c5 | 3229 | validate_mm_mt(mm); |
5beb4930 | 3230 | return NULL; |
1da177e4 | 3231 | } |
119f657c | 3232 | |
3233 | /* | |
3234 | * Return true if the calling process may expand its vm space by the passed | |
3235 | * number of pages | |
3236 | */ | |
84638335 | 3237 | bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) |
119f657c | 3238 | { |
84638335 KK |
3239 | if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) |
3240 | return false; | |
119f657c | 3241 | |
d977d56c KK |
3242 | if (is_data_mapping(flags) && |
3243 | mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { | |
f4fcd558 KK |
3244 | /* Workaround for Valgrind */ |
3245 | if (rlimit(RLIMIT_DATA) == 0 && | |
3246 | mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) | |
3247 | return true; | |
57a7702b DW |
3248 | |
3249 | pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", | |
3250 | current->comm, current->pid, | |
3251 | (mm->data_vm + npages) << PAGE_SHIFT, | |
3252 | rlimit(RLIMIT_DATA), | |
3253 | ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); | |
3254 | ||
3255 | if (!ignore_rlimit_data) | |
d977d56c KK |
3256 | return false; |
3257 | } | |
119f657c | 3258 | |
84638335 KK |
3259 | return true; |
3260 | } | |
3261 | ||
3262 | void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) | |
3263 | { | |
7866076b | 3264 | WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); |
84638335 | 3265 | |
d977d56c | 3266 | if (is_exec_mapping(flags)) |
84638335 | 3267 | mm->exec_vm += npages; |
d977d56c | 3268 | else if (is_stack_mapping(flags)) |
84638335 | 3269 | mm->stack_vm += npages; |
d977d56c | 3270 | else if (is_data_mapping(flags)) |
84638335 | 3271 | mm->data_vm += npages; |
119f657c | 3272 | } |
fa5dc22f | 3273 | |
b3ec9f33 | 3274 | static vm_fault_t special_mapping_fault(struct vm_fault *vmf); |
a62c34bd AL |
3275 | |
3276 | /* | |
3277 | * Having a close hook prevents vma merging regardless of flags. | |
3278 | */ | |
3279 | static void special_mapping_close(struct vm_area_struct *vma) | |
3280 | { | |
3281 | } | |
3282 | ||
3283 | static const char *special_mapping_name(struct vm_area_struct *vma) | |
3284 | { | |
3285 | return ((struct vm_special_mapping *)vma->vm_private_data)->name; | |
3286 | } | |
3287 | ||
14d07113 | 3288 | static int special_mapping_mremap(struct vm_area_struct *new_vma) |
b059a453 DS |
3289 | { |
3290 | struct vm_special_mapping *sm = new_vma->vm_private_data; | |
3291 | ||
280e87e9 DS |
3292 | if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) |
3293 | return -EFAULT; | |
3294 | ||
b059a453 DS |
3295 | if (sm->mremap) |
3296 | return sm->mremap(sm, new_vma); | |
280e87e9 | 3297 | |
b059a453 DS |
3298 | return 0; |
3299 | } | |
3300 | ||
871402e0 DS |
3301 | static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) |
3302 | { | |
3303 | /* | |
3304 | * Forbid splitting special mappings - kernel has expectations over | |
3305 | * the number of pages in mapping. Together with VM_DONTEXPAND | |
3306 | * the size of vma should stay the same over the special mapping's | |
3307 | * lifetime. | |
3308 | */ | |
3309 | return -EINVAL; | |
3310 | } | |
3311 | ||
a62c34bd AL |
3312 | static const struct vm_operations_struct special_mapping_vmops = { |
3313 | .close = special_mapping_close, | |
3314 | .fault = special_mapping_fault, | |
b059a453 | 3315 | .mremap = special_mapping_mremap, |
a62c34bd | 3316 | .name = special_mapping_name, |
af34ebeb DS |
3317 | /* vDSO code relies that VVAR can't be accessed remotely */ |
3318 | .access = NULL, | |
871402e0 | 3319 | .may_split = special_mapping_split, |
a62c34bd AL |
3320 | }; |
3321 | ||
3322 | static const struct vm_operations_struct legacy_special_mapping_vmops = { | |
3323 | .close = special_mapping_close, | |
3324 | .fault = special_mapping_fault, | |
3325 | }; | |
fa5dc22f | 3326 | |
b3ec9f33 | 3327 | static vm_fault_t special_mapping_fault(struct vm_fault *vmf) |
fa5dc22f | 3328 | { |
11bac800 | 3329 | struct vm_area_struct *vma = vmf->vma; |
b1d0e4f5 | 3330 | pgoff_t pgoff; |
fa5dc22f RM |
3331 | struct page **pages; |
3332 | ||
f872f540 | 3333 | if (vma->vm_ops == &legacy_special_mapping_vmops) { |
a62c34bd | 3334 | pages = vma->vm_private_data; |
f872f540 AL |
3335 | } else { |
3336 | struct vm_special_mapping *sm = vma->vm_private_data; | |
3337 | ||
3338 | if (sm->fault) | |
11bac800 | 3339 | return sm->fault(sm, vmf->vma, vmf); |
f872f540 AL |
3340 | |
3341 | pages = sm->pages; | |
3342 | } | |
a62c34bd | 3343 | |
8a9cc3b5 | 3344 | for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) |
b1d0e4f5 | 3345 | pgoff--; |
fa5dc22f RM |
3346 | |
3347 | if (*pages) { | |
3348 | struct page *page = *pages; | |
3349 | get_page(page); | |
b1d0e4f5 NP |
3350 | vmf->page = page; |
3351 | return 0; | |
fa5dc22f RM |
3352 | } |
3353 | ||
b1d0e4f5 | 3354 | return VM_FAULT_SIGBUS; |
fa5dc22f RM |
3355 | } |
3356 | ||
a62c34bd AL |
3357 | static struct vm_area_struct *__install_special_mapping( |
3358 | struct mm_struct *mm, | |
3359 | unsigned long addr, unsigned long len, | |
27f28b97 CG |
3360 | unsigned long vm_flags, void *priv, |
3361 | const struct vm_operations_struct *ops) | |
fa5dc22f | 3362 | { |
462e635e | 3363 | int ret; |
fa5dc22f RM |
3364 | struct vm_area_struct *vma; |
3365 | ||
d4af56c5 | 3366 | validate_mm_mt(mm); |
490fc053 | 3367 | vma = vm_area_alloc(mm); |
fa5dc22f | 3368 | if (unlikely(vma == NULL)) |
3935ed6a | 3369 | return ERR_PTR(-ENOMEM); |
fa5dc22f | 3370 | |
fa5dc22f RM |
3371 | vma->vm_start = addr; |
3372 | vma->vm_end = addr + len; | |
3373 | ||
d9104d1c | 3374 | vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY; |
1fc09228 | 3375 | vma->vm_flags &= VM_LOCKED_CLEAR_MASK; |
3ed75eb8 | 3376 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
fa5dc22f | 3377 | |
a62c34bd AL |
3378 | vma->vm_ops = ops; |
3379 | vma->vm_private_data = priv; | |
fa5dc22f | 3380 | |
462e635e TO |
3381 | ret = insert_vm_struct(mm, vma); |
3382 | if (ret) | |
3383 | goto out; | |
fa5dc22f | 3384 | |
84638335 | 3385 | vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); |
fa5dc22f | 3386 | |
cdd6c482 | 3387 | perf_event_mmap(vma); |
089dd79d | 3388 | |
d4af56c5 | 3389 | validate_mm_mt(mm); |
3935ed6a | 3390 | return vma; |
462e635e TO |
3391 | |
3392 | out: | |
3928d4f5 | 3393 | vm_area_free(vma); |
d4af56c5 | 3394 | validate_mm_mt(mm); |
3935ed6a SS |
3395 | return ERR_PTR(ret); |
3396 | } | |
3397 | ||
2eefd878 DS |
3398 | bool vma_is_special_mapping(const struct vm_area_struct *vma, |
3399 | const struct vm_special_mapping *sm) | |
3400 | { | |
3401 | return vma->vm_private_data == sm && | |
3402 | (vma->vm_ops == &special_mapping_vmops || | |
3403 | vma->vm_ops == &legacy_special_mapping_vmops); | |
3404 | } | |
3405 | ||
a62c34bd | 3406 | /* |
c1e8d7c6 | 3407 | * Called with mm->mmap_lock held for writing. |
a62c34bd AL |
3408 | * Insert a new vma covering the given region, with the given flags. |
3409 | * Its pages are supplied by the given array of struct page *. | |
3410 | * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. | |
3411 | * The region past the last page supplied will always produce SIGBUS. | |
3412 | * The array pointer and the pages it points to are assumed to stay alive | |
3413 | * for as long as this mapping might exist. | |
3414 | */ | |
3415 | struct vm_area_struct *_install_special_mapping( | |
3416 | struct mm_struct *mm, | |
3417 | unsigned long addr, unsigned long len, | |
3418 | unsigned long vm_flags, const struct vm_special_mapping *spec) | |
3419 | { | |
27f28b97 CG |
3420 | return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, |
3421 | &special_mapping_vmops); | |
a62c34bd AL |
3422 | } |
3423 | ||
3935ed6a SS |
3424 | int install_special_mapping(struct mm_struct *mm, |
3425 | unsigned long addr, unsigned long len, | |
3426 | unsigned long vm_flags, struct page **pages) | |
3427 | { | |
a62c34bd | 3428 | struct vm_area_struct *vma = __install_special_mapping( |
27f28b97 CG |
3429 | mm, addr, len, vm_flags, (void *)pages, |
3430 | &legacy_special_mapping_vmops); | |
3935ed6a | 3431 | |
14bd5b45 | 3432 | return PTR_ERR_OR_ZERO(vma); |
fa5dc22f | 3433 | } |
7906d00c AA |
3434 | |
3435 | static DEFINE_MUTEX(mm_all_locks_mutex); | |
3436 | ||
454ed842 | 3437 | static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) |
7906d00c | 3438 | { |
f808c13f | 3439 | if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { |
7906d00c AA |
3440 | /* |
3441 | * The LSB of head.next can't change from under us | |
3442 | * because we hold the mm_all_locks_mutex. | |
3443 | */ | |
da1c55f1 | 3444 | down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); |
7906d00c AA |
3445 | /* |
3446 | * We can safely modify head.next after taking the | |
5a505085 | 3447 | * anon_vma->root->rwsem. If some other vma in this mm shares |
7906d00c AA |
3448 | * the same anon_vma we won't take it again. |
3449 | * | |
3450 | * No need of atomic instructions here, head.next | |
3451 | * can't change from under us thanks to the | |
5a505085 | 3452 | * anon_vma->root->rwsem. |
7906d00c AA |
3453 | */ |
3454 | if (__test_and_set_bit(0, (unsigned long *) | |
f808c13f | 3455 | &anon_vma->root->rb_root.rb_root.rb_node)) |
7906d00c AA |
3456 | BUG(); |
3457 | } | |
3458 | } | |
3459 | ||
454ed842 | 3460 | static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) |
7906d00c AA |
3461 | { |
3462 | if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { | |
3463 | /* | |
3464 | * AS_MM_ALL_LOCKS can't change from under us because | |
3465 | * we hold the mm_all_locks_mutex. | |
3466 | * | |
3467 | * Operations on ->flags have to be atomic because | |
3468 | * even if AS_MM_ALL_LOCKS is stable thanks to the | |
3469 | * mm_all_locks_mutex, there may be other cpus | |
3470 | * changing other bitflags in parallel to us. | |
3471 | */ | |
3472 | if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) | |
3473 | BUG(); | |
da1c55f1 | 3474 | down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); |
7906d00c AA |
3475 | } |
3476 | } | |
3477 | ||
3478 | /* | |
3479 | * This operation locks against the VM for all pte/vma/mm related | |
3480 | * operations that could ever happen on a certain mm. This includes | |
3481 | * vmtruncate, try_to_unmap, and all page faults. | |
3482 | * | |
c1e8d7c6 | 3483 | * The caller must take the mmap_lock in write mode before calling |
7906d00c | 3484 | * mm_take_all_locks(). The caller isn't allowed to release the |
c1e8d7c6 | 3485 | * mmap_lock until mm_drop_all_locks() returns. |
7906d00c | 3486 | * |
c1e8d7c6 | 3487 | * mmap_lock in write mode is required in order to block all operations |
7906d00c | 3488 | * that could modify pagetables and free pages without need of |
27ba0644 | 3489 | * altering the vma layout. It's also needed in write mode to avoid new |
7906d00c AA |
3490 | * anon_vmas to be associated with existing vmas. |
3491 | * | |
3492 | * A single task can't take more than one mm_take_all_locks() in a row | |
3493 | * or it would deadlock. | |
3494 | * | |
bf181b9f | 3495 | * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in |
7906d00c AA |
3496 | * mapping->flags avoid to take the same lock twice, if more than one |
3497 | * vma in this mm is backed by the same anon_vma or address_space. | |
3498 | * | |
88f306b6 KS |
3499 | * We take locks in following order, accordingly to comment at beginning |
3500 | * of mm/rmap.c: | |
3501 | * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for | |
3502 | * hugetlb mapping); | |
3503 | * - all i_mmap_rwsem locks; | |
3504 | * - all anon_vma->rwseml | |
3505 | * | |
3506 | * We can take all locks within these types randomly because the VM code | |
3507 | * doesn't nest them and we protected from parallel mm_take_all_locks() by | |
3508 | * mm_all_locks_mutex. | |
7906d00c AA |
3509 | * |
3510 | * mm_take_all_locks() and mm_drop_all_locks are expensive operations | |
3511 | * that may have to take thousand of locks. | |
3512 | * | |
3513 | * mm_take_all_locks() can fail if it's interrupted by signals. | |
3514 | */ | |
3515 | int mm_take_all_locks(struct mm_struct *mm) | |
3516 | { | |
3517 | struct vm_area_struct *vma; | |
5beb4930 | 3518 | struct anon_vma_chain *avc; |
763ecb03 | 3519 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
7906d00c | 3520 | |
325bca1f | 3521 | mmap_assert_write_locked(mm); |
7906d00c AA |
3522 | |
3523 | mutex_lock(&mm_all_locks_mutex); | |
3524 | ||
763ecb03 | 3525 | mas_for_each(&mas, vma, ULONG_MAX) { |
7906d00c AA |
3526 | if (signal_pending(current)) |
3527 | goto out_unlock; | |
88f306b6 KS |
3528 | if (vma->vm_file && vma->vm_file->f_mapping && |
3529 | is_vm_hugetlb_page(vma)) | |
3530 | vm_lock_mapping(mm, vma->vm_file->f_mapping); | |
3531 | } | |
3532 | ||
763ecb03 LH |
3533 | mas_set(&mas, 0); |
3534 | mas_for_each(&mas, vma, ULONG_MAX) { | |
88f306b6 KS |
3535 | if (signal_pending(current)) |
3536 | goto out_unlock; | |
3537 | if (vma->vm_file && vma->vm_file->f_mapping && | |
3538 | !is_vm_hugetlb_page(vma)) | |
454ed842 | 3539 | vm_lock_mapping(mm, vma->vm_file->f_mapping); |
7906d00c | 3540 | } |
7cd5a02f | 3541 | |
763ecb03 LH |
3542 | mas_set(&mas, 0); |
3543 | mas_for_each(&mas, vma, ULONG_MAX) { | |
7cd5a02f PZ |
3544 | if (signal_pending(current)) |
3545 | goto out_unlock; | |
3546 | if (vma->anon_vma) | |
5beb4930 RR |
3547 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
3548 | vm_lock_anon_vma(mm, avc->anon_vma); | |
7906d00c | 3549 | } |
7cd5a02f | 3550 | |
584cff54 | 3551 | return 0; |
7906d00c AA |
3552 | |
3553 | out_unlock: | |
584cff54 KC |
3554 | mm_drop_all_locks(mm); |
3555 | return -EINTR; | |
7906d00c AA |
3556 | } |
3557 | ||
3558 | static void vm_unlock_anon_vma(struct anon_vma *anon_vma) | |
3559 | { | |
f808c13f | 3560 | if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { |
7906d00c AA |
3561 | /* |
3562 | * The LSB of head.next can't change to 0 from under | |
3563 | * us because we hold the mm_all_locks_mutex. | |
3564 | * | |
3565 | * We must however clear the bitflag before unlocking | |
bf181b9f | 3566 | * the vma so the users using the anon_vma->rb_root will |
7906d00c AA |
3567 | * never see our bitflag. |
3568 | * | |
3569 | * No need of atomic instructions here, head.next | |
3570 | * can't change from under us until we release the | |
5a505085 | 3571 | * anon_vma->root->rwsem. |
7906d00c AA |
3572 | */ |
3573 | if (!__test_and_clear_bit(0, (unsigned long *) | |
f808c13f | 3574 | &anon_vma->root->rb_root.rb_root.rb_node)) |
7906d00c | 3575 | BUG(); |
08b52706 | 3576 | anon_vma_unlock_write(anon_vma); |
7906d00c AA |
3577 | } |
3578 | } | |
3579 | ||
3580 | static void vm_unlock_mapping(struct address_space *mapping) | |
3581 | { | |
3582 | if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { | |
3583 | /* | |
3584 | * AS_MM_ALL_LOCKS can't change to 0 from under us | |
3585 | * because we hold the mm_all_locks_mutex. | |
3586 | */ | |
83cde9e8 | 3587 | i_mmap_unlock_write(mapping); |
7906d00c AA |
3588 | if (!test_and_clear_bit(AS_MM_ALL_LOCKS, |
3589 | &mapping->flags)) | |
3590 | BUG(); | |
3591 | } | |
3592 | } | |
3593 | ||
3594 | /* | |
c1e8d7c6 | 3595 | * The mmap_lock cannot be released by the caller until |
7906d00c AA |
3596 | * mm_drop_all_locks() returns. |
3597 | */ | |
3598 | void mm_drop_all_locks(struct mm_struct *mm) | |
3599 | { | |
3600 | struct vm_area_struct *vma; | |
5beb4930 | 3601 | struct anon_vma_chain *avc; |
763ecb03 | 3602 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
7906d00c | 3603 | |
325bca1f | 3604 | mmap_assert_write_locked(mm); |
7906d00c AA |
3605 | BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); |
3606 | ||
763ecb03 | 3607 | mas_for_each(&mas, vma, ULONG_MAX) { |
7906d00c | 3608 | if (vma->anon_vma) |
5beb4930 RR |
3609 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
3610 | vm_unlock_anon_vma(avc->anon_vma); | |
7906d00c AA |
3611 | if (vma->vm_file && vma->vm_file->f_mapping) |
3612 | vm_unlock_mapping(vma->vm_file->f_mapping); | |
3613 | } | |
3614 | ||
3615 | mutex_unlock(&mm_all_locks_mutex); | |
3616 | } | |
8feae131 DH |
3617 | |
3618 | /* | |
3edf41d8 | 3619 | * initialise the percpu counter for VM |
8feae131 DH |
3620 | */ |
3621 | void __init mmap_init(void) | |
3622 | { | |
00a62ce9 KM |
3623 | int ret; |
3624 | ||
908c7f19 | 3625 | ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); |
00a62ce9 | 3626 | VM_BUG_ON(ret); |
8feae131 | 3627 | } |
c9b1d098 AS |
3628 | |
3629 | /* | |
3630 | * Initialise sysctl_user_reserve_kbytes. | |
3631 | * | |
3632 | * This is intended to prevent a user from starting a single memory hogging | |
3633 | * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER | |
3634 | * mode. | |
3635 | * | |
3636 | * The default value is min(3% of free memory, 128MB) | |
3637 | * 128MB is enough to recover with sshd/login, bash, and top/kill. | |
3638 | */ | |
1640879a | 3639 | static int init_user_reserve(void) |
c9b1d098 AS |
3640 | { |
3641 | unsigned long free_kbytes; | |
3642 | ||
c41f012a | 3643 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
c9b1d098 AS |
3644 | |
3645 | sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); | |
3646 | return 0; | |
3647 | } | |
a64fb3cd | 3648 | subsys_initcall(init_user_reserve); |
4eeab4f5 AS |
3649 | |
3650 | /* | |
3651 | * Initialise sysctl_admin_reserve_kbytes. | |
3652 | * | |
3653 | * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin | |
3654 | * to log in and kill a memory hogging process. | |
3655 | * | |
3656 | * Systems with more than 256MB will reserve 8MB, enough to recover | |
3657 | * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will | |
3658 | * only reserve 3% of free pages by default. | |
3659 | */ | |
1640879a | 3660 | static int init_admin_reserve(void) |
4eeab4f5 AS |
3661 | { |
3662 | unsigned long free_kbytes; | |
3663 | ||
c41f012a | 3664 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
4eeab4f5 AS |
3665 | |
3666 | sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); | |
3667 | return 0; | |
3668 | } | |
a64fb3cd | 3669 | subsys_initcall(init_admin_reserve); |
1640879a AS |
3670 | |
3671 | /* | |
3672 | * Reinititalise user and admin reserves if memory is added or removed. | |
3673 | * | |
3674 | * The default user reserve max is 128MB, and the default max for the | |
3675 | * admin reserve is 8MB. These are usually, but not always, enough to | |
3676 | * enable recovery from a memory hogging process using login/sshd, a shell, | |
3677 | * and tools like top. It may make sense to increase or even disable the | |
3678 | * reserve depending on the existence of swap or variations in the recovery | |
3679 | * tools. So, the admin may have changed them. | |
3680 | * | |
3681 | * If memory is added and the reserves have been eliminated or increased above | |
3682 | * the default max, then we'll trust the admin. | |
3683 | * | |
3684 | * If memory is removed and there isn't enough free memory, then we | |
3685 | * need to reset the reserves. | |
3686 | * | |
3687 | * Otherwise keep the reserve set by the admin. | |
3688 | */ | |
3689 | static int reserve_mem_notifier(struct notifier_block *nb, | |
3690 | unsigned long action, void *data) | |
3691 | { | |
3692 | unsigned long tmp, free_kbytes; | |
3693 | ||
3694 | switch (action) { | |
3695 | case MEM_ONLINE: | |
3696 | /* Default max is 128MB. Leave alone if modified by operator. */ | |
3697 | tmp = sysctl_user_reserve_kbytes; | |
3698 | if (0 < tmp && tmp < (1UL << 17)) | |
3699 | init_user_reserve(); | |
3700 | ||
3701 | /* Default max is 8MB. Leave alone if modified by operator. */ | |
3702 | tmp = sysctl_admin_reserve_kbytes; | |
3703 | if (0 < tmp && tmp < (1UL << 13)) | |
3704 | init_admin_reserve(); | |
3705 | ||
3706 | break; | |
3707 | case MEM_OFFLINE: | |
c41f012a | 3708 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
1640879a AS |
3709 | |
3710 | if (sysctl_user_reserve_kbytes > free_kbytes) { | |
3711 | init_user_reserve(); | |
3712 | pr_info("vm.user_reserve_kbytes reset to %lu\n", | |
3713 | sysctl_user_reserve_kbytes); | |
3714 | } | |
3715 | ||
3716 | if (sysctl_admin_reserve_kbytes > free_kbytes) { | |
3717 | init_admin_reserve(); | |
3718 | pr_info("vm.admin_reserve_kbytes reset to %lu\n", | |
3719 | sysctl_admin_reserve_kbytes); | |
3720 | } | |
3721 | break; | |
3722 | default: | |
3723 | break; | |
3724 | } | |
3725 | return NOTIFY_OK; | |
3726 | } | |
3727 | ||
1640879a AS |
3728 | static int __meminit init_reserve_notifier(void) |
3729 | { | |
1eeaa4fd | 3730 | if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI)) |
b1de0d13 | 3731 | pr_err("Failed registering memory add/remove notifier for admin reserve\n"); |
1640879a AS |
3732 | |
3733 | return 0; | |
3734 | } | |
a64fb3cd | 3735 | subsys_initcall(init_reserve_notifier); |