mm: fix allocator races with memory hotplug
Subsystems (8)
v3 cover mm: fix allocator races with memory hotplug
Eight fixes for allocator paths that can act on state another CPU is
updating: zonelists rebuilt by memory hotplug, and the pagesets of a zone that offlining empties.
I've packaged these together because they're all related to hotplug.
*** Backport note - Patches 5-7 build on patch 1: without it, their
retry is compiled out when CONFIG_MEMORY_HOTREMOVE=n.
Most of these race conditions stem from zonelists being re-ordered in-place at runtime during hotplug. The result is a mixture of failed allocations, NULL dereferences, incorrect placement (GFP_KERNEL in ZONE_MOVABLE), node misplacement, or incorrectly allowing hugetlb / contig allocations on a node/zone otherwise not eligible.
Most of these are incredibly narrow race conditions, but since the most likely impacted machines are VMs with memory hotplug, the narrow condition can more easily occur when a vCPU takes a long interrupt / VM exit of some kind. I've made all of these race conditions deterministics with a kprobe reproduction module described below.
Reproducers
===========
All eight races were reproduced on unmodified source in a QEMU guest with three NUMA nodes:
node 0: 3G, CPUs 0-1
node 1: 1G ZONE_NORMAL, CPU 2
node 2: 1G ZONE_NORMAL, CPU 3
And a 512M pc-dimm cold-plugged on node 1, left offline at boot (CONFIG_ACPI_HOTPLUG_MEMORY=y).
Patch 1 needs CONFIG_MEMORY_HOTREMOVE=n to show the failure Patch 3 needs mm/mmzone.o built with -fno-inline -fno-ipa-sra,
without which the two reads are merged.
Patch 4 needs the reclaim_retry_zone tracepoint enabled. Patch 7's hugetlb case needs a 64G guest (node 1: 4G) so that one
memory block is 1G, with a 2G pc-dimm on node 1.
The windows are a few instructions wide, so I used a test module to widen them with kprobes that stall one CPU inside the window. This emulates an interrupt or vCPU preemption. The vCPU preemption is the more likely event to cause this at large scale with memory hotplug.
Patch 1 (zonelist):
Online node 1's first memory block as ZONE_MOVABLE, which rebuilds node 1's zonelists.
Stall the rebuild after it writes ZONE_MOVABLE into the first entry but before it writes ZONE_NORMAL into the second.
On another CPU, allocate GFP_KERNEL | __GFP_THISNODE from node 1.
Before: the allocation fails, 3/3 runs.
After: the allocation waits for the rebuild and succeeds, 3/3 runs.
Patch 2 (preferred zone):
Online node 1's block as ZONE_MOVABLE, so node 1's local zonelist is [MOVABLE, NORMAL].
On another CPU, allocate GFP_KERNEL | __GFP_THISNODE from node 1, whose preferred slot is NORMAL, and stall it after it loads the zone.
Offline the block, which rebuilds the list as [NORMAL] and moves the terminator into the preferred slot.
Before: NULL dereference in get_page_from_freelist(), 3/3 runs.
After: the allocation succeeds from ZONE_NORMAL, 3/3 runs.
Patch 3 (slot reread):
Online node 1's block as ZONE_MOVABLE, so node 1's local zonelist is [MOVABLE, NORMAL].
On another CPU, allocate GFP_KERNEL | __GFP_THISNODE from node 1 with nodemask {1}, and stall it after __next_zones_zonelist() tests the NORMAL slot for NULL but before it reads the slot again.
Offline the block, which rebuilds the list as [NORMAL] and moves the terminator into that slot.
Before: NULL dereference in zone_to_nid(), 3/3 runs.
After: the allocation succeeds from ZONE_NORMAL, 3/3 runs.
Patch 4 (tracepoint):
Online node 1's block as ZONE_MOVABLE, so node 1's local zonelist is [MOVABLE, NORMAL], and enable the reclaim_retry_zone tracepoint.
On another CPU, allocate GFP_KERNEL | __GFP_THISNODE from node 1, with its get_page_from_freelist() calls failed so that the slowpath reaches should_reclaim_retry(), and stall it in zone_reclaimable_pages() for node 1's NORMAL zone.
Offline the block, which rebuilds the list as [NORMAL] and moves the terminator into that slot.
Before: NULL dereference in the tracepoint, 3/3 runs. With the
tracepoint disabled, no oops (1 run).
After: the allocation succeeds from ZONE_NORMAL, 3/3 runs.
Patch 5 (zonelist slots):
On one CPU, allocate GFP_KERNEL with a nodemask, and stall it after the iterator approves its first zonelist slot but before the allocator loads the zone from that slot.
Online node 1's first memory block as ZONE_MOVABLE, which inserts ZONE_MOVABLE at the front of node 1's zonelists.
This was run for single-page and bulk allocations, and for a single-page allocation whose fast path is made to fail so that the slowpath's walk races.
Before: nodemask {1} gets a node 1 ZONE_MOVABLE page and nodemask
{2} a node 1 ZONE_NORMAL page, 18/18 runs.
After: nodemask {1} gets node 1 ZONE_NORMAL and nodemask {2} gets
node 2 ZONE_NORMAL, 18/18 runs.The check adds 5 instructions to an order-0 allocation on x86, counted with a hardware counter on a non-debug build.
Patch 6 (hugetlb):
Reserve hugetlb pages on every node. On CPU 1, fault a hugetlb page bound to node 2 with mbind(); the pool walk uses node 0's zonelist, where node 2 follows node 1. Stall the walk after it approves node 2's slot.
Online node 1's first memory block as ZONE_MOVABLE, which shifts node 1's ZONE_NORMAL into that slot.
Before: the page comes from node 1, 3/3 runs.
After: the page comes from node 2, 3/3 runs.
Patch 7 (alloc_contig):
Write 1 to node 1's 1G nr_hugepages, and stall the gigantic page's zonelist walk after it approves node 1's ZONE_NORMAL slot.
Online node 1's first memory block as ZONE_MOVABLE, which inserts ZONE_MOVABLE into that slot.
Before: the 1G page is allocated from ZONE_MOVABLE, 3/3 runs.
After: the 1G page is allocated from ZONE_NORMAL, 3/3 runs.
Calling alloc_contig_pages() directly with GFP_KERNEL in the first guest, as in patch 5, nodemask {1} gets a ZONE_MOVABLE range and nodemask {2} a node 1 range before, 3/3 runs each, and neither after.
Patch 8 (pagesets):
Online node 1's block as ZONE_MOVABLE, then offline it.
Stall the offline before the zone empties, and stall the vmstat worker on another CPU (kicked through /proc/sys/vm/stat_refresh) in decay_pcp_high() for that zone. Let the offline free the pagesets, reallocate the freed percpu area with a known value, then release the worker.
Before: decay_pcp_high() runs on the freed pageset, 6/6 runs. In
1/6 the area was reallocated before the worker resumed, and decay_pcp_high() rewrote the new object.
After: the pageset is not freed, 3/3 runs.
All eight reproducers also pass on the series tip, 3/3 runs each.
Changes in v3
=============
- Replaced v2 patch 2 (zonelist_zone_valid() recheck) with patch 5, which checks zonelist_update_seq whenever an allocation succeeds (Vlastimil). A raced attempt's page is discarded, so a stale preferred zone can no longer affect the result (Sashiko).
- Moved the NULL dereference fixes ahead of patch 5.
- Added patches 6 and 7 for the hugetlb pool walk and alloc_contig_pages(), which have the same slot shift.
- Added patch 4 for the reclaim_retry_zone tracepoint's re-read.
- Fixes: tags on patches 2-6 now name the stop_machine removal that made rebuilds run concurrently with zonelist walks.
Gregory Price (Meta) (8):
mm/page_alloc: detect zonelist rebuilds from memory hot-add mm/page_alloc: snapshot the preferred zone against shrink rebuilds mm/mmzone: read each zoneref slot once when filtering by nodemask mm/page_alloc: pass the zone, not the zoneref, to reclaim_retry_zone 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/page_alloc: keep a zone's pagesets when it goes empty
include/trace/events/oom.h | 8 +-- mm/hugetlb.c | 13 +++-- mm/mmzone.c | 11 ++-- mm/page_alloc.c | 106 +++++++++++++++++++++++++++++-------- mm/page_alloc.h | 3 ++ 5 files changed, 107 insertions(+), 34 deletions(-)
Signature · 1 line
2.53.0-Meta
PATCH v3 1/8 mm/page_alloc: detect zonelist rebuilds from memory hot-add
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.
--- mm/page_alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/page_alloc.c +2 -2
@@ -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.53.0-Meta
PATCH v3 2/8 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.
--- mm/page_alloc.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
mm/page_alloc.c +23 -8
@@ -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 =READ_ONCE (ac->preferred_zoneref->zone); +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);
@@ -5183,6 +5192,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;
@@ -5231,6 +5241,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 =READ_ONCE (ac.preferred_zoneref->zone); +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) {
@@ -5241,8 +5256,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; }
@@ -5250,7 +5265,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 ; }
@@ -5309,7 +5324,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.53.0-Meta
PATCH v3 3/8 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.
--- mm/mmzone.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
mm/mmzone.c +6 -5
@@ -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(READ_ONCE(z->zone), nodes)) z++;return z;
Signature · 1 line
2.53.0-Meta
PATCH v3 4/8 mm/page_alloc: pass the zone, not the zoneref, to reclaim_retry_zone
In reply to Gregory Price (Meta)should_reclaim_retry() hands the reclaim_retry_zone tracepoint the zoneref it is walking, and the tracepoint reads the slot again for the zone's node and index. Offlining a zone's last block rebuilds the zonelists in place, and the shrunk list's NULL terminator can land on that slot in between:
CPU 0 CPU 1
should_reclaim_retry()
zone = zonelist_zone(slot 1) /* NORMAL1 */
zone_reclaimable_pages(zone)
offline_pages()
build_all_zonelists()
[MOVABLE1, NORMAL1, NULL] -> [NORMAL1, NULL]
trace_reclaim_retry_zone(slot 1)
zonelist_node_idx(slot 1)
zone_to_nid(NULL)
BUG: kernel NULL pointer dereference, address: 0000000000000060 RIP: 0010:trace_event_raw_event_reclaim_retry_zone+0x57/0xd0 Call Trace: __alloc_pages_slowpath.constprop.0+0xa80/0x16d0 __alloc_frozen_pages_noprof+0x2d5/0x360 alloc_pages_node_noprof+0x53/0xe0
The second read follows zone_reclaimable_pages() and a per-CPU vmstat snapshot, so unlike the iterator's double read no compiler merges it.
Pass the zone the loop already loaded. The event's fields are unchanged.
With the tracepoint enabled and kprobes holding should_reclaim_retry() across the offline, the base kernel oopsed in 3 of 3 runs and this change in none.
--- 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
@@ -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
@@ -4679,7 +4679,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.53.0-Meta
PATCH v3 5/8 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. A GFP_KERNEL request can then land in ZONE_MOVABLE, and a nodemask request can land on a node outside its mask.
The iterator approves a slot by its zone index and node, and the allocator loads the zone from the slot afterwards:
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, and this race ends in a successful allocation.
Check the count whenever an allocation succeeds, and free the page and retry if it changed, as the memcg charge failure path already frees a fresh page.
The fast and bulk paths sample the count without waiting for a writer: a sample taken during a write only costs a retry, and a fastpath ALLOC_NOLOCK caller can be an NMI that interrupted the writer. They read the seqcount inside the seqlock, because on PREEMPT_RT the seqlock accessor takes the spinlock while a write is in progress, which ALLOC_NOLOCK cannot do.
check_retry_zonelist() now returns 0 when the check is compiled out, so a backport without the hot-add change stays harmless.
With kprobes holding the walk across the online, the base kernel misplaced the page in all 18 runs, through the fast path, the slowpath and the bulk allocator, and none was misplaced with this change. The check adds 5 instructions to an order-0 allocation on x86.
--- mm/page_alloc.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
mm/page_alloc.c +33 -1
@@ -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 */
@@ -5114,6 +5127,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; }
@@ -5198,6 +5217,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 ;/*
@@ -5238,6 +5258,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;
@@ -5287,6 +5308,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)
@@ -5427,6 +5452,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 )))
@@ -5456,6 +5482,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 ;
@@ -5472,6 +5499,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.53.0-Meta
[email protected]
AI reviewRe: [PATCH v3 5/8] mm/page_alloc: retry allocations that raced with a zonelist rebuild
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] mm/page_alloc: __alloc_pages_slowpath() restart loses reserve retry - [Medium] mm/page_alloc…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] mm/page_alloc: __alloc_pages_slowpath() restart loses reserve retry
- [Medium] mm/page_alloc: post-rmqueue() zonelist retry leaves ZONE_MOVABLE blocks unmovable --
commit 3b023f87050323ef9781701d5864437d2b2b4b5e
Author: Gregory Price (Meta) <[email protected]>
mm/page_alloc: retry allocations that raced with a zonelist rebuild
This commit checks zonelist_update_seq after page allocation succeeds in the fast path, slowpath, and bulk allocator, freeing the allocated page and retrying if a concurrent zonelist rebuild occurred during the zonelist walk.
Quoted 4 lines
mm/page_alloc.c +0
-0
[ ... ]
mm/page_alloc.c __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, · line 5137
Quoted 12 lines
mm/page_alloc.c +6
-0@@ -5114,6 +5127,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; }
[Severity: Medium]
In __alloc_pages_slowpath() in mm/page_alloc.c, can_retry_reserves is initialized to true only once before the restart: label:
bool compact_first = false; bool can_retry_reserves = true; unsigned long alloc_start_time = jiffies;
When a reserve-eligible allocation (such as PF_MEMALLOC, __GFP_MEMALLOC, or an OOM victim) fails its initial get_page_from_freelist() attempt at retry:, it sets reserve_flags in alloc_flags, clears ac->nodemask, sets can_retry_reserves = false, and jumps to retry:
reserve_flags = __gfp_pfmemalloc_flags(gfp_mask);
if (reserve_flags)
alloc_flags = alloc_flags_cma(gfp_mask) | reserve_flags |
ac->alloc_flags | (alloc_flags & ALLOC_KSWAPD);
if (!(alloc_flags & ALLOC_CPUSET) || reserve_flags) {
ac->nodemask = NULL;
ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
ac->highest_zoneidx, ac->nodemask);
if (can_retry_reserves) {
can_retry_reserves = false;
goto retry;
}
}If that reserve pass allocates a page and reaches got_pg: while check_retry_zonelist(zonelist_iter_cookie) is true, the new check frees the page and jumps to restart:.
At restart:, alloc_flags is reinitialized without reserve_flags:
alloc_flags = ac->alloc_flags | alloc_flags_slowpath(gfp_mask, order);
Because can_retry_reserves is still false (and ac->nodemask remains NULL), if the first get_page_from_freelist() after restart: fails without reserve_flags, the can_retry_reserves retry is skipped. Callers with !can_direct_reclaim (for example GFP_NOWAIT | __GFP_MEMALLOC or GFP_ATOMIC | __GFP_MEMALLOC) or current->flags & PF_MEMALLOC then jump directly to nopage: and fail without ever retrying get_page_from_freelist() with reserve_flags on the rebuilt zonelist.
Could jumping to restart: from got_pg: without resetting can_retry_reserves (and restoring ac->nodemask) cause reserve-eligible allocations to lose their reserve retry and fail spuriously?
[ ... ]
mm/page_alloc.c struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order, · line 5509
Quoted 12 lines
mm/page_alloc.c +5
-0@@ -5472,6 +5499,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;
[Severity: Medium]
When a non-movable request (such as GFP_KERNEL with MIGRATE_UNMOVABLE or MIGRATE_RECLAIMABLE) races with online_pages() -> build_all_zonelists() and get_page_from_freelist() selects ZONE_MOVABLE, rmqueue() runs on ZONE_MOVABLE before check_retry_zonelist() is checked in __alloc_frozen_pages_noprof() or at got_pg: in __alloc_pages_slowpath().
Because ZONE_MOVABLE has no free pages in MIGRATE_UNMOVABLE, rmqueue() -> rmqueue_pcplist() -> __rmqueue_pcplist() -> rmqueue_bulk() -> __rmqueue() -> __rmqueue_claim() -> try_to_claim_block() claims a MIGRATE_MOVABLE block in ZONE_MOVABLE, converts its pageblock migratetype to start_type, and expands the remaining pages into ZONE_MOVABLE's non-movable freelists and PCP list:
/* Take ownership for orders >= pageblock_order */
if (current_order >= pageblock_order) {
unsigned int nr_added;
del_page_from_free_list(page, zone, current_order, block_type);
change_pageblock_range(page, current_order, start_type);
nr_added = expand(zone, page, order, current_order, start_type);
account_freepages(zone, nr_added, start_type);
return page;
}When check_retry_zonelist() then detects the race and calls __free_frozen_pages(), __free_frozen_pages() reads the mutated pageblock migratetype:
zone = page_zone(page); migratetype = get_pfnblock_migratetype(page, pfn);
and frees only that single page back to ZONE_MOVABLE's MIGRATE_UNMOVABLE PCP list or buddy freelist.
Can checking check_retry_zonelist() only after rmqueue() has already mutated the zone leave converted MIGRATE_UNMOVABLE, MIGRATE_RECLAIMABLE, or MIGRATE_HIGHATOMIC pageblocks and populated non-movable PCP/free lists inside ZONE_MOVABLE, where normal movable allocations will not consume the PCP list and compaction will skip the non-movable pageblocks?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v3 6/8 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. A hugetlb fault bound to one node can then take a pool page from another.
dequeue_hugetlb_folio_nodemask() picks pool nodes by walking a zonelist with the policy nodemask. The iterator approves a slot by its node, and the walk loads the zone from the slot afterwards:
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 the walk returns, 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 node the walk last tried on every restart, so a restart does not skip it.
With kprobes holding the walk across the online, a hugetlb fault bound to node 2 took its page from node 1 in 3 of 3 runs, and from node 2 in 3 of 3 runs with this change.
--- mm/hugetlb.c | 13 ++++++++++--- mm/page_alloc.c | 4 ++-- mm/page_alloc.h | 3 +++ 3 files changed, 15 insertions(+), 5 deletions(-)
mm/hugetlb.c +10 -3
@@ -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 +2 -2
@@ -4411,7 +4411,7 @@ EXPORT_SYMBOL_GPL(fs_reclaim_release);*/ static DEFINE_SEQLOCK (zonelist_update_seq); -static unsigned int zonelist_iter_begin (void ) +unsigned int zonelist_iter_begin (void ) {if (IS_ENABLED (CONFIG_MEMORY_HOTPLUG ))return read_seqbegin (&zonelist_update_seq);
@@ -4432,7 +4432,7 @@ static unsigned int zonelist_iter_sample(void)return 0 ; } -static unsigned int check_retry_zonelist (unsigned int seq) +unsigned int check_retry_zonelist (unsigned int seq) {if (IS_ENABLED (CONFIG_MEMORY_HOTPLUG ))return read_seqretry (&zonelist_update_seq, seq);
mm/page_alloc.h +3 -0
@@ -265,6 +265,9 @@ extern void zone_pcp_disable(struct zone *zone);extern void zone_pcp_enable (struct zone *zone);extern void zone_pcp_init (struct zone *zone); +unsigned int zonelist_iter_begin (void ); +unsigned int check_retry_zonelist (unsigned int seq); +enum fallback_result {/* Found suitable migratetype, *mt_out is valid. */ FALLBACK_FOUND ,
Signature · 1 line
2.53.0-Meta
PATCH v3 7/8 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_pages() can then take a range from a zone or node its filters rejected.
Writing 1 to a node's 1G nr_hugepages while that node's first ZONE_MOVABLE block comes online puts the gigantic page in ZONE_MOVABLE, which commit 79b63f12abcb ("mm, hugetlb: do not allocate non-migrateable gigantic pages from movable zones") excludes.
alloc_contig_frozen_pages_noprof() walks the zonelist with the caller's gfp and nodemask, and the iterator approves a slot before the walk 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)
Check zonelist_update_seq before using each zone the walk returns, and on the way out with nothing found, and restart the walk if it changed, as the hugetlb pool walk does.
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.
Calling alloc_contig_pages() directly, GFP_KERNEL with nodemask {1} got a ZONE_MOVABLE range and nodemask {2} a node 1 range in 3 of 3 runs each, and neither did with this change.
--- mm/page_alloc.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/page_alloc.c +8 -0
@@ -7592,6 +7592,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;
@@ -7600,8 +7601,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);
@@ -7640,6 +7646,8 @@ struct page *alloc_contig_frozen_pages_noprof(unsigned long nr_pages, skip_hugetlb = false; goto retry; } +if (unlikely (check_retry_zonelist (zonelist_cookie))) + goto retry;return NULL ; }EXPORT_SYMBOL (alloc_contig_frozen_pages_noprof);
Signature · 1 line
2.53.0-Meta
PATCH v3 8/8 mm/page_alloc: keep a zone's pagesets when it goes empty
In reply to Gregory Price (Meta)When offlining empties a zone, zone_pcp_reset() frees its pagesets while the vmstat worker can still hold one across cond_resched():
CPU 0 CPU 1
refresh_cpu_vm_stats()
pcp = zone->per_cpu_pageset
cond_resched()
offline_pages()
zone_pcp_reset()
free_percpu(zone->per_cpu_pageset)
decay_pcp_high(zone, this_cpu_ptr(pcp))decay_pcp_high() then reads, and can write, memory the percpu allocator may already have handed out again. With kprobes holding the worker across the offline, it used the freed pageset in 6 of 6 runs, and rewrote a new allocation at the freed address in 1 of them.
Keep the pagesets for the life of the zone, as pgdat->per_cpu_nodestats already is, and only fold the stats in zone_pcp_reset().
Commit 340175b7d14d ("mm/hotplug: free zone->pageset when a zone becomes empty") freed them because onlining the zone again allocated a new set and leaked the old one. setup_zone_pageset() now reuses the existing set, and zone_pcp_init() keeps it when the node is re-added, so nothing is allocated twice. The pageset address stayed the same across repeated offline/online cycles and across removing and re-adding the node.
The pagesets take 320 bytes per possible CPU: 30KiB per zone with 96 CPUs, 160KiB with 512 and 2.5MiB with 8192. A populated zone already holds this memory, we just no longer give it back on offline.
--- mm/page_alloc.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
mm/page_alloc.c +15 -8
@@ -6369,6 +6369,12 @@ void __meminit setup_zone_pageset(struct zone *zone) {int cpu; +/* Reuse the pagesets zone_pcp_reset() kept. */ +if (zone->per_cpu_pageset != &boot_pageset) { +zone_set_pageset_high_and_batch (zone,0 ); +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);
@@ -6465,9 +6471,12 @@ __meminit void zone_pcp_init(struct zone *zone)* per cpu subsystem is not up at this point. The following code * relies on the ability of the linker to provide the * offset of a (static) per cpu variable into the per cpu area. +* A re-added node keeps the pagesets zone_pcp_reset() kept. */ - zone->per_cpu_pageset = &boot_pageset; - zone->per_cpu_zonestats = &boot_zonestats; +if (!zone->per_cpu_pageset || zone->per_cpu_pageset == &boot_pageset) { + zone->per_cpu_pageset = &boot_pageset; + zone->per_cpu_zonestats = &boot_zonestats; + } zone->pageset_high_min =BOOT_PAGESET_HIGH ; zone->pageset_high_max =BOOT_PAGESET_HIGH ; zone->pageset_batch =BOOT_PAGESET_BATCH ;
@@ -7749,6 +7758,10 @@ void zone_pcp_enable(struct zone *zone)mutex_unlock (&pcp_batch_high_lock); } +/* +* Keep the pagesets of an emptied zone: refresh_cpu_vm_stats() can hold +* one across cond_resched(), and nothing waits for it to let go. +*/ void zone_pcp_reset (struct zone *zone) {int cpu;
@@ -7759,12 +7772,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.53.0-Meta
David Hildenbrand (Arm)
maintainerOn 10/21/26 04:39, Gregory Price (Meta) wrote:
Usual reminder to slow down a bit :)
We have
Documentation/process/submitting-patches.rst
Saying
"Wait for a minimum of one week before resubmitting or pinging reviewers
- possibly longer during busy times like merge windows.
A lot of this here was broken forever, so there is no need to rush.
(thanks for the fixes ;) )
Quoted 204 lines
Eight fixes for allocator paths that can act on state another CPU is
updating: zonelists rebuilt by memory hotplug, and the pagesets of a zone that offlining empties.I've packaged these together because they're all related to hotplug.
*** Backport note - Patches 5-7 build on patch 1: without it, their
retry is compiled out whenCONFIG_MEMORY_HOTREMOVE=n.Most of these race conditions stem from zonelists being re-ordered in-place at runtime during hotplug. The result is a mixture of failed allocations, NULL dereferences, incorrect placement (
GFP_KERNELinZONE_MOVABLE), node misplacement, or incorrectly allowing hugetlb / contig allocations on a node/zone otherwise not eligible.Most of these are incredibly narrow race conditions, but since the most likely impacted machines are VMs with memory hotplug, the narrow condition can more easily occur when a vCPU takes a long interrupt / VM exit of some kind. I've made all of these race conditions deterministics with a kprobe reproduction module described below.
Reproducers
===========All eight races were reproduced on unmodified source in a QEMU guest with three NUMA nodes:
node 0: 3G, CPUs 0-1
node 1: 1GZONE_NORMAL, CPU 2
node 2: 1GZONE_NORMAL, CPU 3And a 512M pc-dimm cold-plugged on node 1, left offline at boot (
CONFIG_ACPI_HOTPLUG_MEMORY=y).Patch 1 needs
CONFIG_MEMORY_HOTREMOVE=nto show the failure Patch 3 needsmm/mmzone.obuilt with -fno-inline -fno-ipa-sra,
without which the two reads are merged.
Patch 4 needs thereclaim_retry_zonetracepoint enabled. Patch 7's hugetlb case needs a 64G guest (node 1: 4G) so that one
memory block is 1G, with a 2G pc-dimm on node 1.The windows are a few instructions wide, so I used a test module to widen them with kprobes that stall one CPU inside the window. This emulates an interrupt or vCPU preemption. The vCPU preemption is the more likely event to cause this at large scale with memory hotplug.
Patch 1 (zonelist):
Online node 1's first memory block as
ZONE_MOVABLE, which rebuilds node 1's zonelists.Stall the rebuild after it writes
ZONE_MOVABLEinto the first entry but before it writesZONE_NORMALinto the second.On another CPU, allocate
GFP_KERNEL|__GFP_THISNODEfrom node 1.Before: the allocation fails, 3/3 runs.
After: the allocation waits for the rebuild and succeeds, 3/3 runs.Patch 2 (preferred zone):
Online node 1's block as
ZONE_MOVABLE, so node 1's local zonelist is [MOVABLE, NORMAL].On another CPU, allocate
GFP_KERNEL|__GFP_THISNODEfrom node 1, whose preferred slot is NORMAL, and stall it after it loads the zone.Offline the block, which rebuilds the list as [NORMAL] and moves the terminator into the preferred slot.
Before: NULL dereference in
get_page_from_freelist(), 3/3 runs.
After: the allocation succeeds fromZONE_NORMAL, 3/3 runs.Patch 3 (slot reread):
Online node 1's block as
ZONE_MOVABLE, so node 1's local zonelist is [MOVABLE, NORMAL].On another CPU, allocate
GFP_KERNEL|__GFP_THISNODEfrom node 1 with nodemask {1}, and stall it after__next_zones_zonelist()tests the NORMAL slot for NULL but before it reads the slot again.Offline the block, which rebuilds the list as [NORMAL] and moves the terminator into that slot.
Before: NULL dereference in
zone_to_nid(), 3/3 runs.
After: the allocation succeeds fromZONE_NORMAL, 3/3 runs.Patch 4 (tracepoint):
Online node 1's block as
ZONE_MOVABLE, so node 1's local zonelist is [MOVABLE, NORMAL], and enable thereclaim_retry_zonetracepoint.On another CPU, allocate
GFP_KERNEL|__GFP_THISNODEfrom node 1, with itsget_page_from_freelist()calls failed so that the slowpath reachesshould_reclaim_retry(), and stall it inzone_reclaimable_pages()for node 1's NORMAL zone.Offline the block, which rebuilds the list as [NORMAL] and moves the terminator into that slot.
Before: NULL dereference in the tracepoint, 3/3 runs. With the
tracepoint disabled, no oops (1 run).
After: the allocation succeeds fromZONE_NORMAL, 3/3 runs.Patch 5 (zonelist slots):
On one CPU, allocate
GFP_KERNELwith a nodemask, and stall it after the iterator approves its first zonelist slot but before the allocator loads the zone from that slot.Online node 1's first memory block as
ZONE_MOVABLE, which insertsZONE_MOVABLEat the front of node 1's zonelists.This was run for single-page and bulk allocations, and for a single-page allocation whose fast path is made to fail so that the slowpath's walk races.
Before: nodemask {1} gets a node 1
ZONE_MOVABLEpage and nodemask{2} a node 1 ZONE_NORMAL page, 18/18 runs. After: nodemask {1} gets node 1 ZONE_NORMAL and nodemask {2} gets node 2 ZONE_NORMAL, 18/18 runs.The check adds 5 instructions to an order-0 allocation on x86, counted with a hardware counter on a non-debug build.
Patch 6 (hugetlb):
Reserve hugetlb pages on every node. On CPU 1, fault a hugetlb page bound to node 2 with
mbind(); the pool walk uses node 0's zonelist, where node 2 follows node 1. Stall the walk after it approves node 2's slot.Online node 1's first memory block as
ZONE_MOVABLE, which shifts node 1'sZONE_NORMALinto that slot.Before: the page comes from node 1, 3/3 runs.
After: the page comes from node 2, 3/3 runs.Patch 7 (
alloc_contig):Write 1 to node 1's 1G
nr_hugepages, and stall the gigantic page's zonelist walk after it approves node 1'sZONE_NORMALslot.Online node 1's first memory block as
ZONE_MOVABLE, which insertsZONE_MOVABLEinto that slot.Before: the 1G page is allocated from
ZONE_MOVABLE, 3/3 runs.
After: the 1G page is allocated fromZONE_NORMAL, 3/3 runs.Calling
alloc_contig_pages()directly withGFP_KERNELin the first guest, as in patch 5, nodemask {1} gets aZONE_MOVABLErange and nodemask {2} a node 1 range before, 3/3 runs each, and neither after.Patch 8 (pagesets):
Online node 1's block as
ZONE_MOVABLE, then offline it.Stall the offline before the zone empties, and stall the vmstat worker on another CPU (kicked through
/proc/sys/vm/stat_refresh) indecay_pcp_high()for that zone. Let the offline free the pagesets, reallocate the freed percpu area with a known value, then release the worker.Before:
decay_pcp_high()runs on the freed pageset, 6/6 runs. In
1/6 the area was reallocated before the worker resumed, anddecay_pcp_high()rewrote the new object.
After: the pageset is not freed, 3/3 runs.All eight reproducers also pass on the series tip, 3/3 runs each.
Changes in v3
=============
- Replaced v2 patch 2 (zonelist_zone_valid()recheck) with patch 5, which checkszonelist_update_seqwhenever an allocation succeeds (Vlastimil). A raced attempt's page is discarded, so a stale preferred zone can no longer affect the result (Sashiko).
- Moved the NULL dereference fixes ahead of patch 5.
- Added patches 6 and 7 for the hugetlb pool walk andalloc_contig_pages(), which have the same slot shift.
- Added patch 4 for thereclaim_retry_zonetracepoint's re-read.
- Fixes: tags on patches 2-6 now name thestop_machineremoval that made rebuilds run concurrently with zonelist walks.Gregory Price (Meta) (8):
mm/page_alloc: detect zonelist rebuilds from memory hot-addmm/page_alloc: snapshot the preferred zone against shrink rebuilds mm/mmzone: read each zoneref slot once when filtering by nodemaskmm/page_alloc: pass the zone, not the zoneref, toreclaim_retry_zonemm/page_alloc: retry allocations that raced with a zonelist rebuild mm/hugetlb: retry the pool walk when the zonelist is rebuiltmm/page_alloc: retryalloc_contig_pages()when the zonelist is rebuiltmm/page_alloc: keep a zone's pagesets when it goes emptyinclude/trace/events/oom.h | 8 +-- mm/hugetlb.c | 13 +++-- mm/mmzone.c | 11 ++-- mm/page_alloc.c | 106 +++++++++++++++++++++++++++++-------- mm/page_alloc.h | 3 ++ 5 files changed, 107 insertions(+), 34 deletions(-)
Signature · 3 lines
Cheers, David
Quoted 8 lines · David Hildenbrand (Arm)
On 10/21/26 04:39, Gregory Price (Meta) wrote:
Usual reminder to slow down a bit :)
We have
Documentation/process/submitting-patches.rst
Quoted 9 lines
Saying
"Wait for a minimum of one week before resubmitting or pinging reviewers
- possibly longer during busy times like merge windows.A lot of this here was broken forever, so there is no need to rush.
(thanks for the fixes ;) )
felt prudent to get the adjustment Vlastimil recommended out to avoid time wasted on v1 since it was somewhat significant - but ack.
~_~ found a 9th instance of the same bug. Will send next week.
~Gregory
Signature · 1 line
Gregory Price (Meta) <[email protected]>
David Hildenbrand (Arm)
maintainerQuoted 15 lines · Gregory Price (Meta)
Eight fixes for allocator paths that can act on state another CPU is
updating: zonelists rebuilt by memory hotplug, and the pagesets of a zone that offlining empties.I've packaged these together because they're all related to hotplug.
*** Backport note - Patches 5-7 build on patch 1: without it, their
retry is compiled out whenCONFIG_MEMORY_HOTREMOVE=n.Most of these race conditions stem from zonelists being re-ordered in-place at runtime during hotplug. The result is a mixture of failed allocations, NULL dereferences, incorrect placement (
GFP_KERNELinZONE_MOVABLE), node misplacement, or incorrectly allowing hugetlb / contig allocations on a node/zone otherwise not eligible.
Quoted 5 lines
Most of these are incredibly narrow race conditions, but since the most likely impacted machines are VMs with memory hotplug, the narrow condition can more easily occur when a vCPU takes a long interrupt / VM exit of some kind. I've made all of these race conditions deterministics with a kprobe reproduction module described below.
What's the status of this? "memory hotplug" caught my attention, but then then is all page_alloc material :)
Signature · 3 lines
Cheers, David
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 15 lines · Gregory Price (Meta)
Eight fixes for allocator paths that can act on state another CPU is
updating: zonelists rebuilt by memory hotplug, and the pagesets of a zone that offlining empties.I've packaged these together because they're all related to hotplug.
*** Backport note - Patches 5-7 build on patch 1: without it, their
retry is compiled out whenCONFIG_MEMORY_HOTREMOVE=n.Most of these race conditions stem from zonelists being re-ordered in-place at runtime during hotplug. The result is a mixture of failed allocations, NULL dereferences, incorrect placement (
GFP_KERNELinZONE_MOVABLE), node misplacement, or incorrectly allowing hugetlb / contig allocations on a node/zone otherwise not eligible.
Quoted 9 lines
Quoted 5 lines
Most of these are incredibly narrow race conditions, but since the most likely impacted machines are VMs with memory hotplug, the narrow condition can more easily occur when a vCPU takes a long interrupt / VM exit of some kind. I've made all of these race conditions deterministics with a kprobe reproduction module described below.
What's the status of this? "memory hotplug" caught my attention, but then then is all
page_allocmaterial :)
I have a v4 with the requested tweaks and some performance analysis, though i think there is some overlap with the other Fixes: series.
I can send it out and if it helps i can pull together a report on overlap between the different Fixes: series.
~Gregory
Signature · 1 line
~Gregory