* [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation
@ 2026-11-19 9:46 Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 01/12] thermal: core: Add thermal zone device reference helpers Daniel Lezcano
` (11 more replies)
0 siblings, 12 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm
Thermal management may need to estimate a temperature that no sensor
measures directly. For example, skin temperature can be estimated from
readings near the processor, display and battery; an internal board
region's temperature from sensors around the processor, memory and
power-management circuitry; and the temperature surrounding the battery
from readings near the battery, charging circuitry and adjacent
components. Each sensor's contribution depends on its location relative
to the target region and the thermal paths connecting them. A linear
regression model provides a practical approximation of these
relationships: coefficients fitted from platform measurements weight each
sensor's contribution, while a constant offset accounts for the baseline
difference between the weighted readings and the target temperature.
This series provides common support for aggregated thermal zones, allowing
estimated temperatures to benefit from the thermal framework's existing
trip handling, governors and cooling policies. It has two parts: thermal
core support, which makes aggregation available independently of the
firmware interface, and OF support, which allows platforms to describe
aggregated zones in Device Tree.
This series introduces support in the thermal core for deriving a zone's
temperature from a weighted combination of existing thermal zone
temperatures and a constant offset, reusing the readings already provided
by sensor drivers. Each source zone retains its own trips and cooling
policy and can contribute to several aggregates, each representing a
different thermal constraint. The series adds reference-counting helpers
so aggregate owners can keep source zones alive until their dependencies
are torn down. It also introduces registration-time validation of the
aggregation parameters to reject unavailable sources, nested aggregation,
incomplete coefficient sets and incompatible operations before the new
zone is exposed.
The series also adds OF support for describing aggregated zones through
multiple thermal-sensors references and optional coefficients in Device
Tree. These references identify hardware sensors whose registered thermal
zones provide the source temperatures. Aggregate creation waits until all
sources are available, allowing sensor drivers to probe independently.
Dependent aggregates are removed before a source is unregistered and
recreated when it returns. This lets platforms describe their thermal
estimates without changes to sensor drivers, while preserving existing
single-source calibration.
The series includes KUnit tests for both the core and OF support, using
synthetic thermal sources without requiring platform hardware. The core
tests cover parameter validation, default coefficients, temperature
calculations, error handling and source lock contention. The OF tests
cover deferred creation, coefficient parsing, source removal and
recreation, and a temperature read concurrent with source teardown. All
23 cases pass on UML with lockdep enabled. Initial late-init discovery,
real module loading and platform hardware are not covered by these tests.
Testing:
- git diff --check passes.
- Per-patch checkpatch reports no errors. Remaining warnings concern
commit-message line lengths and possible MAINTAINERS updates.
This cover letter was drafted with OpenCode using gpt-6.1-sol from the
commit series, human driven and a source-code study. The same tool ran
the KUnit and patch checks reported above.
Daniel Lezcano (12):
thermal: core: Add thermal zone device reference helpers
thermal: core: Introduce private thermal zone aggregation structures
thermal: core: Validate thermal zone aggregation descriptions
thermal: core: Initialize thermal zone aggregation coefficients
thermal: core: Dispatch aggregate temperature reads
thermal: core: Calculate aggregated thermal zone temperatures
thermal: core: Initialize aggregation during zone registration
thermal: of: Separate zone parsing from sensor lookup
thermal: of: Track registered hardware temperature sources
thermal: of: Instantiate aggregates after their source zones
dt-bindings: thermal: Describe multi-source zone aggregation
thermal: testing: Add KUnit coverage for zone aggregation
.../bindings/thermal/thermal-zones.yaml | 46 ++-
drivers/thermal/Kconfig | 31 ++
drivers/thermal/Makefile | 2 +
drivers/thermal/testing/.kunitconfig | 11 +
drivers/thermal/testing/README.rst | 34 ++
.../thermal/testing/thermal_aggregation.dtso | 53 +++
.../testing/thermal_aggregation_test.c | 351 +++++++++++++++++
.../testing/thermal_of_aggregation_test.c | 330 ++++++++++++++++
drivers/thermal/thermal_aggregation.c | 252 ++++++++++++
drivers/thermal/thermal_core.c | 49 ++-
drivers/thermal/thermal_core.h | 48 ++-
drivers/thermal/thermal_helpers.c | 9 +-
drivers/thermal/thermal_of.c | 370 ++++++++++++++++--
include/linux/thermal.h | 4 +
14 files changed, 1532 insertions(+), 58 deletions(-)
create mode 100644 drivers/thermal/testing/.kunitconfig
create mode 100644 drivers/thermal/testing/README.rst
create mode 100644 drivers/thermal/testing/thermal_aggregation.dtso
create mode 100644 drivers/thermal/testing/thermal_aggregation_test.c
create mode 100644 drivers/thermal/testing/thermal_of_aggregation_test.c
create mode 100644 drivers/thermal/thermal_aggregation.c
base-commit: 0dd55073c872df387d28480d1197c013423d1705
--
2.43.0
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v1 01/12] thermal: core: Add thermal zone device reference helpers
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 02/12] thermal: core: Introduce private thermal zone aggregation structures Daniel Lezcano
` (10 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Add private thermal_zone_device_get() and thermal_zone_device_put()
helpers around the embedded device reference count. Document that callers
must protect the zone pointer when acquiring a reference and that a
reference protects lifetime, not operational availability.
Use the helpers in thermal_zone_get_by_id() and its scoped cleanup class
to keep reference acquisition and release within the thermal core
abstraction. Preserve the existing reference ownership and teardown
behavior.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_core.c | 35 ++++++++++++++++++++++++++++++----
drivers/thermal/thermal_core.h | 5 ++++-
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index e8d4da0aeac3..02c2c08077bf 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -698,6 +698,35 @@ int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
return 0;
}
+/**
+ * thermal_zone_device_get() - acquire a thermal zone device reference
+ * @tz: thermal zone device, or NULL
+ *
+ * The caller must protect @tz against concurrent destruction, for example
+ * by holding an existing reference or thermal_list_lock. The reference
+ * protects the zone's lifetime, not its operational availability.
+ *
+ * Return: @tz, or NULL if @tz is NULL.
+ */
+struct thermal_zone_device *
+thermal_zone_device_get(struct thermal_zone_device *tz)
+{
+ if (tz)
+ get_device(&tz->device);
+
+ return tz;
+}
+
+/**
+ * thermal_zone_device_put() - release a thermal zone device reference
+ * @tz: thermal zone device, or NULL
+ */
+void thermal_zone_device_put(struct thermal_zone_device *tz)
+{
+ if (tz)
+ put_device(&tz->device);
+}
+
struct thermal_zone_device *thermal_zone_get_by_id(int id)
{
struct thermal_zone_device *tz;
@@ -705,10 +734,8 @@ struct thermal_zone_device *thermal_zone_get_by_id(int id)
guard(mutex)(&thermal_list_lock);
list_for_each_entry(tz, &thermal_tz_list, node) {
- if (tz->id == id) {
- get_device(&tz->device);
- return tz;
- }
+ if (tz->id == id)
+ return thermal_zone_device_get(tz);
}
return NULL;
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 7ef7c6cab437..7b91717d9d74 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -211,10 +211,13 @@ int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
void *thermal_governor);
+struct thermal_zone_device *thermal_zone_device_get(struct thermal_zone_device *tz);
+void thermal_zone_device_put(struct thermal_zone_device *tz);
+
struct thermal_zone_device *thermal_zone_get_by_id(int id);
DEFINE_CLASS(thermal_zone_get_by_id, struct thermal_zone_device *,
- if (_T) put_device(&_T->device), thermal_zone_get_by_id(id), int id)
+ thermal_zone_device_put(_T), thermal_zone_get_by_id(id), int id)
static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
{
base-commit: 0dd55073c872df387d28480d1197c013423d1705
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 02/12] thermal: core: Introduce private thermal zone aggregation structures
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 01/12] thermal: core: Add thermal zone device reference helpers Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 03/12] thermal: core: Validate thermal zone aggregation descriptions Daniel Lezcano
` (9 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Represent a linear combination of thermal zone temperatures with an
array of source zone pointers and signed coefficients. Reuse the existing
offset in thermal_zone_params for the signed constant adjustment instead
of duplicating it in the aggregation description.
Keep each coefficient in the aggregation entry because it describes
the contribution of a source zone to a particular aggregate, rather
than a property of the source zone itself. The same source zone can
contribute to several aggregates with different coefficients. A single
coefficient in thermal_zone_device could not represent these independent
relationships.
Define the aggregation structures in thermal_core.h and associate the
description with a zone through an optional pointer in thermal_zone_params.
Keep the definitions private to the thermal core, with only an opaque
forward declaration and pointer in the public thermal header. A NULL
pointer denotes a non-aggregated zone.
This establishes the firmware-independent representation for subsequent
aggregation support without changing registration, callbacks, or
temperature handling.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_core.h | 23 +++++++++++++++++++++++
include/linux/thermal.h | 4 ++++
2 files changed, 27 insertions(+)
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 7b91717d9d74..a5fdf5c24b6c 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -63,6 +63,29 @@ struct thermal_governor {
struct list_head governor_list;
};
+/**
+ * struct thermal_zone_aggregate_entry - source zone and its coefficient
+ * @tz: source thermal zone
+ * @coefficient: signed multiplier for the source zone temperature
+ */
+struct thermal_zone_aggregate_entry {
+ struct thermal_zone_device *tz;
+ s32 coefficient;
+};
+
+/**
+ * struct thermal_zone_aggregate - linear combination of source zone temperatures
+ * @entries: array of source thermal zones and their coefficients
+ * @num_entries: number of entries in the array
+ *
+ * Represents the sum of each source zone temperature multiplied by its
+ * coefficient. The constant offset is stored in struct thermal_zone_params.
+ */
+struct thermal_zone_aggregate {
+ struct thermal_zone_aggregate_entry *entries;
+ unsigned int num_entries;
+};
+
#define TZ_STATE_FLAG_SUSPENDED BIT(0)
#define TZ_STATE_FLAG_RESUMING BIT(1)
#define TZ_STATE_FLAG_INIT BIT(2)
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index 306ad17aed89..8be61b0c2684 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -30,6 +30,7 @@
#define THERMAL_TEMP_INVALID -274000
struct thermal_zone_device;
+struct thermal_zone_aggregate;
struct thermal_cooling_device;
struct thermal_instance;
struct thermal_debugfs;
@@ -192,6 +193,9 @@ struct thermal_zone_params {
* Used by thermal zone drivers (default 0).
*/
int offset;
+
+ /* Core-private aggregation data, or NULL for a non-aggregated zone. */
+ struct thermal_zone_aggregate *aggregate;
};
/* Function declarations */
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 03/12] thermal: core: Validate thermal zone aggregation descriptions
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 01/12] thermal: core: Add thermal zone device reference helpers Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 02/12] thermal: core: Introduce private thermal zone aggregation structures Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 04/12] thermal: core: Initialize thermal zone aggregation coefficients Daniel Lezcano
` (8 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Add a private helper to validate aggregation descriptions before
coefficient initialization and publication. Introduce thermal_aggregation.c
and build it as part of thermal_sys when CONFIG_THERMAL_AGGREGATION
is enabled. Keep aggregation optional and independent of OF support. Check for nonempty entries, valid
source zones, and sources that have not started exiting. Registered
non-aggregated sources already have a temperature callback, so there
is no need to check it again.
Accept the destination operations and reject set_trips because an
aggregate temperature window cannot directly program hardware
thresholds on its individual sources.
Before initialization, use zero to identify unspecified coefficients.
Accept either a complete nonzero coefficient set or an all-zero set
requesting averaging defaults, and reject partial specification. Keep
this check in the validator. Do not apply the presence check after
initialization, where zero is valid for temperature calculation.
Reject aggregated sources because aggregations of aggregated zones are
not supported yet. Inspect each source under its zone lock and document
that callers must hold valid source references and preserve the validated
configuration. Validation does not acquire references or prevent removal.
Leave invocation to the subsequent aggregation initialization changes.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/Kconfig | 8 +++
drivers/thermal/Makefile | 1 +
drivers/thermal/thermal_aggregation.c | 76 +++++++++++++++++++++++++++
3 files changed, 85 insertions(+)
create mode 100644 drivers/thermal/thermal_aggregation.c
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 810eeccedfba..b5185084eac1 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -17,6 +17,14 @@ menuconfig THERMAL
if THERMAL
+config THERMAL_AGGREGATION
+ bool "Thermal zone aggregation"
+ help
+ Allow thermal zones to calculate their temperature from multiple
+ existing thermal zones using signed coefficients and an offset.
+ This also enables discovery of multi-source zones in Device Tree.
+ If unsure, say N.
+
config THERMAL_NETLINK
bool "Thermal netlink management"
depends on NET
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 3b249195c088..d059ca528908 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_THERMAL) += thermal_sys.o
thermal_sys-y += thermal_core.o thermal_sysfs.o
thermal_sys-y += thermal_trip.o thermal_helpers.o
thermal_sys-y += thermal_thresholds.o
+thermal_sys-$(CONFIG_THERMAL_AGGREGATION) += thermal_aggregation.o
# netlink interface to manage the thermal framework
thermal_sys-$(CONFIG_THERMAL_NETLINK) += thermal_netlink.o
diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
new file mode 100644
index 000000000000..f85f5a487d60
--- /dev/null
+++ b/drivers/thermal/thermal_aggregation.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/errno.h>
+
+#include "thermal_core.h"
+
+/**
+ * thermal_zone_aggregate_validate() - check an aggregation before initialization
+ * @aggregate: aggregation description to validate before coefficient initialization
+ * @ops: operations of the destination thermal zone
+ *
+ * The caller must hold valid references to the source zones and keep the entries
+ * stable. Take each source lock separately to inspect its parameters and state;
+ * the caller must not hold a thermal zone lock.
+ *
+ * Aggregations of aggregated zones are not supported yet: every source must be
+ * a registered, non-aggregated thermal zone. This also rejects
+ * a source whose parameters already reference this aggregation description.
+ * Source membership and non-aggregation must remain unchanged after validation.
+ * Validation does not acquire references or prevent subsequent source removal.
+ *
+ * Before initialization, zero denotes an unspecified coefficient. Either all
+ * coefficients must be supplied or all must be zero to request averaging
+ * defaults. This presence check must not be applied after initialization:
+ * zero is valid in the temperature calculation.
+ *
+ * Return: 0 on success, -EINVAL for an invalid description, partial coefficient
+ * specification, aggregated sources or destination set_trips,
+ * or -ENODEV for missing or exiting sources.
+ */
+static int __maybe_unused
+thermal_zone_aggregate_validate(struct thermal_zone_aggregate *aggregate,
+ const struct thermal_zone_device_ops *ops)
+{
+ unsigned int initialized = 0;
+ unsigned int i;
+
+ /* An aggregation needs a description and at least one source entry. */
+ if (!aggregate || !aggregate->entries || !aggregate->num_entries)
+ return -EINVAL;
+
+ /*
+ * Destination operations must exist, but cannot program hardware windows:
+ * aggregate temperature boundaries do not directly map to source thresholds.
+ */
+ if (!ops || ops->set_trips)
+ return -EINVAL;
+
+ for (i = 0; i < aggregate->num_entries; i++) {
+ const struct thermal_zone_aggregate_entry *entry = &aggregate->entries[i];
+ struct thermal_zone_device *source = entry->tz;
+
+ /* Reject unresolved sources before inspecting their zone state. */
+ if (IS_ERR_OR_NULL(source))
+ return -ENODEV;
+ /* Before initialization, zero requests a default contribution. */
+ initialized += entry->coefficient != 0;
+
+ guard(thermal_zone)(source);
+ /* A reference protects lifetime, but does not prevent source exit. */
+ if (source->state & TZ_STATE_FLAG_EXIT)
+ return -ENODEV;
+ /* Nested aggregation, including a direct self-dependency, is unsupported. */
+ if (source->tzp && source->tzp->aggregate)
+ return -EINVAL;
+ }
+
+ /*
+ * Require either a complete explicit combination or all-zero coefficients
+ * for averaging defaults; there is no defined default for a partial set.
+ */
+ if (initialized && initialized != aggregate->num_entries)
+ return -EINVAL;
+
+ return 0;
+}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 04/12] thermal: core: Initialize thermal zone aggregation coefficients
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (2 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 03/12] thermal: core: Validate thermal zone aggregation descriptions Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 05/12] thermal: core: Dispatch aggregate temperature reads Daniel Lezcano
` (7 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Use PERMILLE from linux/units.h for coefficients in thousandths and
add a helper in thermal_aggregation.c to initialize them directly in the
aggregation entries before publication.
Treat zero as an unspecified coefficient before initialization. Preserve
supplied coefficient sets without normalization. Require validation of
the description before initialization; partial specification is rejected
by thermal_zone_aggregate_validate().
When all coefficients are uninitialized, distribute a total of 1000 as
evenly as possible, assigning the remainder in source order to approximate
the arithmetic mean.
Reject default generation for more sources than the coefficient scale
because it would produce zero coefficients. Leave source pointers and
the offset in thermal_zone_params unchanged.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_aggregation.c | 80 +++++++++++++++++++++++++++
drivers/thermal/thermal_core.h | 5 +-
2 files changed, 83 insertions(+), 2 deletions(-)
diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
index f85f5a487d60..8b503fab25bc 100644
--- a/drivers/thermal/thermal_aggregation.c
+++ b/drivers/thermal/thermal_aggregation.c
@@ -1,9 +1,89 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/errno.h>
+#include <linux/units.h>
#include "thermal_core.h"
+/**
+ * thermal_zone_aggregate_init_coefficients() - initialize aggregation multipliers
+ * @aggregate: aggregation description to initialize before publication
+ *
+ * The description must be validated before calling this helper. Before
+ * initialization, zero denotes an unspecified coefficient. If all are zero,
+ * generate approximately equal contributions summing to
+ * PERMILLE, distributing the remainder in source order.
+ * If all coefficients are nonzero, leave them unchanged without normalization.
+ * Partial specification is rejected by thermal_zone_aggregate_validate().
+ *
+ * The zone parameters' offset and source pointers are not modified.
+ *
+ * Return: 0 on success or -EINVAL for invalid arguments. Entries are unchanged
+ * on failure.
+ */
+static int __maybe_unused
+thermal_zone_aggregate_init_coefficients(struct thermal_zone_aggregate *aggregate)
+{
+ unsigned int initialized = 0;
+ unsigned int base, remainder, i;
+
+ if (!aggregate || !aggregate->entries || !aggregate->num_entries)
+ return -EINVAL;
+
+ for (i = 0; i < aggregate->num_entries; i++)
+ initialized += aggregate->entries[i].coefficient != 0;
+
+ /*
+ * Validation has rejected partial specification. If coefficients are
+ * supplied, preserve the explicit affine combination without normalization.
+ */
+ if (initialized)
+ return 0;
+
+ /*
+ * Current systems are far from aggregating 1000 sources, but enforce
+ * this limit for unlikely yet possible configurations. More sources
+ * than the scale would produce zero default coefficients, which denote
+ * uninitialized entries.
+ */
+ if (aggregate->num_entries > PERMILLE)
+ return -EINVAL;
+
+ /*
+ * With no coefficients supplied, approximate the arithmetic mean of
+ * the source temperatures:
+ *
+ * T = sum(coefficient[i] * temperature[i]) / 1000 + offset
+ *
+ * Each source should contribute equally, so its ideal coefficient is
+ * PERMILLE / num_entries. Integer division rounds this
+ * down, leaving a remainder that must be distributed to keep the total
+ * coefficient sum exactly equal to PERMILLE.
+ *
+ * Assign one additional unit to each of the first 'remainder' sources.
+ * This makes the distribution deterministic in source order, with any
+ * two coefficients differing by at most one. For example:
+ *
+ * 2 sources: 500, 500
+ * 3 sources: 334, 333, 333
+ * 6 sources: 167, 167, 167, 167, 166, 166
+ *
+ * Equal source temperatures therefore retain unity gain. For unequal
+ * temperatures, the result approximates their arithmetic mean with a
+ * small bias from coefficient rounding. The signed offset in the zone
+ * parameters remains independent and is applied after division.
+ *
+ * These defaults apply only when every coefficient is uninitialized;
+ * explicitly supplied coefficients are never normalized.
+ */
+ base = PERMILLE / aggregate->num_entries;
+ remainder = PERMILLE % aggregate->num_entries;
+ for (i = 0; i < aggregate->num_entries; i++)
+ aggregate->entries[i].coefficient = base + (i < remainder);
+
+ return 0;
+}
+
/**
* thermal_zone_aggregate_validate() - check an aggregation before initialization
* @aggregate: aggregation description to validate before coefficient initialization
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index a5fdf5c24b6c..365704584966 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -66,7 +66,7 @@ struct thermal_governor {
/**
* struct thermal_zone_aggregate_entry - source zone and its coefficient
* @tz: source thermal zone
- * @coefficient: signed multiplier for the source zone temperature
+ * @coefficient: signed multiplier in thousandths, zero when uninitialized
*/
struct thermal_zone_aggregate_entry {
struct thermal_zone_device *tz;
@@ -79,7 +79,8 @@ struct thermal_zone_aggregate_entry {
* @num_entries: number of entries in the array
*
* Represents the sum of each source zone temperature multiplied by its
- * coefficient. The constant offset is stored in struct thermal_zone_params.
+ * coefficient in thousandths, divided by 1000. The constant offset is
+ * stored in struct thermal_zone_params.
*/
struct thermal_zone_aggregate {
struct thermal_zone_aggregate_entry *entries;
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 05/12] thermal: core: Dispatch aggregate temperature reads
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (3 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 04/12] thermal: core: Initialize thermal zone aggregation coefficients Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 06/12] thermal: core: Calculate aggregated thermal zone temperatures Daniel Lezcano
` (6 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Select a private aggregate temperature helper in __thermal_zone_get_temp()
when the zone parameters contain an aggregation description. Retain the
existing driver callback for non-aggregated zones and the common emulation
and error handling for both paths.
Add the aggregate temperature helper in thermal_aggregation.c and declare
it in the private thermal core header. Keep temperature dispatch in
thermal_helpers.c.
Leave the aggregate helper as a placeholder returning -EOPNOTSUPP until
the calculation is implemented, avoiding a successful return with an
uninitialized temperature.
Provide a private stub returning -EOPNOTSUPP when thermal aggregation
is disabled, preserving the ordinary thermal zone path.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_aggregation.c | 5 +++++
drivers/thermal/thermal_core.h | 8 ++++++++
drivers/thermal/thermal_helpers.c | 7 +++++--
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
index 8b503fab25bc..fa6cfb8669b3 100644
--- a/drivers/thermal/thermal_aggregation.c
+++ b/drivers/thermal/thermal_aggregation.c
@@ -154,3 +154,8 @@ thermal_zone_aggregate_validate(struct thermal_zone_aggregate *aggregate,
return 0;
}
+
+int thermal_zone_get_temp_aggregate(struct thermal_zone_device *tz, int *temp)
+{
+ return -EOPNOTSUPP;
+}
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 365704584966..34903cfeec33 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -313,6 +313,14 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high);
int thermal_zone_trip_id(const struct thermal_zone_device *tz,
const struct thermal_trip *trip);
int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
+#ifdef CONFIG_THERMAL_AGGREGATION
+int thermal_zone_get_temp_aggregate(struct thermal_zone_device *tz, int *temp);
+#else
+static inline int thermal_zone_get_temp_aggregate(struct thermal_zone_device *tz, int *temp)
+{
+ return -EOPNOTSUPP;
+}
+#endif
void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,
struct thermal_trip *trip, int hyst);
diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c
index b1152ad7acc9..cf84d4c1ef65 100644
--- a/drivers/thermal/thermal_helpers.c
+++ b/drivers/thermal/thermal_helpers.c
@@ -74,7 +74,7 @@ EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev);
* temperature and fill @temp.
*
* Both tz and tz->ops must be valid pointers when calling this function,
- * and the tz->ops.get_temp callback must be provided.
+ * and the tz->ops.get_temp callback must be provided for non-aggregated zones.
* The function must be called under tz->lock.
*
* Return: On success returns 0, an error code otherwise
@@ -87,7 +87,10 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
lockdep_assert_held(&tz->lock);
- ret = tz->ops.get_temp(tz, temp);
+ if (tz->tzp && tz->tzp->aggregate)
+ ret = thermal_zone_get_temp_aggregate(tz, temp);
+ else
+ ret = tz->ops.get_temp(tz, temp);
if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
for_each_trip_desc(tz, td) {
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 06/12] thermal: core: Calculate aggregated thermal zone temperatures
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (4 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 05/12] thermal: core: Dispatch aggregate temperature reads Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 07/12] thermal: core: Initialize aggregation during zone registration Daniel Lezcano
` (5 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Replace the aggregate temperature placeholder with a signed fixed-point
affine calculation. Accumulate source temperatures multiplied by their
coefficients using checked 64-bit arithmetic, divide once by the scale
of 1000, and add the existing offset from thermal_zone_params. Reject
arithmetic overflow and results outside the int range without modifying
the output on failure.
Read each source under its zone lock and preserve temperature emulation
and error handling. Use nonblocking lock acquisition to return -EAGAIN
rather than wait for another zone lock while holding the aggregate lock.
Reject exiting sources and invalid temperature readings.
Keep initialization-time checks in the aggregation validator. Require
validated, initialized entries and stable source references
throughout each read. Aggregations of aggregated zones are not supported
yet; source lifetime and detachment synchronization will be integrated
in subsequent changes.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_aggregation.c | 67 ++++++++++++++++++++++++++-
1 file changed, 66 insertions(+), 1 deletion(-)
diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
index fa6cfb8669b3..05668e53fed8 100644
--- a/drivers/thermal/thermal_aggregation.c
+++ b/drivers/thermal/thermal_aggregation.c
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/errno.h>
+#include <linux/math64.h>
+#include <linux/overflow.h>
#include <linux/units.h>
#include "thermal_core.h"
@@ -155,7 +157,70 @@ thermal_zone_aggregate_validate(struct thermal_zone_aggregate *aggregate,
return 0;
}
+/**
+ * thermal_zone_get_temp_aggregate() - read an aggregated zone temperature
+ * @tz: aggregate zone with a previously validated aggregation description
+ * @temp: output temperature in millicelsius, unchanged on failure
+ *
+ * The caller must hold @tz->lock and keep the aggregation description and each
+ * source zone alive and stable throughout the call. Source references must be
+ * acquired under appropriate lifetime protection before publishing the entries;
+ * the aggregate lock alone does not protect source lifetime or detachment.
+ * The description must have passed thermal_zone_aggregate_validate() before
+ * publication. Aggregations of aggregated zones are not supported yet, and
+ * sources must remain non-aggregated. The destination cannot be a source.
+ *
+ * Source readings are acquired sequentially, not as a simultaneous snapshot.
+ * Each source callback runs under its zone lock, including the usual emulation
+ * handling. Source callbacks must not re-enter a held thermal zone lock.
+ *
+ * Return: 0 on success, a source read error, -EAGAIN for a busy source, -ENODEV
+ * for an exiting source, -ENODATA for an invalid source temperature, or -ERANGE
+ * for arithmetic overflow or a result outside the int range.
+ */
int thermal_zone_get_temp_aggregate(struct thermal_zone_device *tz, int *temp)
{
- return -EOPNOTSUPP;
+ struct thermal_zone_aggregate *aggregate = tz->tzp->aggregate;
+ s64 sum = 0, product;
+ unsigned int i;
+ int source_temp, ret;
+
+ lockdep_assert_held(&tz->lock);
+
+ for (i = 0; i < aggregate->num_entries; i++) {
+ const struct thermal_zone_aggregate_entry *entry = &aggregate->entries[i];
+ struct thermal_zone_device *source = entry->tz;
+
+ /*
+ * The aggregate lock is already held. Do not wait for another
+ * zone lock: return -EAGAIN so the normal update path can retry.
+ */
+ if (!mutex_trylock(&source->lock))
+ return -EAGAIN;
+
+ if (source->state & TZ_STATE_FLAG_EXIT)
+ ret = -ENODEV;
+ else
+ ret = __thermal_zone_get_temp(source, &source_temp);
+
+ mutex_unlock(&source->lock);
+ if (ret)
+ return ret;
+ if (source_temp <= THERMAL_TEMP_INVALID)
+ return -ENODATA;
+
+ /* Cast before multiplying and retain fractional terms until summation. */
+ if (check_mul_overflow((s64)entry->coefficient, (s64)source_temp, &product) ||
+ check_add_overflow(sum, product, &sum))
+ return -ERANGE;
+ }
+
+ /* Divide once, truncating toward zero, then apply the unscaled offset. */
+ sum = div_s64(sum, PERMILLE);
+ if (check_add_overflow(sum, (s64)tz->tzp->offset, &sum) ||
+ sum < INT_MIN || sum > INT_MAX)
+ return -ERANGE;
+
+ *temp = sum;
+ return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 07/12] thermal: core: Initialize aggregation during zone registration
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (5 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 06/12] thermal: core: Calculate aggregated thermal zone temperatures Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 08/12] thermal: of: Separate zone parsing from sensor lookup Daniel Lezcano
` (4 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Recognize aggregation descriptions in thermal_zone_device_register_with_trips()
and call a private thermal_zone_aggregate_register() helper before
publishing the zone. Encapsulate validation followed by coefficient
initialization in thermal_aggregation.c and propagate failures to
the caller.
Allow aggregated zones to omit the driver get_temp callback in registration
and thermal_zone_get_temp(), since their temperature is calculated by the
thermal core. Preserve the callback requirement for non-aggregated zones.
Document that callers must keep the aggregation description and source
zones alive and stable throughout the registered zone lifetime. Hardware
set_trips operations remain rejected by aggregation validation.
Provide a private stub returning -EOPNOTSUPP when thermal aggregation
is disabled, preserving the ordinary thermal zone path.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_aggregation.c | 32 ++++++++++++++++++++++-----
drivers/thermal/thermal_core.c | 14 +++++++++++-
drivers/thermal/thermal_core.h | 11 +++++++++
drivers/thermal/thermal_helpers.c | 2 +-
4 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
index 05668e53fed8..1a7e17757025 100644
--- a/drivers/thermal/thermal_aggregation.c
+++ b/drivers/thermal/thermal_aggregation.c
@@ -23,8 +23,7 @@
* Return: 0 on success or -EINVAL for invalid arguments. Entries are unchanged
* on failure.
*/
-static int __maybe_unused
-thermal_zone_aggregate_init_coefficients(struct thermal_zone_aggregate *aggregate)
+static int thermal_zone_aggregate_init_coefficients(struct thermal_zone_aggregate *aggregate)
{
unsigned int initialized = 0;
unsigned int base, remainder, i;
@@ -110,9 +109,8 @@ thermal_zone_aggregate_init_coefficients(struct thermal_zone_aggregate *aggregat
* specification, aggregated sources or destination set_trips,
* or -ENODEV for missing or exiting sources.
*/
-static int __maybe_unused
-thermal_zone_aggregate_validate(struct thermal_zone_aggregate *aggregate,
- const struct thermal_zone_device_ops *ops)
+static int thermal_zone_aggregate_validate(struct thermal_zone_aggregate *aggregate,
+ const struct thermal_zone_device_ops *ops)
{
unsigned int initialized = 0;
unsigned int i;
@@ -157,6 +155,30 @@ thermal_zone_aggregate_validate(struct thermal_zone_aggregate *aggregate,
return 0;
}
+/**
+ * thermal_zone_aggregate_register() - prepare an aggregation for zone registration
+ * @aggregate: caller-owned aggregation description
+ * @ops: operations of the destination thermal zone
+ *
+ * Validate the description before initializing default coefficients. Call before
+ * publishing the destination zone, without holding thermal zone locks. The
+ * caller retains ownership of the description and source references and must
+ * keep them alive and stable until the destination zone is unregistered.
+ *
+ * Return: 0 on success or an error from validation or coefficient initialization.
+ */
+int thermal_zone_aggregate_register(struct thermal_zone_aggregate *aggregate,
+ const struct thermal_zone_device_ops *ops)
+{
+ int ret;
+
+ ret = thermal_zone_aggregate_validate(aggregate, ops);
+ if (ret)
+ return ret;
+
+ return thermal_zone_aggregate_init_coefficients(aggregate);
+}
+
/**
* thermal_zone_get_temp_aggregate() - read an aggregated zone temperature
* @tz: aggregate zone with a previously validated aggregation description
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 02c2c08077bf..cab2bc1d0ac3 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1431,6 +1431,12 @@ static void thermal_zone_device_release(struct device *dev)
* thermal_zone_device_unregister() must be called when the device is no
* longer needed. The passive cooling depends on the .get_trend() return value.
*
+ * If @tzp contains an aggregation description, validate it and initialize its
+ * coefficients before publication. The caller must keep the description and
+ * referenced source zones alive and stable for the registered zone's lifetime.
+ * Aggregated zones do not require a get_temp callback and cannot use set_trips
+ * to program hardware temperature boundaries.
+ *
* Return: a pointer to the created struct thermal_zone_device or an
* in case of error, an ERR_PTR. Caller must check return value with
* IS_ERR*() helpers.
@@ -1470,7 +1476,7 @@ thermal_zone_device_register_with_trips(const char *type,
return ERR_PTR(-EINVAL);
}
- if (!ops || !ops->get_temp) {
+ if (!ops || (!ops->get_temp && !(tzp && tzp->aggregate))) {
pr_err("Thermal zone device ops not defined or invalid\n");
return ERR_PTR(-EINVAL);
}
@@ -1484,6 +1490,12 @@ thermal_zone_device_register_with_trips(const char *type,
if (thermal_class_unavailable)
return ERR_PTR(-ENODEV);
+ if (tzp && tzp->aggregate) {
+ result = thermal_zone_aggregate_register(tzp->aggregate, ops);
+ if (result)
+ return ERR_PTR(result);
+ }
+
tz = kzalloc_flex(*tz, trips, num_trips);
if (!tz)
return ERR_PTR(-ENOMEM);
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 34903cfeec33..0c1cd3c16cb9 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -321,6 +321,17 @@ static inline int thermal_zone_get_temp_aggregate(struct thermal_zone_device *tz
return -EOPNOTSUPP;
}
#endif
+#ifdef CONFIG_THERMAL_AGGREGATION
+int thermal_zone_aggregate_register(struct thermal_zone_aggregate *aggregate,
+ const struct thermal_zone_device_ops *ops);
+#else
+static inline int
+thermal_zone_aggregate_register(struct thermal_zone_aggregate *aggregate,
+ const struct thermal_zone_device_ops *ops)
+{
+ return -EOPNOTSUPP;
+}
+#endif
void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,
struct thermal_trip *trip, int hyst);
diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c
index cf84d4c1ef65..bf5ff50ecfde 100644
--- a/drivers/thermal/thermal_helpers.c
+++ b/drivers/thermal/thermal_helpers.c
@@ -136,7 +136,7 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
guard(thermal_zone)(tz);
- if (!tz->ops.get_temp)
+ if (!tz->ops.get_temp && !(tz->tzp && tz->tzp->aggregate))
return -EINVAL;
ret = __thermal_zone_get_temp(tz, temp);
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 08/12] thermal: of: Separate zone parsing from sensor lookup
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (6 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 07/12] thermal: core: Initialize aggregation during zone registration Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 09/12] thermal: of: Track registered hardware temperature sources Daniel Lezcano
` (3 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Extract node-based thermal zone registration so subsequent aggregation
support can reuse trip, monitoring, cooling-map and critical-action
handling. Pass initialized zone parameters into the shared helper and
keep legacy slope and offset parsing in the hardware sensor path.
Keep device node ownership in the lookup wrapper and preserve existing
source registration and enablement behavior.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_of.c | 70 +++++++++++++++---------------------
1 file changed, 29 insertions(+), 41 deletions(-)
diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 9e96fb1b033e..710ff25140e7 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -363,55 +363,27 @@ static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
thermal_zone_device_unregister(tz);
}
-/**
- * thermal_of_zone_register - Register a thermal zone with device node
- * sensor
- *
- * The thermal_of_zone_register() parses a device tree given a device
- * node sensor and identifier. It searches for the thermal zone
- * associated to the couple sensor/id and retrieves all the thermal
- * zone properties and registers new thermal zone with those
- * properties.
- *
- * @sensor: A device node pointer corresponding to the sensor in the device tree
- * @id: An integer as sensor identifier
- * @data: A private data to be stored in the thermal zone dedicated private area
- * @ops: A set of thermal sensor ops
- *
- * Return: a valid thermal zone structure pointer on success.
- * - EINVAL: if the device tree thermal description is malformed
- * - ENOMEM: if one structure can not be allocated
- * - Other negative errors are returned by the underlying called functions
- */
-static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
- const struct thermal_zone_device_ops *ops)
+static struct thermal_zone_device *
+thermal_of_zone_register_node(struct device_node *np, void *data,
+ const struct thermal_zone_device_ops *ops,
+ const struct thermal_zone_params *params)
{
struct thermal_zone_device_ops of_ops = *ops;
struct thermal_zone_device *tz;
struct thermal_trip *trips;
- struct thermal_zone_params tzp = {};
- struct device_node *np;
const char *action;
int delay, pdelay;
int ntrips;
int ret;
- np = of_thermal_zone_find(sensor, id);
- if (IS_ERR(np)) {
- if (PTR_ERR(np) != -ENODEV)
- pr_err("Failed to find thermal zone for %pOFP id=%d\n", sensor, id);
- return ERR_CAST(np);
- }
-
trips = thermal_of_trips_init(np, &ntrips);
if (IS_ERR(trips)) {
- pr_err("Failed to parse trip points for %pOFP id=%d\n", sensor, id);
- ret = PTR_ERR(trips);
- goto out_of_node_put;
+ pr_err("Failed to parse trip points for %pOFP\n", np);
+ return ERR_CAST(trips);
}
if (!trips)
- pr_info("No trip points found for %pOFP id=%d\n", sensor, id);
+ pr_info("No trip points found for %pOFP\n", np);
ret = thermal_of_monitor_init(np, &delay, &pdelay);
if (ret) {
@@ -419,8 +391,6 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
goto out_kfree_trips;
}
- thermal_of_parameters_init(np, &tzp);
-
of_ops.should_bind = thermal_of_should_bind;
ret = of_property_read_string(np, "critical-action", &action);
@@ -432,7 +402,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
}
tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
- data, &of_ops, &tzp,
+ data, &of_ops, params,
pdelay, delay);
if (IS_ERR(tz)) {
ret = PTR_ERR(tz);
@@ -440,7 +410,6 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
goto out_kfree_trips;
}
- of_node_put(np);
kfree(trips);
ret = thermal_zone_device_enable(tz);
@@ -455,10 +424,29 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
out_kfree_trips:
kfree(trips);
-out_of_node_put:
+ return ERR_PTR(ret);
+}
+
+static struct thermal_zone_device *
+thermal_of_zone_register(struct device_node *sensor, int id, void *data,
+ const struct thermal_zone_device_ops *ops)
+{
+ struct thermal_zone_params tzp = {};
+ struct thermal_zone_device *tz;
+ struct device_node *np;
+
+ np = of_thermal_zone_find(sensor, id);
+ if (IS_ERR(np)) {
+ if (PTR_ERR(np) != -ENODEV)
+ pr_err("Failed to find thermal zone for %pOFP id=%d\n", sensor, id);
+ return ERR_CAST(np);
+ }
+
+ thermal_of_parameters_init(np, &tzp);
+ tz = thermal_of_zone_register_node(np, data, ops, &tzp);
of_node_put(np);
- return ERR_PTR(ret);
+ return tz;
}
static void devm_thermal_of_zone_release(struct device *dev, void *res)
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 09/12] thermal: of: Track registered hardware temperature sources
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (7 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 08/12] thermal: of: Separate zone parsing from sensor lookup Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 10/12] thermal: of: Instantiate aggregates after their source zones Daniel Lezcano
` (2 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Record successfully enabled OF source zones by provider node and sensor
specifier ID. Serialize source publication and removal with an OF-private
lifecycle mutex and retain the provider node while its mapping exists.
Reject ambiguous duplicate provider and ID mappings. Remove the mapping
before unregistering the hardware zone, providing an ownership boundary
for subsequent OF aggregate consumers.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_of.c | 79 ++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 710ff25140e7..1fa7e29f5bc8 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -18,6 +18,75 @@
#include "thermal_core.h"
+#ifdef CONFIG_THERMAL_AGGREGATION
+struct thermal_of_source {
+ struct list_head node;
+ struct device_node *provider;
+ int id;
+ /*
+ * Borrowed from the OF zone registration. The mapping is removed under
+ * thermal_of_lock before the zone is unregistered. Aggregate consumers
+ * acquire their own references while holding the same lock.
+ */
+ struct thermal_zone_device *tz;
+};
+
+/*
+ * OF lifecycle operations are serialized outside thermal zone locks. Thermal
+ * callbacks must not acquire this mutex. Source removal holds it until all OF
+ * consumers have released their references, before calling core unregister.
+ */
+static DEFINE_MUTEX(thermal_of_lock);
+static LIST_HEAD(thermal_of_sources);
+
+static int thermal_of_source_add(struct device_node *provider, int id,
+ struct thermal_zone_device *tz)
+{
+ struct thermal_of_source *source, *pos;
+
+ source = kzalloc_obj(*source);
+ if (!source)
+ return -ENOMEM;
+
+ guard(mutex)(&thermal_of_lock);
+ list_for_each_entry(pos, &thermal_of_sources, node) {
+ if (pos->provider == provider && pos->id == id) {
+ kfree(source);
+ return -EEXIST;
+ }
+ }
+ source->provider = of_node_get(provider);
+ source->id = id;
+ source->tz = tz;
+ list_add_tail(&source->node, &thermal_of_sources);
+ return 0;
+}
+
+static void thermal_of_source_remove(struct thermal_zone_device *tz)
+{
+ struct thermal_of_source *source;
+
+ guard(mutex)(&thermal_of_lock);
+ list_for_each_entry(source, &thermal_of_sources, node) {
+ if (source->tz != tz)
+ continue;
+ list_del(&source->node);
+ of_node_put(source->provider);
+ kfree(source);
+ break;
+ }
+}
+
+#else
+static int thermal_of_source_add(struct device_node *provider, int id,
+ struct thermal_zone_device *tz)
+{
+ return 0;
+}
+
+static void thermal_of_source_remove(struct thermal_zone_device *tz) {}
+#endif
+
/*** functions parsing device tree nodes ***/
/*
@@ -359,6 +428,7 @@ static bool thermal_of_should_bind(struct thermal_zone_device *tz,
*/
static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
{
+ thermal_of_source_remove(tz);
thermal_zone_device_disable(tz);
thermal_zone_device_unregister(tz);
}
@@ -446,6 +516,15 @@ thermal_of_zone_register(struct device_node *sensor, int id, void *data,
tz = thermal_of_zone_register_node(np, data, ops, &tzp);
of_node_put(np);
+ if (!IS_ERR(tz)) {
+ int ret = thermal_of_source_add(sensor, id, tz);
+
+ if (ret) {
+ thermal_of_zone_unregister(tz);
+ return ERR_PTR(ret);
+ }
+ }
+
return tz;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 10/12] thermal: of: Instantiate aggregates after their source zones
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (8 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 09/12] thermal: of: Track registered hardware temperature sources Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 11/12] dt-bindings: thermal: Describe multi-source zone aggregation Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 12/12] thermal: testing: Add KUnit coverage for " Daniel Lezcano
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano, open list
Discover multi-source thermal zones at late init and resolve their
hardware provider and sensor ID references to registered source zones.
Retry pending descriptions synchronously after successful source
publication, without workqueues or changes to sensor driver APIs.
Serialize creation and removal with the OF lifecycle mutex. Acquire
source references while filling aggregation entries, unregister dependent
aggregates before source teardown, and release references and allocations
on failure or removal. Keep descriptions pending for source module reload.
Use the shared node parser with core aggregation operations. Preserve
legacy single-source slope and offset calibration. Missing multi-source
coefficients request averaging defaults; explicit integer multipliers are
converted to thousandths with overflow checks and the offset is unscaled.
Reject partial arrays and explicit zero multipliers, which cannot encode
presence under the current aggregation initialization contract.
Skip multi-source nodes during ordinary hardware zone lookup so they
cannot be bound directly to an individual sensor callback.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/thermal_of.c | 217 ++++++++++++++++++++++++++++++++++-
1 file changed, 216 insertions(+), 1 deletion(-)
diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 1fa7e29f5bc8..64ae7769edcb 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -10,8 +10,11 @@
#include <linux/err.h>
#include <linux/export.h>
+#include <linux/init.h>
#include <linux/of.h>
#include <linux/slab.h>
+#include <linux/overflow.h>
+#include <linux/units.h>
#include <linux/thermal.h>
#include <linux/types.h>
#include <linux/string.h>
@@ -31,6 +34,14 @@ struct thermal_of_source {
struct thermal_zone_device *tz;
};
+struct thermal_of_aggregate {
+ struct list_head node;
+ struct device_node *np;
+ struct thermal_zone_aggregate aggregate;
+ struct thermal_zone_device *tz;
+ bool failed;
+};
+
/*
* OF lifecycle operations are serialized outside thermal zone locks. Thermal
* callbacks must not acquire this mutex. Source removal holds it until all OF
@@ -38,6 +49,11 @@ struct thermal_of_source {
*/
static DEFINE_MUTEX(thermal_of_lock);
static LIST_HEAD(thermal_of_sources);
+static LIST_HEAD(thermal_of_aggregates);
+static bool thermal_of_aggregates_ready;
+
+static void thermal_of_aggregate_retry(void);
+static void thermal_of_aggregate_release(struct thermal_of_aggregate *aggregate);
static int thermal_of_source_add(struct device_node *provider, int id,
struct thermal_zone_device *tz)
@@ -59,18 +75,29 @@ static int thermal_of_source_add(struct device_node *provider, int id,
source->id = id;
source->tz = tz;
list_add_tail(&source->node, &thermal_of_sources);
+ thermal_of_aggregate_retry();
return 0;
}
static void thermal_of_source_remove(struct thermal_zone_device *tz)
{
struct thermal_of_source *source;
+ struct thermal_of_aggregate *aggregate;
+ unsigned int i;
guard(mutex)(&thermal_of_lock);
list_for_each_entry(source, &thermal_of_sources, node) {
if (source->tz != tz)
continue;
list_del(&source->node);
+ list_for_each_entry(aggregate, &thermal_of_aggregates, node) {
+ for (i = 0; i < aggregate->aggregate.num_entries; i++) {
+ if (aggregate->aggregate.entries[i].tz == tz) {
+ thermal_of_aggregate_release(aggregate);
+ break;
+ }
+ }
+ }
of_node_put(source->provider);
kfree(source);
break;
@@ -217,6 +244,9 @@ static struct device_node *of_thermal_zone_find(struct device_node *sensor, int
pr_err("%pOFP: missing thermal sensor\n", child);
return ERR_PTR(-EINVAL);
}
+ /* Multi-source nodes are instantiated by the OF aggregate manager. */
+ if (count > 1)
+ continue;
for (i = 0; i < count; i++) {
@@ -486,7 +516,8 @@ thermal_of_zone_register_node(struct device_node *np, void *data,
if (ret) {
pr_err("Failed to enable thermal zone '%s', id=%d: %d\n",
tz->type, tz->id, ret);
- thermal_of_zone_unregister(tz);
+ thermal_zone_device_disable(tz);
+ thermal_zone_device_unregister(tz);
return ERR_PTR(ret);
}
@@ -528,6 +559,190 @@ thermal_of_zone_register(struct device_node *sensor, int id, void *data,
return tz;
}
+#ifdef CONFIG_THERMAL_AGGREGATION
+/* All aggregate construction and destruction runs under thermal_of_lock. */
+static void thermal_of_aggregate_release(struct thermal_of_aggregate *aggregate)
+{
+ unsigned int i;
+
+ lockdep_assert_held(&thermal_of_lock);
+ if (aggregate->tz) {
+ /* Do not recurse into OF source removal while holding the OF mutex. */
+ thermal_zone_device_disable(aggregate->tz);
+ thermal_zone_device_unregister(aggregate->tz);
+ aggregate->tz = NULL;
+ }
+ for (i = 0; i < aggregate->aggregate.num_entries; i++)
+ thermal_zone_device_put(aggregate->aggregate.entries[i].tz);
+ kfree(aggregate->aggregate.entries);
+ aggregate->aggregate.entries = NULL;
+ aggregate->aggregate.num_entries = 0;
+}
+
+static struct thermal_zone_device *
+thermal_of_source_get(const struct of_phandle_args *spec)
+{
+ struct thermal_of_source *source;
+ int id = spec->args_count ? spec->args[0] : 0;
+
+ lockdep_assert_held(&thermal_of_lock);
+ /* The existing OF source registration API identifies sensors by one ID. */
+ if (spec->args_count > 1)
+ return ERR_PTR(-EINVAL);
+
+ list_for_each_entry(source, &thermal_of_sources, node)
+ if (source->provider == spec->np && source->id == id)
+ return thermal_zone_device_get(source->tz);
+
+ return ERR_PTR(-EPROBE_DEFER);
+}
+
+static int thermal_of_aggregate_create(struct thermal_of_aggregate *aggregate)
+{
+ struct thermal_zone_device_ops ops = {};
+ struct thermal_zone_params tzp = { .no_hwmon = true };
+ struct device_node *np = aggregate->np;
+ struct thermal_zone_aggregate *desc = &aggregate->aggregate;
+ int count, ncoef, ret, i;
+
+ lockdep_assert_held(&thermal_of_lock);
+ count = of_count_phandle_with_args(np, "thermal-sensors", "#thermal-sensor-cells");
+ if (count < 2)
+ return -EINVAL;
+
+ desc->entries = kcalloc(count, sizeof(*desc->entries), GFP_KERNEL);
+ if (!desc->entries)
+ return -ENOMEM;
+
+ for (i = 0; i < count; i++) {
+ struct of_phandle_args spec;
+ struct thermal_zone_device *source;
+
+ ret = of_parse_phandle_with_args(np, "thermal-sensors", "#thermal-sensor-cells",
+ i, &spec);
+ if (ret)
+ goto release;
+ source = thermal_of_source_get(&spec);
+ of_node_put(spec.np);
+ if (IS_ERR(source)) {
+ ret = PTR_ERR(source);
+ goto release;
+ }
+ desc->entries[i].tz = source;
+ desc->num_entries++;
+ }
+
+ /*
+ * Missing multipliers request the core's averaging defaults. Explicit DT
+ * multipliers retain their unscaled integer meaning: convert to internal
+ * thousandths, checking overflow, and leave the final offset unscaled.
+ * Legacy single-source calibration never passes through this path.
+ */
+ if (of_find_property(np, "coefficients", NULL)) {
+ ncoef = of_property_count_u32_elems(np, "coefficients");
+ if (ncoef != count && ncoef != count + 1) {
+ ret = -EINVAL;
+ goto release;
+ }
+ for (i = 0; i < count; i++) {
+ u32 raw;
+
+ ret = of_property_read_u32_index(np, "coefficients", i, &raw);
+ if (ret)
+ goto release;
+ if (check_mul_overflow((s32)raw, (s32)PERMILLE,
+ &desc->entries[i].coefficient)) {
+ ret = -ERANGE;
+ goto release;
+ }
+ /* Zero cannot encode presence in the core initializer. */
+ if (!desc->entries[i].coefficient) {
+ ret = -EINVAL;
+ goto release;
+ }
+ }
+ if (ncoef == count + 1) {
+ u32 raw;
+
+ ret = of_property_read_u32_index(np, "coefficients", count, &raw);
+ if (ret)
+ goto release;
+ tzp.offset = (s32)raw;
+ }
+ }
+ of_property_read_u32(np, "sustainable-power", &tzp.sustainable_power);
+ tzp.aggregate = desc;
+ aggregate->tz = thermal_of_zone_register_node(np, NULL, &ops, &tzp);
+ if (IS_ERR(aggregate->tz)) {
+ ret = PTR_ERR(aggregate->tz);
+ aggregate->tz = NULL;
+ goto release;
+ }
+ return 0;
+
+release:
+ thermal_of_aggregate_release(aggregate);
+ return ret;
+}
+
+static void thermal_of_aggregate_retry(void)
+{
+ struct thermal_of_aggregate *aggregate;
+
+ lockdep_assert_held(&thermal_of_lock);
+ if (!thermal_of_aggregates_ready)
+ return;
+
+ list_for_each_entry(aggregate, &thermal_of_aggregates, node) {
+ int ret;
+
+ if (aggregate->tz || aggregate->failed)
+ continue;
+ ret = thermal_of_aggregate_create(aggregate);
+ if (ret && ret != -EPROBE_DEFER && ret != -ENOMEM) {
+ pr_err("%pOF: failed to create aggregate: %d\n", aggregate->np, ret);
+ aggregate->failed = true;
+ }
+ }
+}
+
+static int __init thermal_of_aggregate_init(void)
+{
+ struct device_node *np __free(device_node) = of_find_node_by_name(NULL, "thermal-zones");
+
+ if (!np)
+ return 0;
+
+ guard(mutex)(&thermal_of_lock);
+ for_each_available_child_of_node_scoped(np, child) {
+ struct thermal_of_aggregate *aggregate;
+ int count;
+
+ count = of_count_phandle_with_args(child, "thermal-sensors",
+ "#thermal-sensor-cells");
+ if (count <= 1)
+ continue;
+ aggregate = kzalloc_obj(*aggregate);
+ if (!aggregate) {
+ struct thermal_of_aggregate *pos, *next;
+
+ list_for_each_entry_safe(pos, next, &thermal_of_aggregates, node) {
+ list_del(&pos->node);
+ of_node_put(pos->np);
+ kfree(pos);
+ }
+ return -ENOMEM;
+ }
+ aggregate->np = of_node_get(child);
+ list_add_tail(&aggregate->node, &thermal_of_aggregates);
+ }
+ thermal_of_aggregates_ready = true;
+ thermal_of_aggregate_retry();
+ return 0;
+}
+late_initcall(thermal_of_aggregate_init);
+#endif /* CONFIG_THERMAL_AGGREGATION */
+
static void devm_thermal_of_zone_release(struct device *dev, void *res)
{
thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 11/12] dt-bindings: thermal: Describe multi-source zone aggregation
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (9 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 10/12] thermal: of: Instantiate aggregates after their source zones Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 12/12] thermal: testing: Add KUnit coverage for " Daniel Lezcano
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano,
Conor Dooley,
open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
open list
Allow multiple hardware thermal sensor references and associate supplied
coefficients positionally with those references. Use signed cells to
represent existing negative calibration values and aggregate offsets.
Document approximate averaging when multi-source coefficients are absent,
with unity gain for one source. Explicit multi-source multipliers retain
the unscaled affine interpretation and are not normalized. Require
nonzero explicit multipliers to distinguish supplied values from the
core initializer default request.
Preserve existing single-source sensor-specific slope and offset
interpretations, including zero values and driver-specific scaling.
Add examples of averaging and explicit signed multi-source combinations.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
.../bindings/thermal/thermal-zones.yaml | 46 ++++++++++++++++---
1 file changed, 39 insertions(+), 7 deletions(-)
diff --git a/Documentation/devicetree/bindings/thermal/thermal-zones.yaml b/Documentation/devicetree/bindings/thermal/thermal-zones.yaml
index 72e419124f98..10cf1fb8cb9a 100644
--- a/Documentation/devicetree/bindings/thermal/thermal-zones.yaml
+++ b/Documentation/devicetree/bindings/thermal/thermal-zones.yaml
@@ -95,10 +95,13 @@ patternProperties:
thermal-sensors:
$ref: /schemas/types.yaml#/definitions/phandle-array
- maxItems: 1
- description:
- The thermal sensor phandle and sensor specifier used to monitor this
- thermal zone.
+ description: |
+ A list of hardware thermal sensor provider phandles and sensor
+ specifiers used to monitor this thermal zone. Each entry uses the
+ number of specifier cells defined by its provider's
+ #thermal-sensor-cells. Multiple entries describe an aggregation of
+ the corresponding source zone temperatures. Thermal zone nodes are
+ not referenced by this property.
coefficients:
$ref: /schemas/types.yaml#/definitions/int32-array
@@ -110,9 +113,23 @@ patternProperties:
z = c0 * x0 + c1 * x1 + ... + c(n-1) * x(n-1) + cn
where c0, c1, .., cn are the coefficients.
- Coefficients default to 1 in case this property is not specified. The
- coefficients are ordered and are matched with sensors by means of the
- sensor ID. Additional coefficients are interpreted as constant offset.
+ For multiple sources, the first n coefficients correspond to entries
+ in thermal-sensors in list order, not to provider-specific sensor IDs.
+ Supply all n multipliers, optionally followed by the constant offset
+ in millicelsius. Multipliers are signed nonzero unscaled integers;
+ there is no implicit divisor or normalization for supplied values.
+ An omitted offset is zero.
+
+ When coefficients are absent for a multi-source zone, the temperature
+ is the approximate arithmetic mean of the source zone temperatures.
+ Contributions are distributed as evenly as possible in source order
+ with a total gain of one. A single source consequently has a default
+ multiplier of one.
+
+ Existing single-source hardware zones retain their sensor-specific
+ slope and offset calibration interpretation, including signed or zero
+ values and any sensor-specific scaling. Their default slope is one
+ and default offset is zero.
sustainable-power:
$ref: /schemas/types.yaml#/definitions/uint32
@@ -339,6 +356,21 @@ examples:
/* ... */
+ // With no coefficients, use approximately equal contributions.
+ skin-thermal {
+ polling-delay-passive = <250>;
+ polling-delay = <1000>;
+ thermal-sensors = <&tsens0 1>, <&tsens0 5>;
+ };
+
+ // Explicit multipliers are not normalized: board = 2*T0 - T1 - 1000.
+ board-thermal {
+ polling-delay-passive = <250>;
+ polling-delay = <1000>;
+ thermal-sensors = <&tsens0 1>, <&tsens0 5>;
+ coefficients = <2 (-1) (-1000)>;
+ };
+
gpu-top-thermal {
polling-delay-passive = <250>;
polling-delay = <1000>;
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v1 12/12] thermal: testing: Add KUnit coverage for zone aggregation
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
` (10 preceding siblings ...)
2026-11-19 9:46 ` [PATCH v1 11/12] dt-bindings: thermal: Describe multi-source zone aggregation Daniel Lezcano
@ 2026-11-19 9:46 ` Daniel Lezcano
11 siblings, 0 replies; 13+ messages in thread
From: Daniel Lezcano @ 2026-11-19 9:46 UTC (permalink / raw)
To: daniel.lezcano, rafael
Cc: Zhang Rui, Lukasz Luba, Rob Herring, Krzysztof Kozlowski,
Anvesh Jain P, Ulf Hansson, linux-pm, Daniel Lezcano,
Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
open list, open list:CLANG/LLVM BUILD SUPPORT
Exercise thermal zone aggregation without platform hardware by adding
core and OF KUnit suites under drivers/thermal/testing/.
Cover default coefficient distributions and their limits, validation of
source zones and destination operations, and registration with core-owned
temperature calculation. Check signed fixed-point arithmetic, fractional
contributions, negative rounding, zero-sum combinations, source errors,
invalid readings, accumulator overflow and final temperature range checks.
Verify that failed calculations preserve the output and busy source locks
return -EAGAIN. Keep legacy source temperature and slope/offset checks.
Use a test-managed OF overlay to exercise deferred aggregate creation,
synchronous retries, duplicate source rejection, zero-cell providers,
signed coefficient conversion and malformed descriptions. Verify cleanup
after registration failure, destruction of dependent aggregates before
source removal, and recreation when a source returns. Coordinate reader
and removal threads to exercise teardown while a source callback is active.
Keep both suites independent of the debugfs testing facility. Include core
and OF suites in their implementation files under dedicated KUnit
options to access static helpers without exporting them.
Provide a UML configuration with lockdep and document execution and the
limits of synthetic coverage.
All 23 cases pass on UML with lockdep, including a run with UBSAN bounds
checking. GCC and Clang x86-64 thermal subsystem builds with W=1 also pass.
Initial late-init discovery and real hardware or module loading are not
covered by these tests.
Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
drivers/thermal/Kconfig | 23 ++
drivers/thermal/Makefile | 1 +
drivers/thermal/testing/.kunitconfig | 11 +
drivers/thermal/testing/README.rst | 34 ++
.../thermal/testing/thermal_aggregation.dtso | 53 +++
.../testing/thermal_aggregation_test.c | 351 ++++++++++++++++++
.../testing/thermal_of_aggregation_test.c | 330 ++++++++++++++++
drivers/thermal/thermal_aggregation.c | 4 +
drivers/thermal/thermal_of.c | 4 +
9 files changed, 811 insertions(+)
create mode 100644 drivers/thermal/testing/.kunitconfig
create mode 100644 drivers/thermal/testing/README.rst
create mode 100644 drivers/thermal/testing/thermal_aggregation.dtso
create mode 100644 drivers/thermal/testing/thermal_aggregation_test.c
create mode 100644 drivers/thermal/testing/thermal_of_aggregation_test.c
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index b5185084eac1..f0023fb7e693 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -57,6 +57,29 @@ config THERMAL_CORE_TESTING
with trip points in order to exercise the thermal core
functionality in a controlled way.
+config THERMAL_AGGREGATION_KUNIT_TEST
+ bool "KUnit tests for thermal zone aggregation" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y && THERMAL_AGGREGATION
+ default KUNIT_ALL_TESTS
+ help
+ Test coefficient initialization, validation, temperature arithmetic,
+ and registration of aggregated thermal zones with synthetic sources.
+ No thermal hardware is required.
+ If unsure, say N.
+
+config THERMAL_OF_AGGREGATION_KUNIT_TEST
+ bool "KUnit tests for OF thermal aggregation" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y && THERMAL_AGGREGATION
+ depends on THERMAL_OF && OF_OVERLAY && OF_EARLY_FLATTREE
+ default KUNIT_ALL_TESTS
+ help
+ Test OF aggregation with a test-managed device tree overlay.
+ Verify source lookup and deferred aggregate creation.
+ Cover coefficient parsing, source removal and recreation.
+ These tests use synthetic sources without thermal hardware.
+ Run these tests with KUnit's OF overlay support enabled.
+ If unsure, say N.
+
config THERMAL_EMERGENCY_POWEROFF_DELAY_MS
int "Emergency poweroff delay in milli-seconds"
default 0
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index d059ca528908..38951f02aa3a 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -8,6 +8,7 @@ thermal_sys-y += thermal_core.o thermal_sysfs.o
thermal_sys-y += thermal_trip.o thermal_helpers.o
thermal_sys-y += thermal_thresholds.o
thermal_sys-$(CONFIG_THERMAL_AGGREGATION) += thermal_aggregation.o
+thermal_sys-$(CONFIG_THERMAL_OF_AGGREGATION_KUNIT_TEST) += testing/thermal_aggregation.dtbo.o
# netlink interface to manage the thermal framework
thermal_sys-$(CONFIG_THERMAL_NETLINK) += thermal_netlink.o
diff --git a/drivers/thermal/testing/.kunitconfig b/drivers/thermal/testing/.kunitconfig
new file mode 100644
index 000000000000..d61242498bff
--- /dev/null
+++ b/drivers/thermal/testing/.kunitconfig
@@ -0,0 +1,11 @@
+CONFIG_KUNIT=y
+CONFIG_THERMAL=y
+CONFIG_THERMAL_AGGREGATION=y
+CONFIG_THERMAL_AGGREGATION_KUNIT_TEST=y
+CONFIG_OF=y
+CONFIG_OF_OVERLAY=y
+CONFIG_OF_EARLY_FLATTREE=y
+CONFIG_THERMAL_OF=y
+CONFIG_THERMAL_OF_AGGREGATION_KUNIT_TEST=y
+CONFIG_DEBUG_KERNEL=y
+CONFIG_PROVE_LOCKING=y
diff --git a/drivers/thermal/testing/README.rst b/drivers/thermal/testing/README.rst
new file mode 100644
index 000000000000..039809822c78
--- /dev/null
+++ b/drivers/thermal/testing/README.rst
@@ -0,0 +1,34 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Thermal aggregation KUnit tests
+==============================
+
+Run the core and OF suites on UML from the kernel source directory::
+
+ ./tools/testing/kunit/kunit.py run \
+ --kunitconfig=drivers/thermal/testing/.kunitconfig
+
+The configuration enables lockdep. Neither suite requires thermal hardware
+or the debugfs-based CONFIG_THERMAL_CORE_TESTING facility.
+Both suites require CONFIG_THERMAL_AGGREGATION.
+
+The core suite covers default coefficients, validation, fixed-point arithmetic,
+registration and source lock contention. Arithmetic cases include signed and
+zero-sum coefficients, negative temperatures, rounding, read failures, overflow
+and output preservation on failure. Zero coefficients are tested in the read
+path only: before initialization they denote unspecified coefficients.
+
+The OF suite applies a test-managed overlay in /thermal-kunit, outside the
+platform's /thermal-zones. It exercises deferred creation, provider lookup,
+zero-cell providers, coefficient conversion, registration failure cleanup,
+source removal and recreation, and a temperature read racing with source
+removal. Source and consumer zones are destroyed before removing the overlay.
+The suite temporarily enables retries because UML's empty root has no
+/thermal-zones node for the production late-init scan.
+
+Core tests are included by thermal_aggregation.c and OF tests by thermal_of.c
+to access static helpers without exporting them. Suites and overlay data are
+enabled only by their KUnit
+configuration options. These tests do not exercise platform-specific hardware,
+the initial late-init discovery scan, or real module loading. Reload is modeled
+by unregistering and recreating a source zone with the same provider and ID.
diff --git a/drivers/thermal/testing/thermal_aggregation.dtso b/drivers/thermal/testing/thermal_aggregation.dtso
new file mode 100644
index 000000000000..b8ce37602036
--- /dev/null
+++ b/drivers/thermal/testing/thermal_aggregation.dtso
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+&{/} {
+ thermal-kunit {
+ provider: sensor {
+ #thermal-sensor-cells = <1>;
+ };
+ provider_zero: sensor-zero {
+ #thermal-sensor-cells = <0>;
+ };
+ zero-cells {
+ thermal-sensors = <&provider_zero>, <&provider 1>;
+ polling-delay = <0>;
+ polling-delay-passive = <0>;
+ };
+
+ average {
+ thermal-sensors = <&provider 0>, <&provider 1>;
+ polling-delay = <0>;
+ polling-delay-passive = <0>;
+ };
+
+ explicit {
+ thermal-sensors = <&provider 0>, <&provider 1>;
+ coefficients = <2 (-1) (-1000)>;
+ polling-delay = <0>;
+ polling-delay-passive = <0>;
+ };
+
+ partial {
+ thermal-sensors = <&provider 0>, <&provider 1>;
+ coefficients = <1>;
+ };
+
+ zero {
+ thermal-sensors = <&provider 0>, <&provider 1>;
+ coefficients = <1 0>;
+ };
+
+ overflow {
+ thermal-sensors = <&provider 0>, <&provider 1>;
+ coefficients = <2147483647 1>;
+ };
+
+ bad-delay {
+ thermal-sensors = <&provider 0>, <&provider 1>;
+ polling-delay = <100>;
+ polling-delay-passive = <200>;
+ };
+ };
+};
diff --git a/drivers/thermal/testing/thermal_aggregation_test.c b/drivers/thermal/testing/thermal_aggregation_test.c
new file mode 100644
index 000000000000..6a5f3fe41db6
--- /dev/null
+++ b/drivers/thermal/testing/thermal_aggregation_test.c
@@ -0,0 +1,351 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <kunit/test.h>
+#include <linux/kthread.h>
+
+#include "../thermal_core.h"
+
+struct aggregation_source {
+ int temperature;
+ int error;
+ unsigned int reads;
+};
+
+struct aggregation_fixture {
+ struct thermal_zone_device *sources[3];
+ struct aggregation_source data[3];
+ struct thermal_zone_device *destination;
+ struct thermal_zone_aggregate_entry entries[3];
+ struct thermal_zone_aggregate aggregate;
+ struct thermal_zone_params params;
+};
+
+static int aggregation_source_temp(struct thermal_zone_device *tz, int *temp)
+{
+ struct aggregation_source *source = thermal_zone_device_priv(tz);
+
+ lockdep_assert_held(&tz->lock);
+ source->reads++;
+ if (source->error)
+ return source->error;
+ *temp = source->temperature;
+ return 0;
+}
+
+static const struct thermal_zone_device_ops aggregation_source_ops = {
+ .get_temp = aggregation_source_temp,
+};
+
+static const struct thermal_zone_device_ops aggregation_destination_ops;
+
+static void aggregation_cleanup(void *data)
+{
+ struct aggregation_fixture *fixture = data;
+ unsigned int i;
+
+ if (fixture->destination)
+ thermal_zone_device_unregister(fixture->destination);
+ for (i = 0; i < ARRAY_SIZE(fixture->sources); i++)
+ if (fixture->sources[i])
+ thermal_zone_device_unregister(fixture->sources[i]);
+}
+
+static int aggregation_init(struct kunit *test)
+{
+ const struct thermal_zone_params params = { .no_hwmon = true };
+ struct aggregation_fixture *fixture;
+ unsigned int i;
+
+ fixture = kunit_kzalloc(test, sizeof(*fixture), GFP_KERNEL);
+ if (!fixture)
+ return -ENOMEM;
+ test->priv = fixture;
+ if (kunit_add_action_or_reset(test, aggregation_cleanup, fixture))
+ return -ENOMEM;
+ for (i = 0; i < ARRAY_SIZE(fixture->sources); i++) {
+ struct thermal_zone_device *tz;
+
+ fixture->data[i].temperature = 20000 + i * 10000;
+ tz = thermal_zone_device_register_with_trips("kunit-source", NULL, 0,
+ &fixture->data[i],
+ &aggregation_source_ops,
+ ¶ms, 0, 0);
+ if (IS_ERR(tz))
+ return PTR_ERR(tz);
+ fixture->sources[i] = tz;
+ fixture->entries[i].tz = tz;
+ }
+ fixture->aggregate.entries = fixture->entries;
+ fixture->aggregate.num_entries = 3;
+ fixture->params.no_hwmon = true;
+ fixture->params.aggregate = &fixture->aggregate;
+ return 0;
+}
+
+static void aggregation_defaults_test(struct kunit *test)
+{
+ static const unsigned int counts[] = { 1, 2, 3, 4, 5, 6, 1000 };
+ static const s32 six[] = { 167, 167, 167, 167, 166, 166 };
+ struct thermal_zone_aggregate aggregate = {};
+ unsigned int c, i;
+
+ aggregate.entries = kunit_kcalloc(test, 1001, sizeof(*aggregate.entries), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, aggregate.entries);
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_init_coefficients(NULL), -EINVAL);
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_init_coefficients(&aggregate), -EINVAL);
+ for (c = 0; c < ARRAY_SIZE(counts); c++) {
+ s32 sum = 0;
+
+ memset(aggregate.entries, 0, 1001 * sizeof(*aggregate.entries));
+ aggregate.num_entries = counts[c];
+ KUNIT_ASSERT_EQ(test, thermal_zone_aggregate_init_coefficients(&aggregate), 0);
+ for (i = 0; i < aggregate.num_entries; i++) {
+ s32 value = aggregate.entries[i].coefficient;
+
+ sum += value;
+ KUNIT_EXPECT_GT(test, value, 0);
+ if (i)
+ KUNIT_EXPECT_LE(test,
+ aggregate.entries[i - 1].coefficient - value, 1);
+ if (counts[c] == 3)
+ KUNIT_EXPECT_EQ(test, value, i ? 333 : 334);
+ if (counts[c] == 6)
+ KUNIT_EXPECT_EQ(test, value, six[i]);
+ }
+ KUNIT_EXPECT_EQ(test, sum, 1000);
+ }
+ memset(aggregate.entries, 0, 1001 * sizeof(*aggregate.entries));
+ aggregate.num_entries = 1001;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_init_coefficients(&aggregate), -EINVAL);
+ KUNIT_EXPECT_EQ(test, aggregate.entries[0].coefficient, 0);
+}
+
+static int aggregation_set_trips(struct thermal_zone_device *tz, int low, int high)
+{
+ return 0;
+}
+
+static void aggregation_validation_test(struct kunit *test)
+{
+ struct aggregation_fixture *fixture = test->priv;
+ struct thermal_zone_device_ops ops = {};
+ struct thermal_zone_params nested = { .aggregate = &fixture->aggregate };
+ struct thermal_zone_device *source = fixture->sources[0];
+ struct thermal_zone_params *saved_params = source->tzp;
+
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(NULL, &ops), -EINVAL);
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, NULL), -EINVAL);
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), 0);
+ fixture->entries[0].coefficient = 500;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), -EINVAL);
+ fixture->entries[1].coefficient = -500;
+ fixture->entries[2].coefficient = 2000;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), 0);
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_init_coefficients(&fixture->aggregate), 0);
+ KUNIT_EXPECT_EQ(test, fixture->entries[0].coefficient, 500);
+ KUNIT_EXPECT_EQ(test, fixture->entries[1].coefficient, -500);
+ ops.set_trips = aggregation_set_trips;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), -EINVAL);
+ ops.set_trips = NULL;
+ fixture->entries[0].tz = NULL;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), -ENODEV);
+ fixture->entries[0].tz = source;
+ source->tzp = &nested;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), -EINVAL);
+ source->tzp = saved_params;
+ source->state |= TZ_STATE_FLAG_EXIT;
+ KUNIT_EXPECT_EQ(test, thermal_zone_aggregate_validate(&fixture->aggregate, &ops), -ENODEV);
+ source->state &= ~TZ_STATE_FLAG_EXIT;
+}
+
+struct aggregation_case {
+ const char *name;
+ unsigned int count;
+ s32 coefficients[3];
+ int temperatures[3];
+ int offset;
+ int error;
+ int expected;
+ int result;
+};
+
+static const struct aggregation_case aggregation_cases[] = {
+ { "single", 1, { 1000 }, { 42000 }, 0, 0, 42000, 0 },
+ { "affine", 2, { 700, 500 }, { 20000, 30000 }, -2000, 0, 27000, 0 },
+ { "fractional", 2, { 250, 750 }, { 10000, 20000 }, 0, 0, 17500, 0 },
+ { "divide once", 2, { 500, 500 }, { 1, 1 }, 0, 0, 1, 0 },
+ { "negative rounding", 1, { -500 }, { 3 }, -2, 0, -3, 0 },
+ { "negative source", 1, { 1500 }, { -10000 }, 0, 0, -15000, 0 },
+ { "zero sum", 2, { 500, -500 }, { 20000, 10000 }, 0, 0, 5000, 0 },
+ { "zero multiplier at read time", 2, { 0, 1000 }, { 20000, 30000 }, 0, 0, 30000, 0 },
+ { "read error", 2, { 1000, 1000 }, { 20000, 30000 }, 0, -EIO, 0, -EIO },
+ { "invalid reading", 1, { 1000 }, { THERMAL_TEMP_INVALID }, 0, 0, 0, -ENODATA },
+ { "positive overflow", 3, { S32_MAX, S32_MAX, S32_MAX },
+ { INT_MAX, INT_MAX, INT_MAX }, 0, 0, 0, -ERANGE },
+ { "negative overflow", 3, { S32_MIN, S32_MIN, S32_MIN },
+ { INT_MAX, INT_MAX, INT_MAX }, 0, 0, 0, -ERANGE },
+ { "upper range", 1, { 1000 }, { INT_MAX }, 1, 0, 0, -ERANGE },
+ { "lower range", 1, { -2000 }, { INT_MAX }, 0, 0, 0, -ERANGE },
+};
+
+static void aggregation_case_desc(const struct aggregation_case *param, char *desc)
+{
+ strscpy(desc, param->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(aggregation, aggregation_cases, aggregation_case_desc);
+
+static void aggregation_calculation_test(struct kunit *test)
+{
+ const struct aggregation_case *param = test->param_value;
+ struct aggregation_fixture *fixture = test->priv;
+ struct thermal_zone_device *tz;
+ unsigned int i;
+ int temp = 12345;
+ int ret;
+
+ /* A private destination allows testing post-initialization zero values. */
+ tz = kunit_kzalloc(test, sizeof(*tz), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, tz);
+ mutex_init(&tz->lock);
+ tz->tzp = &fixture->params;
+ fixture->aggregate.num_entries = param->count;
+ fixture->params.offset = param->offset;
+ for (i = 0; i < param->count; i++) {
+ fixture->entries[i].coefficient = param->coefficients[i];
+ fixture->data[i].temperature = param->temperatures[i];
+ }
+ fixture->data[0].error = param->error;
+ mutex_lock(&tz->lock);
+ ret = thermal_zone_get_temp_aggregate(tz, &temp);
+ mutex_unlock(&tz->lock);
+ KUNIT_EXPECT_EQ(test, ret, param->result);
+ KUNIT_EXPECT_EQ(test, temp, ret ? 12345 : param->expected);
+ if (param->error)
+ KUNIT_EXPECT_EQ(test, fixture->data[1].reads, 0);
+ mutex_destroy(&tz->lock);
+}
+
+static void aggregation_registration_test(struct kunit *test)
+{
+ struct aggregation_fixture *fixture = test->priv;
+ struct thermal_zone_device *tz;
+ int temp;
+
+ tz = thermal_zone_device_register_with_trips("kunit-aggregate", NULL, 0, NULL,
+ &aggregation_destination_ops,
+ &fixture->params, 0, 0);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tz);
+ fixture->destination = tz;
+ KUNIT_EXPECT_EQ(test, fixture->entries[0].coefficient, 334);
+ KUNIT_ASSERT_EQ(test, thermal_zone_get_temp(tz, &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 29990);
+ KUNIT_ASSERT_EQ(test, thermal_zone_device_enable(tz), 0);
+ KUNIT_EXPECT_EQ(test, tz->temperature, 29990);
+ KUNIT_ASSERT_EQ(test, thermal_zone_get_temp(fixture->sources[0], &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 20000);
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_slope(fixture->sources[0]), 0);
+ fixture->sources[0]->tzp->slope = 348;
+ fixture->sources[0]->tzp->offset = -9301;
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_slope(fixture->sources[0]), 348);
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_offset(fixture->sources[0]), -9301);
+ KUNIT_ASSERT_EQ(test, thermal_zone_get_temp(fixture->sources[0], &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 20000);
+
+ fixture->sources[0]->state |= TZ_STATE_FLAG_EXIT;
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_temp(tz, &temp), -ENODEV);
+ fixture->sources[0]->state &= ~TZ_STATE_FLAG_EXIT;
+}
+
+static void aggregation_registration_failure_test(struct kunit *test)
+{
+ struct aggregation_fixture *fixture = test->priv;
+ struct thermal_zone_device_ops ops = { .set_trips = aggregation_set_trips };
+ struct thermal_zone_device *tz;
+
+ tz = thermal_zone_device_register_with_trips("kunit-rejected", NULL, 0, NULL,
+ &ops, &fixture->params, 0, 0);
+ KUNIT_EXPECT_EQ(test, PTR_ERR(tz), -EINVAL);
+ fixture->entries[0].coefficient = 1000;
+ tz = thermal_zone_device_register_with_trips("kunit-partial", NULL, 0, NULL,
+ &aggregation_destination_ops,
+ &fixture->params, 0, 0);
+ KUNIT_EXPECT_EQ(test, PTR_ERR(tz), -EINVAL);
+ KUNIT_EXPECT_EQ(test, fixture->entries[1].coefficient, 0);
+}
+
+struct aggregation_busy_context {
+ struct thermal_zone_device *tz;
+ struct completion locked;
+ struct completion release;
+};
+
+static int aggregation_hold_source(void *data)
+{
+ struct aggregation_busy_context *ctx = data;
+
+ mutex_lock(&ctx->tz->lock);
+ complete(&ctx->locked);
+ wait_for_completion(&ctx->release);
+ mutex_unlock(&ctx->tz->lock);
+ while (!kthread_should_stop()) {
+ set_current_state(TASK_INTERRUPTIBLE);
+ if (!kthread_should_stop())
+ schedule();
+ }
+ __set_current_state(TASK_RUNNING);
+ return 0;
+}
+
+static void aggregation_busy_source_test(struct kunit *test)
+{
+ struct aggregation_fixture *fixture = test->priv;
+ struct aggregation_busy_context ctx = { .tz = fixture->sources[0] };
+ struct task_struct *task;
+ struct thermal_zone_device *tz;
+ int temp = 12345;
+
+ tz = thermal_zone_device_register_with_trips("kunit-busy", NULL, 0, NULL,
+ &aggregation_destination_ops,
+ &fixture->params, 0, 0);
+ if (IS_ERR(tz)) {
+ KUNIT_FAIL(test, "cannot register destination");
+ return;
+ }
+ fixture->destination = tz;
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fixture->destination);
+ init_completion(&ctx.locked);
+ init_completion(&ctx.release);
+ task = kthread_run(aggregation_hold_source, &ctx, "thermal-kunit-lock");
+ KUNIT_ASSERT_FALSE(test, IS_ERR(task));
+ if (!wait_for_completion_timeout(&ctx.locked, HZ)) {
+ complete(&ctx.release);
+ kthread_stop(task);
+ KUNIT_FAIL(test, "source lock was not acquired");
+ return;
+ }
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_temp(fixture->destination, &temp), -EAGAIN);
+ KUNIT_EXPECT_EQ(test, temp, 12345);
+ complete(&ctx.release);
+ kthread_stop(task);
+}
+
+static struct kunit_case thermal_aggregation_cases[] = {
+ KUNIT_CASE(aggregation_defaults_test),
+ KUNIT_CASE(aggregation_validation_test),
+ KUNIT_CASE_PARAM(aggregation_calculation_test, aggregation_gen_params),
+ KUNIT_CASE(aggregation_registration_test),
+ KUNIT_CASE(aggregation_registration_failure_test),
+ KUNIT_CASE(aggregation_busy_source_test),
+ {}
+};
+
+static struct kunit_suite thermal_aggregation_suite = {
+ .name = "thermal-aggregation",
+ .init = aggregation_init,
+ .test_cases = thermal_aggregation_cases,
+};
+
+kunit_test_suite(thermal_aggregation_suite);
+
+MODULE_LICENSE("GPL");
diff --git a/drivers/thermal/testing/thermal_of_aggregation_test.c b/drivers/thermal/testing/thermal_of_aggregation_test.c
new file mode 100644
index 000000000000..82a0c9e9c5c9
--- /dev/null
+++ b/drivers/thermal/testing/thermal_of_aggregation_test.c
@@ -0,0 +1,330 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Included by thermal_of.c to exercise private OF lifecycle operations. */
+
+#include <kunit/of.h>
+#include <kunit/test.h>
+#include <linux/kthread.h>
+
+struct thermal_of_test_reading {
+ int temperature;
+ struct completion *entered;
+ struct completion *release;
+};
+
+struct thermal_of_test_context {
+ struct device_node *provider;
+ struct device_node *root;
+ struct device_node *provider_zero;
+ struct thermal_zone_device *sources[2];
+ struct thermal_of_test_reading readings[2];
+ struct thermal_of_aggregate aggregate;
+ bool listed;
+ bool was_ready;
+};
+
+static int thermal_of_test_temp(struct thermal_zone_device *tz, int *temp)
+{
+ struct thermal_of_test_reading *reading = thermal_zone_device_priv(tz);
+
+ lockdep_assert_held(&tz->lock);
+ if (reading->entered) {
+ complete(reading->entered);
+ wait_for_completion(reading->release);
+ }
+ *temp = reading->temperature;
+ return 0;
+}
+
+static const struct thermal_zone_device_ops thermal_of_test_ops = {
+ .get_temp = thermal_of_test_temp,
+};
+
+static void thermal_of_test_cleanup(void *data)
+{
+ struct thermal_of_test_context *ctx = data;
+ unsigned int i;
+
+ {
+ guard(mutex)(&thermal_of_lock);
+ if (ctx->listed)
+ list_del(&ctx->aggregate.node);
+ thermal_of_aggregate_release(&ctx->aggregate);
+ thermal_of_aggregates_ready = ctx->was_ready;
+ }
+ for (i = 0; i < ARRAY_SIZE(ctx->sources); i++)
+ if (ctx->sources[i])
+ thermal_of_zone_unregister(ctx->sources[i]);
+ of_node_put(ctx->aggregate.np);
+ of_node_put(ctx->provider);
+ of_node_put(ctx->provider_zero);
+ of_node_put(ctx->root);
+}
+
+static int thermal_of_test_init(struct kunit *test)
+{
+ const struct thermal_zone_params params = { .no_hwmon = true };
+ struct thermal_of_test_context *ctx;
+ unsigned int i;
+ int ret;
+
+ ret = of_overlay_apply_kunit(test, thermal_aggregation);
+ if (ret)
+ return ret;
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ if (!ctx)
+ return -ENOMEM;
+ test->priv = ctx;
+ {
+ guard(mutex)(&thermal_of_lock);
+ ctx->was_ready = thermal_of_aggregates_ready;
+ /* The empty UML tree has no /thermal-zones for the late init scan. */
+ thermal_of_aggregates_ready = true;
+ }
+ ret = kunit_add_action_or_reset(test, thermal_of_test_cleanup, ctx);
+ if (ret)
+ return ret;
+ ctx->root = of_find_node_by_path("/thermal-kunit");
+ if (!ctx->root)
+ return -ENODEV;
+ ctx->provider = of_get_child_by_name(ctx->root, "sensor");
+ if (!ctx->provider)
+ return -ENODEV;
+ ctx->provider_zero = of_get_child_by_name(ctx->root, "sensor-zero");
+ if (!ctx->provider_zero)
+ return -ENODEV;
+ for (i = 0; i < ARRAY_SIZE(ctx->sources); i++) {
+ struct thermal_zone_device *tz;
+
+ ctx->readings[i].temperature = 20000 + i * 10000;
+ tz = thermal_zone_device_register_with_trips("of-kunit-source", NULL, 0,
+ &ctx->readings[i],
+ &thermal_of_test_ops, ¶ms, 0, 0);
+ if (IS_ERR(tz))
+ return PTR_ERR(tz);
+ ctx->sources[i] = tz;
+ }
+ return 0;
+}
+
+static void thermal_of_deferred_lifecycle_test(struct kunit *test)
+{
+ struct thermal_of_test_context *ctx = test->priv;
+ struct thermal_zone_device *old_source;
+ const struct thermal_zone_params params = { .no_hwmon = true };
+ int temp;
+
+ ctx->aggregate.np = of_get_child_by_name(ctx->root, "average");
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.np);
+ {
+ guard(mutex)(&thermal_of_lock);
+ list_add(&ctx->aggregate.node, &thermal_of_aggregates);
+ ctx->listed = true;
+ KUNIT_EXPECT_EQ(test, thermal_of_aggregate_create(&ctx->aggregate), -EPROBE_DEFER);
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.aggregate.entries, NULL);
+ }
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 0, ctx->sources[0]), 0);
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.tz, NULL);
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 1, ctx->sources[1]), 0);
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.tz);
+ KUNIT_ASSERT_EQ(test, thermal_zone_get_temp(ctx->aggregate.tz, &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 25000);
+ KUNIT_EXPECT_EQ(test, ctx->aggregate.aggregate.entries[0].coefficient, 500);
+ KUNIT_EXPECT_EQ(test, thermal_of_source_add(ctx->provider, 0, ctx->sources[0]), -EEXIST);
+ {
+ struct thermal_zone_device *tz = ctx->aggregate.tz;
+
+ guard(mutex)(&thermal_of_lock);
+ thermal_of_aggregate_retry();
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.tz, tz);
+ }
+
+ /* Removal must destroy the consumer and release references before returning. */
+ old_source = ctx->sources[0];
+ ctx->sources[0] = NULL;
+ thermal_of_zone_unregister(old_source);
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.tz, NULL);
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.aggregate.entries, NULL);
+ KUNIT_EXPECT_FALSE(test, ctx->aggregate.failed);
+ old_source = thermal_zone_device_register_with_trips("of-kunit-reloaded", NULL, 0,
+ &ctx->readings[0],
+ &thermal_of_test_ops,
+ ¶ms, 0, 0);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_source);
+ ctx->sources[0] = old_source;
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 0, ctx->sources[0]), 0);
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.tz);
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_temp(ctx->aggregate.tz, &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 25000);
+}
+
+static void thermal_of_coefficients_test(struct kunit *test)
+{
+ static const struct {
+ const char *name;
+ int result;
+ } cases[] = {
+ { "explicit", 0 },
+ { "partial", -EINVAL },
+ { "zero", -EINVAL },
+ { "overflow", -ERANGE },
+ { "bad-delay", -EINVAL },
+ };
+ struct thermal_of_test_context *ctx = test->priv;
+ unsigned int i;
+ int temp;
+
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 0, ctx->sources[0]), 0);
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 1, ctx->sources[1]), 0);
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ struct thermal_of_aggregate aggregate = {};
+ int ret;
+
+ aggregate.np = of_get_child_by_name(ctx->root, cases[i].name);
+ KUNIT_ASSERT_NOT_NULL(test, aggregate.np);
+ {
+ guard(mutex)(&thermal_of_lock);
+ ret = thermal_of_aggregate_create(&aggregate);
+ }
+ KUNIT_EXPECT_EQ_MSG(test, ret, cases[i].result, "%s", cases[i].name);
+ if (!ret) {
+ KUNIT_EXPECT_EQ(test, aggregate.aggregate.entries[0].coefficient, 2000);
+ KUNIT_EXPECT_EQ(test, aggregate.aggregate.entries[1].coefficient, -1000);
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_temp(aggregate.tz, &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 9000);
+ } else {
+ KUNIT_EXPECT_PTR_EQ(test, aggregate.aggregate.entries, NULL);
+ KUNIT_EXPECT_EQ(test, aggregate.aggregate.num_entries, 0);
+ }
+ {
+ guard(mutex)(&thermal_of_lock);
+ thermal_of_aggregate_release(&aggregate);
+ }
+ of_node_put(aggregate.np);
+ }
+}
+
+static void thermal_of_zero_cells_test(struct kunit *test)
+{
+ struct thermal_of_test_context *ctx = test->priv;
+ int temp;
+
+ ctx->aggregate.np = of_get_child_by_name(ctx->root, "zero-cells");
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.np);
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider_zero, 0, ctx->sources[0]), 0);
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 1, ctx->sources[1]), 0);
+ {
+ guard(mutex)(&thermal_of_lock);
+ KUNIT_EXPECT_EQ(test, thermal_of_aggregate_create(&ctx->aggregate), 0);
+ }
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.tz);
+ KUNIT_EXPECT_EQ(test, thermal_zone_get_temp(ctx->aggregate.tz, &temp), 0);
+ KUNIT_EXPECT_EQ(test, temp, 25000);
+}
+
+struct thermal_of_test_thread {
+ struct thermal_zone_device *tz;
+ struct completion started;
+ struct completion done;
+ bool remove;
+ int result;
+ int temperature;
+};
+
+static int thermal_of_test_thread_fn(void *data)
+{
+ struct thermal_of_test_thread *thread = data;
+
+ complete(&thread->started);
+ if (thread->remove)
+ thermal_of_zone_unregister(thread->tz);
+ else
+ thread->result = thermal_zone_get_temp(thread->tz, &thread->temperature);
+ complete(&thread->done);
+ /* Keep task storage valid until the test joins with kthread_stop(). */
+ while (!kthread_should_stop()) {
+ set_current_state(TASK_INTERRUPTIBLE);
+ if (!kthread_should_stop())
+ schedule();
+ }
+ __set_current_state(TASK_RUNNING);
+ return 0;
+}
+
+static void thermal_of_concurrent_removal_test(struct kunit *test)
+{
+ struct thermal_of_test_context *ctx = test->priv;
+ struct thermal_of_test_thread reader = {}, remover = { .remove = true };
+ struct task_struct *read_task, *remove_task;
+ struct completion entered, release;
+ unsigned long done;
+
+ ctx->aggregate.np = of_get_child_by_name(ctx->root, "average");
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.np);
+ {
+ guard(mutex)(&thermal_of_lock);
+ list_add(&ctx->aggregate.node, &thermal_of_aggregates);
+ ctx->listed = true;
+ }
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 0, ctx->sources[0]), 0);
+ KUNIT_ASSERT_EQ(test, thermal_of_source_add(ctx->provider, 1, ctx->sources[1]), 0);
+ KUNIT_ASSERT_NOT_NULL(test, ctx->aggregate.tz);
+ init_completion(&entered);
+ init_completion(&release);
+ init_completion(&reader.started);
+ init_completion(&reader.done);
+ init_completion(&remover.started);
+ init_completion(&remover.done);
+ ctx->readings[0].entered = &entered;
+ ctx->readings[0].release = &release;
+ reader.tz = ctx->aggregate.tz;
+ remover.tz = ctx->sources[0];
+ read_task = kthread_run(thermal_of_test_thread_fn, &reader, "thermal-of-read");
+ KUNIT_ASSERT_FALSE(test, IS_ERR(read_task));
+ if (!wait_for_completion_timeout(&entered, HZ)) {
+ complete(&release);
+ kthread_stop(read_task);
+ ctx->readings[0].entered = NULL;
+ KUNIT_FAIL(test, "read did not reach source callback");
+ return;
+ }
+ remove_task = kthread_run(thermal_of_test_thread_fn, &remover, "thermal-of-remove");
+ if (IS_ERR(remove_task)) {
+ complete(&release);
+ kthread_stop(read_task);
+ ctx->readings[0].entered = NULL;
+ KUNIT_FAIL(test, "cannot start removal thread");
+ return;
+ }
+ wait_for_completion_timeout(&remover.started, HZ);
+ done = wait_for_completion_timeout(&remover.done, msecs_to_jiffies(20));
+ KUNIT_EXPECT_EQ(test, done, 0);
+ complete(&release);
+ done = wait_for_completion_timeout(&reader.done, HZ);
+ KUNIT_EXPECT_NE(test, done, 0);
+ done = wait_for_completion_timeout(&remover.done, HZ);
+ KUNIT_EXPECT_NE(test, done, 0);
+ kthread_stop(read_task);
+ kthread_stop(remove_task);
+ ctx->sources[0] = NULL;
+ ctx->readings[0].entered = NULL;
+ KUNIT_EXPECT_EQ(test, reader.result, 0);
+ KUNIT_EXPECT_EQ(test, reader.temperature, 25000);
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.tz, NULL);
+ KUNIT_EXPECT_PTR_EQ(test, ctx->aggregate.aggregate.entries, NULL);
+}
+
+static struct kunit_case thermal_of_aggregation_cases[] = {
+ KUNIT_CASE(thermal_of_deferred_lifecycle_test),
+ KUNIT_CASE(thermal_of_coefficients_test),
+ KUNIT_CASE(thermal_of_zero_cells_test),
+ KUNIT_CASE(thermal_of_concurrent_removal_test),
+ {}
+};
+
+static struct kunit_suite thermal_of_aggregation_suite = {
+ .name = "thermal-of-aggregation",
+ .init = thermal_of_test_init,
+ .test_cases = thermal_of_aggregation_cases,
+};
+
+kunit_test_suite(thermal_of_aggregation_suite);
diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
index 1a7e17757025..004de0e24f9e 100644
--- a/drivers/thermal/thermal_aggregation.c
+++ b/drivers/thermal/thermal_aggregation.c
@@ -246,3 +246,7 @@ int thermal_zone_get_temp_aggregate(struct thermal_zone_device *tz, int *temp)
*temp = sum;
return 0;
}
+
+#if IS_ENABLED(CONFIG_THERMAL_AGGREGATION_KUNIT_TEST)
+#include "testing/thermal_aggregation_test.c"
+#endif
diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 64ae7769edcb..b90e0b500494 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -741,6 +741,10 @@ static int __init thermal_of_aggregate_init(void)
return 0;
}
late_initcall(thermal_of_aggregate_init);
+
+#if IS_ENABLED(CONFIG_THERMAL_OF_AGGREGATION_KUNIT_TEST)
+#include "testing/thermal_of_aggregation_test.c"
+#endif
#endif /* CONFIG_THERMAL_AGGREGATION */
static void devm_thermal_of_zone_release(struct device *dev, void *res)
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
end of thread, other threads:[~2026-11-19 9:48 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 9:46 [PATCH v1 00/12] thermal: Add multi-source thermal zone aggregation Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 01/12] thermal: core: Add thermal zone device reference helpers Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 02/12] thermal: core: Introduce private thermal zone aggregation structures Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 03/12] thermal: core: Validate thermal zone aggregation descriptions Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 04/12] thermal: core: Initialize thermal zone aggregation coefficients Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 05/12] thermal: core: Dispatch aggregate temperature reads Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 06/12] thermal: core: Calculate aggregated thermal zone temperatures Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 07/12] thermal: core: Initialize aggregation during zone registration Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 08/12] thermal: of: Separate zone parsing from sensor lookup Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 09/12] thermal: of: Track registered hardware temperature sources Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 10/12] thermal: of: Instantiate aggregates after their source zones Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 11/12] dt-bindings: thermal: Describe multi-source zone aggregation Daniel Lezcano
2026-11-19 9:46 ` [PATCH v1 12/12] thermal: testing: Add KUnit coverage for " Daniel Lezcano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox