Netdev List
 help / color / mirror / Atom feed
From: Kris Pan <kris.pan@intel.com>
To: tglx@kernel.org, maz@kernel.org
Cc: mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	hpa@zytor.com, x86@kernel.org, peterz@infradead.org,
	frederic@kernel.org, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, longman@redhat.com, tj@kernel.org,
	corbet@lwn.net, linux-doc@vger.kernel.org, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	netdev@vger.kernel.org, joro@8bytes.org, will@kernel.org,
	robin.murphy@arm.com, dwmw2@infradead.org,
	baolu.lu@linux.intel.com, vasant.hegde@amd.com,
	suravee.suthikulpanit@amd.com, iommu@lists.linux.dev,
	haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com,
	linux-hyperv@vger.kernel.org, bhelgaas@google.com,
	lpieralisi@kernel.org, linux-pci@vger.kernel.org,
	steve.wahl@hpe.com, bigeasy@linutronix.de, clrkwllms@kernel.org,
	linux-rt-devel@lists.linux.dev, radu@rendec.net,
	linux-kernel@vger.kernel.org, Kris Pan <kris.pan@intel.com>
Subject: [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation
Date: Mon, 30 Nov 2026 11:37:04 +0800	[thread overview]
Message-ID: <20261130033709.2704805-7-kris.pan@intel.com> (raw)
In-Reply-To: <20261130033709.2704805-1-kris.pan@intel.com>

Filtering changes the mask which is programmed while the requested mask
stays as the driver wrote it.  Consumers of the affinity notifier and
threaded IRQ handlers have to follow the programmed mask, otherwise they
keep steering work to a CPU which the partition just took away.

Report the requested mask from irq_affinity_notify(), as upstream does;
a pending move carries the newest request in the pending mask.  A
consumer which spreads work over several CPUs asks for the mask the
core would hand to the chip with irq_copy_programmed_affinity(): the
request (or the pending request) with the runtime-isolated CPUs
removed, or the automatic relocation.  The effective affinity is
deliberately not used for that; it is a single target on chips with
IRQD_SINGLE_TARGET and would collapse a multi-CPU queue map.  A
consumer which stores the notified mask as the request (net/core does)
must not be handed the filtered mask, or removing the partition later
cannot restore the CPU.

The notifier is queued when the affinity is requested, but a deferred
move programs the pending mask only from interrupt context.  Schedule
the notifier again from irq_move_masked_irq() once the pending mask was
programmed, so that a consumer which follows the programmed mask
converges on the new target.  Only do that while runtime isolation is
active: without it the programmed mask always tracked the request, and
the upstream single notification is kept.

A deferred move cannot program the mask synchronously: a chip which
sets IRQCHIP_MOVE_DEFERRED queues it in the core, and the x86 vector
code returns -EBUSY while a previous move is still in flight.  Mark the
IRQ threads whenever a pending move is armed while runtime isolation is
active: the -EBUSY fallback of irq_try_set_affinity(), the reapply
branch, and irq_set_affinity_locked() when a user request cannot be
programmed immediately.  A handler which is woken by an interrupt on
the isolated CPU can then escape to the requested target instead of
running there.  A pending move carries the newest request in the
pending mask; irq_thread_check_affinity() uses it as the escape target,
because the descriptor affinity is only updated when the move
completes.  When that target is already free of runtime-isolated CPUs
no filtering applies and the thread follows the pending target itself;
falling back to the old effective CPU would keep it on the isolated CPU
until the move completes, which is exactly the window the early mark is
meant to cover.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 include/linux/irq.h    |  7 ++++
 kernel/irq/isolation.c | 40 ++++++++++++++++++
 kernel/irq/manage.c    | 92 ++++++++++++++++++++++++++++++++++++++++--
 kernel/irq/migration.c | 10 +++++
 4 files changed, 146 insertions(+), 3 deletions(-)

diff --git a/include/linux/irq.h b/include/linux/irq.h
index 382591da2038d..ffe2339ac6b6f 100644
--- a/include/linux/irq.h
+++ b/include/linux/irq.h
@@ -641,6 +641,7 @@ bool irq_isolation_can_filter(struct irq_data *data);
 bool irq_isolation_select_mask(struct irq_data *data,
 			       const struct cpumask *src,
 			       struct cpumask *out);
+bool irq_copy_programmed_affinity(unsigned int irq, struct cpumask *mask);
 
 #else
 static inline bool irq_isolation_active(void) { return false; }
@@ -656,6 +657,12 @@ static inline bool irq_isolation_select_mask(struct irq_data *data,
 	return false;
 }
 
+static inline bool irq_copy_programmed_affinity(unsigned int irq,
+						struct cpumask *mask)
+{
+	return false;
+}
+
 #endif
 
 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_IRQ_MIGRATION)
diff --git a/kernel/irq/isolation.c b/kernel/irq/isolation.c
index 4ff45da90f909..6307b7508057e 100644
--- a/kernel/irq/isolation.c
+++ b/kernel/irq/isolation.c
@@ -173,6 +173,46 @@ bool irq_isolation_select_mask(struct irq_data *data,
 	return true;
 }
 
+/**
+ * irq_copy_programmed_affinity - Snapshot the mask the chip is asked to use
+ * @irq:	Interrupt number
+ * @mask:	Storage for the snapshot
+ *
+ * The affinity notifier reports the requested mask.  Runtime IRQ isolation
+ * may program a different mask for a filterable chip: the request with the
+ * runtime-isolated CPUs removed, or an automatic relocation when the request
+ * lost its last online target.  A consumer which spreads work over several
+ * CPUs, such as a reverse map, has to follow that mask.  The effective
+ * affinity is a single target on chips which only deliver to one CPU and is
+ * the wrong input for a multi-CPU map.
+ *
+ * Returns: true when the policy changed the requested mask.
+ * Must not be called with desc->lock held.
+ */
+bool irq_copy_programmed_affinity(unsigned int irq, struct cpumask *mask)
+{
+	struct irq_desc *desc = irq_to_desc(irq);
+	const struct cpumask *req;
+
+	if (!desc)
+		return false;
+
+	guard(raw_spinlock_irqsave)(&desc->lock);
+	/*
+	 * A deferred move keeps the newest request in the pending mask and
+	 * the notifier reports that mask; the descriptor affinity is only
+	 * updated when the move completes.
+	 */
+	if (irq_move_pending(&desc->irq_data))
+		req = irq_desc_get_pending_mask(desc);
+	else
+		req = desc->irq_common_data.affinity;
+	if (cpumask_empty(req))
+		return false;
+
+	return irq_isolation_select_mask(&desc->irq_data, req, mask);
+}
+
 /**
  * irq_isolation_widen_mask - Preferred emergency mask after the filtered
  *			      placement failed
diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
index 76c8cb679f4cc..6e6fdae14bb07 100644
--- a/kernel/irq/manage.c
+++ b/kernel/irq/manage.c
@@ -390,12 +390,25 @@ void irq_reapply_affinity(struct irq_desc *desc, const struct cpumask *changed)
 		return;
 
 	if (irq_can_move_pcntxt(data)) {
+		int ret;
+
 #ifdef CONFIG_GENERIC_PENDING_IRQ
 		if (irqd_is_setaffinity_pending(data))
 			goto pending;
 #endif
-		irq_try_set_affinity(data, desc->irq_common_data.affinity,
-				     false);
+		ret = irq_try_set_affinity(data,
+					   desc->irq_common_data.affinity,
+					   false);
+		/*
+		 * irq_try_set_affinity() turns -EBUSY into a pending move
+		 * and returns success; mark the IRQ threads like the
+		 * pending branch below, so they can escape while the
+		 * hardware still points at the isolated CPU.
+		 */
+		if (!ret && irqd_is_setaffinity_pending(data))
+			irq_set_thread_affinity(desc);
+		if (!ret && desc->affinity_notify)
+			irq_affinity_schedule_notify_work(desc);
 		return;
 	}
 
@@ -410,6 +423,20 @@ void irq_reapply_affinity(struct irq_desc *desc, const struct cpumask *changed)
 		irqd_set_move_pending(data);
 		irq_copy_pending(desc, desc->irq_common_data.affinity);
 	}
+	/*
+	 * The hardware move is deferred, but the IRQ threads must not run
+	 * a handler on a CPU which is being isolated while the move is
+	 * pending.  Mark them so that they re-evaluate their affinity.
+	 */
+	irq_set_thread_affinity(desc);
+	/*
+	 * Report the queued move right away, like a pending user request.
+	 * The completion path reports again once the pending mask was
+	 * programmed, so a consumer which follows the programmed mask
+	 * converges on the new target.
+	 */
+	if (desc->affinity_notify)
+		irq_affinity_schedule_notify_work(desc);
 #endif
 }
 #endif /* CONFIG_CPU_ISOLATION */
@@ -473,6 +500,16 @@ int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
 		irq_copy_pending(desc, mask);
 	}
 
+	/*
+	 * A pending move keeps the newest request in the pending mask.
+	 * Mark the threads so that a handler which is woken on an isolated
+	 * CPU can escape to it before the move completes; without runtime
+	 * isolation the upstream behaviour (mark on completion) is kept.
+	 */
+	if (!ret && irq_isolation_active() &&
+	    irqd_is_setaffinity_pending(data))
+		irq_set_thread_affinity(desc);
+
 	if (desc->affinity_notify)
 		irq_affinity_schedule_notify_work(desc);
 
@@ -659,6 +696,11 @@ static void irq_affinity_notify(struct work_struct *work)
 	if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
 		goto out;
 
+	/*
+	 * Report the requested mask, not the programmed one.  A consumer
+	 * which steers hardware to a CPU uses irq_copy_programmed_affinity()
+	 * instead; runtime IRQ isolation may have filtered the request.
+	 */
 	scoped_guard(raw_spinlock_irqsave, &desc->lock) {
 		if (irq_move_pending(&desc->irq_data))
 			irq_get_pending(cpumask, desc);
@@ -1187,10 +1229,54 @@ static void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *a
 	}
 
 	scoped_guard(raw_spinlock_irq, &desc->lock) {
+		const struct cpumask *target = desc->irq_common_data.affinity;
 		const struct cpumask *m;
+		bool pending, trapped;
 
 		m = irq_data_get_effective_affinity_mask(&desc->irq_data);
-		cpumask_copy(mask, m);
+		/*
+		 * A pending move carries the newest request in the pending
+		 * mask; the descriptor affinity is only updated when the
+		 * move completes.  Use it as the escape target.
+		 */
+		pending = irq_move_pending(&desc->irq_data);
+		if (pending)
+			target = irq_desc_get_pending_mask(desc);
+		/*
+		 * Keep the upstream single-target convergence while the
+		 * hardware is not on a runtime-isolated CPU.  Only a delayed
+		 * move which still points there needs the requested mask as
+		 * the escape target.  The static key keeps the common
+		 * non-isolated case out of RCU and the CPU loop.
+		 */
+		trapped = false;
+		if (irq_isolation_active()) {
+			rcu_read_lock();
+			trapped = cpumask_empty(m) ||
+				  housekeeping_runtime_isolated_intersects(m);
+			rcu_read_unlock();
+		}
+		if (!trapped) {
+			cpumask_copy(mask, m);
+		} else if (!irq_isolation_select_mask(&desc->irq_data, target, mask)) {
+			/*
+			 * Nothing had to be filtered.  A pending move is the
+			 * newest request: when its target is already free of
+			 * runtime-isolated CPUs, the thread must follow it
+			 * instead of the old effective CPU, or it keeps
+			 * running on the isolated CPU until the move
+			 * completes.
+			 */
+			bool clean = false;
+
+			if (pending && irq_isolation_can_filter(&desc->irq_data)) {
+				rcu_read_lock();
+				clean = !housekeeping_runtime_isolated_intersects(target);
+				rcu_read_unlock();
+			}
+			if (!clean || !cpumask_and(mask, target, cpu_online_mask))
+				cpumask_copy(mask, m);
+		}
 	}
 
 	set_cpus_allowed_ptr(current, mask);
diff --git a/kernel/irq/migration.c b/kernel/irq/migration.c
index f2b2929986ffa..39d0e701af52e 100644
--- a/kernel/irq/migration.c
+++ b/kernel/irq/migration.c
@@ -97,6 +97,16 @@ void irq_move_masked_irq(struct irq_data *idata)
 			irqd_set_move_pending(data);
 			return;
 		}
+		/*
+		 * The notifier queued when the move was requested reports
+		 * the requested mask, but the mask a consumer follows only
+		 * changes here.  Report the completed move so that the
+		 * consumer converges on the new target.  Without runtime
+		 * isolation the programmed mask always tracked the request,
+		 * so keep the upstream single notification.
+		 */
+		if (!ret && irq_isolation_active() && desc->affinity_notify)
+			irq_affinity_schedule_notify_work(desc);
 	}
 	cpumask_clear(desc->pending_mask);
 }
-- 
2.43.0


  parent reply	other threads:[~2026-11-30  3:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
2026-11-30  3:36 ` [RFC PATCH 01/10] genirq: Add IRQCHIP_AFFINITY_FILTERABLE flag Kris Pan
2026-11-30  3:37 ` [RFC PATCH 02/10] sched/isolation: Add runtime isolation helpers Kris Pan
2026-11-30  3:37 ` [RFC PATCH 03/10] genirq: Track automatic vs. explicit affinity provenance Kris Pan
2026-11-30  3:37 ` [RFC PATCH 04/10] genirq: Filter unmanaged IRQ affinity against runtime isolation Kris Pan
2026-11-30  3:37 ` [RFC PATCH 05/10] genirq: Reapply unmanaged IRQ affinity on isolation changes Kris Pan
2026-11-30  3:37 ` Kris Pan [this message]
2026-11-30  3:37 ` [RFC PATCH 07/10] genirq: Handle runtime isolation across hotplug and suspend Kris Pan
2026-11-30  3:37 ` [RFC PATCH 08/10] x86/apic: Support runtime IRQ isolation Kris Pan
2026-11-30  3:37 ` [RFC PATCH 09/10] sched/isolation: Publish runtime isolation to genirq Kris Pan
2026-11-30  3:37 ` [RFC PATCH 10/10] net: Keep core-managed IRQ affinity automatic Kris Pan

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=20261130033709.2704805-7-kris.pan@intel.com \
    --to=kris.pan@intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=bhelgaas@google.com \
    --cc=bigeasy@linutronix.de \
    --cc=bp@alien8.de \
    --cc=clrkwllms@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=decui@microsoft.com \
    --cc=dwmw2@infradead.org \
    --cc=edumazet@kernel.org \
    --cc=frederic@kernel.org \
    --cc=haiyangz@microsoft.com \
    --cc=hpa@zytor.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=juri.lelli@redhat.com \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=lpieralisi@kernel.org \
    --cc=maz@kernel.org \
    --cc=mingo@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=peterz@infradead.org \
    --cc=radu@rendec.net \
    --cc=robin.murphy@arm.com \
    --cc=steve.wahl@hpe.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tglx@kernel.org \
    --cc=tj@kernel.org \
    --cc=vasant.hegde@amd.com \
    --cc=vincent.guittot@linaro.org \
    --cc=wei.liu@kernel.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /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