Re: [PATCH v2] PM / devfreq: userspace: Fix memory leak in userspace_init()

From: Jie Zhan

Date: Thu Jul 30 2026 - 08:56:09 EST




On 7/29/2026 9:35 PM, 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.
>
> 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@xxxxxxxxx>
Thanks. LGTM.
Reviewed-by: Jie Zhan <zhanjie9@xxxxxxxxxxxxx>
> ---
> 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;
> }
>