summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuenter Roeck <linux@roeck-us.net>2026-08-20 22:19:13 -0700
committerGuenter Roeck <linux@roeck-us.net>2026-09-07 07:22:19 -0700
commit2bf98a6af10388215398a30d723ff1f6ff5ce4f7 (patch)
tree1a5c679aa1c054604197bec7b897e34f1ab8cc96
parent8afc94bfb0ffdfc4a168081785820aa4818d1d23 (diff)
downloadlinux-next-2bf98a6af10388215398a30d723ff1f6ff5ce4f7.tar.gz
linux-next-2bf98a6af10388215398a30d723ff1f6ff5ce4f7.zip
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. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r--drivers/hwmon/hwmon.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 3e65fc6d25eb..10d2df3efdfa 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -318,6 +318,11 @@ static int hwmon_attr_base(enum hwmon_sensor_types type)
return 1;
}
+static bool is_hwmon_device(struct device *dev)
+{
+ return dev->class == &hwmon_class;
+}
+
#if IS_REACHABLE(CONFIG_I2C)
/*
@@ -338,7 +343,7 @@ static int hwmon_attr_base(enum hwmon_sensor_types type)
static int hwmon_match_device(struct device *dev, const void *data)
{
- return dev->class == &hwmon_class;
+ return is_hwmon_device(dev);
}
static ssize_t pec_show(struct device *dev, const struct device_attribute *dummy,
@@ -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])