[2/4] clk: Initialize the clk_rate_request even if clk_core is NULL

Message ID 20221018-clk-range-checks-fixes-v1-2-f3ef80518140@cerno.tech
State New
Headers
Series clk: Rate range improvements |

Commit Message

Maxime Ripard Oct. 18, 2022, 1:52 p.m. UTC
  Since commit c35e84b09776 ("clk: Introduce clk_hw_init_rate_request()"),
users that used to initialize their clk_rate_request by initializing
their local structure now rely on clk_hw_init_rate_request().

This function is backed by clk_core_init_rate_req(), which will skip the
initialization if either the pointer to struct clk_core or to struct
clk_rate_request are NULL.

However, the core->parent pointer might be NULL because the clock is
orphan, and we will thus end up with our local struct clk_rate_request
left untouched.

And since clk_hw_init_rate_request() doesn't return an error, we will
then call a determine_rate variant with that unitialized structure.

In order to avoid this, let's clear our clk_rate_request if the pointer
to it is valid but the pointer to struct clk_core isn't.

Fixes: c35e84b09776 ("clk: Introduce clk_hw_init_rate_request()")
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/clk/clk.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Comments

AngeloGioacchino Del Regno Oct. 18, 2022, 2:35 p.m. UTC | #1
Il 18/10/22 15:52, Maxime Ripard ha scritto:
> Since commit c35e84b09776 ("clk: Introduce clk_hw_init_rate_request()"),
> users that used to initialize their clk_rate_request by initializing
> their local structure now rely on clk_hw_init_rate_request().
> 
> This function is backed by clk_core_init_rate_req(), which will skip the
> initialization if either the pointer to struct clk_core or to struct
> clk_rate_request are NULL.
> 
> However, the core->parent pointer might be NULL because the clock is
> orphan, and we will thus end up with our local struct clk_rate_request
> left untouched.
> 
> And since clk_hw_init_rate_request() doesn't return an error, we will
> then call a determine_rate variant with that unitialized structure.
> 
> In order to avoid this, let's clear our clk_rate_request if the pointer
> to it is valid but the pointer to struct clk_core isn't.
> 
> Fixes: c35e84b09776 ("clk: Introduce clk_hw_init_rate_request()")
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>

Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
  
Stephen Boyd Oct. 28, 2022, 12:09 a.m. UTC | #2
Quoting Maxime Ripard (2022-10-18 06:52:57)
> Since commit c35e84b09776 ("clk: Introduce clk_hw_init_rate_request()"),
> users that used to initialize their clk_rate_request by initializing
> their local structure now rely on clk_hw_init_rate_request().
> 
> This function is backed by clk_core_init_rate_req(), which will skip the
> initialization if either the pointer to struct clk_core or to struct
> clk_rate_request are NULL.
> 
> However, the core->parent pointer might be NULL because the clock is
> orphan, and we will thus end up with our local struct clk_rate_request
> left untouched.
> 
> And since clk_hw_init_rate_request() doesn't return an error, we will
> then call a determine_rate variant with that unitialized structure.
> 
> In order to avoid this, let's clear our clk_rate_request if the pointer
> to it is valid but the pointer to struct clk_core isn't.
> 
> Fixes: c35e84b09776 ("clk: Introduce clk_hw_init_rate_request()")
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> ---

Applied to clk-fixes
  

Patch

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 37d623c7b73b..eb2f9be9b9aa 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -1459,11 +1459,14 @@  static void clk_core_init_rate_req(struct clk_core * const core,
 {
 	struct clk_core *parent;
 
-	if (!core || WARN_ON(!req))
+	if (WARN_ON(!req))
 		return;
 
 	memset(req, 0, sizeof(*req));
 
+	if (!core)
+		return;
+
 	req->rate = rate;
 	clk_core_get_boundaries(core, &req->min_rate, &req->max_rate);