Skip to contentSkip to conversation

Handle errors returned by radix_tree_insert() within drivers/pinctrl/

PATCH In review Sergey Shtylyov 2 participants 6 messages Jul 19, 2023 – Jul 31, 2023

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

cover Handle errors returned by radix_tree_insert() within drivers/pinctrl/

Here are 3 patches against the 'devel' branch of Linus W.'s 'linux-pinctrl.git' repo...

The code in drivers/pinctrl/ doesn't check the results of radix_tree_insert() calls, although it could propagate the errors from the callers upstream. (Linus Walleij said he has copied the radix tree code from kernel/irq/, where the functions calling radix_tree_insert() are *void* themselves.)

Sergey Shtylyov (3):
pinctrl: core: handle radix_tree_insert() errors in
pinctrl_generic_add_group()
pinctrl: core: handle radix_tree_insert() errors in
pinctrl_register_one_pin()
pinctrl: pinmux: handle radix_tree_insert() errors in
pinmux_generic_add_function()

 drivers/pinctrl/core.c   | 20 +++++++++++++++-----
 drivers/pinctrl/pinmux.c |  6 ++++--
 2 files changed, 19 insertions(+), 7 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/3 pinctrl: core: handle radix_tree_insert() errors in pinctrl_generic_add_group()

pinctrl_generic_add_group() doesn't check the result of radix_tree_insert() despite they both may return a negative error code. Linus Walleij said he has copied the radix tree code from kernel/irq/ where the functions calling radix_tree_insert() are *void* themselves; I think it makes more sense to propagate the errors from radix_tree_insert() upstream if we can do that...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

---
 drivers/pinctrl/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pinctrl/core.c +4 -2
index 401886c81344..3c3fc4ae0f2f 100644
@@ -633,7 +633,7 @@ int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
 			      int *pins, int num_pins, void *data)
 {
 	struct group_desc *group;
-	int selector;
+	int selector, error;
 
 	if (!name)
 		return -EINVAL;
@@ -653,7 +653,9 @@ int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
 	group->num_pins = num_pins;
 	group->data = data;
 
-	radix_tree_insert(&pctldev->pin_group_tree, selector, group);
+	error = radix_tree_insert(&pctldev->pin_group_tree, selector, group);
+	if (error)
+		return error;
 
 	pctldev->num_groups++;
 
Signature · 1 line
2.26.3
#2 lore raw

PATCH 2/3 pinctrl: core: handle radix_tree_insert() errors in pinctrl_register_one_pin()

In reply to Sergey Shtylyov

pinctrl_register_one_pin() doesn't check the result of radix_tree_insert() despite they both may return a negative error code. Linus Walleij said he has copied the radix tree code from kernel/irq/ where the functions calling radix_tree_insert() are *void* themselves; I think it makes more sense to propagate the errors from radix_tree_insert() upstream if we can do that...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

---
 drivers/pinctrl/core.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/pinctrl/core.c +11 -3
index 3c3fc4ae0f2f..e9dc9638120a 100644
@@ -205,6 +205,7 @@ static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
 				    const struct pinctrl_pin_desc *pin)
 {
 	struct pin_desc *pindesc;
+	int error;
 
 	pindesc = pin_desc_get(pctldev, pin->number);
 	if (pindesc) {
@@ -226,18 +227,25 @@ static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
 	} else {
 		pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
 		if (!pindesc->name) {
-			kfree(pindesc);
-			return -ENOMEM;
+			error = -ENOMEM;
+			goto failed;
 		}
 		pindesc->dynamic_name = true;
 	}
 
 	pindesc->drv_data = pin->drv_data;
 
-	radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
+	error = radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
+	if (error)
+		goto failed;
+
 	pr_debug("registered pin %d (%s) on %s\n",
 		 pin->number, pindesc->name, pctldev->desc->name);
 	return 0;
+
+failed:
+	kfree(pindesc);
+	return error;
 }
 
 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
Signature · 1 line
2.26.3
#3 lore raw

PATCH 3/3 pinctrl: pinmux: handle radix_tree_insert() errors in pinmux_generic_add_function()

In reply to Sergey Shtylyov

pinctrl_generic_add_function() doesn't check result of radix_tree_insert() despite they both may return a negative error code. Linus Walleij said he has copied the radix tree code from kernel/irq/ where the functions calling radix_tree_insert() are *void* themselves; I think it makes more sense to propagate the errors from radix_tree_insert() upstream if we can do that...

Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool.

---
 drivers/pinctrl/pinmux.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pinctrl/pinmux.c +4 -2
index 82c750a31952..2a180a5d64a4 100644
@@ -872,7 +872,7 @@ int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
 				void *data)
 {
 	struct function_desc *function;
-	int selector;
+	int selector, error;
 
 	if (!name)
 		return -EINVAL;
@@ -892,7 +892,9 @@ int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
 	function->num_group_names = num_groups;
 	function->data = data;
 
-	radix_tree_insert(&pctldev->pin_function_tree, selector, function);
+	error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);
+	if (error)
+		return error;
 
 	pctldev->num_functions++;
 
Signature · 1 line
2.26.3
#4 lore raw
In reply to Sergey Shtylyov
Quoted 4 lines · Sergey Shtylyov

The code in drivers/pinctrl/ doesn't check the results of radix_tree_insert() calls, although it could propagate the errors from the callers upstream. (Linus Walleij said he has copied the radix tree code from kernel/irq/, where the functions calling radix_tree_insert() are *void* themselves.)

The fix is spot on.
Patches applied!

Yours,
Linus Walleij

#5 lore raw

On 7/28/23 11:09 PM, Linus Walleij wrote:
[...]

Quoted 7 lines
Quoted 4 lines

The code in drivers/pinctrl/ doesn't check the results of radix_tree_insert() calls, although it could propagate the errors from the callers upstream. (Linus Walleij said he has copied the radix tree code from kernel/irq/, where the functions calling radix_tree_insert() are *void* themselves.)

The fix is spot on.
Patches applied!

Thank you!
But to which branch?

Quoted 2 lines

Yours,
Linus Walleij

MBR, Sergey