Skip to contentSkip to conversation

thermal: hwmon: Rework of automatic hwmon device registration

PATCH In review Rafael J. Wysocki 4 participants 10 messages May 5 – Jul 29

Subsystems (2)
lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

v2 cover thermal: hwmon: Rework of automatic hwmon device registration

Hi All,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

sent mosty to address feedback from sashiko.dev:

https://sashiko.dev/#/patchset/5100596.31r3eYUQgx%40rafael.j.wysocki

There is an extra patch in this revision (the first one) that addresses a possible thermal hwmon removal issue pointed out by sashiko.dev.

The second patch in the series reworks the automatic registration of hwmon devices for thermal zones so that one hwmon device is registered for each of them. This is done to address a thermal zone removal deadlock related to the sharing of a hwmon device with other thermal zones of the same type (see the changelog of patch [2/3] for details).

The last patch simplifies the thermal hwmon code further by using the canonical mechanism for registering extra sysfs attributes of hwmon devices instead of manually adding files to sysfs.

The patches are targeted at 7.2.

Thanks!

author maintainer
#1 lore raw

PATCH v2 1/3 thermal: hwmon: Fix critical temperature attribute removal

From: Rafael J. Wysocki <[email protected]>

Since the return value of thermal_zone_crit_temp_valid() depends on the behavior of the thermal zone .get_crit_temp() callback which may change over time in theory, thermal_remove_hwmon_sysfs() may attempt to remove a critical temperature attribute that has not been created, passing a pointer to an uninitialized attribute structure to device_remove_file().

To avoid that, set a flag in struct thermal_hwmon_temp after creating a critical temperature attribute and use the value of that flag to decide whether or not the attribute needs to be removed.

Fixes e8db5d6736a7 ("thermal: hwmon: Make the check for critical temp valid consistent")

---

v1 -> v2: New patch (due to sashiko.dev feedback)

---
 drivers/thermal/thermal_hwmon.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/thermal/thermal_hwmon.c +4 -1
@@ -40,6 +40,7 @@ struct thermal_hwmon_temp {
 	struct thermal_zone_device *tz;
 	struct thermal_hwmon_attr temp_input;	/* hwmon sys attr */
 	struct thermal_hwmon_attr temp_crit;	/* hwmon sys attr */
+	bool temp_crit_present;
 };
 
 static LIST_HEAD(thermal_hwmon_list);
@@ -191,6 +192,8 @@ int thermal_add_hwmon_sysfs(struct therm
 					    &temp->temp_crit.attr);
 		if (result)
 			goto unregister_input;
+
+		temp->temp_crit_present = true;
 	}
 
 	mutex_lock(&thermal_hwmon_list_lock);
@@ -235,7 +238,7 @@ void thermal_remove_hwmon_sysfs(struct t
 	}
 
 	device_remove_file(hwmon->device, &temp->temp_input.attr);
-	if (thermal_zone_crit_temp_valid(tz))
+	if (temp->temp_crit_present)
 		device_remove_file(hwmon->device, &temp->temp_crit.attr);
 
 	mutex_lock(&thermal_hwmon_list_lock);
author maintainer
#2 lore raw

PATCH v2 2/3 thermal: hwmon: Register a hwmon device for each thermal zone

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The current code creates one hwmon device per thermal zone type and that device is registered under the first thermal zone of the given type.

That turns out to be problematic when the thermal zone holding the hwmon device is removed.

For example, say that there are two ACPI thermal zones on a system

/sys/devices/virtual/thermal/thermal_zone0/
/sys/devices/virtual/thermal/thermal_zone1/

The current code registers a hwmon class device for thermal_zone0 only:

/sys/devices/virtual/thermal/thermal_zone0/hwmon0/

because the type is "acpitz" for both of them, but it adds a sysfs attribute that belongs to thermal_zone1 under it:

/sys/devices/virtual/thermal/thermal_zone0/hwmon0/temp2_input

There is also

/sys/devices/virtual/thermal/thermal_zone0/hwmon0/temp1_input

which belongs to thermal_zone0.

When thermal_zone0 is removed, say because the ACPI thermal driver is unbound from the underlying platform device, the removal code skips the removal of hwmon0 because of the temp2_input attribute belonging to thermal_zone1 which effectively prevents thermal_zone0 removal from making progress.

To address this problem, rework the thermal hwmon code to register one hwmon device for each thermal zone, but since user space utilities produce confusing output in some cases when there are multiple hwmon devices with the same name attribute value present under thermal zones of the same type, append the thermal zone ID preceded by an underline character to the name of the hwmon device registered for that thermal zone.

Fixes f6b6b52ef7a5 ("thermal_hwmon: Pass the originating device down to hwmon_device_register_with_info")

---

v1 -> v2:
* Rebase on top of [1/3]
* Subject update
* Introduce THERMAL_HWMON_NAME_LENGTH (for hwmon device name attribute values) and avoid the truncation of thermal zone type when creating those attributes (sashiko.dev)
* Drop hwmon_node (not necessary any more) from struct thermal_hwmon_temp (sashiko.dev)
* Avoid mixing scoped_guard() with gotos in thermal_add_hwmon_sysfs() (sashiko.dev)

---

The reason why I have decided to take this approach is because it allows the code to be simplified subsequently by using regular extra_groups for hwmon instead of adding device attributes directly (see the next patch) and going back to the state before commit f6b6b52ef7a5 is seriously unattractive. However, if it breaks user space for people, there really won't be much choice.

---
 drivers/thermal/thermal_hwmon.c |  151 ++++++++++++----------------------------
 1 file changed, 47 insertions(+), 104 deletions(-)
drivers/thermal/thermal_hwmon.c +47 -104
@@ -19,30 +19,33 @@
 #include "thermal_hwmon.h"
 #include "thermal_core.h"
 
-/* hwmon sys I/F */
-/* thermal zone devices with the same type share one hwmon device */
-struct thermal_hwmon_device {
-	char type[THERMAL_NAME_LENGTH];
-	struct device *device;
-	int count;
-	struct list_head tz_list;
-	struct list_head node;
-};
+/*
+ * Needs to be large enough to hold a thermal zone type string followed by an
+ * underline character and a 32-bit integer in decimal representation.
+ */
+#define THERMAL_HWMON_NAME_LENGTH (THERMAL_NAME_LENGTH + 11)
 
 struct thermal_hwmon_attr {
 	struct device_attribute attr;
-	char name[16];
 };
 
 /* one temperature input for each thermal zone */
 struct thermal_hwmon_temp {
-	struct list_head hwmon_node;
 	struct thermal_zone_device *tz;
 	struct thermal_hwmon_attr temp_input;	/* hwmon sys attr */
 	struct thermal_hwmon_attr temp_crit;	/* hwmon sys attr */
 	bool temp_crit_present;
 };
 
+/* hwmon sys I/F */
+/* thermal zone devices with the same type share one hwmon device */
+struct thermal_hwmon_device {
+	char name[THERMAL_HWMON_NAME_LENGTH];
+	struct device *device;
+	struct list_head node;
+	struct thermal_hwmon_temp tz_temp;
+};
+
 static LIST_HEAD(thermal_hwmon_list);
 
 static DEFINE_MUTEX(thermal_hwmon_list_lock);
@@ -88,45 +91,6 @@ temp_crit_show(struct device *dev, struc
 	return sysfs_emit(buf, "%d\n", temperature);
 }
 
-
-static struct thermal_hwmon_device *
-thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
-{
-	struct thermal_hwmon_device *hwmon;
-	char type[THERMAL_NAME_LENGTH];
-
-	mutex_lock(&thermal_hwmon_list_lock);
-	list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
-		strscpy(type, tz->type);
-		strreplace(type, '-', '_');
-		if (!strcmp(hwmon->type, type)) {
-			mutex_unlock(&thermal_hwmon_list_lock);
-			return hwmon;
-		}
-	}
-	mutex_unlock(&thermal_hwmon_list_lock);
-
-	return NULL;
-}
-
-/* Find the temperature input matching a given thermal zone */
-static struct thermal_hwmon_temp *
-thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
-			  const struct thermal_zone_device *tz)
-{
-	struct thermal_hwmon_temp *temp;
-
-	mutex_lock(&thermal_hwmon_list_lock);
-	list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
-		if (temp->tz == tz) {
-			mutex_unlock(&thermal_hwmon_list_lock);
-			return temp;
-		}
-	mutex_unlock(&thermal_hwmon_list_lock);
-
-	return NULL;
-}
-
 static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
 {
 	int temp;
@@ -137,54 +101,39 @@ int thermal_add_hwmon_sysfs(struct therm
 {
 	struct thermal_hwmon_device *hwmon;
 	struct thermal_hwmon_temp *temp;
-	int new_hwmon_device = 1;
 	int result;
 
-	hwmon = thermal_hwmon_lookup_by_type(tz);
-	if (hwmon) {
-		new_hwmon_device = 0;
-		goto register_sys_interface;
-	}
-
 	hwmon = kzalloc_obj(*hwmon);
 	if (!hwmon)
 		return -ENOMEM;
 
-	INIT_LIST_HEAD(&hwmon->tz_list);
-	strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
-	strreplace(hwmon->type, '-', '_');
+	/*
+	 * Append the thermal zone ID preceded by an underline character to the
+	 * type to disambiguate the sensors command output.
+	 */
+	scnprintf(hwmon->name, THERMAL_HWMON_NAME_LENGTH, "%s_%d", tz->type, tz->id);
+	strreplace(hwmon->name, '-', '_');
 	hwmon->device = hwmon_device_register_for_thermal(&tz->device,
-							  hwmon->type, hwmon);
+							  hwmon->name, hwmon);
 	if (IS_ERR(hwmon->device)) {
 		result = PTR_ERR(hwmon->device);
 		goto free_mem;
 	}
 
- register_sys_interface:
-	temp = kzalloc_obj(*temp);
-	if (!temp) {
-		result = -ENOMEM;
-		goto unregister_name;
-	}
+	temp = &hwmon->tz_temp;
 
 	temp->tz = tz;
-	hwmon->count++;
 
-	snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
-		 "temp%d_input", hwmon->count);
-	temp->temp_input.attr.attr.name = temp->temp_input.name;
+	temp->temp_input.attr.attr.name = "temp1_input";
 	temp->temp_input.attr.attr.mode = 0444;
 	temp->temp_input.attr.show = temp_input_show;
 	sysfs_attr_init(&temp->temp_input.attr.attr);
 	result = device_create_file(hwmon->device, &temp->temp_input.attr);
 	if (result)
-		goto free_temp_mem;
+		goto unregister_name;
 
 	if (thermal_zone_crit_temp_valid(tz)) {
-		snprintf(temp->temp_crit.name,
-				sizeof(temp->temp_crit.name),
-				"temp%d_crit", hwmon->count);
-		temp->temp_crit.attr.attr.name = temp->temp_crit.name;
+		temp->temp_crit.attr.attr.name = "temp1_crit";
 		temp->temp_crit.attr.attr.mode = 0444;
 		temp->temp_crit.attr.show = temp_crit_show;
 		sysfs_attr_init(&temp->temp_crit.attr.attr);
@@ -196,21 +145,17 @@ int thermal_add_hwmon_sysfs(struct therm
 		temp->temp_crit_present = true;
 	}
 
+	/* The list is needed for hwmon lookup during removal. */
 	mutex_lock(&thermal_hwmon_list_lock);
-	if (new_hwmon_device)
-		list_add_tail(&hwmon->node, &thermal_hwmon_list);
-	list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
+	list_add_tail(&hwmon->node, &thermal_hwmon_list);
 	mutex_unlock(&thermal_hwmon_list_lock);
 
 	return 0;
 
  unregister_input:
 	device_remove_file(hwmon->device, &temp->temp_input.attr);
- free_temp_mem:
-	kfree(temp);
  unregister_name:
-	if (new_hwmon_device)
-		hwmon_device_unregister(hwmon->device);
+	hwmon_device_unregister(hwmon->device);
  free_mem:
 	kfree(hwmon);
 
@@ -218,39 +163,37 @@ int thermal_add_hwmon_sysfs(struct therm
 }
 EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
 
+static struct thermal_hwmon_device *
+thermal_hwmon_lookup(const struct thermal_zone_device *tz)
+{
+	struct thermal_hwmon_device *hwmon;
+
+	list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
+		if (hwmon->tz_temp.tz == tz)
+			return hwmon;
+	}
+	return NULL;
+}
+
 void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
 {
 	struct thermal_hwmon_device *hwmon;
 	struct thermal_hwmon_temp *temp;
 
-	hwmon = thermal_hwmon_lookup_by_type(tz);
-	if (unlikely(!hwmon)) {
-		/* Should never happen... */
-		dev_dbg(&tz->device, "hwmon device lookup failed!\n");
-		return;
-	}
+	scoped_guard(mutex, &thermal_hwmon_list_lock) {
+		hwmon = thermal_hwmon_lookup(tz);
+		if (!hwmon)
+			return;
 
-	temp = thermal_hwmon_lookup_temp(hwmon, tz);
-	if (unlikely(!temp)) {
-		/* Should never happen... */
-		dev_dbg(&tz->device, "temperature input lookup failed!\n");
-		return;
+		list_del(&hwmon->node);
 	}
 
+	temp = &hwmon->tz_temp;
+
 	device_remove_file(hwmon->device, &temp->temp_input.attr);
 	if (temp->temp_crit_present)
 		device_remove_file(hwmon->device, &temp->temp_crit.attr);
 
-	mutex_lock(&thermal_hwmon_list_lock);
-	list_del(&temp->hwmon_node);
-	kfree(temp);
-	if (!list_empty(&hwmon->tz_list)) {
-		mutex_unlock(&thermal_hwmon_list_lock);
-		return;
-	}
-	list_del(&hwmon->node);
-	mutex_unlock(&thermal_hwmon_list_lock);
-
 	hwmon_device_unregister(hwmon->device);
 	kfree(hwmon);
 }
#3 lore raw

Re: [PATCH v2 2/3] thermal: hwmon: Register a hwmon device for each thermal zone

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Changing the hwmon device name and splitting aggregate devices breaks userspace hwmon tools…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Changing the hwmon device name and splitting aggregate devices breaks userspace hwmon tools and configurations. --

commit ef04f4845b76df7ab7442699654ee8214cc3a4ea
Author: Rafael J. Wysocki <[email protected]>

thermal: hwmon: Register a hwmon device for each thermal zone

The current code creates one hwmon device per thermal zone type and registers it under the first thermal zone of that type. This commit addresses removal issues by registering one hwmon device for each thermal zone individually, appending the zone ID to the hwmon name, and giving each its own temp1_input.

Quoted 4 lines
drivers/thermal/thermal_hwmon.c +0 -0
index 597c33c8a5550..223ae1571655b 100644

[ ... ]

Quoted 16 lines
drivers/thermal/thermal_hwmon.c +0 -7
@@ -137,54 +101,39 @@ int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
 {
 	struct thermal_hwmon_device *hwmon;
 	struct thermal_hwmon_temp *temp;
-	int new_hwmon_device = 1;
 	int result;
 
-	hwmon = thermal_hwmon_lookup_by_type(tz);
-	if (hwmon) {
-		new_hwmon_device = 0;
-		goto register_sys_interface;
-	}
-
 	hwmon = kzalloc_obj(*hwmon);
 	if (!hwmon)
 		return -ENOMEM;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) · line 115

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +6 -3
 
-	INIT_LIST_HEAD(&hwmon->tz_list);
-	strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
-	strreplace(hwmon->type, '-', '_');
+	/*
+	 * Append the thermal zone ID preceded by an underline character to the
+	 * type to disambiguate the sensors command output.
+	 */
+	scnprintf(hwmon->name, THERMAL_HWMON_NAME_LENGTH, "%s_%d", tz->type, tz->id);
+	strreplace(hwmon->name, '-', '_');

Could this change to the hwmon name attribute cause a regression for userspace tools?

Tools and configuration files, such as sensors.conf, typically use the name attribute for matching devices (for example, matching acpitz-*). Appending an instance-specific ID to the name might prevent these configurations from applying correctly.

Quoted 13 lines
drivers/thermal/thermal_hwmon.c +1 -6
 	hwmon->device = hwmon_device_register_for_thermal(&tz->device,
-							  hwmon->type, hwmon);
+							  hwmon->name, hwmon);
 	if (IS_ERR(hwmon->device)) {
 		result = PTR_ERR(hwmon->device);
 		goto free_mem;
 	}
 
- register_sys_interface:
-	temp = kzalloc_obj(*temp);
-	if (!temp) {
-		result = -ENOMEM;
-		goto unregister_name;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct therm · line 127

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +2 -5
-	}
+	temp = &hwmon->tz_temp;
 
 	temp->tz = tz;
-	hwmon->count++;
 
-	snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
-		 "temp%d_input", hwmon->count);
-	temp->temp_input.attr.attr.name = temp->temp_input.name;
+	temp->temp_input.attr.attr.name = "temp1_input";

Does splitting aggregate devices break existing sysfs access patterns?

Previously, a single hwmon device might have contained multiple temperature inputs, like temp1_input and temp2_input. Since each zone now gets its own hwmon device with only a temp1_input, scripts or tools that explicitly read temp2_input from the old aggregate device path will likely fail with a missing file error.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#4 lore raw

Re: [PATCH v2 2/3] thermal: hwmon: Register a hwmon device for each thermal zone

Quoted 54 lines · On Tue, May 5, 2026 at 2:41 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Changing the hwmon device name and splitting aggregate devices breaks userspace hwmon tools and configurations. --

commit ef04f4845b76df7ab7442699654ee8214cc3a4ea
Author: Rafael J. Wysocki <[email protected]>

thermal: hwmon: Register a hwmon device for each thermal zone

The current code creates one hwmon device per thermal zone type and registers it under the first thermal zone of that type. This commit addresses removal issues by registering one hwmon device for each thermal zone individually, appending the zone ID to the hwmon name, and giving each its own temp1_input.

Quoted 4 lines
drivers/thermal/thermal_hwmon.c +0 -0
index 597c33c8a5550..223ae1571655b 100644

[ ... ]

Quoted 16 lines
drivers/thermal/thermal_hwmon.c +0 -7
@@ -137,54 +101,39 @@ int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
 {
      struct thermal_hwmon_device *hwmon;
      struct thermal_hwmon_temp *temp;
-     int new_hwmon_device = 1;
      int result;
 
-     hwmon = thermal_hwmon_lookup_by_type(tz);
-     if (hwmon) {
-             new_hwmon_device = 0;
-             goto register_sys_interface;
-     }
-
      hwmon = kzalloc_obj(*hwmon);
      if (!hwmon)
              return -ENOMEM;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) · line 115

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +6 -3
 
-     INIT_LIST_HEAD(&hwmon->tz_list);
-     strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
-     strreplace(hwmon->type, '-', '_');
+     /*
+      * Append the thermal zone ID preceded by an underline character to the
+      * type to disambiguate the sensors command output.
+      */
+     scnprintf(hwmon->name, THERMAL_HWMON_NAME_LENGTH, "%s_%d", tz->type, tz->id);
+     strreplace(hwmon->name, '-', '_');

Could this change to the hwmon name attribute cause a regression for userspace tools?

Quoted 4 lines

Tools and configuration files, such as sensors.conf, typically use the name attribute for matching devices (for example, matching acpitz-*). Appending an instance-specific ID to the name might prevent these configurations from applying correctly.

Or not.

Quoted 24 lines
Quoted 13 lines
drivers/thermal/thermal_hwmon.c +1 -6
      hwmon->device = hwmon_device_register_for_thermal(&tz->device,
-                                                       hwmon->type, hwmon);
+                                                       hwmon->name, hwmon);
      if (IS_ERR(hwmon->device)) {
              result = PTR_ERR(hwmon->device);
              goto free_mem;
      }
 
- register_sys_interface:
-     temp = kzalloc_obj(*temp);
-     if (!temp) {
-             result = -ENOMEM;
-             goto unregister_name;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct therm · line 127

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +2 -5
-     }
+     temp = &hwmon->tz_temp;
 
      temp->tz = tz;
-     hwmon->count++;
 
-     snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
-              "temp%d_input", hwmon->count);
-     temp->temp_input.attr.attr.name = temp->temp_input.name;
+     temp->temp_input.attr.attr.name = "temp1_input";
Quoted 7 lines

Does splitting aggregate devices break existing sysfs access patterns?

Previously, a single hwmon device might have contained multiple temperature inputs, like temp1_input and temp2_input. Since each zone now gets its own hwmon device with only a temp1_input, scripts or tools that explicitly read temp2_input from the old aggregate device path will likely fail with a missing file error.

Does this happen in practice though?

maintainer
#5 lore raw

Re: [PATCH v2 2/3] thermal: hwmon: Register a hwmon device for each thermal zone

Quoted 88 lines · Rafael J. Wysocki
Quoted 54 lines · On Tue, May 5, 2026 at 2:41 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Changing the hwmon device name and splitting aggregate devices breaks userspace hwmon tools and configurations. --

commit ef04f4845b76df7ab7442699654ee8214cc3a4ea
Author: Rafael J. Wysocki <[email protected]>

thermal: hwmon: Register a hwmon device for each thermal zone

The current code creates one hwmon device per thermal zone type and registers it under the first thermal zone of that type. This commit addresses removal issues by registering one hwmon device for each thermal zone individually, appending the zone ID to the hwmon name, and giving each its own temp1_input.

Quoted 4 lines
drivers/thermal/thermal_hwmon.c +0 -0
index 597c33c8a5550..223ae1571655b 100644

[ ... ]

Quoted 16 lines
drivers/thermal/thermal_hwmon.c +0 -7
@@ -137,54 +101,39 @@ int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  {
       struct thermal_hwmon_device *hwmon;
       struct thermal_hwmon_temp *temp;
-     int new_hwmon_device = 1;
       int result;
 
-     hwmon = thermal_hwmon_lookup_by_type(tz);
-     if (hwmon) {
-             new_hwmon_device = 0;
-             goto register_sys_interface;
-     }
-
       hwmon = kzalloc_obj(*hwmon);
       if (!hwmon)
               return -ENOMEM;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) · line 115

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +6 -3
 
-     INIT_LIST_HEAD(&hwmon->tz_list);
-     strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
-     strreplace(hwmon->type, '-', '_');
+     /*
+      * Append the thermal zone ID preceded by an underline character to the
+      * type to disambiguate the sensors command output.
+      */
+     scnprintf(hwmon->name, THERMAL_HWMON_NAME_LENGTH, "%s_%d", tz->type, tz->id);
+     strreplace(hwmon->name, '-', '_');

Could this change to the hwmon name attribute cause a regression for userspace tools?

Quoted 4 lines

Tools and configuration files, such as sensors.conf, typically use the name attribute for matching devices (for example, matching acpitz-*). Appending an instance-specific ID to the name might prevent these configurations from applying correctly.

Or not.

Quoted 26 lines
Quoted 13 lines
drivers/thermal/thermal_hwmon.c +1 -6
       hwmon->device = hwmon_device_register_for_thermal(&tz->device,
-                                                       hwmon->type, hwmon);
+                                                       hwmon->name, hwmon);
       if (IS_ERR(hwmon->device)) {
               result = PTR_ERR(hwmon->device);
               goto free_mem;
       }
 
- register_sys_interface:
-     temp = kzalloc_obj(*temp);
-     if (!temp) {
-             result = -ENOMEM;
-             goto unregister_name;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct therm · line 127

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +2 -5
-     }
+     temp = &hwmon->tz_temp;
 
       temp->tz = tz;
-     hwmon->count++;
 
-     snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
-              "temp%d_input", hwmon->count);
-     temp->temp_input.attr.attr.name = temp->temp_input.name;
+     temp->temp_input.attr.attr.name = "temp1_input";

Does splitting aggregate devices break existing sysfs access patterns?

Quoted 8 lines
Quoted 5 lines

Previously, a single hwmon device might have contained multiple temperature inputs, like temp1_input and temp2_input. Since each zone now gets its own hwmon device with only a temp1_input, scripts or tools that explicitly read temp2_input from the old aggregate device path will likely fail with a missing file error.

Does this happen in practice though?

I guess we'll see.

Guenter

author maintainer
#6 lore raw

Re: [PATCH v2 2/3] thermal: hwmon: Register a hwmon device for each thermal zone

Quoted 96 lines · Guenter Roeck
Quoted 94 lines · Rafael J. Wysocki
Quoted 54 lines · On Tue, May 5, 2026 at 2:41 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Changing the hwmon device name and splitting aggregate devices breaks userspace hwmon tools and configurations. --

commit ef04f4845b76df7ab7442699654ee8214cc3a4ea
Author: Rafael J. Wysocki <[email protected]>

thermal: hwmon: Register a hwmon device for each thermal zone

The current code creates one hwmon device per thermal zone type and registers it under the first thermal zone of that type. This commit addresses removal issues by registering one hwmon device for each thermal zone individually, appending the zone ID to the hwmon name, and giving each its own temp1_input.

Quoted 4 lines
drivers/thermal/thermal_hwmon.c +0 -0
index 597c33c8a5550..223ae1571655b 100644

[ ... ]

Quoted 16 lines
drivers/thermal/thermal_hwmon.c +0 -7
@@ -137,54 +101,39 @@ int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  {
       struct thermal_hwmon_device *hwmon;
       struct thermal_hwmon_temp *temp;
-     int new_hwmon_device = 1;
       int result;
 
-     hwmon = thermal_hwmon_lookup_by_type(tz);
-     if (hwmon) {
-             new_hwmon_device = 0;
-             goto register_sys_interface;
-     }
-
       hwmon = kzalloc_obj(*hwmon);
       if (!hwmon)
               return -ENOMEM;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) · line 115

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +6 -3
 
-     INIT_LIST_HEAD(&hwmon->tz_list);
-     strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
-     strreplace(hwmon->type, '-', '_');
+     /*
+      * Append the thermal zone ID preceded by an underline character to the
+      * type to disambiguate the sensors command output.
+      */
+     scnprintf(hwmon->name, THERMAL_HWMON_NAME_LENGTH, "%s_%d", tz->type, tz->id);
+     strreplace(hwmon->name, '-', '_');

Could this change to the hwmon name attribute cause a regression for userspace tools?

Quoted 4 lines

Tools and configuration files, such as sensors.conf, typically use the name attribute for matching devices (for example, matching acpitz-*). Appending an instance-specific ID to the name might prevent these configurations from applying correctly.

Or not.

Quoted 31 lines
Quoted 13 lines
drivers/thermal/thermal_hwmon.c +1 -6
       hwmon->device = hwmon_device_register_for_thermal(&tz->device,
-                                                       hwmon->type, hwmon);
+                                                       hwmon->name, hwmon);
       if (IS_ERR(hwmon->device)) {
               result = PTR_ERR(hwmon->device);
               goto free_mem;
       }
 
- register_sys_interface:
-     temp = kzalloc_obj(*temp);
-     if (!temp) {
-             result = -ENOMEM;
-             goto unregister_name;

drivers/thermal/thermal_hwmon.c int thermal_add_hwmon_sysfs(struct therm · line 127

Quoted 10 lines
drivers/thermal/thermal_hwmon.c +2 -5
-     }
+     temp = &hwmon->tz_temp;
 
       temp->tz = tz;
-     hwmon->count++;
 
-     snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
-              "temp%d_input", hwmon->count);
-     temp->temp_input.attr.attr.name = temp->temp_input.name;
+     temp->temp_input.attr.attr.name = "temp1_input";

Does splitting aggregate devices break existing sysfs access patterns?

Previously, a single hwmon device might have contained multiple temperature inputs, like temp1_input and temp2_input. Since each zone now gets its own hwmon device with only a temp1_input, scripts or tools that explicitly read temp2_input from the old aggregate device path will likely fail with a missing file error.

Quoted 3 lines
Quoted 2 lines

Does this happen in practice though?

I guess we'll see.

Right.

#7 lore raw

Re: [PATCH v2 2/3] thermal: hwmon: Register a hwmon device for each thermal zone

Dear hwmon devs,
(please keep me in CC, I am not subscribed to the list),

after upgrading my systems to kernel 7.1.5 which incorporates this change (Arch and Gentoo), I have for now become aware of two places with userspace fallout from this change:

* KDE / Plasma System Monitor
Report: https://bugs.kde.org/show_bug.cgi?id=523588
Corresponding userspace code: https://invent.kde.org/plasma/ksystemstats/-/blob/46f7e842767774b96bcd22edec0636a267151475/plugins/lmsensors/lmsensors.cpp

As Plasma System Monitor remembers the (full) hwmon sensor name in all widgets and thermal zone IDs may change depending on module load order, sensors are lost both after upgrade to 7.1.5 and at "random" boots after (whenever init order changes for some reason).

* (good old) gkrellm
Report: https://git.srcbox.net/gkrellm/gkrellm/issues/98
As shown in the report, gkrellm remembers the full hwmon sensor name and hence is affected both when upgrading to 7.1.5 and also at "random" boots after (whenever init order changes for some reason).

My custom Conky script followed the established practice of these tools and also broke, but in my case, I fixed it myself by just ignoring the thermal zone ID if present ( https://github.com/olifre/conky-themes/commit/6d015b9d9eae21ffab8c5571c387534bb93cbc50 ).

As I do not use Gnome or other system monitoring tools, I do not know if the fallout is larger than this,
but given that lmsensors returns the new names with thermal zone ID included, it seems likely other tools suffer from similar problems.

Cheers,
Oliver

author maintainer
#8 lore raw

Re: [PATCH v2 2/3] thermal: hwmon: Register a hwmon device for each thermal zone

Hi,

Quoted 16 lines · Oliver Freyermuth

Dear hwmon devs,
(please keep me in CC, I am not subscribed to the list),

after upgrading my systems to kernel 7.1.5 which incorporates this change (Arch and Gentoo), I have for now become aware of two places with userspace fallout from this change:

* KDE / Plasma System Monitor
Report: https://bugs.kde.org/show_bug.cgi?id=523588
Corresponding userspace code: https://invent.kde.org/plasma/ksystemstats/-/blob/46f7e842767774b96bcd22edec0636a267151475/plugins/lmsensors/lmsensors.cpp

As Plasma System Monitor remembers the (full) hwmon sensor name in all widgets and thermal zone IDs may change depending on module load order, sensors are lost both after upgrade to 7.1.5 and at "random" boots after (whenever init order changes for some reason).

* (good old) gkrellm
Report: https://git.srcbox.net/gkrellm/gkrellm/issues/98
As shown in the report, gkrellm remembers the full hwmon sensor name and hence is affected both when upgrading to 7.1.5 and also at "random" boots after (whenever init order changes for some reason).

Quoted 5 lines

My custom Conky script followed the established practice of these tools and also broke, but in my case, I fixed it myself by just ignoring the thermal zone ID if present ( https://github.com/olifre/conky-themes/commit/6d015b9d9eae21ffab8c5571c387534bb93cbc50 ).

As I do not use Gnome or other system monitoring tools, I do not know if the fallout is larger than this,
but given that lmsensors returns the new names with thermal zone ID included, it seems likely other tools suffer from similar problems.

Thanks for the report, we'll clearly need to do something about this.

I'll see what can be done and figure out something.

Thanks!

author maintainer
#9 lore raw

PATCH v2 3/3 thermal: hwmon: Use extra_groups for adding temperature attributes

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Instead of passing NULL as the last argument to __hwmon_device_register() in hwmon_device_register_for_thermal() and then adding each temperature sysfs attribute to the hwmon device via device_create_file(), redefine hwmon_device_register_for_thermal() to take an extra_groups argument that will be passed to __hwmon_device_register(), define an attribute group with a proper .is_visible() callback for the temperature attributes and a related attribute groups pointer, and pass the latter to hwmon_device_register_for_thermal().

This causes the code to be way more straightforward and closer to what the other users of the hwmon subsystem do.

No intentional functional impact.

---

v1 -> v2:
* Rebase on top of [1-2/3]
* Drop struct thermal_hwmon_temp (sashiko.dev)
*

---
 drivers/hwmon/hwmon.c           |    6 +
 drivers/thermal/thermal_hwmon.c |  122 ++++++++++++++--------------------------
 include/linux/hwmon.h           |    3 
 3 files changed, 51 insertions(+), 80 deletions(-)
drivers/hwmon/hwmon.c +4 -2
@@ -1082,6 +1082,7 @@ EXPORT_SYMBOL_GPL(hwmon_device_register_
  * @dev: the parent device
  * @name: hwmon name attribute
  * @drvdata: driver data to attach to created device
+ * @extra_groups: pointer to list of additional non-standard attribute groups
  *
  * The use of this function is restricted. It is provided for legacy reasons
  * and must only be called from the thermal subsystem.
@@ -1093,12 +1094,13 @@ EXPORT_SYMBOL_GPL(hwmon_device_register_
  */
 struct device *
 hwmon_device_register_for_thermal(struct device *dev, const char *name,
-				  void *drvdata)
+				  void *drvdata,
+				  const struct attribute_group **extra_groups)
 {
 	if (!name || !dev)
 		return ERR_PTR(-EINVAL);
 
-	return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
+	return __hwmon_device_register(dev, name, drvdata, NULL, extra_groups);
 }
 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, "HWMON_THERMAL");
 
drivers/thermal/thermal_hwmon.c +45 -77

201 lines not on this page · raw message

include/linux/hwmon.h +2 -1
@@ -477,7 +477,8 @@ hwmon_device_register_with_info(struct d
 				const struct attribute_group **extra_groups);
 struct device *
 hwmon_device_register_for_thermal(struct device *dev, const char *name,
-				  void *drvdata);
+				  void *drvdata,
+				  const struct attribute_group **extra_groups);
 struct device *
 devm_hwmon_device_register_with_info(struct device *dev,
 				const char *name, void *drvdata,