Linux Power Management development
 help / color / mirror / Atom feed
From: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
To: daniel.lezcano@oss.qualcomm.com, rafael@kernel.org
Cc: Zhang Rui <rui.zhang@intel.com>,
	Lukasz Luba <lukasz.luba@arm.com>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Anvesh Jain P <anvesh.p@oss.qualcomm.com>,
	Ulf Hansson <ulf.hansson@oss.qualcomm.com>,
	linux-pm@vger.kernel.org,
	Daniel Lezcano <daniel.lezcano@kernel.org>,
	linux-kernel@vger.kernel.org (open list)
Subject: [PATCH v1 06/12] thermal: core: Calculate aggregated thermal zone temperatures
Date: Thu, 19 Nov 2026 10:46:48 +0100	[thread overview]
Message-ID: <20261119094706.3439040-7-daniel.lezcano@oss.qualcomm.com> (raw)
In-Reply-To: <20261119094706.3439040-1-daniel.lezcano@oss.qualcomm.com>

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


  parent reply	other threads:[~2026-11-19  9:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Daniel Lezcano [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119094706.3439040-7-daniel.lezcano@oss.qualcomm.com \
    --to=daniel.lezcano@oss.qualcomm.com \
    --cc=anvesh.p@oss.qualcomm.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=ulf.hansson@oss.qualcomm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox