mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joshua Covington <joshuacov@gmail.com>
To: Stephen Boyd <sboyd@kernel.org>, Brian Masney <masneyb@kernel.org>
Cc: Brian Masney <bmasney+clk@redhat.com>,
	Jerome Brunet <jbrunet+clk@baylibre.com>,
	Andrea della Porta <andrea.porta@suse.com>,
	Florian Fainelli <florian.fainelli@broadcom.com>,
	Sylwester Nawrocki <s.nawrocki@samsung.com>,
	linux-clk@vger.kernel.org, linux-rpi-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	Joshua Covington <joshuacov@gmail.com>
Subject: [PATCH v3 2/3] clk: tests: add tests for registering a clk_hw again
Date: Tue, 24 Nov 2026 23:17:50 +0000	[thread overview]
Message-ID: <20261124231751.83938-3-joshuacov@gmail.com> (raw)
In-Reply-To: <20261124231751.83938-1-joshuacov@gmail.com>

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.

Signed-off-by: Joshua Covington <joshuacov@gmail.com>
---
 drivers/clk/clk_test.c | 191 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 191 insertions(+)

diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index 0505ed357b..0b6fa518cf 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -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,
-- 
2.56.0


  parent reply	other threads:[~2026-11-24 23:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 23:17 [PATCH v3 0/3] clk: allow a clk_hw to be registered again after unregistering it Joshua Covington
2026-11-24 23:17 ` [PATCH v3 1/3] " Joshua Covington
2026-11-24 23:17 ` Joshua Covington [this message]
2026-11-24 23:17 ` [PATCH v3 3/3] clk: rp1: reset cached rates on probe Joshua Covington

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=20261124231751.83938-3-joshuacov@gmail.com \
    --to=joshuacov@gmail.com \
    --cc=andrea.porta@suse.com \
    --cc=bmasney+clk@redhat.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=jbrunet+clk@baylibre.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=masneyb@kernel.org \
    --cc=s.nawrocki@samsung.com \
    --cc=sboyd@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®