mm/memory_hotplug: maintain N_NORMAL_MEMORY during hotplug
Subsystems (2)
PATCH v2 1/1 mm/memory_hotplug: maintain N_NORMAL_MEMORY during hotplug
N_NORMAL_MEMORY is initialized from zone population at boot, but memory hotplug currently only updates N_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating over N_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.
Restore N_NORMAL_MEMORY maintenance directly in online_pages() and offline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <= ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <= ZONE_NORMAL.
This restores the intended semantics without bringing back the old status_change_nid_normal notifier plumbing which was removed in 8d2882a8edb8.
Current users that benefit include list_lru, zswap, nfsd filecache, hugetlb_cgroup, and has_normal_memory sysfs reporting.
---
Changes: simplify the code. (Thanks Joshua and David)
--- mm/memory_hotplug.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
mm/memory_hotplug.c +20 -0
@@ -1209,6 +1209,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,if (node_arg.nid >=0 )node_set_state (nid,N_MEMORY ); +/* +* Check whether we are adding normal memory to the node for the first +* time. +*/ +if (!node_state (nid,N_NORMAL_MEMORY ) &&zone_idx (zone) <=ZONE_NORMAL ) +node_set_state (nid,N_NORMAL_MEMORY ); +if (need_zonelists_rebuild)build_all_zonelists (NULL );
@@ -1908,6 +1915,8 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,unsigned long flags;char *reason;int ret; +unsigned long normal_pages =0 ; +enum zone_type zt;/* * {on,off}lining is constrained to full memory sections (or more
@@ -2055,6 +2064,17 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,/* reinitialise watermarks and update pcp limits */ init_per_zone_wmark_min (); +/* +* Check whether this operation removes the last normal memory from +* the node. We do this before clearing N_MEMORY to avoid the possible +* transient "!N_MEMORY && N_NORMAL_MEMORY" state. +*/ +if (zone_idx (zone) <=ZONE_NORMAL ) { +for (zt =0 ; zt <=ZONE_NORMAL ; zt++) + normal_pages += pgdat->node_zones[zt].present_pages; +if (!normal_pages) +node_clear_state (node,N_NORMAL_MEMORY ); + }/* * Make sure to mark the node as memory-less before rebuilding the zone * list. Otherwise this node would still appear in the fallback lists.
Signature · 1 line
2.50.1
Quoted 11 lines · Hao Li
N_NORMAL_MEMORYis initialized from zone population at boot, but memory hotplug currently only updatesN_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating overN_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.Restore
N_NORMAL_MEMORYmaintenance directly inonline_pages()andoffline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <=ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <=ZONE_NORMAL.
Quoted 8 lines
This restores the intended semantics without bringing back the old
status_change_nid_normalnotifier plumbing which was removed in8d2882a8edb8.Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")
The Fixes: tag indeed looks correct to me.
By the way, MM needs an explicit Cc: [email protected] for backporting.
Since this commit was introduced in v6.17 and we have v6.18 as LTS, this should to be backported to v6.18.
Quoted 2 lines
Signed-off-by Hao Li <[email protected]>---
Otherwise looks good to me,
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 19 lines · Harry Yoo (Oracle)
Quoted 18 lines · Hao Li
N_NORMAL_MEMORYis initialized from zone population at boot, but memory hotplug currently only updatesN_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating overN_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.Restore
N_NORMAL_MEMORYmaintenance directly inonline_pages()andoffline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <=ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <=ZONE_NORMAL.This restores the intended semantics without bringing back the old
status_change_nid_normalnotifier plumbing which was removed in8d2882a8edb8.Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.
Quoted 8 lines
Quoted 1 line
Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")The Fixes: tag indeed looks correct to me.
By the way, MM needs an explicit Cc: [email protected] for backporting.
Since this commit was introduced in v6.17 and we have v6.18 as LTS, this should to be backported to v6.18.
I had gone back and forth on whether to add the Cc tag, but I'm happy to include it now!
Quoted 6 lines
Quoted 3 lines
Signed-off-by Hao Li <[email protected]>---
Otherwise looks good to me,
Reviewed-by Harry Yoo (Oracle) <[email protected]>
Thanks for the review!
Signature · 2 lines
Thanks, Hao
Andrew Morton
maintainerQuoted 7 lines · On Mon, 30 Mar 2026 17:34:21 +0800 Hao Li <[email protected]> wrote:
Quoted 7 lines
Quoted 4 lines
Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")The Fixes: tag indeed looks correct to me.
Quoted 7 lines
Quoted 4 lines
By the way, MM needs an explicit Cc: [email protected] for backporting.
Since this commit was introduced in v6.17 and we have v6.18 as LTS, this should to be backported to v6.18.
I had gone back and forth on whether to add the Cc tag, but I'm happy to include it now!
That's OK, I'm very cc:stable-vigilant. Thanks, I'll get this upstreamed during this -rc cycle.
I do like our cc:stable patches to include a clear description of the userspace-visible impacts of the bug. Two reasons:
- To explain to -stable maintainers why we're requesting the backport. If they care. Heck, and to explain to me why I'm doing this!
- To help out someone who is using an older kernel (there are many) who is dealing with a bug report. They're looking at this changelog thinking "hm, I wonder if this patch will fix that bug". Let's add a few words in there to help out with their pattern matching.
So, and not for the first time ;), what are the userspace-visible runtime effects of this bug?
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Mon, 30 Mar 2026 17:34:21 +0800 Hao Li <[email protected]> wrote:
Quoted 9 lines
Quoted 4 lines
Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")The Fixes: tag indeed looks correct to me.
By the way, MM needs an explicit Cc: [email protected] for backporting.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Since this commit was introduced in v6.17 and we have v6.18 as LTS, this should to be backported to v6.18.
I had gone back and forth on whether to add the Cc tag, but I'm happy to include it now!
That's OK, I'm very cc:stable-vigilant. Thanks, I'll get this upstreamed during this -rc cycle.
Thanks!
Quoted 4 lines
I do like our cc:stable patches to include a clear description of the userspace-visible impacts of the bug. Two reasons:
Quoted 8 lines
- To explain to -stable maintainers why we're requesting the backport. If they care. Heck, and to explain to me why I'm doing this!
- To help out someone who is using an older kernel (there are many) who is dealing with a bug report. They're looking at this changelog thinking "hm, I wonder if this patch will fix that bug". Let's add a few words in there to help out with their pattern matching.
Agreed, and thanks for spelling out the rationale!
Quoted 4 lines
So, and not for the first time ;), what are the userspace-visible runtime effects of this bug?
The most visible effect is that /sys/devices/system/node/has_normal_memory does not report a node even after that node has gained normal memory via hotplug.
Also, list_lru-based shrinkers can undercount objects on such a node and may skip reclaim on that node entirely, which can lead to a higher memory footprint than expected.
Feel free to fold the above into the changelog if useful :)
Signature · 2 lines
Thanks, Hao
Andrew Morton
maintainerQuoted 9 lines · On Tue, 31 Mar 2026 12:24:46 +0800 Hao Li <[email protected]> wrote:
The most visible effect is that
/sys/devices/system/node/has_normal_memorydoes not report a node even after that node has gained normal memory via hotplug.Also, list_lru-based shrinkers can undercount objects on such a node and may skip reclaim on that node entirely, which can lead to a higher memory footprint than expected.
Feel free to fold the above into the changelog if useful :)
Very useful. Added, thanks!
Quoted 15 lines · Hao Li
N_NORMAL_MEMORYis initialized from zone population at boot, but memory hotplug currently only updatesN_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating overN_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.Restore
N_NORMAL_MEMORYmaintenance directly inonline_pages()andoffline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <=ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <=ZONE_NORMAL.This restores the intended semantics without bringing back the old
status_change_nid_normalnotifier plumbing which was removed in8d2882a8edb8.
Quoted 5 lines
Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")Signed-off-by Hao Li <[email protected]>
Thanks!
Quoted 51 lines
---
Changes: simplify the code. (Thanks Joshua and David)--- mm/memory_hotplug.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)mm/memory_hotplug.c +20
-0@@ -1209,6 +1209,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,if (node_arg.nid >=0 )node_set_state (nid,N_MEMORY ); +/* +* Check whether we are adding normal memory to the node for the first +* time. +*/ +if (!node_state (nid,N_NORMAL_MEMORY ) &&zone_idx (zone) <=ZONE_NORMAL ) +node_set_state (nid,N_NORMAL_MEMORY ); +if (need_zonelists_rebuild)build_all_zonelists (NULL );@@ -1908,6 +1915,8 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,unsigned long flags;char *reason;int ret; +unsigned long normal_pages =0 ; +enum zone_type zt;/* * {on,off}lining is constrained to full memory sections (or more @@ -2055,6 +2064,17 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,/* reinitialise watermarks and update pcp limits */ init_per_zone_wmark_min (); +/* +* Check whether this operation removes the last normal memory from +* the node. We do this before clearing N_MEMORY to avoid the possible +* transient "!N_MEMORY && N_NORMAL_MEMORY" state. +*/ +if (zone_idx (zone) <=ZONE_NORMAL ) { +for (zt =0 ; zt <=ZONE_NORMAL ; zt++) + normal_pages += pgdat->node_zones[zt].present_pages; +if (!normal_pages) +node_clear_state (node,N_NORMAL_MEMORY ); + }/* * Make sure to mark the node as memory-less before rebuilding the zone * list. Otherwise this node would still appear in the fallback lists.
Quoted 15 lines · On Mon, 30 Mar 2026 11:57:49 +0800 Hao Li <[email protected]> wrote:
N_NORMAL_MEMORYis initialized from zone population at boot, but memory hotplug currently only updatesN_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating overN_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.Restore
N_NORMAL_MEMORYmaintenance directly inonline_pages()andoffline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <=ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <=ZONE_NORMAL.This restores the intended semantics without bringing back the old
status_change_nid_normalnotifier plumbing which was removed in8d2882a8edb8.
Quoted 5 lines
Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")Signed-off-by Hao Li <[email protected]>
LGTM, thanks for the new version Hao! I hope you have a great day : -)
Quoted 10 lines
---
Changes: simplify the code. (Thanks Joshua and David)--- mm/memory_hotplug.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)mm/memory_hotplug.c +0
-0
Quoted 11 lines · Hao Li
N_NORMAL_MEMORYis initialized from zone population at boot, but memory hotplug currently only updatesN_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating overN_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.Restore
N_NORMAL_MEMORYmaintenance directly inonline_pages()andoffline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <=ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <=ZONE_NORMAL.
Quoted 8 lines
This restores the intended semantics without bringing back the old
status_change_nid_normalnotifier plumbing which was removed in8d2882a8edb8.Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")
Agreed on CCing stable.
Quoted 39 lines
Signed-off-by Hao Li <[email protected]>---
Changes: simplify the code. (Thanks Joshua and David)--- mm/memory_hotplug.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)mm/memory_hotplug.c +10
-0@@ -1209,6 +1209,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,if (node_arg.nid >=0 )node_set_state (nid,N_MEMORY ); +/* +* Check whether we are adding normal memory to the node for the first +* time. +*/ +if (!node_state (nid,N_NORMAL_MEMORY ) &&zone_idx (zone) <=ZONE_NORMAL ) +node_set_state (nid,N_NORMAL_MEMORY ); +if (need_zonelists_rebuild)build_all_zonelists (NULL );@@ -1908,6 +1915,8 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,unsigned long flags;char *reason;int ret; +unsigned long normal_pages =0 ; +enum zone_type zt;/* * {on,off}lining is constrained to full memory sections (or more @@ -2055,6 +2064,17 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,/* reinitialise watermarks and update pcp limits */ init_per_zone_wmark_min (); +/*
mm/memory_hotplug.c int offline_pages(unsigned long start_pfn, unsigned long nr_pages, · line 2077
Quoted 10 lines
mm/memory_hotplug.c +10
-0+* Check whether this operation removes the last normal memory from +* the node. We do this before clearing N_MEMORY to avoid the possible +* transient "!N_MEMORY && N_NORMAL_MEMORY" state. +*/ +if (zone_idx (zone) <=ZONE_NORMAL ) { +for (zt =0 ; zt <=ZONE_NORMAL ; zt++) + normal_pages += pgdat->node_zones[zt].present_pages; +if (!normal_pages) +node_clear_state (node,N_NORMAL_MEMORY ); + }
Was wondering whether we could just do an early exit, like:
for (zt = 0; zt <= ZONE_NORMAL; zt++) {
if (pgdat->node_zones[zt].present_pages)
break;
}
if (zt > ZONE_NORMAL)
node_clear_state(node, N_NORMAL_MEMORY);But ends up being harder to digest (incl. all variants I tried :) ), so
Signature · 3 lines
Cheers, David
Quoted 76 lines · David Hildenbrand (Arm)
Quoted 11 lines · Hao Li
N_NORMAL_MEMORYis initialized from zone population at boot, but memory hotplug currently only updatesN_MEMORY. As a result, a node that gains normal memory via hotplug can remain invisible to users iterating overN_NORMAL_MEMORY, while a node that loses its last normal memory can stay incorrectly marked as such.Restore
N_NORMAL_MEMORYmaintenance directly inonline_pages()andoffline_pages(). Set the bit when a node that currently lacks normal memory onlines pages into a zone <=ZONE_NORMAL, and clear it when offlining removes the last present pages from zones <=ZONE_NORMAL.Quoted 8 lines
This restores the intended semantics without bringing back the old
status_change_nid_normalnotifier plumbing which was removed in8d2882a8edb8.Current users that benefit include
list_lru, zswap, nfsd filecache,hugetlb_cgroup, andhas_normal_memorysysfs reporting.Fixes 8d2882a8edb8 ("mm,memory_hotplug: remove status_change_nid_normal and update documentation")Agreed on CCing stable.
Quoted 39 lines
Signed-off-by Hao Li <[email protected]>---
Changes: simplify the code. (Thanks Joshua and David)--- mm/memory_hotplug.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)mm/memory_hotplug.c +10
-0@@ -1209,6 +1209,13 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,if (node_arg.nid >=0 )node_set_state (nid,N_MEMORY ); +/* +* Check whether we are adding normal memory to the node for the first +* time. +*/ +if (!node_state (nid,N_NORMAL_MEMORY ) &&zone_idx (zone) <=ZONE_NORMAL ) +node_set_state (nid,N_NORMAL_MEMORY ); +if (need_zonelists_rebuild)build_all_zonelists (NULL );@@ -1908,6 +1915,8 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,unsigned long flags;char *reason;int ret; +unsigned long normal_pages =0 ; +enum zone_type zt;/* * {on,off}lining is constrained to full memory sections (or more @@ -2055,6 +2064,17 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,/* reinitialise watermarks and update pcp limits */ init_per_zone_wmark_min (); +/* mm/memory_hotplug.c int offline_pages(unsigned long start_pfn, unsigned long nr_pages, · line 2077
Quoted 10 lines
mm/memory_hotplug.c +10
-0+* Check whether this operation removes the last normal memory from +* the node. We do this before clearing N_MEMORY to avoid the possible +* transient "!N_MEMORY && N_NORMAL_MEMORY" state. +*/ +if (zone_idx (zone) <=ZONE_NORMAL ) { +for (zt =0 ; zt <=ZONE_NORMAL ; zt++) + normal_pages += pgdat->node_zones[zt].present_pages; +if (!normal_pages) +node_clear_state (node,N_NORMAL_MEMORY ); + }Was wondering whether we could just do an early exit, like:
Quoted 8 lines
for (zt = 0; zt <= ZONE_NORMAL; zt++) { if (pgdat->node_zones[zt].present_pages) break; } if (zt > ZONE_NORMAL) node_clear_state(node, N_NORMAL_MEMORY);But ends up being harder to digest (incl. all variants I tried :) ), so
Thanks for exploring this.
Yeah, this looks like one of those cases where we have to make a bit of a tradeoff for readability :)
Quoted 2 lines
Thanks for the Ack!
Signature · 2 lines
Thanks, Hao