blob: 4b9c5509990cdacc898f1de1938d8ac8b9c2f06d [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>
Minchan Kimecb8ac82020-10-17 16:14:59 -070020#include <linux/sched/mm.h>
21#include <linux/uio.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070022#include <linux/ksm.h>
Hugh Dickins3f31d072012-05-29 15:06:40 -070023#include <linux/fs.h>
Andy Lutomirski9ab42332012-07-05 16:00:11 -070024#include <linux/file.h>
Shaohua Li1998cc02013-02-22 16:32:31 -080025#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040026#include <linux/backing-dev.h>
Christoph Hellwiga5201102019-08-28 16:19:53 +020027#include <linux/pagewalk.h>
Shaohua Li1998cc02013-02-22 16:32:31 -080028#include <linux/swap.h>
29#include <linux/swapops.h>
Hugh Dickins3a4f8a02017-02-24 14:59:36 -080030#include <linux/shmem_fs.h>
Minchan Kim854e9ed2016-01-15 16:54:53 -080031#include <linux/mmu_notifier.h>
32
33#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Kirill A. Shutemov23519072017-02-22 15:46:39 -080035#include "internal.h"
36
Minchan Kimd616d512019-09-25 16:49:19 -070037struct madvise_walk_private {
38 struct mmu_gather *tlb;
39 bool pageout;
40};
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Nick Piggin0a27a142007-05-06 14:49:53 -070043 * Any behaviour which results in changes to the vma->vm_flags needs to
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070044 * take mmap_lock for writing. Others, which simply traverse vmas, need
Nick Piggin0a27a142007-05-06 14:49:53 -070045 * to only take it for reading.
46 */
47static int madvise_need_mmap_write(int behavior)
48{
49 switch (behavior) {
50 case MADV_REMOVE:
51 case MADV_WILLNEED:
52 case MADV_DONTNEED:
Minchan Kim9c276cc2019-09-25 16:49:08 -070053 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -070054 case MADV_PAGEOUT:
Minchan Kim854e9ed2016-01-15 16:54:53 -080055 case MADV_FREE:
David Hildenbrand4ca9b3852021-06-30 18:52:28 -070056 case MADV_POPULATE_READ:
57 case MADV_POPULATE_WRITE:
Nick Piggin0a27a142007-05-06 14:49:53 -070058 return 0;
59 default:
60 /* be safe, default to 1. list exceptions explicitly */
61 return 1;
62 }
63}
64
65/*
Colin Crossac1e9ac2022-01-14 14:05:55 -080066 * Update the vm_flags on region of a vma, splitting it or merging it as
67 * necessary. Must be called with mmap_sem held for writing;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
Colin Crossac1e9ac2022-01-14 14:05:55 -080069static int madvise_update_vma(struct vm_area_struct *vma,
70 struct vm_area_struct **prev, unsigned long start,
71 unsigned long end, unsigned long new_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Vladimir Cernovec9bed92013-09-11 14:20:15 -070073 struct mm_struct *mm = vma->vm_mm;
Colin Crossac1e9ac2022-01-14 14:05:55 -080074 int error;
Prasanna Meda05b74382005-06-21 17:14:37 -070075 pgoff_t pgoff;
Prasanna Medae798c6e2005-06-21 17:14:36 -070076
Prasanna Meda05b74382005-06-21 17:14:37 -070077 if (new_flags == vma->vm_flags) {
78 *prev = vma;
Colin Crossac1e9ac2022-01-14 14:05:55 -080079 return 0;
Prasanna Meda05b74382005-06-21 17:14:37 -070080 }
81
82 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
83 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -070084 vma->vm_file, pgoff, vma_policy(vma),
85 vma->vm_userfaultfd_ctx);
Prasanna Meda05b74382005-06-21 17:14:37 -070086 if (*prev) {
87 vma = *prev;
88 goto success;
89 }
90
91 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (start != vma->vm_start) {
Colin Crossac1e9ac2022-01-14 14:05:55 -080094 if (unlikely(mm->map_count >= sysctl_max_map_count))
95 return -ENOMEM;
David Rientjesdef5efe2017-02-24 14:58:47 -080096 error = __split_vma(mm, vma, start, 1);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -070097 if (error)
Colin Crossac1e9ac2022-01-14 14:05:55 -080098 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 if (end != vma->vm_end) {
Colin Crossac1e9ac2022-01-14 14:05:55 -0800102 if (unlikely(mm->map_count >= sysctl_max_map_count))
103 return -ENOMEM;
David Rientjesdef5efe2017-02-24 14:58:47 -0800104 error = __split_vma(mm, vma, end, 0);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700105 if (error)
Colin Crossac1e9ac2022-01-14 14:05:55 -0800106 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700109success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700111 * vm_flags is protected by the mmap_lock held in write mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Prasanna Medae798c6e2005-06-21 17:14:36 -0700113 vma->vm_flags = new_flags;
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700114
Colin Crossac1e9ac2022-01-14 14:05:55 -0800115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Shaohua Li1998cc02013-02-22 16:32:31 -0800118#ifdef CONFIG_SWAP
119static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
120 unsigned long end, struct mm_walk *walk)
121{
122 pte_t *orig_pte;
123 struct vm_area_struct *vma = walk->private;
124 unsigned long index;
125
126 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
127 return 0;
128
129 for (index = start; index != end; index += PAGE_SIZE) {
130 pte_t pte;
131 swp_entry_t entry;
132 struct page *page;
133 spinlock_t *ptl;
134
135 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
136 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
137 pte_unmap_unlock(orig_pte, ptl);
138
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800139 if (pte_present(pte) || pte_none(pte))
Shaohua Li1998cc02013-02-22 16:32:31 -0800140 continue;
141 entry = pte_to_swp_entry(pte);
142 if (unlikely(non_swap_entry(entry)))
143 continue;
144
145 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
Shaohua Li23955622017-07-10 15:47:11 -0700146 vma, index, false);
Shaohua Li1998cc02013-02-22 16:32:31 -0800147 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300148 put_page(page);
Shaohua Li1998cc02013-02-22 16:32:31 -0800149 }
150
151 return 0;
152}
153
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200154static const struct mm_walk_ops swapin_walk_ops = {
155 .pmd_entry = swapin_walk_pmd_entry,
156};
Shaohua Li1998cc02013-02-22 16:32:31 -0800157
158static void force_shm_swapin_readahead(struct vm_area_struct *vma,
159 unsigned long start, unsigned long end,
160 struct address_space *mapping)
161{
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700162 XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
Matthew Wilcox (Oracle)66383802020-11-21 22:17:22 -0800163 pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
Shaohua Li1998cc02013-02-22 16:32:31 -0800164 struct page *page;
Shaohua Li1998cc02013-02-22 16:32:31 -0800165
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700166 rcu_read_lock();
167 xas_for_each(&xas, page, end_index) {
168 swp_entry_t swap;
Shaohua Li1998cc02013-02-22 16:32:31 -0800169
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700170 if (!xa_is_value(page))
Shaohua Li1998cc02013-02-22 16:32:31 -0800171 continue;
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700172 xas_pause(&xas);
173 rcu_read_unlock();
174
Shaohua Li1998cc02013-02-22 16:32:31 -0800175 swap = radix_to_swp_entry(page);
176 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
Shaohua Li23955622017-07-10 15:47:11 -0700177 NULL, 0, false);
Shaohua Li1998cc02013-02-22 16:32:31 -0800178 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300179 put_page(page);
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700180
181 rcu_read_lock();
Shaohua Li1998cc02013-02-22 16:32:31 -0800182 }
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700183 rcu_read_unlock();
Shaohua Li1998cc02013-02-22 16:32:31 -0800184
185 lru_add_drain(); /* Push any new pages onto the LRU now */
186}
187#endif /* CONFIG_SWAP */
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * Schedule all required I/O operations. Do not wait for completion.
191 */
Vladimir Cernovec9bed92013-09-11 14:20:15 -0700192static long madvise_willneed(struct vm_area_struct *vma,
193 struct vm_area_struct **prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned long start, unsigned long end)
195{
Minchan Kim0726b012020-10-17 16:14:50 -0700196 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct file *file = vma->vm_file;
Jan Kara692fe622019-08-29 09:04:11 -0700198 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
chenjie6ea8d952017-11-29 16:10:54 -0800200 *prev = vma;
Shaohua Li1998cc02013-02-22 16:32:31 -0800201#ifdef CONFIG_SWAP
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100202 if (!file) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200203 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
204 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li1998cc02013-02-22 16:32:31 -0800205 return 0;
206 }
Shaohua Li1998cc02013-02-22 16:32:31 -0800207
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100208 if (shmem_mapping(file->f_mapping)) {
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100209 force_shm_swapin_readahead(vma, start, end,
210 file->f_mapping);
211 return 0;
212 }
213#else
Suzuki1bef4002005-10-11 08:29:06 -0700214 if (!file)
215 return -EBADF;
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100216#endif
Suzuki1bef4002005-10-11 08:29:06 -0700217
Matthew Wilcoxe748dcd2015-02-16 15:59:12 -0800218 if (IS_DAX(file_inode(file))) {
Carsten Ottefe77ba62005-06-23 22:05:29 -0700219 /* no bad return value, but ignore advice */
220 return 0;
221 }
222
Jan Kara692fe622019-08-29 09:04:11 -0700223 /*
224 * Filesystem's fadvise may need to take various locks. We need to
225 * explicitly grab a reference because the vma (and hence the
226 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700227 * mmap_lock.
Jan Kara692fe622019-08-29 09:04:11 -0700228 */
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700229 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Jan Kara692fe622019-08-29 09:04:11 -0700230 get_file(file);
Jan Kara692fe622019-08-29 09:04:11 -0700231 offset = (loff_t)(start - vma->vm_start)
232 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Minchan Kim0726b012020-10-17 16:14:50 -0700233 mmap_read_unlock(mm);
Jan Kara692fe622019-08-29 09:04:11 -0700234 vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
235 fput(file);
Minchan Kim0726b012020-10-17 16:14:50 -0700236 mmap_read_lock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238}
239
Minchan Kimd616d512019-09-25 16:49:19 -0700240static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
241 unsigned long addr, unsigned long end,
242 struct mm_walk *walk)
Minchan Kim9c276cc2019-09-25 16:49:08 -0700243{
Minchan Kimd616d512019-09-25 16:49:19 -0700244 struct madvise_walk_private *private = walk->private;
245 struct mmu_gather *tlb = private->tlb;
246 bool pageout = private->pageout;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700247 struct mm_struct *mm = tlb->mm;
248 struct vm_area_struct *vma = walk->vma;
249 pte_t *orig_pte, *pte, ptent;
250 spinlock_t *ptl;
Minchan Kimd616d512019-09-25 16:49:19 -0700251 struct page *page = NULL;
252 LIST_HEAD(page_list);
253
254 if (fatal_signal_pending(current))
255 return -EINTR;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700256
257#ifdef CONFIG_TRANSPARENT_HUGEPAGE
258 if (pmd_trans_huge(*pmd)) {
259 pmd_t orig_pmd;
260 unsigned long next = pmd_addr_end(addr, end);
261
262 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
263 ptl = pmd_trans_huge_lock(pmd, vma);
264 if (!ptl)
265 return 0;
266
267 orig_pmd = *pmd;
268 if (is_huge_zero_pmd(orig_pmd))
269 goto huge_unlock;
270
271 if (unlikely(!pmd_present(orig_pmd))) {
272 VM_BUG_ON(thp_migration_supported() &&
273 !is_pmd_migration_entry(orig_pmd));
274 goto huge_unlock;
275 }
276
277 page = pmd_page(orig_pmd);
Michal Hocko12e967f2020-03-21 18:22:26 -0700278
279 /* Do not interfere with other mappings of this page */
280 if (page_mapcount(page) != 1)
281 goto huge_unlock;
282
Minchan Kim9c276cc2019-09-25 16:49:08 -0700283 if (next - addr != HPAGE_PMD_SIZE) {
284 int err;
285
Minchan Kim9c276cc2019-09-25 16:49:08 -0700286 get_page(page);
287 spin_unlock(ptl);
288 lock_page(page);
289 err = split_huge_page(page);
290 unlock_page(page);
291 put_page(page);
292 if (!err)
293 goto regular_page;
294 return 0;
295 }
296
297 if (pmd_young(orig_pmd)) {
298 pmdp_invalidate(vma, addr, pmd);
299 orig_pmd = pmd_mkold(orig_pmd);
300
301 set_pmd_at(mm, addr, pmd, orig_pmd);
302 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
303 }
304
Minchan Kimd616d512019-09-25 16:49:19 -0700305 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700306 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700307 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800308 if (!isolate_lru_page(page)) {
309 if (PageUnevictable(page))
310 putback_lru_page(page);
311 else
312 list_add(&page->lru, &page_list);
313 }
Minchan Kimd616d512019-09-25 16:49:19 -0700314 } else
315 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700316huge_unlock:
317 spin_unlock(ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700318 if (pageout)
319 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700320 return 0;
321 }
322
Minchan Kimce268422020-09-14 23:32:15 -0700323regular_page:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700324 if (pmd_trans_unstable(pmd))
325 return 0;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700326#endif
327 tlb_change_page_size(tlb, PAGE_SIZE);
328 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
329 flush_tlb_batched_pending(mm);
330 arch_enter_lazy_mmu_mode();
331 for (; addr < end; pte++, addr += PAGE_SIZE) {
332 ptent = *pte;
333
334 if (pte_none(ptent))
335 continue;
336
337 if (!pte_present(ptent))
338 continue;
339
340 page = vm_normal_page(vma, addr, ptent);
341 if (!page)
342 continue;
343
344 /*
345 * Creating a THP page is expensive so split it only if we
346 * are sure it's worth. Split it if we are only owner.
347 */
348 if (PageTransCompound(page)) {
349 if (page_mapcount(page) != 1)
350 break;
351 get_page(page);
352 if (!trylock_page(page)) {
353 put_page(page);
354 break;
355 }
356 pte_unmap_unlock(orig_pte, ptl);
357 if (split_huge_page(page)) {
358 unlock_page(page);
359 put_page(page);
360 pte_offset_map_lock(mm, pmd, addr, &ptl);
361 break;
362 }
363 unlock_page(page);
364 put_page(page);
365 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
366 pte--;
367 addr -= PAGE_SIZE;
368 continue;
369 }
370
Michal Hocko12e967f2020-03-21 18:22:26 -0700371 /* Do not interfere with other mappings of this page */
372 if (page_mapcount(page) != 1)
373 continue;
374
Minchan Kim9c276cc2019-09-25 16:49:08 -0700375 VM_BUG_ON_PAGE(PageTransCompound(page), page);
376
377 if (pte_young(ptent)) {
378 ptent = ptep_get_and_clear_full(mm, addr, pte,
379 tlb->fullmm);
380 ptent = pte_mkold(ptent);
381 set_pte_at(mm, addr, pte, ptent);
382 tlb_remove_tlb_entry(tlb, pte, addr);
383 }
384
385 /*
386 * We are deactivating a page for accelerating reclaiming.
387 * VM couldn't reclaim the page unless we clear PG_young.
388 * As a side effect, it makes confuse idle-page tracking
389 * because they will miss recent referenced history.
390 */
Minchan Kimd616d512019-09-25 16:49:19 -0700391 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700392 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700393 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800394 if (!isolate_lru_page(page)) {
395 if (PageUnevictable(page))
396 putback_lru_page(page);
397 else
398 list_add(&page->lru, &page_list);
399 }
Minchan Kimd616d512019-09-25 16:49:19 -0700400 } else
401 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700402 }
403
404 arch_leave_lazy_mmu_mode();
405 pte_unmap_unlock(orig_pte, ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700406 if (pageout)
407 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700408 cond_resched();
409
410 return 0;
411}
412
413static const struct mm_walk_ops cold_walk_ops = {
Minchan Kimd616d512019-09-25 16:49:19 -0700414 .pmd_entry = madvise_cold_or_pageout_pte_range,
Minchan Kim9c276cc2019-09-25 16:49:08 -0700415};
416
417static void madvise_cold_page_range(struct mmu_gather *tlb,
418 struct vm_area_struct *vma,
419 unsigned long addr, unsigned long end)
420{
Minchan Kimd616d512019-09-25 16:49:19 -0700421 struct madvise_walk_private walk_private = {
422 .pageout = false,
423 .tlb = tlb,
424 };
425
Minchan Kim9c276cc2019-09-25 16:49:08 -0700426 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700427 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700428 tlb_end_vma(tlb, vma);
429}
430
431static long madvise_cold(struct vm_area_struct *vma,
432 struct vm_area_struct **prev,
433 unsigned long start_addr, unsigned long end_addr)
434{
435 struct mm_struct *mm = vma->vm_mm;
436 struct mmu_gather tlb;
437
438 *prev = vma;
439 if (!can_madv_lru_vma(vma))
440 return -EINVAL;
441
442 lru_add_drain();
Will Deacona72afd82021-01-27 23:53:45 +0000443 tlb_gather_mmu(&tlb, mm);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700444 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
Will Deaconae8eba82021-01-27 23:53:43 +0000445 tlb_finish_mmu(&tlb);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700446
447 return 0;
448}
449
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700450static void madvise_pageout_page_range(struct mmu_gather *tlb,
451 struct vm_area_struct *vma,
452 unsigned long addr, unsigned long end)
453{
Minchan Kimd616d512019-09-25 16:49:19 -0700454 struct madvise_walk_private walk_private = {
455 .pageout = true,
456 .tlb = tlb,
457 };
458
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700459 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700460 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700461 tlb_end_vma(tlb, vma);
462}
463
464static inline bool can_do_pageout(struct vm_area_struct *vma)
465{
466 if (vma_is_anonymous(vma))
467 return true;
468 if (!vma->vm_file)
469 return false;
470 /*
471 * paging out pagecache only for non-anonymous mappings that correspond
472 * to the files the calling process could (if tried) open for writing;
473 * otherwise we'd be including shared non-exclusive mappings, which
474 * opens a side channel.
475 */
Christian Brauner21cb47b2021-01-21 14:19:25 +0100476 return inode_owner_or_capable(&init_user_ns,
477 file_inode(vma->vm_file)) ||
Christian Brauner02f92b32021-01-21 14:19:22 +0100478 file_permission(vma->vm_file, MAY_WRITE) == 0;
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700479}
480
481static long madvise_pageout(struct vm_area_struct *vma,
482 struct vm_area_struct **prev,
483 unsigned long start_addr, unsigned long end_addr)
484{
485 struct mm_struct *mm = vma->vm_mm;
486 struct mmu_gather tlb;
487
488 *prev = vma;
489 if (!can_madv_lru_vma(vma))
490 return -EINVAL;
491
492 if (!can_do_pageout(vma))
493 return 0;
494
495 lru_add_drain();
Will Deacona72afd82021-01-27 23:53:45 +0000496 tlb_gather_mmu(&tlb, mm);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700497 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
Will Deaconae8eba82021-01-27 23:53:43 +0000498 tlb_finish_mmu(&tlb);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700499
500 return 0;
501}
502
Minchan Kim854e9ed2016-01-15 16:54:53 -0800503static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
504 unsigned long end, struct mm_walk *walk)
505
506{
507 struct mmu_gather *tlb = walk->private;
508 struct mm_struct *mm = tlb->mm;
509 struct vm_area_struct *vma = walk->vma;
510 spinlock_t *ptl;
511 pte_t *orig_pte, *pte, ptent;
512 struct page *page;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800513 int nr_swap = 0;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800514 unsigned long next;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800515
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800516 next = pmd_addr_end(addr, end);
517 if (pmd_trans_huge(*pmd))
518 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
519 goto next;
520
Minchan Kim854e9ed2016-01-15 16:54:53 -0800521 if (pmd_trans_unstable(pmd))
522 return 0;
523
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200524 tlb_change_page_size(tlb, PAGE_SIZE);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800525 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Mel Gorman3ea27712017-08-02 13:31:52 -0700526 flush_tlb_batched_pending(mm);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800527 arch_enter_lazy_mmu_mode();
528 for (; addr != end; pte++, addr += PAGE_SIZE) {
529 ptent = *pte;
530
Minchan Kim64b42bc2016-01-15 16:55:06 -0800531 if (pte_none(ptent))
Minchan Kim854e9ed2016-01-15 16:54:53 -0800532 continue;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800533 /*
534 * If the pte has swp_entry, just clear page table to
535 * prevent swap-in which is more expensive rather than
536 * (page allocation + zeroing).
537 */
538 if (!pte_present(ptent)) {
539 swp_entry_t entry;
540
541 entry = pte_to_swp_entry(ptent);
542 if (non_swap_entry(entry))
543 continue;
544 nr_swap--;
545 free_swap_and_cache(entry);
546 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
547 continue;
548 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800549
Christoph Hellwig25b29952019-06-13 22:50:49 +0200550 page = vm_normal_page(vma, addr, ptent);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800551 if (!page)
552 continue;
553
554 /*
555 * If pmd isn't transhuge but the page is THP and
556 * is owned by only this process, split it and
557 * deactivate all pages.
558 */
559 if (PageTransCompound(page)) {
560 if (page_mapcount(page) != 1)
561 goto out;
562 get_page(page);
563 if (!trylock_page(page)) {
564 put_page(page);
565 goto out;
566 }
567 pte_unmap_unlock(orig_pte, ptl);
568 if (split_huge_page(page)) {
569 unlock_page(page);
570 put_page(page);
571 pte_offset_map_lock(mm, pmd, addr, &ptl);
572 goto out;
573 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800574 unlock_page(page);
Eric Biggers263630e2017-08-25 15:55:39 -0700575 put_page(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800576 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
577 pte--;
578 addr -= PAGE_SIZE;
579 continue;
580 }
581
582 VM_BUG_ON_PAGE(PageTransCompound(page), page);
583
584 if (PageSwapCache(page) || PageDirty(page)) {
585 if (!trylock_page(page))
586 continue;
587 /*
588 * If page is shared with others, we couldn't clear
589 * PG_dirty of the page.
590 */
591 if (page_mapcount(page) != 1) {
592 unlock_page(page);
593 continue;
594 }
595
596 if (PageSwapCache(page) && !try_to_free_swap(page)) {
597 unlock_page(page);
598 continue;
599 }
600
601 ClearPageDirty(page);
602 unlock_page(page);
603 }
604
605 if (pte_young(ptent) || pte_dirty(ptent)) {
606 /*
607 * Some of architecture(ex, PPC) don't update TLB
608 * with set_pte_at and tlb_remove_tlb_entry so for
609 * the portability, remap the pte with old|clean
610 * after pte clearing.
611 */
612 ptent = ptep_get_and_clear_full(mm, addr, pte,
613 tlb->fullmm);
614
615 ptent = pte_mkold(ptent);
616 ptent = pte_mkclean(ptent);
617 set_pte_at(mm, addr, pte, ptent);
618 tlb_remove_tlb_entry(tlb, pte, addr);
619 }
Shaohua Li802a3a92017-05-03 14:52:32 -0700620 mark_page_lazyfree(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800621 }
622out:
Minchan Kim64b42bc2016-01-15 16:55:06 -0800623 if (nr_swap) {
624 if (current->mm == mm)
625 sync_mm_rss(mm);
626
627 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
628 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800629 arch_leave_lazy_mmu_mode();
630 pte_unmap_unlock(orig_pte, ptl);
631 cond_resched();
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800632next:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800633 return 0;
634}
635
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200636static const struct mm_walk_ops madvise_free_walk_ops = {
637 .pmd_entry = madvise_free_pte_range,
638};
Minchan Kim854e9ed2016-01-15 16:54:53 -0800639
640static int madvise_free_single_vma(struct vm_area_struct *vma,
641 unsigned long start_addr, unsigned long end_addr)
642{
Minchan Kim854e9ed2016-01-15 16:54:53 -0800643 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800644 struct mmu_notifier_range range;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800645 struct mmu_gather tlb;
646
Minchan Kim854e9ed2016-01-15 16:54:53 -0800647 /* MADV_FREE works for only anon vma at the moment */
648 if (!vma_is_anonymous(vma))
649 return -EINVAL;
650
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800651 range.start = max(vma->vm_start, start_addr);
652 if (range.start >= vma->vm_end)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800653 return -EINVAL;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800654 range.end = min(vma->vm_end, end_addr);
655 if (range.end <= vma->vm_start)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800656 return -EINVAL;
Jérôme Glisse7269f992019-05-13 17:20:53 -0700657 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700658 range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800659
660 lru_add_drain();
Will Deacona72afd82021-01-27 23:53:45 +0000661 tlb_gather_mmu(&tlb, mm);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800662 update_hiwater_rss(mm);
663
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800664 mmu_notifier_invalidate_range_start(&range);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200665 tlb_start_vma(&tlb, vma);
666 walk_page_range(vma->vm_mm, range.start, range.end,
667 &madvise_free_walk_ops, &tlb);
668 tlb_end_vma(&tlb, vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800669 mmu_notifier_invalidate_range_end(&range);
Will Deaconae8eba82021-01-27 23:53:43 +0000670 tlb_finish_mmu(&tlb);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800671
672 return 0;
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/*
676 * Application no longer needs these pages. If the pages are dirty,
677 * it's OK to just throw them away. The app will be more careful about
678 * data it wants to keep. Be sure to free swap resources too. The
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700679 * zap_page_range call sets things up for shrink_active_list to actually free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * these pages later if no one else has touched them in the meantime,
681 * although we could add these pages to a global reuse list for
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700682 * shrink_active_list to pick up before reclaiming other pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
684 * NB: This interface discards data rather than pushes it out to swap,
685 * as some implementations do. This has performance implications for
686 * applications like large transactional databases which want to discard
687 * pages in anonymous maps after committing to backing store the data
688 * that was kept in them. There is no reason to write this data out to
689 * the swap area if the application is discarding it.
690 *
691 * An interface that causes the system to free clean pages and flush
692 * dirty pages is already available as msync(MS_INVALIDATE).
693 */
Mike Rapoport230ca982017-07-10 15:49:02 -0700694static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
695 unsigned long start, unsigned long end)
696{
697 zap_page_range(vma, start, end - start);
698 return 0;
699}
700
701static long madvise_dontneed_free(struct vm_area_struct *vma,
702 struct vm_area_struct **prev,
703 unsigned long start, unsigned long end,
704 int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Minchan Kim0726b012020-10-17 16:14:50 -0700706 struct mm_struct *mm = vma->vm_mm;
707
Prasanna Meda05b74382005-06-21 17:14:37 -0700708 *prev = vma;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700709 if (!can_madv_lru_vma(vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EINVAL;
711
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800712 if (!userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700713 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800714
Minchan Kim0726b012020-10-17 16:14:50 -0700715 mmap_read_lock(mm);
716 vma = find_vma(mm, start);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800717 if (!vma)
718 return -ENOMEM;
719 if (start < vma->vm_start) {
720 /*
721 * This "vma" under revalidation is the one
722 * with the lowest vma->vm_start where start
723 * is also < vma->vm_end. If start <
724 * vma->vm_start it means an hole materialized
725 * in the user address space within the
Mike Rapoport230ca982017-07-10 15:49:02 -0700726 * virtual range passed to MADV_DONTNEED
727 * or MADV_FREE.
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800728 */
729 return -ENOMEM;
730 }
Minchan Kim9c276cc2019-09-25 16:49:08 -0700731 if (!can_madv_lru_vma(vma))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800732 return -EINVAL;
733 if (end > vma->vm_end) {
734 /*
735 * Don't fail if end > vma->vm_end. If the old
Ingo Molnarf0953a12021-05-06 18:06:47 -0700736 * vma was split while the mmap_lock was
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800737 * released the effect of the concurrent
Mike Rapoport230ca982017-07-10 15:49:02 -0700738 * operation may not cause madvise() to
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800739 * have an undefined result. There may be an
740 * adjacent next vma that we'll walk
741 * next. userfaultfd_remove() will generate an
742 * UFFD_EVENT_REMOVE repetition on the
743 * end-vma->vm_end range, but the manager can
744 * handle a repetition fine.
745 */
746 end = vma->vm_end;
747 }
748 VM_WARN_ON(start >= end);
749 }
Mike Rapoport230ca982017-07-10 15:49:02 -0700750
751 if (behavior == MADV_DONTNEED)
752 return madvise_dontneed_single_vma(vma, start, end);
753 else if (behavior == MADV_FREE)
754 return madvise_free_single_vma(vma, start, end);
755 else
756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
David Hildenbrand4ca9b3852021-06-30 18:52:28 -0700759static long madvise_populate(struct vm_area_struct *vma,
760 struct vm_area_struct **prev,
761 unsigned long start, unsigned long end,
762 int behavior)
763{
764 const bool write = behavior == MADV_POPULATE_WRITE;
765 struct mm_struct *mm = vma->vm_mm;
766 unsigned long tmp_end;
767 int locked = 1;
768 long pages;
769
770 *prev = vma;
771
772 while (start < end) {
773 /*
774 * We might have temporarily dropped the lock. For example,
775 * our VMA might have been split.
776 */
777 if (!vma || start >= vma->vm_end) {
778 vma = find_vma(mm, start);
779 if (!vma || start < vma->vm_start)
780 return -ENOMEM;
781 }
782
783 tmp_end = min_t(unsigned long, end, vma->vm_end);
784 /* Populate (prefault) page tables readable/writable. */
785 pages = faultin_vma_page_range(vma, start, tmp_end, write,
786 &locked);
787 if (!locked) {
788 mmap_read_lock(mm);
789 locked = 1;
790 *prev = NULL;
791 vma = NULL;
792 }
793 if (pages < 0) {
794 switch (pages) {
795 case -EINTR:
796 return -EINTR;
David Hildenbrandeb2faa52021-08-13 16:54:37 -0700797 case -EINVAL: /* Incompatible mappings / permissions. */
David Hildenbrand4ca9b3852021-06-30 18:52:28 -0700798 return -EINVAL;
799 case -EHWPOISON:
800 return -EHWPOISON;
David Hildenbrandeb2faa52021-08-13 16:54:37 -0700801 case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
802 return -EFAULT;
David Hildenbrand4ca9b3852021-06-30 18:52:28 -0700803 default:
804 pr_warn_once("%s: unhandled return value: %ld\n",
805 __func__, pages);
806 fallthrough;
807 case -ENOMEM:
808 return -ENOMEM;
809 }
810 }
811 start += pages * PAGE_SIZE;
812 }
813 return 0;
814}
815
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800816/*
817 * Application wants to free up the pages and associated backing store.
818 * This is effectively punching a hole into the middle of a file.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800819 */
820static long madvise_remove(struct vm_area_struct *vma,
Nick Piggin00e9fa22007-03-16 13:38:10 -0800821 struct vm_area_struct **prev,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800822 unsigned long start, unsigned long end)
823{
Hugh Dickins3f31d072012-05-29 15:06:40 -0700824 loff_t offset;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700825 int error;
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700826 struct file *f;
Minchan Kim0726b012020-10-17 16:14:50 -0700827 struct mm_struct *mm = vma->vm_mm;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800828
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700829 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Nick Piggin00e9fa22007-03-16 13:38:10 -0800830
Mike Kravetz72079ba2015-09-08 15:01:57 -0700831 if (vma->vm_flags & VM_LOCKED)
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800832 return -EINVAL;
833
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700834 f = vma->vm_file;
835
836 if (!f || !f->f_mapping || !f->f_mapping->host) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800837 return -EINVAL;
838 }
839
Hugh Dickins69cf0fa2006-04-17 22:46:32 +0100840 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
841 return -EACCES;
842
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800843 offset = (loff_t)(start - vma->vm_start)
844 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700845
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700846 /*
Jan Kara96087032021-04-12 15:50:21 +0200847 * Filesystem's fallocate may need to take i_rwsem. We need to
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700848 * explicitly grab a reference because the vma (and hence the
849 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700850 * mmap_lock.
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700851 */
852 get_file(f);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800853 if (userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700854 /* mmap_lock was not released by userfaultfd_remove() */
Minchan Kim0726b012020-10-17 16:14:50 -0700855 mmap_read_unlock(mm);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800856 }
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500857 error = vfs_fallocate(f,
Hugh Dickins3f31d072012-05-29 15:06:40 -0700858 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
859 offset, end - start);
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700860 fput(f);
Minchan Kim0726b012020-10-17 16:14:50 -0700861 mmap_read_lock(mm);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700862 return error;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800863}
864
Colin Crossac1e9ac2022-01-14 14:05:55 -0800865/*
866 * Apply an madvise behavior to a region of a vma. madvise_update_vma
867 * will handle splitting a vm area into separate areas, each area with its own
868 * behavior.
869 */
870static int madvise_vma_behavior(struct vm_area_struct *vma,
871 struct vm_area_struct **prev,
872 unsigned long start, unsigned long end,
873 unsigned long behavior)
874{
875 int error;
876 unsigned long new_flags = vma->vm_flags;
877
878 switch (behavior) {
879 case MADV_REMOVE:
880 return madvise_remove(vma, prev, start, end);
881 case MADV_WILLNEED:
882 return madvise_willneed(vma, prev, start, end);
883 case MADV_COLD:
884 return madvise_cold(vma, prev, start, end);
885 case MADV_PAGEOUT:
886 return madvise_pageout(vma, prev, start, end);
887 case MADV_FREE:
888 case MADV_DONTNEED:
889 return madvise_dontneed_free(vma, prev, start, end, behavior);
890 case MADV_POPULATE_READ:
891 case MADV_POPULATE_WRITE:
892 return madvise_populate(vma, prev, start, end, behavior);
893 case MADV_NORMAL:
894 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
895 break;
896 case MADV_SEQUENTIAL:
897 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
898 break;
899 case MADV_RANDOM:
900 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
901 break;
902 case MADV_DONTFORK:
903 new_flags |= VM_DONTCOPY;
904 break;
905 case MADV_DOFORK:
906 if (vma->vm_flags & VM_IO)
907 return -EINVAL;
908 new_flags &= ~VM_DONTCOPY;
909 break;
910 case MADV_WIPEONFORK:
911 /* MADV_WIPEONFORK is only supported on anonymous memory. */
912 if (vma->vm_file || vma->vm_flags & VM_SHARED)
913 return -EINVAL;
914 new_flags |= VM_WIPEONFORK;
915 break;
916 case MADV_KEEPONFORK:
917 new_flags &= ~VM_WIPEONFORK;
918 break;
919 case MADV_DONTDUMP:
920 new_flags |= VM_DONTDUMP;
921 break;
922 case MADV_DODUMP:
923 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL)
924 return -EINVAL;
925 new_flags &= ~VM_DONTDUMP;
926 break;
927 case MADV_MERGEABLE:
928 case MADV_UNMERGEABLE:
929 error = ksm_madvise(vma, start, end, behavior, &new_flags);
930 if (error)
931 goto out;
932 break;
933 case MADV_HUGEPAGE:
934 case MADV_NOHUGEPAGE:
935 error = hugepage_madvise(vma, &new_flags, behavior);
936 if (error)
937 goto out;
938 break;
939 }
940
941 error = madvise_update_vma(vma, prev, start, end, new_flags);
942
943out:
944 /*
945 * madvise() returns EAGAIN if kernel resources, such as
946 * slab, are temporarily unavailable.
947 */
948 if (error == -ENOMEM)
949 error = -EAGAIN;
950 return error;
951}
952
Andi Kleen9893e492009-09-16 11:50:17 +0200953#ifdef CONFIG_MEMORY_FAILURE
954/*
955 * Error injection support for memory error handling.
956 */
Anshuman Khandual97167a72017-05-03 14:55:25 -0700957static int madvise_inject_error(int behavior,
958 unsigned long start, unsigned long end)
Andi Kleen9893e492009-09-16 11:50:17 +0200959{
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800960 unsigned long size;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700961
Andi Kleen9893e492009-09-16 11:50:17 +0200962 if (!capable(CAP_SYS_ADMIN))
963 return -EPERM;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700964
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700965
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800966 for (; start < end; start += size) {
Dan Williams23e7b5c2018-07-13 21:50:06 -0700967 unsigned long pfn;
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700968 struct page *page;
Andrew Morton325c4ef2013-09-11 14:23:03 -0700969 int ret;
970
Anshuman Khandual97167a72017-05-03 14:55:25 -0700971 ret = get_user_pages_fast(start, 1, 0, &page);
Andi Kleen9893e492009-09-16 11:50:17 +0200972 if (ret != 1)
973 return ret;
Dan Williams23e7b5c2018-07-13 21:50:06 -0700974 pfn = page_to_pfn(page);
Andrew Morton325c4ef2013-09-11 14:23:03 -0700975
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700976 /*
977 * When soft offlining hugepages, after migrating the page
978 * we dissolve it, therefore in the second loop "page" will
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800979 * no longer be a compound page.
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700980 */
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800981 size = page_size(compound_head(page));
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700982
Anshuman Khandual97167a72017-05-03 14:55:25 -0700983 if (behavior == MADV_SOFT_OFFLINE) {
984 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700985 pfn, start);
Naoya Horiguchifeec24a2019-11-30 17:53:38 -0800986 ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700987 } else {
988 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
989 pfn, start);
Oscar Salvador1e8aaed2020-12-14 19:11:48 -0800990 ret = memory_failure(pfn, MF_COUNT_INCREASED);
Andi Kleenafcf9382009-12-16 12:20:00 +0100991 }
Anshuman Khandual97167a72017-05-03 14:55:25 -0700992
Naoya Horiguchi23a003b2016-03-15 14:56:36 -0700993 if (ret)
994 return ret;
Andi Kleen9893e492009-09-16 11:50:17 +0200995 }
Mel Gormanc461ad62017-08-31 16:15:30 -0700996
Andrew Morton325c4ef2013-09-11 14:23:03 -0700997 return 0;
Andi Kleen9893e492009-09-16 11:50:17 +0200998}
999#endif
1000
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001001static bool
Nick Piggin75927af2009-06-16 15:32:38 -07001002madvise_behavior_valid(int behavior)
1003{
1004 switch (behavior) {
1005 case MADV_DOFORK:
1006 case MADV_DONTFORK:
1007 case MADV_NORMAL:
1008 case MADV_SEQUENTIAL:
1009 case MADV_RANDOM:
1010 case MADV_REMOVE:
1011 case MADV_WILLNEED:
1012 case MADV_DONTNEED:
Minchan Kim854e9ed2016-01-15 16:54:53 -08001013 case MADV_FREE:
Minchan Kim9c276cc2019-09-25 16:49:08 -07001014 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -07001015 case MADV_PAGEOUT:
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001016 case MADV_POPULATE_READ:
1017 case MADV_POPULATE_WRITE:
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001018#ifdef CONFIG_KSM
1019 case MADV_MERGEABLE:
1020 case MADV_UNMERGEABLE:
1021#endif
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001022#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1023 case MADV_HUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001024 case MADV_NOHUGEPAGE:
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001025#endif
Jason Baronaccb61f2012-03-23 15:02:51 -07001026 case MADV_DONTDUMP:
1027 case MADV_DODUMP:
Rik van Rield2cd9ed2017-09-06 16:25:15 -07001028 case MADV_WIPEONFORK:
1029 case MADV_KEEPONFORK:
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001030#ifdef CONFIG_MEMORY_FAILURE
1031 case MADV_SOFT_OFFLINE:
1032 case MADV_HWPOISON:
1033#endif
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001034 return true;
Nick Piggin75927af2009-06-16 15:32:38 -07001035
1036 default:
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001037 return false;
Nick Piggin75927af2009-06-16 15:32:38 -07001038 }
1039}
Hugh Dickins3866ea92009-09-21 17:01:52 -07001040
Minchan Kimecb8ac82020-10-17 16:14:59 -07001041static bool
1042process_madvise_behavior_valid(int behavior)
1043{
1044 switch (behavior) {
1045 case MADV_COLD:
1046 case MADV_PAGEOUT:
zhangkuid5fffc52021-09-02 15:01:11 -07001047 case MADV_WILLNEED:
Minchan Kimecb8ac82020-10-17 16:14:59 -07001048 return true;
1049 default:
1050 return false;
1051 }
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
Colin Crossac1e9ac2022-01-14 14:05:55 -08001055 * Walk the vmas in range [start,end), and call the visit function on each one.
1056 * The visit function will get start and end parameters that cover the overlap
1057 * between the current vma and the original range. Any unmapped regions in the
1058 * original range will result in this function returning -ENOMEM while still
1059 * calling the visit function on all of the existing vmas in the range.
1060 * Must be called with the mmap_lock held for reading or writing.
1061 */
1062static
1063int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
1064 unsigned long end, unsigned long arg,
1065 int (*visit)(struct vm_area_struct *vma,
1066 struct vm_area_struct **prev, unsigned long start,
1067 unsigned long end, unsigned long arg))
1068{
1069 struct vm_area_struct *vma;
1070 struct vm_area_struct *prev;
1071 unsigned long tmp;
1072 int unmapped_error = 0;
1073
1074 /*
1075 * If the interval [start,end) covers some unmapped address
1076 * ranges, just ignore them, but return -ENOMEM at the end.
1077 * - different from the way of handling in mlock etc.
1078 */
1079 vma = find_vma_prev(mm, start, &prev);
1080 if (vma && start > vma->vm_start)
1081 prev = vma;
1082
1083 for (;;) {
1084 int error;
1085
1086 /* Still start < end. */
1087 if (!vma)
1088 return -ENOMEM;
1089
1090 /* Here start < (end|vma->vm_end). */
1091 if (start < vma->vm_start) {
1092 unmapped_error = -ENOMEM;
1093 start = vma->vm_start;
1094 if (start >= end)
1095 break;
1096 }
1097
1098 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1099 tmp = vma->vm_end;
1100 if (end < tmp)
1101 tmp = end;
1102
1103 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1104 error = visit(vma, &prev, start, tmp, arg);
1105 if (error)
1106 return error;
1107 start = tmp;
1108 if (prev && start < prev->vm_end)
1109 start = prev->vm_end;
1110 if (start >= end)
1111 break;
1112 if (prev)
1113 vma = prev->vm_next;
1114 else /* madvise_remove dropped mmap_lock */
1115 vma = find_vma(mm, start);
1116 }
1117
1118 return unmapped_error;
1119}
1120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * The madvise(2) system call.
1123 *
1124 * Applications can use madvise() to advise the kernel how it should
1125 * handle paging I/O in this VM area. The idea is to help the kernel
1126 * use appropriate read-ahead and caching techniques. The information
1127 * provided is advisory only, and can be safely disregarded by the
1128 * kernel without affecting the correct operation of the application.
1129 *
1130 * behavior values:
1131 * MADV_NORMAL - the default behavior is to read clusters. This
1132 * results in some read-ahead and read-behind.
1133 * MADV_RANDOM - the system should read the minimum amount of data
1134 * on any access, since it is unlikely that the appli-
1135 * cation will need more than what it asks for.
1136 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1137 * once, so they can be aggressively read ahead, and
1138 * can be freed soon after they are accessed.
1139 * MADV_WILLNEED - the application is notifying the system to read
1140 * some pages ahead.
1141 * MADV_DONTNEED - the application is finished with the given range,
1142 * so the kernel can free resources associated with it.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001143 * MADV_FREE - the application marks pages in the given range as lazy free,
1144 * where actual purges are postponed until memory pressure happens.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08001145 * MADV_REMOVE - the application wants to free up the given range of
1146 * pages and associated backing store.
Hugh Dickins3866ea92009-09-21 17:01:52 -07001147 * MADV_DONTFORK - omit this area from child's address space when forking:
1148 * typically, to avoid COWing pages pinned by get_user_pages().
1149 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
Yang Shic02c3002017-10-13 15:57:37 -07001150 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1151 * range after a fork.
1152 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001153 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1154 * were corrupted by unrecoverable hardware memory failure.
1155 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001156 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1157 * this area with pages of identical content from other such areas.
1158 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001159 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1160 * huge pages in the future. Existing pages might be coalesced and
1161 * new pages might be allocated as THP.
1162 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1163 * transparent huge pages so the existing pages will not be
1164 * coalesced into THP and new pages will not be allocated as THP.
1165 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1166 * from being included in its core dump.
1167 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
Minchan Kimecb8ac82020-10-17 16:14:59 -07001168 * MADV_COLD - the application is not expected to use this memory soon,
1169 * deactivate pages in this range so that they can be reclaimed
Ingo Molnarf0953a12021-05-06 18:06:47 -07001170 * easily if memory pressure happens.
Minchan Kimecb8ac82020-10-17 16:14:59 -07001171 * MADV_PAGEOUT - the application is not expected to use this memory soon,
1172 * page out the pages in this range immediately.
David Hildenbrand4ca9b3852021-06-30 18:52:28 -07001173 * MADV_POPULATE_READ - populate (prefault) page tables readable by
1174 * triggering read faults if required
1175 * MADV_POPULATE_WRITE - populate (prefault) page tables writable by
1176 * triggering write faults if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 *
1178 * return values:
1179 * zero - success
1180 * -EINVAL - start + len < 0, start is not page-aligned,
1181 * "behavior" is not a valid value, or application
Yang Shic02c3002017-10-13 15:57:37 -07001182 * is attempting to release locked or shared pages,
1183 * or the specified address range includes file, Huge TLB,
1184 * MAP_SHARED or VMPFNMAP range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * -ENOMEM - addresses in the specified range are not currently
1186 * mapped, or are outside the AS of the process.
1187 * -EIO - an I/O error occurred while paging in data.
1188 * -EBADF - map exists, but area maps something that isn't a file.
1189 * -EAGAIN - a kernel resource was temporarily unavailable.
1190 */
Minchan Kim0726b012020-10-17 16:14:50 -07001191int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Colin Crossac1e9ac2022-01-14 14:05:55 -08001193 unsigned long end;
1194 int error;
Jason Baronf7977792007-07-15 23:38:21 -07001195 int write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 size_t len;
Shaohua Li1998cc02013-02-22 16:32:31 -08001197 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Andrey Konovalov057d33892019-09-25 16:48:30 -07001199 start = untagged_addr(start);
1200
Nick Piggin75927af2009-06-16 15:32:38 -07001201 if (!madvise_behavior_valid(behavior))
Colin Crossac1e9ac2022-01-14 14:05:55 -08001202 return -EINVAL;
Nick Piggin75927af2009-06-16 15:32:38 -07001203
Wei Yangdf6c6502019-11-30 17:57:46 -08001204 if (!PAGE_ALIGNED(start))
Colin Crossac1e9ac2022-01-14 14:05:55 -08001205 return -EINVAL;
Wei Yangdf6c6502019-11-30 17:57:46 -08001206 len = PAGE_ALIGN(len_in);
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001207
1208 /* Check to see whether len was rounded up from small -ve to zero */
1209 if (len_in && !len)
Colin Crossac1e9ac2022-01-14 14:05:55 -08001210 return -EINVAL;
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001211
1212 end = start + len;
1213 if (end < start)
Colin Crossac1e9ac2022-01-14 14:05:55 -08001214 return -EINVAL;
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001215
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001216 if (end == start)
Colin Crossac1e9ac2022-01-14 14:05:55 -08001217 return 0;
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001218
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001219#ifdef CONFIG_MEMORY_FAILURE
1220 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1221 return madvise_inject_error(behavior, start, start + len_in);
1222#endif
1223
Jason Baronf7977792007-07-15 23:38:21 -07001224 write = madvise_need_mmap_write(behavior);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001225 if (write) {
Minchan Kim0726b012020-10-17 16:14:50 -07001226 if (mmap_write_lock_killable(mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001227 return -EINTR;
1228 } else {
Minchan Kim0726b012020-10-17 16:14:50 -07001229 mmap_read_lock(mm);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Shaohua Li1998cc02013-02-22 16:32:31 -08001232 blk_start_plug(&plug);
Colin Crossac1e9ac2022-01-14 14:05:55 -08001233 error = madvise_walk_vmas(mm, start, end, behavior,
1234 madvise_vma_behavior);
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001235 blk_finish_plug(&plug);
Jason Baronf7977792007-07-15 23:38:21 -07001236 if (write)
Minchan Kim0726b012020-10-17 16:14:50 -07001237 mmap_write_unlock(mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001238 else
Minchan Kim0726b012020-10-17 16:14:50 -07001239 mmap_read_unlock(mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return error;
1242}
Jens Axboedb08ca22019-12-25 22:14:54 -07001243
1244SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1245{
Minchan Kim0726b012020-10-17 16:14:50 -07001246 return do_madvise(current->mm, start, len_in, behavior);
Jens Axboedb08ca22019-12-25 22:14:54 -07001247}
Minchan Kimecb8ac82020-10-17 16:14:59 -07001248
1249SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1250 size_t, vlen, int, behavior, unsigned int, flags)
1251{
1252 ssize_t ret;
1253 struct iovec iovstack[UIO_FASTIOV], iovec;
1254 struct iovec *iov = iovstack;
1255 struct iov_iter iter;
Minchan Kimecb8ac82020-10-17 16:14:59 -07001256 struct task_struct *task;
1257 struct mm_struct *mm;
1258 size_t total_len;
1259 unsigned int f_flags;
1260
1261 if (flags != 0) {
1262 ret = -EINVAL;
1263 goto out;
1264 }
1265
1266 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1267 if (ret < 0)
1268 goto out;
1269
Christian Brauneree9955d2021-10-11 15:32:45 +02001270 task = pidfd_get_task(pidfd, &f_flags);
1271 if (IS_ERR(task)) {
1272 ret = PTR_ERR(task);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001273 goto free_iov;
1274 }
1275
Minchan Kima68a0262020-12-08 20:57:18 -08001276 if (!process_madvise_behavior_valid(behavior)) {
Minchan Kimecb8ac82020-10-17 16:14:59 -07001277 ret = -EINVAL;
1278 goto release_task;
1279 }
1280
Suren Baghdasaryan96cfe2c2021-03-12 21:08:06 -08001281 /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1282 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001283 if (IS_ERR_OR_NULL(mm)) {
1284 ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1285 goto release_task;
1286 }
1287
Suren Baghdasaryan96cfe2c2021-03-12 21:08:06 -08001288 /*
1289 * Require CAP_SYS_NICE for influencing process performance. Note that
1290 * only non-destructive hints are currently supported.
1291 */
1292 if (!capable(CAP_SYS_NICE)) {
1293 ret = -EPERM;
1294 goto release_mm;
1295 }
1296
Minchan Kimecb8ac82020-10-17 16:14:59 -07001297 total_len = iov_iter_count(&iter);
1298
1299 while (iov_iter_count(&iter)) {
1300 iovec = iov_iter_iovec(&iter);
1301 ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1302 iovec.iov_len, behavior);
1303 if (ret < 0)
1304 break;
1305 iov_iter_advance(&iter, iovec.iov_len);
1306 }
1307
1308 if (ret == 0)
1309 ret = total_len - iov_iter_count(&iter);
1310
Suren Baghdasaryan96cfe2c2021-03-12 21:08:06 -08001311release_mm:
Minchan Kimecb8ac82020-10-17 16:14:59 -07001312 mmput(mm);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001313release_task:
1314 put_task_struct(task);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001315free_iov:
1316 kfree(iov);
1317out:
1318 return ret;
1319}