mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rong Zhang <i@rong.moe>
To: "Mark Pearson" <mpearson-lenovo@squebb.ca>,
	"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: Fri, 20 Nov 2026 02:23:18 +0800	[thread overview]
Message-ID: <5b82091b356a4134ba59e032bd927049b3a6451a.camel@rong.moe> (raw)
In-Reply-To: <b5c81884-73b0-4d43-98e5-cd2fedcf583f@app.fastmail.com>

Hi Mark,

Thanks for reviewing this.

On Wed, 2026-11-18 at 17:27 -0500, Mark Pearson wrote:
> 
> 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

Whoops, I constantly type this word in the wrong way. Sigh...

> 
> > one LENOVO_OTHER_MODE WMI devices.
> > 
> 
> As an aside - is this really fragile? I mean realistically it's never going to happen is it?

I wrote the commit message like this because depending on global states
violates the requirements of a no_singleton driver anyway. I will
rephrase it to make it focus on constifying tunable_attr_01 instead.

> 
> > 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.

If the __COUNTER__ tricks are a bit hard for you to keep up with, let me
explain below:

> > 
> > 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 };

__COUNTER__ is like counter++ in C, but for C preprocessor.

This uses an enumeration to retrieve its value (say, 403). Once
retrieved, __COUNTER__ is self-incremented, so the retrieved value plus
one equals the "current" value.

   TUNABLE_ATTR_01_COUNTER_BASE = (counter++) + 1 = 403 + 1 = 404

> > +#define TUNABLE_ATTR_01_COUNTER (__COUNTER__ - TUNABLE_ATTR_01_COUNTER_BASE)

Defining a macro does not consume the __COUNTER__. The "current" value
remains unchanged. That's why enumeration is used above and below.

> > +
> >  #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,		\

This does not consume the __COUNTER__, too.

> >  	}
> > 
> >  DEFINE_TUNABLE_ATTR_01(ppt_pl1_spl, CPU, CPU_SPL, NONE);

Here it's consumed!

   ppt_pl1_spl.ctx_id      = (counter++) - 404 = 404 - 404 = 0
   ppt_pl1_spl_cl.ctx_id   = (counter++) - 404 = 405 - 404 = 1

...and so on.

> > @@ -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);

   gpu_nv_cpu_boost.ctx_id = (counter++) - 404 = 425 - 404 = 21

> > 
> > +enum { TUNABLE_ATTR_01_NR = TUNABLE_ATTR_01_COUNTER };

   TUNABLE_ATTR_01_NR      = (counter++) - 404 = 426 - 404 = 22

That's it! In this manner, the C preprocessor stores self-increment ids
to all tunable_attr_01 variables, so that the latter can be constified
during compilation.

For your minor note in PATCH 3, I will fix the order when I resubmit the
series. I will wait for Derek's review before the resubmission.

Thanks,
Rong

> > +
> > +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-19 18:28 UTC|newest]

Thread overview: 14+ 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
2026-11-19 18:23     ` Rong Zhang [this message]
2026-11-19 18:43       ` Derek J. Clark
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=5b82091b356a4134ba59e032bd927049b3a6451a.camel@rong.moe \
    --to=i@rong.moe \
    --cc=W_Armin@gmx.de \
    --cc=alok.a.tiwari@oracle.com \
    --cc=derekjohn.clark@gmail.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpearson-lenovo@squebb.ca \
    --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®