Skip to contentSkip to conversation

hwmon: (asus-ec-sensors) fix EC read intervals

PATCH Applied Eugene Shalygin 3 participants 4 messages Jul 12 – Jul 19

Subsystems (2)
lore
Share & tools
lore

Eugene Shalygin

author maintainer
#0 lore raw

v2 cover hwmon: (asus-ec-sensors) fix EC read intervals

v2: revret updating state->next_update before the update_ec_sensors() call.

Eugene Shalygin (1):
hwmon: (asus-ec-sensors) fix EC read intervals

 drivers/hwmon/asus-ec-sensors.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH v2 1/1 hwmon: (asus-ec-sensors) fix EC read intervals

Take INITIAL_JIFFIES into account when setting up next update time.

Fixes d0ddfd241e57 ("hwmon: (asus-ec-sensors) add driver for ASUS EC")
---
 drivers/hwmon/asus-ec-sensors.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/hwmon/asus-ec-sensors.c +4 -4
index 29a23484cbe7..eb71e41fd4f4 100644
@@ -1009,7 +1009,7 @@ struct ec_sensors_data {
 	/* sorted list of unique register banks */
 	u8 banks[ASUS_EC_MAX_BANK + 1];
 	/* in jiffies */
-	unsigned long last_updated;
+	u64 next_update;
 	struct lock_data lock_data;
 	/* number of board EC sensors */
 	u8 nr_sensors;
@@ -1278,13 +1278,12 @@ static int get_cached_value_or_update(const struct device *dev,
 				      int sensor_index,
 				      struct ec_sensors_data *state, s32 *value)
 {
-	if (time_after(jiffies, state->last_updated + HZ)) {
+	if (time_after64(get_jiffies_64(), state->next_update)) {
 		if (update_ec_sensors(dev, state)) {
 			dev_err(dev, "update_ec_sensors() failure\n");
 			return -EIO;
 		}
-
-		state->last_updated = jiffies;
+		state->next_update = get_jiffies_64() + HZ;
 	}
 
 	*value = state->sensors[sensor_index].cached_value;
@@ -1402,6 +1401,7 @@ static int asus_ec_probe(struct platform_device *pdev)
 	if (!ec_data)
 		return -ENOMEM;
 
+	ec_data->next_update = INITIAL_JIFFIES;
 	dev_set_drvdata(dev, ec_data);
 	ec_data->board_info = pboard_info;
 
Signature · 1 line
2.55.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The driver ignores the memory allocation failure from asus_ec_hwmon_add_chan_info(), causing a NULL pointer dereference in the hwmon core.
- [High] Missing rate-limiting timestamp update on EC read failure allows log flooding and ACPI EC spam by unprivileged userspace. --

--- Patch [1]: [PATCH v2 1/1] hwmon: (asus-ec-sensors) fix EC read intervals --- commit 22e0613e28eceb6562850cc438cb81f20f1fefaa
Author: Eugene Shalygin <[email protected]>

hwmon: (asus-ec-sensors) fix EC read intervals

This commit updates the EC read interval logic to take INITIAL_JIFFIES into account when setting up the next update time, using a 64-bit jiffies value for next_update.

Quoted 4 lines
drivers/hwmon/asus-ec-sensors.c +0 -0
index 29a23484cbe7b..eb71e41fd4f4b 100644

[ ... ]

drivers/hwmon/asus-ec-sensors.c static int get_cached_value_or_update(const struct device *dev, · line 1285

Quoted 10 lines
drivers/hwmon/asus-ec-sensors.c +1 -1
@@ -1278,13 +1278,12 @@ static int get_cached_value_or_update(const struct device *dev,
 				      int sensor_index,
 				      struct ec_sensors_data *state, s32 *value)
 {
-	if (time_after(jiffies, state->last_updated + HZ)) {
+	if (time_after64(get_jiffies_64(), state->next_update)) {
 		if (update_ec_sensors(dev, state)) {
 			dev_err(dev, "update_ec_sensors() failure\n");
 			return -EIO;
 		}

[Severity: High]
This is a pre-existing issue, but does returning early on error here cause unbounded log spam?

If update_ec_sensors() fails (e.g., due to a hardware timeout), it returns -EIO without updating state->next_update. Since the timestamp remains in the past, subsequent reads from userspace will immediately retry the failing operation, potentially spamming the log and the ACPI EC.

Should state->next_update be advanced even on failure to maintain rate limiting?

Quoted 6 lines
drivers/hwmon/asus-ec-sensors.c +1 -2
-
-		state->last_updated = jiffies;
+		state->next_update = get_jiffies_64() + HZ;
 	}
 
 	*value = state->sensors[sensor_index].cached_value;

[ ... ]

Quoted 10 lines
drivers/hwmon/asus-ec-sensors.c +0 -0
@@ -1488,8 +1488,8 @@ static int asus_ec_probe(struct platform_device *pdev)
 	for (type = 0; type < hwmon_max; ++type) {
 		if (!nr_count[type])
 			continue;
 
 		asus_ec_hwmon_add_chan_info(asus_ec_hwmon_chan, dev,
 					     nr_count[type], type,
 					     hwmon_attributes[type]);
 		*ptr_asus_ec_ci++ = asus_ec_hwmon_chan++;

}

[Severity: High]
This isn't a bug introduced by this patch, but does this loop ignore memory allocation failures?

If devm_kcalloc() fails inside asus_ec_hwmon_add_chan_info(), it returns -ENOMEM and leaves asus_ec_hwmon_chan->config as NULL.

Since the return value is ignored here, the uninitialized struct is passed to devm_hwmon_device_register_with_info(). The hwmon core unconditionally dereferences the config pointer in hwmon_num_channel_attrs(), which could cause a kernel oops on probe during memory pressure.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#3 lore raw
In reply to Eugene Shalygin
Quoted 5 lines · Eugene Shalygin

Take INITIAL_JIFFIES into account when setting up next update time.

Fixes d0ddfd241e57 ("hwmon: (asus-ec-sensors) add driver for ASUS EC")

Applied.

Thanks,
Guenter

Quoted 41 lines
---
 drivers/hwmon/asus-ec-sensors.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/hwmon/asus-ec-sensors.c +4 -4
index 29a23484cbe7..eb71e41fd4f4 100644
@@ -1009,7 +1009,7 @@ struct ec_sensors_data {
 	/* sorted list of unique register banks */
 	u8 banks[ASUS_EC_MAX_BANK + 1];
 	/* in jiffies */
-	unsigned long last_updated;
+	u64 next_update;
 	struct lock_data lock_data;
 	/* number of board EC sensors */
 	u8 nr_sensors;
@@ -1278,13 +1278,12 @@ static int get_cached_value_or_update(const struct device *dev,
 				      int sensor_index,
 				      struct ec_sensors_data *state, s32 *value)
 {
-	if (time_after(jiffies, state->last_updated + HZ)) {
+	if (time_after64(get_jiffies_64(), state->next_update)) {
 		if (update_ec_sensors(dev, state)) {
 			dev_err(dev, "update_ec_sensors() failure\n");
 			return -EIO;
 		}
-
-		state->last_updated = jiffies;
+		state->next_update = get_jiffies_64() + HZ;
 	}
 
 	*value = state->sensors[sensor_index].cached_value;
@@ -1402,6 +1401,7 @@ static int asus_ec_probe(struct platform_device *pdev)
 	if (!ec_data)
 		return -ENOMEM;
 
+	ec_data->next_update = INITIAL_JIFFIES;
 	dev_set_drvdata(dev, ec_data);
 	ec_data->board_info = pboard_info;