Linux Trace Kernel
 help / color / mirror / Atom feed
From: Hirotaka Yamagata <hiro@pisnp.net>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-api@vger.kernel.org, "Liam R. Howlett" <liam@infradead.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	David Hildenbrand <david@kernel.org>,
	Chris Li <chrisl@kernel.org>, Kairui Song <kasong@tencent.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Arnd Bergmann <arnd@arndb.de>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	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 <jstultz@google.com>,
	Minchan Kim <minchan@kernel.org>,
	Alexandre Marciano Gimenez <raging@google.com>,
	Tatsuyuki Ishi <ishitatsuyuki@google.com>
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	[thread overview]
Message-ID: <20261130083407.2669584-3-hiro@pisnp.net> (raw)
In-Reply-To: <20261130083407.2669584-1-hiro@pisnp.net>

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 <jstultz@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Alexandre Marciano Gimenez <raging@google.com>
Cc: Tatsuyuki Ishi <ishitatsuyuki@google.com>
Signed-off-by: Hirotaka Yamagata <hiro@pisnp.net>
---
 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


  parent reply	other threads:[~2026-11-30  8:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:34 [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 Hirotaka Yamagata
2026-11-30  8:34 ` [RFC PATCH v1 1/4] mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA handling Hirotaka Yamagata
2026-11-30  8:34 ` Hirotaka Yamagata [this message]
2026-11-30  8:34 ` [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Hirotaka Yamagata
2026-11-30  8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130083407.2669584-3-hiro@pisnp.net \
    --to=hiro@pisnp.net \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=cgroups@vger.kernel.org \
    --cc=chrisl@kernel.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=ishitatsuyuki@google.com \
    --cc=jstultz@google.com \
    --cc=kasong@tencent.com \
    --cc=liam@infradead.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=minchan@kernel.org \
    --cc=raging@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=shakeel.butt@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox