From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=49.212.243.145 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796027763; b=JysiRl25XmUwU9foSpNvgrZGhpFsu7BcCuJFcVxo5enhBGR1yszY3pb26ovABCqkBY4h +XSlJ9j1q3RjsL1OVTTD50KmS/qWj+fsoXQWLQhfGNcz7eEQTxQTQ8BJ68Blrqu4HzuSZ /44vBcrr5W/EXxCipJpeK8c8GEPTa8z0Vk+6dFlNSj7tuuTHJqKQKAzgWpJeKGJ1cvbsX uXxdQ3HulnjooPCVxpNbflL3PcpME7DQSC2NexxsaWopEOnilTddpABITqSSoL5lNhh5/ xs9RqbUhgGTu6hIusfMvEoXa6dlfRR1B0w6n+XXtbXhcGKNox9IiY3VDS6SfqtolMJQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796027763; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Anti-Virus-Server:X-Virus-Status; bh=BhdQrSlYysgjwqdDJyxMqhgDO1CC+yKA9l3ZKmWEBdI=; b=emiayRQ61AoYO6IYegEPscsuBBrUx9tZWP+kl6O/z0PA5s86y+2tpj2fBFrEurCn1bI2 F6FwcgTExt7tZsvoIS+gZEISqVtDsxBXQoNfffrwZzseHB+yiZD9ZMkNMkAo501XogMkp RI90E/qc7Z+z5jfZvSG16XpywxOFiIEfhk7HOWEh7k+oOW5UbIiF1q8lsuwFfitmEB6qj zG+jjwPcWFlAXBxX44iZI3OtoH8TCegfhoi2oG1Xk9exhlSa1GI/LdNkK6apTNLZE0Mfw 5bCAb0OkiVYSTuH0PYj3AoTfxw9sPF1HCxNDCblNbs2Fcuze8TTcxJiQai8eVwI1Fyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pisnp.net; spf=pass smtp.mailfrom=pisnp.net; dkim=pass header.d=pisnp.net header.i=@pisnp.net header.a=rsa-sha256 header.s=rs20260908 header.b=iF+0w0TU; arc=none smtp.remote-ip=49.212.243.145 Received: from www3605.sakura.ne.jp (www3605.sakura.ne.jp [49.212.243.145]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FD2B606A327; Mon, 30 Nov 2026 08:36:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5FD2B606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pisnp.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pisnp.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pisnp.net header.i=@pisnp.net header.a=rsa-sha256 header.s=rs20260908 header.b=iF+0w0TU Received: from www3605.sakura.ne.jp (localhost [127.0.0.1]) by www3605.sakura.ne.jp (8.16.1/8.16.1) with ESMTP id 6AU8Yqbc026366; Mon, 30 Nov 2026 17:34:52 +0900 (JST) (envelope-from hiro@pisnp.net) Received: from hirotakay.c.googlers.com.com (57.134.185.35.bc.googleusercontent.com [35.185.134.57]) (authenticated bits=0) by www3605.sakura.ne.jp (8.16.1/8.16.1) with ESMTPSA id 6AU8YbfP026202 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 30 Nov 2026 17:34:51 +0900 (JST) (envelope-from hiro@pisnp.net) DKIM-Signature: a=rsa-sha256; bh=BhdQrSlYysgjwqdDJyxMqhgDO1CC+yKA9l3ZKmWEBdI=; c=relaxed/relaxed; d=pisnp.net; h=From:Message-ID:To:Subject:Date; s=rs20260908; t=1796027692; v=1; b=iF+0w0TUsnMFiUy3GEtNDrXTaRiVG4m6/wD+bFDDmOBT19aLq7a8G0aEoYP8d19A wW8bdlaCQ2hTqoPee5HwtIUvqnpNBAs3FzELb+vfyb/kdvBY1aVem3SQCAtpI+m5 OEsLTAv8TJYNwU0leWwd/JHAoP3MQNLSnbSxzo/YxwX/SCQllAHSXXpxYtCjXK// ExXZURDIV5E8jUQg514jn45j5DK1DbxeAexbVU1YJw9AFMtdFhgNmA8tv330Py1y Jo2f/VoYAMNgPAtSbaBouYv7SDXuMLYw7u0aaV/i6JBkw640Pc33ia7Vol9busNv DTdbrZdsOqfpRMKcOzmrVQ== From: Hirotaka Yamagata To: Andrew Morton Cc: linux-api@vger.kernel.org, "Liam R. Howlett" , Lorenzo Stoakes , David Hildenbrand , Chris Li , Kairui Song , Steven Rostedt , Masami Hiramatsu , Arnd Bergmann , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org, cgroups@vger.kernel.org, John Stultz , Minchan Kim , Alexandre Marciano Gimenez , Tatsuyuki Ishi Subject: [RFC PATCH v1 2/4] mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages Date: Mon, 30 Nov 2026 17:34:05 +0900 Message-ID: <20261130083407.2669584-3-hiro@pisnp.net> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog In-Reply-To: <20261130083407.2669584-1-hiro@pisnp.net> References: <20261130083407.2669584-1-hiro@pisnp.net> X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Anti-Virus-Server: fsav114.rs.sakura.ne.jp X-Virus-Status: clean 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