Skip to contentSkip to conversation

mm: vmscan: rework lru_shrink and write_folio tracepoints

PATCH In review qiwu.chen(陈启武) 3 participants 3 messages Sep 14

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mm: vmscan: rework lru_shrink and write_folio tracepoints

From: "qiwu.chen" <[email protected]>

Currently, reclaim_flags always contains RECLAIM_WB_ASYNC in lru_shrink tracepoints since commit 41ac1999c3e35 ("mm: vmscan: do not stall on writeback during memory compaction"), which is useless for debugging memory pressure issues. Other RECLAIM_WB_* flags are not used anywhere else, so they can be directly removed.
This patch reworks the lru_shrink and write_folio tracepoints for better correlation and analysis:
- traces each folio lru type instead of reclaim_flags.
- traces each lru_shrink with reason.
- remove the printing of the unnecessary PFN for mm_vmscan_write_folio.
- traces the number of activated folios by folio_check_references.

Fixes 41ac1999c3e35 ("mm: vmscan: do not stall on writeback during memory compaction")
---
 include/linux/vmstat.h        |  1 +
 include/trace/events/vmscan.h | 77 ++++++++++++++++-------------------
 mm/vmscan.c                   | 11 ++---
 3 files changed, 41 insertions(+), 48 deletions(-)
include/linux/vmstat.h +1 -0
index 5b31d8e7ae40..72c3b51a633f 100644
@@ -22,6 +22,7 @@ struct reclaim_stat {
 	unsigned nr_immediate;
 	unsigned nr_activate[ANON_AND_FILE];
 	unsigned nr_ref_keep;
+	unsigned nr_ref_activate;
 	unsigned nr_unmap_fail;
 	unsigned nr_lazyfree_fail;
 	unsigned nr_demoted;
include/trace/events/vmscan.h +34 -43

155 lines not on this page · raw message

mm/vmscan.c +6 -5
index f11491ee9ed5..c90e2dc1d324 100644
@@ -1233,6 +1233,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 
 		switch (references) {
 		case FOLIOREF_ACTIVATE:
+			stat->nr_ref_activate += nr_pages;
 			goto activate_locked;
 		case FOLIOREF_KEEP:
 			stat->nr_ref_keep += nr_pages;
@@ -2041,7 +2042,7 @@ static unsigned long shrink_inactive_list(unsigned long nr_to_scan,
 
 	handle_reclaim_writeback(nr_taken, pgdat, sc, &stat);
 	trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
-			nr_scanned, nr_reclaimed, &stat, sc->priority, file);
+			nr_scanned, nr_reclaimed, &stat, sc->priority, lru, item);
 	return nr_reclaimed;
 }
 
@@ -2148,7 +2149,8 @@ static void shrink_active_list(unsigned long nr_to_scan,
 		mod_lruvec_state(lruvec, PGROTATE_ANON + file, nr_rotated);
 
 	trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
-			nr_deactivate, nr_rotated, sc->priority, file);
+			nr_deactivate, nr_rotated, sc->priority, lru,
+			PGSTEAL_KSWAPD + reclaimer_offset(sc));
 }
 
 static unsigned int reclaim_folio_list(struct list_head *folio_list,
@@ -4910,12 +4912,13 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 	reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false, memcg);
 	sc->nr_reclaimed += reclaimed;
 	total_reclaimed += reclaimed;
+	item = PGSTEAL_KSWAPD + reclaimer_offset(sc);
 	/* Retry pass is only meant for clean folios without new isolation */
 	if (isolated)
 		handle_reclaim_writeback(isolated, pgdat, sc, &stat);
 	trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
 			type_scanned, reclaimed, &stat, sc->priority,
-			type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON);
+			type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON, item);
 
 	list_for_each_entry_safe_reverse(folio, next, &list, lru) {
 		DEFINE_MIN_SEQ(lruvec);
@@ -4946,8 +4949,6 @@ static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 
 	mod_lruvec_state(lruvec, PGDEMOTE_KSWAPD + reclaimer_offset(sc),
 					stat.nr_demoted);
-
-	item = PGSTEAL_KSWAPD + reclaimer_offset(sc);
 	mod_lruvec_state(lruvec, item, reclaimed);
 	mod_lruvec_state(lruvec, PGSTEAL_ANON + type, reclaimed);
 
Signature · 1 line
2.50.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] The `mm_vmscan_lru_shrink_inactive` tracepoint prints `nr_ref_keep` twice and omits the newly…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The mm_vmscan_lru_shrink_inactive tracepoint prints nr_ref_keep twice and omits the newly added nr_ref_activate field.
- [Medium] Missing TRACE_DEFINE_ENUM exports for PGSTEAL_* enum values used in trace_reclaim_reason_ops.
- [Medium] The patch breaks the in-tree trace-vmscan-postprocess.pl script by altering tracepoint arguments without updating the script. --

commit 415a9b2c51b867b9381070883253c78fbde68aee
Author: qiwu.chen <[email protected]>

mm: vmscan: rework lru_shrink and write_folio tracepoints

This commit reworks the lru_shrink and write_folio tracepoints for better correlation and analysis. It traces each folio LRU type and reclaim reason, adds activation statistics, and removes some unused reclaim flags.

Quoted 4 lines
include/trace/events/vmscan.h +0 -0
index 8a872990b4bee..db6c6d2d777af 100644

[ ... ]

include/trace/events/vmscan.h TRACE_DEFINE_ENUM(KSWAPD_CLEAR_HOPELESS_PCP); · line 42

Quoted 9 lines
include/trace/events/vmscan.h +5 -4
-#define trace_reclaim_flags(file) ( \
-	(file ? RECLAIM_WB_FILE : RECLAIM_WB_ANON) | \
-	(RECLAIM_WB_ASYNC) \
-	)
+#define trace_reclaim_reason_ops		\
+	{PGSTEAL_KSWAPD,	"KSWAPD"},	\
+	{PGSTEAL_DIRECT,	"DIRECT"},	\
+	{PGSTEAL_KHUGEPAGED,	"KHUGEPAGED"}, \
+	{PGSTEAL_PROACTIVE,	"PROACTIVE"}

[Severity: Medium]
Since this macro maps enums to strings for use with __print_symbolic(), do these enum values need corresponding TRACE_DEFINE_ENUM() exports?

Without them, userspace tools like perf or trace-cmd will not be able to evaluate the enum values from the trace format file and will fail to parse the reason field.

[ ... ]

Quoted 13 lines
include/trace/events/vmscan.h +3 -2
@@ -495,29 +480,32 @@ TRACE_EVENT(mm_vmscan_lru_shrink_inactive,
 		__entry->nr_activate0 = stat->nr_activate[0];
 		__entry->nr_activate1 = stat->nr_activate[1];
 		__entry->nr_ref_keep = stat->nr_ref_keep;
+		__entry->nr_ref_activate = stat->nr_ref_activate;
 		__entry->nr_unmap_fail = stat->nr_unmap_fail;
 		__entry->priority = priority;
-		__entry->reclaim_flags = trace_reclaim_flags(file);
+		__entry->lru = lru;
+		__entry->reason = reason;
 	),
 
-	TP_printk("nid=%d nr_scanned=%ld nr_reclaimed=%ld nr_dirty=%ld nr_writeback=%ld nr_congested=%ld nr_immediate=%ld nr_activate_anon=%d nr_activate_file=%d nr_ref_keep=%ld nr_unmap_fail=%ld priority=%d flags=%s",

include/trace/events/vmscan.h TRACE_EVENT(mm_vmscan_lru_shrink_inactive, · line 496

Quoted 10 lines
include/trace/events/vmscan.h +2 -3
+	TP_printk("nid=%d nr_scanned=%ld nr_reclaimed=%ld nr_dirty=%ld nr_writeback=%ld nr_congested=%ld nr_immediate=%ld nr_activate_anon=%d nr_activate_file=%d nr_ref_keep=%ld nr_ref_activate=%ld nr_unmap_fail=%ld priority=%d lru=%s reason=%s",
 		__entry->nid,
 		__entry->nr_scanned, __entry->nr_reclaimed,
 		__entry->nr_dirty, __entry->nr_writeback,
 		__entry->nr_congested, __entry->nr_immediate,
 		__entry->nr_activate0, __entry->nr_activate1,
-		__entry->nr_ref_keep, __entry->nr_unmap_fail,
-		__entry->priority,
-		show_reclaim_flags(__entry->reclaim_flags))
+		__entry->nr_ref_keep, __entry->nr_ref_keep,

[Severity: Medium]
Is the second __entry->nr_ref_keep here a typo?

It looks like it should be __entry->nr_ref_activate to match the format string. The duplicated nr_ref_keep argument causes the correct nr_ref_activate field to be ignored in the trace output.

[Severity: Medium]
Does the Documentation/trace/postprocess/trace-vmscan-postprocess.pl script also need to be updated to handle this new format?

The script contains a hardcoded regular expression for this tracepoint:

$regex_lru_shrink_inactive = generate_traceevent_regex(

		"vmscan/mm_vmscan_lru_shrink_inactive",
		$regex_lru_shrink_inactive_default,
		"nid", "nr_scanned", "nr_reclaimed", "nr_dirty", "nr_writeback",
		"nr_congested", "nr_immediate", "nr_activate_anon",
		"nr_activate_file", "nr_ref_keep",
		"nr_unmap_fail", "priority", "flags");

Since flags was renamed to lru, and reason and nr_ref_activate were added, the parsing logic in the script will fail on traces using this new format.

Quoted 4 lines
include/trace/events/vmscan.h +3 -0
+		__entry->nr_unmap_fail, __entry->priority,
+		__print_symbolic(__entry->lru, LRU_NAMES),
+		__print_symbolic(__entry->reason, trace_reclaim_reason_ops))
 );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to qiwu.chen(陈启武)

include/trace/events/vmscan.h TRACE_EVENT(mm_vmscan_write_folio, · line 413

Quoted 9 lines · qiwu.chen(陈启武)
include/trace/events/vmscan.h +3 -4
-	TP_printk("page=%p pfn=0x%lx flags=%s",
-		pfn_to_page(__entry->pfn),
-		__entry->pfn,
-		show_reclaim_flags(__entry->reclaim_flags))
+	TP_printk("folio=%p lru=%s",
+		__entry->folio,
+		__print_symbolic(__entry->lru, LRU_NAMES))
 );

Why is it useful to print out the folio pointer? Printing out the PFN made more sense to me.