Skip to content

Commit d1e9dd5

Browse files
kaushlengregkh
authored andcommitted
ACPI: MRRM: Fix memory leaks and improve error handling
[ Upstream commit 4b93d21 ] 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> Reviewed-by: Tony Luck <tony.luck@intel.com> Link: https://patch.msgid.link/20251030023228.3956296-1-kaushlendra.kumar@intel.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent ae02664 commit d1e9dd5

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

drivers/acpi/acpi_mrrm.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,49 @@ ATTRIBUTE_GROUPS(memory_range);
152152

153153
static __init int add_boot_memory_ranges(void)
154154
{
155-
struct kobject *pkobj, *kobj;
155+
struct kobject *pkobj, *kobj, **kobjs;
156156
int ret = -EINVAL;
157-
char *name;
157+
char name[16];
158+
int i;
158159

159160
pkobj = kobject_create_and_add("memory_ranges", acpi_kobj);
161+
if (!pkobj)
162+
return -ENOMEM;
160163

161-
for (int i = 0; i < mrrm_mem_entry_num; i++) {
162-
name = kasprintf(GFP_KERNEL, "range%d", i);
163-
if (!name) {
164-
ret = -ENOMEM;
165-
break;
166-
}
164+
kobjs = kcalloc(mrrm_mem_entry_num, sizeof(*kobjs), GFP_KERNEL);
165+
if (!kobjs) {
166+
kobject_put(pkobj);
167+
return -ENOMEM;
168+
}
167169

170+
for (i = 0; i < mrrm_mem_entry_num; i++) {
171+
scnprintf(name, sizeof(name), "range%d", i);
168172
kobj = kobject_create_and_add(name, pkobj);
173+
if (!kobj) {
174+
ret = -ENOMEM;
175+
goto cleanup;
176+
}
169177

170178
ret = sysfs_create_groups(kobj, memory_range_groups);
171-
if (ret)
172-
return ret;
179+
if (ret) {
180+
kobject_put(kobj);
181+
goto cleanup;
182+
}
183+
kobjs[i] = kobj;
173184
}
174185

186+
kfree(kobjs);
187+
return 0;
188+
189+
cleanup:
190+
for (int j = 0; j < i; j++) {
191+
if (kobjs[j]) {
192+
sysfs_remove_groups(kobjs[j], memory_range_groups);
193+
kobject_put(kobjs[j]);
194+
}
195+
}
196+
kfree(kobjs);
197+
kobject_put(pkobj);
175198
return ret;
176199
}
177200

0 commit comments

Comments
 (0)