From: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
To: rafael@kernel.org, lenb@kernel.org
Cc: linux-acpi@vger.kernel.org,
Kaushlendra Kumar <kaushlendra.kumar@intel.com>
Subject: [PATCH] ACPI: mrrm: Fix memory leaks and improve error handling
Date: Tue, 7 Oct 2025 15:52:37 +0530 [thread overview]
Message-ID: <20251007102237.1015610-1-kaushlendra.kumar@intel.com> (raw)
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.
Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
---
drivers/acpi/acpi_mrrm.c | 33 +++++++++++++++++++++++++++++----
1 file changed, 29 insertions(+), 4 deletions(-)
diff --git a/drivers/acpi/acpi_mrrm.c b/drivers/acpi/acpi_mrrm.c
index 47ea3ccc2142..6ec42eb48783 100644
--- a/drivers/acpi/acpi_mrrm.c
+++ b/drivers/acpi/acpi_mrrm.c
@@ -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;
}
--
2.34.1
next reply other threads:[~2025-10-07 10:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-07 10:22 Kaushlendra Kumar [this message]
2025-10-22 19:31 ` [PATCH] ACPI: mrrm: Fix memory leaks and improve error handling Rafael J. Wysocki
2025-10-22 19:58 ` Luck, Tony
2025-10-22 20:17 ` Rafael J. Wysocki
2025-10-28 4:48 ` Kumar, Kaushlendra
2025-10-28 16:04 ` Luck, Tony
2025-10-28 16:17 ` Kumar, Kaushlendra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251007102237.1015610-1-kaushlendra.kumar@intel.com \
--to=kaushlendra.kumar@intel.com \
--cc=lenb@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).