Skip to contentSkip to conversation

thermal: core: drop duplicate CDEV_STATE_UPDATE netlink events

PATCH New Ilia Snezhkov 1 participant 1 message Nov 30 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: core: drop duplicate CDEV_STATE_UPDATE netlink events

thermal_cdev_set_cur_state() calls thermal_notify_cdev_state_update() unconditionally on every governor update even when the cooling device target state did not change. Under polling governors such as power_allocator, a thermal zone bound to multiple cooling devices (for example little CPU, big CPU and GPU) broadcasts identical THERMAL_GENL_EVENT_CDEV_STATE_UPDATE netlink messages on every polling tick even when each cooling device stays at state 0 or holds a steady throttling level.

On Android devices running Pixel Thermal HAL's ThermalWatcher (which logs every received CDEV_STATE_UPDATE event at INFO level), this generates ~12 redundant netlink broadcasts per second under warm workloads (~25% of total logcat output during sustained benchmarks) without representing any actual state transition.

Track the last notified cooling state in struct thermal_cooling_device (initialized from the existing get_cur_state() call in thermal_cooling_device_add(), invalidated to THERMAL_CSTATE_INVALID on sysfs cur_state_store() and thermal_cooling_device_update() under cdev->lock), and emit thermal_notify_cdev_state_update() only when the state actually changes.

Leave cdev->ops->set_cur_state(), thermal_cooling_device_stats_update(), and thermal_debug_cdev_state_update() untouched so drivers that rely on periodic set_cur_state() invocations keep their existing behavior and no extra per-tick get_cur_state() hardware reads are introduced.

Tested on an MT6789 smartphone (Linux 6.18) with power_allocator across a 45C-52C thermal sweep via kprobe on thermal_notify_cdev_state_update():
102 trace_cdev_update calls over 32 ticks emitted 19 netlink notifications (state transitions only) with 0 consecutive duplicates, and sysfs cur_state writes invalidated last_state as expected.

Fixes 55cdf0a283b8 ("thermal: core: Add notifications call in the framework")

---
Found and drafted with an AI coding assistant.

 drivers/thermal/thermal_core.c    | 4 ++++
 drivers/thermal/thermal_helpers.c | 8 ++++++--
 drivers/thermal/thermal_sysfs.c   | 1 +
 include/linux/thermal.h           | 1 +
 4 files changed, 12 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +4 -0
index e8d4da0..43b592f 100644
@@ -1040,6 +1040,9 @@ int thermal_cooling_device_add(struct thermal_cooling_device *cdev,
 	if (ret)
 		current_state = ULONG_MAX;
 
+	cdev->last_state = current_state <= cdev->max_state ?
+			   current_state : THERMAL_CSTATE_INVALID;
+
 	thermal_cooling_device_setup_sysfs(cdev);
 
 	ret = device_add(&cdev->device);
@@ -1188,6 +1191,7 @@ void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
 		return;
 
 	thermal_cooling_device_stats_reinit(cdev);
+	cdev->last_state = THERMAL_CSTATE_INVALID;
 
 	list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
 		if (ti->upper == cdev->max_state)
drivers/thermal/thermal_helpers.c +6 -2
index b1152ad..4f13eb6 100644
@@ -144,7 +144,8 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
 }
 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
 
-static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
+static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
+				      unsigned long state)
 {
 	int ret;
 
@@ -156,7 +157,10 @@ static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int s
 	if (ret)
 		return ret;
 
-	thermal_notify_cdev_state_update(cdev, state);
+	if (cdev->last_state != state) {
+		cdev->last_state = state;
+		thermal_notify_cdev_state_update(cdev, state);
+	}
 	thermal_cooling_device_stats_update(cdev, state);
 	thermal_debug_cdev_state_update(cdev, state);
 
drivers/thermal/thermal_sysfs.c +1 -0
index 0c2313c..863bcd5 100644
@@ -558,6 +558,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 	if (result)
 		return result;
 
+	cdev->last_state = THERMAL_CSTATE_INVALID;
 	thermal_cooling_device_stats_update(cdev, state);
 
 	return count;
include/linux/thermal.h +1 -0
index 306ad17..494feea 100644
@@ -124,6 +124,7 @@ struct thermal_cooling_device {
 	int id;
 	const char *type;
 	unsigned long max_state;
+	unsigned long last_state;
 	struct device device;
 	void *devdata;
 	void *stats;
Signature · 1 line
2.54.0 (Apple Git-157)