mm: introduce MADV_PAGEOUT
[linux-2.6-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>
f8af4da3 20#include <linux/ksm.h>
3f31d075 21#include <linux/fs.h>
9ab4233d 22#include <linux/file.h>
1998cc04 23#include <linux/blkdev.h>
66114cad 24#include <linux/backing-dev.h>
a520110e 25#include <linux/pagewalk.h>
1998cc04
SL
26#include <linux/swap.h>
27#include <linux/swapops.h>
3a4f8a0b 28#include <linux/shmem_fs.h>
854e9ed0
MK
29#include <linux/mmu_notifier.h>
30
31#include <asm/tlb.h>
1da177e4 32
23519073
KS
33#include "internal.h"
34
0a27a14a
NP
35/*
36 * Any behaviour which results in changes to the vma->vm_flags needs to
37 * take mmap_sem for writing. Others, which simply traverse vmas, need
38 * to only take it for reading.
39 */
40static int madvise_need_mmap_write(int behavior)
41{
42 switch (behavior) {
43 case MADV_REMOVE:
44 case MADV_WILLNEED:
45 case MADV_DONTNEED:
9c276cc6 46 case MADV_COLD:
1a4e58cc 47 case MADV_PAGEOUT:
854e9ed0 48 case MADV_FREE:
0a27a14a
NP
49 return 0;
50 default:
51 /* be safe, default to 1. list exceptions explicitly */
52 return 1;
53 }
54}
55
1da177e4
LT
56/*
57 * We can potentially split a vm area into separate
58 * areas, each area with its own behavior.
59 */
ec9bed9d 60static long madvise_behavior(struct vm_area_struct *vma,
05b74384
PM
61 struct vm_area_struct **prev,
62 unsigned long start, unsigned long end, int behavior)
1da177e4 63{
ec9bed9d 64 struct mm_struct *mm = vma->vm_mm;
1da177e4 65 int error = 0;
05b74384 66 pgoff_t pgoff;
3866ea90 67 unsigned long new_flags = vma->vm_flags;
e798c6e8
PM
68
69 switch (behavior) {
f8225661
MT
70 case MADV_NORMAL:
71 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
72 break;
e798c6e8 73 case MADV_SEQUENTIAL:
f8225661 74 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
e798c6e8
PM
75 break;
76 case MADV_RANDOM:
f8225661 77 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
e798c6e8 78 break;
f8225661
MT
79 case MADV_DONTFORK:
80 new_flags |= VM_DONTCOPY;
81 break;
82 case MADV_DOFORK:
3866ea90
HD
83 if (vma->vm_flags & VM_IO) {
84 error = -EINVAL;
85 goto out;
86 }
f8225661 87 new_flags &= ~VM_DONTCOPY;
e798c6e8 88 break;
d2cd9ede
RR
89 case MADV_WIPEONFORK:
90 /* MADV_WIPEONFORK is only supported on anonymous memory. */
91 if (vma->vm_file || vma->vm_flags & VM_SHARED) {
92 error = -EINVAL;
93 goto out;
94 }
95 new_flags |= VM_WIPEONFORK;
96 break;
97 case MADV_KEEPONFORK:
98 new_flags &= ~VM_WIPEONFORK;
99 break;
accb61fe 100 case MADV_DONTDUMP:
0103bd16 101 new_flags |= VM_DONTDUMP;
accb61fe
JB
102 break;
103 case MADV_DODUMP:
d41aa525 104 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
0103bd16
KK
105 error = -EINVAL;
106 goto out;
107 }
108 new_flags &= ~VM_DONTDUMP;
accb61fe 109 break;
f8af4da3
HD
110 case MADV_MERGEABLE:
111 case MADV_UNMERGEABLE:
112 error = ksm_madvise(vma, start, end, behavior, &new_flags);
f3bc0dba
MR
113 if (error)
114 goto out_convert_errno;
f8af4da3 115 break;
0af4e98b 116 case MADV_HUGEPAGE:
a664b2d8 117 case MADV_NOHUGEPAGE:
60ab3244 118 error = hugepage_madvise(vma, &new_flags, behavior);
f3bc0dba
MR
119 if (error)
120 goto out_convert_errno;
0af4e98b 121 break;
e798c6e8
PM
122 }
123
05b74384
PM
124 if (new_flags == vma->vm_flags) {
125 *prev = vma;
836d5ffd 126 goto out;
05b74384
PM
127 }
128
129 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
130 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
19a809af
AA
131 vma->vm_file, pgoff, vma_policy(vma),
132 vma->vm_userfaultfd_ctx);
05b74384
PM
133 if (*prev) {
134 vma = *prev;
135 goto success;
136 }
137
138 *prev = vma;
1da177e4
LT
139
140 if (start != vma->vm_start) {
def5efe0
DR
141 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
142 error = -ENOMEM;
1da177e4 143 goto out;
def5efe0
DR
144 }
145 error = __split_vma(mm, vma, start, 1);
f3bc0dba
MR
146 if (error)
147 goto out_convert_errno;
1da177e4
LT
148 }
149
150 if (end != vma->vm_end) {
def5efe0
DR
151 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
152 error = -ENOMEM;
1da177e4 153 goto out;
def5efe0
DR
154 }
155 error = __split_vma(mm, vma, end, 0);
f3bc0dba
MR
156 if (error)
157 goto out_convert_errno;
1da177e4
LT
158 }
159
836d5ffd 160success:
1da177e4
LT
161 /*
162 * vm_flags is protected by the mmap_sem held in write mode.
163 */
e798c6e8 164 vma->vm_flags = new_flags;
f3bc0dba
MR
165
166out_convert_errno:
167 /*
168 * madvise() returns EAGAIN if kernel resources, such as
169 * slab, are temporarily unavailable.
170 */
171 if (error == -ENOMEM)
172 error = -EAGAIN;
1da177e4 173out:
1da177e4
LT
174 return error;
175}
176
1998cc04
SL
177#ifdef CONFIG_SWAP
178static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
179 unsigned long end, struct mm_walk *walk)
180{
181 pte_t *orig_pte;
182 struct vm_area_struct *vma = walk->private;
183 unsigned long index;
184
185 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
186 return 0;
187
188 for (index = start; index != end; index += PAGE_SIZE) {
189 pte_t pte;
190 swp_entry_t entry;
191 struct page *page;
192 spinlock_t *ptl;
193
194 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
195 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
196 pte_unmap_unlock(orig_pte, ptl);
197
0661a336 198 if (pte_present(pte) || pte_none(pte))
1998cc04
SL
199 continue;
200 entry = pte_to_swp_entry(pte);
201 if (unlikely(non_swap_entry(entry)))
202 continue;
203
204 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
23955622 205 vma, index, false);
1998cc04 206 if (page)
09cbfeaf 207 put_page(page);
1998cc04
SL
208 }
209
210 return 0;
211}
212
7b86ac33
CH
213static const struct mm_walk_ops swapin_walk_ops = {
214 .pmd_entry = swapin_walk_pmd_entry,
215};
1998cc04
SL
216
217static void force_shm_swapin_readahead(struct vm_area_struct *vma,
218 unsigned long start, unsigned long end,
219 struct address_space *mapping)
220{
221 pgoff_t index;
222 struct page *page;
223 swp_entry_t swap;
224
225 for (; start < end; start += PAGE_SIZE) {
226 index = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
227
55231e5c 228 page = find_get_entry(mapping, index);
3159f943 229 if (!xa_is_value(page)) {
1998cc04 230 if (page)
09cbfeaf 231 put_page(page);
1998cc04
SL
232 continue;
233 }
234 swap = radix_to_swp_entry(page);
235 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
23955622 236 NULL, 0, false);
1998cc04 237 if (page)
09cbfeaf 238 put_page(page);
1998cc04
SL
239 }
240
241 lru_add_drain(); /* Push any new pages onto the LRU now */
242}
243#endif /* CONFIG_SWAP */
244
1da177e4
LT
245/*
246 * Schedule all required I/O operations. Do not wait for completion.
247 */
ec9bed9d
VC
248static long madvise_willneed(struct vm_area_struct *vma,
249 struct vm_area_struct **prev,
1da177e4
LT
250 unsigned long start, unsigned long end)
251{
252 struct file *file = vma->vm_file;
692fe624 253 loff_t offset;
1da177e4 254
6ea8d958 255 *prev = vma;
1998cc04 256#ifdef CONFIG_SWAP
97b713ba 257 if (!file) {
7b86ac33
CH
258 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
259 lru_add_drain(); /* Push any new pages onto the LRU now */
1998cc04
SL
260 return 0;
261 }
1998cc04 262
97b713ba 263 if (shmem_mapping(file->f_mapping)) {
97b713ba
CH
264 force_shm_swapin_readahead(vma, start, end,
265 file->f_mapping);
266 return 0;
267 }
268#else
1bef4003
S
269 if (!file)
270 return -EBADF;
97b713ba 271#endif
1bef4003 272
e748dcd0 273 if (IS_DAX(file_inode(file))) {
fe77ba6f
CO
274 /* no bad return value, but ignore advice */
275 return 0;
276 }
277
692fe624
JK
278 /*
279 * Filesystem's fadvise may need to take various locks. We need to
280 * explicitly grab a reference because the vma (and hence the
281 * vma's reference to the file) can go away as soon as we drop
282 * mmap_sem.
283 */
284 *prev = NULL; /* tell sys_madvise we drop mmap_sem */
285 get_file(file);
286 up_read(&current->mm->mmap_sem);
287 offset = (loff_t)(start - vma->vm_start)
288 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
289 vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
290 fput(file);
291 down_read(&current->mm->mmap_sem);
1da177e4
LT
292 return 0;
293}
294
9c276cc6
MK
295static int madvise_cold_pte_range(pmd_t *pmd, unsigned long addr,
296 unsigned long end, struct mm_walk *walk)
297{
298 struct mmu_gather *tlb = walk->private;
299 struct mm_struct *mm = tlb->mm;
300 struct vm_area_struct *vma = walk->vma;
301 pte_t *orig_pte, *pte, ptent;
302 spinlock_t *ptl;
303 struct page *page;
304
305#ifdef CONFIG_TRANSPARENT_HUGEPAGE
306 if (pmd_trans_huge(*pmd)) {
307 pmd_t orig_pmd;
308 unsigned long next = pmd_addr_end(addr, end);
309
310 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
311 ptl = pmd_trans_huge_lock(pmd, vma);
312 if (!ptl)
313 return 0;
314
315 orig_pmd = *pmd;
316 if (is_huge_zero_pmd(orig_pmd))
317 goto huge_unlock;
318
319 if (unlikely(!pmd_present(orig_pmd))) {
320 VM_BUG_ON(thp_migration_supported() &&
321 !is_pmd_migration_entry(orig_pmd));
322 goto huge_unlock;
323 }
324
325 page = pmd_page(orig_pmd);
326 if (next - addr != HPAGE_PMD_SIZE) {
327 int err;
328
329 if (page_mapcount(page) != 1)
330 goto huge_unlock;
331
332 get_page(page);
333 spin_unlock(ptl);
334 lock_page(page);
335 err = split_huge_page(page);
336 unlock_page(page);
337 put_page(page);
338 if (!err)
339 goto regular_page;
340 return 0;
341 }
342
343 if (pmd_young(orig_pmd)) {
344 pmdp_invalidate(vma, addr, pmd);
345 orig_pmd = pmd_mkold(orig_pmd);
346
347 set_pmd_at(mm, addr, pmd, orig_pmd);
348 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
349 }
350
351 test_and_clear_page_young(page);
352 deactivate_page(page);
353huge_unlock:
354 spin_unlock(ptl);
355 return 0;
356 }
357
358 if (pmd_trans_unstable(pmd))
359 return 0;
360regular_page:
361#endif
362 tlb_change_page_size(tlb, PAGE_SIZE);
363 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
364 flush_tlb_batched_pending(mm);
365 arch_enter_lazy_mmu_mode();
366 for (; addr < end; pte++, addr += PAGE_SIZE) {
367 ptent = *pte;
368
369 if (pte_none(ptent))
370 continue;
371
372 if (!pte_present(ptent))
373 continue;
374
375 page = vm_normal_page(vma, addr, ptent);
376 if (!page)
377 continue;
378
379 /*
380 * Creating a THP page is expensive so split it only if we
381 * are sure it's worth. Split it if we are only owner.
382 */
383 if (PageTransCompound(page)) {
384 if (page_mapcount(page) != 1)
385 break;
386 get_page(page);
387 if (!trylock_page(page)) {
388 put_page(page);
389 break;
390 }
391 pte_unmap_unlock(orig_pte, ptl);
392 if (split_huge_page(page)) {
393 unlock_page(page);
394 put_page(page);
395 pte_offset_map_lock(mm, pmd, addr, &ptl);
396 break;
397 }
398 unlock_page(page);
399 put_page(page);
400 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
401 pte--;
402 addr -= PAGE_SIZE;
403 continue;
404 }
405
406 VM_BUG_ON_PAGE(PageTransCompound(page), page);
407
408 if (pte_young(ptent)) {
409 ptent = ptep_get_and_clear_full(mm, addr, pte,
410 tlb->fullmm);
411 ptent = pte_mkold(ptent);
412 set_pte_at(mm, addr, pte, ptent);
413 tlb_remove_tlb_entry(tlb, pte, addr);
414 }
415
416 /*
417 * We are deactivating a page for accelerating reclaiming.
418 * VM couldn't reclaim the page unless we clear PG_young.
419 * As a side effect, it makes confuse idle-page tracking
420 * because they will miss recent referenced history.
421 */
422 test_and_clear_page_young(page);
423 deactivate_page(page);
424 }
425
426 arch_leave_lazy_mmu_mode();
427 pte_unmap_unlock(orig_pte, ptl);
428 cond_resched();
429
430 return 0;
431}
432
433static const struct mm_walk_ops cold_walk_ops = {
434 .pmd_entry = madvise_cold_pte_range,
435};
436
437static void madvise_cold_page_range(struct mmu_gather *tlb,
438 struct vm_area_struct *vma,
439 unsigned long addr, unsigned long end)
440{
441 tlb_start_vma(tlb, vma);
442 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, NULL);
443 tlb_end_vma(tlb, vma);
444}
445
446static long madvise_cold(struct vm_area_struct *vma,
447 struct vm_area_struct **prev,
448 unsigned long start_addr, unsigned long end_addr)
449{
450 struct mm_struct *mm = vma->vm_mm;
451 struct mmu_gather tlb;
452
453 *prev = vma;
454 if (!can_madv_lru_vma(vma))
455 return -EINVAL;
456
457 lru_add_drain();
458 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
459 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
460 tlb_finish_mmu(&tlb, start_addr, end_addr);
461
462 return 0;
463}
464
1a4e58cc
MK
465static int madvise_pageout_pte_range(pmd_t *pmd, unsigned long addr,
466 unsigned long end, struct mm_walk *walk)
467{
468 struct mmu_gather *tlb = walk->private;
469 struct mm_struct *mm = tlb->mm;
470 struct vm_area_struct *vma = walk->vma;
471 pte_t *orig_pte, *pte, ptent;
472 spinlock_t *ptl;
473 LIST_HEAD(page_list);
474 struct page *page;
475
476 if (fatal_signal_pending(current))
477 return -EINTR;
478
479#ifdef CONFIG_TRANSPARENT_HUGEPAGE
480 if (pmd_trans_huge(*pmd)) {
481 pmd_t orig_pmd;
482 unsigned long next = pmd_addr_end(addr, end);
483
484 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
485 ptl = pmd_trans_huge_lock(pmd, vma);
486 if (!ptl)
487 return 0;
488
489 orig_pmd = *pmd;
490 if (is_huge_zero_pmd(orig_pmd))
491 goto huge_unlock;
492
493 if (unlikely(!pmd_present(orig_pmd))) {
494 VM_BUG_ON(thp_migration_supported() &&
495 !is_pmd_migration_entry(orig_pmd));
496 goto huge_unlock;
497 }
498
499 page = pmd_page(orig_pmd);
500 if (next - addr != HPAGE_PMD_SIZE) {
501 int err;
502
503 if (page_mapcount(page) != 1)
504 goto huge_unlock;
505 get_page(page);
506 spin_unlock(ptl);
507 lock_page(page);
508 err = split_huge_page(page);
509 unlock_page(page);
510 put_page(page);
511 if (!err)
512 goto regular_page;
513 return 0;
514 }
515
516 if (pmd_young(orig_pmd)) {
517 pmdp_invalidate(vma, addr, pmd);
518 orig_pmd = pmd_mkold(orig_pmd);
519
520 set_pmd_at(mm, addr, pmd, orig_pmd);
521 tlb_remove_tlb_entry(tlb, pmd, addr);
522 }
523
524 ClearPageReferenced(page);
525 test_and_clear_page_young(page);
526
527 if (!isolate_lru_page(page))
528 list_add(&page->lru, &page_list);
529huge_unlock:
530 spin_unlock(ptl);
531 reclaim_pages(&page_list);
532 return 0;
533 }
534
535 if (pmd_trans_unstable(pmd))
536 return 0;
537regular_page:
538#endif
539 tlb_change_page_size(tlb, PAGE_SIZE);
540 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
541 flush_tlb_batched_pending(mm);
542 arch_enter_lazy_mmu_mode();
543 for (; addr < end; pte++, addr += PAGE_SIZE) {
544 ptent = *pte;
545 if (!pte_present(ptent))
546 continue;
547
548 page = vm_normal_page(vma, addr, ptent);
549 if (!page)
550 continue;
551
552 /*
553 * creating a THP page is expensive so split it only if we
554 * are sure it's worth. Split it if we are only owner.
555 */
556 if (PageTransCompound(page)) {
557 if (page_mapcount(page) != 1)
558 break;
559 get_page(page);
560 if (!trylock_page(page)) {
561 put_page(page);
562 break;
563 }
564 pte_unmap_unlock(orig_pte, ptl);
565 if (split_huge_page(page)) {
566 unlock_page(page);
567 put_page(page);
568 pte_offset_map_lock(mm, pmd, addr, &ptl);
569 break;
570 }
571 unlock_page(page);
572 put_page(page);
573 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
574 pte--;
575 addr -= PAGE_SIZE;
576 continue;
577 }
578
579 VM_BUG_ON_PAGE(PageTransCompound(page), page);
580
581 if (pte_young(ptent)) {
582 ptent = ptep_get_and_clear_full(mm, addr, pte,
583 tlb->fullmm);
584 ptent = pte_mkold(ptent);
585 set_pte_at(mm, addr, pte, ptent);
586 tlb_remove_tlb_entry(tlb, pte, addr);
587 }
588 ClearPageReferenced(page);
589 test_and_clear_page_young(page);
590
591 if (!isolate_lru_page(page))
592 list_add(&page->lru, &page_list);
593 }
594
595 arch_leave_lazy_mmu_mode();
596 pte_unmap_unlock(orig_pte, ptl);
597 reclaim_pages(&page_list);
598 cond_resched();
599
600 return 0;
601}
602
603static void madvise_pageout_page_range(struct mmu_gather *tlb,
604 struct vm_area_struct *vma,
605 unsigned long addr, unsigned long end)
606{
607 tlb_start_vma(tlb, vma);
608 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, NULL);
609 tlb_end_vma(tlb, vma);
610}
611
612static inline bool can_do_pageout(struct vm_area_struct *vma)
613{
614 if (vma_is_anonymous(vma))
615 return true;
616 if (!vma->vm_file)
617 return false;
618 /*
619 * paging out pagecache only for non-anonymous mappings that correspond
620 * to the files the calling process could (if tried) open for writing;
621 * otherwise we'd be including shared non-exclusive mappings, which
622 * opens a side channel.
623 */
624 return inode_owner_or_capable(file_inode(vma->vm_file)) ||
625 inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
626}
627
628static long madvise_pageout(struct vm_area_struct *vma,
629 struct vm_area_struct **prev,
630 unsigned long start_addr, unsigned long end_addr)
631{
632 struct mm_struct *mm = vma->vm_mm;
633 struct mmu_gather tlb;
634
635 *prev = vma;
636 if (!can_madv_lru_vma(vma))
637 return -EINVAL;
638
639 if (!can_do_pageout(vma))
640 return 0;
641
642 lru_add_drain();
643 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
644 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
645 tlb_finish_mmu(&tlb, start_addr, end_addr);
646
647 return 0;
648}
649
854e9ed0
MK
650static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
651 unsigned long end, struct mm_walk *walk)
652
653{
654 struct mmu_gather *tlb = walk->private;
655 struct mm_struct *mm = tlb->mm;
656 struct vm_area_struct *vma = walk->vma;
657 spinlock_t *ptl;
658 pte_t *orig_pte, *pte, ptent;
659 struct page *page;
64b42bc1 660 int nr_swap = 0;
b8d3c4c3
MK
661 unsigned long next;
662
663 next = pmd_addr_end(addr, end);
664 if (pmd_trans_huge(*pmd))
665 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
666 goto next;
854e9ed0 667
854e9ed0
MK
668 if (pmd_trans_unstable(pmd))
669 return 0;
670
ed6a7935 671 tlb_change_page_size(tlb, PAGE_SIZE);
854e9ed0 672 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
3ea27719 673 flush_tlb_batched_pending(mm);
854e9ed0
MK
674 arch_enter_lazy_mmu_mode();
675 for (; addr != end; pte++, addr += PAGE_SIZE) {
676 ptent = *pte;
677
64b42bc1 678 if (pte_none(ptent))
854e9ed0 679 continue;
64b42bc1
MK
680 /*
681 * If the pte has swp_entry, just clear page table to
682 * prevent swap-in which is more expensive rather than
683 * (page allocation + zeroing).
684 */
685 if (!pte_present(ptent)) {
686 swp_entry_t entry;
687
688 entry = pte_to_swp_entry(ptent);
689 if (non_swap_entry(entry))
690 continue;
691 nr_swap--;
692 free_swap_and_cache(entry);
693 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
694 continue;
695 }
854e9ed0 696
25b2995a 697 page = vm_normal_page(vma, addr, ptent);
854e9ed0
MK
698 if (!page)
699 continue;
700
701 /*
702 * If pmd isn't transhuge but the page is THP and
703 * is owned by only this process, split it and
704 * deactivate all pages.
705 */
706 if (PageTransCompound(page)) {
707 if (page_mapcount(page) != 1)
708 goto out;
709 get_page(page);
710 if (!trylock_page(page)) {
711 put_page(page);
712 goto out;
713 }
714 pte_unmap_unlock(orig_pte, ptl);
715 if (split_huge_page(page)) {
716 unlock_page(page);
717 put_page(page);
718 pte_offset_map_lock(mm, pmd, addr, &ptl);
719 goto out;
720 }
854e9ed0 721 unlock_page(page);
263630e8 722 put_page(page);
854e9ed0
MK
723 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
724 pte--;
725 addr -= PAGE_SIZE;
726 continue;
727 }
728
729 VM_BUG_ON_PAGE(PageTransCompound(page), page);
730
731 if (PageSwapCache(page) || PageDirty(page)) {
732 if (!trylock_page(page))
733 continue;
734 /*
735 * If page is shared with others, we couldn't clear
736 * PG_dirty of the page.
737 */
738 if (page_mapcount(page) != 1) {
739 unlock_page(page);
740 continue;
741 }
742
743 if (PageSwapCache(page) && !try_to_free_swap(page)) {
744 unlock_page(page);
745 continue;
746 }
747
748 ClearPageDirty(page);
749 unlock_page(page);
750 }
751
752 if (pte_young(ptent) || pte_dirty(ptent)) {
753 /*
754 * Some of architecture(ex, PPC) don't update TLB
755 * with set_pte_at and tlb_remove_tlb_entry so for
756 * the portability, remap the pte with old|clean
757 * after pte clearing.
758 */
759 ptent = ptep_get_and_clear_full(mm, addr, pte,
760 tlb->fullmm);
761
762 ptent = pte_mkold(ptent);
763 ptent = pte_mkclean(ptent);
764 set_pte_at(mm, addr, pte, ptent);
765 tlb_remove_tlb_entry(tlb, pte, addr);
766 }
802a3a92 767 mark_page_lazyfree(page);
854e9ed0
MK
768 }
769out:
64b42bc1
MK
770 if (nr_swap) {
771 if (current->mm == mm)
772 sync_mm_rss(mm);
773
774 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
775 }
854e9ed0
MK
776 arch_leave_lazy_mmu_mode();
777 pte_unmap_unlock(orig_pte, ptl);
778 cond_resched();
b8d3c4c3 779next:
854e9ed0
MK
780 return 0;
781}
782
7b86ac33
CH
783static const struct mm_walk_ops madvise_free_walk_ops = {
784 .pmd_entry = madvise_free_pte_range,
785};
854e9ed0
MK
786
787static int madvise_free_single_vma(struct vm_area_struct *vma,
788 unsigned long start_addr, unsigned long end_addr)
789{
854e9ed0 790 struct mm_struct *mm = vma->vm_mm;
ac46d4f3 791 struct mmu_notifier_range range;
854e9ed0
MK
792 struct mmu_gather tlb;
793
854e9ed0
MK
794 /* MADV_FREE works for only anon vma at the moment */
795 if (!vma_is_anonymous(vma))
796 return -EINVAL;
797
ac46d4f3
JG
798 range.start = max(vma->vm_start, start_addr);
799 if (range.start >= vma->vm_end)
854e9ed0 800 return -EINVAL;
ac46d4f3
JG
801 range.end = min(vma->vm_end, end_addr);
802 if (range.end <= vma->vm_start)
854e9ed0 803 return -EINVAL;
7269f999 804 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
6f4f13e8 805 range.start, range.end);
854e9ed0
MK
806
807 lru_add_drain();
ac46d4f3 808 tlb_gather_mmu(&tlb, mm, range.start, range.end);
854e9ed0
MK
809 update_hiwater_rss(mm);
810
ac46d4f3 811 mmu_notifier_invalidate_range_start(&range);
7b86ac33
CH
812 tlb_start_vma(&tlb, vma);
813 walk_page_range(vma->vm_mm, range.start, range.end,
814 &madvise_free_walk_ops, &tlb);
815 tlb_end_vma(&tlb, vma);
ac46d4f3
JG
816 mmu_notifier_invalidate_range_end(&range);
817 tlb_finish_mmu(&tlb, range.start, range.end);
854e9ed0
MK
818
819 return 0;
820}
821
1da177e4
LT
822/*
823 * Application no longer needs these pages. If the pages are dirty,
824 * it's OK to just throw them away. The app will be more careful about
825 * data it wants to keep. Be sure to free swap resources too. The
7e6cbea3 826 * zap_page_range call sets things up for shrink_active_list to actually free
1da177e4
LT
827 * these pages later if no one else has touched them in the meantime,
828 * although we could add these pages to a global reuse list for
7e6cbea3 829 * shrink_active_list to pick up before reclaiming other pages.
1da177e4
LT
830 *
831 * NB: This interface discards data rather than pushes it out to swap,
832 * as some implementations do. This has performance implications for
833 * applications like large transactional databases which want to discard
834 * pages in anonymous maps after committing to backing store the data
835 * that was kept in them. There is no reason to write this data out to
836 * the swap area if the application is discarding it.
837 *
838 * An interface that causes the system to free clean pages and flush
839 * dirty pages is already available as msync(MS_INVALIDATE).
840 */
230ca982
MR
841static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
842 unsigned long start, unsigned long end)
843{
844 zap_page_range(vma, start, end - start);
845 return 0;
846}
847
848static long madvise_dontneed_free(struct vm_area_struct *vma,
849 struct vm_area_struct **prev,
850 unsigned long start, unsigned long end,
851 int behavior)
1da177e4 852{
05b74384 853 *prev = vma;
9c276cc6 854 if (!can_madv_lru_vma(vma))
1da177e4
LT
855 return -EINVAL;
856
70ccb92f
AA
857 if (!userfaultfd_remove(vma, start, end)) {
858 *prev = NULL; /* mmap_sem has been dropped, prev is stale */
859
860 down_read(&current->mm->mmap_sem);
861 vma = find_vma(current->mm, start);
862 if (!vma)
863 return -ENOMEM;
864 if (start < vma->vm_start) {
865 /*
866 * This "vma" under revalidation is the one
867 * with the lowest vma->vm_start where start
868 * is also < vma->vm_end. If start <
869 * vma->vm_start it means an hole materialized
870 * in the user address space within the
230ca982
MR
871 * virtual range passed to MADV_DONTNEED
872 * or MADV_FREE.
70ccb92f
AA
873 */
874 return -ENOMEM;
875 }
9c276cc6 876 if (!can_madv_lru_vma(vma))
70ccb92f
AA
877 return -EINVAL;
878 if (end > vma->vm_end) {
879 /*
880 * Don't fail if end > vma->vm_end. If the old
881 * vma was splitted while the mmap_sem was
882 * released the effect of the concurrent
230ca982 883 * operation may not cause madvise() to
70ccb92f
AA
884 * have an undefined result. There may be an
885 * adjacent next vma that we'll walk
886 * next. userfaultfd_remove() will generate an
887 * UFFD_EVENT_REMOVE repetition on the
888 * end-vma->vm_end range, but the manager can
889 * handle a repetition fine.
890 */
891 end = vma->vm_end;
892 }
893 VM_WARN_ON(start >= end);
894 }
230ca982
MR
895
896 if (behavior == MADV_DONTNEED)
897 return madvise_dontneed_single_vma(vma, start, end);
898 else if (behavior == MADV_FREE)
899 return madvise_free_single_vma(vma, start, end);
900 else
901 return -EINVAL;
1da177e4
LT
902}
903
f6b3ec23
BP
904/*
905 * Application wants to free up the pages and associated backing store.
906 * This is effectively punching a hole into the middle of a file.
f6b3ec23
BP
907 */
908static long madvise_remove(struct vm_area_struct *vma,
00e9fa2d 909 struct vm_area_struct **prev,
f6b3ec23
BP
910 unsigned long start, unsigned long end)
911{
3f31d075 912 loff_t offset;
90ed52eb 913 int error;
9ab4233d 914 struct file *f;
f6b3ec23 915
90ed52eb 916 *prev = NULL; /* tell sys_madvise we drop mmap_sem */
00e9fa2d 917
72079ba0 918 if (vma->vm_flags & VM_LOCKED)
f6b3ec23
BP
919 return -EINVAL;
920
9ab4233d
AL
921 f = vma->vm_file;
922
923 if (!f || !f->f_mapping || !f->f_mapping->host) {
f6b3ec23
BP
924 return -EINVAL;
925 }
926
69cf0fac
HD
927 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
928 return -EACCES;
929
f6b3ec23
BP
930 offset = (loff_t)(start - vma->vm_start)
931 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
90ed52eb 932
9ab4233d
AL
933 /*
934 * Filesystem's fallocate may need to take i_mutex. We need to
935 * explicitly grab a reference because the vma (and hence the
936 * vma's reference to the file) can go away as soon as we drop
937 * mmap_sem.
938 */
939 get_file(f);
70ccb92f
AA
940 if (userfaultfd_remove(vma, start, end)) {
941 /* mmap_sem was not released by userfaultfd_remove() */
942 up_read(&current->mm->mmap_sem);
943 }
72c72bdf 944 error = vfs_fallocate(f,
3f31d075
HD
945 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
946 offset, end - start);
9ab4233d 947 fput(f);
0a27a14a 948 down_read(&current->mm->mmap_sem);
90ed52eb 949 return error;
f6b3ec23
BP
950}
951
9893e49d
AK
952#ifdef CONFIG_MEMORY_FAILURE
953/*
954 * Error injection support for memory error handling.
955 */
97167a76
AK
956static int madvise_inject_error(int behavior,
957 unsigned long start, unsigned long end)
9893e49d 958{
97167a76 959 struct page *page;
c461ad6a 960 struct zone *zone;
19bfbe22 961 unsigned int order;
97167a76 962
9893e49d
AK
963 if (!capable(CAP_SYS_ADMIN))
964 return -EPERM;
97167a76 965
19bfbe22
AM
966
967 for (; start < end; start += PAGE_SIZE << order) {
23e7b5c2 968 unsigned long pfn;
325c4ef5
AM
969 int ret;
970
97167a76 971 ret = get_user_pages_fast(start, 1, 0, &page);
9893e49d
AK
972 if (ret != 1)
973 return ret;
23e7b5c2 974 pfn = page_to_pfn(page);
325c4ef5 975
19bfbe22
AM
976 /*
977 * When soft offlining hugepages, after migrating the page
978 * we dissolve it, therefore in the second loop "page" will
979 * no longer be a compound page, and order will be 0.
980 */
981 order = compound_order(compound_head(page));
982
97167a76
AK
983 if (PageHWPoison(page)) {
984 put_page(page);
29b4eede
WL
985 continue;
986 }
97167a76
AK
987
988 if (behavior == MADV_SOFT_OFFLINE) {
989 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
23e7b5c2 990 pfn, start);
97167a76
AK
991
992 ret = soft_offline_page(page, MF_COUNT_INCREASED);
afcf938e 993 if (ret)
8302423b 994 return ret;
afcf938e
AK
995 continue;
996 }
23e7b5c2 997
97167a76 998 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
23e7b5c2 999 pfn, start);
97167a76 1000
23e7b5c2
DW
1001 /*
1002 * Drop the page reference taken by get_user_pages_fast(). In
1003 * the absence of MF_COUNT_INCREASED the memory_failure()
1004 * routine is responsible for pinning the page to prevent it
1005 * from being released back to the page allocator.
1006 */
1007 put_page(page);
1008 ret = memory_failure(pfn, 0);
23a003bf
NH
1009 if (ret)
1010 return ret;
9893e49d 1011 }
c461ad6a
MG
1012
1013 /* Ensure that all poisoned pages are removed from per-cpu lists */
1014 for_each_populated_zone(zone)
1015 drain_all_pages(zone);
1016
325c4ef5 1017 return 0;
9893e49d
AK
1018}
1019#endif
1020
165cd402 1021static long
1022madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
1023 unsigned long start, unsigned long end, int behavior)
1da177e4 1024{
1da177e4 1025 switch (behavior) {
f6b3ec23 1026 case MADV_REMOVE:
3866ea90 1027 return madvise_remove(vma, prev, start, end);
1da177e4 1028 case MADV_WILLNEED:
3866ea90 1029 return madvise_willneed(vma, prev, start, end);
9c276cc6
MK
1030 case MADV_COLD:
1031 return madvise_cold(vma, prev, start, end);
1a4e58cc
MK
1032 case MADV_PAGEOUT:
1033 return madvise_pageout(vma, prev, start, end);
854e9ed0 1034 case MADV_FREE:
1da177e4 1035 case MADV_DONTNEED:
230ca982 1036 return madvise_dontneed_free(vma, prev, start, end, behavior);
1da177e4 1037 default:
3866ea90 1038 return madvise_behavior(vma, prev, start, end, behavior);
1da177e4 1039 }
1da177e4
LT
1040}
1041
1ecef9ed 1042static bool
75927af8
NP
1043madvise_behavior_valid(int behavior)
1044{
1045 switch (behavior) {
1046 case MADV_DOFORK:
1047 case MADV_DONTFORK:
1048 case MADV_NORMAL:
1049 case MADV_SEQUENTIAL:
1050 case MADV_RANDOM:
1051 case MADV_REMOVE:
1052 case MADV_WILLNEED:
1053 case MADV_DONTNEED:
854e9ed0 1054 case MADV_FREE:
9c276cc6 1055 case MADV_COLD:
1a4e58cc 1056 case MADV_PAGEOUT:
f8af4da3
HD
1057#ifdef CONFIG_KSM
1058 case MADV_MERGEABLE:
1059 case MADV_UNMERGEABLE:
0af4e98b
AA
1060#endif
1061#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1062 case MADV_HUGEPAGE:
a664b2d8 1063 case MADV_NOHUGEPAGE:
f8af4da3 1064#endif
accb61fe
JB
1065 case MADV_DONTDUMP:
1066 case MADV_DODUMP:
d2cd9ede
RR
1067 case MADV_WIPEONFORK:
1068 case MADV_KEEPONFORK:
5e451be7
AK
1069#ifdef CONFIG_MEMORY_FAILURE
1070 case MADV_SOFT_OFFLINE:
1071 case MADV_HWPOISON:
1072#endif
1ecef9ed 1073 return true;
75927af8
NP
1074
1075 default:
1ecef9ed 1076 return false;
75927af8
NP
1077 }
1078}
3866ea90 1079
1da177e4
LT
1080/*
1081 * The madvise(2) system call.
1082 *
1083 * Applications can use madvise() to advise the kernel how it should
1084 * handle paging I/O in this VM area. The idea is to help the kernel
1085 * use appropriate read-ahead and caching techniques. The information
1086 * provided is advisory only, and can be safely disregarded by the
1087 * kernel without affecting the correct operation of the application.
1088 *
1089 * behavior values:
1090 * MADV_NORMAL - the default behavior is to read clusters. This
1091 * results in some read-ahead and read-behind.
1092 * MADV_RANDOM - the system should read the minimum amount of data
1093 * on any access, since it is unlikely that the appli-
1094 * cation will need more than what it asks for.
1095 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1096 * once, so they can be aggressively read ahead, and
1097 * can be freed soon after they are accessed.
1098 * MADV_WILLNEED - the application is notifying the system to read
1099 * some pages ahead.
1100 * MADV_DONTNEED - the application is finished with the given range,
1101 * so the kernel can free resources associated with it.
d7206a70
NH
1102 * MADV_FREE - the application marks pages in the given range as lazy free,
1103 * where actual purges are postponed until memory pressure happens.
f6b3ec23
BP
1104 * MADV_REMOVE - the application wants to free up the given range of
1105 * pages and associated backing store.
3866ea90
HD
1106 * MADV_DONTFORK - omit this area from child's address space when forking:
1107 * typically, to avoid COWing pages pinned by get_user_pages().
1108 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
c02c3009
YS
1109 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1110 * range after a fork.
1111 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
d7206a70
NH
1112 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1113 * were corrupted by unrecoverable hardware memory failure.
1114 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
f8af4da3
HD
1115 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1116 * this area with pages of identical content from other such areas.
1117 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
d7206a70
NH
1118 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1119 * huge pages in the future. Existing pages might be coalesced and
1120 * new pages might be allocated as THP.
1121 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1122 * transparent huge pages so the existing pages will not be
1123 * coalesced into THP and new pages will not be allocated as THP.
1124 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1125 * from being included in its core dump.
1126 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1da177e4
LT
1127 *
1128 * return values:
1129 * zero - success
1130 * -EINVAL - start + len < 0, start is not page-aligned,
1131 * "behavior" is not a valid value, or application
c02c3009
YS
1132 * is attempting to release locked or shared pages,
1133 * or the specified address range includes file, Huge TLB,
1134 * MAP_SHARED or VMPFNMAP range.
1da177e4
LT
1135 * -ENOMEM - addresses in the specified range are not currently
1136 * mapped, or are outside the AS of the process.
1137 * -EIO - an I/O error occurred while paging in data.
1138 * -EBADF - map exists, but area maps something that isn't a file.
1139 * -EAGAIN - a kernel resource was temporarily unavailable.
1140 */
3480b257 1141SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1da177e4 1142{
05b74384 1143 unsigned long end, tmp;
ec9bed9d 1144 struct vm_area_struct *vma, *prev;
1da177e4
LT
1145 int unmapped_error = 0;
1146 int error = -EINVAL;
f7977793 1147 int write;
1da177e4 1148 size_t len;
1998cc04 1149 struct blk_plug plug;
1da177e4 1150
057d3389
AK
1151 start = untagged_addr(start);
1152
75927af8
NP
1153 if (!madvise_behavior_valid(behavior))
1154 return error;
1155
1da177e4 1156 if (start & ~PAGE_MASK)
84d96d89 1157 return error;
1da177e4
LT
1158 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1159
1160 /* Check to see whether len was rounded up from small -ve to zero */
1161 if (len_in && !len)
84d96d89 1162 return error;
1da177e4
LT
1163
1164 end = start + len;
1165 if (end < start)
84d96d89 1166 return error;
1da177e4
LT
1167
1168 error = 0;
1169 if (end == start)
84d96d89
RV
1170 return error;
1171
5e451be7
AK
1172#ifdef CONFIG_MEMORY_FAILURE
1173 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1174 return madvise_inject_error(behavior, start, start + len_in);
1175#endif
1176
84d96d89 1177 write = madvise_need_mmap_write(behavior);
dc0ef0df
MH
1178 if (write) {
1179 if (down_write_killable(&current->mm->mmap_sem))
1180 return -EINTR;
1181 } else {
84d96d89 1182 down_read(&current->mm->mmap_sem);
dc0ef0df 1183 }
1da177e4
LT
1184
1185 /*
1186 * If the interval [start,end) covers some unmapped address
1187 * ranges, just ignore them, but return -ENOMEM at the end.
05b74384 1188 * - different from the way of handling in mlock etc.
1da177e4 1189 */
05b74384 1190 vma = find_vma_prev(current->mm, start, &prev);
836d5ffd
HD
1191 if (vma && start > vma->vm_start)
1192 prev = vma;
1193
1998cc04 1194 blk_start_plug(&plug);
1da177e4
LT
1195 for (;;) {
1196 /* Still start < end. */
1197 error = -ENOMEM;
1198 if (!vma)
84d96d89 1199 goto out;
1da177e4 1200
05b74384 1201 /* Here start < (end|vma->vm_end). */
1da177e4
LT
1202 if (start < vma->vm_start) {
1203 unmapped_error = -ENOMEM;
1204 start = vma->vm_start;
05b74384 1205 if (start >= end)
84d96d89 1206 goto out;
1da177e4
LT
1207 }
1208
05b74384
PM
1209 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1210 tmp = vma->vm_end;
1211 if (end < tmp)
1212 tmp = end;
1da177e4 1213
05b74384
PM
1214 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1215 error = madvise_vma(vma, &prev, start, tmp, behavior);
1da177e4 1216 if (error)
84d96d89 1217 goto out;
05b74384 1218 start = tmp;
90ed52eb 1219 if (prev && start < prev->vm_end)
05b74384
PM
1220 start = prev->vm_end;
1221 error = unmapped_error;
1222 if (start >= end)
84d96d89 1223 goto out;
90ed52eb
HD
1224 if (prev)
1225 vma = prev->vm_next;
1226 else /* madvise_remove dropped mmap_sem */
1227 vma = find_vma(current->mm, start);
1da177e4 1228 }
1da177e4 1229out:
84d96d89 1230 blk_finish_plug(&plug);
f7977793 1231 if (write)
0a27a14a
NP
1232 up_write(&current->mm->mmap_sem);
1233 else
1234 up_read(&current->mm->mmap_sem);
1235
1da177e4
LT
1236 return error;
1237}