blob: 3e431f71fb272c5a2e17daa73489b0de17b8d8e2 [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>
Bing Hane3f469b2022-05-30 14:34:16 +080032#include <trace/hooks/mm.h>
Minchan Kim854e9ed2016-01-15 16:54:53 -080033
34#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Kirill A. Shutemov23519072017-02-22 15:46:39 -080036#include "internal.h"
37
Minchan Kimd616d512019-09-25 16:49:19 -070038struct madvise_walk_private {
39 struct mmu_gather *tlb;
40 bool pageout;
Pavankumar Kondetid84fac92022-11-09 10:48:36 +053041 bool can_pageout_file;
Minchan Kimd616d512019-09-25 16:49:19 -070042};
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
Nick Piggin0a27a142007-05-06 14:49:53 -070045 * Any behaviour which results in changes to the vma->vm_flags needs to
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070046 * take mmap_lock for writing. Others, which simply traverse vmas, need
Nick Piggin0a27a142007-05-06 14:49:53 -070047 * to only take it for reading.
48 */
49static int madvise_need_mmap_write(int behavior)
50{
51 switch (behavior) {
52 case MADV_REMOVE:
53 case MADV_WILLNEED:
54 case MADV_DONTNEED:
Minchan Kim9c276cc2019-09-25 16:49:08 -070055 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -070056 case MADV_PAGEOUT:
Minchan Kim854e9ed2016-01-15 16:54:53 -080057 case MADV_FREE:
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * We can potentially split a vm area into separate
67 * areas, each area with its own behavior.
68 */
Vladimir Cernovec9bed92013-09-11 14:20:15 -070069static long madvise_behavior(struct vm_area_struct *vma,
Prasanna Meda05b74382005-06-21 17:14:37 -070070 struct vm_area_struct **prev,
71 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Vladimir Cernovec9bed92013-09-11 14:20:15 -070073 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int error = 0;
Prasanna Meda05b74382005-06-21 17:14:37 -070075 pgoff_t pgoff;
Hugh Dickins3866ea92009-09-21 17:01:52 -070076 unsigned long new_flags = vma->vm_flags;
Prasanna Medae798c6e2005-06-21 17:14:36 -070077
78 switch (behavior) {
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080079 case MADV_NORMAL:
80 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
81 break;
Prasanna Medae798c6e2005-06-21 17:14:36 -070082 case MADV_SEQUENTIAL:
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080083 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
Prasanna Medae798c6e2005-06-21 17:14:36 -070084 break;
85 case MADV_RANDOM:
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080086 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
Prasanna Medae798c6e2005-06-21 17:14:36 -070087 break;
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080088 case MADV_DONTFORK:
89 new_flags |= VM_DONTCOPY;
90 break;
91 case MADV_DOFORK:
Hugh Dickins3866ea92009-09-21 17:01:52 -070092 if (vma->vm_flags & VM_IO) {
93 error = -EINVAL;
94 goto out;
95 }
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080096 new_flags &= ~VM_DONTCOPY;
Prasanna Medae798c6e2005-06-21 17:14:36 -070097 break;
Rik van Rield2cd9ed2017-09-06 16:25:15 -070098 case MADV_WIPEONFORK:
99 /* MADV_WIPEONFORK is only supported on anonymous memory. */
100 if (vma->vm_file || vma->vm_flags & VM_SHARED) {
101 error = -EINVAL;
102 goto out;
103 }
104 new_flags |= VM_WIPEONFORK;
105 break;
106 case MADV_KEEPONFORK:
107 new_flags &= ~VM_WIPEONFORK;
108 break;
Jason Baronaccb61f2012-03-23 15:02:51 -0700109 case MADV_DONTDUMP:
Konstantin Khlebnikov0103bd12012-10-08 16:28:59 -0700110 new_flags |= VM_DONTDUMP;
Jason Baronaccb61f2012-03-23 15:02:51 -0700111 break;
112 case MADV_DODUMP:
Daniel Blackd41aa522018-10-05 15:52:19 -0700113 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
Konstantin Khlebnikov0103bd12012-10-08 16:28:59 -0700114 error = -EINVAL;
115 goto out;
116 }
117 new_flags &= ~VM_DONTDUMP;
Jason Baronaccb61f2012-03-23 15:02:51 -0700118 break;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700119 case MADV_MERGEABLE:
120 case MADV_UNMERGEABLE:
121 error = ksm_madvise(vma, start, end, behavior, &new_flags);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700122 if (error)
123 goto out_convert_errno;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700124 break;
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800125 case MADV_HUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -0800126 case MADV_NOHUGEPAGE:
Andrea Arcangeli60ab3242011-01-13 15:47:18 -0800127 error = hugepage_madvise(vma, &new_flags, behavior);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700128 if (error)
129 goto out_convert_errno;
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800130 break;
Prasanna Medae798c6e2005-06-21 17:14:36 -0700131 }
132
Prasanna Meda05b74382005-06-21 17:14:37 -0700133 if (new_flags == vma->vm_flags) {
134 *prev = vma;
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700135 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -0700136 }
137
138 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
139 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700140 vma->vm_file, pgoff, vma_policy(vma),
Colin Cross60500a42015-10-27 16:42:08 -0700141 vma->vm_userfaultfd_ctx, vma_get_anon_name(vma));
Prasanna Meda05b74382005-06-21 17:14:37 -0700142 if (*prev) {
143 vma = *prev;
144 goto success;
145 }
146
147 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (start != vma->vm_start) {
David Rientjesdef5efe2017-02-24 14:58:47 -0800150 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
151 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto out;
David Rientjesdef5efe2017-02-24 14:58:47 -0800153 }
154 error = __split_vma(mm, vma, start, 1);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700155 if (error)
156 goto out_convert_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159 if (end != vma->vm_end) {
David Rientjesdef5efe2017-02-24 14:58:47 -0800160 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
161 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 goto out;
David Rientjesdef5efe2017-02-24 14:58:47 -0800163 }
164 error = __split_vma(mm, vma, end, 0);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700165 if (error)
166 goto out_convert_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700169success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700171 * vm_flags is protected by the mmap_lock held in write mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200173 vm_write_begin(vma);
174 WRITE_ONCE(vma->vm_flags, new_flags);
175 vm_write_end(vma);
Mike Rapoportf3bc0db2019-09-23 15:39:31 -0700176
177out_convert_errno:
178 /*
179 * madvise() returns EAGAIN if kernel resources, such as
180 * slab, are temporarily unavailable.
181 */
182 if (error == -ENOMEM)
183 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return error;
186}
187
Shaohua Li1998cc02013-02-22 16:32:31 -0800188#ifdef CONFIG_SWAP
189static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
190 unsigned long end, struct mm_walk *walk)
191{
192 pte_t *orig_pte;
193 struct vm_area_struct *vma = walk->private;
194 unsigned long index;
195
196 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
197 return 0;
198
199 for (index = start; index != end; index += PAGE_SIZE) {
200 pte_t pte;
201 swp_entry_t entry;
202 struct page *page;
203 spinlock_t *ptl;
204
205 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
206 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
207 pte_unmap_unlock(orig_pte, ptl);
208
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800209 if (pte_present(pte) || pte_none(pte))
Shaohua Li1998cc02013-02-22 16:32:31 -0800210 continue;
211 entry = pte_to_swp_entry(pte);
212 if (unlikely(non_swap_entry(entry)))
213 continue;
214
215 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
Shaohua Li23955622017-07-10 15:47:11 -0700216 vma, index, false);
Shaohua Li1998cc02013-02-22 16:32:31 -0800217 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300218 put_page(page);
Shaohua Li1998cc02013-02-22 16:32:31 -0800219 }
220
221 return 0;
222}
223
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200224static const struct mm_walk_ops swapin_walk_ops = {
225 .pmd_entry = swapin_walk_pmd_entry,
226};
Shaohua Li1998cc02013-02-22 16:32:31 -0800227
228static void force_shm_swapin_readahead(struct vm_area_struct *vma,
229 unsigned long start, unsigned long end,
230 struct address_space *mapping)
231{
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700232 XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
Matthew Wilcox (Oracle)66383802020-11-21 22:17:22 -0800233 pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
Shaohua Li1998cc02013-02-22 16:32:31 -0800234 struct page *page;
Shaohua Li1998cc02013-02-22 16:32:31 -0800235
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700236 rcu_read_lock();
237 xas_for_each(&xas, page, end_index) {
238 swp_entry_t swap;
Shaohua Li1998cc02013-02-22 16:32:31 -0800239
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700240 if (!xa_is_value(page))
Shaohua Li1998cc02013-02-22 16:32:31 -0800241 continue;
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700242 xas_pause(&xas);
243 rcu_read_unlock();
244
Shaohua Li1998cc02013-02-22 16:32:31 -0800245 swap = radix_to_swp_entry(page);
246 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
Shaohua Li23955622017-07-10 15:47:11 -0700247 NULL, 0, false);
Shaohua Li1998cc02013-02-22 16:32:31 -0800248 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 put_page(page);
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700250
251 rcu_read_lock();
Shaohua Li1998cc02013-02-22 16:32:31 -0800252 }
Matthew Wilcox (Oracle)e6e88712020-10-13 16:51:24 -0700253 rcu_read_unlock();
Shaohua Li1998cc02013-02-22 16:32:31 -0800254
255 lru_add_drain(); /* Push any new pages onto the LRU now */
256}
257#endif /* CONFIG_SWAP */
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * Schedule all required I/O operations. Do not wait for completion.
261 */
Vladimir Cernovec9bed92013-09-11 14:20:15 -0700262static long madvise_willneed(struct vm_area_struct *vma,
263 struct vm_area_struct **prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned long start, unsigned long end)
265{
Minchan Kim0726b012020-10-17 16:14:50 -0700266 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct file *file = vma->vm_file;
Jan Kara692fe622019-08-29 09:04:11 -0700268 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
chenjie6ea8d952017-11-29 16:10:54 -0800270 *prev = vma;
Shaohua Li1998cc02013-02-22 16:32:31 -0800271#ifdef CONFIG_SWAP
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100272 if (!file) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200273 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
274 lru_add_drain(); /* Push any new pages onto the LRU now */
Shaohua Li1998cc02013-02-22 16:32:31 -0800275 return 0;
276 }
Shaohua Li1998cc02013-02-22 16:32:31 -0800277
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100278 if (shmem_mapping(file->f_mapping)) {
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100279 force_shm_swapin_readahead(vma, start, end,
280 file->f_mapping);
281 return 0;
282 }
283#else
Suzuki1bef4002005-10-11 08:29:06 -0700284 if (!file)
285 return -EBADF;
Christoph Hellwig97b713b2015-01-14 10:42:31 +0100286#endif
Suzuki1bef4002005-10-11 08:29:06 -0700287
Matthew Wilcoxe748dcd2015-02-16 15:59:12 -0800288 if (IS_DAX(file_inode(file))) {
Carsten Ottefe77ba62005-06-23 22:05:29 -0700289 /* no bad return value, but ignore advice */
290 return 0;
291 }
292
Jan Kara692fe622019-08-29 09:04:11 -0700293 /*
294 * Filesystem's fadvise may need to take various locks. We need to
295 * explicitly grab a reference because the vma (and hence the
296 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700297 * mmap_lock.
Jan Kara692fe622019-08-29 09:04:11 -0700298 */
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700299 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Jan Kara692fe622019-08-29 09:04:11 -0700300 get_file(file);
Jan Kara692fe622019-08-29 09:04:11 -0700301 offset = (loff_t)(start - vma->vm_start)
302 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Minchan Kim0726b012020-10-17 16:14:50 -0700303 mmap_read_unlock(mm);
Jan Kara692fe622019-08-29 09:04:11 -0700304 vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
305 fput(file);
Minchan Kim0726b012020-10-17 16:14:50 -0700306 mmap_read_lock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308}
309
Minchan Kimd616d512019-09-25 16:49:19 -0700310static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
311 unsigned long addr, unsigned long end,
312 struct mm_walk *walk)
Minchan Kim9c276cc2019-09-25 16:49:08 -0700313{
Minchan Kimd616d512019-09-25 16:49:19 -0700314 struct madvise_walk_private *private = walk->private;
315 struct mmu_gather *tlb = private->tlb;
316 bool pageout = private->pageout;
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530317 bool pageout_anon_only = pageout && !private->can_pageout_file;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700318 struct mm_struct *mm = tlb->mm;
319 struct vm_area_struct *vma = walk->vma;
320 pte_t *orig_pte, *pte, ptent;
321 spinlock_t *ptl;
Minchan Kimd616d512019-09-25 16:49:19 -0700322 struct page *page = NULL;
323 LIST_HEAD(page_list);
Pavankumar Kondeti6d04d8c2022-09-26 18:36:10 +0530324 bool allow_shared = false;
Minchan Kimd616d512019-09-25 16:49:19 -0700325
326 if (fatal_signal_pending(current))
327 return -EINTR;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700328
Pavankumar Kondeti6d04d8c2022-09-26 18:36:10 +0530329 trace_android_vh_madvise_cold_or_pageout(vma, &allow_shared);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700330#ifdef CONFIG_TRANSPARENT_HUGEPAGE
331 if (pmd_trans_huge(*pmd)) {
332 pmd_t orig_pmd;
333 unsigned long next = pmd_addr_end(addr, end);
334
335 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
336 ptl = pmd_trans_huge_lock(pmd, vma);
337 if (!ptl)
338 return 0;
339
340 orig_pmd = *pmd;
341 if (is_huge_zero_pmd(orig_pmd))
342 goto huge_unlock;
343
344 if (unlikely(!pmd_present(orig_pmd))) {
345 VM_BUG_ON(thp_migration_supported() &&
346 !is_pmd_migration_entry(orig_pmd));
347 goto huge_unlock;
348 }
349
350 page = pmd_page(orig_pmd);
Michal Hocko12e967f2020-03-21 18:22:26 -0700351
352 /* Do not interfere with other mappings of this page */
353 if (page_mapcount(page) != 1)
354 goto huge_unlock;
355
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530356 if (pageout_anon_only && !PageAnon(page))
357 goto huge_unlock;
358
Minchan Kim9c276cc2019-09-25 16:49:08 -0700359 if (next - addr != HPAGE_PMD_SIZE) {
360 int err;
361
Minchan Kim9c276cc2019-09-25 16:49:08 -0700362 get_page(page);
363 spin_unlock(ptl);
364 lock_page(page);
365 err = split_huge_page(page);
366 unlock_page(page);
367 put_page(page);
368 if (!err)
369 goto regular_page;
370 return 0;
371 }
372
373 if (pmd_young(orig_pmd)) {
374 pmdp_invalidate(vma, addr, pmd);
375 orig_pmd = pmd_mkold(orig_pmd);
376
377 set_pmd_at(mm, addr, pmd, orig_pmd);
378 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
379 }
380
Minchan Kimd616d512019-09-25 16:49:19 -0700381 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700382 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700383 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800384 if (!isolate_lru_page(page)) {
385 if (PageUnevictable(page))
386 putback_lru_page(page);
387 else
388 list_add(&page->lru, &page_list);
389 }
Minchan Kimd616d512019-09-25 16:49:19 -0700390 } else
391 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700392huge_unlock:
393 spin_unlock(ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700394 if (pageout)
395 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700396 return 0;
397 }
398
Minchan Kimce268422020-09-14 23:32:15 -0700399regular_page:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700400 if (pmd_trans_unstable(pmd))
401 return 0;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700402#endif
403 tlb_change_page_size(tlb, PAGE_SIZE);
404 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
405 flush_tlb_batched_pending(mm);
406 arch_enter_lazy_mmu_mode();
407 for (; addr < end; pte++, addr += PAGE_SIZE) {
408 ptent = *pte;
409
410 if (pte_none(ptent))
411 continue;
412
413 if (!pte_present(ptent))
414 continue;
415
416 page = vm_normal_page(vma, addr, ptent);
417 if (!page)
418 continue;
419
420 /*
421 * Creating a THP page is expensive so split it only if we
422 * are sure it's worth. Split it if we are only owner.
423 */
424 if (PageTransCompound(page)) {
425 if (page_mapcount(page) != 1)
426 break;
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530427 if (pageout_anon_only && !PageAnon(page))
428 break;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700429 get_page(page);
430 if (!trylock_page(page)) {
431 put_page(page);
432 break;
433 }
434 pte_unmap_unlock(orig_pte, ptl);
435 if (split_huge_page(page)) {
436 unlock_page(page);
437 put_page(page);
438 pte_offset_map_lock(mm, pmd, addr, &ptl);
439 break;
440 }
441 unlock_page(page);
442 put_page(page);
443 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
444 pte--;
445 addr -= PAGE_SIZE;
446 continue;
447 }
448
Michal Hocko12e967f2020-03-21 18:22:26 -0700449 /* Do not interfere with other mappings of this page */
Pavankumar Kondeti6d04d8c2022-09-26 18:36:10 +0530450 if (!allow_shared && page_mapcount(page) != 1)
Michal Hocko12e967f2020-03-21 18:22:26 -0700451 continue;
452
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530453 if (pageout_anon_only && !PageAnon(page))
454 continue;
455
Minchan Kim9c276cc2019-09-25 16:49:08 -0700456 VM_BUG_ON_PAGE(PageTransCompound(page), page);
457
458 if (pte_young(ptent)) {
459 ptent = ptep_get_and_clear_full(mm, addr, pte,
460 tlb->fullmm);
461 ptent = pte_mkold(ptent);
462 set_pte_at(mm, addr, pte, ptent);
463 tlb_remove_tlb_entry(tlb, pte, addr);
464 }
465
466 /*
467 * We are deactivating a page for accelerating reclaiming.
468 * VM couldn't reclaim the page unless we clear PG_young.
469 * As a side effect, it makes confuse idle-page tracking
470 * because they will miss recent referenced history.
471 */
Minchan Kimd616d512019-09-25 16:49:19 -0700472 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700473 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700474 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800475 if (!isolate_lru_page(page)) {
476 if (PageUnevictable(page))
477 putback_lru_page(page);
Bing Hane3f469b2022-05-30 14:34:16 +0800478 else {
zhong jiang82072962019-11-15 17:34:36 -0800479 list_add(&page->lru, &page_list);
Bing Hane3f469b2022-05-30 14:34:16 +0800480 trace_android_vh_page_isolated_for_reclaim(mm, page);
481 }
zhong jiang82072962019-11-15 17:34:36 -0800482 }
Minchan Kimd616d512019-09-25 16:49:19 -0700483 } else
484 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700485 }
486
487 arch_leave_lazy_mmu_mode();
488 pte_unmap_unlock(orig_pte, ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700489 if (pageout)
490 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700491 cond_resched();
492
493 return 0;
494}
495
496static const struct mm_walk_ops cold_walk_ops = {
Minchan Kimd616d512019-09-25 16:49:19 -0700497 .pmd_entry = madvise_cold_or_pageout_pte_range,
Minchan Kim9c276cc2019-09-25 16:49:08 -0700498};
499
500static void madvise_cold_page_range(struct mmu_gather *tlb,
501 struct vm_area_struct *vma,
502 unsigned long addr, unsigned long end)
503{
Minchan Kimd616d512019-09-25 16:49:19 -0700504 struct madvise_walk_private walk_private = {
505 .pageout = false,
506 .tlb = tlb,
507 };
508
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200509 vm_write_begin(vma);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700510 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700511 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700512 tlb_end_vma(tlb, vma);
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200513 vm_write_end(vma);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700514}
515
516static long madvise_cold(struct vm_area_struct *vma,
517 struct vm_area_struct **prev,
518 unsigned long start_addr, unsigned long end_addr)
519{
520 struct mm_struct *mm = vma->vm_mm;
521 struct mmu_gather tlb;
522
523 *prev = vma;
524 if (!can_madv_lru_vma(vma))
525 return -EINVAL;
526
527 lru_add_drain();
528 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
529 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
530 tlb_finish_mmu(&tlb, start_addr, end_addr);
531
532 return 0;
533}
534
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700535static void madvise_pageout_page_range(struct mmu_gather *tlb,
536 struct vm_area_struct *vma,
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530537 unsigned long addr, unsigned long end,
538 bool can_pageout_file)
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700539{
Minchan Kimd616d512019-09-25 16:49:19 -0700540 struct madvise_walk_private walk_private = {
541 .pageout = true,
542 .tlb = tlb,
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530543 .can_pageout_file = can_pageout_file,
Minchan Kimd616d512019-09-25 16:49:19 -0700544 };
545
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200546 vm_write_begin(vma);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700547 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700548 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700549 tlb_end_vma(tlb, vma);
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200550 vm_write_end(vma);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700551}
552
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530553static inline bool can_do_file_pageout(struct vm_area_struct *vma)
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700554{
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700555 if (!vma->vm_file)
556 return false;
557 /*
558 * paging out pagecache only for non-anonymous mappings that correspond
559 * to the files the calling process could (if tried) open for writing;
560 * otherwise we'd be including shared non-exclusive mappings, which
561 * opens a side channel.
562 */
563 return inode_owner_or_capable(file_inode(vma->vm_file)) ||
564 inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
565}
566
567static long madvise_pageout(struct vm_area_struct *vma,
568 struct vm_area_struct **prev,
569 unsigned long start_addr, unsigned long end_addr)
570{
571 struct mm_struct *mm = vma->vm_mm;
572 struct mmu_gather tlb;
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530573 bool can_pageout_file;
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700574
575 *prev = vma;
576 if (!can_madv_lru_vma(vma))
577 return -EINVAL;
578
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530579 /*
580 * If the VMA belongs to a private file mapping, there can be private
581 * dirty pages which can be paged out if even this process is neither
582 * owner nor write capable of the file. Cache the file access check
583 * here and use it later during page walk.
584 */
585 can_pageout_file = can_do_file_pageout(vma);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700586
587 lru_add_drain();
588 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530589 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr, can_pageout_file);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700590 tlb_finish_mmu(&tlb, start_addr, end_addr);
591
592 return 0;
593}
594
Minchan Kim854e9ed2016-01-15 16:54:53 -0800595static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
596 unsigned long end, struct mm_walk *walk)
597
598{
599 struct mmu_gather *tlb = walk->private;
600 struct mm_struct *mm = tlb->mm;
601 struct vm_area_struct *vma = walk->vma;
602 spinlock_t *ptl;
603 pte_t *orig_pte, *pte, ptent;
604 struct page *page;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800605 int nr_swap = 0;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800606 unsigned long next;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800607
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800608 next = pmd_addr_end(addr, end);
609 if (pmd_trans_huge(*pmd))
610 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
611 goto next;
612
Minchan Kim854e9ed2016-01-15 16:54:53 -0800613 if (pmd_trans_unstable(pmd))
614 return 0;
615
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200616 tlb_change_page_size(tlb, PAGE_SIZE);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800617 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Mel Gorman3ea27712017-08-02 13:31:52 -0700618 flush_tlb_batched_pending(mm);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800619 arch_enter_lazy_mmu_mode();
620 for (; addr != end; pte++, addr += PAGE_SIZE) {
621 ptent = *pte;
622
Minchan Kim64b42bc2016-01-15 16:55:06 -0800623 if (pte_none(ptent))
Minchan Kim854e9ed2016-01-15 16:54:53 -0800624 continue;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800625 /*
626 * If the pte has swp_entry, just clear page table to
627 * prevent swap-in which is more expensive rather than
628 * (page allocation + zeroing).
629 */
630 if (!pte_present(ptent)) {
631 swp_entry_t entry;
632
633 entry = pte_to_swp_entry(ptent);
634 if (non_swap_entry(entry))
635 continue;
636 nr_swap--;
637 free_swap_and_cache(entry);
638 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
639 continue;
640 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800641
Christoph Hellwig25b29952019-06-13 22:50:49 +0200642 page = vm_normal_page(vma, addr, ptent);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800643 if (!page)
644 continue;
645
646 /*
647 * If pmd isn't transhuge but the page is THP and
648 * is owned by only this process, split it and
649 * deactivate all pages.
650 */
651 if (PageTransCompound(page)) {
652 if (page_mapcount(page) != 1)
653 goto out;
654 get_page(page);
655 if (!trylock_page(page)) {
656 put_page(page);
657 goto out;
658 }
659 pte_unmap_unlock(orig_pte, ptl);
660 if (split_huge_page(page)) {
661 unlock_page(page);
662 put_page(page);
663 pte_offset_map_lock(mm, pmd, addr, &ptl);
664 goto out;
665 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800666 unlock_page(page);
Eric Biggers263630e2017-08-25 15:55:39 -0700667 put_page(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800668 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
669 pte--;
670 addr -= PAGE_SIZE;
671 continue;
672 }
673
674 VM_BUG_ON_PAGE(PageTransCompound(page), page);
675
676 if (PageSwapCache(page) || PageDirty(page)) {
677 if (!trylock_page(page))
678 continue;
679 /*
680 * If page is shared with others, we couldn't clear
681 * PG_dirty of the page.
682 */
683 if (page_mapcount(page) != 1) {
684 unlock_page(page);
685 continue;
686 }
687
688 if (PageSwapCache(page) && !try_to_free_swap(page)) {
689 unlock_page(page);
690 continue;
691 }
692
693 ClearPageDirty(page);
694 unlock_page(page);
695 }
696
697 if (pte_young(ptent) || pte_dirty(ptent)) {
698 /*
699 * Some of architecture(ex, PPC) don't update TLB
700 * with set_pte_at and tlb_remove_tlb_entry so for
701 * the portability, remap the pte with old|clean
702 * after pte clearing.
703 */
704 ptent = ptep_get_and_clear_full(mm, addr, pte,
705 tlb->fullmm);
706
707 ptent = pte_mkold(ptent);
708 ptent = pte_mkclean(ptent);
709 set_pte_at(mm, addr, pte, ptent);
710 tlb_remove_tlb_entry(tlb, pte, addr);
711 }
Shaohua Li802a3a92017-05-03 14:52:32 -0700712 mark_page_lazyfree(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800713 }
714out:
Minchan Kim64b42bc2016-01-15 16:55:06 -0800715 if (nr_swap) {
716 if (current->mm == mm)
717 sync_mm_rss(mm);
718
719 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
720 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800721 arch_leave_lazy_mmu_mode();
722 pte_unmap_unlock(orig_pte, ptl);
723 cond_resched();
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800724next:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800725 return 0;
726}
727
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200728static const struct mm_walk_ops madvise_free_walk_ops = {
729 .pmd_entry = madvise_free_pte_range,
730};
Minchan Kim854e9ed2016-01-15 16:54:53 -0800731
732static int madvise_free_single_vma(struct vm_area_struct *vma,
733 unsigned long start_addr, unsigned long end_addr)
734{
Minchan Kim854e9ed2016-01-15 16:54:53 -0800735 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800736 struct mmu_notifier_range range;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800737 struct mmu_gather tlb;
738
Minchan Kim854e9ed2016-01-15 16:54:53 -0800739 /* MADV_FREE works for only anon vma at the moment */
740 if (!vma_is_anonymous(vma))
741 return -EINVAL;
742
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800743 range.start = max(vma->vm_start, start_addr);
744 if (range.start >= vma->vm_end)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800745 return -EINVAL;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800746 range.end = min(vma->vm_end, end_addr);
747 if (range.end <= vma->vm_start)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800748 return -EINVAL;
Jérôme Glisse7269f992019-05-13 17:20:53 -0700749 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700750 range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800751
752 lru_add_drain();
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800753 tlb_gather_mmu(&tlb, mm, range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800754 update_hiwater_rss(mm);
755
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800756 mmu_notifier_invalidate_range_start(&range);
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200757 vm_write_begin(vma);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200758 tlb_start_vma(&tlb, vma);
759 walk_page_range(vma->vm_mm, range.start, range.end,
760 &madvise_free_walk_ops, &tlb);
761 tlb_end_vma(&tlb, vma);
Laurent Dufour9cfe1682018-04-17 16:33:15 +0200762 vm_write_end(vma);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800763 mmu_notifier_invalidate_range_end(&range);
764 tlb_finish_mmu(&tlb, range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800765
766 return 0;
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * Application no longer needs these pages. If the pages are dirty,
771 * it's OK to just throw them away. The app will be more careful about
772 * data it wants to keep. Be sure to free swap resources too. The
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700773 * zap_page_range call sets things up for shrink_active_list to actually free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * these pages later if no one else has touched them in the meantime,
775 * although we could add these pages to a global reuse list for
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700776 * shrink_active_list to pick up before reclaiming other pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *
778 * NB: This interface discards data rather than pushes it out to swap,
779 * as some implementations do. This has performance implications for
780 * applications like large transactional databases which want to discard
781 * pages in anonymous maps after committing to backing store the data
782 * that was kept in them. There is no reason to write this data out to
783 * the swap area if the application is discarding it.
784 *
785 * An interface that causes the system to free clean pages and flush
786 * dirty pages is already available as msync(MS_INVALIDATE).
787 */
Mike Rapoport230ca982017-07-10 15:49:02 -0700788static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
789 unsigned long start, unsigned long end)
790{
791 zap_page_range(vma, start, end - start);
792 return 0;
793}
794
795static long madvise_dontneed_free(struct vm_area_struct *vma,
796 struct vm_area_struct **prev,
797 unsigned long start, unsigned long end,
798 int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Minchan Kim0726b012020-10-17 16:14:50 -0700800 struct mm_struct *mm = vma->vm_mm;
801
Prasanna Meda05b74382005-06-21 17:14:37 -0700802 *prev = vma;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700803 if (!can_madv_lru_vma(vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EINVAL;
805
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800806 if (!userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700807 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800808
Minchan Kim0726b012020-10-17 16:14:50 -0700809 mmap_read_lock(mm);
810 vma = find_vma(mm, start);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800811 if (!vma)
812 return -ENOMEM;
813 if (start < vma->vm_start) {
814 /*
815 * This "vma" under revalidation is the one
816 * with the lowest vma->vm_start where start
817 * is also < vma->vm_end. If start <
818 * vma->vm_start it means an hole materialized
819 * in the user address space within the
Mike Rapoport230ca982017-07-10 15:49:02 -0700820 * virtual range passed to MADV_DONTNEED
821 * or MADV_FREE.
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800822 */
823 return -ENOMEM;
824 }
Minchan Kim9c276cc2019-09-25 16:49:08 -0700825 if (!can_madv_lru_vma(vma))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800826 return -EINVAL;
827 if (end > vma->vm_end) {
828 /*
829 * Don't fail if end > vma->vm_end. If the old
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700830 * vma was splitted while the mmap_lock was
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800831 * released the effect of the concurrent
Mike Rapoport230ca982017-07-10 15:49:02 -0700832 * operation may not cause madvise() to
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800833 * have an undefined result. There may be an
834 * adjacent next vma that we'll walk
835 * next. userfaultfd_remove() will generate an
836 * UFFD_EVENT_REMOVE repetition on the
837 * end-vma->vm_end range, but the manager can
838 * handle a repetition fine.
839 */
840 end = vma->vm_end;
841 }
842 VM_WARN_ON(start >= end);
843 }
Mike Rapoport230ca982017-07-10 15:49:02 -0700844
845 if (behavior == MADV_DONTNEED)
846 return madvise_dontneed_single_vma(vma, start, end);
847 else if (behavior == MADV_FREE)
848 return madvise_free_single_vma(vma, start, end);
849 else
850 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800853/*
854 * Application wants to free up the pages and associated backing store.
855 * This is effectively punching a hole into the middle of a file.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800856 */
857static long madvise_remove(struct vm_area_struct *vma,
Nick Piggin00e9fa22007-03-16 13:38:10 -0800858 struct vm_area_struct **prev,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800859 unsigned long start, unsigned long end)
860{
Hugh Dickins3f31d072012-05-29 15:06:40 -0700861 loff_t offset;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700862 int error;
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700863 struct file *f;
Minchan Kim0726b012020-10-17 16:14:50 -0700864 struct mm_struct *mm = vma->vm_mm;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800865
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700866 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Nick Piggin00e9fa22007-03-16 13:38:10 -0800867
Mike Kravetz72079ba2015-09-08 15:01:57 -0700868 if (vma->vm_flags & VM_LOCKED)
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800869 return -EINVAL;
870
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700871 f = vma->vm_file;
872
873 if (!f || !f->f_mapping || !f->f_mapping->host) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800874 return -EINVAL;
875 }
876
Hugh Dickins69cf0fa2006-04-17 22:46:32 +0100877 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
878 return -EACCES;
879
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800880 offset = (loff_t)(start - vma->vm_start)
881 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700882
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700883 /*
884 * Filesystem's fallocate may need to take i_mutex. We need to
885 * explicitly grab a reference because the vma (and hence the
886 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700887 * mmap_lock.
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700888 */
889 get_file(f);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800890 if (userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700891 /* mmap_lock was not released by userfaultfd_remove() */
Minchan Kim0726b012020-10-17 16:14:50 -0700892 mmap_read_unlock(mm);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800893 }
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500894 error = vfs_fallocate(f,
Hugh Dickins3f31d072012-05-29 15:06:40 -0700895 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
896 offset, end - start);
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700897 fput(f);
Minchan Kim0726b012020-10-17 16:14:50 -0700898 mmap_read_lock(mm);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700899 return error;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800900}
901
Andi Kleen9893e492009-09-16 11:50:17 +0200902#ifdef CONFIG_MEMORY_FAILURE
903/*
904 * Error injection support for memory error handling.
905 */
Anshuman Khandual97167a72017-05-03 14:55:25 -0700906static int madvise_inject_error(int behavior,
907 unsigned long start, unsigned long end)
Andi Kleen9893e492009-09-16 11:50:17 +0200908{
Mel Gormanc461ad62017-08-31 16:15:30 -0700909 struct zone *zone;
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800910 unsigned long size;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700911
Andi Kleen9893e492009-09-16 11:50:17 +0200912 if (!capable(CAP_SYS_ADMIN))
913 return -EPERM;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700914
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700915
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800916 for (; start < end; start += size) {
Dan Williams23e7b5c2018-07-13 21:50:06 -0700917 unsigned long pfn;
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700918 struct page *page;
Andrew Morton325c4ef2013-09-11 14:23:03 -0700919 int ret;
920
Anshuman Khandual97167a72017-05-03 14:55:25 -0700921 ret = get_user_pages_fast(start, 1, 0, &page);
Andi Kleen9893e492009-09-16 11:50:17 +0200922 if (ret != 1)
923 return ret;
Dan Williams23e7b5c2018-07-13 21:50:06 -0700924 pfn = page_to_pfn(page);
Andrew Morton325c4ef2013-09-11 14:23:03 -0700925
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700926 /*
927 * When soft offlining hugepages, after migrating the page
928 * we dissolve it, therefore in the second loop "page" will
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800929 * no longer be a compound page.
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700930 */
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800931 size = page_size(compound_head(page));
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700932
Anshuman Khandual97167a72017-05-03 14:55:25 -0700933 if (behavior == MADV_SOFT_OFFLINE) {
934 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700935 pfn, start);
Naoya Horiguchifeec24a2019-11-30 17:53:38 -0800936 ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700937 } else {
938 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
939 pfn, start);
Oscar Salvadorb7bf8ed2020-12-14 19:11:48 -0800940 ret = memory_failure(pfn, MF_COUNT_INCREASED);
Andi Kleenafcf9382009-12-16 12:20:00 +0100941 }
Anshuman Khandual97167a72017-05-03 14:55:25 -0700942
Naoya Horiguchi23a003b2016-03-15 14:56:36 -0700943 if (ret)
944 return ret;
Andi Kleen9893e492009-09-16 11:50:17 +0200945 }
Mel Gormanc461ad62017-08-31 16:15:30 -0700946
947 /* Ensure that all poisoned pages are removed from per-cpu lists */
948 for_each_populated_zone(zone)
949 drain_all_pages(zone);
950
Andrew Morton325c4ef2013-09-11 14:23:03 -0700951 return 0;
Andi Kleen9893e492009-09-16 11:50:17 +0200952}
953#endif
954
suzuki165cd402005-07-27 11:43:59 -0700955static long
956madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
957 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 switch (behavior) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800960 case MADV_REMOVE:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700961 return madvise_remove(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 case MADV_WILLNEED:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700963 return madvise_willneed(vma, prev, start, end);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700964 case MADV_COLD:
965 return madvise_cold(vma, prev, start, end);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700966 case MADV_PAGEOUT:
967 return madvise_pageout(vma, prev, start, end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800968 case MADV_FREE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 case MADV_DONTNEED:
Mike Rapoport230ca982017-07-10 15:49:02 -0700970 return madvise_dontneed_free(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 default:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700972 return madvise_behavior(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Nicholas Krause1ecef9e2015-09-04 15:48:24 -0700976static bool
Nick Piggin75927af2009-06-16 15:32:38 -0700977madvise_behavior_valid(int behavior)
978{
979 switch (behavior) {
980 case MADV_DOFORK:
981 case MADV_DONTFORK:
982 case MADV_NORMAL:
983 case MADV_SEQUENTIAL:
984 case MADV_RANDOM:
985 case MADV_REMOVE:
986 case MADV_WILLNEED:
987 case MADV_DONTNEED:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800988 case MADV_FREE:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700989 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700990 case MADV_PAGEOUT:
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700991#ifdef CONFIG_KSM
992 case MADV_MERGEABLE:
993 case MADV_UNMERGEABLE:
994#endif
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800995#ifdef CONFIG_TRANSPARENT_HUGEPAGE
996 case MADV_HUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -0800997 case MADV_NOHUGEPAGE:
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800998#endif
Jason Baronaccb61f2012-03-23 15:02:51 -0700999 case MADV_DONTDUMP:
1000 case MADV_DODUMP:
Rik van Rield2cd9ed2017-09-06 16:25:15 -07001001 case MADV_WIPEONFORK:
1002 case MADV_KEEPONFORK:
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001003#ifdef CONFIG_MEMORY_FAILURE
1004 case MADV_SOFT_OFFLINE:
1005 case MADV_HWPOISON:
1006#endif
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001007 return true;
Nick Piggin75927af2009-06-16 15:32:38 -07001008
1009 default:
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001010 return false;
Nick Piggin75927af2009-06-16 15:32:38 -07001011 }
1012}
Hugh Dickins3866ea92009-09-21 17:01:52 -07001013
Minchan Kimecb8ac82020-10-17 16:14:59 -07001014static bool
1015process_madvise_behavior_valid(int behavior)
1016{
1017 switch (behavior) {
1018 case MADV_COLD:
1019 case MADV_PAGEOUT:
Liujie Xie2e06e5e2021-08-14 14:46:42 +08001020 case MADV_WILLNEED:
Minchan Kimecb8ac82020-10-17 16:14:59 -07001021 return true;
1022 default:
1023 return false;
1024 }
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/*
1028 * The madvise(2) system call.
1029 *
1030 * Applications can use madvise() to advise the kernel how it should
1031 * handle paging I/O in this VM area. The idea is to help the kernel
1032 * use appropriate read-ahead and caching techniques. The information
1033 * provided is advisory only, and can be safely disregarded by the
1034 * kernel without affecting the correct operation of the application.
1035 *
1036 * behavior values:
1037 * MADV_NORMAL - the default behavior is to read clusters. This
1038 * results in some read-ahead and read-behind.
1039 * MADV_RANDOM - the system should read the minimum amount of data
1040 * on any access, since it is unlikely that the appli-
1041 * cation will need more than what it asks for.
1042 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1043 * once, so they can be aggressively read ahead, and
1044 * can be freed soon after they are accessed.
1045 * MADV_WILLNEED - the application is notifying the system to read
1046 * some pages ahead.
1047 * MADV_DONTNEED - the application is finished with the given range,
1048 * so the kernel can free resources associated with it.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001049 * MADV_FREE - the application marks pages in the given range as lazy free,
1050 * where actual purges are postponed until memory pressure happens.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08001051 * MADV_REMOVE - the application wants to free up the given range of
1052 * pages and associated backing store.
Hugh Dickins3866ea92009-09-21 17:01:52 -07001053 * MADV_DONTFORK - omit this area from child's address space when forking:
1054 * typically, to avoid COWing pages pinned by get_user_pages().
1055 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
Yang Shic02c3002017-10-13 15:57:37 -07001056 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1057 * range after a fork.
1058 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001059 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1060 * were corrupted by unrecoverable hardware memory failure.
1061 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001062 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1063 * this area with pages of identical content from other such areas.
1064 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001065 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1066 * huge pages in the future. Existing pages might be coalesced and
1067 * new pages might be allocated as THP.
1068 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1069 * transparent huge pages so the existing pages will not be
1070 * coalesced into THP and new pages will not be allocated as THP.
1071 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1072 * from being included in its core dump.
1073 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
Minchan Kimecb8ac82020-10-17 16:14:59 -07001074 * MADV_COLD - the application is not expected to use this memory soon,
1075 * deactivate pages in this range so that they can be reclaimed
1076 * easily if memory pressure hanppens.
1077 * MADV_PAGEOUT - the application is not expected to use this memory soon,
1078 * page out the pages in this range immediately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
1080 * return values:
1081 * zero - success
1082 * -EINVAL - start + len < 0, start is not page-aligned,
1083 * "behavior" is not a valid value, or application
Yang Shic02c3002017-10-13 15:57:37 -07001084 * is attempting to release locked or shared pages,
1085 * or the specified address range includes file, Huge TLB,
1086 * MAP_SHARED or VMPFNMAP range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * -ENOMEM - addresses in the specified range are not currently
1088 * mapped, or are outside the AS of the process.
1089 * -EIO - an I/O error occurred while paging in data.
1090 * -EBADF - map exists, but area maps something that isn't a file.
1091 * -EAGAIN - a kernel resource was temporarily unavailable.
1092 */
Minchan Kim0726b012020-10-17 16:14:50 -07001093int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Prasanna Meda05b74382005-06-21 17:14:37 -07001095 unsigned long end, tmp;
Vladimir Cernovec9bed92013-09-11 14:20:15 -07001096 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int unmapped_error = 0;
1098 int error = -EINVAL;
Jason Baronf7977792007-07-15 23:38:21 -07001099 int write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 size_t len;
Shaohua Li1998cc02013-02-22 16:32:31 -08001101 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Andrey Konovalov057d33892019-09-25 16:48:30 -07001103 start = untagged_addr(start);
1104
Nick Piggin75927af2009-06-16 15:32:38 -07001105 if (!madvise_behavior_valid(behavior))
1106 return error;
1107
Wei Yangdf6c6502019-11-30 17:57:46 -08001108 if (!PAGE_ALIGNED(start))
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001109 return error;
Wei Yangdf6c6502019-11-30 17:57:46 -08001110 len = PAGE_ALIGN(len_in);
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001111
1112 /* Check to see whether len was rounded up from small -ve to zero */
1113 if (len_in && !len)
1114 return error;
1115
1116 end = start + len;
1117 if (end < start)
1118 return error;
1119
1120 error = 0;
1121 if (end == start)
1122 return error;
1123
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001124#ifdef CONFIG_MEMORY_FAILURE
1125 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1126 return madvise_inject_error(behavior, start, start + len_in);
1127#endif
1128
Jason Baronf7977792007-07-15 23:38:21 -07001129 write = madvise_need_mmap_write(behavior);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001130 if (write) {
Minchan Kim0726b012020-10-17 16:14:50 -07001131 if (mmap_write_lock_killable(mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001132 return -EINTR;
1133 } else {
Minchan Kim0726b012020-10-17 16:14:50 -07001134 mmap_read_lock(mm);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * If the interval [start,end) covers some unmapped address
1139 * ranges, just ignore them, but return -ENOMEM at the end.
Prasanna Meda05b74382005-06-21 17:14:37 -07001140 * - different from the way of handling in mlock etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Minchan Kim0726b012020-10-17 16:14:50 -07001142 vma = find_vma_prev(mm, start, &prev);
Hugh Dickins836d5ff2005-09-03 15:54:53 -07001143 if (vma && start > vma->vm_start)
1144 prev = vma;
1145
Shaohua Li1998cc02013-02-22 16:32:31 -08001146 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 for (;;) {
1148 /* Still start < end. */
1149 error = -ENOMEM;
1150 if (!vma)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Prasanna Meda05b74382005-06-21 17:14:37 -07001153 /* Here start < (end|vma->vm_end). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (start < vma->vm_start) {
1155 unmapped_error = -ENOMEM;
1156 start = vma->vm_start;
Prasanna Meda05b74382005-06-21 17:14:37 -07001157 if (start >= end)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Prasanna Meda05b74382005-06-21 17:14:37 -07001161 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1162 tmp = vma->vm_end;
1163 if (end < tmp)
1164 tmp = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Prasanna Meda05b74382005-06-21 17:14:37 -07001166 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1167 error = madvise_vma(vma, &prev, start, tmp, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (error)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001169 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -07001170 start = tmp;
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001171 if (prev && start < prev->vm_end)
Prasanna Meda05b74382005-06-21 17:14:37 -07001172 start = prev->vm_end;
1173 error = unmapped_error;
1174 if (start >= end)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001175 goto out;
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001176 if (prev)
1177 vma = prev->vm_next;
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001178 else /* madvise_remove dropped mmap_lock */
Minchan Kim0726b012020-10-17 16:14:50 -07001179 vma = find_vma(mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181out:
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001182 blk_finish_plug(&plug);
Jason Baronf7977792007-07-15 23:38:21 -07001183 if (write)
Minchan Kim0726b012020-10-17 16:14:50 -07001184 mmap_write_unlock(mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001185 else
Minchan Kim0726b012020-10-17 16:14:50 -07001186 mmap_read_unlock(mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return error;
1189}
Jens Axboedb08ca22019-12-25 22:14:54 -07001190
1191SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1192{
Minchan Kim0726b012020-10-17 16:14:50 -07001193 return do_madvise(current->mm, start, len_in, behavior);
Jens Axboedb08ca22019-12-25 22:14:54 -07001194}
Minchan Kimecb8ac82020-10-17 16:14:59 -07001195
1196SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1197 size_t, vlen, int, behavior, unsigned int, flags)
1198{
1199 ssize_t ret;
1200 struct iovec iovstack[UIO_FASTIOV], iovec;
1201 struct iovec *iov = iovstack;
1202 struct iov_iter iter;
1203 struct pid *pid;
1204 struct task_struct *task;
1205 struct mm_struct *mm;
1206 size_t total_len;
1207 unsigned int f_flags;
1208
1209 if (flags != 0) {
1210 ret = -EINVAL;
1211 goto out;
1212 }
1213
1214 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1215 if (ret < 0)
1216 goto out;
1217
1218 pid = pidfd_get_pid(pidfd, &f_flags);
1219 if (IS_ERR(pid)) {
1220 ret = PTR_ERR(pid);
1221 goto free_iov;
1222 }
1223
1224 task = get_pid_task(pid, PIDTYPE_PID);
1225 if (!task) {
1226 ret = -ESRCH;
1227 goto put_pid;
1228 }
1229
Minchan Kima68a0262020-12-08 20:57:18 -08001230 if (!process_madvise_behavior_valid(behavior)) {
Minchan Kimecb8ac82020-10-17 16:14:59 -07001231 ret = -EINVAL;
1232 goto release_task;
1233 }
1234
Suren Baghdasaryan518f98e2021-03-12 21:08:06 -08001235 /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1236 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001237 if (IS_ERR_OR_NULL(mm)) {
1238 ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1239 goto release_task;
1240 }
1241
Suren Baghdasaryan518f98e2021-03-12 21:08:06 -08001242 /*
1243 * Require CAP_SYS_NICE for influencing process performance. Note that
1244 * only non-destructive hints are currently supported.
1245 */
1246 if (!capable(CAP_SYS_NICE)) {
1247 ret = -EPERM;
1248 goto release_mm;
1249 }
1250
Minchan Kimecb8ac82020-10-17 16:14:59 -07001251 total_len = iov_iter_count(&iter);
1252
1253 while (iov_iter_count(&iter)) {
1254 iovec = iov_iter_iovec(&iter);
1255 ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1256 iovec.iov_len, behavior);
1257 if (ret < 0)
1258 break;
1259 iov_iter_advance(&iter, iovec.iov_len);
1260 }
1261
Charan Teja Kalla8b354e32022-03-22 14:46:44 -07001262 ret = (total_len - iov_iter_count(&iter)) ? : ret;
Minchan Kimecb8ac82020-10-17 16:14:59 -07001263
Suren Baghdasaryan518f98e2021-03-12 21:08:06 -08001264release_mm:
Minchan Kimecb8ac82020-10-17 16:14:59 -07001265 mmput(mm);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001266release_task:
1267 put_task_struct(task);
1268put_pid:
1269 put_pid(pid);
1270free_iov:
1271 kfree(iov);
1272out:
1273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}