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 5/5] platform/x86: lenovo-wmi-other: Hide unsupported attributes using is_visible() callback
Date: Wed, 18 Nov 2026 17:23:11 -0500	[thread overview]
Message-ID: <b6f3d294-6be4-4d01-99a3-9d41c3c010a0@app.fastmail.com> (raw)
In-Reply-To: <20261119-lwmi-no-singleton-v1-5-6b06fcfb5126@rong.moe>



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

  reply	other threads:[~2026-11-18 22:23 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
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 [this message]
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=b6f3d294-6be4-4d01-99a3-9d41c3c010a0@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®