]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/mm/madvise.c | |
4 | * | |
5 | * Copyright (C) 1999 Linus Torvalds | |
6 | * Copyright (C) 2002 Christoph Hellwig | |
7 | */ | |
8 | ||
9 | #include <linux/mman.h> | |
10 | #include <linux/pagemap.h> | |
11 | #include <linux/syscalls.h> | |
05b74384 | 12 | #include <linux/mempolicy.h> |
afcf938e | 13 | #include <linux/page-isolation.h> |
9c276cc6 | 14 | #include <linux/page_idle.h> |
05ce7724 | 15 | #include <linux/userfaultfd_k.h> |
1da177e4 | 16 | #include <linux/hugetlb.h> |
3f31d075 | 17 | #include <linux/falloc.h> |
692fe624 | 18 | #include <linux/fadvise.h> |
e8edc6e0 | 19 | #include <linux/sched.h> |
ecb8ac8b | 20 | #include <linux/sched/mm.h> |
17fca131 | 21 | #include <linux/mm_inline.h> |
9a10064f | 22 | #include <linux/string.h> |
ecb8ac8b | 23 | #include <linux/uio.h> |
f8af4da3 | 24 | #include <linux/ksm.h> |
3f31d075 | 25 | #include <linux/fs.h> |
9ab4233d | 26 | #include <linux/file.h> |
1998cc04 | 27 | #include <linux/blkdev.h> |
66114cad | 28 | #include <linux/backing-dev.h> |
a520110e | 29 | #include <linux/pagewalk.h> |
1998cc04 SL |
30 | #include <linux/swap.h> |
31 | #include <linux/swapops.h> | |
3a4f8a0b | 32 | #include <linux/shmem_fs.h> |
854e9ed0 MK |
33 | #include <linux/mmu_notifier.h> |
34 | ||
35 | #include <asm/tlb.h> | |
1da177e4 | 36 | |
23519073 | 37 | #include "internal.h" |
014bb1de | 38 | #include "swap.h" |
23519073 | 39 | |
d616d512 MK |
40 | struct madvise_walk_private { |
41 | struct mmu_gather *tlb; | |
42 | bool pageout; | |
43 | }; | |
44 | ||
0a27a14a NP |
45 | /* |
46 | * Any behaviour which results in changes to the vma->vm_flags needs to | |
c1e8d7c6 | 47 | * take mmap_lock for writing. Others, which simply traverse vmas, need |
0a27a14a NP |
48 | * to only take it for reading. |
49 | */ | |
50 | static int madvise_need_mmap_write(int behavior) | |
51 | { | |
52 | switch (behavior) { | |
53 | case MADV_REMOVE: | |
54 | case MADV_WILLNEED: | |
55 | case MADV_DONTNEED: | |
9457056a | 56 | case MADV_DONTNEED_LOCKED: |
9c276cc6 | 57 | case MADV_COLD: |
1a4e58cc | 58 | case MADV_PAGEOUT: |
854e9ed0 | 59 | case MADV_FREE: |
4ca9b385 DH |
60 | case MADV_POPULATE_READ: |
61 | case MADV_POPULATE_WRITE: | |
0a27a14a NP |
62 | return 0; |
63 | default: | |
64 | /* be safe, default to 1. list exceptions explicitly */ | |
65 | return 1; | |
66 | } | |
67 | } | |
68 | ||
9a10064f | 69 | #ifdef CONFIG_ANON_VMA_NAME |
5c26f6ac | 70 | struct anon_vma_name *anon_vma_name_alloc(const char *name) |
78db3412 SB |
71 | { |
72 | struct anon_vma_name *anon_name; | |
73 | size_t count; | |
74 | ||
75 | /* Add 1 for NUL terminator at the end of the anon_name->name */ | |
76 | count = strlen(name) + 1; | |
77 | anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL); | |
78 | if (anon_name) { | |
79 | kref_init(&anon_name->kref); | |
80 | memcpy(anon_name->name, name, count); | |
81 | } | |
82 | ||
83 | return anon_name; | |
84 | } | |
85 | ||
5c26f6ac | 86 | void anon_vma_name_free(struct kref *kref) |
78db3412 SB |
87 | { |
88 | struct anon_vma_name *anon_name = | |
89 | container_of(kref, struct anon_vma_name, kref); | |
90 | kfree(anon_name); | |
91 | } | |
92 | ||
5c26f6ac | 93 | struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) |
9a10064f | 94 | { |
9a10064f CC |
95 | mmap_assert_locked(vma->vm_mm); |
96 | ||
5c26f6ac SB |
97 | if (vma->vm_file) |
98 | return NULL; | |
9a10064f | 99 | |
5c26f6ac | 100 | return vma->anon_name; |
9a10064f CC |
101 | } |
102 | ||
103 | /* mmap_lock should be write-locked */ | |
5c26f6ac SB |
104 | static int replace_anon_vma_name(struct vm_area_struct *vma, |
105 | struct anon_vma_name *anon_name) | |
9a10064f | 106 | { |
5c26f6ac | 107 | struct anon_vma_name *orig_name = anon_vma_name(vma); |
78db3412 | 108 | |
5c26f6ac SB |
109 | if (!anon_name) { |
110 | vma->anon_name = NULL; | |
111 | anon_vma_name_put(orig_name); | |
9a10064f CC |
112 | return 0; |
113 | } | |
114 | ||
5c26f6ac SB |
115 | if (anon_vma_name_eq(orig_name, anon_name)) |
116 | return 0; | |
9a10064f | 117 | |
96403e11 | 118 | vma->anon_name = anon_vma_name_reuse(anon_name); |
5c26f6ac | 119 | anon_vma_name_put(orig_name); |
9a10064f CC |
120 | |
121 | return 0; | |
122 | } | |
123 | #else /* CONFIG_ANON_VMA_NAME */ | |
5c26f6ac SB |
124 | static int replace_anon_vma_name(struct vm_area_struct *vma, |
125 | struct anon_vma_name *anon_name) | |
9a10064f | 126 | { |
5c26f6ac | 127 | if (anon_name) |
9a10064f CC |
128 | return -EINVAL; |
129 | ||
130 | return 0; | |
131 | } | |
132 | #endif /* CONFIG_ANON_VMA_NAME */ | |
1da177e4 | 133 | /* |
ac1e9acc CC |
134 | * Update the vm_flags on region of a vma, splitting it or merging it as |
135 | * necessary. Must be called with mmap_sem held for writing; | |
942341dc SB |
136 | * Caller should ensure anon_name stability by raising its refcount even when |
137 | * anon_name belongs to a valid vma because this function might free that vma. | |
1da177e4 | 138 | */ |
ac1e9acc CC |
139 | static int madvise_update_vma(struct vm_area_struct *vma, |
140 | struct vm_area_struct **prev, unsigned long start, | |
9a10064f | 141 | unsigned long end, unsigned long new_flags, |
5c26f6ac | 142 | struct anon_vma_name *anon_name) |
1da177e4 | 143 | { |
ec9bed9d | 144 | struct mm_struct *mm = vma->vm_mm; |
ac1e9acc | 145 | int error; |
05b74384 | 146 | pgoff_t pgoff; |
e798c6e8 | 147 | |
5c26f6ac | 148 | if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) { |
05b74384 | 149 | *prev = vma; |
ac1e9acc | 150 | return 0; |
05b74384 PM |
151 | } |
152 | ||
153 | pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); | |
154 | *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma, | |
19a809af | 155 | vma->vm_file, pgoff, vma_policy(vma), |
5c26f6ac | 156 | vma->vm_userfaultfd_ctx, anon_name); |
05b74384 PM |
157 | if (*prev) { |
158 | vma = *prev; | |
159 | goto success; | |
160 | } | |
161 | ||
162 | *prev = vma; | |
1da177e4 LT |
163 | |
164 | if (start != vma->vm_start) { | |
ac1e9acc CC |
165 | if (unlikely(mm->map_count >= sysctl_max_map_count)) |
166 | return -ENOMEM; | |
def5efe0 | 167 | error = __split_vma(mm, vma, start, 1); |
f3bc0dba | 168 | if (error) |
ac1e9acc | 169 | return error; |
1da177e4 LT |
170 | } |
171 | ||
172 | if (end != vma->vm_end) { | |
ac1e9acc CC |
173 | if (unlikely(mm->map_count >= sysctl_max_map_count)) |
174 | return -ENOMEM; | |
def5efe0 | 175 | error = __split_vma(mm, vma, end, 0); |
f3bc0dba | 176 | if (error) |
ac1e9acc | 177 | return error; |
1da177e4 LT |
178 | } |
179 | ||
836d5ffd | 180 | success: |
1da177e4 | 181 | /* |
c1e8d7c6 | 182 | * vm_flags is protected by the mmap_lock held in write mode. |
1da177e4 | 183 | */ |
e798c6e8 | 184 | vma->vm_flags = new_flags; |
9a10064f | 185 | if (!vma->vm_file) { |
5c26f6ac | 186 | error = replace_anon_vma_name(vma, anon_name); |
9a10064f CC |
187 | if (error) |
188 | return error; | |
189 | } | |
f3bc0dba | 190 | |
ac1e9acc | 191 | return 0; |
1da177e4 LT |
192 | } |
193 | ||
1998cc04 SL |
194 | #ifdef CONFIG_SWAP |
195 | static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, | |
196 | unsigned long end, struct mm_walk *walk) | |
197 | { | |
1998cc04 SL |
198 | struct vm_area_struct *vma = walk->private; |
199 | unsigned long index; | |
5169b844 | 200 | struct swap_iocb *splug = NULL; |
1998cc04 SL |
201 | |
202 | if (pmd_none_or_trans_huge_or_clear_bad(pmd)) | |
203 | return 0; | |
204 | ||
205 | for (index = start; index != end; index += PAGE_SIZE) { | |
206 | pte_t pte; | |
207 | swp_entry_t entry; | |
208 | struct page *page; | |
209 | spinlock_t *ptl; | |
f7cc67ae | 210 | pte_t *ptep; |
1998cc04 | 211 | |
f7cc67ae ML |
212 | ptep = pte_offset_map_lock(vma->vm_mm, pmd, index, &ptl); |
213 | pte = *ptep; | |
214 | pte_unmap_unlock(ptep, ptl); | |
1998cc04 | 215 | |
f7cc67ae | 216 | if (!is_swap_pte(pte)) |
1998cc04 SL |
217 | continue; |
218 | entry = pte_to_swp_entry(pte); | |
219 | if (unlikely(non_swap_entry(entry))) | |
220 | continue; | |
221 | ||
222 | page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE, | |
5169b844 | 223 | vma, index, false, &splug); |
1998cc04 | 224 | if (page) |
09cbfeaf | 225 | put_page(page); |
1998cc04 | 226 | } |
5169b844 | 227 | swap_read_unplug(splug); |
1998cc04 SL |
228 | |
229 | return 0; | |
230 | } | |
231 | ||
7b86ac33 CH |
232 | static const struct mm_walk_ops swapin_walk_ops = { |
233 | .pmd_entry = swapin_walk_pmd_entry, | |
234 | }; | |
1998cc04 SL |
235 | |
236 | static void force_shm_swapin_readahead(struct vm_area_struct *vma, | |
237 | unsigned long start, unsigned long end, | |
238 | struct address_space *mapping) | |
239 | { | |
e6e88712 | 240 | XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start)); |
66383800 | 241 | pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1); |
1998cc04 | 242 | struct page *page; |
5169b844 | 243 | struct swap_iocb *splug = NULL; |
1998cc04 | 244 | |
e6e88712 MWO |
245 | rcu_read_lock(); |
246 | xas_for_each(&xas, page, end_index) { | |
247 | swp_entry_t swap; | |
1998cc04 | 248 | |
e6e88712 | 249 | if (!xa_is_value(page)) |
1998cc04 | 250 | continue; |
ba6851b4 ML |
251 | swap = radix_to_swp_entry(page); |
252 | /* There might be swapin error entries in shmem mapping. */ | |
253 | if (non_swap_entry(swap)) | |
254 | continue; | |
e6e88712 MWO |
255 | xas_pause(&xas); |
256 | rcu_read_unlock(); | |
257 | ||
1998cc04 | 258 | page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE, |
5169b844 | 259 | NULL, 0, false, &splug); |
1998cc04 | 260 | if (page) |
09cbfeaf | 261 | put_page(page); |
e6e88712 MWO |
262 | |
263 | rcu_read_lock(); | |
1998cc04 | 264 | } |
e6e88712 | 265 | rcu_read_unlock(); |
5169b844 | 266 | swap_read_unplug(splug); |
1998cc04 SL |
267 | |
268 | lru_add_drain(); /* Push any new pages onto the LRU now */ | |
269 | } | |
270 | #endif /* CONFIG_SWAP */ | |
271 | ||
1da177e4 LT |
272 | /* |
273 | * Schedule all required I/O operations. Do not wait for completion. | |
274 | */ | |
ec9bed9d VC |
275 | static long madvise_willneed(struct vm_area_struct *vma, |
276 | struct vm_area_struct **prev, | |
1da177e4 LT |
277 | unsigned long start, unsigned long end) |
278 | { | |
0726b01e | 279 | struct mm_struct *mm = vma->vm_mm; |
1da177e4 | 280 | struct file *file = vma->vm_file; |
692fe624 | 281 | loff_t offset; |
1da177e4 | 282 | |
6ea8d958 | 283 | *prev = vma; |
1998cc04 | 284 | #ifdef CONFIG_SWAP |
97b713ba | 285 | if (!file) { |
7b86ac33 CH |
286 | walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma); |
287 | lru_add_drain(); /* Push any new pages onto the LRU now */ | |
1998cc04 SL |
288 | return 0; |
289 | } | |
1998cc04 | 290 | |
97b713ba | 291 | if (shmem_mapping(file->f_mapping)) { |
97b713ba CH |
292 | force_shm_swapin_readahead(vma, start, end, |
293 | file->f_mapping); | |
294 | return 0; | |
295 | } | |
296 | #else | |
1bef4003 S |
297 | if (!file) |
298 | return -EBADF; | |
97b713ba | 299 | #endif |
1bef4003 | 300 | |
e748dcd0 | 301 | if (IS_DAX(file_inode(file))) { |
fe77ba6f CO |
302 | /* no bad return value, but ignore advice */ |
303 | return 0; | |
304 | } | |
305 | ||
692fe624 JK |
306 | /* |
307 | * Filesystem's fadvise may need to take various locks. We need to | |
308 | * explicitly grab a reference because the vma (and hence the | |
309 | * vma's reference to the file) can go away as soon as we drop | |
c1e8d7c6 | 310 | * mmap_lock. |
692fe624 | 311 | */ |
c1e8d7c6 | 312 | *prev = NULL; /* tell sys_madvise we drop mmap_lock */ |
692fe624 | 313 | get_file(file); |
692fe624 JK |
314 | offset = (loff_t)(start - vma->vm_start) |
315 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); | |
0726b01e | 316 | mmap_read_unlock(mm); |
692fe624 JK |
317 | vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED); |
318 | fput(file); | |
0726b01e | 319 | mmap_read_lock(mm); |
1da177e4 LT |
320 | return 0; |
321 | } | |
322 | ||
d616d512 MK |
323 | static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, |
324 | unsigned long addr, unsigned long end, | |
325 | struct mm_walk *walk) | |
9c276cc6 | 326 | { |
d616d512 MK |
327 | struct madvise_walk_private *private = walk->private; |
328 | struct mmu_gather *tlb = private->tlb; | |
329 | bool pageout = private->pageout; | |
9c276cc6 MK |
330 | struct mm_struct *mm = tlb->mm; |
331 | struct vm_area_struct *vma = walk->vma; | |
332 | pte_t *orig_pte, *pte, ptent; | |
333 | spinlock_t *ptl; | |
d616d512 MK |
334 | struct page *page = NULL; |
335 | LIST_HEAD(page_list); | |
336 | ||
337 | if (fatal_signal_pending(current)) | |
338 | return -EINTR; | |
9c276cc6 MK |
339 | |
340 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
341 | if (pmd_trans_huge(*pmd)) { | |
342 | pmd_t orig_pmd; | |
343 | unsigned long next = pmd_addr_end(addr, end); | |
344 | ||
345 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE); | |
346 | ptl = pmd_trans_huge_lock(pmd, vma); | |
347 | if (!ptl) | |
348 | return 0; | |
349 | ||
350 | orig_pmd = *pmd; | |
351 | if (is_huge_zero_pmd(orig_pmd)) | |
352 | goto huge_unlock; | |
353 | ||
354 | if (unlikely(!pmd_present(orig_pmd))) { | |
355 | VM_BUG_ON(thp_migration_supported() && | |
356 | !is_pmd_migration_entry(orig_pmd)); | |
357 | goto huge_unlock; | |
358 | } | |
359 | ||
360 | page = pmd_page(orig_pmd); | |
12e967fd MH |
361 | |
362 | /* Do not interfere with other mappings of this page */ | |
363 | if (page_mapcount(page) != 1) | |
364 | goto huge_unlock; | |
365 | ||
9c276cc6 MK |
366 | if (next - addr != HPAGE_PMD_SIZE) { |
367 | int err; | |
368 | ||
9c276cc6 MK |
369 | get_page(page); |
370 | spin_unlock(ptl); | |
371 | lock_page(page); | |
372 | err = split_huge_page(page); | |
373 | unlock_page(page); | |
374 | put_page(page); | |
375 | if (!err) | |
376 | goto regular_page; | |
377 | return 0; | |
378 | } | |
379 | ||
380 | if (pmd_young(orig_pmd)) { | |
381 | pmdp_invalidate(vma, addr, pmd); | |
382 | orig_pmd = pmd_mkold(orig_pmd); | |
383 | ||
384 | set_pmd_at(mm, addr, pmd, orig_pmd); | |
385 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); | |
386 | } | |
387 | ||
d616d512 | 388 | ClearPageReferenced(page); |
9c276cc6 | 389 | test_and_clear_page_young(page); |
d616d512 | 390 | if (pageout) { |
82072962 | 391 | if (!isolate_lru_page(page)) { |
392 | if (PageUnevictable(page)) | |
393 | putback_lru_page(page); | |
394 | else | |
395 | list_add(&page->lru, &page_list); | |
396 | } | |
d616d512 MK |
397 | } else |
398 | deactivate_page(page); | |
9c276cc6 MK |
399 | huge_unlock: |
400 | spin_unlock(ptl); | |
d616d512 MK |
401 | if (pageout) |
402 | reclaim_pages(&page_list); | |
9c276cc6 MK |
403 | return 0; |
404 | } | |
405 | ||
ce268425 | 406 | regular_page: |
9c276cc6 MK |
407 | if (pmd_trans_unstable(pmd)) |
408 | return 0; | |
9c276cc6 MK |
409 | #endif |
410 | tlb_change_page_size(tlb, PAGE_SIZE); | |
411 | orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
412 | flush_tlb_batched_pending(mm); | |
413 | arch_enter_lazy_mmu_mode(); | |
414 | for (; addr < end; pte++, addr += PAGE_SIZE) { | |
415 | ptent = *pte; | |
416 | ||
417 | if (pte_none(ptent)) | |
418 | continue; | |
419 | ||
420 | if (!pte_present(ptent)) | |
421 | continue; | |
422 | ||
423 | page = vm_normal_page(vma, addr, ptent); | |
3218f871 | 424 | if (!page || is_zone_device_page(page)) |
9c276cc6 MK |
425 | continue; |
426 | ||
427 | /* | |
428 | * Creating a THP page is expensive so split it only if we | |
429 | * are sure it's worth. Split it if we are only owner. | |
430 | */ | |
431 | if (PageTransCompound(page)) { | |
432 | if (page_mapcount(page) != 1) | |
433 | break; | |
434 | get_page(page); | |
435 | if (!trylock_page(page)) { | |
436 | put_page(page); | |
437 | break; | |
438 | } | |
439 | pte_unmap_unlock(orig_pte, ptl); | |
440 | if (split_huge_page(page)) { | |
441 | unlock_page(page); | |
442 | put_page(page); | |
f3b9e8cc | 443 | orig_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
9c276cc6 MK |
444 | break; |
445 | } | |
446 | unlock_page(page); | |
447 | put_page(page); | |
f3b9e8cc | 448 | orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
9c276cc6 MK |
449 | pte--; |
450 | addr -= PAGE_SIZE; | |
451 | continue; | |
452 | } | |
453 | ||
58d426a7 MK |
454 | /* |
455 | * Do not interfere with other mappings of this page and | |
456 | * non-LRU page. | |
457 | */ | |
458 | if (!PageLRU(page) || page_mapcount(page) != 1) | |
12e967fd MH |
459 | continue; |
460 | ||
9c276cc6 MK |
461 | VM_BUG_ON_PAGE(PageTransCompound(page), page); |
462 | ||
463 | if (pte_young(ptent)) { | |
464 | ptent = ptep_get_and_clear_full(mm, addr, pte, | |
465 | tlb->fullmm); | |
466 | ptent = pte_mkold(ptent); | |
467 | set_pte_at(mm, addr, pte, ptent); | |
468 | tlb_remove_tlb_entry(tlb, pte, addr); | |
469 | } | |
470 | ||
471 | /* | |
472 | * We are deactivating a page for accelerating reclaiming. | |
473 | * VM couldn't reclaim the page unless we clear PG_young. | |
474 | * As a side effect, it makes confuse idle-page tracking | |
475 | * because they will miss recent referenced history. | |
476 | */ | |
d616d512 | 477 | ClearPageReferenced(page); |
9c276cc6 | 478 | test_and_clear_page_young(page); |
d616d512 | 479 | if (pageout) { |
82072962 | 480 | if (!isolate_lru_page(page)) { |
481 | if (PageUnevictable(page)) | |
482 | putback_lru_page(page); | |
483 | else | |
484 | list_add(&page->lru, &page_list); | |
485 | } | |
d616d512 MK |
486 | } else |
487 | deactivate_page(page); | |
9c276cc6 MK |
488 | } |
489 | ||
490 | arch_leave_lazy_mmu_mode(); | |
491 | pte_unmap_unlock(orig_pte, ptl); | |
d616d512 MK |
492 | if (pageout) |
493 | reclaim_pages(&page_list); | |
9c276cc6 MK |
494 | cond_resched(); |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
499 | static const struct mm_walk_ops cold_walk_ops = { | |
d616d512 | 500 | .pmd_entry = madvise_cold_or_pageout_pte_range, |
9c276cc6 MK |
501 | }; |
502 | ||
503 | static void madvise_cold_page_range(struct mmu_gather *tlb, | |
504 | struct vm_area_struct *vma, | |
505 | unsigned long addr, unsigned long end) | |
506 | { | |
d616d512 MK |
507 | struct madvise_walk_private walk_private = { |
508 | .pageout = false, | |
509 | .tlb = tlb, | |
510 | }; | |
511 | ||
9c276cc6 | 512 | tlb_start_vma(tlb, vma); |
d616d512 | 513 | walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private); |
9c276cc6 MK |
514 | tlb_end_vma(tlb, vma); |
515 | } | |
516 | ||
a213e5cf HD |
517 | static inline bool can_madv_lru_vma(struct vm_area_struct *vma) |
518 | { | |
9457056a | 519 | return !(vma->vm_flags & (VM_LOCKED|VM_PFNMAP|VM_HUGETLB)); |
a213e5cf HD |
520 | } |
521 | ||
9c276cc6 MK |
522 | static long madvise_cold(struct vm_area_struct *vma, |
523 | struct vm_area_struct **prev, | |
524 | unsigned long start_addr, unsigned long end_addr) | |
525 | { | |
526 | struct mm_struct *mm = vma->vm_mm; | |
527 | struct mmu_gather tlb; | |
528 | ||
529 | *prev = vma; | |
530 | if (!can_madv_lru_vma(vma)) | |
531 | return -EINVAL; | |
532 | ||
533 | lru_add_drain(); | |
a72afd87 | 534 | tlb_gather_mmu(&tlb, mm); |
9c276cc6 | 535 | madvise_cold_page_range(&tlb, vma, start_addr, end_addr); |
ae8eba8b | 536 | tlb_finish_mmu(&tlb); |
9c276cc6 MK |
537 | |
538 | return 0; | |
539 | } | |
540 | ||
1a4e58cc MK |
541 | static void madvise_pageout_page_range(struct mmu_gather *tlb, |
542 | struct vm_area_struct *vma, | |
543 | unsigned long addr, unsigned long end) | |
544 | { | |
d616d512 MK |
545 | struct madvise_walk_private walk_private = { |
546 | .pageout = true, | |
547 | .tlb = tlb, | |
548 | }; | |
549 | ||
1a4e58cc | 550 | tlb_start_vma(tlb, vma); |
d616d512 | 551 | walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private); |
1a4e58cc MK |
552 | tlb_end_vma(tlb, vma); |
553 | } | |
554 | ||
555 | static inline bool can_do_pageout(struct vm_area_struct *vma) | |
556 | { | |
557 | if (vma_is_anonymous(vma)) | |
558 | return true; | |
559 | if (!vma->vm_file) | |
560 | return false; | |
561 | /* | |
562 | * paging out pagecache only for non-anonymous mappings that correspond | |
563 | * to the files the calling process could (if tried) open for writing; | |
564 | * otherwise we'd be including shared non-exclusive mappings, which | |
565 | * opens a side channel. | |
566 | */ | |
21cb47be CB |
567 | return inode_owner_or_capable(&init_user_ns, |
568 | file_inode(vma->vm_file)) || | |
02f92b38 | 569 | file_permission(vma->vm_file, MAY_WRITE) == 0; |
1a4e58cc MK |
570 | } |
571 | ||
572 | static long madvise_pageout(struct vm_area_struct *vma, | |
573 | struct vm_area_struct **prev, | |
574 | unsigned long start_addr, unsigned long end_addr) | |
575 | { | |
576 | struct mm_struct *mm = vma->vm_mm; | |
577 | struct mmu_gather tlb; | |
578 | ||
579 | *prev = vma; | |
580 | if (!can_madv_lru_vma(vma)) | |
581 | return -EINVAL; | |
582 | ||
583 | if (!can_do_pageout(vma)) | |
584 | return 0; | |
585 | ||
586 | lru_add_drain(); | |
a72afd87 | 587 | tlb_gather_mmu(&tlb, mm); |
1a4e58cc | 588 | madvise_pageout_page_range(&tlb, vma, start_addr, end_addr); |
ae8eba8b | 589 | tlb_finish_mmu(&tlb); |
1a4e58cc MK |
590 | |
591 | return 0; | |
592 | } | |
593 | ||
854e9ed0 MK |
594 | static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, |
595 | unsigned long end, struct mm_walk *walk) | |
596 | ||
597 | { | |
598 | struct mmu_gather *tlb = walk->private; | |
599 | struct mm_struct *mm = tlb->mm; | |
600 | struct vm_area_struct *vma = walk->vma; | |
601 | spinlock_t *ptl; | |
602 | pte_t *orig_pte, *pte, ptent; | |
603 | struct page *page; | |
64b42bc1 | 604 | int nr_swap = 0; |
b8d3c4c3 MK |
605 | unsigned long next; |
606 | ||
607 | next = pmd_addr_end(addr, end); | |
608 | if (pmd_trans_huge(*pmd)) | |
609 | if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next)) | |
610 | goto next; | |
854e9ed0 | 611 | |
854e9ed0 MK |
612 | if (pmd_trans_unstable(pmd)) |
613 | return 0; | |
614 | ||
ed6a7935 | 615 | tlb_change_page_size(tlb, PAGE_SIZE); |
854e9ed0 | 616 | orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
3ea27719 | 617 | flush_tlb_batched_pending(mm); |
854e9ed0 MK |
618 | arch_enter_lazy_mmu_mode(); |
619 | for (; addr != end; pte++, addr += PAGE_SIZE) { | |
620 | ptent = *pte; | |
621 | ||
64b42bc1 | 622 | if (pte_none(ptent)) |
854e9ed0 | 623 | continue; |
64b42bc1 MK |
624 | /* |
625 | * If the pte has swp_entry, just clear page table to | |
626 | * prevent swap-in which is more expensive rather than | |
627 | * (page allocation + zeroing). | |
628 | */ | |
629 | if (!pte_present(ptent)) { | |
630 | swp_entry_t entry; | |
631 | ||
632 | entry = pte_to_swp_entry(ptent); | |
7b49514f ML |
633 | if (!non_swap_entry(entry)) { |
634 | nr_swap--; | |
635 | free_swap_and_cache(entry); | |
636 | pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); | |
637 | } else if (is_hwpoison_entry(entry) || | |
638 | is_swapin_error_entry(entry)) { | |
639 | pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); | |
640 | } | |
64b42bc1 MK |
641 | continue; |
642 | } | |
854e9ed0 | 643 | |
25b2995a | 644 | page = vm_normal_page(vma, addr, ptent); |
3218f871 | 645 | if (!page || is_zone_device_page(page)) |
854e9ed0 MK |
646 | continue; |
647 | ||
648 | /* | |
649 | * If pmd isn't transhuge but the page is THP and | |
650 | * is owned by only this process, split it and | |
651 | * deactivate all pages. | |
652 | */ | |
653 | if (PageTransCompound(page)) { | |
654 | if (page_mapcount(page) != 1) | |
655 | goto out; | |
656 | get_page(page); | |
657 | if (!trylock_page(page)) { | |
658 | put_page(page); | |
659 | goto out; | |
660 | } | |
661 | pte_unmap_unlock(orig_pte, ptl); | |
662 | if (split_huge_page(page)) { | |
663 | unlock_page(page); | |
664 | put_page(page); | |
f3b9e8cc | 665 | orig_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
854e9ed0 MK |
666 | goto out; |
667 | } | |
854e9ed0 | 668 | unlock_page(page); |
263630e8 | 669 | put_page(page); |
f3b9e8cc | 670 | orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
854e9ed0 MK |
671 | pte--; |
672 | addr -= PAGE_SIZE; | |
673 | continue; | |
674 | } | |
675 | ||
676 | VM_BUG_ON_PAGE(PageTransCompound(page), page); | |
677 | ||
678 | if (PageSwapCache(page) || PageDirty(page)) { | |
679 | if (!trylock_page(page)) | |
680 | continue; | |
681 | /* | |
682 | * If page is shared with others, we couldn't clear | |
683 | * PG_dirty of the page. | |
684 | */ | |
685 | if (page_mapcount(page) != 1) { | |
686 | unlock_page(page); | |
687 | continue; | |
688 | } | |
689 | ||
690 | if (PageSwapCache(page) && !try_to_free_swap(page)) { | |
691 | unlock_page(page); | |
692 | continue; | |
693 | } | |
694 | ||
695 | ClearPageDirty(page); | |
696 | unlock_page(page); | |
697 | } | |
698 | ||
699 | if (pte_young(ptent) || pte_dirty(ptent)) { | |
700 | /* | |
701 | * Some of architecture(ex, PPC) don't update TLB | |
702 | * with set_pte_at and tlb_remove_tlb_entry so for | |
703 | * the portability, remap the pte with old|clean | |
704 | * after pte clearing. | |
705 | */ | |
706 | ptent = ptep_get_and_clear_full(mm, addr, pte, | |
707 | tlb->fullmm); | |
708 | ||
709 | ptent = pte_mkold(ptent); | |
710 | ptent = pte_mkclean(ptent); | |
711 | set_pte_at(mm, addr, pte, ptent); | |
712 | tlb_remove_tlb_entry(tlb, pte, addr); | |
713 | } | |
802a3a92 | 714 | mark_page_lazyfree(page); |
854e9ed0 MK |
715 | } |
716 | out: | |
64b42bc1 MK |
717 | if (nr_swap) { |
718 | if (current->mm == mm) | |
719 | sync_mm_rss(mm); | |
720 | ||
721 | add_mm_counter(mm, MM_SWAPENTS, nr_swap); | |
722 | } | |
854e9ed0 MK |
723 | arch_leave_lazy_mmu_mode(); |
724 | pte_unmap_unlock(orig_pte, ptl); | |
725 | cond_resched(); | |
b8d3c4c3 | 726 | next: |
854e9ed0 MK |
727 | return 0; |
728 | } | |
729 | ||
7b86ac33 CH |
730 | static const struct mm_walk_ops madvise_free_walk_ops = { |
731 | .pmd_entry = madvise_free_pte_range, | |
732 | }; | |
854e9ed0 MK |
733 | |
734 | static int madvise_free_single_vma(struct vm_area_struct *vma, | |
735 | unsigned long start_addr, unsigned long end_addr) | |
736 | { | |
854e9ed0 | 737 | struct mm_struct *mm = vma->vm_mm; |
ac46d4f3 | 738 | struct mmu_notifier_range range; |
854e9ed0 MK |
739 | struct mmu_gather tlb; |
740 | ||
854e9ed0 MK |
741 | /* MADV_FREE works for only anon vma at the moment */ |
742 | if (!vma_is_anonymous(vma)) | |
743 | return -EINVAL; | |
744 | ||
ac46d4f3 JG |
745 | range.start = max(vma->vm_start, start_addr); |
746 | if (range.start >= vma->vm_end) | |
854e9ed0 | 747 | return -EINVAL; |
ac46d4f3 JG |
748 | range.end = min(vma->vm_end, end_addr); |
749 | if (range.end <= vma->vm_start) | |
854e9ed0 | 750 | return -EINVAL; |
7269f999 | 751 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, |
6f4f13e8 | 752 | range.start, range.end); |
854e9ed0 MK |
753 | |
754 | lru_add_drain(); | |
a72afd87 | 755 | tlb_gather_mmu(&tlb, mm); |
854e9ed0 MK |
756 | update_hiwater_rss(mm); |
757 | ||
ac46d4f3 | 758 | mmu_notifier_invalidate_range_start(&range); |
7b86ac33 CH |
759 | tlb_start_vma(&tlb, vma); |
760 | walk_page_range(vma->vm_mm, range.start, range.end, | |
761 | &madvise_free_walk_ops, &tlb); | |
762 | tlb_end_vma(&tlb, vma); | |
ac46d4f3 | 763 | mmu_notifier_invalidate_range_end(&range); |
ae8eba8b | 764 | tlb_finish_mmu(&tlb); |
854e9ed0 MK |
765 | |
766 | return 0; | |
767 | } | |
768 | ||
1da177e4 LT |
769 | /* |
770 | * Application no longer needs these pages. If the pages are dirty, | |
771 | * it's OK to just throw them away. The app will be more careful about | |
772 | * data it wants to keep. Be sure to free swap resources too. The | |
7e6cbea3 | 773 | * zap_page_range call sets things up for shrink_active_list to actually free |
1da177e4 LT |
774 | * these pages later if no one else has touched them in the meantime, |
775 | * although we could add these pages to a global reuse list for | |
7e6cbea3 | 776 | * shrink_active_list to pick up before reclaiming other pages. |
1da177e4 LT |
777 | * |
778 | * NB: This interface discards data rather than pushes it out to swap, | |
779 | * as some implementations do. This has performance implications for | |
780 | * applications like large transactional databases which want to discard | |
781 | * pages in anonymous maps after committing to backing store the data | |
782 | * that was kept in them. There is no reason to write this data out to | |
783 | * the swap area if the application is discarding it. | |
784 | * | |
785 | * An interface that causes the system to free clean pages and flush | |
786 | * dirty pages is already available as msync(MS_INVALIDATE). | |
787 | */ | |
230ca982 MR |
788 | static long madvise_dontneed_single_vma(struct vm_area_struct *vma, |
789 | unsigned long start, unsigned long end) | |
790 | { | |
791 | zap_page_range(vma, start, end - start); | |
792 | return 0; | |
793 | } | |
794 | ||
90e7e7f5 MK |
795 | static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma, |
796 | unsigned long start, | |
797 | unsigned long *end, | |
798 | int behavior) | |
799 | { | |
9457056a JW |
800 | if (!is_vm_hugetlb_page(vma)) { |
801 | unsigned int forbidden = VM_PFNMAP; | |
802 | ||
803 | if (behavior != MADV_DONTNEED_LOCKED) | |
804 | forbidden |= VM_LOCKED; | |
805 | ||
806 | return !(vma->vm_flags & forbidden); | |
807 | } | |
90e7e7f5 | 808 | |
9457056a | 809 | if (behavior != MADV_DONTNEED && behavior != MADV_DONTNEED_LOCKED) |
90e7e7f5 MK |
810 | return false; |
811 | if (start & ~huge_page_mask(hstate_vma(vma))) | |
812 | return false; | |
813 | ||
814 | *end = ALIGN(*end, huge_page_size(hstate_vma(vma))); | |
815 | return true; | |
816 | } | |
817 | ||
230ca982 MR |
818 | static long madvise_dontneed_free(struct vm_area_struct *vma, |
819 | struct vm_area_struct **prev, | |
820 | unsigned long start, unsigned long end, | |
821 | int behavior) | |
1da177e4 | 822 | { |
0726b01e MK |
823 | struct mm_struct *mm = vma->vm_mm; |
824 | ||
05b74384 | 825 | *prev = vma; |
90e7e7f5 | 826 | if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior)) |
1da177e4 LT |
827 | return -EINVAL; |
828 | ||
70ccb92f | 829 | if (!userfaultfd_remove(vma, start, end)) { |
c1e8d7c6 | 830 | *prev = NULL; /* mmap_lock has been dropped, prev is stale */ |
70ccb92f | 831 | |
0726b01e MK |
832 | mmap_read_lock(mm); |
833 | vma = find_vma(mm, start); | |
70ccb92f AA |
834 | if (!vma) |
835 | return -ENOMEM; | |
836 | if (start < vma->vm_start) { | |
837 | /* | |
838 | * This "vma" under revalidation is the one | |
839 | * with the lowest vma->vm_start where start | |
840 | * is also < vma->vm_end. If start < | |
841 | * vma->vm_start it means an hole materialized | |
842 | * in the user address space within the | |
230ca982 MR |
843 | * virtual range passed to MADV_DONTNEED |
844 | * or MADV_FREE. | |
70ccb92f AA |
845 | */ |
846 | return -ENOMEM; | |
847 | } | |
90e7e7f5 MK |
848 | /* |
849 | * Potential end adjustment for hugetlb vma is OK as | |
850 | * the check below keeps end within vma. | |
851 | */ | |
852 | if (!madvise_dontneed_free_valid_vma(vma, start, &end, | |
853 | behavior)) | |
70ccb92f AA |
854 | return -EINVAL; |
855 | if (end > vma->vm_end) { | |
856 | /* | |
857 | * Don't fail if end > vma->vm_end. If the old | |
f0953a1b | 858 | * vma was split while the mmap_lock was |
70ccb92f | 859 | * released the effect of the concurrent |
230ca982 | 860 | * operation may not cause madvise() to |
70ccb92f AA |
861 | * have an undefined result. There may be an |
862 | * adjacent next vma that we'll walk | |
863 | * next. userfaultfd_remove() will generate an | |
864 | * UFFD_EVENT_REMOVE repetition on the | |
865 | * end-vma->vm_end range, but the manager can | |
866 | * handle a repetition fine. | |
867 | */ | |
868 | end = vma->vm_end; | |
869 | } | |
870 | VM_WARN_ON(start >= end); | |
871 | } | |
230ca982 | 872 | |
9457056a | 873 | if (behavior == MADV_DONTNEED || behavior == MADV_DONTNEED_LOCKED) |
230ca982 MR |
874 | return madvise_dontneed_single_vma(vma, start, end); |
875 | else if (behavior == MADV_FREE) | |
876 | return madvise_free_single_vma(vma, start, end); | |
877 | else | |
878 | return -EINVAL; | |
1da177e4 LT |
879 | } |
880 | ||
4ca9b385 DH |
881 | static long madvise_populate(struct vm_area_struct *vma, |
882 | struct vm_area_struct **prev, | |
883 | unsigned long start, unsigned long end, | |
884 | int behavior) | |
885 | { | |
886 | const bool write = behavior == MADV_POPULATE_WRITE; | |
887 | struct mm_struct *mm = vma->vm_mm; | |
888 | unsigned long tmp_end; | |
889 | int locked = 1; | |
890 | long pages; | |
891 | ||
892 | *prev = vma; | |
893 | ||
894 | while (start < end) { | |
895 | /* | |
896 | * We might have temporarily dropped the lock. For example, | |
897 | * our VMA might have been split. | |
898 | */ | |
899 | if (!vma || start >= vma->vm_end) { | |
531037a0 ML |
900 | vma = vma_lookup(mm, start); |
901 | if (!vma) | |
4ca9b385 DH |
902 | return -ENOMEM; |
903 | } | |
904 | ||
905 | tmp_end = min_t(unsigned long, end, vma->vm_end); | |
906 | /* Populate (prefault) page tables readable/writable. */ | |
907 | pages = faultin_vma_page_range(vma, start, tmp_end, write, | |
908 | &locked); | |
909 | if (!locked) { | |
910 | mmap_read_lock(mm); | |
911 | locked = 1; | |
912 | *prev = NULL; | |
913 | vma = NULL; | |
914 | } | |
915 | if (pages < 0) { | |
916 | switch (pages) { | |
917 | case -EINTR: | |
918 | return -EINTR; | |
eb2faa51 | 919 | case -EINVAL: /* Incompatible mappings / permissions. */ |
4ca9b385 DH |
920 | return -EINVAL; |
921 | case -EHWPOISON: | |
922 | return -EHWPOISON; | |
eb2faa51 DH |
923 | case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */ |
924 | return -EFAULT; | |
4ca9b385 DH |
925 | default: |
926 | pr_warn_once("%s: unhandled return value: %ld\n", | |
927 | __func__, pages); | |
928 | fallthrough; | |
929 | case -ENOMEM: | |
930 | return -ENOMEM; | |
931 | } | |
932 | } | |
933 | start += pages * PAGE_SIZE; | |
934 | } | |
935 | return 0; | |
936 | } | |
937 | ||
f6b3ec23 BP |
938 | /* |
939 | * Application wants to free up the pages and associated backing store. | |
940 | * This is effectively punching a hole into the middle of a file. | |
f6b3ec23 BP |
941 | */ |
942 | static long madvise_remove(struct vm_area_struct *vma, | |
00e9fa2d | 943 | struct vm_area_struct **prev, |
f6b3ec23 BP |
944 | unsigned long start, unsigned long end) |
945 | { | |
3f31d075 | 946 | loff_t offset; |
90ed52eb | 947 | int error; |
9ab4233d | 948 | struct file *f; |
0726b01e | 949 | struct mm_struct *mm = vma->vm_mm; |
f6b3ec23 | 950 | |
c1e8d7c6 | 951 | *prev = NULL; /* tell sys_madvise we drop mmap_lock */ |
00e9fa2d | 952 | |
72079ba0 | 953 | if (vma->vm_flags & VM_LOCKED) |
f6b3ec23 BP |
954 | return -EINVAL; |
955 | ||
9ab4233d AL |
956 | f = vma->vm_file; |
957 | ||
958 | if (!f || !f->f_mapping || !f->f_mapping->host) { | |
f6b3ec23 BP |
959 | return -EINVAL; |
960 | } | |
961 | ||
69cf0fac HD |
962 | if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE)) |
963 | return -EACCES; | |
964 | ||
f6b3ec23 BP |
965 | offset = (loff_t)(start - vma->vm_start) |
966 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); | |
90ed52eb | 967 | |
9ab4233d | 968 | /* |
9608703e | 969 | * Filesystem's fallocate may need to take i_rwsem. We need to |
9ab4233d AL |
970 | * explicitly grab a reference because the vma (and hence the |
971 | * vma's reference to the file) can go away as soon as we drop | |
c1e8d7c6 | 972 | * mmap_lock. |
9ab4233d AL |
973 | */ |
974 | get_file(f); | |
70ccb92f | 975 | if (userfaultfd_remove(vma, start, end)) { |
c1e8d7c6 | 976 | /* mmap_lock was not released by userfaultfd_remove() */ |
0726b01e | 977 | mmap_read_unlock(mm); |
70ccb92f | 978 | } |
72c72bdf | 979 | error = vfs_fallocate(f, |
3f31d075 HD |
980 | FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, |
981 | offset, end - start); | |
9ab4233d | 982 | fput(f); |
0726b01e | 983 | mmap_read_lock(mm); |
90ed52eb | 984 | return error; |
f6b3ec23 BP |
985 | } |
986 | ||
ac1e9acc CC |
987 | /* |
988 | * Apply an madvise behavior to a region of a vma. madvise_update_vma | |
989 | * will handle splitting a vm area into separate areas, each area with its own | |
990 | * behavior. | |
991 | */ | |
992 | static int madvise_vma_behavior(struct vm_area_struct *vma, | |
993 | struct vm_area_struct **prev, | |
994 | unsigned long start, unsigned long end, | |
995 | unsigned long behavior) | |
996 | { | |
997 | int error; | |
942341dc | 998 | struct anon_vma_name *anon_name; |
ac1e9acc CC |
999 | unsigned long new_flags = vma->vm_flags; |
1000 | ||
1001 | switch (behavior) { | |
1002 | case MADV_REMOVE: | |
1003 | return madvise_remove(vma, prev, start, end); | |
1004 | case MADV_WILLNEED: | |
1005 | return madvise_willneed(vma, prev, start, end); | |
1006 | case MADV_COLD: | |
1007 | return madvise_cold(vma, prev, start, end); | |
1008 | case MADV_PAGEOUT: | |
1009 | return madvise_pageout(vma, prev, start, end); | |
1010 | case MADV_FREE: | |
1011 | case MADV_DONTNEED: | |
9457056a | 1012 | case MADV_DONTNEED_LOCKED: |
ac1e9acc CC |
1013 | return madvise_dontneed_free(vma, prev, start, end, behavior); |
1014 | case MADV_POPULATE_READ: | |
1015 | case MADV_POPULATE_WRITE: | |
1016 | return madvise_populate(vma, prev, start, end, behavior); | |
1017 | case MADV_NORMAL: | |
1018 | new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ; | |
1019 | break; | |
1020 | case MADV_SEQUENTIAL: | |
1021 | new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ; | |
1022 | break; | |
1023 | case MADV_RANDOM: | |
1024 | new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ; | |
1025 | break; | |
1026 | case MADV_DONTFORK: | |
1027 | new_flags |= VM_DONTCOPY; | |
1028 | break; | |
1029 | case MADV_DOFORK: | |
1030 | if (vma->vm_flags & VM_IO) | |
1031 | return -EINVAL; | |
1032 | new_flags &= ~VM_DONTCOPY; | |
1033 | break; | |
1034 | case MADV_WIPEONFORK: | |
1035 | /* MADV_WIPEONFORK is only supported on anonymous memory. */ | |
1036 | if (vma->vm_file || vma->vm_flags & VM_SHARED) | |
1037 | return -EINVAL; | |
1038 | new_flags |= VM_WIPEONFORK; | |
1039 | break; | |
1040 | case MADV_KEEPONFORK: | |
1041 | new_flags &= ~VM_WIPEONFORK; | |
1042 | break; | |
1043 | case MADV_DONTDUMP: | |
1044 | new_flags |= VM_DONTDUMP; | |
1045 | break; | |
1046 | case MADV_DODUMP: | |
1047 | if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) | |
1048 | return -EINVAL; | |
1049 | new_flags &= ~VM_DONTDUMP; | |
1050 | break; | |
1051 | case MADV_MERGEABLE: | |
1052 | case MADV_UNMERGEABLE: | |
1053 | error = ksm_madvise(vma, start, end, behavior, &new_flags); | |
1054 | if (error) | |
1055 | goto out; | |
1056 | break; | |
1057 | case MADV_HUGEPAGE: | |
1058 | case MADV_NOHUGEPAGE: | |
1059 | error = hugepage_madvise(vma, &new_flags, behavior); | |
1060 | if (error) | |
1061 | goto out; | |
1062 | break; | |
1063 | } | |
1064 | ||
942341dc SB |
1065 | anon_name = anon_vma_name(vma); |
1066 | anon_vma_name_get(anon_name); | |
9a10064f | 1067 | error = madvise_update_vma(vma, prev, start, end, new_flags, |
942341dc SB |
1068 | anon_name); |
1069 | anon_vma_name_put(anon_name); | |
ac1e9acc CC |
1070 | |
1071 | out: | |
1072 | /* | |
1073 | * madvise() returns EAGAIN if kernel resources, such as | |
1074 | * slab, are temporarily unavailable. | |
1075 | */ | |
1076 | if (error == -ENOMEM) | |
1077 | error = -EAGAIN; | |
1078 | return error; | |
1079 | } | |
1080 | ||
9893e49d AK |
1081 | #ifdef CONFIG_MEMORY_FAILURE |
1082 | /* | |
1083 | * Error injection support for memory error handling. | |
1084 | */ | |
97167a76 AK |
1085 | static int madvise_inject_error(int behavior, |
1086 | unsigned long start, unsigned long end) | |
9893e49d | 1087 | { |
d3cd257c | 1088 | unsigned long size; |
97167a76 | 1089 | |
9893e49d AK |
1090 | if (!capable(CAP_SYS_ADMIN)) |
1091 | return -EPERM; | |
97167a76 | 1092 | |
19bfbe22 | 1093 | |
d3cd257c | 1094 | for (; start < end; start += size) { |
23e7b5c2 | 1095 | unsigned long pfn; |
dc7560b4 | 1096 | struct page *page; |
325c4ef5 AM |
1097 | int ret; |
1098 | ||
97167a76 | 1099 | ret = get_user_pages_fast(start, 1, 0, &page); |
9893e49d AK |
1100 | if (ret != 1) |
1101 | return ret; | |
23e7b5c2 | 1102 | pfn = page_to_pfn(page); |
325c4ef5 | 1103 | |
19bfbe22 AM |
1104 | /* |
1105 | * When soft offlining hugepages, after migrating the page | |
1106 | * we dissolve it, therefore in the second loop "page" will | |
d3cd257c | 1107 | * no longer be a compound page. |
19bfbe22 | 1108 | */ |
d3cd257c | 1109 | size = page_size(compound_head(page)); |
19bfbe22 | 1110 | |
97167a76 AK |
1111 | if (behavior == MADV_SOFT_OFFLINE) { |
1112 | pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n", | |
dc7560b4 | 1113 | pfn, start); |
feec24a6 | 1114 | ret = soft_offline_page(pfn, MF_COUNT_INCREASED); |
dc7560b4 OS |
1115 | } else { |
1116 | pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n", | |
1117 | pfn, start); | |
67f22ba7 | 1118 | ret = memory_failure(pfn, MF_COUNT_INCREASED | MF_SW_SIMULATED); |
d1fe111f | 1119 | if (ret == -EOPNOTSUPP) |
1120 | ret = 0; | |
afcf938e | 1121 | } |
23e7b5c2 | 1122 | |
23a003bf NH |
1123 | if (ret) |
1124 | return ret; | |
9893e49d | 1125 | } |
c461ad6a | 1126 | |
325c4ef5 | 1127 | return 0; |
9893e49d AK |
1128 | } |
1129 | #endif | |
1130 | ||
1ecef9ed | 1131 | static bool |
75927af8 NP |
1132 | madvise_behavior_valid(int behavior) |
1133 | { | |
1134 | switch (behavior) { | |
1135 | case MADV_DOFORK: | |
1136 | case MADV_DONTFORK: | |
1137 | case MADV_NORMAL: | |
1138 | case MADV_SEQUENTIAL: | |
1139 | case MADV_RANDOM: | |
1140 | case MADV_REMOVE: | |
1141 | case MADV_WILLNEED: | |
1142 | case MADV_DONTNEED: | |
9457056a | 1143 | case MADV_DONTNEED_LOCKED: |
854e9ed0 | 1144 | case MADV_FREE: |
9c276cc6 | 1145 | case MADV_COLD: |
1a4e58cc | 1146 | case MADV_PAGEOUT: |
4ca9b385 DH |
1147 | case MADV_POPULATE_READ: |
1148 | case MADV_POPULATE_WRITE: | |
f8af4da3 HD |
1149 | #ifdef CONFIG_KSM |
1150 | case MADV_MERGEABLE: | |
1151 | case MADV_UNMERGEABLE: | |
0af4e98b AA |
1152 | #endif |
1153 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
1154 | case MADV_HUGEPAGE: | |
a664b2d8 | 1155 | case MADV_NOHUGEPAGE: |
f8af4da3 | 1156 | #endif |
accb61fe JB |
1157 | case MADV_DONTDUMP: |
1158 | case MADV_DODUMP: | |
d2cd9ede RR |
1159 | case MADV_WIPEONFORK: |
1160 | case MADV_KEEPONFORK: | |
5e451be7 AK |
1161 | #ifdef CONFIG_MEMORY_FAILURE |
1162 | case MADV_SOFT_OFFLINE: | |
1163 | case MADV_HWPOISON: | |
1164 | #endif | |
1ecef9ed | 1165 | return true; |
75927af8 NP |
1166 | |
1167 | default: | |
1ecef9ed | 1168 | return false; |
75927af8 NP |
1169 | } |
1170 | } | |
3866ea90 | 1171 | |
ecb8ac8b MK |
1172 | static bool |
1173 | process_madvise_behavior_valid(int behavior) | |
1174 | { | |
1175 | switch (behavior) { | |
1176 | case MADV_COLD: | |
1177 | case MADV_PAGEOUT: | |
d5fffc5a | 1178 | case MADV_WILLNEED: |
ecb8ac8b MK |
1179 | return true; |
1180 | default: | |
1181 | return false; | |
1182 | } | |
1183 | } | |
1184 | ||
ac1e9acc CC |
1185 | /* |
1186 | * Walk the vmas in range [start,end), and call the visit function on each one. | |
1187 | * The visit function will get start and end parameters that cover the overlap | |
1188 | * between the current vma and the original range. Any unmapped regions in the | |
1189 | * original range will result in this function returning -ENOMEM while still | |
1190 | * calling the visit function on all of the existing vmas in the range. | |
1191 | * Must be called with the mmap_lock held for reading or writing. | |
1192 | */ | |
1193 | static | |
1194 | int madvise_walk_vmas(struct mm_struct *mm, unsigned long start, | |
1195 | unsigned long end, unsigned long arg, | |
1196 | int (*visit)(struct vm_area_struct *vma, | |
1197 | struct vm_area_struct **prev, unsigned long start, | |
1198 | unsigned long end, unsigned long arg)) | |
1199 | { | |
1200 | struct vm_area_struct *vma; | |
1201 | struct vm_area_struct *prev; | |
1202 | unsigned long tmp; | |
1203 | int unmapped_error = 0; | |
1204 | ||
1205 | /* | |
1206 | * If the interval [start,end) covers some unmapped address | |
1207 | * ranges, just ignore them, but return -ENOMEM at the end. | |
1208 | * - different from the way of handling in mlock etc. | |
1209 | */ | |
1210 | vma = find_vma_prev(mm, start, &prev); | |
1211 | if (vma && start > vma->vm_start) | |
1212 | prev = vma; | |
1213 | ||
1214 | for (;;) { | |
1215 | int error; | |
1216 | ||
1217 | /* Still start < end. */ | |
1218 | if (!vma) | |
1219 | return -ENOMEM; | |
1220 | ||
1221 | /* Here start < (end|vma->vm_end). */ | |
1222 | if (start < vma->vm_start) { | |
1223 | unmapped_error = -ENOMEM; | |
1224 | start = vma->vm_start; | |
1225 | if (start >= end) | |
1226 | break; | |
1227 | } | |
1228 | ||
1229 | /* Here vma->vm_start <= start < (end|vma->vm_end) */ | |
1230 | tmp = vma->vm_end; | |
1231 | if (end < tmp) | |
1232 | tmp = end; | |
1233 | ||
1234 | /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ | |
1235 | error = visit(vma, &prev, start, tmp, arg); | |
1236 | if (error) | |
1237 | return error; | |
1238 | start = tmp; | |
1239 | if (prev && start < prev->vm_end) | |
1240 | start = prev->vm_end; | |
1241 | if (start >= end) | |
1242 | break; | |
1243 | if (prev) | |
1244 | vma = prev->vm_next; | |
1245 | else /* madvise_remove dropped mmap_lock */ | |
1246 | vma = find_vma(mm, start); | |
1247 | } | |
1248 | ||
1249 | return unmapped_error; | |
1250 | } | |
1251 | ||
9a10064f CC |
1252 | #ifdef CONFIG_ANON_VMA_NAME |
1253 | static int madvise_vma_anon_name(struct vm_area_struct *vma, | |
1254 | struct vm_area_struct **prev, | |
1255 | unsigned long start, unsigned long end, | |
5c26f6ac | 1256 | unsigned long anon_name) |
9a10064f CC |
1257 | { |
1258 | int error; | |
1259 | ||
1260 | /* Only anonymous mappings can be named */ | |
1261 | if (vma->vm_file) | |
1262 | return -EBADF; | |
1263 | ||
1264 | error = madvise_update_vma(vma, prev, start, end, vma->vm_flags, | |
5c26f6ac | 1265 | (struct anon_vma_name *)anon_name); |
9a10064f CC |
1266 | |
1267 | /* | |
1268 | * madvise() returns EAGAIN if kernel resources, such as | |
1269 | * slab, are temporarily unavailable. | |
1270 | */ | |
1271 | if (error == -ENOMEM) | |
1272 | error = -EAGAIN; | |
1273 | return error; | |
1274 | } | |
1275 | ||
1276 | int madvise_set_anon_name(struct mm_struct *mm, unsigned long start, | |
5c26f6ac | 1277 | unsigned long len_in, struct anon_vma_name *anon_name) |
9a10064f CC |
1278 | { |
1279 | unsigned long end; | |
1280 | unsigned long len; | |
1281 | ||
1282 | if (start & ~PAGE_MASK) | |
1283 | return -EINVAL; | |
1284 | len = (len_in + ~PAGE_MASK) & PAGE_MASK; | |
1285 | ||
1286 | /* Check to see whether len was rounded up from small -ve to zero */ | |
1287 | if (len_in && !len) | |
1288 | return -EINVAL; | |
1289 | ||
1290 | end = start + len; | |
1291 | if (end < start) | |
1292 | return -EINVAL; | |
1293 | ||
1294 | if (end == start) | |
1295 | return 0; | |
1296 | ||
5c26f6ac | 1297 | return madvise_walk_vmas(mm, start, end, (unsigned long)anon_name, |
9a10064f CC |
1298 | madvise_vma_anon_name); |
1299 | } | |
1300 | #endif /* CONFIG_ANON_VMA_NAME */ | |
1da177e4 LT |
1301 | /* |
1302 | * The madvise(2) system call. | |
1303 | * | |
1304 | * Applications can use madvise() to advise the kernel how it should | |
1305 | * handle paging I/O in this VM area. The idea is to help the kernel | |
1306 | * use appropriate read-ahead and caching techniques. The information | |
1307 | * provided is advisory only, and can be safely disregarded by the | |
1308 | * kernel without affecting the correct operation of the application. | |
1309 | * | |
1310 | * behavior values: | |
1311 | * MADV_NORMAL - the default behavior is to read clusters. This | |
1312 | * results in some read-ahead and read-behind. | |
1313 | * MADV_RANDOM - the system should read the minimum amount of data | |
1314 | * on any access, since it is unlikely that the appli- | |
1315 | * cation will need more than what it asks for. | |
1316 | * MADV_SEQUENTIAL - pages in the given range will probably be accessed | |
1317 | * once, so they can be aggressively read ahead, and | |
1318 | * can be freed soon after they are accessed. | |
1319 | * MADV_WILLNEED - the application is notifying the system to read | |
1320 | * some pages ahead. | |
1321 | * MADV_DONTNEED - the application is finished with the given range, | |
1322 | * so the kernel can free resources associated with it. | |
d7206a70 NH |
1323 | * MADV_FREE - the application marks pages in the given range as lazy free, |
1324 | * where actual purges are postponed until memory pressure happens. | |
f6b3ec23 BP |
1325 | * MADV_REMOVE - the application wants to free up the given range of |
1326 | * pages and associated backing store. | |
3866ea90 HD |
1327 | * MADV_DONTFORK - omit this area from child's address space when forking: |
1328 | * typically, to avoid COWing pages pinned by get_user_pages(). | |
1329 | * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking. | |
c02c3009 YS |
1330 | * MADV_WIPEONFORK - present the child process with zero-filled memory in this |
1331 | * range after a fork. | |
1332 | * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK | |
d7206a70 NH |
1333 | * MADV_HWPOISON - trigger memory error handler as if the given memory range |
1334 | * were corrupted by unrecoverable hardware memory failure. | |
1335 | * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory. | |
f8af4da3 HD |
1336 | * MADV_MERGEABLE - the application recommends that KSM try to merge pages in |
1337 | * this area with pages of identical content from other such areas. | |
1338 | * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others. | |
d7206a70 NH |
1339 | * MADV_HUGEPAGE - the application wants to back the given range by transparent |
1340 | * huge pages in the future. Existing pages might be coalesced and | |
1341 | * new pages might be allocated as THP. | |
1342 | * MADV_NOHUGEPAGE - mark the given range as not worth being backed by | |
1343 | * transparent huge pages so the existing pages will not be | |
1344 | * coalesced into THP and new pages will not be allocated as THP. | |
1345 | * MADV_DONTDUMP - the application wants to prevent pages in the given range | |
1346 | * from being included in its core dump. | |
1347 | * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump. | |
ecb8ac8b MK |
1348 | * MADV_COLD - the application is not expected to use this memory soon, |
1349 | * deactivate pages in this range so that they can be reclaimed | |
f0953a1b | 1350 | * easily if memory pressure happens. |
ecb8ac8b MK |
1351 | * MADV_PAGEOUT - the application is not expected to use this memory soon, |
1352 | * page out the pages in this range immediately. | |
4ca9b385 DH |
1353 | * MADV_POPULATE_READ - populate (prefault) page tables readable by |
1354 | * triggering read faults if required | |
1355 | * MADV_POPULATE_WRITE - populate (prefault) page tables writable by | |
1356 | * triggering write faults if required | |
1da177e4 LT |
1357 | * |
1358 | * return values: | |
1359 | * zero - success | |
1360 | * -EINVAL - start + len < 0, start is not page-aligned, | |
1361 | * "behavior" is not a valid value, or application | |
c02c3009 YS |
1362 | * is attempting to release locked or shared pages, |
1363 | * or the specified address range includes file, Huge TLB, | |
1364 | * MAP_SHARED or VMPFNMAP range. | |
1da177e4 LT |
1365 | * -ENOMEM - addresses in the specified range are not currently |
1366 | * mapped, or are outside the AS of the process. | |
1367 | * -EIO - an I/O error occurred while paging in data. | |
1368 | * -EBADF - map exists, but area maps something that isn't a file. | |
1369 | * -EAGAIN - a kernel resource was temporarily unavailable. | |
1370 | */ | |
0726b01e | 1371 | int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior) |
1da177e4 | 1372 | { |
ac1e9acc CC |
1373 | unsigned long end; |
1374 | int error; | |
f7977793 | 1375 | int write; |
1da177e4 | 1376 | size_t len; |
1998cc04 | 1377 | struct blk_plug plug; |
1da177e4 | 1378 | |
057d3389 AK |
1379 | start = untagged_addr(start); |
1380 | ||
75927af8 | 1381 | if (!madvise_behavior_valid(behavior)) |
ac1e9acc | 1382 | return -EINVAL; |
75927af8 | 1383 | |
df6c6500 | 1384 | if (!PAGE_ALIGNED(start)) |
ac1e9acc | 1385 | return -EINVAL; |
df6c6500 | 1386 | len = PAGE_ALIGN(len_in); |
1da177e4 LT |
1387 | |
1388 | /* Check to see whether len was rounded up from small -ve to zero */ | |
1389 | if (len_in && !len) | |
ac1e9acc | 1390 | return -EINVAL; |
1da177e4 LT |
1391 | |
1392 | end = start + len; | |
1393 | if (end < start) | |
ac1e9acc | 1394 | return -EINVAL; |
1da177e4 | 1395 | |
1da177e4 | 1396 | if (end == start) |
ac1e9acc | 1397 | return 0; |
84d96d89 | 1398 | |
5e451be7 AK |
1399 | #ifdef CONFIG_MEMORY_FAILURE |
1400 | if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE) | |
1401 | return madvise_inject_error(behavior, start, start + len_in); | |
1402 | #endif | |
1403 | ||
84d96d89 | 1404 | write = madvise_need_mmap_write(behavior); |
dc0ef0df | 1405 | if (write) { |
0726b01e | 1406 | if (mmap_write_lock_killable(mm)) |
dc0ef0df MH |
1407 | return -EINTR; |
1408 | } else { | |
0726b01e | 1409 | mmap_read_lock(mm); |
dc0ef0df | 1410 | } |
1da177e4 | 1411 | |
1998cc04 | 1412 | blk_start_plug(&plug); |
ac1e9acc CC |
1413 | error = madvise_walk_vmas(mm, start, end, behavior, |
1414 | madvise_vma_behavior); | |
84d96d89 | 1415 | blk_finish_plug(&plug); |
f7977793 | 1416 | if (write) |
0726b01e | 1417 | mmap_write_unlock(mm); |
0a27a14a | 1418 | else |
0726b01e | 1419 | mmap_read_unlock(mm); |
0a27a14a | 1420 | |
1da177e4 LT |
1421 | return error; |
1422 | } | |
db08ca25 JA |
1423 | |
1424 | SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) | |
1425 | { | |
0726b01e | 1426 | return do_madvise(current->mm, start, len_in, behavior); |
db08ca25 | 1427 | } |
ecb8ac8b MK |
1428 | |
1429 | SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, | |
1430 | size_t, vlen, int, behavior, unsigned int, flags) | |
1431 | { | |
1432 | ssize_t ret; | |
1433 | struct iovec iovstack[UIO_FASTIOV], iovec; | |
1434 | struct iovec *iov = iovstack; | |
1435 | struct iov_iter iter; | |
ecb8ac8b MK |
1436 | struct task_struct *task; |
1437 | struct mm_struct *mm; | |
1438 | size_t total_len; | |
1439 | unsigned int f_flags; | |
1440 | ||
1441 | if (flags != 0) { | |
1442 | ret = -EINVAL; | |
1443 | goto out; | |
1444 | } | |
1445 | ||
1446 | ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter); | |
1447 | if (ret < 0) | |
1448 | goto out; | |
1449 | ||
ee9955d6 CB |
1450 | task = pidfd_get_task(pidfd, &f_flags); |
1451 | if (IS_ERR(task)) { | |
1452 | ret = PTR_ERR(task); | |
ecb8ac8b MK |
1453 | goto free_iov; |
1454 | } | |
1455 | ||
a68a0262 | 1456 | if (!process_madvise_behavior_valid(behavior)) { |
ecb8ac8b MK |
1457 | ret = -EINVAL; |
1458 | goto release_task; | |
1459 | } | |
1460 | ||
96cfe2c0 SB |
1461 | /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */ |
1462 | mm = mm_access(task, PTRACE_MODE_READ_FSCREDS); | |
ecb8ac8b MK |
1463 | if (IS_ERR_OR_NULL(mm)) { |
1464 | ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH; | |
1465 | goto release_task; | |
1466 | } | |
1467 | ||
96cfe2c0 SB |
1468 | /* |
1469 | * Require CAP_SYS_NICE for influencing process performance. Note that | |
1470 | * only non-destructive hints are currently supported. | |
1471 | */ | |
1472 | if (!capable(CAP_SYS_NICE)) { | |
1473 | ret = -EPERM; | |
1474 | goto release_mm; | |
1475 | } | |
1476 | ||
ecb8ac8b MK |
1477 | total_len = iov_iter_count(&iter); |
1478 | ||
1479 | while (iov_iter_count(&iter)) { | |
1480 | iovec = iov_iter_iovec(&iter); | |
1481 | ret = do_madvise(mm, (unsigned long)iovec.iov_base, | |
1482 | iovec.iov_len, behavior); | |
e6b0a7b3 | 1483 | if (ret < 0) |
ecb8ac8b MK |
1484 | break; |
1485 | iov_iter_advance(&iter, iovec.iov_len); | |
1486 | } | |
1487 | ||
5bd009c7 | 1488 | ret = (total_len - iov_iter_count(&iter)) ? : ret; |
ecb8ac8b | 1489 | |
96cfe2c0 | 1490 | release_mm: |
ecb8ac8b | 1491 | mmput(mm); |
ecb8ac8b MK |
1492 | release_task: |
1493 | put_task_struct(task); | |
ecb8ac8b MK |
1494 | free_iov: |
1495 | kfree(iov); | |
1496 | out: | |
1497 | return ret; | |
1498 | } |