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 07/10] genirq: Handle runtime isolation across hotplug and suspend
Date: Mon, 30 Nov 2026 11:37:05 +0800	[thread overview]
Message-ID: <20261130033709.2704805-8-kris.pan@intel.com> (raw)
In-Reply-To: <20261130033709.2704805-1-kris.pan@intel.com>

CPU hotplug runs with the IRQ chip masked for interrupts which cannot
move in process context, and a suspend must not lose a pending reapply.

The CPU offline path keeps the upstream behaviour: an interrupt whose
request cannot be satisfied is broken to the online CPUs, and the
isolation filter still applies while that emergency mask is
programmed.  A CPU which comes back re-evaluates an unmanaged
interrupt; irq_isolation_restore_affinity() returns true when the
returning CPU is part of the request and the interrupt is still parked
on an isolated CPU, outside its request after the -ENOSPC fallback, or
displaced off the device NUMA node.  This also covers isolation which
was removed while the interrupt sat outside its requested mask.

The CPU-online convergence only runs when runtime isolation was active
at least once; a system which never used it keeps the upstream "leave
unmanaged interrupts alone" behaviour.

A reapply which runs while the interrupt is suspended is remembered in
IRQS_ISOLATION_PENDING and replayed from resume_irq() and
rearm_wake_irq() once IRQS_SUSPENDED is cleared.  The window is real:
the cpuset housekeeping work runs on system_dfl_wq, which is
events_unbound without WQ_FREEZABLE, so an update queued around the
userspace freeze can still execute after dpm_suspend_noirq() has set
IRQS_SUSPENDED.  The replay is best effort and clears the bit once
resume runs; a mask which cannot be programmed right away converges on
the next isolation change or the CPU-online restore.  A user affinity
request supersedes the pending replay, and free_irq() drops it with the
last action.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 kernel/irq/cpuhotplug.c | 12 ++++-
 kernel/irq/debugfs.c    |  1 +
 kernel/irq/internals.h  |  9 ++++
 kernel/irq/irqdesc.c    |  1 +
 kernel/irq/isolation.c  | 97 +++++++++++++++++++++++++++++++++++++++++
 kernel/irq/manage.c     | 19 +++++++-
 kernel/irq/pm.c         | 15 +++++++
 7 files changed, 150 insertions(+), 4 deletions(-)

diff --git a/kernel/irq/cpuhotplug.c b/kernel/irq/cpuhotplug.c
index cd5689e383b00..ee14fc5dd16ff 100644
--- a/kernel/irq/cpuhotplug.c
+++ b/kernel/irq/cpuhotplug.c
@@ -208,8 +208,16 @@ static void irq_restore_affinity_of_irq(struct irq_desc *desc, unsigned int cpu)
 	struct irq_data *data = irq_desc_get_irq_data(desc);
 	const struct cpumask *affinity = irq_data_get_affinity_mask(data);
 
-	if (!irqd_affinity_is_managed(data) || !desc->action ||
-	    !irq_data_get_irq_chip(data) || !cpumask_test_cpu(cpu, affinity))
+	if (!desc->action || !irq_data_get_irq_chip(data))
+		return;
+
+	if (!irqd_affinity_is_managed(data)) {
+		if (irq_isolation_restore_affinity(desc, cpu))
+			irq_reapply_affinity(desc, NULL);
+		return;
+	}
+
+	if (!cpumask_test_cpu(cpu, affinity))
 		return;
 
 	if (irqd_is_managed_and_shutdown(data))
diff --git a/kernel/irq/debugfs.c b/kernel/irq/debugfs.c
index 515c01754f1f0..776289a4f91e2 100644
--- a/kernel/irq/debugfs.c
+++ b/kernel/irq/debugfs.c
@@ -155,6 +155,7 @@ static const struct irq_bit_descr irqdesc_istates[] = {
 	BIT_MASK_DESCR(IRQS_SUSPENDED),
 	BIT_MASK_DESCR(IRQS_NMI),
 	BIT_MASK_DESCR(IRQS_AFFINITY_AUTO),
+	BIT_MASK_DESCR(IRQS_ISOLATION_PENDING),
 };
 
 
diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h
index d2123d140a1c4..2adac3316ea7e 100644
--- a/kernel/irq/internals.h
+++ b/kernel/irq/internals.h
@@ -65,6 +65,7 @@ enum {
  * IRQS_NMI			- irq line is used to deliver NMIs
  * IRQS_SYSFS			- descriptor has been added to sysfs
  * IRQS_AFFINITY_AUTO		- affinity placement was chosen automatically
+ * IRQS_ISOLATION_PENDING	- reapply skipped due to suspend
  */
 enum {
 	IRQS_AUTODETECT		= 0x00000001,
@@ -79,6 +80,7 @@ enum {
 	IRQS_NMI		= 0x00002000,
 	IRQS_SYSFS		= 0x00004000,
 	IRQS_AFFINITY_AUTO	= 0x00008000,
+	IRQS_ISOLATION_PENDING	= 0x00010000,
 };
 
 static inline bool irq_affinity_is_auto(struct irq_desc *desc)
@@ -190,6 +192,7 @@ static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; }
 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_restore_affinity(struct irq_desc *desc, unsigned int cpu);
 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,
@@ -207,6 +210,12 @@ static inline bool irq_isolation_needs_reapply(struct irq_desc *desc,
 	return false;
 }
 
+static inline bool irq_isolation_restore_affinity(struct irq_desc *desc,
+						  unsigned int cpu)
+{
+	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/irqdesc.c b/kernel/irq/irqdesc.c
index 4f578f8d497fd..7c000241784af 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -138,6 +138,7 @@ static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
 	irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
 	irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
 	irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
+	desc->istate &= ~IRQS_ISOLATION_PENDING;
 	irq_affinity_set_auto(desc);
 	desc->handle_irq = handle_bad_irq;
 	desc->depth = 1;
diff --git a/kernel/irq/isolation.c b/kernel/irq/isolation.c
index 6307b7508057e..1d65447f38d6c 100644
--- a/kernel/irq/isolation.c
+++ b/kernel/irq/isolation.c
@@ -38,6 +38,8 @@ 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;
+/* Set once a runtime isolated CPU existed, never cleared. */
+static bool irq_isolation_used;
 
 bool irq_isolation_active(void)
 {
@@ -411,6 +413,99 @@ bool irq_isolation_needs_reapply(struct irq_desc *desc,
 	       !unaffected || needs_restore;
 }
 
+/**
+ * irq_isolation_restore_affinity - Re-evaluate an unmanaged IRQ on CPU online
+ * @desc:	interrupt descriptor, desc->lock held
+ * @cpu:	CPU which just came online
+ *
+ * An AUTO IRQ may be parked on a runtime-isolated CPU or displaced to a
+ * remote NUMA node when no local housekeeping CPU was online.  A returning
+ * CPU may also bring back a request which was filtered away, or complete an
+ * emergency fallback of the CPU offline path.  Return true when @cpu can
+ * improve the placement and the caller should reapply the requested
+ * affinity.
+ */
+bool irq_isolation_restore_affinity(struct irq_desc *desc, unsigned int cpu)
+{
+	const struct cpumask *affinity = irq_data_get_affinity_mask(&desc->irq_data);
+	struct irq_data *data = &desc->irq_data;
+	int node = irq_data_get_node(data);
+	bool isolated_cpu, trapped;
+	const struct cpumask *eff;
+
+	lockdep_assert_held(&desc->lock);
+
+	if (!irq_isolation_can_filter(data) || !irqd_can_balance(data) ||
+	    irqd_affinity_is_managed(data))
+		return false;
+
+	eff = irq_data_get_effective_affinity_mask(data);
+	/*
+	 * A chip which does not report its programming cannot be reasoned
+	 * about; do not rebind on every CPU online event.
+	 */
+	if (cpumask_empty(eff))
+		return false;
+
+	if (!irq_isolation_active()) {
+		/*
+		 * Isolation is gone, but a CPU offline may have parked the
+		 * interrupt outside its requested mask through the -ENOSPC
+		 * emergency fallback.  Nothing else reprograms an unmanaged
+		 * IRQ when a CPU comes online, so converge here.
+		 *
+		 * Only do that on a system which used runtime isolation at
+		 * least once.  A pristine system keeps the upstream "leave
+		 * unmanaged interrupts alone" behaviour, even when the
+		 * vector fallback placed one outside its request.
+		 */
+		if (!READ_ONCE(irq_isolation_used))
+			return false;
+		return cpumask_test_cpu(cpu, affinity) &&
+		       !cpumask_intersects(eff, affinity);
+	}
+
+	rcu_read_lock();
+	isolated_cpu = housekeeping_runtime_isolated_cpu(cpu);
+	if (isolated_cpu) {
+		/*
+		 * A CPU which just entered an isolated partition cannot
+		 * improve any placement; the filter would only remove it
+		 * again.  Return before the NUMA and request checks so that
+		 * the caller does not program the chip and notify for
+		 * nothing.
+		 */
+		rcu_read_unlock();
+		return false;
+	}
+	trapped = housekeeping_runtime_isolated_intersects(eff);
+	rcu_read_unlock();
+
+	if (!cpumask_test_cpu(cpu, affinity)) {
+		/*
+		 * Do not redirect an explicit mask which does not contain
+		 * the new CPU.  An AUTO fallback, however, may still be
+		 * parked on a runtime-isolated CPU or on a remote NUMA
+		 * node.  Only a request which wants the device node counts
+		 * as displaced; a queue which was hinted onto another node
+		 * is already where it belongs.
+		 */
+		return irq_affinity_is_auto(desc) &&
+		       (trapped ||
+			(node != NUMA_NO_NODE && cpu_to_node(cpu) == node &&
+			 cpumask_intersects(affinity, cpumask_of_node(node)) &&
+			 !cpumask_intersects(eff, cpumask_of_node(node))));
+	}
+
+	/*
+	 * The CPU is back in the requested mask.  Reapply only if the IRQ
+	 * is still parked on an isolated CPU or its effective mask no
+	 * longer intersects the request; avoid rebinding every unmanaged
+	 * IRQ on each CPU online event.
+	 */
+	return trapped || !cpumask_intersects(eff, affinity);
+}
+
 static void irq_isolation_reapply_all(const struct cpumask *changed)
 {
 	unsigned int irq;
@@ -499,6 +594,8 @@ void irq_isolation_update(void)
 	housekeeping_runtime_isolated(irq_isolation_curr);
 	rcu_read_unlock();
 	active = !cpumask_empty(irq_isolation_curr);
+	if (active)
+		WRITE_ONCE(irq_isolation_used, true);
 
 	if (cpumask_equal(irq_isolation_old, irq_isolation_curr))
 		return;
diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
index 6e6fdae14bb07..06bfaaa9d6b23 100644
--- a/kernel/irq/manage.c
+++ b/kernel/irq/manage.c
@@ -231,7 +231,8 @@ static inline void irq_validate_effective_affinity(struct irq_data *data) { }
 
 static DEFINE_PER_CPU(struct cpumask, __tmp_mask);
 
-int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
+int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
+			bool force)
 {
 	struct cpumask *tmp_mask = this_cpu_ptr(&__tmp_mask);
 	struct irq_desc *desc = irq_data_to_desc(data);
@@ -386,8 +387,11 @@ void irq_reapply_affinity(struct irq_desc *desc, const struct cpumask *changed)
 	if (changed && !irq_isolation_needs_reapply(desc, changed))
 		return;
 
-	if (desc->istate & IRQS_SUSPENDED)
+	if (desc->istate & IRQS_SUSPENDED) {
+		desc->istate |= IRQS_ISOLATION_PENDING;
 		return;
+	}
+	desc->istate &= ~IRQS_ISOLATION_PENDING;
 
 	if (irq_can_move_pcntxt(data)) {
 		int ret;
@@ -493,6 +497,11 @@ int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
 	if (irq_set_affinity_deactivated(data, mask))
 		return 0;
 
+	/*
+	 * A replay which was skipped because the IRQ was suspended is
+	 * superseded by this request.  The notifier is scheduled below.
+	 */
+	desc->istate &= ~IRQS_ISOLATION_PENDING;
 	if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
 		ret = irq_try_set_affinity(data, mask, force);
 	} else {
@@ -2171,6 +2180,12 @@ static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
 	/* If this was the last handler, shut down the IRQ line: */
 	if (!desc->action) {
 		irq_settings_clr_disable_unlazy(desc);
+		/*
+		 * AUTO and IRQD_AFFINITY_SET describe the current placement
+		 * and must survive close/reopen.  Only the per-request
+		 * isolation replay state is cleared here.
+		 */
+		desc->istate &= ~IRQS_ISOLATION_PENDING;
 		/* Only shutdown. Deactivate after synchronize_hardirq() */
 		irq_shutdown(desc);
 	}
diff --git a/kernel/irq/pm.c b/kernel/irq/pm.c
index 99ff65466d871..63b59e610f4af 100644
--- a/kernel/irq/pm.c
+++ b/kernel/irq/pm.c
@@ -170,6 +170,16 @@ static void resume_irq(struct irq_desc *desc)
 	irq_state_set_masked(desc);
 resume:
 	desc->istate &= ~IRQS_SUSPENDED;
+	if (desc->istate & IRQS_ISOLATION_PENDING) {
+		desc->istate &= ~IRQS_ISOLATION_PENDING;
+		/*
+		 * Best effort: a replay which cannot program the mask now
+		 * is retried by the next isolation change or by the CPU
+		 * online restore, so do not keep the bit set on an
+		 * interrupt which is no longer suspended.
+		 */
+		irq_reapply_affinity(desc, NULL);
+	}
 	__enable_irq(desc);
 }
 
@@ -204,6 +214,11 @@ void rearm_wake_irq(unsigned int irq)
 			return;
 
 		desc->istate &= ~IRQS_SUSPENDED;
+		if (desc->istate & IRQS_ISOLATION_PENDING) {
+			desc->istate &= ~IRQS_ISOLATION_PENDING;
+			/* Best effort, see resume_irq(). */
+			irq_reapply_affinity(desc, NULL);
+		}
 		irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED);
 		__enable_irq(desc);
 	}
-- 
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 ` [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation Kris Pan
2026-11-30  3:37 ` Kris Pan [this message]
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-8-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