Skip to contentSkip to conversation

libnvdimm/of_pmem: Use devm_kstrdup instead of kstrdup and check its return value

PATCH In review Chen Ni 3 participants 4 messages Sep 14, 2023 – Sep 15, 2023

Subsystems (2)
lore
Share & tools
lore

Chen Ni

author
#0 lore raw

PATCH v3 1/1 libnvdimm/of_pmem: Use devm_kstrdup instead of kstrdup and check its return value

Use devm_kstrdup() instead of kstrdup() and check its return value to avoid memory leak.

Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")

---
Changelog:

v2 -> v3:

1. Use devm_kstrdup() instead of kstrdup()

v1 -> v2:

1. Add a fixes tag.
2. Update commit message.

---
 drivers/nvdimm/of_pmem.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/nvdimm/of_pmem.c +7 -1
index 1b9f5b8a6167..5765674b36f2 100644
@@ -30,7 +30,13 @@ static int of_pmem_region_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL);
+	priv->bus_desc.provider_name = devm_kstrdup(&pdev->dev, pdev->name,
+							GFP_KERNEL);
+	if (!priv->bus_desc.provider_name) {
+		kfree(priv);
+		return -ENOMEM;
+	}
+
 	priv->bus_desc.module = THIS_MODULE;
 	priv->bus_desc.of_node = np;
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 5 lines · Chen Ni

Use devm_kstrdup() instead of kstrdup() and check its return value to avoid memory leak.

Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")

One unrelated comment below.

Quoted 20 lines

---
Changelog:

v2 -> v3:

1. Use devm_kstrdup() instead of kstrdup()

v1 -> v2:

1. Add a fixes tag.
2. Update commit message.

---
 drivers/nvdimm/of_pmem.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/nvdimm/of_pmem.c +0 -0
index 1b9f5b8a6167..5765674b36f2 100644

drivers/nvdimm/of_pmem.c static int of_pmem_region_probe(struct platform_device *pdev) · line 36

Quoted 8 lines
drivers/nvdimm/of_pmem.c +4 -1
@@ -30,7 +30,13 @@ static int of_pmem_region_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL);
+	priv->bus_desc.provider_name = devm_kstrdup(&pdev->dev, pdev->name,
+							GFP_KERNEL);
+	if (!priv->bus_desc.provider_name) {
+		kfree(priv);

I wonder if priv should be allocated with devm_kzalloc() instead to reduce the resource management burden.

Quoted 6 lines
drivers/nvdimm/of_pmem.c +3 -0
+		return -ENOMEM;
+	}
+
 	priv->bus_desc.module = THIS_MODULE;
 	priv->bus_desc.of_node = np;
 
#2 lore raw
Quoted 3 lines · Dave Jiang

On 9/14/23 00:03, Chen Ni wrote:

[snip]

Quoted 8 lines
Quoted 8 lines
drivers/nvdimm/of_pmem.c +0 -0
index 1b9f5b8a6167..5765674b36f2 100644
@@ -30,7 +30,13 @@ static int of_pmem_region_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
Quoted 7 lines

drivers/nvdimm/of_pmem.c static int of_pmem_region_probe(struct platform_device *pdev) · line 36

Quoted 5 lines
drivers/nvdimm/of_pmem.c +4 -1
-	priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL);
+	priv->bus_desc.provider_name = devm_kstrdup(&pdev->dev, pdev->name,
+							GFP_KERNEL);
+	if (!priv->bus_desc.provider_name) {
+		kfree(priv);

I wonder if priv should be allocated with devm_kzalloc() instead to reduce the resource management burden.

I think it could be but this is the driver and I wonder if leaving the allocation around until the platform device goes away was undesirable for some reason?

Ira

Reviewed-by on v3 1/1
#3 lore raw
In reply to Chen Ni
Quoted 5 lines · Chen Ni

Use devm_kstrdup() instead of kstrdup() and check its return value to avoid memory leak.

Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")
Quoted 37 lines

---
Changelog:

v2 -> v3:

1. Use devm_kstrdup() instead of kstrdup()

v1 -> v2:

1. Add a fixes tag.
2. Update commit message.

---
 drivers/nvdimm/of_pmem.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/nvdimm/of_pmem.c +7 -1
index 1b9f5b8a6167..5765674b36f2 100644
@@ -30,7 +30,13 @@ static int of_pmem_region_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL);
+	priv->bus_desc.provider_name = devm_kstrdup(&pdev->dev, pdev->name,
+							GFP_KERNEL);
+	if (!priv->bus_desc.provider_name) {
+		kfree(priv);
+		return -ENOMEM;
+	}
+
 	priv->bus_desc.module = THIS_MODULE;
 	priv->bus_desc.of_node = np;
 
Signature · 1 line
2.25.1