When a volatile anonymous page is discarded under memory pressure, the page table must record that the page was purged so that: 1. Accessing the purged address while still volatile raises SIGBUS. 2. A subsequent MADV_NONVOLATILE call can detect that data in the range was discarded, clear the marker, and notify userspace. Introduce PTE_MARKER_PURGED (BIT(3)) using the PTE marker infrastructure and wire up its lifecycle across core mm paths: - Deliver VM_FAULT_SIGBUS in handle_pte_marker() when faulting on a PTE_MARKER_PURGED entry. - Preserve PTE_MARKER_PURGED across fork() in copy_pte_marker() and ignore it during mprotect() in change_softleaf_pte(). - Drop PTE_MARKER_PURGED unconditionally in zap_nonpresent_ptes() on munmap() or MADV_DONTNEED. - Add TTU_PURGE to enum ttu_flags so try_to_unmap_one() installs a PTE_MARKER_PURGED entry when unmapping a purged volatile page; like swap-out, it backs off when an anon-exclusive page may be pinned. - Make khugepaged and MADV_COLLAPSE skip PTE markers (other than uffd-wp) at scan time, so that collapse does not try to swap in purged markers. Cc: John Stultz Cc: Minchan Kim Cc: Alexandre Marciano Gimenez Cc: Tatsuyuki Ishi Signed-off-by: Hirotaka Yamagata --- include/linux/leafops.h | 14 ++++++++++++++ include/linux/mm_inline.h | 5 +++-- include/linux/rmap.h | 1 + include/linux/swapops.h | 18 +++++++++++++++++- mm/khugepaged.c | 17 +++++++++++++++++ mm/memory.c | 5 +++++ mm/mprotect.c | 6 ++++-- mm/rmap.c | 14 ++++++++++++++ 8 files changed, 75 insertions(+), 5 deletions(-) diff --git a/include/linux/leafops.h b/include/linux/leafops.h index 4c1476ae3234..1408c1086c71 100644 --- a/include/linux/leafops.h +++ b/include/linux/leafops.h @@ -464,6 +464,20 @@ static inline bool softleaf_is_guard_marker(softleaf_t entry) return softleaf_to_marker(entry) & PTE_MARKER_GUARD; } +/** + * softleaf_is_purged_marker() - Is this leaf entry a purged marker? + * @entry: Leaf entry. + * + * Returns: true if the leaf entry is a purged marker, otherwise false. + */ +static inline bool softleaf_is_purged_marker(softleaf_t entry) +{ + if (!softleaf_is_marker(entry)) + return false; + + return softleaf_to_marker(entry) & PTE_MARKER_PURGED; +} + /** * softleaf_is_uffd_wp_marker() - Is this leaf entry a userfautlfd write protect * marker? diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 621c8653d8f7..f9ec2defc7b2 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -556,8 +556,9 @@ static inline pte_marker copy_pte_marker( softleaf_t entry, struct vm_area_struct *dst_vma) { const pte_marker srcm = softleaf_to_marker(entry); - /* Always copy error entries. */ - pte_marker dstm = srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD); + /* Always copy error and purged entries. */ + pte_marker dstm = srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD | + PTE_MARKER_PURGED); /* Only copy PTE markers if UFFD register matches. */ if ((srcm & PTE_MARKER_UFFD_WP) && userfaultfd_wp(dst_vma)) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 0b332770abee..88b69a2dce96 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -102,6 +102,7 @@ enum ttu_flags { * do a final flush if necessary */ TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock: * caller holds it */ + TTU_PURGE = 0x100, /* convert pte to purged marker entry */ }; #ifdef CONFIG_MMU diff --git a/include/linux/swapops.h b/include/linux/swapops.h index e7d0d529f3e0..1aa2baaed49e 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -298,7 +298,13 @@ typedef unsigned long pte_marker; * PROT_NONE, rather than if they were a memory hole or equivalent. */ #define PTE_MARKER_GUARD BIT(2) -#define PTE_MARKER_MASK (BIT(3) - 1) +/* + * Indicates that a volatile anonymous page mapped by this PTE was purged + * under memory pressure. MADV_NONVOLATILE clears it and reports the purge; + * see handle_pte_marker() for faults before MADV_NONVOLATILE. + */ +#define PTE_MARKER_PURGED BIT(3) +#define PTE_MARKER_MASK (BIT(4) - 1) static inline swp_entry_t make_pte_marker_entry(pte_marker marker) { @@ -320,6 +326,16 @@ static inline swp_entry_t make_guard_swp_entry(void) return make_pte_marker_entry(PTE_MARKER_GUARD); } +static inline swp_entry_t make_purged_swp_entry(void) +{ + return make_pte_marker_entry(PTE_MARKER_PURGED); +} + +static inline pte_t make_purged_entry(void) +{ + return make_pte_marker(PTE_MARKER_PURGED); +} + struct page_vma_mapped_walk; #ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 11ff98d55c76..24cb60230746 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1200,6 +1200,14 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm, pte_present(vmf.orig_pte)) continue; + /* Installed since the scan, see collapse_scan_pmd(). */ + if (pte_is_marker(vmf.orig_pte)) { + pte_unmap(pte); + mmap_read_unlock(mm); + result = SCAN_PTE_NON_PRESENT; + goto out; + } + /* * TODO: Support swapin without leading to further mTHP * collapses. Currently bringing in new pages via swapin may @@ -1667,6 +1675,15 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm, continue; } if (!pte_present(pteval)) { + /* + * Markers such as purged or guard markers do not map a + * page that could be swapped in; faulting on them only + * raises a signal. uffd-wp markers are handled below. + */ + if (pte_is_marker(pteval) && !pte_is_uffd_wp_marker(pteval)) { + result = SCAN_PTE_NON_PRESENT; + goto out_unmap; + } if (++unmapped > max_ptes_swap) { result = SCAN_EXCEED_SWAP_PTE; count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SWAP_PTE, diff --git a/mm/memory.c b/mm/memory.c index 8b0c2c735d3d..eecb84bf76a6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1919,6 +1919,8 @@ static inline int zap_nonpresent_ptes(struct mmu_gather *tlb, */ if (!zap_drop_markers(details)) return 1; + } else if (softleaf_is_purged_marker(entry)) { + /* Unlike guard markers, purged markers are always dropped. */ } else if (softleaf_is_hwpoison(entry) || softleaf_is_poison_marker(entry)) { if (!should_zap_cows(details)) @@ -4736,6 +4738,9 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf) if (marker & PTE_MARKER_GUARD) return VM_FAULT_SIGSEGV; + if (marker & PTE_MARKER_PURGED) + return VM_FAULT_SIGBUS; + if (softleaf_is_uffd_wp_marker(entry)) return pte_marker_handle_uffd_wp(vmf); diff --git a/mm/mprotect.c b/mm/mprotect.c index 2888ee638d87..7e725c9ce215 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -247,10 +247,12 @@ static long change_softleaf_pte(struct vm_area_struct *vma, /* * Ignore error swap entries unconditionally, * because any access should sigbus/sigsegv - * anyway. + * anyway. Purged markers carry no protection + * information. */ if (softleaf_is_poison_marker(entry) || - softleaf_is_guard_marker(entry)) + softleaf_is_guard_marker(entry) || + softleaf_is_purged_marker(entry)) return 0; /* * If this is uffd-wp pte marker and we'd like diff --git a/mm/rmap.c b/mm/rmap.c index d1819fd69938..a3273a9a7d22 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2359,6 +2359,20 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, pteval = swp_entry_to_pte(make_hwpoison_entry(page)); dec_mm_counter(mm, mm_counter(folio)); set_pte_at(mm, address, pvmw.pte, pteval); + } else if (flags & TTU_PURGE) { + /* + * Like swap-out, back off if the page may be pinned. + * See folio_try_share_anon_rmap(): clear PTE first. + */ + if (PageAnonExclusive(page) && + folio_try_share_anon_rmap_pte(folio, page)) { + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); + goto walk_abort; + } + pteval = make_purged_entry(); + dec_mm_counter(mm, mm_counter(folio)); + set_pte_at(mm, address, pvmw.pte, pteval); + } else if (likely(pte_present(pteval)) && pte_unused(pteval) && !userfaultfd_armed(vma)) { /* -- 2.56.1.839.g22a8290db9-goog