Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6597966rwl; Wed, 22 Mar 2023 12:46:09 -0700 (PDT) X-Google-Smtp-Source: AK7set9nGgSVhbugfjoRkxPlmn8UqTEuTHdncsUkfS+Vi2GVdcA/3ZsN8dKAMBaJl8kVBLfCOS6i X-Received: by 2002:a17:903:22d0:b0:19d:5fd:11fb with SMTP id y16-20020a17090322d000b0019d05fd11fbmr4148322plg.23.1679514368745; Wed, 22 Mar 2023 12:46:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679514368; cv=none; d=google.com; s=arc-20160816; b=mtE0wqM6/wQw8NwnQ6NxZqKnIq6gbou3CCqx6+T3odDsyHnI/MkAj1LdCmR1h/hvWw Ng3689zhKh7311f8EEzXZAytjZEOYQommwiSSW4Qdp25o0vVmHu5CYK348E1mkhnfnCt Rv5JG/eCCqG33udq8eQxO8TuKlXzEVDi1uS/hOZafntmKy4N6YfdRYEOgb6kLuFKje4C Ygdf/2d3IYk3R+oBEzfofMhIOEBKMheqieKf9OmArna2A5/wxq48afU98O6NaHnTSv7I 4PBvZ2TcX3fJd+76d9k8JV9WVcRKnVEafQzsMNDbtMKyfE34o+MoIBDqKOuNZ3PUKRtj mZbg== 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=QWh0w4a34SfD/fykTzaCD4NrRTzf1m09qyJhbCAzRJc=; b=pfIlfD8wOZhdXUr5z3+AS7kQWRTKVH9J71rf3xdMgL+aBzQE9JTfh3dhh1hfC+Q/Pv lJVI1YXL8kG+vermIGNpYt7hTcBFN+4nbTTdKFs3MqTEwy3G0YV3phapFULunASU14ot H6TraPyXz9pAkhpdj62qYiUqU3PyktQdqY+sCYhzPWeaXiHq6J+nO6j4xfrbYygmXc/+ dwQ9g8sibEYDOhCC2LXmB8PdTUxPyIsQ1hqKYO2Vqt+856vdJ0WEPf/eolI5Q1UZ620R oNAHjELfgOvvZG/L8vNOZJk31wfU+PsGR36tOFjHPU/l6A8Jju5iQKPcMgA0DRi9YeEk uY8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Z4SeTifB; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o20-20020a634e54000000b0050ac7d1b330si16092879pgl.289.2023.03.22.12.45.56; Wed, 22 Mar 2023 12:46:08 -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=Z4SeTifB; 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 S230074AbjCVTpZ (ORCPT + 99 others); Wed, 22 Mar 2023 15:45:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbjCVTpS (ORCPT ); Wed, 22 Mar 2023 15:45:18 -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 DE7ED32CEB; Wed, 22 Mar 2023 12:45:11 -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 7BD9A622B1; Wed, 22 Mar 2023 19:45:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B7CDC4339B; Wed, 22 Mar 2023 19:45:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679514310; bh=mP8fCieTmDyHtxHWglNueIzXLMyc5wglCPH3QTQMCKk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z4SeTifBUVRdEuhbIjeDAFkhZN28kaeoNGLv044rupmYaop8EMvGXNg2ZnWWILzay 0/ndpYG1LRNCIps3ZYm+OjblNQ2Jx0Z3xyIAkpKvkR1m1tRxv8P7j1HqZY7LEGGRy1 X0ePCHGOVygrJy6zx3cr2agJePWejOCbV1uAqk+a8eH1knyQ1aUreHgnlv+jkpKuci KXqPE/9qDyKZR1ZiG2tgroIW0BCGzx+YdpgkmPLqfpoduP4LSHPPzwj0YANEhzCJeA z1vOPQ5UwH7MIwmZQr92YILDBs0gB2vgKDgl5LGO7A2ob8JooFjAcnOmAIp+xfTlVy KBFcXUO9lpmjA== From: Frederic Weisbecker To: "Paul E . McKenney" Cc: LKML , Frederic Weisbecker , rcu , Uladzislau Rezki , Neeraj Upadhyay , Boqun Feng , Joel Fernandes Subject: [PATCH 3/4] rcu/nocb: Recheck lazy callbacks under the ->nocb_lock from shrinker Date: Wed, 22 Mar 2023 20:44:55 +0100 Message-Id: <20230322194456.2331527-4-frederic@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230322194456.2331527-1-frederic@kernel.org> References: <20230322194456.2331527-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: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 cb57e8312231..a3dc7465b0b2 100644 --- a/kernel/rcu/tree_nocb.h +++ b/kernel/rcu/tree_nocb.h @@ -1350,12 +1350,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); -- 2.34.1