Skip to contentSkip to conversation

clk: allow a clk_hw to be registered again after unregistering it

PATCH In review Joshua Covington 2 participants 5 messages Nov 24 – Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover clk: allow a clk_hw to be registered again after unregistering it

Drivers with statically allocated clk_hw structures cannot register their clocks again once they have been unregistered, e.g. when the device is unbound and bound again. __clk_register() sets hw->init to NULL, so the second registration has no init data and oopses, and clk_unregister() leaves hw->core and hw->clk pointing to the clk_core and clk it releases. On the Raspberry Pi 5 / CM5 this is hit by unbinding and binding the RP1 southbridge, whose clk-rp1 driver uses static descriptors.

v2 worked around this in clk-rp1 alone. Following Brian's review, v3 fixes it in the clk core instead, so that the other drivers with static clk_hw structures benefit as well:

1/3 keeps the init pointer in the clk_core and puts it back into the
clk_hw when the registration fails and when the clock is unregistered, and clears hw->core and hw->clk on unregistration. Init data on the stack of the registering function is not handed back. This is decided at registration, as object_is_on_stack() only looks at the stack of the current task.
2/3 adds KUnit tests for this.
3/3 resets the cached rates of the clk-rp1 descriptors on probe, which
is the part of the v2 fix that is still needed in the driver.

Testing:
- CM5 hardware (arm64), 6.18.64 backport: the new KUnit tests pass, and repeated RP1 unbind/bind with all RP1 drivers loaded runs without an oops.
- x86_64 QEMU with KASAN: the clk KUnit suites pass (138 tests, 134 passed, 4 skipped as before), no KASAN reports. Without 1/3 all four new tests fail and KASAN reports a slab-use-after-free in __clk_register(). With init data on the stack handed back on unregistration, the stack test fails.
- arm64 defconfig with CLK_KUNIT_TEST, MISC_RP1 and COMMON_CLK_RP1, W=1, GCC and clang: no new warnings. checkpatch --strict and kernel-doc are clean.

Claude Opus 5.5 (Anthropic) was used to help write the tests, the changelogs and the clk-rp1 change, and to run the testing above apart from the CM5 runs. I have reviewed all of it and tested it on CM5 hardware.

Changes in v3:
- Fix this in the clk core (Brian Masney), with Brian as Co-developed-by
- Do not hand back init data that was on the stack
- Add KUnit tests
- Reduce the clk-rp1 change to resetting the cached rates
- Add Fixes: and Cc: stable tags

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Joshua Covington (3):
clk: allow a clk_hw to be registered again after unregistering it
clk: tests: add tests for registering a clk_hw again
clk: rp1: reset cached rates on probe

 drivers/clk/clk-rp1.c        |   6 ++
 drivers/clk/clk.c            |  23 +++++
 drivers/clk/clk_test.c       | 191 +++++++++++++++++++++++++++++++++++
 include/linux/clk-provider.h |   5 +-
 4 files changed, 224 insertions(+), 1 deletion(-)

base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc

Signature · 1 line
2.56.0
#1 lore raw

PATCH v3 1/3 clk: allow a clk_hw to be registered again after unregistering it

__clk_register() sets hw->init to NULL, and clk_unregister() leaves hw->core and hw->clk pointing to the clk_core and clk it is about to release. A driver with statically allocated clk_hw structures can therefore not register them again, e.g. when its device is unbound and bound again. The second registration has no init data and oopses. A clk_parent_data.hw reference to such a clk_hw also resolves to the freed clk_core of the previous registration.

Keep the init pointer in the clk_core, and put it back into the clk_hw when the registration fails and when the clock is unregistered. Init data on the stack of the registering function is gone by the time the clock is unregistered, so leave hw->init NULL in that case. This has to be checked at registration, as object_is_on_stack() only looks at the stack of the current task. Also clear hw->core and hw->clk on unregistration, so that a reference to an unregistered clk_hw is treated as an orphan.

---
 drivers/clk/clk.c            | 23 +++++++++++++++++++++++
 include/linux/clk-provider.h |  5 ++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
drivers/clk/clk.c +23 -0
index 4d2cc86bfb..c19ae1812c 100644
@@ -20,6 +20,7 @@
 #include <linux/of.h>
 #include <linux/pm_runtime.h>
 #include <linux/sched.h>
+#include <linux/sched/task_stack.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
@@ -70,6 +71,9 @@ struct clk_parent_map {
  * @ops:               Pointer to hardware-specific operations for this clk.
  * @hw:                Pointer for traversing from a struct clk to its
  *                     corresponding hardware-specific structure.
+ * @hw_init:           Init data of @hw, put back into @hw when the
+ *                     registration fails or the clk is unregistered. NULL
+ *                     if the init data was on the stack.
  * @owner:             Kernel module owning this clk (for reference counting).
  * @dev:               Device associated with this clk (optional)
  * @rpm_node:          Node for runtime power management list management.
@@ -119,6 +123,7 @@ struct clk_core {
 	const char		*name;
 	const struct clk_ops	*ops;
 	struct clk_hw		*hw;
+	const struct clk_init_data *hw_init;
 	struct module		*owner;
 	struct device		*dev;
 	struct hlist_node	rpm_node;
@@ -4457,6 +4462,8 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
 	if (dev && dev->driver)
 		core->owner = dev->driver->owner;
 	core->hw = hw;
+	/* Stack init data is gone once the registering function returns. */
+	core->hw_init = object_is_on_stack(init) ? NULL : init;
 	core->flags = init->flags;
 	core->num_parents = init->num_parents;
 	core->min_rate = 0;
@@ -4505,6 +4512,9 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
 		pr_err("%pOF: error %pe: failed to register clk '%s' (%pS)\n",
 		       np, ERR_PTR(ret), init->name, hw);
 	}
+
+	hw->init = init;
+
 	return ERR_PTR(ret);
 }
 
@@ -4656,11 +4666,16 @@ static void clk_core_evict_parent_cache(struct clk_core *core)
 /**
  * clk_unregister - unregister a currently registered clock
  * @clk: clock to unregister
+ *
+ * The clk_hw of @clk is returned to its state before registration, so
+ * that it can be registered again. Until then, a clk_parent_data.hw
+ * reference to it is treated as an orphan.
  */
 void clk_unregister(struct clk *clk)
 {
 	unsigned long flags;
 	const struct clk_ops *ops;
+	struct clk_hw *hw;
 
 	if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
 		return;
@@ -4709,6 +4724,14 @@ void clk_unregister(struct clk *clk)
 	if (clk->core->protect_count)
 		pr_warn("%s: unregistering protected clock: %s\n",
 					__func__, clk->core->name);
+
+	hw = clk->core->hw;
+	if (!WARN_ON(hw->core != clk->core || hw->clk != clk)) {
+		hw->core = NULL;
+		hw->init = clk->core->hw_init;
+		hw->clk = NULL;
+	}
+
 	clk_prepare_unlock();
 
 	kref_put(&clk->core->ref, __clk_release);
include/linux/clk-provider.h +4 -1
index a355d1ee51..026efed876 100644
@@ -359,7 +359,10 @@ struct clk_init_data {
  *
  * @init: pointer to struct clk_init_data that contains the init data shared
  * with the common clock framework. This pointer will be set to NULL once
- * a clk_register() variant is called on this clk_hw pointer.
+ * a clk_register() variant is called on this clk_hw pointer. It is restored
+ * if registration fails, and when the clock is unregistered unless the init
+ * data was on the stack of the registering function. The clk_hw can then be
+ * registered again as long as the init data it points to is still valid.
  */
 struct clk_hw {
 	struct clk_core *core;
Signature · 1 line
2.56.0
#2 lore raw

PATCH v3 2/3 clk: tests: add tests for registering a clk_hw again

In reply to Joshua Covington

Add tests that a statically allocated clk_hw can be registered again after it has been unregistered and after its registration has failed, that a parent referenced through clk_parent_data.hw is treated as an orphan after it has been unregistered, then reparented once it is registered again, and that init data on the stack of the registering function is not handed back on unregistration.

---
 drivers/clk/clk_test.c | 191 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 191 insertions(+)
drivers/clk/clk_test.c +191 -0
index 0505ed357b..0b6fa518cf 100644
@@ -7,6 +7,7 @@
 #include <linux/clk/clk-conf.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/sched/task_stack.h>
 #include <linux/units.h>
 
 /* Needed for clk_hw_get_clk() */
@@ -3876,6 +3877,195 @@ static struct kunit_suite clk_parse_clkspec_test_suite = {
 	.test_cases = clk_parse_clkspec_test_cases,
 };
 
+/*
+ * Allocate init data that outlives the registering function, as the init data
+ * of a statically allocated clk_hw does.
+ */
+static struct clk_init_data *
+clk_register_again_init(struct kunit *test, const char *name,
+			const struct clk_ops *ops, struct clk_hw *parent)
+{
+	struct clk_init_data *init;
+	const struct clk_hw **parents;
+
+	init = kunit_kzalloc(test, sizeof(*init), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, init);
+	init->name = name;
+	init->ops = ops;
+
+	if (parent) {
+		parents = kunit_kzalloc(test, sizeof(*parents), GFP_KERNEL);
+		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parents);
+		parents[0] = parent;
+		init->parent_hws = parents;
+		init->num_parents = 1;
+	}
+
+	return init;
+}
+
+/*
+ * Test that a statically allocated clk_hw can be registered again after it
+ * has been unregistered, and that unregistering returns it to the state it
+ * had before the first registration.
+ */
+static void clk_register_again_after_unregister(struct kunit *test)
+{
+	struct clk_dummy_context *ctx;
+	struct clk_init_data *init;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	init = clk_register_again_init(test, "reregister-clk",
+				       &clk_dummy_rate_ops, NULL);
+	ctx->rate = DUMMY_CLOCK_INIT_RATE;
+	ctx->hw.init = init;
+
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &ctx->hw), 0);
+	KUNIT_EXPECT_NULL(test, ctx->hw.init);
+
+	clk_hw_unregister(&ctx->hw);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->hw.init, init);
+	KUNIT_EXPECT_NULL(test, ctx->hw.core);
+	KUNIT_EXPECT_NULL(test, ctx->hw.clk);
+
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &ctx->hw), 0);
+	KUNIT_EXPECT_STREQ(test, clk_hw_get_name(&ctx->hw), "reregister-clk");
+	KUNIT_EXPECT_EQ(test, clk_hw_get_rate(&ctx->hw), DUMMY_CLOCK_INIT_RATE);
+
+	clk_hw_unregister(&ctx->hw);
+}
+
+/*
+ * Test that a failed registration leaves the init data in the clk_hw, so
+ * that the clk_hw can be registered once the cause of the failure is gone.
+ */
+static void clk_register_again_after_failure(struct kunit *test)
+{
+	struct clk_dummy_context *ctx1, *ctx2;
+	struct clk_init_data *init;
+
+	ctx1 = kunit_kzalloc(test, sizeof(*ctx1), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx1);
+	ctx2 = kunit_kzalloc(test, sizeof(*ctx2), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx2);
+	init = clk_register_again_init(test, "reregister-dup",
+				       &clk_dummy_rate_ops, NULL);
+	ctx1->hw.init = init;
+	ctx2->hw.init = init;
+
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &ctx1->hw), 0);
+
+	/* A second clock with the same name must fail to register. */
+	KUNIT_EXPECT_EQ(test, clk_hw_register(NULL, &ctx2->hw), -EEXIST);
+	KUNIT_EXPECT_PTR_EQ(test, ctx2->hw.init, init);
+	KUNIT_EXPECT_NULL(test, ctx2->hw.core);
+	KUNIT_EXPECT_NULL(test, ctx2->hw.clk);
+
+	clk_hw_unregister(&ctx1->hw);
+
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &ctx2->hw), 0);
+	clk_hw_unregister(&ctx2->hw);
+}
+
+/*
+ * Test that a clk_parent_data.hw reference to a clk_hw that has been
+ * unregistered is treated as an orphan rather than resolved to the clk_core
+ * of the previous registration, and that the clock is reparented once the
+ * parent is registered again.
+ */
+static void clk_register_again_parent_hw_orphan(struct kunit *test)
+{
+	struct clk_init_data *parent_init, *child_init;
+	struct clk_dummy_context *parent;
+	struct clk_hw *child;
+
+	parent = kunit_kzalloc(test, sizeof(*parent), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
+	child = kunit_kzalloc(test, sizeof(*child), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, child);
+
+	parent_init = clk_register_again_init(test, "reregister-parent",
+					      &clk_dummy_rate_ops, NULL);
+	child_init = clk_register_again_init(test, "reregister-child",
+					     &clk_dummy_single_parent_ops,
+					     &parent->hw);
+	parent->rate = DUMMY_CLOCK_INIT_RATE;
+	parent->hw.init = parent_init;
+	child->init = child_init;
+
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &parent->hw), 0);
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, child), 0);
+	KUNIT_EXPECT_PTR_EQ(test, clk_hw_get_parent(child), &parent->hw);
+
+	clk_hw_unregister(child);
+	clk_hw_unregister(&parent->hw);
+
+	/*
+	 * Set the init data again so that this test only depends on the
+	 * parent reference, not on the init data being restored.
+	 */
+	parent->hw.init = parent_init;
+	child->init = child_init;
+
+	/* The child comes back first: its parent must be an orphan. */
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, child), 0);
+	KUNIT_EXPECT_NULL(test, clk_hw_get_parent(child));
+
+	/* Once the parent is back, the child must be reparented to it. */
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &parent->hw), 0);
+	KUNIT_EXPECT_PTR_EQ(test, clk_hw_get_parent(child), &parent->hw);
+	KUNIT_EXPECT_EQ(test, clk_hw_get_rate(child), DUMMY_CLOCK_INIT_RATE);
+
+	clk_hw_unregister(child);
+	clk_hw_unregister(&parent->hw);
+}
+
+/*
+ * Test that init data on the stack of the registering function is not
+ * handed back when the clock is unregistered, as it would point to a stack
+ * frame that is gone by then.
+ */
+static void clk_register_again_stack_init(struct kunit *test)
+{
+	struct clk_init_data init = {
+		.name = "reregister-stack",
+		.ops = &clk_dummy_rate_ops,
+	};
+	struct clk_dummy_context *ctx;
+
+	if (!object_is_on_stack(&init))
+		kunit_skip(test, "init data is not on the stack");
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
+	ctx->hw.init = &init;
+
+	KUNIT_ASSERT_EQ(test, clk_hw_register(NULL, &ctx->hw), 0);
+	clk_hw_unregister(&ctx->hw);
+
+	KUNIT_EXPECT_NULL(test, ctx->hw.init);
+	KUNIT_EXPECT_NULL(test, ctx->hw.core);
+	KUNIT_EXPECT_NULL(test, ctx->hw.clk);
+}
+
+static struct kunit_case clk_register_again_test_cases[] = {
+	KUNIT_CASE(clk_register_again_after_unregister),
+	KUNIT_CASE(clk_register_again_after_failure),
+	KUNIT_CASE(clk_register_again_parent_hw_orphan),
+	KUNIT_CASE(clk_register_again_stack_init),
+	{}
+};
+
+/*
+ * Test suite for registering a clk_hw again after it has been unregistered
+ * or after its registration has failed.
+ */
+static struct kunit_suite clk_register_again_test_suite = {
+	.name = "clk-register-again-test",
+	.test_cases = clk_register_again_test_cases,
+};
+
 kunit_test_suites(
 	&clk_assigned_rates_suite,
 	&clk_assigned_sscs_suite,
@@ -3892,6 +4082,7 @@ kunit_test_suites(
 	&clk_range_test_suite,
 	&clk_range_maximize_test_suite,
 	&clk_range_minimize_test_suite,
+	&clk_register_again_test_suite,
 	&clk_register_clk_parent_data_of_suite,
 	&clk_register_clk_parent_data_device_suite,
 	&clk_single_parent_mux_test_suite,
Signature · 1 line
2.56.0
#3 lore raw

PATCH v3 3/3 clk: rp1: reset cached rates on probe

In reply to Joshua Covington

The clock descriptors are static, so their cached rates survive when RP1 is unbound and bound again. The "varsrc" clocks then report the rate of the previous probe, and the audio PLL and I2S clocks start with stale cached values.

Reset the cached rates before registering the clocks, so that a second probe starts from the same state as the first.

Fixes 6486341721a2 ("clk: rp1: Add support for clocks provided by RP1")
---
 drivers/clk/clk-rp1.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/clk/clk-rp1.c +6 -0
index 1dd276dd2b..60037b19db 100644
@@ -2416,6 +2416,12 @@ static int rp1_clk_probe(struct platform_device *pdev)
 	clockman->onecell.num = asize;
 	hws = clockman->onecell.hws;
 
+	for (i = 0; i < asize; i++) {
+		desc = clk_desc_array[i];
+		if (desc)
+			desc->cached_rate = 0;
+	}
+
 	for (i = 0; i < asize; i++) {
 		desc = clk_desc_array[i];
 		if (desc && desc->clk_register && desc->data)
Signature · 1 line
2.56.0
maintainer Reviewed-by on v3 3/3
#4 lore raw

Re: [PATCH v3 3/3] clk: rp1: reset cached rates on probe

Hi Joshua,

Quoted 21 lines · Joshua Covington

The clock descriptors are static, so their cached rates survive when RP1 is unbound and bound again. The "varsrc" clocks then report the rate of the previous probe, and the audio PLL and I2S clocks start with stale cached values.

Reset the cached rates before registering the clocks, so that a second probe starts from the same state as the first.

Fixes 6486341721a2 ("clk: rp1: Add support for clocks provided by RP1")
---
 drivers/clk/clk-rp1.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/clk/clk-rp1.c +0 -0
index 1dd276dd2b..60037b19db 100644
@@ -2416,6 +2416,12 @@ static int rp1_clk_probe(struct platform_device *pdev)
 	clockman->onecell.num = asize;
 	hws = clockman->onecell.hws;

drivers/clk/clk-rp1.c static int rp1_clk_probe(struct platform_device *pdev) · line 2427

Quoted 10 lines
drivers/clk/clk-rp1.c +6 -0
 
+	for (i = 0; i < asize; i++) {
+		desc = clk_desc_array[i];
+		if (desc)
+			desc->cached_rate = 0;
+	}
+
 	for (i = 0; i < asize; i++) {
 		desc = clk_desc_array[i];
 		if (desc && desc->clk_register && desc->data)

Thanks for your patch!

Quoted 3 lines
Signature · 1 line
2.56.0