Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] PM / devfreq: userspace: Fix memory leak in userspace_init()
@ 2026-07-04 16:32 Malaya Kumar Rout
  2026-07-24  6:17 ` malaya kumar rout
  2026-07-28  6:31 ` Jie Zhan
  0 siblings, 2 replies; 4+ messages in thread
From: Malaya Kumar Rout @ 2026-07-04 16:32 UTC (permalink / raw)
  To: linux-kernel, stable, linux-pm
  Cc: mrout, skhan, me, Malaya Kumar Rout, MyungJoo Ham, Kyungmin Park,
	Chanwoo Choi, Kant Fan

Fix a memory leak in the userspace_init() function where allocated
memory is not freed when sysfs_create_group() fails.

When sysfs_create_group() fails, the function returns without freeing
the memory allocated for 'data', leading to a memory leak. This patch
adds proper error handling to free the allocated memory and reset
governor_data to NULL on failure.

Fixes: 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
 drivers/devfreq/governor_userspace.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
index 3906ebedbae8..b9fbcacdfba1 100644
--- a/drivers/devfreq/governor_userspace.c
+++ b/drivers/devfreq/governor_userspace.c
@@ -97,6 +97,12 @@ static int userspace_init(struct devfreq *devfreq)
 	devfreq->governor_data = data;
 
 	err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
+
+	if (err) {
+		kfree(data);
+		devfreq->governor_data = NULL;
+	}
+
 out:
 	return err;
 }
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] PM / devfreq: userspace: Fix memory leak in userspace_init()
  2026-07-04 16:32 [PATCH] PM / devfreq: userspace: Fix memory leak in userspace_init() Malaya Kumar Rout
@ 2026-07-24  6:17 ` malaya kumar rout
  2026-07-28  6:31 ` Jie Zhan
  1 sibling, 0 replies; 4+ messages in thread
From: malaya kumar rout @ 2026-07-24  6:17 UTC (permalink / raw)
  To: linux-pm, MyungJoo Ham, Kyungmin Park, Chanwoo Choi, Kant Fan
  Cc: mrout, skhan, me

On Sat, Jul 4, 2026 at 10:03 PM Malaya Kumar Rout
<malayarout91@gmail.com> wrote:
>
> Fix a memory leak in the userspace_init() function where allocated
> memory is not freed when sysfs_create_group() fails.
>
> When sysfs_create_group() fails, the function returns without freeing
> the memory allocated for 'data', leading to a memory leak. This patch
> adds proper error handling to free the allocated memory and reset
> governor_data to NULL on failure.
>
> Fixes: 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")
> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> ---
>  drivers/devfreq/governor_userspace.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
> index 3906ebedbae8..b9fbcacdfba1 100644
> --- a/drivers/devfreq/governor_userspace.c
> +++ b/drivers/devfreq/governor_userspace.c
> @@ -97,6 +97,12 @@ static int userspace_init(struct devfreq *devfreq)
>         devfreq->governor_data = data;
>
>         err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
> +
> +       if (err) {
> +               kfree(data);
> +               devfreq->governor_data = NULL;
> +       }
> +
>  out:
>         return err;
>  }
> --
> 2.54.0
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] PM / devfreq: userspace: Fix memory leak in userspace_init()
  2026-07-04 16:32 [PATCH] PM / devfreq: userspace: Fix memory leak in userspace_init() Malaya Kumar Rout
  2026-07-24  6:17 ` malaya kumar rout
@ 2026-07-28  6:31 ` Jie Zhan
  2026-07-29 13:35   ` [PATCH v2] " Malaya Kumar Rout
  1 sibling, 1 reply; 4+ messages in thread
From: Jie Zhan @ 2026-07-28  6:31 UTC (permalink / raw)
  To: Malaya Kumar Rout, linux-kernel, stable, linux-pm
  Cc: mrout, skhan, me, MyungJoo Ham, Kyungmin Park, Chanwoo Choi,
	Kant Fan



On 7/5/2026 12:32 AM, Malaya Kumar Rout wrote:
> Fix a memory leak in the userspace_init() function where allocated
> memory is not freed when sysfs_create_group() fails.
> 
> When sysfs_create_group() fails, the function returns without freeing
> the memory allocated for 'data', leading to a memory leak. This patch
> adds proper error handling to free the allocated memory and reset
> governor_data to NULL on failure.
> 
> Fixes: 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")
> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> ---
>  drivers/devfreq/governor_userspace.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
> index 3906ebedbae8..b9fbcacdfba1 100644
> --- a/drivers/devfreq/governor_userspace.c
> +++ b/drivers/devfreq/governor_userspace.c
> @@ -97,6 +97,12 @@ static int userspace_init(struct devfreq *devfreq)
>  	devfreq->governor_data = data;
>  
>  	err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
> +
Redundant blank line. Otherwise,
Reviewed-by: Jie Zhan <zhanjie9@hisilicon.com> 

BTW, this function can be slightly cleaned up, e.g. the 'goto' statement
can be removed.
> +	if (err) {
> +		kfree(data);
> +		devfreq->governor_data = NULL;
> +	}
> +
>  out:
>  	return err;
>  }

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] PM / devfreq: userspace: Fix memory leak in userspace_init()
  2026-07-28  6:31 ` Jie Zhan
@ 2026-07-29 13:35   ` Malaya Kumar Rout
  0 siblings, 0 replies; 4+ messages in thread
From: Malaya Kumar Rout @ 2026-07-29 13:35 UTC (permalink / raw)
  To: zhanjie9, linux-pm, myungjoo.ham, kyungmin.park, cw00.choi, kant
  Cc: skhan, me, Malaya Kumar Rout, linux-kernel

Fix a memory leak in the userspace_init() function where allocated
memory is not freed when sysfs_create_group() fails.

When sysfs_create_group() fails, the function returns without freeing
the memory allocated for 'data', leading to a memory leak. This patch
adds proper error handling to free the allocated memory and reset
governor_data to NULL on failure.

v2:
- Removed redundant 'out:' goto label and returned -ENOMEM directly
  on allocation failure.

Fixes: 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
 drivers/devfreq/governor_userspace.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
index 3906ebedbae8..8211aadb81f0 100644
--- a/drivers/devfreq/governor_userspace.c
+++ b/drivers/devfreq/governor_userspace.c
@@ -89,15 +89,18 @@ static int userspace_init(struct devfreq *devfreq)
 	int err = 0;
 	struct userspace_data *data = kzalloc_obj(struct userspace_data);
 
-	if (!data) {
-		err = -ENOMEM;
-		goto out;
-	}
+	if (!data)
+		return -ENOMEM;
+
 	data->valid = false;
 	devfreq->governor_data = data;
 
 	err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
-out:
+	if (err) {
+		kfree(data);
+		devfreq->governor_data = NULL;
+	}
+
 	return err;
 }
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-29 13:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-04 16:32 [PATCH] PM / devfreq: userspace: Fix memory leak in userspace_init() Malaya Kumar Rout
2026-07-24  6:17 ` malaya kumar rout
2026-07-28  6:31 ` Jie Zhan
2026-07-29 13:35   ` [PATCH v2] " Malaya Kumar Rout

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox