Re: [PATCH 06/13] platform/x86: lenovo: tlmi: Add BIOS attributes sysfs entries
From: Rong Zhang
Date: Fri Dec 04 2026 - 12:08:55 EST
Hi Mark,
On Fri, 2026-12-04 at 10:41 -0500, Mark Pearson wrote:
>
> On Fri, Dec 4, 2026, at 8:44 AM, Rong Zhang wrote:
> > Hi Mark,
> >
> > On Thu, 2026-12-03 at 21:20 -0500, Mark Pearson wrote:
> > > Implement the sysfs entries for the attribute settings, along with the
> > > ability to modify and query them.
> > >
> > > Implement the save_settings and pending_reboot sysfs entries.
> > >
> > > Signed-off-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
> > > ---
> > > drivers/platform/x86/lenovo/think-lmi.h | 1 +
> > > drivers/platform/x86/lenovo/tlmi-attr.c | 4 +-
> > > drivers/platform/x86/lenovo/tlmi-config.c | 362 ++++++++++++++++++++++
> > > 3 files changed, 366 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/platform/x86/lenovo/think-lmi.h b/drivers/platform/x86/lenovo/think-lmi.h
> > > index 82f3644b9089..07a235b3a886 100644
> > > --- a/drivers/platform/x86/lenovo/think-lmi.h
> > > +++ b/drivers/platform/x86/lenovo/think-lmi.h
> > > @@ -148,5 +148,6 @@ struct think_lmi {
> > > /* Used by tlmi to sync the different sub-drivers */
> > > struct component_match;
> > > void tlmi_attr_match_add(struct device *master, struct component_match **matchptr);
> > > +void tlmi_attr_release_setting(struct tlmi_attr_setting *setting);
> > >
> > > #endif /* !_THINK_LMI_H_ */
> > > diff --git a/drivers/platform/x86/lenovo/tlmi-attr.c b/drivers/platform/x86/lenovo/tlmi-attr.c
> > > index 04c2ecf5053b..8818010a45cd 100644
> > > --- a/drivers/platform/x86/lenovo/tlmi-attr.c
> > > +++ b/drivers/platform/x86/lenovo/tlmi-attr.c
> > > @@ -21,7 +21,7 @@
> > > #include <linux/wmi.h>
> > > #include "think-lmi.h"
> > >
> > > -static void tlmi_attr_release_setting(struct tlmi_attr_setting *setting)
> > > +void tlmi_attr_release_setting(struct tlmi_attr_setting *setting)
> >
> > This was introduced by the previous patch. Please avoid unnecessary back-
> > and-forth changes.
> >
> Sure. The change is just because in this patch tlmi-config needs to use that function - hence the change, but not a problem
>
> > > {
> > > if (setting) {
> > > list_del(&setting->node);
> > > @@ -33,6 +33,7 @@ static void tlmi_attr_release_setting(struct tlmi_attr_setting *setting)
> > > kfree(setting);
> > > }
> > > }
> > > +EXPORT_SYMBOL_NS_GPL(tlmi_attr_release_setting, "TLMI-ATTR");
> > >
> > > #define ATTRDATA_ENTRIES 8 /*number of entries returned from BIOS */
> > >
> > > @@ -157,6 +158,7 @@ static void tlmi_attr_component_unbind(struct device *attr_dev, struct device *c
> > > struct tlmi_attr_setting *attr, *na;
> > > struct think_lmi *td = d;
> > >
> > > + /* This should not be needed - but make sure settings list is clear */
> > > list_for_each_entry_safe(attr, na, &td->settings, node)
> > > tlmi_attr_release_setting(attr);
> > > }
> > > diff --git a/drivers/platform/x86/lenovo/tlmi-config.c b/drivers/platform/x86/lenovo/tlmi-config.c
> > > index 9f57b0ba8dc5..ef5efae2e395 100644
> > > --- a/drivers/platform/x86/lenovo/tlmi-config.c
> > > +++ b/drivers/platform/x86/lenovo/tlmi-config.c
> > > @@ -18,6 +18,7 @@
> > > #include <linux/mutex.h>
> > > #include <linux/mutex_types.h>
> > > #include <linux/stddef.h>
> > > +#include <linux/string_helpers.h>
> > > #include <linux/types.h>
> > > #include <linux/wmi.h>
> > > #include "think-lmi.h"
> > > @@ -28,6 +29,359 @@
> > > #define SET_ID 1
> > > #define SAVE_ID 2
> > >
> > > +static DEFINE_MUTEX(tlmi_config_lock);
> > > +
> > > +/* ---- Helper functions---- */
> > > +
> > > +static ssize_t tlmi_populate_string_buffer(char *buffer, size_t buffer_len, const char *str)
> > > +{
> > > + struct wmi_string *wmistr = (struct wmi_string *)buffer;
> > > + u16 len;
> > > + int ret;
> > > +
> > > + if (!str || !buffer)
> > > + return -EINVAL;
> > > +
> > > + ret = wmi_string_from_utf8s((struct wmi_string *)buffer, buffer_len, str, strlen(str));
> >
> > Use wmistr you introduced above.
> >
> > Passing buffer_len is incorrect, see also
> > drivers/platform/wmi/tests/string_kunit.c for examples.
> >
> This function gave me so many headaches. Thanks for the pointers.
>
> > > + if (ret < 0)
> > > + return ret;
> > > +
> > > + len = le16_to_cpu(wmistr->length);
> > > + wmistr->length = cpu_to_le16(len - 2); /* Remove the extra null added */
> > > + return len; /* minus extra null and + the length field */
> > > +}
> > > +
> > > +/* Send request to BIOS and scan return buffer for error codes */
> > > +static int tlmi_request(struct wmi_device *wdev, u8 instance, u32 method_id,
> > > + struct wmi_buffer *wmibuf)
> > > +{
> > > + struct wmi_buffer wmibuf_return = {0, NULL};
> >
> > = {};
>
> ack
>
> >
> > > + char errstr[32];
> > > + int ret;
> > > +
> > > + ret = wmidev_invoke_method(wdev, instance, method_id, wmibuf, &wmibuf_return,
> > > + sizeof(struct wmi_string));
> > > + if (!ret && wmibuf_return.data) {
> > > + /* extract error string */
> > > + if (wmibuf_return.length < 2 || wmibuf_return.length > sizeof(errstr) * 2)
> >
> > The buffer's length contains the wmi_string's length field, why not
> > subtract it? And you didn't check the wmi_string's validity.
> >
> Not following this one I'm afraid - but I assume it's the same as what sashiko has highlighted? Let me know if you mean something different to sashiko.
Yeah, I meant the same.
>
> > > + ret = -EINVAL;
> > > + else
> > > + ret = wmi_string_to_utf8s((struct wmi_string *)wmibuf_return.data, errstr,
> > > + sizeof(errstr));
> > > + kfree(wmibuf_return.data);
> >
> > Why not:
> >
> > if (...)
> > return -EINVAL;
> >
> > struct wmi_string *wmistr __free(kfree) = wmibuf_return.data;
> >
> > ret = wmi_string_to_utf8s();
> >
> Ack - that does look better
>
> > > + if (ret < 0)
> > > + return ret;
> > > +
> > > + if (!strcmp("Success", errstr))
> > > + return 0;
> > > + else if (!strcmp("Not Supported", errstr))
> > > + return -EOPNOTSUPP;
> > > + else if (!strcmp("Invalid Parameter", errstr))
> > > + return -EINVAL;
> > > + else if (!strcmp("Access Denied", errstr))
> > > + return -EACCES;
> > > + else if (!strcmp("System Busy", errstr))
> > > + return -EBUSY;
> > > + else if (!strcmp("System Locked", errstr))
> > > + return -EACCES;
> > > + dev_warn(&wdev->dev, "Unknown error condition %s\n", errstr);
> > > + return -EIO;
> >
> > -EPROTO?
> >
> Ack - that's nicer
>
> > > + }
> > > + return ret;
> > > +}
> > > +
> > > +static ssize_t common_type_show(char *possible_values, char *buf)
> > > +{
> > > + if (possible_values) {
> > > + /* Figure out what setting type is as BIOS does not return this */
> > > + if (strchr(possible_values, ';'))
> > > + return sysfs_emit(buf, "enumeration\n");
> > > + }
> > > + /* Anything else is going to be a string */
> > > + return sysfs_emit(buf, "string\n");
> > > +}
> > > +
> > > +static ssize_t common_current_value_store(char *name, const char *buf, int wmi_id,
> >
> > `name' should be const char * if you are not intended to manipulate it.
> >
> > > + struct think_lmi *td)
> > > +{
> > > + char *new_setting __free(kfree) = NULL;
> > > + char *wmidata __free(kfree) = NULL;
> >
> > Move them to where the resources are allocated.
> >
> > > + size_t namelen, valuelen;
> > > + struct wmi_buffer wmibuf;
> > > + char *start;
> > > + int ix = 0;
> > > +
> > > + /* If a save has been done then a reboot is needed */
> > > + if (td->reboot_required)
> > > + return -EPERM;
> > > +
> > > + /* Strip out CR if one is present */
> >
> > \n is LF.
> >
> > > + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> > > + if (!new_setting)
> > > + return -ENOMEM;
> >
> > I am not in favor of the idea of abusing kstrdup_and_replace(), because:
> >
> > - Only the trailing LF should be stripped
> > - You don't take the advantage of the sysfs buffer size
> >
> > I'd suggest:
> >
> > if (size && buf[size - 1] == '\n')
> > size -= 1;
> > if (!size)
> > return -EINVAL;
> >
> > char *new_setting __free(kfree) = kmemdup_nul(buf, size, GFP_KERNEL);
> >
> > If LF in between is not allowed, please return -EINVAL instead of
> > silently clipping the string.
> >
>
> Funnily enough this bit is borrowed directly from think-lmi. It's probably not needed here as the FW teams are giving much cleaner strings now...but seemed sensible to keep it just in case.
"the FW teams are giving much cleaner strings"
But the string here is given by userspace, isn't it?
>
> Personally I prefer the original but I don't feel strongly about it.
Basically the issue is about the inconsistency of the comment ("Strip out
CR if one is present") and the behavior. "Strip" usually means removing
leading and/or trailing ones, not ones in between, but the behavior here
is simply cutting away anything after the first LF. The behavior leads to
a counterintuitive ABI without any documentation.
Moreover, the behavior mismatches most sysfs attributes, where only the
last trailing LF are allowed to be stripped by default, in order to
properly handle the default behavior of POSIX echo, i.e., `echo whatever
>/sys/attribute'.
By cutting away anything after the first LF, the string written to
firmware could differ from the user's intent (though the intent could be
a mistake). If you want to prevent LF from reaching the firmware, it'd
better to tell userspace about their mistake, instead of making decisions
on their behalf.
OTOH, if you don't want to change the ABI, please mention the behavior in
the documentation. Though I believe it should be harmless to change it as
no one should really relying on the current behavior instead of the de
facto sysfs standard.
I also believe that the temporary buffer can be eliminated as long as
tlmi_populate_string_buffer() accepts an additional parameter to describe
the string's length instead of relying on strlen(), in which case:
/* Determine the string's length in the chosen ABI's manner. */
length = ...
valuelen = struct_size_t(struct wmi_string, chars, length + 1);
...
ix = tlmi_populate_string_buffer(start, valuelen, buf, length);
That being said, since you'll anyway need a copy of the string in
current_value_store(), it's also OK to allocate a buffer and finish the
strip work there.
Thanks,
Rong
>
> > > +
> > > + /* Calculate and alloc buffer for command string */
> > > + namelen = struct_size_t(struct wmi_string, chars, strlen(name) + 1);
> > > + valuelen = struct_size_t(struct wmi_string, chars, strlen(new_setting) + 1);
> > > + wmibuf.length = namelen + valuelen;
> > > + wmidata = kzalloc(wmibuf.length, GFP_KERNEL);
> > > + wmibuf.data = wmidata;
> > > + if (!wmibuf.data)
> > > + return -ENOMEM;
> > > +
> > > + /* build argument buffer from inputs */
> > > + start = wmibuf.data;
> > > + ix = tlmi_populate_string_buffer(start, namelen, name);
> > > + if (ix < 0)
> > > + return ix;
> > > +
> > > + start += ix;
> > > + ix = tlmi_populate_string_buffer(start, valuelen, new_setting);
> > > + if (ix < 0)
> > > + return ix;
> > > +
> > > + return tlmi_request(td->config_wdev, 0, wmi_id, &wmibuf);
> > > +}
> > > +
> > > +/* ---- Attribute sysfs ---- */
> > > +static inline struct tlmi_attr_setting *to_tlmi_attr_setting(struct kobject *kobj)
> > > +{
> > > + return container_of(kobj, struct tlmi_attr_setting, kobj);
> > > +}
> > > +
> > > +static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *attr,
> > > + char *buf)
> > > +{
> > > + struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
> > > +
> > > + return sysfs_emit(buf, "%s\n", setting->display_name);
> > > +}
> > > +
> > > +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);
> > > +
> > > + guard(mutex)(&tlmi_config_lock);
> > > +
> > > + 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)
> > > +{
> > > + struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
> > > +
> > > + return sysfs_emit(buf, "%s\n", setting->possible_values);
> > > +}
> > > +
> > > +static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
> > > + char *buf)
> > > +{
> > > + struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
> > > +
> > > + return common_type_show(setting->possible_values, buf);
> > > +}
> > > +
> > > +static ssize_t current_value_store(struct kobject *kobj,
> > > + struct kobj_attribute *attr,
> > > + const char *buf, size_t count)
> > > +{
> > > + struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
> > > + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> > > + int ret;
> > > +
> > > + guard(mutex)(&tlmi_config_lock);
> > > +
> > > + ret = common_current_value_store(setting->name, buf, SET_ID, td);
> > > + if (!ret) {
> > > + char *s;
> > > +
> > > + td->save_required = true;
> > > + /* Update setting so read back shows correctly */
> > > + s = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> >
> > ??? Please don't repeat the same operation twice.
>
> Yep - fair.
> >
> > > + kfree(setting->current_value);
> > > + /* if kstrdup fails, we'll handle it in the show */
> > > + setting->current_value = s;
> > > + return count;
> > > + }
> > > + return ret;
> > > +}
> > > +
> > > +static struct kobj_attribute attr_display_name = __ATTR_RO(display_name);
> > > +
> > > +static struct kobj_attribute attr_possible_values = __ATTR_RO(possible_values);
> > > +
> > > +static struct kobj_attribute attr_current_val = __ATTR_RW_MODE(current_value, 0600);
> > > +
> > > +static struct kobj_attribute attr_type = __ATTR_RO(type);
> > > +
> > > +static struct attribute *tlmi_attrs[] = {
> > > + &attr_display_name.attr,
> > > + &attr_current_val.attr,
> > > + &attr_possible_values.attr,
> > > + &attr_type.attr,
> > > + NULL
> > > +};
> > > +
> > > +static const struct attribute_group tlmi_attr_group = {
> > > + .attrs = tlmi_attrs,
> > > +};
> > > +__ATTRIBUTE_GROUPS(tlmi_attr);
> > > +
> > > +static void tlmi_attr_setting_release(struct kobject *kobj)
> > > +{
> > > + struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
> > > +
> > > + tlmi_attr_release_setting(setting);
> > > +}
> > > +
> > > +static const struct kobj_type tlmi_attr_setting_ktype = {
> > > + .release = &tlmi_attr_setting_release,
> > > + .sysfs_ops = &kobj_sysfs_ops,
> > > + .default_groups = tlmi_attr_groups,
> > > +};
> > > +
> > > +/* ---- sysfs entries --------------------------------------------------------- */
> > > +static ssize_t pending_reboot_show(struct kobject *kobj, struct kobj_attribute *attr,
> > > + char *buf)
> > > +{
> > > + struct think_lmi *td = dev_get_drvdata(kobj_to_dev(kobj->parent));
> > > +
> >
> > Why doesn't this need guard(mutex)(&tlmi_config_lock)?
>
> It probably should
> >
> > > + return sysfs_emit(buf, "%d\n", td->pending_changes);
> > > +}
> > > +
> > > +static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);
> > > +
> > > +static ssize_t save_settings_show(struct kobject *kobj, struct kobj_attribute *attr,
> > > + char *buf)
> > > +{
> > > + struct think_lmi *td = dev_get_drvdata(kobj_to_dev(kobj->parent));
> > > +
> >
> > Ditto?
>
> ack
>
> >
> > > + return sysfs_emit(buf, "%d\n", td->save_required);
> > > +}
> > > +
> > > +static ssize_t save_settings_store(struct kobject *kobj, struct kobj_attribute *attr,
> > > + const char *buf, size_t count)
> > > +{
> > > + struct think_lmi *td = dev_get_drvdata(kobj_to_dev(kobj->parent));
> > > + char *wmidata __free(kfree) = NULL;
> > > + struct wmi_buffer wmibuf;
> > > + int ret = 0;
> > > +
> > > + /* save command triggered by writing 'save' */
> > > + if (!sysfs_streq(buf, "save"))
> > > + return -EINVAL;
> > > +
> > > + /* Use lock in case multiple WMI operations needed */
> > > + guard(mutex)(&tlmi_config_lock);
> > > +
> > > + /* Check there is actually something to save */
> > > + if (!td->save_required)
> > > + return -ENOENT;
> >
> > Returning -ENOENT on a file could be confusing IMO.
> >
> > Though thinklmi is already doing so, sigh...
> >
> Yeah. I'm happy to change it to something else. Will look into that for v2.
> Two wrongs don't make a right etc.
>
> Mark