Skip to contentSkip to conversation

clk: mux: check value returned from clk_mux_val_to_index()

PATCH In review Maksim Kiselev 2 participants 2 messages Sep 5, 2023 – Nov 1, 2023 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: mux: check value returned from clk_mux_val_to_index()

The clk_mux_val_to_index() may return -EINVAL. In this case clk_mux_get_parent() will return a wrong parent index and this will lead to out of bounds error.

E.g. when we register mux clk:

	ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
			   parent_names[clk_mux_get_parent(clk)]);

Let's add a check on the return value of clk_mux_val_to_index() and if it's negative, set parent index to 0.

---
 drivers/clk/clk-mux.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/clk/clk-mux.c +8 -1
index 017f25f7a5..00fe916171 100644
@@ -88,6 +88,7 @@ unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
 u8 clk_mux_get_parent(struct clk *clk)
 {
 	struct clk_mux *mux = to_clk_mux(clk);
+	int index;
 	u32 val;
 
 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
@@ -98,7 +99,13 @@ u8 clk_mux_get_parent(struct clk *clk)
 	val >>= mux->shift;
 	val &= mux->mask;
 
-	return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
+	index = clk_mux_val_to_index(clk, mux->table, mux->flags, val);
+	if (index < 0) {
+		log_err("Could not fetch index\n");
+		index = 0;
+	}
+
+	return index;
 }
 
 static int clk_fetch_parent_index(struct clk *clk,
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 33 lines · Maksim Kiselev

The clk_mux_val_to_index() may return -EINVAL. In this case clk_mux_get_parent() will return a wrong parent index and this will lead to out of bounds error.

E.g. when we register mux clk:

	ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
			   parent_names[clk_mux_get_parent(clk)]);

Let's add a check on the return value of clk_mux_val_to_index() and if it's negative, set parent index to 0.

---
  drivers/clk/clk-mux.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/clk/clk-mux.c +1 -1
index 017f25f7a5..00fe916171 100644
@@ -88,6 +88,7 @@ unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
  u8 clk_mux_get_parent(struct clk *clk)
  {
  	struct clk_mux *mux = to_clk_mux(clk);
+	int index;
  	u32 val;
 
  #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
@@ -98,7 +99,13 @@ u8 clk_mux_get_parent(struct clk *clk)
  	val >>= mux->shift;
  	val &= mux->mask;
 
-	return clk_mux_val_to_index(clk, mux->table, mux->flags, val);

drivers/clk/clk-mux.c u8 clk_mux_get_parent(struct clk *clk) · line 111

Quoted 10 lines
drivers/clk/clk-mux.c +7 -0
+	index = clk_mux_val_to_index(clk, mux->table, mux->flags, val);
+	if (index < 0) {
+		log_err("Could not fetch index\n");
+		index = 0;
+	}
+
+	return index;
  }
 
  static int clk_fetch_parent_index(struct clk *clk,

But can you add something to the commit (or error) message about how this indicates a bug in the driver (e.g. there aren't enough parents to match the hardware).

--Sean