Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Harry Yoo <harry@kernel.org>
To: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Puranjay Mohan <puranjay@kernel.org>,
	Amery Hung <ameryhung@gmail.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Clark Williams <clrkwllms@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Frederic Weisbecker <frederic@kernel.org>,
	Neeraj Upadhyay <neeraj.upadhyay@kernel.org>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	Josh Triplett <josh@joshtriplett.org>,
	Boqun Feng <boqun@kernel.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Lai Jiangshan <jiangshanlai@gmail.com>,
	Zqiang <qiang.zhang@linux.dev>, Pedro Falcato <pfalcato@suse.de>,
	Suren Baghdasaryan <surenb@google.com>,
	Shengming Hu <hu.shengming@zte.com.cn>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linux-rt-devel@lists.linux.dev, rcu@vger.kernel.org,
	bpf@vger.kernel.org
Subject: Re: [PATCH slab/for-next v4 7/8] mm/slab: introduce kfree_rcu_nolock()
Date: Wed, 22 Jul 2026 17:15:24 +0900	[thread overview]
Message-ID: <734ecdc6-f415-41d8-9357-7fc11379e4da@kernel.org> (raw)
In-Reply-To: <c7607f25-5336-4f64-9244-9476dcc8ff3d@kernel.org>


[-- Attachment #1.1: Type: text/plain, Size: 3963 bytes --]



On 7/21/26 10:09 PM, Vlastimil Babka (SUSE) wrote:
> On 7/20/26 14:44, Harry Yoo (Oracle) wrote:
>> Currently, k[v]free_rcu() cannot be called in unknown context since
>> it could lead to a deadlock when called in the middle of k[v]free_rcu().
>>
>> Make users' lives easier by introducing kfree_rcu_nolock() variant,
>> now that kfree_rcu_sheaf() is available on PREEMPT_RT and
>> __kfree_rcu_sheaf() handles unknown context.
>>
>> kfree_rcu_nolock() falls back to the kvfree_rcu batching when sheaves
>> path fails.
> 
> From the code I would say it falls back to defer_kfree_rcu(), which is an
> irq_work that does kvfree_rcu batching?

Indeed that was what I intended to say, but I messed up while
writing it :-) Will fix that.

> Otherwise the sentence would contradict the following:
>
>> In most cases, the sheaves path is expected to succeed
>> and it's unnecessary to add complexity to the existing kvfree_rcu
>> batching by teaching it how to handle unknown context.
> 
> This talks about trying to perform the batching immediately without defering.

You mean the earlier sentence "kfree_rcu_nolock() falls back to the
kvfree_rcu batching when sheaves path fails", right?

>> Since defer_kfree_rcu() can be called on caches without sheaves, move
>> deferred_work_barrier() and rcu_barrier() outside the branch in
>> kvfree_rcu_barrier_on_cache().
>>
>> Signed-off-by: Harry Yoo (Oracle) <harry@kernel.org>
> 
> Other than that and a comment correction below, LGTM.
> 
> Reviewed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>

Thanks!

>> ---
>>  include/linux/rcupdate.h | 19 +++++++++++++++++++
>>  mm/slab_common.c         | 28 ++++++++++++++++++++++++++--
>>  mm/slub.c                | 24 +++++++++++++++++++++++-
>>  3 files changed, 68 insertions(+), 3 deletions(-)
>>
>> diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
>> index ef5bb6981133..ce0fd401352e 100644
>> --- a/include/linux/rcupdate.h
>> +++ b/include/linux/rcupdate.h
>> @@ -1099,6 +1099,7 @@ static inline void rcu_read_unlock_migrate(void)
>>   * In mm/slab_common.c, no suitable header to include here.
>>   */
>>  void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr);
>> +void kfree_call_rcu_nolock(struct kvfree_rcu_head *head, void *ptr);
>>  
>>  /*
>>   * The BUILD_BUG_ON() makes sure the rcu_head offset can be handled. See the
>> @@ -1124,6 +1125,24 @@ do {								\
>>  		kvfree_call_rcu(NULL, (void *) (___p));		\
>>  } while (0)
>>  
>> +/**
>> + * kfree_rcu_nolock() - a version of kfree_rcu() that can be called in any context.
>> + * @ptr: pointer to kfree for double-argument invocations.
>> + * @kvrhf: the name of the struct kvfree_rcu_head within the type of @ptr.
>> + *
>> + * Objects that are not allocated via kmalloc_nolock() are not supported.
> 
> Is this true? If __kfree_rcu_sheaf() succeeds, rcu_sheaf is either flushed
> normally (call_rcu with rcu_free_sheaf()) or via the irq_work doing the
> same. The defer_kfree_rcu() fallback ends up doing a normal
> kvfree_call_rcu(). I don't see any path that ends up doing kfree_nolock().
> Am I missing something?

I added this limitation because I thought it doesn't make sense
to support this when we don't support large kmalloc and vmalloc in
unknown context.

Perhaps too limiting than it should be though.

>> + * kfree_rcu_nolock() supports 2-arg variant only.
>> + */
>> +#define kfree_rcu_nolock(ptr, kvrhf)						\
>> +do {										\
>> +	typeof (ptr) ___p = (ptr);						\
>> +										\
>> +	if (___p) {								\
>> +		BUILD_BUG_ON(offsetof(typeof(*(ptr)), kvrhf) >= 4096);		\
>> +		kfree_call_rcu_nolock(&((___p)->kvrhf), (void *) (___p));	\
>> +	}									\
>> +} while (0)
>> +
>>  /*
>>   * Place this after a lock-acquisition primitive to guarantee that
>>   * an UNLOCK+LOCK pair acts as a full barrier.  This guarantee applies

-- 
Cheers,
Harry / Hyeonggon

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

  reply	other threads:[~2026-07-22  8:15 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-20 12:44 [PATCH slab/for-next v4 0/8] mm/slab: introduce kfree_rcu_nolock() and improve slub_kunit coverage Harry Yoo (Oracle)
2026-07-20 12:44 ` [PATCH slab/for-next v4 1/8] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs Harry Yoo (Oracle)
2026-07-22  3:27   ` hu.shengming
2026-07-22  7:16     ` Harry Yoo
2026-07-22  8:32       ` hu.shengming
2026-07-20 12:44 ` [PATCH slab/for-next v4 2/8] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf() Harry Yoo (Oracle)
2026-07-21 10:07   ` Vlastimil Babka (SUSE)
2026-07-22  7:28     ` Harry Yoo
2026-07-22  7:37       ` Vlastimil Babka (SUSE)
2026-07-22  7:39         ` Harry Yoo
2026-07-20 12:44 ` [PATCH slab/for-next v4 3/8] mm/slab: use call_rcu() in unknown context if irqs are enabled Harry Yoo (Oracle)
2026-07-20 12:44 ` [PATCH slab/for-next v4 4/8] mm/slab: extend deferred free mechanism to handle rcu sheaves Harry Yoo (Oracle)
2026-07-20 12:44 ` [PATCH slab/for-next v4 5/8] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT Harry Yoo (Oracle)
2026-07-21 10:46   ` Vlastimil Babka (SUSE)
2026-07-22  7:41     ` Harry Yoo
2026-07-20 12:44 ` [PATCH slab/for-next v4 6/8] mm/slab: introduce struct kvfree_rcu_head for kvfree_rcu batching Harry Yoo (Oracle)
2026-07-21 11:06   ` Vlastimil Babka (SUSE)
2026-07-22  7:42     ` Harry Yoo
2026-07-20 12:44 ` [PATCH slab/for-next v4 7/8] mm/slab: introduce kfree_rcu_nolock() Harry Yoo (Oracle)
2026-07-21 13:09   ` Vlastimil Babka (SUSE)
2026-07-22  8:15     ` Harry Yoo [this message]
2026-07-22 10:05       ` Vlastimil Babka (SUSE)
2026-07-22 13:03         ` Harry Yoo
2026-07-20 12:44 ` [PATCH slab/for-next v4 8/8] slub_kunit: extend the test for kfree_rcu_nolock() Harry Yoo (Oracle)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=734ecdc6-f415-41d8-9357-7fc11379e4da@kernel.org \
    --to=harry@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=ameryhung@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bigeasy@linutronix.de \
    --cc=boqun@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cl@gentwo.org \
    --cc=clrkwllms@kernel.org \
    --cc=frederic@kernel.org \
    --cc=hao.li@linux.dev \
    --cc=hu.shengming@zte.com.cn \
    --cc=jiangshanlai@gmail.com \
    --cc=joelagnelf@nvidia.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=neeraj.upadhyay@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=pfalcato@suse.de \
    --cc=puranjay@kernel.org \
    --cc=qiang.zhang@linux.dev \
    --cc=rcu@vger.kernel.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=surenb@google.com \
    --cc=urezki@gmail.com \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox