Skip to contentSkip to conversation

clk: uclass fixes and improvements

PATCH In review Samuel Holland 4 participants 27 messages Feb 20, 2023 – Mar 4, 2023 Common clk

lore
Share & tools
lore
#0 lore raw

cover clk: uclass fixes and improvements

This series fixes a few issues found while writing a clk driver for a new SoC (Bouffalo Lab BL808), and adds the new functionality needed to implement a hierarchy of clocks in a single device (without the CCF). The .get_parent hook will be useful on other platforms as well; I plan to use it to implement PLL rate setting on sunxi.

Samuel Holland (6):
clk: Handle error pointers in clk_valid()
clk: Fix error handling in clk_get_rate()
clk: Fix error handling in clk_get_parent()
clk: Fix rate caching in clk_get_parent_rate()
clk: Remove an unneeded check from clk_get_parent_rate()
clk: Add a .get_parent operation

 drivers/clk/clk-uclass.c | 35 ++++++++++++++++-------------------
 include/clk-uclass.h     |  2 ++
 include/clk.h            |  2 +-
 3 files changed, 19 insertions(+), 20 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/6 clk: Handle error pointers in clk_valid()

Some clk uclass functions, such as devm_clk_get() and clk_get_parent(), return error pointers. clk_valid() should not consider these pointers to be valid.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

 include/clk.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/clk.h +1 -1
index d91285235f7..4acb878ec6d 100644
@@ -671,7 +671,7 @@ static inline bool clk_dev_binded(struct clk *clk)
  */
 static inline bool clk_valid(struct clk *clk)
 {
-	return clk && !!clk->dev;
+	return clk && !IS_ERR(clk) && !!clk->dev;
 }
 
 int soc_clk_dump(void);
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 1/6] clk: Handle error pointers in clk_valid()

Quoted 16 lines · Samuel Holland

Some clk uclass functions, such as devm_clk_get() and clk_get_parent(), return error pointers. clk_valid() should not consider these pointers to be valid.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

 include/clk.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/clk.h +0 -0
index d91285235f7..4acb878ec6d 100644

include/clk.h static inline bool clk_dev_binded(struct clk *clk) · line 677

Quoted 8 lines
include/clk.h +1 -1
@@ -671,7 +671,7 @@ static inline bool clk_dev_binded(struct clk *clk)
  */
 static inline bool clk_valid(struct clk *clk)
 {
-	return clk && !!clk->dev;
+	return clk && !IS_ERR(clk) && !!clk->dev;
 }
 
 int soc_clk_dump(void);

Maybe it would be better to avoid the error pointers instead, there should not be that many places where they are returned.

Thanks

Michal

#3 lore raw

Re: [PATCH 1/6] clk: Handle error pointers in clk_valid()

Quoted 24 lines · Michal Suchánek
Quoted 23 lines · Samuel Holland

Some clk uclass functions, such as devm_clk_get() and clk_get_parent(), return error pointers. clk_valid() should not consider these pointers to be valid.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

  include/clk.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/clk.h +1 -1
index d91285235f7..4acb878ec6d 100644
@@ -671,7 +671,7 @@ static inline bool clk_dev_binded(struct clk *clk)
   */
  static inline bool clk_valid(struct clk *clk)
  {
-	return clk && !!clk->dev;
+	return clk && !IS_ERR(clk) && !!clk->dev;
  }
 
Quoted 4 lines

include/clk.h static inline bool clk_dev_binded(struct clk *clk) · line 677

Quoted 1 line
include/clk.h +0 -0
  int soc_clk_dump(void);

Maybe it would be better to avoid the error pointers instead, there should not be that many places where they are returned.

This not quite the right way to phrase this. Instead, I would ask: where are places where the return value is not checked correctly? It's perfectly fine to pass NULL or uninitialized clocks to other clock functions, but it's not OK to ignore errors.

--Sean

#4 lore raw

Re: [PATCH 1/6] clk: Handle error pointers in clk_valid()

Quoted 30 lines · Sean Anderson
Quoted 12 lines · Michal Suchánek
Quoted 11 lines · Samuel Holland

Some clk uclass functions, such as devm_clk_get() and clk_get_parent(), return error pointers. clk_valid() should not consider these pointers to be valid.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

  include/clk.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

include/clk.h static inline bool clk_dev_binded(struct clk *clk) · line 676

Quoted 12 lines
include/clk.h +1 -1
index d91285235f7..4acb878ec6d 100644
@@ -671,7 +671,7 @@ static inline bool clk_dev_binded(struct clk *clk)
   */
  static inline bool clk_valid(struct clk *clk)
  {
-	return clk && !!clk->dev;
+	return clk && !IS_ERR(clk) && !!clk->dev;
  }
  int soc_clk_dump(void);

Maybe it would be better to avoid the error pointers instead, there should not be that many places where they are returned.

Quoted 2 lines

This not quite the right way to phrase this. Instead, I would ask: where are places where the return value is not checked correctly? It's perfectly

Pretty much everywhere where clk_get_parent is used outside of core code absolutely no error checking is done.

Quoted 2 lines

fine to pass NULL or uninitialized clocks to other clock functions, but it's not OK to ignore errors.

Is there some documentation on what is the distinction, specifically?

If there isn't it's meaningless and NULL can be returned on error simplifying the code, thinking about the code, debugging, pretty much everything.

Thanks

Michal

#5 lore raw

Re: [PATCH 1/6] clk: Handle error pointers in clk_valid()

Quoted 31 lines · Michal Suchánek
Quoted 30 lines · Sean Anderson
Quoted 27 lines · Michal Suchánek
Quoted 16 lines · Samuel Holland

Some clk uclass functions, such as devm_clk_get() and clk_get_parent(), return error pointers. clk_valid() should not consider these pointers to be valid.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

  include/clk.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/clk.h +0 -0
index d91285235f7..4acb878ec6d 100644

include/clk.h static inline bool clk_dev_binded(struct clk *clk) · line 676

Quoted 7 lines
include/clk.h +1 -1
@@ -671,7 +671,7 @@ static inline bool clk_dev_binded(struct clk *clk)
   */
  static inline bool clk_valid(struct clk *clk)
  {
-	return clk && !!clk->dev;
+	return clk && !IS_ERR(clk) && !!clk->dev;
  }
  int soc_clk_dump(void);

Maybe it would be better to avoid the error pointers instead, there should not be that many places where they are returned.

Quoted 5 lines
Quoted 2 lines

This not quite the right way to phrase this. Instead, I would ask: where are places where the return value is not checked correctly? It's perfectly

Pretty much everywhere where clk_get_parent is used outside of core code absolutely no error checking is done.

There are 105 uses of ERR_PTR in drivers/clk, mostly in CCF clock registration functions. There is also devm_clk_get() with about 30 callers. Those mostly seem to have reasonable error checking; the error pointer ends up as the driver probe function's return value via PTR_ERR.

Quoted 8 lines
Quoted 2 lines

fine to pass NULL or uninitialized clocks to other clock functions, but it's not OK to ignore errors.

Is there some documentation on what is the distinction, specifically?

If there isn't it's meaningless and NULL can be returned on error simplifying the code, thinking about the code, debugging, pretty much everything.

What should I do for v2? Keep this patch? Convert clk_get_parent() to return NULL instead of ERR_PTR, and hope nobody uses clk_valid() on the pointer returned from devm_clk_get()?

Regards,
Samuel

Reviewed-by
#6 lore raw

Re: [PATCH 1/6] clk: Handle error pointers in clk_valid()

Quoted 35 lines · Samuel Holland
Quoted 34 lines · Michal Suchánek
Quoted 32 lines · Sean Anderson
Quoted 12 lines · Michal Suchánek
Quoted 11 lines · Samuel Holland

Some clk uclass functions, such as devm_clk_get() and clk_get_parent(), return error pointers. clk_valid() should not consider these pointers to be valid.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

  include/clk.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

include/clk.h static inline bool clk_dev_binded(struct clk *clk) · line 676

Quoted 12 lines
include/clk.h +1 -1
index d91285235f7..4acb878ec6d 100644
@@ -671,7 +671,7 @@ static inline bool clk_dev_binded(struct clk *clk)
   */
  static inline bool clk_valid(struct clk *clk)
  {
-	return clk && !!clk->dev;
+	return clk && !IS_ERR(clk) && !!clk->dev;
  }
  int soc_clk_dump(void);

Maybe it would be better to avoid the error pointers instead, there should not be that many places where they are returned.

This not quite the right way to phrase this. Instead, I would ask: where are places where the return value is not checked correctly? It's perfectly

Quoted 7 lines
Quoted 2 lines

Pretty much everywhere where clk_get_parent is used outside of core code absolutely no error checking is done.

There are 105 uses of ERR_PTR in drivers/clk, mostly in CCF clock registration functions. There is also devm_clk_get() with about 30 callers. Those mostly seem to have reasonable error checking; the error pointer ends up as the driver probe function's return value via PTR_ERR.

Yes, there are too many. clk_valid should check both.

Thanks

Michal

#7 lore raw

PATCH 2/6 clk: Fix error handling in clk_get_rate()

In reply to Samuel Holland

log_ret() cannot work with unsigned values, and the assignment to 'ret' incorrectly truncates the rate from long to int.

Fixes 5c5992cb90cf ("clk: Add debugging for return values")

---

 drivers/clk/clk-uclass.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/clk/clk-uclass.c +1 -6
index dc3e9d6a261..78299dbceb2 100644
@@ -471,7 +471,6 @@ void clk_free(struct clk *clk)
 ulong clk_get_rate(struct clk *clk)
 {
 	const struct clk_ops *ops;
-	int ret;
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))
@@ -481,11 +480,7 @@ ulong clk_get_rate(struct clk *clk)
 	if (!ops->get_rate)
 		return -ENOSYS;
 
-	ret = ops->get_rate(clk);
-	if (ret)
-		return log_ret(ret);
-
-	return 0;
+	return ops->get_rate(clk);
 }
 
 struct clk *clk_get_parent(struct clk *clk)
Signature · 1 line
2.39.2
#8 lore raw

Re: [PATCH 2/6] clk: Fix error handling in clk_get_rate()

Hello,

Quoted 23 lines · Samuel Holland

log_ret() cannot work with unsigned values, and the assignment to 'ret' incorrectly truncates the rate from long to int.

Fixes 5c5992cb90cf ("clk: Add debugging for return values")

---

 drivers/clk/clk-uclass.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/clk/clk-uclass.c +0 -1
index dc3e9d6a261..78299dbceb2 100644
@@ -471,7 +471,6 @@ void clk_free(struct clk *clk)
 ulong clk_get_rate(struct clk *clk)
 {
 	const struct clk_ops *ops;
-	int ret;
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))

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

Quoted 10 lines
drivers/clk/clk-uclass.c +1 -5
@@ -481,11 +480,7 @@ ulong clk_get_rate(struct clk *clk)
 	if (!ops->get_rate)
 		return -ENOSYS;
 
-	ret = ops->get_rate(clk);
-	if (ret)
-		return log_ret(ret);
-
-	return 0;
+	return ops->get_rate(clk);
 }

This is generally poor design of the clock stuff in u-boot.

It returns -ERROR for many error conditions, but also 0 for other error conditions.

Some error handling checks for 0, some for errval, some casts to int and checks for <= 0.

I think that using -ERROR for clocks does not make much sense in u-boot.

Even in the kernel the errval checks are pretty much limited to places where integers are used to store page frame numbers or pointers, that is errptr stored in an integer. For adresses it is pretty easy to make sure that the last page is not mapped making the error pointers invalid (although bugs in that part happened too).

For clocks no such guarantee exists. The only apparently invalid clock is 0, and the correct fix is to fix up the clock code to return 0 on error, always. It's a lot of code to fix, though.

If you do not want to fix everything then the correct thing to do is make ret ulong, and check for errval *and* 0.

There is not much point in returning detailed error codes in u-boot, anyway. It's not like there is some userspace that could interpret them. Most errors are logged when they happen if ever, and callers only check if error happened or not.

Thanks

Michal

#9 lore raw

Re: [PATCH 2/6] clk: Fix error handling in clk_get_rate()

Quoted 60 lines · Michal Suchánek

Hello,

Quoted 23 lines · Samuel Holland

log_ret() cannot work with unsigned values, and the assignment to 'ret' incorrectly truncates the rate from long to int.

Fixes 5c5992cb90cf ("clk: Add debugging for return values")

---

  drivers/clk/clk-uclass.c | 7 +------
  1 file changed, 1 insertion(+), 6 deletions(-)
drivers/clk/clk-uclass.c +0 -1
index dc3e9d6a261..78299dbceb2 100644
@@ -471,7 +471,6 @@ void clk_free(struct clk *clk)
  ulong clk_get_rate(struct clk *clk)
  {
  	const struct clk_ops *ops;
-	int ret;
 
  	debug("%s(clk=%p)\n", __func__, clk);
  	if (!clk_valid(clk))

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

Quoted 10 lines
drivers/clk/clk-uclass.c +1 -5
@@ -481,11 +480,7 @@ ulong clk_get_rate(struct clk *clk)
  	if (!ops->get_rate)
  		return -ENOSYS;
 
-	ret = ops->get_rate(clk);
-	if (ret)
-		return log_ret(ret);
-
-	return 0;
+	return ops->get_rate(clk);
  }

This is generally poor design of the clock stuff in u-boot.

It returns -ERROR for many error conditions, but also 0 for other error conditions.

Some error handling checks for 0, some for errval, some casts to int and checks for <= 0.

I think that using -ERROR for clocks does not make much sense in u-boot.

Even in the kernel the errval checks are pretty much limited to places where integers are used to store page frame numbers or pointers, that is errptr stored in an integer. For adresses it is pretty easy to make sure that the last page is not mapped making the error pointers invalid (although bugs in that part happened too).

For clocks no such guarantee exists. The only apparently invalid clock is 0, and the correct fix is to fix up the clock code to return 0 on error, always. It's a lot of code to fix, though.

If you do not want to fix everything then the correct thing to do is make ret ulong, and check for errval *and* 0.

Quoted 8 lines

There is not much point in returning detailed error codes in u-boot, anyway. It's not like there is some userspace that could interpret them. Most errors are logged when they happen if ever, and callers only check if error happened or not.

Thanks

Michal

clk_get_parent is the only place where we return a clock pointer directly. Everywhere else, we have an integer return we can use. This function is different of course because CCF is broken and assumes there is only one canonical struct clk for a logical clock. So it can't initialize a caller-passed struct clk, but instead has to return the one true struct clk.

I agree with Michal here. The signature should really be

int clk_get_parent(struct clk *child, struct clk *parent)

but for now there is no point confusing the rest of the clock subsystem to make it work with error pointers.

--Sean

#10 lore raw

Re: [PATCH 2/6] clk: Fix error handling in clk_get_rate()

Hello,

Quoted 74 lines · Sean Anderson
Quoted 58 lines · Michal Suchánek

Hello,

Quoted 22 lines · Samuel Holland

log_ret() cannot work with unsigned values, and the assignment to 'ret' incorrectly truncates the rate from long to int.

Fixes 5c5992cb90cf ("clk: Add debugging for return values")

---

  drivers/clk/clk-uclass.c | 7 +------
  1 file changed, 1 insertion(+), 6 deletions(-)
drivers/clk/clk-uclass.c +0 -1
index dc3e9d6a261..78299dbceb2 100644
@@ -471,7 +471,6 @@ void clk_free(struct clk *clk)
  ulong clk_get_rate(struct clk *clk)
  {
  	const struct clk_ops *ops;
-	int ret;
  	debug("%s(clk=%p)\n", __func__, clk);
  	if (!clk_valid(clk))

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

Quoted 9 lines
drivers/clk/clk-uclass.c +1 -5
@@ -481,11 +480,7 @@ ulong clk_get_rate(struct clk *clk)
  	if (!ops->get_rate)
  		return -ENOSYS;
-	ret = ops->get_rate(clk);
-	if (ret)
-		return log_ret(ret);
-
-	return 0;
+	return ops->get_rate(clk);
  }

This is generally poor design of the clock stuff in u-boot.

It returns -ERROR for many error conditions, but also 0 for other error conditions.

Some error handling checks for 0, some for errval, some casts to int and checks for <= 0.

I think that using -ERROR for clocks does not make much sense in u-boot.

Even in the kernel the errval checks are pretty much limited to places where integers are used to store page frame numbers or pointers, that is errptr stored in an integer. For adresses it is pretty easy to make sure that the last page is not mapped making the error pointers invalid (although bugs in that part happened too).

For clocks no such guarantee exists. The only apparently invalid clock is 0, and the correct fix is to fix up the clock code to return 0 on error, always. It's a lot of code to fix, though.

If you do not want to fix everything then the correct thing to do is make ret ulong, and check for errval *and* 0.

Quoted 8 lines

There is not much point in returning detailed error codes in u-boot, anyway. It's not like there is some userspace that could interpret them. Most errors are logged when they happen if ever, and callers only check if error happened or not.

Thanks

Michal

clk_get_parent is the only place where we return a clock pointer directly. Everywhere else, we have an integer return we can use. This function is different of course because CCF is broken and assumes there is only one canonical struct clk for a logical clock. So it can't initialize a caller-passed struct clk, but instead has to return the one true struct clk.

Quoted 6 lines

I agree with Michal here. The signature should really be

int clk_get_parent(struct clk *child, struct clk *parent)

but for now there is no point confusing the rest of the clock subsystem to make it work with error pointers.

This is clk_get_rate. It returns a clock rate. A clock rate of 2.5GHz is not out of question, and cannot be expressed with a signed integer.

Hence clock rate should not be treated as signed, and -ERROR should not be returned as clock rate.

Thanks

Michal

Reviewed-by
#11 lore raw

Re: [PATCH 2/6] clk: Fix error handling in clk_get_rate()

Quoted 77 lines · Michal Suchánek

Hello,

Quoted 74 lines · Sean Anderson
Quoted 58 lines · Michal Suchánek

Hello,

Quoted 22 lines · Samuel Holland

log_ret() cannot work with unsigned values, and the assignment to 'ret' incorrectly truncates the rate from long to int.

Fixes 5c5992cb90cf ("clk: Add debugging for return values")

---

   drivers/clk/clk-uclass.c | 7 +------
   1 file changed, 1 insertion(+), 6 deletions(-)
drivers/clk/clk-uclass.c +0 -1
index dc3e9d6a261..78299dbceb2 100644
@@ -471,7 +471,6 @@ void clk_free(struct clk *clk)
   ulong clk_get_rate(struct clk *clk)
   {
   	const struct clk_ops *ops;
-	int ret;
   	debug("%s(clk=%p)\n", __func__, clk);
   	if (!clk_valid(clk))

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

Quoted 9 lines
drivers/clk/clk-uclass.c +1 -5
@@ -481,11 +480,7 @@ ulong clk_get_rate(struct clk *clk)
   	if (!ops->get_rate)
   		return -ENOSYS;
-	ret = ops->get_rate(clk);
-	if (ret)
-		return log_ret(ret);
-
-	return 0;
+	return ops->get_rate(clk);
   }

This is generally poor design of the clock stuff in u-boot.

It returns -ERROR for many error conditions, but also 0 for other error conditions.

Some error handling checks for 0, some for errval, some casts to int and checks for <= 0.

I think that using -ERROR for clocks does not make much sense in u-boot.

Even in the kernel the errval checks are pretty much limited to places where integers are used to store page frame numbers or pointers, that is errptr stored in an integer. For adresses it is pretty easy to make sure that the last page is not mapped making the error pointers invalid (although bugs in that part happened too).

For clocks no such guarantee exists. The only apparently invalid clock is 0, and the correct fix is to fix up the clock code to return 0 on error, always. It's a lot of code to fix, though.

If you do not want to fix everything then the correct thing to do is make ret ulong, and check for errval *and* 0.

Quoted 8 lines

There is not much point in returning detailed error codes in u-boot, anyway. It's not like there is some userspace that could interpret them. Most errors are logged when they happen if ever, and callers only check if error happened or not.

Thanks

Michal

clk_get_parent is the only place where we return a clock pointer directly. Everywhere else, we have an integer return we can use. This function is different of course because CCF is broken and assumes there is only one canonical struct clk for a logical clock. So it can't initialize a caller-passed struct clk, but instead has to return the one true struct clk.

Quoted 8 lines
Quoted 6 lines

I agree with Michal here. The signature should really be

int clk_get_parent(struct clk *child, struct clk *parent)

but for now there is no point confusing the rest of the clock subsystem to make it work with error pointers.

This is clk_get_rate.

Ah, whoops. In that case,

Quoted 2 lines

It returns a clock rate. A clock rate of 2.5GHz is
not out of question, and cannot be expressed with a signed integer.

And of course neither is a rate of 5 GHz :)

Quoted 2 lines

Hence clock rate should not be treated as signed, and -ERROR should not be returned as clock rate.

Yes...

I would accept a conversion to "0 means unknown rate (for whatever reason)" API.

--Sean

#12 lore raw

PATCH 3/6 clk: Fix error handling in clk_get_parent()

In reply to Samuel Holland

Do not return both NULL and error pointers. The function is only documented as returning error pointers.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

 drivers/clk/clk-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-uclass.c +1 -1
index 78299dbceb2..5bce976b060 100644
@@ -490,7 +490,7 @@ struct clk *clk_get_parent(struct clk *clk)
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	pdev = dev_get_parent(clk->dev);
 	if (!pdev)
Signature · 1 line
2.39.2
#13 lore raw

Re: [PATCH 3/6] clk: Fix error handling in clk_get_parent()

Quoted 15 lines · Samuel Holland

Do not return both NULL and error pointers. The function is only documented as returning error pointers.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

 drivers/clk/clk-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 78299dbceb2..5bce976b060 100644

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

Quoted 8 lines
drivers/clk/clk-uclass.c +1 -1
@@ -490,7 +490,7 @@ struct clk *clk_get_parent(struct clk *clk)
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	pdev = dev_get_parent(clk->dev);
 	if (!pdev)

Do we really need this?

Who cares about the distinction?

Just returning NULL on any error makes the code so much simpler and easier to understand.

Thanks

Michal

#14 lore raw

Re: [PATCH 3/6] clk: Fix error handling in clk_get_parent()

Quoted 27 lines · Michal Suchánek
Quoted 15 lines · Samuel Holland

Do not return both NULL and error pointers. The function is only documented as returning error pointers.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

 drivers/clk/clk-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 78299dbceb2..5bce976b060 100644

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

Quoted 8 lines
drivers/clk/clk-uclass.c +1 -1
@@ -490,7 +490,7 @@ struct clk *clk_get_parent(struct clk *clk)
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	pdev = dev_get_parent(clk->dev);
 	if (!pdev)

Do we really need this?

Quoted 4 lines

Who cares about the distinction?

Just returning NULL on any error makes the code so much simpler and easier to understand.

I'm fine with returning only NULL, or only ERR_PTR(-ENODEV) as done later in the function, but returning both makes the error handling in callers more complicated for no benefit. It sounds like you would prefer I change the later ERR_PTR(-ENODEV) to NULL instead of this change? I can do that for v2.

Regards,
Samuel

#15 lore raw

Re: [PATCH 3/6] clk: Fix error handling in clk_get_parent()

Quoted 30 lines · Samuel Holland
Quoted 29 lines · Michal Suchánek
Quoted 15 lines · Samuel Holland

Do not return both NULL and error pointers. The function is only documented as returning error pointers.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

 drivers/clk/clk-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 78299dbceb2..5bce976b060 100644

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

Quoted 8 lines
drivers/clk/clk-uclass.c +1 -1
@@ -490,7 +490,7 @@ struct clk *clk_get_parent(struct clk *clk)
 
 	debug("%s(clk=%p)\n", __func__, clk);
 	if (!clk_valid(clk))
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	pdev = dev_get_parent(clk->dev);
 	if (!pdev)

Do we really need this?

Who cares about the distinction?

Quoted 8 lines
Quoted 2 lines

Just returning NULL on any error makes the code so much simpler and easier to understand.

I'm fine with returning only NULL, or only ERR_PTR(-ENODEV) as done later in the function, but returning both makes the error handling in callers more complicated for no benefit. It sounds like you would prefer I change the later ERR_PTR(-ENODEV) to NULL instead of this change? I can do that for v2.

I think NULL is easier to check, and so long as there is no meaningful distinction between ERR_PTR and NULL we should converge towards NULL as the single error return value.

Thanks

Michal

#16 lore raw

Re: [PATCH 3/6] clk: Fix error handling in clk_get_parent()

Quoted 40 lines · Michal Suchánek
Quoted 30 lines · Samuel Holland
Quoted 29 lines · Michal Suchánek
Quoted 15 lines · Samuel Holland

Do not return both NULL and error pointers. The function is only documented as returning error pointers.

Fixes 8a1661f20e6c ("drivers: clk: Handle gracefully NULL pointers")

---

  drivers/clk/clk-uclass.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 78299dbceb2..5bce976b060 100644

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

Quoted 8 lines
drivers/clk/clk-uclass.c +1 -1
@@ -490,7 +490,7 @@ struct clk *clk_get_parent(struct clk *clk)
 
  	debug("%s(clk=%p)\n", __func__, clk);
  	if (!clk_valid(clk))
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
  	pdev = dev_get_parent(clk->dev);
  	if (!pdev)

Do we really need this?

Who cares about the distinction?

Quoted 8 lines
Quoted 2 lines

Just returning NULL on any error makes the code so much simpler and easier to understand.

I'm fine with returning only NULL, or only ERR_PTR(-ENODEV) as done later in the function, but returning both makes the error handling in callers more complicated for no benefit. It sounds like you would prefer I change the later ERR_PTR(-ENODEV) to NULL instead of this change? I can do that for v2.

Quoted 3 lines

I think NULL is easier to check, and so long as there is no meaningful distinction between ERR_PTR and NULL we should converge towards NULL as the single error return value.

The only issue I can see is if you have e.g. an I2C clock and you need to examine the register to determine the parent and the slave doesn't respond. Is the distinction between "I have no parent" and "I have a parent but I don't know which" meaningful? Linux uses one return because parents must be determined at probe time. I'm fine with making this return NULL on error; this should be uncommon enough that we can recommend dev_err() reporting in the driver.

--Sean

#17 lore raw

PATCH 4/6 clk: Fix rate caching in clk_get_parent_rate()

In reply to Samuel Holland

clk_get_rate() can return an error value. Recompute the rate if the cached value is an error value.

Fixes 4aa78300a025 ("dm: clk: Define clk_get_parent_rate() for clk operations")

---

 drivers/clk/clk-uclass.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk-uclass.c +2 -1
index 5bce976b060..9d052e5a814 100644
@@ -520,7 +520,8 @@ ulong clk_get_parent_rate(struct clk *clk)
 		return -ENOSYS;
 
 	/* Read the 'rate' if not already set or if proper flag set*/
-	if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
+	if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
+	    pclk->flags & CLK_GET_RATE_NOCACHE)
 		pclk->rate = clk_get_rate(pclk);
 
 	return pclk->rate;
Signature · 1 line
2.39.2
Reviewed-by on v1 4/6
#18 lore raw

Re: [PATCH 4/6] clk: Fix rate caching in clk_get_parent_rate()

Quoted 15 lines · Samuel Holland

clk_get_rate() can return an error value. Recompute the rate if the cached value is an error value.

Fixes 4aa78300a025 ("dm: clk: Define clk_get_parent_rate() for clk operations")

---

 drivers/clk/clk-uclass.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 5bce976b060..9d052e5a814 100644

drivers/clk/clk-uclass.c ulong clk_get_parent_rate(struct clk *clk) · line 527

Quoted 9 lines
drivers/clk/clk-uclass.c +2 -1
@@ -520,7 +520,8 @@ ulong clk_get_parent_rate(struct clk *clk)
 		return -ENOSYS;
 
 	/* Read the 'rate' if not already set or if proper flag set*/
-	if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
+	if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
+	    pclk->flags & CLK_GET_RATE_NOCACHE)
 		pclk->rate = clk_get_rate(pclk);
 
 	return pclk->rate;

With the current code that randomly returns one or the other this is the correct thing to do.

Thanks

Michal

#19 lore raw

Re: [PATCH 4/6] clk: Fix rate caching in clk_get_parent_rate()

In reply to Samuel Holland
Quoted 15 lines · Samuel Holland

clk_get_rate() can return an error value. Recompute the rate if the cached value is an error value.

Fixes 4aa78300a025 ("dm: clk: Define clk_get_parent_rate() for clk operations")

---

  drivers/clk/clk-uclass.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 5bce976b060..9d052e5a814 100644

drivers/clk/clk-uclass.c ulong clk_get_parent_rate(struct clk *clk) · line 527

Quoted 9 lines
drivers/clk/clk-uclass.c +2 -1
@@ -520,7 +520,8 @@ ulong clk_get_parent_rate(struct clk *clk)
  		return -ENOSYS;
 
  	/* Read the 'rate' if not already set or if proper flag set*/
-	if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
+	if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
+	    pclk->flags & CLK_GET_RATE_NOCACHE)
  		pclk->rate = clk_get_rate(pclk);
 
  	return pclk->rate;

The correct fix here is

	/* Read the 'rate' if not already set or if proper flag set*/
	if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE) {
		rate = clk_get_rate(pclk);
		if (!IS_ERR_VALUE(rate))
			pclk->rate = rate;
	}

	return rate;

No point caching errors.

--Sean

#20 lore raw

Re: [PATCH 4/6] clk: Fix rate caching in clk_get_parent_rate()

Quoted 34 lines · Sean Anderson
Quoted 16 lines · Samuel Holland

clk_get_rate() can return an error value. Recompute the rate if the cached value is an error value.

Fixes 4aa78300a025 ("dm: clk: Define clk_get_parent_rate() for clk

operations")

---

  drivers/clk/clk-uclass.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk-uclass.c +0 -0
index 5bce976b060..9d052e5a814 100644

drivers/clk/clk-uclass.c ulong clk_get_parent_rate(struct clk *clk) · line 525

Quoted 7 lines
drivers/clk/clk-uclass.c +2 -1
@@ -520,7 +520,8 @@ ulong clk_get_parent_rate(struct clk *clk)
           return -ENOSYS;
         /* Read the 'rate' if not already set or if proper flag set*/
-    if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
+    if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
+        pclk->flags & CLK_GET_RATE_NOCACHE)
           pclk->rate = clk_get_rate(pclk);
         return pclk->rate;

The correct fix here is

    /* Read the 'rate' if not already set or if proper flag set*/ if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE) { rate = clk_get_rate(pclk);
        if (!IS_ERR_VALUE(rate))
            pclk->rate = rate;
    }

Quoted 3 lines

    return rate;

No point caching errors.

Good point. I will do this for v2.

Regards,
Samuel

#21 lore raw

PATCH 5/6 clk: Remove an unneeded check from clk_get_parent_rate()

In reply to Samuel Holland

There is no need to check the parent clock's ops. The following call to clk_get_rate() does that already.

---

 drivers/clk/clk-uclass.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/clk/clk-uclass.c +0 -5
index 9d052e5a814..53cfd819779 100644
@@ -504,7 +504,6 @@ struct clk *clk_get_parent(struct clk *clk)
 
 ulong clk_get_parent_rate(struct clk *clk)
 {
-	const struct clk_ops *ops;
 	struct clk *pclk;
 
 	debug("%s(clk=%p)\n", __func__, clk);
@@ -515,10 +514,6 @@ ulong clk_get_parent_rate(struct clk *clk)
 	if (IS_ERR(pclk))
 		return -ENODEV;
 
-	ops = clk_dev_ops(pclk->dev);
-	if (!ops->get_rate)
-		return -ENOSYS;
-
 	/* Read the 'rate' if not already set or if proper flag set*/
 	if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
 	    pclk->flags & CLK_GET_RATE_NOCACHE)
Signature · 1 line
2.39.2
Reviewed-by on v1 5/6
#22 lore raw

Re: [PATCH 5/6] clk: Remove an unneeded check from clk_get_parent_rate()

Quoted 22 lines · Samuel Holland

There is no need to check the parent clock's ops. The following call to clk_get_rate() does that already.

---

 drivers/clk/clk-uclass.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/clk/clk-uclass.c +0 -1
index 9d052e5a814..53cfd819779 100644
@@ -504,7 +504,6 @@ struct clk *clk_get_parent(struct clk *clk)
 
 ulong clk_get_parent_rate(struct clk *clk)
 {
-	const struct clk_ops *ops;
 	struct clk *pclk;
 
 	debug("%s(clk=%p)\n", __func__, clk);

drivers/clk/clk-uclass.c ulong clk_get_parent_rate(struct clk *clk) · line 519

Quoted 10 lines
drivers/clk/clk-uclass.c +0 -4
@@ -515,10 +514,6 @@ ulong clk_get_parent_rate(struct clk *clk)
 	if (IS_ERR(pclk))
 		return -ENODEV;
 
-	ops = clk_dev_ops(pclk->dev);
-	if (!ops->get_rate)
-		return -ENOSYS;
-
 	/* Read the 'rate' if not already set or if proper flag set*/
 	if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
 	    pclk->flags & CLK_GET_RATE_NOCACHE)

Thanks

Michal

Reviewed-by on v1 5/6
#23 lore raw

Re: [PATCH 5/6] clk: Remove an unneeded check from clk_get_parent_rate()

In reply to Samuel Holland
Quoted 22 lines · Samuel Holland

There is no need to check the parent clock's ops. The following call to clk_get_rate() does that already.

---

  drivers/clk/clk-uclass.c | 5 -----
  1 file changed, 5 deletions(-)
drivers/clk/clk-uclass.c +0 -1
index 9d052e5a814..53cfd819779 100644
@@ -504,7 +504,6 @@ struct clk *clk_get_parent(struct clk *clk)
 
  ulong clk_get_parent_rate(struct clk *clk)
  {
-	const struct clk_ops *ops;
  	struct clk *pclk;
 
  	debug("%s(clk=%p)\n", __func__, clk);

drivers/clk/clk-uclass.c ulong clk_get_parent_rate(struct clk *clk) · line 519

Quoted 10 lines
drivers/clk/clk-uclass.c +0 -4
@@ -515,10 +514,6 @@ ulong clk_get_parent_rate(struct clk *clk)
  	if (IS_ERR(pclk))
  		return -ENODEV;
 
-	ops = clk_dev_ops(pclk->dev);
-	if (!ops->get_rate)
-		return -ENOSYS;
-
  	/* Read the 'rate' if not already set or if proper flag set*/
  	if (!pclk->rate || IS_ERR_VALUE(pclk->rate) ||
  	    pclk->flags & CLK_GET_RATE_NOCACHE)
#24 lore raw

PATCH 6/6 clk: Add a .get_parent operation

In reply to Samuel Holland

This allows clk_get_parent() to work with non-CCF clock drivers.

---

 drivers/clk/clk-uclass.c | 18 ++++++++++++------
 include/clk-uclass.h     |  2 ++
 2 files changed, 14 insertions(+), 6 deletions(-)
drivers/clk/clk-uclass.c +12 -6
index 53cfd819779..d0f8906cd60 100644
@@ -485,6 +485,7 @@ ulong clk_get_rate(struct clk *clk)
 
 struct clk *clk_get_parent(struct clk *clk)
 {
+	const struct clk_ops *ops;
 	struct udevice *pdev;
 	struct clk *pclk;
 
@@ -492,12 +493,17 @@ struct clk *clk_get_parent(struct clk *clk)
 	if (!clk_valid(clk))
 		return ERR_PTR(-ENODEV);
 
-	pdev = dev_get_parent(clk->dev);
-	if (!pdev)
-		return ERR_PTR(-ENODEV);
-	pclk = dev_get_clk_ptr(pdev);
-	if (!pclk)
-		return ERR_PTR(-ENODEV);
+	ops = clk_dev_ops(clk->dev);
+	if (ops->get_parent) {
+		pclk = ops->get_parent(clk);
+	} else {
+		pdev = dev_get_parent(clk->dev);
+		if (!pdev)
+			return ERR_PTR(-ENODEV);
+		pclk = dev_get_clk_ptr(pdev);
+		if (!pclk)
+			return ERR_PTR(-ENODEV);
+	}
 
 	return pclk;
 }
include/clk-uclass.h +2 -0
index 65ebff9ed27..4d616720865 100644
@@ -22,6 +22,7 @@ struct ofnode_phandle_args;
  * @round_rate: Adjust a rate to the exact rate a clock can provide.
  * @get_rate: Get current clock rate.
  * @set_rate: Set current clock rate.
+ * @get_parent: Get current clock parent
  * @set_parent: Set current clock parent
  * @enable: Enable a clock.
  * @disable: Disable a clock.
@@ -36,6 +37,7 @@ struct clk_ops {
 	ulong (*round_rate)(struct clk *clk, ulong rate);
 	ulong (*get_rate)(struct clk *clk);
 	ulong (*set_rate)(struct clk *clk, ulong rate);
+	struct clk *(*get_parent)(struct clk *clk);
 	int (*set_parent)(struct clk *clk, struct clk *parent);
 	int (*enable)(struct clk *clk);
 	int (*disable)(struct clk *clk);
Signature · 1 line
2.39.2
#25 lore raw

Re: [PATCH 6/6] clk: Add a .get_parent operation

Quoted 58 lines · Samuel Holland

This allows clk_get_parent() to work with non-CCF clock drivers.

---

  drivers/clk/clk-uclass.c | 18 ++++++++++++------
  include/clk-uclass.h     |  2 ++
  2 files changed, 14 insertions(+), 6 deletions(-)
drivers/clk/clk-uclass.c +12 -6
index 53cfd819779..d0f8906cd60 100644
@@ -485,6 +485,7 @@ ulong clk_get_rate(struct clk *clk)
 
  struct clk *clk_get_parent(struct clk *clk)
  {
+	const struct clk_ops *ops;
  	struct udevice *pdev;
  	struct clk *pclk;
 
@@ -492,12 +493,17 @@ struct clk *clk_get_parent(struct clk *clk)
  	if (!clk_valid(clk))
  		return ERR_PTR(-ENODEV);
 
-	pdev = dev_get_parent(clk->dev);
-	if (!pdev)
-		return ERR_PTR(-ENODEV);
-	pclk = dev_get_clk_ptr(pdev);
-	if (!pclk)
-		return ERR_PTR(-ENODEV);
+	ops = clk_dev_ops(clk->dev);
+	if (ops->get_parent) {
+		pclk = ops->get_parent(clk);
+	} else {
+		pdev = dev_get_parent(clk->dev);
+		if (!pdev)
+			return ERR_PTR(-ENODEV);
+		pclk = dev_get_clk_ptr(pdev);
+		if (!pclk)
+			return ERR_PTR(-ENODEV);
+	}
 
  	return pclk;
  }
include/clk-uclass.h +1 -0
index 65ebff9ed27..4d616720865 100644
@@ -22,6 +22,7 @@ struct ofnode_phandle_args;
   * @round_rate: Adjust a rate to the exact rate a clock can provide.
   * @get_rate: Get current clock rate.
   * @set_rate: Set current clock rate.
+ * @get_parent: Get current clock parent
   * @set_parent: Set current clock parent
   * @enable: Enable a clock.
   * @disable: Disable a clock.

include/clk-uclass.h struct clk_ops { · line 43

Quoted 7 lines
include/clk-uclass.h +1 -0
@@ -36,6 +37,7 @@ struct clk_ops {
  	ulong (*round_rate)(struct clk *clk, ulong rate);
  	ulong (*get_rate)(struct clk *clk);
  	ulong (*set_rate)(struct clk *clk, ulong rate);
+	struct clk *(*get_parent)(struct clk *clk);
  	int (*set_parent)(struct clk *clk, struct clk *parent);
  	int (*enable)(struct clk *clk);
  	int (*disable)(struct clk *clk);

Missing documentation.

--Sean

#26 lore raw

Re: [PATCH 6/6] clk: Add a .get_parent operation

In reply to Samuel Holland

Hi Samuel,

Quoted 10 lines · Samuel Holland

This allows clk_get_parent() to work with non-CCF clock drivers.

---

 drivers/clk/clk-uclass.c | 18 ++++++++++++------
 include/clk-uclass.h     |  2 ++
 2 files changed, 14 insertions(+), 6 deletions(-)

Please can you add a test for this and do full comments for the function you added?

Quoted 57 lines
drivers/clk/clk-uclass.c +12 -6
index 53cfd819779..d0f8906cd60 100644
@@ -485,6 +485,7 @@ ulong clk_get_rate(struct clk *clk)
 
 struct clk *clk_get_parent(struct clk *clk)
 {
+       const struct clk_ops *ops;
        struct udevice *pdev;
        struct clk *pclk;
 
@@ -492,12 +493,17 @@ struct clk *clk_get_parent(struct clk *clk)
        if (!clk_valid(clk))
                return ERR_PTR(-ENODEV);
 
-       pdev = dev_get_parent(clk->dev);
-       if (!pdev)
-               return ERR_PTR(-ENODEV);
-       pclk = dev_get_clk_ptr(pdev);
-       if (!pclk)
-               return ERR_PTR(-ENODEV);
+       ops = clk_dev_ops(clk->dev);
+       if (ops->get_parent) {
+               pclk = ops->get_parent(clk);
+       } else {
+               pdev = dev_get_parent(clk->dev);
+               if (!pdev)
+                       return ERR_PTR(-ENODEV);
+               pclk = dev_get_clk_ptr(pdev);
+               if (!pclk)
+                       return ERR_PTR(-ENODEV);
+       }
 
        return pclk;
 }
include/clk-uclass.h +2 -1
index 65ebff9ed27..4d616720865 100644
@@ -22,6 +22,7 @@ struct ofnode_phandle_args;
  * @round_rate: Adjust a rate to the exact rate a clock can provide.
  * @get_rate: Get current clock rate.
  * @set_rate: Set current clock rate.
+ * @get_parent: Get current clock parent
  * @set_parent: Set current clock parent
  * @enable: Enable a clock.
  * @disable: Disable a clock.
@@ -36,6 +37,7 @@ struct clk_ops {
        ulong (*round_rate)(struct clk *clk, ulong rate);
        ulong (*get_rate)(struct clk *clk);
        ulong (*set_rate)(struct clk *clk, ulong rate);
+       struct clk *(*get_parent)(struct clk *clk);
        int (*set_parent)(struct clk *clk, struct clk *parent);
        int (*enable)(struct clk *clk);
        int (*disable)(struct clk *clk);
--
Quoted 2 lines

2.39.2

Regards,
Simon