mm/memory_hotplug: factor out node_is_memoryless()
Subsystems (2)
PATCH 1/1 mm/memory_hotplug: factor out node_is_memoryless()
A memoryless node neither spans present pages (populated or ZONE_DEVICE) nor has an offline-but-added memory block still linked to it in sysfs.
try_offline_node() presently open-codes this memoryless check.
Pull that into a node_is_memoryless() helper and pull the existing check_no_memblock_for_node_cb() helper ahead of the add/online path so it's clearer what is happening here.
No functional change.
--- mm/memory_hotplug.c | 60 +++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-)
mm/memory_hotplug.c +31 -29
@@ -1491,6 +1491,36 @@ static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,return ret; } +static int check_no_memblock_for_node_cb (struct memory_block *mem,void *arg) +{ +int nid = *(int *)arg; + +/* +* If a memory block belongs to multiple nodes, the stored nid is not +* reliable. However, such blocks are always online (e.g., cannot get +* offlined) and, therefore, are still spanned by the node. +*/ +return mem->nid == nid ? -EEXIST :0 ; +} + +/* Caller must hold the memory hotplug lock for this check. */ +static bool node_is_memoryless (int nid) +{ +/* +* A node still spanning pages (especially ZONE_DEVICE) is not +* memoryless. A node spans memory after move_pfn_range_to_zone(), +* e.g. once a memory block has been onlined. +*/ +if (node_spanned_pages (nid)) +return false; +/* +* Offline memory blocks may not be spanned by the node yet, but they +* link to it in sysfs and can be onlined later, so the node is not +* memoryless while any remain. +*/ +return !for_each_memory_block (&nid, check_no_memblock_for_node_cb); +} +/* * NOTE: The caller must call lock_device_hotplug() to serialize hotplug * and online/offline operations (triggered e.g. by sysfs).
@@ -2214,18 +2244,6 @@ static int check_cpu_on_node(int nid)return 0 ; } -static int check_no_memblock_for_node_cb (struct memory_block *mem,void *arg) -{ -int nid = *(int *)arg; - -/* -* If a memory block belongs to multiple nodes, the stored nid is not -* reliable. However, such blocks are always online (e.g., cannot get -* offlined) and, therefore, are still spanned by the node. -*/ -return mem->nid == nid ? -EEXIST :0 ; -} -/** * try_offline_node * @nid: the node ID
@@ -2237,23 +2255,7 @@ static int check_no_memblock_for_node_cb(struct memory_block *mem, void *arg)*/ void try_offline_node (int nid) { -int rc; - -/* -* If the node still spans pages (especially ZONE_DEVICE), don't -* offline it. A node spans memory after move_pfn_range_to_zone(), -* e.g., after the memory block was onlined. -*/ -if (node_spanned_pages (nid)) -return ; - -/* -* Especially offline memory blocks might not be spanned by the -* node. They will get spanned by the node once they get onlined. -* However, they link to the node in sysfs and can get onlined later. -*/ - rc =for_each_memory_block (&nid, check_no_memblock_for_node_cb); -if (rc) +if (!node_is_memoryless (nid))return ;if (check_cpu_on_node (nid))
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Low] Unused function warnings when CONFIG_MEMORY_HOTREMOVE is disabled.
Pre-existing issues:
- [High] Data race on node_spanned_pages between ACPI CPU hot-remove and ZONE_DEVICE memory hotplug due to missing memory hotplug lock. --
commit 239875f97a26527846083c3d6ab82ee6b800ce0a
Author: Gregory Price <[email protected]>
mm/memory_hotplug: factor out node_is_memoryless()
A memoryless node neither spans present pages (populated or ZONE_DEVICE) nor has an offline-but-added memory block still linked to it in sysfs.
try_offline_node() presently open-codes this memoryless check.
Pull that into a node_is_memoryless() helper and pull the existing check_no_memblock_for_node_cb() helper ahead of the add/online path so it's clearer what is happening here.
Quoted 19 lines
mm/memory_hotplug.c +11
-0@@ -1491,6 +1491,36 @@ static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,return ret; } +static int check_no_memblock_for_node_cb (struct memory_block *mem,void *arg) +{ +int nid = *(int *)arg; + +/* +* If a memory block belongs to multiple nodes, the stored nid is not +* reliable. However, such blocks are always online (e.g., cannot get +* offlined) and, therefore, are still spanned by the node. +*/ +return mem->nid == nid ? -EEXIST :0 ; +}
mm/memory_hotplug.c static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group, · line 1514
Quoted 10 lines
mm/memory_hotplug.c +10
-0+ +/* Caller must hold the memory hotplug lock for this check. */ +static bool node_is_memoryless (int nid) +{ +/* +* A node still spanning pages (especially ZONE_DEVICE) is not +* memoryless. A node spans memory after move_pfn_range_to_zone(), +* e.g. once a memory block has been onlined. +*/ +if (node_spanned_pages (nid))
[Severity: High]
This is a pre-existing issue and was not introduced by this patch, but can this lockless read of node_spanned_pages() race with ZONE_DEVICE memory hotplug?
The comment explicitly requires the memory hotplug lock, but the ACPI CPU hot-remove path in drivers/acpi/acpi_processor.c:acpi_processor_post_eject() calls try_offline_node() while holding only device_hotplug_lock():
drivers/acpi/acpi_processor.c:acpi_processor_post_eject() {
...
cpus_write_unlock();
cpu_maps_update_done();
try_offline_node(cpu_to_node(pr->id));
}Since ZONE_DEVICE memory hotplug (via memremap_pages()) modifies node_spanned_pages under mem_hotplug_lock() without holding device_hotplug_lock(), could this race result in a NUMA node with active ZONE_DEVICE memory being erroneously offlined and unregistered from sysfs?
mm/memory_hotplug.c static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group, · line 1522
Quoted 8 lines
mm/memory_hotplug.c +8
-0+return false; +/* +* Offline memory blocks may not be spanned by the node yet, but they +* link to it in sysfs and can be onlined later, so the node is not +* memoryless while any remain. +*/ +return !for_each_memory_block (&nid, check_no_memblock_for_node_cb); +}
[Severity: Low]
Does moving check_no_memblock_for_node_cb() and node_is_memoryless() outside of the #ifdef CONFIG_MEMORY_HOTREMOVE block cause -Wunused-function warnings when CONFIG_MEMORY_HOTREMOVE is disabled?
Their only caller, try_offline_node(), is conditionally compiled inside that block, so these helpers would be unused in builds without it.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 36 lines · Gregory Price
A memoryless node neither spans present pages (populated or
ZONE_DEVICE) nor has an offline-but-added memory block still linked to it in sysfs.
try_offline_node()presently open-codes this memoryless check.Pull that into a
node_is_memoryless()helper and pull the existingcheck_no_memblock_for_node_cb()helper ahead of the add/online path so it's clearer what is happening here.No functional change.
Signed-off-by Gregory Price <[email protected]>--- mm/memory_hotplug.c | 60 +++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-)mm/memory_hotplug.c +12
-0@@ -1491,6 +1491,36 @@ static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group,return ret; } +static int check_no_memblock_for_node_cb (struct memory_block *mem,void *arg) +{ +int nid = *(int *)arg; + +/* +* If a memory block belongs to multiple nodes, the stored nid is not +* reliable. However, such blocks are always online (e.g., cannot get +* offlined) and, therefore, are still spanned by the node. +*/ +return mem->nid == nid ? -EEXIST :0 ; +} +
mm/memory_hotplug.c static int create_altmaps_and_memory_blocks(int nid, struct memory_group *group, · line 1515
Quoted 10 lines
mm/memory_hotplug.c +10
-0+/* Caller must hold the memory hotplug lock for this check. */ +static bool node_is_memoryless (int nid) +{ +/* +* A node still spanning pages (especially ZONE_DEVICE) is not +* memoryless. A node spans memory after move_pfn_range_to_zone(), +* e.g. once a memory block has been onlined. +*/ +if (node_spanned_pages (nid)) +return false;
browsing sashiko feedback:
[Severity: High]
This is a pre-existing issue and was not introduced by this patch, but can this lockless read of node_spanned_pages() race with ZONE_DEVICE memory hotplug?
---
This seems legit and worth addressing (other notes are addressible as well, but this is maybe noteworthy).
I actually have some old patches sandbagged that tried to marry the ZONE_DEVICE hotplug pattern through mm/memory_hotplug.c rather than its separare entry-point. Might be worth a revisit.
I don't know that I want to predicate this particular fix on this patch but it's worth a think.
~Gregory
Quoted 5 lines · Gregory Price
A memoryless node neither spans present pages (populated or
ZONE_DEVICE) nor has an offline-but-added memory block still linked to it in sysfs.
try_offline_node()presently open-codes this memoryless check.
Quoted 8 lines
Pull that into a
node_is_memoryless()helper and pull the existingcheck_no_memblock_for_node_cb()helper ahead of the add/online path so it's clearer what is happening here.No functional change.
Signed-off-by Gregory Price <[email protected]>---
Signature · 3 lines
Cheers, David