Skip to contentSkip to conversation

pinctrl:sunplus: Add check for kmalloc

PATCH In review Wells Lu 4 participants 7 messages May 28, 2023 – Jun 13, 2023

Subsystems (4)
lore
Share & tools
lore

Wells Lu

author maintainer
#0 lore raw

PATCH v2 1/1 pinctrl:sunplus: Add check for kmalloc

Fix Smatch static checker warning:
potential null dereference 'configs'. (kmalloc returns null)

Changes in v2:
1. Add free allocated memory before returned -ENOMEM.
2. Add call of_node_put() before returned -ENOMEM.

Fixes aa74c44be19c ("pinctrl: Add driver for Sunplus SP7021")
---
 drivers/pinctrl/sunplus/sppctl.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/pinctrl/sunplus/sppctl.c +18 -6
index 6bbbab3..e91ce5b 100644
@@ -834,11 +834,6 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 	int i, size = 0;
 
 	list = of_get_property(np_config, "sunplus,pins", &size);
-
-	if (nmG <= 0)
-		nmG = 0;
-
-	parent = of_get_parent(np_config);
 	*num_maps = size / sizeof(*list);
 
 	/*
@@ -866,10 +861,14 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 		}
 	}
 
+	if (nmG <= 0)
+		nmG = 0;
+
 	*map = kcalloc(*num_maps + nmG, sizeof(**map), GFP_KERNEL);
-	if (*map == NULL)
+	if (!(*map))
 		return -ENOMEM;
 
+	parent = of_get_parent(np_config);
 	for (i = 0; i < (*num_maps); i++) {
 		dt_pin = be32_to_cpu(list[i]);
 		pin_num = FIELD_GET(GENMASK(31, 24), dt_pin);
@@ -883,6 +882,8 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 			(*map)[i].data.configs.num_configs = 1;
 			(*map)[i].data.configs.group_or_pin = pin_get_name(pctldev, pin_num);
 			configs = kmalloc(sizeof(*configs), GFP_KERNEL);
+			if (!configs)
+				goto sppctl_map_err;
 			*configs = FIELD_GET(GENMASK(7, 0), dt_pin);
 			(*map)[i].data.configs.configs = configs;
 
@@ -896,6 +897,8 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 			(*map)[i].data.configs.num_configs = 1;
 			(*map)[i].data.configs.group_or_pin = pin_get_name(pctldev, pin_num);
 			configs = kmalloc(sizeof(*configs), GFP_KERNEL);
+			if (!configs)
+				goto sppctl_map_err;
 			*configs = SPPCTL_IOP_CONFIGS;
 			(*map)[i].data.configs.configs = configs;
 
@@ -965,6 +968,15 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 	of_node_put(parent);
 	dev_dbg(pctldev->dev, "%d pins mapped\n", *num_maps);
 	return 0;
+
+sppctl_map_err:
+	for (i = 0; i < (*num_maps); i++)
+		if (((*map)[i].type == PIN_MAP_TYPE_CONFIGS_PIN) &&
+		    (*map)[i].data.configs.configs)
+			kfree((*map)[i].data.configs.configs);
+	kfree(*map);
+	of_node_put(parent);
+	return -ENOMEM;
 }
 
 static const struct pinctrl_ops sppctl_pctl_ops = {
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#1 lore raw

Sun, May 28, 2023 at 08:34:37PM +0800, Wells Lu kirjoitti:

Quoted 6 lines

Fix Smatch static checker warning:
potential null dereference 'configs'. (kmalloc returns null)

Changes in v2:
1. Add free allocated memory before returned -ENOMEM.
2. Add call of_node_put() before returned -ENOMEM.

The placeholder for this is after the cutter '---' line. I think it may be fixed during application by Linus W.

Otherwise LGTM,

Quoted 75 lines
Fixes aa74c44be19c ("pinctrl: Add driver for Sunplus SP7021")
---
 drivers/pinctrl/sunplus/sppctl.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/pinctrl/sunplus/sppctl.c +18 -6
index 6bbbab3..e91ce5b 100644
@@ -834,11 +834,6 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 	int i, size = 0;
 
 	list = of_get_property(np_config, "sunplus,pins", &size);
-
-	if (nmG <= 0)
-		nmG = 0;
-
-	parent = of_get_parent(np_config);
 	*num_maps = size / sizeof(*list);
 
 	/*
@@ -866,10 +861,14 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 		}
 	}
 
+	if (nmG <= 0)
+		nmG = 0;
+
 	*map = kcalloc(*num_maps + nmG, sizeof(**map), GFP_KERNEL);
-	if (*map == NULL)
+	if (!(*map))
 		return -ENOMEM;
 
+	parent = of_get_parent(np_config);
 	for (i = 0; i < (*num_maps); i++) {
 		dt_pin = be32_to_cpu(list[i]);
 		pin_num = FIELD_GET(GENMASK(31, 24), dt_pin);
@@ -883,6 +882,8 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 			(*map)[i].data.configs.num_configs = 1;
 			(*map)[i].data.configs.group_or_pin = pin_get_name(pctldev, pin_num);
 			configs = kmalloc(sizeof(*configs), GFP_KERNEL);
+			if (!configs)
+				goto sppctl_map_err;
 			*configs = FIELD_GET(GENMASK(7, 0), dt_pin);
 			(*map)[i].data.configs.configs = configs;
 
@@ -896,6 +897,8 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 			(*map)[i].data.configs.num_configs = 1;
 			(*map)[i].data.configs.group_or_pin = pin_get_name(pctldev, pin_num);
 			configs = kmalloc(sizeof(*configs), GFP_KERNEL);
+			if (!configs)
+				goto sppctl_map_err;
 			*configs = SPPCTL_IOP_CONFIGS;
 			(*map)[i].data.configs.configs = configs;
 
@@ -965,6 +968,15 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 	of_node_put(parent);
 	dev_dbg(pctldev->dev, "%d pins mapped\n", *num_maps);
 	return 0;
+
+sppctl_map_err:
+	for (i = 0; i < (*num_maps); i++)
+		if (((*map)[i].type == PIN_MAP_TYPE_CONFIGS_PIN) &&
+		    (*map)[i].data.configs.configs)
+			kfree((*map)[i].data.configs.configs);
+	kfree(*map);
+	of_node_put(parent);
+	return -ENOMEM;
 }
 
 static const struct pinctrl_ops sppctl_pctl_ops = {
Signature · 1 line
2.7.4
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Wells Lu
Quoted 9 lines · Wells Lu

Fix Smatch static checker warning:
potential null dereference 'configs'. (kmalloc returns null)

Changes in v2:
1. Add free allocated memory before returned -ENOMEM.
2. Add call of_node_put() before returned -ENOMEM.

Fixes aa74c44be19c ("pinctrl: Add driver for Sunplus SP7021")

Patch applied as non-urgent fix, dropped the changelog as pointed out by Andy.

Yours,
Linus Walleij

#3 lore raw

PATCH 1/1 pinctrl: sunplus: Delete an unnecessary check before kfree() in sppctl_dt_node_to_map()

In reply to Wells Lu

From: Markus Elfring <[email protected]>
Date: Tue, 6 Jun 2023 15:00:18 +0200

It can be known that the function “kfree” performs a null pointer check for its input parameter.
It is therefore not needed to repeat such a check before its call.

Thus remove a redundant pointer check.

---
 drivers/pinctrl/sunplus/sppctl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/pinctrl/sunplus/sppctl.c +1 -2
index e91ce5b5d559..150996949ede 100644
@@ -971,8 +971,7 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 
 sppctl_map_err:
 	for (i = 0; i < (*num_maps); i++)
-		if (((*map)[i].type == PIN_MAP_TYPE_CONFIGS_PIN) &&
-		    (*map)[i].data.configs.configs)
+		if ((*map)[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
 			kfree((*map)[i].data.configs.configs);
 	kfree(*map);
 	of_node_put(parent);

--
2.40.1

Reviewed-by on v1 1/1
#4 lore raw

Re: [PATCH] pinctrl: sunplus: Delete an unnecessary check before kfree() in sppctl_dt_node_to_map()

Quoted 3 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 6 Jun 2023 15:00:18 +0200

You need to utilize what MAINTAINERS file has.

Quoted 5 lines

It can be known that the function “kfree” performs a null pointer check for its input parameter.
It is therefore not needed to repeat such a check before its call.

Thus remove a redundant pointer check.

Seems reasonable to me.
FWIW,

Quoted 22 lines
---
 drivers/pinctrl/sunplus/sppctl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/pinctrl/sunplus/sppctl.c +1 -3
index e91ce5b5d559..150996949ede 100644
@@ -971,8 +971,7 @@ static int sppctl_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node
 
 sppctl_map_err:
        for (i = 0; i < (*num_maps); i++)
-               if (((*map)[i].type == PIN_MAP_TYPE_CONFIGS_PIN) &&
-                   (*map)[i].data.configs.configs)
+               if ((*map)[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
                        kfree((*map)[i].data.configs.configs);
        kfree(*map);
        of_node_put(parent);
--

2.40.1

Signature · 7 lines
With Best Regards,
Andy Shevchenko

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#5 lore raw

Re: [PATCH] pinctrl: sunplus: Delete an unnecessary check before kfree() in sppctl_dt_node_to_map()

In reply to Markus Elfring
Quoted 10 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 6 Jun 2023 15:00:18 +0200

It can be known that the function “kfree” performs a null pointer check for its input parameter.
It is therefore not needed to repeat such a check before its call.

Thus remove a redundant pointer check.

Lu Hongfei sent a patch changing the code around this location so I rebased it manually and applied, check the result, thanks!

Yours,
Linus Walleij

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#6 lore raw

Re: [cocci] pinctrl: sunplus: Delete an unnecessary check before kfree() in sppctl_dt_node_to_map()

Quoted 1 line

Lu Hongfei sent a patch changing the code around this location

To which contribution do you refer here?

Quoted 1 line

so I rebased it manually and applied, check the result, thanks!

I noticed another commit with the questionable subject “pinctrl:sunplus: Add check for kmalloc”.
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/drivers/pinctrl/sunplus/sppctl.c?h=next-20230613&id=73f8ce7f961afcb3be49352efeb7c26cc1c00cc4

I find such a commit message inappropriate for the applied source code adjustment.

Regards,
Markus