blob: d16fa1d8f60fc77785590f83bd9eec403d3991b0 [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;
shenjiangjiang28f1c8e2023-07-05 17:50:26 +0800325 bool abort_madvise = false;
Minchan Kimd616d512019-09-25 16:49:19 -0700326
shenjiangjiang28f1c8e2023-07-05 17:50:26 +0800327 trace_android_vh_madvise_cold_or_pageout_abort(vma, &abort_madvise);
328 if (fatal_signal_pending(current) || abort_madvise)
Minchan Kimd616d512019-09-25 16:49:19 -0700329 return -EINTR;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700330
Pavankumar Kondeti6d04d8c2022-09-26 18:36:10 +0530331 trace_android_vh_madvise_cold_or_pageout(vma, &allow_shared);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700332#ifdef CONFIG_TRANSPARENT_HUGEPAGE
333 if (pmd_trans_huge(*pmd)) {
334 pmd_t orig_pmd;
335 unsigned long next = pmd_addr_end(addr, end);
336
337 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
338 ptl = pmd_trans_huge_lock(pmd, vma);
339 if (!ptl)
340 return 0;
341
342 orig_pmd = *pmd;
343 if (is_huge_zero_pmd(orig_pmd))
344 goto huge_unlock;
345
346 if (unlikely(!pmd_present(orig_pmd))) {
347 VM_BUG_ON(thp_migration_supported() &&
348 !is_pmd_migration_entry(orig_pmd));
349 goto huge_unlock;
350 }
351
352 page = pmd_page(orig_pmd);
Michal Hocko12e967f2020-03-21 18:22:26 -0700353
354 /* Do not interfere with other mappings of this page */
355 if (page_mapcount(page) != 1)
356 goto huge_unlock;
357
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530358 if (pageout_anon_only && !PageAnon(page))
359 goto huge_unlock;
360
Minchan Kim9c276cc2019-09-25 16:49:08 -0700361 if (next - addr != HPAGE_PMD_SIZE) {
362 int err;
363
Minchan Kim9c276cc2019-09-25 16:49:08 -0700364 get_page(page);
365 spin_unlock(ptl);
366 lock_page(page);
367 err = split_huge_page(page);
368 unlock_page(page);
369 put_page(page);
370 if (!err)
371 goto regular_page;
372 return 0;
373 }
374
375 if (pmd_young(orig_pmd)) {
376 pmdp_invalidate(vma, addr, pmd);
377 orig_pmd = pmd_mkold(orig_pmd);
378
379 set_pmd_at(mm, addr, pmd, orig_pmd);
380 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
381 }
382
Minchan Kimd616d512019-09-25 16:49:19 -0700383 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700384 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700385 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800386 if (!isolate_lru_page(page)) {
387 if (PageUnevictable(page))
388 putback_lru_page(page);
389 else
390 list_add(&page->lru, &page_list);
391 }
Minchan Kimd616d512019-09-25 16:49:19 -0700392 } else
393 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700394huge_unlock:
395 spin_unlock(ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700396 if (pageout)
397 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700398 return 0;
399 }
400
Minchan Kimce268422020-09-14 23:32:15 -0700401regular_page:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700402 if (pmd_trans_unstable(pmd))
403 return 0;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700404#endif
405 tlb_change_page_size(tlb, PAGE_SIZE);
406 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
407 flush_tlb_batched_pending(mm);
408 arch_enter_lazy_mmu_mode();
409 for (; addr < end; pte++, addr += PAGE_SIZE) {
410 ptent = *pte;
411
412 if (pte_none(ptent))
413 continue;
414
415 if (!pte_present(ptent))
416 continue;
417
418 page = vm_normal_page(vma, addr, ptent);
419 if (!page)
420 continue;
421
422 /*
423 * Creating a THP page is expensive so split it only if we
424 * are sure it's worth. Split it if we are only owner.
425 */
426 if (PageTransCompound(page)) {
427 if (page_mapcount(page) != 1)
428 break;
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530429 if (pageout_anon_only && !PageAnon(page))
430 break;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700431 get_page(page);
432 if (!trylock_page(page)) {
433 put_page(page);
434 break;
435 }
436 pte_unmap_unlock(orig_pte, ptl);
437 if (split_huge_page(page)) {
438 unlock_page(page);
439 put_page(page);
440 pte_offset_map_lock(mm, pmd, addr, &ptl);
441 break;
442 }
443 unlock_page(page);
444 put_page(page);
445 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
446 pte--;
447 addr -= PAGE_SIZE;
448 continue;
449 }
450
Minchan Kimbe2cd262022-09-08 08:12:04 -0700451 /*
452 * Do not interfere with other mappings of this page and
453 * non-LRU page.
454 */
Greg Kroah-Hartmanbc7618b2022-10-05 11:58:18 +0200455 if (!allow_shared && (!PageLRU(page) || page_mapcount(page) != 1))
Michal Hocko12e967f2020-03-21 18:22:26 -0700456 continue;
457
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530458 if (pageout_anon_only && !PageAnon(page))
459 continue;
460
Minchan Kim9c276cc2019-09-25 16:49:08 -0700461 VM_BUG_ON_PAGE(PageTransCompound(page), page);
462
463 if (pte_young(ptent)) {
464 ptent = ptep_get_and_clear_full(mm, addr, pte,
465 tlb->fullmm);
466 ptent = pte_mkold(ptent);
467 set_pte_at(mm, addr, pte, ptent);
468 tlb_remove_tlb_entry(tlb, pte, addr);
469 }
470
471 /*
472 * We are deactivating a page for accelerating reclaiming.
473 * VM couldn't reclaim the page unless we clear PG_young.
474 * As a side effect, it makes confuse idle-page tracking
475 * because they will miss recent referenced history.
476 */
Minchan Kimd616d512019-09-25 16:49:19 -0700477 ClearPageReferenced(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700478 test_and_clear_page_young(page);
Minchan Kimd616d512019-09-25 16:49:19 -0700479 if (pageout) {
zhong jiang82072962019-11-15 17:34:36 -0800480 if (!isolate_lru_page(page)) {
481 if (PageUnevictable(page))
482 putback_lru_page(page);
Bing Hane3f469b2022-05-30 14:34:16 +0800483 else {
zhong jiang82072962019-11-15 17:34:36 -0800484 list_add(&page->lru, &page_list);
Bing Hane3f469b2022-05-30 14:34:16 +0800485 trace_android_vh_page_isolated_for_reclaim(mm, page);
486 }
zhong jiang82072962019-11-15 17:34:36 -0800487 }
Minchan Kimd616d512019-09-25 16:49:19 -0700488 } else
489 deactivate_page(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700490 }
491
492 arch_leave_lazy_mmu_mode();
493 pte_unmap_unlock(orig_pte, ptl);
Minchan Kimd616d512019-09-25 16:49:19 -0700494 if (pageout)
495 reclaim_pages(&page_list);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700496 cond_resched();
497
498 return 0;
499}
500
501static const struct mm_walk_ops cold_walk_ops = {
Minchan Kimd616d512019-09-25 16:49:19 -0700502 .pmd_entry = madvise_cold_or_pageout_pte_range,
Minchan Kim9c276cc2019-09-25 16:49:08 -0700503};
504
505static void madvise_cold_page_range(struct mmu_gather *tlb,
506 struct vm_area_struct *vma,
507 unsigned long addr, unsigned long end)
508{
Minchan Kimd616d512019-09-25 16:49:19 -0700509 struct madvise_walk_private walk_private = {
510 .pageout = false,
511 .tlb = tlb,
512 };
513
Minchan Kim9c276cc2019-09-25 16:49:08 -0700514 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700515 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700516 tlb_end_vma(tlb, vma);
517}
518
519static long madvise_cold(struct vm_area_struct *vma,
520 struct vm_area_struct **prev,
521 unsigned long start_addr, unsigned long end_addr)
522{
523 struct mm_struct *mm = vma->vm_mm;
524 struct mmu_gather tlb;
525
526 *prev = vma;
527 if (!can_madv_lru_vma(vma))
528 return -EINVAL;
529
530 lru_add_drain();
531 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
532 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
533 tlb_finish_mmu(&tlb, start_addr, end_addr);
534
535 return 0;
536}
537
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700538static void madvise_pageout_page_range(struct mmu_gather *tlb,
539 struct vm_area_struct *vma,
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530540 unsigned long addr, unsigned long end,
541 bool can_pageout_file)
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700542{
Minchan Kimd616d512019-09-25 16:49:19 -0700543 struct madvise_walk_private walk_private = {
544 .pageout = true,
545 .tlb = tlb,
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530546 .can_pageout_file = can_pageout_file,
Minchan Kimd616d512019-09-25 16:49:19 -0700547 };
548
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700549 tlb_start_vma(tlb, vma);
Minchan Kimd616d512019-09-25 16:49:19 -0700550 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700551 tlb_end_vma(tlb, vma);
552}
553
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530554static inline bool can_do_file_pageout(struct vm_area_struct *vma)
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700555{
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700556 if (!vma->vm_file)
557 return false;
558 /*
559 * paging out pagecache only for non-anonymous mappings that correspond
560 * to the files the calling process could (if tried) open for writing;
561 * otherwise we'd be including shared non-exclusive mappings, which
562 * opens a side channel.
563 */
564 return inode_owner_or_capable(file_inode(vma->vm_file)) ||
565 inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
566}
567
568static long madvise_pageout(struct vm_area_struct *vma,
569 struct vm_area_struct **prev,
570 unsigned long start_addr, unsigned long end_addr)
571{
572 struct mm_struct *mm = vma->vm_mm;
573 struct mmu_gather tlb;
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530574 bool can_pageout_file;
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700575
576 *prev = vma;
577 if (!can_madv_lru_vma(vma))
578 return -EINVAL;
579
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530580 /*
581 * If the VMA belongs to a private file mapping, there can be private
582 * dirty pages which can be paged out if even this process is neither
583 * owner nor write capable of the file. Cache the file access check
584 * here and use it later during page walk.
585 */
586 can_pageout_file = can_do_file_pageout(vma);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700587
588 lru_add_drain();
589 tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
Pavankumar Kondetid84fac92022-11-09 10:48:36 +0530590 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr, can_pageout_file);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700591 tlb_finish_mmu(&tlb, start_addr, end_addr);
592
593 return 0;
594}
595
Minchan Kim854e9ed2016-01-15 16:54:53 -0800596static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
597 unsigned long end, struct mm_walk *walk)
598
599{
600 struct mmu_gather *tlb = walk->private;
601 struct mm_struct *mm = tlb->mm;
602 struct vm_area_struct *vma = walk->vma;
603 spinlock_t *ptl;
604 pte_t *orig_pte, *pte, ptent;
605 struct page *page;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800606 int nr_swap = 0;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800607 unsigned long next;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800608
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800609 next = pmd_addr_end(addr, end);
610 if (pmd_trans_huge(*pmd))
611 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
612 goto next;
613
Minchan Kim854e9ed2016-01-15 16:54:53 -0800614 if (pmd_trans_unstable(pmd))
615 return 0;
616
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200617 tlb_change_page_size(tlb, PAGE_SIZE);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800618 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Mel Gorman3ea27712017-08-02 13:31:52 -0700619 flush_tlb_batched_pending(mm);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800620 arch_enter_lazy_mmu_mode();
621 for (; addr != end; pte++, addr += PAGE_SIZE) {
622 ptent = *pte;
623
Minchan Kim64b42bc2016-01-15 16:55:06 -0800624 if (pte_none(ptent))
Minchan Kim854e9ed2016-01-15 16:54:53 -0800625 continue;
Minchan Kim64b42bc2016-01-15 16:55:06 -0800626 /*
627 * If the pte has swp_entry, just clear page table to
628 * prevent swap-in which is more expensive rather than
629 * (page allocation + zeroing).
630 */
631 if (!pte_present(ptent)) {
632 swp_entry_t entry;
633
634 entry = pte_to_swp_entry(ptent);
635 if (non_swap_entry(entry))
636 continue;
637 nr_swap--;
638 free_swap_and_cache(entry);
639 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
640 continue;
641 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800642
Christoph Hellwig25b29952019-06-13 22:50:49 +0200643 page = vm_normal_page(vma, addr, ptent);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800644 if (!page)
645 continue;
646
647 /*
648 * If pmd isn't transhuge but the page is THP and
649 * is owned by only this process, split it and
650 * deactivate all pages.
651 */
652 if (PageTransCompound(page)) {
653 if (page_mapcount(page) != 1)
654 goto out;
655 get_page(page);
656 if (!trylock_page(page)) {
657 put_page(page);
658 goto out;
659 }
660 pte_unmap_unlock(orig_pte, ptl);
661 if (split_huge_page(page)) {
662 unlock_page(page);
663 put_page(page);
664 pte_offset_map_lock(mm, pmd, addr, &ptl);
665 goto out;
666 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800667 unlock_page(page);
Eric Biggers263630e2017-08-25 15:55:39 -0700668 put_page(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800669 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
670 pte--;
671 addr -= PAGE_SIZE;
672 continue;
673 }
674
675 VM_BUG_ON_PAGE(PageTransCompound(page), page);
676
677 if (PageSwapCache(page) || PageDirty(page)) {
678 if (!trylock_page(page))
679 continue;
680 /*
681 * If page is shared with others, we couldn't clear
682 * PG_dirty of the page.
683 */
684 if (page_mapcount(page) != 1) {
685 unlock_page(page);
686 continue;
687 }
688
689 if (PageSwapCache(page) && !try_to_free_swap(page)) {
690 unlock_page(page);
691 continue;
692 }
693
694 ClearPageDirty(page);
695 unlock_page(page);
696 }
697
698 if (pte_young(ptent) || pte_dirty(ptent)) {
699 /*
700 * Some of architecture(ex, PPC) don't update TLB
701 * with set_pte_at and tlb_remove_tlb_entry so for
702 * the portability, remap the pte with old|clean
703 * after pte clearing.
704 */
705 ptent = ptep_get_and_clear_full(mm, addr, pte,
706 tlb->fullmm);
707
708 ptent = pte_mkold(ptent);
709 ptent = pte_mkclean(ptent);
710 set_pte_at(mm, addr, pte, ptent);
711 tlb_remove_tlb_entry(tlb, pte, addr);
712 }
Shaohua Li802a3a92017-05-03 14:52:32 -0700713 mark_page_lazyfree(page);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800714 }
715out:
Minchan Kim64b42bc2016-01-15 16:55:06 -0800716 if (nr_swap) {
717 if (current->mm == mm)
718 sync_mm_rss(mm);
719
720 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
721 }
Minchan Kim854e9ed2016-01-15 16:54:53 -0800722 arch_leave_lazy_mmu_mode();
723 pte_unmap_unlock(orig_pte, ptl);
724 cond_resched();
Minchan Kimb8d3c4c2016-01-15 16:55:42 -0800725next:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800726 return 0;
727}
728
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200729static const struct mm_walk_ops madvise_free_walk_ops = {
730 .pmd_entry = madvise_free_pte_range,
731};
Minchan Kim854e9ed2016-01-15 16:54:53 -0800732
733static int madvise_free_single_vma(struct vm_area_struct *vma,
734 unsigned long start_addr, unsigned long end_addr)
735{
Minchan Kim854e9ed2016-01-15 16:54:53 -0800736 struct mm_struct *mm = vma->vm_mm;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800737 struct mmu_notifier_range range;
Minchan Kim854e9ed2016-01-15 16:54:53 -0800738 struct mmu_gather tlb;
739
Minchan Kim854e9ed2016-01-15 16:54:53 -0800740 /* MADV_FREE works for only anon vma at the moment */
741 if (!vma_is_anonymous(vma))
742 return -EINVAL;
743
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800744 range.start = max(vma->vm_start, start_addr);
745 if (range.start >= vma->vm_end)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800746 return -EINVAL;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800747 range.end = min(vma->vm_end, end_addr);
748 if (range.end <= vma->vm_start)
Minchan Kim854e9ed2016-01-15 16:54:53 -0800749 return -EINVAL;
Jérôme Glisse7269f992019-05-13 17:20:53 -0700750 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -0700751 range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800752
753 lru_add_drain();
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800754 tlb_gather_mmu(&tlb, mm, range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800755 update_hiwater_rss(mm);
756
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800757 mmu_notifier_invalidate_range_start(&range);
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);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800762 mmu_notifier_invalidate_range_end(&range);
763 tlb_finish_mmu(&tlb, range.start, range.end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800764
765 return 0;
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
769 * Application no longer needs these pages. If the pages are dirty,
770 * it's OK to just throw them away. The app will be more careful about
771 * data it wants to keep. Be sure to free swap resources too. The
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700772 * zap_page_range call sets things up for shrink_active_list to actually free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * these pages later if no one else has touched them in the meantime,
774 * although we could add these pages to a global reuse list for
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700775 * shrink_active_list to pick up before reclaiming other pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
777 * NB: This interface discards data rather than pushes it out to swap,
778 * as some implementations do. This has performance implications for
779 * applications like large transactional databases which want to discard
780 * pages in anonymous maps after committing to backing store the data
781 * that was kept in them. There is no reason to write this data out to
782 * the swap area if the application is discarding it.
783 *
784 * An interface that causes the system to free clean pages and flush
785 * dirty pages is already available as msync(MS_INVALIDATE).
786 */
Mike Rapoport230ca982017-07-10 15:49:02 -0700787static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
788 unsigned long start, unsigned long end)
789{
790 zap_page_range(vma, start, end - start);
791 return 0;
792}
793
794static long madvise_dontneed_free(struct vm_area_struct *vma,
795 struct vm_area_struct **prev,
796 unsigned long start, unsigned long end,
797 int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Minchan Kim0726b012020-10-17 16:14:50 -0700799 struct mm_struct *mm = vma->vm_mm;
800
Prasanna Meda05b74382005-06-21 17:14:37 -0700801 *prev = vma;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700802 if (!can_madv_lru_vma(vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EINVAL;
804
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800805 if (!userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700806 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800807
Minchan Kim0726b012020-10-17 16:14:50 -0700808 mmap_read_lock(mm);
809 vma = find_vma(mm, start);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800810 if (!vma)
811 return -ENOMEM;
812 if (start < vma->vm_start) {
813 /*
814 * This "vma" under revalidation is the one
815 * with the lowest vma->vm_start where start
816 * is also < vma->vm_end. If start <
817 * vma->vm_start it means an hole materialized
818 * in the user address space within the
Mike Rapoport230ca982017-07-10 15:49:02 -0700819 * virtual range passed to MADV_DONTNEED
820 * or MADV_FREE.
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800821 */
822 return -ENOMEM;
823 }
Minchan Kim9c276cc2019-09-25 16:49:08 -0700824 if (!can_madv_lru_vma(vma))
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800825 return -EINVAL;
826 if (end > vma->vm_end) {
827 /*
828 * Don't fail if end > vma->vm_end. If the old
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700829 * vma was splitted while the mmap_lock was
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800830 * released the effect of the concurrent
Mike Rapoport230ca982017-07-10 15:49:02 -0700831 * operation may not cause madvise() to
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800832 * have an undefined result. There may be an
833 * adjacent next vma that we'll walk
834 * next. userfaultfd_remove() will generate an
835 * UFFD_EVENT_REMOVE repetition on the
836 * end-vma->vm_end range, but the manager can
837 * handle a repetition fine.
838 */
839 end = vma->vm_end;
840 }
841 VM_WARN_ON(start >= end);
842 }
Mike Rapoport230ca982017-07-10 15:49:02 -0700843
844 if (behavior == MADV_DONTNEED)
845 return madvise_dontneed_single_vma(vma, start, end);
846 else if (behavior == MADV_FREE)
847 return madvise_free_single_vma(vma, start, end);
848 else
849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800852/*
853 * Application wants to free up the pages and associated backing store.
854 * This is effectively punching a hole into the middle of a file.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800855 */
856static long madvise_remove(struct vm_area_struct *vma,
Nick Piggin00e9fa22007-03-16 13:38:10 -0800857 struct vm_area_struct **prev,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800858 unsigned long start, unsigned long end)
859{
Hugh Dickins3f31d072012-05-29 15:06:40 -0700860 loff_t offset;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700861 int error;
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700862 struct file *f;
Minchan Kim0726b012020-10-17 16:14:50 -0700863 struct mm_struct *mm = vma->vm_mm;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800864
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700865 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
Nick Piggin00e9fa22007-03-16 13:38:10 -0800866
Mike Kravetz72079ba2015-09-08 15:01:57 -0700867 if (vma->vm_flags & VM_LOCKED)
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800868 return -EINVAL;
869
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700870 f = vma->vm_file;
871
872 if (!f || !f->f_mapping || !f->f_mapping->host) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800873 return -EINVAL;
874 }
875
Hugh Dickins69cf0fa2006-04-17 22:46:32 +0100876 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
877 return -EACCES;
878
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800879 offset = (loff_t)(start - vma->vm_start)
880 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700881
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700882 /*
883 * Filesystem's fallocate may need to take i_mutex. We need to
884 * explicitly grab a reference because the vma (and hence the
885 * vma's reference to the file) can go away as soon as we drop
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700886 * mmap_lock.
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700887 */
888 get_file(f);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800889 if (userfaultfd_remove(vma, start, end)) {
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700890 /* mmap_lock was not released by userfaultfd_remove() */
Minchan Kim0726b012020-10-17 16:14:50 -0700891 mmap_read_unlock(mm);
Andrea Arcangeli70ccb922017-03-09 16:17:11 -0800892 }
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500893 error = vfs_fallocate(f,
Hugh Dickins3f31d072012-05-29 15:06:40 -0700894 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
895 offset, end - start);
Andy Lutomirski9ab42332012-07-05 16:00:11 -0700896 fput(f);
Minchan Kim0726b012020-10-17 16:14:50 -0700897 mmap_read_lock(mm);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700898 return error;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800899}
900
Andi Kleen9893e492009-09-16 11:50:17 +0200901#ifdef CONFIG_MEMORY_FAILURE
902/*
903 * Error injection support for memory error handling.
904 */
Anshuman Khandual97167a72017-05-03 14:55:25 -0700905static int madvise_inject_error(int behavior,
906 unsigned long start, unsigned long end)
Andi Kleen9893e492009-09-16 11:50:17 +0200907{
Mel Gormanc461ad62017-08-31 16:15:30 -0700908 struct zone *zone;
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800909 unsigned long size;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700910
Andi Kleen9893e492009-09-16 11:50:17 +0200911 if (!capable(CAP_SYS_ADMIN))
912 return -EPERM;
Anshuman Khandual97167a72017-05-03 14:55:25 -0700913
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700914
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800915 for (; start < end; start += size) {
Dan Williams23e7b5c2018-07-13 21:50:06 -0700916 unsigned long pfn;
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700917 struct page *page;
Andrew Morton325c4ef2013-09-11 14:23:03 -0700918 int ret;
919
Anshuman Khandual97167a72017-05-03 14:55:25 -0700920 ret = get_user_pages_fast(start, 1, 0, &page);
Andi Kleen9893e492009-09-16 11:50:17 +0200921 if (ret != 1)
922 return ret;
Dan Williams23e7b5c2018-07-13 21:50:06 -0700923 pfn = page_to_pfn(page);
Andrew Morton325c4ef2013-09-11 14:23:03 -0700924
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700925 /*
926 * When soft offlining hugepages, after migrating the page
927 * we dissolve it, therefore in the second loop "page" will
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800928 * no longer be a compound page.
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700929 */
Yunfeng Yed3cd2572019-11-30 17:57:42 -0800930 size = page_size(compound_head(page));
Alexandru Moise19bfbe22017-10-03 16:14:31 -0700931
Anshuman Khandual97167a72017-05-03 14:55:25 -0700932 if (behavior == MADV_SOFT_OFFLINE) {
933 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700934 pfn, start);
Naoya Horiguchifeec24a2019-11-30 17:53:38 -0800935 ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
Oscar Salvadordc7560b2020-10-15 20:06:53 -0700936 } else {
937 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
938 pfn, start);
Oscar Salvadorb7bf8ed2020-12-14 19:11:48 -0800939 ret = memory_failure(pfn, MF_COUNT_INCREASED);
Andi Kleenafcf9382009-12-16 12:20:00 +0100940 }
Anshuman Khandual97167a72017-05-03 14:55:25 -0700941
Naoya Horiguchi23a003b2016-03-15 14:56:36 -0700942 if (ret)
943 return ret;
Andi Kleen9893e492009-09-16 11:50:17 +0200944 }
Mel Gormanc461ad62017-08-31 16:15:30 -0700945
946 /* Ensure that all poisoned pages are removed from per-cpu lists */
947 for_each_populated_zone(zone)
948 drain_all_pages(zone);
949
Andrew Morton325c4ef2013-09-11 14:23:03 -0700950 return 0;
Andi Kleen9893e492009-09-16 11:50:17 +0200951}
952#endif
953
suzuki165cd402005-07-27 11:43:59 -0700954static long
955madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
956 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 switch (behavior) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800959 case MADV_REMOVE:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700960 return madvise_remove(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 case MADV_WILLNEED:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700962 return madvise_willneed(vma, prev, start, end);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700963 case MADV_COLD:
964 return madvise_cold(vma, prev, start, end);
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700965 case MADV_PAGEOUT:
966 return madvise_pageout(vma, prev, start, end);
Minchan Kim854e9ed2016-01-15 16:54:53 -0800967 case MADV_FREE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 case MADV_DONTNEED:
Mike Rapoport230ca982017-07-10 15:49:02 -0700969 return madvise_dontneed_free(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 default:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700971 return madvise_behavior(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Nicholas Krause1ecef9e2015-09-04 15:48:24 -0700975static bool
Nick Piggin75927af2009-06-16 15:32:38 -0700976madvise_behavior_valid(int behavior)
977{
978 switch (behavior) {
979 case MADV_DOFORK:
980 case MADV_DONTFORK:
981 case MADV_NORMAL:
982 case MADV_SEQUENTIAL:
983 case MADV_RANDOM:
984 case MADV_REMOVE:
985 case MADV_WILLNEED:
986 case MADV_DONTNEED:
Minchan Kim854e9ed2016-01-15 16:54:53 -0800987 case MADV_FREE:
Minchan Kim9c276cc2019-09-25 16:49:08 -0700988 case MADV_COLD:
Minchan Kim1a4e58c2019-09-25 16:49:15 -0700989 case MADV_PAGEOUT:
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700990#ifdef CONFIG_KSM
991 case MADV_MERGEABLE:
992 case MADV_UNMERGEABLE:
993#endif
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800994#ifdef CONFIG_TRANSPARENT_HUGEPAGE
995 case MADV_HUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -0800996 case MADV_NOHUGEPAGE:
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800997#endif
Jason Baronaccb61f2012-03-23 15:02:51 -0700998 case MADV_DONTDUMP:
999 case MADV_DODUMP:
Rik van Rield2cd9ed2017-09-06 16:25:15 -07001000 case MADV_WIPEONFORK:
1001 case MADV_KEEPONFORK:
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001002#ifdef CONFIG_MEMORY_FAILURE
1003 case MADV_SOFT_OFFLINE:
1004 case MADV_HWPOISON:
1005#endif
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001006 return true;
Nick Piggin75927af2009-06-16 15:32:38 -07001007
1008 default:
Nicholas Krause1ecef9e2015-09-04 15:48:24 -07001009 return false;
Nick Piggin75927af2009-06-16 15:32:38 -07001010 }
1011}
Hugh Dickins3866ea92009-09-21 17:01:52 -07001012
Minchan Kimecb8ac82020-10-17 16:14:59 -07001013static bool
1014process_madvise_behavior_valid(int behavior)
1015{
1016 switch (behavior) {
1017 case MADV_COLD:
1018 case MADV_PAGEOUT:
Liujie Xie2e06e5e2021-08-14 14:46:42 +08001019 case MADV_WILLNEED:
Minchan Kimecb8ac82020-10-17 16:14:59 -07001020 return true;
1021 default:
1022 return false;
1023 }
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
1027 * The madvise(2) system call.
1028 *
1029 * Applications can use madvise() to advise the kernel how it should
1030 * handle paging I/O in this VM area. The idea is to help the kernel
1031 * use appropriate read-ahead and caching techniques. The information
1032 * provided is advisory only, and can be safely disregarded by the
1033 * kernel without affecting the correct operation of the application.
1034 *
1035 * behavior values:
1036 * MADV_NORMAL - the default behavior is to read clusters. This
1037 * results in some read-ahead and read-behind.
1038 * MADV_RANDOM - the system should read the minimum amount of data
1039 * on any access, since it is unlikely that the appli-
1040 * cation will need more than what it asks for.
1041 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1042 * once, so they can be aggressively read ahead, and
1043 * can be freed soon after they are accessed.
1044 * MADV_WILLNEED - the application is notifying the system to read
1045 * some pages ahead.
1046 * MADV_DONTNEED - the application is finished with the given range,
1047 * so the kernel can free resources associated with it.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001048 * MADV_FREE - the application marks pages in the given range as lazy free,
1049 * where actual purges are postponed until memory pressure happens.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08001050 * MADV_REMOVE - the application wants to free up the given range of
1051 * pages and associated backing store.
Hugh Dickins3866ea92009-09-21 17:01:52 -07001052 * MADV_DONTFORK - omit this area from child's address space when forking:
1053 * typically, to avoid COWing pages pinned by get_user_pages().
1054 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
Yang Shic02c3002017-10-13 15:57:37 -07001055 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1056 * range after a fork.
1057 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001058 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1059 * were corrupted by unrecoverable hardware memory failure.
1060 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001061 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1062 * this area with pages of identical content from other such areas.
1063 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
Naoya Horiguchid7206a72016-03-15 14:56:58 -07001064 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1065 * huge pages in the future. Existing pages might be coalesced and
1066 * new pages might be allocated as THP.
1067 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1068 * transparent huge pages so the existing pages will not be
1069 * coalesced into THP and new pages will not be allocated as THP.
1070 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1071 * from being included in its core dump.
1072 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
Minchan Kimecb8ac82020-10-17 16:14:59 -07001073 * MADV_COLD - the application is not expected to use this memory soon,
1074 * deactivate pages in this range so that they can be reclaimed
1075 * easily if memory pressure hanppens.
1076 * MADV_PAGEOUT - the application is not expected to use this memory soon,
1077 * page out the pages in this range immediately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
1079 * return values:
1080 * zero - success
1081 * -EINVAL - start + len < 0, start is not page-aligned,
1082 * "behavior" is not a valid value, or application
Yang Shic02c3002017-10-13 15:57:37 -07001083 * is attempting to release locked or shared pages,
1084 * or the specified address range includes file, Huge TLB,
1085 * MAP_SHARED or VMPFNMAP range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * -ENOMEM - addresses in the specified range are not currently
1087 * mapped, or are outside the AS of the process.
1088 * -EIO - an I/O error occurred while paging in data.
1089 * -EBADF - map exists, but area maps something that isn't a file.
1090 * -EAGAIN - a kernel resource was temporarily unavailable.
1091 */
Minchan Kim0726b012020-10-17 16:14:50 -07001092int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Prasanna Meda05b74382005-06-21 17:14:37 -07001094 unsigned long end, tmp;
Vladimir Cernovec9bed92013-09-11 14:20:15 -07001095 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int unmapped_error = 0;
1097 int error = -EINVAL;
Jason Baronf7977792007-07-15 23:38:21 -07001098 int write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 size_t len;
Shaohua Li1998cc02013-02-22 16:32:31 -08001100 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Andrey Konovalov057d33892019-09-25 16:48:30 -07001102 start = untagged_addr(start);
1103
Nick Piggin75927af2009-06-16 15:32:38 -07001104 if (!madvise_behavior_valid(behavior))
1105 return error;
1106
Wei Yangdf6c6502019-11-30 17:57:46 -08001107 if (!PAGE_ALIGNED(start))
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001108 return error;
Wei Yangdf6c6502019-11-30 17:57:46 -08001109 len = PAGE_ALIGN(len_in);
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001110
1111 /* Check to see whether len was rounded up from small -ve to zero */
1112 if (len_in && !len)
1113 return error;
1114
1115 end = start + len;
1116 if (end < start)
1117 return error;
1118
1119 error = 0;
1120 if (end == start)
1121 return error;
1122
Anshuman Khandual5e451be2017-05-03 14:55:28 -07001123#ifdef CONFIG_MEMORY_FAILURE
1124 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1125 return madvise_inject_error(behavior, start, start + len_in);
1126#endif
1127
Jason Baronf7977792007-07-15 23:38:21 -07001128 write = madvise_need_mmap_write(behavior);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001129 if (write) {
Minchan Kim0726b012020-10-17 16:14:50 -07001130 if (mmap_write_lock_killable(mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001131 return -EINTR;
1132 } else {
Minchan Kim0726b012020-10-17 16:14:50 -07001133 mmap_read_lock(mm);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /*
1137 * If the interval [start,end) covers some unmapped address
1138 * ranges, just ignore them, but return -ENOMEM at the end.
Prasanna Meda05b74382005-06-21 17:14:37 -07001139 * - different from the way of handling in mlock etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Minchan Kim0726b012020-10-17 16:14:50 -07001141 vma = find_vma_prev(mm, start, &prev);
Hugh Dickins836d5ff2005-09-03 15:54:53 -07001142 if (vma && start > vma->vm_start)
1143 prev = vma;
1144
Shaohua Li1998cc02013-02-22 16:32:31 -08001145 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 for (;;) {
1147 /* Still start < end. */
1148 error = -ENOMEM;
1149 if (!vma)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Prasanna Meda05b74382005-06-21 17:14:37 -07001152 /* Here start < (end|vma->vm_end). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (start < vma->vm_start) {
1154 unmapped_error = -ENOMEM;
1155 start = vma->vm_start;
Prasanna Meda05b74382005-06-21 17:14:37 -07001156 if (start >= end)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Prasanna Meda05b74382005-06-21 17:14:37 -07001160 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1161 tmp = vma->vm_end;
1162 if (end < tmp)
1163 tmp = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Prasanna Meda05b74382005-06-21 17:14:37 -07001165 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1166 error = madvise_vma(vma, &prev, start, tmp, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (error)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001168 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -07001169 start = tmp;
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001170 if (prev && start < prev->vm_end)
Prasanna Meda05b74382005-06-21 17:14:37 -07001171 start = prev->vm_end;
1172 error = unmapped_error;
1173 if (start >= end)
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001174 goto out;
Hugh Dickins90ed52e2007-03-29 01:20:38 -07001175 if (prev)
1176 vma = prev->vm_next;
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001177 else /* madvise_remove dropped mmap_lock */
Minchan Kim0726b012020-10-17 16:14:50 -07001178 vma = find_vma(mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180out:
Rasmus Villemoes84d96d82013-04-29 15:08:23 -07001181 blk_finish_plug(&plug);
Jason Baronf7977792007-07-15 23:38:21 -07001182 if (write)
Minchan Kim0726b012020-10-17 16:14:50 -07001183 mmap_write_unlock(mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001184 else
Minchan Kim0726b012020-10-17 16:14:50 -07001185 mmap_read_unlock(mm);
Nick Piggin0a27a142007-05-06 14:49:53 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return error;
1188}
Jens Axboedb08ca22019-12-25 22:14:54 -07001189
1190SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1191{
Minchan Kim0726b012020-10-17 16:14:50 -07001192 return do_madvise(current->mm, start, len_in, behavior);
Jens Axboedb08ca22019-12-25 22:14:54 -07001193}
Minchan Kimecb8ac82020-10-17 16:14:59 -07001194
1195SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1196 size_t, vlen, int, behavior, unsigned int, flags)
1197{
1198 ssize_t ret;
1199 struct iovec iovstack[UIO_FASTIOV], iovec;
1200 struct iovec *iov = iovstack;
1201 struct iov_iter iter;
1202 struct pid *pid;
1203 struct task_struct *task;
1204 struct mm_struct *mm;
1205 size_t total_len;
1206 unsigned int f_flags;
1207
1208 if (flags != 0) {
1209 ret = -EINVAL;
1210 goto out;
1211 }
1212
1213 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1214 if (ret < 0)
1215 goto out;
1216
1217 pid = pidfd_get_pid(pidfd, &f_flags);
1218 if (IS_ERR(pid)) {
1219 ret = PTR_ERR(pid);
1220 goto free_iov;
1221 }
1222
1223 task = get_pid_task(pid, PIDTYPE_PID);
1224 if (!task) {
1225 ret = -ESRCH;
1226 goto put_pid;
1227 }
1228
Minchan Kima68a0262020-12-08 20:57:18 -08001229 if (!process_madvise_behavior_valid(behavior)) {
Minchan Kimecb8ac82020-10-17 16:14:59 -07001230 ret = -EINVAL;
1231 goto release_task;
1232 }
1233
Suren Baghdasaryan518f98e2021-03-12 21:08:06 -08001234 /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1235 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001236 if (IS_ERR_OR_NULL(mm)) {
1237 ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1238 goto release_task;
1239 }
1240
Suren Baghdasaryan518f98e2021-03-12 21:08:06 -08001241 /*
1242 * Require CAP_SYS_NICE for influencing process performance. Note that
1243 * only non-destructive hints are currently supported.
1244 */
1245 if (!capable(CAP_SYS_NICE)) {
1246 ret = -EPERM;
1247 goto release_mm;
1248 }
1249
Minchan Kimecb8ac82020-10-17 16:14:59 -07001250 total_len = iov_iter_count(&iter);
1251
1252 while (iov_iter_count(&iter)) {
1253 iovec = iov_iter_iovec(&iter);
1254 ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1255 iovec.iov_len, behavior);
1256 if (ret < 0)
1257 break;
1258 iov_iter_advance(&iter, iovec.iov_len);
1259 }
1260
Charan Teja Kalla8b354e32022-03-22 14:46:44 -07001261 ret = (total_len - iov_iter_count(&iter)) ? : ret;
Minchan Kimecb8ac82020-10-17 16:14:59 -07001262
Suren Baghdasaryan518f98e2021-03-12 21:08:06 -08001263release_mm:
Minchan Kimecb8ac82020-10-17 16:14:59 -07001264 mmput(mm);
Minchan Kimecb8ac82020-10-17 16:14:59 -07001265release_task:
1266 put_task_struct(task);
1267put_pid:
1268 put_pid(pid);
1269free_iov:
1270 kfree(iov);
1271out:
1272 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}