[RFC PATCH V3 13/17] mm/vmstat: Add vmstat counters

Raghavendra K T
2025-08-14T15:35:56Z

Add vmstat counter to track scanning, migration and
type of pages.

Signed-off-by: Raghavendra K T <raghavendra.kt@amd.com>
---
include/linux/mm.h | 13 ++++++++
include/linux/vm_event_item.h | 12 +++++++
mm/kscand.c | 63 +++++++++++++++++++++++++++++++++--
mm/vmstat.c | 12 +++++++
4 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index fa538feaa8d9..0d579d0294bf 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -655,6 +655,19 @@ struct vm_operations_struct {
unsigned long addr);
};

+#ifdef CONFIG_KSCAND
+void count_kscand_mm_scans(void);
+void count_kscand_vma_scans(void);
+void count_kscand_migadded(void);
+void count_kscand_migrated(void);
+void count_kscand_migrate_failed(void);
+void count_kscand_slowtier(void);
+void count_kscand_toptier(void);
+void count_kscand_idlepage(void);
+void count_kscand_hotpage(void);
+void count_kscand_coldpage(void);
+#endif
+
#ifdef CONFIG_NUMA_BALANCING
static inline void vma_numab_state_init(struct vm_area_struct *vma)
{
diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index 9e15a088ba38..b5643be5dd94 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -67,6 +67,18 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
NUMA_HINT_FAULTS_LOCAL,
NUMA_PAGE_MIGRATE,
#endif
+#ifdef CONFIG_KSCAND
+ KSCAND_MM_SCANS,
+ KSCAND_VMA_SCANS,
+ KSCAND_MIGADDED,
+ KSCAND_MIGRATED,
+ KSCAND_MIGRATE_FAILED,
+ KSCAND_SLOWTIER,
+ KSCAND_TOPTIER,
+ KSCAND_IDLEPAGE,
+ KSCAND_HOTPAGE,
+ KSCAND_COLDPAGE,
+#endif
#ifdef CONFIG_MIGRATION
PGMIGRATE_SUCCESS, PGMIGRATE_FAIL,
THP_MIGRATION_SUCCESS,
diff --git a/mm/kscand.c b/mm/kscand.c
index a73606f7ca3c..e14645565ba7 100644
--- a/mm/kscand.c
+++ b/mm/kscand.c
@@ -339,6 +339,47 @@ struct attribute_group kscand_attr_group = {
};
#endif

+void count_kscand_mm_scans(void)
+{
+ count_vm_numa_event(KSCAND_MM_SCANS);
+}
+void count_kscand_vma_scans(void)
+{
+ count_vm_numa_event(KSCAND_VMA_SCANS);
+}
+void count_kscand_migadded(void)
+{
+ count_vm_numa_event(KSCAND_MIGADDED);
+}
+void count_kscand_migrated(void)
+{
+ count_vm_numa_event(KSCAND_MIGRATED);
+}
+void count_kscand_migrate_failed(void)
+{
+ count_vm_numa_event(KSCAND_MIGRATE_FAILED);
+}
+void count_kscand_slowtier(void)
+{
+ count_vm_numa_event(KSCAND_SLOWTIER);
+}
+void count_kscand_toptier(void)
+{
+ count_vm_numa_event(KSCAND_TOPTIER);
+}
+void count_kscand_idlepage(void)
+{
+ count_vm_numa_event(KSCAND_IDLEPAGE);
+}
+void count_kscand_hotpage(void)
+{
+ count_vm_numa_event(KSCAND_HOTPAGE);
+}
+void count_kscand_coldpage(void)
+{
+ count_vm_numa_event(KSCAND_COLDPAGE);
+}
+
static inline int kscand_has_work(void)
{
return !list_empty(&kscand_scan.mm_head);
@@ -653,6 +694,8 @@ static int kmigrated_promote_folio(struct kscand_migrate_info *info,

if (!is_hot_page(folio))
return KSCAND_NOT_HOT_PAGE;
+ else
+ count_kscand_hotpage();

folio_get(folio);

@@ -803,12 +846,15 @@ static int hot_vma_idle_pte_entry(pte_t *pte,
}

if (!kscand_eligible_srcnid(srcnid)) {
+ count_kscand_toptier();
if (folio_test_young(folio) || folio_test_referenced(folio)
|| pte_young(pteval)) {
scanctrl->nodeinfo[srcnid]->nr_accessed++;
}
folio_put(folio);
return 0;
+ } else {
+ count_kscand_slowtier();
}
if (!folio_test_idle(folio) && !prev_idle &&
(folio_test_young(folio) || folio_test_referenced(folio))) {
@@ -820,7 +866,14 @@ static int hot_vma_idle_pte_entry(pte_t *pte,
info->pfn = folio_pfn(folio);
info->address = addr;
list_add_tail(&info->migrate_node, &scanctrl->scan_list);
+ count_kscand_migadded();
}
+ folio_put(folio);
+ return 0;
+ } else {
+ if (prev_idle)
+ count_kscand_coldpage();
+ count_kscand_idlepage();
}

folio_put(folio);
@@ -1045,10 +1098,13 @@ static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot)
mstat_counter--;

/* TBD: encode migrated count here, currently assume folio_nr_pages */
- if (!ret)
+ if (!ret) {
+ count_kscand_migrated();
msuccess++;
- else
+ } else {
+ count_kscand_migrate_failed();
mfailed++;
+ }

kfree(info);

@@ -1269,6 +1325,7 @@ static unsigned long kscand_scan_mm_slot(void)

for_each_vma(vmi, vma) {
kscand_walk_page_vma(vma, &kscand_scanctrl);
+ count_kscand_vma_scans();
vma_scanned_size += vma->vm_end - vma->vm_start;

if (vma_scanned_size >= mm_slot_scan_size ||
@@ -1304,6 +1361,8 @@ static unsigned long kscand_scan_mm_slot(void)

update_mmslot_info = true;

+ count_kscand_mm_scans();
+
total = get_slowtier_accesed(&kscand_scanctrl);
target_node = get_target_node(&kscand_scanctrl);

diff --git a/mm/vmstat.c b/mm/vmstat.c
index a78d70ddeacd..5ba82d2ffe71 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1347,6 +1347,18 @@ const char * const vmstat_text[] = {
"numa_hint_faults_local",
"numa_pages_migrated",
#endif
+#ifdef CONFIG_KSCAND
+ "nr_kscand_mm_scans",
+ "nr_kscand_vma_scans",
+ "nr_kscand_migadded",
+ "nr_kscand_migrated",
+ "nr_kscand_migrate_failed",
+ "nr_kscand_slowtier",
+ "nr_kscand_toptier",
+ "nr_kscand_idlepage",
+ "nr_kscand_hotpage",
+ "nr_kscand_coldpage",
+#endif
#ifdef CONFIG_MIGRATION
"pgmigrate_success",
"pgmigrate_fail",
--
2.34.1