[PATCH] staging: vchiq_arm: Utilize devm_kzalloc in the probe() function

Umang Jain posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
.../staging/vc04_services/interface/vchiq_arm/vchiq_arm.c   | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
[PATCH] staging: vchiq_arm: Utilize devm_kzalloc in the probe() function
Posted by Umang Jain 1 month, 2 weeks ago
The two resources, 'mgmt' and 'platform_state' are currently allocated
dynamically using kzalloc(). Unfortunately, both are subject to memory
leaks in the error handling paths of the probe() function.

To address this issue, use device resource management helper devm_kzalloc()
for proper cleanup during allocation.

Cc: stable@vger.kernel.org
Fixes: 1c9e16b73166 ("staging: vc04_services: vchiq_arm: Split driver static and runtime data")
Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>
---
 .../staging/vc04_services/interface/vchiq_arm/vchiq_arm.c   | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 29e78700463f..373cfdd5b020 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -285,7 +285,7 @@ vchiq_platform_init_state(struct vchiq_state *state)
 {
 	struct vchiq_arm_state *platform_state;
 
-	platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
+	platform_state = devm_kzalloc(state->dev, sizeof(*platform_state), GFP_KERNEL);
 	if (!platform_state)
 		return -ENOMEM;
 
@@ -1344,7 +1344,7 @@ static int vchiq_probe(struct platform_device *pdev)
 		return -ENOENT;
 	}
 
-	mgmt = kzalloc(sizeof(*mgmt), GFP_KERNEL);
+	mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
 	if (!mgmt)
 		return -ENOMEM;
 
@@ -1402,8 +1402,6 @@ static void vchiq_remove(struct platform_device *pdev)
 
 	arm_state = vchiq_platform_get_arm_state(&mgmt->state);
 	kthread_stop(arm_state->ka_thread);
-
-	kfree(mgmt);
 }
 
 static struct platform_driver vchiq_driver = {
-- 
2.45.2
Re: [PATCH] staging: vchiq_arm: Utilize devm_kzalloc in the probe() function
Posted by Stefan Wahren 1 month, 2 weeks ago
Hi Umang,

Am 13.10.24 um 19:16 schrieb Umang Jain:
> The two resources, 'mgmt' and 'platform_state' are currently allocated
> dynamically using kzalloc(). Unfortunately, both are subject to memory
> leaks in the error handling paths of the probe() function.
>
> To address this issue, use device resource management helper devm_kzalloc()
> for proper cleanup during allocation.
>
> Cc: stable@vger.kernel.org
> Fixes: 1c9e16b73166 ("staging: vc04_services: vchiq_arm: Split driver static and runtime data")
> Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>
> ---
>   .../staging/vc04_services/interface/vchiq_arm/vchiq_arm.c   | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> index 29e78700463f..373cfdd5b020 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> @@ -285,7 +285,7 @@ vchiq_platform_init_state(struct vchiq_state *state)
>   {
>   	struct vchiq_arm_state *platform_state;
>
> -	platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
> +	platform_state = devm_kzalloc(state->dev, sizeof(*platform_state), GFP_KERNEL);
 From my understand this leak has been there from the beginning and
platform_state is never freed. So I think this patch should be splitted,
because the Fixes applies only to the rest of the patch.

Regards
>   	if (!platform_state)
>   		return -ENOMEM;
>
> @@ -1344,7 +1344,7 @@ static int vchiq_probe(struct platform_device *pdev)
>   		return -ENOENT;
>   	}
>
> -	mgmt = kzalloc(sizeof(*mgmt), GFP_KERNEL);
> +	mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
>   	if (!mgmt)
>   		return -ENOMEM;
>
> @@ -1402,8 +1402,6 @@ static void vchiq_remove(struct platform_device *pdev)
>
>   	arm_state = vchiq_platform_get_arm_state(&mgmt->state);
>   	kthread_stop(arm_state->ka_thread);
> -
> -	kfree(mgmt);
>   }
>
>   static struct platform_driver vchiq_driver = {