Skip to contentSkip to conversation

PM / devfreq: userspace: Fix memory leak in userspace_init()

PATCH Superseded Malaya Kumar Rout 3 participants 6 messages Jul 4 – Nov 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM / devfreq: userspace: Fix memory leak in userspace_init()

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")
---
 drivers/devfreq/governor_userspace.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/devfreq/governor_userspace.c +6 -0
index 3906ebedbae8..b9fbcacdfba1 100644
@@ -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;
 }
Signature · 1 line
2.54.0
#1 lore raw
Quoted 35 lines · Malaya Kumar Rout

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")
---
 drivers/devfreq/governor_userspace.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/devfreq/governor_userspace.c +6 -1
index 3906ebedbae8..b9fbcacdfba1 100644
@@ -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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Malaya Kumar Rout
Quoted 19 lines · Malaya Kumar Rout

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")
---
 drivers/devfreq/governor_userspace.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/devfreq/governor_userspace.c +0 -0
index 3906ebedbae8..b9fbcacdfba1 100644

drivers/devfreq/governor_userspace.c static int userspace_init(struct devfreq *devfreq) · line 100

Quoted 4 lines
drivers/devfreq/governor_userspace.c +1 -0
@@ -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,

BTW, this function can be slightly cleaned up, e.g. the 'goto' statement can be removed.

Quoted 8 lines
drivers/devfreq/governor_userspace.c +5 -0
+	if (err) {
+		kfree(data);
+		devfreq->governor_data = NULL;
+	}
+
 out:
 	return err;
 }
#3 lore raw

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

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")
---
 drivers/devfreq/governor_userspace.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/devfreq/governor_userspace.c +8 -5
index 3906ebedbae8..8211aadb81f0 100644
@@ -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;
 }
 
Signature · 1 line
2.54.0
Reviewed-by on v2 1/1
#4 lore raw
Quoted 8 lines · Malaya Kumar Rout

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.

Quoted 6 lines

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

Thanks. LGTM.

Quoted 32 lines
---
 drivers/devfreq/governor_userspace.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/devfreq/governor_userspace.c +8 -5
index 3906ebedbae8..8211aadb81f0 100644
@@ -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;
 }
 
#5 lore raw
In reply to Malaya Kumar Rout
Quoted 36 lines · Malaya Kumar Rout

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")
---
 drivers/devfreq/governor_userspace.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/devfreq/governor_userspace.c +3 -4
index 3906ebedbae8..8211aadb81f0 100644
@@ -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;
 

drivers/devfreq/governor_userspace.c static int userspace_init(struct devfreq *devfreq) · line 106

Quoted 10 lines
drivers/devfreq/governor_userspace.c +5 -1
 	err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
-out:
+	if (err) {
+		kfree(data);
+		devfreq->governor_data = NULL;
+	}
+
 	return err;
 }
 

Applied it.

Signature · 3 lines
Best Regards,
Chanwoo Choi
Principal Engineer, Samsung Electronics