mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Mark Pearson" <mpearson-lenovo@squebb.ca>
To: "Rong Zhang" <i@rong.moe>,
	"Derek J . Clark" <derekjohn.clark@gmail.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Alok Tiwari" <alok.a.tiwari@oracle.com>,
	"Armin Wolf" <W_Armin@gmx.de>
Cc: "platform-driver-x86@vger.kernel.org"
	<platform-driver-x86@vger.kernel.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver
Date: Wed, 18 Nov 2026 17:27:31 -0500	[thread overview]
Message-ID: <b5c81884-73b0-4d43-98e5-cd2fedcf583f@app.fastmail.com> (raw)
In-Reply-To: <20261119-lwmi-no-singleton-v1-4-6b06fcfb5126@rong.moe>



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

  reply	other threads:[~2026-11-18 22:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b5c81884-73b0-4d43-98e5-cd2fedcf583f@app.fastmail.com \
    --to=mpearson-lenovo@squebb.ca \
    --cc=W_Armin@gmx.de \
    --cc=alok.a.tiwari@oracle.com \
    --cc=derekjohn.clark@gmail.com \
    --cc=hansg@kernel.org \
    --cc=i@rong.moe \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®