Message ID | 20230329160203.191380-4-frederic@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp541318vqo; Wed, 29 Mar 2023 09:33:45 -0700 (PDT) X-Google-Smtp-Source: AK7set/I45VtYgjiwFO3n9AD61IsirWjHVGbci1TbWefqWRHC4Q/1aIE5iDW3E6I3YwNAGthHSXy X-Received: by 2002:a05:6a20:b55c:b0:da:a7db:48bb with SMTP id ev28-20020a056a20b55c00b000daa7db48bbmr16010896pzb.42.1680107625560; Wed, 29 Mar 2023 09:33:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680107625; cv=none; d=google.com; s=arc-20160816; b=hlN86BAUaBKpdUv1iOH9CAvAmaCGqZ1ZvaV75vaEd5UYXjDnIn8v7LHEuN5LakVh1a ITbEy2ciePIKiGG36bXi1xfM2phOJvjMyx0S2mWTmN1ctP6/+8VEy0t4XFOV+3VJc8O5 qgIojZpkTNxOUHRABC3CkwIyEI6swgj3L9tP5Wm4rfrN+Tg+pKZUCUwkABCCZn4pIgzP 6Tf7SNoqJf8QrvTpE+MWKNyFXygsUanHWisZml780+gvKqhJzSnuzi2VsLlGho+aezRN Z3JqAzcfVKnQ9BIXFZMauP1eYF8NX5rLCYJQ7lE1UN2EEvZtlsLu8ZoXaGwq0/h7dUrD 5gXQ== 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=pxRhWFyb27BvogwhMK7gi/x5UwosXX92hs5VIGhZfD0=; b=FK+7Ddt/8+lPBCxqF/JcD8YOz/tpf2Ft8cLiDsHfJUcHipMHK3by/QLcW65yg14wlb iXaHM26n9SIHxmw+LaMtX1b6Vu8Z/n7X1xn0KC2AP8kheJTt4xCJEvBdbS/i8q5VESZP FCoFAftEZtM/euoL9kzhX7zsikphNmrFQ3WZZ0CeqaRI9FlMgs6LQCDS1j/g8pMARxmg C0KqP8dt2ygvjAsljdkCXTjAv0kwAIPjlGKYO6SEKXAlYgYifgDmDm8vubv1/BSBhXVM RjcrW5+1NnO8TyP5L4gGWiAZOH8xPusn3yvo4CiCSiBOEsFY+qmXYlSNUVQV+wPsg4x9 J7zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=We6Sppzs; 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 d17-20020a056a00245100b00628030da823si23505332pfj.318.2023.03.29.09.33.32; Wed, 29 Mar 2023 09:33:45 -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=We6Sppzs; 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 S231215AbjC2QEz (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Wed, 29 Mar 2023 12:04:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231582AbjC2QEU (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 29 Mar 2023 12:04:20 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF252558E; Wed, 29 Mar 2023 09:03:10 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 3F88461DAA; Wed, 29 Mar 2023 16:02:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DFC75C4339C; Wed, 29 Mar 2023 16:02:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680105735; bh=+h4XK6+UI6FKKHszmm1wj9jmGuHr+bG0mxcM6PK0eo8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=We6SppzsOzlK2//1daxu59Df7ANxDJyMznwP+OkNrFmQmFcLdYSH8atr1ezJd2MQj WGhWJt5bkH3VrhROVfbJh02EKGkS4HvFe7yGH7qz1L3O1hPSNGdLY0xegZuyYPbhO4 HFJ39S8in9KjgKsomrNfCm6kkGsr3ndqR6XGOtnZzBDu5NF8SysXHICJOoLTeTUZlf t8uQZIL7RyPCnaGpbfjU1TLNYO2Hj0+jgz4c93W2sJu3rtVZTMLioZzU+iPZjGmRfC D6tzE56MJESRUNbCdPEERhTIsa0krSKTMJRV4SDPnbDJ+cY+nwa+73GhrVeGcZadG2 6mVRYA++CYdug== From: Frederic Weisbecker <frederic@kernel.org> To: "Paul E . McKenney" <paulmck@kernel.org> Cc: LKML <linux-kernel@vger.kernel.org>, Frederic Weisbecker <frederic@kernel.org>, rcu <rcu@vger.kernel.org>, Uladzislau Rezki <urezki@gmail.com>, Neeraj Upadhyay <quic_neeraju@quicinc.com>, Boqun Feng <boqun.feng@gmail.com>, Joel Fernandes <joel@joelfernandes.org> Subject: [PATCH 3/4] rcu/nocb: Recheck lazy callbacks under the ->nocb_lock from shrinker Date: Wed, 29 Mar 2023 18:02:02 +0200 Message-Id: <20230329160203.191380-4-frederic@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230329160203.191380-1-frederic@kernel.org> References: <20230329160203.191380-1-frederic@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=unavailable 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?1761720533679437799?= X-GMAIL-MSGID: =?utf-8?q?1761720533679437799?= |
Series |
rcu/nocb: Shrinker related boring fixes
|
|
Commit Message
Frederic Weisbecker
March 29, 2023, 4:02 p.m. UTC
The ->lazy_len is only checked locklessly. Recheck again under the
->nocb_lock to avoid spending more time on flushing/waking if not
necessary. The ->lazy_len can still increment concurrently (from 1 to
infinity) but under the ->nocb_lock we at least know for sure if there
are lazy callbacks at all (->lazy_len > 0).
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
---
kernel/rcu/tree_nocb.h | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
Comments
On Wed, Mar 29, 2023 at 06:02:02PM +0200, Frederic Weisbecker wrote: > The ->lazy_len is only checked locklessly. Recheck again under the > ->nocb_lock to avoid spending more time on flushing/waking if not > necessary. The ->lazy_len can still increment concurrently (from 1 to > infinity) but under the ->nocb_lock we at least know for sure if there > are lazy callbacks at all (->lazy_len > 0). > > Signed-off-by: Frederic Weisbecker <frederic@kernel.org> > --- > kernel/rcu/tree_nocb.h | 16 ++++++++++++---- > 1 file changed, 12 insertions(+), 4 deletions(-) > > diff --git a/kernel/rcu/tree_nocb.h b/kernel/rcu/tree_nocb.h > index c321fce2af8e..dfa9c10d6727 100644 > --- a/kernel/rcu/tree_nocb.h > +++ b/kernel/rcu/tree_nocb.h > @@ -1358,12 +1358,20 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) > if (!rcu_rdp_is_offloaded(rdp)) > continue; > > + if (!READ_ONCE(rdp->lazy_len)) > + continue; Do you depend on the ordering of the above read of ->lazy_len against anything in the following, aside from the re-read of ->lazy_len? (Same variable, both READ_ONCE() or stronger, so you do get that ordering.) If you do need that ordering, the above READ_ONCE() needs to instead be smp_load_acquire() or similar. If you don't need that ordering, what you have is good. > + rcu_nocb_lock_irqsave(rdp, flags); > + /* > + * Recheck under the nocb lock. Since we are not holding the bypass > + * lock we may still race with increments from the enqueuer but still > + * we know for sure if there is at least one lazy callback. > + */ > _count = READ_ONCE(rdp->lazy_len); > - > - if (_count == 0) > + if (!_count) { > + rcu_nocb_unlock_irqrestore(rdp, flags); > continue; > - > - rcu_nocb_lock_irqsave(rdp, flags); > + } > WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies, false)); > rcu_nocb_unlock_irqrestore(rdp, flags); > wake_nocb_gp(rdp, false); > -- > 2.34.1 >
On Wed, Mar 29, 2023 at 01:54:20PM -0700, Paul E. McKenney wrote: > On Wed, Mar 29, 2023 at 06:02:02PM +0200, Frederic Weisbecker wrote: > > The ->lazy_len is only checked locklessly. Recheck again under the > > ->nocb_lock to avoid spending more time on flushing/waking if not > > necessary. The ->lazy_len can still increment concurrently (from 1 to > > infinity) but under the ->nocb_lock we at least know for sure if there > > are lazy callbacks at all (->lazy_len > 0). > > > > Signed-off-by: Frederic Weisbecker <frederic@kernel.org> > > --- > > kernel/rcu/tree_nocb.h | 16 ++++++++++++---- > > 1 file changed, 12 insertions(+), 4 deletions(-) > > > > diff --git a/kernel/rcu/tree_nocb.h b/kernel/rcu/tree_nocb.h > > index c321fce2af8e..dfa9c10d6727 100644 > > --- a/kernel/rcu/tree_nocb.h > > +++ b/kernel/rcu/tree_nocb.h > > @@ -1358,12 +1358,20 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) > > if (!rcu_rdp_is_offloaded(rdp)) > > continue; > > > > + if (!READ_ONCE(rdp->lazy_len)) > > + continue; > > Do you depend on the ordering of the above read of ->lazy_len against > anything in the following, aside from the re-read of ->lazy_len? (Same > variable, both READ_ONCE() or stronger, so you do get that ordering.) > > If you do need that ordering, the above READ_ONCE() needs to instead > be smp_load_acquire() or similar. If you don't need that ordering, > what you have is good. No ordering dependency intended here. The early ->lazy_len read is really just an optimization here to avoid locking if it *seems* there is nothing to do with this rdp. But what follows doesn't depend on that read. Thanks.
On Wed, Mar 29, 2023 at 11:22:45PM +0200, Frederic Weisbecker wrote: > On Wed, Mar 29, 2023 at 01:54:20PM -0700, Paul E. McKenney wrote: > > On Wed, Mar 29, 2023 at 06:02:02PM +0200, Frederic Weisbecker wrote: > > > The ->lazy_len is only checked locklessly. Recheck again under the > > > ->nocb_lock to avoid spending more time on flushing/waking if not > > > necessary. The ->lazy_len can still increment concurrently (from 1 to > > > infinity) but under the ->nocb_lock we at least know for sure if there > > > are lazy callbacks at all (->lazy_len > 0). > > > > > > Signed-off-by: Frederic Weisbecker <frederic@kernel.org> > > > --- > > > kernel/rcu/tree_nocb.h | 16 ++++++++++++---- > > > 1 file changed, 12 insertions(+), 4 deletions(-) > > > > > > diff --git a/kernel/rcu/tree_nocb.h b/kernel/rcu/tree_nocb.h > > > index c321fce2af8e..dfa9c10d6727 100644 > > > --- a/kernel/rcu/tree_nocb.h > > > +++ b/kernel/rcu/tree_nocb.h > > > @@ -1358,12 +1358,20 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) > > > if (!rcu_rdp_is_offloaded(rdp)) > > > continue; > > > > > > + if (!READ_ONCE(rdp->lazy_len)) > > > + continue; > > > > Do you depend on the ordering of the above read of ->lazy_len against > > anything in the following, aside from the re-read of ->lazy_len? (Same > > variable, both READ_ONCE() or stronger, so you do get that ordering.) > > > > If you do need that ordering, the above READ_ONCE() needs to instead > > be smp_load_acquire() or similar. If you don't need that ordering, > > what you have is good. > > No ordering dependency intended here. The early ->lazy_len read is really just > an optimization here to avoid locking if it *seems* there is nothing to do with > this rdp. But what follows doesn't depend on that read. Full steam ahead with READ_ONCE(), then! ;-) Thanx, Paul
diff --git a/kernel/rcu/tree_nocb.h b/kernel/rcu/tree_nocb.h index c321fce2af8e..dfa9c10d6727 100644 --- a/kernel/rcu/tree_nocb.h +++ b/kernel/rcu/tree_nocb.h @@ -1358,12 +1358,20 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) if (!rcu_rdp_is_offloaded(rdp)) continue; + if (!READ_ONCE(rdp->lazy_len)) + continue; + + rcu_nocb_lock_irqsave(rdp, flags); + /* + * Recheck under the nocb lock. Since we are not holding the bypass + * lock we may still race with increments from the enqueuer but still + * we know for sure if there is at least one lazy callback. + */ _count = READ_ONCE(rdp->lazy_len); - - if (_count == 0) + if (!_count) { + rcu_nocb_unlock_irqrestore(rdp, flags); continue; - - rcu_nocb_lock_irqsave(rdp, flags); + } WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies, false)); rcu_nocb_unlock_irqrestore(rdp, flags); wake_nocb_gp(rdp, false);