irq_work: Fix use-after-free in irq_work_single on PREEMPT_RT
Subsystems (5)
PATCH v2 1/1 irq_work: Fix use-after-free in irq_work_single on PREEMPT_RT
On PREEMPT_RT, non-HARD irq_work runs in per-CPU kthreads via run_irq_workd(), so irq_work_sync() uses rcuwait to wait for BUSY==0.
After irq_work_single() clears BUSY via atomic_cmpxchg(), it still dereferences @work for irq_work_is_hard() and rcuwait_wake_up(). An irq_work_sync() caller on another CPU that enters after BUSY is cleared can observe BUSY==0 immediately, return, and free the work before those accesses complete — causing a use-after-free.
Fix this by wrapping run_irq_workd() in guard(rcu)() so that the entire irq_work_single() execution is within an RCU read-side critical section. Then add synchronize_rcu() in irq_work_sync() after rcuwait_wait_event() to ensure the caller waits for the RCU grace period before returning, preventing premature frees.
--- kernel/irq_work.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/irq_work.c +7 -0
@@ -292,6 +292,12 @@ void irq_work_sync(struct irq_work *work) !arch_irq_work_has_interrupt()) {rcuwait_wait_event (&work->irqwait, !irq_work_is_busy (work),TASK_UNINTERRUPTIBLE ); +/* +* Ensure irq_work_single() does not access @work +* after removing IRQ_WORK_BUSY. It is always +* accessed within a RCU-read section. +*/ +synchronize_rcu ();return ; }
@@ -302,6 +308,7 @@ EXPORT_SYMBOL_GPL(irq_work_sync);static void run_irq_workd (unsigned int cpu) { +guard (rcu)();irq_work_run_list (this_cpu_ptr (&lazy_list)); }
Signature · 1 line
2.43.0
Quoted 16 lines · Jiayuan Chen
On
PREEMPT_RT, non-HARDirq_workruns in per-CPU kthreads viarun_irq_workd(), soirq_work_sync()uses rcuwait to wait for BUSY==0.After
irq_work_single()clears BUSY viaatomic_cmpxchg(), it still dereferences @work forirq_work_is_hard()andrcuwait_wake_up(). Anirq_work_sync()caller on another CPU that enters after BUSY is cleared can observe BUSY==0 immediately, return, and free the work before those accesses complete — causing a use-after-free.Fix this by wrapping
run_irq_workd()in guard(rcu)() so that the entireirq_work_single()execution is within an RCU read-side critical section. Then addsynchronize_rcu()inirq_work_sync()afterrcuwait_wait_event()to ensure the caller waits for the RCU grace period before returning, preventing premature frees.
Quoted 4 lines
Fixes 810979682ccc ("irq_work: Allow irq_work_sync() to sleep if irq_work() no IRQ support.")Suggested-by Sebastian Andrzej Siewior <[email protected]>Suggested-by Steven Rostedt <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>
Sebastian
Quoted 17 lines · Sebastian Andrzej Siewior
Quoted 16 lines · Jiayuan Chen
On
PREEMPT_RT, non-HARDirq_workruns in per-CPU kthreads viarun_irq_workd(), soirq_work_sync()uses rcuwait to wait for BUSY==0.After
irq_work_single()clears BUSY viaatomic_cmpxchg(), it still dereferences @work forirq_work_is_hard()andrcuwait_wake_up(). Anirq_work_sync()caller on another CPU that enters after BUSY is cleared can observe BUSY==0 immediately, return, and free the work before those accesses complete — causing a use-after-free.Fix this by wrapping
run_irq_workd()in guard(rcu)() so that the entireirq_work_single()execution is within an RCU read-side critical section. Then addsynchronize_rcu()inirq_work_sync()afterrcuwait_wait_event()to ensure the caller waits for the RCU grace period before returning, preventing premature frees.
Quoted 8 lines
Quoted 4 lines
Fixes 810979682ccc ("irq_work: Allow irq_work_sync() to sleep if irq_work() no IRQ support.")Suggested-by Sebastian Andrzej Siewior <[email protected]>Suggested-by Steven Rostedt <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>Reviewed-by Sebastian Andrzej Siewior <[email protected]>Sebastian
Hi,
This commit significantly slows down execve() (and perhaps other things) on MIPS. A very simple program:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int x = argc > 1 ? atoi(argv[1]) : 114;
if (!x)
exit(0);
char buf[10];
sprintf(buf, "%d", x - 1);
execl("/proc/self/exe", argv[0], buf, NULL);
__builtin_trap();
}now spends several (2 to 6) seconds on a Loongson 3A4000, with the commit reverted it only spends 0.06 second.
I noticed https://lore.kernel.org/all/20260604182407.3109536-1-
[email protected]/ but it doesn't help.
The kernel configuration is attached. Any thoughts?
Signature · 1 line
Xi Ruoyao <[email protected]>
Sebastian Andrzej Siewior
maintainerQuoted 4 lines · Xi Ruoyao
Hi,
This commit significantly slows down
execve()(and perhaps other things) on MIPS. A very simple program:
This is probably originating from sched_mm_cid_exit(). Can you check?
Sebastian
Quoted 7 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Xi Ruoyao
Hi,
This commit significantly slows down
execve()(and perhaps other things) on MIPS. A very simple program:This is probably originating from
sched_mm_cid_exit(). Can you check?
I've not checked that yet (I have to admit I don't really know how).
On the other hand, Yao Zi told me she speculated this might be related to the lack of arch_irq_work_raise on MIPS. For mips/loongson64 the IPI support is already there and we only need 16 lines to implement arch_irq_work_raise:
arch/mips/include/asm/irq_work.h new file +4 -0
@@ -0,0 +1,4 @@ +static inline bool arch_irq_work_has_interrupt (void ) +{ +return IS_ENABLED (CONFIG_MACH_LOONGSON64 ) &&IS_ENABLED (CONFIG_SMP ); +}
arch/mips/include/asm/smp.h +2 -0
@@ -50,6 +50,8 @@ extern int __cpu_logical_map[NR_CPUS];#define SMP_CALL_FUNCTION 0x2/* Octeon - Tell another core to flush its icache */ #define SMP_ICACHE_FLUSH 0x4 +/* Loongson 3 - IRQ work */ +#define SMP_IRQ_WORK 0x8/* Mask of CPUs which are currently definitely operating coherently */ extern cpumask_t cpu_coherent_mask;
arch/mips/loongson64/smp.c +10 -0
@@ -381,6 +381,13 @@ loongson3_send_ipi_mask(const struct cpumask *mask, unsigned int action)ipi_write_action (cpu_logical_map (i), (u32 )action); } +#ifdef CONFIG_IRQ_WORK +void arch_irq_work_raise (void ) +{ +loongson3_send_ipi_single (smp_processor_id (),SMP_IRQ_WORK ); +} +#endif +static irqreturn_t loongson3_ipi_interrupt (int irq,void *dev_id) {int cpu =smp_processor_id ();
@@ -397,6 +404,9 @@ static irqreturn_t loongson3_ipi_interrupt(int irq, void *dev_id)irq_exit (); } +if (action &SMP_IRQ_WORK ) +irq_work_run (); +return IRQ_HANDLED ; }
With this the performance indeed becomes normal.
Signature · 1 line
Xi Ruoyao <[email protected]>
[tip: irq/urgent] irq_work: Fix use-after-free in irq_work_single() on PREEMPT_RT
In reply to Jiayuan ChenThe following commit has been merged into the irq/urgent branch of tip:
The following commit has been merged into the irq/urgent branch of tip:
Commit-ID: 91840be8f710370607f949a627e070896faeddb8 Gitweb: https://git.kernel.org/tip/91840be8f710370607f949a627e070896faeddb8 Author: Jiayuan Chen <[email protected]> AuthorDate: Mon, 30 Mar 2026 15:32:29 +08:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Mon, 11 May 2026 16:28:04 +02:00
irq_work: Fix use-after-free in irq_work_single() on PREEMPT_RT
On PREEMPT_RT, non-HARD irq_work runs in per-CPU kthreads via run_irq_workd(), so irq_work_sync() uses rcuwait() to wait for BUSY==0.
After irq_work_single() clears BUSY via atomic_cmpxchg(), it still dereferences @work for irq_work_is_hard() and rcuwait_wake_up().
An irq_work_sync() caller on another CPU that enters after BUSY is cleared can observe BUSY==0 immediately, return, and free the work before those accesses complete — causing a use-after-free.
Fix this by wrapping run_irq_workd() in guard(rcu)() so that the entire irq_work_single() execution is within an RCU read-side critical section. Then add synchronize_rcu() in irq_work_sync() after rcuwait_wait_event() to ensure the caller waits for the RCU grace period before returning, preventing premature frees.
--- kernel/irq_work.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/irq_work.c +7 -0
@@ -292,6 +292,12 @@ void irq_work_sync(struct irq_work *work) !arch_irq_work_has_interrupt()) {rcuwait_wait_event (&work->irqwait, !irq_work_is_busy (work),TASK_UNINTERRUPTIBLE ); +/* +* Ensure irq_work_single() does not access @work +* after removing IRQ_WORK_BUSY. It is always +* accessed within a RCU-read section. +*/ +synchronize_rcu ();return ; }
@@ -302,6 +308,7 @@ EXPORT_SYMBOL_GPL(irq_work_sync);static void run_irq_workd (unsigned int cpu) { +guard (rcu)();irq_work_run_list (this_cpu_ptr (&lazy_list)); }
Quoted 42 lines · Jiayuan Chen
On
PREEMPT_RT, non-HARDirq_workruns in per-CPU kthreads viarun_irq_workd(), soirq_work_sync()uses rcuwait to wait for BUSY==0.After
irq_work_single()clears BUSY viaatomic_cmpxchg(), it still dereferences @work forirq_work_is_hard()andrcuwait_wake_up(). Anirq_work_sync()caller on another CPU that enters after BUSY is cleared can observe BUSY==0 immediately, return, and free the work before those accesses complete — causing a use-after-free.Fix this by wrapping
run_irq_workd()in guard(rcu)() so that the entireirq_work_single()execution is within an RCU read-side critical section. Then addsynchronize_rcu()inirq_work_sync()afterrcuwait_wait_event()to ensure the caller waits for the RCU grace period before returning, preventing premature frees.Fixes 810979682ccc ("irq_work: Allow irq_work_sync() to sleep if irq_work() no IRQ support.")Suggested-by Sebastian Andrzej Siewior <[email protected]>Suggested-by Steven Rostedt <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>--- kernel/irq_work.c | 7 +++++++ 1 file changed, 7 insertions(+)kernel/irq_work.c +6
-0@@ -292,6 +292,12 @@ void irq_work_sync(struct irq_work *work) !arch_irq_work_has_interrupt()) {rcuwait_wait_event (&work->irqwait, !irq_work_is_busy (work),TASK_UNINTERRUPTIBLE ); +/* +* Ensure irq_work_single() does not access @work +* after removing IRQ_WORK_BUSY. It is always +* accessed within a RCU-read section. +*/ +synchronize_rcu ();return ; }
kernel/irq_work.c EXPORT_SYMBOL_GPL(irq_work_sync); · line 314
Quoted 7 lines
kernel/irq_work.c +1
-0@@ -302,6 +308,7 @@ EXPORT_SYMBOL_GPL(irq_work_sync);static void run_irq_workd (unsigned int cpu) { +guard (rcu)();irq_work_run_list (this_cpu_ptr (&lazy_list)); }
With this patch, arm 32-bit, and a single-core SoC, I got a noticeable slowdown of irq_work_sync. Reverting the patch resolves this. So I dug deeper, found out that things were even worse before [1], but even with that, I could still see up to 2 ticks delay per call. If you combine that with an unfortunate loop of irq_work_sync calls (mine is out-of-tree, but I see something even "worse" in bpf_mem_alloc_destroy), there is this impact.
While this patch is motivated by PREEMPT_RT, the condition to enter the modified branch are not limited to it:
... || !arch_irq_work_has_interrupt()
arch_irq_work_has_interrupt() is false on some archs, either always (very rare) or under certain conditions. On arm, it's false when is_smp() is false.
If we "only" need the synchronize_rcu() for PREEMPT_RT, should we limit it to that configuration? Or do we actually need otherwise as well?
And what could be done to accelerate irq_work_sync loops? Practically, a single synchronize_rcu() at the end could be enough, no?
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Sebastian Andrzej Siewior
maintainerQuoted 10 lines · Jan Kiszka
With this patch, arm 32-bit, and a single-core SoC, I got a noticeable slowdown of
irq_work_sync. Reverting the patch resolves this. So I dug deeper, found out that things were even worse before [1], but even with that, I could still see up to 2 ticks delay per call. If you combine that with an unfortunate loop ofirq_work_synccalls (mine is out-of-tree, but I see something even "worse" inbpf_mem_alloc_destroy), there is this impact.While this patch is motivated by
PREEMPT_RT, the condition to enter the modified branch are not limited to it:
It is not motivated by PREEMPT_RT. All the lazy-work has "this" problem. It is just on RT most of the work is stuffed into the lazy queue.
Quoted 11 lines
... || !arch_irq_work_has_interrupt()
arch_irq_work_has_interrupt()is false on some archs, either always (very rare) or under certain conditions. On arm, it's false whenis_smp()is false.If we "only" need the
synchronize_rcu()forPREEMPT_RT, should we limit it to that configuration? Or do we actually need otherwise as well?And what could be done to accelerate
irq_work_syncloops? Practically, a singlesynchronize_rcu()at the end could be enough, no?
Let me look into this again…
Quoted 2 lines
Jan
Sebastian
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Jan Kiszka
With this patch, arm 32-bit, and a single-core SoC, I got a noticeable slowdown of
irq_work_sync. Reverting the patch resolves this. So I dug deeper, found out that things were even worse before [1], but even with that, I could still see up to 2 ticks delay per call. If you combine that with an unfortunate loop ofirq_work_synccalls (mine is out-of-tree, but I see something even "worse" inbpf_mem_alloc_destroy), there is this impact.
Quoted 5 lines
Quoted 2 lines
While this patch is motivated by
PREEMPT_RT, the condition to enter the modified branch are not limited to it:It is not motivated by
PREEMPT_RT. All the lazy-work has "this" problem. It is just on RT most of the work is stuffed into the lazy queue.
I was suspecting that already, but the patch description still reads rt-only.
Quoted 7 lines
Quoted 7 lines
... || !arch_irq_work_has_interrupt()
arch_irq_work_has_interrupt()is false on some archs, either always (very rare) or under certain conditions. On arm, it's false whenis_smp()is false.
Quoted 8 lines
Quoted 5 lines
If we "only" need the
synchronize_rcu()forPREEMPT_RT, should we limit it to that configuration? Or do we actually need otherwise as well?And what could be done to accelerate
irq_work_syncloops? Practically, a singlesynchronize_rcu()at the end could be enough, no?Let me look into this again…
Thanks!
I was meanwhile considering to backport the synchronize_rcu accelerator commit. Seems to be straightforward for 6.12, but older kernels need more...
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Vlastimil Babka (SUSE)
maintainerQuoted 35 lines · Jan Kiszka
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Jan Kiszka
With this patch, arm 32-bit, and a single-core SoC, I got a noticeable slowdown of
irq_work_sync. Reverting the patch resolves this. So I dug deeper, found out that things were even worse before [1], but even with that, I could still see up to 2 ticks delay per call. If you combine that with an unfortunate loop ofirq_work_synccalls (mine is out-of-tree, but I see something even "worse" inbpf_mem_alloc_destroy), there is this impact.Quoted 5 lines
Quoted 2 lines
While this patch is motivated by
PREEMPT_RT, the condition to enter the modified branch are not limited to it:It is not motivated by
PREEMPT_RT. All the lazy-work has "this" problem. It is just on RT most of the work is stuffed into the lazy queue.I was suspecting that already, but the patch description still reads rt-only.
Quoted 7 lines
Quoted 7 lines
... || !arch_irq_work_has_interrupt()
arch_irq_work_has_interrupt()is false on some archs, either always (very rare) or under certain conditions. On arm, it's false whenis_smp()is false.Quoted 8 lines
Quoted 5 lines
If we "only" need the
synchronize_rcu()forPREEMPT_RT, should we limit it to that configuration? Or do we actually need otherwise as well?And what could be done to accelerate
irq_work_syncloops? Practically, a singlesynchronize_rcu()at the end could be enough, no?Let me look into this again…
Quoted 6 lines
Thanks!
I was meanwhile considering to backport the
synchronize_rcuaccelerator commit. Seems to be straightforward for 6.12, but older kernels need more...Jan
Hi,
for slab we recently got proposed the AI fix below. While it seems correct to me, it feels wrong to do such an ad-hoc workaround. I'd expect there would be other cases where the work is static and no UAF can exist. Can we accomodate them in a generic way? Thanks.
----8<----
From: Chris Mason <[email protected]>
On PREEMPT_RT, or on an architecture without an irq_work interrupt, destroying a slab cache stalls for two RCU grace periods per possible CPU. kvfree_rcu_barrier() stalls for one grace period per possible CPU. Tasks that do either at the same time also wait for each other.
deferred_work_barrier() calls irq_work_sync() on the deferred work of each possible CPU. kmem_cache_destroy() calls the barrier twice, and kvfree_rcu_barrier() calls it once.
Commit 91840be8f710 ("irq_work: Fix use-after-free in irq_work_single() on PREEMPT_RT") added a synchronize_rcu() at the end of irq_work_sync(), so that the caller can free the work afterwards. Only the kernels named above have it, since irq_work_sync() sleeps on them instead of spinning. Before that commit irq_work_sync() returned at once for an idle work. Now it waits for a grace period for an idle work too.
But the deferred works are static per-CPU data that nobody frees, so the use-after-free that the grace period prevents cannot happen here. The barrier also runs its whole loop under a global mutex, added by "mm/slab: serialize deferred_work_barrier()".
The fix used here is to call irq_work_sync() only for a work that is busy.
--- mm/slub.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
mm/slub.c +11 -2
@@ -6624,8 +6624,17 @@ void deferred_work_barrier(void)/* irq_work_sync() may use rcuwait that requires serialization */ guard (mutex)(&barrier_mutex); -for_each_possible_cpu (cpu) -irq_work_sync (&per_cpu_ptr (&deferred_percpu_work, cpu)->work); +for_each_possible_cpu (cpu) { +struct irq_work *work; + + work = &per_cpu_ptr (&deferred_percpu_work, cpu)->work; +/* +* irq_work_sync() may end in synchronize_rcu(), for callers +* that free the work. Ours is static, so skip it while idle. +*/ +if (irq_work_is_busy (work)) +irq_work_sync (work); + } } static __fastpath_inline
Signature · 1 line
2.56.1