diff mbox series

[v2] libnvdimm/of_pmem: Fix memory leak in of_pmem_region_probe()

Message ID 20221123150447.194267-1-xiujianfeng@huawei.com (mailing list archive)
State New, archived
Headers show
Series [v2] libnvdimm/of_pmem: Fix memory leak in of_pmem_region_probe() | expand

Commit Message

Xiu Jianfeng Nov. 23, 2022, 3:04 p.m. UTC
After changes in commit 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique
name for bus provider"), @priv->bus_desc.provider_name is no longer a
const string, but a dynamic string allocated by kstrdup(), it should be
freed on the error path, and when driver is removed.

Fixes: 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")
Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
---
v2: check the return value of kstrdup();
---
 drivers/nvdimm/of_pmem.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Tarun Sahu Nov. 23, 2022, 3:54 p.m. UTC | #1
Hi,

Look all good to me.

Reviewed-by: Tarun Sahu <tsahu@linux.ibm.com>

On Nov 23 2022, Xiu Jianfeng wrote:
> After changes in commit 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique
> name for bus provider"), @priv->bus_desc.provider_name is no longer a
> const string, but a dynamic string allocated by kstrdup(), it should be
> freed on the error path, and when driver is removed.
> 
> Fixes: 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")
> Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
> ---
> v2: check the return value of kstrdup();
> ---
>  drivers/nvdimm/of_pmem.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/nvdimm/of_pmem.c b/drivers/nvdimm/of_pmem.c
> index 10dbdcdfb9ce..08e7f2502479 100644
> --- a/drivers/nvdimm/of_pmem.c
> +++ b/drivers/nvdimm/of_pmem.c
> @@ -31,11 +31,16 @@ static int of_pmem_region_probe(struct platform_device *pdev)
>  		return -ENOMEM;
>  
>  	priv->bus_desc.provider_name = kstrdup(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;
>  
>  	priv->bus = bus = nvdimm_bus_register(&pdev->dev, &priv->bus_desc);
>  	if (!bus) {
> +		kfree(priv->bus_desc.provider_name);
>  		kfree(priv);
>  		return -ENODEV;
>  	}
> @@ -83,6 +88,7 @@ static int of_pmem_region_remove(struct platform_device *pdev)
>  	struct of_pmem_private *priv = platform_get_drvdata(pdev);
>  
>  	nvdimm_bus_unregister(priv->bus);
> +	kfree(priv->bus_desc.provider_name);
>  	kfree(priv);
>  
>  	return 0;
> -- 
> 2.17.1
>
diff mbox series

Patch

diff --git a/drivers/nvdimm/of_pmem.c b/drivers/nvdimm/of_pmem.c
index 10dbdcdfb9ce..08e7f2502479 100644
--- a/drivers/nvdimm/of_pmem.c
+++ b/drivers/nvdimm/of_pmem.c
@@ -31,11 +31,16 @@  static int of_pmem_region_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	priv->bus_desc.provider_name = kstrdup(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;
 
 	priv->bus = bus = nvdimm_bus_register(&pdev->dev, &priv->bus_desc);
 	if (!bus) {
+		kfree(priv->bus_desc.provider_name);
 		kfree(priv);
 		return -ENODEV;
 	}
@@ -83,6 +88,7 @@  static int of_pmem_region_remove(struct platform_device *pdev)
 	struct of_pmem_private *priv = platform_get_drvdata(pdev);
 
 	nvdimm_bus_unregister(priv->bus);
+	kfree(priv->bus_desc.provider_name);
 	kfree(priv);
 
 	return 0;