Skip to contentSkip to conversation

clk: Don't write error code into divider register

PATCH Applied Peter De Schrijver 2 participants 5 messages Jul 25, 2017 – Sep 1, 2017 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: Don't write error code into divider register

From: Alex Frid <[email protected]>

Add a check for error returned by divider value calculation to avoid writing error code into hw register.

---
 drivers/clk/clk-divider.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clk/clk-divider.c +4 -2
index 9bb472c..4ed516c 100644
@@ -385,12 +385,14 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
 				unsigned long parent_rate)
 {
 	struct clk_divider *divider = to_clk_divider(hw);
-	unsigned int value;
+	int value;
 	unsigned long flags = 0;
 	u32 val;
 
 	value = divider_get_val(rate, parent_rate, divider->table,
 				divider->width, divider->flags);
+	if (value < 0)
+		return value;
 
 	if (divider->lock)
 		spin_lock_irqsave(divider->lock, flags);
@@ -403,7 +405,7 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
 		val = clk_readl(divider->reg);
 		val &= ~(div_mask(divider->width) << divider->shift);
 	}
-	val |= value << divider->shift;
+	val |= (u32)value << divider->shift;
 	clk_writel(val, divider->reg);
 
 	if (divider->lock)
Signature · 1 line
1.9.1
#1 lore raw
Quoted 9 lines · Peter De Schrijver

From: Alex Frid <[email protected]>

Add a check for error returned by divider value calculation to avoid writing error code into hw register.

---

Fixes bca9690b9426 ("clk: divider: Make generic for usage elsewhere")

perhaps?

Also, curious how this got triggered? Presumably round_rate would have failed before a set_rate call with something invalid came through so that's why nobody has reported anything so far.

Quoted 9 lines
 drivers/clk/clk-divider.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clk/clk-divider.c +0 -0
index 9bb472c..4ed516c 100644
@@ -385,12 +385,14 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
 				unsigned long parent_rate)

drivers/clk/clk-divider.c static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, · line 394

Quoted 10 lines
drivers/clk/clk-divider.c +2 -1
 {
 	struct clk_divider *divider = to_clk_divider(hw);
-	unsigned int value;
+	int value;
 	unsigned long flags = 0;
 	u32 val;
 
 	value = divider_get_val(rate, parent_rate, divider->table,
 				divider->width, divider->flags);
+	if (value < 0)

Perhaps value should be an s32 if we're doing a writel with it shifted so much. And then, we could declare it as a u32 and test it with

	if ((s32)value < 0)

to see if there was an error. Sort of annoying that we've limited the available space of divider_get_val() by combining the value with the error code number space. We may want to not do that in case people have huge dividers.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw
Quoted 11 lines · Stephen Boyd
Quoted 9 lines · Peter De Schrijver

From: Alex Frid <[email protected]>

Add a check for error returned by divider value calculation to avoid writing error code into hw register.

---

Quoted 8 lines
Fixes bca9690b9426 ("clk: divider: Make generic for usage elsewhere")

perhaps?

Also, curious how this got triggered? Presumably round_rate would have failed before a set_rate call with something invalid came through so that's why nobody has reported anything so far.

I don't really know. Aleks do you remember?

Quoted 24 lines
Quoted 9 lines
 drivers/clk/clk-divider.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clk/clk-divider.c +0 -0
index 9bb472c..4ed516c 100644
@@ -385,12 +385,14 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
 				unsigned long parent_rate)

drivers/clk/clk-divider.c static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, · line 394

Quoted 10 lines
drivers/clk/clk-divider.c +2 -1
 {
 	struct clk_divider *divider = to_clk_divider(hw);
-	unsigned int value;
+	int value;
 	unsigned long flags = 0;
 	u32 val;
 
 	value = divider_get_val(rate, parent_rate, divider->table,
 				divider->width, divider->flags);
+	if (value < 0)

Perhaps value should be an s32 if we're doing a writel with it shifted so much. And then, we could declare it as a u32 and test it with

Quoted 7 lines
	if ((s32)value < 0)

to see if there was an error. Sort of annoying that we've limited the available space of divider_get_val() by combining the value with the error code number space. We may want to not do that in case people have huge dividers.

Yes. It would have been better to return the divider in a separate variable. There seem to be few users though, only 6.

Peter.

Quoted 3 lines
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#3 lore raw
Quoted 14 lines · Peter De Schrijver
Quoted 13 lines · Stephen Boyd
Quoted 9 lines · Peter De Schrijver

From: Alex Frid <[email protected]>

Add a check for error returned by divider value calculation to avoid writing error code into hw register.

---

Fixes bca9690b9426 ("clk: divider: Make generic for usage elsewhere")
Quoted 9 lines
Quoted 6 lines

perhaps?

Also, curious how this got triggered? Presumably round_rate would have failed before a set_rate call with something invalid came through so that's why nobody has reported anything so far.

I don't really know. Aleks do you remember?

Still wondering.

Quoted 10 lines
Quoted 7 lines
	if ((s32)value < 0)

to see if there was an error. Sort of annoying that we've limited the available space of divider_get_val() by combining the value with the error code number space. We may want to not do that in case people have huge dividers.

Yes. It would have been better to return the divider in a separate variable. There seem to be few users though, only 6.

Patches welcome.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Applied
#4 lore raw
In reply to Peter De Schrijver
Quoted 9 lines · Peter De Schrijver

From: Alex Frid <[email protected]>

Add a check for error returned by divider value calculation to avoid writing error code into hw register.

---

Applied to clk-next

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project