* [PATCH RFC 0/2] clk: introduce CLK_GET_RATE_RECALC_ANCESTORS flag for firmware-controlled clocks
@ 2026-12-06 2:42 Alexey Klimov
2026-12-06 2:42 ` [PATCH RFC 1/2] clk: add CLK_GET_RATE_RECALC_ANCESTORS flag Alexey Klimov
2026-12-06 2:42 ` [PATCH RFC 2/2] clk: samsung: acpm: recalculate parent chain rates on Exynos850 Alexey Klimov
0 siblings, 2 replies; 5+ messages in thread
From: Alexey Klimov @ 2026-12-06 2:42 UTC (permalink / raw)
To: Stephen Boyd, Brian Masney, Jerome Brunet, Tudor Ambarus,
Krzysztof Kozlowski, Sylwester Nawrocki, Chanwoo Choi,
Peter Griffin, Alim Akhtar
Cc: Sam Protsenko, Thomas Turner, linux-clk, linux-samsung-soc,
Claude Opus 5.5
On Exynos850 the ACPM clocks are registered as children of the CMU clocks,
because the ACPM firmware has no rate read call, and ->recalc_rate() is
currently implemented to return the parent rate.
The rate of the parent chain is changed behind the clock framework's back:
the ACPM firmware reprograms the PLL on a set_rate request,
Initially the problem was described here:
https://lore.kernel.org/linux-samsung-soc/DLAZXSNZK6QC.1VFAX37XCXBOV@linaro.org/
Then was discussed again after submitting acpm enablement patches for
exynos850.
This is an attempt to solve it by adding CLK_GET_RATE_RECALC_ANCESTORS
flag that forces rate updates up the tree.
CLK_GET_RATE_NOCACHE only recalculates the clock itself from the cached
rate of its parent, so clk_get_rate() on the ACPM clocks keeps returning
the boot rate, and clk_set_rate() can drop a request because of the stale
cached rate.
Downstream kernel implements the get_rate() meachanism by reading hw
registers of CMU block directly and then matching the values to the entry
in some table that contains PLL rate for such set of values. The current
thinking process is that caching the last rate requested from the firmware
in the driver is not an option, since the ACPM may not set the requested
rate and, it is also expected that for DVFS clocks the thermal hw block
can change the rate without a request to firmware (the latter needs a
bit of testing to confirm though).
First commit adds a CLK_GET_RATE_RECALC_ANCESTORS flag. For a clock with
that flag, the framework re-reads the rates of all its ancestors from the
root down when the rate is read or set. The subtree of the topmost ancestor
whose rate changed is recalculated, so that siblings stay consistent.
Existing CLK_GET_RATE_NOCACHE users are not meant to be affected.
Second commit uses the flag for the Exynos850 ACPM clocks.
Explicit clk_recalc_ancestors() call was considered instead of a flag but
seems there are no good places to call it. The TMU changes are asynchronous
and the consumers like cpufreq or devfreq are generic.
Signed-off-by: Alexey Klimov <alexey.klimov@linaro.org>
---
Alexey Klimov (2):
clk: add CLK_GET_RATE_RECALC_ANCESTORS flag
clk: samsung: acpm: recalculate parent chain rates on Exynos850
drivers/clk/clk.c | 82 ++++++++++++++--
drivers/clk/clk_test.c | 217 +++++++++++++++++++++++++++++++++++++++++
drivers/clk/samsung/clk-acpm.c | 8 +-
include/linux/clk-provider.h | 5 +
4 files changed, 304 insertions(+), 8 deletions(-)
---
base-commit: 0b5bb03d26a268f2606e3f574b277a1105136328
change-id: 20261204-e850_acpm_clks_unstale-a853b7e3a79b
Best regards,
--
Alexey Klimov <alexey.klimov@linaro.org>
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH RFC 1/2] clk: add CLK_GET_RATE_RECALC_ANCESTORS flag 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 ` Alexey Klimov 2026-12-06 3:05 ` sashiko-bot 2026-12-06 2:42 ` [PATCH RFC 2/2] clk: samsung: acpm: recalculate parent chain rates on Exynos850 Alexey Klimov 1 sibling, 1 reply; 5+ messages in thread From: Alexey Klimov @ 2026-12-06 2:42 UTC (permalink / raw) To: Stephen Boyd, Brian Masney, Jerome Brunet, Tudor Ambarus, Krzysztof Kozlowski, Sylwester Nawrocki, Chanwoo Choi, Peter Griffin, Alim Akhtar Cc: Sam Protsenko, Thomas Turner, linux-clk, linux-samsung-soc, Claude Opus 5.5 Some clocks sit at the bottom of a chain of PLLs, muxes and dividers that can be reprogrammed without the clock framework knowing about it. On Exynos850, for example, the ACPM firmware reprograms the CPU cluster PLLs on DVFS requests, and potentially on thermal frequency scaling events. ACPM controls different clocks; DVFS clocks is the main usage at this point. CLK_GET_RATE_NOCACHE does not help for such clocks and its parent clock. Add a CLK_GET_RATE_RECALC_ANCESTORS flag. For a clock with this flag: - clk_get_rate() walks the ancestors from the root down and re-reads their rates from hardware. The subtree of the topmost ancestor whose rate changed is recalculated, so that the other children of that ancestor are updated as well and the tree stays consistent. - clk_set_rate() refreshes the rates before rounding the requested rate. Otherwise the early bail-out, which compares the rounded rate with the cached rate, can skip a rate change because the cached rate happens to equal the requested rate while the hardware runs at a different one. - clk_change_rate() refreshes the ancestors after ->set_rate(), since setting the rate may have changed the ancestors as well, and uses the refreshed parent rate to recalculate the clock rate. The flag implies the CLK_GET_RATE_NOCACHE behaviour for the clock itself. Existing CLK_GET_RATE_NOCACHE users should not be affected. Rate change notifiers are not called for changes found this way, since the framework only finds out about them after the fact. A mux parent changed behind the framework's back is not detected either. Add KUnit tests covering a change of the root and of an intermediate ancestor, the update of a sibling, a ->set_rate() that changes the ancestors, and the stale early bail-out in clk_set_rate(). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Alexey Klimov <alexey.klimov@linaro.org> --- drivers/clk/clk.c | 82 ++++++++++++++-- drivers/clk/clk_test.c | 217 +++++++++++++++++++++++++++++++++++++++++++ include/linux/clk-provider.h | 5 + 3 files changed, 298 insertions(+), 6 deletions(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 673eac47450b..5977a5c7be77 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); } +/** + * clk_core_recalc_ancestors - re-read the rates of all ancestors of a clk + * @core: the clk whose ancestors are recalculated + * + * Walks the ancestors of @core from the root down and recalculates their rates + * from hardware. As soon as an ancestor is found whose rate differs from the + * cached one, the whole subtree of that ancestor is recalculated, so that its + * other children see the new rate as well. + * + * Return: true if the subtree of an ancestor, which includes @core, has been + * recalculated, false if all ancestor rates were up to date. + */ +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; +} + +static bool clk_core_rate_is_uncached(struct clk_core *core) +{ + return core->flags & (CLK_GET_RATE_NOCACHE | + CLK_GET_RATE_RECALC_ANCESTORS); +} + +static void clk_core_recalc_uncached_rate(struct clk_core *core) +{ + if ((core->flags & CLK_GET_RATE_RECALC_ANCESTORS) && + clk_core_recalc_ancestors(core)) + return; + + __clk_recalc_rates(core, false, 0); +} + 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); } @@ -2044,9 +2095,10 @@ static unsigned long clk_core_get_rate_recalc(struct clk_core *core) * @clk: the clk whose rate is being returned * * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag - * is set, which means a recalc_rate will be issued. Can be called regardless of - * the clock enabledness. If clk is NULL, or if an error occurred, then returns - * 0. + * is set, which means a recalc_rate will be issued. If the + * CLK_GET_RATE_RECALC_ANCESTORS flag is set, the rates of all ancestors of the + * clk are recalculated as well. Can be called regardless of the clock + * enabledness. If clk is NULL, or if an error occurred, then returns 0. */ unsigned long clk_get_rate(struct clk *clk) { @@ -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); if (core->flags & CLK_SET_RATE_UNGATE) { @@ -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); /* bail early if nothing to do */ @@ -2751,7 +2820,7 @@ static int clk_set_rate_range_nolock(struct clk *clk, } rate = clk->core->req_rate; - if (clk->core->flags & CLK_GET_RATE_NOCACHE) + if (clk_core_rate_is_uncached(clk->core)) rate = clk_core_get_rate_recalc(clk->core); /* @@ -3639,6 +3708,7 @@ static const struct { ENTRY(CLK_IS_CRITICAL), ENTRY(CLK_OPS_PARENT_ENABLE), ENTRY(CLK_DUTY_CYCLE_PARENT), + ENTRY(CLK_GET_RATE_RECALC_ANCESTORS), #undef ENTRY }; diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c index 0505ed357b20..7c3dd9ad9de3 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, }; +/* + * Clock tree mimicking a firmware-controlled DVFS domain: + * + * pll (root) -> div -> leaf (CLK_GET_RATE_RECALC_ANCESTORS) + * \-> sibling + * + * The pll rate and the div value can change behind the clock framework's + * back, and setting the rate of the leaf reprograms the pll, as firmware + * would do. + */ +struct clk_recalc_ancestors_context { + struct clk_dummy_context pll; + struct clk_hw div_hw; + unsigned int div; + struct clk_hw leaf_hw; + unsigned int leaf_set_rate_calls; + struct clk_hw sibling_hw; +}; + +static unsigned long clk_recalc_ancestors_div_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct clk_recalc_ancestors_context *ctx = + container_of(hw, struct clk_recalc_ancestors_context, div_hw); + + return parent_rate / ctx->div; +} + +static const struct clk_ops clk_recalc_ancestors_div_ops = { + .recalc_rate = clk_recalc_ancestors_div_recalc_rate, +}; + +static unsigned long clk_recalc_ancestors_leaf_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + return parent_rate; +} + +static int clk_recalc_ancestors_leaf_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + struct clk_recalc_ancestors_context *ctx = + container_of(hw, struct clk_recalc_ancestors_context, leaf_hw); + + /* The "firmware" reprograms the pll behind the framework's back */ + ctx->pll.rate = rate * ctx->div; + ctx->leaf_set_rate_calls++; + + return 0; +} + +static const struct clk_ops clk_recalc_ancestors_leaf_ops = { + .recalc_rate = clk_recalc_ancestors_leaf_recalc_rate, + .determine_rate = clk_determine_rate_noop, + .set_rate = clk_recalc_ancestors_leaf_set_rate, +}; + +static int clk_recalc_ancestors_test_init(struct kunit *test) +{ + struct clk_recalc_ancestors_context *ctx; + int ret; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + ctx->pll.rate = DUMMY_CLOCK_INIT_RATE; + ctx->pll.hw.init = CLK_HW_INIT_NO_PARENT("test-pll", + &clk_dummy_rate_ops, 0); + ret = clk_hw_register_kunit(test, NULL, &ctx->pll.hw); + if (ret) + return ret; + + ctx->div = 1; + ctx->div_hw.init = CLK_HW_INIT_HW("test-div", &ctx->pll.hw, + &clk_recalc_ancestors_div_ops, 0); + ret = clk_hw_register_kunit(test, NULL, &ctx->div_hw); + if (ret) + return ret; + + ctx->leaf_hw.init = CLK_HW_INIT_HW("test-leaf", &ctx->div_hw, + &clk_recalc_ancestors_leaf_ops, + CLK_GET_RATE_RECALC_ANCESTORS); + ret = clk_hw_register_kunit(test, NULL, &ctx->leaf_hw); + if (ret) + return ret; + + ctx->sibling_hw.init = CLK_HW_INIT_HW("test-sibling", &ctx->div_hw, + &empty_clk_ops, 0); + ret = clk_hw_register_kunit(test, NULL, &ctx->sibling_hw); + if (ret) + return ret; + + return 0; +} + +/* + * Test that clk_get_rate() on a CLK_GET_RATE_RECALC_ANCESTORS clock returns + * the new rate if the root of its parent chain changed rate behind the clock + * framework's back. + */ +static void clk_test_recalc_ancestors_root_changed(struct kunit *test) +{ + struct clk_recalc_ancestors_context *ctx = test->priv; + struct clk *clk = clk_hw_get_clk(&ctx->leaf_hw, NULL); + + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_INIT_RATE); + + ctx->pll.rate = DUMMY_CLOCK_RATE_1; + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_RATE_1); + + clk_put(clk); +} + +/* + * Test that clk_get_rate() on a CLK_GET_RATE_RECALC_ANCESTORS clock returns + * the new rate if an intermediate ancestor changed rate behind the clock + * framework's back. + */ +static void clk_test_recalc_ancestors_intermediate_changed(struct kunit *test) +{ + struct clk_recalc_ancestors_context *ctx = test->priv; + struct clk *clk = clk_hw_get_clk(&ctx->leaf_hw, NULL); + + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_INIT_RATE); + + ctx->div = 2; + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_INIT_RATE / 2); + + clk_put(clk); +} + +/* + * Test that once an ancestor rate change has been detected, the other + * children of that ancestor are updated as well. + */ +static void clk_test_recalc_ancestors_sibling_updated(struct kunit *test) +{ + struct clk_recalc_ancestors_context *ctx = test->priv; + struct clk *clk = clk_hw_get_clk(&ctx->leaf_hw, NULL); + + ctx->pll.rate = DUMMY_CLOCK_RATE_1; + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_RATE_1); + KUNIT_EXPECT_EQ(test, clk_hw_get_rate(&ctx->sibling_hw), + DUMMY_CLOCK_RATE_1); + KUNIT_EXPECT_EQ(test, clk_hw_get_rate(&ctx->div_hw), + DUMMY_CLOCK_RATE_1); + + clk_put(clk); +} + +/* + * Test that when setting the rate of a CLK_GET_RATE_RECALC_ANCESTORS clock + * changes the rate of its ancestors, the cached rates of the clock and of its + * ancestors are correct afterwards. + */ +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); +} + +/* + * Test that clk_set_rate() on a CLK_GET_RATE_RECALC_ANCESTORS clock does not + * bail out early based on a stale cached rate. + * + * The hardware changed from the initial rate to DUMMY_CLOCK_RATE_1 without + * the clock framework knowing, so the cached rate is still the initial one. + * Requesting the initial rate must still call ->set_rate(). + */ +static void clk_test_recalc_ancestors_set_rate_stale_cache(struct kunit *test) +{ + struct clk_recalc_ancestors_context *ctx = test->priv; + struct clk *clk = clk_hw_get_clk(&ctx->leaf_hw, NULL); + + ctx->pll.rate = DUMMY_CLOCK_RATE_1; + + KUNIT_ASSERT_EQ(test, clk_set_rate(clk, DUMMY_CLOCK_INIT_RATE), 0); + KUNIT_EXPECT_EQ(test, ctx->leaf_set_rate_calls, 1); + KUNIT_EXPECT_EQ(test, ctx->pll.rate, DUMMY_CLOCK_INIT_RATE); + KUNIT_EXPECT_EQ(test, clk_get_rate(clk), DUMMY_CLOCK_INIT_RATE); + + clk_put(clk); +} + +static struct kunit_case clk_recalc_ancestors_test_cases[] = { + KUNIT_CASE(clk_test_recalc_ancestors_root_changed), + KUNIT_CASE(clk_test_recalc_ancestors_intermediate_changed), + KUNIT_CASE(clk_test_recalc_ancestors_sibling_updated), + KUNIT_CASE(clk_test_recalc_ancestors_set_rate), + KUNIT_CASE(clk_test_recalc_ancestors_set_rate_stale_cache), + {} +}; + +/* + * Test suite for a CLK_GET_RATE_RECALC_ANCESTORS clock whose ancestors can + * change rate behind the clock framework's back. + */ +static struct kunit_suite clk_recalc_ancestors_test_suite = { + .name = "clk-recalc-ancestors-test", + .init = clk_recalc_ancestors_test_init, + .test_cases = clk_recalc_ancestors_test_cases, +}; + static int clk_multiple_parents_mux_test_init(struct kunit *test) { @@ -3896,6 +4112,7 @@ kunit_test_suites( &clk_register_clk_parent_data_device_suite, &clk_single_parent_mux_test_suite, &clk_uncached_test_suite, + &clk_recalc_ancestors_test_suite, ); MODULE_DESCRIPTION("Kunit tests for clk framework"); MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h index c2b80cb3ab0b..6309588363a0 100644 --- a/include/linux/clk-provider.h +++ b/include/linux/clk-provider.h @@ -30,6 +30,9 @@ * * CLK_IS_CRITICAL - do not gate, ever * * CLK_OPS_PARENT_ENABLE - parents need enable during gate/ungate, set rate and re-parent * * CLK_DUTY_CYCLE_PARENT - duty cycle call may be forwarded to the parent clock + * * CLK_GET_RATE_RECALC_ANCESTORS - do not use the cached clk rate, and re-read + * the rates of all ancestors first, as they can change without the clock + * framework knowing */ /* Please update clk_flags[] in drivers/clk/clk.c when making changes here! */ #define CLK_SET_RATE_GATE BIT(0) @@ -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) struct clk; struct clk_hw; -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH RFC 1/2] clk: add CLK_GET_RATE_RECALC_ANCESTORS flag 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 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-12-06 3:05 UTC (permalink / raw) To: Alexey Klimov; +Cc: linux-clk 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH RFC 2/2] clk: samsung: acpm: recalculate parent chain rates on Exynos850 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 2:42 ` Alexey Klimov 2026-12-06 2:59 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: Alexey Klimov @ 2026-12-06 2:42 UTC (permalink / raw) To: Stephen Boyd, Brian Masney, Jerome Brunet, Tudor Ambarus, Krzysztof Kozlowski, Sylwester Nawrocki, Chanwoo Choi, Peter Griffin, Alim Akhtar Cc: Sam Protsenko, Thomas Turner, linux-clk, linux-samsung-soc, Claude Opus 5.5 On Exynos850 the ACPM firmware does not implement a rate read, so the ACPM clocks are registered as children of the CMU clocks and ->recalc_rate() returns the parent rate. The clocks are flagged CLK_GET_RATE_NOCACHE, but that only recalculates the ACPM clock itself from the cached rate of its parent. The parent rate does change behind the clock framework's back: the ACPM firmware reprograms the PLLs when the rate of an ACPM clock is set. As a result: - clk_get_rate() keeps returning the rate cached at boot, which breaks cpufreq and devfreq; - after clk_set_rate(), the cached rate of the ACPM clock is still the old parent rate. A following request for that old rate is then dropped by the early bail-out in clk_set_rate() without reaching the firmware, and the domain stays at the previous rate. Use CLK_GET_RATE_RECALC_ANCESTORS instead, so that the clock framework re-reads the PLL, mux and divider registers of the whole parent chain whenever the rate of an ACPM clock is read or set. Fixes: 13634cde9771 ("clk: samsung: acpm: add Exynos850 support") Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Alexey Klimov <alexey.klimov@linaro.org> --- drivers/clk/samsung/clk-acpm.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/clk/samsung/clk-acpm.c b/drivers/clk/samsung/clk-acpm.c index d5f1e73e05f8..2a9a164946b4 100644 --- a/drivers/clk/samsung/clk-acpm.c +++ b/drivers/clk/samsung/clk-acpm.c @@ -128,12 +128,16 @@ static int acpm_clk_register(struct device *dev, struct acpm_clk *aclk, init.ops = &acpm_clk_ops; aclk->hw.init = &init; - /* If get_rate is set, the SoC relies on parent topology */ + /* + * If get_rate is set, the SoC relies on parent topology. The firmware + * and the hardware can change the rates of the parent chain behind the + * clock framework's back, so re-read the whole chain on every request. + */ if (drv_data->get_rate) { pdata.fw_name = name; init.parent_data = &pdata; init.num_parents = 1; - init.flags = CLK_GET_RATE_NOCACHE; + init.flags = CLK_GET_RATE_RECALC_ANCESTORS; } return devm_clk_hw_register(dev, &aclk->hw); -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH RFC 2/2] clk: samsung: acpm: recalculate parent chain rates on Exynos850 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 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-12-06 2:59 UTC (permalink / raw) To: Alexey Klimov; +Cc: linux-clk Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] clk: samsung: CLK_GET_RATE_RECALC_ANCESTORS ignores mux switches on Exynos850 -- commit 72f8fd076942edb559374323ade796c613446bde Author: Alexey Klimov <alexey.klimov@linaro.org> clk: samsung: acpm: recalculate parent chain rates on Exynos850 This commit switches Exynos850 ACPM clocks from CLK_GET_RATE_NOCACHE to CLK_GET_RATE_RECALC_ANCESTORS so the clock framework recalculates parent rates when an ACPM clock rate is queried or set. > diff --git a/drivers/clk/samsung/clk-acpm.c b/drivers/clk/samsung/clk-acpm.c > index d5f1e73e05f8b..2a9a164946b40 100644 > --- a/drivers/clk/samsung/clk-acpm.c > +++ b/drivers/clk/samsung/clk-acpm.c > @@ -128,12 +128,16 @@ static int acpm_clk_register(struct device *dev, struct acpm_clk *aclk, > init.ops = &acpm_clk_ops; > aclk->hw.init = &init; > > - /* If get_rate is set, the SoC relies on parent topology */ > + /* > + * If get_rate is set, the SoC relies on parent topology. The firmware > + * and the hardware can change the rates of the parent chain behind the > + * clock framework's back, so re-read the whole chain on every request. > + */ > if (drv_data->get_rate) { > pdata.fw_name = name; > init.parent_data = &pdata; > init.num_parents = 1; > - init.flags = CLK_GET_RATE_NOCACHE; > + init.flags = CLK_GET_RATE_RECALC_ANCESTORS; > } [Severity: Medium] Does CLK_GET_RATE_RECALC_ANCESTORS actually handle mux switches on Exynos850? clk_core_recalc_ancestors() only walks cached core->parent links and calls clk_recalc(), never ops->get_parent(). Samsung CMU muxes use clk_mux_ops with no recalc_rate callback, so mux registers are never re-read (as noted in commit "clk: add CLK_GET_RATE_RECALC_ANCESTORS flag"). On Exynos850, DVFS domains without dedicated PLLs such as int (CMU_CORE) rely on CMU_TOP muxes selecting among shared PLL dividers. When ACPM firmware switches such a mux, clk_get_rate() still returns the stale rate from the cached parent and clk_set_rate() can drop a request for that rate. The commit message also states that mux registers are re-read, which does not match clk_core_recalc_ancestors(). Either clk_core_recalc_ancestors() needs to re-read mux parents via get_parent(), or the commit message should reflect that mux switches are not detected. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261206-e850_acpm_clks_unstale-v1-0-3eda94959b0b@linaro.org?part=2 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-06 3:05 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
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.