Re: [PATCH 4/5] platform/x86: lenovo-wmi-other: Properly implement no_singleton driver

From: Mark Pearson

Date: Mon Nov 23 2026 - 11:42:43 EST



On Thu, Nov 19, 2026, at 1:23 PM, Rong Zhang wrote:
> 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@xxxxxxxx>
>> > ---
>> > 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
>

Thanks for the more detailed explanation :)
Reviewed-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>

Mark