Re: [PATCH 06/13] platform/x86: lenovo: tlmi: Add BIOS attributes sysfs entries
From: Rong Zhang
Date: Fri Dec 04 2026 - 08:49:56 EST
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.
> {
> 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.
> + 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};
= {};
> + 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.
> + 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();
> + 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?
> + }
> + 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.
> +
> + /* 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.
> + 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)?
> + 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?
> + 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...
Thanks,
Rong
> +
> + wmibuf.length = struct_size_t(struct wmi_string, chars, 1 + 1);
> + wmidata = kzalloc(wmibuf.length, GFP_KERNEL);
> + wmibuf.data = wmidata;
> + if (!wmibuf.data)
> + return -ENOMEM;
> +
> + ret = tlmi_populate_string_buffer(wmibuf.data, 1, "\0");
> + if (ret < 0)
> + return ret;
> +
> + ret = tlmi_request(td->config_wdev, 0, SAVE_ID, &wmibuf);
> + if (ret < 0)
> + return ret;
> +
> + td->save_required = false;
> + td->reboot_required = true;
> +
> + if (!td->pending_changes) {
> + td->pending_changes = true;
> + /* let userland know it may need to check reboot pending again */
> + kobject_uevent(&td->class_dev->kobj, KOBJ_CHANGE);
> + }
> + return count;
> +}
> +
> +static struct kobj_attribute save_settings = __ATTR_RW(save_settings);
> +
> +static void tlmi_config_sysfs_release(struct think_lmi *td)
> +{
> + struct kobject *pos, *n;
> +
> + if (!td->attribute_kset)
> + return;
> +
> + sysfs_remove_file(&td->attribute_kset->kobj, &pending_reboot.attr);
> + sysfs_remove_file(&td->attribute_kset->kobj, &save_settings.attr);
> +
> + list_for_each_entry_safe(pos, n, &td->attribute_kset->list, entry)
> + kobject_put(pos);
> +
> + kset_unregister(td->attribute_kset);
> + td->attribute_kset = NULL;
> +}
> +
> +static int tlmi_validate_setting_name(struct kset *attribute_kset, char *name)
> +{
> + struct kobject *duplicate;
> +
> + if (!strcmp(name, ""))
> + return -EINVAL;
> + if (!strcmp(name, "Reserved"))
> + return -EINVAL;
> +
> + duplicate = kset_find_obj(attribute_kset, name);
> + if (duplicate) {
> + /* kset_find_obj() returns a reference */
> + kobject_put(duplicate);
> + return -EBUSY;
> + }
> +
> + return 0;
> +}
> +
> +static int tlmi_config_sysfs_init(struct think_lmi *td)
> +{
> + struct tlmi_attr_setting *attr, *na;
> + int ret;
> +
> + /* Create sysfs entries */
> + td->attribute_kset = kset_create_and_add("attributes", NULL,
> + &td->class_dev->kobj);
> + if (!td->attribute_kset)
> + return -ENOMEM;
> +
> + list_for_each_entry_safe(attr, na, &td->settings, node) {
> + /* check for duplicate or reserved values */
> + if (tlmi_validate_setting_name(td->attribute_kset,
> + attr->name) < 0)
> + continue;
> +
> + /* Build attribute */
> + attr->kobj.kset = td->attribute_kset;
> + ret = kobject_init_and_add(&attr->kobj, &tlmi_attr_setting_ktype,
> + NULL, "%s", attr->name);
> + if (ret) {
> + kobject_put(&attr->kobj);
> + goto out;
> + }
> + }
> +
> + ret = sysfs_create_file(&td->attribute_kset->kobj, &pending_reboot.attr);
> + if (ret)
> + goto out;
> +
> + ret = sysfs_create_file(&td->attribute_kset->kobj, &save_settings.attr);
> +
> +out:
> + if (ret) /* Clean up if any failures */
> + tlmi_config_sysfs_release(td);
> + return ret;
> +}
> +
> static int tlmi_config_bind(struct device *dev)
> {
> struct think_lmi *td = dev_get_drvdata(dev);
> @@ -35,6 +389,13 @@ static int tlmi_config_bind(struct device *dev)
>
> /* Triggers the bind callbacks for registered sub-components */
> ret = component_bind_all(dev, td);
> + if (ret)
> + return ret;
> + /* Create sysfs entries */
> + ret = tlmi_config_sysfs_init(td);
> + if (ret)
> + component_unbind_all(dev, td);
> +
> return ret;
> }
>
> @@ -42,6 +403,7 @@ static void tlmi_config_unbind(struct device *dev)
> {
> struct think_lmi *td = dev_get_drvdata(dev);
>
> + tlmi_config_sysfs_release(td);
> component_unbind_all(dev, td);
> }
>