Message ID | 20221019224659.2499511-5-paulmck@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp571633wrs; Wed, 19 Oct 2022 15:52:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4CVwA+xroJfirQy0QLv5EUv6ces+xtpa+Fq8eWxRWZ41s4R4AlafH4wVTaglI+FZtxA5MP X-Received: by 2002:a17:907:7b8b:b0:78d:cee5:3145 with SMTP id ne11-20020a1709077b8b00b0078dcee53145mr8258807ejc.537.1666219955981; Wed, 19 Oct 2022 15:52:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666219955; cv=none; d=google.com; s=arc-20160816; b=kHW2g0EeHpN1J/AxOqmaocy803n/vjzCowQSbAUpXC9/FW9+YdOfDd+KIfcd9goVcS ftSg7ZlZn4+AiLPKL2g3ftJcWfZdKRWJoAUo7nZZ+4Fu1O/MstzM+4wJZdUU3BVYRPLp aCNhAGORCre8Ae+PArMPJbanTDM/l3DVj6DF5VbY1EOrX2YJAurwX7vKK7G0Daj2Hfnx yZLVGj8u86rdGYhag8Rcp7qOeNfkb/R06bFwwTxOR5WrfjSk4a8+TkgzfG1aApNHGApR wjZMb63zFhrdWwqbMbS7fiCvEv/1+QUsLVxPcMvkPzv7X8QpGLml1n9wl0o9aXr2mF02 xypA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vaBNEjQiN5imowm/0xL8cBoXu3aflqCt38ox8eogPH0=; b=AP0b8kKAZ4MfoGkLcLKlTuFWZgWpBP4U74gPS/LZ9gnwSGQv2v+8497VKKje2MtI3F Q6UrrL3WFq1ROQPG2JAjvDlZ0/JdmovTKqXd3Ujcd+ZrUePiNP6V4sDYNLCuOQ3+hB6M iAvEuojG+B7Ah+Whqr1vP5pUh2k9MWjNObeXTLxKICCX+afgal66TWG8AgydGM39H110 tHLbpwtmKCURarWW9SuDEkYPpx03P9eNnnpmMvJ9MpgeYBpltLUfGN4CYuvoXnxj+uPX 7b/wQ56x6E0o7k3cid1Du9oeOB8pUA/J2KZWm7EvW7xoeZ2G2/T2s7LbL6oujan+r95i DAbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XMSKFGEu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs13-20020a170906dc8d00b0077b4248b138si15114342ejc.127.2022.10.19.15.52.10; Wed, 19 Oct 2022 15:52:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XMSKFGEu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231287AbiJSWr0 (ORCPT <rfc822;samuel.l.nystrom@gmail.com> + 99 others); Wed, 19 Oct 2022 18:47:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230299AbiJSWrE (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 19 Oct 2022 18:47:04 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B463E186D70; Wed, 19 Oct 2022 15:47:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 64496B825E1; Wed, 19 Oct 2022 22:47:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0562DC4347C; Wed, 19 Oct 2022 22:47:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666219621; bh=U+1luf3mbgyTSKOMl8vtX4dFmH8rl+GKNY2TDjN1IyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XMSKFGEu/uzR3N52gimUSRvlUY77ksX6bwSP5MEEbrhoYubbKoJVO455NMN69R4xj x9GRwiRrinWyIWvqcLaI+JhV5ZHw0Ow+vjol3spPlLV+EDFsRspfhySJYB1hNYEyDD aSGLKC8qpWBT4nbxwHNQOo7RmG1Z0Y+5PDZjmaBTcnkG3j3YRGb2Ycr+/iXr0GVAM8 f3R8P+eO4JCLRSgp7mHQtfS9keccHmF/hGloP0UzUPKujvCNZbkgV3dyp0o5SzcWdJ GLdFjAAokYxUviKsgseded/9rmE6dJI2yUYwAvyjELLvaU6fXnXMYEQiD9ItgNieST uSvkQeYinB1kw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id AD5D05C0A04; Wed, 19 Oct 2022 15:47:00 -0700 (PDT) From: "Paul E. McKenney" <paulmck@kernel.org> To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com, rostedt@goodmis.org, "Paul E. McKenney" <paulmck@kernel.org>, Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Joonsoo Kim <iamjoonsoo.kim@lge.com>, Andrew Morton <akpm@linux-foundation.org>, Vlastimil Babka <vbabka@suse.cz>, Roman Gushchin <roman.gushchin@linux.dev>, Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org Subject: [PATCH rcu 5/8] slab: Explain why SLAB_DESTROY_BY_RCU reference before locking Date: Wed, 19 Oct 2022 15:46:56 -0700 Message-Id: <20221019224659.2499511-5-paulmck@kernel.org> X-Mailer: git-send-email 2.31.1.189.g2e36527f23 In-Reply-To: <20221019224652.GA2499358@paulmck-ThinkPad-P17-Gen-1> References: <20221019224652.GA2499358@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747158256615942836?= X-GMAIL-MSGID: =?utf-8?q?1747158256615942836?= |
Series |
Miscellaneous fixes for v6.2
|
|
Commit Message
Paul E. McKenney
Oct. 19, 2022, 10:46 p.m. UTC
It is not obvious to the casual user why it is absolutely necessary to
acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring
a lock in that structure. Therefore, add a comment explaining this point.
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Cc: Christoph Lameter <cl@linux.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: <linux-mm@kvack.org>
---
include/linux/slab.h | 6 ++++++
1 file changed, 6 insertions(+)
Comments
On 10/20/22 00:46, Paul E. McKenney wrote: > It is not obvious to the casual user why it is absolutely necessary to > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring > a lock in that structure. Therefore, add a comment explaining this point. s/SLAB_DESTROY_BY_RCU/SLAB_TYPESAFE_BY_RCU/ in subject, commit log and the added comment? :) > Signed-off-by: Paul E. McKenney <paulmck@kernel.org> > Cc: Christoph Lameter <cl@linux.com> > Cc: Pekka Enberg <penberg@kernel.org> > Cc: David Rientjes <rientjes@google.com> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: Roman Gushchin <roman.gushchin@linux.dev> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > Cc: <linux-mm@kvack.org> > --- > include/linux/slab.h | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 90877fcde70bd..446303e385265 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -76,6 +76,12 @@ > * rcu_read_lock before reading the address, then rcu_read_unlock after > * taking the spinlock within the structure expected at that address. > * > + * Note that it is not possible to acquire a lock within a structure > + * allocated with SLAB_DESTROY_BY_RCU without first acquiring a reference > + * as described above. The reason is that SLAB_DESTROY_BY_RCU pages are > + * not zeroed before being given to the slab, which means that any locks > + * must be initialized after each and every kmem_struct_alloc(). > + * Wonder if slab caches with a constructor should be OK here as AFAIK it should mean the object has to be in the initialized state both when allocated and freed? > * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. > */ > /* Defer freeing slabs to RCU */
On Thu, Oct 20, 2022 at 09:10:49AM +0200, Vlastimil Babka wrote: > On 10/20/22 00:46, Paul E. McKenney wrote: > > It is not obvious to the casual user why it is absolutely necessary to > > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring > > a lock in that structure. Therefore, add a comment explaining this point. > > s/SLAB_DESTROY_BY_RCU/SLAB_TYPESAFE_BY_RCU/ in subject, commit log and the > added comment? :) Boy, I was certainly living in the past when I did this patch, wasn't I? Thank you, will fix on next rebase. > > Signed-off-by: Paul E. McKenney <paulmck@kernel.org> > > Cc: Christoph Lameter <cl@linux.com> > > Cc: Pekka Enberg <penberg@kernel.org> > > Cc: David Rientjes <rientjes@google.com> > > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > > Cc: Andrew Morton <akpm@linux-foundation.org> > > Cc: Vlastimil Babka <vbabka@suse.cz> > > Cc: Roman Gushchin <roman.gushchin@linux.dev> > > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > > Cc: <linux-mm@kvack.org> > > --- > > include/linux/slab.h | 6 ++++++ > > 1 file changed, 6 insertions(+) > > > > diff --git a/include/linux/slab.h b/include/linux/slab.h > > index 90877fcde70bd..446303e385265 100644 > > --- a/include/linux/slab.h > > +++ b/include/linux/slab.h > > @@ -76,6 +76,12 @@ > > * rcu_read_lock before reading the address, then rcu_read_unlock after > > * taking the spinlock within the structure expected at that address. > > * > > + * Note that it is not possible to acquire a lock within a structure > > + * allocated with SLAB_DESTROY_BY_RCU without first acquiring a reference > > + * as described above. The reason is that SLAB_DESTROY_BY_RCU pages are > > + * not zeroed before being given to the slab, which means that any locks > > + * must be initialized after each and every kmem_struct_alloc(). > > + * > > Wonder if slab caches with a constructor should be OK here as AFAIK it > should mean the object has to be in the initialized state both when > allocated and freed? It does look that way, thank you! And __i915_request_ctor(), sighand_ctor(), and anon_vma_ctor() actually do this, initializing a lock in the process. The ctor function could just initialize the locks, and all would be well. In addition, this makes sequence-lock-like approaches a bit easier, as in "just use a sequence lock". I will update with attribution. Thanx, Paul > > * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. > > */ > > /* Defer freeing slabs to RCU */ >
On Wed, 19 Oct 2022, Paul E. McKenney wrote: > It is not obvious to the casual user why it is absolutely necessary to > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring > a lock in that structure. Therefore, add a comment explaining this point. Sorry but this is not correct and difficult to comprehend. 1. You do not need a reference to a slab object after it was allocated. Objects must be properly protected by rcu_locks. 2. Locks are initialized once on slab allocation via a constructor (*not* on object allocation via kmem_cache_alloc) 3. Modifying locks at allocation/free is not possible since references to these objects may still persist after free and before alloc. 4. The old term SLAB_DESTROY_BY_RCU is used here.
On Fri, Oct 21, 2022 at 09:44:23AM +0200, Christoph Lameter wrote: > On Wed, 19 Oct 2022, Paul E. McKenney wrote: > > > It is not obvious to the casual user why it is absolutely necessary to > > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring > > a lock in that structure. Therefore, add a comment explaining this point. > > Sorry but this is not correct and difficult to comprehend. > > 1. You do not need a reference to a slab object after it was allocated. > Objects must be properly protected by rcu_locks. > > 2. Locks are initialized once on slab allocation via a constructor (*not* on object allocation via kmem_cache_alloc) > > 3. Modifying locks at allocation/free is not possible since references to > these objects may still persist after free and before alloc. > > 4. The old term SLAB_DESTROY_BY_RCU is used here. Thank you for looking this over, but Vlastimil beat you to it. How does the update below look? Thanx, Paul ------------------------------------------------------------------------ commit ff4c536e6b44e2e185e38c3653851f92e07139da Author: Paul E. McKenney <paulmck@kernel.org> Date: Mon Sep 26 08:57:56 2022 -0700 slab: Explain why SLAB_TYPESAFE_BY_RCU reference before locking It is not obvious to the casual user why it is absolutely necessary to acquire a reference to a SLAB_TYPESAFE_BY_RCU structure before acquiring a lock in that structure. Therefore, add a comment explaining this point. [ paulmck: Apply Vlastimil Babka feedback. ] Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Cc: Christoph Lameter <cl@linux.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: <linux-mm@kvack.org> diff --git a/include/linux/slab.h b/include/linux/slab.h index 90877fcde70bd..487418c7ea8cd 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -76,6 +76,17 @@ * rcu_read_lock before reading the address, then rcu_read_unlock after * taking the spinlock within the structure expected at that address. * + * Note that it is not possible to acquire a lock within a structure + * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference + * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages + * are not zeroed before being given to the slab, which means that any + * locks must be initialized after each and every kmem_struct_alloc(). + * Alternatively, make the ctor passed to kmem_cache_create() initialize + * the locks at page-allocation time, as is done in __i915_request_ctor(), + * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers + * to safely acquire those ctor-initialized locks under rcu_read_lock() + * protection. + * * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. */ /* Defer freeing slabs to RCU */
On 10/21/22 15:43, Paul E. McKenney wrote: > On Fri, Oct 21, 2022 at 09:44:23AM +0200, Christoph Lameter wrote: >> On Wed, 19 Oct 2022, Paul E. McKenney wrote: >> >> > It is not obvious to the casual user why it is absolutely necessary to >> > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring >> > a lock in that structure. Therefore, add a comment explaining this point. >> >> Sorry but this is not correct and difficult to comprehend. >> >> 1. You do not need a reference to a slab object after it was allocated. >> Objects must be properly protected by rcu_locks. >> >> 2. Locks are initialized once on slab allocation via a constructor (*not* on object allocation via kmem_cache_alloc) >> >> 3. Modifying locks at allocation/free is not possible since references to >> these objects may still persist after free and before alloc. >> >> 4. The old term SLAB_DESTROY_BY_RCU is used here. > > Thank you for looking this over, but Vlastimil beat you to it. How does > the update below look? LGTM. > Thanx, Paul > > ------------------------------------------------------------------------ > > commit ff4c536e6b44e2e185e38c3653851f92e07139da > Author: Paul E. McKenney <paulmck@kernel.org> > Date: Mon Sep 26 08:57:56 2022 -0700 > > slab: Explain why SLAB_TYPESAFE_BY_RCU reference before locking > > It is not obvious to the casual user why it is absolutely necessary to > acquire a reference to a SLAB_TYPESAFE_BY_RCU structure before acquiring > a lock in that structure. Therefore, add a comment explaining this point. > > [ paulmck: Apply Vlastimil Babka feedback. ] > > Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Acked-by: Vlastimil Babka <vbabka@suse.cz> > Cc: Christoph Lameter <cl@linux.com> > Cc: Pekka Enberg <penberg@kernel.org> > Cc: David Rientjes <rientjes@google.com> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: Roman Gushchin <roman.gushchin@linux.dev> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > Cc: <linux-mm@kvack.org> > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 90877fcde70bd..487418c7ea8cd 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -76,6 +76,17 @@ > * rcu_read_lock before reading the address, then rcu_read_unlock after > * taking the spinlock within the structure expected at that address. > * > + * Note that it is not possible to acquire a lock within a structure > + * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference > + * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages > + * are not zeroed before being given to the slab, which means that any > + * locks must be initialized after each and every kmem_struct_alloc(). > + * Alternatively, make the ctor passed to kmem_cache_create() initialize > + * the locks at page-allocation time, as is done in __i915_request_ctor(), > + * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers > + * to safely acquire those ctor-initialized locks under rcu_read_lock() > + * protection. > + * > * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. > */ > /* Defer freeing slabs to RCU */
On Fri, Oct 21, 2022 at 03:50:17PM +0200, Vlastimil Babka wrote: > On 10/21/22 15:43, Paul E. McKenney wrote: > > On Fri, Oct 21, 2022 at 09:44:23AM +0200, Christoph Lameter wrote: > >> On Wed, 19 Oct 2022, Paul E. McKenney wrote: > >> > >> > It is not obvious to the casual user why it is absolutely necessary to > >> > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring > >> > a lock in that structure. Therefore, add a comment explaining this point. > >> > >> Sorry but this is not correct and difficult to comprehend. > >> > >> 1. You do not need a reference to a slab object after it was allocated. > >> Objects must be properly protected by rcu_locks. > >> > >> 2. Locks are initialized once on slab allocation via a constructor (*not* on object allocation via kmem_cache_alloc) > >> > >> 3. Modifying locks at allocation/free is not possible since references to > >> these objects may still persist after free and before alloc. > >> > >> 4. The old term SLAB_DESTROY_BY_RCU is used here. > > > > Thank you for looking this over, but Vlastimil beat you to it. How does > > the update below look? > > LGTM. May I please have your ack? Thanx, Paul > > ------------------------------------------------------------------------ > > > > commit ff4c536e6b44e2e185e38c3653851f92e07139da > > Author: Paul E. McKenney <paulmck@kernel.org> > > Date: Mon Sep 26 08:57:56 2022 -0700 > > > > slab: Explain why SLAB_TYPESAFE_BY_RCU reference before locking > > > > It is not obvious to the casual user why it is absolutely necessary to > > acquire a reference to a SLAB_TYPESAFE_BY_RCU structure before acquiring > > a lock in that structure. Therefore, add a comment explaining this point. > > > > [ paulmck: Apply Vlastimil Babka feedback. ] > > > > Signed-off-by: Paul E. McKenney <paulmck@kernel.org> > > Acked-by: Vlastimil Babka <vbabka@suse.cz> > > > Cc: Christoph Lameter <cl@linux.com> > > Cc: Pekka Enberg <penberg@kernel.org> > > Cc: David Rientjes <rientjes@google.com> > > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > > Cc: Andrew Morton <akpm@linux-foundation.org> > > Cc: Vlastimil Babka <vbabka@suse.cz> > > Cc: Roman Gushchin <roman.gushchin@linux.dev> > > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > > Cc: <linux-mm@kvack.org> > > > > diff --git a/include/linux/slab.h b/include/linux/slab.h > > index 90877fcde70bd..487418c7ea8cd 100644 > > --- a/include/linux/slab.h > > +++ b/include/linux/slab.h > > @@ -76,6 +76,17 @@ > > * rcu_read_lock before reading the address, then rcu_read_unlock after > > * taking the spinlock within the structure expected at that address. > > * > > + * Note that it is not possible to acquire a lock within a structure > > + * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference > > + * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages > > + * are not zeroed before being given to the slab, which means that any > > + * locks must be initialized after each and every kmem_struct_alloc(). > > + * Alternatively, make the ctor passed to kmem_cache_create() initialize > > + * the locks at page-allocation time, as is done in __i915_request_ctor(), > > + * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers > > + * to safely acquire those ctor-initialized locks under rcu_read_lock() > > + * protection. > > + * > > * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. > > */ > > /* Defer freeing slabs to RCU */ >
On 10/21/22 17:42, Paul E. McKenney wrote: > On Fri, Oct 21, 2022 at 03:50:17PM +0200, Vlastimil Babka wrote: >> On 10/21/22 15:43, Paul E. McKenney wrote: >> > On Fri, Oct 21, 2022 at 09:44:23AM +0200, Christoph Lameter wrote: >> >> On Wed, 19 Oct 2022, Paul E. McKenney wrote: >> >> >> >> > It is not obvious to the casual user why it is absolutely necessary to >> >> > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring >> >> > a lock in that structure. Therefore, add a comment explaining this point. >> >> >> >> Sorry but this is not correct and difficult to comprehend. >> >> >> >> 1. You do not need a reference to a slab object after it was allocated. >> >> Objects must be properly protected by rcu_locks. >> >> >> >> 2. Locks are initialized once on slab allocation via a constructor (*not* on object allocation via kmem_cache_alloc) >> >> >> >> 3. Modifying locks at allocation/free is not possible since references to >> >> these objects may still persist after free and before alloc. >> >> >> >> 4. The old term SLAB_DESTROY_BY_RCU is used here. >> > >> > Thank you for looking this over, but Vlastimil beat you to it. How does >> > the update below look? >> >> LGTM. > > May I please have your ack? > > Thanx, Paul > >> > ------------------------------------------------------------------------ >> > >> > commit ff4c536e6b44e2e185e38c3653851f92e07139da >> > Author: Paul E. McKenney <paulmck@kernel.org> >> > Date: Mon Sep 26 08:57:56 2022 -0700 >> > >> > slab: Explain why SLAB_TYPESAFE_BY_RCU reference before locking >> > >> > It is not obvious to the casual user why it is absolutely necessary to >> > acquire a reference to a SLAB_TYPESAFE_BY_RCU structure before acquiring >> > a lock in that structure. Therefore, add a comment explaining this point. >> > >> > [ paulmck: Apply Vlastimil Babka feedback. ] >> > >> > Signed-off-by: Paul E. McKenney <paulmck@kernel.org> >> >> Acked-by: Vlastimil Babka <vbabka@suse.cz> It was there :) >> > Cc: Christoph Lameter <cl@linux.com> >> > Cc: Pekka Enberg <penberg@kernel.org> >> > Cc: David Rientjes <rientjes@google.com> >> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> >> > Cc: Andrew Morton <akpm@linux-foundation.org> >> > Cc: Vlastimil Babka <vbabka@suse.cz> >> > Cc: Roman Gushchin <roman.gushchin@linux.dev> >> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> >> > Cc: <linux-mm@kvack.org> >> > >> > diff --git a/include/linux/slab.h b/include/linux/slab.h >> > index 90877fcde70bd..487418c7ea8cd 100644 >> > --- a/include/linux/slab.h >> > +++ b/include/linux/slab.h >> > @@ -76,6 +76,17 @@ >> > * rcu_read_lock before reading the address, then rcu_read_unlock after >> > * taking the spinlock within the structure expected at that address. >> > * >> > + * Note that it is not possible to acquire a lock within a structure >> > + * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference >> > + * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages >> > + * are not zeroed before being given to the slab, which means that any >> > + * locks must be initialized after each and every kmem_struct_alloc(). >> > + * Alternatively, make the ctor passed to kmem_cache_create() initialize >> > + * the locks at page-allocation time, as is done in __i915_request_ctor(), >> > + * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers >> > + * to safely acquire those ctor-initialized locks under rcu_read_lock() >> > + * protection. >> > + * >> > * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. >> > */ >> > /* Defer freeing slabs to RCU */ >>
On Fri, Oct 21, 2022 at 05:50:39PM +0200, Vlastimil Babka wrote: > On 10/21/22 17:42, Paul E. McKenney wrote: > > On Fri, Oct 21, 2022 at 03:50:17PM +0200, Vlastimil Babka wrote: > >> On 10/21/22 15:43, Paul E. McKenney wrote: > >> > On Fri, Oct 21, 2022 at 09:44:23AM +0200, Christoph Lameter wrote: > >> >> On Wed, 19 Oct 2022, Paul E. McKenney wrote: > >> >> > >> >> > It is not obvious to the casual user why it is absolutely necessary to > >> >> > acquire a reference to a SLAB_DESTROY_BY_RCU structure before acquiring > >> >> > a lock in that structure. Therefore, add a comment explaining this point. > >> >> > >> >> Sorry but this is not correct and difficult to comprehend. > >> >> > >> >> 1. You do not need a reference to a slab object after it was allocated. > >> >> Objects must be properly protected by rcu_locks. > >> >> > >> >> 2. Locks are initialized once on slab allocation via a constructor (*not* on object allocation via kmem_cache_alloc) > >> >> > >> >> 3. Modifying locks at allocation/free is not possible since references to > >> >> these objects may still persist after free and before alloc. > >> >> > >> >> 4. The old term SLAB_DESTROY_BY_RCU is used here. > >> > > >> > Thank you for looking this over, but Vlastimil beat you to it. How does > >> > the update below look? > >> > >> LGTM. > > > > May I please have your ack? > > > > Thanx, Paul > > > >> > ------------------------------------------------------------------------ > >> > > >> > commit ff4c536e6b44e2e185e38c3653851f92e07139da > >> > Author: Paul E. McKenney <paulmck@kernel.org> > >> > Date: Mon Sep 26 08:57:56 2022 -0700 > >> > > >> > slab: Explain why SLAB_TYPESAFE_BY_RCU reference before locking > >> > > >> > It is not obvious to the casual user why it is absolutely necessary to > >> > acquire a reference to a SLAB_TYPESAFE_BY_RCU structure before acquiring > >> > a lock in that structure. Therefore, add a comment explaining this point. > >> > > >> > [ paulmck: Apply Vlastimil Babka feedback. ] > >> > > >> > Signed-off-by: Paul E. McKenney <paulmck@kernel.org> > >> > >> Acked-by: Vlastimil Babka <vbabka@suse.cz> > > It was there :) One of those mornings, I guess... Thank you very much!!! Thanx, Paul > >> > Cc: Christoph Lameter <cl@linux.com> > >> > Cc: Pekka Enberg <penberg@kernel.org> > >> > Cc: David Rientjes <rientjes@google.com> > >> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > >> > Cc: Andrew Morton <akpm@linux-foundation.org> > >> > Cc: Vlastimil Babka <vbabka@suse.cz> > >> > Cc: Roman Gushchin <roman.gushchin@linux.dev> > >> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > >> > Cc: <linux-mm@kvack.org> > >> > > >> > diff --git a/include/linux/slab.h b/include/linux/slab.h > >> > index 90877fcde70bd..487418c7ea8cd 100644 > >> > --- a/include/linux/slab.h > >> > +++ b/include/linux/slab.h > >> > @@ -76,6 +76,17 @@ > >> > * rcu_read_lock before reading the address, then rcu_read_unlock after > >> > * taking the spinlock within the structure expected at that address. > >> > * > >> > + * Note that it is not possible to acquire a lock within a structure > >> > + * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference > >> > + * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages > >> > + * are not zeroed before being given to the slab, which means that any > >> > + * locks must be initialized after each and every kmem_struct_alloc(). > >> > + * Alternatively, make the ctor passed to kmem_cache_create() initialize > >> > + * the locks at page-allocation time, as is done in __i915_request_ctor(), > >> > + * sighand_ctor(), and anon_vma_ctor(). Such a ctor permits readers > >> > + * to safely acquire those ctor-initialized locks under rcu_read_lock() > >> > + * protection. > >> > + * > >> > * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. > >> > */ > >> > /* Defer freeing slabs to RCU */ > >> >
diff --git a/include/linux/slab.h b/include/linux/slab.h index 90877fcde70bd..446303e385265 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -76,6 +76,12 @@ * rcu_read_lock before reading the address, then rcu_read_unlock after * taking the spinlock within the structure expected at that address. * + * Note that it is not possible to acquire a lock within a structure + * allocated with SLAB_DESTROY_BY_RCU without first acquiring a reference + * as described above. The reason is that SLAB_DESTROY_BY_RCU pages are + * not zeroed before being given to the slab, which means that any locks + * must be initialized after each and every kmem_struct_alloc(). + * * Note that SLAB_TYPESAFE_BY_RCU was originally named SLAB_DESTROY_BY_RCU. */ /* Defer freeing slabs to RCU */