Re: [PATCH 02/13] platform/x86: lenovo: think-lmi: Clean up attributes if not added to list

From: Mark Pearson

Date: Fri Dec 04 2026 - 10:10:17 EST




On Fri, Dec 4, 2026, at 8:20 AM, Rong Zhang wrote:
> Hi Mark,
>
> On Thu, 2026-12-03 at 21:20 -0500, Mark Pearson wrote:
>> 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@xxxxxxxxx>
>> ---
>> 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);
>
> It'd better to add a comment explaining what's serialized during
> initialization (gating attribute access?).
>
>> 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;
>> + }
>
> Let's factor out a helper function like:
>
> kobj.kset = ...
> ret = kobject_init_and_add();
> if (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;
>
> Why would you need to defer the bail-out path?
>
>> }
>> -
>> return ret;
>>
>> fail_create_attr:
>> + /* Release any kobject items */
>> tlmi_release_attr();
>
> tlmi_release_attr() calls kobject_put() without nullifying
> tlmi_priv.pwd_*, so ...
>
>> + 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);
>
> a bunch of double free.
>
Thanks - agreed on all the points. Will address in v2

Mark