* [PATCH 0/3] mm/page_alloc: drop the pcp lock before re-enabling IRQs
@ 2026-11-25 21:27 Joshua Hahn
2026-11-25 21:27 ` [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain Joshua Hahn
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Joshua Hahn @ 2026-11-25 21:27 UTC (permalink / raw)
To: Andrew Morton
Cc: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, linux-mm, kernel-team
When the zone lock needs to be acquired from within the pcp lock
(due to refills or drains), IRQs are disabled to prevent deadlocks
with the IRQ handler. Today it looks like this:
pcp lock
IRQ disabled
zone lock
zone unlock
IRQ enabled
pcp unlock
So any interrupt that queues during the IRQ disabled section fires
before the pcp unlock. This is not a rare operation; the zone lock
remains one of the most contended locks on large systems.
Together, those two observations mean that any IRQ handler that requires
taking the pcp lock will fire before the interrupted task can let go of
the pcp lock, and the handler will always fall back to the already
contended zone lock.
In Meta production workloads, we've seen softlockups on large machines
where one CPU spent 23s in an RCU softirq freeing page tables, and
taking zone->lock once per page because it had interrupted a task
already holding the pcp lock.
Instead, keep the IRQ disabled until the pcp unlock, so that interrupt
handlers can have a better chance at taking the pcp lock and relieving
contention on the zone lock:
pcp lock
IRQ disabled
zone lock
zone unlock
pcp unlock
IRQ enabled
This series was tested on a 128-vCPU KVM guest, where every vCPU faults
in and MADV_DONTNEEDs in a loop. The "pcp fallbacks" (taking the zone
lock instead of the pcp lock) and long softirqs were summed across 6
runs of 30 seconds each.
+----------+---------------+-------------------+-----------------+
| | pcp fallbacks | callback avg (ms) | softirqs > 65ms |
+----------+---------------+-------------------+-----------------+
| mm-new | 135590 | 14 | 832 |
| +patch 1 | 10481 | 1.5 | 116 |
| +patch 2 | 2327 | 0.6 | 55 |
| +patch 3 | 190 | 0.6 | 10 |
+----------+---------------+-------------------+-----------------+
Running the same tests with PROVE_LOCKING, DEBUG_VM, and DEBUG_PREEMPT
also raised no concerns.
Joshua Hahn (3):
mm/page_alloc: drop the pcp lock before enabling IRQs after a drain
mm/page_alloc: drop the pcp lock before enabling IRQs after a refill
mm/page_alloc: disable IRQs around the remaining pcp drains
mm/page_alloc.c | 59 +++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 50 insertions(+), 9 deletions(-)
base-commit: 9f004372ebeb18fdeb699f9880a0e472df90135b
--
2.53.1-Meta
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain
2026-11-25 21:27 [PATCH 0/3] mm/page_alloc: drop the pcp lock before re-enabling IRQs Joshua Hahn
@ 2026-11-25 21:27 ` Joshua Hahn
2026-11-26 21:27 ` sashiko-bot
2026-11-25 21:27 ` [PATCH 2/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a refill Joshua Hahn
2026-11-25 21:27 ` [PATCH 3/3] mm/page_alloc: disable IRQs around the remaining pcp drains Joshua Hahn
2 siblings, 1 reply; 5+ messages in thread
From: Joshua Hahn @ 2026-11-25 21:27 UTC (permalink / raw)
To: Andrew Morton
Cc: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, linux-mm, kernel-team
Commit 574907741599 ("mm/page_alloc: leave IRQs enabled for per-cpu page
allocations") stopped disabling IRQs under the pcp lock. A softirq that
interrupts the holder always fails pcp_spin_trylock() and falls back to
taking zone->lock for each page, since the pcp lock is still held by the
interrupted process.
This is common when zone->lock is contended. free_pcppages_bulk() spins
on it with IRQs disabled, so an interrupt is often pending when it
unlocks, and happens with the pcp lock still held.
Restore IRQs only after dropping the pcp lock, and return with the pcp
unlocked. PREEMPT_RT keeps IRQs enabled.
With zone->lock contended in a 128-vCPU guest, tlb_remove_table_rcu()
callbacks hitting the fallback went from 93% to 7%, and the average
callback duration dropped from 14ms to 1.5ms. Throughput was unchanged.
Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
mm/page_alloc.c | 30 +++++++++++++++++++++++++-----
1 file changed, 25 insertions(+), 5 deletions(-)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index aa1ed599c7c6b..bc272f8d8ec75 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -141,6 +141,18 @@ static DEFINE_MUTEX(pcp_batch_high_lock);
pcpu_task_unpin(); \
})
+/*
+ * Like pcp_spin_unlock(), for callers that disabled IRQs (except on PREEMPT_RT)
+ * before taking zone->lock. An interrupt that arrives in between runs only once
+ * the pcp lock is dropped, so its softirqs can use the pcp lists. Disable IRQs
+ * after taking the pcp lock as other holders may be waiting for an IPI.
+ */
+#define pcp_spin_unlock_irqrestore(ptr, flags) \
+({ \
+ spin_unlock_irqrestore(&(ptr)->lock, flags); \
+ pcpu_task_unpin(); \
+})
+
/*
* On CONFIG_SMP=n the UP implementation of spin_trylock() never fails and thus
* is not compatible with our locking scheme. However we do not need pcp for
@@ -153,6 +165,9 @@ static DEFINE_MUTEX(pcp_batch_high_lock);
#define pcp_spin_unlock(ptr) \
BUG_ON(1)
+
+#define pcp_spin_unlock_irqrestore(ptr, flags) \
+ BUG_ON(1)
#endif
/*
@@ -2846,8 +2861,8 @@ static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone,
* Tune pcp alloc factor and adjust count & free_count. Free pages to bring the
* pcp's watermarks below high.
*
- * May return a freed pcp, if during page freeing the pcp spinlock cannot be
- * reacquired. Return true if pcp is locked, false otherwise.
+ * May return with the pcp unlocked after freeing pages to the buddy allocator.
+ * Return true if pcp is locked, false otherwise.
*/
static bool free_frozen_page_commit(struct zone *zone,
struct per_cpu_pages *pcp, struct page *page, int migratetype,
@@ -2904,14 +2919,19 @@ static bool free_frozen_page_commit(struct zone *zone,
to_free = nr_pcp_free(pcp, batch, high, free_high);
while (to_free > 0 && pcp->count > 0) {
+ unsigned long flags = 0;
+
to_free_batched = min(to_free, batch);
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+ local_irq_save(flags);
free_pcppages_bulk(zone, to_free_batched, pcp, pindex);
to_free -= to_free_batched;
+ pcp_spin_unlock_irqrestore(pcp, flags);
- if (to_free == 0 || pcp->count == 0)
+ if (!to_free) {
+ ret = false;
break;
-
- pcp_spin_unlock(pcp);
+ }
pcp = pcp_spin_trylock(zone->per_cpu_pageset);
if (!pcp) {
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a refill
2026-11-25 21:27 [PATCH 0/3] mm/page_alloc: drop the pcp lock before re-enabling IRQs Joshua Hahn
2026-11-25 21:27 ` [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain Joshua Hahn
@ 2026-11-25 21:27 ` Joshua Hahn
2026-11-25 21:27 ` [PATCH 3/3] mm/page_alloc: disable IRQs around the remaining pcp drains Joshua Hahn
2 siblings, 0 replies; 5+ messages in thread
From: Joshua Hahn @ 2026-11-25 21:27 UTC (permalink / raw)
To: Andrew Morton
Cc: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, linux-mm, kernel-team
rmqueue_pcplist() refills an empty pcp list with rmqueue_bulk(), which
spins on zone->lock with IRQs disabled. An interrupt that comes in
the meantime will run while the pcp lock is held, meaning the softirqs
fall back to taking zone->lock for each page.
When the list is empty, disable IRQs before the refill and restore them
only after dropping the pcp lock. PREEMPT_RT keeps IRQs enabled.
With zone->lock contended in a 128-vCPU guest, tlb_remove_table_rcu()
callbacks hitting the fallback went from 7% to 1.5% and the average
callback duration from 1.5ms to 0.6ms (the previous commit brought
these down from 93% and 14ms, respectively).
Note that alloc_pages_bulk_noprof() still refills with IRQs enabled
under the pcp lock. This one needs a different fix as it keeps the
lock across prep_new_page(), so leave this for future work.
Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
mm/page_alloc.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index bc272f8d8ec75..0e669e206d371 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -3405,6 +3405,8 @@ static struct page *rmqueue_pcplist(struct zone *preferred_zone,
struct per_cpu_pages *pcp;
struct list_head *list;
struct page *page;
+ unsigned long flags;
+ bool refill;
/* spin_trylock may fail due to a parallel drain or IRQ reentrancy. */
pcp = pcp_spin_trylock(zone->per_cpu_pageset);
@@ -3418,8 +3420,15 @@ static struct page *rmqueue_pcplist(struct zone *preferred_zone,
*/
pcp->free_count >>= 1;
list = &pcp->lists[order_to_pindex(migratetype, order)];
+ /* Refilling takes zone->lock, see pcp_spin_unlock_irqrestore() */
+ refill = !IS_ENABLED(CONFIG_PREEMPT_RT) && list_empty(list);
+ if (refill)
+ local_irq_save(flags);
page = __rmqueue_pcplist(zone, order, migratetype, alloc_flags, pcp, list);
- pcp_spin_unlock(pcp);
+ if (refill)
+ pcp_spin_unlock_irqrestore(pcp, flags);
+ else
+ pcp_spin_unlock(pcp);
if (page) {
__count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
zone_statistics(preferred_zone, zone, 1);
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/3] mm/page_alloc: disable IRQs around the remaining pcp drains
2026-11-25 21:27 [PATCH 0/3] mm/page_alloc: drop the pcp lock before re-enabling IRQs Joshua Hahn
2026-11-25 21:27 ` [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain Joshua Hahn
2026-11-25 21:27 ` [PATCH 2/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a refill Joshua Hahn
@ 2026-11-25 21:27 ` Joshua Hahn
2 siblings, 0 replies; 5+ messages in thread
From: Joshua Hahn @ 2026-11-25 21:27 UTC (permalink / raw)
To: Andrew Morton
Cc: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, linux-mm, kernel-team
decay_pcp_high(), drain_zone_pages(), and drain_pages_zone() also call
free_pcppages_bulk() under the pcp lock, so an interrupt can happen
with the lock still held.
Disable IRQs after taking the pcp lock and restore them after dropping
it. Note that we can't use spin_lock_irqsave() here since
drain_pages_zone() takes remote CPUs' pcp locks, whose holders keep
IRQs enabled and may be waiting for this CPU to answer an IPI.
PREEMPT_RT keeps IRQs enabled.
With zone->lock contended in a 128-vCPU guest, tlb_remove_table_rcu()
callbacks hitting the fallback went from 1.5% to 0.1% (the two previous
commits brought it down from 93% to 7% and then to 1.5%).
Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
mm/page_alloc.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 0e669e206d371..f2cbc820f24b7 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -178,6 +178,9 @@ static DEFINE_MUTEX(pcp_batch_high_lock);
spin_lock(&(ptr)->lock)
#define pcp_spin_unlock_nopin(ptr) \
spin_unlock(&(ptr)->lock)
+/* Like pcp_spin_unlock_irqrestore(), for callers that did not pin */
+#define pcp_spin_unlock_nopin_irqrestore(ptr, flags) \
+ spin_unlock_irqrestore(&(ptr)->lock, flags)
#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
DEFINE_PER_CPU(int, numa_node);
@@ -2599,6 +2602,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp)
{
int high_min, to_drain, to_drain_batched, batch;
+ unsigned long flags = 0;
bool todo = false;
high_min = READ_ONCE(pcp->high_min);
@@ -2619,8 +2623,10 @@ bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp)
while (to_drain > 0) {
to_drain_batched = min(to_drain, batch);
pcp_spin_lock_nopin(pcp);
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+ local_irq_save(flags);
free_pcppages_bulk(zone, to_drain_batched, pcp, 0);
- pcp_spin_unlock_nopin(pcp);
+ pcp_spin_unlock_nopin_irqrestore(pcp, flags);
todo = true;
to_drain -= to_drain_batched;
@@ -2637,14 +2643,17 @@ bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp)
*/
void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
{
+ unsigned long flags = 0;
int to_drain, batch;
batch = READ_ONCE(pcp->batch);
to_drain = min(pcp->count, batch);
if (to_drain > 0) {
pcp_spin_lock_nopin(pcp);
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+ local_irq_save(flags);
free_pcppages_bulk(zone, to_drain, pcp, 0);
- pcp_spin_unlock_nopin(pcp);
+ pcp_spin_unlock_nopin_irqrestore(pcp, flags);
}
}
#endif
@@ -2655,10 +2664,13 @@ void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
static void drain_pages_zone(unsigned int cpu, struct zone *zone)
{
struct per_cpu_pages *pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu);
+ unsigned long flags = 0;
int count;
do {
pcp_spin_lock_nopin(pcp);
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+ local_irq_save(flags);
count = pcp->count;
if (count) {
int to_drain = min(count,
@@ -2667,7 +2679,7 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)
free_pcppages_bulk(zone, to_drain, pcp, 0);
count -= to_drain;
}
- pcp_spin_unlock_nopin(pcp);
+ pcp_spin_unlock_nopin_irqrestore(pcp, flags);
} while (count);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain
2026-11-25 21:27 ` [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain Joshua Hahn
@ 2026-11-26 21:27 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-26 21:27 UTC (permalink / raw)
To: Joshua Hahn, Andrew Morton
Cc: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, linux-mm, kernel-team
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm/page_alloc: unused-but-set flags variable in
free_frozen_page_commit()
--
commit d66ecc1b75f6ab35dc480b529fcd4baafd313549
Author: Joshua Hahn <joshua.hahnjy@gmail.com>
mm/page_alloc: drop the pcp lock before enabling IRQs after a drain
This commit disables IRQs before calling free_pcppages_bulk() and
restores them only after dropping the pcp lock, returning with the pcp
unlocked after draining pages.
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index aa1ed599c7c6b..bc272f8d8ec75 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -153,6 +165,9 @@ static DEFINE_MUTEX(pcp_batch_high_lock);
>
> #define pcp_spin_unlock(ptr) \
> BUG_ON(1)
> +
> +#define pcp_spin_unlock_irqrestore(ptr, flags) \
> + BUG_ON(1)
> #endif
[ ... ]
> @@ -2904,14 +2919,19 @@ static bool free_frozen_page_commit(struct zone *zone,
>
> to_free = nr_pcp_free(pcp, batch, high, free_high);
> while (to_free > 0 && pcp->count > 0) {
> + unsigned long flags = 0;
> +
> to_free_batched = min(to_free, batch);
> + if (!IS_ENABLED(CONFIG_PREEMPT_RT))
> + local_irq_save(flags);
> free_pcppages_bulk(zone, to_free_batched, pcp, pindex);
> to_free -= to_free_batched;
> + pcp_spin_unlock_irqrestore(pcp, flags);
[Severity: Low]
Does this trigger a -Wunused-but-set-variable warning on flags when built
with CONFIG_SMP=n, CONFIG_TRACE_IRQFLAGS=n, and W=1? With
CONFIG_TRACE_IRQFLAGS=n, local_irq_save(flags) only writes to flags, and
the !CONFIG_SMP definition of pcp_spin_unlock_irqrestore(ptr, flags) does
not evaluate flags.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125212730.1128631-1-joshua.hahnjy@gmail.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-26 21:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 21:27 [PATCH 0/3] mm/page_alloc: drop the pcp lock before re-enabling IRQs Joshua Hahn
2026-11-25 21:27 ` [PATCH 1/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a drain Joshua Hahn
2026-11-26 21:27 ` sashiko-bot
2026-11-25 21:27 ` [PATCH 2/3] mm/page_alloc: drop the pcp lock before enabling IRQs after a refill Joshua Hahn
2026-11-25 21:27 ` [PATCH 3/3] mm/page_alloc: disable IRQs around the remaining pcp drains Joshua Hahn
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox