Skip to contentSkip to conversation

ACPI: mrrm: Fix memory leaks and improve error handling

PATCH Superseded Kaushlendra Kumar 2 participants 3 messages Oct 29, 2025 ACPI

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 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..a554f6443b4b 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++) {
+		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;
 	}
 
+	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
#1 lore raw
Quoted 45 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 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 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 +5 -6
index 47ea3ccc2142..a554f6443b4b 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;

drivers/acpi/acpi_mrrm.c ATTRIBUTE_GROUPS(memory_range); · line 168

Quoted 10 lines
drivers/acpi/acpi_mrrm.c +7 -2
-                       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);

So someone will subsequently send a patch to replace this with scnprintf().

Quoted 32 lines
drivers/acpi/acpi_mrrm.c +21 -3
                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;
 }
 
--
#2 lore raw
Quoted 3 lines · Kaushlendra Kumar

+ snprintf(name, sizeof(name), "range%d", i);

So someone will subsequently send a patch to replace this with scnprintf().

Good point! I'll use scnprintf() in v4 to be future-proof and avoid the potential follow-up patch.

BR,
Kaushlendra