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