[PATCH v2 3/3] mm: memblock: synchronize debugfs reads with memory hotplug
From: Meijing Zhao
Date: Thu Aug 20 2026 - 03:52:21 EST
From: Meijing Zhao <zhaomeijing@xxxxxxxxxxx>
memblock_debug_show() walks a memblock region array without
synchronization. With CONFIG_ARCH_KEEP_MEMBLOCK, memory hotplug can
concurrently add a region. If the array has to grow,
memblock_double_array() replaces type->regions and frees the old
allocation while the debugfs reader may still be using it.
Hold mem_hotplug_lock in read mode while producing the debugfs output.
Memory hotplug updates already hold the write side of this lock, so the
region array remains stable throughout the walk. The helpers are no-ops
when CONFIG_MEMORY_HOTPLUG is disabled.
Fixes: f9126ab9241f ("memory-hotplug: fix wrong edge when hot add a new node")
Signed-off-by: Meijing Zhao <zhaomeijing@xxxxxxxxxxx>
---
mm/memblock.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/memblock.c b/mm/memblock.c
index 7eddcc412618..620a5dd7f5f8 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -14,6 +14,7 @@
#include <linux/pfn.h>
#include <linux/debugfs.h>
#include <linux/kmemleak.h>
+#include <linux/memory_hotplug.h>
#include <linux/seq_file.h>
#include <linux/memblock.h>
#include <linux/mutex.h>
@@ -2916,6 +2917,7 @@ static int memblock_debug_show(struct seq_file *m, void *private)
phys_addr_t end;
bool first;
+ get_online_mems();
for (i = 0; i < type->cnt; i++) {
reg = &type->regions[i];
end = reg->base + reg->size - 1;
@@ -2944,6 +2946,7 @@ static int memblock_debug_show(struct seq_file *m, void *private)
seq_puts(m, "NONE\n");
}
}
+ put_online_mems();
return 0;
}
DEFINE_SHOW_ATTRIBUTE(memblock_debug);
--
2.25.1