[RFC 4/6] mm: avoid show invalid zone in /proc/zoneinfo

From: Huang Ying
Date: Thu May 11 2023 - 02:57:42 EST


In the following patch of the series, one or multiple zone instances
may be created for one zone type. So the total number of zone
instances of a node becomes dynamic based on system configurations.
So, the real zone instance number instead of static max zone number is
used to show zone information in /proc/zoneinfo.

Signed-off-by: "Huang, Ying" <ying.huang@xxxxxxxxx>
Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxx>
Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: Johannes Weiner <jweiner@xxxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxx>
Cc: Pavel Tatashin <pasha.tatashin@xxxxxxxxxx>
Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx>
---
include/linux/mmzone.h | 5 +++++
mm/vmstat.c | 3 ++-
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 0c569c5fa0d1..18d64cf1263c 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -1809,6 +1809,11 @@ static inline int last_zone_idx(pg_data_t *pgdat, enum zone_type zt)
return pgdat->node_zone_types[zt].last_zone_idx;
}

+static inline int last_zone_idx_pgdat(pg_data_t *pgdat)
+{
+ return last_zone_idx(pgdat, MAX_NR_ZONE_TYPES - 1);
+}
+
#ifdef CONFIG_SPARSEMEM
#include <asm/sparsemem.h>
#endif
diff --git a/mm/vmstat.c b/mm/vmstat.c
index 1ea6a5ce1c41..bade3f50d1f8 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1441,8 +1441,9 @@ static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
struct zone *zone;
struct zone *node_zones = pgdat->node_zones;
unsigned long flags;
+ int last_zone_idx = last_zone_idx_pgdat(pgdat);

- for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
+ for (zone = node_zones; zone - node_zones <= last_zone_idx; ++zone) {
if (assert_populated && !populated_zone(zone))
continue;

--
2.39.2