From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796526334; b=y67qK4g+3abU/KOyWsf77O1THQctHlzfY5HOFt6OJiMntYQJQS7m/WpYb0c2o9XBbgyW L5ACBxGUpNKvA5rcTTqMPOY19kbrJ2p5DHJhtBRNPLiuIw2a4N5xmuQSP3bRdzlWR1TBA VQA7j7Sv+dUR8RXailwsOpplEFoyMKPhy8QK3Oq9YGKgd1Y8xLT8WVCMSsF5XLpGiJM3m FEN2SeOvnjQqHp+9mSXvKusrTQHvPp0SDEwz5Nz+85PTwYrUtIMDs2XnCOmHbOtz8nIMu Sc1RIDTDTXE/hLyBmgiLw6CfxHkhT2rB799lwWzD34TuIQI3TwQD6QQlG33nzCdc7jQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796526334; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=/vTbmL3p++k8pgDs7LUOrEb0kJhrRS/nPchlflFbhEU=; b=w6bPKZkiWWhzRiAFA2rzpPkz38WikTAdlCFeZfHTE8OcrgyhjAXNOZcJe5Ld1fa/hd+G D4Crz+P+5GTdZlX5Z4p3Z8HdUccHmRJvAfqgUK9xQtcX04zEZntsHArwPx25sP+ZPQBHi zzaJ7E+2pKm7yWdyC8DsFN6bopc4cOsDbeKR0r4Fzz4cDmHhuWQ7yzrSHX2x8He52zdea ckYfSpCZeESOETO2Ok50/KOQU77fYP9v5oBhac/mHpDEY/Lty6794/g5z1M7dHiqtOW45 4gvjYFVdnBwYB1yppeveJJy3AiTV0wk/kGCUunTsHbkMlzQ/wuujl6fTQgreoUKGHOA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ILMYt8u5; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FE8D606A336 for ; Sun, 6 Dec 2026 03:05:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ILMYt8u5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92B7D1F0092B; Sun, 6 Dec 2026 03:05:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796526333; bh=/vTbmL3p++k8pgDs7LUOrEb0kJhrRS/nPchlflFbhEU=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=ILMYt8u5gBW31ylCyPplrZzT4+8CaHFA7pdqf/Ztpy2f2sxfXOd95Md2TalJLMwOg Rh3kkEbRpCeQCXJSXcbNmX7+3eELTryEtpcAcir7umppM1LfGneP8gtotJ95P9b1xT NrhshO0kTJ5OffEVhhWP+TeIk0FNXePZE/aK8pedT6rgNy3gLi8nWDL0zfS77WMuPf eO1q2byjZEVOm/nD8x8xQYl7ihQNcVWo4hf9/hl0VUQXqk/bE0ZKSHB9V4TsIT2ejw PI8aUgpMtwX40b7uag5IJW4Lzu5veCaQT91fg1mbECwUiM6+gXMuJAhWSYDqrG5Acf 9FBAVhAEACEsg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH RFC 1/2] clk: add CLK_GET_RATE_RECALC_ANCESTORS flag Reply-To: sashiko-reviews@lists.linux.dev To: "Alexey Klimov" Cc: linux-clk@vger.kernel.org In-Reply-To: <20261206-e850_acpm_clks_unstale-v1-1-3eda94959b0b@linaro.org> References: <20261206-e850_acpm_clks_unstale-v1-0-3eda94959b0b@linaro.org> <20261206-e850_acpm_clks_unstale-v1-1-3eda94959b0b@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 06 Dec 2026 03:05:32 +0000 X-Mailing-List: linux-clk@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] clk: clk_change_rate() drops POST_RATE_CHANGE notifiers on ancestor recalc - [Medium] clk: clk_round_rate() uses stale rates for CLK_GET_RATE_RECALC_ANCESTORS - [Low] clk: redundant rate recalculations for CLK_GET_RATE_RECALC_ANCESTORS - [Low] clk: clk_summary and clk_dump report inconsistent rates for recalc ancestors - [Low] clk: unchecked clk_hw_get_clk() and clk leak on assert in KUnit tests - [Low] scripts/gdb: lx-clk-summary ignores CLK_GET_RATE_RECALC_ANCESTORS flag -- commit eb875d1168b4827f18822543ba2c3d47549f2073 Author: Alexey Klimov clk: add CLK_GET_RATE_RECALC_ANCESTORS flag This commit introduces the CLK_GET_RATE_RECALC_ANCESTORS clock flag so that clocks whose ancestors can be reprogrammed out of band refresh ancestor rates on get_rate, set_rate, and clk_change_rate(), along with KUnit tests for the new behavior. > diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c > index 673eac47450b1..5977a5c7be77b 100644 > --- a/drivers/clk/clk.c > +++ b/drivers/clk/clk.c > @@ -2031,10 +2031,61 @@ static void __clk_recalc_rates(struct clk_core *c= ore, bool update_req, > __clk_recalc_rates(child, update_req, msg); > } > =20 [ ... ] > +static bool clk_core_recalc_ancestors(struct clk_core *core) > +{ > + struct clk_core *parent =3D core->parent; > + unsigned long parent_rate =3D 0; > + > + lockdep_assert_held(&prepare_lock); > + > + if (!parent) > + return false; > + > + if (clk_core_recalc_ancestors(parent)) > + return true; > + > + if (parent->parent) > + parent_rate =3D parent->parent->rate; > + > + if (clk_recalc(parent, parent_rate) =3D=3D parent->rate) > + return false; > + > + __clk_recalc_rates(parent, false, 0); > + > + return true; > +} [Severity: Low] Can the redundant clk_recalc() calls while holding prepare_lock be avoided? Here clk_recalc(parent, parent_rate) is called to detect a change, its result is discarded, and __clk_recalc_rates(parent, false, 0) immediately calls clk_recalc() on parent a second time. Similarly, clk_change_rate() ignores the return value of clk_core_recalc_ancestors(core) and recalculates core a second time even when the subtree was already updated. clk_set_rate_range_nolock() also walks the ancestor chain via clk_core_get_rate_recalc() right before clk_core_set_rate_nolock() repeats the same walk under the same prepare_lock hold. [ ... ] > static unsigned long clk_core_get_rate_recalc(struct clk_core *core) > { > - if (core && (core->flags & CLK_GET_RATE_NOCACHE)) > - __clk_recalc_rates(core, false, 0); > + if (core && clk_core_rate_is_uncached(core)) > + clk_core_recalc_uncached_rate(core); > =20 > return clk_core_get_rate_nolock(core); > } [Severity: Low] Will debugfs clk_summary and clk_dump report inconsistent rates within a single snapshot after an out-of-band ancestor change? Both clk_summary_show_subtree() and clk_dump_subtree() walk the clock tree top-down and call clk_core_get_rate_recalc() on each parent before visiting its children. Ancestors and earlier siblings are therefore printed with their stale cached rates, and only when traversal reaches a CLK_GET_RATE_RECALC_ANCESTORS leaf are the ancestor rates refreshed for the leaf and any remaining siblings. [ ... ] > @@ -2509,6 +2561,16 @@ static void clk_change_rate(struct clk_core *core) > =20 > trace_clk_set_rate_complete(core, core->new_rate); > =20 > + /* > + * Setting the rate may have changed the rates of the ancestors behind > + * the clock framework's back, for example when the rate change is > + * carried out by firmware. > + */ > + if ((core->flags & CLK_GET_RATE_RECALC_ANCESTORS) && parent) { > + clk_core_recalc_ancestors(core); > + best_parent_rate =3D parent->rate; > + } > + > core->rate =3D clk_recalc(core, best_parent_rate); [Severity: High] Does calling clk_core_recalc_ancestors() here silently drop POST_RATE_CHANGE notifiers for descendants and remaining siblings? clk_core_set_rate_nolock() has already sent PRE_RATE_CHANGE across the subtree rooted at top. When set_rate() updates an ancestor, clk_core_recalc_ancestors() calls __clk_recalc_rates(parent, false, 0), which overwrites ->rate across the entire ancestor subtree without notifying. When clk_change_rate() then visits core->children or remaining siblings under top, old_rate =3D core->rate reads the already-overwritten post-change rate. The subsequent old_rate !=3D core->rate check evaluates to false and skips __clk_notify(core, POST_RATE_CHANGE, ...), which can leave consumer drivers that wait for POST_RATE_CHANGE stalled or desynchronized. [ ... ] > @@ -2583,6 +2645,13 @@ static int clk_core_set_rate_nolock(struct clk_cor= e *core, > if (!core) > return 0; > =20 > + /* > + * The cached rates may be stale, refresh them so that both the > + * rounding and the early bail-out below use the hardware rates. > + */ > + if (core->flags & CLK_GET_RATE_RECALC_ANCESTORS) > + clk_core_recalc_uncached_rate(core); > + > rate =3D clk_core_req_round_rate_nolock(core, req_rate); [Severity: Medium] Should clk_round_rate() also refresh rates for CLK_GET_RATE_RECALC_ANCESTORS clocks before rounding? clk_round_rate() calls clk_core_init_rate_req() and clk_core_round_rate_nolock() without calling clk_core_recalc_uncached_rate() first, so it computes the rounded rate from stale cached parent->rate and core->rate values and can disagree with what clk_set_rate() actually applies. Likewise, clk_core_set_parent_nolock() reads cached parent->rate without refreshing the ancestors of the new parent. [ ... ] > diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c > index 0505ed357b20f..7c3dd9ad9de34 100644 > --- a/drivers/clk/clk_test.c > +++ b/drivers/clk/clk_test.c > @@ -475,6 +475,222 @@ static struct kunit_suite clk_uncached_test_suite = =3D { > .test_cases =3D clk_uncached_test_cases, > }; [ ... ] > +static void clk_test_recalc_ancestors_set_rate(struct kunit *test) > +{ > + struct clk_recalc_ancestors_context *ctx =3D test->priv; > + struct clk *clk =3D clk_hw_get_clk(&ctx->leaf_hw, NULL); > + > + KUNIT_ASSERT_EQ(test, clk_set_rate(clk, DUMMY_CLOCK_RATE_1), 0); > + KUNIT_EXPECT_EQ(test, ctx->leaf_set_rate_calls, 1); > + KUNIT_EXPECT_EQ(test, clk_hw_get_rate(&ctx->leaf_hw), > + DUMMY_CLOCK_RATE_1); > + KUNIT_EXPECT_EQ(test, clk_hw_get_rate(&ctx->pll.hw), > + DUMMY_CLOCK_RATE_1); > + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_RATE_1); > + > + clk_put(clk); > +} [Severity: Low] Can this dereference an ERR_PTR or leak clk on assertion failure? All five new KUnit tests pass clk_hw_get_clk(&ctx->leaf_hw, NULL) directly to clk_get_rate() or clk_set_rate() without checking for an error pointer, which would oops on allocation failure. In addition, if KUNIT_ASSERT_EQ() fails here or in clk_test_recalc_ancestors_set_rate_stale_cache(), clk_put(clk) is skipped and leaks both struct clk and struct clk_core. Using clk_hw_get_clk_kunit() with KUNIT_ASSERT_NOT_ERR_OR_NULL() avoids both issues. [ ... ] > diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h > index c2b80cb3ab0b6..6309588363a0a 100644 > --- a/include/linux/clk-provider.h > +++ b/include/linux/clk-provider.h [ ... ] > @@ -46,6 +49,8 @@ > #define CLK_IS_CRITICAL BIT(11) > #define CLK_OPS_PARENT_ENABLE BIT(12) > #define CLK_DUTY_CYCLE_PARENT BIT(13) > +/* ancestor rates can change behind the clk framework's back, re-read th= em */ > +#define CLK_GET_RATE_RECALC_ANCESTORS BIT(14) [Severity: Low] Should scripts/gdb/linux/constants.py.in and scripts/gdb/linux/clk.py also handle CLK_GET_RATE_RECALC_ANCESTORS? Since this flag implies CLK_GET_RATE_NOCACHE, clocks that set only CLK_GET_RATE_RECALC_ANCESTORS will not be annotated with (c) in LxClkSummary.show_subtree() to warn that the cached rate may be stale. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261206-e850_acpm_= clks_unstale-v1-0-3eda94959b0b@linaro.org?part=3D1