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 vma->anon_name = anon_vma_name_reuse(anon_name);
117 anon_vma_name_put(orig_name);
121 #else /* CONFIG_ANON_VMA_NAME */
122 static int replace_anon_vma_name(struct vm_area_struct *vma,
123 struct anon_vma_name *anon_name)
130 #endif /* CONFIG_ANON_VMA_NAME */
132 * Update the vm_flags on region of a vma, splitting it or merging it as
133 * necessary. Must be called with mmap_sem held for writing;
134 * Caller should ensure anon_name stability by raising its refcount even when
135 * anon_name belongs to a valid vma because this function might free that vma.
137 static int madvise_update_vma(struct vm_area_struct *vma,
138 struct vm_area_struct **prev, unsigned long start,
139 unsigned long end, unsigned long new_flags,
140 struct anon_vma_name *anon_name)
142 struct mm_struct *mm = vma->vm_mm;
146 if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) {
151 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
152 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
153 vma->vm_file, pgoff, vma_policy(vma),
154 vma->vm_userfaultfd_ctx, anon_name);
162 if (start != vma->vm_start) {
163 if (unlikely(mm->map_count >= sysctl_max_map_count))
165 error = __split_vma(mm, vma, start, 1);
170 if (end != vma->vm_end) {
171 if (unlikely(mm->map_count >= sysctl_max_map_count))
173 error = __split_vma(mm, vma, end, 0);
180 * vm_flags is protected by the mmap_lock held in write mode.
182 vma->vm_flags = new_flags;
184 error = replace_anon_vma_name(vma, anon_name);
193 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
194 unsigned long end, struct mm_walk *walk)
197 struct vm_area_struct *vma = walk->private;
200 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
203 for (index = start; index != end; index += PAGE_SIZE) {
209 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
210 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
211 pte_unmap_unlock(orig_pte, ptl);
213 if (pte_present(pte) || pte_none(pte))
215 entry = pte_to_swp_entry(pte);
216 if (unlikely(non_swap_entry(entry)))
219 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
228 static const struct mm_walk_ops swapin_walk_ops = {
229 .pmd_entry = swapin_walk_pmd_entry,
232 static void force_shm_swapin_readahead(struct vm_area_struct *vma,
233 unsigned long start, unsigned long end,
234 struct address_space *mapping)
236 XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
237 pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
241 xas_for_each(&xas, page, end_index) {
244 if (!xa_is_value(page))
249 swap = radix_to_swp_entry(page);
250 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
259 lru_add_drain(); /* Push any new pages onto the LRU now */
261 #endif /* CONFIG_SWAP */
264 * Schedule all required I/O operations. Do not wait for completion.
266 static long madvise_willneed(struct vm_area_struct *vma,
267 struct vm_area_struct **prev,
268 unsigned long start, unsigned long end)
270 struct mm_struct *mm = vma->vm_mm;
271 struct file *file = vma->vm_file;
277 walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
278 lru_add_drain(); /* Push any new pages onto the LRU now */
282 if (shmem_mapping(file->f_mapping)) {
283 force_shm_swapin_readahead(vma, start, end,
292 if (IS_DAX(file_inode(file))) {
293 /* no bad return value, but ignore advice */
298 * Filesystem's fadvise may need to take various locks. We need to
299 * explicitly grab a reference because the vma (and hence the
300 * vma's reference to the file) can go away as soon as we drop
303 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
305 offset = (loff_t)(start - vma->vm_start)
306 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
307 mmap_read_unlock(mm);
308 vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
314 static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
315 unsigned long addr, unsigned long end,
316 struct mm_walk *walk)
318 struct madvise_walk_private *private = walk->private;
319 struct mmu_gather *tlb = private->tlb;
320 bool pageout = private->pageout;
321 struct mm_struct *mm = tlb->mm;
322 struct vm_area_struct *vma = walk->vma;
323 pte_t *orig_pte, *pte, ptent;
325 struct page *page = NULL;
326 LIST_HEAD(page_list);
328 if (fatal_signal_pending(current))
331 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
332 if (pmd_trans_huge(*pmd)) {
334 unsigned long next = pmd_addr_end(addr, end);
336 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
337 ptl = pmd_trans_huge_lock(pmd, vma);
342 if (is_huge_zero_pmd(orig_pmd))
345 if (unlikely(!pmd_present(orig_pmd))) {
346 VM_BUG_ON(thp_migration_supported() &&
347 !is_pmd_migration_entry(orig_pmd));
351 page = pmd_page(orig_pmd);
353 /* Do not interfere with other mappings of this page */
354 if (page_mapcount(page) != 1)
357 if (next - addr != HPAGE_PMD_SIZE) {
363 err = split_huge_page(page);
371 if (pmd_young(orig_pmd)) {
372 pmdp_invalidate(vma, addr, pmd);
373 orig_pmd = pmd_mkold(orig_pmd);
375 set_pmd_at(mm, addr, pmd, orig_pmd);
376 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
379 ClearPageReferenced(page);
380 test_and_clear_page_young(page);
382 if (!isolate_lru_page(page)) {
383 if (PageUnevictable(page))
384 putback_lru_page(page);
386 list_add(&page->lru, &page_list);
389 deactivate_page(page);
393 reclaim_pages(&page_list);
398 if (pmd_trans_unstable(pmd))
401 tlb_change_page_size(tlb, PAGE_SIZE);
402 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
403 flush_tlb_batched_pending(mm);
404 arch_enter_lazy_mmu_mode();
405 for (; addr < end; pte++, addr += PAGE_SIZE) {
411 if (!pte_present(ptent))
414 page = vm_normal_page(vma, addr, ptent);
419 * Creating a THP page is expensive so split it only if we
420 * are sure it's worth. Split it if we are only owner.
422 if (PageTransCompound(page)) {
423 if (page_mapcount(page) != 1)
426 if (!trylock_page(page)) {
430 pte_unmap_unlock(orig_pte, ptl);
431 if (split_huge_page(page)) {
434 pte_offset_map_lock(mm, pmd, addr, &ptl);
439 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
445 /* Do not interfere with other mappings of this page */
446 if (page_mapcount(page) != 1)
449 VM_BUG_ON_PAGE(PageTransCompound(page), page);
451 if (pte_young(ptent)) {
452 ptent = ptep_get_and_clear_full(mm, addr, pte,
454 ptent = pte_mkold(ptent);
455 set_pte_at(mm, addr, pte, ptent);
456 tlb_remove_tlb_entry(tlb, pte, addr);
460 * We are deactivating a page for accelerating reclaiming.
461 * VM couldn't reclaim the page unless we clear PG_young.
462 * As a side effect, it makes confuse idle-page tracking
463 * because they will miss recent referenced history.
465 ClearPageReferenced(page);
466 test_and_clear_page_young(page);
468 if (!isolate_lru_page(page)) {
469 if (PageUnevictable(page))
470 putback_lru_page(page);
472 list_add(&page->lru, &page_list);
475 deactivate_page(page);
478 arch_leave_lazy_mmu_mode();
479 pte_unmap_unlock(orig_pte, ptl);
481 reclaim_pages(&page_list);
487 static const struct mm_walk_ops cold_walk_ops = {
488 .pmd_entry = madvise_cold_or_pageout_pte_range,
491 static void madvise_cold_page_range(struct mmu_gather *tlb,
492 struct vm_area_struct *vma,
493 unsigned long addr, unsigned long end)
495 struct madvise_walk_private walk_private = {
500 tlb_start_vma(tlb, vma);
501 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
502 tlb_end_vma(tlb, vma);
505 static inline bool can_madv_lru_vma(struct vm_area_struct *vma)
507 return !(vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP));
510 static long madvise_cold(struct vm_area_struct *vma,
511 struct vm_area_struct **prev,
512 unsigned long start_addr, unsigned long end_addr)
514 struct mm_struct *mm = vma->vm_mm;
515 struct mmu_gather tlb;
518 if (!can_madv_lru_vma(vma))
522 tlb_gather_mmu(&tlb, mm);
523 madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
524 tlb_finish_mmu(&tlb);
529 static void madvise_pageout_page_range(struct mmu_gather *tlb,
530 struct vm_area_struct *vma,
531 unsigned long addr, unsigned long end)
533 struct madvise_walk_private walk_private = {
538 tlb_start_vma(tlb, vma);
539 walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
540 tlb_end_vma(tlb, vma);
543 static inline bool can_do_pageout(struct vm_area_struct *vma)
545 if (vma_is_anonymous(vma))
550 * paging out pagecache only for non-anonymous mappings that correspond
551 * to the files the calling process could (if tried) open for writing;
552 * otherwise we'd be including shared non-exclusive mappings, which
553 * opens a side channel.
555 return inode_owner_or_capable(&init_user_ns,
556 file_inode(vma->vm_file)) ||
557 file_permission(vma->vm_file, MAY_WRITE) == 0;
560 static long madvise_pageout(struct vm_area_struct *vma,
561 struct vm_area_struct **prev,
562 unsigned long start_addr, unsigned long end_addr)
564 struct mm_struct *mm = vma->vm_mm;
565 struct mmu_gather tlb;
568 if (!can_madv_lru_vma(vma))
571 if (!can_do_pageout(vma))
575 tlb_gather_mmu(&tlb, mm);
576 madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
577 tlb_finish_mmu(&tlb);
582 static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
583 unsigned long end, struct mm_walk *walk)
586 struct mmu_gather *tlb = walk->private;
587 struct mm_struct *mm = tlb->mm;
588 struct vm_area_struct *vma = walk->vma;
590 pte_t *orig_pte, *pte, ptent;
595 next = pmd_addr_end(addr, end);
596 if (pmd_trans_huge(*pmd))
597 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
600 if (pmd_trans_unstable(pmd))
603 tlb_change_page_size(tlb, PAGE_SIZE);
604 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
605 flush_tlb_batched_pending(mm);
606 arch_enter_lazy_mmu_mode();
607 for (; addr != end; pte++, addr += PAGE_SIZE) {
613 * If the pte has swp_entry, just clear page table to
614 * prevent swap-in which is more expensive rather than
615 * (page allocation + zeroing).
617 if (!pte_present(ptent)) {
620 entry = pte_to_swp_entry(ptent);
621 if (non_swap_entry(entry))
624 free_swap_and_cache(entry);
625 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
629 page = vm_normal_page(vma, addr, ptent);
634 * If pmd isn't transhuge but the page is THP and
635 * is owned by only this process, split it and
636 * deactivate all pages.
638 if (PageTransCompound(page)) {
639 if (page_mapcount(page) != 1)
642 if (!trylock_page(page)) {
646 pte_unmap_unlock(orig_pte, ptl);
647 if (split_huge_page(page)) {
650 pte_offset_map_lock(mm, pmd, addr, &ptl);
655 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
661 VM_BUG_ON_PAGE(PageTransCompound(page), page);
663 if (PageSwapCache(page) || PageDirty(page)) {
664 if (!trylock_page(page))
667 * If page is shared with others, we couldn't clear
668 * PG_dirty of the page.
670 if (page_mapcount(page) != 1) {
675 if (PageSwapCache(page) && !try_to_free_swap(page)) {
680 ClearPageDirty(page);
684 if (pte_young(ptent) || pte_dirty(ptent)) {
686 * Some of architecture(ex, PPC) don't update TLB
687 * with set_pte_at and tlb_remove_tlb_entry so for
688 * the portability, remap the pte with old|clean
689 * after pte clearing.
691 ptent = ptep_get_and_clear_full(mm, addr, pte,
694 ptent = pte_mkold(ptent);
695 ptent = pte_mkclean(ptent);
696 set_pte_at(mm, addr, pte, ptent);
697 tlb_remove_tlb_entry(tlb, pte, addr);
699 mark_page_lazyfree(page);
703 if (current->mm == mm)
706 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
708 arch_leave_lazy_mmu_mode();
709 pte_unmap_unlock(orig_pte, ptl);
715 static const struct mm_walk_ops madvise_free_walk_ops = {
716 .pmd_entry = madvise_free_pte_range,
719 static int madvise_free_single_vma(struct vm_area_struct *vma,
720 unsigned long start_addr, unsigned long end_addr)
722 struct mm_struct *mm = vma->vm_mm;
723 struct mmu_notifier_range range;
724 struct mmu_gather tlb;
726 /* MADV_FREE works for only anon vma at the moment */
727 if (!vma_is_anonymous(vma))
730 range.start = max(vma->vm_start, start_addr);
731 if (range.start >= vma->vm_end)
733 range.end = min(vma->vm_end, end_addr);
734 if (range.end <= vma->vm_start)
736 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
737 range.start, range.end);
740 tlb_gather_mmu(&tlb, mm);
741 update_hiwater_rss(mm);
743 mmu_notifier_invalidate_range_start(&range);
744 tlb_start_vma(&tlb, vma);
745 walk_page_range(vma->vm_mm, range.start, range.end,
746 &madvise_free_walk_ops, &tlb);
747 tlb_end_vma(&tlb, vma);
748 mmu_notifier_invalidate_range_end(&range);
749 tlb_finish_mmu(&tlb);
755 * Application no longer needs these pages. If the pages are dirty,
756 * it's OK to just throw them away. The app will be more careful about
757 * data it wants to keep. Be sure to free swap resources too. The
758 * zap_page_range call sets things up for shrink_active_list to actually free
759 * these pages later if no one else has touched them in the meantime,
760 * although we could add these pages to a global reuse list for
761 * shrink_active_list to pick up before reclaiming other pages.
763 * NB: This interface discards data rather than pushes it out to swap,
764 * as some implementations do. This has performance implications for
765 * applications like large transactional databases which want to discard
766 * pages in anonymous maps after committing to backing store the data
767 * that was kept in them. There is no reason to write this data out to
768 * the swap area if the application is discarding it.
770 * An interface that causes the system to free clean pages and flush
771 * dirty pages is already available as msync(MS_INVALIDATE).
773 static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
774 unsigned long start, unsigned long end)
776 zap_page_range(vma, start, end - start);
780 static long madvise_dontneed_free(struct vm_area_struct *vma,
781 struct vm_area_struct **prev,
782 unsigned long start, unsigned long end,
785 struct mm_struct *mm = vma->vm_mm;
788 if (!can_madv_lru_vma(vma))
791 if (!userfaultfd_remove(vma, start, end)) {
792 *prev = NULL; /* mmap_lock has been dropped, prev is stale */
795 vma = find_vma(mm, start);
798 if (start < vma->vm_start) {
800 * This "vma" under revalidation is the one
801 * with the lowest vma->vm_start where start
802 * is also < vma->vm_end. If start <
803 * vma->vm_start it means an hole materialized
804 * in the user address space within the
805 * virtual range passed to MADV_DONTNEED
810 if (!can_madv_lru_vma(vma))
812 if (end > vma->vm_end) {
814 * Don't fail if end > vma->vm_end. If the old
815 * vma was split while the mmap_lock was
816 * released the effect of the concurrent
817 * operation may not cause madvise() to
818 * have an undefined result. There may be an
819 * adjacent next vma that we'll walk
820 * next. userfaultfd_remove() will generate an
821 * UFFD_EVENT_REMOVE repetition on the
822 * end-vma->vm_end range, but the manager can
823 * handle a repetition fine.
827 VM_WARN_ON(start >= end);
830 if (behavior == MADV_DONTNEED)
831 return madvise_dontneed_single_vma(vma, start, end);
832 else if (behavior == MADV_FREE)
833 return madvise_free_single_vma(vma, start, end);
838 static long madvise_populate(struct vm_area_struct *vma,
839 struct vm_area_struct **prev,
840 unsigned long start, unsigned long end,
843 const bool write = behavior == MADV_POPULATE_WRITE;
844 struct mm_struct *mm = vma->vm_mm;
845 unsigned long tmp_end;
851 while (start < end) {
853 * We might have temporarily dropped the lock. For example,
854 * our VMA might have been split.
856 if (!vma || start >= vma->vm_end) {
857 vma = vma_lookup(mm, start);
862 tmp_end = min_t(unsigned long, end, vma->vm_end);
863 /* Populate (prefault) page tables readable/writable. */
864 pages = faultin_vma_page_range(vma, start, tmp_end, write,
876 case -EINVAL: /* Incompatible mappings / permissions. */
880 case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
883 pr_warn_once("%s: unhandled return value: %ld\n",
890 start += pages * PAGE_SIZE;
896 * Application wants to free up the pages and associated backing store.
897 * This is effectively punching a hole into the middle of a file.
899 static long madvise_remove(struct vm_area_struct *vma,
900 struct vm_area_struct **prev,
901 unsigned long start, unsigned long end)
906 struct mm_struct *mm = vma->vm_mm;
908 *prev = NULL; /* tell sys_madvise we drop mmap_lock */
910 if (vma->vm_flags & VM_LOCKED)
915 if (!f || !f->f_mapping || !f->f_mapping->host) {
919 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
922 offset = (loff_t)(start - vma->vm_start)
923 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
926 * Filesystem's fallocate may need to take i_rwsem. We need to
927 * explicitly grab a reference because the vma (and hence the
928 * vma's reference to the file) can go away as soon as we drop
932 if (userfaultfd_remove(vma, start, end)) {
933 /* mmap_lock was not released by userfaultfd_remove() */
934 mmap_read_unlock(mm);
936 error = vfs_fallocate(f,
937 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
938 offset, end - start);
945 * Apply an madvise behavior to a region of a vma. madvise_update_vma
946 * will handle splitting a vm area into separate areas, each area with its own
949 static int madvise_vma_behavior(struct vm_area_struct *vma,
950 struct vm_area_struct **prev,
951 unsigned long start, unsigned long end,
952 unsigned long behavior)
955 struct anon_vma_name *anon_name;
956 unsigned long new_flags = vma->vm_flags;
960 return madvise_remove(vma, prev, start, end);
962 return madvise_willneed(vma, prev, start, end);
964 return madvise_cold(vma, prev, start, end);
966 return madvise_pageout(vma, prev, start, end);
969 return madvise_dontneed_free(vma, prev, start, end, behavior);
970 case MADV_POPULATE_READ:
971 case MADV_POPULATE_WRITE:
972 return madvise_populate(vma, prev, start, end, behavior);
974 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
976 case MADV_SEQUENTIAL:
977 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
980 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
983 new_flags |= VM_DONTCOPY;
986 if (vma->vm_flags & VM_IO)
988 new_flags &= ~VM_DONTCOPY;
990 case MADV_WIPEONFORK:
991 /* MADV_WIPEONFORK is only supported on anonymous memory. */
992 if (vma->vm_file || vma->vm_flags & VM_SHARED)
994 new_flags |= VM_WIPEONFORK;
996 case MADV_KEEPONFORK:
997 new_flags &= ~VM_WIPEONFORK;
1000 new_flags |= VM_DONTDUMP;
1003 if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL)
1005 new_flags &= ~VM_DONTDUMP;
1007 case MADV_MERGEABLE:
1008 case MADV_UNMERGEABLE:
1009 error = ksm_madvise(vma, start, end, behavior, &new_flags);
1014 case MADV_NOHUGEPAGE:
1015 error = hugepage_madvise(vma, &new_flags, behavior);
1021 anon_name = anon_vma_name(vma);
1022 anon_vma_name_get(anon_name);
1023 error = madvise_update_vma(vma, prev, start, end, new_flags,
1025 anon_vma_name_put(anon_name);
1029 * madvise() returns EAGAIN if kernel resources, such as
1030 * slab, are temporarily unavailable.
1032 if (error == -ENOMEM)
1037 #ifdef CONFIG_MEMORY_FAILURE
1039 * Error injection support for memory error handling.
1041 static int madvise_inject_error(int behavior,
1042 unsigned long start, unsigned long end)
1046 if (!capable(CAP_SYS_ADMIN))
1050 for (; start < end; start += size) {
1055 ret = get_user_pages_fast(start, 1, 0, &page);
1058 pfn = page_to_pfn(page);
1061 * When soft offlining hugepages, after migrating the page
1062 * we dissolve it, therefore in the second loop "page" will
1063 * no longer be a compound page.
1065 size = page_size(compound_head(page));
1067 if (behavior == MADV_SOFT_OFFLINE) {
1068 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
1070 ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
1072 pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
1074 ret = memory_failure(pfn, MF_COUNT_INCREASED);
1075 if (ret == -EOPNOTSUPP)
1088 madvise_behavior_valid(int behavior)
1094 case MADV_SEQUENTIAL:
1102 case MADV_POPULATE_READ:
1103 case MADV_POPULATE_WRITE:
1105 case MADV_MERGEABLE:
1106 case MADV_UNMERGEABLE:
1108 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1110 case MADV_NOHUGEPAGE:
1114 case MADV_WIPEONFORK:
1115 case MADV_KEEPONFORK:
1116 #ifdef CONFIG_MEMORY_FAILURE
1117 case MADV_SOFT_OFFLINE:
1128 process_madvise_behavior_valid(int behavior)
1141 * Walk the vmas in range [start,end), and call the visit function on each one.
1142 * The visit function will get start and end parameters that cover the overlap
1143 * between the current vma and the original range. Any unmapped regions in the
1144 * original range will result in this function returning -ENOMEM while still
1145 * calling the visit function on all of the existing vmas in the range.
1146 * Must be called with the mmap_lock held for reading or writing.
1149 int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
1150 unsigned long end, unsigned long arg,
1151 int (*visit)(struct vm_area_struct *vma,
1152 struct vm_area_struct **prev, unsigned long start,
1153 unsigned long end, unsigned long arg))
1155 struct vm_area_struct *vma;
1156 struct vm_area_struct *prev;
1158 int unmapped_error = 0;
1161 * If the interval [start,end) covers some unmapped address
1162 * ranges, just ignore them, but return -ENOMEM at the end.
1163 * - different from the way of handling in mlock etc.
1165 vma = find_vma_prev(mm, start, &prev);
1166 if (vma && start > vma->vm_start)
1172 /* Still start < end. */
1176 /* Here start < (end|vma->vm_end). */
1177 if (start < vma->vm_start) {
1178 unmapped_error = -ENOMEM;
1179 start = vma->vm_start;
1184 /* Here vma->vm_start <= start < (end|vma->vm_end) */
1189 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1190 error = visit(vma, &prev, start, tmp, arg);
1194 if (prev && start < prev->vm_end)
1195 start = prev->vm_end;
1199 vma = prev->vm_next;
1200 else /* madvise_remove dropped mmap_lock */
1201 vma = find_vma(mm, start);
1204 return unmapped_error;
1207 #ifdef CONFIG_ANON_VMA_NAME
1208 static int madvise_vma_anon_name(struct vm_area_struct *vma,
1209 struct vm_area_struct **prev,
1210 unsigned long start, unsigned long end,
1211 unsigned long anon_name)
1215 /* Only anonymous mappings can be named */
1219 error = madvise_update_vma(vma, prev, start, end, vma->vm_flags,
1220 (struct anon_vma_name *)anon_name);
1223 * madvise() returns EAGAIN if kernel resources, such as
1224 * slab, are temporarily unavailable.
1226 if (error == -ENOMEM)
1231 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
1232 unsigned long len_in, struct anon_vma_name *anon_name)
1237 if (start & ~PAGE_MASK)
1239 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1241 /* Check to see whether len was rounded up from small -ve to zero */
1252 return madvise_walk_vmas(mm, start, end, (unsigned long)anon_name,
1253 madvise_vma_anon_name);
1255 #endif /* CONFIG_ANON_VMA_NAME */
1257 * The madvise(2) system call.
1259 * Applications can use madvise() to advise the kernel how it should
1260 * handle paging I/O in this VM area. The idea is to help the kernel
1261 * use appropriate read-ahead and caching techniques. The information
1262 * provided is advisory only, and can be safely disregarded by the
1263 * kernel without affecting the correct operation of the application.
1266 * MADV_NORMAL - the default behavior is to read clusters. This
1267 * results in some read-ahead and read-behind.
1268 * MADV_RANDOM - the system should read the minimum amount of data
1269 * on any access, since it is unlikely that the appli-
1270 * cation will need more than what it asks for.
1271 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
1272 * once, so they can be aggressively read ahead, and
1273 * can be freed soon after they are accessed.
1274 * MADV_WILLNEED - the application is notifying the system to read
1276 * MADV_DONTNEED - the application is finished with the given range,
1277 * so the kernel can free resources associated with it.
1278 * MADV_FREE - the application marks pages in the given range as lazy free,
1279 * where actual purges are postponed until memory pressure happens.
1280 * MADV_REMOVE - the application wants to free up the given range of
1281 * pages and associated backing store.
1282 * MADV_DONTFORK - omit this area from child's address space when forking:
1283 * typically, to avoid COWing pages pinned by get_user_pages().
1284 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
1285 * MADV_WIPEONFORK - present the child process with zero-filled memory in this
1286 * range after a fork.
1287 * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
1288 * MADV_HWPOISON - trigger memory error handler as if the given memory range
1289 * were corrupted by unrecoverable hardware memory failure.
1290 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
1291 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1292 * this area with pages of identical content from other such areas.
1293 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
1294 * MADV_HUGEPAGE - the application wants to back the given range by transparent
1295 * huge pages in the future. Existing pages might be coalesced and
1296 * new pages might be allocated as THP.
1297 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1298 * transparent huge pages so the existing pages will not be
1299 * coalesced into THP and new pages will not be allocated as THP.
1300 * MADV_DONTDUMP - the application wants to prevent pages in the given range
1301 * from being included in its core dump.
1302 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1303 * MADV_COLD - the application is not expected to use this memory soon,
1304 * deactivate pages in this range so that they can be reclaimed
1305 * easily if memory pressure happens.
1306 * MADV_PAGEOUT - the application is not expected to use this memory soon,
1307 * page out the pages in this range immediately.
1308 * MADV_POPULATE_READ - populate (prefault) page tables readable by
1309 * triggering read faults if required
1310 * MADV_POPULATE_WRITE - populate (prefault) page tables writable by
1311 * triggering write faults if required
1315 * -EINVAL - start + len < 0, start is not page-aligned,
1316 * "behavior" is not a valid value, or application
1317 * is attempting to release locked or shared pages,
1318 * or the specified address range includes file, Huge TLB,
1319 * MAP_SHARED or VMPFNMAP range.
1320 * -ENOMEM - addresses in the specified range are not currently
1321 * mapped, or are outside the AS of the process.
1322 * -EIO - an I/O error occurred while paging in data.
1323 * -EBADF - map exists, but area maps something that isn't a file.
1324 * -EAGAIN - a kernel resource was temporarily unavailable.
1326 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
1332 struct blk_plug plug;
1334 start = untagged_addr(start);
1336 if (!madvise_behavior_valid(behavior))
1339 if (!PAGE_ALIGNED(start))
1341 len = PAGE_ALIGN(len_in);
1343 /* Check to see whether len was rounded up from small -ve to zero */
1354 #ifdef CONFIG_MEMORY_FAILURE
1355 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1356 return madvise_inject_error(behavior, start, start + len_in);
1359 write = madvise_need_mmap_write(behavior);
1361 if (mmap_write_lock_killable(mm))
1367 blk_start_plug(&plug);
1368 error = madvise_walk_vmas(mm, start, end, behavior,
1369 madvise_vma_behavior);
1370 blk_finish_plug(&plug);
1372 mmap_write_unlock(mm);
1374 mmap_read_unlock(mm);
1379 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1381 return do_madvise(current->mm, start, len_in, behavior);
1384 SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1385 size_t, vlen, int, behavior, unsigned int, flags)
1388 struct iovec iovstack[UIO_FASTIOV], iovec;
1389 struct iovec *iov = iovstack;
1390 struct iov_iter iter;
1391 struct task_struct *task;
1392 struct mm_struct *mm;
1394 unsigned int f_flags;
1401 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1405 task = pidfd_get_task(pidfd, &f_flags);
1407 ret = PTR_ERR(task);
1411 if (!process_madvise_behavior_valid(behavior)) {
1416 /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1417 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1418 if (IS_ERR_OR_NULL(mm)) {
1419 ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1424 * Require CAP_SYS_NICE for influencing process performance. Note that
1425 * only non-destructive hints are currently supported.
1427 if (!capable(CAP_SYS_NICE)) {
1432 total_len = iov_iter_count(&iter);
1434 while (iov_iter_count(&iter)) {
1435 iovec = iov_iter_iovec(&iter);
1437 * do_madvise returns ENOMEM if unmapped holes are present
1438 * in the passed VMA. process_madvise() is expected to skip
1439 * unmapped holes passed to it in the 'struct iovec' list
1440 * and not fail because of them. Thus treat -ENOMEM return
1441 * from do_madvise as valid and continue processing.
1443 ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1444 iovec.iov_len, behavior);
1445 if (ret < 0 && ret != -ENOMEM)
1447 iov_iter_advance(&iter, iovec.iov_len);
1450 ret = (total_len - iov_iter_count(&iter)) ? : ret;
1455 put_task_struct(task);