* [PATCH v2] clk: tests: Add tests for clk lookup by name
@ 2025-10-15 9:06 Chen-Yu Tsai
2025-10-27 20:26 ` Brian Masney
2025-11-11 1:29 ` Stephen Boyd
0 siblings, 2 replies; 3+ messages in thread
From: Chen-Yu Tsai @ 2025-10-15 9:06 UTC (permalink / raw)
To: Stephen Boyd
Cc: Chen-Yu Tsai, linux-clk, linux-arm-kernel, linux-kernel,
Brian Masney
Clk lookup (by name) recently gained some performance improvements at
the expense of more complexity within the lookup code.
To make sure that this works as intended and doesn't break, add some
basic tests for this part of the CCF.
A new "clk_hw_lookup()" function is added purely for running kunit
tests.
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
---
Changes since v1:
- Added missing prepare lock/unlock
- Switched to EXPORT_SYMBOL_IF_KUNIT and VISIBLE_IF_KUNIT kunit
visibility macros for consistency (Brian)
This probably doesn't make much difference except that the symbol is
now in the EXPORT_SYMBOL_IF_KUNIT namespace
---
drivers/clk/clk.c | 18 ++++++++++++
drivers/clk/clk.h | 4 +++
drivers/clk/clk_test.c | 67 +++++++++++++++++++++++++++++++++++++++++-
3 files changed, 88 insertions(+), 1 deletion(-)
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 85d2f2481acf..b39ad8944a06 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -10,6 +10,7 @@
#include <linux/clkdev.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
+#include <linux/compiler_attributes.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/hashtable.h>
@@ -25,6 +26,8 @@
#include <linux/string.h>
#include <linux/stringhash.h>
+#include <kunit/visibility.h>
+
#include "clk.h"
static DEFINE_SPINLOCK(enable_lock);
@@ -778,6 +781,21 @@ struct clk *__clk_lookup(const char *name)
return !core ? NULL : core->hw->clk;
}
+/* This is only provided for kunit tests to test the core lookup functions. */
+#if IS_ENABLED(CONFIG_CLK_KUNIT_TEST)
+VISIBLE_IF_KUNIT struct clk_hw * __maybe_unused __must_check clk_hw_lookup(const char *name)
+{
+ struct clk_core *core;
+
+ clk_prepare_lock();
+ core = clk_core_lookup(name);
+ clk_prepare_unlock();
+
+ return !core ? NULL : core->hw;
+}
+EXPORT_SYMBOL_IF_KUNIT(clk_hw_lookup);
+#endif
+
static void clk_core_get_boundaries(struct clk_core *core,
unsigned long *min_rate,
unsigned long *max_rate)
diff --git a/drivers/clk/clk.h b/drivers/clk/clk.h
index 2d801900cad5..a8ed54f5b572 100644
--- a/drivers/clk/clk.h
+++ b/drivers/clk/clk.h
@@ -8,6 +8,10 @@ struct clk_hw;
struct device;
struct of_phandle_args;
+#if IS_ENABLED(CONFIG_CLK_KUNIT_TEST)
+struct clk_hw *clk_hw_lookup(const char *name);
+#endif
+
#if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
struct clk_hw *of_clk_get_hw(struct device_node *np,
int index, const char *con_id);
diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index a268d7b5d4cb..a8989566946b 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -175,6 +175,8 @@ static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
.set_parent = clk_multiple_parents_mux_set_parent,
};
+#define DUMMY_CLK_NAME "test_dummy_rate"
+
static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
{
struct clk_dummy_context *ctx;
@@ -187,7 +189,7 @@ static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
ctx->rate = DUMMY_CLOCK_INIT_RATE;
test->priv = ctx;
- init.name = "test_dummy_rate";
+ init.name = DUMMY_CLK_NAME;
init.ops = ops;
ctx->hw.init = &init;
@@ -3541,6 +3543,67 @@ static struct kunit_suite clk_hw_get_dev_of_node_test_suite = {
.test_cases = clk_hw_get_dev_of_node_test_cases,
};
+/*
+ * Test that clk lookup with a name that is not registered returns NULL.
+ */
+static void clk_lookup_not_registered_clk_returns_NULL(struct kunit *test)
+{
+ KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_lookup(DUMMY_CLK_NAME));
+}
+
+/*
+ * Test that clk lookup with a name that is registered returns the clk.
+ */
+static void clk_lookup_registered_clk_returns_clk(struct kunit *test)
+{
+ struct clk_hw *hw;
+ struct clk_init_data init = {
+ .name = DUMMY_CLK_NAME,
+ .ops = &empty_clk_ops,
+ };
+
+ hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
+
+ hw->init = &init;
+ KUNIT_ASSERT_EQ(test, 0, clk_hw_register_kunit(test, NULL, hw));
+
+ KUNIT_EXPECT_PTR_EQ(test, hw, clk_hw_lookup(DUMMY_CLK_NAME));
+}
+
+/*
+ * Test that clk lookup with a name that was unregistered returns NULL.
+ */
+static void clk_lookup_unregistered_clk_returns_NULL(struct kunit *test)
+{
+ struct clk_hw *hw;
+ struct clk_init_data init = {
+ .name = DUMMY_CLK_NAME,
+ .ops = &empty_clk_ops,
+ };
+
+ hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
+
+ hw->init = &init;
+ KUNIT_ASSERT_FALSE(test, clk_hw_register(NULL, hw));
+
+ clk_hw_unregister(hw);
+
+ KUNIT_EXPECT_PTR_EQ(test, NULL, clk_hw_lookup(DUMMY_CLK_NAME));
+}
+
+static struct kunit_case clk_lookup_test_cases[] = {
+ KUNIT_CASE(clk_lookup_not_registered_clk_returns_NULL),
+ KUNIT_CASE(clk_lookup_registered_clk_returns_clk),
+ KUNIT_CASE(clk_lookup_unregistered_clk_returns_NULL),
+ {}
+};
+
+static struct kunit_suite clk_lookup_test_suite = {
+ .name = "clk-lookup",
+ .test_cases = clk_lookup_test_cases,
+};
kunit_test_suites(
&clk_assigned_rates_suite,
@@ -3560,6 +3623,8 @@ kunit_test_suites(
&clk_register_clk_parent_data_device_suite,
&clk_single_parent_mux_test_suite,
&clk_uncached_test_suite,
+ &clk_lookup_test_suite,
);
MODULE_DESCRIPTION("Kunit tests for clk framework");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
MODULE_LICENSE("GPL v2");
--
2.51.0.788.g6d19910ace-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] clk: tests: Add tests for clk lookup by name
2025-10-15 9:06 [PATCH v2] clk: tests: Add tests for clk lookup by name Chen-Yu Tsai
@ 2025-10-27 20:26 ` Brian Masney
2025-11-11 1:29 ` Stephen Boyd
1 sibling, 0 replies; 3+ messages in thread
From: Brian Masney @ 2025-10-27 20:26 UTC (permalink / raw)
To: Chen-Yu Tsai; +Cc: Stephen Boyd, linux-clk, linux-arm-kernel, linux-kernel
On Wed, Oct 15, 2025 at 05:06:59PM +0800, Chen-Yu Tsai wrote:
> Clk lookup (by name) recently gained some performance improvements at
> the expense of more complexity within the lookup code.
>
> To make sure that this works as intended and doesn't break, add some
> basic tests for this part of the CCF.
>
> A new "clk_hw_lookup()" function is added purely for running kunit
> tests.
>
> Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Reviewed-by: Brian Masney <bmasney@redhat.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] clk: tests: Add tests for clk lookup by name
2025-10-15 9:06 [PATCH v2] clk: tests: Add tests for clk lookup by name Chen-Yu Tsai
2025-10-27 20:26 ` Brian Masney
@ 2025-11-11 1:29 ` Stephen Boyd
1 sibling, 0 replies; 3+ messages in thread
From: Stephen Boyd @ 2025-11-11 1:29 UTC (permalink / raw)
To: Chen-Yu Tsai
Cc: Chen-Yu Tsai, linux-clk, linux-arm-kernel, linux-kernel,
Brian Masney
Quoting Chen-Yu Tsai (2025-10-15 02:06:59)
> Clk lookup (by name) recently gained some performance improvements at
> the expense of more complexity within the lookup code.
>
> To make sure that this works as intended and doesn't break, add some
> basic tests for this part of the CCF.
>
> A new "clk_hw_lookup()" function is added purely for running kunit
> tests.
>
> Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
>
> ---
Urgh I see I missed the new one. Please look at v1 and fix things up and
resend.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-11 1:29 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-15 9:06 [PATCH v2] clk: tests: Add tests for clk lookup by name Chen-Yu Tsai
2025-10-27 20:26 ` Brian Masney
2025-11-11 1:29 ` Stephen Boyd
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).