diff mbox series

hwmon: (acpi_power_meter) Fix 4.29 MW bug

Message ID 20231124182747.13956-1-W_Armin@gmx.de (mailing list archive)
State Accepted
Headers show
Series hwmon: (acpi_power_meter) Fix 4.29 MW bug | expand

Commit Message

Armin Wolf Nov. 24, 2023, 6:27 p.m. UTC
The ACPI specification says:

"If an error occurs while obtaining the meter reading or if the value
is not available then an Integer with all bits set is returned"

Since the "integer" is 32 bits in case of the ACPI power meter,
userspace will get a power reading of 2^32 * 1000 miliwatts (~4.29 MW)
in case of such an error. This was discovered due to a lm_sensors
bugreport (https://github.com/lm-sensors/lm-sensors/issues/460).
Fix this by returning -ENODATA instead.

Tested-by: <urbinek@gmail.com>
Fixes: de584afa5e18 ("hwmon driver for ACPI 4.0 power meters")
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
 drivers/hwmon/acpi_power_meter.c | 4 ++++
 1 file changed, 4 insertions(+)

--
2.39.2

Comments

Guenter Roeck Dec. 1, 2023, 4:12 a.m. UTC | #1
On Fri, Nov 24, 2023 at 07:27:47PM +0100, Armin Wolf wrote:
> The ACPI specification says:
> 
> "If an error occurs while obtaining the meter reading or if the value
> is not available then an Integer with all bits set is returned"
> 
> Since the "integer" is 32 bits in case of the ACPI power meter,
> userspace will get a power reading of 2^32 * 1000 miliwatts (~4.29 MW)
> in case of such an error. This was discovered due to a lm_sensors
> bugreport (https://github.com/lm-sensors/lm-sensors/issues/460).
> Fix this by returning -ENODATA instead.
> 
> Tested-by: <urbinek@gmail.com>
> Fixes: de584afa5e18 ("hwmon driver for ACPI 4.0 power meters")
> Signed-off-by: Armin Wolf <W_Armin@gmx.de>

Applied.

Thanks,
Guenter

> ---
>  drivers/hwmon/acpi_power_meter.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> --
> 2.39.2
> 
> diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c
> index 8db740214ffd..703666b95bf4 100644
> --- a/drivers/hwmon/acpi_power_meter.c
> +++ b/drivers/hwmon/acpi_power_meter.c
> @@ -31,6 +31,7 @@
>  #define POWER_METER_CAN_NOTIFY	(1 << 3)
>  #define POWER_METER_IS_BATTERY	(1 << 8)
>  #define UNKNOWN_HYSTERESIS	0xFFFFFFFF
> +#define UNKNOWN_POWER		0xFFFFFFFF
> 
>  #define METER_NOTIFY_CONFIG	0x80
>  #define METER_NOTIFY_TRIP	0x81
> @@ -348,6 +349,9 @@ static ssize_t show_power(struct device *dev,
>  	update_meter(resource);
>  	mutex_unlock(&resource->lock);
> 
> +	if (resource->power == UNKNOWN_POWER)
> +		return -ENODATA;
> +
>  	return sprintf(buf, "%llu\n", resource->power * 1000);
>  }
diff mbox series

Patch

diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c
index 8db740214ffd..703666b95bf4 100644
--- a/drivers/hwmon/acpi_power_meter.c
+++ b/drivers/hwmon/acpi_power_meter.c
@@ -31,6 +31,7 @@ 
 #define POWER_METER_CAN_NOTIFY	(1 << 3)
 #define POWER_METER_IS_BATTERY	(1 << 8)
 #define UNKNOWN_HYSTERESIS	0xFFFFFFFF
+#define UNKNOWN_POWER		0xFFFFFFFF

 #define METER_NOTIFY_CONFIG	0x80
 #define METER_NOTIFY_TRIP	0x81
@@ -348,6 +349,9 @@  static ssize_t show_power(struct device *dev,
 	update_meter(resource);
 	mutex_unlock(&resource->lock);

+	if (resource->power == UNKNOWN_POWER)
+		return -ENODATA;
+
 	return sprintf(buf, "%llu\n", resource->power * 1000);
 }