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