From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.112 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796390800; b=IPMbjbALfyc34C/oKpgyHDNlW6fATaU4MWvfGtkBSAWNutOwQmIDOURDnsXLjdRUsUMU 91Yv+aiskClp1LQeNMEItwiAX0ziQmAmaei+rF/qCmNbbma4SmSfXlIq0Bm0xW2vsLmhe zB0npC6ha+Vjh0loxFh3JZIEuVJIE1439HNhdf00jEdqYKBL5nBG3nM2V1vgOXONwuHXN UefnUEW8rnqYkCeCE9KEK9DByYilo7dBTzvnlNQT5QhVunITuGjmtkUFioATGyMqkOAyH QBiBCW7Oq31g4HN+rkifXboWmNoAALt41s005ukPuqlxKmgQzr30kEclNgkNR2UVwrQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796390800; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Subject:From:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date: MIME-Version:User-Agent:X-ZohoMailClient; bh=LS3zyH8zIPD8mRMa2n1T41f+OylkrQiG6xMKTMGseMQ=; b=HNJrrekm3k8R6u9oBeUeYsHUEMhDj/QSiMHsz9VcGYNTCQ+2ApNwBFdJkU/eU3oAHKZn vUjw7ClErLLi2+Dpgi6Tt2zGmkVlM5ZtGKQip8814AihQxyxTO/J+R8yT9JSL3/8Aeoak XtQCFa73uxCB/Ul+m8QNIT9vO6XeqWrpAPYttLG4e0/UVJZHTu8A9G7yna6FaU6ZgNy68 cK1aChhtsmayzaUHHfWyDZ+XKxOcusb5eJsjEL2H8RaHGFs/P/v4UU9ARZfWQ/m9sY6YT bar7CKS9vIm0CMJY7jHFlN0WhEiXs8dIjNondbjGz2GDJ/wS75fUCRUBU0lYZZDWzug== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=iJALXZbO; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.112 Received: from sender5-pp-f112.zoho.com (sender5-pp-f112.zoho.com [165.173.182.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2A08606A322; Fri, 4 Dec 2026 13:26:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C2A08606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=iJALXZbO ARC-Seal: i=1; a=rsa-sha256; t=1796390791; cv=none; d=zohomail.com; s=zohoarc; b=V+SQEZXe/zMa4YSzs6YLVd3VqJAJe2HMnuX/icEgc0na4p3z/tsdsndUVtwRSOUWO4HC7G7EJkdyz/aIx0uxWcpbtbq5dX/xyDWPhoTsjEtAetVD9m3t/03mUDPzwEfM+Y7ooph9gexgsrhOQcpy5x3f9a1q569bWXfYivOuGmY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796390791; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=LS3zyH8zIPD8mRMa2n1T41f+OylkrQiG6xMKTMGseMQ=; b=RUdSI9MSwVt00LbB3XAIviulI9o5cAFF4rDN5K1kl1BxU6p3PQghzrCN3DN44Ng4kWs62RLKLr2P+EoZOVgbUCtoIThEgoUNNdYaAnobXrCOqV/kjwyaD2IuzSI10rmIuferEwuixHpYB60K9rV56Ok/0en3ksNm2fe0L7wijPQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=iJALXZbONmMlBM+GXEfa5Hkb+EO5G1kKa2S2tYU3C2UtuNBFNmLfcelUGzQ0aw2gKQFhc/ncSopPs4GDZ7YRxm9EEgc1J9ok1+oepcHkmGg2Cgzchja9UlMcsfodNNYYEI0SKeM3dv5JfO7MO6BoTOMd1Yl74RaPiDsC/XgQmcoeems1yFo+XKHJTu4fK0D/eKL7HzxepK2B+aM4O3Jzz7wJM9AvfnGldpLfCwgdIrJlOSrmlmqEKceTkv7CjFN3H4G3fqyXXExEMl8olH1tuCZiSSg2PX9Ux5DfdeHpiR75QVj/6otopt0uc0gwzJDFUQCUCbq9Iz042p6S7grH+g==; c=relaxed/relaxed; s=zmail2048; d=rong.moe; v=1; bh=LS3zyH8zIPD8mRMa2n1T41f+OylkrQiG6xMKTMGseMQ=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796390789543745.6096243802479; Fri, 4 Dec 2026 05:26:29 -0800 (PST) Message-ID: <959f9607dfb935549fd4ce1a14a02ae32654abdc.camel@rong.moe> Subject: Re: [PATCH 02/13] platform/x86: lenovo: think-lmi: Clean up attributes if not added to list From: Rong Zhang To: Mark Pearson Cc: derekjohn.clark@gmail.com, hansg@kernel.org, ilpo.jarvinen@linux.intel.com, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20261204022032.3074618-3-mpearson-lenovo@squebb.ca> References: <20261204022032.3074618-1-mpearson-lenovo@squebb.ca> <20261204022032.3074618-3-mpearson-lenovo@squebb.ca> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 21:20:24 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Evolution 3.62.2-1 X-ZohoMailClient: External 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 no= t > get freed during the subsequent kobject_put. > Make sure to free resources before going ahead with the rest of the > cleanup. >=20 > Signed-off-by: Mark Pearson > --- > drivers/platform/x86/lenovo/think-lmi.c | 87 ++++++++++++++++++------- > 1 file changed, 65 insertions(+), 22 deletions(-) >=20 > diff --git a/drivers/platform/x86/lenovo/think-lmi.c b/drivers/platform/x= 86/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 *kob= j, struct kobj_attribute *attr > static struct kobj_attribute debug_cmd =3D __ATTR_WO(debug_cmd); > =20 > /* ---- Initialisation -------------------------------------------------= -------- */ > +/* Utility function to free record when not in the attribute_kset list *= / > +static void tlmi_free_attr_record(int i) > +{ > +=09kfree(tlmi_priv.setting[i]->name); > +=09kfree(tlmi_priv.setting[i]->display_name); > +=09kfree(tlmi_priv.setting[i]->possible_values); > +=09kfree(tlmi_priv.setting[i]); > +=09tlmi_priv.setting[i] =3D NULL; > +} > + > static void tlmi_release_attr(void) > { > =09struct kobject *pos, *n; > @@ -1461,8 +1471,10 @@ static void tlmi_release_attr(void) > =09kset_unregister(tlmi_priv.attribute_kset); > =20 > =09/* Free up any saved signatures */ > -=09kfree(tlmi_priv.pwd_admin->signature); > -=09kfree(tlmi_priv.pwd_admin->save_signature); > +=09if (tlmi_priv.pwd_admin) { > +=09=09kfree(tlmi_priv.pwd_admin->signature); > +=09=09kfree(tlmi_priv.pwd_admin->save_signature); > +=09} > =09if (tlmi_priv.pwd_system) { > =09=09kfree(tlmi_priv.pwd_system->signature); > =09=09kfree(tlmi_priv.pwd_system->save_signature); > @@ -1495,8 +1507,9 @@ static int tlmi_validate_setting_name(struct kset *= attribute_kset, char *name) > =20 > static int tlmi_sysfs_init(void) > { > -=09int i, ret; > +=09int i, ret =3D 0, auth_ret =3D 0; > =20 > +=09guard(mutex)(&tlmi_mutex); It'd better to add a comment explaining what's serialized during initialization (gating attribute access?). > =09tlmi_priv.class_dev =3D device_create(&firmware_attributes_class, NUL= L, MKDEV(0, 0), > =09=09=09NULL, "%s", "thinklmi"); > =09if (IS_ERR(tlmi_priv.class_dev)) { > @@ -1524,14 +1537,15 @@ static int tlmi_sysfs_init(void) > =09=09if (!tlmi_priv.setting[i]) > =09=09=09continue; > =20 > +=09=09if (ret) { /* We had a critical failure. Just clean up */ > +=09=09=09tlmi_free_attr_record(i); > +=09=09=09continue; > +=09=09} > + > =09=09/* check for duplicate or reserved values */ > =09=09if (tlmi_validate_setting_name(tlmi_priv.attribute_kset, > =09=09=09=09=09 tlmi_priv.setting[i]->display_name) < 0) { > -=09=09=09kfree(tlmi_priv.setting[i]->name); > -=09=09=09kfree(tlmi_priv.setting[i]->display_name); > -=09=09=09kfree(tlmi_priv.setting[i]->possible_values); > -=09=09=09kfree(tlmi_priv.setting[i]); > -=09=09=09tlmi_priv.setting[i] =3D NULL; > +=09=09=09tlmi_free_attr_record(i); > =09=09=09continue; > =09=09} > =20 > @@ -1539,9 +1553,14 @@ static int tlmi_sysfs_init(void) > =09=09tlmi_priv.setting[i]->kobj.kset =3D tlmi_priv.attribute_kset; > =09=09ret =3D kobject_init_and_add(&tlmi_priv.setting[i]->kobj, &tlmi_at= tr_setting_ktype, > =09=09=09=09=09 NULL, "%s", tlmi_priv.setting[i]->display_name); > -=09=09if (ret) > -=09=09=09goto fail_create_attr; > +=09=09if (ret) { > +=09=09=09kobject_put(&tlmi_priv.setting[i]->kobj); > +=09=09=09continue; /* use loop to clear remaining items */ > +=09=09} > =09} > +=09/* Check if we had an error during the attribute creations */ > +=09if (ret) > +=09=09goto fail_create_attr; > =20 > =09ret =3D sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &pending_r= eboot.attr); > =09if (ret) > @@ -1561,39 +1580,63 @@ static int tlmi_sysfs_init(void) > =09tlmi_priv.pwd_admin->kobj.kset =3D tlmi_priv.authentication_kset; > =09ret =3D kobject_init_and_add(&tlmi_priv.pwd_admin->kobj, &tlmi_pwd_se= tting_ktype, > =09=09=09=09 NULL, "%s", "Admin"); > -=09if (ret) > -=09=09goto fail_create_attr; > - > +=09if (ret) { > +=09=09kobject_put(&tlmi_priv.pwd_admin->kobj); > +=09=09tlmi_priv.pwd_admin =3D NULL; > +=09=09auth_ret =3D ret; > +=09} Let's factor out a helper function like: kobj.kset =3D ... ret =3D kobject_init_and_add(); if (ret) { =09... } > =09tlmi_priv.pwd_power->kobj.kset =3D tlmi_priv.authentication_kset; > =09ret =3D kobject_init_and_add(&tlmi_priv.pwd_power->kobj, &tlmi_pwd_se= tting_ktype, > =09=09=09=09 NULL, "%s", "Power-on"); > -=09if (ret) > -=09=09goto fail_create_attr; > +=09if (ret) { > +=09=09kobject_put(&tlmi_priv.pwd_power->kobj); > +=09=09tlmi_priv.pwd_power =3D NULL; > +=09=09auth_ret =3D ret; > +=09} > =20 > =09if (tlmi_priv.opcode_support) { > =09=09tlmi_priv.pwd_system->kobj.kset =3D tlmi_priv.authentication_kset; > =09=09ret =3D kobject_init_and_add(&tlmi_priv.pwd_system->kobj, &tlmi_pw= d_setting_ktype, > =09=09=09=09=09 NULL, "%s", "System"); > -=09=09if (ret) > -=09=09=09goto fail_create_attr; > +=09=09if (ret) { > +=09=09=09kobject_put(&tlmi_priv.pwd_system->kobj); > +=09=09=09tlmi_priv.pwd_system =3D NULL; > +=09=09=09auth_ret =3D ret; > +=09=09} > =20 > =09=09tlmi_priv.pwd_hdd->kobj.kset =3D tlmi_priv.authentication_kset; > =09=09ret =3D kobject_init_and_add(&tlmi_priv.pwd_hdd->kobj, &tlmi_pwd_s= etting_ktype, > =09=09=09=09=09 NULL, "%s", "HDD"); > -=09=09if (ret) > -=09=09=09goto fail_create_attr; > +=09=09if (ret) { > +=09=09=09kobject_put(&tlmi_priv.pwd_hdd->kobj); > +=09=09=09tlmi_priv.pwd_hdd =3D NULL; > +=09=09=09auth_ret =3D ret; > +=09=09} > =20 > =09=09tlmi_priv.pwd_nvme->kobj.kset =3D tlmi_priv.authentication_kset; > =09=09ret =3D kobject_init_and_add(&tlmi_priv.pwd_nvme->kobj, &tlmi_pwd_= setting_ktype, > =09=09=09=09=09 NULL, "%s", "NVMe"); > -=09=09if (ret) > -=09=09=09goto fail_create_attr; > +=09=09if (ret) { > +=09=09=09kobject_put(&tlmi_priv.pwd_nvme->kobj); > +=09=09=09tlmi_priv.pwd_nvme =3D NULL; > +=09=09=09auth_ret =3D ret; > +=09=09} > +=09} > +=09/* If one of the auth entries fail, we'd best go and clean up */ > +=09if (auth_ret) { > +=09=09ret =3D auth_ret; > +=09=09goto fail_create_attr; Why would you need to defer the bail-out path? > =09} > - > =09return ret; > =20 > fail_create_attr: > +=09/* Release any kobject items */ > =09tlmi_release_attr(); tlmi_release_attr() calls kobject_put() without nullifying tlmi_priv.pwd_*, so ... > +=09kfree(tlmi_priv.pwd_admin); > +=09kfree(tlmi_priv.pwd_power); > +=09kfree(tlmi_priv.pwd_system); > +=09kfree(tlmi_priv.pwd_hdd); > +=09kfree(tlmi_priv.pwd_nvme); a bunch of double free. Thanks, Rong > fail_device_created: > =09device_unregister(tlmi_priv.class_dev); > fail_class_created: