[PATCH 18/22] mm/compaction: restrict compaction to common memory

From: Gregory Price (Meta)

Date: Sat Nov 14 2026 - 02:12:47 EST


Compaction serves the ordinary allocation domain.

Restrict it to N_MEMORY_COMMON:
- start kcompactd only on common nodes, at boot and hotplug.
- walk only common zones in direct compaction and when deciding
whether more reclaim could let compaction succeed.
- compact only common nodes for /proc/sys/vm/compact_memory,
and trigger proactive compaction only on common nodes.

The fallback zonelists hold only common memory, and all memory is common
at this point, so behavior is unchanged.

Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/compaction.c | 14 +++++++-------
mm/memory_hotplug.c | 3 ++-
2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/mm/compaction.c b/mm/compaction.c
index 426563acf7acf..6a40e1a9ba78c 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -2496,8 +2496,8 @@ bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
* Make sure at least one zone would pass __compaction_suitable if we continue
* retrying the reclaim.
*/
- for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
- ac->highest_zoneidx, ac->nodemask) {
+ for_each_zone_node_state(zone, z, ac->zonelist, ac->highest_zoneidx,
+ ac->nodemask, N_MEMORY_COMMON) {
unsigned long available;

if (cpusets_enabled() && (alloc_flags & ALLOC_CPUSET) &&
@@ -2855,8 +2855,8 @@ enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);

/* Compact each zone in the list */
- for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
- ac->highest_zoneidx, ac->nodemask) {
+ for_each_zone_node_state(zone, z, ac->zonelist, ac->highest_zoneidx,
+ ac->nodemask, N_MEMORY_COMMON) {
enum compact_result status;

if (cpusets_enabled() &&
@@ -2973,7 +2973,7 @@ static int compact_nodes(void)
/* Flush pending updates to the LRU lists */
lru_add_drain_all();

- for_each_online_node(nid) {
+ for_each_node_state(nid, N_MEMORY_COMMON) {
ret = compact_node(NODE_DATA(nid), false);
if (ret)
return ret;
@@ -2992,7 +2992,7 @@ static int compaction_proactiveness_sysctl_handler(const struct ctl_table *table
return rc;

if (write && sysctl_compaction_proactiveness) {
- for_each_online_node(nid) {
+ for_each_node_state(nid, N_MEMORY_COMMON) {
pg_data_t *pgdat = NODE_DATA(nid);

if (pgdat->proactive_compact_trigger)
@@ -3366,7 +3366,7 @@ static int __init kcompactd_init(void)
{
int nid;

- for_each_node_state(nid, N_MEMORY)
+ for_each_node_state(nid, N_MEMORY_COMMON)
kcompactd_run(nid);
register_sysctl_init("vm", vm_compaction);
return 0;
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 410a29732bae7..c8ff60e6417ba 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -1209,7 +1209,8 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,

if (node_state(nid, N_MEMORY_COMMON))
kswapd_run(nid);
- kcompactd_run(nid);
+ if (node_state(nid, N_MEMORY_COMMON))
+ kcompactd_run(nid);

if (node_arg.nid >= 0)
/* First memory added successfully. Notify consumers. */
--
2.53.1-Meta