Re: [PATCH 03/13] platform/x86: lenovo: think-lmi: cache current value at init

From: Rong Zhang

Date: Fri Dec 04 2026 - 08:33:46 EST


Hi Mark,

On Thu, 2026-12-03 at 21:20 -0500, Mark Pearson wrote:
> When we read in current BIOS setting at init, cache it then to avoid
> having to process it later, as it doesn't change.
>
> This does introduce a couple of changes in behaviour:
>
> - If the attribute value is changed we modify the cached value to show
> what was set. Previously this would have read back the value - which
> would have been wrong, so this should be an improvement.
>
> - Previously a malformed attribute (invalid name or value) would have
> been registered, whereas now it is skipped. It is better not to show
> invalid entries
>
> During code review it picked up that if attributes are cleared the
> setting pointer could be leaked with a driver re-probe.
>
> Signed-off-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
> ---
> drivers/platform/x86/lenovo/think-lmi.c | 67 ++++++++++++++++---------
> drivers/platform/x86/lenovo/think-lmi.h | 1 +
> 2 files changed, 43 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/think-lmi.c b/drivers/platform/x86/lenovo/think-lmi.c
> index 63ea212b4bda..6580b1401f01 100644
> --- a/drivers/platform/x86/lenovo/think-lmi.c
> +++ b/drivers/platform/x86/lenovo/think-lmi.c
> @@ -1047,25 +1047,11 @@ static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *at
> static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> {
> struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
> - char *item, *value;
> - int ret;
> -
> - ret = tlmi_setting(setting->wdev, setting->index, &item);
> - if (ret)
> - return ret;
>
> - /* validate and split from `item,value` -> `value` */
> - value = strpbrk(item, ",");
> - if (!value || value == item || !strlen(value + 1))
> - ret = -EINVAL;
> - else {
> - /* On Workstations remove the Options part after the value */
> - strreplace(value, ';', '\0');
> - ret = sysfs_emit(buf, "%s\n", value + 1);
> - }
> - kfree(item);
> -
> - return ret;
> + guard(mutex)(&tlmi_mutex);
> + if (!setting->current_value)
> + return sysfs_emit(buf, "unknown\n");
> + return sysfs_emit(buf, "%s\n", setting->current_value);
> }
>
> static ssize_t possible_values_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> @@ -1204,8 +1190,16 @@ static ssize_t current_value_store(struct kobject *kobj,
> ret = tlmi_save_bios_settings("");
> }
> }
> - if (!ret && !tlmi_priv.pending_changes) {
> - tlmi_priv.pending_changes = true;
> + if (!ret) {
> + char *s;
> +
> + if (!tlmi_priv.pending_changes)

Is the `if' doing anything? Overriding true with true is idempotent.

> + tlmi_priv.pending_changes = true;
> + /* Update setting so read back shows correctly */
> + s = kstrdup(new_setting, GFP_KERNEL);
> + kfree(setting->current_value);
> + /* if kstrdup fails, show will handle null value */
> + setting->current_value = s;

Since new_setting is going to be freed later, i.e., no one else will use
it, why not assign it to setting->current_value directly?

Hint: adopt __free() and guard(), and then no_free_ptr() is here to help
you.

> /* let userland know it may need to check reboot pending again */
> kobject_uevent(&tlmi_priv.class_dev->kobj, KOBJ_CHANGE);

What's the intent here? Notify on every successful write or only on
tlmi_priv.pending_changes flip?

Thanks,
Rong

> }
> @@ -1258,6 +1252,7 @@ static void tlmi_attr_setting_release(struct kobject *kobj)
> kfree(setting->name);
> kfree(setting->display_name);
> kfree(setting->possible_values);
> + kfree(setting->current_value);
> kfree(setting);
> }
>
> @@ -1450,6 +1445,7 @@ 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]->current_value);
> kfree(tlmi_priv.setting[i]);
> tlmi_priv.setting[i] = NULL;
> }
> @@ -1469,6 +1465,8 @@ static void tlmi_release_attr(void)
> kobject_put(pos);
>
> kset_unregister(tlmi_priv.attribute_kset);
> + /* Clear up settings array */
> + memset(tlmi_priv.setting, 0, sizeof(tlmi_priv.setting));
>
> /* Free up any saved signatures */
> if (tlmi_priv.pwd_admin) {
> @@ -1707,9 +1705,8 @@ static int tlmi_analyze(struct wmi_device *wdev)
> */
> for (i = 0; i < TLMI_SETTINGS_COUNT; ++i) {
> struct tlmi_attr_setting *setting;
> - char *item = NULL;
> + char *item = NULL, *value;
>
> - tlmi_priv.setting[i] = NULL;
> ret = tlmi_setting(wdev, i, &item);
> if (ret)
> break;
> @@ -1720,9 +1717,6 @@ static int tlmi_analyze(struct wmi_device *wdev)
> continue;
> }
>
> - /* Remove the value part */
> - strreplace(item, ',', '\0');
> -
> /* Create a setting entry */
> setting = kzalloc_obj(*setting);
> if (!setting) {
> @@ -1733,10 +1727,31 @@ static int tlmi_analyze(struct wmi_device *wdev)
> setting->wdev = wdev;
> setting->index = i;
>
> + /* validate and split from `item,value` -> `value` */
> + value = strpbrk(item, ",");
> + if (!value || value == item || !strlen(value + 1)) {
> + pr_info("Incorrectly formed attribute %s\n", item);
> + kfree(item);
> + kfree(setting);
> + continue;
> + }
> +
> + /* On Workstations remove the Options part after the value */
> + strreplace(value, ';', '\0');
> + setting->current_value = kstrdup(value + 1, GFP_KERNEL);
> + if (!setting->current_value) {
> + ret = -ENOMEM;
> + kfree(item);
> + kfree(setting);
> + goto fail_clear_attr;
> + }
> +
> + strreplace(item, ',', '\0'); /* Remove the value part */
> setting->name = kstrdup(item, GFP_KERNEL);
> if (!setting->name) {
> ret = -ENOMEM;
> kfree(item);
> + kfree(setting->current_value);
> kfree(setting);
> goto fail_clear_attr;
> }
> @@ -1748,6 +1763,7 @@ static int tlmi_analyze(struct wmi_device *wdev)
> ret = -ENOMEM;
> kfree(item);
> kfree(setting->name);
> + kfree(setting->current_value);
> kfree(setting);
> goto fail_clear_attr;
> }
> @@ -1879,6 +1895,7 @@ static int tlmi_analyze(struct wmi_device *wdev)
> 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]->current_value);
> kfree(tlmi_priv.setting[i]);
> }
> }
> diff --git a/drivers/platform/x86/lenovo/think-lmi.h b/drivers/platform/x86/lenovo/think-lmi.h
> index 8ced29253f40..54d5c3b8233e 100644
> --- a/drivers/platform/x86/lenovo/think-lmi.h
> +++ b/drivers/platform/x86/lenovo/think-lmi.h
> @@ -104,6 +104,7 @@ struct tlmi_attr_setting {
> char *name;
> char *display_name;
> char *possible_values;
> + char *current_value;
> };
>
> struct think_lmi {