mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface
@ 2026-11-18 18:58 Rong Zhang
  2026-11-18 18:58 ` [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device Rong Zhang
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Rong Zhang @ 2026-11-18 18:58 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel, Rong Zhang

The driver declares `.no_singleton = true' but stores device-specific
data to statically allocated tunable_attr_01 variables. This makes the
driver depend on the frigile fact that no device in wild has more than
one LENOVO_OTHER_MODE WMI devices.

The series moves tunable_attr_01.device to the firmware_attributes
device's drvdata and introcdces a tunable_attr_01_ctx array to struct
to store c[dv]_mode_id. In this manner, all statically allocated
variables relevant to our firmware_attributes interface are constified.

This also eliminates all attribute-specific callbacks by moving their
data into a newly-introduced container struct capdata01_attr, so that
all remaining attribute callbacks becomes generic and are shared among
different attributes, which shrinks the module size by 42.7%.

To further follow the common practice of sysfs attributes, all attribute
groups are now created and removed at once using sysfs_create_groups()
and sysfs_remove_groups() by providing an is_visible() callback.

Signed-off-by: Rong Zhang <i@rong.moe>
---
Rong Zhang (5):
      platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device
      platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks
      platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro
      platform/x86: lenovo-wmi-other: Properly implement no_singleton driver
      platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback

 drivers/platform/x86/lenovo/wmi-other.c | 566 +++++++++++++++-----------------
 1 file changed, 259 insertions(+), 307 deletions(-)
---
base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
change-id: 2c0ec814-lwmi-no-singleton-a3f945179a50

Thanks,
Rong


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device
  2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
@ 2026-11-18 18:58 ` Rong Zhang
  2026-11-18 22:19   ` Mark Pearson
  2026-11-18 18:58 ` [PATCH 2/5] platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks Rong Zhang
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Rong Zhang @ 2026-11-18 18:58 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel, Rong Zhang

Currently, each tunable_attr_01 stores a pointer to the WMI device,
which is unnecessary, as attributes' show/store callbacks can always
access the firmware_attributes device through the kobject hierarchy.
This also makes the driver fail to satisfy the requirements of a
no_singleton driver.

Store a pointer to the driver's privated data in the firmware_attributes
device's drvdata, and retrieve it in attributes' show/store callbacks,
so that we can get rid of tunable_attr_01.device and make it possible
for upcoming refactoring to eliminate all wrapper callbacks created by
macros and constify all statically allocated variables relevant to our
firmware_attributes interface.

Fixes: edc4b183b794 ("platform/x86: Add Lenovo Other Mode WMI Driver")
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/wmi-other.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/drivers/platform/x86/lenovo/wmi-other.c b/drivers/platform/x86/lenovo/wmi-other.c
index bd64e7b44669..c31a8bd08a85 100644
--- a/drivers/platform/x86/lenovo/wmi-other.c
+++ b/drivers/platform/x86/lenovo/wmi-other.c
@@ -967,7 +967,6 @@ static void lwmi_om_psy_remove(struct lwmi_om_priv *priv)
 /* ======== fw_attributes (component: lenovo-wmi-capdata 01) ======== */
 
 struct tunable_attr_01 {
-	struct device *dev;
 	u8 feature_id;
 	u8 device_id;
 	u8 type_id;
@@ -1124,6 +1123,11 @@ struct capdata01_attr_group {
 	struct tunable_attr_01 *tunable_attr;
 };
 
+static inline struct lwmi_om_priv *kobj_to_om_priv(const struct kobject *kobj)
+{
+	return dev_get_drvdata(kobj_to_dev(kobj->parent));
+}
+
 /* Attribute Methods */
 
 /**
@@ -1163,7 +1167,7 @@ static ssize_t attr_capdata01_show(struct kobject *kobj,
 				   struct tunable_attr_01 *tunable_attr,
 				   enum attribute_property prop)
 {
-	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
+	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
 	struct capdata01 capdata;
 	u32 attribute_id;
 	int value, ret;
@@ -1219,7 +1223,7 @@ static ssize_t attr_current_value_store(struct kobject *kobj,
 					const char *buf, size_t count,
 					struct tunable_attr_01 *tunable_attr)
 {
-	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
+	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
 	struct wmi_method_args_32 args = {};
 	struct capdata01 capdata;
 	enum thermal_mode mode;
@@ -1277,7 +1281,7 @@ static ssize_t attr_current_value_show(struct kobject *kobj,
 				       struct kobj_attribute *kattr, char *buf,
 				       struct tunable_attr_01 *tunable_attr)
 {
-	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
+	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
 	struct wmi_method_args_32 args = {};
 	enum thermal_mode mode;
 	int retval;
@@ -1304,6 +1308,7 @@ static ssize_t attr_current_value_show(struct kobject *kobj,
 
 /**
  * lwmi_attr_01_is_supported() - Determine if the given attribute is supported.
+ * @priv: Driver private data
  * @tunable_attr: The attribute to verify.
  *
  * For an attribute to be supported it must have a functional get/set method,
@@ -1324,10 +1329,10 @@ static ssize_t attr_current_value_show(struct kobject *kobj,
  *
  * Return: true if capdata and set/get modes are found, otherwise false.
  */
-static bool lwmi_attr_01_is_supported(struct tunable_attr_01 *tunable_attr)
+static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
+				      struct tunable_attr_01 *tunable_attr)
 {
 	u8 modes[2] = { LWMI_GZ_THERMAL_MODE_CUSTOM, LWMI_GZ_THERMAL_MODE_NONE };
-	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
 	struct wmi_method_args_32 args = {};
 	bool cd_mode_found = false;
 	bool cv_mode_found = false;
@@ -1350,8 +1355,7 @@ static bool lwmi_attr_01_is_supported(struct tunable_attr_01 *tunable_attr)
 	if (!cd_mode_found)
 		return cd_mode_found;
 
-	dev_dbg(tunable_attr->dev,
-		"cd_mode_id: %#010x\n", args.arg0);
+	dev_dbg(&priv->wdev->dev, "cd_mode_id: %#010x\n", args.arg0);
 
 	/* Determine tunable_attr->cv_mode_id, returns 1 if supported */
 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
@@ -1371,7 +1375,7 @@ static bool lwmi_attr_01_is_supported(struct tunable_attr_01 *tunable_attr)
 	if (!cv_mode_found)
 		return cv_mode_found;
 
-	dev_dbg(tunable_attr->dev, "cv_mode_id: %#010x, attribute support level: %#010x\n",
+	dev_dbg(&priv->wdev->dev, "cv_mode_id: %#010x, attribute support level: %#010x\n",
 		args.arg0, capdata.supported);
 
 	return capdata.supported > 0;
@@ -1558,9 +1562,10 @@ static void lwmi_om_fw_attr_add(struct lwmi_om_priv *priv)
 		goto err_destroy_classdev;
 	}
 
+	dev_set_drvdata(priv->fw_attr_dev, priv);
+
 	for (i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++) {
-		cd01_attr_groups[i].tunable_attr->dev = &priv->wdev->dev;
-		if (!lwmi_attr_01_is_supported(cd01_attr_groups[i].tunable_attr))
+		if (!lwmi_attr_01_is_supported(priv, cd01_attr_groups[i].tunable_attr))
 			continue;
 
 		err = sysfs_create_group(&priv->fw_attr_kset->kobj,

-- 
2.55.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 2/5] platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks
  2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
  2026-11-18 18:58 ` [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device Rong Zhang
@ 2026-11-18 18:58 ` Rong Zhang
  2026-11-18 22:17   ` Mark Pearson
  2026-11-18 18:58 ` [PATCH 3/5] platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro Rong Zhang
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Rong Zhang @ 2026-11-18 18:58 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel, Rong Zhang

Currently, all firmware-attributes are backed by numerous
attribute-specific wrapper callbacks generated by macros. Besides,
every firmware attribute has the same type "integer" and shares the same
show callback int_type_show(), but LWMI_ATTR_GROUP_TUNABLE_CAP01()
defines attr_*_type over and over again.

As a result, the definition of attributes significantly bloats up the
module size.

Eliminating these attribute-specific callbacks by moving their data into
a newly-introduced container struct capdata01_attr, and utilize
container_of_const() to retrieve the required data in the corresponding
callbacks, which are adjusted to make them generic and shared among
different attributes. Also introduce a unified attr_int_type attribute
to replace all attr_*_type attributes.

With these, all firmware-attributes now use generic callbacks directly
and are constified.

This shrinks the module size by 23.8K (42.7%).

      text       data        bss      total filename (before)
     23463      32197          3      55663 lenovo-wmi-other.ko

      text       data        bss      total filename (after)
      5816      26059          3      31878 lenovo-wmi-other.ko

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/wmi-other.c | 217 ++++++++++++++++----------------
 1 file changed, 108 insertions(+), 109 deletions(-)

diff --git a/drivers/platform/x86/lenovo/wmi-other.c b/drivers/platform/x86/lenovo/wmi-other.c
index c31a8bd08a85..4d51b05a5acf 100644
--- a/drivers/platform/x86/lenovo/wmi-other.c
+++ b/drivers/platform/x86/lenovo/wmi-other.c
@@ -981,7 +981,7 @@ struct tunable_attr_01 {
  *
  * Return: encoded capability data attribute ID.
  */
-static u32 tunable_attr_01_id(struct tunable_attr_01 *attr, u8 mode)
+static u32 tunable_attr_01_id(const struct tunable_attr_01 *attr, u8 mode)
 {
 	return lwmi_attr_id(attr->device_id, attr->feature_id, mode, attr->type_id);
 }
@@ -1118,6 +1118,15 @@ static struct tunable_attr_01 gpu_nv_cpu_boost = {
 	.type_id = LWMI_TYPE_ID_NONE,
 };
 
+struct capdata01_attr {
+	struct kobj_attribute kobj_attr;
+	struct tunable_attr_01 *tunable_attr;
+	union {
+		enum attribute_property prop;
+		const char *display_name;
+	};
+};
+
 struct capdata01_attr_group {
 	const struct attribute_group *attr_group;
 	struct tunable_attr_01 *tunable_attr;
@@ -1128,6 +1137,12 @@ static inline struct lwmi_om_priv *kobj_to_om_priv(const struct kobject *kobj)
 	return dev_get_drvdata(kobj_to_dev(kobj->parent));
 }
 
+static inline const struct capdata01_attr *
+kobj_attr_to_cd01_attr(const struct kobj_attribute *kattr)
+{
+	return container_of_const(kattr, struct capdata01_attr, kobj_attr);
+}
+
 /* Attribute Methods */
 
 /**
@@ -1138,36 +1153,54 @@ static inline struct lwmi_om_priv *kobj_to_om_priv(const struct kobject *kobj)
  *
  * Return: Number of characters written to buf.
  */
-static ssize_t int_type_show(struct kobject *kobj, struct kobj_attribute *kattr,
+static ssize_t int_type_show(struct kobject *kobj, const struct kobj_attribute *kattr,
 			     char *buf)
 {
 	return sysfs_emit(buf, "integer\n");
 }
 
+static const struct kobj_attribute attr_int_type = __KOBJ_ATTR(type, 0444, int_type_show, NULL);
+
+/**
+ * attr_display_name_show() - Get the display name of the given attribute
+ * @kobj: Pointer to the driver object.
+ * @kattr: Pointer to the attribute calling this function.
+ * @buf: The buffer to write to.
+ *
+ * Retrieves the display name from the capdata01_attr struct for the specified
+ * attribute. This function is intended to be generic so it can be called from
+ * any integer attributes.
+ *
+ * Return: Number of characters written to buf.
+ */
+static ssize_t attr_display_name_show(struct kobject *kobj,
+				      const struct kobj_attribute *kattr, char *buf)
+{
+	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
+
+	return sysfs_emit(buf, "%s\n", cd01_attr->display_name);
+}
+
 /**
  * attr_capdata01_show() - Get the value of the specified attribute property
  *
  * @kobj: Pointer to the driver object.
  * @kattr: Pointer to the attribute calling this function.
  * @buf: The buffer to write to.
- * @tunable_attr: The attribute to be read.
- * @prop: The property of this attribute to be read.
  *
  * Retrieves the given property from the capability data 01 struct for the
  * specified attribute's "custom" thermal mode. This function is intended
- * to be generic so it can be called from any integer attributes "_show"
- * function.
- *
- * If the WMI is success the sysfs attribute is notified.
+ * to be generic so it can be called from any integer attributes.
  *
  * Return: Either number of characters written to buf, or an error code.
  */
 static ssize_t attr_capdata01_show(struct kobject *kobj,
-				   struct kobj_attribute *kattr, char *buf,
-				   struct tunable_attr_01 *tunable_attr,
-				   enum attribute_property prop)
+				   const struct kobj_attribute *kattr, char *buf)
 {
 	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
+	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
+	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
+	enum attribute_property prop = cd01_attr->prop;
 	struct capdata01 capdata;
 	u32 attribute_id;
 	int value, ret;
@@ -1204,26 +1237,23 @@ static ssize_t attr_capdata01_show(struct kobject *kobj,
  * @kattr: Pointer to the attribute calling this function.
  * @buf: The buffer to read from, this is parsed to `int` type.
  * @count: Required by sysfs attribute macros, pass in from the callee attr.
- * @tunable_attr: The attribute to be stored.
  *
  * Sets the value of the given attribute when operating under the "custom"
  * smartfan profile. The current smartfan profile is retrieved from the
  * lenovo-wmi-gamezone driver and error is returned if the result is not
  * "custom". This function is intended to be generic so it can be called from
- * any integer attribute's "_store" function. The integer to be sent to the WMI
- * method is range checked and an error code is returned if out of range.
- *
- * If the value is valid and WMI is success, then the sysfs attribute is
- * notified.
+ * any integer attributes. The integer to be sent to the WMI method is range
+ * checked and an error code is returned if out of range.
  *
  * Return: Either count, or an error code.
  */
 static ssize_t attr_current_value_store(struct kobject *kobj,
-					struct kobj_attribute *kattr,
-					const char *buf, size_t count,
-					struct tunable_attr_01 *tunable_attr)
+					const struct kobj_attribute *kattr,
+					const char *buf, size_t count)
 {
 	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
+	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
+	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
 	struct wmi_method_args_32 args = {};
 	struct capdata01 capdata;
 	enum thermal_mode mode;
@@ -1266,22 +1296,20 @@ static ssize_t attr_current_value_store(struct kobject *kobj,
  * @kobj: Pointer to the driver object.
  * @kattr: Pointer to the attribute calling this function.
  * @buf: The buffer to write to.
- * @tunable_attr: The attribute to be read.
  *
  * Retrieves the value of the given attribute for the current smartfan profile.
  * The current smartfan profile is retrieved from the lenovo-wmi-gamezone driver.
  * This function is intended to be generic so it can be called from any integer
- * attribute's "_show" function.
- *
- * If the WMI is success the sysfs attribute is notified.
+ * attributes.
  *
  * Return: Either number of characters written to buf, or an error code.
  */
 static ssize_t attr_current_value_show(struct kobject *kobj,
-				       struct kobj_attribute *kattr, char *buf,
-				       struct tunable_attr_01 *tunable_attr)
+				       const struct kobj_attribute *kattr, char *buf)
 {
 	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
+	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
+	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
 	struct wmi_method_args_32 args = {};
 	enum thermal_mode mode;
 	int retval;
@@ -1382,128 +1410,99 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 }
 
 /* Lenovo WMI Other Mode Attribute macros */
-#define __LWMI_ATTR_RO(_func, _name)                                  \
-	{                                                             \
-		.attr = { .name = __stringify(_name), .mode = 0444 }, \
-		.show = _func##_##_name##_show,                       \
-	}
 
-#define __LWMI_ATTR_RO_AS(_name, _show)                               \
-	{                                                             \
-		.attr = { .name = __stringify(_name), .mode = 0444 }, \
-		.show = _show,                                        \
+/* Attribute current value read/write */
+#define __LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname)                                        \
+	static const struct capdata01_attr ext_attr_##_attrname##_current_value = {          \
+		.kobj_attr = __KOBJ_ATTR(current_value, 0644,                                \
+					 attr_current_value_show, attr_current_value_store), \
+		.tunable_attr = &_attrname,                                                  \
 	}
 
-#define __LWMI_ATTR_RW(_func, _name) \
-	__ATTR(_name, 0644, _func##_##_name##_show, _func##_##_name##_store)
-
-/* Shows a formatted static variable */
-#define __LWMI_ATTR_SHOW_FMT(_prop, _attrname, _fmt, _val)                     \
-	static ssize_t _attrname##_##_prop##_show(                             \
-		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
-	{                                                                      \
-		return sysfs_emit(buf, _fmt, _val);                            \
-	}                                                                      \
-	static struct kobj_attribute attr_##_attrname##_##_prop =              \
-		__LWMI_ATTR_RO(_attrname, _prop)
+/* Attribute property read only */
+#define __LWMI_TUNABLE_RO_CAP01(_prop, _attrname, _prop_type)                               \
+	static const struct capdata01_attr ext_attr_##_attrname##_##_prop = {               \
+		.kobj_attr = __KOBJ_ATTR(_prop, 0444, attr_capdata01_show, NULL),           \
+		.tunable_attr = &_attrname,                                                 \
+		.prop = _prop_type,                                                         \
+	}
 
-/* Attribute current value read/write */
-#define __LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname)                          \
-	static ssize_t _attrname##_current_value_store(                        \
-		struct kobject *kobj, struct kobj_attribute *kattr,            \
-		const char *buf, size_t count)                                 \
-	{                                                                      \
-		return attr_current_value_store(kobj, kattr, buf, count,       \
-						&_attrname);                   \
-	}                                                                      \
-	static ssize_t _attrname##_current_value_show(                         \
-		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
-	{                                                                      \
-		return attr_current_value_show(kobj, kattr, buf, &_attrname);  \
-	}                                                                      \
-	static struct kobj_attribute attr_##_attrname##_current_value =        \
-		__LWMI_ATTR_RW(_attrname, current_value)
+/* Attribute display name */
+#define __LWMI_DISPLAY_NAME(_attrname, _val)                                                \
+	static const struct capdata01_attr ext_attr_##_attrname##_display_name = {          \
+		.kobj_attr = __KOBJ_ATTR(display_name, 0444, attr_display_name_show, NULL), \
+		.tunable_attr = &_attrname,                                                 \
+		.display_name = _val,                                                       \
+	}
 
-/* Attribute property read only */
-#define __LWMI_TUNABLE_RO_CAP01(_prop, _attrname, _prop_type)                  \
-	static ssize_t _attrname##_##_prop##_show(                             \
-		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
-	{                                                                      \
-		return attr_capdata01_show(kobj, kattr, buf, &_attrname,       \
-					   _prop_type);                        \
-	}                                                                      \
-	static struct kobj_attribute attr_##_attrname##_##_prop =              \
-		__LWMI_ATTR_RO(_attrname, _prop)
-
-#define LWMI_ATTR_GROUP_TUNABLE_CAP01(_attrname, _fsname, _dispname)      \
+#define LWMI_ATTR_GROUP_TUNABLE_CAP01(_attrname, _dispname)               \
 	__LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname);                    \
 	__LWMI_TUNABLE_RO_CAP01(default_value, _attrname, DEFAULT_VAL);   \
-	__LWMI_ATTR_SHOW_FMT(display_name, _attrname, "%s\n", _dispname); \
+	__LWMI_DISPLAY_NAME(_attrname, _dispname);                        \
 	__LWMI_TUNABLE_RO_CAP01(max_value, _attrname, MAX_VAL);           \
 	__LWMI_TUNABLE_RO_CAP01(min_value, _attrname, MIN_VAL);           \
 	__LWMI_TUNABLE_RO_CAP01(scalar_increment, _attrname, STEP_VAL);   \
-	static struct kobj_attribute attr_##_attrname##_type =            \
-		__LWMI_ATTR_RO_AS(type, int_type_show);                   \
-	static struct attribute *_attrname##_attrs[] = {                  \
-		&attr_##_attrname##_current_value.attr,                   \
-		&attr_##_attrname##_default_value.attr,                   \
-		&attr_##_attrname##_display_name.attr,                    \
-		&attr_##_attrname##_max_value.attr,                       \
-		&attr_##_attrname##_min_value.attr,                       \
-		&attr_##_attrname##_scalar_increment.attr,                \
-		&attr_##_attrname##_type.attr,                            \
+	static const struct attribute *const _attrname##_attrs[] = {      \
+		&ext_attr_##_attrname##_current_value.kobj_attr.attr,     \
+		&ext_attr_##_attrname##_default_value.kobj_attr.attr,     \
+		&ext_attr_##_attrname##_display_name.kobj_attr.attr,      \
+		&ext_attr_##_attrname##_max_value.kobj_attr.attr,         \
+		&ext_attr_##_attrname##_min_value.kobj_attr.attr,         \
+		&ext_attr_##_attrname##_scalar_increment.kobj_attr.attr,  \
+		&attr_int_type.attr,                                      \
 		NULL,                                                     \
 	};                                                                \
 	static const struct attribute_group _attrname##_attr_group = {    \
-		.name = _fsname, .attrs = _attrname##_attrs               \
+		.name = #_attrname,                                       \
+		.attrs_const = _attrname##_attrs,                         \
 	}
 
 /* CPU tunable attributes */
-LWMI_ATTR_GROUP_TUNABLE_CAP01(cpu_temp, "cpu_temp",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(cpu_temp,
 			      "Set the CPU thermal load limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_cpu_cl, "ppt_cpu_cl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_cpu_cl,
 			      "Set the CPU cross loading power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_apu_spl, "ppt_pl1_apu_spl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_apu_spl,
 			      "Set the APU sustained power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl, "ppt_pl1_spl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl,
 			      "Set the CPU sustained power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl_cl, "ppt_pl1_spl_cl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl_cl,
 			      "Set the CPU cross loading sustained power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt, "ppt_pl2_sppt",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt,
 			      "Set the CPU slow package power tracking limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt_cl, "ppt_pl2_sppt_cl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt_cl,
 			      "Set the CPU cross loading slow package power tracking limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt, "ppt_pl3_fppt",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt,
 			      "Set the CPU fast package power tracking limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt_cl, "ppt_pl3_fppt_cl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt_cl,
 			      "Set the CPU cross loading fast package power tracking limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_tau, "ppt_pl1_tau",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_tau,
 			      "Set the CPU sustained power limit exceed duration");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl, "ppt_pl4_ipl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl,
 			      "Set the CPU instantaneous power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl_cl, "ppt_pl4_ipl_cl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl_cl,
 			      "Set the CPU cross loading instantaneous power limit");
 
 /* GPU tunable attributes */
-LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_boost_clk, "dgpu_boost_clk",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_boost_clk,
 			      "Set the dedicated GPU boost clock");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_didvid, "dgpu_didvid",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_didvid,
 			      "Get the GPU device identifier and vendor identifier");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_enable, "dgpu_enable",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_enable,
 			      "Set the dedicated Nvidia GPU enabled status");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_mode, "gpu_mode",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_mode,
 			      "Set the GPU mode by power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ac_offset, "gpu_nv_ac_offset",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ac_offset,
 			      "Set the Nvidia GPU AC total processing power baseline offset");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_bpl, "gpu_nv_bpl",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_bpl,
 			      "Set the Nvidia GPU base power limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_cpu_boost, "gpu_nv_cpu_boost",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_cpu_boost,
 			      "Set the Nvidia GPU to CPU dynamic boost limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ctgp, "gpu_nv_ctgp",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ctgp,
 			      "Set the GPU configurable total graphics power");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab, "gpu_nv_ppab",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab,
 			      "Set the Nvidia GPU power performance aware boost limit");
-LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp, "gpu_temp",
+LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp,
 			      "Set the GPU thermal load limit");
 
 static struct capdata01_attr_group cd01_attr_groups[] = {

-- 
2.55.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 3/5] platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro
  2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
  2026-11-18 18:58 ` [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device Rong Zhang
  2026-11-18 18:58 ` [PATCH 2/5] platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks Rong Zhang
@ 2026-11-18 18:58 ` Rong Zhang
  2026-11-18 22:19   ` Mark Pearson
  2026-11-18 18:58 ` [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver Rong Zhang
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Rong Zhang @ 2026-11-18 18:58 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel, Rong Zhang

Currently, all definitions of statically allocated tunable_attr_01
variables are open-coded, making them hard to refactor.

Add a macro to define statically allocated tunable_attr_01 variables so
that further refactoring only touches the macro itself.

Meanwhile, move the definition of statically allocated tunable_attr_01
variables before the definition of struct lwmi_om_priv, so that upcoming
changes can reference the former as needed.

No functional change intended.

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/wmi-other.c | 178 +++++++-------------------------
 1 file changed, 38 insertions(+), 140 deletions(-)

diff --git a/drivers/platform/x86/lenovo/wmi-other.c b/drivers/platform/x86/lenovo/wmi-other.c
index 4d51b05a5acf..252ef775f300 100644
--- a/drivers/platform/x86/lenovo/wmi-other.c
+++ b/drivers/platform/x86/lenovo/wmi-other.c
@@ -130,6 +130,44 @@ struct lwmi_fan_info {
 	long max_rpm;
 };
 
+struct tunable_attr_01 {
+	u8 feature_id;
+	u8 device_id;
+	u8 type_id;
+	u8 cd_mode_id; /* mode arg for searching capdata */
+	u8 cv_mode_id; /* mode arg for set/get current_value */
+};
+
+#define DEFINE_TUNABLE_ATTR_01(_name, _device, _feature, _type)	\
+	static struct tunable_attr_01 _name = {			\
+		.device_id = LWMI_DEVICE_ID_##_device,		\
+		.feature_id = LWMI_FEATURE_ID_##_feature,	\
+		.type_id = LWMI_TYPE_ID_##_type,		\
+	}
+
+DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl, CPU, CPU_SPL, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl_cl, CPU, CPU_SPL, CROSSLOAD);
+DEFINE_TUNABLE_ATTR_01(ppt_pl2_sppt, CPU, CPU_SPPT, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl2_sppt_cl, CPU, CPU_SPPT, CROSSLOAD);
+DEFINE_TUNABLE_ATTR_01(ppt_pl3_fppt, CPU, CPU_FPPT, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl3_fppt_cl, CPU, CPU_FPPT, CROSSLOAD);
+DEFINE_TUNABLE_ATTR_01(cpu_temp, CPU, CPU_TEMP, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl1_apu_spl, CPU, CPU_APU, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_cpu_cl, CPU, CPU_CL, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl1_tau, CPU, CPU_TAU, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl4_ipl, CPU, CPU_IPL, NONE);
+DEFINE_TUNABLE_ATTR_01(ppt_pl4_ipl_cl, CPU, CPU_IPL, CROSSLOAD);
+DEFINE_TUNABLE_ATTR_01(gpu_nv_ppab, GPU, GPU_NV_PPAB, NONE);
+DEFINE_TUNABLE_ATTR_01(gpu_nv_ctgp, GPU, GPU_NV_CTGP, NONE);
+DEFINE_TUNABLE_ATTR_01(gpu_temp, GPU, GPU_TEMP, NONE);
+DEFINE_TUNABLE_ATTR_01(gpu_nv_ac_offset, GPU, GPU_AC_OFFSET, NONE);
+DEFINE_TUNABLE_ATTR_01(dgpu_boost_clk, GPU, DGPU_BOOST_CLK, NONE);
+DEFINE_TUNABLE_ATTR_01(dgpu_enable, GPU, DGPU_EN, NONE);
+DEFINE_TUNABLE_ATTR_01(gpu_mode, GPU, GPU_MODE, NONE);
+DEFINE_TUNABLE_ATTR_01(dgpu_didvid, GPU, DGPU_DIDVID, NONE);
+DEFINE_TUNABLE_ATTR_01(gpu_nv_bpl, GPU, GPU_NV_BPL, NONE);
+DEFINE_TUNABLE_ATTR_01(gpu_nv_cpu_boost, GPU, GPU_NV_CPU_BOOST, NONE);
+
 struct lwmi_om_priv {
 	struct component_master_ops *ops;
 
@@ -966,14 +1004,6 @@ static void lwmi_om_psy_remove(struct lwmi_om_priv *priv)
 
 /* ======== fw_attributes (component: lenovo-wmi-capdata 01) ======== */
 
-struct tunable_attr_01 {
-	u8 feature_id;
-	u8 device_id;
-	u8 type_id;
-	u8 cd_mode_id; /* mode arg for searching capdata */
-	u8 cv_mode_id; /* mode arg for set/get current_value */
-};
-
 /**
  * tunable_attr_01_id() - Formats a tunable_attr_01 to a capdata attribute ID
  * @attr: The tunable_attr_01 to format.
@@ -986,138 +1016,6 @@ static u32 tunable_attr_01_id(const struct tunable_attr_01 *attr, u8 mode)
 	return lwmi_attr_id(attr->device_id, attr->feature_id, mode, attr->type_id);
 }
 
-static struct tunable_attr_01 ppt_pl1_spl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_SPL,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl1_spl_cl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_SPL,
-	.type_id = LWMI_TYPE_ID_CROSSLOAD,
-};
-
-static struct tunable_attr_01 ppt_pl2_sppt = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_SPPT,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl2_sppt_cl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_SPPT,
-	.type_id = LWMI_TYPE_ID_CROSSLOAD,
-};
-
-static struct tunable_attr_01 ppt_pl3_fppt = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_FPPT,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl3_fppt_cl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_FPPT,
-	.type_id = LWMI_TYPE_ID_CROSSLOAD,
-};
-
-static struct tunable_attr_01 cpu_temp = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_TEMP,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl1_apu_spl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_APU,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_cpu_cl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_CL,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl1_tau = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_TAU,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl4_ipl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_IPL,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 ppt_pl4_ipl_cl = {
-	.device_id = LWMI_DEVICE_ID_CPU,
-	.feature_id = LWMI_FEATURE_ID_CPU_IPL,
-	.type_id = LWMI_TYPE_ID_CROSSLOAD,
-};
-
-static struct tunable_attr_01 gpu_nv_ppab = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_NV_PPAB,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 gpu_nv_ctgp = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_NV_CTGP,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 gpu_temp = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_TEMP,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 gpu_nv_ac_offset = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_AC_OFFSET,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 dgpu_boost_clk = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_DGPU_BOOST_CLK,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 dgpu_enable = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_DGPU_EN,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 gpu_mode = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_MODE,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 dgpu_didvid = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_DGPU_DIDVID,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 gpu_nv_bpl = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_NV_BPL,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
-static struct tunable_attr_01 gpu_nv_cpu_boost = {
-	.device_id = LWMI_DEVICE_ID_GPU,
-	.feature_id = LWMI_FEATURE_ID_GPU_NV_CPU_BOOST,
-	.type_id = LWMI_TYPE_ID_NONE,
-};
-
 struct capdata01_attr {
 	struct kobj_attribute kobj_attr;
 	struct tunable_attr_01 *tunable_attr;

-- 
2.55.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver
  2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
                   ` (2 preceding siblings ...)
  2026-11-18 18:58 ` [PATCH 3/5] platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro Rong Zhang
@ 2026-11-18 18:58 ` Rong Zhang
  2026-11-18 22:27   ` Mark Pearson
  2026-11-18 18:58 ` [PATCH 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback Rong Zhang
  2026-11-18 22:19 ` [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Mark Pearson
  5 siblings, 1 reply; 12+ messages in thread
From: Rong Zhang @ 2026-11-18 18:58 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel, Rong Zhang

The driver declares `.no_singleton = true' but stores device-specific
data to statically allocated tunable_attr_01 variables. This makes the
driver depend on the frigile fact that no device in wild has more than
one LENOVO_OTHER_MODE WMI devices.

Add unique indexes to all statically allocated tunable_attr_01 variables
with some __COUNTER__ tricks, and use them to access lwmi_om_priv's
newly-introduced tunable_attr_01_ctx array. In this manner, move all
device-specific data to the tunable_attr_01_ctx array, and constify all
statically allocated tunable_attr_01 variables to make the driver a
truly no_singleton driver.

Fixes: 03bb5147da08 ("platform/x86: lenovo-wmi-other: Limit adding attributes to supported devices")
Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/wmi-other.c | 54 ++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/drivers/platform/x86/lenovo/wmi-other.c b/drivers/platform/x86/lenovo/wmi-other.c
index 252ef775f300..f37dca7db034 100644
--- a/drivers/platform/x86/lenovo/wmi-other.c
+++ b/drivers/platform/x86/lenovo/wmi-other.c
@@ -134,15 +134,23 @@ struct tunable_attr_01 {
 	u8 feature_id;
 	u8 device_id;
 	u8 type_id;
+	u8 ctx_id; /* Index to tunable_attr_01_ctx[] */
+};
+
+struct tunable_attr_01_ctx {
 	u8 cd_mode_id; /* mode arg for searching capdata */
 	u8 cv_mode_id; /* mode arg for set/get current_value */
 };
 
+enum { TUNABLE_ATTR_01_COUNTER_BASE = __COUNTER__ + 1 };
+#define TUNABLE_ATTR_01_COUNTER (__COUNTER__ - TUNABLE_ATTR_01_COUNTER_BASE)
+
 #define DEFINE_TUNABLE_ATTR_01(_name, _device, _feature, _type)	\
-	static struct tunable_attr_01 _name = {			\
+	static const struct tunable_attr_01 _name = {		\
 		.device_id = LWMI_DEVICE_ID_##_device,		\
 		.feature_id = LWMI_FEATURE_ID_##_feature,	\
 		.type_id = LWMI_TYPE_ID_##_type,		\
+		.ctx_id = TUNABLE_ATTR_01_COUNTER,		\
 	}
 
 DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl, CPU, CPU_SPL, NONE);
@@ -168,6 +176,10 @@ DEFINE_TUNABLE_ATTR_01(dgpu_didvid, GPU, DGPU_DIDVID, NONE);
 DEFINE_TUNABLE_ATTR_01(gpu_nv_bpl, GPU, GPU_NV_BPL, NONE);
 DEFINE_TUNABLE_ATTR_01(gpu_nv_cpu_boost, GPU, GPU_NV_CPU_BOOST, NONE);
 
+enum { TUNABLE_ATTR_01_NR = TUNABLE_ATTR_01_COUNTER };
+
+static_assert(TUNABLE_ATTR_01_NR > 0 && TUNABLE_ATTR_01_NR < U8_MAX);
+
 struct lwmi_om_priv {
 	struct component_master_ops *ops;
 
@@ -183,6 +195,8 @@ struct lwmi_om_priv {
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];
 
+	struct tunable_attr_01_ctx tunable_attr_ctx[TUNABLE_ATTR_01_NR];
+
 	struct {
 		bool capdata00_collected : 1;
 		bool capdata_fan_collected : 1;
@@ -1018,7 +1032,7 @@ static u32 tunable_attr_01_id(const struct tunable_attr_01 *attr, u8 mode)
 
 struct capdata01_attr {
 	struct kobj_attribute kobj_attr;
-	struct tunable_attr_01 *tunable_attr;
+	const struct tunable_attr_01 *tunable_attr;
 	union {
 		enum attribute_property prop;
 		const char *display_name;
@@ -1027,7 +1041,7 @@ struct capdata01_attr {
 
 struct capdata01_attr_group {
 	const struct attribute_group *attr_group;
-	struct tunable_attr_01 *tunable_attr;
+	const struct tunable_attr_01 *tunable_attr;
 };
 
 static inline struct lwmi_om_priv *kobj_to_om_priv(const struct kobject *kobj)
@@ -1041,6 +1055,12 @@ kobj_attr_to_cd01_attr(const struct kobj_attribute *kattr)
 	return container_of_const(kattr, struct capdata01_attr, kobj_attr);
 }
 
+static inline struct tunable_attr_01_ctx *tunable_to_ctx(struct lwmi_om_priv *priv,
+							 const struct tunable_attr_01 *tunable_attr)
+{
+	return &priv->tunable_attr_ctx[tunable_attr->ctx_id];
+}
+
 /* Attribute Methods */
 
 /**
@@ -1098,12 +1118,13 @@ static ssize_t attr_capdata01_show(struct kobject *kobj,
 	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
 	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
 	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
+	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
 	enum attribute_property prop = cd01_attr->prop;
 	struct capdata01 capdata;
 	u32 attribute_id;
 	int value, ret;
 
-	attribute_id = tunable_attr_01_id(tunable_attr, tunable_attr->cd_mode_id);
+	attribute_id = tunable_attr_01_id(tunable_attr, ctx->cd_mode_id);
 
 	ret = lwmi_cd01_get_data(priv->cd01_list, attribute_id, &capdata);
 	if (ret)
@@ -1152,6 +1173,7 @@ static ssize_t attr_current_value_store(struct kobject *kobj,
 	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
 	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
 	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
+	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
 	struct wmi_method_args_32 args = {};
 	struct capdata01 capdata;
 	enum thermal_mode mode;
@@ -1165,7 +1187,7 @@ static ssize_t attr_current_value_store(struct kobject *kobj,
 	if (mode != LWMI_GZ_THERMAL_MODE_CUSTOM)
 		return -EBUSY;
 
-	args.arg0 = tunable_attr_01_id(tunable_attr, tunable_attr->cd_mode_id);
+	args.arg0 = tunable_attr_01_id(tunable_attr, ctx->cd_mode_id);
 
 	ret = lwmi_cd01_get_data(priv->cd01_list, args.arg0, &capdata);
 	if (ret)
@@ -1178,7 +1200,7 @@ static ssize_t attr_current_value_store(struct kobject *kobj,
 	if (value < capdata.min_value || value > capdata.max_value)
 		return -EINVAL;
 
-	args.arg0 = tunable_attr_01_id(tunable_attr, tunable_attr->cv_mode_id);
+	args.arg0 = tunable_attr_01_id(tunable_attr, ctx->cv_mode_id);
 	args.arg1 = value;
 
 	ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_SET,
@@ -1208,6 +1230,7 @@ static ssize_t attr_current_value_show(struct kobject *kobj,
 	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
 	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
 	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
+	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
 	struct wmi_method_args_32 args = {};
 	enum thermal_mode mode;
 	int retval;
@@ -1218,8 +1241,8 @@ static ssize_t attr_current_value_show(struct kobject *kobj,
 		return ret;
 
 	/* If "no-mode" is the supported mode, ensure we never send current mode */
-	if (tunable_attr->cv_mode_id == LWMI_GZ_THERMAL_MODE_NONE)
-		mode = tunable_attr->cv_mode_id;
+	if (ctx->cv_mode_id == LWMI_GZ_THERMAL_MODE_NONE)
+		mode = ctx->cv_mode_id;
 
 	args.arg0 = tunable_attr_01_id(tunable_attr, mode);
 
@@ -1243,21 +1266,22 @@ static ssize_t attr_current_value_show(struct kobject *kobj,
  * First check if the attribute has a corresponding data table under custom mode
  * (0xff), then under no mode (0x00). If either of those passes, check if the
  * supported field of the capdata struct is > 0. If it is supported, store the
- * successful mode in the cd_mode_id field of tunable_attr.
+ * successful mode in the cd_mode_id field of tunable_attr_01_ctx.
  *
  * If the attribute capdata shows it is supported, attempt to determine the mode
  * for the current value property get/set methods using a similar pattern to the
  * capdata table check. If the value returned by either mode is 0 or an error,
  * assume that mode is not supported. Otherwise, store the successful mode in the
- * cv_mode_id field of tunable_attr.
+ * cv_mode_id field of tunable_attr_01_ctx.
  *
  * If any of the above checks fail then the attribute is not fully supported.
  *
  * Return: true if capdata and set/get modes are found, otherwise false.
  */
 static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
-				      struct tunable_attr_01 *tunable_attr)
+				      const struct tunable_attr_01 *tunable_attr)
 {
+	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
 	u8 modes[2] = { LWMI_GZ_THERMAL_MODE_CUSTOM, LWMI_GZ_THERMAL_MODE_NONE };
 	struct wmi_method_args_32 args = {};
 	bool cd_mode_found = false;
@@ -1265,7 +1289,7 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 	struct capdata01 capdata;
 	int retval, ret, i;
 
-	/* Determine tunable_attr->cd_mode_id */
+	/* Determine cd_mode_id  */
 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
 		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
 
@@ -1273,7 +1297,7 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 		if (ret || !capdata.supported)
 			continue;
 
-		tunable_attr->cd_mode_id = modes[i];
+		ctx->cd_mode_id = modes[i];
 		cd_mode_found = true;
 		break;
 	}
@@ -1283,7 +1307,7 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 
 	dev_dbg(&priv->wdev->dev, "cd_mode_id: %#010x\n", args.arg0);
 
-	/* Determine tunable_attr->cv_mode_id, returns 1 if supported */
+	/* Determine cv_mode_id, returns 1 if supported  */
 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
 		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
 
@@ -1293,7 +1317,7 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 		if (ret || !retval)
 			continue;
 
-		tunable_attr->cv_mode_id = modes[i];
+		ctx->cv_mode_id = modes[i];
 		cv_mode_found = true;
 		break;
 	}

-- 
2.55.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback
  2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
                   ` (3 preceding siblings ...)
  2026-11-18 18:58 ` [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver Rong Zhang
@ 2026-11-18 18:58 ` Rong Zhang
  2026-11-18 22:23   ` Mark Pearson
  2026-11-18 22:19 ` [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Mark Pearson
  5 siblings, 1 reply; 12+ messages in thread
From: Rong Zhang @ 2026-11-18 18:58 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel, Rong Zhang

Currently, all attribute groups are created individually with support
check beforehand. This is unnecessary as unsupported attributes can be
hidden by returning SYSFS_GROUP_INVISIBLE in the is_visible() callback of
the attribute group.

Refactor the lwmi_attr_01_is_supported() function, and wrap it with
lwmi_attr_01_is_visible(). In this manner, the capdata01_attr_group
array is now replaced by a standard attribute_group array, which can be
created and removed at once using sysfs_create_groups() and
sysfs_remove_groups().

A new field `initialized' is added to tunable_attr_01_ctx to prevent it
from being initialized twice, as lwmi_attr_01_is_visible() is called
twice with n == 0, in which once is against the group and the other is
against the first attribute. The field is cleared on unbind to make sure
the next rebind caused by userspace writing to the sysfs bind file won't
get leaked data.

Signed-off-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/wmi-other.c | 118 +++++++++++++++++++-------------
 1 file changed, 72 insertions(+), 46 deletions(-)

diff --git a/drivers/platform/x86/lenovo/wmi-other.c b/drivers/platform/x86/lenovo/wmi-other.c
index f37dca7db034..6df6669e426e 100644
--- a/drivers/platform/x86/lenovo/wmi-other.c
+++ b/drivers/platform/x86/lenovo/wmi-other.c
@@ -140,6 +140,8 @@ struct tunable_attr_01 {
 struct tunable_attr_01_ctx {
 	u8 cd_mode_id; /* mode arg for searching capdata */
 	u8 cv_mode_id; /* mode arg for set/get current_value */
+
+	bool initialized;
 };
 
 enum { TUNABLE_ATTR_01_COUNTER_BASE = __COUNTER__ + 1 };
@@ -1039,11 +1041,6 @@ struct capdata01_attr {
 	};
 };
 
-struct capdata01_attr_group {
-	const struct attribute_group *attr_group;
-	const struct tunable_attr_01 *tunable_attr;
-};
-
 static inline struct lwmi_om_priv *kobj_to_om_priv(const struct kobject *kobj)
 {
 	return dev_get_drvdata(kobj_to_dev(kobj->parent));
@@ -1055,6 +1052,13 @@ kobj_attr_to_cd01_attr(const struct kobj_attribute *kattr)
 	return container_of_const(kattr, struct capdata01_attr, kobj_attr);
 }
 
+static inline const struct capdata01_attr *attr_to_cd01_attr(const struct attribute *attr)
+{
+	const struct kobj_attribute *kattr = container_of_const(attr, struct kobj_attribute, attr);
+
+	return kobj_attr_to_cd01_attr(kattr);
+}
+
 static inline struct tunable_attr_01_ctx *tunable_to_ctx(struct lwmi_om_priv *priv,
 							 const struct tunable_attr_01 *tunable_attr)
 {
@@ -1289,6 +1293,9 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 	struct capdata01 capdata;
 	int retval, ret, i;
 
+	if (ctx->initialized)
+		return true;
+
 	/* Determine cd_mode_id  */
 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
 		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
@@ -1328,7 +1335,32 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 	dev_dbg(&priv->wdev->dev, "cv_mode_id: %#010x, attribute support level: %#010x\n",
 		args.arg0, capdata.supported);
 
-	return capdata.supported > 0;
+	ctx->initialized = true;
+	return true;
+}
+
+/**
+ * lwmi_attr_01_is_visible() - Determine if the given attribute is visible.
+ * @kobj: Pointer to the driver object.
+ * @attr: Pointer to the attribute calling this function.
+ * @n: @attr is the N-th attribute.
+ *
+ * Return: umode_t or SYSFS_GROUP_INVISIBLE.
+ */
+static umode_t lwmi_attr_01_is_visible(struct kobject *kobj, const struct attribute *attr, int n)
+{
+	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
+
+	/* Only grab tunable_attr here as not all attributes are cd01_attr. */
+	if (n == 0) {
+		const struct capdata01_attr *cd01_attr = attr_to_cd01_attr(attr);
+		const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
+
+		if (!lwmi_attr_01_is_supported(priv, tunable_attr))
+			return SYSFS_GROUP_INVISIBLE;
+	}
+
+	return attr->mode;
 }
 
 /* Lenovo WMI Other Mode Attribute macros */
@@ -1377,6 +1409,7 @@ static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
 	static const struct attribute_group _attrname##_attr_group = {    \
 		.name = #_attrname,                                       \
 		.attrs_const = _attrname##_attrs,                         \
+		.is_visible_const = lwmi_attr_01_is_visible,              \
 	}
 
 /* CPU tunable attributes */
@@ -1427,30 +1460,30 @@ LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab,
 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp,
 			      "Set the GPU thermal load limit");
 
-static struct capdata01_attr_group cd01_attr_groups[] = {
-	{ &cpu_temp_attr_group, &cpu_temp },
-	{ &dgpu_boost_clk_attr_group, &dgpu_boost_clk },
-	{ &dgpu_didvid_attr_group, &dgpu_didvid },
-	{ &dgpu_enable_attr_group, &dgpu_enable },
-	{ &gpu_mode_attr_group, &gpu_mode },
-	{ &gpu_nv_ac_offset_attr_group, &gpu_nv_ac_offset },
-	{ &gpu_nv_bpl_attr_group, &gpu_nv_bpl },
-	{ &gpu_nv_cpu_boost_attr_group, &gpu_nv_cpu_boost },
-	{ &gpu_nv_ctgp_attr_group, &gpu_nv_ctgp },
-	{ &gpu_nv_ppab_attr_group, &gpu_nv_ppab },
-	{ &gpu_temp_attr_group, &gpu_temp },
-	{ &ppt_cpu_cl_attr_group, &ppt_cpu_cl },
-	{ &ppt_pl1_apu_spl_attr_group, &ppt_pl1_apu_spl },
-	{ &ppt_pl1_spl_attr_group, &ppt_pl1_spl },
-	{ &ppt_pl1_spl_cl_attr_group, &ppt_pl1_spl_cl },
-	{ &ppt_pl1_tau_attr_group, &ppt_pl1_tau },
-	{ &ppt_pl2_sppt_attr_group, &ppt_pl2_sppt },
-	{ &ppt_pl2_sppt_cl_attr_group, &ppt_pl2_sppt_cl },
-	{ &ppt_pl3_fppt_attr_group, &ppt_pl3_fppt },
-	{ &ppt_pl3_fppt_cl_attr_group, &ppt_pl3_fppt_cl },
-	{ &ppt_pl4_ipl_attr_group, &ppt_pl4_ipl },
-	{ &ppt_pl4_ipl_cl_attr_group, &ppt_pl4_ipl_cl },
-	{},
+static const struct attribute_group *const cd01_attr_groups[] = {
+	&cpu_temp_attr_group,
+	&dgpu_boost_clk_attr_group,
+	&dgpu_didvid_attr_group,
+	&dgpu_enable_attr_group,
+	&gpu_mode_attr_group,
+	&gpu_nv_ac_offset_attr_group,
+	&gpu_nv_bpl_attr_group,
+	&gpu_nv_cpu_boost_attr_group,
+	&gpu_nv_ctgp_attr_group,
+	&gpu_nv_ppab_attr_group,
+	&gpu_temp_attr_group,
+	&ppt_cpu_cl_attr_group,
+	&ppt_pl1_apu_spl_attr_group,
+	&ppt_pl1_spl_attr_group,
+	&ppt_pl1_spl_cl_attr_group,
+	&ppt_pl1_tau_attr_group,
+	&ppt_pl2_sppt_attr_group,
+	&ppt_pl2_sppt_cl_attr_group,
+	&ppt_pl3_fppt_attr_group,
+	&ppt_pl3_fppt_cl_attr_group,
+	&ppt_pl4_ipl_attr_group,
+	&ppt_pl4_ipl_cl_attr_group,
+	NULL
 };
 
 /**
@@ -1459,7 +1492,6 @@ static struct capdata01_attr_group cd01_attr_groups[] = {
  */
 static void lwmi_om_fw_attr_add(struct lwmi_om_priv *priv)
 {
-	unsigned int i;
 	int err;
 
 	err = ida_alloc(&lwmi_om_ida, GFP_KERNEL);
@@ -1485,21 +1517,14 @@ static void lwmi_om_fw_attr_add(struct lwmi_om_priv *priv)
 
 	dev_set_drvdata(priv->fw_attr_dev, priv);
 
-	for (i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++) {
-		if (!lwmi_attr_01_is_supported(priv, cd01_attr_groups[i].tunable_attr))
-			continue;
+	err = sysfs_create_groups(&priv->fw_attr_kset->kobj, cd01_attr_groups);
+	if (err)
+		goto err_remove_kset;
 
-		err = sysfs_create_group(&priv->fw_attr_kset->kobj,
-					 cd01_attr_groups[i].attr_group);
-		if (err)
-			goto err_remove_groups;
-	}
 	return;
 
-err_remove_groups:
-	while (i--)
-		sysfs_remove_group(&priv->fw_attr_kset->kobj,
-				   cd01_attr_groups[i].attr_group);
+err_remove_kset:
+	memset(priv->tunable_attr_ctx, 0, sizeof(priv->tunable_attr_ctx));
 
 	kset_unregister(priv->fw_attr_kset);
 
@@ -1525,9 +1550,10 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 	if (priv->ida_id < 0)
 		return;
 
-	for (unsigned int i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++)
-		sysfs_remove_group(&priv->fw_attr_kset->kobj,
-				   cd01_attr_groups[i].attr_group);
+	sysfs_remove_groups(&priv->fw_attr_kset->kobj, cd01_attr_groups);
+
+	/* Don't leak ctx->initialized to the next rebind from userspace. */
+	memset(priv->tunable_attr_ctx, 0, sizeof(priv->tunable_attr_ctx));
 
 	kset_unregister(priv->fw_attr_kset);
 	device_unregister(priv->fw_attr_dev);

-- 
2.55.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/5] platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks
  2026-11-18 18:58 ` [PATCH 2/5] platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks Rong Zhang
@ 2026-11-18 22:17   ` Mark Pearson
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Pearson @ 2026-11-18 22:17 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel



On Wed, Nov 18, 2026, at 1:58 PM, Rong Zhang wrote:
> Currently, all firmware-attributes are backed by numerous
> attribute-specific wrapper callbacks generated by macros. Besides,
> every firmware attribute has the same type "integer" and shares the same
> show callback int_type_show(), but LWMI_ATTR_GROUP_TUNABLE_CAP01()
> defines attr_*_type over and over again.
>
> As a result, the definition of attributes significantly bloats up the
> module size.
>
> Eliminating these attribute-specific callbacks by moving their data into
> a newly-introduced container struct capdata01_attr, and utilize
> container_of_const() to retrieve the required data in the corresponding
> callbacks, which are adjusted to make them generic and shared among
> different attributes. Also introduce a unified attr_int_type attribute
> to replace all attr_*_type attributes.
>
> With these, all firmware-attributes now use generic callbacks directly
> and are constified.
>
> This shrinks the module size by 23.8K (42.7%).
>
>       text       data        bss      total filename (before)
>      23463      32197          3      55663 lenovo-wmi-other.ko
>
>       text       data        bss      total filename (after)
>       5816      26059          3      31878 lenovo-wmi-other.ko
>
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
>  drivers/platform/x86/lenovo/wmi-other.c | 217 ++++++++++++++++----------------
>  1 file changed, 108 insertions(+), 109 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/wmi-other.c 
> b/drivers/platform/x86/lenovo/wmi-other.c
> index c31a8bd08a85..4d51b05a5acf 100644
> --- a/drivers/platform/x86/lenovo/wmi-other.c
> +++ b/drivers/platform/x86/lenovo/wmi-other.c
> @@ -981,7 +981,7 @@ struct tunable_attr_01 {
>   *
>   * Return: encoded capability data attribute ID.
>   */
> -static u32 tunable_attr_01_id(struct tunable_attr_01 *attr, u8 mode)
> +static u32 tunable_attr_01_id(const struct tunable_attr_01 *attr, u8 
> mode)
>  {
>  	return lwmi_attr_id(attr->device_id, attr->feature_id, mode, 
> attr->type_id);
>  }
> @@ -1118,6 +1118,15 @@ static struct tunable_attr_01 gpu_nv_cpu_boost = 
> {
>  	.type_id = LWMI_TYPE_ID_NONE,
>  };
> 
> +struct capdata01_attr {
> +	struct kobj_attribute kobj_attr;
> +	struct tunable_attr_01 *tunable_attr;
> +	union {
> +		enum attribute_property prop;
> +		const char *display_name;
> +	};
> +};
> +
>  struct capdata01_attr_group {
>  	const struct attribute_group *attr_group;
>  	struct tunable_attr_01 *tunable_attr;
> @@ -1128,6 +1137,12 @@ static inline struct lwmi_om_priv 
> *kobj_to_om_priv(const struct kobject *kobj)
>  	return dev_get_drvdata(kobj_to_dev(kobj->parent));
>  }
> 
> +static inline const struct capdata01_attr *
> +kobj_attr_to_cd01_attr(const struct kobj_attribute *kattr)
> +{
> +	return container_of_const(kattr, struct capdata01_attr, kobj_attr);
> +}
> +
>  /* Attribute Methods */
> 
>  /**
> @@ -1138,36 +1153,54 @@ static inline struct lwmi_om_priv 
> *kobj_to_om_priv(const struct kobject *kobj)
>   *
>   * Return: Number of characters written to buf.
>   */
> -static ssize_t int_type_show(struct kobject *kobj, struct 
> kobj_attribute *kattr,
> +static ssize_t int_type_show(struct kobject *kobj, const struct 
> kobj_attribute *kattr,
>  			     char *buf)
>  {
>  	return sysfs_emit(buf, "integer\n");
>  }
> 
> +static const struct kobj_attribute attr_int_type = __KOBJ_ATTR(type, 
> 0444, int_type_show, NULL);
> +
> +/**
> + * attr_display_name_show() - Get the display name of the given 
> attribute
> + * @kobj: Pointer to the driver object.
> + * @kattr: Pointer to the attribute calling this function.
> + * @buf: The buffer to write to.
> + *
> + * Retrieves the display name from the capdata01_attr struct for the 
> specified
> + * attribute. This function is intended to be generic so it can be 
> called from
> + * any integer attributes.
> + *
> + * Return: Number of characters written to buf.
> + */
> +static ssize_t attr_display_name_show(struct kobject *kobj,
> +				      const struct kobj_attribute *kattr, char *buf)
> +{
> +	const struct capdata01_attr *cd01_attr = 
> kobj_attr_to_cd01_attr(kattr);
> +
> +	return sysfs_emit(buf, "%s\n", cd01_attr->display_name);
> +}
> +
>  /**
>   * attr_capdata01_show() - Get the value of the specified attribute 
> property
>   *
>   * @kobj: Pointer to the driver object.
>   * @kattr: Pointer to the attribute calling this function.
>   * @buf: The buffer to write to.
> - * @tunable_attr: The attribute to be read.
> - * @prop: The property of this attribute to be read.
>   *
>   * Retrieves the given property from the capability data 01 struct for 
> the
>   * specified attribute's "custom" thermal mode. This function is 
> intended
> - * to be generic so it can be called from any integer attributes 
> "_show"
> - * function.
> - *
> - * If the WMI is success the sysfs attribute is notified.
> + * to be generic so it can be called from any integer attributes.
>   *
>   * Return: Either number of characters written to buf, or an error 
> code.
>   */
>  static ssize_t attr_capdata01_show(struct kobject *kobj,
> -				   struct kobj_attribute *kattr, char *buf,
> -				   struct tunable_attr_01 *tunable_attr,
> -				   enum attribute_property prop)
> +				   const struct kobj_attribute *kattr, char *buf)
>  {
>  	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
> +	const struct capdata01_attr *cd01_attr = 
> kobj_attr_to_cd01_attr(kattr);
> +	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
> +	enum attribute_property prop = cd01_attr->prop;
>  	struct capdata01 capdata;
>  	u32 attribute_id;
>  	int value, ret;
> @@ -1204,26 +1237,23 @@ static ssize_t attr_capdata01_show(struct 
> kobject *kobj,
>   * @kattr: Pointer to the attribute calling this function.
>   * @buf: The buffer to read from, this is parsed to `int` type.
>   * @count: Required by sysfs attribute macros, pass in from the callee 
> attr.
> - * @tunable_attr: The attribute to be stored.
>   *
>   * Sets the value of the given attribute when operating under the 
> "custom"
>   * smartfan profile. The current smartfan profile is retrieved from the
>   * lenovo-wmi-gamezone driver and error is returned if the result is 
> not
>   * "custom". This function is intended to be generic so it can be 
> called from
> - * any integer attribute's "_store" function. The integer to be sent 
> to the WMI
> - * method is range checked and an error code is returned if out of 
> range.
> - *
> - * If the value is valid and WMI is success, then the sysfs attribute 
> is
> - * notified.
> + * any integer attributes. The integer to be sent to the WMI method is 
> range
> + * checked and an error code is returned if out of range.
>   *
>   * Return: Either count, or an error code.
>   */
>  static ssize_t attr_current_value_store(struct kobject *kobj,
> -					struct kobj_attribute *kattr,
> -					const char *buf, size_t count,
> -					struct tunable_attr_01 *tunable_attr)
> +					const struct kobj_attribute *kattr,
> +					const char *buf, size_t count)
>  {
>  	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
> +	const struct capdata01_attr *cd01_attr = 
> kobj_attr_to_cd01_attr(kattr);
> +	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
>  	struct wmi_method_args_32 args = {};
>  	struct capdata01 capdata;
>  	enum thermal_mode mode;
> @@ -1266,22 +1296,20 @@ static ssize_t attr_current_value_store(struct 
> kobject *kobj,
>   * @kobj: Pointer to the driver object.
>   * @kattr: Pointer to the attribute calling this function.
>   * @buf: The buffer to write to.
> - * @tunable_attr: The attribute to be read.
>   *
>   * Retrieves the value of the given attribute for the current smartfan 
> profile.
>   * The current smartfan profile is retrieved from the 
> lenovo-wmi-gamezone driver.
>   * This function is intended to be generic so it can be called from 
> any integer
> - * attribute's "_show" function.
> - *
> - * If the WMI is success the sysfs attribute is notified.
> + * attributes.
>   *
>   * Return: Either number of characters written to buf, or an error 
> code.
>   */
>  static ssize_t attr_current_value_show(struct kobject *kobj,
> -				       struct kobj_attribute *kattr, char *buf,
> -				       struct tunable_attr_01 *tunable_attr)
> +				       const struct kobj_attribute *kattr, char *buf)
>  {
>  	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
> +	const struct capdata01_attr *cd01_attr = 
> kobj_attr_to_cd01_attr(kattr);
> +	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
>  	struct wmi_method_args_32 args = {};
>  	enum thermal_mode mode;
>  	int retval;
> @@ -1382,128 +1410,99 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  }
> 
>  /* Lenovo WMI Other Mode Attribute macros */
> -#define __LWMI_ATTR_RO(_func, _name)                                  \
> -	{                                                             \
> -		.attr = { .name = __stringify(_name), .mode = 0444 }, \
> -		.show = _func##_##_name##_show,                       \
> -	}
> 
> -#define __LWMI_ATTR_RO_AS(_name, _show)                               \
> -	{                                                             \
> -		.attr = { .name = __stringify(_name), .mode = 0444 }, \
> -		.show = _show,                                        \
> +/* Attribute current value read/write */
> +#define __LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname)                  
>                       \
> +	static const struct capdata01_attr 
> ext_attr_##_attrname##_current_value = {          \
> +		.kobj_attr = __KOBJ_ATTR(current_value, 0644,                        
>         \
> +					 attr_current_value_show, attr_current_value_store), \
> +		.tunable_attr = &_attrname,                                          
>         \
>  	}
> 
> -#define __LWMI_ATTR_RW(_func, _name) \
> -	__ATTR(_name, 0644, _func##_##_name##_show, _func##_##_name##_store)
> -
> -/* Shows a formatted static variable */
> -#define __LWMI_ATTR_SHOW_FMT(_prop, _attrname, _fmt, _val)             
>         \
> -	static ssize_t _attrname##_##_prop##_show(                            
>  \
> -		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
> -	{                                                                     
>  \
> -		return sysfs_emit(buf, _fmt, _val);                            \
> -	}                                                                     
>  \
> -	static struct kobj_attribute attr_##_attrname##_##_prop =             
>  \
> -		__LWMI_ATTR_RO(_attrname, _prop)
> +/* Attribute property read only */
> +#define __LWMI_TUNABLE_RO_CAP01(_prop, _attrname, _prop_type)          
>                      \
> +	static const struct capdata01_attr ext_attr_##_attrname##_##_prop = { 
>               \
> +		.kobj_attr = __KOBJ_ATTR(_prop, 0444, attr_capdata01_show, NULL),    
>        \
> +		.tunable_attr = &_attrname,                                          
>        \
> +		.prop = _prop_type,                                                  
>        \
> +	}
> 
> -/* Attribute current value read/write */
> -#define __LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname)                  
>         \
> -	static ssize_t _attrname##_current_value_store(                       
>  \
> -		struct kobject *kobj, struct kobj_attribute *kattr,            \
> -		const char *buf, size_t count)                                 \
> -	{                                                                     
>  \
> -		return attr_current_value_store(kobj, kattr, buf, count,       \
> -						&_attrname);                   \
> -	}                                                                     
>  \
> -	static ssize_t _attrname##_current_value_show(                        
>  \
> -		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
> -	{                                                                     
>  \
> -		return attr_current_value_show(kobj, kattr, buf, &_attrname);  \
> -	}                                                                     
>  \
> -	static struct kobj_attribute attr_##_attrname##_current_value =       
>  \
> -		__LWMI_ATTR_RW(_attrname, current_value)
> +/* Attribute display name */
> +#define __LWMI_DISPLAY_NAME(_attrname, _val)                           
>                      \
> +	static const struct capdata01_attr 
> ext_attr_##_attrname##_display_name = {          \
> +		.kobj_attr = __KOBJ_ATTR(display_name, 0444, attr_display_name_show, 
> NULL), \
> +		.tunable_attr = &_attrname,                                          
>        \
> +		.display_name = _val,                                                
>        \
> +	}
> 
> -/* Attribute property read only */
> -#define __LWMI_TUNABLE_RO_CAP01(_prop, _attrname, _prop_type)          
>         \
> -	static ssize_t _attrname##_##_prop##_show(                            
>  \
> -		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
> -	{                                                                     
>  \
> -		return attr_capdata01_show(kobj, kattr, buf, &_attrname,       \
> -					   _prop_type);                        \
> -	}                                                                     
>  \
> -	static struct kobj_attribute attr_##_attrname##_##_prop =             
>  \
> -		__LWMI_ATTR_RO(_attrname, _prop)
> -
> -#define LWMI_ATTR_GROUP_TUNABLE_CAP01(_attrname, _fsname, _dispname)   
>    \
> +#define LWMI_ATTR_GROUP_TUNABLE_CAP01(_attrname, _dispname)            
>    \
>  	__LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname);                    \
>  	__LWMI_TUNABLE_RO_CAP01(default_value, _attrname, DEFAULT_VAL);   \
> -	__LWMI_ATTR_SHOW_FMT(display_name, _attrname, "%s\n", _dispname); \
> +	__LWMI_DISPLAY_NAME(_attrname, _dispname);                        \
>  	__LWMI_TUNABLE_RO_CAP01(max_value, _attrname, MAX_VAL);           \
>  	__LWMI_TUNABLE_RO_CAP01(min_value, _attrname, MIN_VAL);           \
>  	__LWMI_TUNABLE_RO_CAP01(scalar_increment, _attrname, STEP_VAL);   \
> -	static struct kobj_attribute attr_##_attrname##_type =            \
> -		__LWMI_ATTR_RO_AS(type, int_type_show);                   \
> -	static struct attribute *_attrname##_attrs[] = {                  \
> -		&attr_##_attrname##_current_value.attr,                   \
> -		&attr_##_attrname##_default_value.attr,                   \
> -		&attr_##_attrname##_display_name.attr,                    \
> -		&attr_##_attrname##_max_value.attr,                       \
> -		&attr_##_attrname##_min_value.attr,                       \
> -		&attr_##_attrname##_scalar_increment.attr,                \
> -		&attr_##_attrname##_type.attr,                            \
> +	static const struct attribute *const _attrname##_attrs[] = {      \
> +		&ext_attr_##_attrname##_current_value.kobj_attr.attr,     \
> +		&ext_attr_##_attrname##_default_value.kobj_attr.attr,     \
> +		&ext_attr_##_attrname##_display_name.kobj_attr.attr,      \
> +		&ext_attr_##_attrname##_max_value.kobj_attr.attr,         \
> +		&ext_attr_##_attrname##_min_value.kobj_attr.attr,         \
> +		&ext_attr_##_attrname##_scalar_increment.kobj_attr.attr,  \
> +		&attr_int_type.attr,                                      \
>  		NULL,                                                     \
>  	};                                                                \
>  	static const struct attribute_group _attrname##_attr_group = {    \
> -		.name = _fsname, .attrs = _attrname##_attrs               \
> +		.name = #_attrname,                                       \
> +		.attrs_const = _attrname##_attrs,                         \
>  	}
> 
>  /* CPU tunable attributes */
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(cpu_temp, "cpu_temp",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(cpu_temp,
>  			      "Set the CPU thermal load limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_cpu_cl, "ppt_cpu_cl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_cpu_cl,
>  			      "Set the CPU cross loading power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_apu_spl, "ppt_pl1_apu_spl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_apu_spl,
>  			      "Set the APU sustained power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl, "ppt_pl1_spl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl,
>  			      "Set the CPU sustained power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl_cl, "ppt_pl1_spl_cl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl_cl,
>  			      "Set the CPU cross loading sustained power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt, "ppt_pl2_sppt",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt,
>  			      "Set the CPU slow package power tracking limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt_cl, "ppt_pl2_sppt_cl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt_cl,
>  			      "Set the CPU cross loading slow package power tracking limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt, "ppt_pl3_fppt",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt,
>  			      "Set the CPU fast package power tracking limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt_cl, "ppt_pl3_fppt_cl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt_cl,
>  			      "Set the CPU cross loading fast package power tracking limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_tau, "ppt_pl1_tau",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_tau,
>  			      "Set the CPU sustained power limit exceed duration");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl, "ppt_pl4_ipl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl,
>  			      "Set the CPU instantaneous power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl_cl, "ppt_pl4_ipl_cl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl_cl,
>  			      "Set the CPU cross loading instantaneous power limit");
> 
>  /* GPU tunable attributes */
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_boost_clk, "dgpu_boost_clk",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_boost_clk,
>  			      "Set the dedicated GPU boost clock");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_didvid, "dgpu_didvid",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_didvid,
>  			      "Get the GPU device identifier and vendor identifier");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_enable, "dgpu_enable",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_enable,
>  			      "Set the dedicated Nvidia GPU enabled status");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_mode, "gpu_mode",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_mode,
>  			      "Set the GPU mode by power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ac_offset, "gpu_nv_ac_offset",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ac_offset,
>  			      "Set the Nvidia GPU AC total processing power baseline offset");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_bpl, "gpu_nv_bpl",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_bpl,
>  			      "Set the Nvidia GPU base power limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_cpu_boost, "gpu_nv_cpu_boost",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_cpu_boost,
>  			      "Set the Nvidia GPU to CPU dynamic boost limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ctgp, "gpu_nv_ctgp",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ctgp,
>  			      "Set the GPU configurable total graphics power");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab, "gpu_nv_ppab",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab,
>  			      "Set the Nvidia GPU power performance aware boost limit");
> -LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp, "gpu_temp",
> +LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp,
>  			      "Set the GPU thermal load limit");
> 
>  static struct capdata01_attr_group cd01_attr_groups[] = {
>
This one made my head hurt...but I think it's all good.
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Mark

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface
  2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
                   ` (4 preceding siblings ...)
  2026-11-18 18:58 ` [PATCH 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback Rong Zhang
@ 2026-11-18 22:19 ` Mark Pearson
  5 siblings, 0 replies; 12+ messages in thread
From: Mark Pearson @ 2026-11-18 22:19 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel

Hi Rong,

On Wed, Nov 18, 2026, at 1:58 PM, Rong Zhang wrote:
> The driver declares `.no_singleton = true' but stores device-specific
> data to statically allocated tunable_attr_01 variables. This makes the
> driver depend on the frigile fact that no device in wild has more than

minor typo: frigile/fragile

> one LENOVO_OTHER_MODE WMI devices.
>
> The series moves tunable_attr_01.device to the firmware_attributes
> device's drvdata and introcdces a tunable_attr_01_ctx array to struct

minor typo: introcdeces/introduces

> to store c[dv]_mode_id. In this manner, all statically allocated
> variables relevant to our firmware_attributes interface are constified.
>
> This also eliminates all attribute-specific callbacks by moving their
> data into a newly-introduced container struct capdata01_attr, so that
> all remaining attribute callbacks becomes generic and are shared among
> different attributes, which shrinks the module size by 42.7%.
>
> To further follow the common practice of sysfs attributes, all attribute
> groups are now created and removed at once using sysfs_create_groups()
> and sysfs_remove_groups() by providing an is_visible() callback.
>
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
> Rong Zhang (5):
>       platform/x86: lenovo-wmi-other: Retrieve private data from 
> firmware_attributes device
>       platform/x86: lenovo-wmi-other: Eliminate attribute-specific 
> callbacks
>       platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 
> definition into a macro
>       platform/x86: lenovo-wmi-other: Properly implement no_singleton 
> driver
>       platform/x86: lenovo-wmi-other: Hide unsupported attributes using 
> is_visible() callback
>
>  drivers/platform/x86/lenovo/wmi-other.c | 566 +++++++++++++++-----------------
>  1 file changed, 259 insertions(+), 307 deletions(-)
> ---
> base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
> change-id: 2c0ec814-lwmi-no-singleton-a3f945179a50
>
> Thanks,
> Rong

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device
  2026-11-18 18:58 ` [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device Rong Zhang
@ 2026-11-18 22:19   ` Mark Pearson
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Pearson @ 2026-11-18 22:19 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel

On Wed, Nov 18, 2026, at 1:58 PM, Rong Zhang wrote:
> Currently, each tunable_attr_01 stores a pointer to the WMI device,
> which is unnecessary, as attributes' show/store callbacks can always
> access the firmware_attributes device through the kobject hierarchy.
> This also makes the driver fail to satisfy the requirements of a
> no_singleton driver.
>
> Store a pointer to the driver's privated data in the firmware_attributes
> device's drvdata, and retrieve it in attributes' show/store callbacks,
> so that we can get rid of tunable_attr_01.device and make it possible
> for upcoming refactoring to eliminate all wrapper callbacks created by
> macros and constify all statically allocated variables relevant to our
> firmware_attributes interface.
>
> Fixes: edc4b183b794 ("platform/x86: Add Lenovo Other Mode WMI Driver")
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
>  drivers/platform/x86/lenovo/wmi-other.c | 27 ++++++++++++++++-----------
>  1 file changed, 16 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/wmi-other.c 
> b/drivers/platform/x86/lenovo/wmi-other.c
> index bd64e7b44669..c31a8bd08a85 100644
> --- a/drivers/platform/x86/lenovo/wmi-other.c
> +++ b/drivers/platform/x86/lenovo/wmi-other.c
> @@ -967,7 +967,6 @@ static void lwmi_om_psy_remove(struct lwmi_om_priv 
> *priv)
>  /* ======== fw_attributes (component: lenovo-wmi-capdata 01) ======== 
> */
> 
>  struct tunable_attr_01 {
> -	struct device *dev;
>  	u8 feature_id;
>  	u8 device_id;
>  	u8 type_id;
> @@ -1124,6 +1123,11 @@ struct capdata01_attr_group {
>  	struct tunable_attr_01 *tunable_attr;
>  };
> 
> +static inline struct lwmi_om_priv *kobj_to_om_priv(const struct kobject *kobj)
> +{
> +	return dev_get_drvdata(kobj_to_dev(kobj->parent));
> +}
> +
>  /* Attribute Methods */
> 
>  /**
> @@ -1163,7 +1167,7 @@ static ssize_t attr_capdata01_show(struct kobject 
> *kobj,
>  				   struct tunable_attr_01 *tunable_attr,
>  				   enum attribute_property prop)
>  {
> -	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
> +	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
>  	struct capdata01 capdata;
>  	u32 attribute_id;
>  	int value, ret;
> @@ -1219,7 +1223,7 @@ static ssize_t attr_current_value_store(struct 
> kobject *kobj,
>  					const char *buf, size_t count,
>  					struct tunable_attr_01 *tunable_attr)
>  {
> -	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
> +	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
>  	struct wmi_method_args_32 args = {};
>  	struct capdata01 capdata;
>  	enum thermal_mode mode;
> @@ -1277,7 +1281,7 @@ static ssize_t attr_current_value_show(struct 
> kobject *kobj,
>  				       struct kobj_attribute *kattr, char *buf,
>  				       struct tunable_attr_01 *tunable_attr)
>  {
> -	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
> +	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
>  	struct wmi_method_args_32 args = {};
>  	enum thermal_mode mode;
>  	int retval;
> @@ -1304,6 +1308,7 @@ static ssize_t attr_current_value_show(struct 
> kobject *kobj,
> 
>  /**
>   * lwmi_attr_01_is_supported() - Determine if the given attribute is 
> supported.
> + * @priv: Driver private data
>   * @tunable_attr: The attribute to verify.
>   *
>   * For an attribute to be supported it must have a functional get/set 
> method,
> @@ -1324,10 +1329,10 @@ static ssize_t attr_current_value_show(struct 
> kobject *kobj,
>   *
>   * Return: true if capdata and set/get modes are found, otherwise 
> false.
>   */
> -static bool lwmi_attr_01_is_supported(struct tunable_attr_01 
> *tunable_attr)
> +static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
> +				      struct tunable_attr_01 *tunable_attr)
>  {
>  	u8 modes[2] = { LWMI_GZ_THERMAL_MODE_CUSTOM, 
> LWMI_GZ_THERMAL_MODE_NONE };
> -	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
>  	struct wmi_method_args_32 args = {};
>  	bool cd_mode_found = false;
>  	bool cv_mode_found = false;
> @@ -1350,8 +1355,7 @@ static bool lwmi_attr_01_is_supported(struct 
> tunable_attr_01 *tunable_attr)
>  	if (!cd_mode_found)
>  		return cd_mode_found;
> 
> -	dev_dbg(tunable_attr->dev,
> -		"cd_mode_id: %#010x\n", args.arg0);
> +	dev_dbg(&priv->wdev->dev, "cd_mode_id: %#010x\n", args.arg0);
> 
>  	/* Determine tunable_attr->cv_mode_id, returns 1 if supported */
>  	for (i = 0; i < ARRAY_SIZE(modes); i++) {
> @@ -1371,7 +1375,7 @@ static bool lwmi_attr_01_is_supported(struct 
> tunable_attr_01 *tunable_attr)
>  	if (!cv_mode_found)
>  		return cv_mode_found;
> 
> -	dev_dbg(tunable_attr->dev, "cv_mode_id: %#010x, attribute support 
> level: %#010x\n",
> +	dev_dbg(&priv->wdev->dev, "cv_mode_id: %#010x, attribute support 
> level: %#010x\n",
>  		args.arg0, capdata.supported);
> 
>  	return capdata.supported > 0;
> @@ -1558,9 +1562,10 @@ static void lwmi_om_fw_attr_add(struct 
> lwmi_om_priv *priv)
>  		goto err_destroy_classdev;
>  	}
> 
> +	dev_set_drvdata(priv->fw_attr_dev, priv);
> +
>  	for (i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++) {
> -		cd01_attr_groups[i].tunable_attr->dev = &priv->wdev->dev;
> -		if (!lwmi_attr_01_is_supported(cd01_attr_groups[i].tunable_attr))
> +		if (!lwmi_attr_01_is_supported(priv, cd01_attr_groups[i].tunable_attr))
>  			continue;
> 
>  		err = sysfs_create_group(&priv->fw_attr_kset->kobj,
>
> -- 
> 2.55.0

Looks good to me
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Mark

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 3/5] platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro
  2026-11-18 18:58 ` [PATCH 3/5] platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro Rong Zhang
@ 2026-11-18 22:19   ` Mark Pearson
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Pearson @ 2026-11-18 22:19 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel



On Wed, Nov 18, 2026, at 1:58 PM, Rong Zhang wrote:
> Currently, all definitions of statically allocated tunable_attr_01
> variables are open-coded, making them hard to refactor.
>
> Add a macro to define statically allocated tunable_attr_01 variables so
> that further refactoring only touches the macro itself.
>
> Meanwhile, move the definition of statically allocated tunable_attr_01
> variables before the definition of struct lwmi_om_priv, so that upcoming
> changes can reference the former as needed.
>
> No functional change intended.
>
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
>  drivers/platform/x86/lenovo/wmi-other.c | 178 +++++++-------------------------
>  1 file changed, 38 insertions(+), 140 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/wmi-other.c 
> b/drivers/platform/x86/lenovo/wmi-other.c
> index 4d51b05a5acf..252ef775f300 100644
> --- a/drivers/platform/x86/lenovo/wmi-other.c
> +++ b/drivers/platform/x86/lenovo/wmi-other.c
> @@ -130,6 +130,44 @@ struct lwmi_fan_info {
>  	long max_rpm;
>  };
> 
> +struct tunable_attr_01 {
> +	u8 feature_id;
> +	u8 device_id;
> +	u8 type_id;

Really just a minor note that everywhere else in the code goes device, feature, type.
If you end up doing a v2 can you swap the order of feature and device in the above - purely for universal karma?

> +	u8 cd_mode_id; /* mode arg for searching capdata */
> +	u8 cv_mode_id; /* mode arg for set/get current_value */
> +};
> +
> +#define DEFINE_TUNABLE_ATTR_01(_name, _device, _feature, _type)	\
> +	static struct tunable_attr_01 _name = {			\
> +		.device_id = LWMI_DEVICE_ID_##_device,		\
> +		.feature_id = LWMI_FEATURE_ID_##_feature,	\
> +		.type_id = LWMI_TYPE_ID_##_type,		\
> +	}
> +
> +DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl, CPU, CPU_SPL, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl_cl, CPU, CPU_SPL, CROSSLOAD);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl2_sppt, CPU, CPU_SPPT, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl2_sppt_cl, CPU, CPU_SPPT, CROSSLOAD);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl3_fppt, CPU, CPU_FPPT, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl3_fppt_cl, CPU, CPU_FPPT, CROSSLOAD);
> +DEFINE_TUNABLE_ATTR_01(cpu_temp, CPU, CPU_TEMP, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl1_apu_spl, CPU, CPU_APU, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_cpu_cl, CPU, CPU_CL, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl1_tau, CPU, CPU_TAU, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl4_ipl, CPU, CPU_IPL, NONE);
> +DEFINE_TUNABLE_ATTR_01(ppt_pl4_ipl_cl, CPU, CPU_IPL, CROSSLOAD);
> +DEFINE_TUNABLE_ATTR_01(gpu_nv_ppab, GPU, GPU_NV_PPAB, NONE);
> +DEFINE_TUNABLE_ATTR_01(gpu_nv_ctgp, GPU, GPU_NV_CTGP, NONE);
> +DEFINE_TUNABLE_ATTR_01(gpu_temp, GPU, GPU_TEMP, NONE);
> +DEFINE_TUNABLE_ATTR_01(gpu_nv_ac_offset, GPU, GPU_AC_OFFSET, NONE);
> +DEFINE_TUNABLE_ATTR_01(dgpu_boost_clk, GPU, DGPU_BOOST_CLK, NONE);
> +DEFINE_TUNABLE_ATTR_01(dgpu_enable, GPU, DGPU_EN, NONE);
> +DEFINE_TUNABLE_ATTR_01(gpu_mode, GPU, GPU_MODE, NONE);
> +DEFINE_TUNABLE_ATTR_01(dgpu_didvid, GPU, DGPU_DIDVID, NONE);
> +DEFINE_TUNABLE_ATTR_01(gpu_nv_bpl, GPU, GPU_NV_BPL, NONE);
> +DEFINE_TUNABLE_ATTR_01(gpu_nv_cpu_boost, GPU, GPU_NV_CPU_BOOST, NONE);
> +
>  struct lwmi_om_priv {
>  	struct component_master_ops *ops;
> 
> @@ -966,14 +1004,6 @@ static void lwmi_om_psy_remove(struct lwmi_om_priv *priv)
> 
>  /* ======== fw_attributes (component: lenovo-wmi-capdata 01) ======== */
> 
> -struct tunable_attr_01 {
> -	u8 feature_id;
> -	u8 device_id;
> -	u8 type_id;
> -	u8 cd_mode_id; /* mode arg for searching capdata */
> -	u8 cv_mode_id; /* mode arg for set/get current_value */
> -};
> -
>  /**
>   * tunable_attr_01_id() - Formats a tunable_attr_01 to a capdata 
> attribute ID
>   * @attr: The tunable_attr_01 to format.
> @@ -986,138 +1016,6 @@ static u32 tunable_attr_01_id(const struct 
> tunable_attr_01 *attr, u8 mode)
>  	return lwmi_attr_id(attr->device_id, attr->feature_id, mode, 
> attr->type_id);
>  }
> 
> -static struct tunable_attr_01 ppt_pl1_spl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_SPL,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl1_spl_cl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_SPL,
> -	.type_id = LWMI_TYPE_ID_CROSSLOAD,
> -};
> -
> -static struct tunable_attr_01 ppt_pl2_sppt = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_SPPT,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl2_sppt_cl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_SPPT,
> -	.type_id = LWMI_TYPE_ID_CROSSLOAD,
> -};
> -
> -static struct tunable_attr_01 ppt_pl3_fppt = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_FPPT,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl3_fppt_cl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_FPPT,
> -	.type_id = LWMI_TYPE_ID_CROSSLOAD,
> -};
> -
> -static struct tunable_attr_01 cpu_temp = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_TEMP,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl1_apu_spl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_APU,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_cpu_cl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_CL,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl1_tau = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_TAU,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl4_ipl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_IPL,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 ppt_pl4_ipl_cl = {
> -	.device_id = LWMI_DEVICE_ID_CPU,
> -	.feature_id = LWMI_FEATURE_ID_CPU_IPL,
> -	.type_id = LWMI_TYPE_ID_CROSSLOAD,
> -};
> -
> -static struct tunable_attr_01 gpu_nv_ppab = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_NV_PPAB,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 gpu_nv_ctgp = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_NV_CTGP,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 gpu_temp = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_TEMP,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 gpu_nv_ac_offset = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_AC_OFFSET,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 dgpu_boost_clk = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_DGPU_BOOST_CLK,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 dgpu_enable = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_DGPU_EN,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 gpu_mode = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_MODE,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 dgpu_didvid = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_DGPU_DIDVID,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 gpu_nv_bpl = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_NV_BPL,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
> -static struct tunable_attr_01 gpu_nv_cpu_boost = {
> -	.device_id = LWMI_DEVICE_ID_GPU,
> -	.feature_id = LWMI_FEATURE_ID_GPU_NV_CPU_BOOST,
> -	.type_id = LWMI_TYPE_ID_NONE,
> -};
> -
>  struct capdata01_attr {
>  	struct kobj_attribute kobj_attr;
>  	struct tunable_attr_01 *tunable_attr;
>
> -- 
> 2.55.0

Looks good to me (I'm OK if my comment above is ignored)
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Mark

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback
  2026-11-18 18:58 ` [PATCH 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback Rong Zhang
@ 2026-11-18 22:23   ` Mark Pearson
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Pearson @ 2026-11-18 22:23 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel



On Wed, Nov 18, 2026, at 1:58 PM, Rong Zhang wrote:
> Currently, all attribute groups are created individually with support
> check beforehand. This is unnecessary as unsupported attributes can be
> hidden by returning SYSFS_GROUP_INVISIBLE in the is_visible() callback of
> the attribute group.
>
> Refactor the lwmi_attr_01_is_supported() function, and wrap it with
> lwmi_attr_01_is_visible(). In this manner, the capdata01_attr_group
> array is now replaced by a standard attribute_group array, which can be
> created and removed at once using sysfs_create_groups() and
> sysfs_remove_groups().
>
> A new field `initialized' is added to tunable_attr_01_ctx to prevent it
> from being initialized twice, as lwmi_attr_01_is_visible() is called
> twice with n == 0, in which once is against the group and the other is
> against the first attribute. The field is cleared on unbind to make sure
> the next rebind caused by userspace writing to the sysfs bind file won't
> get leaked data.
>
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
>  drivers/platform/x86/lenovo/wmi-other.c | 118 +++++++++++++++++++-------------
>  1 file changed, 72 insertions(+), 46 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/wmi-other.c 
> b/drivers/platform/x86/lenovo/wmi-other.c
> index f37dca7db034..6df6669e426e 100644
> --- a/drivers/platform/x86/lenovo/wmi-other.c
> +++ b/drivers/platform/x86/lenovo/wmi-other.c
> @@ -140,6 +140,8 @@ struct tunable_attr_01 {
>  struct tunable_attr_01_ctx {
>  	u8 cd_mode_id; /* mode arg for searching capdata */
>  	u8 cv_mode_id; /* mode arg for set/get current_value */
> +
> +	bool initialized;
>  };
> 
>  enum { TUNABLE_ATTR_01_COUNTER_BASE = __COUNTER__ + 1 };
> @@ -1039,11 +1041,6 @@ struct capdata01_attr {
>  	};
>  };
> 
> -struct capdata01_attr_group {
> -	const struct attribute_group *attr_group;
> -	const struct tunable_attr_01 *tunable_attr;
> -};
> -
>  static inline struct lwmi_om_priv *kobj_to_om_priv(const struct 
> kobject *kobj)
>  {
>  	return dev_get_drvdata(kobj_to_dev(kobj->parent));
> @@ -1055,6 +1052,13 @@ kobj_attr_to_cd01_attr(const struct 
> kobj_attribute *kattr)
>  	return container_of_const(kattr, struct capdata01_attr, kobj_attr);
>  }
> 
> +static inline const struct capdata01_attr *attr_to_cd01_attr(const 
> struct attribute *attr)
> +{
> +	const struct kobj_attribute *kattr = container_of_const(attr, struct 
> kobj_attribute, attr);
> +
> +	return kobj_attr_to_cd01_attr(kattr);
> +}
> +
>  static inline struct tunable_attr_01_ctx *tunable_to_ctx(struct 
> lwmi_om_priv *priv,
>  							 const struct tunable_attr_01 *tunable_attr)
>  {
> @@ -1289,6 +1293,9 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  	struct capdata01 capdata;
>  	int retval, ret, i;
> 
> +	if (ctx->initialized)
> +		return true;
> +
>  	/* Determine cd_mode_id  */
>  	for (i = 0; i < ARRAY_SIZE(modes); i++) {
>  		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
> @@ -1328,7 +1335,32 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  	dev_dbg(&priv->wdev->dev, "cv_mode_id: %#010x, attribute support 
> level: %#010x\n",
>  		args.arg0, capdata.supported);
> 
> -	return capdata.supported > 0;
> +	ctx->initialized = true;
> +	return true;
> +}
> +
> +/**
> + * lwmi_attr_01_is_visible() - Determine if the given attribute is 
> visible.
> + * @kobj: Pointer to the driver object.
> + * @attr: Pointer to the attribute calling this function.
> + * @n: @attr is the N-th attribute.
> + *
> + * Return: umode_t or SYSFS_GROUP_INVISIBLE.
> + */
> +static umode_t lwmi_attr_01_is_visible(struct kobject *kobj, const 
> struct attribute *attr, int n)
> +{
> +	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
> +
> +	/* Only grab tunable_attr here as not all attributes are cd01_attr. */
> +	if (n == 0) {
> +		const struct capdata01_attr *cd01_attr = attr_to_cd01_attr(attr);
> +		const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
> +
> +		if (!lwmi_attr_01_is_supported(priv, tunable_attr))
> +			return SYSFS_GROUP_INVISIBLE;
> +	}
> +
> +	return attr->mode;
>  }
> 
>  /* Lenovo WMI Other Mode Attribute macros */
> @@ -1377,6 +1409,7 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  	static const struct attribute_group _attrname##_attr_group = {    \
>  		.name = #_attrname,                                       \
>  		.attrs_const = _attrname##_attrs,                         \
> +		.is_visible_const = lwmi_attr_01_is_visible,              \
>  	}
> 
>  /* CPU tunable attributes */
> @@ -1427,30 +1460,30 @@ LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab,
>  LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp,
>  			      "Set the GPU thermal load limit");
> 
> -static struct capdata01_attr_group cd01_attr_groups[] = {
> -	{ &cpu_temp_attr_group, &cpu_temp },
> -	{ &dgpu_boost_clk_attr_group, &dgpu_boost_clk },
> -	{ &dgpu_didvid_attr_group, &dgpu_didvid },
> -	{ &dgpu_enable_attr_group, &dgpu_enable },
> -	{ &gpu_mode_attr_group, &gpu_mode },
> -	{ &gpu_nv_ac_offset_attr_group, &gpu_nv_ac_offset },
> -	{ &gpu_nv_bpl_attr_group, &gpu_nv_bpl },
> -	{ &gpu_nv_cpu_boost_attr_group, &gpu_nv_cpu_boost },
> -	{ &gpu_nv_ctgp_attr_group, &gpu_nv_ctgp },
> -	{ &gpu_nv_ppab_attr_group, &gpu_nv_ppab },
> -	{ &gpu_temp_attr_group, &gpu_temp },
> -	{ &ppt_cpu_cl_attr_group, &ppt_cpu_cl },
> -	{ &ppt_pl1_apu_spl_attr_group, &ppt_pl1_apu_spl },
> -	{ &ppt_pl1_spl_attr_group, &ppt_pl1_spl },
> -	{ &ppt_pl1_spl_cl_attr_group, &ppt_pl1_spl_cl },
> -	{ &ppt_pl1_tau_attr_group, &ppt_pl1_tau },
> -	{ &ppt_pl2_sppt_attr_group, &ppt_pl2_sppt },
> -	{ &ppt_pl2_sppt_cl_attr_group, &ppt_pl2_sppt_cl },
> -	{ &ppt_pl3_fppt_attr_group, &ppt_pl3_fppt },
> -	{ &ppt_pl3_fppt_cl_attr_group, &ppt_pl3_fppt_cl },
> -	{ &ppt_pl4_ipl_attr_group, &ppt_pl4_ipl },
> -	{ &ppt_pl4_ipl_cl_attr_group, &ppt_pl4_ipl_cl },
> -	{},
> +static const struct attribute_group *const cd01_attr_groups[] = {
> +	&cpu_temp_attr_group,
> +	&dgpu_boost_clk_attr_group,
> +	&dgpu_didvid_attr_group,
> +	&dgpu_enable_attr_group,
> +	&gpu_mode_attr_group,
> +	&gpu_nv_ac_offset_attr_group,
> +	&gpu_nv_bpl_attr_group,
> +	&gpu_nv_cpu_boost_attr_group,
> +	&gpu_nv_ctgp_attr_group,
> +	&gpu_nv_ppab_attr_group,
> +	&gpu_temp_attr_group,
> +	&ppt_cpu_cl_attr_group,
> +	&ppt_pl1_apu_spl_attr_group,
> +	&ppt_pl1_spl_attr_group,
> +	&ppt_pl1_spl_cl_attr_group,
> +	&ppt_pl1_tau_attr_group,
> +	&ppt_pl2_sppt_attr_group,
> +	&ppt_pl2_sppt_cl_attr_group,
> +	&ppt_pl3_fppt_attr_group,
> +	&ppt_pl3_fppt_cl_attr_group,
> +	&ppt_pl4_ipl_attr_group,
> +	&ppt_pl4_ipl_cl_attr_group,
> +	NULL
>  };
> 
>  /**
> @@ -1459,7 +1492,6 @@ static struct capdata01_attr_group cd01_attr_groups[] = {
>   */
>  static void lwmi_om_fw_attr_add(struct lwmi_om_priv *priv)
>  {
> -	unsigned int i;
>  	int err;
> 
>  	err = ida_alloc(&lwmi_om_ida, GFP_KERNEL);
> @@ -1485,21 +1517,14 @@ static void lwmi_om_fw_attr_add(struct 
> lwmi_om_priv *priv)
> 
>  	dev_set_drvdata(priv->fw_attr_dev, priv);
> 
> -	for (i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++) {
> -		if (!lwmi_attr_01_is_supported(priv, cd01_attr_groups[i].tunable_attr))
> -			continue;
> +	err = sysfs_create_groups(&priv->fw_attr_kset->kobj, cd01_attr_groups);
> +	if (err)
> +		goto err_remove_kset;
> 
> -		err = sysfs_create_group(&priv->fw_attr_kset->kobj,
> -					 cd01_attr_groups[i].attr_group);
> -		if (err)
> -			goto err_remove_groups;
> -	}
>  	return;
> 
> -err_remove_groups:
> -	while (i--)
> -		sysfs_remove_group(&priv->fw_attr_kset->kobj,
> -				   cd01_attr_groups[i].attr_group);
> +err_remove_kset:
> +	memset(priv->tunable_attr_ctx, 0, sizeof(priv->tunable_attr_ctx));
> 
>  	kset_unregister(priv->fw_attr_kset);
> 
> @@ -1525,9 +1550,10 @@ static void lwmi_om_fw_attr_remove(struct 
> lwmi_om_priv *priv)
>  	if (priv->ida_id < 0)
>  		return;
> 
> -	for (unsigned int i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++)
> -		sysfs_remove_group(&priv->fw_attr_kset->kobj,
> -				   cd01_attr_groups[i].attr_group);
> +	sysfs_remove_groups(&priv->fw_attr_kset->kobj, cd01_attr_groups);
> +
> +	/* Don't leak ctx->initialized to the next rebind from userspace. */
> +	memset(priv->tunable_attr_ctx, 0, sizeof(priv->tunable_attr_ctx));
> 
>  	kset_unregister(priv->fw_attr_kset);
>  	device_unregister(priv->fw_attr_dev);
>
Looks good to me
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Mark

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver
  2026-11-18 18:58 ` [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver Rong Zhang
@ 2026-11-18 22:27   ` Mark Pearson
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Pearson @ 2026-11-18 22:27 UTC (permalink / raw)
  To: Rong Zhang, Derek J . Clark, Hans de Goede, Ilpo Järvinen,
	Alok Tiwari, Armin Wolf
  Cc: platform-driver-x86, linux-kernel



On Wed, Nov 18, 2026, at 1:58 PM, Rong Zhang wrote:
> The driver declares `.no_singleton = true' but stores device-specific
> data to statically allocated tunable_attr_01 variables. This makes the
> driver depend on the frigile fact that no device in wild has more than

Minor typo - frigile/fragile

> one LENOVO_OTHER_MODE WMI devices.
>

As an aside - is this really fragile? I mean realistically it's never going to happen is it?

> Add unique indexes to all statically allocated tunable_attr_01 variables
> with some __COUNTER__ tricks, and use them to access lwmi_om_priv's
> newly-introduced tunable_attr_01_ctx array. In this manner, move all
> device-specific data to the tunable_attr_01_ctx array, and constify all
> statically allocated tunable_attr_01 variables to make the driver a
> truly no_singleton driver.
>
> Fixes: 03bb5147da08 ("platform/x86: lenovo-wmi-other: Limit adding 
> attributes to supported devices")
> Signed-off-by: Rong Zhang <i@rong.moe>
> ---
>  drivers/platform/x86/lenovo/wmi-other.c | 54 
> ++++++++++++++++++++++++---------
>  1 file changed, 39 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo/wmi-other.c 
> b/drivers/platform/x86/lenovo/wmi-other.c
> index 252ef775f300..f37dca7db034 100644
> --- a/drivers/platform/x86/lenovo/wmi-other.c
> +++ b/drivers/platform/x86/lenovo/wmi-other.c
> @@ -134,15 +134,23 @@ struct tunable_attr_01 {
>  	u8 feature_id;
>  	u8 device_id;
>  	u8 type_id;
> +	u8 ctx_id; /* Index to tunable_attr_01_ctx[] */
> +};
> +
> +struct tunable_attr_01_ctx {
>  	u8 cd_mode_id; /* mode arg for searching capdata */
>  	u8 cv_mode_id; /* mode arg for set/get current_value */
>  };
> 
> +enum { TUNABLE_ATTR_01_COUNTER_BASE = __COUNTER__ + 1 };
> +#define TUNABLE_ATTR_01_COUNTER (__COUNTER__ - TUNABLE_ATTR_01_COUNTER_BASE)
> +
>  #define DEFINE_TUNABLE_ATTR_01(_name, _device, _feature, _type)	\
> -	static struct tunable_attr_01 _name = {			\
> +	static const struct tunable_attr_01 _name = {		\
>  		.device_id = LWMI_DEVICE_ID_##_device,		\
>  		.feature_id = LWMI_FEATURE_ID_##_feature,	\
>  		.type_id = LWMI_TYPE_ID_##_type,		\
> +		.ctx_id = TUNABLE_ATTR_01_COUNTER,		\
>  	}
> 
>  DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl, CPU, CPU_SPL, NONE);
> @@ -168,6 +176,10 @@ DEFINE_TUNABLE_ATTR_01(dgpu_didvid, GPU, 
> DGPU_DIDVID, NONE);
>  DEFINE_TUNABLE_ATTR_01(gpu_nv_bpl, GPU, GPU_NV_BPL, NONE);
>  DEFINE_TUNABLE_ATTR_01(gpu_nv_cpu_boost, GPU, GPU_NV_CPU_BOOST, NONE);
> 
> +enum { TUNABLE_ATTR_01_NR = TUNABLE_ATTR_01_COUNTER };
> +
> +static_assert(TUNABLE_ATTR_01_NR > 0 && TUNABLE_ATTR_01_NR < U8_MAX);
> +
>  struct lwmi_om_priv {
>  	struct component_master_ops *ops;
> 
> @@ -183,6 +195,8 @@ struct lwmi_om_priv {
> 
>  	struct lwmi_fan_info fan_info[LWMI_FAN_NR];
> 
> +	struct tunable_attr_01_ctx tunable_attr_ctx[TUNABLE_ATTR_01_NR];
> +
>  	struct {
>  		bool capdata00_collected : 1;
>  		bool capdata_fan_collected : 1;
> @@ -1018,7 +1032,7 @@ static u32 tunable_attr_01_id(const struct 
> tunable_attr_01 *attr, u8 mode)
> 
>  struct capdata01_attr {
>  	struct kobj_attribute kobj_attr;
> -	struct tunable_attr_01 *tunable_attr;
> +	const struct tunable_attr_01 *tunable_attr;
>  	union {
>  		enum attribute_property prop;
>  		const char *display_name;
> @@ -1027,7 +1041,7 @@ struct capdata01_attr {
> 
>  struct capdata01_attr_group {
>  	const struct attribute_group *attr_group;
> -	struct tunable_attr_01 *tunable_attr;
> +	const struct tunable_attr_01 *tunable_attr;
>  };
> 
>  static inline struct lwmi_om_priv *kobj_to_om_priv(const struct 
> kobject *kobj)
> @@ -1041,6 +1055,12 @@ kobj_attr_to_cd01_attr(const struct 
> kobj_attribute *kattr)
>  	return container_of_const(kattr, struct capdata01_attr, kobj_attr);
>  }
> 
> +static inline struct tunable_attr_01_ctx *tunable_to_ctx(struct 
> lwmi_om_priv *priv,
> +							 const struct tunable_attr_01 *tunable_attr)
> +{
> +	return &priv->tunable_attr_ctx[tunable_attr->ctx_id];
> +}
> +
>  /* Attribute Methods */
> 
>  /**
> @@ -1098,12 +1118,13 @@ static ssize_t attr_capdata01_show(struct kobject *kobj,
>  	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
>  	const struct capdata01_attr *cd01_attr = kobj_attr_to_cd01_attr(kattr);
>  	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
> +	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
>  	enum attribute_property prop = cd01_attr->prop;
>  	struct capdata01 capdata;
>  	u32 attribute_id;
>  	int value, ret;
> 
> -	attribute_id = tunable_attr_01_id(tunable_attr, tunable_attr->cd_mode_id);
> +	attribute_id = tunable_attr_01_id(tunable_attr, ctx->cd_mode_id);
> 
>  	ret = lwmi_cd01_get_data(priv->cd01_list, attribute_id, &capdata);
>  	if (ret)
> @@ -1152,6 +1173,7 @@ static ssize_t attr_current_value_store(struct 
> kobject *kobj,
>  	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
>  	const struct capdata01_attr *cd01_attr = 
> kobj_attr_to_cd01_attr(kattr);
>  	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
> +	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
>  	struct wmi_method_args_32 args = {};
>  	struct capdata01 capdata;
>  	enum thermal_mode mode;
> @@ -1165,7 +1187,7 @@ static ssize_t attr_current_value_store(struct 
> kobject *kobj,
>  	if (mode != LWMI_GZ_THERMAL_MODE_CUSTOM)
>  		return -EBUSY;
> 
> -	args.arg0 = tunable_attr_01_id(tunable_attr, tunable_attr->cd_mode_id);
> +	args.arg0 = tunable_attr_01_id(tunable_attr, ctx->cd_mode_id);
> 
>  	ret = lwmi_cd01_get_data(priv->cd01_list, args.arg0, &capdata);
>  	if (ret)
> @@ -1178,7 +1200,7 @@ static ssize_t attr_current_value_store(struct 
> kobject *kobj,
>  	if (value < capdata.min_value || value > capdata.max_value)
>  		return -EINVAL;
> 
> -	args.arg0 = tunable_attr_01_id(tunable_attr, tunable_attr->cv_mode_id);
> +	args.arg0 = tunable_attr_01_id(tunable_attr, ctx->cv_mode_id);
>  	args.arg1 = value;
> 
>  	ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_SET,
> @@ -1208,6 +1230,7 @@ static ssize_t attr_current_value_show(struct 
> kobject *kobj,
>  	struct lwmi_om_priv *priv = kobj_to_om_priv(kobj);
>  	const struct capdata01_attr *cd01_attr = 
> kobj_attr_to_cd01_attr(kattr);
>  	const struct tunable_attr_01 *tunable_attr = cd01_attr->tunable_attr;
> +	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
>  	struct wmi_method_args_32 args = {};
>  	enum thermal_mode mode;
>  	int retval;
> @@ -1218,8 +1241,8 @@ static ssize_t attr_current_value_show(struct 
> kobject *kobj,
>  		return ret;
> 
>  	/* If "no-mode" is the supported mode, ensure we never send current mode */
> -	if (tunable_attr->cv_mode_id == LWMI_GZ_THERMAL_MODE_NONE)
> -		mode = tunable_attr->cv_mode_id;
> +	if (ctx->cv_mode_id == LWMI_GZ_THERMAL_MODE_NONE)
> +		mode = ctx->cv_mode_id;
> 
>  	args.arg0 = tunable_attr_01_id(tunable_attr, mode);
> 
> @@ -1243,21 +1266,22 @@ static ssize_t attr_current_value_show(struct 
> kobject *kobj,
>   * First check if the attribute has a corresponding data table under 
> custom mode
>   * (0xff), then under no mode (0x00). If either of those passes, check 
> if the
>   * supported field of the capdata struct is > 0. If it is supported, 
> store the
> - * successful mode in the cd_mode_id field of tunable_attr.
> + * successful mode in the cd_mode_id field of tunable_attr_01_ctx.
>   *
>   * If the attribute capdata shows it is supported, attempt to 
> determine the mode
>   * for the current value property get/set methods using a similar 
> pattern to the
>   * capdata table check. If the value returned by either mode is 0 or 
> an error,
>   * assume that mode is not supported. Otherwise, store the successful 
> mode in the
> - * cv_mode_id field of tunable_attr.
> + * cv_mode_id field of tunable_attr_01_ctx.
>   *
>   * If any of the above checks fail then the attribute is not fully 
> supported.
>   *
>   * Return: true if capdata and set/get modes are found, otherwise 
> false.
>   */
>  static bool lwmi_attr_01_is_supported(struct lwmi_om_priv *priv,
> -				      struct tunable_attr_01 *tunable_attr)
> +				      const struct tunable_attr_01 *tunable_attr)
>  {
> +	struct tunable_attr_01_ctx *ctx = tunable_to_ctx(priv, tunable_attr);
>  	u8 modes[2] = { LWMI_GZ_THERMAL_MODE_CUSTOM, 
> LWMI_GZ_THERMAL_MODE_NONE };
>  	struct wmi_method_args_32 args = {};
>  	bool cd_mode_found = false;
> @@ -1265,7 +1289,7 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  	struct capdata01 capdata;
>  	int retval, ret, i;
> 
> -	/* Determine tunable_attr->cd_mode_id */
> +	/* Determine cd_mode_id  */
>  	for (i = 0; i < ARRAY_SIZE(modes); i++) {
>  		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
> 
> @@ -1273,7 +1297,7 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  		if (ret || !capdata.supported)
>  			continue;
> 
> -		tunable_attr->cd_mode_id = modes[i];
> +		ctx->cd_mode_id = modes[i];
>  		cd_mode_found = true;
>  		break;
>  	}
> @@ -1283,7 +1307,7 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
> 
>  	dev_dbg(&priv->wdev->dev, "cd_mode_id: %#010x\n", args.arg0);
> 
> -	/* Determine tunable_attr->cv_mode_id, returns 1 if supported */
> +	/* Determine cv_mode_id, returns 1 if supported  */
>  	for (i = 0; i < ARRAY_SIZE(modes); i++) {
>  		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
> 
> @@ -1293,7 +1317,7 @@ static bool lwmi_attr_01_is_supported(struct 
> lwmi_om_priv *priv,
>  		if (ret || !retval)
>  			continue;
> 
> -		tunable_attr->cv_mode_id = modes[i];
> +		ctx->cv_mode_id = modes[i];
>  		cv_mode_found = true;
>  		break;
>  	}
>
> -- 
> 2.55.0

This one defeated me I'm afraid (the counter bits in particular) - I need to look at it with fresh eyes as my brain has packed up for the day.

Mark

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-18 22:27 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 18:58 [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Rong Zhang
2026-11-18 18:58 ` [PATCH 1/5] platform/x86: lenovo-wmi-other: Retrieve private data from firmware_attributes device Rong Zhang
2026-11-18 22:19   ` Mark Pearson
2026-11-18 18:58 ` [PATCH 2/5] platform/x86: lenovo-wmi-other: Eliminate attribute-specific callbacks Rong Zhang
2026-11-18 22:17   ` Mark Pearson
2026-11-18 18:58 ` [PATCH 3/5] platform/x86: lenovo-wmi-other: Factor out tunable_attr_01 definition into a macro Rong Zhang
2026-11-18 22:19   ` Mark Pearson
2026-11-18 18:58 ` [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver Rong Zhang
2026-11-18 22:27   ` Mark Pearson
2026-11-18 18:58 ` [PATCH 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback Rong Zhang
2026-11-18 22:23   ` Mark Pearson
2026-11-18 22:19 ` [PATCH 0/5] platform/x86: lenovo-wmi-other: Refine firmware_attributes interface Mark Pearson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®