Skip to contentSkip to conversation

ACPI: mrrm: Fix memory leaks and improve error handling

PATCH Applied Kaushlendra Kumar 3 participants 3 messages Oct 30, 2025 – Nov 7, 2025 ACPI

lore
Share & tools
lore
#0 lore raw

PATCH v4 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(), 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.

---
Changes in v4:
- Use scnprintf() instead of snprintf() for safer string formatting

Changes in v3:
- Fix memory leak in success path by adding kfree(kobjs) before return.

Changes in v2:
- Use local buffer for range names instead of kasprintf/kfree as suggested in review.

---
 drivers/acpi/acpi_mrrm.c | 43 ++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)
drivers/acpi/acpi_mrrm.c +33 -10
index 47ea3ccc2142..c0a5caeff2d7 100644
@@ -149,26 +149,49 @@ 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++) {
+		scnprintf(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;
 	}
 
+	kfree(kobjs);
+	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;
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v4 1/1
#1 lore raw
Quoted 10 lines

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.

-Tony

maintainer Applied
#2 lore raw
Quoted 7 lines · Tony
Quoted 7 lines

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.

Quoted 6 lines
Quoted 4 lines

This prevents resource leaks when kobject creation or sysfs group creation fails during boot memory range initialization.

Applied as 6.18-rc material, thanks!