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