From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.112 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795436614; b=x9FCQNHor7JjR/bg+7kFDJ7JO4/Dzt9zCgq8swL9W2YLXnHo43puXzzTVxV1vW5N5+vU 3AB9u3tF9oIbZFeiPnGkC3MTdqK3eR+MsDjjZSEArtrDImf7AVQI24Lx/nGDuvMemZAcP wbM5FUM9/sxdIT1/cOhucyJJ3eE5uokuHpFbrI+Bd2A+c9mKIihMFu2fFB33qiS64RBGc G8ISscmfjR+ogJYggFc9EULJegVhuhKv+dZ392KnC4m3D+SRrpq8Zi5GNYf9M0VPYSWjZ UAGYdA3vFnwpLjrWWQOil3AIkDhQ8AQLSgvmjt3FMF+TWcnRZ25X4/Rm+kW8MouOhZg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795436614; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Subject:From:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date: MIME-Version:User-Agent:X-ZohoMailClient; bh=+0Iq7/O1gnMZeJW4lf9/hEDndImbz4PDUBPnQB4YBz4=; b=YGzucWDuy+riZOY1BqaSsNFTg0nInzhte55n4MU/BKt2joazkr37jFF/lsSS0RNzuQj1 FBXx+v6TNdXJhAMXp9uRBPkc9BeBjynD5QggVQ8aeP6WaU6Bv2Cl8wHJWgI0MDZRDP8Vi JYOZCa5C088rTjrnWMeUB8ZIGk8BKY1Z5mHdvb67OOiwiw8N5RWD9/gpPAY6omwOQn7v8 mtNK4/Sxc8JHSQLorRY43iTMsDYeJZ8zcbQqqrT+DKo1XhABEtdPwqwV2cnAVW92UPV7K 3GU7O/ibCRbkWCzQpGQf7m0q0H7DC9J+uWhounuDwf6Qd7UhIkWq17umqIbgXJ7Ov3g== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=FyEjOoa1; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.112 Received: from sender5-pp-f112.zoho.com (sender5-pp-f112.zoho.com [165.173.182.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30079606A31A; Mon, 23 Nov 2026 12:23:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 30079606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=FyEjOoa1 ARC-Seal: i=1; a=rsa-sha256; t=1795436533; cv=none; d=zohomail.com; s=zohoarc; b=X+QKPJ4XIrMNZ9ThznmxhHmobGWLEMNUskjNy8KJG2oTnIhfwwWMapW1PLe5z14cKlNInZtRY68q2I6D5vVKuq9DN3e3H2d3ftcrhazg9yr39znkSataGzrDiF6BhHpItWFMgOsBTWVrjhkq8N9z4WEJShj+3bro9cNeaNCjJUw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795436533; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=+0Iq7/O1gnMZeJW4lf9/hEDndImbz4PDUBPnQB4YBz4=; b=OY7iWwF29wtuqnJ0dGxJGCYEgIUbk0aBiEqbZKc9lCyHS1auo9sW9iXayLam4lBy+xcJlr9lyteNW51AzpUAvefElwK3/C1o4DDJWQ+osQBQQnJFdgB3mWXf+n2aM3rSzarMCzapu+mg5gjM6SON2KiDOJPpDBe9Mu02sKMIcto= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=FyEjOoa1Qdxf64k2Z7IuOt/v3Ymn/gePU/cqmgaDgiYKf9H2HE/ei9y7Sg79KAcr+VFAoffbScfTD6izEG6kAMjxKaq3XYz7FT3r2e9mS5FzKmeVvRqhr7+pzP1TPYeZTMZCTC+3jaRtdi5JgHSiBwXzJ8gU8R0IH4zQ1Are/canbaY31X+ZLopDkVwmM0YRdhX0IkmxkwLeLk961JNsH4HNl5wg88MeuZqTflIDwNSnQbY6FWFXK3vEwE4yfXqnn5KSDCnN8dtZ3+o0blkoTIoJ3xSCtf96rTfYYhbIsZsb8OaVNo4xFh0rFGsuWsp5qaUf7SyFK1TQ1TRu8fjX5w==; c=relaxed/relaxed; s=zmail2048; d=rong.moe; v=1; bh=+0Iq7/O1gnMZeJW4lf9/hEDndImbz4PDUBPnQB4YBz4=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1795436530959650.8267467877844; Mon, 23 Nov 2026 04:22:10 -0800 (PST) Message-ID: Subject: Re: [PATCH v2] platform/x86: lenovo-wmi-other: Add raw GetDataByPackage interface From: Rong Zhang To: Alan Pluto , platform-driver-x86@vger.kernel.org Cc: mpearson-lenovo@squebb.ca, derekjohn.clark@gmail.com, W_Armin@gmx.de, corbet@lwn.net, skhan@linuxfoundation.org, rdunlap@infradead.org, hansg@kernel.org, ilpo.jarvinen@linux.intel.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20261123061120.18740-1-alanpluto@163.com> References: <20261116040532.154928-1-alanpluto@163.com> <20261123061120.18740-1-alanpluto@163.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Date: Mon, 23 Nov 2026 20:21:26 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Evolution 3.62.1-4 X-ZohoMailClient: External Hi Alan, On Mon, 2026-11-23 at 14:11 +0800, Alan Pluto wrote: > Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent. > method (99) to check device readiness. Expose the method through debugfs > so the firmware request and response can be inspected on Linux without > adding a stable userspace ABI. >=20 > The interface is disabled by default and can be enabled by passing > enable_get_data_by_package=3D1 when loading the module. >=20 > Signed-off-by: Alan Pluto > --- > Changes in v2: > - Replace the character device with a debugfs file, disabled by default. > - Allow one open per WMI device and require a single read for the respons= e. > - Use scoped locks, lockdep checks, automatic buffer cleanup, and the > WMI helper's minimum response size check. >=20 > Documentation/wmi/devices/lenovo-wmi-other.rst | 12 +- > drivers/platform/x86/lenovo/wmi-other.c | 208 +++++++++++++++++++= ++++++ > 2 files changed, 219 insertions(+), 1 deletion(-) >=20 > diff --git a/Documentation/wmi/devices/lenovo-wmi-other.rst b/Documentati= on/wmi/devices/lenovo-wmi-other.rst > index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4= 047b1f09c5b770f 100644 > --- a/Documentation/wmi/devices/lenovo-wmi-other.rst > +++ b/Documentation/wmi/devices/lenovo-wmi-other.rst > @@ -33,6 +33,15 @@ under the following path: > =20 > Additionally, this driver also exports attributes to HWMON. > =20 > +GetDataByPackage diagnostics > +~~~~~~~~~~~~~~~~~~~~~~~~~~~ Missing a ~ > + > +The driver does not create the diagnostic file by default. To enable it, > +pass ``enable_get_data_by_package=3D1`` when loading ``lenovo-wmi-other`= `. > +The file is at ``lenovo_wmi//get_data_by_package``. > +Not all devices support the method. Use only commands whose behavior and > +side effects are understood. > + > LENOVO_CAPABILITY_DATA_00 > ------------------------- > =20 > @@ -107,7 +116,8 @@ WMI interface description > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D > =20 > The WMI interface description can be decoded from the embedded binary MO= F (bmof) > -data using the `bmfdec `_ utility: > +data using the `bmfdec `_ utility. This = older > +example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 = bytes. ??? Could you move the sentence to the right place? > =20 > :: > =20 > diff --git a/drivers/platform/x86/lenovo/wmi-other.c b/drivers/platform/x= 86/lenovo/wmi-other.c > index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff= 0672e68e65517f0 100644 > --- a/drivers/platform/x86/lenovo/wmi-other.c > +++ b/drivers/platform/x86/lenovo/wmi-other.c > @@ -27,13 +27,16 @@ > */ > =20 > #include > +#include > #include > #include > #include > #include > +#include > #include > #include > #include > +#include > #include > #include > #include > @@ -41,9 +44,12 @@ > #include > #include > #include > +#include > #include > #include > +#include > #include > +#include > #include > =20 > #include > @@ -90,6 +96,8 @@ enum lwmi_feature_id_psu { > =20 > #define LWMI_FEATURE_VALUE_GET 17 > #define LWMI_FEATURE_VALUE_SET 18 > +#define LWMI_DATA_PACKAGE_GET 99 > +#define LWMI_DATA_PACKAGE_INPUT_MAX 100 > =20 > #define LWMI_FAN_ID_BASE 1 > #define LWMI_FAN_NR 4 > @@ -141,6 +149,8 @@ struct lwmi_om_priv { > =09struct device *fw_attr_dev; > =09struct kset *fw_attr_kset; > =09struct wmi_device *wdev; > +=09struct dentry *debugfs_dir; > +=09atomic_t package_opened; > =09int ida_id; > =20 > =09struct lwmi_fan_info fan_info[LWMI_FAN_NR]; > @@ -156,6 +166,23 @@ struct lwmi_om_priv { > =09bool bh_registered; > }; > =20 > +struct lwmi_package_response { > +=09__le32 data_size; > +=09u8 data[]; > +}; Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent. > + > +struct lwmi_package_file { > +=09struct lwmi_om_priv *priv; > +=09/* Protects the pending response on shared descriptors. */ > +=09struct mutex lock; > +=09struct lwmi_package_response *response; > +=09size_t response_len; > +}; > + > +static bool enable_get_data_by_package; > +module_param(enable_get_data_by_package, bool, 0400); > +MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackag= e debugfs interface"); > + > /* > * Visibility of fan channels: > * > @@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om= _priv *priv) > =20 > /* =3D=3D=3D=3D=3D=3D=3D=3D Self (master: lenovo-wmi-other) =3D=3D=3D=3D= =3D=3D=3D=3D */ > =20 > +static int lwmi_package_open(struct inode *inode, struct file *file) > +{ > +=09struct lwmi_om_priv *priv =3D inode->i_private; > +=09struct lwmi_package_file *state __free(kfree) =3D NULL; The declarations of __free() pointers should be placed at where it's allocated, i.e., ... > + > +=09if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) !=3D (FMODE_READ | FM= ODE_WRITE)) > +=09=09return -EINVAL; > + > +=09state =3D kzalloc_obj(*state); ...here. Meanwhile,=C2=A0opening the file while priv->package_opened =3D=3D 1 alloca= tes memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either. > +=09if (!state) > +=09=09return -ENOMEM; > + > +=09if (atomic_cmpxchg(&priv->package_opened, 0, 1)) atomic_cmpxchg_acquire() > +=09=09return -EBUSY; > + > +=09mutex_init(&state->lock); > +=09state->priv =3D priv; > +=09file->private_data =3D no_free_ptr(state); > + > +=09return stream_open(inode, file); > +} > + > +static int lwmi_package_release(struct inode *inode, struct file *file) > +{ > +=09struct lwmi_package_file *state =3D file->private_data; > +=09struct dentry *dentry =3D file->f_path.dentry; > + > +=09kfree_sensitive(state->response); You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation. Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?=20 TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface. An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code. Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions. > +=09mutex_destroy(&state->lock); > + > +=09/* Unlike read and write, release can run after debugfs removal. */ The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects: The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid. > +=09if (!debugfs_file_get(dentry)) { > +=09=09atomic_set(&state->priv->package_opened, 0); atomic_set_release() > +=09=09debugfs_file_put(dentry); > +=09} > + > +=09kfree(state); > +=09return 0; > +} > + > +static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data= ) > +{ > +=09struct lwmi_om_priv *priv =3D state->priv; > +=09struct wmi_buffer input =3D { .length =3D LWMI_DATA_PACKAGE_INPUT_MAX= , .data =3D data }; > +=09struct wmi_buffer output; > +=09u32 data_size; > +=09int ret; > + > +=09lockdep_assert_held(&state->lock); > + > +=09/* The tested firmware needs two calls before its response is usable.= */ > +=09ret =3D wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET, > +=09=09=09=09 &input, &output, 0); > +=09if (ret) > +=09=09return ret; > + > +=09kfree_sensitive(output.data); > + > +=09ret =3D wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET, > +=09=09=09=09 &input, &output, sizeof(struct lwmi_package_response)); > +=09if (ret) > +=09=09return ret; > + > +=09struct lwmi_package_response *response __free(kfree_sensitive) =3D ou= tput.data; > + > +=09data_size =3D le32_to_cpu(response->data_size); > +=09if (data_size > output.length - sizeof(*response)) Why not use struct_size() here? Considering that... > +=09=09return -EPROTO; > + > +=09state->response_len =3D struct_size(response, data, data_size); ...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there. > +=09state->response =3D no_free_ptr(response); > +=09return 0; > +} > + > +static ssize_t lwmi_package_write(struct file *file, const char __user *= buffer, > +=09=09=09=09 size_t count, loff_t *ppos) > +{ > +=09struct lwmi_package_file *state =3D file->private_data; > +=09u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] =3D {}; > +=09int ret; > + > +=09if (!count || count > sizeof(input)) > +=09=09return -EINVAL; > + > +=09ACQUIRE(mutex_try, state_guard)(&state->lock); > +=09if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response) > +=09=09return -EBUSY; I don't think it's a good idea to share the same errno between the two cases. While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY. > + > +=09if (copy_from_user(input, buffer, count)) > +=09=09return -EFAULT; > + > +=09ret =3D lwmi_package_invoke(state, input); > +=09if (ret) > +=09=09return ret; > + > +=09return count; > +} > + > +static ssize_t lwmi_package_read(struct file *file, char __user *buffer, > +=09=09=09=09 size_t count, loff_t *ppos) > +{ > +=09struct lwmi_package_file *state =3D file->private_data; > +=09size_t len; > + > +=09if (!count) > +=09=09return 0; > + > +=09ACQUIRE(mutex_try, state_guard)(&state->lock); > +=09if (ACQUIRE_ERR(mutex_try, &state_guard)) > +=09=09return -EBUSY; > + > +=09if (!state->response) > +=09=09return 0; Reading the file without writing to it in prior is a mistake, isn't it? Thus: =09=09return -ENODATA; > + > +=09len =3D state->response_len; > +=09if (len > count) > +=09=09return -ENOSPC; Returning -ENOSPC is confusing as there is nothing to allocate during read. How about -ENOBUFS? > + > +=09if (copy_to_user(buffer, state->response, len)) > +=09=09return -EFAULT; > + > +=09kfree_sensitive(state->response); > +=09state->response =3D NULL; > +=09return len; > +} > + > +static const struct file_operations lwmi_package_fops =3D { > +=09.owner =3D THIS_MODULE, > +=09.open =3D lwmi_package_open, > +=09.release =3D lwmi_package_release, > +=09.read =3D lwmi_package_read, > +=09.write =3D lwmi_package_write, > +}; > + > +static void lwmi_package_debugfs_remove(void *data) > +{ > +=09debugfs_remove_recursive(data); > +} > + > +static int devm_lwmi_package_debugfs_add(struct lwmi_om_priv *priv) > +{ > +=09struct device *dev =3D &priv->wdev->dev; > +=09struct dentry *dir, *entry; > +=09int ret; > + > +=09if (!wmidev_instance_count(priv->wdev)) > +=09=09return 0; > + > +=09dir =3D lwmi_debugfs_create_dir(priv->wdev); > +=09if (IS_ERR(dir)) > +=09=09return PTR_ERR(dir); > + > +=09atomic_set(&priv->package_opened, 0); Why would you need this? The struct is kzalloc()-allocated. > + > +=09ret =3D devm_add_action_or_reset(dev, lwmi_package_debugfs_remove, di= r); > +=09if (ret) > +=09=09return ret; > + > +=09entry =3D debugfs_create_file("get_data_by_package", 0600, dir, priv,= &lwmi_package_fops); > +=09if (IS_ERR(entry)) { > +=09=09devm_release_action(dev, lwmi_package_debugfs_remove, dir); > +=09=09return PTR_ERR(entry); > +=09} > + > +=09priv->debugfs_dir =3D dir; Why would you need this? The dir is devm-managed. > +=09return 0; > +} > + > /** > * lwmi_om_master_bind() - Bind all components of the other mode driver > * @dev: The lenovo-wmi-other driver basic device. > @@ -1693,6 +1888,7 @@ static int lwmi_other_probe(struct wmi_device *wdev= , const void *context) > { > =09struct component_match *master_match =3D NULL; > =09struct lwmi_om_priv *priv; > +=09int ret; > =20 > =09priv =3D devm_kzalloc_obj(&wdev->dev, *priv); > =09if (!priv) > @@ -1708,12 +1904,24 @@ static int lwmi_other_probe(struct wmi_device *wd= ev, const void *context) > =09if (IS_ERR(master_match)) > =09=09return PTR_ERR(master_match); > =20 > +=09if (enable_get_data_by_package) { > +=09=09ret =3D devm_lwmi_package_debugfs_add(priv); > +=09=09if (ret) > +=09=09=09dev_warn(&wdev->dev, "failed to create GetDataByPackage debugfs= files: %d\n", The file is named get_data_by_package. > +=09=09=09=09 ret); > +=09} > + > =09return component_master_add_with_match(&wdev->dev, &lwmi_om_master_op= s, > =09=09=09=09=09 master_match); > } > =20 > static void lwmi_other_remove(struct wmi_device *wdev) > { > +=09struct lwmi_om_priv *priv =3D dev_get_drvdata(&wdev->dev); > + > +=09if (priv->debugfs_dir) > +=09=09devm_release_action(&wdev->dev, lwmi_package_debugfs_remove, priv-= >debugfs_dir); Why would you need this? The interface does not depend on the components. Thanks, Rong > + > =09component_master_del(&wdev->dev, &lwmi_om_master_ops); > } > =20