mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mark Pearson <mpearson-lenovo@squebb.ca>
To: mpearson-lenovo@squebb.ca
Cc: derekjohn.clark@gmail.com, i@rong.moe, hansg@kernel.org,
	ilpo.jarvinen@linux.intel.com,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 02/13] platform/x86: lenovo: think-lmi: Clean up attributes if not added to list
Date: Thu,  3 Dec 2026 21:20:11 -0500	[thread overview]
Message-ID: <20261204022032.3074618-3-mpearson-lenovo@squebb.ca> (raw)
In-Reply-To: <20261204022032.3074618-1-mpearson-lenovo@squebb.ca>

If we fail to add an attribute to the attribute_kset list then it will not
get freed during the subsequent kobject_put.
Make sure to free resources before going ahead with the rest of the
cleanup.

Signed-off-by: Mark Pearson <mpearson-lenovo@squebb.ca>
---
 drivers/platform/x86/lenovo/think-lmi.c | 87 ++++++++++++++++++-------
 1 file changed, 65 insertions(+), 22 deletions(-)

diff --git a/drivers/platform/x86/lenovo/think-lmi.c b/drivers/platform/x86/lenovo/think-lmi.c
index c060a0a360ca..63ea212b4bda 100644
--- a/drivers/platform/x86/lenovo/think-lmi.c
+++ b/drivers/platform/x86/lenovo/think-lmi.c
@@ -1444,6 +1444,16 @@ static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attr
 static struct kobj_attribute debug_cmd = __ATTR_WO(debug_cmd);
 
 /* ---- Initialisation --------------------------------------------------------- */
+/* Utility function to free record when not in the attribute_kset list */
+static void tlmi_free_attr_record(int i)
+{
+	kfree(tlmi_priv.setting[i]->name);
+	kfree(tlmi_priv.setting[i]->display_name);
+	kfree(tlmi_priv.setting[i]->possible_values);
+	kfree(tlmi_priv.setting[i]);
+	tlmi_priv.setting[i] = NULL;
+}
+
 static void tlmi_release_attr(void)
 {
 	struct kobject *pos, *n;
@@ -1461,8 +1471,10 @@ static void tlmi_release_attr(void)
 	kset_unregister(tlmi_priv.attribute_kset);
 
 	/* Free up any saved signatures */
-	kfree(tlmi_priv.pwd_admin->signature);
-	kfree(tlmi_priv.pwd_admin->save_signature);
+	if (tlmi_priv.pwd_admin) {
+		kfree(tlmi_priv.pwd_admin->signature);
+		kfree(tlmi_priv.pwd_admin->save_signature);
+	}
 	if (tlmi_priv.pwd_system) {
 		kfree(tlmi_priv.pwd_system->signature);
 		kfree(tlmi_priv.pwd_system->save_signature);
@@ -1495,8 +1507,9 @@ static int tlmi_validate_setting_name(struct kset *attribute_kset, char *name)
 
 static int tlmi_sysfs_init(void)
 {
-	int i, ret;
+	int i, ret = 0, auth_ret = 0;
 
+	guard(mutex)(&tlmi_mutex);
 	tlmi_priv.class_dev = device_create(&firmware_attributes_class, NULL, MKDEV(0, 0),
 			NULL, "%s", "thinklmi");
 	if (IS_ERR(tlmi_priv.class_dev)) {
@@ -1524,14 +1537,15 @@ static int tlmi_sysfs_init(void)
 		if (!tlmi_priv.setting[i])
 			continue;
 
+		if (ret) { /* We had a critical failure. Just clean up */
+			tlmi_free_attr_record(i);
+			continue;
+		}
+
 		/* check for duplicate or reserved values */
 		if (tlmi_validate_setting_name(tlmi_priv.attribute_kset,
 					       tlmi_priv.setting[i]->display_name) < 0) {
-			kfree(tlmi_priv.setting[i]->name);
-			kfree(tlmi_priv.setting[i]->display_name);
-			kfree(tlmi_priv.setting[i]->possible_values);
-			kfree(tlmi_priv.setting[i]);
-			tlmi_priv.setting[i] = NULL;
+			tlmi_free_attr_record(i);
 			continue;
 		}
 
@@ -1539,9 +1553,14 @@ static int tlmi_sysfs_init(void)
 		tlmi_priv.setting[i]->kobj.kset = tlmi_priv.attribute_kset;
 		ret = kobject_init_and_add(&tlmi_priv.setting[i]->kobj, &tlmi_attr_setting_ktype,
 					   NULL, "%s", tlmi_priv.setting[i]->display_name);
-		if (ret)
-			goto fail_create_attr;
+		if (ret) {
+			kobject_put(&tlmi_priv.setting[i]->kobj);
+			continue; /* use loop to clear remaining items */
+		}
 	}
+	/* Check if we had an error during the attribute creations */
+	if (ret)
+		goto fail_create_attr;
 
 	ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
 	if (ret)
@@ -1561,39 +1580,63 @@ static int tlmi_sysfs_init(void)
 	tlmi_priv.pwd_admin->kobj.kset = tlmi_priv.authentication_kset;
 	ret = kobject_init_and_add(&tlmi_priv.pwd_admin->kobj, &tlmi_pwd_setting_ktype,
 				   NULL, "%s", "Admin");
-	if (ret)
-		goto fail_create_attr;
-
+	if (ret) {
+		kobject_put(&tlmi_priv.pwd_admin->kobj);
+		tlmi_priv.pwd_admin = NULL;
+		auth_ret = ret;
+	}
 	tlmi_priv.pwd_power->kobj.kset = tlmi_priv.authentication_kset;
 	ret = kobject_init_and_add(&tlmi_priv.pwd_power->kobj, &tlmi_pwd_setting_ktype,
 				   NULL, "%s", "Power-on");
-	if (ret)
-		goto fail_create_attr;
+	if (ret) {
+		kobject_put(&tlmi_priv.pwd_power->kobj);
+		tlmi_priv.pwd_power = NULL;
+		auth_ret = ret;
+	}
 
 	if (tlmi_priv.opcode_support) {
 		tlmi_priv.pwd_system->kobj.kset = tlmi_priv.authentication_kset;
 		ret = kobject_init_and_add(&tlmi_priv.pwd_system->kobj, &tlmi_pwd_setting_ktype,
 					   NULL, "%s", "System");
-		if (ret)
-			goto fail_create_attr;
+		if (ret) {
+			kobject_put(&tlmi_priv.pwd_system->kobj);
+			tlmi_priv.pwd_system = NULL;
+			auth_ret = ret;
+		}
 
 		tlmi_priv.pwd_hdd->kobj.kset = tlmi_priv.authentication_kset;
 		ret = kobject_init_and_add(&tlmi_priv.pwd_hdd->kobj, &tlmi_pwd_setting_ktype,
 					   NULL, "%s", "HDD");
-		if (ret)
-			goto fail_create_attr;
+		if (ret) {
+			kobject_put(&tlmi_priv.pwd_hdd->kobj);
+			tlmi_priv.pwd_hdd = NULL;
+			auth_ret = ret;
+		}
 
 		tlmi_priv.pwd_nvme->kobj.kset = tlmi_priv.authentication_kset;
 		ret = kobject_init_and_add(&tlmi_priv.pwd_nvme->kobj, &tlmi_pwd_setting_ktype,
 					   NULL, "%s", "NVMe");
-		if (ret)
-			goto fail_create_attr;
+		if (ret) {
+			kobject_put(&tlmi_priv.pwd_nvme->kobj);
+			tlmi_priv.pwd_nvme = NULL;
+			auth_ret = ret;
+		}
+	}
+	/* If one of the auth entries fail, we'd best go and clean up */
+	if (auth_ret) {
+		ret = auth_ret;
+		goto fail_create_attr;
 	}
-
 	return ret;
 
 fail_create_attr:
+	/* Release any kobject items */
 	tlmi_release_attr();
+	kfree(tlmi_priv.pwd_admin);
+	kfree(tlmi_priv.pwd_power);
+	kfree(tlmi_priv.pwd_system);
+	kfree(tlmi_priv.pwd_hdd);
+	kfree(tlmi_priv.pwd_nvme);
 fail_device_created:
 	device_unregister(tlmi_priv.class_dev);
 fail_class_created:
-- 
2.43.0


  parent reply	other threads:[~2026-12-04  2:20 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  2:20 [PATCH 00/13] New Lenovo common WMI driver - tlmi Mark Pearson
2026-12-04  2:20 ` [PATCH 01/13] platform/x86: lenovo: think-lmi: Convert name arrays to pointers Mark Pearson
2026-12-04  2:20 ` Mark Pearson [this message]
2026-12-04 13:20   ` [PATCH 02/13] platform/x86: lenovo: think-lmi: Clean up attributes if not added to list Rong Zhang
2026-12-04 15:09     ` Mark Pearson
2026-12-04  2:20 ` [PATCH 03/13] platform/x86: lenovo: think-lmi: cache current value at init Mark Pearson
2026-12-04 13:28   ` Rong Zhang
2026-12-04 15:14     ` Mark Pearson
2026-12-04  2:20 ` [PATCH 04/13] platform/x86: lenovo: think-lmi: store attributes as linked list Mark Pearson
2026-12-04 13:30   ` Rong Zhang
2026-12-04 15:21     ` Mark Pearson
2026-12-04 16:10       ` Rong Zhang
2026-12-04  2:20 ` [PATCH 05/13] platform/x86: lenovo: tlmi: New tlmi driver for common WMI implementation Mark Pearson
2026-12-04 13:41   ` Rong Zhang
2026-12-04 15:27     ` Mark Pearson
2026-12-05  9:54   ` kernel test robot
2026-12-05  9:55   ` kernel test robot
2026-12-04  2:20 ` [PATCH 06/13] platform/x86: lenovo: tlmi: Add BIOS attributes sysfs entries Mark Pearson
2026-12-04 13:44   ` Rong Zhang
2026-12-04 15:41     ` Mark Pearson
2026-12-04 17:02       ` Rong Zhang
2026-12-04  2:20 ` [PATCH 07/13] platform/x86: lenovo: tlmi: Retrieve auth information Mark Pearson
2026-12-04 13:48   ` Rong Zhang
2026-12-04 15:44     ` Mark Pearson
2026-12-04 17:14       ` Rong Zhang
2026-12-04 17:26         ` Mark Pearson
2026-12-04  2:20 ` [PATCH 08/13] platform/x86: lenovo: Move TLMI helper functions into wmi-helpers Mark Pearson
2026-12-04 13:49   ` Rong Zhang
2026-12-04 15:50     ` Mark Pearson
2026-12-04 17:19       ` Rong Zhang
2026-12-04 17:27         ` Mark Pearson
2026-12-04  2:20 ` [PATCH 09/13] platform/x86: lenovo: tlmi: Add authentication sysfs entries Mark Pearson
2026-12-04 13:52   ` Rong Zhang
2026-12-05  9:55   ` kernel test robot
2026-12-04  2:20 ` [PATCH 10/13] platform/x86: lenovo: tlmi: Add platform attribute support Mark Pearson
2026-12-04  2:20 ` [PATCH 11/13] platform/x86: lenovo: tlmi: Add function attributes support Mark Pearson
2026-12-04  2:20 ` [PATCH 12/13] platform/x86: lenovo: tlmi: Add method identifier Mark Pearson
2026-12-04  2:20 ` [PATCH 13/13] docs: ABI: sysfs-class-firmware-attributes: Update Lenovo sections Mark Pearson
2026-12-04 13:19 ` [PATCH 00/13] New Lenovo common WMI driver - tlmi Rong Zhang
2026-12-04 14:42   ` Mark Pearson
2026-12-04 17:49     ` Rong Zhang
2026-12-04 18:22       ` Mark Pearson
2026-12-04 20:04         ` Rong Zhang

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=20261204022032.3074618-3-mpearson-lenovo@squebb.ca \
    --to=mpearson-lenovo@squebb.ca \
    --cc=derekjohn.clark@gmail.com \
    --cc=hansg@kernel.org \
    --cc=i@rong.moe \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=platform-driver-x86@vger.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

all inboxes | Powered by JetHome®