mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
To: "Rafael J. Wysocki" <rafael@kernel.org>,
	 Daniel Lezcano <daniel.lezcano@kernel.org>,
	Zhang Rui <rui.zhang@intel.com>,
	 Lukasz Luba <lukasz.luba@arm.com>,
	 Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Cc: linux-pm@vger.kernel.org, linux-api@vger.kernel.org,
	 linux-kernel@vger.kernel.org, ricardo.neri@intel.com,
	 Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
Subject: [PATCH 10/13] thermal: intel: dpti: Add rank-based owner selection
Date: Mon, 09 Nov 2026 19:36:37 -0800	[thread overview]
Message-ID: <20261109-dpti-custom-cpu-v1-10-ead8e2d2067e@linux.intel.com> (raw)
In-Reply-To: <20261109-dpti-custom-cpu-v1-0-ead8e2d2067e@linux.intel.com>

Prepare for a sysfs interface that lets the user choose a CPU to own a
package's directed package-level thermal interrupt (DPTI). Add the state
and selection functionality for a pinned owner.

Implement a ranking mechanism to identify the best-suited CPU to own the
interrupt. The highest-ranked CPU in a package is the one the user selects
(DPTI_RANK_PINNED). All other CPUs are ranked as DPTI_RANK_ONLINE until
owner selection becomes isolation-aware.

On CPU hotplug events, re-evaluate which CPU owns the DPTI. If a
better-ranked CPU comes online, move the interrupt to that CPU. Likewise,
if the owner goes offline, find the next best-ranked CPU and move the
interrupt there. Both cases can occur if the user-selected CPU changes its
online status.

dpti_select_cpu() also reports the rank of its choice, which the online
path needs for its strict-improvement test. dpti_offline() ignores it until
isolation-aware selection warns about an isolated owner, but passes it
anyway rather than making the argument optional.

Route both hotplug callbacks through dpti_lookup_pkg_id() so that they
validate the package id the same way. dpti_online() needs only the
validation: let dpti_lookup_pkg_id() take a NULL pkg_id. dpti_online() now
returns the lookup's -ENODEV rather than -EINVAL: the CPU comes from the
hotplug core and is valid; what is missing is a per-package slot. The
caller only tests the value against zero.

With all CPUs ranked equally, behavior changes only after a failed
acknowledgment: the package then retries on its lowest-numbered online CPU
rather than on the CPU coming online.

Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
---
 drivers/thermal/intel/therm_throt.c | 151 +++++++++++++++++++++++++++++++-----
 1 file changed, 130 insertions(+), 21 deletions(-)

diff --git a/drivers/thermal/intel/therm_throt.c b/drivers/thermal/intel/therm_throt.c
index e6ce443c0a9d..97c205dab1b6 100644
--- a/drivers/thermal/intel/therm_throt.c
+++ b/drivers/thermal/intel/therm_throt.c
@@ -559,9 +559,13 @@ static int dpti_program_on_cpu(unsigned int cpu)
  * struct dpti_pkg - Per-package DPTI state
  * @owner:	CPU currently programmed to receive the package-level
  *		interrupt, or nr_cpu_ids when the package has no owner.
+ * @pin:	CPU the user requested as the owner, or nr_cpu_ids for
+ *		automatic selection. A pinned CPU overrides the automatic
+ *		selection whenever it is online.
  */
 struct dpti_pkg {
 	unsigned int owner;
+	unsigned int pin;
 };
 
 /*
@@ -596,6 +600,7 @@ static bool dpti_supported(void)
 	return true;
 }
 
+/* @pkg_id may be NULL for a caller that only validates @cpu. */
 static int dpti_lookup_pkg_id(unsigned int cpu, u16 *pkg_id)
 {
 	u16 id;
@@ -607,7 +612,8 @@ static int dpti_lookup_pkg_id(unsigned int cpu, u16 *pkg_id)
 	if (id >= topology_max_packages())
 		return -ENODEV;
 
-	*pkg_id = id;
+	if (pkg_id)
+		*pkg_id = id;
 
 	return 0;
 }
@@ -655,44 +661,145 @@ static void dpti_clear_all(void)
 	}
 }
 
-static int dpti_online(unsigned int cpu)
+/**
+ * enum dpti_rank - How suitable a CPU is as a DPTI owner
+ * @DPTI_RANK_NONE:		No eligible CPU.
+ * @DPTI_RANK_ONLINE:		An online CPU of the package (automatic choice).
+ * @DPTI_RANK_PINNED:		Explicitly requested by the user; overrides any
+ *				automatic choice.
+ *
+ * Higher is better; the selection picks the highest-ranked online CPU in the
+ * package.
+ */
+enum dpti_rank {
+	DPTI_RANK_NONE,
+	DPTI_RANK_ONLINE,
+	DPTI_RANK_PINNED,
+};
+
+static enum dpti_rank dpti_cpu_rank(unsigned int cpu)
 {
-	u16 pkg_id;
-	int ret;
+	/* No concept of isolation yet: every online CPU ranks the same. */
+	return DPTI_RANK_ONLINE;
+}
 
-	if (!dpti_supported())
-		return 0;
+/*
+ * Select the owner of the package-level thermal interrupt for @pkg_cpu's
+ * package. @skip is excluded from the selection (e.g., if it is going offline).
+ * @rank receives the rank of the selected CPU.
+ *
+ * A user-pinned CPU takes precedence whenever it is online; otherwise the best
+ * ranked CPU in the package is chosen.
+ */
+static unsigned int dpti_select_cpu(unsigned int pkg_cpu, unsigned int skip,
+				    enum dpti_rank *rank)
+{
+	const struct cpumask *pkg_mask = topology_core_cpumask(pkg_cpu);
+	enum dpti_rank best_rank = DPTI_RANK_NONE;
+	unsigned int cpu, pin, best = nr_cpu_ids;
 
-	pkg_id = topology_logical_package_id(cpu);
-	if (pkg_id >= topology_max_packages())
-		return -EINVAL;
+	lockdep_assert_cpus_held();
+
+	/*
+	 * Choose the user-pinned CPU if available: it is online and not going
+	 * offline (as indicated by @skip).
+	 */
+	pin = dpti_pkg_of_cpu_nocheck(pkg_cpu)->pin;
+	if (pin < nr_cpu_ids && pin != skip &&
+	    cpumask_test_cpu(pin, pkg_mask)) {
+		*rank = DPTI_RANK_PINNED;
+		return pin;
+	}
+
+	for_each_cpu(cpu, pkg_mask) {
+		enum dpti_rank cpu_rank;
+
+		if (cpu == skip)
+			continue;
+
+		cpu_rank = dpti_cpu_rank(cpu);
+		if (cpu_rank > best_rank) {
+			best_rank = cpu_rank;
+			best = cpu;
+			/*
+			 * Nothing outranks an online CPU in automatic selection.
+			 */
+			if (best_rank == DPTI_RANK_ONLINE)
+				break;
+		}
+	}
+
+	*rank = best_rank;
+
+	return best;
+}
+
+/*
+ * Reselect the owner for @cpu's package and migrate to it, but only on a
+ * strict rank improvement.
+ */
+static int dpti_update_owner(unsigned int cpu)
+{
+	struct dpti_pkg *pkg = dpti_pkg_of_cpu_nocheck(cpu);
+	enum dpti_rank new_rank, old_rank;
+	unsigned int new_cpu, old_cpu;
+	int ret;
 
 	if (dpti_shut_down)
 		return 0;
 
-	/* Another CPU in this package already owns the directed interrupt. */
-	if (dpti_pkgs[pkg_id].owner != nr_cpu_ids)
+	old_cpu = pkg->owner;
+	new_cpu = dpti_select_cpu(cpu, nr_cpu_ids, &new_rank);
+	if (new_cpu == old_cpu)
 		return 0;
 
-	ret = dpti_program_on_cpu(cpu);
+	old_rank = DPTI_RANK_NONE;
+	if (old_cpu < nr_cpu_ids)
+		old_rank = dpti_cpu_rank(old_cpu);
+	if (new_rank <= old_rank)
+		return 0;
+
+	ret = dpti_program_on_cpu(new_cpu);
 	if (ret)
 		return ret;
 
-	dpti_pkgs[pkg_id].owner = cpu;
+	if (old_cpu < nr_cpu_ids)
+		dpti_clear_on_cpu(old_cpu);
+
+	pkg->owner = new_cpu;
 
 	return 0;
 }
 
+static int dpti_online(unsigned int cpu)
+{
+	int ret;
+
+	if (!dpti_supported())
+		return 0;
+
+	/*
+	 * Validate @cpu's package id on behalf of dpti_update_owner(), which
+	 * indexes dpti_pkgs[] based on @cpu.
+	 */
+	ret = dpti_lookup_pkg_id(cpu, NULL);
+	if (ret)
+		return ret;
+
+	/*
+	 * A CPU coming online may be a better owner than the one currently
+	 * programmed.
+	 */
+	return dpti_update_owner(cpu);
+}
+
 static void dpti_offline(unsigned int cpu)
 {
+	enum dpti_rank new_rank;
 	unsigned int new_cpu;
 	u16 pkg_id;
 
-	if (!dpti_supported())
-		return;
-
-	pkg_id = topology_logical_package_id(cpu);
-	if (pkg_id >= topology_max_packages())
+	if (dpti_lookup_pkg_id(cpu, &pkg_id))
 		return;
 
 	if (dpti_shut_down)
@@ -706,7 +813,7 @@ static void dpti_offline(unsigned int cpu)
 	 * The package-level interrupt must remain directed after this CPU goes
 	 * offline.
 	 */
-	new_cpu = cpumask_any_but(topology_core_cpumask(cpu), cpu);
+	new_cpu = dpti_select_cpu(cpu, cpu, &new_rank);
 
 	/*
 	 * dpti_program_on_cpu() undoes a setup that the hardware does not
@@ -727,7 +834,7 @@ static void dpti_offline(unsigned int cpu)
 	 */
 	dpti_config_this_cpu(false);
 
-	dpti_pkgs[pkg_id].owner = (new_cpu < nr_cpu_ids) ? new_cpu : nr_cpu_ids;
+	dpti_pkgs[pkg_id].owner = new_cpu;
 }
 
 /*
@@ -785,8 +892,10 @@ static __init void dpti_init(void)
 	if (!dpti_pkgs)
 		return;
 
-	for (i = 0; i < topology_max_packages(); i++)
+	for (i = 0; i < topology_max_packages(); i++) {
 		dpti_pkgs[i].owner = nr_cpu_ids;
+		dpti_pkgs[i].pin = nr_cpu_ids;
+	}
 
 	register_syscore(&dpti_syscore);
 }

-- 
2.43.0


  parent reply	other threads:[~2026-11-10  3:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  3:36 [PATCH 00/13] thermal: intel: dpti: Direct the package thermal interrupt to a chosen CPU Ricardo Neri
2026-11-10  3:36 ` [PATCH 01/13] thermal: intel: Keep directed package interrupts off after syscore shutdown Ricardo Neri
2026-11-10  3:36 ` [PATCH 02/13] thermal: intel: Prefix log messages via pr_fmt() Ricardo Neri
2026-11-10  3:36 ` [PATCH 03/13] thermal: intel: Move the sysfs device helpers next to their callers Ricardo Neri
2026-11-10  3:36 ` [PATCH 04/13] thermal: intel: dpti: Adopt the dpti_ prefix and the owner terminology Ricardo Neri
2026-11-10  3:36 ` [PATCH 05/13] thermal: intel: dpti: Tear down DPTI only during boot Ricardo Neri
2026-11-10  3:36 ` [PATCH 06/13] thermal: intel: dpti: Introduce struct dpti_pkg for per-package state Ricardo Neri
2026-11-10  3:36 ` [PATCH 07/13] thermal: intel: dpti: Configure the local CPU by value Ricardo Neri
2026-11-10  3:36 ` [PATCH 08/13] thermal: intel: dpti: Factor the programming sequence out into a helper Ricardo Neri
2026-11-10  3:36 ` [PATCH 09/13] thermal: intel: dpti: Program the directed interrupt on the target CPU Ricardo Neri
2026-11-10  3:36 ` Ricardo Neri [this message]
2026-11-10  3:36 ` [PATCH 11/13] thermal: intel: dpti: Serialize updates to the directed interrupt state Ricardo Neri
2026-11-10  3:36 ` [PATCH 12/13] thermal: intel: dpti: Avoid nohz_full CPUs when possible Ricardo Neri
2026-11-10  3:36 ` [PATCH 13/13] thermal: intel: dpti: Add sysfs control of interrupt owner Ricardo Neri

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=20261109-dpti-custom-cpu-v1-10-ead8e2d2067e@linux.intel.com \
    --to=ricardo.neri-calderon@linux.intel.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=rafael@kernel.org \
    --cc=ricardo.neri@intel.com \
    --cc=rui.zhang@intel.com \
    --cc=srinivas.pandruvada@linux.intel.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

all inboxes | Powered by JetHome®