[PATCH v1 03/12] thermal: core: Validate thermal zone aggregation descriptions

From: Daniel Lezcano

Date: Thu Nov 19 2026 - 04:48:15 EST


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@xxxxxxxxxxxxxxxx>
---
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