blob: 0039bfe4ca9fb5a82f96ca3e3e61cff43ad6e06c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Prasanna Meda05b74382005-06-21 17:14:37 -070012#include <linux/mempolicy.h>
Andi Kleenafcf9382009-12-16 12:20:00 +010013#include <linux/page-isolation.h>
Minchan Kim9c276cc2019-09-25 16:49:08 -070014#include <linux/page_idle.h>
Pavel Emelyanov05ce7722017-02-22 15:42:40 -080015#include <linux/userfaultfd_k.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/hugetlb.h>
Hugh Dickins3f31d072012-05-29 15:06:40 -070017#include <linux/falloc.h>
Jan Kara692fe622019-08-29 09:04:11 -070018#include <linux/fadvise.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040019#include <linux/sched.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070020#include <linux/ksm.h>
Hugh Dickins3f31d072012-05-29 15:06:40 -070021#include <linux/fs.h>
Andy Lutomirski9ab42332012-07-05 16:00:11 -070022#include <linux/file.h>
Shaohua Li1998cc02013-02-22 16:32:31 -080023#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040024#include <linux/backing-dev.h>
Christoph Hellwiga5201102019-08-28 16:19:53 +020025#include <linux/pagewalk.h>
Shaohua Li1998cc02013-02-22 16:32:31 -080026#include <linux/swap.h>
27#include <linux/swapops.h>
Hugh Dickins3a4f8a02017-02-24 14:59:36 -080028#include <linux/shmem_fs.h>
Minchan Kim854e9ed2016-01-15 16:54:53 -080029#include <linux/mmu_notifier.h>
Linus Torvaldsbc0c4d12020-04-24 11:10:58 -070030#include <linux/sched/mm.h>
Minchan Kim854e9ed2016-01-15 16:54:53 -080031
32#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Kirill A. Shutemov23519072017-02-22 15:46:39 -080034#include "internal.h"
35
Minchan Kimd616d512019-09-25 16:49:19 -070036struct madvise_walk_private {
37 struct mmu_gather *tlb;
38 bool pageout;
39};
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
Nick Piggin0a27a142007-05-06 14:49:53 -070042 * Any behaviour which results in changes to the vma->vm_flags needs to
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070043 * take mmap_lock for writing. Others, which simply traverse vmas, need
Nick Piggin0a27a142007-05-06 14:49:53 -070044 * to only take it for reading.
45 */
46static int madvise_need_mmap_write(int behavior)
47{
48 switch (behavior) {
49 case MADV_REMOVE:
50 case MADV_WILLNEED:
51 case MADV_DONTNEED:
Minchan Kim9c276cc2019-09-25 16:49:08 -070052 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -070053 case MADV_PAGEOUT:
Minchan Kim854e9ed2016-01-15 16:54:53 -080054 case MADV_FREE:
Nick Piggin0a27a142007-05-06 14:49:53 -070055 return 0;
56 default:
57 /* be safe, default to 1. list exceptions explicitly */
58 return 1;
59 }
60}
61
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * We can potentially split a vm area into separate
64 * areas, each area with its own behavior.
65 */
Vladimir Cernovec9bed92013-09-11 14:20:15 -070066static long madvise_behavior(struct vm_area_struct *vma,
Prasanna Meda05b74382005-06-21 17:14:37 -070067 struct vm_area_struct **prev,
68 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Vladimir Cernovec9bed92013-09-11 14:20:15 -070070 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int error = 0;
Prasanna Meda05b74382005-06-21 17:14:37 -070072 pgoff_t pgoff;
Hugh Dickins3866ea92009-09-21 17:01:52 -070073 unsigned long new_flags = vma->vm_flags;
Prasanna Medae798c6e2005-06-21 17:14:36 -070074
75 switch (behavior) {
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080076 case MADV_NORMAL:
77 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
78 break;
Prasanna Medae798c6e2005-06-21 17:14:36 -070079 case MADV_SEQUENTIAL:
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080080 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
Prasanna Medae798c6e2005-06-21 17:14:36 -070081 break;
82 case MADV_RANDOM:
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080083 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
Prasanna Medae798c6e2005-06-21 17:14:36 -070084 break;
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080085 case MADV_DONTFORK:
86 new_flags |= VM_DONTCOPY;
87 break;
88 case MADV_DOFORK:
Hugh Dickins3866ea92009-09-21 17:01:52 -070089 if (vma->vm_flags & VM_IO) {
90 error = -EINVAL;
91 goto out;
92 }
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080093 new_flags &= ~VM_DONTCOPY;
Prasanna Medae798c6e2005-06-21 17:14:36 -070094 break;
Rik van Rield2cd9ed2017-09-06 16:25:15 -070095 case MADV_WIPEONFORK:
96 /* MADV_WIPEONFORK is only supported on anonymous memory. */
97 if (vma->vm_file || vma->vm_flags & VM_SHARED) {
98 error = -EINVAL;
99 goto out;
100 }
101 new_flags |= VM_WIPEONFORK;
102 break;
103 case MADV_KEEPONFORK:
104 new_flags &= ~VM_WIPEONFORK;
105 break;
Jason Baronaccb61f2012-03-23 15:02:51 -0700106 case MADV_DONTDUMP:
Konstantin Khlebnikov0103bd12012-10-08 16:28:59 -0700107 new_flags |= VM_DONTDUMP;
Jason Baronaccb61f2012-03-23 15:02:51 -0700108 break;
109 case MADV_DODUMP:
Daniel Blackd41aa522018-10-05 15:52:19 -0700110 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
Konstantin Khlebnikov0103bd12012-10-08 16:28:59 -0700111 error = -EINVAL;
112 goto out;
113 }
114 new_flags &= ~VM_DONTDUMP;
Jason Baronaccb61f2012-03-23 15:02:51 -0700115 break;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700116 case MADV_MERGEABLE:
117 case MADV_UNMERGEABLE:
118 error = ksm_madvise(vma, start, end, behavior, &new_flags);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700119 if (error)
120 goto out_convert_errno;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700121 break;
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800122 case MADV_HUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -0800123 case MADV_NOHUGEPAGE:
Andrea Arcangeli60ab3242011-01-13 15:47:18 -0800124 error = hugepage_madvise(vma, &new_flags, behavior);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700125 if (error)
126 goto out_convert_errno;
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800127 break;
Prasanna Medae798c6e2005-06-21 17:14:36 -0700128 }
129
Prasanna Meda05b74382005-06-21 17:14:37 -0700130 if (new_flags == vma->vm_flags) {
131 *prev = vma;
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700132 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -0700133 }
134
135 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
136 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700137 vma->vm_file, pgoff, vma_policy(vma),
138 vma->vm_userfaultfd_ctx);
Prasanna Meda05b74382005-06-21 17:14:37 -0700139 if (*prev) {
140 vma = *prev;
141 goto success;
142 }
143
144 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (start != vma->vm_start) {
David Rientjesdef5efe2017-02-24 14:58:47 -0800147 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
148 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out;
David Rientjesdef5efe2017-02-24 14:58:47 -0800150 }
151 error = __split_vma(mm, vma, start, 1);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700152 if (error)
153 goto out_convert_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
156 if (end != vma->vm_end) {
David Rientjesdef5efe2017-02-24 14:58:47 -0800157 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
158 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
David Rientjesdef5efe2017-02-24 14:58:47 -0800160 }
161 error = __split_vma(mm, vma, end, 0);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700162 if (error)
163 goto out_convert_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700166success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700168 * vm_flags is protected by the mmap_lock held in write mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Prasanna Medae798c6e2005-06-21 17:14:36 -0700170 vma->vm_flags = new_flags;
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700171
172out_convert_errno:
173 /*
174 * madvise() returns EAGAIN if kernel resources, such as
175 * slab, are temporarily unavailable.
176 */
177 if (error == -ENOMEM)
178 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return error;
181}
182
Shaohua Li1998cc02013-02-22 16:32:31 -0800183#ifdef CONFIG_SWAP
184static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
185 unsigned long end, struct mm_walk *walk)
186{
187 pte_t *orig_pte;
188 struct vm_area_struct *vma = walk->private;
189 unsigned long index;
190
191 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
192 return 0;
193
194 for (index = start; index != end; index += PAGE_SIZE) {
195 pte_t pte;
196 swp_entry_t entry;
197 struct page *page;
198 spinlock_t *ptl;
199
200 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
201 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
202 pte_unmap_unlock(orig_pte, ptl);
203
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800204 if (pte_present(pte) || pte_none(pte))
Shaohua Li1998cc02013-02-22 16:32:31 -0800205 continue;
206 entry = pte_to_swp_entry(pte);
207 if (unlikely(non_swap_entry(entry)))
208 continue;
209
210 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
Shaohua Li23955622017-07-10 15:47:11 -0700211 vma, index, false);
Shaohua Li1998cc02013-02-22 16:32:31 -0800212 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 put_page(page);
Shaohua Li1998cc02013-02-22 16:32:31 -0800214 }
215
216 return 0;
217}
218
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200219static const struct mm_walk_ops swapin_walk_ops = {
220 .pmd_entry = swapin_walk_pmd_entry,
221};
Shaohua Li1998cc02013-02-22 16:32:31 -0800222
223static void force_shm_swapin_readahead(struct vm_area_struct *vma,
224 unsigned long start, unsigned long end,
225 struct address_space *mapping)
226{
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700227 XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
228 pgoff_t end_index = end / PAGE_SIZE;
Shaohua Li1998cc02013-02-22 16:32:31 -0800229 struct page *page;
Shaohua Li1998cc02013-02-22 16:32:31 -0800230
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700231 rcu_read_lock();
232 xas_for_each(&xas, page, end_index) {
233 swp_entry_t swap;
Shaohua Li1998cc02013-02-22 16:32:31 -0800234
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700235 if (!xa_is_value(page))
Shaohua Li1998cc02013-02-22 16:32:31 -0800236 continue;
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700237 xas_pause(&xas);
238 rcu_read_unlock();
239
Shaohua Li1998cc02013-02-22 16:32:31 -0800240 swap = radix_to_swp_entry(page);
241 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
Shaohua Li23955622017-07-10 15:47:11 -0700242 NULL, 0, false);
Shaohua Li1998cc02013-02-22 16:32:31 -0800243 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 put_page(page);
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700245
246 rcu_read_lock();
Shaohua Li1998cc02013-02-22 16:32:31 -0800247 }
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700248 rcu_read_unlock();
Shaohua Li1998cc02013-02-22 16:32:31 -0800249
250 lru_add_drain(); /* Push any new pages onto the LRU now */
251}
252#endif /* CONFIG_SWAP */
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
255 * Schedule all required I/O operations. Do not wait for completion.
256 */
Vladimir Cernovec9bed92013-09-11 14:20:15 -0700257static long madvise_willneed(struct vm_area_struct *vma,
258 struct vm_area_struct **prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long start, unsigned long end)
260{
261 struct file *file = vma->vm_file;
Jan Kara692fe622019-08-29 09:04:11 -0700262 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
chenjie6ea8d952017-11-29 16:10:54 -0800264 *prev = vma;
Shaohua Li1998cc02013-02-22 16:32:31 -0800265#ifdef CONFIG_SWAP
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100266 if (!file) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200267 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
268 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li1998cc02013-02-22 16:32:31 -0800269 return 0;
270 }
Shaohua Li1998cc02013-02-22 16:32:31 -0800271
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100272 if (shmem_mapping(file->f_mapping)) {
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100273 force_shm_swapin_readahead(vma, start, end,
274 file->f_mapping);
275 return 0;
276 }
277#else
Suzuki1bef4002005-10-11 08:29:06 -0700278 if (!file)
279 return -EBADF;
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100280#endif
Suzuki1bef4002005-10-11 08:29:06 -0700281
Matthew Wilcoxe748dcd2015-02-16 15:59:12 -0800282 if (IS_DAX(file_inode(file))) {
Carsten Ottefe77ba62005-06-23 22:05:29 -0700283 /* no bad return value, but ignore advice */
284 return 0;
285 }
286
Jan Kara692fe622019-08-29 09:04:11 -0700287 /*
288 * Filesystem's fadvise may need to take various locks. We need to
289 * explicitly grab a reference because the vma (and hence the
290 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700291 * mmap_lock.
Jan Kara692fe622019-08-29 09:04:11 -0700292 */
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700293 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Jan Kara692fe622019-08-29 09:04:11 -0700294 get_file(file);
Jan Kara692fe622019-08-29 09:04:11 -0700295 offset = (loff_t)(start - vma->vm_start)
296 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Yang Shi7867fd72020-09-04 16:35:55 -0700297 mmap_read_unlock(current->mm);
Jan Kara692fe622019-08-29 09:04:11 -0700298 vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
299 fput(file);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700300 mmap_read_lock(current->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302}
303
Minchan Kimd616d512019-09-25 16:49:19 -0700304static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
305 unsigned long addr, unsigned long end,
306 struct mm_walk *walk)
Minchan Kim9c276cc2019-09-25 16:49:08 -0700307{
Minchan Kimd616d512019-09-25 16:49:19 -0700308 struct madvise_walk_private *private = walk->private;
309 struct mmu_gather *tlb = private->tlb;
310 bool pageout = private->pageout;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700311 struct mm_struct *mm = tlb->mm;
312 struct vm_area_struct *vma = walk->vma;
313 pte_t *orig_pte, *pte, ptent;
314 spinlock_t *ptl;
Minchan Kimd616d512019-09-25 16:49:19 -0700315 struct page *page = NULL;
316 LIST_HEAD(page_list);
317
318 if (fatal_signal_pending(current))
319 return -EINTR;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700320
321#ifdef CONFIG_TRANSPARENT_HUGEPAGE
322 if (pmd_trans_huge(*pmd)) {
323 pmd_t orig_pmd;
324 unsigned long next = pmd_addr_end(addr, end);
325
326 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
327 ptl = pmd_trans_huge_lock(pmd, vma);
328 if (!ptl)
329 return 0;
330
331 orig_pmd = *pmd;
332 if (is_huge_zero_pmd(orig_pmd))
333 goto huge_unlock;
334
335 if (unlikely(!pmd_present(orig_pmd))) {
336 VM_BUG_ON(thp_migration_supported() &&
337 !is_pmd_migration_entry(orig_pmd));
338 goto huge_unlock;
339 }
340
341 page = pmd_page(orig_pmd);
Michal Hocko12e967f2020-03-21 18:22:26 -0700342
343 /* Do not interfere with other mappings of this page */
344 if (page_mapcount(page) != 1)
345 goto huge_unlock;
346
Minchan Kim9c276cc2019-09-25 16:49:08 -0700347 if (next - addr != HPAGE_PMD_SIZE) {
348 int err;
349
Minchan Kim9c276cc2019-09-25 16:49:08 -0700350 get_page(page);
351 spin_unlock(ptl);
352 lock_page(page);
353 err = split_huge_page(page);
354 unlock_page(page);
355 put_page(page);
356 if (!err)
357 goto regular_page;
358 return 0;
359 }
360
361 if (pmd_young(orig_pmd)) {
362 pmdp_invalidate(vma, addr, pmd);
363 orig_pmd = pmd_mkold(orig_pmd);
364
365 set_pmd_at(mm, addr, pmd, orig_pmd);
366 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
367 }
368
Minchan Kimd616d512019-09-25 16:49:19 -0700369 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700370 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700371 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800372 if (!isolate_lru_page(page)) {
373 if (PageUnevictable(page))
374 putback_lru_page(page);
375 else
376 list_add(&page->lru, &page_list);
377 }
Minchan Kimd616d512019-09-25 16:49:19 -0700378 } else
379 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700380huge_unlock:
381 spin_unlock(ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700382 if (pageout)
383 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700384 return 0;
385 }
386
Minchan Kimce268422020-09-14 23:32:15 -0700387regular_page:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700388 if (pmd_trans_unstable(pmd))
389 return 0;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700390#endif
391 tlb_change_page_size(tlb, PAGE_SIZE);
392 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
393 flush_tlb_batched_pending(mm);
394 arch_enter_lazy_mmu_mode();
395 for (; addr < end; pte++, addr += PAGE_SIZE) {
396 ptent = *pte;
397
398 if (pte_none(ptent))
399 continue;
400
401 if (!pte_present(ptent))
402 continue;
403
404 page = vm_normal_page(vma, addr, ptent);
405 if (!page)
406 continue;
407
408 /*
409 * Creating a THP page is expensive so split it only if we
410 * are sure it's worth. Split it if we are only owner.
411 */
412 if (PageTransCompound(page)) {
413 if (page_mapcount(page) != 1)
414 break;
415 get_page(page);
416 if (!trylock_page(page)) {
417 put_page(page);
418 break;
419 }
420 pte_unmap_unlock(orig_pte, ptl);
421 if (split_huge_page(page)) {
422 unlock_page(page);
423 put_page(page);
424 pte_offset_map_lock(mm, pmd, addr, &ptl);
425 break;
426 }
427 unlock_page(page);
428 put_page(page);
429 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
430 pte--;
431 addr -= PAGE_SIZE;
432 continue;
433 }
434
Michal Hocko12e967f2020-03-21 18:22:26 -0700435 /* Do not interfere with other mappings of this page */
436 if (page_mapcount(page) != 1)
437 continue;
438
Minchan Kim9c276cc2019-09-25 16:49:08 -0700439 VM_BUG_ON_PAGE(PageTransCompound(page), page);
440
441 if (pte_young(ptent)) {
442 ptent = ptep_get_and_clear_full(mm, addr, pte,
443 tlb->fullmm);
444 ptent = pte_mkold(ptent);
445 set_pte_at(mm, addr, pte, ptent);
446 tlb_remove_tlb_entry(tlb, pte, addr);
447 }
448
449 /*
450 * We are deactivating a page for accelerating reclaiming.
451 * VM couldn't reclaim the page unless we clear PG_young.
452 * As a side effect, it makes confuse idle-page tracking
453 * because they will miss recent referenced history.
454 */
Minchan Kimd616d512019-09-25 16:49:19 -0700455 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700456 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700457 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800458 if (!isolate_lru_page(page)) {
459 if (PageUnevictable(page))
460 putback_lru_page(page);
461 else
462 list_add(&page->lru, &page_list);
463 }
Minchan Kimd616d512019-09-25 16:49:19 -0700464 } else
465 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700466 }
467
468 arch_leave_lazy_mmu_mode();
469 pte_unmap_unlock(orig_pte, ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700470 if (pageout)
471 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700472 cond_resched();
473
474 return 0;
475}
476
477static const struct mm_walk_ops cold_walk_ops = {
Minchan Kimd616d512019-09-25 16:49:19 -0700478 .pmd_entry = madvise_cold_or_pageout_pte_range,
Minchan Kim9c276cc2019-09-25 16:49:08 -0700479};
480
481static void madvise_cold_page_range(struct mmu_gather *tlb,
482 struct vm_area_struct *vma,
483 unsigned long addr, unsigned long end)
484{
Minchan Kimd616d512019-09-25 16:49:19 -0700485 struct madvise_walk_private walk_private = {
486 .pageout = false,
487 .tlb = tlb,
488 };
489
Minchan Kim9c276cc2019-09-25 16:49:08 -0700490 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700491 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700492 tlb_end_vma(tlb, vma);
493}
494
495static long madvise_cold(struct vm_area_struct *vma,
496 struct vm_area_struct **prev,
497 unsigned long start_addr, unsigned long end_addr)
498{
499 struct mm_struct *mm = vma->vm_mm;
500 struct mmu_gather tlb;
501
502 *prev = vma;
503 if (!can_madv_lru_vma(vma))
504 return -EINVAL;
505
506 lru_add_drain();
507 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
508 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
509 tlb_finish_mmu(&tlb, start_addr, end_addr);
510
511 return 0;
512}
513
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700514static void madvise_pageout_page_range(struct mmu_gather *tlb,
515 struct vm_area_struct *vma,
516 unsigned long addr, unsigned long end)
517{
Minchan Kimd616d512019-09-25 16:49:19 -0700518 struct madvise_walk_private walk_private = {
519 .pageout = true,
520 .tlb = tlb,
521 };
522
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700523 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700524 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700525 tlb_end_vma(tlb, vma);
526}
527
528static inline bool can_do_pageout(struct vm_area_struct *vma)
529{
530 if (vma_is_anonymous(vma))
531 return true;
532 if (!vma->vm_file)
533 return false;
534 /*
535 * paging out pagecache only for non-anonymous mappings that correspond
536 * to the files the calling process could (if tried) open for writing;
537 * otherwise we'd be including shared non-exclusive mappings, which
538 * opens a side channel.
539 */
540 return inode_owner_or_capable(file_inode(vma->vm_file)) ||
541 inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
542}
543
544static long madvise_pageout(struct vm_area_struct *vma,
545 struct vm_area_struct **prev,
546 unsigned long start_addr, unsigned long end_addr)
547{
548 struct mm_struct *mm = vma->vm_mm;
549 struct mmu_gather tlb;
550
551 *prev = vma;
552 if (!can_madv_lru_vma(vma))
553 return -EINVAL;
554
555 if (!can_do_pageout(vma))
556 return 0;
557
558 lru_add_drain();
559 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
560 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
561 tlb_finish_mmu(&tlb, start_addr, end_addr);
562
563 return 0;
564}
565
Minchan Kim854e9ed2016-01-15 16:54:53 -0800566static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
567 unsigned long end, struct mm_walk *walk)
568
569{
570 struct mmu_gather *tlb = walk->private;
571 struct mm_struct *mm = tlb->mm;
572 struct vm_area_struct *vma = walk->vma;
573 spinlock_t *ptl;
574 pte_t *orig_pte, *pte, ptent;
575 struct page *page;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800576 int nr_swap = 0;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800577 unsigned long next;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800578
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800579 next = pmd_addr_end(addr, end);
580 if (pmd_trans_huge(*pmd))
581 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
582 goto next;
583
Minchan Kim854e9ed2016-01-15 16:54:53 -0800584 if (pmd_trans_unstable(pmd))
585 return 0;
586
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200587 tlb_change_page_size(tlb, PAGE_SIZE);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800588 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Mel Gorman3ea27712017-08-02 13:31:52 -0700589 flush_tlb_batched_pending(mm);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800590 arch_enter_lazy_mmu_mode();
591 for (; addr != end; pte++, addr += PAGE_SIZE) {
592 ptent = *pte;
593
Minchan Kim64b42bc2016-01-15 16:55:06 -0800594 if (pte_none(ptent))
Minchan Kim854e9ed2016-01-15 16:54:53 -0800595 continue;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800596 /*
597 * If the pte has swp_entry, just clear page table to
598 * prevent swap-in which is more expensive rather than
599 * (page allocation + zeroing).
600 */
601 if (!pte_present(ptent)) {
602 swp_entry_t entry;
603
604 entry = pte_to_swp_entry(ptent);
605 if (non_swap_entry(entry))
606 continue;
607 nr_swap--;
608 free_swap_and_cache(entry);
609 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
610 continue;
611 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800612
Christoph Hellwig25b29952019-06-13 22:50:49 +0200613 page = vm_normal_page(vma, addr, ptent);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800614 if (!page)
615 continue;
616
617 /*
618 * If pmd isn't transhuge but the page is THP and
619 * is owned by only this process, split it and
620 * deactivate all pages.
621 */
622 if (PageTransCompound(page)) {
623 if (page_mapcount(page) != 1)
624 goto out;
625 get_page(page);
626 if (!trylock_page(page)) {
627 put_page(page);
628 goto out;
629 }
630 pte_unmap_unlock(orig_pte, ptl);
631 if (split_huge_page(page)) {
632 unlock_page(page);
633 put_page(page);
634 pte_offset_map_lock(mm, pmd, addr, &ptl);
635 goto out;
636 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800637 unlock_page(page);
Eric Biggers263630e2017-08-25 15:55:39 -0700638 put_page(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800639 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
640 pte--;
641 addr -= PAGE_SIZE;
642 continue;
643 }
644
645 VM_BUG_ON_PAGE(PageTransCompound(page), page);
646
647 if (PageSwapCache(page) || PageDirty(page)) {
648 if (!trylock_page(page))
649 continue;
650 /*
651 * If page is shared with others, we couldn't clear
652 * PG_dirty of the page.
653 */
654 if (page_mapcount(page) != 1) {
655 unlock_page(page);
656 continue;
657 }
658
659 if (PageSwapCache(page) && !try_to_free_swap(page)) {
660 unlock_page(page);
661 continue;
662 }
663
664 ClearPageDirty(page);
665 unlock_page(page);
666 }
667
668 if (pte_young(ptent) || pte_dirty(ptent)) {
669 /*
670 * Some of architecture(ex, PPC) don't update TLB
671 * with set_pte_at and tlb_remove_tlb_entry so for
672 * the portability, remap the pte with old|clean
673 * after pte clearing.
674 */
675 ptent = ptep_get_and_clear_full(mm, addr, pte,
676 tlb->fullmm);
677
678 ptent = pte_mkold(ptent);
679 ptent = pte_mkclean(ptent);
680 set_pte_at(mm, addr, pte, ptent);
681 tlb_remove_tlb_entry(tlb, pte, addr);
682 }
Shaohua Li802a3a92017-05-03 14:52:32 -0700683 mark_page_lazyfree(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800684 }
685out:
Minchan Kim64b42bc2016-01-15 16:55:06 -0800686 if (nr_swap) {
687 if (current->mm == mm)
688 sync_mm_rss(mm);
689
690 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
691 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800692 arch_leave_lazy_mmu_mode();
693 pte_unmap_unlock(orig_pte, ptl);
694 cond_resched();
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800695next:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800696 return 0;
697}
698
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200699static const struct mm_walk_ops madvise_free_walk_ops = {
700 .pmd_entry = madvise_free_pte_range,
701};
Minchan Kim854e9ed2016-01-15 16:54:53 -0800702
703static int madvise_free_single_vma(struct vm_area_struct *vma,
704 unsigned long start_addr, unsigned long end_addr)
705{
Minchan Kim854e9ed2016-01-15 16:54:53 -0800706 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800707 struct mmu_notifier_range range;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800708 struct mmu_gather tlb;
709
Minchan Kim854e9ed2016-01-15 16:54:53 -0800710 /* MADV_FREE works for only anon vma at the moment */
711 if (!vma_is_anonymous(vma))
712 return -EINVAL;
713
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800714 range.start = max(vma->vm_start, start_addr);
715 if (range.start >= vma->vm_end)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800716 return -EINVAL;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800717 range.end = min(vma->vm_end, end_addr);
718 if (range.end <= vma->vm_start)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800719 return -EINVAL;
Jérôme Glisse7269f992019-05-13 17:20:53 -0700720 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700721 range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800722
723 lru_add_drain();
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800724 tlb_gather_mmu(&tlb, mm, range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800725 update_hiwater_rss(mm);
726
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800727 mmu_notifier_invalidate_range_start(&range);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200728 tlb_start_vma(&tlb, vma);
729 walk_page_range(vma->vm_mm, range.start, range.end,
730 &madvise_free_walk_ops, &tlb);
731 tlb_end_vma(&tlb, vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800732 mmu_notifier_invalidate_range_end(&range);
733 tlb_finish_mmu(&tlb, range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800734
735 return 0;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/*
739 * Application no longer needs these pages. If the pages are dirty,
740 * it's OK to just throw them away. The app will be more careful about
741 * data it wants to keep. Be sure to free swap resources too. The
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700742 * zap_page_range call sets things up for shrink_active_list to actually free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * these pages later if no one else has touched them in the meantime,
744 * although we could add these pages to a global reuse list for
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700745 * shrink_active_list to pick up before reclaiming other pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
747 * NB: This interface discards data rather than pushes it out to swap,
748 * as some implementations do. This has performance implications for
749 * applications like large transactional databases which want to discard
750 * pages in anonymous maps after committing to backing store the data
751 * that was kept in them. There is no reason to write this data out to
752 * the swap area if the application is discarding it.
753 *
754 * An interface that causes the system to free clean pages and flush
755 * dirty pages is already available as msync(MS_INVALIDATE).
756 */
Mike Rapoport230ca982017-07-10 15:49:02 -0700757static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
758 unsigned long start, unsigned long end)
759{
760 zap_page_range(vma, start, end - start);
761 return 0;
762}
763
764static long madvise_dontneed_free(struct vm_area_struct *vma,
765 struct vm_area_struct **prev,
766 unsigned long start, unsigned long end,
767 int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Prasanna Meda05b74382005-06-21 17:14:37 -0700769 *prev = vma;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700770 if (!can_madv_lru_vma(vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -EINVAL;
772
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800773 if (!userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700774 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800775
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700776 mmap_read_lock(current->mm);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800777 vma = find_vma(current->mm, start);
778 if (!vma)
779 return -ENOMEM;
780 if (start < vma->vm_start) {
781 /*
782 * This "vma" under revalidation is the one
783 * with the lowest vma->vm_start where start
784 * is also < vma->vm_end. If start <
785 * vma->vm_start it means an hole materialized
786 * in the user address space within the
Mike Rapoport230ca982017-07-10 15:49:02 -0700787 * virtual range passed to MADV_DONTNEED
788 * or MADV_FREE.
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800789 */
790 return -ENOMEM;
791 }
Minchan Kim9c276cc2019-09-25 16:49:08 -0700792 if (!can_madv_lru_vma(vma))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800793 return -EINVAL;
794 if (end > vma->vm_end) {
795 /*
796 * Don't fail if end > vma->vm_end. If the old
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700797 * vma was splitted while the mmap_lock was
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800798 * released the effect of the concurrent
Mike Rapoport230ca982017-07-10 15:49:02 -0700799 * operation may not cause madvise() to
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800800 * have an undefined result. There may be an
801 * adjacent next vma that we'll walk
802 * next. userfaultfd_remove() will generate an
803 * UFFD_EVENT_REMOVE repetition on the
804 * end-vma->vm_end range, but the manager can
805 * handle a repetition fine.
806 */
807 end = vma->vm_end;
808 }
809 VM_WARN_ON(start >= end);
810 }
Mike Rapoport230ca982017-07-10 15:49:02 -0700811
812 if (behavior == MADV_DONTNEED)
813 return madvise_dontneed_single_vma(vma, start, end);
814 else if (behavior == MADV_FREE)
815 return madvise_free_single_vma(vma, start, end);
816 else
817 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800820/*
821 * Application wants to free up the pages and associated backing store.
822 * This is effectively punching a hole into the middle of a file.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800823 */
824static long madvise_remove(struct vm_area_struct *vma,
Nick Piggin00e9fa22007-03-16 13:38:10 -0800825 struct vm_area_struct **prev,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800826 unsigned long start, unsigned long end)
827{
Hugh Dickins3f31d072012-05-29 15:06:40 -0700828 loff_t offset;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700829 int error;
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700830 struct file *f;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800831
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700832 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Nick Piggin00e9fa22007-03-16 13:38:10 -0800833
Mike Kravetz72079ba2015-09-08 15:01:57 -0700834 if (vma->vm_flags & VM_LOCKED)
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800835 return -EINVAL;
836
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700837 f = vma->vm_file;
838
839 if (!f || !f->f_mapping || !f->f_mapping->host) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800840 return -EINVAL;
841 }
842
Hugh Dickins69cf0fa2006-04-17 22:46:32 +0100843 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
844 return -EACCES;
845
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800846 offset = (loff_t)(start - vma->vm_start)
847 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700848
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700849 /*
850 * Filesystem's fallocate may need to take i_mutex. We need to
851 * explicitly grab a reference because the vma (and hence the
852 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700853 * mmap_lock.
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700854 */
855 get_file(f);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800856 if (userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700857 /* mmap_lock was not released by userfaultfd_remove() */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700858 mmap_read_unlock(current->mm);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800859 }
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500860 error = vfs_fallocate(f,
Hugh Dickins3f31d072012-05-29 15:06:40 -0700861 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
862 offset, end - start);
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700863 fput(f);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700864 mmap_read_lock(current->mm);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700865 return error;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800866}
867
Andi Kleen9893e492009-09-16 11:50:17 +0200868#ifdef CONFIG_MEMORY_FAILURE
869/*
870 * Error injection support for memory error handling.
871 */
Anshuman Khandual97167a72017-05-03 14:55:25 -0700872static int madvise_inject_error(int behavior,
873 unsigned long start, unsigned long end)
Andi Kleen9893e492009-09-16 11:50:17 +0200874{
Mel Gormanc461ad62017-08-31 16:15:30 -0700875 struct zone *zone;
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800876 unsigned long size;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700877
Andi Kleen9893e492009-09-16 11:50:17 +0200878 if (!capable(CAP_SYS_ADMIN))
879 return -EPERM;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700880
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700881
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800882 for (; start < end; start += size) {
Dan Williams23e7b5c2018-07-13 21:50:06 -0700883 unsigned long pfn;
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700884 struct page *page;
Andrew Morton325c4ef2013-09-11 14:23:03 -0700885 int ret;
886
Anshuman Khandual97167a72017-05-03 14:55:25 -0700887 ret = get_user_pages_fast(start, 1, 0, &page);
Andi Kleen9893e492009-09-16 11:50:17 +0200888 if (ret != 1)
889 return ret;
Dan Williams23e7b5c2018-07-13 21:50:06 -0700890 pfn = page_to_pfn(page);
Andrew Morton325c4ef2013-09-11 14:23:03 -0700891
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700892 /*
893 * When soft offlining hugepages, after migrating the page
894 * we dissolve it, therefore in the second loop "page" will
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800895 * no longer be a compound page.
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700896 */
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800897 size = page_size(compound_head(page));
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700898
Anshuman Khandual97167a72017-05-03 14:55:25 -0700899 if (PageHWPoison(page)) {
900 put_page(page);
Wanpeng Li29b4eed2013-09-11 14:22:59 -0700901 continue;
902 }
Anshuman Khandual97167a72017-05-03 14:55:25 -0700903
904 if (behavior == MADV_SOFT_OFFLINE) {
905 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700906 pfn, start);
Naoya Horiguchifeec24a2019-11-30 17:53:38 -0800907 ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700908 } else {
909 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
910 pfn, start);
911 /*
912 * Drop the page reference taken by get_user_pages_fast(). In
913 * the absence of MF_COUNT_INCREASED the memory_failure()
914 * routine is responsible for pinning the page to prevent it
915 * from being released back to the page allocator.
916 */
917 put_page(page);
918 ret = memory_failure(pfn, 0);
Andi Kleenafcf9382009-12-16 12:20:00 +0100919 }
Anshuman Khandual97167a72017-05-03 14:55:25 -0700920
Naoya Horiguchi23a003b2016-03-15 14:56:36 -0700921 if (ret)
922 return ret;
Andi Kleen9893e492009-09-16 11:50:17 +0200923 }
Mel Gormanc461ad62017-08-31 16:15:30 -0700924
925 /* Ensure that all poisoned pages are removed from per-cpu lists */
926 for_each_populated_zone(zone)
927 drain_all_pages(zone);
928
Andrew Morton325c4ef2013-09-11 14:23:03 -0700929 return 0;
Andi Kleen9893e492009-09-16 11:50:17 +0200930}
931#endif
932
suzuki165cd402005-07-27 11:43:59 -0700933static long
934madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
935 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 switch (behavior) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800938 case MADV_REMOVE:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700939 return madvise_remove(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 case MADV_WILLNEED:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700941 return madvise_willneed(vma, prev, start, end);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700942 case MADV_COLD:
943 return madvise_cold(vma, prev, start, end);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700944 case MADV_PAGEOUT:
945 return madvise_pageout(vma, prev, start, end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800946 case MADV_FREE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case MADV_DONTNEED:
Mike Rapoport230ca982017-07-10 15:49:02 -0700948 return madvise_dontneed_free(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 default:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700950 return madvise_behavior(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Nicholas Krause1ecef9e2015-09-04 15:48:24 -0700954static bool
Nick Piggin75927af2009-06-16 15:32:38 -0700955madvise_behavior_valid(int behavior)
956{
957 switch (behavior) {
958 case MADV_DOFORK:
959 case MADV_DONTFORK:
960 case MADV_NORMAL:
961 case MADV_SEQUENTIAL:
962 case MADV_RANDOM:
963 case MADV_REMOVE:
964 case MADV_WILLNEED:
965 case MADV_DONTNEED:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800966 case MADV_FREE:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700967 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700968 case MADV_PAGEOUT:
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700969#ifdef CONFIG_KSM
970 case MADV_MERGEABLE:
971 case MADV_UNMERGEABLE:
972#endif
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800973#ifdef CONFIG_TRANSPARENT_HUGEPAGE
974 case MADV_HUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -0800975 case MADV_NOHUGEPAGE:
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800976#endif
Jason Baronaccb61f2012-03-23 15:02:51 -0700977 case MADV_DONTDUMP:
978 case MADV_DODUMP:
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700979 case MADV_WIPEONFORK:
980 case MADV_KEEPONFORK:
Anshuman Khandual5e451be2017-05-03 14:55:28 -0700981#ifdef CONFIG_MEMORY_FAILURE
982 case MADV_SOFT_OFFLINE:
983 case MADV_HWPOISON:
984#endif
Nicholas Krause1ecef9e2015-09-04 15:48:24 -0700985 return true;
Nick Piggin75927af2009-06-16 15:32:38 -0700986
987 default:
Nicholas Krause1ecef9e2015-09-04 15:48:24 -0700988 return false;
Nick Piggin75927af2009-06-16 15:32:38 -0700989 }
990}
Hugh Dickins3866ea92009-09-21 17:01:52 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/*
993 * The madvise(2) system call.
994 *
995 * Applications can use madvise() to advise the kernel how it should
996 * handle paging I/O in this VM area. The idea is to help the kernel
997 * use appropriate read-ahead and caching techniques. The information
998 * provided is advisory only, and can be safely disregarded by the
999 * kernel without affecting the correct operation of the application.
1000 *
1001 * behavior values:
1002 * MADV_NORMAL - the default behavior is to read clusters. This
1003 * results in some read-ahead and read-behind.
1004 * MADV_RANDOM - the system should read the minimum amount of data
1005 * on any access, since it is unlikely that the appli-
1006 * cation will need more than what it asks for.
1007 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1008 * once, so they can be aggressively read ahead, and
1009 * can be freed soon after they are accessed.
1010 * MADV_WILLNEED - the application is notifying the system to read
1011 * some pages ahead.
1012 * MADV_DONTNEED - the application is finished with the given range,
1013 * so the kernel can free resources associated with it.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001014 * MADV_FREE - the application marks pages in the given range as lazy free,
1015 * where actual purges are postponed until memory pressure happens.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08001016 * MADV_REMOVE - the application wants to free up the given range of
1017 * pages and associated backing store.
Hugh Dickins3866ea92009-09-21 17:01:52 -07001018 * MADV_DONTFORK - omit this area from child's address space when forking:
1019 * typically, to avoid COWing pages pinned by get_user_pages().
1020 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
Yang Shic02c3002017-10-13 15:57:37 -07001021 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1022 * range after a fork.
1023 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001024 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1025 * were corrupted by unrecoverable hardware memory failure.
1026 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001027 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1028 * this area with pages of identical content from other such areas.
1029 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001030 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1031 * huge pages in the future. Existing pages might be coalesced and
1032 * new pages might be allocated as THP.
1033 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1034 * transparent huge pages so the existing pages will not be
1035 * coalesced into THP and new pages will not be allocated as THP.
1036 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1037 * from being included in its core dump.
1038 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 *
1040 * return values:
1041 * zero - success
1042 * -EINVAL - start + len < 0, start is not page-aligned,
1043 * "behavior" is not a valid value, or application
Yang Shic02c3002017-10-13 15:57:37 -07001044 * is attempting to release locked or shared pages,
1045 * or the specified address range includes file, Huge TLB,
1046 * MAP_SHARED or VMPFNMAP range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * -ENOMEM - addresses in the specified range are not currently
1048 * mapped, or are outside the AS of the process.
1049 * -EIO - an I/O error occurred while paging in data.
1050 * -EBADF - map exists, but area maps something that isn't a file.
1051 * -EAGAIN - a kernel resource was temporarily unavailable.
1052 */
Jens Axboedb08ca22019-12-25 22:14:54 -07001053int do_madvise(unsigned long start, size_t len_in, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Prasanna Meda05b74382005-06-21 17:14:37 -07001055 unsigned long end, tmp;
Vladimir Cernovec9bed92013-09-11 14:20:15 -07001056 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int unmapped_error = 0;
1058 int error = -EINVAL;
Jason Baronf7977792007-07-15 23:38:21 -07001059 int write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 size_t len;
Shaohua Li1998cc02013-02-22 16:32:31 -08001061 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Andrey Konovalov057d33892019-09-25 16:48:30 -07001063 start = untagged_addr(start);
1064
Nick Piggin75927af2009-06-16 15:32:38 -07001065 if (!madvise_behavior_valid(behavior))
1066 return error;
1067
Wei Yangdf6c6502019-11-30 17:57:46 -08001068 if (!PAGE_ALIGNED(start))
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001069 return error;
Wei Yangdf6c6502019-11-30 17:57:46 -08001070 len = PAGE_ALIGN(len_in);
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001071
1072 /* Check to see whether len was rounded up from small -ve to zero */
1073 if (len_in && !len)
1074 return error;
1075
1076 end = start + len;
1077 if (end < start)
1078 return error;
1079
1080 error = 0;
1081 if (end == start)
1082 return error;
1083
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001084#ifdef CONFIG_MEMORY_FAILURE
1085 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1086 return madvise_inject_error(behavior, start, start + len_in);
1087#endif
1088
Jason Baronf7977792007-07-15 23:38:21 -07001089 write = madvise_need_mmap_write(behavior);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001090 if (write) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001091 if (mmap_write_lock_killable(current->mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001092 return -EINTR;
Linus Torvaldsbc0c4d12020-04-24 11:10:58 -07001093
1094 /*
1095 * We may have stolen the mm from another process
1096 * that is undergoing core dumping.
1097 *
1098 * Right now that's io_ring, in the future it may
1099 * be remote process management and not "current"
1100 * at all.
1101 *
1102 * We need to fix core dumping to not do this,
1103 * but for now we have the mmget_still_valid()
1104 * model.
1105 */
1106 if (!mmget_still_valid(current->mm)) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001107 mmap_write_unlock(current->mm);
Linus Torvaldsbc0c4d12020-04-24 11:10:58 -07001108 return -EINTR;
1109 }
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001110 } else {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001111 mmap_read_lock(current->mm);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * If the interval [start,end) covers some unmapped address
1116 * ranges, just ignore them, but return -ENOMEM at the end.
Prasanna Meda05b74382005-06-21 17:14:37 -07001117 * - different from the way of handling in mlock etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Prasanna Meda05b74382005-06-21 17:14:37 -07001119 vma = find_vma_prev(current->mm, start, &prev);
Hugh Dickins836d5ff2005-09-03 15:54:53 -07001120 if (vma && start > vma->vm_start)
1121 prev = vma;
1122
Shaohua Li1998cc02013-02-22 16:32:31 -08001123 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 for (;;) {
1125 /* Still start < end. */
1126 error = -ENOMEM;
1127 if (!vma)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Prasanna Meda05b74382005-06-21 17:14:37 -07001130 /* Here start < (end|vma->vm_end). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (start < vma->vm_start) {
1132 unmapped_error = -ENOMEM;
1133 start = vma->vm_start;
Prasanna Meda05b74382005-06-21 17:14:37 -07001134 if (start >= end)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Prasanna Meda05b74382005-06-21 17:14:37 -07001138 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1139 tmp = vma->vm_end;
1140 if (end < tmp)
1141 tmp = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Prasanna Meda05b74382005-06-21 17:14:37 -07001143 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1144 error = madvise_vma(vma, &prev, start, tmp, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (error)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001146 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -07001147 start = tmp;
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001148 if (prev && start < prev->vm_end)
Prasanna Meda05b74382005-06-21 17:14:37 -07001149 start = prev->vm_end;
1150 error = unmapped_error;
1151 if (start >= end)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001152 goto out;
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001153 if (prev)
1154 vma = prev->vm_next;
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001155 else /* madvise_remove dropped mmap_lock */
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001156 vma = find_vma(current->mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158out:
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001159 blk_finish_plug(&plug);
Jason Baronf7977792007-07-15 23:38:21 -07001160 if (write)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001161 mmap_write_unlock(current->mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001162 else
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001163 mmap_read_unlock(current->mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return error;
1166}
Jens Axboedb08ca22019-12-25 22:14:54 -07001167
1168SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1169{
1170 return do_madvise(start, len_in, behavior);
1171}