[PATCH 22/22] clk: Prevent a clock without a rate to register

Maxime Ripard
2022-04-08T09:14:46Z

A rate of 0 for a clock is considered an error, as evidenced by the
documentation of clk_get_rate() and the code of clk_get_rate() and
clk_core_get_rate_nolock().

The main source of that error is if the clock is supposed to have a
parent but is orphan at the moment of the call. This is likely to be
transient and solved later in the life of the system as more clocks are
registered.

The corollary is thus that if a clock is not an orphan, has a parent that
has a rate (so is not an orphan itself either) but returns a rate of 0,
something is wrong in the driver. Let's return an error in such a case.

Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
drivers/clk/clk.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 8bbb6adeeead..e8c55678da85 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -3773,6 +3773,16 @@ static int __clk_core_init(struct clk_core *core)
rate = 0;
core->rate = core->req_rate = rate;

+ /*
+ * If we're not an orphan clock and our parent has a rate, then
+ * if our rate is 0, something is badly broken in recalc_rate.
+ */
+ if (!core->orphan && (parent && parent->rate) && !core->rate) {
+ ret = -EINVAL;
+ pr_warn("%s: recalc_rate returned a null rate\n", core->name);
+ goto out;
+ }
+
/*
* Enable CLK_IS_CRITICAL clocks so newly added critical clocks
* don't get accidentally disabled when walking the orphan tree and
--
2.35.1