Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Harry Yoo <[email protected]>
Newsgroups org.kernel.vger.rcu,dev.linux.lists.linux-rt-devel,org.kernel.vger.bpf,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <[email protected]>

On 6/18/26 5:40 AM, Paul E. McKenney wrote:
> On Wed, Jun 17, 2026 at 07:38:16AM +0200, Vlastimil Babka (SUSE) wrote:
>> On 6/17/26 07:14, Harry Yoo wrote:
>>> On 6/17/26 2:24 AM, Vlastimil Babka (SUSE) wrote:
>>>> On 6/15/26 13:06, Harry Yoo (Oracle) wrote:
>>>>> As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used
>>>>> on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT.
>>>>> This is because local_trylock and spinlock_t are safe to use with
>>>>> trylock variant as long as the kernel does not spin and the context is
>>>>> not NMI and not hardirq.
>>>>>
>>>>> Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false,
>>>>> relax the limitation and try the sheaves path on PREEMPT_RT as well.
>>>>>
>>>>> Keep the lockdep map on non RT kernels. However, do not use the lockdep
>>>>> map on PREEMPT_RT to avoid suppressing valid lockdep warnings.
>>>>>
>>>>> Link: https://lore.kernel.org/linux-mm/[email protected]
>>>>> Suggested-by: Vlastimil Babka (SUSE) <[email protected]>
>>>>> Signed-off-by: Harry Yoo (Oracle) <[email protected]>
>>>>
>>>> LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT?
>>>> I thought (in the Link: above) we'd only need to downgrade allow_spin to
>>>> false on PREEMPT_RT for handling sheaves movement from/to barn and
>>>> alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on
>>>> RT?
>>>
>>> Indeed. Good point, thanks!
>>>
>>> Hmm, but I'm not sure that it's worth the complexity given that
>>> PREEMPT_RT tries very hard to avoid disabling IRQs...
>>>
>>>> Or is the irqs_disabled() condition rare enough so we don't care?
>>>
>>> Given that most users don't call kfree_rcu() under raw spinlock or
>>> IRQs-disabled section on PREEMPT_RT, I think it's okay to keep it as is
>>> (it's not making things worse, at least) and wait for call_rcu_nolock()?
>>
>> Sounds good.
>>
>>> On a side note, I don't have much idea on what needs to call kfree_rcu()
>>> under a raw spinlock, other than set_cpus_allowed_force(), which should
>>> really be using kfree_nolock() instead of kfree_rcu() once we support
>>> kmalloc() -> kfree_nolock():
>>
>> Looks like the case. Well if the fallback path of kfree_nolock() that is
>> irq_work_queue() is indeed safe here.
>>
>>>> 	/*
>>>> 	 * Because this is called with p->pi_lock held, it is not possible
>>>> 	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
>>>> 	 * kfree_rcu().
>>>> 	 */
>>>> 	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
>>>
>>> Any thoughts, RCU/RT folks? 

Thanks for looking into it, Paul!

Perhaps I'm missing some context here... let me clarify.

> For the call_rcu*() counterparts, I am currently considering making the
> existing functions check for interrupts disabled, using irq_work_queue()
> in that case. 

Ack.

> I suppose that I could use raise_softirq() in the
> use_softirq=1 case when in_hardirq(). 

Ack.

> Either way, the check should be
> cheap compared to rest of the processing.

Agreed.

> The additional rcu_barrier() work required is of course way down in

Assuming "The additional rcu_barrier() work required" means
rcu_barrier() now needs to wait for all CPUs to complete irq_work or
softirq...

> the noise compared to acquiring a global mutex.

Are you referring to a specific mutex, or just in general?

> In your case, kfree_rcu() can be quite a bit lighter weight, though.
> So the extra checks might not be lost in the noise.

Assuming "the extra checks" means checking whether interrupts are
disabled in call_rcu()*...

It will probably be fine since we invoke call_rcu() only when sheaves
become full, not once for each object?

Perhaps slightly off-topic; at some point, though, I think it'd be
better to teach SLUB to use RCU polling API for RCU sheaves.

e.g.) put sheaves into barn->sheaves_pending instead of invoking
call_rcu(), and the SLUB alloc slowpath checks if there are sheaves
past grace period before allocating and refilling sheaves.

-- 
Cheers,
Harry / Hyeonggon
OpenPGP_signature.asc (application/pgp-signature, 228 B)
-----BEGIN PGP SIGNATURE-----

iHUEARYKAB0WIQQQ1ub6gR5ogjaKRmOGXBN6rc5S1gUCajPojAAKCRCGXBN6rc5S
1iYYAP9R9SRxcpb+Lf9AzvYwC0ginyzWgaosKoEpwXTRJndtRQEApm4GZI/2RNXu
Q24xU67ieuyKBqFI2alWnLxiGr7+Bws=
=8O7i
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.