PM / devfreq: userspace: Fix memory leak in userspace_init()
Subsystems (2)
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.
--- drivers/devfreq/governor_userspace.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/devfreq/governor_userspace.c +6 -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); + +if (err) { +kfree (data); + devfreq->governor_data =NULL ; + } + out:return err; }
Signature · 1 line
2.54.0
Quoted 35 lines · Malaya Kumar Rout
Fix a memory leak in the
userspace_init()function where allocated memory is not freed whensysfs_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 resetgovernor_datato NULL on failure.Fixes 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")Signed-off-by Malaya Kumar Rout <[email protected]>--- drivers/devfreq/governor_userspace.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/devfreq/governor_userspace.c +6
-1@@ -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
Quoted 19 lines · Malaya Kumar Rout
Fix a memory leak in the
userspace_init()function where allocated memory is not freed whensysfs_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 resetgovernor_datato NULL on failure.Fixes 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")Signed-off-by Malaya Kumar Rout <[email protected]>--- drivers/devfreq/governor_userspace.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/devfreq/governor_userspace.c +0
-0
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; }
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.
--- drivers/devfreq/governor_userspace.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/devfreq/governor_userspace.c +8 -5
@@ -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
Quoted 8 lines · Malaya Kumar Rout
Fix a memory leak in the
userspace_init()function where allocated memory is not freed whensysfs_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 resetgovernor_datato NULL on failure.
Quoted 6 lines
v2:
- Removed redundant 'out:' goto label and returned-ENOMEMdirectly on allocation failure.Fixes 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")Signed-off-by Malaya Kumar Rout <[email protected]>
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@@ -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; }
Quoted 36 lines · Malaya Kumar Rout
Fix a memory leak in the
userspace_init()function where allocated memory is not freed whensysfs_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 resetgovernor_datato NULL on failure.v2:
- Removed redundant 'out:' goto label and returned-ENOMEMdirectly on allocation failure.Fixes 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")Signed-off-by Malaya Kumar Rout <[email protected]>--- drivers/devfreq/governor_userspace.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)drivers/devfreq/governor_userspace.c +3
-4@@ -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
-1err =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