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