Skip to contentSkip to conversation

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

PATCH New yahia 1 participant 3 messages Jul 21 x86 platform drivers

lore
Share & tools
lore

yahia

author
#0 lore raw

v2 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 use newer APIs, resource cleanup attributes, and 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 | 100 +++++++++++++++++++++----------
 1 file changed, 67 insertions(+), 33 deletions(-)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 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 wmi_evaluate_method(), Which is depracted and replaced with the modern wmidev_evaluate_method().

---
v1->v2:
-Fix a potential null derefernce highlighted by sashiko -Fix a typo in the unregisteration process

---
 drivers/platform/x86/hp/hp-wmi.c | 51 ++++++++++++++++++++++++++++----
 1 file changed, 46 insertions(+), 5 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +46 -5
index 8ba286ed8721..5ac29361dc17 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;
@@ -587,6 +596,8 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
 	size_t bios_args_size;
 	int ret;
 
+	if (!hp_wmi_wdev)
+		return -ENODEV;
 	mid = encode_outsize_for_pvsz(outsize);
 	if (WARN_ON(mid < 0))
 		return mid;
@@ -606,7 +617,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 +2728,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 +2770,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 +2808,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_wdev_driver);
 	return err;
 }
 module_init(hp_wmi_init);
@@ -2794,5 +2832,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
author
#2 lore raw

PATCH v2 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 5ac29361dc17..bf74667838e7 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;
@@ -604,7 +608,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;
 
@@ -619,19 +623,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;
@@ -640,20 +637,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