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