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