[PATCH 01/22] mm: add node-state-filtered zone and node iterators

From: Gregory Price (Meta)

Date: Sat Nov 14 2026 - 02:10:30 EST


Add node-state-filter helper functions/macros so MM walkers can select
their node eligibility explicitly.

Add nodemask intersection and nodemask/state iterators using the bitmap
intersection helpers, without allocating a temporary nodemask.

Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
include/linux/mmzone.h | 46 ++++++++++++++++++++++++++++++++++++++++
include/linux/nodemask.h | 28 ++++++++++++++++++++----
2 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index c161d563ce8fd..5ff190d870830 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -1845,6 +1845,13 @@ extern struct zone *next_zone(struct zone *zone);
; /* do nothing */ \
else

+/* Populated zones on nodes in @state. */
+#define for_each_populated_zone_state(zone, state) \
+ for_each_populated_zone(zone) \
+ if (!node_state(zone_to_nid(zone), state)) \
+ ; /* do nothing */ \
+ else
+
static inline struct zone *zonelist_zone(struct zoneref *zoneref)
{
return zoneref->zone;
@@ -1937,6 +1944,45 @@ static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
zone = zonelist_zone(z))


+static inline struct zoneref *
+next_zones_zonelist_state(struct zoneref *z, enum zone_type highest_zoneidx,
+ const nodemask_t *nodes, enum node_states state)
+{
+ z = next_zones_zonelist(z, highest_zoneidx, nodes);
+ while (zonelist_zone(z) && !node_state(zonelist_node_idx(z), state))
+ z = next_zones_zonelist(++z, highest_zoneidx, nodes);
+ return z;
+}
+
+static inline struct zoneref *
+first_zones_zonelist_state(struct zonelist *zonelist,
+ enum zone_type highest_zoneidx,
+ const nodemask_t *nodes, enum node_states state)
+{
+ return next_zones_zonelist_state(zonelist->_zonerefs, highest_zoneidx,
+ nodes, state);
+}
+
+/**
+ * for_each_zone_node_state - iterate zones in intersect(nodemask, node_state(state))
+ * @zone: The current zone in the iterator
+ * @z: The current pointer within zonelist->_zonerefs being iterated
+ * @zlist: The zonelist being iterated
+ * @highidx: The zone index of the highest zone to return
+ * @nodemask: Nodemask allowed by the allocator
+ * @state: The node state the zone's node must be in
+ *
+ * The state filter is part of the iteration rather than a trailing if, so that
+ * this expands to a single for statement: a macro ending in an unbraced if
+ * silently steals a following else from the caller.
+ */
+#define for_each_zone_node_state(zone, z, zlist, highidx, nodemask, state) \
+ for (z = first_zones_zonelist_state(zlist, highidx, nodemask, state), \
+ zone = zonelist_zone(z); \
+ zone; \
+ z = next_zones_zonelist_state(++z, highidx, nodemask, state), \
+ zone = zonelist_zone(z))
+
/**
* for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
* @zone: The current zone in the iterator
diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index 607373bd83ee6..28a456c3f5964 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -386,6 +386,10 @@ static __always_inline void __nodes_fold(nodemask_t *dstp, const nodemask_t *ori
for ((node) = 0; (node) < 1 && !nodes_empty(mask); (node)++)
#endif /* MAX_NUMNODES */

+/* Iterate the nodes present in both masks without a temporary nodemask. */
+#define for_each_node_mask_and(node, mask1, mask2) \
+ for_each_and_bit(node, nodes_addr(mask1), nodes_addr(mask2), MAX_NUMNODES)
+
/*
* Bitmasks that are kept for all the nodes.
*/
@@ -411,6 +415,10 @@ enum node_states {

extern nodemask_t node_states[NR_NODE_STATES];

+/* Iterate the nodes in a mask that also belong to the specified state. */
+#define for_each_node_mask_state(node, mask, state) \
+ for_each_node_mask_and(node, mask, node_states[state])
+
#if MAX_NUMNODES > 1
static __always_inline int node_state(int node, enum node_states state)
{
@@ -435,15 +443,21 @@ static __always_inline int num_node_state(enum node_states state)
#define for_each_node_state(__node, __state) \
for_each_node_mask((__node), node_states[__state])

-#define first_online_node first_node(node_states[N_ONLINE])
-#define first_memory_node first_node(node_states[N_MEMORY])
+#define first_node_state(state) first_node(node_states[state])
+static __always_inline unsigned int next_node_state(int nid, enum node_states state)
+{
+ return next_node(nid, node_states[state]);
+}
+
+#define first_online_node first_node_state(N_ONLINE)
+#define first_memory_node first_node_state(N_MEMORY)
static __always_inline unsigned int next_online_node(int nid)
{
- return next_node(nid, node_states[N_ONLINE]);
+ return next_node_state(nid, N_ONLINE);
}
static __always_inline unsigned int next_memory_node(int nid)
{
- return next_node(nid, node_states[N_MEMORY]);
+ return next_node_state(nid, N_MEMORY);
}

static __always_inline void node_set_online(int nid)
@@ -481,6 +495,12 @@ static __always_inline int num_node_state(enum node_states state)
#define for_each_node_state(node, __state) \
for ( (node) = 0; (node) == 0; (node) = 1)

+#define first_node_state(state) 0
+static __always_inline unsigned int next_node_state(int nid, enum node_states state)
+{
+ return MAX_NUMNODES;
+}
+
#define first_online_node 0
#define first_memory_node 0
#define next_online_node(nid) (MAX_NUMNODES)
--
2.53.1-Meta