Skip to contentSkip to conversation

clk: Skip clk provider registration when np is NULL

PATCH In review Tudor Ambarus 6 participants 6 messages Apr 26, 2021 – May 11, 2021

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

PATCH v2 1/1 clk: Skip clk provider registration when np is NULL

commit 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added") revealed that clk/bcm/clk-raspberrypi.c driver calls devm_of_clk_add_hw_provider(), with a NULL dev->of_node, which resulted in a NULL pointer dereference in of_clk_add_hw_provider() when calling fwnode_dev_initialized().

Returning 0 is reducing the if conditions in driver code and is being consistent with the CONFIG_OF=n inline stub that returns 0 when CONFIG_OF is disabled. The downside is that drivers will maybe register clkdev lookups when they don't need to and waste some memory.

Fixes 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added")
Fixes 3c9ea42802a1 ("clk: Mark fwnodes when their clock provider is added/removed")

---
v2:
- s/return 0;/return; in void of_clk_del_provider()
- add second fixes tag and Stephen's R-b tag
The opinions on whether to return an error or zero were split. Returning 0 and skipping the logic was considered safer as we don't know for sure if other drivers are affected. See:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

 drivers/clk/clk.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk.c +9 -0
index a3b30f7de2ef..b47460b40d14 100644
@@ -4552,6 +4552,9 @@ int of_clk_add_provider(struct device_node *np,
 	struct of_clk_provider *cp;
 	int ret;
 
+	if (!np)
+		return 0;
+
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
@@ -4591,6 +4594,9 @@ int of_clk_add_hw_provider(struct device_node *np,
 	struct of_clk_provider *cp;
 	int ret;
 
+	if (!np)
+		return 0;
+
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
@@ -4688,6 +4694,9 @@ void of_clk_del_provider(struct device_node *np)
 {
 	struct of_clk_provider *cp;
 
+	if (!np)
+		return;
+
 	mutex_lock(&of_clk_mutex);
 	list_for_each_entry(cp, &of_clk_providers, link) {
 		if (cp->node == np) {
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 12 lines · Tudor Ambarus

commit 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added") revealed that clk/bcm/clk-raspberrypi.c driver calls devm_of_clk_add_hw_provider(), with a NULL dev->of_node, which resulted in a NULL pointer dereference in of_clk_add_hw_provider() when calling fwnode_dev_initialized().

Returning 0 is reducing the if conditions in driver code and is being consistent with the CONFIG_OF=n inline stub that returns 0 when CONFIG_OF is disabled. The downside is that drivers will maybe register clkdev lookups when they don't need to and waste some memory.

Quoted 6 lines
Fixes 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added")
Fixes 3c9ea42802a1 ("clk: Mark fwnodes when their clock provider is added/removed")

---

-Saravana

Quoted 49 lines

v2:
- s/return 0;/return; in void of_clk_del_provider()
- add second fixes tag and Stephen's R-b tag
The opinions on whether to return an error or zero were split. Returning 0 and skipping the logic was considered safer as we don't know for sure if other drivers are affected. See:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

 drivers/clk/clk.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk.c +9 -1
index a3b30f7de2ef..b47460b40d14 100644
@@ -4552,6 +4552,9 @@ int of_clk_add_provider(struct device_node *np,
        struct of_clk_provider *cp;
        int ret;
 
+       if (!np)
+               return 0;
+
        cp = kzalloc(sizeof(*cp), GFP_KERNEL);
        if (!cp)
                return -ENOMEM;
@@ -4591,6 +4594,9 @@ int of_clk_add_hw_provider(struct device_node *np,
        struct of_clk_provider *cp;
        int ret;
 
+       if (!np)
+               return 0;
+
        cp = kzalloc(sizeof(*cp), GFP_KERNEL);
        if (!cp)
                return -ENOMEM;
@@ -4688,6 +4694,9 @@ void of_clk_del_provider(struct device_node *np)
 {
        struct of_clk_provider *cp;
 
+       if (!np)
+               return;
+
        mutex_lock(&of_clk_mutex);
        list_for_each_entry(cp, &of_clk_providers, link) {
                if (cp->node == np) {
--

2.25.1

Reviewed-by on v2 1/1
#2 lore raw
In reply to Tudor Ambarus
Quoted 11 lines · Tudor Ambarus

commit 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added") revealed that clk/bcm/clk-raspberrypi.c driver calls devm_of_clk_add_hw_provider(), with a NULL dev->of_node, which resulted in a NULL pointer dereference in of_clk_add_hw_provider() when calling fwnode_dev_initialized().

Returning 0 is reducing the if conditions in driver code and is being consistent with the CONFIG_OF=n inline stub that returns 0 when CONFIG_OF is disabled. The downside is that drivers will maybe register clkdev lookups when they don't need to and waste some memory.

Quoted 6 lines
Fixes 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added")
Fixes 3c9ea42802a1 ("clk: Mark fwnodes when their clock provider is added/removed")

---

Regards,
Nicolas

Tested-by on v2 1/1
#3 lore raw
In reply to Tudor Ambarus
Quoted 11 lines · Tudor Ambarus

commit 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added") revealed that clk/bcm/clk-raspberrypi.c driver calls devm_of_clk_add_hw_provider(), with a NULL dev->of_node, which resulted in a NULL pointer dereference in of_clk_add_hw_provider() when calling fwnode_dev_initialized().

Returning 0 is reducing the if conditions in driver code and is being consistent with the CONFIG_OF=n inline stub that returns 0 when CONFIG_OF is disabled. The downside is that drivers will maybe register clkdev lookups when they don't need to and waste some memory.

Quoted 5 lines
Fixes 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added")
Fixes 3c9ea42802a1 ("clk: Mark fwnodes when their clock provider is added/removed")
Quoted 50 lines

---
v2:
- s/return 0;/return; in void of_clk_del_provider()
- add second fixes tag and Stephen's R-b tag
The opinions on whether to return an error or zero were split. Returning 0 and skipping the logic was considered safer as we don't know for sure if other drivers are affected. See:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

 drivers/clk/clk.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk.c +9 -0
index a3b30f7de2ef..b47460b40d14 100644
@@ -4552,6 +4552,9 @@ int of_clk_add_provider(struct device_node *np,
 	struct of_clk_provider *cp;
 	int ret;
 
+	if (!np)
+		return 0;
+
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
@@ -4591,6 +4594,9 @@ int of_clk_add_hw_provider(struct device_node *np,
 	struct of_clk_provider *cp;
 	int ret;
 
+	if (!np)
+		return 0;
+
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
@@ -4688,6 +4694,9 @@ void of_clk_del_provider(struct device_node *np)
 {
 	struct of_clk_provider *cp;
 
+	if (!np)
+		return;
+
 	mutex_lock(&of_clk_mutex);
 	list_for_each_entry(cp, &of_clk_providers, link) {
 		if (cp->node == np) {
Signature · 1 line
2.25.1
Tested-by on v2 1/1
#5 lore raw
In reply to Tudor Ambarus
Quoted 11 lines · Tudor Ambarus

commit 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added") revealed that clk/bcm/clk-raspberrypi.c driver calls devm_of_clk_add_hw_provider(), with a NULL dev->of_node, which resulted in a NULL pointer dereference in of_clk_add_hw_provider() when calling fwnode_dev_initialized().

Returning 0 is reducing the if conditions in driver code and is being consistent with the CONFIG_OF=n inline stub that returns 0 when CONFIG_OF is disabled. The downside is that drivers will maybe register clkdev lookups when they don't need to and waste some memory.

Quoted 5 lines
Fixes 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added")
Fixes 3c9ea42802a1 ("clk: Mark fwnodes when their clock provider is added/removed")
Quoted 50 lines

---
v2:
- s/return 0;/return; in void of_clk_del_provider()
- add second fixes tag and Stephen's R-b tag
The opinions on whether to return an error or zero were split. Returning 0 and skipping the logic was considered safer as we don't know for sure if other drivers are affected. See:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

 drivers/clk/clk.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk.c +9 -0
index a3b30f7de2ef..b47460b40d14 100644
@@ -4552,6 +4552,9 @@ int of_clk_add_provider(struct device_node *np,
 	struct of_clk_provider *cp;
 	int ret;
 
+	if (!np)
+		return 0;
+
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
@@ -4591,6 +4594,9 @@ int of_clk_add_hw_provider(struct device_node *np,
 	struct of_clk_provider *cp;
 	int ret;
 
+	if (!np)
+		return 0;
+
 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
@@ -4688,6 +4694,9 @@ void of_clk_del_provider(struct device_node *np)
 {
 	struct of_clk_provider *cp;
 
+	if (!np)
+		return;
+
 	mutex_lock(&of_clk_mutex);
 	list_for_each_entry(cp, &of_clk_providers, link) {
 		if (cp->node == np) {
Signature · 1 line
2.25.1
#4 lore raw
In reply to Guenter Roeck
Quoted 12 lines · Guenter Roeck
Quoted 11 lines · Tudor Ambarus

commit 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added") revealed that clk/bcm/clk-raspberrypi.c driver calls devm_of_clk_add_hw_provider(), with a NULL dev->of_node, which resulted in a NULL pointer dereference in of_clk_add_hw_provider() when calling fwnode_dev_initialized().

Returning 0 is reducing the if conditions in driver code and is being consistent with the CONFIG_OF=n inline stub that returns 0 when CONFIG_OF is disabled. The downside is that drivers will maybe register clkdev lookups when they don't need to and waste some memory.

Quoted 7 lines
Quoted 5 lines
Fixes 6579c8d97ad7 ("clk: Mark fwnodes when their clock provider is added")
Fixes 3c9ea42802a1 ("clk: Mark fwnodes when their clock provider is added/removed")

Now applied to my tree, thanks and sorry for the delay, I thought this was going through the clk tree.

greg k-h