[PATCH 1/2] clk: Introduce kunit wrapper around clk_hw_init_rate_request

From: Maxime Ripard
Date: Fri Jul 21 2023 - 03:09:53 EST


Some kunit tests are meant to test the behaviour of providers (and
most notably __clk_determine_rate()), but don't run those functions with
the clk prepare_lock held which results in lockdep warning.

clk_hw_init_rate_request is one of the functions being executed from a
test which should have the lock held. Let's introduce a wrapper around
it meant to be used only by kunit tests.

Reported-by: Guenter Roeck <linux@xxxxxxxxxxxx>
Link: https://lore.kernel.org/linux-clk/2b594e50-2bbf-4a2d-88e6-49fc39f3957a@xxxxxxxxxxxx/
Reported-by: kernel test robot <yujie.liu@xxxxxxxxx>
Link: https://lore.kernel.org/oe-lkp/202301310919.b9d56ee3-yujie.liu@xxxxxxxxx
Fixes: 262ca38f4b6e ("clk: Stop forwarding clk_rate_requests to the parent")
Signed-off-by: Maxime Ripard <mripard@xxxxxxxxxx>
---
drivers/clk/clk.c | 26 ++++++++++++++++++++++++++
drivers/clk/clk_test.c | 2 +-
include/linux/clk-provider.h | 11 +++++++++++
3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index c249f9791ae8..8ee9bd02af76 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -6,6 +6,7 @@
* Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
*/

+#include <kunit/test-bug.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/clk/clk-conf.h>
@@ -1556,6 +1557,31 @@ void clk_hw_init_rate_request(const struct clk_hw *hw,
}
EXPORT_SYMBOL_GPL(clk_hw_init_rate_request);

+#if IS_ENABLED(CONFIG_KUNIT)
+/**
+ * clk_kunit_init_rate_request - Initializes a clk_rate_request
+ * @hw: the clk for which we want to submit a rate request
+ * @req: the clk_rate_request structure we want to initialise
+ * @rate: the rate which is to be requested
+ *
+ * Initializes a clk_rate_request structure to submit to
+ * __clk_determine_rate() or similar functions. Only usable in kunit
+ * test contexts, use clk_hw_init_rate_request() otherwise.
+ */
+void clk_kunit_init_rate_request(const struct clk_hw *hw,
+ struct clk_rate_request *req,
+ unsigned long rate)
+{
+ if (!kunit_get_current_test())
+ return;
+
+ clk_prepare_lock();
+ clk_hw_init_rate_request(hw, req, rate);
+ clk_prepare_unlock();
+}
+EXPORT_SYMBOL_GPL(clk_kunit_init_rate_request);
+#endif
+
/**
* clk_hw_forward_rate_request - Forwards a clk_rate_request to a clock's parent
* @hw: the original clock that got the rate request
diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index a154ec9d0111..a64f7036baa3 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -2230,7 +2230,7 @@ static void clk_leaf_mux_set_rate_parent_determine_rate(struct kunit *test)
rate = clk_get_rate(clk);
KUNIT_ASSERT_EQ(test, rate, DUMMY_CLOCK_RATE_1);

- clk_hw_init_rate_request(hw, &req, DUMMY_CLOCK_RATE_2);
+ clk_kunit_init_rate_request(hw, &req, DUMMY_CLOCK_RATE_2);

ret = __clk_determine_rate(hw, &req);
KUNIT_ASSERT_EQ(test, ret, 0);
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 0f0cd01906b4..efdebfa3fce9 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -67,6 +67,17 @@ struct clk_rate_request {
void clk_hw_init_rate_request(const struct clk_hw *hw,
struct clk_rate_request *req,
unsigned long rate);
+#if IS_ENABLED(CONFIG_KUNIT)
+void clk_kunit_init_rate_request(const struct clk_hw *hw,
+ struct clk_rate_request *req,
+ unsigned long rate);
+#else
+static inline void clk_kunit_init_rate_request(const struct clk_hw *hw,
+ struct clk_rate_request *req,
+ unsigned long rate)
+{
+}
+#endif
void clk_hw_forward_rate_request(const struct clk_hw *core,
const struct clk_rate_request *old_req,
const struct clk_hw *parent,

--
2.41.0