usb: dwc3: core: fix clk_get_rate returning negative value
Subsystems (2)
PATCH 1/1 usb: dwc3: core: fix clk_get_rate returning negative value
Unlike it's Linux counterpart, clk_get_rate can return a negative value, -ve. The driver does not take that into account, stores the rate into an unsigned long, and if clk_get_rate fails, it will take into consideration the actual value and wrongly program the hardware.
E.g. on error -2 (no such entry), the rate will be 18446744073709551614 and this will be subsequently used by the driver to program the DWC3 To fix this, exit the function if the value is negative.
--- drivers/usb/dwc3/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/dwc3/core.c +1 -1
@@ -138,7 +138,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc)if (dwc->ref_clk) { rate =clk_get_rate (dwc->ref_clk); -if (!rate) +if (!rate || (long )rate <0 )return ; period =NSEC_PER_SEC / rate; }else {
Signature · 1 line
2.34.1
Quoted 19 lines · Eugen Hristev
Unlike it's Linux counterpart,
clk_get_ratecan return a negative value, -ve. The driver does not take that into account, stores the rate into an unsigned long, and ifclk_get_ratefails, it will take into consideration the actual value and wrongly program the hardware.
E.g. on error -2 (no such entry), the rate will be 18446744073709551614 and this will be subsequently used by the driver to program the DWC3 To fix this, exit the function if the value is negative.Fixes 6bae0eb5b8bd ("usb: dwc3: Calculate REFCLKPER based on reference clock")Signed-off-by Eugen Hristev <[email protected]>--- drivers/usb/dwc3/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/dwc3/core.c +0
-0
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 140
Quoted 3 lines
drivers/usb/dwc3/core.c +0
-0@@ -138,7 +138,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc)if (dwc->ref_clk) { rate =clk_get_rate (dwc->ref_clk);
This ^ assigns to unsigned long .
Please just change the data type of the $rate to 'long' (drop the unsigned).
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 141
Quoted 2 lines
drivers/usb/dwc3/core.c +1
-1-if (!rate) +if (!rate || (long )rate <0 )
if (rate <= 0) will work too ^
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Eugen Hristev
Unlike it's Linux counterpart,
clk_get_ratecan return a negative value, -ve. The driver does not take that into account, stores the rate into an unsigned long, and ifclk_get_ratefails, it will take into consideration the actual value and wrongly program the hardware.
E.g. on error -2 (no such entry), the rate will be 18446744073709551614 and this will be subsequently used by the driver to program the DWC3 To fix this, exit the function if the value is negative.Fixes 6bae0eb5b8bd ("usb: dwc3: Calculate REFCLKPER based on reference clock")Signed-off-by Eugen Hristev <[email protected]>---
drivers/usb/dwc3/core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 9 lines
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 139
Quoted 7 lines
drivers/usb/dwc3/core.c +0
-0@@ -138,7 +138,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc) if (dwc->ref_clk) { rate = clk_get_rate(dwc->ref_clk);This ^ assigns to unsigned long .
That's right, because clk_get_rate returns an unsigned long
Quoted 2 lines
Please just change the data type of the $rate to 'long' (drop the unsigned).
clk_get_rate returns unsigned long, so it's not normal to assign it to a signed long.
It is not normal that clk_get_rate returns negative values in an unsigned, but oh well, that's how the clock API is in U-boot
Quoted 5 lines
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 141
Quoted 3 lines
drivers/usb/dwc3/core.c +1
-1- if (!rate) + if (!rate || (long)rate < 0)if (rate <= 0) will work too ^
Quoted 26 lines · Eugen Hristev
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Eugen Hristev
Unlike it's Linux counterpart,
clk_get_ratecan return a negative value, -ve. The driver does not take that into account, stores the rate into an unsigned long, and ifclk_get_ratefails, it will take into consideration the actual value and wrongly program the hardware.
E.g. on error -2 (no such entry), the rate will be 18446744073709551614 and this will be subsequently used by the driver to program the DWC3 To fix this, exit the function if the value is negative.Fixes 6bae0eb5b8bd ("usb: dwc3: Calculate REFCLKPER based on reference clock")Signed-off-by Eugen Hristev <[email protected]>---
drivers/usb/dwc3/core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 9 lines
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 139
Quoted 7 lines
drivers/usb/dwc3/core.c +0
-0@@ -138,7 +138,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc) if (dwc->ref_clk) { rate = clk_get_rate(dwc->ref_clk);This ^ assigns to unsigned long .
Quoted 7 lines
That's right, because
clk_get_ratereturns an unsigned longQuoted 2 lines
Please just change the data type of the $rate to 'long' (drop the unsigned).
clk_get_ratereturns unsigned long, so it's not normal to assign it to a signed long.It is not normal that
clk_get_ratereturns negative values in an unsigned, but oh well, that's how the clock API is in U-boot
Ah, I see. Then why not do this diff, and fix any clock driver which returns negative from clk_get_rate() instead ?
include/clk.h +0 -0
@@ -453,8 +453,7 @@ void clk_free(struct clk *clk);* @clk: A clock struct that was previously successfully
requested by
* clk_request/get_by_*().
*
- * Return: clock rate in Hz on success, 0 for invalid clock, or -ve error code
- * for other errors.
+ * Return: clock rate in Hz on success, 0 for invalid clock.
*/
ulong clk_get_rate(struct clk *clk);
Quoted 44 lines · Marek Vasut
Quoted 26 lines · Eugen Hristev
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Eugen Hristev
Unlike it's Linux counterpart,
clk_get_ratecan return a negative value, -ve. The driver does not take that into account, stores the rate into an unsigned long, and ifclk_get_ratefails, it will take into consideration the actual value and wrongly program the hardware.
E.g. on error -2 (no such entry), the rate will be 18446744073709551614 and this will be subsequently used by the driver to program the DWC3 To fix this, exit the function if the value is negative.Fixes 6bae0eb5b8bd ("usb: dwc3: Calculate REFCLKPER based on reference clock")Signed-off-by Eugen Hristev <[email protected]>---
drivers/usb/dwc3/core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 9 lines
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 139
Quoted 7 lines
drivers/usb/dwc3/core.c +0
-0@@ -138,7 +138,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc) if (dwc->ref_clk) { rate = clk_get_rate(dwc->ref_clk);This ^ assigns to unsigned long .
Quoted 7 lines
That's right, because
clk_get_ratereturns an unsigned longQuoted 2 lines
Please just change the data type of the $rate to 'long' (drop the unsigned).
clk_get_ratereturns unsigned long, so it's not normal to assign it to a signed long.It is not normal that
clk_get_ratereturns negative values in an unsigned, but oh well, that's how the clock API is in U-bootAh, I see. Then why not do this diff, and fix any clock driver which returns negative from
clk_get_rate()instead ?include/clk.h +0
-0@@ -453,8 +453,7 @@ void clk_free(struct clk *clk); * @clk: A clock struct that was previously successfullyrequested by
Quoted 9 lines
* clk_request/get_by_*().
*
- * Return: clock rate in Hz on success, 0 for invalid clock, or -ve error code
- * for other errors.
+ * Return: clock rate in Hz on success, 0 for invalid clock. */
ulongclk_get_rate(struct clk *clk);
Because clk_get_rate really returns negative numbers, I cannot update the documentation for it when in fact it still can return errors.
This patch which I sent, fixes the driver from the perspective of 'clk_get_rate is like this, driver should cope with it'
If you disagree with my patch, the the only way forward that I see right now, is to modify the clk_get_rate to no longer return negative numbers as errors. But this would mean modifying the clk subsystem, and it's no longer a simple fix rather a change in the API. (a simple fix which I intended with this patch )
Marek Vasut
NAKQuoted 47 lines · Eugen Hristev
Quoted 46 lines · Marek Vasut
Quoted 26 lines · Eugen Hristev
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Eugen Hristev
Unlike it's Linux counterpart,
clk_get_ratecan return a negative value, -ve. The driver does not take that into account, stores the rate into an unsigned long, and ifclk_get_ratefails, it will take into consideration the actual value and wrongly program the hardware.
E.g. on error -2 (no such entry), the rate will be 18446744073709551614 and this will be subsequently used by the driver to program the DWC3 To fix this, exit the function if the value is negative.Fixes 6bae0eb5b8bd ("usb: dwc3: Calculate REFCLKPER based on reference clock")Signed-off-by Eugen Hristev <[email protected]>---
drivers/usb/dwc3/core.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 9 lines
drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 139
Quoted 7 lines
drivers/usb/dwc3/core.c +0
-0@@ -138,7 +138,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc) if (dwc->ref_clk) { rate = clk_get_rate(dwc->ref_clk);This ^ assigns to unsigned long .
Quoted 7 lines
That's right, because
clk_get_ratereturns an unsigned longQuoted 2 lines
Please just change the data type of the $rate to 'long' (drop the unsigned).
clk_get_ratereturns unsigned long, so it's not normal to assign it to a signed long.It is not normal that
clk_get_ratereturns negative values in an unsigned, but oh well, that's how the clock API is in U-bootAh, I see. Then why not do this diff, and fix any clock driver which returns negative from
clk_get_rate()instead ?include/clk.h +0
-1@@ -453,8 +453,7 @@ void clk_free(struct clk *clk); * @clk: A clock struct that was previously successfully requested by * clk_request/get_by_*(). * - * Return: clock rate in Hz on success, 0 for invalid clock, or -ve
Quoted 8 lines
Quoted 6 lines
error code
- * for other errors.
+ * Return: clock rate in Hz on success, 0 for invalid clock. */
ulongclk_get_rate(struct clk *clk);Because
clk_get_ratereally returns negative numbers, I cannot update the documentation for it when in fact it still can return errors.
Let me make it clear, let's not hack around bugs by introducing known defective code into the USB subsystem.
NAK
Quoted 1 line
This patch which I sent, fixes the driver from the perspective of '
clk_get_rateis like this, driver should cope with it'
No, it just adds a broken workaround which hides the real issue. The real issue should be addressed.
Quoted 1 line
If you disagree with my patch, the the only way forward that I see right now, is to modify the
clk_get_rateto no longer return negative numbers as errors.
Yes please. Just align the API with Linux, that would be even better.
Quoted 1 line
But this would mean modifying the clk subsystem, and it's no longer a simple fix rather a change in the API. (a simple fix which I intended with this patch )
Try and add 'ccflags-y += -Werror -Wsign-conversion' into drivers/clk/Makefile , then run buildman to get all the signed conversion issues, and just fix them. That should be rather a mechanical task.