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