Skip to contentSkip to conversation

hwmon: Ensure that 'dev' passed to hwmon_notify_event() is a hwmon device

PATCH New Guenter Roeck 2 participants 2 messages Aug 21 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: Ensure that 'dev' passed to hwmon_notify_event() is a hwmon device

The device parameter of hwmon_notify_event() must be a hardware monitoring device. Since this is easy to get wrong, and since passing a non-hwmon device may result in a crash, generate a warning traceback and abort if a wrong device class is passed as parameter.

---
 drivers/hwmon/hwmon.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/hwmon/hwmon.c +9 -1
index 3e65fc6d25eb..24ce28239316 100644
@@ -336,11 +336,16 @@ static int hwmon_attr_base(enum hwmon_sensor_types type)
  * attached to an i2c client device.
  */
 
-static int hwmon_match_device(struct device *dev, const void *data)
+static bool is_hwmon_device(struct device *dev)
 {
 	return dev->class == &hwmon_class;
 }
 
+static int hwmon_match_device(struct device *dev, const void *data)
+{
+	return is_hwmon_device(dev);
+}
+
 static ssize_t pec_show(struct device *dev, const struct device_attribute *dummy,
 			char *buf)
 {
@@ -781,6 +786,9 @@ int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
 	const char *template;
 	int base;
 
+	if (WARN(!is_hwmon_device(dev), "%s is not a hardware monitoring device\n",
+		 dev_name(dev)))
+		return -EINVAL;
 	if (type >= ARRAY_SIZE(__templates))
 		return -EINVAL;
 	if (attr >= __templates_size[type])
Signature · 1 line
2.45.2
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

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

Pre-existing issues:
- [High] The newly added WARN will be unconditionally triggered by the existing bnxt driver, causing a loud regression. --

--- Patch [1]: [PATCH] hwmon: Ensure that 'dev' passed to hwmon_notify_event() is a hwmon device --- commit d2961b1d473d8c128ab5a2aa551a3dc59e8bed39
Author: Guenter Roeck <[email protected]>

hwmon: Ensure that 'dev' passed to hwmon_notify_event() is a hwmon device

This patch enforces that the device passed to hwmon_notify_event() is actually a hardware monitoring device. It introduces a warning and aborts the event notification if an incorrect device class is provided.

Quoted 4 lines
drivers/hwmon/hwmon.c +0 -0
index 41755910a25a0..4110b5c45ee8d 100644

[ ... ]

drivers/hwmon/hwmon.c int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type, · line 792

Quoted 7 lines
drivers/hwmon/hwmon.c +3 -0
@@ -782,6 +787,9 @@ int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
 	const char *template;
 	int base;
 
+	if (WARN(!is_hwmon_device(dev), "%s is not a hardware monitoring device\n",
+		 dev_name(dev)))
+		return -EINVAL;

[Severity: High]
This isn't a bug introduced by this patch, but will this strict check cause a regression for the bnxt driver?

Looking at bnxt_hwmon_notify_event() in drivers/net/ethernet/broadcom/bnxt/bnxt_hwmon.c, it currently passes the parent PCI device instead of the hwmon device:

void bnxt_hwmon_notify_event(struct bnxt *bp)

{
	u32 attr;
	...
	hwmon_notify_event(&bp->pdev->dev, hwmon_temp, attr, 0);
}

Since the device passed here is not a hwmon device, won't this new WARN trigger unconditionally on every thermal event for bnxt hardware?

For systems configured with panic_on_warn, wouldn't this lead to a panic during normal hardware thermal events?

Quoted 3 lines
drivers/hwmon/hwmon.c +0 -0
 	if (type >= ARRAY_SIZE(__templates))
 		return -EINVAL;
 	if (attr >= __templates_size[type])
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1