Skip to contentSkip to conversation

usb: dwc3: core: Use IS_ERR_VALUE() for ref_clk rate check

PATCH In review Jonas Karlman 3 participants 8 messages Feb 9 – Feb 10

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: dwc3: core: Use IS_ERR_VALUE() for ref_clk rate check

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

---
 drivers/usb/dwc3/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/dwc3/core.c +1 -1
index 6f22b9232bad..5bfca580824a 100644
@@ -149,7 +149,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc)
 
 	if (dwc->ref_clk) {
 		rate = clk_get_rate(dwc->ref_clk);
-		if (!rate)
+		if (IS_ERR_VALUE(rate) || !rate)
 			return;
 		period = NSEC_PER_SEC / rate;
 	} else {
Signature · 1 line
2.52.0
#1 lore raw
Quoted 4 lines · Jonas Karlman

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Quoted 8 lines

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

I wonder if it would be better to fix the API discrepancy . Could you try that instead ?

#2 lore raw
Quoted 8 lines · Marek Vasut
Quoted 7 lines · Jonas Karlman

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Quoted 7 lines
Quoted 5 lines

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

I wonder if it would be better to fix the API discrepancy . Could you try that instead ?

Which API discrepancy? clk_get_rate behaves the way it behaves intentionally and there's some fairly long and recent threads about that resulting from attepmts to change that.

Signature · 1 line
Tom
#3 lore raw
Quoted 13 lines · Tom Rini
Quoted 12 lines · Marek Vasut
Quoted 11 lines · Jonas Karlman

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I wonder if it would be better to fix the API discrepancy . Could you try that instead ?

Which API discrepancy? clk_get_rate behaves the way it behaves intentionally and there's some fairly long and recent threads about that resulting from attepmts to change that.

Align clk_get_rate() between U-Boot and Linux kind of discrepancy .

#4 lore raw
Quoted 14 lines · Marek Vasut
Quoted 13 lines · Tom Rini
Quoted 12 lines · Marek Vasut
Quoted 11 lines · Jonas Karlman

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I wonder if it would be better to fix the API discrepancy . Could you try that instead ?

Which API discrepancy? clk_get_rate behaves the way it behaves intentionally and there's some fairly long and recent threads about that resulting from attepmts to change that.

Align clk_get_rate() between U-Boot and Linux kind of discrepancy .

That's not in the current list of options. Please look at the various threads in the past 6-8 months (and some reverts about series that tried this). That was my initial opinion too, FWIW.

Signature · 1 line
Tom
#5 lore raw
Quoted 19 lines · Tom Rini
Quoted 18 lines · Marek Vasut
Quoted 17 lines · Tom Rini
Quoted 15 lines · Marek Vasut
Quoted 4 lines · Jonas Karlman

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Quoted 8 lines

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

I wonder if it would be better to fix the API discrepancy . Could you try that instead ?

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

Which API discrepancy? clk_get_rate behaves the way it behaves intentionally and there's some fairly long and recent threads about that resulting from attepmts to change that.

Align clk_get_rate() between U-Boot and Linux kind of discrepancy .

That's not in the current list of options. Please look at the various threads in the past 6-8 months (and some reverts about series that tried this). That was my initial opinion too, FWIW.

Can you please elaborate WHY clk_get_rate() cannot behave the same way between U-Boot and Linux ? It is confusing to users and it is a source of bugs, this should be fixed.

#6 lore raw
Quoted 23 lines · Marek Vasut
Quoted 22 lines · Tom Rini
Quoted 21 lines · Marek Vasut
Quoted 20 lines · Tom Rini
Quoted 8 lines · Marek Vasut
Quoted 7 lines · Jonas Karlman

clk_get_rate() in U-Boot is documented to return clock rate on success, 0 for invalid clock and -ve error code for other errors. This differ slightly from Linux where only >= 0 is returned from clk_get_rate().

Some clock drivers take advantage of this difference and may return -ve error code for clocks not fully supported in U-Boot.

Quoted 7 lines
Quoted 5 lines

Use IS_ERR_VALUE() to check for an error code in addition to current invalid clock check to fix broken and unpredicted behavior when clock driver returns a -ve error code for the ref_clk.

I wonder if it would be better to fix the API discrepancy . Could you try that instead ?

Which API discrepancy? clk_get_rate behaves the way it behaves intentionally and there's some fairly long and recent threads about that resulting from attepmts to change that.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Align clk_get_rate() between U-Boot and Linux kind of discrepancy .

That's not in the current list of options. Please look at the various threads in the past 6-8 months (and some reverts about series that tried this). That was my initial opinion too, FWIW.

Can you please elaborate WHY clk_get_rate() cannot behave the same way between U-Boot and Linux ? It is confusing to users and it is a source of bugs, this should be fixed.

I think main issue is that U-Boot != Linux, and in U-Boot we can disable subsystems such as CLK, and the normal U-Boot way to signal that is to return -ENOSYS from functions, what clk_get_rate() does when disabled.

Other clk_*() related functions also differs from U-Boot and Linux, some only exists in U-Boot, others behave differently.

Last attempt at changing clk_get_rate() behavior in U-Boot I gave a NAK on the Rockchip driver side. I am not fully against changing behavior of clk_get_rate(), but the driver ops should continue to work the same. There is a difference between an unsupported and an invalid clock.

Something like below, and similar for other clk ops, could possible be fine if someone really want to align clk_*() funcs with Linux.

Back to this patch, I will send other patches to implement the missing/ unsupported dwc3 ref clocks on Rockchip, this patch is still valid and matches current and documented behavior for clk_get_rate() in U-Boot and prevent lockups or crashes when ref clk is missing/unsupported.

drivers/clk/clk-uclass.c +11 -2
index 748359de287b..4603ecef82b5 100644
@@ -485,6 +485,7 @@ int clk_request(struct udevice *dev, struct clk *clk)
 ulong clk_get_rate(struct clk *clk)
 {
 	const struct clk_ops *ops;
+	ulong rate;
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))
@@ -492,9 +493,17 @@ ulong clk_get_rate(struct clk *clk)
 	ops = clk_dev_ops(clk->dev);
 
 	if (!ops->get_rate)
-		return -ENOSYS;
+		rate = -ENOSYS;
+	else
+		rate = ops->get_rate(clk);
+
+	if (IS_ERR_VALUE(rate)) {
+		dm_warn("clk_get_rate(clk=%p, id=%lu) failed: %ld\n",
+			clk, clk->id, (long)rate);
+		return 0;
+	}
 
-	return ops->get_rate(clk);
+	return rate;
 }
 
 struct clk *clk_get_parent(struct clk *clk)

Regards,
Jonas

#7 lore raw

On 2/10/26 1:52 PM, Jonas Karlman wrote:

[...]

Quoted 3 lines

I think main issue is that U-Boot != Linux, and in U-Boot we can disable subsystems such as CLK, and the normal U-Boot way to signal that is to return -ENOSYS from functions, what clk_get_rate() does when disabled.

Sure, but if the function is called the same, people will make the assumption that the function return value handling is the same. So either the function has to be renamed and maybe a wrapper added that makes it linux-compatible, or simply make the function linux-compatible.

Quoted 2 lines

Other clk_*() related functions also differs from U-Boot and Linux, some only exists in U-Boot, others behave differently.

Which is really not good.

Quoted 27 lines

Last attempt at changing clk_get_rate() behavior in U-Boot I gave a NAK on the Rockchip driver side. I am not fully against changing behavior of clk_get_rate(), but the driver ops should continue to work the same. There is a difference between an unsupported and an invalid clock.

Something like below, and similar for other clk ops, could possible be fine if someone really want to align clk_*() funcs with Linux.

Back to this patch, I will send other patches to implement the missing/ unsupported dwc3 ref clocks on Rockchip, this patch is still valid and matches current and documented behavior for clk_get_rate() in U-Boot and prevent lockups or crashes when ref clk is missing/unsupported.

drivers/clk/clk-uclass.c +1 -0
index 748359de287b..4603ecef82b5 100644
@@ -485,6 +485,7 @@ int clk_request(struct udevice *dev, struct clk *clk)
  ulong clk_get_rate(struct clk *clk)
  {
  	const struct clk_ops *ops;
+	ulong rate;
 
  	debug("%s(clk=%p)\n", __func__, clk);
  	if (!clk_valid(clk))

drivers/clk/clk-uclass.c ulong clk_get_rate(struct clk *clk) · line 496

Quoted 5 lines
drivers/clk/clk-uclass.c +1 -1
@@ -492,9 +493,17 @@ ulong clk_get_rate(struct clk *clk)
  	ops = clk_dev_ops(clk->dev);
 
  	if (!ops->get_rate)
-		return -ENOSYS;
+		rate = -ENOSYS;

This would have to be PTR_ERR() , right ?

Quoted 14 lines
drivers/clk/clk-uclass.c +9 -1
+	else
+		rate = ops->get_rate(clk);
+
+	if (IS_ERR_VALUE(rate)) {
+		dm_warn("clk_get_rate(clk=%p, id=%lu) failed: %ld\n",
+			clk, clk->id, (long)rate);
+		return 0;
+	}
 
-	return ops->get_rate(clk);
+	return rate;
  }
 
  struct clk *clk_get_parent(struct clk *clk)

[...]