[PATCH 09/12] clk: Prevent unregistered clk_hw from being reinserted into clk tree

Stephen Boyd
2024-08-15T00:55:26Z

The clk framework removes the clk_core from the clk tree by deleting the
'child_node' hlist for a clk_hw when it is unregistered in
clk_unregister(). This removes the clk from the clk tree, but a call to
clk_set_parent() will allow the clk to be added right back into the clk
tree, undoing all the work done in clk_unregister() to remove it from
the tree and orphan all descendants. This results in a double hlist
delete as well, because clk_reparent() deletes the 'child_node' hlist
before adding the clk to either the orphan list or the clk tree.

The 'clk_nodrv_ops' have a set_parent clk_op that returns failure, but
that still won't save us even if we fix the clk_reparent() code to
operate on an empty list. That's because we'll reparent the clk back to
the original parent when the clk_op returns an error, effectively
putting the clk back into the clk tree.

Force the number of parents to be zero in clk_unregister() so that
clk_set_parent() can't get past the part where it figures out which
index to use to call the clk_op with.

Cc: Nuno Sá <nuno.sa@analog.com>
Cc: Sylwester Nawrocki <s.nawrocki@samsung.com>
Fixes: fcb0ee6a3d33 ("clk: Implement clk_unregister")
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
---
drivers/clk/clk.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index a0c275e156ad..d99d2d4dd411 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -4611,6 +4611,11 @@ void clk_unregister(struct clk *clk)
clk_core_evict_parent_cache(clk->core);

hlist_del_init(&clk->core->child_node);
+ /*
+ * Prevent clk from being reinserted into the clk tree via
+ * clk_set_parent()
+ */
+ clk->core->num_parents = 0;

if (clk->core->prepare_count)
pr_warn("%s: unregistering prepared clock: %s\n",