Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH v2] ACPI: mrrm: Fix memory leaks and improve error handling
@ 2025-10-29  9:10 Kaushlendra Kumar
  2025-10-29 15:23 ` Luck, Tony
  0 siblings, 1 reply; 3+ messages in thread
From: Kaushlendra Kumar @ 2025-10-29  9:10 UTC (permalink / raw)
  To: rafael, tony.luck; +Cc: linux-pm, 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
from kobject_create_and_add(), uses local buffer for range names to
avoid dynamic allocation, 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 <kaushlendra.kumar@intel.com>
---
Changes in v2:
- Use local buffer for range names instead of kasprintf/kfree as
  suggested in review.

 drivers/acpi/acpi_mrrm.c | 42 ++++++++++++++++++++++++++++++----------
 1 file changed, 32 insertions(+), 10 deletions(-)

diff --git a/drivers/acpi/acpi_mrrm.c b/drivers/acpi/acpi_mrrm.c
index 47ea3ccc2142..1718a35a5f56 100644
--- a/drivers/acpi/acpi_mrrm.c
+++ b/drivers/acpi/acpi_mrrm.c
@@ -149,26 +149,48 @@ ATTRIBUTE_GROUPS(memory_range);
 
 static __init int add_boot_memory_ranges(void)
 {
-	struct kobject *pkobj, *kobj;
+	struct kobject *pkobj, *kobj, **kobjs;
 	int ret = -EINVAL;
-	char *name;
+	char name[16];
+	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++) {
-		name = kasprintf(GFP_KERNEL, "range%d", i);
-		if (!name) {
-			ret = -ENOMEM;
-			break;
-		}
+	kobjs = kcalloc(mrrm_mem_entry_num, sizeof(*kobjs), GFP_KERNEL);
+	if (!kobjs) {
+		kobject_put(pkobj);
+		return -ENOMEM;
+	}
 
+	for (i = 0; i < mrrm_mem_entry_num; i++) {
+		snprintf(name, sizeof(name), "range%d", i);
 		kobj = kobject_create_and_add(name, pkobj);
+		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;
+		}
+		kobjs[i] = kobj;
 	}
 
+	return 0;
+
+cleanup:
+	for (int j = 0; j < i; j++) {
+		if (kobjs[j]) {
+			sysfs_remove_groups(kobjs[j], memory_range_groups);
+			kobject_put(kobjs[j]);
+		}
+	}
+	kfree(kobjs);
+	kobject_put(pkobj);
 	return ret;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] ACPI: mrrm: Fix memory leaks and improve error handling
  2025-10-29  9:10 [PATCH v2] ACPI: mrrm: Fix memory leaks and improve error handling Kaushlendra Kumar
@ 2025-10-29 15:23 ` Luck, Tony
  2025-10-29 15:42   ` Kumar, Kaushlendra
  0 siblings, 1 reply; 3+ messages in thread
From: Luck, Tony @ 2025-10-29 15:23 UTC (permalink / raw)
  To: Kaushlendra Kumar; +Cc: rafael, linux-pm

On Wed, Oct 29, 2025 at 02:40:13PM +0530, Kaushlendra Kumar wrote:
> 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(), uses local buffer for range names to
> avoid dynamic allocation, 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 <kaushlendra.kumar@intel.com>
> ---
> Changes in v2:
> - Use local buffer for range names instead of kasprintf/kfree as
>   suggested in review.
> 
>  drivers/acpi/acpi_mrrm.c | 42 ++++++++++++++++++++++++++++++----------
>  1 file changed, 32 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/acpi/acpi_mrrm.c b/drivers/acpi/acpi_mrrm.c
> index 47ea3ccc2142..1718a35a5f56 100644
> --- a/drivers/acpi/acpi_mrrm.c
> +++ b/drivers/acpi/acpi_mrrm.c
> @@ -149,26 +149,48 @@ ATTRIBUTE_GROUPS(memory_range);
>  
>  static __init int add_boot_memory_ranges(void)
>  {
> -	struct kobject *pkobj, *kobj;
> +	struct kobject *pkobj, *kobj, **kobjs;
>  	int ret = -EINVAL;
> -	char *name;
> +	char name[16];
> +	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++) {
> -		name = kasprintf(GFP_KERNEL, "range%d", i);
> -		if (!name) {
> -			ret = -ENOMEM;
> -			break;
> -		}
> +	kobjs = kcalloc(mrrm_mem_entry_num, sizeof(*kobjs), GFP_KERNEL);
> +	if (!kobjs) {
> +		kobject_put(pkobj);
> +		return -ENOMEM;
> +	}
>  
> +	for (i = 0; i < mrrm_mem_entry_num; i++) {
> +		snprintf(name, sizeof(name), "range%d", i);
>  		kobj = kobject_create_and_add(name, pkobj);
> +		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;
> +		}
> +		kobjs[i] = kobj;
>  	}
>  
Need to
	kfree(kobjs);
here.

> +	return 0;
> +
> +cleanup:
> +	for (int j = 0; j < i; j++) {
> +		if (kobjs[j]) {
> +			sysfs_remove_groups(kobjs[j], memory_range_groups);
> +			kobject_put(kobjs[j]);
> +		}
> +	}
> +	kfree(kobjs);
> +	kobject_put(pkobj);
>  	return ret;
>  }
>  
> -- 
> 2.34.1

-Tony

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH v2] ACPI: mrrm: Fix memory leaks and improve error handling
  2025-10-29 15:23 ` Luck, Tony
@ 2025-10-29 15:42   ` Kumar, Kaushlendra
  0 siblings, 0 replies; 3+ messages in thread
From: Kumar, Kaushlendra @ 2025-10-29 15:42 UTC (permalink / raw)
  To: Luck, Tony; +Cc: rafael@kernel.org, linux-pm@vger.kernel.org

On Wed, Oct 29, 2025 at 02:40:13PM +0530, Tony Luck wrote:
> Need to
> 	kfree(kobjs);
> here.

You're right! I missed freeing the kobjs array in the success 
path. I'll add the kfree(kobjs) before return 0 and send v3 patch.

Thanks for catching this!

Kaushlendra

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-29 15:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-29  9:10 [PATCH v2] ACPI: mrrm: Fix memory leaks and improve error handling Kaushlendra Kumar
2025-10-29 15:23 ` Luck, Tony
2025-10-29 15:42   ` Kumar, Kaushlendra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox