Skip to contentSkip to conversation

mm: fix allocator races with memory hotplug

PATCH Applied Gregory Price (Meta) 3 participants 30 messages Nov 1 – Nov 5

Subsystems (9)
lore
Share & tools
lore

Gregory Price (Meta)

author maintainer
#0 lore raw

v5 cover mm: fix allocator races with memory hotplug

Memory hotplug rebuilds zonelists in place while allocations walk them, and offlining an emptied zone frees pagesets the vmstat worker can still hold.

These eleven fixes cover the walkers that crash, fail, or pick the wrong zone or node as a result, and a twelfth patch removes a helper that invites the same bug.

1. a restarted slowpath loses its retry with reserves
2. offlining an emptied zone frees pagesets still in use
3. the reclaim_retry_zone tracepoint rereads a zoneref slot
4. the zonelist retry is compiled out without MEMORY_HOTREMOVE
5. zonelist_zone() may load its slot more than once
6. NULL preferred zone after a shrink rebuild
7. __next_zones_zonelist() rereads a slot
8. allocations land outside their zone or nodemask
9. the hugetlb pool walk takes another node's page
10. alloc_contig_pages() takes a range outside its filters
11. mempolicy_slab_node() rereads a slot
12. zonelist_node_idx() invites the same double read

Patches 1-11 are for stable. Patch 12 is a cleanup and is not.

Patches 8-10 need patch 4, without which their retry is compiled out on HOTREMOVE=n kernels.

Patch 8 needs patch 1.

Patches 6, 7 and 11 need patch 5, which makes zonelist_zone() read the slot once. Patch 12 needs patches 7 and 11, which remove the other callers of zonelist_node_idx().

Patches 2 and 3 are patches from anthropic, pulled in from Vlastimil's series to keep the backport issues self-contained on the series.

Testing
=======

Each race was reproduced on unmodified source in a QEMU guest with three NUMA nodes and a 512M pc-dimm on node 1, left offline at boot.

A test module widens each window with a kprobe that stalls one CPU inside it, as an interrupt or a vCPU preemption would.

       base                            this series
patch  outcome                  runs   outcome                  runs
=====  =======================  =====  =======================  =====
 1     NULL returned            3/3    page from reserves       3/3
 2     UAF                      6/6    no UAF                   3/3
 3     NULL dereference         3/3    allocation succeeds      3/3
 4     allocation fails         3/3    waits for the rebuild    3/3
 6     NULL dereference         3/3    ZONE_NORMAL page         3/3
 7     NULL dereference         3/3    ZONE_NORMAL page         3/3
 8     wrong zone or node       18/18  right zone and node      18/18
 9     page from node 1         3/3    page from node 2         3/3
10     1G page in ZONE_MOVABLE  3/3    1G page in ZONE_NORMAL   3/3
11     NULL dereference         4/4    keeps allocating         3/3

Cost (these tests were run on v4, v5 hasn't substantially changed)
====
Kernel instructions per order-0 allocation and free in the QEMU guest, counted with a task-bound hardware instruction counter around 1M iterations on a pinned kthread, non-debug kernel, mean of 15 rounds.

instructions per alloc+free
base this series

  alloc_pages()             890.5         900.5
  bulk, nr=1, nodemask      865.5         875.5
  bulk, nr=16, per page     477.2         478.1

Bare metal, two-socket Xeon Gold 6138 (Skylake-SP, 80 CPUs).

will-it-scale page_fault1-3: each iteration touches one page, so the unit is millions of faults per second, summed over all tasks.

Base, millions of faults per second:

tasks 1 10 40 80

  page_fault1, processes     0.46   4.16  10.51   9.23
  page_fault1, threads       0.46   3.51   4.35   4.19
  page_fault2, processes     0.43   3.66   9.02   8.33
  page_fault2, threads       0.43   3.48   4.37   4.19
  page_fault3, processes     3.89  14.78   8.10   8.00
  page_fault3, threads       3.87  14.29   6.88   7.96

Change in faults per second with this series, + is faster:

tasks 1 10 40 80

  page_fault1, processes    -0.3%  +0.1%  -3.0%  -1.7%
  page_fault1, threads      -0.5%  -1.6%  -0.9%  -0.9%
  page_fault2, processes    +0.0%  +0.9%  -1.4%  -0.5%
  page_fault2, threads      +0.3%  +1.5%  -1.4%  -1.0%
  page_fault3, processes    -1.0%  -3.8%  -0.7%  +1.2%
  page_fault3, threads      -0.9%  -0.7%  +0.8%  -0.2%

The boot-to-boot stddev is 1.0-1.8% at 1 task and up to 15% in 10-80 tasks, so basically this is all within noise.

Changes in v5
=============
- Pulled in patches from Vlastimil's series (patches 2 and 3 here)
- inline functions (Vlastimil)
- Added patch 5: zonelist_zone() reads the slot with READ_ONCE(), as recommended by sashiko on v4's mempolicy patch.
Patches 6, 7 and 11 call it instead of open-coding the load.
- Added patch 12, removing zonelist_node_idx(). Not for stable.
- Patch 1: reworded the changelog (Vlastimil).
- Collected Reviewed-by tags.

Gregory Price (Meta) (10):
mm/page_alloc: reset can_retry_reserves when the slowpath restarts mm/page_alloc: detect zonelist rebuilds from memory hot-add mm/mmzone: read the zoneref slot once in zonelist_zone() mm/page_alloc: snapshot the preferred zone against shrink rebuilds mm/mmzone: read each zoneref slot once when filtering by nodemask mm/page_alloc: retry allocations that raced with a zonelist rebuild mm/hugetlb: retry the pool walk when the zonelist is rebuilt mm/page_alloc: retry alloc_contig_pages() when the zonelist is rebuilt mm/mempolicy: read the zoneref slot once in mempolicy_slab_node() mm/mmzone: remove zonelist_node_idx()

Josef Bacik (2):
mm/page_alloc: keep the pagesets of an empty zone mm, page_alloc: pass zone pointer to trace_reclaim_retry_zone

 include/linux/mmzone.h     |   9 +---
 include/trace/events/oom.h |   8 +--
 mm/hugetlb.c               |  13 +++--
 mm/mempolicy.c             |   6 ++-
 mm/mmzone.c                |  11 ++--
 mm/page_alloc.c            | 107 +++++++++++++++++++++++++++----------
 mm/page_alloc.h            |  19 +++++++
 7 files changed, 123 insertions(+), 50 deletions(-)
Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH v5 1/12 mm/page_alloc: reset can_retry_reserves when the slowpath restarts

Commit 2c4c3e29897d ("mm/page_alloc: simplify __alloc_pages_slowpath() flow") retries an allocation once with reserve_flags when the caller becomes eligible for reserves, guarded by can_retry_reserves.

A restart after a cpuset update or a zonelist rebuild retries with can_retry_reserves already set to false, so a caller that cannot reclaim (GFP_NOWAIT | __GFP_MEMALLOC), fails without trying reserves.

Reset can_retry_reserves on restart. Only a cpuset or zonelist change restarts the slowpath, so the retry still runs at most once per change.

With a kprobe helping to make the race deterministic, an allocation can be made to return NULL. With this patch the allocation will succeed.

Fixes 2c4c3e29897d ("mm/page_alloc: simplify __alloc_pages_slowpath() flow")
---
 mm/page_alloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +2 -1
index a8cac5e01066..f1048168c90c 100644
@@ -4783,7 +4783,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	unsigned int zonelist_iter_cookie;
 	int reserve_flags;
 	bool compact_first = false;
-	bool can_retry_reserves = true;
+	bool can_retry_reserves;
 	unsigned long alloc_start_time = jiffies;
 
 	if (unlikely(nofail)) {
@@ -4805,6 +4805,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	no_progress_loops = 0;
 	compact_result = COMPACT_SKIPPED;
 	compact_priority = DEF_COMPACT_PRIORITY;
+	can_retry_reserves = true;
 	cpuset_mems_cookie = read_mems_allowed_begin();
 	zonelist_iter_cookie = zonelist_iter_begin();
 
Signature · 1 line
2.56.0
reviewer Acked-by on v5 1/12
#2 lore raw

Re: [PATCH v5 01/12] mm/page_alloc: reset can_retry_reserves when the slowpath restarts

Quoted 14 lines · Gregory Price (Meta)

Commit 2c4c3e29897d ("mm/page_alloc: simplify __alloc_pages_slowpath() flow") retries an allocation once with reserve_flags when the caller becomes eligible for reserves, guarded by can_retry_reserves.

A restart after a cpuset update or a zonelist rebuild retries with can_retry_reserves already set to false, so a caller that cannot reclaim (GFP_NOWAIT | __GFP_MEMALLOC), fails without trying reserves.

Reset can_retry_reserves on restart. Only a cpuset or zonelist change restarts the slowpath, so the retry still runs at most once per change.

With a kprobe helping to make the race deterministic, an allocation can be made to return NULL. With this patch the allocation will succeed.

Quoted 7 lines
Fixes 2c4c3e29897d ("mm/page_alloc: simplify __alloc_pages_slowpath() flow")
---
 mm/page_alloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#3 lore raw

PATCH v5 2/12 mm/page_alloc: keep the pagesets of an empty zone

In reply to Gregory Price (Meta)

From: Josef Bacik <[email protected]>

When the last memory of a zone goes offline, zone_pcp_reset() frees zone->per_cpu_pageset and zone->per_cpu_zonestats. Nothing waits for those who found the zone populated and still use the two pointers. They take neither the memory hotplug lock nor any other lock which offline_pages() holds, and some of them can sleep:

CPU0 vmstat_update() CPU1 offline_pages()
refresh_cpu_vm_stats()

    for_each_populated_zone(zone)
      pcp = zone->per_cpu_pageset
      cond_resched()
                                      adjust_present_page_count()
                                      zone_pcp_reset(zone)
                                        free_percpu(zone->per_cpu_pageset)
      decay_pcp_high(zone, pcp)
      __this_cpu_write(pcp->expire, 0)

The same goes for drain_all_pages(NULL), which takes pcp->lock, for zone_pcp_update(), show_free_areas() and the readers and writers of the zonestats. If the per-cpu memory has got a new owner in the meantime, it is corrupted.

The pagesets are only freed because setup_zone_pageset() would otherwise leak them when the zone gets memory again. Keep them instead and let setup_zone_pageset() and zone_pcp_init() use them again, like it is done with pgdat->per_cpu_nodestats. Their lists are empty, and their limits have been calculated for a zone without memory by the init_per_zone_wmark_min() of offline_pages(), which is what setup_zone_pageset() would come up with as well.

Fixes 340175b7d14d ("mm/hotplug: free zone->pageset when a zone becomes empty")
Assisted-by LLM
---
 mm/page_alloc.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
mm/page_alloc.c +14 -6
index f1048168c90c..1850a41f24f7 100644
@@ -6308,6 +6308,10 @@ void __meminit setup_zone_pageset(struct zone *zone)
 {
 	int cpu;
 
+	/* The zone has been populated before, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/* Size may be 0 on !SMP && !NUMA */
 	if (sizeof(struct per_cpu_zonestat) > 0)
 		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);
@@ -6400,6 +6404,10 @@ void __init setup_per_cpu_pageset(void)
 
 __meminit void zone_pcp_init(struct zone *zone)
 {
+	/* A node which is added again, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/*
 	 * per cpu subsystem is not up at this point. The following code
 	 * relies on the ability of the linker to provide the
@@ -7680,6 +7688,12 @@ void zone_pcp_enable(struct zone *zone)
 	mutex_unlock(&pcp_batch_high_lock);
 }
 
+/*
+ * The zone has become empty. Its pagesets and zonestats are not freed: nothing
+ * keeps somebody who found the zone populated, such as refresh_cpu_vm_stats()
+ * or drain_all_pages(), from still using them. They are used again when the
+ * zone gets memory again.
+ */
 void zone_pcp_reset(struct zone *zone)
 {
 	int cpu;
@@ -7690,12 +7704,6 @@ void zone_pcp_reset(struct zone *zone)
 			pzstats = per_cpu_ptr(zone->per_cpu_zonestats, cpu);
 			drain_zonestat(zone, pzstats);
 		}
-		free_percpu(zone->per_cpu_pageset);
-		zone->per_cpu_pageset = &boot_pageset;
-		if (zone->per_cpu_zonestats != &boot_zonestats) {
-			free_percpu(zone->per_cpu_zonestats);
-			zone->per_cpu_zonestats = &boot_zonestats;
-		}
 	}
 }
 
Signature · 1 line
2.56.0
reviewer Reviewed-by on v5 2/12
#4 lore raw

Re: [PATCH v5 02/12] mm/page_alloc: keep the pagesets of an empty zone

Quoted 40 lines · Gregory Price (Meta)

From: Josef Bacik <[email protected]>

When the last memory of a zone goes offline, zone_pcp_reset() frees zone->per_cpu_pageset and zone->per_cpu_zonestats. Nothing waits for those who found the zone populated and still use the two pointers. They take neither the memory hotplug lock nor any other lock which offline_pages() holds, and some of them can sleep:

CPU0 vmstat_update() CPU1 offline_pages()
refresh_cpu_vm_stats()

    for_each_populated_zone(zone)
      pcp = zone->per_cpu_pageset
      cond_resched()
                                      adjust_present_page_count()
                                      zone_pcp_reset(zone)
                                        free_percpu(zone->per_cpu_pageset)
      decay_pcp_high(zone, pcp)
      __this_cpu_write(pcp->expire, 0)

The same goes for drain_all_pages(NULL), which takes pcp->lock, for zone_pcp_update(), show_free_areas() and the readers and writers of the zonestats. If the per-cpu memory has got a new owner in the meantime, it is corrupted.

The pagesets are only freed because setup_zone_pageset() would otherwise leak them when the zone gets memory again. Keep them instead and let setup_zone_pageset() and zone_pcp_init() use them again, like it is done with pgdat->per_cpu_nodestats. Their lists are empty, and their limits have been calculated for a zone without memory by the init_per_zone_wmark_min() of offline_pages(), which is what setup_zone_pageset() would come up with as well.

Fixes 340175b7d14d ("mm/hotplug: free zone->pageset when a zone becomes empty")
Assisted-by LLM
Quoted 3 lines
---
 mm/page_alloc.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

LGTM. Just a nit below:

Quoted 17 lines
mm/page_alloc.c +4 -0
index f1048168c90c..1850a41f24f7 100644
@@ -6308,6 +6308,10 @@ void __meminit setup_zone_pageset(struct zone *zone)
 {
 	int cpu;
 
+	/* The zone has been populated before, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/* Size may be 0 on !SMP && !NUMA */
 	if (sizeof(struct per_cpu_zonestat) > 0)
 		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6407

Quoted 4 lines
mm/page_alloc.c +1 -0
@@ -6400,6 +6404,10 @@ void __init setup_per_cpu_pageset(void)
 
 __meminit void zone_pcp_init(struct zone *zone)
 {
+	/* A node which is added again, see zone_pcp_reset(). */

it might be better to explain zone->per_cpu_pageset can only be NULL at boot time and it needs an initialization.

Quoted 6 lines
mm/page_alloc.c +3 -0
+	if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/*
 	 * per cpu subsystem is not up at this point. The following code
 	 * relies on the ability of the linker to provide the
Signature · 2 lines
Best Regards,
Yan, Zi
#5 lore raw

Re: [PATCH v5 02/12] mm/page_alloc: keep the pagesets of an empty zone

Quoted 67 lines · Zi Yan
Quoted 40 lines · Gregory Price (Meta)

From: Josef Bacik <[email protected]>

When the last memory of a zone goes offline, zone_pcp_reset() frees zone->per_cpu_pageset and zone->per_cpu_zonestats. Nothing waits for those who found the zone populated and still use the two pointers. They take neither the memory hotplug lock nor any other lock which offline_pages() holds, and some of them can sleep:

CPU0 vmstat_update() CPU1 offline_pages()
refresh_cpu_vm_stats()

    for_each_populated_zone(zone)
      pcp = zone->per_cpu_pageset
      cond_resched()
                                      adjust_present_page_count()
                                      zone_pcp_reset(zone)
                                        free_percpu(zone->per_cpu_pageset)
      decay_pcp_high(zone, pcp)
      __this_cpu_write(pcp->expire, 0)

The same goes for drain_all_pages(NULL), which takes pcp->lock, for zone_pcp_update(), show_free_areas() and the readers and writers of the zonestats. If the per-cpu memory has got a new owner in the meantime, it is corrupted.

The pagesets are only freed because setup_zone_pageset() would otherwise leak them when the zone gets memory again. Keep them instead and let setup_zone_pageset() and zone_pcp_init() use them again, like it is done with pgdat->per_cpu_nodestats. Their lists are empty, and their limits have been calculated for a zone without memory by the init_per_zone_wmark_min() of offline_pages(), which is what setup_zone_pageset() would come up with as well.

Fixes 340175b7d14d ("mm/hotplug: free zone->pageset when a zone becomes empty")
Assisted-by LLM
Quoted 3 lines
---
 mm/page_alloc.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

LGTM. Just a nit below:

Quoted 18 lines
mm/page_alloc.c +4 -0
index f1048168c90c..1850a41f24f7 100644
@@ -6308,6 +6308,10 @@ void __meminit setup_zone_pageset(struct zone *zone)
 {
 	int cpu;
 
+	/* The zone has been populated before, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/* Size may be 0 on !SMP && !NUMA */
 	if (sizeof(struct per_cpu_zonestat) > 0)
 		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);
@@ -6400,6 +6404,10 @@ void __init setup_per_cpu_pageset(void)
 
Quoted 6 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6407

Quoted 3 lines
mm/page_alloc.c +1 -0
 __meminit void zone_pcp_init(struct zone *zone)
 {
+	/* A node which is added again, see zone_pcp_reset(). */

it might be better to explain zone->per_cpu_pageset can only be NULL at boot time and it needs an initialization.

Hmm.

Quoted 2 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6409

Quoted 2 lines
mm/page_alloc.c +2 -0
+	if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+		return;

Shouldn't then the check be just:

if (zone->per_cpu_pageset)

	return;

Because if it's == &boot_pageset, we are just setting the same values that area already there?

Quoted 5 lines
Quoted 3 lines
mm/page_alloc.c +0 -0
 	/*
 	 * per cpu subsystem is not up at this point. The following code
 	 * relies on the ability of the linker to provide the
reviewer
#6 lore raw

Re: [PATCH v5 02/12] mm/page_alloc: keep the pagesets of an empty zone

Quoted 82 lines · Vlastimil Babka (SUSE)
Quoted 67 lines · Zi Yan
Quoted 40 lines · Gregory Price (Meta)

From: Josef Bacik <[email protected]>

When the last memory of a zone goes offline, zone_pcp_reset() frees zone->per_cpu_pageset and zone->per_cpu_zonestats. Nothing waits for those who found the zone populated and still use the two pointers. They take neither the memory hotplug lock nor any other lock which offline_pages() holds, and some of them can sleep:

CPU0 vmstat_update() CPU1 offline_pages()
refresh_cpu_vm_stats()

    for_each_populated_zone(zone)
      pcp = zone->per_cpu_pageset
      cond_resched()
                                      adjust_present_page_count()
                                      zone_pcp_reset(zone)
                                        free_percpu(zone->per_cpu_pageset)
      decay_pcp_high(zone, pcp)
      __this_cpu_write(pcp->expire, 0)

The same goes for drain_all_pages(NULL), which takes pcp->lock, for zone_pcp_update(), show_free_areas() and the readers and writers of the zonestats. If the per-cpu memory has got a new owner in the meantime, it is corrupted.

The pagesets are only freed because setup_zone_pageset() would otherwise leak them when the zone gets memory again. Keep them instead and let setup_zone_pageset() and zone_pcp_init() use them again, like it is done with pgdat->per_cpu_nodestats. Their lists are empty, and their limits have been calculated for a zone without memory by the init_per_zone_wmark_min() of offline_pages(), which is what setup_zone_pageset() would come up with as well.

Fixes 340175b7d14d ("mm/hotplug: free zone->pageset when a zone becomes empty")
Assisted-by LLM
Quoted 3 lines
---
 mm/page_alloc.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

LGTM. Just a nit below:

Quoted 18 lines
mm/page_alloc.c +4 -0
index f1048168c90c..1850a41f24f7 100644
@@ -6308,6 +6308,10 @@ void __meminit setup_zone_pageset(struct zone *zone)
 {
 	int cpu;
 
+	/* The zone has been populated before, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/* Size may be 0 on !SMP && !NUMA */
 	if (sizeof(struct per_cpu_zonestat) > 0)
 		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);
@@ -6400,6 +6404,10 @@ void __init setup_per_cpu_pageset(void)
 
Quoted 6 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6407

Quoted 3 lines
mm/page_alloc.c +1 -0
 __meminit void zone_pcp_init(struct zone *zone)
 {
+	/* A node which is added again, see zone_pcp_reset(). */

it might be better to explain zone->per_cpu_pageset can only be NULL at boot time and it needs an initialization.

Hmm.

Quoted 2 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6409

Quoted 2 lines
mm/page_alloc.c +2 -0
+	if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+		return;

Shouldn't then the check be just:

Quoted 6 lines

if (zone->per_cpu_pageset)

	return;

Because if it's == &boot_pageset, we are just setting the same values that area already there?

Right. And zone_pcp_init(), called by zone_init_internals(), becomes a nop for free_area_init_core_hotplug(). Then I wonder if zone_init_internals() is needed for hotplug, but that belongs to a later cleanup.

Signature · 2 lines
Best Regards,
Yan, Zi
#7 lore raw

Re: [PATCH v5 02/12] mm/page_alloc: keep the pagesets of an empty zone

Quoted 35 lines · Zi Yan
Quoted 34 lines · Vlastimil Babka (SUSE)
Quoted 17 lines
Quoted 17 lines
mm/page_alloc.c +4 -0
index f1048168c90c..1850a41f24f7 100644
@@ -6308,6 +6308,10 @@ void __meminit setup_zone_pageset(struct zone *zone)
 {
 	int cpu;
 
+	/* The zone has been populated before, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/* Size may be 0 on !SMP && !NUMA */
 	if (sizeof(struct per_cpu_zonestat) > 0)
 		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);
@@ -6400,6 +6404,10 @@ void __init setup_per_cpu_pageset(void)
 
Quoted 6 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6407

Quoted 3 lines
mm/page_alloc.c +1 -0
 __meminit void zone_pcp_init(struct zone *zone)
 {
+	/* A node which is added again, see zone_pcp_reset(). */

it might be better to explain zone->per_cpu_pageset can only be NULL at boot time and it needs an initialization.

Hmm.

Quoted 2 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6409

Quoted 2 lines
mm/page_alloc.c +2 -0
+	if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+		return;

Shouldn't then the check be just:

if (zone->per_cpu_pageset)

	return;
Quoted 7 lines
Quoted 3 lines

Because if it's == &boot_pageset, we are just setting the same values that area already there?

Right. And zone_pcp_init(), called by zone_init_internals(), becomes a nop for free_area_init_core_hotplug(). Then I wonder if zone_init_internals() is needed for hotplug, but that belongs to a later cleanup.

Yep, that's for later cleanup. Right now the minimum that's correct and not unnecessarily misleading is enough. So I've done this:

mm/page_alloc.c +2 -2
index 1850a41f24f7..20bfd11c5851 100644
@@ -6404,8 +6404,8 @@ void __init setup_per_cpu_pageset(void)
 
 __meminit void zone_pcp_init(struct zone *zone)
 {
-       /* A node which is added again, see zone_pcp_reset(). */
-       if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+       /* This initialization only needs to be done once */
+       if (zone->per_cpu_pageset)
                return;
 
        /*
reviewer
#8 lore raw

Re: [PATCH v5 02/12] mm/page_alloc: keep the pagesets of an empty zone

Quoted 52 lines · Vlastimil Babka (SUSE)
Quoted 35 lines · Zi Yan
Quoted 34 lines · Vlastimil Babka (SUSE)
Quoted 17 lines
Quoted 17 lines
mm/page_alloc.c +4 -0
index f1048168c90c..1850a41f24f7 100644
@@ -6308,6 +6308,10 @@ void __meminit setup_zone_pageset(struct zone *zone)
 {
 	int cpu;
 
+	/* The zone has been populated before, see zone_pcp_reset(). */
+	if (zone->per_cpu_pageset != &boot_pageset)
+		return;
+
 	/* Size may be 0 on !SMP && !NUMA */
 	if (sizeof(struct per_cpu_zonestat) > 0)
 		zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat);
@@ -6400,6 +6404,10 @@ void __init setup_per_cpu_pageset(void)
 
Quoted 6 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void)

Quoted 3 lines
mm/page_alloc.c +1 -0
 __meminit void zone_pcp_init(struct zone *zone)
 {
+	/* A node which is added again, see zone_pcp_reset(). */

it might be better to explain zone->per_cpu_pageset can only be NULL at boot time and it needs an initialization.

Hmm.

Quoted 2 lines

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6409

Quoted 2 lines
mm/page_alloc.c +2 -0
+	if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+		return;

Shouldn't then the check be just:

if (zone->per_cpu_pageset)

	return;
Quoted 7 lines
Quoted 3 lines

Because if it's == &boot_pageset, we are just setting the same values that area already there?

Right. And zone_pcp_init(), called by zone_init_internals(), becomes a nop for free_area_init_core_hotplug(). Then I wonder if zone_init_internals() is needed for hotplug, but that belongs to a later cleanup.

Yep, that's for later cleanup. Right now the minimum that's correct and not unnecessarily misleading is enough. So I've done this:

mm/page_alloc.c +0 -0
index 1850a41f24f7..20bfd11c5851 100644

mm/page_alloc.c void __init setup_per_cpu_pageset(void) · line 6411

Quoted 10 lines
mm/page_alloc.c +2 -2
@@ -6404,8 +6404,8 @@ void __init setup_per_cpu_pageset(void)
 
 __meminit void zone_pcp_init(struct zone *zone)
 {
-       /* A node which is added again, see zone_pcp_reset(). */
-       if (zone->per_cpu_pageset && zone->per_cpu_pageset != &boot_pageset)
+       /* This initialization only needs to be done once */
+       if (zone->per_cpu_pageset)
                return;
 
        /*

LGTM. Thanks.

Best Regards,
Yan, Zi

author maintainer
#9 lore raw

PATCH v5 3/12 mm, page_alloc: pass zone pointer to trace_reclaim_retry_zone

In reply to Gregory Price (Meta)

From: Josef Bacik <[email protected]>

should_reclaim_retry() walks a zonelist and passes the zoneref pointer to the reclaim_retry_zone tracepoint, which loads the zone from the entry again to find the node.

Memory hotplug rebuilds the zonelists in place while allocations are under way. When a zone in front of the zoneref entry has become empty, the entries move up, and this one can be the end marker by the time the tracepoint looks at it. zonelist_node_idx() then dereferences a NULL zone. Short of that, the event can name another zone than the one its numbers belong to.

Pass the zone pointer instead of the zoneref pointer.

Fixes d379f01de095 ("oom, trace: add oom detection tracepoints")
Assisted-by LLM
---
 include/trace/events/oom.h | 8 ++++----
 mm/page_alloc.c            | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
include/trace/events/oom.h +4 -4
index 9f0a5d1482c4..cbcf6fe4cc61 100644
@@ -33,7 +33,7 @@ TRACE_EVENT(oom_score_adj_update,
 
 TRACE_EVENT(reclaim_retry_zone,
 
-	TP_PROTO(struct zoneref *zoneref,
+	TP_PROTO(struct zone *zone,
 		int order,
 		unsigned long reclaimable,
 		unsigned long available,
@@ -41,7 +41,7 @@ TRACE_EVENT(reclaim_retry_zone,
 		int no_progress_loops,
 		bool wmark_check),
 
-	TP_ARGS(zoneref, order, reclaimable, available, min_wmark, no_progress_loops, wmark_check),
+	TP_ARGS(zone, order, reclaimable, available, min_wmark, no_progress_loops, wmark_check),
 
 	TP_STRUCT__entry(
 		__field(	int, node)
@@ -55,8 +55,8 @@ TRACE_EVENT(reclaim_retry_zone,
 	),
 
 	TP_fast_assign(
-		__entry->node = zonelist_node_idx(zoneref);
-		__entry->zone_idx = zonelist_zone_idx(zoneref);
+		__entry->node = zone_to_nid(zone);
+		__entry->zone_idx = zone_idx(zone);
 		__entry->order = order;
 		__entry->reclaimable = reclaimable;
 		__entry->available = available;
mm/page_alloc.c +1 -1
index 1850a41f24f7..e14d550d4128 100644
@@ -4670,7 +4670,7 @@ should_reclaim_retry(gfp_t gfp_mask, unsigned order,
 		 */
 		wmark = __zone_watermark_ok(zone, order, min_wmark,
 				ac->highest_zoneidx, alloc_flags, available);
-		trace_reclaim_retry_zone(z, order, reclaimable,
+		trace_reclaim_retry_zone(zone, order, reclaimable,
 				available, min_wmark, *no_progress_loops, wmark);
 		if (wmark) {
 			ret = true;
Signature · 1 line
2.56.0
reviewer Reviewed-by on v5 3/12
#10 lore raw

Re: [PATCH v5 03/12] mm, page_alloc: pass zone pointer to trace_reclaim_retry_zone

Quoted 22 lines · Gregory Price (Meta)

From: Josef Bacik <[email protected]>

should_reclaim_retry() walks a zonelist and passes the zoneref pointer to the reclaim_retry_zone tracepoint, which loads the zone from the entry again to find the node.

Memory hotplug rebuilds the zonelists in place while allocations are under way. When a zone in front of the zoneref entry has become empty, the entries move up, and this one can be the end marker by the time the tracepoint looks at it. zonelist_node_idx() then dereferences a NULL zone. Short of that, the event can name another zone than the one its numbers belong to.

Pass the zone pointer instead of the zoneref pointer.

Fixes d379f01de095 ("oom, trace: add oom detection tracepoints")
Assisted-by LLM
Quoted 5 lines
---
 include/trace/events/oom.h | 8 ++++----
 mm/page_alloc.c            | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

Makes sense.

Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#11 lore raw

PATCH v5 4/12 mm/page_alloc: detect zonelist rebuilds from memory hot-add

In reply to Gregory Price (Meta)

The zonelist_update_seq retry was limited to CONFIG_MEMORY_HOTREMOVE, presumably because only hot-remove takes zones out of a zonelist.

However, build_zonerefs_node() rewrites the zonelists in place, so onlining the first block of a zone shifts the entries that follow it in every zonelist containing that zone. This can cause a spurious allocation failure for a __GFP_THISNODE caller.

For a node with only ZONE_NORMAL, onlining its first ZONE_MOVABLE block rewrites the node's ZONELIST_NOFALLBACK list as:

[NORMAL, NULL] -> [MOVABLE, NULL] -> [MOVABLE, NORMAL, NULL]

A concurrent GFP_KERNEL | __GFP_THISNODE allocation can observe the intermediate list, skip ZONE_MOVABLE, and find no eligible zone. With CONFIG_MEMORY_HOTREMOVE=n, check_retry_zonelist() returns 0 and the allocation fails even though the node has free ZONE_NORMAL memory.

Gate the sequence checks on CONFIG_MEMORY_HOTPLUG, which covers every configuration that rebuilds zonelists after boot.

Fixes 3d36424b3b58 ("mm/page_alloc: fix race condition between build_all_zonelists and page allocation")
---
 mm/page_alloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/page_alloc.c +2 -2
index e14d550d4128..11be12b443ad 100644
@@ -4404,7 +4404,7 @@ static DEFINE_SEQLOCK(zonelist_update_seq);
 
 static unsigned int zonelist_iter_begin(void)
 {
-	if (IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
+	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
 		return read_seqbegin(&zonelist_update_seq);
 
 	return 0;
@@ -4412,7 +4412,7 @@ static unsigned int zonelist_iter_begin(void)
 
 static unsigned int check_retry_zonelist(unsigned int seq)
 {
-	if (IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
+	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
 		return read_seqretry(&zonelist_update_seq, seq);
 
 	return seq;
Signature · 1 line
2.56.0
reviewer Acked-by on v5 4/12
#12 lore raw

Re: [PATCH v5 04/12] mm/page_alloc: detect zonelist rebuilds from memory hot-add

Quoted 21 lines · Gregory Price (Meta)

The zonelist_update_seq retry was limited to CONFIG_MEMORY_HOTREMOVE, presumably because only hot-remove takes zones out of a zonelist.

However, build_zonerefs_node() rewrites the zonelists in place, so onlining the first block of a zone shifts the entries that follow it in every zonelist containing that zone. This can cause a spurious allocation failure for a __GFP_THISNODE caller.

For a node with only ZONE_NORMAL, onlining its first ZONE_MOVABLE block rewrites the node's ZONELIST_NOFALLBACK list as:

[NORMAL, NULL] -> [MOVABLE, NULL] -> [MOVABLE, NORMAL, NULL]

A concurrent GFP_KERNEL | __GFP_THISNODE allocation can observe the intermediate list, skip ZONE_MOVABLE, and find no eligible zone. With CONFIG_MEMORY_HOTREMOVE=n, check_retry_zonelist() returns 0 and the allocation fails even though the node has free ZONE_NORMAL memory.

Gate the sequence checks on CONFIG_MEMORY_HOTPLUG, which covers every configuration that rebuilds zonelists after boot.

Quoted 8 lines
Fixes 3d36424b3b58 ("mm/page_alloc: fix race condition between build_all_zonelists and page allocation")
---
 mm/page_alloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#13 lore raw

PATCH v5 5/12 mm/mmzone: read the zoneref slot once in zonelist_zone()

In reply to Gregory Price (Meta)

Memory hotplug rebuilds zonelists in place while allocations walk them, so a zoneref slot can change between two loads of it.

zonelist_zone() loads the slot with a plain access. The compiler may load it again later instead of keeping the zone it already read, so a caller can check one zone and then use another, or the NULL terminator.

Use READ_ONCE(). Callers that take a zone from a slot and keep using it, such as get_page_from_freelist() with ac->preferred_zoneref, need that single load.

Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 include/linux/mmzone.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/mmzone.h +2 -1
index ecd1ebd49756..4bc2318ac37f 100644
@@ -1855,7 +1855,8 @@ extern struct zone *next_zone(struct zone *zone);
 
 static inline struct zone *zonelist_zone(struct zoneref *zoneref)
 {
-	return zoneref->zone;
+	/* Memory hotplug rewrites zonelists in place. */
+	return READ_ONCE(zoneref->zone);
 }
 
 static inline int zonelist_zone_idx(const struct zoneref *zoneref)
Signature · 1 line
2.56.0
reviewer Acked-by on v5 5/12
#14 lore raw

Re: [PATCH v5 05/12] mm/mmzone: read the zoneref slot once in zonelist_zone()

Quoted 11 lines · Gregory Price (Meta)

Memory hotplug rebuilds zonelists in place while allocations walk them, so a zoneref slot can change between two loads of it.

zonelist_zone() loads the slot with a plain access. The compiler may load it again later instead of keeping the zone it already read, so a caller can check one zone and then use another, or the NULL terminator.

Use READ_ONCE(). Callers that take a zone from a slot and keep using it, such as get_page_from_freelist() with ac->preferred_zoneref, need that single load.

Quoted 7 lines
Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 include/linux/mmzone.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#15 lore raw

PATCH v5 6/12 mm/page_alloc: snapshot the preferred zone against shrink rebuilds

In reply to Gregory Price (Meta)

get_page_from_freelist() and alloc_pages_bulk_noprof() reread ac->preferred_zoneref->zone at every use. Offlining a zone's last block rebuilds the zonelists in place, and the shrunk list's NULL terminator can land on the preferred slot mid-walk:

CPU 0 CPU 1
get_page_from_freelist(GFP_KERNEL | __GFP_THISNODE)
zone = NORMAL (slot 1)
offline_pages()
build_all_zonelists()
[MOVABLE, NORMAL, NULL] -> [NORMAL, NULL]
rmqueue(zonelist_zone(slot 1), zone)
zone_statistics(NULL, zone)

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:get_page_from_freelist+0x6a5/0x15f0
  Call Trace:
   __alloc_frozen_pages_noprof+0x125/0x330

Read the slot once. Zones are never freed, so a stale zone is safe, and a NULL slot fails the same way as walking off the end.

Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/page_alloc.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
mm/page_alloc.c +23 -8
index 11be12b443ad..28d46f987685 100644
@@ -3794,12 +3794,21 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 {
 	struct zoneref *z;
 	struct zone *zone;
+	struct zone *preferred_zone;
 	struct pglist_data *last_pgdat = NULL;
 	bool last_pgdat_dirty_ok = false;
 	bool no_fallback;
 	bool skip_kswapd_nodes = nr_online_nodes > 1;
 	bool skipped_kswapd_nodes = false;
 
+	/*
+	 * A concurrent zonelist rebuild can NULL or replace this slot. Zones
+	 * are never freed, so reading it once keeps a usable zone.
+	 */
+	preferred_zone = zonelist_zone(ac->preferred_zoneref);
+	if (!preferred_zone)
+		return NULL;
+
 retry:
 	/*
 	 * Scan zonelist, looking for a zone with enough free.
@@ -3846,7 +3855,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		}
 
 		if (no_fallback && !defrag_mode && nr_online_nodes > 1 &&
-		    zone != zonelist_zone(ac->preferred_zoneref)) {
+		    zone != preferred_zone) {
 			int local_nid;
 
 			/*
@@ -3854,7 +3863,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 			 * fragmenting fallbacks. Locality is more important
 			 * than fragmentation avoidance.
 			 */
-			local_nid = zonelist_node_idx(ac->preferred_zoneref);
+			local_nid = zone_to_nid(preferred_zone);
 			if (zone_to_nid(zone) != local_nid) {
 				alloc_flags &= ~ALLOC_NOFRAGMENT;
 				goto retry;
@@ -3916,7 +3925,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 				goto try_this_zone;
 
 			if (!node_reclaim_enabled() ||
-			    !zone_allows_reclaim(zonelist_zone(ac->preferred_zoneref), zone))
+			    !zone_allows_reclaim(preferred_zone, zone))
 				continue;
 
 			if (!node_reclaim(zone->zone_pgdat, gfp_mask, order))
@@ -3929,7 +3938,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		}
 
 try_this_zone:
-		page = rmqueue(zonelist_zone(ac->preferred_zoneref), zone, order,
+		page = rmqueue(preferred_zone, zone, order,
 				gfp_mask, alloc_flags, ac->migratetype);
 		if (page) {
 			prep_new_page(page, order, gfp_mask, alloc_flags);
@@ -5169,6 +5178,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 {
 	struct page *page;
 	struct zone *zone;
+	struct zone *preferred_zone;
 	struct zoneref *z;
 	struct per_cpu_pages *pcp;
 	struct list_head *pcp_list;
@@ -5217,6 +5227,11 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 	if (!prepare_alloc_pages(gfp, 0, preferred_nid, nodemask, &ac, &gfp, &alloc_flags))
 		goto out;
 
+	/* Read once; see get_page_from_freelist(). */
+	preferred_zone = zonelist_zone(ac.preferred_zoneref);
+	if (!preferred_zone)
+		goto failed;
+
 	/* Find an allowed local zone that meets the low watermark. */
 	z = ac.preferred_zoneref;
 	for_next_zone_zonelist_nodemask(zone, z, ac.highest_zoneidx, ac.nodemask) {
@@ -5227,8 +5242,8 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 			continue;
 		}
 
-		if (nr_online_nodes > 1 && zone != zonelist_zone(ac.preferred_zoneref) &&
-		    zone_to_nid(zone) != zonelist_node_idx(ac.preferred_zoneref)) {
+		if (nr_online_nodes > 1 && zone != preferred_zone &&
+		    zone_to_nid(zone) != zone_to_nid(preferred_zone)) {
 			goto failed;
 		}
 
@@ -5236,7 +5251,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 retry_this_zone:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages - nr_populated;
 		if (zone_watermark_fast(zone, 0,  mark,
-				zonelist_zone_idx(ac.preferred_zoneref),
+				zone_idx(preferred_zone),
 				alloc_flags, gfp)) {
 			break;
 		}
@@ -5295,7 +5310,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 	pcp_spin_unlock(pcp);
 
 	__count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account);
-	zone_statistics(zonelist_zone(ac.preferred_zoneref), zone, nr_account);
+	zone_statistics(preferred_zone, zone, nr_account);
 
 out:
 	return nr_populated;
Signature · 1 line
2.56.0
reviewer Acked-by on v5 6/12
#16 lore raw

Re: [PATCH v5 06/12] mm/page_alloc: snapshot the preferred zone against shrink rebuilds

Quoted 23 lines · Gregory Price (Meta)

get_page_from_freelist() and alloc_pages_bulk_noprof() reread ac->preferred_zoneref->zone at every use. Offlining a zone's last block rebuilds the zonelists in place, and the shrunk list's NULL terminator can land on the preferred slot mid-walk:

CPU 0 CPU 1
get_page_from_freelist(GFP_KERNEL | __GFP_THISNODE)
zone = NORMAL (slot 1)
offline_pages()
build_all_zonelists()
[MOVABLE, NORMAL, NULL] -> [NORMAL, NULL]
rmqueue(zonelist_zone(slot 1), zone)
zone_statistics(NULL, zone)

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:get_page_from_freelist+0x6a5/0x15f0
  Call Trace:
   __alloc_frozen_pages_noprof+0x125/0x330

Read the slot once. Zones are never freed, so a stale zone is safe, and a NULL slot fails the same way as walking off the end.

Quoted 8 lines
Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/page_alloc.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

Makes sense.

Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#17 lore raw

PATCH v5 7/12 mm/mmzone: read each zoneref slot once when filtering by nodemask

In reply to Gregory Price (Meta)

__next_zones_zonelist() tests a slot's zone for NULL, then reads the slot again to look up the zone's node. Offlining a zone's last block rebuilds the zonelists in place, so the second read can return the terminator:

CPU 0 CPU 1
__next_zones_zonelist(nodemask)
zonelist_zone(slot 1) = NORMAL
offline_pages()
build_all_zonelists()
[MOVABLE, NORMAL, NULL] -> [NORMAL, NULL]
zref_in_nodemask(slot 1)
zone_to_nid(NULL)

Current GCC and clang merge the two reads, but nothing requires it. With mm/mmzone.o built with -fno-inline -fno-ipa-sra, holding a nodemask allocation between the reads while the node's last movable block goes offline oopses every time:

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:zone_to_nid+0x0/0x10
  Call Trace:
   zref_in_nodemask+0x9/0x20
   __next_zones_zonelist+0x23/0x70
   prepare_alloc_pages.constprop.0+0x16c/0x190
   __alloc_frozen_pages_noprof+0xaf/0x330

Read the slot once and test that zone. A NULL zone still ends the walk.

Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/mmzone.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/mmzone.c +6 -5
index 9cc9ef588580..65880396f104 100644
@@ -43,13 +43,14 @@ struct zone *next_zone(struct zone *zone)
 	return zone;
 }
 
-static inline int zref_in_nodemask(struct zoneref *zref,
-				   const nodemask_t *nodes)
+/* A NULL zone terminates the zonelist, so it ends the search. */
+static inline bool zone_in_nodemask(const struct zone *zone,
+				    const nodemask_t *nodes)
 {
 #ifdef CONFIG_NUMA
-	return node_isset(zonelist_node_idx(zref), *nodes);
+	return !zone || node_isset(zone_to_nid(zone), *nodes);
 #else
-	return 1;
+	return true;
 #endif /* CONFIG_NUMA */
 }
 
@@ -67,7 +68,7 @@ struct zoneref *__next_zones_zonelist(struct zoneref *z,
 			z++;
 	else
 		while (zonelist_zone_idx(z) > highest_zoneidx ||
-				(zonelist_zone(z) && !zref_in_nodemask(z, nodes)))
+		       !zone_in_nodemask(zonelist_zone(z), nodes))
 			z++;
 
 	return z;
Signature · 1 line
2.56.0
reviewer Reviewed-by on v5 7/12
#18 lore raw

Re: [PATCH v5 07/12] mm/mmzone: read each zoneref slot once when filtering by nodemask

Quoted 28 lines · Gregory Price (Meta)

__next_zones_zonelist() tests a slot's zone for NULL, then reads the slot again to look up the zone's node. Offlining a zone's last block rebuilds the zonelists in place, so the second read can return the terminator:

CPU 0 CPU 1
__next_zones_zonelist(nodemask)
zonelist_zone(slot 1) = NORMAL
offline_pages()
build_all_zonelists()
[MOVABLE, NORMAL, NULL] -> [NORMAL, NULL]
zref_in_nodemask(slot 1)
zone_to_nid(NULL)

Current GCC and clang merge the two reads, but nothing requires it. With mm/mmzone.o built with -fno-inline -fno-ipa-sra, holding a nodemask allocation between the reads while the node's last movable block goes offline oopses every time:

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:zone_to_nid+0x0/0x10
  Call Trace:
   zref_in_nodemask+0x9/0x20
   __next_zones_zonelist+0x23/0x70
   prepare_alloc_pages.constprop.0+0x16c/0x190
   __alloc_frozen_pages_noprof+0xaf/0x330
Quoted 1 line

Read the slot once and test that zone. A NULL zone still ends the walk.

It is better to spell out the rename from zref_in_nodemask() to zone_in_nodemask().

Quoted 9 lines
Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/mmzone.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Makes sense.

Signature · 2 lines
Best Regards,
Yan, Zi
#19 lore raw

Re: [PATCH v5 07/12] mm/mmzone: read each zoneref slot once when filtering by nodemask

Quoted 29 lines · Zi Yan
Quoted 28 lines · Gregory Price (Meta)

__next_zones_zonelist() tests a slot's zone for NULL, then reads the slot again to look up the zone's node. Offlining a zone's last block rebuilds the zonelists in place, so the second read can return the terminator:

CPU 0 CPU 1
__next_zones_zonelist(nodemask)
zonelist_zone(slot 1) = NORMAL
offline_pages()
build_all_zonelists()
[MOVABLE, NORMAL, NULL] -> [NORMAL, NULL]
zref_in_nodemask(slot 1)
zone_to_nid(NULL)

Current GCC and clang merge the two reads, but nothing requires it. With mm/mmzone.o built with -fno-inline -fno-ipa-sra, holding a nodemask allocation between the reads while the node's last movable block goes offline oopses every time:

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:zone_to_nid+0x0/0x10
  Call Trace:
   zref_in_nodemask+0x9/0x20
   __next_zones_zonelist+0x23/0x70
   prepare_alloc_pages.constprop.0+0x16c/0x190
   __alloc_frozen_pages_noprof+0xaf/0x330
Quoted 4 lines
Quoted 1 line

Read the slot once and test that zone. A NULL zone still ends the walk.

It is better to spell out the rename from zref_in_nodemask() to zone_in_nodemask().

Added:

To achieve that, rename zref_in_nodemask() to zone_in_nodemask() and pass a zone instead of zoneref pointer.

Quoted 16 lines
Quoted 10 lines
Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/mmzone.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Makes sense.

author maintainer
#20 lore raw

Re: [PATCH v5 07/12] mm/mmzone: read each zoneref slot once when filtering by nodemask

Quoted 9 lines · Vlastimil Babka (SUSE)
Quoted 3 lines

It is better to spell out the rename from zref_in_nodemask() to zone_in_nodemask().

Added:

To achieve that, rename zref_in_nodemask() to zone_in_nodemask() and pass a zone instead of zoneref pointer.

ack. Assuming you don't need me to respin just for that.

~Gregory

author maintainer
#21 lore raw

PATCH v5 8/12 mm/page_alloc: retry allocations that raced with a zonelist rebuild

In reply to Gregory Price (Meta)

Onlining the first block of a zone rebuilds every zonelist in place, shifting the slots after the new zone. The iterator approves a slot, then the allocator loads the zone from it, so a GFP_KERNEL request can land in ZONE_MOVABLE and a nodemask request on a node outside its mask:

CPU 0 CPU 1
prepare_alloc_pages()

    first_zones_zonelist(nodemask {1})
      slot 0 (NORMAL1) approved
                                    online_pages()
                                      build_all_zonelists()
                                        [NORMAL1, NORMAL2, ...]
                                        -> [MOVABLE1, NORMAL1, ...]

get_page_from_freelist()
zone = zonelist_zone(slot 0) /* MOVABLE1 */
allocates from MOVABLE1

zonelist_update_seq detects the rebuild, but only the slowpath's failure exits check it.

Check it whenever an allocation succeeds too, and if it changed, free the page and retry, as the memcg charge failure path does. Eligible pages are dropped as well: zonelists only change when memory comes or goes, and a retry is simpler than rechecking the page.

Freeing the page does not undo a block the allocation claimed. A GFP_KERNEL request that lands in a fresh ZONE_MOVABLE converts a free block to MIGRATE_UNMOVABLE, which stays until the zone's movable free list runs out and a movable allocation takes it back.

The fast and bulk paths read the seqlock's raw seqcount without waiting:
an ALLOC_NOLOCK caller can be an NMI that interrupted the writer, and on PREEMPT_RT the seqlock accessor takes the spinlock. A sample taken mid-write costs a retry, or NULL under ALLOC_NOLOCK. check_retry_zonelist() returns 0 when compiled out, so a backport without the hot-add change stays harmless.

Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/page_alloc.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
mm/page_alloc.c +33 -1
index 28d46f987685..f2f7e96bdef2 100644
@@ -4419,12 +4419,25 @@ static unsigned int zonelist_iter_begin(void)
 	return 0;
 }
 
+/*
+ * Never wait for the writer, which an ALLOC_NOLOCK caller in NMI may have
+ * interrupted. Read the raw seqcount: on PREEMPT_RT the seqlock accessor takes
+ * the spinlock while a write is in progress.
+ */
+static unsigned int zonelist_iter_sample(void)
+{
+	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
+		return raw_seqcount_begin(&zonelist_update_seq.seqcount.seqcount);
+
+	return 0;
+}
+
 static unsigned int check_retry_zonelist(unsigned int seq)
 {
 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
 		return read_seqretry(&zonelist_update_seq, seq);
 
-	return seq;
+	return 0;
 }
 
 /* Perform direct synchronous page reclaim */
@@ -5100,6 +5113,12 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 	warn_alloc(gfp_mask, ac->nodemask,
 			"page allocation failure: order:%u", order);
 got_pg:
+	/* A rebuild during the walk may have handed out an ineligible zone. */
+	if (page && check_retry_zonelist(zonelist_iter_cookie)) {
+		__free_frozen_pages(page, order, FPI_NONE);
+		page = NULL;
+		goto restart;
+	}
 	return page;
 }
 
@@ -5184,6 +5203,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 	struct list_head *pcp_list;
 	struct alloc_context ac;
 	unsigned int alloc_flags = ALLOC_WMARK_LOW;
+	unsigned int zonelist_iter_cookie;
 	int nr_populated = 0, nr_account = 0;
 
 	/*
@@ -5224,6 +5244,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 	/* May set ALLOC_NOFRAGMENT, fragmentation will return 1 page. */
 	gfp &= gfp_allowed_mask;
 	gfp = current_gfp_context(gfp);
+	zonelist_iter_cookie = zonelist_iter_sample();
 	if (!prepare_alloc_pages(gfp, 0, preferred_nid, nodemask, &ac, &gfp, &alloc_flags))
 		goto out;
 
@@ -5273,6 +5294,10 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 	if (unlikely(!zone))
 		goto failed;
 
+	/* The zone was chosen from a zonelist that a rebuild may have changed. */
+	if (unlikely(check_retry_zonelist(zonelist_iter_cookie)))
+		goto failed;
+
 	/* spin_trylock may fail due to a parallel drain or IRQ reentrancy. */
 	pcp = pcp_spin_trylock(zone->per_cpu_pageset);
 	if (!pcp)
@@ -5413,6 +5438,7 @@ struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order,
 		.alloc_flags = alloc_flags,
 	};
 	unsigned int fastpath_alloc_flags = alloc_flags;
+	unsigned int zonelist_iter_cookie;
 
 	/* Other flags could be supported later if needed. */
 	if (WARN_ON(alloc_flags & ~(ALLOC_NOLOCK | ALLOC_NO_CODETAG)))
@@ -5442,6 +5468,7 @@ struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order,
 	 */
 	gfp = current_gfp_context(gfp);
 	alloc_gfp = gfp;
+	zonelist_iter_cookie = zonelist_iter_sample();
 	if (!prepare_alloc_pages(gfp, order, preferred_nid, nodemask, &ac,
 			&alloc_gfp, &fastpath_alloc_flags))
 		return NULL;
@@ -5458,6 +5485,11 @@ struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order,
 
 	/* First allocation attempt (or, for nolock, only attempt) */
 	page = get_page_from_freelist(alloc_gfp, order, fastpath_alloc_flags, &ac);
+	if (likely(page) && unlikely(check_retry_zonelist(zonelist_iter_cookie))) {
+		__free_frozen_pages(page, order,
+				    alloc_flags & ALLOC_NOLOCK ? FPI_NOLOCK : 0);
+		page = NULL;
+	}
 	if (likely(page) || (alloc_flags & ALLOC_NOLOCK))
 		goto out;
 
Signature · 1 line
2.56.0
author maintainer
#22 lore raw

PATCH v5 9/12 mm/hugetlb: retry the pool walk when the zonelist is rebuilt

In reply to Gregory Price (Meta)

Onlining the first block of a zone rebuilds every zonelist in place, shifting the slots after the new zone. dequeue_hugetlb_folio_nodemask() approves a slot by its node, then loads the zone from it, so a hugetlb fault bound to one node can take a pool page from another:

CPU 0 CPU 1
dequeue_hugetlb_folio_nodemask(nodemask {2})
slot 3 (NORMAL2) approved
online_pages()
build_all_zonelists()
[.., NORMAL1, NORMAL2] -> [.., MOVABLE1, NORMAL1, ...]
zone = zonelist_zone(slot 3) /* NORMAL1 */
dequeue_hugetlb_folio_node_exact(h, 1)

Check zonelist_update_seq before using each zone, and on the way out with nothing found, and restart the walk if it changed. Checking before the dequeue leaves nothing to put back. Reset the last-tried node on restart, so it is not skipped.

With kprobes holding the walk across the online, a fault bound to node 2 took a node 1 page in 3 of 3 runs, and a node 2 page in 3 of 3 with this change.

Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/hugetlb.c    | 13 ++++++++++---
 mm/page_alloc.c | 18 +-----------------
 mm/page_alloc.h | 19 +++++++++++++++++++
 3 files changed, 30 insertions(+), 20 deletions(-)
mm/hugetlb.c +10 -3
index da5d04edf7c1..b8a4fc2a996c 100644
@@ -1293,11 +1293,11 @@ static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,
 static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_mask,
 							int nid, nodemask_t *nmask)
 {
-	unsigned int cpuset_mems_cookie;
+	unsigned int cpuset_mems_cookie, zonelist_cookie;
 	struct zonelist *zonelist;
 	struct zone *zone;
 	struct zoneref *z;
-	int node = NUMA_NO_NODE;
+	int node;
 
 	/* 'nid' should not be NUMA_NO_NODE. Try to catch any misuse of it and rectifiy. */
 	if (nid == NUMA_NO_NODE)
@@ -1306,10 +1306,16 @@ static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_
 	zonelist = node_zonelist(nid, gfp_mask);
 
 retry_cpuset:
+	node = NUMA_NO_NODE;
 	cpuset_mems_cookie = read_mems_allowed_begin();
+	zonelist_cookie = zonelist_iter_begin();
 	for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
 		struct folio *folio;
 
+		/* A rebuild may have shifted a node outside @nmask into this slot. */
+		if (unlikely(check_retry_zonelist(zonelist_cookie)))
+			goto retry_cpuset;
+
 		if (!cpuset_zone_allowed(zone, gfp_mask))
 			continue;
 		/*
@@ -1324,7 +1330,8 @@ static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_
 		if (folio)
 			return folio;
 	}
-	if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
+	if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie) ||
+		     check_retry_zonelist(zonelist_cookie)))
 		goto retry_cpuset;
 
 	return NULL;
mm/page_alloc.c +1 -17
index f2f7e96bdef2..242bef2fe400 100644
@@ -4409,15 +4409,7 @@ EXPORT_SYMBOL_GPL(fs_reclaim_release);
  * retries the allocation if zonelist changes. Writer side is protected by the
  * embedded spin_lock.
  */
-static DEFINE_SEQLOCK(zonelist_update_seq);
-
-static unsigned int zonelist_iter_begin(void)
-{
-	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
-		return read_seqbegin(&zonelist_update_seq);
-
-	return 0;
-}
+DEFINE_SEQLOCK(zonelist_update_seq);
 
 /*
  * Never wait for the writer, which an ALLOC_NOLOCK caller in NMI may have
@@ -4432,14 +4424,6 @@ static unsigned int zonelist_iter_sample(void)
 	return 0;
 }
 
-static unsigned int check_retry_zonelist(unsigned int seq)
-{
-	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
-		return read_seqretry(&zonelist_update_seq, seq);
-
-	return 0;
-}
-
 /* Perform direct synchronous page reclaim */
 static unsigned long
 __perform_reclaim(gfp_t gfp_mask, unsigned int order,
mm/page_alloc.h +19 -0
index e238ee91ed24..10a40ead6c00 100644
@@ -9,6 +9,7 @@
 #include <linux/mm.h>
 #include <linux/mmzone.h>
 #include <linux/nodemask.h>
+#include <linux/seqlock.h>
 #include <linux/types.h>
 
 #define ALLOC_DEFAULT		0
@@ -265,6 +266,24 @@ extern void zone_pcp_disable(struct zone *zone);
 extern void zone_pcp_enable(struct zone *zone);
 extern void zone_pcp_init(struct zone *zone);
 
+extern seqlock_t zonelist_update_seq;
+
+static inline unsigned int zonelist_iter_begin(void)
+{
+	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
+		return read_seqbegin(&zonelist_update_seq);
+
+	return 0;
+}
+
+static inline unsigned int check_retry_zonelist(unsigned int seq)
+{
+	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
+		return read_seqretry(&zonelist_update_seq, seq);
+
+	return 0;
+}
+
 enum fallback_result {
 	/* Found suitable migratetype, *mt_out is valid. */
 	FALLBACK_FOUND,
Signature · 1 line
2.56.0
author maintainer
#23 lore raw

PATCH v5 10/12 mm/page_alloc: retry alloc_contig_pages() when the zonelist is rebuilt

In reply to Gregory Price (Meta)

Onlining the first block of a zone rebuilds every zonelist in place, shifting the slots after the new zone. alloc_contig_frozen_pages_noprof() approves a slot by the caller's gfp and nodemask, then loads the zone from it:

CPU 0 CPU 1
alloc_contig_frozen_pages_noprof(GFP_HIGHUSER | __GFP_THISNODE)
slot 0 (NORMAL1) approved
online_pages()
build_all_zonelists()
[NORMAL1]
-> [MOVABLE1, NORMAL1]
zone = zonelist_zone(slot 0) /* MOVABLE1 */
alloc_contig_frozen_range(MOVABLE1 pfns)

A gigantic hugetlb page can then land in ZONE_MOVABLE, and a nodemask caller can get a range from another node. Gigantic pages are kept out of ZONE_MOVABLE since commit 79b63f12abcb ("mm, hugetlb: do not allocate non-migrateable gigantic pages from movable zones").

Check zonelist_update_seq before using each zone, and restart the walk if it changed, as the hugetlb pool walk does. Check again before escalating to the pass that may migrate hugetlb pages, so a pass a rebuild cut short is redone rather than escalated.

With kprobes holding the walk across the online, the base kernel put the 1G page in ZONE_MOVABLE in 3 of 3 runs, and this change put it in ZONE_NORMAL in 3 of 3. Direct GFP_KERNEL calls with nodemask {1} and {2} got a ZONE_MOVABLE range and a node 1 range on base, 3 of 3 each, and neither with this change.

Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")
---
 mm/page_alloc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/page_alloc.c +9 -0
index 242bef2fe400..91ad979409e5 100644
@@ -7570,6 +7570,7 @@ struct page *alloc_contig_frozen_pages_noprof(unsigned long nr_pages,
 		gfp_t gfp_mask, int nid, nodemask_t *nodemask)
 {
 	unsigned long ret, pfn, flags;
+	unsigned int zonelist_cookie;
 	struct zonelist *zonelist;
 	struct zone *zone;
 	struct zoneref *z;
@@ -7578,8 +7579,13 @@ struct page *alloc_contig_frozen_pages_noprof(unsigned long nr_pages,
 
 retry:
 	zonelist = node_zonelist(nid, gfp_mask);
+	zonelist_cookie = zonelist_iter_begin();
 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
 					gfp_zone(gfp_mask), nodemask) {
+		/* A rebuild may have shifted an ineligible zone into this slot. */
+		if (unlikely(check_retry_zonelist(zonelist_cookie)))
+			goto retry;
+
 		spin_lock_irqsave(&zone->lock, flags);
 
 		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
@@ -7614,6 +7620,9 @@ struct page *alloc_contig_frozen_pages_noprof(unsigned long nr_pages,
 	 * second pass, we will try moving HugeTLB pages when those pages are
 	 * smaller than the requested contiguous region size.
 	 */
+	/* Redo a pass that a rebuild cut short before escalating. */
+	if (unlikely(check_retry_zonelist(zonelist_cookie)))
+		goto retry;
 	if (skip_hugetlb && skipped_hugetlb) {
 		skip_hugetlb = false;
 		goto retry;
Signature · 1 line
2.56.0
reviewer Acked-by on v5 10/12
#24 lore raw

Re: [PATCH v5 10/12] mm/page_alloc: retry alloc_contig_pages() when the zonelist is rebuilt

Quoted 31 lines · Gregory Price (Meta)

Onlining the first block of a zone rebuilds every zonelist in place, shifting the slots after the new zone. alloc_contig_frozen_pages_noprof() approves a slot by the caller's gfp and nodemask, then loads the zone from it:

CPU 0 CPU 1
alloc_contig_frozen_pages_noprof(GFP_HIGHUSER | __GFP_THISNODE)
slot 0 (NORMAL1) approved
online_pages()
build_all_zonelists()
[NORMAL1]
-> [MOVABLE1, NORMAL1]
zone = zonelist_zone(slot 0) /* MOVABLE1 */
alloc_contig_frozen_range(MOVABLE1 pfns)

A gigantic hugetlb page can then land in ZONE_MOVABLE, and a nodemask caller can get a range from another node. Gigantic pages are kept out of ZONE_MOVABLE since commit 79b63f12abcb ("mm, hugetlb: do not allocate non-migrateable gigantic pages from movable zones").

Check zonelist_update_seq before using each zone, and restart the walk if it changed, as the hugetlb pool walk does. Check again before escalating to the pass that may migrate hugetlb pages, so a pass a rebuild cut short is redone rather than escalated.

With kprobes holding the walk across the online, the base kernel put the 1G page in ZONE_MOVABLE in 3 of 3 runs, and this change put it in ZONE_NORMAL in 3 of 3. Direct GFP_KERNEL calls with nodemask {1} and {2} got a ZONE_MOVABLE range and a node 1 range on base, 3 of 3 each, and neither with this change.

Quoted 8 lines
Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")
---
 mm/page_alloc.c | 9 +++++++++
 1 file changed, 9 insertions(+)

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#25 lore raw

PATCH v5 11/12 mm/mempolicy: read the zoneref slot once in mempolicy_slab_node()

In reply to Gregory Price (Meta)

For a bind or preferred-many policy, mempolicy_slab_node() tests the first matching slot's zone for NULL, then reads the slot again to look up the zone's node. Offlining a zone's last block rebuilds the zonelists in place, so the second read can return the terminator:

CPU 0 CPU 1
mempolicy_slab_node()
zonelist_zone(slot 4)
= node 2 NORMAL
offline_pages()
build_all_zonelists()
slot 4 = NULL
zonelist_node_idx(slot 4)
zone_to_nid(NULL)

Current GCC merges the two reads, but nothing requires it. With mm/mempolicy.o built with -fno-inline -fno-ipa-sra, holding a bound task's slab_strict_numa allocation between the reads while a movable block earlier in the zonelist goes offline oopses every time:

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:zone_to_nid+0x0/0x10
  Call Trace:
   kmem_cache_alloc_noprof+0x294/0x610
   do_getname+0x34/0x230

mempolicy_slab_node() and zonelist_node_idx() are tail calls here, so neither appears in the trace.

Read the slot once and use that zone.

Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/mempolicy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/mempolicy.c +3 -1
index 0884c629670d..cd63c3e2c029 100644
@@ -2176,6 +2176,7 @@ unsigned int mempolicy_slab_node(void)
 	case MPOL_PREFERRED_MANY:
 	{
 		struct zoneref *z;
+		struct zone *zone;
 
 		/*
 		 * Follow bind policy behavior and start allocation at the
@@ -2186,7 +2187,8 @@ unsigned int mempolicy_slab_node(void)
 		zonelist = &NODE_DATA(node)->node_zonelists[ZONELIST_FALLBACK];
 		z = first_zones_zonelist(zonelist, highest_zoneidx,
 							&policy->nodes);
-		return zonelist_zone(z) ? zonelist_node_idx(z) : node;
+		zone = zonelist_zone(z);
+		return zone ? zone_to_nid(zone) : node;
 	}
 	case MPOL_LOCAL:
 		return node;
Signature · 1 line
2.56.0
reviewer Reviewed-by on v5 11/12
#26 lore raw

Re: [PATCH v5 11/12] mm/mempolicy: read the zoneref slot once in mempolicy_slab_node()

Quoted 31 lines · Gregory Price (Meta)

For a bind or preferred-many policy, mempolicy_slab_node() tests the first matching slot's zone for NULL, then reads the slot again to look up the zone's node. Offlining a zone's last block rebuilds the zonelists in place, so the second read can return the terminator:

CPU 0 CPU 1
mempolicy_slab_node()
zonelist_zone(slot 4)
= node 2 NORMAL
offline_pages()
build_all_zonelists()
slot 4 = NULL
zonelist_node_idx(slot 4)
zone_to_nid(NULL)

Current GCC merges the two reads, but nothing requires it. With mm/mempolicy.o built with -fno-inline -fno-ipa-sra, holding a bound task's slab_strict_numa allocation between the reads while a movable block earlier in the zonelist goes offline oopses every time:

  BUG: kernel NULL pointer dereference, address: 0000000000000060
  RIP: 0010:zone_to_nid+0x0/0x10
  Call Trace:
   kmem_cache_alloc_noprof+0x294/0x610
   do_getname+0x34/0x230

mempolicy_slab_node() and zonelist_node_idx() are tail calls here, so neither appears in the trace.

Read the slot once and use that zone.

Quoted 8 lines
Fixes 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists")
---
 mm/mempolicy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Signature · 2 lines
Best Regards,
Yan, Zi
author maintainer
#27 lore raw

PATCH v5 12/12 mm/mmzone: remove zonelist_node_idx()

In reply to Gregory Price (Meta)

zonelist_node_idx() looks like it returns a field of the zoneref, as zonelist_zone_idx() does, but it loads the zone from the slot again.

Memory hotplug rewrites zonelists in place, so a caller that has already checked the slot can get a different zone, or the NULL terminator, from that second load.

__next_zones_zonelist() and mempolicy_slab_node() had this bug.

Remove the helper and open-code it in its two remaining callers, neither of which can race with a rebuild. local_memory_node() runs either inside __build_all_zonelists() or during CPU bring-up, which holds cpu_hotplug_lock for write and so excludes mem_hotplug_begin().

The lookup in mpol_misplaced() is unreachable: it needs a policy with MPOL_F_MOF but not MPOL_F_MORON, and nothing sets one without the other.

---
 include/linux/mmzone.h | 6 ------
 mm/mempolicy.c         | 2 +-
 mm/page_alloc.c        | 2 +-
 3 files changed, 2 insertions(+), 8 deletions(-)
include/linux/mmzone.h +0 -6
index 4bc2318ac37f..72ffb2959c4f 100644
@@ -1439,7 +1439,6 @@ struct zoneref {
  *
  * zonelist_zone()	- Return the struct zone * for an entry in _zonerefs
  * zonelist_zone_idx()	- Return the index of the zone for an entry
- * zonelist_node_idx()	- Return the index of the node for an entry
  */
 struct zonelist {
 	struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
@@ -1864,11 +1863,6 @@ static inline int zonelist_zone_idx(const struct zoneref *zoneref)
 	return zoneref->zone_idx;
 }
 
-static inline int zonelist_node_idx(const struct zoneref *zoneref)
-{
-	return zone_to_nid(zoneref->zone);
-}
-
 struct zoneref *__next_zones_zonelist(struct zoneref *z,
 					enum zone_type highest_zoneidx,
 					const nodemask_t *nodes);
mm/mempolicy.c +1 -1
index cd63c3e2c029..267576054980 100644
@@ -3087,7 +3087,7 @@ int mpol_misplaced(struct folio *folio, struct vm_fault *vmf,
 				node_zonelist(thisnid, GFP_HIGHUSER),
 				gfp_zone(GFP_HIGHUSER),
 				&pol->nodes);
-		polnid = zonelist_node_idx(z);
+		polnid = zone_to_nid(zonelist_zone(z));
 		break;
 
 	default:
mm/page_alloc.c +1 -1
index 91ad979409e5..81c1de714658 100644
@@ -5973,7 +5973,7 @@ int local_memory_node(int node)
 	z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
 				   gfp_zone(GFP_KERNEL),
 				   NULL);
-	return zonelist_node_idx(z);
+	return zone_to_nid(zonelist_zone(z));
 }
 #endif
 
Signature · 1 line
2.56.0
reviewer Reviewed-by on v5 12/12
#28 lore raw

Re: [PATCH v5 12/12] mm/mmzone: remove zonelist_node_idx()

Quoted 17 lines · Gregory Price (Meta)

zonelist_node_idx() looks like it returns a field of the zoneref, as zonelist_zone_idx() does, but it loads the zone from the slot again.

Memory hotplug rewrites zonelists in place, so a caller that has already checked the slot can get a different zone, or the NULL terminator, from that second load.

__next_zones_zonelist() and mempolicy_slab_node() had this bug.

Remove the helper and open-code it in its two remaining callers, neither of which can race with a rebuild. local_memory_node() runs either inside __build_all_zonelists() or during CPU bring-up, which holds cpu_hotplug_lock for write and so excludes mem_hotplug_begin().

The lookup in mpol_misplaced() is unreachable: it needs a policy with MPOL_F_MOF but not MPOL_F_MORON, and nothing sets one without the other.

Quoted 7 lines
---
 include/linux/mmzone.h | 6 ------
 mm/mempolicy.c         | 2 +-
 mm/page_alloc.c        | 2 +-
 3 files changed, 2 insertions(+), 8 deletions(-)

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
maintainer Applied
#29 lore raw
In reply to Gregory Price (Meta)
Quoted 129 lines · Gregory Price (Meta)

Memory hotplug rebuilds zonelists in place while allocations walk them, and offlining an emptied zone frees pagesets the vmstat worker can still hold.

These eleven fixes cover the walkers that crash, fail, or pick the wrong zone or node as a result, and a twelfth patch removes a helper that invites the same bug.

1. a restarted slowpath loses its retry with reserves
2. offlining an emptied zone frees pagesets still in use
3. the reclaim_retry_zone tracepoint rereads a zoneref slot
4. the zonelist retry is compiled out without MEMORY_HOTREMOVE
5. zonelist_zone() may load its slot more than once
6. NULL preferred zone after a shrink rebuild
7. __next_zones_zonelist() rereads a slot
8. allocations land outside their zone or nodemask
9. the hugetlb pool walk takes another node's page
10. alloc_contig_pages() takes a range outside its filters
11. mempolicy_slab_node() rereads a slot
12. zonelist_node_idx() invites the same double read

Patches 1-11 are for stable. Patch 12 is a cleanup and is not.

Patches 8-10 need patch 4, without which their retry is compiled out on HOTREMOVE=n kernels.

Patch 8 needs patch 1.

Patches 6, 7 and 11 need patch 5, which makes zonelist_zone() read the slot once. Patch 12 needs patches 7 and 11, which remove the other callers of zonelist_node_idx().

Patches 2 and 3 are patches from anthropic, pulled in from Vlastimil's series to keep the backport issues self-contained on the series.

Testing
=======

Each race was reproduced on unmodified source in a QEMU guest with three NUMA nodes and a 512M pc-dimm on node 1, left offline at boot.

A test module widens each window with a kprobe that stalls one CPU inside it, as an interrupt or a vCPU preemption would.

       base                            this series
patch  outcome                  runs   outcome                  runs
=====  =======================  =====  =======================  =====
 1     NULL returned            3/3    page from reserves       3/3
 2     UAF                      6/6    no UAF                   3/3
 3     NULL dereference         3/3    allocation succeeds      3/3
 4     allocation fails         3/3    waits for the rebuild    3/3
 6     NULL dereference         3/3    ZONE_NORMAL page         3/3
 7     NULL dereference         3/3    ZONE_NORMAL page         3/3
 8     wrong zone or node       18/18  right zone and node      18/18
 9     page from node 1         3/3    page from node 2         3/3
10     1G page in ZONE_MOVABLE  3/3    1G page in ZONE_NORMAL   3/3
11     NULL dereference         4/4    keeps allocating         3/3

Cost (these tests were run on v4, v5 hasn't substantially changed)
====
Kernel instructions per order-0 allocation and free in the QEMU guest, counted with a task-bound hardware instruction counter around 1M iterations on a pinned kthread, non-debug kernel, mean of 15 rounds.

instructions per alloc+free
base this series

  alloc_pages()             890.5         900.5
  bulk, nr=1, nodemask      865.5         875.5
  bulk, nr=16, per page     477.2         478.1

Bare metal, two-socket Xeon Gold 6138 (Skylake-SP, 80 CPUs).

will-it-scale page_fault1-3: each iteration touches one page, so the unit is millions of faults per second, summed over all tasks.

Base, millions of faults per second:

tasks 1 10 40 80

  page_fault1, processes     0.46   4.16  10.51   9.23
  page_fault1, threads       0.46   3.51   4.35   4.19
  page_fault2, processes     0.43   3.66   9.02   8.33
  page_fault2, threads       0.43   3.48   4.37   4.19
  page_fault3, processes     3.89  14.78   8.10   8.00
  page_fault3, threads       3.87  14.29   6.88   7.96

Change in faults per second with this series, + is faster:

tasks 1 10 40 80

  page_fault1, processes    -0.3%  +0.1%  -3.0%  -1.7%
  page_fault1, threads      -0.5%  -1.6%  -0.9%  -0.9%
  page_fault2, processes    +0.0%  +0.9%  -1.4%  -0.5%
  page_fault2, threads      +0.3%  +1.5%  -1.4%  -1.0%
  page_fault3, processes    -1.0%  -3.8%  -0.7%  +1.2%
  page_fault3, threads      -0.9%  -0.7%  +0.8%  -0.2%

The boot-to-boot stddev is 1.0-1.8% at 1 task and up to 15% in 10-80 tasks, so basically this is all within noise.

Changes in v5
=============
- Pulled in patches from Vlastimil's series (patches 2 and 3 here)
- inline functions (Vlastimil)
- Added patch 5: zonelist_zone() reads the slot with READ_ONCE(), as recommended by sashiko on v4's mempolicy patch.
Patches 6, 7 and 11 call it instead of open-coding the load.
- Added patch 12, removing zonelist_node_idx(). Not for stable.
- Patch 1: reworded the changelog (Vlastimil).
- Collected Reviewed-by tags.

Gregory Price (Meta) (10):
mm/page_alloc: reset can_retry_reserves when the slowpath restarts mm/page_alloc: detect zonelist rebuilds from memory hot-add mm/mmzone: read the zoneref slot once in zonelist_zone() mm/page_alloc: snapshot the preferred zone against shrink rebuilds mm/mmzone: read each zoneref slot once when filtering by nodemask mm/page_alloc: retry allocations that raced with a zonelist rebuild mm/hugetlb: retry the pool walk when the zonelist is rebuilt mm/page_alloc: retry alloc_contig_pages() when the zonelist is rebuilt mm/mempolicy: read the zoneref slot once in mempolicy_slab_node() mm/mmzone: remove zonelist_node_idx()

Quoted 3 lines

Josef Bacik (2):
mm/page_alloc: keep the pagesets of an empty zone mm, page_alloc: pass zone pointer to trace_reclaim_retry_zone

Thanks, applied to mm/page_alloc [1] page_alloc/for-next-fixes

It should result in no conflicts with other trees AFAICS.

Following David's MAINTAINERS updates [2] and mm-next call discussions, we'll move ahead with the page_alloc.git tree (integrated to mm-next) now to spread the handling of the endless fixes stream. I'll send the MAINTAINERS update soon.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/mm/page_alloc.git
[2]
https://lore.kernel.org/all/[email protected]/