[RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint

From
Hirotaka Yamagata <hidden>
Date
2026-11-30 08:36:02
Also in
cgroups, linux-arch, linux-fsdevel, linux-mm, linux-trace-kernel, lkml
Reply to
Hirotaka Yamagata, 2026-11-30
Add accounting and a tracepoint for volatile folio purging:
- Add the PGPURGE ("pgpurge") vmstat and memcg event item counting the
  pages discarded by purge_volatile_folio(). Pages are counted once
  their mappings hold PTE_MARKER_PURGED markers and the swap cache is
  gone, and are charged to the folio's memcg as well, so memory.stat
  shows purges per cgroup.
- Add the mm_purge_volatile_folio tracepoint in
  include/trace/events/vmscan.h recording the folio PFN, page count, and
  whether the folio was purged or, being a large folio, left to swap.

Cc: John Stultz <jstultz@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Alexandre Marciano Gimenez <redacted>
Cc: Tatsuyuki Ishi <redacted>
Signed-off-by: Hirotaka Yamagata <redacted>
---
 include/linux/vm_event_item.h |  1 +
 include/trace/events/vmscan.h | 24 ++++++++++++++++++++++++
 mm/memcontrol.c               |  1 +
 mm/mvolatile.c                | 20 ++++++++++++++++++--
 mm/vmstat.c                   |  1 +
 5 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index 2628ccda076a..9832711eafa7 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -38,6 +38,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 		PGFREE, PGACTIVATE, PGDEACTIVATE, PGLAZYFREE,
 		PGFAULT, PGMAJFAULT,
 		PGLAZYFREED,
+		PGPURGE,
 		PGREUSE,
 		PGSCAN_DIRECT_THROTTLE,
 #ifdef CONFIG_NUMA
diff --git a/include/trace/events/vmscan.h b/include/trace/events/vmscan.h
index b4bf7b8def1f..15e22b119914 100644
--- a/include/trace/events/vmscan.h
+++ b/include/trace/events/vmscan.h
@@ -429,6 +429,30 @@ TRACE_EVENT(mm_vmscan_write_folio,
 		show_reclaim_flags(__entry->reclaim_flags))
 );
 
+TRACE_EVENT(mm_purge_volatile_folio,
+
+	TP_PROTO(struct folio *folio, bool purged),
+
+	TP_ARGS(folio, purged),
+
+	TP_STRUCT__entry(
+		__field(unsigned long, pfn)
+		__field(unsigned long, nr_pages)
+		__field(bool, purged)
+	),
+
+	TP_fast_assign(
+		__entry->pfn = folio_pfn(folio);
+		__entry->nr_pages = folio_nr_pages(folio);
+		__entry->purged = purged;
+	),
+
+	TP_printk("pfn=0x%lx nr_pages=%lu purged=%d",
+		__entry->pfn,
+		__entry->nr_pages,
+		__entry->purged)
+);
+
 TRACE_EVENT(mm_vmscan_reclaim_pages,
 
 	TP_PROTO(int nid,
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1271d390b617..d075920cdd2b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -608,6 +608,7 @@ static const unsigned int memcg_vm_event_stat[] = {
 	PGDEACTIVATE,
 	PGLAZYFREE,
 	PGLAZYFREED,
+	PGPURGE,
 #ifdef CONFIG_SWAP
 	SWPIN_ZERO,
 	SWPOUT_ZERO,
diff --git a/mm/mvolatile.c b/mm/mvolatile.c
index c111bc4f111d..aa532d72d81c 100644
--- a/mm/mvolatile.c
+++ b/mm/mvolatile.c
@@ -15,6 +15,9 @@
 #include <linux/swap.h>
 #include <linux/swapops.h>
 #include <linux/pagewalk.h>
+#include <linux/memcontrol.h>
+#include <linux/vmstat.h>
+#include <trace/events/vmscan.h>
 #include "internal.h"
 #include "vma.h"
 
@@ -261,9 +264,13 @@ enum purge_result __purge_volatile_folio(struct folio *folio)
 	if (!mapped)
 		return PURGE_NONE;
 
-	/* Only order-0 folios are supported for now. */
+	/*
+	 * Only order-0 folios are supported for now. Checked after the
+	 * volatile test so that the tracepoint shows large volatile folios
+	 * falling back to swap.
+	 */
 	if (folio_test_large(folio))
-		return PURGE_NONE;
+		goto fail;
 
 	try_to_unmap(folio, TTU_PURGE | TTU_SYNC);
 
@@ -280,6 +287,11 @@ enum purge_result __purge_volatile_folio(struct folio *folio)
 	if (folio_test_swapcache(folio) && !folio_free_swap(folio))
 		return PURGE_NONE;
 
+	/* Count before the freeze, so that a retry is not counted again. */
+	count_vm_events(PGPURGE, folio_nr_pages(folio));
+	count_memcg_folio_events(folio, PGPURGE, folio_nr_pages(folio));
+	trace_mm_purge_volatile_folio(folio, true);
+
 freeze:
 	/*
 	 * Like __remove_mapping(), back off on a transient reference. No new
@@ -292,4 +304,8 @@ enum purge_result __purge_volatile_folio(struct folio *folio)
 
 	folio_unlock(folio);
 	return PURGE_DONE;
+
+fail:
+	trace_mm_purge_volatile_folio(folio, false);
+	return PURGE_NONE;
 }
diff --git a/mm/vmstat.c b/mm/vmstat.c
index cb57714539fb..c2d4be97e0b0 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1347,6 +1347,7 @@ const char * const vmstat_text[] = {
 	[I(PGFAULT)]				= "pgfault",
 	[I(PGMAJFAULT)]				= "pgmajfault",
 	[I(PGLAZYFREED)]			= "pglazyfreed",
+	[I(PGPURGE)]				= "pgpurge",
 
 	[I(PGREUSE)]				= "pgreuse",
 	[I(PGSCAN_DIRECT_THROTTLE)]		= "pgscan_direct_throttle",
-- 
2.56.1.839.g22a8290db9-goog
Thread (9 messages) 9 messages, 4 authors, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help