[PATCH 44/60] mm: add N_MEMORY_COMPACTION node state

From: Gregory Price via B4 Relay

Date: Fri Nov 20 2026 - 13:19:01 EST


From: "Gregory Price (Meta)" <gourry@xxxxxxxxxx>

Add N_MEMORY_COMPACTION for the nodes compaction may operate on, and
NODE_MEMORY_FEAT_COMPACTION for memory providers to select it.

Common memory carries every feature, so a mask with
NODE_MEMORY_FEAT_COMMON must now include compaction.

The state is internal and has no sysfs file. Internal states follow
N_GENERIC_INITIATOR, so check sysfs coverage only for the states up to
it.

Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
Documentation/mm/physical_memory.rst | 7 +++++++
drivers/base/node.c | 5 +++--
include/linux/nodemask.h | 20 ++++++++++++++++----
mm/page_alloc.c | 4 ++++
4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/Documentation/mm/physical_memory.rst b/Documentation/mm/physical_memory.rst
index ddba7c890418..5d0a69f6c23c 100644
--- a/Documentation/mm/physical_memory.rst
+++ b/Documentation/mm/physical_memory.rst
@@ -182,6 +182,12 @@ nodes with particular properties as defined by ``enum node_states``:
``N_GENERIC_INITIATOR``
The node has one or more Generic Initiators

+The following states are internal and not exported through sysfs:
+
+``N_MEMORY_COMPACTION``
+ The kernel compacts the node on its own initiative (kcompactd, proactive
+ and explicit compaction).
+
For each node that has a property described above, the bit corresponding to the
node ID in the ``node_states[<property>]`` bitmask is set.

@@ -194,6 +200,7 @@ For example, for node 2 with normal memory and CPUs, bit 2 will be set in ::
node_states[N_MEMORY]
node_states[N_MEMORY_COMMON]
node_states[N_CPU]
+ node_states[N_MEMORY_COMPACTION]

For various operations possible with nodemasks please refer to
``include/linux/nodemask.h``.
diff --git a/drivers/base/node.c b/drivers/base/node.c
index 56a1741a6a45..19f93cda90be 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -1000,8 +1000,9 @@ void __init node_dev_init(void)
{
int ret, i;

- BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
- BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
+ /* Node states after N_GENERIC_INITIATOR have no sysfs file. */
+ BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != N_GENERIC_INITIATOR + 1);
+ BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != N_GENERIC_INITIATOR + 1);

ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
if (ret)
diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index 02908ef23a62..2757acffff2b 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -411,6 +411,8 @@ enum node_states {
N_MEMORY_PRIVATE, /* memory outside the common pool */
N_CPU, /* The node has one or more cpus */
N_GENERIC_INITIATOR, /* The node has one or more Generic Initiators */
+ /* Internal states, not exported through sysfs */
+ N_MEMORY_COMPACTION, /* mm compaction may operate on the node */
NR_NODE_STATES
};

@@ -542,16 +544,26 @@ static __always_inline int node_random(const nodemask_t *maskp)
#define for_each_online_node(node) for_each_node_state(node, N_ONLINE)
#define for_each_node_with_cpus(node) for_each_node_state(node, N_CPU)

-/* Private nodes omit NODE_MEMORY_FEAT_COMMON. */
+/*
+ * Private nodes omit NODE_MEMORY_FEAT_COMMON. Their providers opt into
+ * individual MM services with the remaining bits.
+ */
#define NODE_MEMORY_FEAT_COMMON BIT(0) /* add to fallback zonelist */
-#define NODE_MEMORY_FEAT_ALL NODE_MEMORY_FEAT_COMMON
+#define NODE_MEMORY_FEAT_COMPACTION BIT(1) /* mm compaction */
+#define NODE_MEMORY_FEAT_ALL (NODE_MEMORY_FEAT_COMMON | \
+ NODE_MEMORY_FEAT_COMPACTION)

-/* Private memory needs NUMA: node 0 holds all memory without it. */
+/*
+ * Common memory carries every feature. Private memory needs NUMA: node 0
+ * holds all memory without it.
+ */
static inline bool node_memory_features_valid(unsigned long features)
{
if (features & ~NODE_MEMORY_FEAT_ALL)
return false;
- return IS_ENABLED(CONFIG_NUMA) || (features & NODE_MEMORY_FEAT_COMMON);
+ if (features & NODE_MEMORY_FEAT_COMMON)
+ return features == NODE_MEMORY_FEAT_ALL;
+ return IS_ENABLED(CONFIG_NUMA);
}

/*
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index b7339bc634d5..335ce73e645e 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -204,6 +204,7 @@ nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
#endif
[N_MEMORY] = { { [0] = 1UL } },
[N_MEMORY_COMMON] = { { [0] = 1UL } },
+ [N_MEMORY_COMPACTION] = { { [0] = 1UL } },
[N_CPU] = { { [0] = 1UL } },
#endif /* NUMA */
};
@@ -218,10 +219,13 @@ void __meminit node_set_memory_state(int nid, unsigned long features)
node_set_state(nid, N_MEMORY_COMMON);
else
node_set_state(nid, N_MEMORY_PRIVATE);
+ if (features & NODE_MEMORY_FEAT_COMPACTION)
+ node_set_state(nid, N_MEMORY_COMPACTION);
}

void __meminit node_clear_memory_state(int nid)
{
+ node_clear_state(nid, N_MEMORY_COMPACTION);
node_clear_state(nid, N_MEMORY_PRIVATE);
node_clear_state(nid, N_MEMORY_COMMON);
node_clear_state(nid, N_MEMORY);

--
2.56.1