ACPI: mrrm: Fix memory leaks and improve error handling
PATCH 1/1 ACPI: mrrm: Fix memory leaks and improve error handling
Add proper error handling and resource cleanup to prevent memory leaks in add_boot_memory_ranges(). The function now checks for NULL return from kobject_create_and_add(), frees allocated names after use, and implements a cleanup path that removes previously created sysfs groups and kobjects on failure.
This prevents resource leaks when kobject creation or sysfs group creation fails during boot memory range initialization.
--- drivers/acpi/acpi_mrrm.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
drivers/acpi/acpi_mrrm.c +29 -4
@@ -152,23 +152,48 @@ static __init int add_boot_memory_ranges(void)struct kobject *pkobj, *kobj;int ret = -EINVAL ;char *name; +int i; pkobj =kobject_create_and_add ("memory_ranges" , acpi_kobj); +if (!pkobj) +return -ENOMEM ; -for (int i =0 ; i < mrrm_mem_entry_num; i++) { +for (i =0 ; i < mrrm_mem_entry_num; i++) { name =kasprintf (GFP_KERNEL ,"range%d" , i);if (!name) { ret = -ENOMEM ; -break ; + goto cleanup; } kobj =kobject_create_and_add (name, pkobj); +kfree (name); +if (!kobj) { + ret = -ENOMEM ; + goto cleanup; + } ret =sysfs_create_groups (kobj, memory_range_groups); -if (ret) -return ret; +if (ret) { +kobject_put (kobj); + goto cleanup; + } } +return 0 ; + +cleanup: +for (int j =0 ; j < i; j++) { +char cleanup_name[32 ]; +struct kobject *cleanup_kobj; + +snprintf (cleanup_name,sizeof (cleanup_name),"range%d" , j); + cleanup_kobj =kobject_get (pkobj); +if (cleanup_kobj) { +sysfs_remove_groups (cleanup_kobj, memory_range_groups); +kobject_put (cleanup_kobj); + } + } +kobject_put (pkobj);return ret; }
Signature · 1 line
2.34.1
Rafael J. Wysocki
maintainerQuoted 20 lines · Kaushlendra Kumar
Add proper error handling and resource cleanup to prevent memory leaks in
add_boot_memory_ranges(). The function now checks for NULL return fromkobject_create_and_add(), frees allocated names after use, and implements a cleanup path that removes previously created sysfs groups and kobjects on failure.This prevents resource leaks when kobject creation or sysfs group creation fails during boot memory range initialization.
Signed-off-by Kaushlendra Kumar <[email protected]>--- drivers/acpi/acpi_mrrm.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)drivers/acpi/acpi_mrrm.c +0
-0
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 158
Quoted 10 lines
drivers/acpi/acpi_mrrm.c +3
-1@@ -152,23 +152,48 @@ static __init int add_boot_memory_ranges(void)struct kobject *pkobj, *kobj;int ret = -EINVAL ;char *name; +int i; pkobj =kobject_create_and_add ("memory_ranges" , acpi_kobj); +if (!pkobj) +return -ENOMEM ; -for (int i =0 ; i < mrrm_mem_entry_num; i++) {
Yes, i should be declared in the preamble.
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 169
Quoted 10 lines
drivers/acpi/acpi_mrrm.c +3
-1+for (i =0 ; i < mrrm_mem_entry_num; i++) { name =kasprintf (GFP_KERNEL ,"range%d" , i);if (!name) { ret = -ENOMEM ; -break ; + goto cleanup; } kobj =kobject_create_and_add (name, pkobj); +kfree (name);
OK, this fixes a memory leak.
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 172
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (!kobj) { + ret = -ENOMEM ; + goto cleanup;
Why terminate this? Why not continue?
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 169
Quoted 5 lines
drivers/acpi/acpi_mrrm.c +1
-2+ } ret =sysfs_create_groups (kobj, memory_range_groups); -if (ret) -return ret;
Well, this returns already, but I'm not sure why. Tony, wouldn't it be better to continue?
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 178
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (ret) { +kobject_put (kobj); + goto cleanup;
I would do a "continue" instead.
Quoted 22 lines
drivers/acpi/acpi_mrrm.c +16
-1+ } } +return 0 ; + +cleanup: +for (int j =0 ; j < i; j++) { +char cleanup_name[32 ]; +struct kobject *cleanup_kobj; + +snprintf (cleanup_name,sizeof (cleanup_name),"range%d" , j); + cleanup_kobj =kobject_get (pkobj); +if (cleanup_kobj) { +sysfs_remove_groups (cleanup_kobj, memory_range_groups); +kobject_put (cleanup_kobj); + } + } +kobject_put (pkobj);return ret; } --
Quoted 43 lines · Rafael J. Wysocki
Quoted 20 lines · Kaushlendra Kumar
Add proper error handling and resource cleanup to prevent memory leaks in
add_boot_memory_ranges(). The function now checks for NULL return fromkobject_create_and_add(), frees allocated names after use, and implements a cleanup path that removes previously created sysfs groups and kobjects on failure.This prevents resource leaks when kobject creation or sysfs group creation fails during boot memory range initialization.
Signed-off-by Kaushlendra Kumar <[email protected]>--- drivers/acpi/acpi_mrrm.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)drivers/acpi/acpi_mrrm.c +0
-0drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 158
Quoted 10 lines
drivers/acpi/acpi_mrrm.c +3
-1@@ -152,23 +152,48 @@ static __init int add_boot_memory_ranges(void)struct kobject *pkobj, *kobj;int ret = -EINVAL ;char *name; +int i; pkobj =kobject_create_and_add ("memory_ranges" , acpi_kobj); +if (!pkobj) +return -ENOMEM ; -for (int i =0 ; i < mrrm_mem_entry_num; i++) {Yes, i should be declared in the preamble.
Quoted 8 lines
drivers/acpi/acpi_mrrm.c +2
-1+for (i =0 ; i < mrrm_mem_entry_num; i++) { name =kasprintf (GFP_KERNEL ,"range%d" , i);if (!name) { ret = -ENOMEM ; -break ; + goto cleanup; }
Quoted 4 lines
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 169
Quoted 2 lines
drivers/acpi/acpi_mrrm.c +1
-0kobj =kobject_create_and_add (name, pkobj); +kfree (name);OK, this fixes a memory leak.
I didn't realize that kobject_create_and_add() made its own copy of the "name" parameter. Maybe the code should avoid the alloc/free by making "name" a local variable?
char name[16] = "rangeXXXXXXXXXX"; sprintf(&name[5], "%d", i);
Quoted 5 lines
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 172
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (!kobj) { + ret = -ENOMEM ; + goto cleanup;Why terminate this? Why not continue?
Terminating here (and below) will go to Kaushlendra's cleanup code to remove all the ranges. Maybe this is better than having some random subset of files appear (based on which allocations succeeded/failed)?
Quoted 36 lines
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 169
Quoted 5 lines
drivers/acpi/acpi_mrrm.c +1
-2+ } ret =sysfs_create_groups (kobj, memory_range_groups); -if (ret) -return ret;Well, this returns already, but I'm not sure why. Tony, wouldn't it be better to continue?
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 178
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (ret) { +kobject_put (kobj); + goto cleanup;I would do a "continue" instead.
Quoted 21 lines
drivers/acpi/acpi_mrrm.c +16
-0+ } } +return 0 ; + +cleanup: +for (int j =0 ; j < i; j++) { +char cleanup_name[32 ]; +struct kobject *cleanup_kobj; + +snprintf (cleanup_name,sizeof (cleanup_name),"range%d" , j); + cleanup_kobj =kobject_get (pkobj); +if (cleanup_kobj) { +sysfs_remove_groups (cleanup_kobj, memory_range_groups); +kobject_put (cleanup_kobj); + } + } +kobject_put (pkobj);return ret; }
Quoted 1 line
Quoted 1 line
--
-Tony
Rafael J. Wysocki
maintainerQuoted 50 lines · Tony
Quoted 43 lines · Rafael J. Wysocki
Quoted 20 lines · Kaushlendra Kumar
Add proper error handling and resource cleanup to prevent memory leaks in
add_boot_memory_ranges(). The function now checks for NULL return fromkobject_create_and_add(), frees allocated names after use, and implements a cleanup path that removes previously created sysfs groups and kobjects on failure.This prevents resource leaks when kobject creation or sysfs group creation fails during boot memory range initialization.
Signed-off-by Kaushlendra Kumar <[email protected]>--- drivers/acpi/acpi_mrrm.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)drivers/acpi/acpi_mrrm.c +0
-0drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 158
Quoted 10 lines
drivers/acpi/acpi_mrrm.c +3
-1@@ -152,23 +152,48 @@ static __init int add_boot_memory_ranges(void)struct kobject *pkobj, *kobj;int ret = -EINVAL ;char *name; +int i; pkobj =kobject_create_and_add ("memory_ranges" , acpi_kobj); +if (!pkobj) +return -ENOMEM ; -for (int i =0 ; i < mrrm_mem_entry_num; i++) {Yes, i should be declared in the preamble.
Quoted 8 lines
drivers/acpi/acpi_mrrm.c +2
-1+for (i =0 ; i < mrrm_mem_entry_num; i++) { name =kasprintf (GFP_KERNEL ,"range%d" , i);if (!name) { ret = -ENOMEM ; -break ; + goto cleanup; }Quoted 4 lines
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 169
Quoted 2 lines
drivers/acpi/acpi_mrrm.c +1
-0kobj =kobject_create_and_add (name, pkobj); +kfree (name);OK, this fixes a memory leak.
Quoted 7 lines
I didn't realize that
kobject_create_and_add()made its own copy of the "name" parameter. Maybe the code should avoid the alloc/free by making "name" a local variable?char name[16] = "rangeXXXXXXXXXX"; sprintf(&name[5], "%d", i);
That'd work.
Quoted 10 lines
Quoted 5 lines
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 172
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (!kobj) { + ret = -ENOMEM ; + goto cleanup;Why terminate this? Why not continue?
Terminating here (and below) will go to Kaushlendra's cleanup code to remove all the ranges. Maybe this is better than having some random subset of files appear (based on which allocations succeeded/failed)?
Well, I guess it can be argued both ways and either one is fine with me. If that's what you prefer, let it be done, but the changelog needs a bit of work because the cleanup is done in error cases.
Quoted 39 lines
Quoted 36 lines
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 169
Quoted 5 lines
drivers/acpi/acpi_mrrm.c +1
-2+ } ret =sysfs_create_groups (kobj, memory_range_groups); -if (ret) -return ret;Well, this returns already, but I'm not sure why. Tony, wouldn't it be better to continue?
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 178
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (ret) { +kobject_put (kobj); + goto cleanup;I would do a "continue" instead.
Quoted 21 lines
drivers/acpi/acpi_mrrm.c +16
-0+ } } +return 0 ; + +cleanup: +for (int j =0 ; j < i; j++) { +char cleanup_name[32 ]; +struct kobject *cleanup_kobj; + +snprintf (cleanup_name,sizeof (cleanup_name),"range%d" , j); + cleanup_kobj =kobject_get (pkobj); +if (cleanup_kobj) { +sysfs_remove_groups (cleanup_kobj, memory_range_groups); +kobject_put (cleanup_kobj); + } + } +kobject_put (pkobj);return ret; }Quoted 1 line
Quoted 1 line
--
-Tony
Quoted 6 lines · Rafael J. Wysocki
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 172
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (!kobj) { + ret = -ENOMEM ; + goto cleanup;Why terminate this? Why not continue?
Quoted 7 lines
I didn't realize that
kobject_create_and_add()made its own copy of the "name" parameter. Maybe the code should avoid the alloc/free by making "name" a local variable?char name[16] = "rangeXXXXXXXXXX"; sprintf(&name[5], "%d", i);Terminating here (and below) will go to Kaushlendra's cleanup code to remove all the ranges. Maybe this is better than having some random subset of files appear (based on which allocations succeeded/failed)?
Tony is exactly right here. I believe the current approach is more appropriate Here.
Regarding Tony's suggestion about the local buffer - that's a valid optimization, but I'd prefer to keep the current approach for clarity. The kasprintf/kfree pattern is more explicit about memory management in kernel.
The cleanup path ensures we maintain system consistency - either we successfully create the complete interface, or we leave the system in a clean state with no partial artifacts.
Thanks for the detailed review!
Kaushlendra
Quoted 13 lines
Quoted 11 lines · Rafael J. Wysocki
drivers/acpi/acpi_mrrm.c static __init int add_boot_memory_ranges(void) · line 172
Quoted 3 lines
drivers/acpi/acpi_mrrm.c +3
-0+if (!kobj) { + ret = -ENOMEM ; + goto cleanup;Why terminate this? Why not continue?
I didn't realize that
kobject_create_and_add()made its own copy of the "name" parameter. Maybe the code should avoid the alloc/free by making "name" a local variable?char name[16] = "rangeXXXXXXXXXX"; sprintf(&name[5], "%d", i);
Quoted 8 lines
Quoted 1 line
Terminating here (and below) will go to Kaushlendra's cleanup code to remove all the ranges. Maybe this is better than having some random subset of files appear (based on which allocations succeeded/failed)?
Tony is exactly right here. I believe the current approach is more appropriate Here.
Regarding Tony's suggestion about the local buffer - that's a valid optimization, but I'd prefer to keep the current approach for clarity. The kasprintf/kfree pattern is more explicit about memory management in kernel.
Why? I'll note that the cleanup code you added uses a local buffer "char cleanup_name[32];" to hold filenames (though now that I look back at the patch, that string isn't actually used!)
Quoted 6 lines
The cleanup path ensures we maintain system consistency - either we successfully create the complete interface, or we leave the system in a clean state with no partial artifacts.
Thanks for the detailed review!
-Tony
Quoted 2 lines · Tony Luck
Why? I'll note that the cleanup code you added uses a local buffer "char cleanup_name[32];" to hold filenames (though now that I look back at the patch, that string isn't actually used!)
Got it. Looking at the cleanup code again,
I see the inconsistency - I'm using a local buffer there but kasprintf() in the main loop.
I will use a local buffer approach.
Also thanks for catching the unused cleanup_name variable - I'll fix that in the cleanup logic as well.
I'll send a v2 with these improvements.
Thanks for the review!