libnvdimm/of_pmem: Use devm_kstrdup instead of kstrdup and check its return value
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.
---
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
@@ -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
Quoted 5 lines · Chen Ni
Use
devm_kstrdup()instead ofkstrdup()and check its return value to avoid memory leak.Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")Signed-off-by Chen Ni <[email protected]>
One unrelated comment below.
Quoted 20 lines
---
Changelog:v2 -> v3:
1. Use
devm_kstrdup()instead ofkstrdup()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
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;
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@@ -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
Quoted 5 lines · Chen Ni
Use
devm_kstrdup()instead ofkstrdup()and check its return value to avoid memory leak.Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")Signed-off-by Chen Ni <[email protected]>
Quoted 37 lines
---
Changelog:v2 -> v3:
1. Use
devm_kstrdup()instead ofkstrdup()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@@ -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