1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 2002 Christoph Hellwig
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>
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>
39 struct madvise_walk_private {
40 struct mmu_gather *tlb;
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.
49 static int madvise_need_mmap_write(int behavior)
58 case MADV_POPULATE_READ:
59 case MADV_POPULATE_WRITE:
62 /* be safe, default to 1. list exceptions explicitly */
67 #ifdef CONFIG_ANON_VMA_NAME
68 struct anon_vma_name *anon_vma_name_alloc(const char *name)
70 struct anon_vma_name *anon_name;
73 /* Add 1 for NUL terminator at the end of the anon_name->name */
74 count = strlen(name) + 1;
75 anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL);
77 kref_init(&anon_name->kref);
78 memcpy(anon_name->name, name, count);
84 void anon_vma_name_free(struct kref *kref)
86 struct anon_vma_name *anon_name =
87 container_of(kref, struct anon_vma_name, kref);
91 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
93 mmap_assert_locked(vma->vm_mm);
98 return vma->anon_name;
101 /* mmap_lock should be write-locked */
102 static int replace_anon_vma_name(struct vm_area_struct *vma,
103 struct anon_vma_name *anon_name)
105 struct anon_vma_name *orig_name = anon_vma_name(vma);
108 vma->anon_name = NULL;
109 anon_vma_name_put(orig_name);
113 if (anon_vma_name_eq(orig_name, anon_name))
116 anon_vma_name_get(anon_name);
117 vma->anon_name = anon_name;
118 anon_vma_name_put(orig_name);
122 #else /* CONFIG_ANON_VMA_NAME */
123 static int replace_anon_vma_name(struct vm_area_struct *vma,
124 struct anon_vma_name *anon_name)
131 #endif /* CONFIG_ANON_VMA_NAME */
133 * Update the vm_flags on region of a vma, splitting it or merging it as
134 * necessary. Must be called with mmap_sem held for writing;
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)
141 struct mm_struct *mm = vma->vm_mm;
145 if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) {
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);
161 if (start != vma->vm_start) {
162 if (unlikely(mm->map_count >= sysctl_max_map_count))
164 error = __split_vma(mm, vma, start, 1);
169 if (end != vma->vm_end) {
170 if (unlikely(mm->map_count >= sysctl_max_map_count))
172 error = __split_vma(mm, vma, end, 0);
179 * vm_flags is protected by the mmap_lock held in write mode.
181 vma->vm_flags = new_flags;
183 error = replace_anon_vma_name(vma, anon_name);
192 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
193 unsigned long end, struct mm_walk *walk)
196 struct vm_area_struct *vma = walk->private;
199 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
202 for (index = start; index != end; index += PAGE_SIZE) {
208 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
209 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
210 pte_unmap_unlock(orig_pte, ptl);
212 if (pte_present(pte) || pte_none(pte))
214 entry = pte_to_swp_entry(pte);
215 if (unlikely(non_swap_entry(entry)))
218 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
227 static const struct mm_walk_ops swapin_walk_ops = {
228 .pmd_entry = swapin_walk_pmd_entry,
231 static void force_shm_swapin_readahead(struct vm_area_struct *vma,
232 unsigned long start, unsigned long end,
233 struct address_space *mapping)
235 XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
236 pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
240 xas_for_each(&xas, page, end_index) {
243 if (!xa_is_value(page))
248 swap = radix_to_swp_entry(page);
249 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
258 lru_add_drain(); /* Push any new pages onto the LRU now */
260 #endif /* CONFIG_SWAP */
263 * Schedule all required I/O operations. Do not wait for completion.
265 static long madvise_willneed(struct vm_area_struct *vma,
266 struct vm_area_struct **prev,
267 unsigned long start, unsigned long end)
269 struct mm_struct *mm = vma->vm_mm;
270 struct file *file = vma->vm_file;
276 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
277 lru_add_drain(); /* Push any new pages onto the LRU now */
281 if (shmem_mapping(file->f_mapping)) {
282 force_shm_swapin_readahead(vma, start, end,
291 if (IS_DAX(file_inode(file))) {
292 /* no bad return value, but ignore advice */
297 * Filesystem's fadvise may need to take various locks. We need to
298 * explicitly grab a reference because the vma (and hence the
299 * vma's reference to the file) can go away as soon as we drop
302 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
304 offset = (loff_t)(start - vma->vm_start)
305 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
306 mmap_read_unlock(mm);
307 vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
313 static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
314 unsigned long addr, unsigned long end,
315 struct mm_walk *walk)
317 struct madvise_walk_private *private = walk->private;
318 struct mmu_gather *tlb = private->tlb;
319 bool pageout = private->pageout;
320 struct mm_struct *mm = tlb->mm;
321 struct vm_area_struct *vma = walk->vma;
322 pte_t *orig_pte, *pte, ptent;
324 struct page *page = NULL;
325 LIST_HEAD(page_list);
327 if (fatal_signal_pending(current))
330 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
331 if (pmd_trans_huge(*pmd)) {
333 unsigned long next = pmd_addr_end(addr, end);
335 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
336 ptl = pmd_trans_huge_lock(pmd, vma);
341 if (is_huge_zero_pmd(orig_pmd))
344 if (unlikely(!pmd_present(orig_pmd))) {
345 VM_BUG_ON(thp_migration_supported() &&
346 !is_pmd_migration_entry(orig_pmd));
350 page = pmd_page(orig_pmd);
352 /* Do not interfere with other mappings of this page */
353 if (page_mapcount(page) != 1)
356 if (next - addr != HPAGE_PMD_SIZE) {
362 err = split_huge_page(page);
370 if (pmd_young(orig_pmd)) {
371 pmdp_invalidate(vma, addr, pmd);
372 orig_pmd = pmd_mkold(orig_pmd);
374 set_pmd_at(mm, addr, pmd, orig_pmd);
375 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
378 ClearPageReferenced(page);
379 test_and_clear_page_young(page);
381 if (!isolate_lru_page(page)) {
382 if (PageUnevictable(page))
383 putback_lru_page(page);
385 list_add(&page->lru, &page_list);
388 deactivate_page(page);
392 reclaim_pages(&page_list);
397 if (pmd_trans_unstable(pmd))
400 tlb_change_page_size(tlb, PAGE_SIZE);
401 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
402 flush_tlb_batched_pending(mm);
403 arch_enter_lazy_mmu_mode();
404 for (; addr < end; pte++, addr += PAGE_SIZE) {
410 if (!pte_present(ptent))
413 page = vm_normal_page(vma, addr, ptent);
418 * Creating a THP page is expensive so split it only if we
419 * are sure it's worth. Split it if we are only owner.
421 if (PageTransCompound(page)) {
422 if (page_mapcount(page) != 1)
425 if (!trylock_page(page)) {
429 pte_unmap_unlock(orig_pte, ptl);
430 if (split_huge_page(page)) {
433 pte_offset_map_lock(mm, pmd, addr, &ptl);
438 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
444 /* Do not interfere with other mappings of this page */
445 if (page_mapcount(page) != 1)
448 VM_BUG_ON_PAGE(PageTransCompound(page), page);
450 if (pte_young(ptent)) {
451 ptent = ptep_get_and_clear_full(mm, addr, pte,
453 ptent = pte_mkold(ptent);
454 set_pte_at(mm, addr, pte, ptent);
455 tlb_remove_tlb_entry(tlb, pte, addr);
459 * We are deactivating a page for accelerating reclaiming.
460 * VM couldn't reclaim the page unless we clear PG_young.
461 * As a side effect, it makes confuse idle-page tracking
462 * because they will miss recent referenced history.
464 ClearPageReferenced(page);
465 test_and_clear_page_young(page);
467 if (!isolate_lru_page(page)) {
468 if (PageUnevictable(page))
469 putback_lru_page(page);
471 list_add(&page->lru, &page_list);
474 deactivate_page(page);
477 arch_leave_lazy_mmu_mode();
478 pte_unmap_unlock(orig_pte, ptl);
480 reclaim_pages(&page_list);
486 static const struct mm_walk_ops cold_walk_ops = {
487 .pmd_entry = madvise_cold_or_pageout_pte_range,
490 static void madvise_cold_page_range(struct mmu_gather *tlb,
491 struct vm_area_struct *vma,
492 unsigned long addr, unsigned long end)
494 struct madvise_walk_private walk_private = {
499 tlb_start_vma(tlb, vma);
500 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
501 tlb_end_vma(tlb, vma);
504 static long madvise_cold(struct vm_area_struct *vma,
505 struct vm_area_struct **prev,
506 unsigned long start_addr, unsigned long end_addr)
508 struct mm_struct *mm = vma->vm_mm;
509 struct mmu_gather tlb;
512 if (!can_madv_lru_vma(vma))
516 tlb_gather_mmu(&tlb, mm);
517 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
518 tlb_finish_mmu(&tlb);
523 static void madvise_pageout_page_range(struct mmu_gather *tlb,
524 struct vm_area_struct *vma,
525 unsigned long addr, unsigned long end)
527 struct madvise_walk_private walk_private = {
532 tlb_start_vma(tlb, vma);
533 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
534 tlb_end_vma(tlb, vma);
537 static inline bool can_do_pageout(struct vm_area_struct *vma)
539 if (vma_is_anonymous(vma))
544 * paging out pagecache only for non-anonymous mappings that correspond
545 * to the files the calling process could (if tried) open for writing;
546 * otherwise we'd be including shared non-exclusive mappings, which
547 * opens a side channel.
549 return inode_owner_or_capable(&init_user_ns,
550 file_inode(vma->vm_file)) ||
551 file_permission(vma->vm_file, MAY_WRITE) == 0;
554 static long madvise_pageout(struct vm_area_struct *vma,
555 struct vm_area_struct **prev,
556 unsigned long start_addr, unsigned long end_addr)
558 struct mm_struct *mm = vma->vm_mm;
559 struct mmu_gather tlb;
562 if (!can_madv_lru_vma(vma))
565 if (!can_do_pageout(vma))
569 tlb_gather_mmu(&tlb, mm);
570 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
571 tlb_finish_mmu(&tlb);
576 static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
577 unsigned long end, struct mm_walk *walk)
580 struct mmu_gather *tlb = walk->private;
581 struct mm_struct *mm = tlb->mm;
582 struct vm_area_struct *vma = walk->vma;
584 pte_t *orig_pte, *pte, ptent;
589 next = pmd_addr_end(addr, end);
590 if (pmd_trans_huge(*pmd))
591 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
594 if (pmd_trans_unstable(pmd))
597 tlb_change_page_size(tlb, PAGE_SIZE);
598 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
599 flush_tlb_batched_pending(mm);
600 arch_enter_lazy_mmu_mode();
601 for (; addr != end; pte++, addr += PAGE_SIZE) {
607 * If the pte has swp_entry, just clear page table to
608 * prevent swap-in which is more expensive rather than
609 * (page allocation + zeroing).
611 if (!pte_present(ptent)) {
614 entry = pte_to_swp_entry(ptent);
615 if (non_swap_entry(entry))
618 free_swap_and_cache(entry);
619 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
623 page = vm_normal_page(vma, addr, ptent);
628 * If pmd isn't transhuge but the page is THP and
629 * is owned by only this process, split it and
630 * deactivate all pages.
632 if (PageTransCompound(page)) {
633 if (page_mapcount(page) != 1)
636 if (!trylock_page(page)) {
640 pte_unmap_unlock(orig_pte, ptl);
641 if (split_huge_page(page)) {
644 pte_offset_map_lock(mm, pmd, addr, &ptl);
649 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
655 VM_BUG_ON_PAGE(PageTransCompound(page), page);
657 if (PageSwapCache(page) || PageDirty(page)) {
658 if (!trylock_page(page))
661 * If page is shared with others, we couldn't clear
662 * PG_dirty of the page.
664 if (page_mapcount(page) != 1) {
669 if (PageSwapCache(page) && !try_to_free_swap(page)) {
674 ClearPageDirty(page);
678 if (pte_young(ptent) || pte_dirty(ptent)) {
680 * Some of architecture(ex, PPC) don't update TLB
681 * with set_pte_at and tlb_remove_tlb_entry so for
682 * the portability, remap the pte with old|clean
683 * after pte clearing.
685 ptent = ptep_get_and_clear_full(mm, addr, pte,
688 ptent = pte_mkold(ptent);
689 ptent = pte_mkclean(ptent);
690 set_pte_at(mm, addr, pte, ptent);
691 tlb_remove_tlb_entry(tlb, pte, addr);
693 mark_page_lazyfree(page);
697 if (current->mm == mm)
700 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
702 arch_leave_lazy_mmu_mode();
703 pte_unmap_unlock(orig_pte, ptl);
709 static const struct mm_walk_ops madvise_free_walk_ops = {
710 .pmd_entry = madvise_free_pte_range,
713 static int madvise_free_single_vma(struct vm_area_struct *vma,
714 unsigned long start_addr, unsigned long end_addr)
716 struct mm_struct *mm = vma->vm_mm;
717 struct mmu_notifier_range range;
718 struct mmu_gather tlb;
720 /* MADV_FREE works for only anon vma at the moment */
721 if (!vma_is_anonymous(vma))
724 range.start = max(vma->vm_start, start_addr);
725 if (range.start >= vma->vm_end)
727 range.end = min(vma->vm_end, end_addr);
728 if (range.end <= vma->vm_start)
730 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
731 range.start, range.end);
734 tlb_gather_mmu(&tlb, mm);
735 update_hiwater_rss(mm);
737 mmu_notifier_invalidate_range_start(&range);
738 tlb_start_vma(&tlb, vma);
739 walk_page_range(vma->vm_mm, range.start, range.end,
740 &madvise_free_walk_ops, &tlb);
741 tlb_end_vma(&tlb, vma);
742 mmu_notifier_invalidate_range_end(&range);
743 tlb_finish_mmu(&tlb);
749 * Application no longer needs these pages. If the pages are dirty,
750 * it's OK to just throw them away. The app will be more careful about
751 * data it wants to keep. Be sure to free swap resources too. The
752 * zap_page_range call sets things up for shrink_active_list to actually free
753 * these pages later if no one else has touched them in the meantime,
754 * although we could add these pages to a global reuse list for
755 * shrink_active_list to pick up before reclaiming other pages.
757 * NB: This interface discards data rather than pushes it out to swap,
758 * as some implementations do. This has performance implications for
759 * applications like large transactional databases which want to discard
760 * pages in anonymous maps after committing to backing store the data
761 * that was kept in them. There is no reason to write this data out to
762 * the swap area if the application is discarding it.
764 * An interface that causes the system to free clean pages and flush
765 * dirty pages is already available as msync(MS_INVALIDATE).
767 static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
768 unsigned long start, unsigned long end)
770 zap_page_range(vma, start, end - start);
774 static long madvise_dontneed_free(struct vm_area_struct *vma,
775 struct vm_area_struct **prev,
776 unsigned long start, unsigned long end,
779 struct mm_struct *mm = vma->vm_mm;
782 if (!can_madv_lru_vma(vma))
785 if (!userfaultfd_remove(vma, start, end)) {
786 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
789 vma = find_vma(mm, start);
792 if (start < vma->vm_start) {
794 * This "vma" under revalidation is the one
795 * with the lowest vma->vm_start where start
796 * is also < vma->vm_end. If start <
797 * vma->vm_start it means an hole materialized
798 * in the user address space within the
799 * virtual range passed to MADV_DONTNEED
804 if (!can_madv_lru_vma(vma))
806 if (end > vma->vm_end) {
808 * Don't fail if end > vma->vm_end. If the old
809 * vma was split while the mmap_lock was
810 * released the effect of the concurrent
811 * operation may not cause madvise() to
812 * have an undefined result. There may be an
813 * adjacent next vma that we'll walk
814 * next. userfaultfd_remove() will generate an
815 * UFFD_EVENT_REMOVE repetition on the
816 * end-vma->vm_end range, but the manager can
817 * handle a repetition fine.
821 VM_WARN_ON(start >= end);
824 if (behavior == MADV_DONTNEED)
825 return madvise_dontneed_single_vma(vma, start, end);
826 else if (behavior == MADV_FREE)
827 return madvise_free_single_vma(vma, start, end);
832 static long madvise_populate(struct vm_area_struct *vma,
833 struct vm_area_struct **prev,
834 unsigned long start, unsigned long end,
837 const bool write = behavior == MADV_POPULATE_WRITE;
838 struct mm_struct *mm = vma->vm_mm;
839 unsigned long tmp_end;
845 while (start < end) {
847 * We might have temporarily dropped the lock. For example,
848 * our VMA might have been split.
850 if (!vma || start >= vma->vm_end) {
851 vma = find_vma(mm, start);
852 if (!vma || start < vma->vm_start)
856 tmp_end = min_t(unsigned long, end, vma->vm_end);
857 /* Populate (prefault) page tables readable/writable. */
858 pages = faultin_vma_page_range(vma, start, tmp_end, write,
870 case -EINVAL: /* Incompatible mappings / permissions. */
874 case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
877 pr_warn_once("%s: unhandled return value: %ld\n",
884 start += pages * PAGE_SIZE;
890 * Application wants to free up the pages and associated backing store.
891 * This is effectively punching a hole into the middle of a file.
893 static long madvise_remove(struct vm_area_struct *vma,
894 struct vm_area_struct **prev,
895 unsigned long start, unsigned long end)
900 struct mm_struct *mm = vma->vm_mm;
902 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
904 if (vma->vm_flags & VM_LOCKED)
909 if (!f || !f->f_mapping || !f->f_mapping->host) {
913 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
916 offset = (loff_t)(start - vma->vm_start)
917 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
920 * Filesystem's fallocate may need to take i_rwsem. We need to
921 * explicitly grab a reference because the vma (and hence the
922 * vma's reference to the file) can go away as soon as we drop
926 if (userfaultfd_remove(vma, start, end)) {
927 /* mmap_lock was not released by userfaultfd_remove() */
928 mmap_read_unlock(mm);
930 error = vfs_fallocate(f,
931 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
932 offset, end - start);
939 * Apply an madvise behavior to a region of a vma. madvise_update_vma
940 * will handle splitting a vm area into separate areas, each area with its own
943 static int madvise_vma_behavior(struct vm_area_struct *vma,
944 struct vm_area_struct **prev,
945 unsigned long start, unsigned long end,
946 unsigned long behavior)
949 unsigned long new_flags = vma->vm_flags;
953 return madvise_remove(vma, prev, start, end);
955 return madvise_willneed(vma, prev, start, end);
957 return madvise_cold(vma, prev, start, end);
959 return madvise_pageout(vma, prev, start, end);
962 return madvise_dontneed_free(vma, prev, start, end, behavior);
963 case MADV_POPULATE_READ:
964 case MADV_POPULATE_WRITE:
965 return madvise_populate(vma, prev, start, end, behavior);
967 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
969 case MADV_SEQUENTIAL:
970 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
973 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
976 new_flags |= VM_DONTCOPY;
979 if (vma->vm_flags & VM_IO)
981 new_flags &= ~VM_DONTCOPY;
983 case MADV_WIPEONFORK:
984 /* MADV_WIPEONFORK is only supported on anonymous memory. */
985 if (vma->vm_file || vma->vm_flags & VM_SHARED)
987 new_flags |= VM_WIPEONFORK;
989 case MADV_KEEPONFORK:
990 new_flags &= ~VM_WIPEONFORK;
993 new_flags |= VM_DONTDUMP;
996 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL)
998 new_flags &= ~VM_DONTDUMP;
1000 case MADV_MERGEABLE:
1001 case MADV_UNMERGEABLE:
1002 error = ksm_madvise(vma, start, end, behavior, &new_flags);
1007 case MADV_NOHUGEPAGE:
1008 error = hugepage_madvise(vma, &new_flags, behavior);
1014 error = madvise_update_vma(vma, prev, start, end, new_flags,
1015 anon_vma_name(vma));
1019 * madvise() returns EAGAIN if kernel resources, such as
1020 * slab, are temporarily unavailable.
1022 if (error == -ENOMEM)
1027 #ifdef CONFIG_MEMORY_FAILURE
1029 * Error injection support for memory error handling.
1031 static int madvise_inject_error(int behavior,
1032 unsigned long start, unsigned long end)
1036 if (!capable(CAP_SYS_ADMIN))
1040 for (; start < end; start += size) {
1045 ret = get_user_pages_fast(start, 1, 0, &page);
1048 pfn = page_to_pfn(page);
1051 * When soft offlining hugepages, after migrating the page
1052 * we dissolve it, therefore in the second loop "page" will
1053 * no longer be a compound page.
1055 size = page_size(compound_head(page));
1057 if (behavior == MADV_SOFT_OFFLINE) {
1058 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
1060 ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
1062 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
1064 ret = memory_failure(pfn, MF_COUNT_INCREASED);
1076 madvise_behavior_valid(int behavior)
1082 case MADV_SEQUENTIAL:
1090 case MADV_POPULATE_READ:
1091 case MADV_POPULATE_WRITE:
1093 case MADV_MERGEABLE:
1094 case MADV_UNMERGEABLE:
1096 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1098 case MADV_NOHUGEPAGE:
1102 case MADV_WIPEONFORK:
1103 case MADV_KEEPONFORK:
1104 #ifdef CONFIG_MEMORY_FAILURE
1105 case MADV_SOFT_OFFLINE:
1116 process_madvise_behavior_valid(int behavior)
1129 * Walk the vmas in range [start,end), and call the visit function on each one.
1130 * The visit function will get start and end parameters that cover the overlap
1131 * between the current vma and the original range. Any unmapped regions in the
1132 * original range will result in this function returning -ENOMEM while still
1133 * calling the visit function on all of the existing vmas in the range.
1134 * Must be called with the mmap_lock held for reading or writing.
1137 int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
1138 unsigned long end, unsigned long arg,
1139 int (*visit)(struct vm_area_struct *vma,
1140 struct vm_area_struct **prev, unsigned long start,
1141 unsigned long end, unsigned long arg))
1143 struct vm_area_struct *vma;
1144 struct vm_area_struct *prev;
1146 int unmapped_error = 0;
1149 * If the interval [start,end) covers some unmapped address
1150 * ranges, just ignore them, but return -ENOMEM at the end.
1151 * - different from the way of handling in mlock etc.
1153 vma = find_vma_prev(mm, start, &prev);
1154 if (vma && start > vma->vm_start)
1160 /* Still start < end. */
1164 /* Here start < (end|vma->vm_end). */
1165 if (start < vma->vm_start) {
1166 unmapped_error = -ENOMEM;
1167 start = vma->vm_start;
1172 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1177 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1178 error = visit(vma, &prev, start, tmp, arg);
1182 if (prev && start < prev->vm_end)
1183 start = prev->vm_end;
1187 vma = prev->vm_next;
1188 else /* madvise_remove dropped mmap_lock */
1189 vma = find_vma(mm, start);
1192 return unmapped_error;
1195 #ifdef CONFIG_ANON_VMA_NAME
1196 static int madvise_vma_anon_name(struct vm_area_struct *vma,
1197 struct vm_area_struct **prev,
1198 unsigned long start, unsigned long end,
1199 unsigned long anon_name)
1203 /* Only anonymous mappings can be named */
1207 error = madvise_update_vma(vma, prev, start, end, vma->vm_flags,
1208 (struct anon_vma_name *)anon_name);
1211 * madvise() returns EAGAIN if kernel resources, such as
1212 * slab, are temporarily unavailable.
1214 if (error == -ENOMEM)
1219 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
1220 unsigned long len_in, struct anon_vma_name *anon_name)
1225 if (start & ~PAGE_MASK)
1227 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1229 /* Check to see whether len was rounded up from small -ve to zero */
1240 return madvise_walk_vmas(mm, start, end, (unsigned long)anon_name,
1241 madvise_vma_anon_name);
1243 #endif /* CONFIG_ANON_VMA_NAME */
1245 * The madvise(2) system call.
1247 * Applications can use madvise() to advise the kernel how it should
1248 * handle paging I/O in this VM area. The idea is to help the kernel
1249 * use appropriate read-ahead and caching techniques. The information
1250 * provided is advisory only, and can be safely disregarded by the
1251 * kernel without affecting the correct operation of the application.
1254 * MADV_NORMAL - the default behavior is to read clusters. This
1255 * results in some read-ahead and read-behind.
1256 * MADV_RANDOM - the system should read the minimum amount of data
1257 * on any access, since it is unlikely that the appli-
1258 * cation will need more than what it asks for.
1259 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1260 * once, so they can be aggressively read ahead, and
1261 * can be freed soon after they are accessed.
1262 * MADV_WILLNEED - the application is notifying the system to read
1264 * MADV_DONTNEED - the application is finished with the given range,
1265 * so the kernel can free resources associated with it.
1266 * MADV_FREE - the application marks pages in the given range as lazy free,
1267 * where actual purges are postponed until memory pressure happens.
1268 * MADV_REMOVE - the application wants to free up the given range of
1269 * pages and associated backing store.
1270 * MADV_DONTFORK - omit this area from child's address space when forking:
1271 * typically, to avoid COWing pages pinned by get_user_pages().
1272 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
1273 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1274 * range after a fork.
1275 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
1276 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1277 * were corrupted by unrecoverable hardware memory failure.
1278 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
1279 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1280 * this area with pages of identical content from other such areas.
1281 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
1282 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1283 * huge pages in the future. Existing pages might be coalesced and
1284 * new pages might be allocated as THP.
1285 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1286 * transparent huge pages so the existing pages will not be
1287 * coalesced into THP and new pages will not be allocated as THP.
1288 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1289 * from being included in its core dump.
1290 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1291 * MADV_COLD - the application is not expected to use this memory soon,
1292 * deactivate pages in this range so that they can be reclaimed
1293 * easily if memory pressure happens.
1294 * MADV_PAGEOUT - the application is not expected to use this memory soon,
1295 * page out the pages in this range immediately.
1296 * MADV_POPULATE_READ - populate (prefault) page tables readable by
1297 * triggering read faults if required
1298 * MADV_POPULATE_WRITE - populate (prefault) page tables writable by
1299 * triggering write faults if required
1303 * -EINVAL - start + len < 0, start is not page-aligned,
1304 * "behavior" is not a valid value, or application
1305 * is attempting to release locked or shared pages,
1306 * or the specified address range includes file, Huge TLB,
1307 * MAP_SHARED or VMPFNMAP range.
1308 * -ENOMEM - addresses in the specified range are not currently
1309 * mapped, or are outside the AS of the process.
1310 * -EIO - an I/O error occurred while paging in data.
1311 * -EBADF - map exists, but area maps something that isn't a file.
1312 * -EAGAIN - a kernel resource was temporarily unavailable.
1314 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
1320 struct blk_plug plug;
1322 start = untagged_addr(start);
1324 if (!madvise_behavior_valid(behavior))
1327 if (!PAGE_ALIGNED(start))
1329 len = PAGE_ALIGN(len_in);
1331 /* Check to see whether len was rounded up from small -ve to zero */
1342 #ifdef CONFIG_MEMORY_FAILURE
1343 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1344 return madvise_inject_error(behavior, start, start + len_in);
1347 write = madvise_need_mmap_write(behavior);
1349 if (mmap_write_lock_killable(mm))
1355 blk_start_plug(&plug);
1356 error = madvise_walk_vmas(mm, start, end, behavior,
1357 madvise_vma_behavior);
1358 blk_finish_plug(&plug);
1360 mmap_write_unlock(mm);
1362 mmap_read_unlock(mm);
1367 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1369 return do_madvise(current->mm, start, len_in, behavior);
1372 SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1373 size_t, vlen, int, behavior, unsigned int, flags)
1376 struct iovec iovstack[UIO_FASTIOV], iovec;
1377 struct iovec *iov = iovstack;
1378 struct iov_iter iter;
1379 struct task_struct *task;
1380 struct mm_struct *mm;
1382 unsigned int f_flags;
1389 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1393 task = pidfd_get_task(pidfd, &f_flags);
1395 ret = PTR_ERR(task);
1399 if (!process_madvise_behavior_valid(behavior)) {
1404 /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1405 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1406 if (IS_ERR_OR_NULL(mm)) {
1407 ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1412 * Require CAP_SYS_NICE for influencing process performance. Note that
1413 * only non-destructive hints are currently supported.
1415 if (!capable(CAP_SYS_NICE)) {
1420 total_len = iov_iter_count(&iter);
1422 while (iov_iter_count(&iter)) {
1423 iovec = iov_iter_iovec(&iter);
1424 ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1425 iovec.iov_len, behavior);
1428 iov_iter_advance(&iter, iovec.iov_len);
1432 ret = total_len - iov_iter_count(&iter);
1437 put_task_struct(task);