mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
Subsystems (3)
PATCH 1/1 mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
syzbot reports a possible circular locking dependency between &p->pi_lock and the per-CPU kfree_rcu sheaf lock (_T->lock) on PREEMPT_RT:
__balance_push_cpu_stop() [holds p->pi_lock, raw]
select_fallback_rq()
cpuset_cpus_allowed_fallback()
set_cpus_allowed_force()
kfree_rcu(ac.user_mask)
kvfree_call_rcu()
kfree_rcu_sheaf()
__kfree_rcu_sheaf()
local_trylock(&s->cpu_sheaves->lock) <- _T->lockset_cpus_allowed_force() uses kfree_rcu() instead of kfree() here specifically because it can be called with p->pi_lock (a raw spinlock) held, and plain kfree() may sleep under PREEMPT_RT.
Commit 2a8bb29ec9b2 ("mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT") made kvfree_call_rcu() try the sheaves fast path on PREEMPT_RT too, since __kfree_rcu_sheaf() only trylocks there and so cannot itself block. True, but the sheaf/barn locks it trylocks are also taken as regular, blocking locks elsewhere, so lockdep still records a lock-class ordering cycle against any raw spinlock already held by the caller, which is what syzbot caught.
The plain kfree_rcu()/kvfree_rcu() API gives kvfree_call_rcu() no way to know the caller is in such a context, so keep it conservative on PREEMPT_RT and skip the sheaves layer there, falling back to the existing raw_spinlock_t-protected krcp list, which is always safe to nest under another raw spinlock. This restores the pre-2a8bb29ec9b2 behavior of kvfree_call_rcu().
kfree_call_rcu_nolock(), added later in commit 3bc999d944b3 ("mm/slab: introduce kfree_rcu_nolock()") for unknown/atomic contexts, is unaffected: it already falls back to a lock-free defer_kfree_rcu() when the sheaves trylock doesn't pan out, so it keeps using SLAB_FREE_NOLOCK on PREEMPT_RT. Callers like set_cpus_allowed_force() that want the sheaves fast path under a raw spinlock should migrate to that API instead.
--- mm/slab_common.c | 9 ++++++++- mm/slub.c | 7 ++++--- 2 files changed, 12 insertions(+), 4 deletions(-)
mm/slab_common.c +8 -1
@@ -2034,7 +2034,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw spinlock here on PREEMPT_RT (e.g. +* set_cpus_allowed_force() with p->pi_lock held), and the sheaf/barn +* locks are also taken as blocking locks elsewhere, so trying them +* here creates a lockdep-visible ordering conflict. Skip sheaves on +* PREEMPT_RT; use kfree_rcu_nolock() instead if this doesn't apply. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))return ;// Queue the object but don't yet schedule the batch.
mm/slub.c +4 -3
@@ -6088,10 +6088,11 @@ static void rcu_free_sheaf(struct rcu_head *head)/* * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT), -* this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids -* this problem by passing SLAB_FREE_NOLOCK on PREEMPT_RT. +* this would violate lock nesting rules. kvfree_call_rcu() avoids this by +* bypassing the sheaves layer on PREEMPT_RT; use kfree_call_rcu_nolock() +* instead for atomic/unknown-context callers that need the sheaves path. * -* However, lockdep still complains that it is invalid to acquire spinlock_t +* lockdep still complains that it is invalid to acquire spinlock_t * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a * spinning lock. Tell lockdep that acquiring spinlock_t is valid here * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Medium] The commit message and comments incorrectly recommend using kfree_call_rcu_nolock() under a raw spinlock on PREEMPT_RT, but doing so will trigger the exact same lockdep violation.
- [Low] The patch introduces unreachable dead code inside kfree_rcu_sheaf().
Pre-existing issues:
- [High] Headless kfree_rcu() calls might_sleep() and synchronize_rcu(), making it unsafe to call under a raw spinlock. --
commit 766d83bf3f72fb4bed74c42f068169aae388d06f
Author: ThangNN99 <[email protected]>
mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
This patch skips the sheaves fast path on PREEMPT_RT in kvfree_call_rcu() to avoid a lockdep circular dependency when the caller holds a raw spinlock. It modifies kvfree_call_rcu() to avoid calling into the sheaf machinery when PREEMPT_RT is enabled, falling back to the krcp list. The commit message and comments also suggest raw spinlock callers needing the fast path should migrate to kfree_call_rcu_nolock() instead.
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2036
Quoted 8 lines
mm/slab_common.c +0
-0@@ -2034,7 +2034,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep ();
[Severity: High]
This is a pre-existing issue, but does using headless kfree_rcu() under a raw spinlock actually avoid sleeping?
The commit message states that set_cpus_allowed_force() uses kfree_rcu() because it holds the raw p->pi_lock and cannot sleep. However, if head is NULL (which is the case for the single-argument kfree_rcu() macro), this might_sleep() is unconditionally called. Does this mean set_cpus_allowed_force() is actually triggering a "sleeping function called from invalid context" bug?
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2044
Quoted 9 lines
mm/slab_common.c +8
-1-if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw spinlock here on PREEMPT_RT (e.g. +* set_cpus_allowed_force() with p->pi_lock held), and the sheaf/barn +* locks are also taken as blocking locks elsewhere, so trying them +* here creates a lockdep-visible ordering conflict. Skip sheaves on +* PREEMPT_RT; use kfree_rcu_nolock() instead if this doesn't apply. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))
[Severity: Medium]
Will callers migrating to kfree_call_rcu_nolock() on PREEMPT_RT hit the same issue this patch is trying to fix?
Looking at kfree_call_rcu_nolock(), it unconditionally calls __kfree_rcu_sheaf() with SLAB_FREE_NOLOCK:
bool kfree_call_rcu_nolock(void *ptr)
{
...
if (unlikely(!__kfree_rcu_sheaf(slab->slab_cache, ptr, SLAB_FREE_NOLOCK)))
...
}This in turn calls local_trylock(&s->cpu_sheaves->lock). On PREEMPT_RT, this lock is a sleepable spinlock_t. Doesn't acquiring a sleepable lock under a raw spinlock violate lock nesting rules, even with a trylock?
[Severity: Low]
Does skipping kfree_rcu_sheaf() when PREEMPT_RT is enabled turn the early check inside kfree_rcu_sheaf() into unreachable dead code?
kfree_rcu_sheaf() has an early check:
static bool kfree_rcu_sheaf(void *obj)
{
...
if (IS_ENABLED(CONFIG_PREEMPT_RT))
free_flags = SLAB_FREE_NOLOCK;
...
}Since kvfree_call_rcu() is the only caller of kfree_rcu_sheaf(), that code appears to be completely unreachable now.
Quoted 2 lines
mm/slab_common.c +0
-0return ;
[ ... ]
Quoted 6 lines
mm/slub.c +0
-0@@ -6088,10 +6088,11 @@ static void rcu_free_sheaf(struct rcu_head *head)/*
mm/slub.c static void rcu_free_sheaf(struct rcu_head *head) · line 6095
Quoted 10 lines
mm/slub.c +4
-3* kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT), -* this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids -* this problem by passing SLAB_FREE_NOLOCK on PREEMPT_RT. +* this would violate lock nesting rules. kvfree_call_rcu() avoids this by +* bypassing the sheaves layer on PREEMPT_RT; use kfree_call_rcu_nolock() +* instead for atomic/unknown-context callers that need the sheaves path. * -* However, lockdep still complains that it is invalid to acquire spinlock_t +* lockdep still complains that it is invalid to acquire spinlock_t
[Severity: Medium]
As mentioned above, if callers follow this comment and use kfree_call_rcu_nolock() under a raw spinlock on PREEMPT_RT, won't lockdep still complain because kfree_call_rcu_nolock() will call __kfree_rcu_sheaf() which attempts to acquire the sleepable spinlock_t?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
In reply to ThangNN99syzbot reports a possible circular locking dependency between &p->pi_lock and the per-CPU kfree_rcu sheaf lock (_T->lock) on PREEMPT_RT:
__balance_push_cpu_stop() [holds p->pi_lock, raw]
select_fallback_rq()
cpuset_cpus_allowed_fallback()
set_cpus_allowed_force()
kfree_rcu(ac.user_mask)
kvfree_call_rcu()
kfree_rcu_sheaf()
__kfree_rcu_sheaf()
local_trylock(&s->cpu_sheaves->lock) <- _T->lockset_cpus_allowed_force() uses kfree_rcu() instead of kfree() here specifically because it can be called with p->pi_lock (a raw spinlock) held, and plain kfree() may sleep under PREEMPT_RT.
Commit 2a8bb29ec9b2 ("mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT") made kvfree_call_rcu() try the sheaves fast path on PREEMPT_RT too, since __kfree_rcu_sheaf() only trylocks there and so cannot itself block. True, but the sheaf/barn locks it trylocks are also taken as regular, blocking locks elsewhere, so lockdep still records a lock-class ordering cycle against any raw spinlock already held by the caller, which is what syzbot caught.
The plain kfree_rcu()/kvfree_rcu() API gives kvfree_call_rcu() no way to know the caller is in such a context, so keep it conservative on PREEMPT_RT and skip the sheaves layer there, falling back to the existing raw_spinlock_t-protected krcp list, which is always safe to nest under another raw spinlock. This restores the pre-2a8bb29ec9b2 behavior of kvfree_call_rcu().
kfree_call_rcu_nolock(), added later in commit 3bc999d944b3 ("mm/slab: introduce kfree_rcu_nolock()"), is untouched by this patch. Note it would not be a safe substitute here either: it still reaches __kfree_rcu_sheaf()'s local_trylock() on &s->cpu_sheaves->lock unconditionally, so a caller already holding a raw spinlock would hit the same lockdep ordering cycle through that path too.
---
v2 (per automated review on v1):
- Corrected commit message / comments: kfree_call_rcu_nolock() is not a safe alternative here either, since it still trylocks the same &s->cpu_sheaves->lock unconditionally.
- Removed the now-unreachable CONFIG_PREEMPT_RT branch inside kfree_rcu_sheaf() left over by this fix (it can no longer run, since kvfree_call_rcu() already skips calling it on PREEMPT_RT).
mm/slab_common.c | 20 ++++++++++---------- mm/slub.c | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-)
mm/slab_common.c +10 -10
@@ -1667,15 +1667,8 @@ static bool kfree_rcu_sheaf(void *obj) {struct kmem_cache *s;struct slab *slab; -unsigned int free_flags =SLAB_FREE_DEFAULT ; - -/* -* It is not safe to spin on PREEMPT_RT because the kernel might be -* holding a raw spinlock and slab acquires sleeping locks. -*/ -if (IS_ENABLED (CONFIG_PREEMPT_RT )) - free_flags =SLAB_FREE_NOLOCK ; +/* Callers on PREEMPT_RT never reach here, see kvfree_call_rcu(). */ if (is_vmalloc_addr (obj))return false;
@@ -1685,7 +1678,7 @@ static bool kfree_rcu_sheaf(void *obj) s = slab->slab_cache;if (likely (!IS_ENABLED (CONFIG_NUMA ) ||slab_nid (slab) ==numa_mem_id ())) -return __kfree_rcu_sheaf (s, obj, free_flags); +return __kfree_rcu_sheaf (s, obj,SLAB_FREE_DEFAULT );return false; }
@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw spinlock here on PREEMPT_RT (e.g. +* set_cpus_allowed_force() with p->pi_lock held), and the sheaf/barn +* locks are also taken as blocking locks elsewhere, so trying them +* here creates a lockdep-visible ordering conflict. Skip sheaves on +* PREEMPT_RT. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))return ;// Queue the object but don't yet schedule the batch.
mm/slub.c +3 -3
@@ -6088,10 +6088,10 @@ static void rcu_free_sheaf(struct rcu_head *head)/* * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT), -* this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids -* this problem by passing SLAB_FREE_NOLOCK on PREEMPT_RT. +* this would violate lock nesting rules. kvfree_call_rcu() avoids this by +* bypassing the sheaves layer on PREEMPT_RT. * -* However, lockdep still complains that it is invalid to acquire spinlock_t +* lockdep still complains that it is invalid to acquire spinlock_t * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a * spinning lock. Tell lockdep that acquiring spinlock_t is valid here * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Lock recursion deadlock in kfree_rcu() on !PREEMPT_RT when caller holds scheduler locks
- [High] kfree_call_rcu_nolock() violates its 'any context' API guarantee on PREEMPT_RT, leading to Invalid Wait Context --
commit 405a1ecd91cd7581e29421663a94a33714163104
Author: ThangNN99 <[email protected]>
mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
This commit addresses a lockdep warning by skipping the sheaf fast path in kvfree_call_rcu() on PREEMPT_RT, avoiding a local_trylock() under a raw spinlock. It also notes that kfree_call_rcu_nolock() has a similar problem but leaves it untouched.
Quoted 6 lines
kfree_call_rcu_nolock(), added later in commit3bc999d944b3("mm/slab: introducekfree_rcu_nolock()"), is untouched by this patch. Note it would not be a safe substitute here either: it still reaches __kfree_rcu_sheaf()'slocal_trylock()on&s->cpu_sheaves->lockunconditionally, so a caller already holding a raw spinlock would hit the same lockdep ordering cycle through that path too.
[Severity: High]
This is a pre-existing issue, but does leaving this unaddressed violate the API guarantee of kfree_call_rcu_nolock() in mm/slab_common.c?
The documentation for kfree_call_rcu_nolock() states it is safe for any context. However, on PREEMPT_RT it leads to this call chain:
kfree_call_rcu_nolock()
__kfree_rcu_sheaf(..., SLAB_FREE_NOLOCK)
local_trylock(&s->cpu_sheaves->lock)
Since local_lock maps to a sleepable rt_mutex on PREEMPT_RT, executing this trylock from a raw spinlock or hardirq context violates wait-type rules.
Won't any subsystem using kfree_call_rcu_nolock() in these contexts still trigger an Invalid wait context lockdep splat on PREEMPT_RT?
Quoted 34 lines
mm/slab_common.c +2
-9@@ -1667,15 +1667,8 @@ static bool kfree_rcu_sheaf(void *obj) {struct kmem_cache *s;struct slab *slab; -unsigned int free_flags =SLAB_FREE_DEFAULT ; - -/* -* It is not safe to spin on PREEMPT_RT because the kernel might be -* holding a raw spinlock and slab acquires sleeping locks. -*/ -if (IS_ENABLED (CONFIG_PREEMPT_RT )) - free_flags =SLAB_FREE_NOLOCK ; +/* Callers on PREEMPT_RT never reach here, see kvfree_call_rcu(). */ if (is_vmalloc_addr (obj))return false;@@ -1685,7 +1678,7 @@ static bool kfree_rcu_sheaf(void *obj) s = slab->slab_cache;if (likely (!IS_ENABLED (CONFIG_NUMA ) ||slab_nid (slab) ==numa_mem_id ())) -return __kfree_rcu_sheaf (s, obj, free_flags); +return __kfree_rcu_sheaf (s, obj,SLAB_FREE_DEFAULT );return false; }@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep ();
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2038
Quoted 10 lines
mm/slab_common.c +8
-1-if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw spinlock here on PREEMPT_RT (e.g. +* set_cpus_allowed_force() with p->pi_lock held), and the sheaf/barn +* locks are also taken as blocking locks elsewhere, so trying them +* here creates a lockdep-visible ordering conflict. Skip sheaves on +* PREEMPT_RT. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))return ;
[Severity: High]
This isn't a bug introduced by this patch, but does this lead to a lock recursion deadlock on !PREEMPT_RT?
On !PREEMPT_RT, this can result in the following call chain:
kvfree_call_rcu()
kfree_rcu_sheaf()
__kfree_rcu_sheaf(s, obj, SLAB_FREE_DEFAULT)
alloc_empty_sheaf() with GFP_NOWAIT
wakeup_kswapd()
try_to_wake_up()
Since GFP_NOWAIT includes __GFP_KSWAPD_RECLAIM, the page allocator can call wakeup_kswapd() if the zone watermark is low.
This enters the scheduler and attempts to acquire scheduler locks. If the caller, like set_cpus_allowed_force() in the trace above, already holds a scheduler raw spinlock, won't this result in a hard deadlock?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks, both flagged items are pre-existing and out of scope for this patch:
- The headless kvfree_rcu(ptr) might_sleep()/synchronize_rcu() path is never reached by the caller this patch fixes: set_cpus_allowed_force() uses the headed two-argument kfree_rcu(ptr, rcu) form specifically to avoid it (see its own comment), so it's unrelated to the sheaf-bypass this patch adds.
- kfree_call_rcu_nolock() still hitting local_trylock(&s->cpu_sheaves->lock) unconditionally is exactly what this patch's commit message already calls out as unsafe under a raw spinlock. This patch intentionally leaves kfree_call_rcu_nolock() untouched; fixing its 'any context' guarantee belongs in a separate patch.
This patch stays scoped to the specific syzbot-reported splat in kvfree_call_rcu(). Happy to look at the two pre-existing issues separately if a maintainer wants a follow-up patch for either.
Thanks,
ThangNN99
Sebastian Andrzej Siewior
maintainerQuoted 14 lines · On 2026-08-31 13:06:32 [+0700], ThangNN99 wrote:
syzbot reports a possible circular locking dependency between
&p->pi_lockand the per-CPUkfree_rcusheaf lock (_T->lock) onPREEMPT_RT:
__balance_push_cpu_stop()[holdsp->pi_lock, raw]select_fallback_rq() cpuset_cpus_allowed_fallback() set_cpus_allowed_force() kfree_rcu(ac.user_mask) kvfree_call_rcu() kfree_rcu_sheaf() __kfree_rcu_sheaf() local_trylock(&s->cpu_sheaves->lock) <- _T->lock
Quoted 3 lines
set_cpus_allowed_force()useskfree_rcu()instead ofkfree()here specifically because it can be called withp->pi_lock(a raw spinlock) held, and plainkfree()may sleep underPREEMPT_RT.
A raw_spinlock_t. Please don't invent new things.
…
Quoted 31 lines
mm/slab_common.c +2
-9@@ -1667,15 +1667,8 @@ static bool kfree_rcu_sheaf(void *obj) {struct kmem_cache *s;struct slab *slab; -unsigned int free_flags =SLAB_FREE_DEFAULT ; - -/* -* It is not safe to spin on PREEMPT_RT because the kernel might be -* holding a raw spinlock and slab acquires sleeping locks. -*/ -if (IS_ENABLED (CONFIG_PREEMPT_RT )) - free_flags =SLAB_FREE_NOLOCK ; +/* Callers on PREEMPT_RT never reach here, see kvfree_call_rcu(). */ if (is_vmalloc_addr (obj))return false;@@ -1685,7 +1678,7 @@ static bool kfree_rcu_sheaf(void *obj) s = slab->slab_cache;if (likely (!IS_ENABLED (CONFIG_NUMA ) ||slab_nid (slab) ==numa_mem_id ())) -return __kfree_rcu_sheaf (s, obj, free_flags); +return __kfree_rcu_sheaf (s, obj,SLAB_FREE_DEFAULT );return false; }
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2032
Quoted 7 lines
mm/slab_common.c +3
-1@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw spinlock here on PREEMPT_RT (e.g. +* set_cpus_allowed_force() with p->pi_lock held), and the sheaf/barn
No. All callers of set_cpus_allowed_force() hold task_struct::pi_lock.
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2037
Quoted 5 lines
mm/slab_common.c +5
-0+* locks are also taken as blocking locks elsewhere, so trying them +* here creates a lockdep-visible ordering conflict. Skip sheaves on +* PREEMPT_RT. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))
Given that this can hold the pi_lock I am not a big of this trylock underneath. So skipping it is my favorite.
Quoted 11 lines
mm/slab_common.c +0
-0return ;// Queue the object but don't yet schedule the batch. mm/slub.c +0
-0@@ -6088,10 +6088,10 @@ static void rcu_free_sheaf(struct rcu_head *head)/* * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT),
mm/slub.c static void rcu_free_sheaf(struct rcu_head *head) · line 6097
Quoted 10 lines
mm/slub.c +3
-3-* this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids -* this problem by passing SLAB_FREE_NOLOCK on PREEMPT_RT. +* this would violate lock nesting rules. kvfree_call_rcu() avoids this by +* bypassing the sheaves layer on PREEMPT_RT. * -* However, lockdep still complains that it is invalid to acquire spinlock_t +* lockdep still complains that it is invalid to acquire spinlock_t * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a * spinning lock. Tell lockdep that acquiring spinlock_t is valid here * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
Sebastian
PATCH v3 1/1 mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
syzbot reports a possible circular locking dependency between &p->pi_lock and the per-CPU kfree_rcu sheaf lock (_T->lock) on PREEMPT_RT:
__balance_push_cpu_stop() [holds p->pi_lock]
select_fallback_rq()
cpuset_cpus_allowed_fallback()
set_cpus_allowed_force()
kfree_rcu(ac.user_mask)
kvfree_call_rcu()
kfree_rcu_sheaf()
__kfree_rcu_sheaf()
local_trylock(&s->cpu_sheaves->lock) <- _T->lockset_cpus_allowed_force() uses kfree_rcu() instead of kfree() here because all of its callers hold task_struct::pi_lock (a raw_spinlock_t), and plain kfree() may sleep under PREEMPT_RT.
Commit 2a8bb29ec9b2 ("mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT") made kvfree_call_rcu() try the sheaves fast path on PREEMPT_RT too, since __kfree_rcu_sheaf() only trylocks there and so cannot itself block. True, but the sheaf/barn locks it trylocks are also taken as regular, blocking locks elsewhere, so lockdep still records a lock-class ordering cycle against any raw_spinlock_t already held by the caller, which is what syzbot caught.
The plain kfree_rcu()/kvfree_rcu() API gives kvfree_call_rcu() no way to know the caller is in such a context, so keep it conservative on PREEMPT_RT and skip the sheaves layer there, falling back to the existing raw_spinlock_t-protected krcp list, which is always safe to nest under another raw_spinlock_t. This restores the pre-2a8bb29ec9b2 behavior of kvfree_call_rcu().
kfree_call_rcu_nolock(), added later in commit 3bc999d944b3 ("mm/slab: introduce kfree_rcu_nolock()"), is untouched by this patch. Note it would not be a safe substitute here either: it still reaches __kfree_rcu_sheaf()'s local_trylock() on &s->cpu_sheaves->lock unconditionally, so a caller already holding a raw_spinlock_t would hit the same lockdep ordering cycle through that path too.
---
v3 (per Sebastian Andrzej Siewior's review on v2):
- Say "raw_spinlock_t" instead of the vague "a raw spinlock" throughout the commit message and comment.
- State plainly that *all* callers of set_cpus_allowed_force() hold task_struct::pi_lock, not just that it "can be called" with it held.
v2 (per automated review on v1):
- Corrected commit message / comments: kfree_call_rcu_nolock() is not a safe alternative here either, since it still trylocks the same &s->cpu_sheaves->lock unconditionally.
- Removed the now-unreachable CONFIG_PREEMPT_RT branch inside kfree_rcu_sheaf() left over by this fix (it can no longer run, since kvfree_call_rcu() already skips calling it on PREEMPT_RT).
mm/slab_common.c | 20 ++++++++++---------- mm/slub.c | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-)
mm/slab_common.c +10 -10
@@ -1667,15 +1667,8 @@ static bool kfree_rcu_sheaf(void *obj) {struct kmem_cache *s;struct slab *slab; -unsigned int free_flags =SLAB_FREE_DEFAULT ; - -/* -* It is not safe to spin on PREEMPT_RT because the kernel might be -* holding a raw spinlock and slab acquires sleeping locks. -*/ -if (IS_ENABLED (CONFIG_PREEMPT_RT )) - free_flags =SLAB_FREE_NOLOCK ; +/* Callers on PREEMPT_RT never reach here, see kvfree_call_rcu(). */ if (is_vmalloc_addr (obj))return false;
@@ -1685,7 +1678,7 @@ static bool kfree_rcu_sheaf(void *obj) s = slab->slab_cache;if (likely (!IS_ENABLED (CONFIG_NUMA ) ||slab_nid (slab) ==numa_mem_id ())) -return __kfree_rcu_sheaf (s, obj, free_flags); +return __kfree_rcu_sheaf (s, obj,SLAB_FREE_DEFAULT );return false; }
@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw_spinlock_t here on PREEMPT_RT (e.g. +* set_cpus_allowed_force(), whose callers all hold +* task_struct::pi_lock), and the sheaf/barn locks are also taken +* as blocking locks elsewhere, so trying them here creates a +* lockdep-visible ordering conflict. Skip sheaves on PREEMPT_RT. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))return ;// Queue the object but don't yet schedule the batch.
mm/slub.c +3 -3
@@ -6088,10 +6088,10 @@ static void rcu_free_sheaf(struct rcu_head *head)/* * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT), -* this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids -* this problem by passing SLAB_FREE_NOLOCK on PREEMPT_RT. +* this would violate lock nesting rules. kvfree_call_rcu() avoids this by +* bypassing the sheaves layer on PREEMPT_RT. * -* However, lockdep still complains that it is invalid to acquire spinlock_t +* lockdep still complains that it is invalid to acquire spinlock_t * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a * spinning lock. Tell lockdep that acquiring spinlock_t is valid here * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] __kfree_rcu_sheaf() allocates with GFP_NOWAIT on !PREEMPT_RT, which can trigger wakeup_kswapd() and deadlock against scheduler locks (e.g., pi_lock).
- [High] kfree_call_rcu_nolock() unconditionally calls __kfree_rcu_sheaf() and triggers lockdep wait context violations and circular dependencies on PREEMPT_RT when called from raw spinlock context. --
commit fade274cd80fc5162ad3fd4de36a4d5b181861b2
Author: ThangNN99 <[email protected]>
mm/slab: don't use kfree_rcu sheaves on PREEMPT_RT in kvfree_call_rcu()
This commit fixes a circular locking dependency on PREEMPT_RT by skipping the kfree_rcu sheaves layer in kvfree_call_rcu() when called from contexts that hold a raw spinlock. This safely falls back to the existing list.
Quoted 6 lines
kfree_call_rcu_nolock(), added later in commit3bc999d944b3("mm/slab: introducekfree_rcu_nolock()"), is untouched by this patch. Note it would not be a safe substitute here either: it still reaches __kfree_rcu_sheaf()'slocal_trylock()on&s->cpu_sheaves->lockunconditionally, so a caller already holding araw_spinlock_twould hit the same lockdep ordering cycle through that path too.
[Severity: High]
This is a pre-existing issue, but since the patch leaves kfree_call_rcu_nolock() untouched, does the API contract for the nolock variant remain violated on PREEMPT_RT?
As noted in the commit message, calling kfree_call_rcu_nolock() still unconditionally reaches __kfree_rcu_sheaf() with SLAB_FREE_NOLOCK, which executes:
mm/slub.c:__kfree_rcu_sheaf() {
...
if (!local_trylock(&s->cpu_sheaves->lock))
goto fail;
...
empty = alloc_empty_sheaf(s, gfp, alloc_flags);
...
}While local_trylock() bypasses the Invalid wait context check, it still creates a lock-class ordering dependency (raw_spinlock_t -> spinlock_t), which causes circular dependency splats against other paths taking the lock.
Furthermore, if it needs an empty sheaf, it calls alloc_empty_sheaf(), which enters the page allocator. The page allocator acquires sleeping locks like zone->lock via spin_lock_irqsave. This triggers a hard Invalid wait context violation and exposes the system to genuine deadlocks on PREEMPT_RT.
Could this be addressed to ensure kfree_call_rcu_nolock() is truly safe for raw spinlock contexts as intended?
Quoted 4 lines
mm/slab_common.c +0
-0
[ ... ]
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2037
Quoted 13 lines
mm/slab_common.c +8
-1@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw_spinlock_t here on PREEMPT_RT (e.g. +* set_cpus_allowed_force(), whose callers all hold +* task_struct::pi_lock), and the sheaf/barn locks are also taken +* as blocking locks elsewhere, so trying them here creates a +* lockdep-visible ordering conflict. Skip sheaves on PREEMPT_RT. +*/ +if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))
[Severity: Critical]
This isn't a bug introduced by this patch, but does falling back to kfree_rcu_sheaf() on !PREEMPT_RT expose us to a deadlock when called under scheduler locks?
When kvfree_call_rcu() is called holding pi_lock, like in set_cpus_allowed_force(), on !PREEMPT_RT it enters __kfree_rcu_sheaf() with allow_spin evaluating to true. This sets up the allocation:
mm/slub.c:__kfree_rcu_sheaf() {
...
gfp_t gfp = allow_spin ? GFP_NOWAIT : __GFP_NOWARN;
...
empty = alloc_empty_sheaf(s, gfp, alloc_flags);
...
}GFP_NOWAIT includes __GFP_KSWAPD_RECLAIM. If alloc_empty_sheaf() needs to be called, the page allocator will wake kswapd.
Waking kswapd calls try_to_wake_up(), which attempts to acquire scheduler locks such as the runqueue lock or kswapd->pi_lock.
If the original caller already holds pi_lock or rq_lock, wouldn't this cause lock recursion (e.g., rq_lock -> pi_lock -> rq_lock) and deadlock the system?
Quoted 3 lines
mm/slab_common.c +0
-0return ;// Queue the object but don't yet schedule the batch.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks, both are pre-existing and out of scope for this patch:
- The GFP_NOWAIT/wakeup_kswapd() concern in __kfree_rcu_sheaf() applies identically before and after this patch on !PREEMPT_RT; this patch only changes the PREEMPT_RT branch. It also predates the commit this patch fixes: alloc_empty_sheaf() has used GFP_NOWAIT in this call chain since the sheaves feature was introduced in ec66e0d59952 ("slab: add sheaf support for batching kfree_rcu() operations"), not since 2a8bb29ec9b2. It's a real question about the sheaf/barn allocation design itself, not something a small, targeted fix for the syzbot-reported splat should also try to redesign.
- Same as noted for the previous review: kfree_call_rcu_nolock() is intentionally left untouched by this patch. Agreed it doesn't fully deliver on its 'any context' contract on PREEMPT_RT either, for the reasons given (local_trylock ordering, and now also alloc_empty_sheaf() reaching zone->lock). That's a separate fix.
Both would be good candidates for their own patches from whoever knows the sheaf/barn allocation path best; happy to help test if one gets posted. This patch stays scoped to the specific circular-locking splat in kvfree_call_rcu() reported by syzbot.
Thanks,
ThangNN99
Sebastian Andrzej Siewior
maintainerQuoted 4 lines · On 2026-08-31 20:32:22 [+0700], ThangNN99 wrote:
mm/slab_common.c +0
-0@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2036
Quoted 10 lines
mm/slab_common.c +7
-1might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw_spinlock_t here on PREEMPT_RT (e.g. +* set_cpus_allowed_force(), whose callers all hold +* task_struct::pi_lock), and the sheaf/barn locks are also taken +* as blocking locks elsewhere, so trying them here creates a +* lockdep-visible ordering conflict. Skip sheaves on PREEMPT_RT. +*/
You mix up things. A raw_spinlock_t should work in general and should not cause a problem. The task_struct::pi_lock is special: It used during wakes and the inner waitlock of the rtmutex acquires it even during a trylock. For PREEMPT_RT we don't want to acquire any locks while the pi_lock is held.
What about
kvfree_rcu() is called by set_cpus_allowed_force() with task_struct::pi_lock acquired. On PREEMPT_RT the local_trylock() usage below will acquire the waitlock which must be avoided. Therefore avoid it on PREEMPT_RT.
Vlastimil?
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2040
Quoted 4 lines
mm/slab_common.c +1
-0+if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))return ;// Queue the object but don't yet schedule the batch.
Sebastian
Vlastimil Babka (SUSE)
maintainerQuoted 22 lines · Sebastian Andrzej Siewior
Quoted 4 lines · On 2026-08-31 20:32:22 [+0700], ThangNN99 wrote:
mm/slab_common.c +0
-0@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2036
Quoted 10 lines
mm/slab_common.c +7
-1might_sleep (); -if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw_spinlock_t here on PREEMPT_RT (e.g. +* set_cpus_allowed_force(), whose callers all hold +* task_struct::pi_lock), and the sheaf/barn locks are also taken +* as blocking locks elsewhere, so trying them here creates a +* lockdep-visible ordering conflict. Skip sheaves on PREEMPT_RT. +*/ You mix up things. A
raw_spinlock_tshould work in general and should not cause a problem. Thetask_struct::pi_lockis special: It used during wakes and the inner waitlock of the rtmutex acquires it even during a trylock. ForPREEMPT_RTwe don't want to acquire any locks while thepi_lockis held.
Quoted 6 lines
What about
kvfree_rcu()is called byset_cpus_allowed_force()withtask_struct::pi_lockacquired. OnPREEMPT_RTthelocal_trylock()usage below will acquire the waitlock which must be avoided. Therefore avoid it onPREEMPT_RT.
Thanks.
Quoted 1 line
Vlastimil?
IMHO it's not useful to iterate a fix through emails with apparently a LLM agent, so let's continue here:
https://lore.kernel.org/all/[email protected]/
Quoted 7 lines
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2040
Quoted 5 lines
mm/slab_common.c +1
-0+if (!IS_ENABLED (CONFIG_PREEMPT_RT ) &&kfree_rcu_sheaf (ptr))return ;// Queue the object but don't yet schedule the batch. Sebastian
ThangNN99
Tested-byRe: [PATCH] mm/slab: don't use kfree_rcu_sheaf() on PREEMPT_RT again
Thanks Vlastimil, this matches what I found and is functionally the same fix I had in my v3. I built this exact logic (skip kfree_rcu_sheaf() under CONFIG_PREEMPT_RT in kvfree_call_rcu(), same dead-code removal in kfree_rcu_sheaf()) against commit 08dbfad3f504 with the syzbot .config and reproduced the original splat with syzbot's C repro (writing "off" to /sys/devices/system/cpu/smt/control to trigger the __balance_push_cpu_stop -> select_fallback_rq -> cpuset_cpus_allowed_fallback -> set_cpus_allowed_force -> kvfree_call_rcu chain) — confirmed the exact same lockdep report as syzbot, then rebuilt with the fix and confirmed the splat no longer appears while the same code path still executes.
Feel free to add:
Thanks for picking this up and for the pi_lock/waitlock explanation, that's a much more precise description than what I had.
ThangNN99
Harry Yoo
maintainerQuoted 7 lines · Sebastian Andrzej Siewior
Quoted 6 lines · On 2026-08-31 20:32:22 [+0700], ThangNN99 wrote:
mm/slab_common.c +0
-0@@ -2034,7 +2027,14 @@ void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr)if (!head)might_sleep ();
Quoted 14 lines
mm/slab_common.c void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr) · line 2036
Quoted 8 lines
mm/slab_common.c +7
-1-if (kfree_rcu_sheaf (ptr)) +/* +* Callers may hold a raw_spinlock_t here on PREEMPT_RT (e.g. +* set_cpus_allowed_force(), whose callers all hold +* task_struct::pi_lock), and the sheaf/barn locks are also taken +* as blocking locks elsewhere, so trying them here creates a +* lockdep-visible ordering conflict. Skip sheaves on PREEMPT_RT. +*/ You mix up things. A
raw_spinlock_tshould work in general and should not cause a problem. Thetask_struct::pi_lockis special: It used during wakes and the inner waitlock of the rtmutex acquires it even during a trylock. ForPREEMPT_RTwe don't want to acquire any locks while thepi_lockis held.
So... even can_spin_trylock() is not enough handle the pi_lock's special case.
Probably we should make can_spin_trylock() check if current tasks's pi_lock is acquired, to determine if it's safe to call _trylock()?
Quoted 6 lines
What about
kvfree_rcu()is called byset_cpus_allowed_force()withtask_struct::pi_lockacquired. OnPREEMPT_RTthelocal_trylock()usage below will acquire the waitlock which must be avoided. Therefore avoid it onPREEMPT_RT.
Signature · 2 lines
Cheers, Harry / Hyeonggon