mm: refactor vm_area_struct::anon_vma_name usage code
[linux-block.git] / mm / madvise.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *      linux/mm/madvise.c
4  *
5  * Copyright (C) 1999  Linus Torvalds
6  * Copyright (C) 2002  Christoph Hellwig
7  */
8
9 #include <linux/mman.h>
10 #include <linux/pagemap.h>
11 #include <linux/syscalls.h>
12 #include <linux/mempolicy.h>
13 #include <linux/page-isolation.h>
14 #include <linux/page_idle.h>
15 #include <linux/userfaultfd_k.h>
16 #include <linux/hugetlb.h>
17 #include <linux/falloc.h>
18 #include <linux/fadvise.h>
19 #include <linux/sched.h>
20 #include <linux/sched/mm.h>
21 #include <linux/mm_inline.h>
22 #include <linux/string.h>
23 #include <linux/uio.h>
24 #include <linux/ksm.h>
25 #include <linux/fs.h>
26 #include <linux/file.h>
27 #include <linux/blkdev.h>
28 #include <linux/backing-dev.h>
29 #include <linux/pagewalk.h>
30 #include <linux/swap.h>
31 #include <linux/swapops.h>
32 #include <linux/shmem_fs.h>
33 #include <linux/mmu_notifier.h>
34
35 #include <asm/tlb.h>
36
37 #include "internal.h"
38
39 struct madvise_walk_private {
40         struct mmu_gather *tlb;
41         bool pageout;
42 };
43
44 /*
45  * Any behaviour which results in changes to the vma->vm_flags needs to
46  * take mmap_lock for writing. Others, which simply traverse vmas, need
47  * to only take it for reading.
48  */
49 static int madvise_need_mmap_write(int behavior)
50 {
51         switch (behavior) {
52         case MADV_REMOVE:
53         case MADV_WILLNEED:
54         case MADV_DONTNEED:
55         case MADV_COLD:
56         case MADV_PAGEOUT:
57         case MADV_FREE:
58         case MADV_POPULATE_READ:
59         case MADV_POPULATE_WRITE:
60                 return 0;
61         default:
62                 /* be safe, default to 1. list exceptions explicitly */
63                 return 1;
64         }
65 }
66
67 #ifdef CONFIG_ANON_VMA_NAME
68 struct anon_vma_name *anon_vma_name_alloc(const char *name)
69 {
70         struct anon_vma_name *anon_name;
71         size_t count;
72
73         /* Add 1 for NUL terminator at the end of the anon_name->name */
74         count = strlen(name) + 1;
75         anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL);
76         if (anon_name) {
77                 kref_init(&anon_name->kref);
78                 memcpy(anon_name->name, name, count);
79         }
80
81         return anon_name;
82 }
83
84 void anon_vma_name_free(struct kref *kref)
85 {
86         struct anon_vma_name *anon_name =
87                         container_of(kref, struct anon_vma_name, kref);
88         kfree(anon_name);
89 }
90
91 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
92 {
93         mmap_assert_locked(vma->vm_mm);
94
95         if (vma->vm_file)
96                 return NULL;
97
98         return vma->anon_name;
99 }
100
101 /* mmap_lock should be write-locked */
102 static int replace_anon_vma_name(struct vm_area_struct *vma,
103                                  struct anon_vma_name *anon_name)
104 {
105         struct anon_vma_name *orig_name = anon_vma_name(vma);
106
107         if (!anon_name) {
108                 vma->anon_name = NULL;
109                 anon_vma_name_put(orig_name);
110                 return 0;
111         }
112
113         if (anon_vma_name_eq(orig_name, anon_name))
114                 return 0;
115
116         anon_vma_name_get(anon_name);
117         vma->anon_name = anon_name;
118         anon_vma_name_put(orig_name);
119
120         return 0;
121 }
122 #else /* CONFIG_ANON_VMA_NAME */
123 static int replace_anon_vma_name(struct vm_area_struct *vma,
124                                  struct anon_vma_name *anon_name)
125 {
126         if (anon_name)
127                 return -EINVAL;
128
129         return 0;
130 }
131 #endif /* CONFIG_ANON_VMA_NAME */
132 /*
133  * Update the vm_flags on region of a vma, splitting it or merging it as
134  * necessary.  Must be called with mmap_sem held for writing;
135  */
136 static int madvise_update_vma(struct vm_area_struct *vma,
137                               struct vm_area_struct **prev, unsigned long start,
138                               unsigned long end, unsigned long new_flags,
139                               struct anon_vma_name *anon_name)
140 {
141         struct mm_struct *mm = vma->vm_mm;
142         int error;
143         pgoff_t pgoff;
144
145         if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) {
146                 *prev = vma;
147                 return 0;
148         }
149
150         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
151         *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
152                           vma->vm_file, pgoff, vma_policy(vma),
153                           vma->vm_userfaultfd_ctx, anon_name);
154         if (*prev) {
155                 vma = *prev;
156                 goto success;
157         }
158
159         *prev = vma;
160
161         if (start != vma->vm_start) {
162                 if (unlikely(mm->map_count >= sysctl_max_map_count))
163                         return -ENOMEM;
164                 error = __split_vma(mm, vma, start, 1);
165                 if (error)
166                         return error;
167         }
168
169         if (end != vma->vm_end) {
170                 if (unlikely(mm->map_count >= sysctl_max_map_count))
171                         return -ENOMEM;
172                 error = __split_vma(mm, vma, end, 0);
173                 if (error)
174                         return error;
175         }
176
177 success:
178         /*
179          * vm_flags is protected by the mmap_lock held in write mode.
180          */
181         vma->vm_flags = new_flags;
182         if (!vma->vm_file) {
183                 error = replace_anon_vma_name(vma, anon_name);
184                 if (error)
185                         return error;
186         }
187
188         return 0;
189 }
190
191 #ifdef CONFIG_SWAP
192 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
193         unsigned long end, struct mm_walk *walk)
194 {
195         pte_t *orig_pte;
196         struct vm_area_struct *vma = walk->private;
197         unsigned long index;
198
199         if (pmd_none_or_trans_huge_or_clear_bad(pmd))
200                 return 0;
201
202         for (index = start; index != end; index += PAGE_SIZE) {
203                 pte_t pte;
204                 swp_entry_t entry;
205                 struct page *page;
206                 spinlock_t *ptl;
207
208                 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
209                 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
210                 pte_unmap_unlock(orig_pte, ptl);
211
212                 if (pte_present(pte) || pte_none(pte))
213                         continue;
214                 entry = pte_to_swp_entry(pte);
215                 if (unlikely(non_swap_entry(entry)))
216                         continue;
217
218                 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
219                                                         vma, index, false);
220                 if (page)
221                         put_page(page);
222         }
223
224         return 0;
225 }
226
227 static const struct mm_walk_ops swapin_walk_ops = {
228         .pmd_entry              = swapin_walk_pmd_entry,
229 };
230
231 static void force_shm_swapin_readahead(struct vm_area_struct *vma,
232                 unsigned long start, unsigned long end,
233                 struct address_space *mapping)
234 {
235         XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
236         pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
237         struct page *page;
238
239         rcu_read_lock();
240         xas_for_each(&xas, page, end_index) {
241                 swp_entry_t swap;
242
243                 if (!xa_is_value(page))
244                         continue;
245                 xas_pause(&xas);
246                 rcu_read_unlock();
247
248                 swap = radix_to_swp_entry(page);
249                 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
250                                                         NULL, 0, false);
251                 if (page)
252                         put_page(page);
253
254                 rcu_read_lock();
255         }
256         rcu_read_unlock();
257
258         lru_add_drain();        /* Push any new pages onto the LRU now */
259 }
260 #endif          /* CONFIG_SWAP */
261
262 /*
263  * Schedule all required I/O operations.  Do not wait for completion.
264  */
265 static long madvise_willneed(struct vm_area_struct *vma,
266                              struct vm_area_struct **prev,
267                              unsigned long start, unsigned long end)
268 {
269         struct mm_struct *mm = vma->vm_mm;
270         struct file *file = vma->vm_file;
271         loff_t offset;
272
273         *prev = vma;
274 #ifdef CONFIG_SWAP
275         if (!file) {
276                 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
277                 lru_add_drain(); /* Push any new pages onto the LRU now */
278                 return 0;
279         }
280
281         if (shmem_mapping(file->f_mapping)) {
282                 force_shm_swapin_readahead(vma, start, end,
283                                         file->f_mapping);
284                 return 0;
285         }
286 #else
287         if (!file)
288                 return -EBADF;
289 #endif
290
291         if (IS_DAX(file_inode(file))) {
292                 /* no bad return value, but ignore advice */
293                 return 0;
294         }
295
296         /*
297          * Filesystem's fadvise may need to take various locks.  We need to
298          * explicitly grab a reference because the vma (and hence the
299          * vma's reference to the file) can go away as soon as we drop
300          * mmap_lock.
301          */
302         *prev = NULL;   /* tell sys_madvise we drop mmap_lock */
303         get_file(file);
304         offset = (loff_t)(start - vma->vm_start)
305                         + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
306         mmap_read_unlock(mm);
307         vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
308         fput(file);
309         mmap_read_lock(mm);
310         return 0;
311 }
312
313 static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
314                                 unsigned long addr, unsigned long end,
315                                 struct mm_walk *walk)
316 {
317         struct madvise_walk_private *private = walk->private;
318         struct mmu_gather *tlb = private->tlb;
319         bool pageout = private->pageout;
320         struct mm_struct *mm = tlb->mm;
321         struct vm_area_struct *vma = walk->vma;
322         pte_t *orig_pte, *pte, ptent;
323         spinlock_t *ptl;
324         struct page *page = NULL;
325         LIST_HEAD(page_list);
326
327         if (fatal_signal_pending(current))
328                 return -EINTR;
329
330 #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);
351
352                 /* Do not interfere with other mappings of this page */
353                 if (page_mapcount(page) != 1)
354                         goto huge_unlock;
355
356                 if (next - addr != HPAGE_PMD_SIZE) {
357                         int err;
358
359                         get_page(page);
360                         spin_unlock(ptl);
361                         lock_page(page);
362                         err = split_huge_page(page);
363                         unlock_page(page);
364                         put_page(page);
365                         if (!err)
366                                 goto regular_page;
367                         return 0;
368                 }
369
370                 if (pmd_young(orig_pmd)) {
371                         pmdp_invalidate(vma, addr, pmd);
372                         orig_pmd = pmd_mkold(orig_pmd);
373
374                         set_pmd_at(mm, addr, pmd, orig_pmd);
375                         tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
376                 }
377
378                 ClearPageReferenced(page);
379                 test_and_clear_page_young(page);
380                 if (pageout) {
381                         if (!isolate_lru_page(page)) {
382                                 if (PageUnevictable(page))
383                                         putback_lru_page(page);
384                                 else
385                                         list_add(&page->lru, &page_list);
386                         }
387                 } else
388                         deactivate_page(page);
389 huge_unlock:
390                 spin_unlock(ptl);
391                 if (pageout)
392                         reclaim_pages(&page_list);
393                 return 0;
394         }
395
396 regular_page:
397         if (pmd_trans_unstable(pmd))
398                 return 0;
399 #endif
400         tlb_change_page_size(tlb, PAGE_SIZE);
401         orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
402         flush_tlb_batched_pending(mm);
403         arch_enter_lazy_mmu_mode();
404         for (; addr < end; pte++, addr += PAGE_SIZE) {
405                 ptent = *pte;
406
407                 if (pte_none(ptent))
408                         continue;
409
410                 if (!pte_present(ptent))
411                         continue;
412
413                 page = vm_normal_page(vma, addr, ptent);
414                 if (!page)
415                         continue;
416
417                 /*
418                  * Creating a THP page is expensive so split it only if we
419                  * are sure it's worth. Split it if we are only owner.
420                  */
421                 if (PageTransCompound(page)) {
422                         if (page_mapcount(page) != 1)
423                                 break;
424                         get_page(page);
425                         if (!trylock_page(page)) {
426                                 put_page(page);
427                                 break;
428                         }
429                         pte_unmap_unlock(orig_pte, ptl);
430                         if (split_huge_page(page)) {
431                                 unlock_page(page);
432                                 put_page(page);
433                                 pte_offset_map_lock(mm, pmd, addr, &ptl);
434                                 break;
435                         }
436                         unlock_page(page);
437                         put_page(page);
438                         pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
439                         pte--;
440                         addr -= PAGE_SIZE;
441                         continue;
442                 }
443
444                 /* Do not interfere with other mappings of this page */
445                 if (page_mapcount(page) != 1)
446                         continue;
447
448                 VM_BUG_ON_PAGE(PageTransCompound(page), page);
449
450                 if (pte_young(ptent)) {
451                         ptent = ptep_get_and_clear_full(mm, addr, pte,
452                                                         tlb->fullmm);
453                         ptent = pte_mkold(ptent);
454                         set_pte_at(mm, addr, pte, ptent);
455                         tlb_remove_tlb_entry(tlb, pte, addr);
456                 }
457
458                 /*
459                  * We are deactivating a page for accelerating reclaiming.
460                  * VM couldn't reclaim the page unless we clear PG_young.
461                  * As a side effect, it makes confuse idle-page tracking
462                  * because they will miss recent referenced history.
463                  */
464                 ClearPageReferenced(page);
465                 test_and_clear_page_young(page);
466                 if (pageout) {
467                         if (!isolate_lru_page(page)) {
468                                 if (PageUnevictable(page))
469                                         putback_lru_page(page);
470                                 else
471                                         list_add(&page->lru, &page_list);
472                         }
473                 } else
474                         deactivate_page(page);
475         }
476
477         arch_leave_lazy_mmu_mode();
478         pte_unmap_unlock(orig_pte, ptl);
479         if (pageout)
480                 reclaim_pages(&page_list);
481         cond_resched();
482
483         return 0;
484 }
485
486 static const struct mm_walk_ops cold_walk_ops = {
487         .pmd_entry = madvise_cold_or_pageout_pte_range,
488 };
489
490 static void madvise_cold_page_range(struct mmu_gather *tlb,
491                              struct vm_area_struct *vma,
492                              unsigned long addr, unsigned long end)
493 {
494         struct madvise_walk_private walk_private = {
495                 .pageout = false,
496                 .tlb = tlb,
497         };
498
499         tlb_start_vma(tlb, vma);
500         walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
501         tlb_end_vma(tlb, vma);
502 }
503
504 static long madvise_cold(struct vm_area_struct *vma,
505                         struct vm_area_struct **prev,
506                         unsigned long start_addr, unsigned long end_addr)
507 {
508         struct mm_struct *mm = vma->vm_mm;
509         struct mmu_gather tlb;
510
511         *prev = vma;
512         if (!can_madv_lru_vma(vma))
513                 return -EINVAL;
514
515         lru_add_drain();
516         tlb_gather_mmu(&tlb, mm);
517         madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
518         tlb_finish_mmu(&tlb);
519
520         return 0;
521 }
522
523 static void madvise_pageout_page_range(struct mmu_gather *tlb,
524                              struct vm_area_struct *vma,
525                              unsigned long addr, unsigned long end)
526 {
527         struct madvise_walk_private walk_private = {
528                 .pageout = true,
529                 .tlb = tlb,
530         };
531
532         tlb_start_vma(tlb, vma);
533         walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
534         tlb_end_vma(tlb, vma);
535 }
536
537 static inline bool can_do_pageout(struct vm_area_struct *vma)
538 {
539         if (vma_is_anonymous(vma))
540                 return true;
541         if (!vma->vm_file)
542                 return false;
543         /*
544          * paging out pagecache only for non-anonymous mappings that correspond
545          * to the files the calling process could (if tried) open for writing;
546          * otherwise we'd be including shared non-exclusive mappings, which
547          * opens a side channel.
548          */
549         return inode_owner_or_capable(&init_user_ns,
550                                       file_inode(vma->vm_file)) ||
551                file_permission(vma->vm_file, MAY_WRITE) == 0;
552 }
553
554 static long madvise_pageout(struct vm_area_struct *vma,
555                         struct vm_area_struct **prev,
556                         unsigned long start_addr, unsigned long end_addr)
557 {
558         struct mm_struct *mm = vma->vm_mm;
559         struct mmu_gather tlb;
560
561         *prev = vma;
562         if (!can_madv_lru_vma(vma))
563                 return -EINVAL;
564
565         if (!can_do_pageout(vma))
566                 return 0;
567
568         lru_add_drain();
569         tlb_gather_mmu(&tlb, mm);
570         madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
571         tlb_finish_mmu(&tlb);
572
573         return 0;
574 }
575
576 static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
577                                 unsigned long end, struct mm_walk *walk)
578
579 {
580         struct mmu_gather *tlb = walk->private;
581         struct mm_struct *mm = tlb->mm;
582         struct vm_area_struct *vma = walk->vma;
583         spinlock_t *ptl;
584         pte_t *orig_pte, *pte, ptent;
585         struct page *page;
586         int nr_swap = 0;
587         unsigned long next;
588
589         next = pmd_addr_end(addr, end);
590         if (pmd_trans_huge(*pmd))
591                 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
592                         goto next;
593
594         if (pmd_trans_unstable(pmd))
595                 return 0;
596
597         tlb_change_page_size(tlb, PAGE_SIZE);
598         orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
599         flush_tlb_batched_pending(mm);
600         arch_enter_lazy_mmu_mode();
601         for (; addr != end; pte++, addr += PAGE_SIZE) {
602                 ptent = *pte;
603
604                 if (pte_none(ptent))
605                         continue;
606                 /*
607                  * If the pte has swp_entry, just clear page table to
608                  * prevent swap-in which is more expensive rather than
609                  * (page allocation + zeroing).
610                  */
611                 if (!pte_present(ptent)) {
612                         swp_entry_t entry;
613
614                         entry = pte_to_swp_entry(ptent);
615                         if (non_swap_entry(entry))
616                                 continue;
617                         nr_swap--;
618                         free_swap_and_cache(entry);
619                         pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
620                         continue;
621                 }
622
623                 page = vm_normal_page(vma, addr, ptent);
624                 if (!page)
625                         continue;
626
627                 /*
628                  * If pmd isn't transhuge but the page is THP and
629                  * is owned by only this process, split it and
630                  * deactivate all pages.
631                  */
632                 if (PageTransCompound(page)) {
633                         if (page_mapcount(page) != 1)
634                                 goto out;
635                         get_page(page);
636                         if (!trylock_page(page)) {
637                                 put_page(page);
638                                 goto out;
639                         }
640                         pte_unmap_unlock(orig_pte, ptl);
641                         if (split_huge_page(page)) {
642                                 unlock_page(page);
643                                 put_page(page);
644                                 pte_offset_map_lock(mm, pmd, addr, &ptl);
645                                 goto out;
646                         }
647                         unlock_page(page);
648                         put_page(page);
649                         pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
650                         pte--;
651                         addr -= PAGE_SIZE;
652                         continue;
653                 }
654
655                 VM_BUG_ON_PAGE(PageTransCompound(page), page);
656
657                 if (PageSwapCache(page) || PageDirty(page)) {
658                         if (!trylock_page(page))
659                                 continue;
660                         /*
661                          * If page is shared with others, we couldn't clear
662                          * PG_dirty of the page.
663                          */
664                         if (page_mapcount(page) != 1) {
665                                 unlock_page(page);
666                                 continue;
667                         }
668
669                         if (PageSwapCache(page) && !try_to_free_swap(page)) {
670                                 unlock_page(page);
671                                 continue;
672                         }
673
674                         ClearPageDirty(page);
675                         unlock_page(page);
676                 }
677
678                 if (pte_young(ptent) || pte_dirty(ptent)) {
679                         /*
680                          * Some of architecture(ex, PPC) don't update TLB
681                          * with set_pte_at and tlb_remove_tlb_entry so for
682                          * the portability, remap the pte with old|clean
683                          * after pte clearing.
684                          */
685                         ptent = ptep_get_and_clear_full(mm, addr, pte,
686                                                         tlb->fullmm);
687
688                         ptent = pte_mkold(ptent);
689                         ptent = pte_mkclean(ptent);
690                         set_pte_at(mm, addr, pte, ptent);
691                         tlb_remove_tlb_entry(tlb, pte, addr);
692                 }
693                 mark_page_lazyfree(page);
694         }
695 out:
696         if (nr_swap) {
697                 if (current->mm == mm)
698                         sync_mm_rss(mm);
699
700                 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
701         }
702         arch_leave_lazy_mmu_mode();
703         pte_unmap_unlock(orig_pte, ptl);
704         cond_resched();
705 next:
706         return 0;
707 }
708
709 static const struct mm_walk_ops madvise_free_walk_ops = {
710         .pmd_entry              = madvise_free_pte_range,
711 };
712
713 static int madvise_free_single_vma(struct vm_area_struct *vma,
714                         unsigned long start_addr, unsigned long end_addr)
715 {
716         struct mm_struct *mm = vma->vm_mm;
717         struct mmu_notifier_range range;
718         struct mmu_gather tlb;
719
720         /* MADV_FREE works for only anon vma at the moment */
721         if (!vma_is_anonymous(vma))
722                 return -EINVAL;
723
724         range.start = max(vma->vm_start, start_addr);
725         if (range.start >= vma->vm_end)
726                 return -EINVAL;
727         range.end = min(vma->vm_end, end_addr);
728         if (range.end <= vma->vm_start)
729                 return -EINVAL;
730         mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
731                                 range.start, range.end);
732
733         lru_add_drain();
734         tlb_gather_mmu(&tlb, mm);
735         update_hiwater_rss(mm);
736
737         mmu_notifier_invalidate_range_start(&range);
738         tlb_start_vma(&tlb, vma);
739         walk_page_range(vma->vm_mm, range.start, range.end,
740                         &madvise_free_walk_ops, &tlb);
741         tlb_end_vma(&tlb, vma);
742         mmu_notifier_invalidate_range_end(&range);
743         tlb_finish_mmu(&tlb);
744
745         return 0;
746 }
747
748 /*
749  * Application no longer needs these pages.  If the pages are dirty,
750  * it's OK to just throw them away.  The app will be more careful about
751  * data it wants to keep.  Be sure to free swap resources too.  The
752  * zap_page_range call sets things up for shrink_active_list to actually free
753  * these pages later if no one else has touched them in the meantime,
754  * although we could add these pages to a global reuse list for
755  * shrink_active_list to pick up before reclaiming other pages.
756  *
757  * NB: This interface discards data rather than pushes it out to swap,
758  * as some implementations do.  This has performance implications for
759  * applications like large transactional databases which want to discard
760  * pages in anonymous maps after committing to backing store the data
761  * that was kept in them.  There is no reason to write this data out to
762  * the swap area if the application is discarding it.
763  *
764  * An interface that causes the system to free clean pages and flush
765  * dirty pages is already available as msync(MS_INVALIDATE).
766  */
767 static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
768                                         unsigned long start, unsigned long end)
769 {
770         zap_page_range(vma, start, end - start);
771         return 0;
772 }
773
774 static long madvise_dontneed_free(struct vm_area_struct *vma,
775                                   struct vm_area_struct **prev,
776                                   unsigned long start, unsigned long end,
777                                   int behavior)
778 {
779         struct mm_struct *mm = vma->vm_mm;
780
781         *prev = vma;
782         if (!can_madv_lru_vma(vma))
783                 return -EINVAL;
784
785         if (!userfaultfd_remove(vma, start, end)) {
786                 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
787
788                 mmap_read_lock(mm);
789                 vma = find_vma(mm, start);
790                 if (!vma)
791                         return -ENOMEM;
792                 if (start < vma->vm_start) {
793                         /*
794                          * This "vma" under revalidation is the one
795                          * with the lowest vma->vm_start where start
796                          * is also < vma->vm_end. If start <
797                          * vma->vm_start it means an hole materialized
798                          * in the user address space within the
799                          * virtual range passed to MADV_DONTNEED
800                          * or MADV_FREE.
801                          */
802                         return -ENOMEM;
803                 }
804                 if (!can_madv_lru_vma(vma))
805                         return -EINVAL;
806                 if (end > vma->vm_end) {
807                         /*
808                          * Don't fail if end > vma->vm_end. If the old
809                          * vma was split while the mmap_lock was
810                          * released the effect of the concurrent
811                          * operation may not cause madvise() to
812                          * have an undefined result. There may be an
813                          * adjacent next vma that we'll walk
814                          * next. userfaultfd_remove() will generate an
815                          * UFFD_EVENT_REMOVE repetition on the
816                          * end-vma->vm_end range, but the manager can
817                          * handle a repetition fine.
818                          */
819                         end = vma->vm_end;
820                 }
821                 VM_WARN_ON(start >= end);
822         }
823
824         if (behavior == MADV_DONTNEED)
825                 return madvise_dontneed_single_vma(vma, start, end);
826         else if (behavior == MADV_FREE)
827                 return madvise_free_single_vma(vma, start, end);
828         else
829                 return -EINVAL;
830 }
831
832 static long madvise_populate(struct vm_area_struct *vma,
833                              struct vm_area_struct **prev,
834                              unsigned long start, unsigned long end,
835                              int behavior)
836 {
837         const bool write = behavior == MADV_POPULATE_WRITE;
838         struct mm_struct *mm = vma->vm_mm;
839         unsigned long tmp_end;
840         int locked = 1;
841         long pages;
842
843         *prev = vma;
844
845         while (start < end) {
846                 /*
847                  * We might have temporarily dropped the lock. For example,
848                  * our VMA might have been split.
849                  */
850                 if (!vma || start >= vma->vm_end) {
851                         vma = find_vma(mm, start);
852                         if (!vma || start < vma->vm_start)
853                                 return -ENOMEM;
854                 }
855
856                 tmp_end = min_t(unsigned long, end, vma->vm_end);
857                 /* Populate (prefault) page tables readable/writable. */
858                 pages = faultin_vma_page_range(vma, start, tmp_end, write,
859                                                &locked);
860                 if (!locked) {
861                         mmap_read_lock(mm);
862                         locked = 1;
863                         *prev = NULL;
864                         vma = NULL;
865                 }
866                 if (pages < 0) {
867                         switch (pages) {
868                         case -EINTR:
869                                 return -EINTR;
870                         case -EINVAL: /* Incompatible mappings / permissions. */
871                                 return -EINVAL;
872                         case -EHWPOISON:
873                                 return -EHWPOISON;
874                         case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
875                                 return -EFAULT;
876                         default:
877                                 pr_warn_once("%s: unhandled return value: %ld\n",
878                                              __func__, pages);
879                                 fallthrough;
880                         case -ENOMEM:
881                                 return -ENOMEM;
882                         }
883                 }
884                 start += pages * PAGE_SIZE;
885         }
886         return 0;
887 }
888
889 /*
890  * Application wants to free up the pages and associated backing store.
891  * This is effectively punching a hole into the middle of a file.
892  */
893 static long madvise_remove(struct vm_area_struct *vma,
894                                 struct vm_area_struct **prev,
895                                 unsigned long start, unsigned long end)
896 {
897         loff_t offset;
898         int error;
899         struct file *f;
900         struct mm_struct *mm = vma->vm_mm;
901
902         *prev = NULL;   /* tell sys_madvise we drop mmap_lock */
903
904         if (vma->vm_flags & VM_LOCKED)
905                 return -EINVAL;
906
907         f = vma->vm_file;
908
909         if (!f || !f->f_mapping || !f->f_mapping->host) {
910                         return -EINVAL;
911         }
912
913         if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
914                 return -EACCES;
915
916         offset = (loff_t)(start - vma->vm_start)
917                         + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
918
919         /*
920          * Filesystem's fallocate may need to take i_rwsem.  We need to
921          * explicitly grab a reference because the vma (and hence the
922          * vma's reference to the file) can go away as soon as we drop
923          * mmap_lock.
924          */
925         get_file(f);
926         if (userfaultfd_remove(vma, start, end)) {
927                 /* mmap_lock was not released by userfaultfd_remove() */
928                 mmap_read_unlock(mm);
929         }
930         error = vfs_fallocate(f,
931                                 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
932                                 offset, end - start);
933         fput(f);
934         mmap_read_lock(mm);
935         return error;
936 }
937
938 /*
939  * Apply an madvise behavior to a region of a vma.  madvise_update_vma
940  * will handle splitting a vm area into separate areas, each area with its own
941  * behavior.
942  */
943 static int madvise_vma_behavior(struct vm_area_struct *vma,
944                                 struct vm_area_struct **prev,
945                                 unsigned long start, unsigned long end,
946                                 unsigned long behavior)
947 {
948         int error;
949         unsigned long new_flags = vma->vm_flags;
950
951         switch (behavior) {
952         case MADV_REMOVE:
953                 return madvise_remove(vma, prev, start, end);
954         case MADV_WILLNEED:
955                 return madvise_willneed(vma, prev, start, end);
956         case MADV_COLD:
957                 return madvise_cold(vma, prev, start, end);
958         case MADV_PAGEOUT:
959                 return madvise_pageout(vma, prev, start, end);
960         case MADV_FREE:
961         case MADV_DONTNEED:
962                 return madvise_dontneed_free(vma, prev, start, end, behavior);
963         case MADV_POPULATE_READ:
964         case MADV_POPULATE_WRITE:
965                 return madvise_populate(vma, prev, start, end, behavior);
966         case MADV_NORMAL:
967                 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
968                 break;
969         case MADV_SEQUENTIAL:
970                 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
971                 break;
972         case MADV_RANDOM:
973                 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
974                 break;
975         case MADV_DONTFORK:
976                 new_flags |= VM_DONTCOPY;
977                 break;
978         case MADV_DOFORK:
979                 if (vma->vm_flags & VM_IO)
980                         return -EINVAL;
981                 new_flags &= ~VM_DONTCOPY;
982                 break;
983         case MADV_WIPEONFORK:
984                 /* MADV_WIPEONFORK is only supported on anonymous memory. */
985                 if (vma->vm_file || vma->vm_flags & VM_SHARED)
986                         return -EINVAL;
987                 new_flags |= VM_WIPEONFORK;
988                 break;
989         case MADV_KEEPONFORK:
990                 new_flags &= ~VM_WIPEONFORK;
991                 break;
992         case MADV_DONTDUMP:
993                 new_flags |= VM_DONTDUMP;
994                 break;
995         case MADV_DODUMP:
996                 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL)
997                         return -EINVAL;
998                 new_flags &= ~VM_DONTDUMP;
999                 break;
1000         case MADV_MERGEABLE:
1001         case MADV_UNMERGEABLE:
1002                 error = ksm_madvise(vma, start, end, behavior, &new_flags);
1003                 if (error)
1004                         goto out;
1005                 break;
1006         case MADV_HUGEPAGE:
1007         case MADV_NOHUGEPAGE:
1008                 error = hugepage_madvise(vma, &new_flags, behavior);
1009                 if (error)
1010                         goto out;
1011                 break;
1012         }
1013
1014         error = madvise_update_vma(vma, prev, start, end, new_flags,
1015                                    anon_vma_name(vma));
1016
1017 out:
1018         /*
1019          * madvise() returns EAGAIN if kernel resources, such as
1020          * slab, are temporarily unavailable.
1021          */
1022         if (error == -ENOMEM)
1023                 error = -EAGAIN;
1024         return error;
1025 }
1026
1027 #ifdef CONFIG_MEMORY_FAILURE
1028 /*
1029  * Error injection support for memory error handling.
1030  */
1031 static int madvise_inject_error(int behavior,
1032                 unsigned long start, unsigned long end)
1033 {
1034         unsigned long size;
1035
1036         if (!capable(CAP_SYS_ADMIN))
1037                 return -EPERM;
1038
1039
1040         for (; start < end; start += size) {
1041                 unsigned long pfn;
1042                 struct page *page;
1043                 int ret;
1044
1045                 ret = get_user_pages_fast(start, 1, 0, &page);
1046                 if (ret != 1)
1047                         return ret;
1048                 pfn = page_to_pfn(page);
1049
1050                 /*
1051                  * When soft offlining hugepages, after migrating the page
1052                  * we dissolve it, therefore in the second loop "page" will
1053                  * no longer be a compound page.
1054                  */
1055                 size = page_size(compound_head(page));
1056
1057                 if (behavior == MADV_SOFT_OFFLINE) {
1058                         pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
1059                                  pfn, start);
1060                         ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
1061                 } else {
1062                         pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
1063                                  pfn, start);
1064                         ret = memory_failure(pfn, MF_COUNT_INCREASED);
1065                 }
1066
1067                 if (ret)
1068                         return ret;
1069         }
1070
1071         return 0;
1072 }
1073 #endif
1074
1075 static bool
1076 madvise_behavior_valid(int behavior)
1077 {
1078         switch (behavior) {
1079         case MADV_DOFORK:
1080         case MADV_DONTFORK:
1081         case MADV_NORMAL:
1082         case MADV_SEQUENTIAL:
1083         case MADV_RANDOM:
1084         case MADV_REMOVE:
1085         case MADV_WILLNEED:
1086         case MADV_DONTNEED:
1087         case MADV_FREE:
1088         case MADV_COLD:
1089         case MADV_PAGEOUT:
1090         case MADV_POPULATE_READ:
1091         case MADV_POPULATE_WRITE:
1092 #ifdef CONFIG_KSM
1093         case MADV_MERGEABLE:
1094         case MADV_UNMERGEABLE:
1095 #endif
1096 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1097         case MADV_HUGEPAGE:
1098         case MADV_NOHUGEPAGE:
1099 #endif
1100         case MADV_DONTDUMP:
1101         case MADV_DODUMP:
1102         case MADV_WIPEONFORK:
1103         case MADV_KEEPONFORK:
1104 #ifdef CONFIG_MEMORY_FAILURE
1105         case MADV_SOFT_OFFLINE:
1106         case MADV_HWPOISON:
1107 #endif
1108                 return true;
1109
1110         default:
1111                 return false;
1112         }
1113 }
1114
1115 static bool
1116 process_madvise_behavior_valid(int behavior)
1117 {
1118         switch (behavior) {
1119         case MADV_COLD:
1120         case MADV_PAGEOUT:
1121         case MADV_WILLNEED:
1122                 return true;
1123         default:
1124                 return false;
1125         }
1126 }
1127
1128 /*
1129  * Walk the vmas in range [start,end), and call the visit function on each one.
1130  * The visit function will get start and end parameters that cover the overlap
1131  * between the current vma and the original range.  Any unmapped regions in the
1132  * original range will result in this function returning -ENOMEM while still
1133  * calling the visit function on all of the existing vmas in the range.
1134  * Must be called with the mmap_lock held for reading or writing.
1135  */
1136 static
1137 int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
1138                       unsigned long end, unsigned long arg,
1139                       int (*visit)(struct vm_area_struct *vma,
1140                                    struct vm_area_struct **prev, unsigned long start,
1141                                    unsigned long end, unsigned long arg))
1142 {
1143         struct vm_area_struct *vma;
1144         struct vm_area_struct *prev;
1145         unsigned long tmp;
1146         int unmapped_error = 0;
1147
1148         /*
1149          * If the interval [start,end) covers some unmapped address
1150          * ranges, just ignore them, but return -ENOMEM at the end.
1151          * - different from the way of handling in mlock etc.
1152          */
1153         vma = find_vma_prev(mm, start, &prev);
1154         if (vma && start > vma->vm_start)
1155                 prev = vma;
1156
1157         for (;;) {
1158                 int error;
1159
1160                 /* Still start < end. */
1161                 if (!vma)
1162                         return -ENOMEM;
1163
1164                 /* Here start < (end|vma->vm_end). */
1165                 if (start < vma->vm_start) {
1166                         unmapped_error = -ENOMEM;
1167                         start = vma->vm_start;
1168                         if (start >= end)
1169                                 break;
1170                 }
1171
1172                 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1173                 tmp = vma->vm_end;
1174                 if (end < tmp)
1175                         tmp = end;
1176
1177                 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1178                 error = visit(vma, &prev, start, tmp, arg);
1179                 if (error)
1180                         return error;
1181                 start = tmp;
1182                 if (prev && start < prev->vm_end)
1183                         start = prev->vm_end;
1184                 if (start >= end)
1185                         break;
1186                 if (prev)
1187                         vma = prev->vm_next;
1188                 else    /* madvise_remove dropped mmap_lock */
1189                         vma = find_vma(mm, start);
1190         }
1191
1192         return unmapped_error;
1193 }
1194
1195 #ifdef CONFIG_ANON_VMA_NAME
1196 static int madvise_vma_anon_name(struct vm_area_struct *vma,
1197                                  struct vm_area_struct **prev,
1198                                  unsigned long start, unsigned long end,
1199                                  unsigned long anon_name)
1200 {
1201         int error;
1202
1203         /* Only anonymous mappings can be named */
1204         if (vma->vm_file)
1205                 return -EBADF;
1206
1207         error = madvise_update_vma(vma, prev, start, end, vma->vm_flags,
1208                                    (struct anon_vma_name *)anon_name);
1209
1210         /*
1211          * madvise() returns EAGAIN if kernel resources, such as
1212          * slab, are temporarily unavailable.
1213          */
1214         if (error == -ENOMEM)
1215                 error = -EAGAIN;
1216         return error;
1217 }
1218
1219 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
1220                           unsigned long len_in, struct anon_vma_name *anon_name)
1221 {
1222         unsigned long end;
1223         unsigned long len;
1224
1225         if (start & ~PAGE_MASK)
1226                 return -EINVAL;
1227         len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1228
1229         /* Check to see whether len was rounded up from small -ve to zero */
1230         if (len_in && !len)
1231                 return -EINVAL;
1232
1233         end = start + len;
1234         if (end < start)
1235                 return -EINVAL;
1236
1237         if (end == start)
1238                 return 0;
1239
1240         return madvise_walk_vmas(mm, start, end, (unsigned long)anon_name,
1241                                  madvise_vma_anon_name);
1242 }
1243 #endif /* CONFIG_ANON_VMA_NAME */
1244 /*
1245  * The madvise(2) system call.
1246  *
1247  * Applications can use madvise() to advise the kernel how it should
1248  * handle paging I/O in this VM area.  The idea is to help the kernel
1249  * use appropriate read-ahead and caching techniques.  The information
1250  * provided is advisory only, and can be safely disregarded by the
1251  * kernel without affecting the correct operation of the application.
1252  *
1253  * behavior values:
1254  *  MADV_NORMAL - the default behavior is to read clusters.  This
1255  *              results in some read-ahead and read-behind.
1256  *  MADV_RANDOM - the system should read the minimum amount of data
1257  *              on any access, since it is unlikely that the appli-
1258  *              cation will need more than what it asks for.
1259  *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
1260  *              once, so they can be aggressively read ahead, and
1261  *              can be freed soon after they are accessed.
1262  *  MADV_WILLNEED - the application is notifying the system to read
1263  *              some pages ahead.
1264  *  MADV_DONTNEED - the application is finished with the given range,
1265  *              so the kernel can free resources associated with it.
1266  *  MADV_FREE - the application marks pages in the given range as lazy free,
1267  *              where actual purges are postponed until memory pressure happens.
1268  *  MADV_REMOVE - the application wants to free up the given range of
1269  *              pages and associated backing store.
1270  *  MADV_DONTFORK - omit this area from child's address space when forking:
1271  *              typically, to avoid COWing pages pinned by get_user_pages().
1272  *  MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
1273  *  MADV_WIPEONFORK - present the child process with zero-filled memory in this
1274  *              range after a fork.
1275  *  MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
1276  *  MADV_HWPOISON - trigger memory error handler as if the given memory range
1277  *              were corrupted by unrecoverable hardware memory failure.
1278  *  MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
1279  *  MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1280  *              this area with pages of identical content from other such areas.
1281  *  MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
1282  *  MADV_HUGEPAGE - the application wants to back the given range by transparent
1283  *              huge pages in the future. Existing pages might be coalesced and
1284  *              new pages might be allocated as THP.
1285  *  MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1286  *              transparent huge pages so the existing pages will not be
1287  *              coalesced into THP and new pages will not be allocated as THP.
1288  *  MADV_DONTDUMP - the application wants to prevent pages in the given range
1289  *              from being included in its core dump.
1290  *  MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1291  *  MADV_COLD - the application is not expected to use this memory soon,
1292  *              deactivate pages in this range so that they can be reclaimed
1293  *              easily if memory pressure happens.
1294  *  MADV_PAGEOUT - the application is not expected to use this memory soon,
1295  *              page out the pages in this range immediately.
1296  *  MADV_POPULATE_READ - populate (prefault) page tables readable by
1297  *              triggering read faults if required
1298  *  MADV_POPULATE_WRITE - populate (prefault) page tables writable by
1299  *              triggering write faults if required
1300  *
1301  * return values:
1302  *  zero    - success
1303  *  -EINVAL - start + len < 0, start is not page-aligned,
1304  *              "behavior" is not a valid value, or application
1305  *              is attempting to release locked or shared pages,
1306  *              or the specified address range includes file, Huge TLB,
1307  *              MAP_SHARED or VMPFNMAP range.
1308  *  -ENOMEM - addresses in the specified range are not currently
1309  *              mapped, or are outside the AS of the process.
1310  *  -EIO    - an I/O error occurred while paging in data.
1311  *  -EBADF  - map exists, but area maps something that isn't a file.
1312  *  -EAGAIN - a kernel resource was temporarily unavailable.
1313  */
1314 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
1315 {
1316         unsigned long end;
1317         int error;
1318         int write;
1319         size_t len;
1320         struct blk_plug plug;
1321
1322         start = untagged_addr(start);
1323
1324         if (!madvise_behavior_valid(behavior))
1325                 return -EINVAL;
1326
1327         if (!PAGE_ALIGNED(start))
1328                 return -EINVAL;
1329         len = PAGE_ALIGN(len_in);
1330
1331         /* Check to see whether len was rounded up from small -ve to zero */
1332         if (len_in && !len)
1333                 return -EINVAL;
1334
1335         end = start + len;
1336         if (end < start)
1337                 return -EINVAL;
1338
1339         if (end == start)
1340                 return 0;
1341
1342 #ifdef CONFIG_MEMORY_FAILURE
1343         if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1344                 return madvise_inject_error(behavior, start, start + len_in);
1345 #endif
1346
1347         write = madvise_need_mmap_write(behavior);
1348         if (write) {
1349                 if (mmap_write_lock_killable(mm))
1350                         return -EINTR;
1351         } else {
1352                 mmap_read_lock(mm);
1353         }
1354
1355         blk_start_plug(&plug);
1356         error = madvise_walk_vmas(mm, start, end, behavior,
1357                         madvise_vma_behavior);
1358         blk_finish_plug(&plug);
1359         if (write)
1360                 mmap_write_unlock(mm);
1361         else
1362                 mmap_read_unlock(mm);
1363
1364         return error;
1365 }
1366
1367 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1368 {
1369         return do_madvise(current->mm, start, len_in, behavior);
1370 }
1371
1372 SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1373                 size_t, vlen, int, behavior, unsigned int, flags)
1374 {
1375         ssize_t ret;
1376         struct iovec iovstack[UIO_FASTIOV], iovec;
1377         struct iovec *iov = iovstack;
1378         struct iov_iter iter;
1379         struct task_struct *task;
1380         struct mm_struct *mm;
1381         size_t total_len;
1382         unsigned int f_flags;
1383
1384         if (flags != 0) {
1385                 ret = -EINVAL;
1386                 goto out;
1387         }
1388
1389         ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1390         if (ret < 0)
1391                 goto out;
1392
1393         task = pidfd_get_task(pidfd, &f_flags);
1394         if (IS_ERR(task)) {
1395                 ret = PTR_ERR(task);
1396                 goto free_iov;
1397         }
1398
1399         if (!process_madvise_behavior_valid(behavior)) {
1400                 ret = -EINVAL;
1401                 goto release_task;
1402         }
1403
1404         /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1405         mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1406         if (IS_ERR_OR_NULL(mm)) {
1407                 ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1408                 goto release_task;
1409         }
1410
1411         /*
1412          * Require CAP_SYS_NICE for influencing process performance. Note that
1413          * only non-destructive hints are currently supported.
1414          */
1415         if (!capable(CAP_SYS_NICE)) {
1416                 ret = -EPERM;
1417                 goto release_mm;
1418         }
1419
1420         total_len = iov_iter_count(&iter);
1421
1422         while (iov_iter_count(&iter)) {
1423                 iovec = iov_iter_iovec(&iter);
1424                 ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1425                                         iovec.iov_len, behavior);
1426                 if (ret < 0)
1427                         break;
1428                 iov_iter_advance(&iter, iovec.iov_len);
1429         }
1430
1431         if (ret == 0)
1432                 ret = total_len - iov_iter_count(&iter);
1433
1434 release_mm:
1435         mmput(mm);
1436 release_task:
1437         put_task_struct(task);
1438 free_iov:
1439         kfree(iov);
1440 out:
1441         return ret;
1442 }