Skip to contentSkip to conversation

clk: handle NULL args in clk_set_parent()

PATCH Applied Bastian Krause 3 participants 5 messages May 11, 2022 – May 12, 2022 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: handle NULL args in clk_set_parent()

NULL struct clk pointers should be treated just as the Linux kernel clk driver does [1]. The reasoning should also apply to the parent clk argument.

A NULL struct clk pointer can happen for example on the Freescale i.MX6 SABRE Smart Device Board if CONFIG_DRIVER_VIDEO_IMX_IPUV3 is disabled, leading to assigned-clocks IMX6QDL_CLK_LDB_DI0_SEL and IMX6QDL_CLK_LDB_DI1_SEL [2] being unavailable. Without this patch, the board hangs while setting those assigned clock configurations since [3].

[1] 89ac8d7ae1cd ("clk: handle NULL struct clk gracefully")
[2] dts/src/arm/imx6qdl-sabresd.dtsi
[3] f5eb5fddb4 ("clk: fix of clk set defaults when dev is a clk provider")

---
 drivers/clk/clk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/clk/clk.c +6 -1
index 8e317b4b05..efb5d4ad4a 100644
@@ -230,7 +230,10 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 {
 	struct clk_hw *hw;
 	int i, ret;
-	struct clk *curparent = clk_get_parent(clk);
+	struct clk *curparent;
+
+	if (!clk || !newparent)
+		return 0;
 
 	if (IS_ERR(clk))
 		return PTR_ERR(clk);
@@ -254,6 +257,8 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 	if (i == clk->num_parents)
 		return -EINVAL;
 
+	curparent = clk_get_parent(clk);
+
 	if (clk->enable_count)
 		clk_enable(newparent);
 
Signature · 7 lines
2.30.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

Hello Bastian,

Quoted 10 lines · Bastian Krause

NULL struct clk pointers should be treated just as the Linux kernel clk driver does [1]. The reasoning should also apply to the parent clk argument.

A NULL struct clk pointer can happen for example on the Freescale i.MX6 SABRE Smart Device Board if CONFIG_DRIVER_VIDEO_IMX_IPUV3 is disabled, leading to assigned-clocks IMX6QDL_CLK_LDB_DI0_SEL and IMX6QDL_CLK_LDB_DI1_SEL [2] being unavailable. Without this patch, the board hangs while setting those assigned clock configurations since [3].

Quoted 3 lines

[1] 89ac8d7ae1cd ("clk: handle NULL struct clk gracefully")
[2] dts/src/arm/imx6qdl-sabresd.dtsi
[3] f5eb5fddb4 ("clk: fix of clk set defaults when dev is a clk provider")

Thanks for your patch.

Quoted 11 lines
---
 drivers/clk/clk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/clk/clk.c +0 -0
index 8e317b4b05..efb5d4ad4a 100644

drivers/clk/clk.c int clk_set_parent(struct clk *clk, struct clk *newparent) · line 233

Quoted 4 lines
drivers/clk/clk.c +0 -1
@@ -230,7 +230,10 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 {
 	struct clk_hw *hw;
 	int i, ret;
-	struct clk *curparent = clk_get_parent(clk);

Linux clk_get_parent returns NULL if clk is NULL. Can you teach barebox to do the same?

Linux clk_set_parent accepts a NULL parent to add a clock to the orphan list. We don't seem to maintain an orphan list in barebox, so early exist with return value zero seems okay.

drivers/clk/clk.c int clk_set_parent(struct clk *clk, struct clk *newparent) · line 261

Quoted 13 lines
drivers/clk/clk.c +6 -0
+	struct clk *curparent;
+
+	if (!clk || !newparent)
+		return 0;
 
 	if (IS_ERR(clk))
 		return PTR_ERR(clk);
@@ -254,6 +257,8 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 	if (i == clk->num_parents)
 		return -EINVAL;
 
+	curparent = clk_get_parent(clk);
+

With early-exiting clk_get_parent, you won't need to shift this code around.

Cheers,
Ahmad

Quoted 3 lines
drivers/clk/clk.c +0 -0
 	if (clk->enable_count)
 		clk_enable(newparent);
 
Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#2 lore raw
In reply to Bastian Krause
Quoted 10 lines · Bastian Krause

NULL struct clk pointers should be treated just as the Linux kernel clk driver does [1]. The reasoning should also apply to the parent clk argument.

A NULL struct clk pointer can happen for example on the Freescale i.MX6 SABRE Smart Device Board if CONFIG_DRIVER_VIDEO_IMX_IPUV3 is disabled, leading to assigned-clocks IMX6QDL_CLK_LDB_DI0_SEL and IMX6QDL_CLK_LDB_DI1_SEL [2] being unavailable. Without this patch, the board hangs while setting those assigned clock configurations since [3].

Quoted 8 lines

[1] 89ac8d7ae1cd ("clk: handle NULL struct clk gracefully")
[2] dts/src/arm/imx6qdl-sabresd.dtsi
[3] f5eb5fddb4 ("clk: fix of clk set defaults when dev is a clk provider")

---
 drivers/clk/clk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Applied, thanks

Sascha

Quoted 35 lines
drivers/clk/clk.c +6 -1
index 8e317b4b05..efb5d4ad4a 100644
@@ -230,7 +230,10 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 {
 	struct clk_hw *hw;
 	int i, ret;
-	struct clk *curparent = clk_get_parent(clk);
+	struct clk *curparent;
+
+	if (!clk || !newparent)
+		return 0;
 
 	if (IS_ERR(clk))
 		return PTR_ERR(clk);
@@ -254,6 +257,8 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 	if (i == clk->num_parents)
 		return -EINVAL;
 
+	curparent = clk_get_parent(clk);
+
 	if (clk->enable_count)
 		clk_enable(newparent);
 
Signature · 7 lines
2.30.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

Hi Sascha,

Quoted 15 lines · Sascha Hauer
Quoted 14 lines · Bastian Krause

NULL struct clk pointers should be treated just as the Linux kernel clk driver does [1]. The reasoning should also apply to the parent clk argument.

A NULL struct clk pointer can happen for example on the Freescale i.MX6 SABRE Smart Device Board if CONFIG_DRIVER_VIDEO_IMX_IPUV3 is disabled, leading to assigned-clocks IMX6QDL_CLK_LDB_DI0_SEL and IMX6QDL_CLK_LDB_DI1_SEL [2] being unavailable. Without this patch, the board hangs while setting those assigned clock configurations since [3].

[1] 89ac8d7ae1cd ("clk: handle NULL struct clk gracefully")
[2] dts/src/arm/imx6qdl-sabresd.dtsi
[3] f5eb5fddb4 ("clk: fix of clk set defaults when dev is a clk provider")

Quoted 6 lines
Quoted 4 lines
---
  drivers/clk/clk.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)

Applied, thanks

Did you see the v2 of this patch?

Regards,
Bastian

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
Quoted 18 lines · Bastian Krause

Hi Sascha,

Quoted 15 lines · Sascha Hauer
Quoted 14 lines · Bastian Krause

NULL struct clk pointers should be treated just as the Linux kernel clk driver does [1]. The reasoning should also apply to the parent clk argument.

A NULL struct clk pointer can happen for example on the Freescale i.MX6 SABRE Smart Device Board if CONFIG_DRIVER_VIDEO_IMX_IPUV3 is disabled, leading to assigned-clocks IMX6QDL_CLK_LDB_DI0_SEL and IMX6QDL_CLK_LDB_DI1_SEL [2] being unavailable. Without this patch, the board hangs while setting those assigned clock configurations since [3].

[1] 89ac8d7ae1cd ("clk: handle NULL struct clk gracefully")
[2] dts/src/arm/imx6qdl-sabresd.dtsi
[3] f5eb5fddb4 ("clk: fix of clk set defaults when dev is a clk provider")

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
---
  drivers/clk/clk.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)

Applied, thanks

Did you see the v2 of this patch?

Yes, but still I accidently applied v1 and then fixed it up to v2 :/

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox