Linux Documentation
 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 05/10] genirq: Reapply unmanaged IRQ affinity on isolation changes
Date: Mon, 30 Nov 2026 11:37:03 +0800	[thread overview]
Message-ID: <20261130033709.2704805-6-kris.pan@intel.com> (raw)
In-Reply-To: <20261130033709.2704805-1-kris.pan@intel.com>

The filter only affects new placements.  An interrupt which is already
programmed on a CPU handed to an isolated partition must be re-evaluated
when the partition mask changes.

Keep the previous runtime isolated set and walk the active descriptors
on every change.  The mask policy lives in isolation.c:
irq_isolation_needs_reapply() decides whether the change can affect the
interrupt, so the walk does not program chips or schedule notifier work
for descriptors which are unaffected.  irq_reapply_affinity() keeps the
state machine in manage.c and reprograms the requested mask through the
regular affinity path, so the current filter is applied while the
descriptor keeps its original request.

A CPU which leaves a partition hands the full requested mask to the
irqchip again even when the effective mask still intersects the
request, so a chip which can place the interrupt on another target
includes the returning CPU.  This also re-marks the IRQ threads and
schedules the affinity notifier, which is what lets a consumer which
follows the programmed mask drop the filtered state after the
partition is gone; the request itself never changed.

A CPU which enters isolation is re-applied when a consumer follows the
programmed mask: the request loses a target, so the notifier runs and
the reverse map drops that CPU.  An interrupt without a notifier keeps
its current target, so an entering CPU does not reprogram every
interrupt in the machine.  While a move is queued the pending mask
carries the newest request and is checked as well.

An automatic placement which lost its last online target sits outside
its request; its emergency mask is derived from the housekeeping set,
so every isolation change re-applies it and the reverse map converges.
A chip which reports no effective affinity is not evidence of a
displacement: the de-isolation convergence skips an empty effective
mask.

The walk runs under one cpus_read_lock() taken together with the static
key update; the descriptors are then taken one by one under desc->lock.
The order is irq_isolation_mutex -> cpus_read_lock -> sparse_irq_lock ->
desc->lock.  The masks are allocated once in an early initcall.

A chip which cannot move in process context defers the reprogram to
interrupt context through the existing pending mechanism.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 include/linux/sched/isolation.h |  10 ++
 kernel/irq/internals.h          |  14 ++
 kernel/irq/isolation.c          | 265 +++++++++++++++++++++++++++++++-
 kernel/irq/manage.c             |  60 ++++++++
 4 files changed, 348 insertions(+), 1 deletion(-)

diff --git a/include/linux/sched/isolation.h b/include/linux/sched/isolation.h
index cec4e051dfab9..66bda7eb6d3cc 100644
--- a/include/linux/sched/isolation.h
+++ b/include/linux/sched/isolation.h
@@ -91,6 +91,14 @@ void housekeeping_runtime_isolated(struct cpumask *out);
 bool housekeeping_runtime_isolated_cpu(unsigned int cpu);
 bool housekeeping_runtime_isolated_intersects(const struct cpumask *mask);
 
+/*
+ * Provided by the generic IRQ layer: called when the runtime-isolated set
+ * changes, so that genirq can adapt the programmed affinities.  Declared
+ * here so that the housekeeping code does not have to include
+ * <linux/irq.h>.
+ */
+void irq_isolation_update(void);
+
 #else
 static inline void housekeeping_runtime_isolated(struct cpumask *out)
 {
@@ -107,6 +115,8 @@ housekeeping_runtime_isolated_intersects(const struct cpumask *mask)
 {
 	return false;
 }
+
+static inline void irq_isolation_update(void) { }
 #endif /* CONFIG_CPU_ISOLATION */
 
 static inline bool housekeeping_cpu(int cpu, enum hk_type type)
diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h
index abadf24b3d0eb..d2123d140a1c4 100644
--- a/kernel/irq/internals.h
+++ b/kernel/irq/internals.h
@@ -187,12 +187,26 @@ static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; }
 #endif
 
 #ifdef CONFIG_CPU_ISOLATION
+void irq_reapply_affinity(struct irq_desc *desc, const struct cpumask *changed);
+bool irq_isolation_needs_reapply(struct irq_desc *desc,
+				 const struct cpumask *changed);
 bool irq_isolation_widen_mask(struct irq_desc *desc,
 			      const struct cpumask *mask, struct cpumask *out);
 bool irq_isolation_widen_mask_full(struct irq_desc *desc,
 				   const struct cpumask *mask,
 				   struct cpumask *out);
 #else
+static inline void irq_reapply_affinity(struct irq_desc *desc,
+					const struct cpumask *changed)
+{
+}
+
+static inline bool irq_isolation_needs_reapply(struct irq_desc *desc,
+					       const struct cpumask *changed)
+{
+	return false;
+}
+
 static inline bool irq_isolation_widen_mask(struct irq_desc *desc,
 					    const struct cpumask *mask,
 					    struct cpumask *out)
diff --git a/kernel/irq/isolation.c b/kernel/irq/isolation.c
index af7244ce181a4..4ff45da90f909 100644
--- a/kernel/irq/isolation.c
+++ b/kernel/irq/isolation.c
@@ -4,22 +4,40 @@
  *
  * The requested affinity of an interrupt is never changed here.  A mask
  * derived from housekeeping tells irq_do_set_affinity() which CPUs must not
- * be programmed into the chip.
+ * be programmed into the chip.  When the mask changes, active interrupts are
+ * asked to reapply their requested affinity so that hardware converges
+ * without losing the original request.
  */
 
+#include <linux/bug.h>
+#include <linux/cleanup.h>
+#include <linux/cpu.h>
+#include <linux/cpuhplock.h>
 #include <linux/cpumask.h>
+#include <linux/errno.h>
+#include <linux/gfp.h>
+#include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irqdesc.h>
+#include <linux/lockdep.h>
+#include <linux/mutex.h>
+#include <linux/printk.h>
 #include <linux/rcupdate.h>
 #include <linux/sched.h>
 #include <linux/sched/isolation.h>
+#include <linux/spinlock.h>
 #include <linux/static_key.h>
 #include <linux/topology.h>
 
 #include "internals.h"
 
 static DEFINE_STATIC_KEY_FALSE(irq_isolation_key);
+static DEFINE_MUTEX(irq_isolation_mutex);
+static cpumask_var_t irq_isolation_old;
+static cpumask_var_t irq_isolation_curr;
+static cpumask_var_t irq_isolation_changed;
+static bool irq_isolation_ready;
 
 bool irq_isolation_active(void)
 {
@@ -221,3 +239,248 @@ bool irq_isolation_widen_mask_full(struct irq_desc *desc,
 	return !cpumask_empty(out) && !cpumask_subset(out, mask);
 }
 
+/*
+ * Return true when @changed contains a CPU which left a runtime-isolated
+ * partition and is part of @affinity.  Such a CPU was filtered out before
+ * and is a valid target again.
+ *
+ * Must be called under RCU read lock.
+ */
+static bool irq_reapply_restores_target(const struct cpumask *changed,
+					const struct cpumask *affinity)
+{
+	unsigned int cpu;
+
+	for_each_cpu_and(cpu, changed, affinity) {
+		if (!housekeeping_runtime_isolated_cpu(cpu))
+			return true;
+	}
+
+	return false;
+}
+
+/**
+ * irq_isolation_needs_reapply - Does @changed affect this interrupt?
+ * @desc:	Interrupt descriptor, desc->lock held
+ * @changed:	Old/current difference of the runtime-isolated set
+ *
+ * A change may isolate a CPU the interrupt is running on, take a CPU out
+ * of the requested mask, return a CPU to it, move the programmed mask, or
+ * leave an emergency fallback outside the request while isolation is
+ * going away.  Only the interrupts where one of those applies have to be
+ * re-applied; everything else keeps its programming untouched.  A CPU
+ * which enters isolation while it sits in the request changes the policy
+ * mask even when the chip already targets another member, so a registered
+ * notifier has to run and the reverse map has to drop it.  An automatic
+ * placement which sits outside its request is in the emergency fallback;
+ * its mask follows the housekeeping set, so every change reaches it.
+ *
+ * Returns: true when the interrupt has to be re-applied.
+ */
+bool irq_isolation_needs_reapply(struct irq_desc *desc,
+				 const struct cpumask *changed)
+{
+	struct irq_data *data = &desc->irq_data;
+	const struct cpumask *eff, *affinity;
+	bool restored, trapped = false;
+	bool unaffected, needs_restore, notify_changed;
+
+	lockdep_assert_held(&desc->lock);
+	WARN_ON_ONCE(!changed);
+
+	eff = irq_data_get_effective_affinity_mask(data);
+	affinity = desc->irq_common_data.affinity;
+
+	rcu_read_lock();
+
+	/*
+	 * An automatic placement may be stuck on an isolated CPU, or
+	 * parked outside its request after the isolation went away.
+	 *
+	 * While isolation is active, an automatic placement which sits
+	 * outside its request is in the emergency fallback: its programmed
+	 * mask is derived from the housekeeping set, not from the request,
+	 * so every isolation change can change it.  Converge it even when
+	 * the change does not touch the current CPU.
+	 */
+	if (irq_affinity_is_auto(desc)) {
+		if (irq_isolation_active())
+			trapped = cpumask_empty(eff) ||
+				  housekeeping_runtime_isolated_intersects(eff) ||
+				  (!cpumask_empty(eff) &&
+				   !cpumask_intersects(eff, affinity));
+		else
+			trapped = !cpumask_empty(eff) &&
+				  !cpumask_intersects(eff, affinity);
+	}
+
+	/*
+	 * A CPU which left a runtime-isolated partition is a valid target
+	 * again.  Hand the full requested mask to the irqchip again even
+	 * when the current effective mask still intersects it, so a chip
+	 * which can place the interrupt on another target includes the
+	 * returning CPU, and so that the notifier and the IRQ threads see
+	 * the restored request.  The request itself is never changed.
+	 */
+	restored = irq_reapply_restores_target(changed, affinity);
+
+	/*
+	 * A CPU which enters isolation changes the policy mask even if the
+	 * chip is not delivering on it: the request loses a target, so a
+	 * consumer which follows the programmed mask has to be told.
+	 * Only the notifier consumer needs the update, and only it gets
+	 * the chip reprogrammed; an interrupt without a notifier keeps its
+	 * current target and does not pay for a hardware write.  While a
+	 * move is queued, the pending mask carries the newest request and
+	 * is the mask the notifier reports.
+	 */
+	notify_changed = desc->affinity_notify &&
+			 (cpumask_intersects(changed, affinity) ||
+			  (irq_move_pending(data) &&
+			   cpumask_intersects(changed,
+					      irq_desc_get_pending_mask(desc))));
+
+	rcu_read_unlock();
+
+	/*
+	 * A CPU which just left a runtime-isolated partition must pull back
+	 * an interrupt that was displaced away from it, even while other
+	 * partitions remain active.
+	 */
+	if (cpumask_intersects(changed, affinity) &&
+	    !cpumask_intersects(eff, affinity))
+		trapped = true;
+
+	/*
+	 * An empty effective mask means the chip does not report its
+	 * programming; treat the programmed placement as unaffected here
+	 * and let @trapped, @restored and @notify_changed decide.  A
+	 * change which does not touch the effective CPU and does not
+	 * change the request leaves the chip alone.
+	 */
+	unaffected = cpumask_empty(eff) || !cpumask_intersects(changed, eff);
+	/*
+	 * With runtime isolation gone the -ENOSPC emergency fallback may
+	 * have parked the interrupt outside its requested mask; converge it
+	 * even when @changed did not touch the programmed mask.
+	 */
+	needs_restore = !irq_isolation_active() && !cpumask_empty(eff) &&
+			!cpumask_intersects(eff, affinity);
+
+	return trapped || restored || notify_changed ||
+	       !unaffected || needs_restore;
+}
+
+static void irq_isolation_reapply_all(const struct cpumask *changed)
+{
+	unsigned int irq;
+
+	lockdep_assert_cpus_held();
+	irq_lock_sparse();
+	for_each_active_irq(irq) {
+		struct irq_desc *desc = irq_to_desc(irq);
+
+		if (!desc)
+			continue;
+
+		/*
+		 * Lockless pre-check to keep the walk short.  The two bits
+		 * are written under desc->lock by request_irq() and
+		 * irq_update_affinity_desc(), so this read is advisory;
+		 * irq_reapply_affinity() re-checks them under the lock.
+		 */
+		if (irqd_is_per_cpu(&desc->irq_data) ||
+		    irqd_affinity_is_managed(&desc->irq_data))
+			continue;
+
+		raw_spin_lock_irq(&desc->lock);
+		irq_reapply_affinity(desc, changed);
+		raw_spin_unlock_irq(&desc->lock);
+		cond_resched();
+	}
+	irq_unlock_sparse();
+}
+
+static int __init irq_isolation_init(void)
+{
+	if (!zalloc_cpumask_var(&irq_isolation_old, GFP_KERNEL) ||
+	    !zalloc_cpumask_var(&irq_isolation_curr, GFP_KERNEL) ||
+	    !zalloc_cpumask_var(&irq_isolation_changed, GFP_KERNEL)) {
+		pr_err("genirq: Failed to allocate runtime IRQ isolation masks\n");
+		free_cpumask_var(irq_isolation_old);
+		free_cpumask_var(irq_isolation_curr);
+		free_cpumask_var(irq_isolation_changed);
+		return -ENOMEM;
+	}
+
+	irq_isolation_ready = true;
+	return 0;
+}
+early_initcall(irq_isolation_init);
+
+/**
+ * irq_isolation_update - reapply a new runtime IRQ isolation mask
+ *
+ * Called from process context after housekeeping has committed the new
+ * runtime isolation set.  The set is not stored in genirq; filtering reads
+ * the current housekeeping difference directly.  Only the previous/current
+ * difference is kept to compute the reapply delta.
+ *
+ * Context: must be called without cpus_read_lock() or cpus_write_lock()
+ * held.  The read lock is taken around the static key update and the
+ * reapply walk to keep cpu_online_mask stable while programming the chips.
+ */
+void irq_isolation_update(void)
+{
+	bool active;
+
+	if (!irq_isolation_ready)
+		return;
+
+	/*
+	 * The CPU hotplug lock is acquired below.  A caller which already
+	 * holds it, for example a CPU hotplug callback under
+	 * cpus_write_lock(), would deadlock when this function takes the
+	 * read lock; taking it there would also invert the
+	 * cpuset_top_mutex -> cpus_read_lock order this design relies on.
+	 */
+#ifdef CONFIG_HOTPLUG_CPU
+	/*
+	 * Without hotplug the cpus lock helpers are stubs which report the
+	 * lock as held, so the assertion would fire spuriously; it only
+	 * makes sense when the lock exists.
+	 */
+	lockdep_assert(!lockdep_is_cpus_held());
+#endif
+
+	guard(mutex)(&irq_isolation_mutex);
+
+	rcu_read_lock();
+	housekeeping_runtime_isolated(irq_isolation_curr);
+	rcu_read_unlock();
+	active = !cpumask_empty(irq_isolation_curr);
+
+	if (cpumask_equal(irq_isolation_old, irq_isolation_curr))
+		return;
+
+	cpumask_xor(irq_isolation_changed, irq_isolation_old,
+		    irq_isolation_curr);
+	cpumask_copy(irq_isolation_old, irq_isolation_curr);
+
+	/*
+	 * Keep irq_isolation_mutex held while the changed mask is consumed,
+	 * and take the CPU hotplug read lock once for the static key update
+	 * and the reapply walk.  static_branch_enable() would take it again
+	 * internally; entering both under one lock also keeps a CPU coming
+	 * online in between from observing a half-updated state.  The
+	 * established order is irq_isolation_mutex -> cpus_read_lock ->
+	 * sparse_irq_lock -> desc->lock.
+	 */
+	cpus_read_lock();
+	if (active && !static_key_enabled(&irq_isolation_key))
+		static_branch_enable_cpuslocked(&irq_isolation_key);
+	else if (!active && static_key_enabled(&irq_isolation_key))
+		static_branch_disable_cpuslocked(&irq_isolation_key);
+	irq_isolation_reapply_all(irq_isolation_changed);
+	cpus_read_unlock();
+}
diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
index 25f42256aeabf..76c8cb679f4cc 100644
--- a/kernel/irq/manage.c
+++ b/kernel/irq/manage.c
@@ -354,6 +354,66 @@ static int irq_try_set_affinity(struct irq_data *data,
 	return ret;
 }
 
+#ifdef CONFIG_CPU_ISOLATION
+/*
+ * Reapply the requested affinity after a global isolation change.
+ * Does not set IRQD_AFFINITY_SET.
+ */
+void irq_reapply_affinity(struct irq_desc *desc, const struct cpumask *changed)
+{
+	struct irq_chip *chip = irq_desc_get_chip(desc);
+	struct irq_data *data = &desc->irq_data;
+
+	lockdep_assert_held(&desc->lock);
+
+	if (!chip || !chip->irq_set_affinity)
+		return;
+	if (!irq_isolation_can_filter(data))
+		return;
+	if (irqd_affinity_is_managed(data))
+		return;
+	if (!irqd_can_balance(data))
+		return;
+	if (!irqd_is_activated(data))
+		return;
+
+	/*
+	 * The caller holds desc->lock, so the query sees a stable request
+	 * and effective mask.  Skip interrupts which cannot be affected by
+	 * the changed set; the pre-check in the caller is only advisory
+	 * and avoids taking desc->lock for clearly inactive descriptors.
+	 */
+	if (changed && !irq_isolation_needs_reapply(desc, changed))
+		return;
+
+	if (desc->istate & IRQS_SUSPENDED)
+		return;
+
+	if (irq_can_move_pcntxt(data)) {
+#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);
+		return;
+	}
+
+#ifdef CONFIG_GENERIC_PENDING_IRQ
+pending:
+	/*
+	 * Do not overwrite an existing pending request.  It may be a newer
+	 * user request which has not completed yet; the migration path
+	 * applies the current isolation mask when it consumes it.
+	 */
+	if (!irqd_is_setaffinity_pending(data)) {
+		irqd_set_move_pending(data);
+		irq_copy_pending(desc, desc->irq_common_data.affinity);
+	}
+#endif
+}
+#endif /* CONFIG_CPU_ISOLATION */
+
 static bool irq_set_affinity_deactivated(struct irq_data *data,
 					 const struct cpumask *mask)
 {
-- 
2.43.0


  parent reply	other threads:[~2026-11-30  3:40 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 ` Kris Pan [this message]
2026-11-30  3:37 ` [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation Kris Pan
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-6-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