Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7613084rwr; Wed, 10 May 2023 10:14:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6IkMSigmtwPxyqyU6a/dTRsDR00MIzOCCzSZ0JWvO+o9FZYbIS318VcJaV6trVJIr6oSfM X-Received: by 2002:a05:6a20:442a:b0:ff:68f1:679 with SMTP id ce42-20020a056a20442a00b000ff68f10679mr21553179pzb.52.1683738860705; Wed, 10 May 2023 10:14:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683738860; cv=none; d=google.com; s=arc-20160816; b=pt5JJtMqmYzp6dZn9Xumt7b7+OUFWbd9HtSPsjEHeI2YzCng66QMTSFbVQRkFqsFs9 0R3gy09V04A9fLWcaQm/1ToOOxWPqe2mFOtuEDEK8uJ0icy2603iO0ZjYDHyKYj+kuDb mbFofw85EB02K3YSWmC4MhaXmJ5K7XE1hAhKJgzHz0hAyiEP8VqDrr0WUuA+9hfiezNw Z24Ls5aw6c5MV2f9qwT7Sks6U1LUAENCslZP+qVbvDjY5jeicJ1xD8w0ePGcZyNm8mdV lGCIev1qaxyb/f9M/PiJmPJ90KMwTG7aiIELmoUf51yqUA9r+xVPq1ueLk+rptLBFL1w WKDQ== 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=8iIXnkvQV2CvyUF8BGDfWUK+kYxh6uHj3hYB1wuvP/E=; b=xdIV7Wi8ZU9gRZmTTx+tgWg5dzMUMtRJ/zNBGMEB5fJ4OWE7K1gf2mBUuEbZ9+ORjy zYOenIBlrnwvEaKVew8FR+CqeT2nPRXfVX1j6wzqttyNhCFvtmYjzN7iB9Dol6vMyOLj vUbxO6nK8mm3R2Q9fsJ5TSfarderarbVKPUXJM1NWcA5eEakI/bnsyUOMFr/6sIwJoKF j3tTD53fn2slMEMffs79DmXMfyUUv1XUl10g6hEG6G1I8wA5iQH8qr409EEPJY+ydEa7 3IgDZWLMUrHKn49po0INnTNycawfPxW+ePP9CSyfKV9cfkMkewlHb1Tr5bBHqB2e9z4q 2N+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Anbm9VOA; 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 l197-20020a633ece000000b0051b85b5a3d1si4619769pga.127.2023.05.10.10.14.05; Wed, 10 May 2023 10:14:20 -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=Anbm9VOA; 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 S236449AbjEJRG1 (ORCPT + 99 others); Wed, 10 May 2023 13:06:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236367AbjEJRGX (ORCPT ); Wed, 10 May 2023 13:06:23 -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 8ABD6358C; Wed, 10 May 2023 10:06: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 1FE75636DF; Wed, 10 May 2023 17:06:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F747C433EF; Wed, 10 May 2023 17:06:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683738370; bh=kQwhUcHnUV1oB8K+SYCdQwdWT6mtPz7OHiOwXCmWFXs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Anbm9VOAYtEtoX7qGLRyHb8ln3njJ14N8ou716QX9jLtd0EpbHyNlc2Ij8OpNmEPC JfpGlBBiSXXmTdoJMZpLaSTmod4JNdZFsA1Ok2UZhRgVbN2DRHUNBj9fNr0kb3KYg9 i3Csz5mZgQ5uogU9/JBg3+gQp/LAULludnnBBF6OSjDCl8M92XV9w//2ZWovyGFt3o STHmsrW6Cq2+bTeTAQE95svezqRBeyWRELebPS1TNyPVnLK0MIkhw/nM5Jc0nrszke 9Vcc7J+uTIlgoKD7UFBqjIq3UCbr9XwsW4kqeqd6NFM9hjW6Qpa4tOLLlpNCgnFjlz PkGcd1uMMKl5A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 2BB0ECE126E; Wed, 10 May 2023 10:06:10 -0700 (PDT) From: "Paul E. McKenney" To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com, rostedt@goodmis.org, Frederic Weisbecker , "Paul E . McKenney" Subject: [PATCH rcu 1/4] rcu/nocb: Protect lazy shrinker against concurrent (de-)offloading Date: Wed, 10 May 2023 10:06:04 -0700 Message-Id: <20230510170607.2187978-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <9c5fae22-e25e-4548-b6e9-117495b29e62@paulmck-laptop> References: <9c5fae22-e25e-4548-b6e9-117495b29e62@paulmck-laptop> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: X-Mailing-List: linux-kernel@vger.kernel.org From: Frederic Weisbecker The shrinker may run concurrently with callbacks (de-)offloading. As such, calling rcu_nocb_lock() is very dangerous because it does a conditional locking. The worst outcome is that rcu_nocb_lock() doesn't lock but rcu_nocb_unlock() eventually unlocks, or the reverse, creating an imbalance. Fix this with protecting against (de-)offloading using the barrier mutex. Although if the barrier mutex is contended, which should be rare, then step aside so as not to trigger a mutex VS allocation dependency chain. Signed-off-by: Frederic Weisbecker Signed-off-by: Paul E. McKenney --- kernel/rcu/tree_nocb.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/kernel/rcu/tree_nocb.h b/kernel/rcu/tree_nocb.h index f2280616f9d5..1a86883902ce 100644 --- a/kernel/rcu/tree_nocb.h +++ b/kernel/rcu/tree_nocb.h @@ -1336,13 +1336,33 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) unsigned long flags; unsigned long count = 0; + /* + * Protect against concurrent (de-)offloading. Otherwise nocb locking + * may be ignored or imbalanced. + */ + if (!mutex_trylock(&rcu_state.barrier_mutex)) { + /* + * But really don't insist if barrier_mutex is contended since we + * can't guarantee that it will never engage in a dependency + * chain involving memory allocation. The lock is seldom contended + * anyway. + */ + return 0; + } + /* Snapshot count of all CPUs */ for_each_possible_cpu(cpu) { struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); - int _count = READ_ONCE(rdp->lazy_len); + int _count; + + if (!rcu_rdp_is_offloaded(rdp)) + continue; + + _count = READ_ONCE(rdp->lazy_len); if (_count == 0) continue; + rcu_nocb_lock_irqsave(rdp, flags); WRITE_ONCE(rdp->lazy_len, 0); rcu_nocb_unlock_irqrestore(rdp, flags); @@ -1352,6 +1372,9 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) if (sc->nr_to_scan <= 0) break; } + + mutex_unlock(&rcu_state.barrier_mutex); + return count ? count : SHRINK_STOP; } -- 2.40.1