Re: [PATCH 02/22] mm: publish and clear node memory states via helpers

From: Richard Cheng

Date: Wed Nov 18 2026 - 02:54:21 EST


On Sat, Nov 14, 2026 at 02:09:39AM +0800, Gregory Price (Meta) wrote:
> N_MEMORY, N_NORMAL_MEMORY and N_HIGH_MEMORY are set separately in
> free_area_init(), check_for_memory() and online_pages(), and cleared
> in offline_pages().
>
> Centralize those updates in node_set/clear_memory_state().
>
> Behavior is unchanged.
>
> Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> ---
> mm/internal.h | 3 +++
> mm/memory_hotplug.c | 11 ++++-------
> mm/mm_init.c | 14 ++++++--------
> mm/page_alloc.c | 15 +++++++++++++++
> 4 files changed, 28 insertions(+), 15 deletions(-)
>
> diff --git a/mm/internal.h b/mm/internal.h
> index cd3675c044e0a..608a83c34c224 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -62,6 +62,9 @@ void lru_add_drain_cpu_zone(struct zone *zone);
> void folio_deactivate(struct folio *folio);
> void folio_mark_lazyfree(struct folio *folio);
>
> +void node_set_memory_state(int nid, bool high, bool normal);
> +void node_clear_memory_state(int nid);
> +
> /* mm/vmscan.c */
> unsigned long zone_reclaimable_pages(struct zone *zone);
> unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index f04a248d8adaf..490f400498f14 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -1184,14 +1184,11 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,
> online_pages_range(pfn, nr_pages);
> adjust_present_page_count(pfn_to_page(pfn), group, 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.
> + * Publish this node's memory states, including any newly-normal zone.
> + * Hotplug never adds highmem.
> */
> - if (!node_state(nid, N_NORMAL_MEMORY) && zone_idx(zone) <= ZONE_NORMAL)
> - node_set_state(nid, N_NORMAL_MEMORY);
> + node_set_memory_state(nid, false, zone_idx(zone) <= ZONE_NORMAL);
>

Um, so I am not sure how much cache pressure this will add, I assume it's small ?

Just want to point out that the old code only wrote these bits for a node's first
memory. THis now does up to 3 set_bit() calls on node_states[] for every onlined block.
The result is the same, but node_states[] is __read_mostly. A test-before-set in the helper
would keep the old behavior, or if you would like to keep it, please address in commit message.

Best regards,
Richard Cheng.


> if (need_zonelists_rebuild)
> build_all_zonelists(NULL);
> @@ -2146,7 +2143,7 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages,
> * list. Otherwise this node would still appear in the fallback lists.
> */
> if (node_arg.nid >= 0)
> - node_clear_state(node, N_MEMORY);
> + node_clear_memory_state(node);
> if (!populated_zone(zone)) {
> zone_pcp_reset(zone);
> build_all_zonelists(NULL);
> diff --git a/mm/mm_init.c b/mm/mm_init.c
> index a9d848c92678b..b4c4dba93652d 100644
> --- a/mm/mm_init.c
> +++ b/mm/mm_init.c
> @@ -1763,21 +1763,21 @@ static void __init free_area_init_node(int nid)
> lru_gen_init_pgdat(pgdat);
> }
>
> -/* Any regular or high memory on that node? */
> +/* Publish the memory states of a node that has present pages. */
> static void __init check_for_memory(pg_data_t *pgdat)
> {
> enum zone_type zone_type;
> + bool high = false, normal = false;
>
> for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
> struct zone *zone = &pgdat->node_zones[zone_type];
> if (populated_zone(zone)) {
> - if (IS_ENABLED(CONFIG_HIGHMEM))
> - node_set_state(pgdat->node_id, N_HIGH_MEMORY);
> - if (zone_type <= ZONE_NORMAL)
> - node_set_state(pgdat->node_id, N_NORMAL_MEMORY);
> + high = IS_ENABLED(CONFIG_HIGHMEM);
> + normal = zone_type <= ZONE_NORMAL;
> break;
> }
> }
> + node_set_memory_state(pgdat->node_id, high, normal);
> }
>
> #if MAX_NUMNODES > 1
> @@ -1924,10 +1924,8 @@ static void __init free_area_init(void)
> *memory-less node. The pgdat will get fully initialized by
> *hotadd_init_pgdat() when memory is hotplugged into this node.
> */
> - if (pgdat->node_present_pages) {
> - node_set_state(nid, N_MEMORY);
> + if (pgdat->node_present_pages)
> check_for_memory(pgdat);
> - }
> }
>
> calc_nr_kernel_pages();
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 8532af052d74b..834d199ff8429 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -208,6 +208,21 @@ nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
> };
> EXPORT_SYMBOL(node_states);
>
> +void __meminit node_set_memory_state(int nid, bool high, bool normal)
> +{
> + node_set_state(nid, N_MEMORY);
> + if (high)
> + node_set_state(nid, N_HIGH_MEMORY);
> + if (normal)
> + node_set_state(nid, N_NORMAL_MEMORY);
> +}
> +
> +void __meminit node_clear_memory_state(int nid)
> +{
> + node_clear_state(nid, N_NORMAL_MEMORY);
> + node_clear_state(nid, N_MEMORY);
> +}
> +
> gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
>
> #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
> --
> 2.53.1-Meta
>
>