[PATCH 3/3] platform/x86: think-lmi: Fix sysfs group cleanup

From: Kurt Borja
Date: Sat Jun 28 2025 - 01:02:17 EST


Many error paths in tlmi_sysfs_init() lead to sysfs groups being removed
when they were not even created.

Fix this by letting the kobject core manage these groups through their
kobj_type's defult_groups.

Fixes: a40cd7ef22fb ("platform/x86: think-lmi: Add WMI interface support on Lenovo platforms")
Signed-off-by: Kurt Borja <kuurtb@xxxxxxxxx>
---
drivers/platform/x86/lenovo/think-lmi.c | 43 +++------------------------------
1 file changed, 4 insertions(+), 39 deletions(-)

diff --git a/drivers/platform/x86/lenovo/think-lmi.c b/drivers/platform/x86/lenovo/think-lmi.c
index da3568727d79832f4669d7217e2fdf691acf1cf5..a38262295abad9f97f67208dc9e3f4f4c6b27b32 100644
--- a/drivers/platform/x86/lenovo/think-lmi.c
+++ b/drivers/platform/x86/lenovo/think-lmi.c
@@ -975,6 +975,7 @@ static const struct attribute_group auth_attr_group = {
.is_visible = auth_attr_is_visible,
.attrs = auth_attrs,
};
+__ATTRIBUTE_GROUPS(auth_attr);

/* ---- Attributes sysfs --------------------------------------------------------- */
static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *attr,
@@ -1190,6 +1191,7 @@ static const struct attribute_group tlmi_attr_group = {
.is_visible = attr_is_visible,
.attrs = tlmi_attrs,
};
+__ATTRIBUTE_GROUPS(tlmi_attr);

static void tlmi_attr_setting_release(struct kobject *kobj)
{
@@ -1209,11 +1211,13 @@ static void tlmi_pwd_setting_release(struct kobject *kobj)
static const struct kobj_type tlmi_attr_setting_ktype = {
.release = &tlmi_attr_setting_release,
.sysfs_ops = &kobj_sysfs_ops,
+ .default_groups = tlmi_attr_groups,
};

static const struct kobj_type tlmi_pwd_setting_ktype = {
.release = &tlmi_pwd_setting_release,
.sysfs_ops = &kobj_sysfs_ops,
+ .default_groups = auth_attr_groups,
};

static ssize_t pending_reboot_show(struct kobject *kobj, struct kobj_attribute *attr,
@@ -1383,14 +1387,8 @@ static struct kobj_attribute debug_cmd = __ATTR_WO(debug_cmd);
static void tlmi_release_attr(void)
{
struct kobject *pos, *n;
- int i;

/* Attribute structures */
- for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
- if (tlmi_priv.setting[i]) {
- sysfs_remove_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
- }
- }
sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &save_settings.attr);

@@ -1409,15 +1407,6 @@ static void tlmi_release_attr(void)
kfree(tlmi_priv.pwd_admin->save_signature);

/* Authentication structures */
- sysfs_remove_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
- sysfs_remove_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
-
- if (tlmi_priv.opcode_support) {
- sysfs_remove_group(&tlmi_priv.pwd_system->kobj, &auth_attr_group);
- sysfs_remove_group(&tlmi_priv.pwd_hdd->kobj, &auth_attr_group);
- sysfs_remove_group(&tlmi_priv.pwd_nvme->kobj, &auth_attr_group);
- }
-
list_for_each_entry_safe(pos, n, &tlmi_priv.authentication_kset->list, entry) {
kobject_del(pos);
kobject_put(pos);
@@ -1490,10 +1479,6 @@ static int tlmi_sysfs_init(void)
NULL, "%s", tlmi_priv.setting[i]->display_name);
if (ret)
goto fail_create_attr;
-
- ret = sysfs_create_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
- if (ret)
- goto fail_create_attr;
}

ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
@@ -1517,20 +1502,12 @@ static int tlmi_sysfs_init(void)
if (ret)
goto fail_create_attr;

- ret = sysfs_create_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
- if (ret)
- goto fail_create_attr;
-
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;

- ret = sysfs_create_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
- if (ret)
- goto fail_create_attr;
-
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,
@@ -1538,29 +1515,17 @@ static int tlmi_sysfs_init(void)
if (ret)
goto fail_create_attr;

- ret = sysfs_create_group(&tlmi_priv.pwd_system->kobj, &auth_attr_group);
- if (ret)
- goto fail_create_attr;
-
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;

- ret = sysfs_create_group(&tlmi_priv.pwd_hdd->kobj, &auth_attr_group);
- if (ret)
- goto fail_create_attr;
-
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;
-
- ret = sysfs_create_group(&tlmi_priv.pwd_nvme->kobj, &auth_attr_group);
- if (ret)
- goto fail_create_attr;
}

return ret;

--
2.50.0