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

From: Malaya Kumar Rout

Date: Wed Jul 29 2026 - 11:00:28 EST


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>
---
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