Re: [PATCH v2] irq_work: Fix use-after-free in irq_work_single on PREEMPT_RT
From: Vlastimil Babka (SUSE)
Date: Fri Nov 27 2026 - 15:45:02 EST
On 8/19/26 13:46, Jan Kiszka wrote:
> On 19.08.26 12:52, Sebastian Andrzej Siewior wrote:
>> On 2026-08-19 12:44:57 [+0200], Jan Kiszka wrote:
>>> 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:
>>
>> 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.
>
>>
>>> ... || !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?
>>
>> 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
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 <mason@xxxxxxxxxx>
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.
Fixes: 91840be8f710 ("irq_work: Fix use-after-free in irq_work_single() on PREEMPT_RT")
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/slub.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/mm/slub.c b/mm/slub.c
index 3b8ac5310d07f..3318e94350947 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -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
--
2.56.1