All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexey Klimov" <alexey.klimov@linaro.org>
Cc: linux-clk@vger.kernel.org
Subject: Re: [PATCH RFC 1/2] clk: add CLK_GET_RATE_RECALC_ANCESTORS flag
Date: Sun, 06 Dec 2026 03:05:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-252104@kernel.org> (raw)
In-Reply-To: <20261206-e850_acpm_clks_unstale-v1-1-3eda94959b0b@linaro.org>

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 <alexey.klimov@linaro.org>

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 *core, bool update_req,
>  		__clk_recalc_rates(child, update_req, msg);
>  }
>  
[ ... ]
> +static bool clk_core_recalc_ancestors(struct clk_core *core)
> +{
> +	struct clk_core *parent = core->parent;
> +	unsigned long parent_rate = 0;
> +
> +	lockdep_assert_held(&prepare_lock);
> +
> +	if (!parent)
> +		return false;
> +
> +	if (clk_core_recalc_ancestors(parent))
> +		return true;
> +
> +	if (parent->parent)
> +		parent_rate = parent->parent->rate;
> +
> +	if (clk_recalc(parent, parent_rate) == 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);
>  
>  	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)
>  
>  	trace_clk_set_rate_complete(core, core->new_rate);
>  
> +	/*
> +	 * 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 = parent->rate;
> +	}
> +
>  	core->rate = 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 = core->rate reads the already-overwritten post-change
rate.  The subsequent old_rate != 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_core *core,
>  	if (!core)
>  		return 0;
>  
> +	/*
> +	 * 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 = 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 = {
>  	.test_cases = clk_uncached_test_cases,
>  };
[ ... ]
> +static void clk_test_recalc_ancestors_set_rate(struct kunit *test)
> +{
> +	struct clk_recalc_ancestors_context *ctx = test->priv;
> +	struct clk *clk = 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 them */
> +#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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206-e850_acpm_clks_unstale-v1-0-3eda94959b0b@linaro.org?part=1

  reply	other threads:[~2026-12-06  3:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  2:42 [PATCH RFC 0/2] clk: introduce CLK_GET_RATE_RECALC_ANCESTORS flag for firmware-controlled clocks Alexey Klimov
2026-12-06  2:42 ` [PATCH RFC 1/2] clk: add CLK_GET_RATE_RECALC_ANCESTORS flag Alexey Klimov
2026-12-06  3:05   ` sashiko-bot [this message]
2026-12-06  2:42 ` [PATCH RFC 2/2] clk: samsung: acpm: recalculate parent chain rates on Exynos850 Alexey Klimov
2026-12-06  2:59   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-252104@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexey.klimov@linaro.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.