Skip to contentSkip to conversation

platform/x86: hp-wmi: Modernize driver API and perform_query()

PATCH Superseded yahia 2 participants 10 messages Jul 21 – Aug 2 x86 platform drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

yahia

author
#0 lore raw

cover platform/x86: hp-wmi: Modernize driver API and perform_query()

From: yahia ahmed <[email protected]>

Refactor various parts of hp-wmi, including hp_wmi_perform_query() and encode_outsize_for_pvsz(), to adopt modern kernel APIs, resource cleanup attributes, and buffer bounds safety checks.

Patch 1 migrates the hp-wmi driver from the deprecated wmi_evaluate_method() API to the modern wmidev_evaluate_method() function by registering as a WMI driver.

Patch 2 refactors encode_outsize_for_pvsz() with a switch statement, adds early ACPI return buffer length validation to prevent potential integer underflow in hp_wmi_perform_query(), replaces kmalloc with kzalloc_flex to zero memory, and use __free() macro to free variables.

yahia (2):
hp-wmi: Modernize hp_wmi_perform_query() to use the modern API
hp-wmi: use modern cleanup and refactor allocation functions

 drivers/platform/x86/hp/hp-wmi.c | 98 +++++++++++++++++++++-----------
 1 file changed, 65 insertions(+), 33 deletions(-)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/2 hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

From: yahia ahmed <[email protected]>

hp_wmi_perform_query() currently uses the deprecated GUID-based WMI interface wmi_evaluate_method().

Migrate hp-wmi to the modern bus-based WMI API by registering a struct wmi_driver and replacing wmi_evaluate_method() calls with wmidev_evaluate_method().

---
 drivers/platform/x86/hp/hp-wmi.c | 49 ++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 5 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +44 -5
index 8ba286ed8721..5845b169df39 100644
@@ -14,6 +14,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/acpi.h>
+#include <linux/wmi.h>
 #include <linux/cleanup.h>
 #include <linux/compiler_attributes.h>
 #include <linux/dmi.h>
@@ -506,6 +507,14 @@ struct hp_wmi_hwmon_priv {
 	struct delayed_work keep_alive_dwork;
 };
 
+struct wmi_device *hp_wmi_wdev;
+
+static const struct wmi_device_id hp_wmi_guid_table[] = {
+	{ .guid_string = HPWMI_BIOS_GUID },
+	{}
+};
+MODULE_DEVICE_TABLE(wmi, hp_wmi_guid_table);
+
 struct victus_s_fan_table_header {
 	u8 num_fans;
 	u8 unknown;
@@ -606,7 +615,7 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
 	args->datasize = insize;
 	memcpy(args->data, buffer, flex_array_size(args, data, insize));
 
-	ret = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
+	ret = wmidev_evaluate_method(hp_wmi_wdev, 0, mid, &input, &output);
 	if (ret)
 		goto out_free;
 
@@ -2717,14 +2726,40 @@ static void __init setup_active_thermal_profile_params(void)
 	}
 }
 
+static int hp_wmi_probe(struct wmi_device *wdev, const void *context)
+{
+	hp_wmi_wdev = wdev;
+	return 0;
+}
+
+static void hp_wmi_remove(struct wmi_device *wdev)
+{
+	hp_wmi_wdev = NULL;
+}
+
+static struct wmi_driver hp_wmi_wdev_driver = {
+	.driver = {
+		.name = "hp-wmi-wdev",
+	},
+	.id_table = hp_wmi_guid_table,
+	.probe = hp_wmi_probe,
+	.remove = hp_wmi_remove,
+};
+
+
 static int __init hp_wmi_init(void)
 {
 	int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
 	int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
 	int err, tmp = 0;
+	err = wmi_driver_register(&hp_wmi_wdev_driver);
+	if (err)
+		return err;
 
-	if (!bios_capable && !event_capable)
-		return -ENODEV;
+	if (!bios_capable && !event_capable) {
+		err = -ENODEV;
+		goto err_unregister_wmi;
+	}
 
 	if (hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &tmp,
 				 sizeof(tmp), sizeof(tmp)) == HPWMI_RET_INVALID_PARAMETERS)
@@ -2733,7 +2768,7 @@ static int __init hp_wmi_init(void)
 	if (event_capable) {
 		err = hp_wmi_input_setup();
 		if (err)
-			return err;
+			goto err_unregister_wmi;
 	}
 
 	if (bios_capable) {
@@ -2771,7 +2806,8 @@ static int __init hp_wmi_init(void)
 err_destroy_input:
 	if (event_capable)
 		hp_wmi_input_destroy();
-
+err_unregister_wmi:
+	wmi_driver_unregister(&hp_wmi_wmi_driver);
 	return err;
 }
 module_init(hp_wmi_init);
@@ -2794,5 +2830,8 @@ static void __exit hp_wmi_exit(void)
 		platform_device_unregister(hp_wmi_platform_dev);
 		platform_driver_unregister(&hp_wmi_driver);
 	}
+
+	if (hp_wmi_wdev)
+		wmi_driver_unregister(&hp_wmi_wdev_driver);
 }
 module_exit(hp_wmi_exit);
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

Am 22.07.26 um 00:20 schrieb yahia:

Quoted 8 lines

From: yahia ahmed <[email protected]>

hp_wmi_perform_query() currently uses the deprecated GUID-based WMI interface wmi_evaluate_method().

Migrate hp-wmi to the modern bus-based WMI API by registering a struct wmi_driver and replacing wmi_evaluate_method() calls with wmidev_evaluate_method().

Hi,

using the WMI device as a global variable is not a good idea, especially without locking. I suggest that you instead perform much of the setup done in hp_wmi_init() inside the WMI driver probe() callback and replace the usage of global variables with a state container:

https://docs.kernel.org/driver-api/driver-model/design-patterns.html

Doing so requires that you first move the WMI event handling into a proper WMI event driver, see https://docs.kernel.org/wmi/driver-development-guide.html (sections "WMI event drivers" and "Handling multiple WMI devices at once") for details. You can use the uniwill-wmi driver as an inspiration regarding WMI event handling coupled with notifier blocks.

Thanks,
Armin Wolf

Quoted 113 lines
---
  drivers/platform/x86/hp/hp-wmi.c | 49 ++++++++++++++++++++++++++++----
  1 file changed, 44 insertions(+), 5 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +44 -5
index 8ba286ed8721..5845b169df39 100644
@@ -14,6 +14,7 @@
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
  #include <linux/acpi.h>
+#include <linux/wmi.h>
  #include <linux/cleanup.h>
  #include <linux/compiler_attributes.h>
  #include <linux/dmi.h>
@@ -506,6 +507,14 @@ struct hp_wmi_hwmon_priv {
  	struct delayed_work keep_alive_dwork;
  };
 
+struct wmi_device *hp_wmi_wdev;
+
+static const struct wmi_device_id hp_wmi_guid_table[] = {
+	{ .guid_string = HPWMI_BIOS_GUID },
+	{}
+};
+MODULE_DEVICE_TABLE(wmi, hp_wmi_guid_table);
+
  struct victus_s_fan_table_header {
  	u8 num_fans;
  	u8 unknown;
@@ -606,7 +615,7 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
  	args->datasize = insize;
  	memcpy(args->data, buffer, flex_array_size(args, data, insize));
 
-	ret = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
+	ret = wmidev_evaluate_method(hp_wmi_wdev, 0, mid, &input, &output);
  	if (ret)
  		goto out_free;
 
@@ -2717,14 +2726,40 @@ static void __init setup_active_thermal_profile_params(void)
  	}
  }
 
+static int hp_wmi_probe(struct wmi_device *wdev, const void *context)
+{
+	hp_wmi_wdev = wdev;
+	return 0;
+}
+
+static void hp_wmi_remove(struct wmi_device *wdev)
+{
+	hp_wmi_wdev = NULL;
+}
+
+static struct wmi_driver hp_wmi_wdev_driver = {
+	.driver = {
+		.name = "hp-wmi-wdev",
+	},
+	.id_table = hp_wmi_guid_table,
+	.probe = hp_wmi_probe,
+	.remove = hp_wmi_remove,
+};
+
+
  static int __init hp_wmi_init(void)
  {
  	int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
  	int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
  	int err, tmp = 0;
+	err = wmi_driver_register(&hp_wmi_wdev_driver);
+	if (err)
+		return err;
 
-	if (!bios_capable && !event_capable)
-		return -ENODEV;
+	if (!bios_capable && !event_capable) {
+		err = -ENODEV;
+		goto err_unregister_wmi;
+	}
 
  	if (hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &tmp,
  				 sizeof(tmp), sizeof(tmp)) == HPWMI_RET_INVALID_PARAMETERS)
@@ -2733,7 +2768,7 @@ static int __init hp_wmi_init(void)
  	if (event_capable) {
  		err = hp_wmi_input_setup();
  		if (err)
-			return err;
+			goto err_unregister_wmi;
  	}
 
  	if (bios_capable) {
@@ -2771,7 +2806,8 @@ static int __init hp_wmi_init(void)
  err_destroy_input:
  	if (event_capable)
  		hp_wmi_input_destroy();
-
+err_unregister_wmi:
+	wmi_driver_unregister(&hp_wmi_wmi_driver);
  	return err;
  }
  module_init(hp_wmi_init);
@@ -2794,5 +2830,8 @@ static void __exit hp_wmi_exit(void)
  		platform_device_unregister(hp_wmi_platform_dev);
  		platform_driver_unregister(&hp_wmi_driver);
  	}
+
+	if (hp_wmi_wdev)
+		wmi_driver_unregister(&hp_wmi_wdev_driver);
  }
  module_exit(hp_wmi_exit);
#3 lore raw

Re: [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

Hi,

Thanks fot your review, I agree that that we should refactor to a state container based setup and move initial setup to a probe function.

Best regards,
yahia

#4 lore raw

Re: [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

In reply to Armin Wolf

Hi,

Currently hp_bioscfg and hp_wmi will both collide to register in the 5FB7F034-2C63-45E9-BE91-3D44E2C707E4 GUID, so if we modernize to use the wmi interface we will lock one of the drivers out, So how should we optimally handle them?

Best regards,
yahia

#5 lore raw

Re: [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

Am 01.08.26 um 16:20 schrieb yahia ahmed:

Quoted 9 lines

Hi,

Currently hp_bioscfg and hp_wmi will both collide to register in the 5FB7F034-2C63-45E9-BE91-3D44E2C707E4 GUID, so if we modernize to use the wmi interface we will lock one of the drivers out, So how should we optimally handle them?

Best regards,
yahia

Oh no :/

I suggest that you use the component framework to register a component inside the hp-wmi driver when probing a WMI device. The hp-bioscfg driver can then register a component master to bind to this component. During binding the WMI device handled by the hp-wmi driver can be shared with the hp-bioscfg driver, so both can use it.

This however might turn out to be a bit complicated, do you have a device where you can test both drivers? If no, then i suggest that you focus on removing all usage of wmi_install_notify_handler() from the hp-wmi driver instead. This does not require that you convert the whole driver to the new bus-based API.

Thanks,
Armin Wolf

#6 lore raw

Re: [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

Hi,

I think that the component framework will be better though I dont have all the hardware to test stuff like legacy RFKill, I agree for now we should remove usage of wmi_install_notify_handler() though I will await ilpo's response maybe he has another idea.

Best regards,
yahia

#7 lore raw

Re: [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

Am 01.08.26 um 18:09 schrieb yahia ahmed:

Quoted 9 lines

Hi,

I think that the component framework will be better though I dont have all the hardware to test stuff like legacy RFKill, I agree for now we should remove usage of wmi_install_notify_handler() though I will await ilpo's response maybe he has another idea.

Best regards,
yahia

Alight, keep in mind that the hp-wmi and hp-wmi-sensors driver seem to use the same WMI event GUID (95F24279-4D7B-4334-9387-ACCDC67EF61C) for handling events, so you have to move both of them away from wmi_install_notify_handler().

For this i suggest that you introduce a separate WMI driver for the event GUID, where both drivers can register notifiers to receive the ACPI object payload of the WMI event (see drivers/platform/x86/uniwill/uniwill-wmi.c for inspiration). In this case i would ignore the fact that the ACPI object-based WMI event callback is deprecated, so you do not have to rework the whole event handling in both drivers.

Thanks,
Armin Wolf

#8 lore raw

Re [PATCH 1/2] hp-wmi: Modernize hp_wmi_perform_query() to use the modern API

Thanks for bringing up the shared GUID with hp-wmi-sensors, I agree we should setup up a small driver that registers with the GUID and both register as notifiers but unfournately i don't have the hardware to test hp_wmi_sensors, So I think for now to just drop the patch till someone who has the hardware does it.

Best regards,
yahia

author
#9 lore raw

PATCH 2/2 hp-wmi: use modern cleanup and refactor allocation functions

In reply to yahia

From: yahia ahmed <[email protected]>

Refactor hp_wmi_perform_query() and encode_outsize_for_pvsz() to improve readability, ensure memory zeroing, and verify bounds.

Replace the brittle if condition tree in encode_outsize_for_pvsz() with a switch statement, replace the manual out_free label in hp_wmi_perform_query() to free args and obj variables with compiler provided cleanup __free(), replace kmalloc with kzalloc_flex to ensure that memory is zeroed before use, and add a bounds check for obj->buffer.length to attest that it is larger than sizeof(struct bios_return).

---
 drivers/platform/x86/hp/hp-wmi.c | 49 ++++++++++++++------------------
 1 file changed, 21 insertions(+), 28 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +21 -28
index 5845b169df39..67bd3d6825f4 100644
@@ -552,16 +552,20 @@ static inline u8 pwm_to_rpm(u8 pwm, struct hp_wmi_hwmon_priv *priv)
 /* map output size to the corresponding WMI method id */
 static inline int encode_outsize_for_pvsz(int outsize)
 {
-	if (outsize > 4096)
+	if (unlikely(outsize > 4096 || outsize < 0))
 		return -EINVAL;
-	if (outsize > 1024)
-		return 5;
-	if (outsize > 128)
-		return 4;
-	if (outsize > 4)
-		return 3;
-	if (outsize > 0)
+	switch (outsize) {
+	case 1 ... 4:
 		return 2;
+	case 5 ... 128:
+		return 3;
+	case 129 ... 1024:
+		return 4;
+	case 1025 ... 4096:
+		return 5;
+	default:
+		break;
+	}
 	return 1;
 }
 
@@ -590,8 +594,8 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
 {
 	struct acpi_buffer input, output = { ACPI_ALLOCATE_BUFFER, NULL };
 	struct bios_return *bios_return;
-	union acpi_object *obj = NULL;
-	struct bios_args *args = NULL;
+	union acpi_object *obj __free(kfree) = NULL;
+	struct bios_args *args __free(kfree) = NULL;
 	int mid, actual_insize, actual_outsize;
 	size_t bios_args_size;
 	int ret;
@@ -602,7 +606,7 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
 
 	actual_insize = max(insize, 128);
 	bios_args_size = struct_size(args, data, actual_insize);
-	args = kmalloc(bios_args_size, GFP_KERNEL);
+	args = kzalloc_flex(*args, data, actual_insize, GFP_KERNEL);
 	if (!args)
 		return -ENOMEM;
 
@@ -617,19 +621,12 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
 
 	ret = wmidev_evaluate_method(hp_wmi_wdev, 0, mid, &input, &output);
 	if (ret)
-		goto out_free;
+		return ret;
 
 	obj = output.pointer;
-	if (!obj) {
-		ret = -EINVAL;
-		goto out_free;
-	}
-
-	if (obj->type != ACPI_TYPE_BUFFER) {
-		pr_warn("query 0x%x returned an invalid object 0x%x\n", query, ret);
-		ret = -EINVAL;
-		goto out_free;
-	}
+	if (!obj || obj->type != ACPI_TYPE_BUFFER ||
+	    obj->buffer.length < sizeof(struct bios_return))
+		 return -EINVAL;
 
 	bios_return = (struct bios_return *)obj->buffer.pointer;
 	ret = bios_return->return_code;
@@ -638,20 +635,16 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
 		if (ret != HPWMI_RET_UNKNOWN_COMMAND &&
 		    ret != HPWMI_RET_UNKNOWN_CMDTYPE)
 			pr_warn("query 0x%x returned error 0x%x\n", query, ret);
-		goto out_free;
+		return ret;
 	}
 
 	/* Ignore output data of zero size */
 	if (!outsize)
-		goto out_free;
+		return 0;
 
 	actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
 	memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
 	memset(buffer + actual_outsize, 0, outsize - actual_outsize);
-
-out_free:
-	kfree(obj);
-	kfree(args);
 	return ret;
 }
 
Signature · 1 line
2.55.0