mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs
@ 2026-11-30  3:36 Kris Pan
  2026-11-30  3:36 ` [RFC PATCH 01/10] genirq: Add IRQCHIP_AFFINITY_FILTERABLE flag Kris Pan
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:36 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

cpuset.cpus.partition=isolated removes CPUs from scheduler load
balancing and HK_TYPE_DOMAIN housekeeping at runtime, but genirq does
not consume that state.  Drivers requesting IRQs after container
start, reopening an interface, or resetting hardware can still route
unmanaged interrupts onto isolated CPUs.

  - Patch 1 defines the IRQCHIP_AFFINITY_FILTERABLE flag and its
    hierarchy contract.
  - Patch 2 adds the sched/isolation runtime-isolated helper.
  - Patch 3 records the placement provenance (automatic or explicit)
    and adds the core-only irq_set_affinity_auto() helper; a bare
    irq_set_affinity() is an explicit pin.
  - Patch 4 adds the filter, the shared mask selection and the
    late-IRQF_NOBALANCING re-align.
  - Patch 5 reapplies the requested affinity when the isolation set
    changes; the policy query lives in isolation.c.
  - Patch 6 keeps the requested mask in the affinity notifier, reports
    a deferred move again when it completes, and aligns the IRQ threads
    with the filtered mask; consumers which build a reverse map follow
    the programmed mask.
  - Patch 7 handles hotplug, suspend/resume replay and the CPU-online
    restore.
  - Patch 8 opts x86 APIC in, filters its vector fallbacks and marks
    the forwarding irqchips PASSTHROUGH.
  - Patch 9 publishes runtime isolation and documents the boundaries.
  - Patch 10 keeps the netdev core-managed affinity automatic and
    steers its ARFS map by the programmed mask while runtime isolation
    is active; it is the consumer of patches 3 and 6.

Runtime-isolated CPUs are detected as DOMAIN_BOOT - DOMAIN.  A mask
which still has an online target after that subtraction keeps its
boot-time isolcpus=domain CPUs, whether it was placed automatically or
explicitly.  Only when no online target remains may an automatically
placed IRQ fall back to online HK_TYPE_DOMAIN CPUs, preferring the
nodes the request covers and then irq_default_affinity and the device
node, so a queue hinted onto another socket is not pulled back.  The
x86 vector fallback
subtracts only the runtime set, so boot isolcpus=domain fallback
targets do not change; a CPU which is both boot-isolated and part of
an isolated partition is not in the runtime set either, so its
interrupt behaviour is unchanged.  The -ENOSPC retry in the core honours
irq_default_affinity first and then widens to the other online
HK_TYPE_DOMAIN CPUs, so a boot-time isolcpus=domain CPU is never used
as an emergency target and the request itself stays the last resort.

Automatic placement is tracked in a genirq-private IRQS_AFFINITY_AUTO
state bit in desc->istate rather than in a public IRQD bit or in
status_use_accessors: it is core-internal, survives
free_irq()/request_irq(), and irq_modify_status() cannot reach it.
Default placement, unmanaged affinity descriptors, the hint API
(irq_set_affinity_and_hint() and its deprecated alias) and the
core-only irq_set_affinity_auto() record automatic placement.  A bare
irq_set_affinity() is an explicit pin, the safe default for per-CPU
PMU, timer and architecture code; a driver which wants a queue to
follow the housekeeping set uses the hint API, so no driver has to
change.  IRQF_NOBALANCING is applied after the interrupt is activated,
so a default placement still starts on a housekeeping CPU; an explicit
request which was narrowed before the flag was applied is then
programmed again, because the filter is inert for a non-balancing
interrupt.  The replay runs through the regular affinity path, so a
chip which cannot move in process context reprograms it from interrupt
context.  It only runs while runtime isolation is active, on a chip
which opted in, for a request which was actually set and still has an
online target.  /proc writes and irq_force_affinity() are
explicit and are never redirected: while the mask keeps an online
non-isolated target the filter still subtracts the runtime-isolated
CPUs, but the request itself remains the last resort and is never
widened to CPUs outside it.  A default placement has no target of its
own and is still chosen among housekeeping CPUs when IRQF_NOBALANCING
is passed to request_irq(); a driver which pre-marks the IRQ with
irq_set_status_flags() skips both steps.

Per-CPU and managed interrupts are excluded.  The affinity notifier
keeps reporting the requested mask for the pending and programmed
paths, as it does upstream: a consumer which stores the notified mask
as the request (net/core does) must not be handed the filtered policy,
or removing the partition later cannot restore the CPU.  A consumer
which spreads work over several CPUs follows the mask the core asks
the chip to program, irq_copy_programmed_affinity(): the request minus
the runtime-isolated CPUs, or the automatic relocation.  The effective
affinity is deliberately not used there; on chips with
IRQD_SINGLE_TARGET it is one CPU, and a reverse map built from it would
collapse to a single entry.  Patch 10 converts net/core and
lib/cpu_rmap.c.  A driver which registers its own irq_affinity_notify
callback and steers hardware to a single CPU (bnxt TPH) is left
unchanged here; it keeps following the notified request, and
converting it needs a per-driver decision about which CPU actually
handles the interrupt.  The other custom callbacks (i40e's polling
check, the mask caches of ionic, funeth, hfi1, qla2xxx and qedf) are
left on the request for the same reason; only the reverse-map
consumers are converted.
The notifier is scheduled from the reapply path as well, and a deferred
move is reported when it is queued and again when the pending mask has
been programmed, so a consumer which follows the programmed mask
converges on the new target.  A CPU which enters isolation re-applies
the interrupts which have a notifier consumer even when the chip
already targets another member of the request, because the policy mask
lost that CPU and a reverse map has to drop it.  Without a notifier
nothing observes the change, so the chip is left alone.  An explicit mask which loses every
online target is not relocated and keeps its request.  When a CPU leaves a
partition the preserved request is handed to the irqchip again even if the
current effective mask still intersects it, so a chip which can place the
interrupt elsewhere includes the returning CPU and the notifier and the
IRQ threads see the restored request.  A single-target IRQ is no
exception: x86 marks its vector IRQs IRQD_SINGLE_TARGET, so the chip
keeps the current target, but the re-apply still restores the reverse
map and the thread mask.  A broad mask whose IRQs were all evicted to
one housekeeping CPU therefore stays there physically until a balancer
or an explicit affinity write moves it.  An automatic IRQ whose whole
request became isolated is different: the fallback moved it outside the
request, so its current CPU is not in the mask and the chip does move
it back when the request becomes placeable again.  A CPU which returns
also completes an automatic fallback which had parked an interrupt
outside its requested mask after -ENOSPC.  A threaded IRQ escapes to
the requested mask when its delayed hardware move still points at a
runtime-isolated CPU; the move marks the IRQ threads when it is queued,
not only when it completes.  Reapply runs under cpuset_top_mutex ->
irq_isolation_mutex -> cpus_read_lock -> sparse_irq_lock -> desc->lock,
with the CPU hotplug read lock taken once around the static key update
and the reapply walk.

Points for maintainer feedback:

  - Placement provenance: a bare irq_set_affinity() is explicit, the
    hint API keeps automatic placement, and core code which manages an
    affinity on a driver's behalf uses irq_set_affinity_auto().  The
    helper is not exported and its only consumer is net/core (patch
    10), so no driver changes.  A tree-wide audit of all 69 bare
    irq_set_affinity() call sites confirms that none binds a
    multiqueue NIC queue (they are per-CPU PMU/timer/portal,
    irqchip/MSI-mux, or explicit management pins), whereas multiqueue
    drivers already use the hint API (129 call sites tree-wide, 69
    under drivers/net) or core-managed NAPI/PCI affinity.  Is that the
    preferred shape?

  - Hierarchy contract for IRQCHIP_AFFINITY_FILTERABLE.  Every level
    of the parent chain which implements .irq_set_affinity has to
    declare itself either FILTERABLE (the level which programs
    delivery) or PASSTHROUGH (a forwarder which derives no state from
    the mask); an unmarked level keeps filtering off for the whole
    chain.  This makes an unaudited domain default-off instead of
    inheriting the capability from its parent.  On x86 the vector
    domain is FILTERABLE and the IO-APIC, PCI-MSI, DMAR, HPET,
    INTEL-IR, AMD-IR and HYPERV-IR levels, the Hyper-V PCI MSI parents
    and the UV core domain are PASSTHROUGH.  ARM64/
    GICv3-ITS would have to mark the ITS level explicitly before it
    can be enabled.  Is this the
    contract you want, or should a delivery level be able to opt in on
    behalf of its children?

  - Trigger policy vs. genirq mechanism (LPC 2026 cgroup IRQ isolation
    discussions): this RFC wires the filter directly to runtime
    HK_TYPE_DOMAIN changes (DOMAIN_BOOT - DOMAIN) so existing
    cpuset.cpus.partition=isolated users get unmanaged IRQ eviction out
    of the box.  We are aware of the discussions around exposing a
    dedicated cgroup knob or a separate housekeeping type for IRQ
    isolation, and of the subsystem-specific queue management for
    networking.  The genirq filtering and x86 vector mechanics in
    patches 1 and 4-8 are intentionally decoupled from how the isolated
    mask is configured, and can be switched to a dedicated housekeeping
    mask or cgroup toggle with a contained change to
    housekeeping_runtime_isolated().

Testing:

  - x86_64 and ARM64 build with W=1 and no warnings in the touched
    files.
  - focused KVM test, 8 vCPUs, with isolcpus=domain,2 and also without
    boot-time isolcpus: automatic and explicit filtering, the
    boot-isolated mask preservation, the -ENOSPC fallback, the
    late-IRQF_NOBALANCING replay, the threaded IRQ escape, de-isolation
    restore, the explicit pin and the empty-effective-affinity case all
    pass.
  - a QEMU boot with intel-iommu,intremap=on and an e1000e NIC: a
    PCIe MSI-X IRQ behind INTEL-IR is moved off the isolated CPU when
    its request is widened to include that CPU, which exercises the
    FILTERABLE/PASSTHROUGH walk on the real PCI-MSI -> INTEL-IR ->
    vector chain.
  - scaling up to 4096 unmanaged filterable IRQs, all evicted from and
    restored to the isolated CPU, adds ~1 ms (~0.24 us/IRQ) over the
    ~8.5 ms no-IRQ baseline; the cpuset write path remains dominated by
    housekeeping_update().  Those are whole-request evictions: the
    fallback left them outside the request, so the chip moves them back
    on de-isolation.  A broad mask which was only narrowed is the case
    which is not re-spread.

Boundaries:

  - managed IRQs, queue reallocation and APIC vector compression are
    out of scope;
  - x86 APIC without interrupt remapping migrates on the next
    interrupt;
  - irqbalance must be disabled from boot: stopping it later does not
    restore the automatic placement of the single-CPU /proc pins it
    already wrote.  Writing a broad mask back keeps it explicit but
    still filters the runtime-isolated CPUs; only an IRQ whose entire
    online mask is isolated loses the fallback to other housekeeping
    CPUs.  A balancer which reads cpuset.cpus.isolated is the
    alternative; it cannot close the window in which a driver calls
    request_irq() or pci_alloc_irq_vectors() while the container is
    already running, and there is no balancer on a no-daemon edge
    system.  The kernel-side filter covers that window;
  - de-isolation hands the preserved request to the irqchip again and
    restores the reverse map, but does not force a re-spread.  A
    single-target IRQ (x86 marks its vector IRQs IRQD_SINGLE_TARGET)
    whose effective CPU is still in the request keeps that CPU, because
    the x86 vector code does not move an interrupt whose target is
    still valid; IRQs which were only narrowed (a broad mask evicted to
    one housekeeping CPU) stay there physically after the partition is
    gone, so a balancer which reads cpuset.cpus.isolated, or a new
    affinity write, is needed to spread them again.  An IRQ whose whole
    request was isolated is moved back, because the fallback left it
    outside the request;
  - on PREEMPT_RT the IRQ thread processes IRQTF_AFFINITY at the top of
    its wait loop and blocks in set_cpus_allowed_ptr() until it has
    been moved.  The wake-up is queued against the old scheduling
    affinity, so the thread can still be scheduled once on the isolated
    CPU before it migrates; the handler itself runs on the housekeeping
    CPU.  Keep latency-critical RT tasks at a higher real-time priority
    than the threaded IRQ default, or let the device settle before
    entering the critical loop;
  - only the x86 APIC vector chip opts in today; ARM64 is
    compile-tested only, GICv3/ITS does not opt in yet.

Kris Pan (10):
  genirq: Add IRQCHIP_AFFINITY_FILTERABLE flag
  sched/isolation: Add runtime isolation helpers
  genirq: Track automatic vs. explicit affinity provenance
  genirq: Filter unmanaged IRQ affinity against runtime isolation
  genirq: Reapply unmanaged IRQ affinity on isolation changes
  genirq: Align affinity notifiers and IRQ threads with isolation
  genirq: Handle runtime isolation across hotplug and suspend
  x86/apic: Support runtime IRQ isolation
  sched/isolation: Publish runtime isolation to genirq
  net: Keep core-managed IRQ affinity automatic

 Documentation/admin-guide/cpu-isolation.rst | 102 ++++
 arch/x86/hyperv/irqdomain.c                 |   4 +-
 arch/x86/kernel/apic/io_apic.c              |   6 +-
 arch/x86/kernel/apic/msi.c                  |   8 +-
 arch/x86/kernel/apic/vector.c               | 105 +++-
 arch/x86/kernel/hpet.c                      |   3 +-
 arch/x86/platform/uv/uv_irq.c               |   1 +
 drivers/iommu/amd/iommu.c                   |   1 +
 drivers/iommu/hyperv/hv-irq-remap-x86.c     |   2 +
 drivers/iommu/intel/irq_remapping.c         |   2 +
 drivers/pci/controller/pci-hyperv.c         |   2 +
 include/linux/interrupt.h                   |   8 +
 include/linux/irq.h                         |  54 ++
 include/linux/sched/isolation.h             |  42 ++
 kernel/irq/Makefile                         |   1 +
 kernel/irq/cpuhotplug.c                     |  12 +-
 kernel/irq/debugfs.c                        |   4 +
 kernel/irq/internals.h                      |  62 ++
 kernel/irq/irqdesc.c                        |  10 +
 kernel/irq/isolation.c                      | 623 ++++++++++++++++++++
 kernel/irq/manage.c                         | 385 ++++++++++--
 kernel/irq/migration.c                      |  10 +
 kernel/irq/pm.c                             |  15 +
 kernel/sched/isolation.c                    |  33 ++
 lib/cpu_rmap.c                              |  32 +-
 net/core/dev.c                              |  24 +-
 26 files changed, 1481 insertions(+), 70 deletions(-)
 create mode 100644 kernel/irq/isolation.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 01/10] genirq: Add IRQCHIP_AFFINITY_FILTERABLE flag
  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 ` Kris Pan
  2026-11-30  3:37 ` [RFC PATCH 02/10] sched/isolation: Add runtime isolation helpers Kris Pan
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:36 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

Runtime IRQ isolation needs to program a mask which differs from
irq_common_data.affinity for unmanaged interrupts: it is either the
request with runtime-isolated CPUs subtracted, or a relocation to
housekeeping CPUs when an automatic placement loses every online
target.  Legacy irqchips which derive mask/unmask or routing state
directly from the requested affinity cannot tolerate that divergence.

Define IRQCHIP_AFFINITY_FILTERABLE for delivery irqchips which can
safely accept a filtered or relocated mask, and
IRQCHIP_AFFINITY_PASSTHROUGH for the forwarding levels above them
which only hand the mask to their parent.  The core requires every
level which implements .irq_set_affinity to be one of the two, so an
unmarked domain keeps filtering off instead of inheriting it from a
parent.  Expose both flags in debugfs.  No irqchip opts in yet.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 include/linux/irq.h  | 23 +++++++++++++++++++++++
 kernel/irq/debugfs.c |  2 ++
 2 files changed, 25 insertions(+)

diff --git a/include/linux/irq.h b/include/linux/irq.h
index f485369b1b4f7..d47ebe8ec3708 100644
--- a/include/linux/irq.h
+++ b/include/linux/irq.h
@@ -569,6 +569,27 @@ struct irq_chip {
  * IRQCHIP_AFFINITY_PRE_STARTUP:      Default affinity update before startup
  * IRQCHIP_IMMUTABLE:		      Don't ever change anything in this chip
  * IRQCHIP_MOVE_DEFERRED:	      Move the interrupt in actual interrupt context
+ * IRQCHIP_AFFINITY_FILTERABLE:       Delivery chip accepts a filtered or
+ *                                    relocated affinity mask instead of the
+ *                                    requested one.  Set it on the level
+ *                                    which programs the delivery target; the
+ *                                    core walks the parent chain and requires
+ *                                    every level which implements
+ *                                    .irq_set_affinity to be FILTERABLE or
+ *                                    PASSTHROUGH.  A chip which derives
+ *                                    routing from irq_common_data.affinity
+ *                                    must not opt in.  The opt-in also covers
+ *                                    the initial placement: the activation
+ *                                    path must respect the runtime isolation
+ *                                    filter before the first affinity request
+ *                                    runs.
+ * IRQCHIP_AFFINITY_PASSTHROUGH:      Forwarding level above a filterable
+ *                                    delivery chip.  The chip only hands the
+ *                                    mask to its parent and derives no state
+ *                                    from it, so it accepts a filtered mask.
+ *                                    An unmarked level which implements
+ *                                    .irq_set_affinity disables filtering for
+ *                                    the whole chain.
  */
 enum {
 	IRQCHIP_SET_TYPE_MASKED			= (1 <<  0),
@@ -584,6 +605,8 @@ enum {
 	IRQCHIP_AFFINITY_PRE_STARTUP		= (1 << 10),
 	IRQCHIP_IMMUTABLE			= (1 << 11),
 	IRQCHIP_MOVE_DEFERRED			= (1 << 12),
+	IRQCHIP_AFFINITY_FILTERABLE		= (1 << 13),
+	IRQCHIP_AFFINITY_PASSTHROUGH		= (1 << 14),
 };
 
 #include <linux/irqdesc.h>
diff --git a/kernel/irq/debugfs.c b/kernel/irq/debugfs.c
index 5c5ebaee35f2c..3bdc5a388ea5f 100644
--- a/kernel/irq/debugfs.c
+++ b/kernel/irq/debugfs.c
@@ -54,6 +54,8 @@ static const struct irq_bit_descr irqchip_flags[] = {
 	BIT_MASK_DESCR(IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND),
 	BIT_MASK_DESCR(IRQCHIP_IMMUTABLE),
 	BIT_MASK_DESCR(IRQCHIP_MOVE_DEFERRED),
+	BIT_MASK_DESCR(IRQCHIP_AFFINITY_FILTERABLE),
+	BIT_MASK_DESCR(IRQCHIP_AFFINITY_PASSTHROUGH),
 };
 
 static void
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 02/10] sched/isolation: Add runtime isolation helpers
  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 ` Kris Pan
  2026-11-30  3:37 ` [RFC PATCH 03/10] genirq: Track automatic vs. explicit affinity provenance Kris Pan
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

cpuset isolated partitions update HK_TYPE_DOMAIN at runtime.
HK_TYPE_DOMAIN_BOOT still reflects only boot-time isolcpus=domain
CPUs, so the difference is exactly the set isolated at runtime.

Expose housekeeping_runtime_isolated(), a scratch-free CPU test and a
runtime-only intersection helper in <linux/sched/isolation.h>.  The
helpers read the existing housekeeping masks under RCU and do not
change boot-time IRQ semantics.  RCU_LOCKDEP_WARN() catches a caller
which forgets the RCU read lock when CONFIG_PROVE_RCU is enabled.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 include/linux/sched/isolation.h | 32 ++++++++++++++++++++++++++++++++
 kernel/sched/isolation.c        | 31 +++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/include/linux/sched/isolation.h b/include/linux/sched/isolation.h
index ae5109450ac92..cec4e051dfab9 100644
--- a/include/linux/sched/isolation.h
+++ b/include/linux/sched/isolation.h
@@ -77,6 +77,38 @@ static inline void housekeeping_set_cpu(int cpu, enum hk_type type) { }
 static inline void housekeeping_init(void) { }
 #endif /* CONFIG_CPU_ISOLATION */
 
+#ifdef CONFIG_CPU_ISOLATION
+/*
+ * Runtime-only isolated CPUs.
+ *
+ * DOMAIN_BOOT excludes boot-time isolcpus=domain CPUs and DOMAIN further
+ * excludes runtime cpuset isolated CPUs.  The difference is therefore the
+ * runtime PRS_ISOLATED set, without changing boot IRQ semantics.
+ *
+ * All three helpers below must be called under RCU read lock.
+ */
+void housekeeping_runtime_isolated(struct cpumask *out);
+bool housekeeping_runtime_isolated_cpu(unsigned int cpu);
+bool housekeeping_runtime_isolated_intersects(const struct cpumask *mask);
+
+#else
+static inline void housekeeping_runtime_isolated(struct cpumask *out)
+{
+	cpumask_clear(out);
+}
+
+static inline bool housekeeping_runtime_isolated_cpu(unsigned int cpu)
+{
+	return false;
+}
+
+static inline bool
+housekeeping_runtime_isolated_intersects(const struct cpumask *mask)
+{
+	return false;
+}
+#endif /* CONFIG_CPU_ISOLATION */
+
 static inline bool housekeeping_cpu(int cpu, enum hk_type type)
 {
 #ifdef CONFIG_CPU_ISOLATION
diff --git a/kernel/sched/isolation.c b/kernel/sched/isolation.c
index c0c8b8862cbdc..418579766a364 100644
--- a/kernel/sched/isolation.c
+++ b/kernel/sched/isolation.c
@@ -131,6 +131,37 @@ void __init housekeeping_set_cpu(int cpu, enum hk_type type)
 		cpumask_set_cpu(cpu, housekeeping_cpumask_dereference(type));
 }
 
+void housekeeping_runtime_isolated(struct cpumask *out)
+{
+	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
+			 "runtime isolation mask needs RCU read lock");
+	cpumask_andnot(out, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT),
+		       housekeeping_cpumask(HK_TYPE_DOMAIN));
+}
+
+bool housekeeping_runtime_isolated_cpu(unsigned int cpu)
+{
+	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
+			 "runtime isolation CPU test needs RCU read lock");
+	return cpumask_test_cpu(cpu, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)) &&
+	       !cpumask_test_cpu(cpu, housekeeping_cpumask(HK_TYPE_DOMAIN));
+}
+
+bool housekeeping_runtime_isolated_intersects(const struct cpumask *mask)
+{
+	const struct cpumask *boot = housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT);
+	const struct cpumask *dom = housekeeping_cpumask(HK_TYPE_DOMAIN);
+	unsigned int cpu;
+
+	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
+			 "runtime isolation intersection needs RCU read lock");
+	for_each_cpu_and(cpu, mask, boot) {
+		if (!cpumask_test_cpu(cpu, dom))
+			return true;
+	}
+	return false;
+}
+
 int housekeeping_update(struct cpumask *isol_mask)
 {
 	struct cpumask *trial, *old = NULL;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 03/10] genirq: Track automatic vs. explicit affinity provenance
  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 ` Kris Pan
  2026-11-30  3:37 ` [RFC PATCH 04/10] genirq: Filter unmanaged IRQ affinity against runtime isolation Kris Pan
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

Runtime IRQ isolation has to distinguish an automatically placed
interrupt from one which was pinned explicitly: only the former may be
redirected to other housekeeping CPUs when its requested targets become
isolated.

Record the provenance in a private descriptor state bit,
IRQS_AFFINITY_AUTO, in desc->istate next to IRQS_SYSFS.  istate is
genirq-private, so the public IRQ_* space in include/linux/irq.h and
the status_use_accessors word stay untouched, irq_modify_status()
cannot reach the bit, and it survives free_irq()/request_irq().  The
bit is set or cleared by the descriptor setup, by the managed affinity
descriptor update and by irq_setup_affinity().

In-kernel irq_set_affinity_and_hint() describes automatic placement:
drivers use it to bind a queue, not to exclude other CPUs.  A bare
irq_set_affinity() is an explicit pin, the safe default for a per-CPU
PMU or an architecture timer which needs the interrupt on one CPU.
Core code which manages an affinity on a driver's behalf marks it
automatic with irq_set_affinity_auto(); the helper is not exported, so
a module cannot opt in by accident.  A /proc/irq/N/smp_affinity write,
irq_force_affinity() and a bare irq_set_affinity() clear the bit;
subsequent filtering does not redirect them to unrelated housekeeping
CPUs, so only their cross-mask fallback is lost.

No behaviour change: the bit is recorded, not read.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 include/linux/interrupt.h |  8 ++++
 kernel/irq/debugfs.c      |  1 +
 kernel/irq/internals.h    | 17 ++++++++
 kernel/irq/irqdesc.c      |  9 +++++
 kernel/irq/manage.c       | 85 +++++++++++++++++++++++++++++++++++----
 5 files changed, 112 insertions(+), 8 deletions(-)

diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
index ee2dd5349697d..58ec0acf82772 100644
--- a/include/linux/interrupt.h
+++ b/include/linux/interrupt.h
@@ -318,6 +318,8 @@ struct irq_affinity_desc {
 extern cpumask_var_t irq_default_affinity;
 
 extern int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask);
+/* Core-internal only; drivers must use irq_set_affinity_and_hint() */
+int irq_set_affinity_auto(unsigned int irq, const struct cpumask *cpumask);
 extern int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask);
 
 extern int irq_can_set_affinity(unsigned int irq);
@@ -382,6 +384,12 @@ static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
 	return -EINVAL;
 }
 
+static inline int irq_set_affinity_auto(unsigned int irq,
+					const struct cpumask *m)
+{
+	return -EINVAL;
+}
+
 static inline int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
 {
 	return 0;
diff --git a/kernel/irq/debugfs.c b/kernel/irq/debugfs.c
index 3bdc5a388ea5f..515c01754f1f0 100644
--- a/kernel/irq/debugfs.c
+++ b/kernel/irq/debugfs.c
@@ -154,6 +154,7 @@ static const struct irq_bit_descr irqdesc_istates[] = {
 	BIT_MASK_DESCR(IRQS_PENDING),
 	BIT_MASK_DESCR(IRQS_SUSPENDED),
 	BIT_MASK_DESCR(IRQS_NMI),
+	BIT_MASK_DESCR(IRQS_AFFINITY_AUTO),
 };
 
 
diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h
index 703a062be8783..06cfa77409022 100644
--- a/kernel/irq/internals.h
+++ b/kernel/irq/internals.h
@@ -64,6 +64,7 @@ enum {
  * IRQS_SUSPENDED		- irq is suspended
  * 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
  */
 enum {
 	IRQS_AUTODETECT		= 0x00000001,
@@ -77,8 +78,24 @@ enum {
 	IRQS_TIMINGS		= 0x00001000,
 	IRQS_NMI		= 0x00002000,
 	IRQS_SYSFS		= 0x00004000,
+	IRQS_AFFINITY_AUTO	= 0x00008000,
 };
 
+static inline bool irq_affinity_is_auto(struct irq_desc *desc)
+{
+	return desc->istate & IRQS_AFFINITY_AUTO;
+}
+
+static inline void irq_affinity_set_auto(struct irq_desc *desc)
+{
+	desc->istate |= IRQS_AFFINITY_AUTO;
+}
+
+static inline void irq_affinity_clr_auto(struct irq_desc *desc)
+{
+	desc->istate &= ~IRQS_AFFINITY_AUTO;
+}
+
 #include "debug.h"
 #include "settings.h"
 
diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index 7cbcc3fb9e6d4..4f578f8d497fd 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);
+	irq_affinity_set_auto(desc);
 	desc->handle_irq = handle_bad_irq;
 	desc->depth = 1;
 	desc->irq_count = 0;
@@ -519,6 +520,7 @@ static int alloc_descs(unsigned int start, unsigned int cnt, int node,
 	}
 
 	for (i = 0; i < cnt; i++) {
+		bool managed = affinity && affinity->is_managed;
 		const struct cpumask *mask = NULL;
 		unsigned int flags = 0;
 
@@ -536,6 +538,13 @@ static int alloc_descs(unsigned int start, unsigned int cnt, int node,
 		desc = alloc_desc(start + i, node, flags, mask, owner);
 		if (!desc)
 			goto err;
+		/*
+		 * A managed interrupt is placed by the block layer and does
+		 * not take part in the runtime isolation filter.  It must
+		 * not be reported as automatically placed.
+		 */
+		if (managed)
+			irq_affinity_clr_auto(desc);
 		irq_insert_desc(start + i, desc);
 		irq_sysfs_add(start + i, desc);
 		irq_add_debugfs_entry(start + i, desc);
diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
index ec3ecb0e48cec..4f5c776e8c5de 100644
--- a/kernel/irq/manage.c
+++ b/kernel/irq/manage.c
@@ -452,6 +452,10 @@ int irq_update_affinity_desc(unsigned int irq, struct irq_affinity_desc *affinit
 		}
 
 		cpumask_copy(desc->irq_common_data.affinity, &affinity->mask);
+		if (!affinity->is_managed)
+			irq_affinity_set_auto(desc);
+		else
+			irq_affinity_clr_auto(desc);
 
 		/* Restore the activation state */
 		if (activated)
@@ -461,16 +465,40 @@ int irq_update_affinity_desc(unsigned int irq, struct irq_affinity_desc *affinit
 	return -EINVAL;
 }
 
+enum irq_affinity_origin {
+	IRQ_AFFINITY_SET_AUTO,
+	IRQ_AFFINITY_CLEAR_AUTO,
+};
+
 static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask,
-			      bool force)
+			      bool force, enum irq_affinity_origin origin)
 {
 	struct irq_desc *desc = irq_to_desc(irq);
+	bool old_auto;
+	int ret;
 
 	if (!desc)
 		return -EINVAL;
 
 	guard(raw_spinlock_irqsave)(&desc->lock);
-	return irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
+	old_auto = irq_affinity_is_auto(desc);
+	switch (origin) {
+	case IRQ_AFFINITY_SET_AUTO:
+		irq_affinity_set_auto(desc);
+		break;
+	case IRQ_AFFINITY_CLEAR_AUTO:
+		irq_affinity_clr_auto(desc);
+		break;
+	}
+
+	ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
+	if (ret) {
+		if (old_auto)
+			irq_affinity_set_auto(desc);
+		else
+			irq_affinity_clr_auto(desc);
+	}
+	return ret;
 }
 
 /**
@@ -479,13 +507,38 @@ static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask,
  * @cpumask:	cpumask
  *
  * Fails if cpumask does not contain an online CPU
+ *
+ * The affinity is recorded as an explicit request and is not redirected
+ * by runtime IRQ isolation.  This is the safe default for a driver which
+ * needs the interrupt on a specific CPU, for example a per-CPU PMU or an
+ * architecture timer.  A driver which binds a queue for load spreading
+ * uses irq_set_affinity_and_hint() instead, which keeps the placement
+ * automatic.
  */
 int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
 {
-	return __irq_set_affinity(irq, cpumask, false);
+	return __irq_set_affinity(irq, cpumask, false, IRQ_AFFINITY_CLEAR_AUTO);
 }
 EXPORT_SYMBOL_GPL(irq_set_affinity);
 
+/**
+ * irq_set_affinity_auto - Program an automatically placed irq affinity
+ * @irq:	Interrupt to set affinity
+ * @cpumask:	cpumask
+ *
+ * Same as irq_set_affinity(), but records the request as automatic
+ * placement, so runtime IRQ isolation may move the interrupt off a CPU
+ * which later becomes isolated.
+ *
+ * Not exported: it is for core code which manages a device's affinity on
+ * the driver's behalf, not for drivers themselves.  A driver which wants
+ * automatic placement uses irq_set_affinity_and_hint().
+ */
+int irq_set_affinity_auto(unsigned int irq, const struct cpumask *cpumask)
+{
+	return __irq_set_affinity(irq, cpumask, false, IRQ_AFFINITY_SET_AUTO);
+}
+
 /**
  * irq_force_affinity - Force the irq affinity of a given irq
  * @irq:	Interrupt to set affinity
@@ -499,11 +552,12 @@ EXPORT_SYMBOL_GPL(irq_set_affinity);
  */
 int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
 {
-	return __irq_set_affinity(irq, cpumask, true);
+	return __irq_set_affinity(irq, cpumask, true, IRQ_AFFINITY_CLEAR_AUTO);
 }
 EXPORT_SYMBOL_GPL(irq_force_affinity);
 
-int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool setaffinity)
+int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m,
+			      bool setaffinity)
 {
 	int ret = -EINVAL;
 
@@ -512,8 +566,8 @@ int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool se
 		ret = 0;
 	}
 
-	if (!ret && m && setaffinity)
-		__irq_set_affinity(irq, m, false);
+	if (!ret && setaffinity && m)
+		__irq_set_affinity(irq, m, false, IRQ_AFFINITY_SET_AUTO);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(__irq_apply_affinity_hint);
@@ -595,6 +649,8 @@ int irq_setup_affinity(struct irq_desc *desc)
 {
 	struct cpumask *set = irq_default_affinity;
 	int node = irq_desc_get_node(desc);
+	bool old_auto, set_auto;
+	int ret;
 
 	static DEFINE_RAW_SPINLOCK(mask_lock);
 	static struct cpumask mask;
@@ -628,7 +684,20 @@ int irq_setup_affinity(struct irq_desc *desc)
 		if (cpumask_intersects(&mask, nodemask))
 			cpumask_and(&mask, &mask, nodemask);
 	}
-	return irq_do_set_affinity(&desc->irq_data, &mask, false);
+	/*
+	 * Read IRQD_AFFINITY_SET after the block above: it may have been
+	 * cleared because the requested targets are offline, in which case
+	 * this placement is automatic again.
+	 */
+	old_auto = irq_affinity_is_auto(desc);
+	set_auto = !irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET) &&
+		   !irqd_affinity_is_managed(&desc->irq_data);
+	if (set_auto)
+		irq_affinity_set_auto(desc);
+	ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
+	if (ret && set_auto && !old_auto)
+		irq_affinity_clr_auto(desc);
+	return ret;
 }
 #else
 /* Wrapper for ALPHA specific affinity selector magic */
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 04/10] genirq: Filter unmanaged IRQ affinity against runtime isolation
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (2 preceding siblings ...)
  2026-11-30  3:37 ` [RFC PATCH 03/10] genirq: Track automatic vs. explicit affinity provenance Kris Pan
@ 2026-11-30  3:37 ` Kris Pan
  2026-11-30  3:37 ` [RFC PATCH 05/10] genirq: Reapply unmanaged IRQ affinity on isolation changes Kris Pan
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

An interrupt whose affinity includes a CPU handed to a cpuset isolated
partition still receives work from that CPU.  Runtime isolation has to
steer the programmed affinity away from it without changing the request
itself.

Add the mask policy.  irq_isolation_select_mask() subtracts the runtime
isolated set from the requested mask; a mask without an isolated CPU is
left alone, so boot-time isolcpus=domain CPUs are never stripped from an
unaffected request.  When an automatic placement loses every online
target it falls back to the online HK_TYPE_DOMAIN CPUs, preferring a
node the request covers over the device node, while an explicit request
keeps its mask.

Chips opt in with IRQCHIP_AFFINITY_FILTERABLE.  The walk requires every
level which implements .irq_set_affinity to be either FILTERABLE (the
delivery level) or PASSTHROUGH (a forwarder which derives no state from
the mask), so an unmarked domain keeps filtering off instead of
inheriting it from a parent.

A filtered mask can exhaust the vector space of its targets.  Isolation
is a soft-avoid policy, not a reason to break a working configuration,
so retry before failing.  An automatic placement may widen the search
to the online HK_TYPE_DOMAIN housekeeping CPUs, honouring
irq_default_affinity first and only then the full housekeeping set; it
prefers a housekeeping CPU in another NUMA node over an isolated CPU in
the local one and never spills onto a boot-time isolcpus=domain CPU.
An explicit request is never widened and only loses the filter, as
before.

A driver which applies IRQF_NOBALANCING in request_irq() after an early
irq_set_affinity() on an already activated MSI may have had that request
narrowed before the flag became visible.  Replay it once through
irq_set_affinity_locked(): the filter is inert for a no-balancing
interrupt, and a chip which cannot move in process context defers the
replay to interrupt context instead of programming it from the wrong
one.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 include/linux/irq.h    |  24 +++++
 kernel/irq/Makefile    |   1 +
 kernel/irq/internals.h |  22 ++++
 kernel/irq/isolation.c | 223 +++++++++++++++++++++++++++++++++++++++++
 kernel/irq/manage.c    | 135 ++++++++++++++++---------
 5 files changed, 360 insertions(+), 45 deletions(-)
 create mode 100644 kernel/irq/isolation.c

diff --git a/include/linux/irq.h b/include/linux/irq.h
index d47ebe8ec3708..382591da2038d 100644
--- a/include/linux/irq.h
+++ b/include/linux/irq.h
@@ -634,6 +634,30 @@ extern int irq_set_affinity_locked(struct irq_data *data,
 				   const struct cpumask *cpumask, bool force);
 extern int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info);
 
+/* Runtime IRQ isolation for cpuset isolated partitions. */
+#ifdef CONFIG_CPU_ISOLATION
+bool irq_isolation_active(void);
+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);
+
+#else
+static inline bool irq_isolation_active(void) { return false; }
+static inline bool irq_isolation_can_filter(struct irq_data *data)
+{
+	return false;
+}
+
+static inline bool irq_isolation_select_mask(struct irq_data *data,
+					     const struct cpumask *src,
+					     struct cpumask *out)
+{
+	return false;
+}
+
+#endif
+
 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_IRQ_MIGRATION)
 extern void irq_migrate_all_off_this_cpu(void);
 extern int irq_affinity_online_cpu(unsigned int cpu);
diff --git a/kernel/irq/Makefile b/kernel/irq/Makefile
index 0e5df962a1497..e56b62d954d99 100644
--- a/kernel/irq/Makefile
+++ b/kernel/irq/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_GENERIC_MSI_IRQ) += msi.o
 obj-$(CONFIG_GENERIC_IRQ_IPI) += ipi.o
 obj-$(CONFIG_GENERIC_IRQ_IPI_MUX) += ipi-mux.o
 obj-$(CONFIG_SMP) += affinity.o
+obj-$(CONFIG_CPU_ISOLATION) += isolation.o
 obj-$(CONFIG_GENERIC_IRQ_DEBUGFS) += debugfs.o
 obj-$(CONFIG_GENERIC_IRQ_MATRIX_ALLOCATOR) += matrix.o
 obj-$(CONFIG_IRQ_KUNIT_TEST) += irq_test.o
diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h
index 06cfa77409022..abadf24b3d0eb 100644
--- a/kernel/irq/internals.h
+++ b/kernel/irq/internals.h
@@ -186,6 +186,28 @@ extern int irq_setup_affinity(struct irq_desc *desc);
 static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; }
 #endif
 
+#ifdef CONFIG_CPU_ISOLATION
+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 bool irq_isolation_widen_mask(struct irq_desc *desc,
+					    const struct cpumask *mask,
+					    struct cpumask *out)
+{
+	return false;
+}
+
+static inline bool irq_isolation_widen_mask_full(struct irq_desc *desc,
+						 const struct cpumask *mask,
+						 struct cpumask *out)
+{
+	return false;
+}
+#endif
+
 #define for_each_action_of_desc(desc, act)			\
 	for (act = desc->action; act; act = act->next)
 
diff --git a/kernel/irq/isolation.c b/kernel/irq/isolation.c
new file mode 100644
index 0000000000000..af7244ce181a4
--- /dev/null
+++ b/kernel/irq/isolation.c
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Runtime IRQ isolation for cpuset isolated partitions.
+ *
+ * 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.
+ */
+
+#include <linux/cpumask.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqdesc.h>
+#include <linux/rcupdate.h>
+#include <linux/sched.h>
+#include <linux/sched/isolation.h>
+#include <linux/static_key.h>
+#include <linux/topology.h>
+
+#include "internals.h"
+
+static DEFINE_STATIC_KEY_FALSE(irq_isolation_key);
+
+bool irq_isolation_active(void)
+{
+	return static_branch_unlikely(&irq_isolation_key);
+}
+
+/*
+ * Compute the fallback mask for an auto-placed IRQ.  @src is the requested
+ * mask.  Must be called under RCU read lock and must never return a
+ * boot-time isolcpus=domain CPU.
+ */
+static bool irq_isolation_fallback_mask(struct irq_data *data,
+					const struct cpumask *src,
+					struct cpumask *mask)
+{
+	const struct cpumask *hk = housekeeping_cpumask(HK_TYPE_DOMAIN);
+	unsigned int cpu;
+	int node;
+
+	cpumask_and(mask, irq_default_affinity, hk);
+	cpumask_and(mask, mask, cpu_online_mask);
+	if (cpumask_empty(mask))
+		cpumask_and(mask, hk, cpu_online_mask);
+	if (cpumask_empty(mask))
+		return false;
+
+	/*
+	 * Prefer a node which the request covers over the node the device
+	 * sits on: a queue which a driver hinted onto another socket must
+	 * not be pulled back to the device node when its CPUs become
+	 * isolated.
+	 */
+	for_each_cpu(cpu, src) {
+		node = cpu_to_node(cpu);
+		if (node != NUMA_NO_NODE &&
+		    cpumask_intersects(mask, cpumask_of_node(node))) {
+			cpumask_and(mask, mask, cpumask_of_node(node));
+			return true;
+		}
+	}
+
+	node = irq_data_get_node(data);
+	if (node != NUMA_NO_NODE) {
+		const struct cpumask *node_mask = cpumask_of_node(node);
+
+		if (cpumask_intersects(mask, node_mask))
+			cpumask_and(mask, mask, node_mask);
+	}
+	return true;
+}
+
+bool irq_isolation_can_filter(struct irq_data *data)
+{
+	bool filterable = false;
+
+	/*
+	 * The flag is owned by the chip which actually routes the
+	 * interrupt.  Every level which implements .irq_set_affinity has
+	 * to declare itself either as the delivery level (FILTERABLE) or
+	 * as a pure forwarder (PASSTHROUGH); an unmarked level disables
+	 * filtering for the whole chain.  A level which does not implement
+	 * .irq_set_affinity is not part of the affinity path and is
+	 * skipped, matching irq_chip_set_affinity_parent().
+	 */
+	for (; data; data = irqd_get_parent_data(data)) {
+		if (!data->chip || !data->chip->irq_set_affinity)
+			continue;
+		if (data->chip->flags & IRQCHIP_AFFINITY_FILTERABLE) {
+			filterable = true;
+			continue;
+		}
+		if (data->chip->flags & IRQCHIP_AFFINITY_PASSTHROUGH)
+			continue;
+		return false;
+	}
+
+	return filterable;
+}
+
+/*
+ * Apply the runtime-isolation policy to @data's requested affinity and
+ * store the mask to program in @out.  Returns false if the chip is not
+ * filterable, isolation is inactive, @src is unaffected, or no online
+ * target remains in @src.
+ *
+ * @out must not alias @src.
+ */
+bool irq_isolation_select_mask(struct irq_data *data,
+			       const struct cpumask *src, struct cpumask *out)
+{
+	bool auto_placed;
+
+	if (!irq_isolation_active() || !irq_isolation_can_filter(data) ||
+	    !irqd_can_balance(data) || irqd_affinity_is_managed(data))
+		return false;
+
+	rcu_read_lock();
+	housekeeping_runtime_isolated(out);
+	cpumask_andnot(out, src, out);
+	/*
+	 * Do not touch masks which contain no runtime-isolated CPU.  Doing
+	 * so would also strip boot-time isolcpus=domain CPUs from an
+	 * otherwise unaffected request.
+	 */
+	if (cpumask_equal(out, src)) {
+		rcu_read_unlock();
+		return false;
+	}
+
+	/*
+	 * A remaining online target keeps the boot isolcpus=domain CPUs in
+	 * the mask, for explicit and automatic placements alike.  Only when
+	 * runtime isolation removed every online target may an automatic
+	 * placement be redirected to online HK_TYPE_DOMAIN CPUs; an
+	 * explicit request which lost all targets is preserved.
+	 */
+	auto_placed = irq_affinity_is_auto(irq_data_to_desc(data));
+	if (!cpumask_intersects(out, cpu_online_mask) && auto_placed)
+		irq_isolation_fallback_mask(data, src, out);
+
+	/*
+	 * An explicit request which lost every online target, or an
+	 * automatic placement without a housekeeping target, is kept as
+	 * requested.  Report the mask as unchanged so that the caller does
+	 * not retry it after -ENOSPC and does not widen a single-target
+	 * IRQ thread beyond its effective CPU.
+	 */
+	if (!cpumask_intersects(out, cpu_online_mask)) {
+		rcu_read_unlock();
+		return false;
+	}
+	rcu_read_unlock();
+	return true;
+}
+
+/**
+ * irq_isolation_widen_mask - Preferred emergency mask after the filtered
+ *			      placement failed
+ * @desc:	Interrupt descriptor
+ * @mask:	Requested mask which just failed
+ * @out:	Storage for the widened mask
+ *
+ * An automatically placed interrupt may widen the search to the online
+ * housekeeping CPUs when the requested mask has no vector left.  The
+ * administrator's irq_default_affinity is honoured first, so an
+ * emergency placement does not spill onto a CPU excluded with
+ * irqaffinity= while another default CPU has a vector; only when it is
+ * empty is the full housekeeping set used.
+ *
+ * An explicit request is never widened here.
+ *
+ * Returns: true when @out contains a CPU outside @mask.
+ */
+bool irq_isolation_widen_mask(struct irq_desc *desc,
+			      const struct cpumask *mask, struct cpumask *out)
+{
+	const struct cpumask *hk;
+
+	if (!irq_affinity_is_auto(desc))
+		return false;
+
+	rcu_read_lock();
+	hk = housekeeping_cpumask(HK_TYPE_DOMAIN);
+	cpumask_and(out, irq_default_affinity, hk);
+	cpumask_and(out, out, cpu_online_mask);
+	if (cpumask_empty(out))
+		cpumask_and(out, hk, cpu_online_mask);
+	rcu_read_unlock();
+
+	return !cpumask_empty(out) && !cpumask_subset(out, mask);
+}
+
+/**
+ * irq_isolation_widen_mask_full - Full housekeeping emergency mask
+ * @desc:	Interrupt descriptor
+ * @mask:	Requested mask which just failed
+ * @out:	Storage for the widened mask
+ *
+ * The second -ENOSPC retry: the preferred irq_default_affinity subset
+ * was already tried and is exhausted, so use every online housekeeping
+ * CPU.  The requested mask stays the last resort in the caller.
+ *
+ * Returns: true when @out contains a CPU outside @mask.
+ */
+bool irq_isolation_widen_mask_full(struct irq_desc *desc,
+				   const struct cpumask *mask,
+				   struct cpumask *out)
+{
+	const struct cpumask *hk;
+
+	if (!irq_affinity_is_auto(desc))
+		return false;
+
+	rcu_read_lock();
+	hk = housekeeping_cpumask(HK_TYPE_DOMAIN);
+	cpumask_and(out, hk, cpu_online_mask);
+	rcu_read_unlock();
+
+	return !cpumask_empty(out) && !cpumask_subset(out, mask);
+}
+
diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
index 4f5c776e8c5de..25f42256aeabf 100644
--- a/kernel/irq/manage.c
+++ b/kernel/irq/manage.c
@@ -236,63 +236,81 @@ int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask, bool
 	struct cpumask *tmp_mask = this_cpu_ptr(&__tmp_mask);
 	struct irq_desc *desc = irq_data_to_desc(data);
 	struct irq_chip *chip = irq_data_get_irq_chip(data);
-	const struct cpumask  *prog_mask;
+	const struct cpumask *prog_mask = mask;
 	int ret;
 
 	if (!chip || !chip->irq_set_affinity)
 		return -EINVAL;
 
-	/*
-	 * If this is a managed interrupt and housekeeping is enabled on
-	 * it check whether the requested affinity mask intersects with
-	 * a housekeeping CPU. If so, then remove the isolated CPUs from
-	 * the mask and just keep the housekeeping CPU(s). This prevents
-	 * the affinity setter from routing the interrupt to an isolated
-	 * CPU to avoid that I/O submitted from a housekeeping CPU causes
-	 * interrupts on an isolated one.
-	 *
-	 * If the masks do not intersect or include online CPU(s) then
-	 * keep the requested mask. The isolated target CPUs are only
-	 * receiving interrupts when the I/O operation was submitted
-	 * directly from them.
-	 *
-	 * If all housekeeping CPUs in the affinity mask are offline, the
-	 * interrupt will be migrated by the CPU hotplug code once a
-	 * housekeeping CPU which belongs to the affinity mask comes
-	 * online.
-	 */
-	if (irqd_affinity_is_managed(data) &&
-	    housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) {
-		const struct cpumask *hk_mask;
-
-		hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
+	if (force) {
+		/*
+		 * Preserve the original force-affinity contract: the
+		 * caller-supplied mask is passed through without online or
+		 * policy filtering.  force is not the same as the hotplug
+		 * emergency fallback.
+		 */
+		ret = chip->irq_set_affinity(data, mask, true);
+	} else {
+		if (irqd_affinity_is_managed(data) &&
+		    housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) {
+			const struct cpumask *hk_mask;
+
+			hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
+			cpumask_and(tmp_mask, mask, hk_mask);
+			if (!cpumask_intersects(tmp_mask, cpu_online_mask))
+				prog_mask = mask;
+			else
+				prog_mask = tmp_mask;
+		} else if (!irqd_affinity_is_managed(data) &&
+			   irq_isolation_active() &&
+			   irq_isolation_select_mask(data, mask, tmp_mask)) {
+			prog_mask = tmp_mask;
+		}
 
-		cpumask_and(tmp_mask, mask, hk_mask);
-		if (!cpumask_intersects(tmp_mask, cpu_online_mask))
-			prog_mask = mask;
+		if (cpumask_and(tmp_mask, prog_mask, cpu_online_mask))
+			ret = chip->irq_set_affinity(data, tmp_mask, false);
 		else
-			prog_mask = tmp_mask;
-	} else {
-		prog_mask = mask;
-	}
+			ret = -EINVAL;
 
-	/*
-	 * Make sure we only provide online CPUs to the irqchip,
-	 * unless we are being asked to force the affinity (in which
-	 * case we do as we are told).
-	 */
-	cpumask_and(tmp_mask, prog_mask, cpu_online_mask);
-	if (!force && !cpumask_empty(tmp_mask))
-		ret = chip->irq_set_affinity(data, tmp_mask, force);
-	else if (force)
-		ret = chip->irq_set_affinity(data, mask, force);
-	else
-		ret = -EINVAL;
+		/*
+		 * The filtered target may have exhausted its vectors.  This
+		 * is a soft-avoid policy: an automatic placement may widen
+		 * the search to the online housekeeping CPUs, which prefers
+		 * a CPU outside the (possibly NUMA-node narrowed) request
+		 * over an isolated CPU in it.  Boot-time isolcpus=domain
+		 * CPUs are not housekeeping, so they are never used as an
+		 * emergency target; the request itself is the last resort
+		 * below.  An explicit request is never widened; it only
+		 * loses the filter, as before.
+		 */
+		if (ret == -ENOSPC && !irqd_affinity_is_managed(data) &&
+		    prog_mask != mask) {
+			/*
+			 * @prog_mask aliases @tmp_mask at this point; the
+			 * widening helpers compare their result with the
+			 * request, so only a CPU outside the request is
+			 * worth a retry.
+			 */
+			if (irq_isolation_widen_mask(desc, mask, tmp_mask))
+				ret = chip->irq_set_affinity(data, tmp_mask, false);
+			if (ret == -ENOSPC &&
+			    irq_isolation_widen_mask_full(desc, mask, tmp_mask))
+				ret = chip->irq_set_affinity(data, tmp_mask, false);
+			if (ret == -ENOSPC &&
+			    cpumask_and(tmp_mask, mask, cpu_online_mask))
+				ret = chip->irq_set_affinity(data, tmp_mask, false);
+		}
+	}
 
 	switch (ret) {
 	case IRQ_SET_MASK_OK:
 	case IRQ_SET_MASK_OK_DONE:
-		cpumask_copy(desc->irq_common_data.affinity, mask);
+		/*
+		 * A replay passes the requested mask itself; copying it
+		 * onto itself is undefined.
+		 */
+		if (desc->irq_common_data.affinity != mask)
+			cpumask_copy(desc->irq_common_data.affinity, mask);
 		fallthrough;
 	case IRQ_SET_MASK_OK_NOCOPY:
 		irq_validate_effective_affinity(data);
@@ -1835,6 +1853,33 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
 		if (new->flags & IRQF_NOBALANCING) {
 			irq_settings_set_no_balancing(desc);
 			irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
+#ifdef CONFIG_CPU_ISOLATION
+			/*
+			 * The flag is applied after irq_activate(), so a
+			 * default placement still avoided isolated CPUs
+			 * when the vector was assigned.  An explicit
+			 * request which was set before request_irq() was
+			 * filtered at that point; now the filter is inert
+			 * for this interrupt and the request can be
+			 * programmed again.
+			 *
+			 * Only touch a filterable chip whose request was
+			 * really set, with an online target.  A deferred
+			 * move is left to the regular migration path, which
+			 * programs it unfiltered when it completes.
+			 */
+			if (irq_isolation_active() &&
+			    irq_isolation_can_filter(&desc->irq_data) &&
+			    !irqd_is_per_cpu(&desc->irq_data) &&
+			    irqd_affinity_was_set(&desc->irq_data) &&
+			    irqd_is_activated(&desc->irq_data) &&
+			    !irqd_is_setaffinity_pending(&desc->irq_data) &&
+			    cpumask_intersects(desc->irq_common_data.affinity,
+					       cpu_online_mask))
+				irq_set_affinity_locked(&desc->irq_data,
+							desc->irq_common_data.affinity,
+							false);
+#endif
 		}
 
 		if (!(new->flags & IRQF_NO_AUTOEN) &&
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 05/10] genirq: Reapply unmanaged IRQ affinity on isolation changes
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (3 preceding siblings ...)
  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
  2026-11-30  3:37 ` [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation Kris Pan
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (4 preceding siblings ...)
  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
  2026-11-30  3:37 ` [RFC PATCH 07/10] genirq: Handle runtime isolation across hotplug and suspend Kris Pan
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 07/10] genirq: Handle runtime isolation across hotplug and suspend
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (5 preceding siblings ...)
  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
  2026-11-30  3:37 ` [RFC PATCH 08/10] x86/apic: Support runtime IRQ isolation Kris Pan
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 08/10] x86/apic: Support runtime IRQ isolation
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (6 preceding siblings ...)
  2026-11-30  3:37 ` [RFC PATCH 07/10] genirq: Handle runtime isolation across hotplug and suspend Kris Pan
@ 2026-11-30  3:37 ` 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
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

x86_vector_activate() allocates an initial APIC vector before
irq_setup_affinity() runs, so the initial target can be a runtime
isolated CPU even though later affinity updates are filtered.

The policy must be evaluated from the descriptor's leaf irq_data:
x86_vector_activate() runs with the vector level's irq_data, whose
parent_data is NULL, so a filterability walk from there would only see
lapic_controller and bypass the FILTERABLE/PASSTHROUGH contract of the
intermediate domains.  Resolve the leaf first and gate the filtered
fallbacks on the same check.
Use the shared isolation selection helper for the initial vector.
Then try the node-local and global online fallbacks with only the
runtime-isolated CPUs removed, before the upstream unfiltered ones.
Boot-time isolcpus=domain CPUs stay valid fallback targets exactly as
they were before any runtime partition existed, and an isolated CPU in
the local node is not picked while a housekeeping CPU exists elsewhere.
Read the runtime isolated set once per vector assignment and share it
between the two filtered fallbacks, so they cannot observe different
isolation states.  Guard these paths with RCU and the managed/balance
checks, and opt lapic_controller into IRQCHIP_AFFINITY_FILTERABLE.

The forwarding levels above the vector chip are not delivery chips:
ioapic_chip, ioapic_ir_chip, the MSI chips installed by
x86_init_dev_msi_info() (including the DMAR and AMDVI parents),
dmar_msi_controller, hpet_msi_controller, intel_ir_chip,
intel_ir_chip_post_msi, amd_ir_chip, hyperv_ir_chip,
hyperv_root_ir_chip, the Hyper-V PCI MSI parents
(arch/x86/hyperv/irqdomain.c and
drivers/pci/controller/pci-hyperv.c) and the UV core domain only pass
the mask to their parent and derive no state from it, so mark them
IRQCHIP_AFFINITY_PASSTHROUGH.  Without the interrupt-remapping levels
the walk would disable filtering on every machine which boots with
VT-d or AMD-Vi interrupt remapping.

An unmarked MSI parent domain keeps filtering off for its interrupts.
The Hyper-V parents and the UV core domain were missed by the first
version, which left filtering off on every Hyper-V guest and
root-partition pass-through device.

Allocate the isolation mask only when CONFIG_CPU_ISOLATION is
enabled, so it stays out of the image otherwise.
arch_early_irq_init() has no error path, so a failed allocation
disables the x86 fallback filtering with a warning instead of returning
-ENOMEM and continuing with an unusable mask, or adding a new BUG_ON().
The cpumask_available() check makes the separate ready flag redundant.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 arch/x86/hyperv/irqdomain.c             |   4 +-
 arch/x86/kernel/apic/io_apic.c          |   6 +-
 arch/x86/kernel/apic/msi.c              |   8 +-
 arch/x86/kernel/apic/vector.c           | 105 +++++++++++++++++++++++-
 arch/x86/kernel/hpet.c                  |   3 +-
 arch/x86/platform/uv/uv_irq.c           |   1 +
 drivers/iommu/amd/iommu.c               |   1 +
 drivers/iommu/hyperv/hv-irq-remap-x86.c |   2 +
 drivers/iommu/intel/irq_remapping.c     |   2 +
 drivers/pci/controller/pci-hyperv.c     |   2 +
 10 files changed, 124 insertions(+), 10 deletions(-)

diff --git a/arch/x86/hyperv/irqdomain.c b/arch/x86/hyperv/irqdomain.c
index b3ad50a874dca..65d067df0b4eb 100644
--- a/arch/x86/hyperv/irqdomain.c
+++ b/arch/x86/hyperv/irqdomain.c
@@ -304,6 +304,7 @@ static struct irq_chip hv_pci_msi_controller = {
 	.irq_ack		= irq_chip_ack_parent,
 	.irq_compose_msi_msg	= hv_irq_compose_msi_msg,
 	.irq_set_affinity	= irq_chip_set_affinity_parent,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static bool hv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
@@ -315,7 +316,8 @@ static bool hv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 	if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
 		return false;
 
-	chip->flags |= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MOVE_DEFERRED;
+	chip->flags |= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MOVE_DEFERRED |
+		       IRQCHIP_AFFINITY_PASSTHROUGH;
 
 	info->ops->msi_prepare = pci_msi_prepare;
 
diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c
index 7d7175d012288..d5f0c78bcb6f8 100644
--- a/arch/x86/kernel/apic/io_apic.c
+++ b/arch/x86/kernel/apic/io_apic.c
@@ -1860,7 +1860,8 @@ static struct irq_chip ioapic_chip __read_mostly = {
 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
 	.irq_get_irqchip_state	= ioapic_irq_get_chip_state,
 	.flags			= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MOVE_DEFERRED |
-				  IRQCHIP_AFFINITY_PRE_STARTUP,
+				  IRQCHIP_AFFINITY_PRE_STARTUP |
+				  IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static struct irq_chip ioapic_ir_chip __read_mostly = {
@@ -1874,7 +1875,8 @@ static struct irq_chip ioapic_ir_chip __read_mostly = {
 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
 	.irq_get_irqchip_state	= ioapic_irq_get_chip_state,
 	.flags			= IRQCHIP_SKIP_SET_WAKE |
-				  IRQCHIP_AFFINITY_PRE_STARTUP,
+				  IRQCHIP_AFFINITY_PRE_STARTUP |
+				  IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static inline void init_IO_APIC_traps(void)
diff --git a/arch/x86/kernel/apic/msi.c b/arch/x86/kernel/apic/msi.c
index da5f44a80313f..c1fc271720a46 100644
--- a/arch/x86/kernel/apic/msi.c
+++ b/arch/x86/kernel/apic/msi.c
@@ -214,10 +214,13 @@ static bool x86_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 		if (WARN_ON_ONCE(domain != real_parent))
 			return false;
 		info->chip->irq_set_affinity = msi_set_affinity;
-		info->chip->flags |= IRQCHIP_MOVE_DEFERRED;
+		info->chip->flags |= IRQCHIP_MOVE_DEFERRED |
+				     IRQCHIP_AFFINITY_PASSTHROUGH;
 		break;
 	case DOMAIN_BUS_DMAR:
 	case DOMAIN_BUS_AMDVI:
+		/* The IR domain in between only forwards the mask. */
+		info->chip->flags |= IRQCHIP_AFFINITY_PASSTHROUGH;
 		break;
 	default:
 		WARN_ON_ONCE(1);
@@ -317,7 +320,8 @@ static struct irq_chip dmar_msi_controller = {
 	.irq_compose_msi_msg	= dmar_msi_compose_msg,
 	.irq_write_msi_msg	= dmar_msi_write_msg,
 	.flags			= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MOVE_DEFERRED |
-				  IRQCHIP_AFFINITY_PRE_STARTUP,
+				  IRQCHIP_AFFINITY_PRE_STARTUP |
+				  IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static int dmar_msi_init(struct irq_domain *domain,
diff --git a/arch/x86/kernel/apic/vector.c b/arch/x86/kernel/apic/vector.c
index bddc544653999..1848ef920caec 100644
--- a/arch/x86/kernel/apic/vector.c
+++ b/arch/x86/kernel/apic/vector.c
@@ -9,6 +9,8 @@
  */
 #include <linux/interrupt.h>
 #include <linux/irq.h>
+#include <linux/irqdesc.h>
+#include <linux/sched/isolation.h>
 #include <linux/seq_file.h>
 #include <linux/init.h>
 #include <linux/compiler.h>
@@ -41,6 +43,24 @@ struct irq_domain *x86_vector_domain;
 EXPORT_SYMBOL_GPL(x86_vector_domain);
 static DEFINE_RAW_SPINLOCK(vector_lock);
 static cpumask_var_t vector_searchmask;
+#ifdef CONFIG_CPU_ISOLATION
+static cpumask_var_t vector_isolation_mask;
+
+/*
+ * Return the scratch mask for the runtime IRQ isolation filter, or NULL
+ * when CPU isolation is disabled or the mask could not be allocated.
+ */
+static struct cpumask *irq_isolation_scratch(void)
+{
+	return cpumask_available(vector_isolation_mask) ?
+	       vector_isolation_mask : NULL;
+}
+#else
+static inline struct cpumask *irq_isolation_scratch(void)
+{
+	return NULL;
+}
+#endif
 static struct irq_chip lapic_controller;
 static struct irq_matrix *vector_matrix;
 #ifdef CONFIG_SMP
@@ -284,11 +304,44 @@ static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest)
 	return ret;
 }
 
+/*
+ * The isolation policy has to be evaluated against the descriptor's leaf
+ * irq_data.  x86_vector_activate() runs with the vector level's irq_data,
+ * whose parent_data is NULL, so a filterability walk from there would
+ * only see the vector chip and bypass the FILTERABLE/PASSTHROUGH contract
+ * of the intermediate domains.
+ */
+static struct irq_data *irq_isolation_policy_data(struct irq_data *irqd)
+{
+	struct irq_desc *desc = irq_data_to_desc(irqd);
+
+	return desc ? &desc->irq_data : irqd;
+}
+
+static const struct cpumask *
+irq_isolation_affinity_mask(struct irq_data *data)
+{
+	const struct cpumask *affmsk = irq_data_get_affinity_mask(data);
+	struct cpumask *scratch = irq_isolation_scratch();
+
+	if (!scratch || !irq_isolation_active() || !irqd_can_balance(data) ||
+	    irqd_affinity_is_managed(data))
+		return affmsk;
+
+	lockdep_assert_held(&vector_lock);
+	if (irq_isolation_select_mask(data, affmsk, scratch))
+		affmsk = scratch;
+	return affmsk;
+}
+
 static int assign_irq_vector_any_locked(struct irq_data *irqd)
 {
+	struct irq_data *policy = irq_isolation_policy_data(irqd);
 	/* Get the affinity mask - either irq_default_affinity or (user) set */
-	const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
+	const struct cpumask *affmsk = irq_isolation_affinity_mask(policy);
+	struct cpumask *scratch = irq_isolation_scratch();
 	int node = irq_data_get_node(irqd);
+	bool isolate;
 
 	if (node != NUMA_NO_NODE) {
 		/* Try the intersection of @affmsk and node mask */
@@ -302,13 +355,52 @@ static int assign_irq_vector_any_locked(struct irq_data *irqd)
 	if (!assign_vector_locked(irqd, vector_searchmask))
 		return 0;
 
+	isolate = scratch && irq_isolation_active() &&
+		  irq_isolation_can_filter(policy) &&
+		  !irqd_affinity_is_managed(policy) && irqd_can_balance(policy);
+
+	/*
+	 * Take a single snapshot of the runtime isolated set for both
+	 * fallbacks below.  Reading it twice could observe two different
+	 * isolation states, and the scratch buffer is no longer needed by
+	 * @affmsk at this point.
+	 */
+	if (isolate) {
+		rcu_read_lock();
+		housekeeping_runtime_isolated(scratch);
+		rcu_read_unlock();
+	}
+
+	if (node != NUMA_NO_NODE && isolate) {
+		/*
+		 * Only runtime-isolated CPUs are excluded here.  Boot-time
+		 * isolcpus=domain CPUs must stay valid fallback targets,
+		 * exactly as without runtime isolation.
+		 */
+		cpumask_andnot(vector_searchmask, cpumask_of_node(node),
+			       scratch);
+		if (!cpumask_empty(vector_searchmask) &&
+		    !assign_vector_locked(irqd, vector_searchmask))
+			return 0;
+	}
+
+	if (isolate) {
+		cpumask_andnot(vector_searchmask, cpu_online_mask, scratch);
+		if (!cpumask_empty(vector_searchmask) &&
+		    !assign_vector_locked(irqd, vector_searchmask))
+			return 0;
+	}
+
+	/*
+	 * The filtered targets are exhausted, or isolation does not apply.
+	 * Retry the upstream unfiltered targets, but only after the
+	 * filtered global set: an isolated CPU in the local node must not
+	 * be preferred over a housekeeping CPU elsewhere.
+	 */
 	if (node != NUMA_NO_NODE) {
-		/* Try the node mask */
 		if (!assign_vector_locked(irqd, cpumask_of_node(node)))
 			return 0;
 	}
-
-	/* Try the full online mask */
 	return assign_vector_locked(irqd, cpu_online_mask);
 }
 
@@ -808,6 +900,10 @@ int __init arch_early_irq_init(void)
 	irq_set_default_domain(x86_vector_domain);
 
 	BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
+#ifdef CONFIG_CPU_ISOLATION
+	if (!zalloc_cpumask_var(&vector_isolation_mask, GFP_KERNEL))
+		pr_err("x86/apic: Failed to allocate the IRQ isolation mask\n");
+#endif
 
 	/*
 	 * Allocate the vector matrix allocator data structure and limit the
@@ -1036,6 +1132,7 @@ static struct irq_chip lapic_controller = {
 	.irq_compose_msi_msg		= x86_vector_msi_compose_msg,
 	.irq_force_complete_move	= apic_force_complete_move,
 	.irq_retrigger			= apic_retrigger_irq,
+	.flags				= IRQCHIP_AFFINITY_FILTERABLE,
 };
 
 #ifdef CONFIG_SMP
diff --git a/arch/x86/kernel/hpet.c b/arch/x86/kernel/hpet.c
index 8dc7b710e1250..dcbf33e15774c 100644
--- a/arch/x86/kernel/hpet.c
+++ b/arch/x86/kernel/hpet.c
@@ -511,7 +511,8 @@ static struct irq_chip hpet_msi_controller __ro_after_init = {
 	.irq_set_affinity = msi_domain_set_affinity,
 	.irq_retrigger = irq_chip_retrigger_hierarchy,
 	.irq_write_msi_msg = hpet_msi_write_msg,
-	.flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_AFFINITY_PRE_STARTUP,
+	.flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_AFFINITY_PRE_STARTUP |
+		 IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static int hpet_msi_init(struct irq_domain *domain,
diff --git a/arch/x86/platform/uv/uv_irq.c b/arch/x86/platform/uv/uv_irq.c
index 4f200ac96ce08..8f810259092f0 100644
--- a/arch/x86/platform/uv/uv_irq.c
+++ b/arch/x86/platform/uv/uv_irq.c
@@ -70,6 +70,7 @@ static struct irq_chip uv_irq_chip = {
 	.irq_unmask		= uv_noop,
 	.irq_eoi		= apic_ack_irq,
 	.irq_set_affinity	= uv_set_irq_affinity,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static int uv_domain_alloc(struct irq_domain *domain, unsigned int virq,
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 0f04013f590d4..9705831adef37 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -4285,6 +4285,7 @@ static struct irq_chip amd_ir_chip = {
 	.irq_set_affinity	= amd_ir_set_affinity,
 	.irq_set_vcpu_affinity	= amd_ir_set_vcpu_affinity,
 	.irq_compose_msi_msg	= ir_compose_msi_msg,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static const struct msi_parent_ops amdvi_msi_parent_ops = {
diff --git a/drivers/iommu/hyperv/hv-irq-remap-x86.c b/drivers/iommu/hyperv/hv-irq-remap-x86.c
index 4963c9ce05dc5..2b6a5693e7de7 100644
--- a/drivers/iommu/hyperv/hv-irq-remap-x86.c
+++ b/drivers/iommu/hyperv/hv-irq-remap-x86.c
@@ -58,6 +58,7 @@ static struct irq_chip hyperv_ir_chip = {
 	.name			= "HYPERV-IR",
 	.irq_ack		= apic_ack_irq,
 	.irq_set_affinity	= hyperv_ir_set_affinity,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
@@ -256,6 +257,7 @@ static struct irq_chip hyperv_root_ir_chip = {
 	.irq_ack		= apic_ack_irq,
 	.irq_set_affinity	= hyperv_root_ir_set_affinity,
 	.irq_compose_msi_msg	= hyperv_root_ir_compose_msi_msg,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static int hyperv_root_irq_remapping_alloc(struct irq_domain *domain,
diff --git a/drivers/iommu/intel/irq_remapping.c b/drivers/iommu/intel/irq_remapping.c
index 25c26f706984a..c9483bb0c6961 100644
--- a/drivers/iommu/intel/irq_remapping.c
+++ b/drivers/iommu/intel/irq_remapping.c
@@ -1282,6 +1282,7 @@ static struct irq_chip intel_ir_chip = {
 	.irq_set_affinity	= intel_ir_set_affinity,
 	.irq_compose_msi_msg	= intel_ir_compose_msi_msg,
 	.irq_set_vcpu_affinity	= intel_ir_set_vcpu_affinity,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 /*
@@ -1326,6 +1327,7 @@ static struct irq_chip intel_ir_chip_post_msi = {
 	.irq_set_affinity	= intel_ir_set_affinity,
 	.irq_compose_msi_msg	= intel_ir_compose_msi_msg,
 	.irq_set_vcpu_affinity	= intel_ir_set_vcpu_affinity,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static void fill_msi_msg(struct msi_msg *msg, u32 index, u32 subhandle)
diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
index 824cf27e85e67..0263b1ba75959 100644
--- a/drivers/pci/controller/pci-hyperv.c
+++ b/drivers/pci/controller/pci-hyperv.c
@@ -2138,6 +2138,7 @@ static bool hv_pcie_init_dev_msi_info(struct device *dev, struct irq_domain *dom
 
 	chip->irq_set_affinity = irq_chip_set_affinity_parent;
 	chip->irq_retrigger = irq_chip_retrigger_hierarchy;
+	chip->flags |= IRQCHIP_AFFINITY_PASSTHROUGH;
 
 	if (IS_ENABLED(CONFIG_X86))
 		chip->flags |= IRQCHIP_MOVE_DEFERRED;
@@ -2171,6 +2172,7 @@ static struct irq_chip hv_msi_irq_chip = {
 	.irq_eoi		= irq_chip_eoi_parent,
 	.irq_mask		= hv_irq_mask,
 	.irq_unmask		= hv_irq_unmask,
+	.flags			= IRQCHIP_AFFINITY_PASSTHROUGH,
 };
 
 static int hv_pcie_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs,
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 09/10] sched/isolation: Publish runtime isolation to genirq
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (7 preceding siblings ...)
  2026-11-30  3:37 ` [RFC PATCH 08/10] x86/apic: Support runtime IRQ isolation Kris Pan
@ 2026-11-30  3:37 ` Kris Pan
  2026-11-30  3:37 ` [RFC PATCH 10/10] net: Keep core-managed IRQ affinity automatic Kris Pan
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

Call irq_isolation_update() from housekeeping_update() after
cpuset_mutex and cpus_read_lock() have been released, so runtime
PRS_ISOLATED changes trigger genirq affinity reapplication through the
existing housekeeping synchronization point without lock inversion.
At this point only cpuset_top_mutex is held, so the full order is
cpuset_top_mutex -> irq_isolation_mutex -> cpus_read_lock ->
sparse_irq_lock -> desc->lock.

CPU hotplug already funnels its housekeeping update through
hk_sd_workfn() instead of running it under cpus_write_lock(), so the
same call covers it without a second entry point.  irq_isolation_update()
asserts that the CPU hotplug lock is not held and takes one read lock
itself around the static key update and the reapply walk.

Document runtime-only filtering, the HK_TYPE_DOMAIN AUTO fallback,
explicit /proc pins, the unfiltered irq_force_affinity(), the automatic
placement of irq_set_affinity_and_hint() and irq_set_affinity_auto()
versus the explicit bare irq_set_affinity(), deferred migration on
IRQCHIP_MOVE_DEFERRED chips and the -EBUSY pending path, the notifier
at queue and completion (the completion one only while runtime
isolation is active), the requested or pending mask it reports versus
the mask a reverse-map consumer follows with
irq_copy_programmed_affinity(), the exclusions (per-CPU, managed), the
IRQF_NOBALANCING scope (an explicit request is replayed unfiltered
while a default placement is still chosen among housekeeping CPUs), the
x86-only opt-in today, the threaded IRQ self-migration, and the x86
fallback which only excludes runtime-isolated CPUs.  Warn that
irqbalance has to be disabled before the partition is created, because
stopping it later does not restore the automatic placement of its
earlier single-CPU /proc pins.  A broad mask written back through
/proc is still explicit but is filtered as well; only the cross-mask
fallback to other housekeeping CPUs is lost.  Describe the PREEMPT_RT
window: the IRQ thread self-migrates before it runs the next handler,
the wake-up is queued against the old scheduling affinity so the thread
can be scheduled once on the isolated CPU, and a handler which is
already running finishes on the old CPU.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 Documentation/admin-guide/cpu-isolation.rst | 102 ++++++++++++++++++++
 kernel/sched/isolation.c                    |   2 +
 2 files changed, 104 insertions(+)

diff --git a/Documentation/admin-guide/cpu-isolation.rst b/Documentation/admin-guide/cpu-isolation.rst
index b9f13a9286734..df4e9e61e3f3e 100644
--- a/Documentation/admin-guide/cpu-isolation.rst
+++ b/Documentation/admin-guide/cpu-isolation.rst
@@ -355,3 +355,105 @@ Some tools may also be useful for higher level analysis:
 
 - cpunoise does something similar to rtla-osnoise but in userspace. It is available
   at https://git.kernel.org/pub/scm/linux/kernel/git/frederic/cpunoise.git
+
+Runtime IRQ isolation
+=====================
+
+When at least one ``cpuset.cpus.partition=isolated`` partition is active
+at runtime, genirq removes the runtime-isolated CPUs from the affinity
+mask programmed for unmanaged interrupts.  Boot-time
+``isolcpus=domain`` CPUs are not filtered out, preserving existing IRQ
+behavior.  The requested affinity is preserved, so the original mask is
+handed to the irqchip again when runtime isolation is removed.  That
+does not force a re-spread: a single-target IRQ (x86 marks its vector
+IRQs as single-target) whose effective CPU is still in the request
+keeps that CPU, because the x86 vector code does not move an interrupt
+whose current target is still valid.  The re-apply still schedules the
+affinity notification and re-marks the IRQ threads, so a reverse map is
+restored even though the vector stays.  IRQs which were only narrowed
+therefore stay on the housekeeping CPU physically after the partition
+is gone unless a balancer or an explicit affinity write moves them.  An automatic IRQ whose whole request became isolated is
+different: the fallback moved it outside the request, so its current
+CPU is not in the mask and the chip does move it back when the CPU
+returns.  The x86 vector allocator
+likewise excludes only runtime-isolated CPUs from its fallback masks, so
+boot-time ``isolcpus=domain`` fallback targets do not change.  A CPU
+which is both boot-isolated and part of a runtime isolated partition is
+not in the runtime set either (it is computed as
+``HK_TYPE_DOMAIN_BOOT`` minus ``HK_TYPE_DOMAIN``), so its interrupt
+behaviour is unchanged from a kernel without runtime isolation.
+
+Only delivery chips which advertise ``IRQCHIP_AFFINITY_FILTERABLE``
+participate; today that is the x86 APIC vector chip, GICv3/ITS is not
+enabled yet.  Every level between the device and the delivery chip which
+implements ``.irq_set_affinity`` has to be marked either FILTERABLE or
+``IRQCHIP_AFFINITY_PASSTHROUGH``: on x86 those are the IO-APIC, PCI-MSI,
+DMAR/AMDVI, HPET, INTEL-IR, AMD-IR, HYPERV-IR, the Hyper-V PCI MSI
+parents and the UV core domain.  An unmarked level keeps filtering off
+for the whole chain instead of inheriting the capability from its
+parent.  Per-CPU and managed interrupts are never filtered.
+``IRQF_NOBALANCING`` resolves the filter in two steps.  The interrupt is
+activated before the flag is applied, so a default placement still
+starts on a housekeeping CPU; an explicit request which was narrowed
+before ``request_irq()`` is then programmed again, because the filter is
+inert for a non-balancing interrupt.  A driver which pre-marks the
+interrupt with ``irq_set_status_flags(irq, IRQ_NO_BALANCING)`` skips
+both steps.
+Default placement and affinity hints are automatic and fall back to
+online ``HK_TYPE_DOMAIN`` housekeeping CPUs when all requested targets
+are runtime-isolated.  A request made through
+``/proc/irq/N/smp_affinity`` is explicit and remains authoritative; it
+is preserved if all online CPUs in the requested mask are
+runtime-isolated.  ``irq_force_affinity()`` is not filtered at all: it
+bypasses the isolation filter and programs the caller supplied mask
+verbatim.  An in-kernel ``irq_set_affinity()`` call is an explicit
+request and is not redirected.  ``irq_set_affinity_and_hint()`` keeps
+the placement automatic, so a driver which binds a queue after the
+partition became isolated is still evicted; core code which manages an
+affinity on a driver's behalf uses ``irq_set_affinity_auto()``.
+
+irqbalance must be disabled before the runtime partition is created,
+preferably from boot.  It writes single-CPU ``/proc`` affinity masks
+which are explicit pins, and stopping the balancer later does not
+restore their automatic placement.  An on-demand "stop irqbalance
+before starting the RT container" is therefore not sufficient: either
+disable it from boot or use a userspace balancer which reads
+``cpuset.cpus.isolated``.  Writing a broad mask back through ``/proc``
+also keeps the IRQ away from runtime-isolated CPUs, because the filter
+subtracts them from explicit masks as well.  The mask stays explicit
+though: if every online CPU in it is isolated, the IRQ cannot fall back
+to other housekeeping CPUs.  Otherwise the earlier ``/proc`` writes
+keep the IRQs pinned to CPUs which later become isolated.
+
+This is best-effort avoidance, not a hard guarantee
+that no interrupt can ever target an isolated CPU.
+
+A chip which sets ``IRQCHIP_MOVE_DEFERRED`` cannot program a mask in
+process context.  The x86 APIC vector code does not set the flag, but
+returns ``-EBUSY`` while a previous move is still in flight; the old
+vector is then cleaned up by the next interrupt.  In both cases the
+core queues the requested mask and programs it from interrupt context.
+The affinity notification is sent when the move is queued and again
+when the pending mask has been programmed; the second one only runs
+while runtime isolation is active.  The notifier reports the requested
+mask (or the pending request).  A consumer which spreads work over
+several CPUs uses the mask the core asks the chip to program,
+``irq_copy_programmed_affinity()``: the request minus the
+runtime-isolated CPUs, or the automatic relocation.
+On ``CONFIG_PREEMPT_RT`` most handlers are forced to threaded context.
+The IRQ thread follows the requested affinity through the existing
+``IRQTF_AFFINITY`` lazy update:
+it processes the flag at the top of its wait loop and blocks in
+``set_cpus_allowed_ptr()`` until it has been moved, so an idle thread
+runs its next handler on a housekeeping CPU.  A deferred hardware
+move marks the IRQ threads as soon as the move is queued, so an
+awakened thread escapes before the hardware move completes.  The
+wake-up itself is queued against the old scheduling affinity, so the
+thread can still be scheduled once on the isolated CPU before it
+migrates; the migration itself completes before the next handler runs.
+A handler which is already running when the affinity changes finishes
+on the old isolated CPU.
+Keep latency-critical RT tasks at a higher real-time priority than the
+threaded IRQ default (``SCHED_FIFO`` 50), or let the device settle
+before entering the critical loop.  Adding a housekeeping CPU to such a
+task's affinity is not an option inside an isolated cpuset partition.
diff --git a/kernel/sched/isolation.c b/kernel/sched/isolation.c
index 418579766a364..1539d6ecd4ffb 100644
--- a/kernel/sched/isolation.c
+++ b/kernel/sched/isolation.c
@@ -201,6 +201,8 @@ int housekeeping_update(struct cpumask *isol_mask)
 	err = kthreads_update_housekeeping();
 	WARN_ON_ONCE(err < 0);
 
+	irq_isolation_update();
+
 	kfree(old);
 
 	return 0;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [RFC PATCH 10/10] net: Keep core-managed IRQ affinity automatic
  2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
                   ` (8 preceding siblings ...)
  2026-11-30  3:37 ` [RFC PATCH 09/10] sched/isolation: Publish runtime isolation to genirq Kris Pan
@ 2026-11-30  3:37 ` Kris Pan
  9 siblings, 0 replies; 11+ messages in thread
From: Kris Pan @ 2026-11-30  3:37 UTC (permalink / raw)
  To: tglx, maz
  Cc: mingo, bp, dave.hansen, hpa, x86, peterz, frederic, juri.lelli,
	vincent.guittot, longman, tj, corbet, linux-doc, davem, edumazet,
	kuba, pabeni, netdev, joro, will, robin.murphy, dwmw2, baolu.lu,
	vasant.hegde, suravee.suthikulpanit, iommu, haiyangz, wei.liu,
	decui, linux-hyperv, bhelgaas, lpieralisi, linux-pci, steve.wahl,
	bigeasy, clrkwllms, linux-rt-devel, radu, linux-kernel, Kris Pan

napi_restore_config() re-applies the affinity which netdev stored for a
NAPI instance.  After the genirq provenance change a bare
irq_set_affinity() is an explicit pin, which would stop runtime IRQ
isolation from moving a queue off a CPU handed to an isolated
partition.

Use irq_set_affinity_auto(), the non-exported helper which records the
request as automatic placement.  It is core-only on purpose: drivers
keep using irq_set_affinity_and_hint() or let netdev manage the
affinity through netif_set_affinity_auto().

The affinity notifier reports the requested mask, which netdev stores
as the affinity to restore.  ARFS steering must follow the mask which
the core asks the chip to program instead: runtime isolation may filter
the request or relocate it when its last online target is gone.  Feed
irq_copy_programmed_affinity() into cpu_rmap_update() while runtime
isolation is active and fall back to the notified mask otherwise.
Without isolation the programmed target still tracks the request, so
the upstream behaviour is kept.

The effective affinity is deliberately not used here: on chips with
IRQD_SINGLE_TARGET it describes one CPU, and a reverse map which covers
several CPUs would collapse to that single entry.  The helper reports
the mask which the policy asked the chip to program: the right input
for a multi-CPU map.

Take the snapshot under desc->lock: the helper runs concurrently with
the reapply path, which may queue a move and update the pending mask.
While a move is pending the newest request lives there, and the
descriptor affinity only follows when it completes.  The legacy
irq_cpu_rmap_add() glue in lib/cpu_rmap.c consumes the same notifier
for mlx5, sfc and others; it needs the same mask.  A driver which
registers its own irq_affinity_notify callback replaces that glue and
is not converted here; bnxt does so for its TPH and aRFS steering and
would have to take the same snapshot itself.

Initial queue placement is unaffected: default descriptor setup and
the driver hint path (irq_set_affinity_and_hint()) already record
automatic placement.  napi_restore_config() is the only place where
net/core re-applies a stored automatic affinity with a bare
irq_set_affinity() call, which is why only that call site changes.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 lib/cpu_rmap.c | 32 +++++++++++++++++++++++++++++++-
 net/core/dev.c | 24 ++++++++++++++++++++++--
 2 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c
index c86ab6e55d178..f2e9a3ac135f4 100644
--- a/lib/cpu_rmap.c
+++ b/lib/cpu_rmap.c
@@ -6,6 +6,7 @@
 
 #include <linux/cpu_rmap.h>
 #include <linux/interrupt.h>
+#include <linux/irq.h>
 #include <linux/export.h>
 
 /*
@@ -264,11 +265,40 @@ irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
 {
 	struct irq_glue *glue =
 		container_of(notify, struct irq_glue, notify);
+	const struct cpumask *prog;
+	cpumask_var_t prog_mask;
+	bool allocated;
 	int rc;
 
-	rc = cpu_rmap_update(glue->rmap, glue->index, mask);
+	prog = mask;
+	allocated = false;
+	/*
+	 * Runtime IRQ isolation may program a mask which differs from the
+	 * requested one.  A reverse map describes the CPUs the chip is
+	 * asked to use, so take the filtered mask while isolation is
+	 * active; without it the upstream behaviour of using the notified
+	 * mask is kept.  Check the static key before allocating the
+	 * snapshot buffer, so a system which never isolates keeps the
+	 * allocation out of the notifier path.
+	 */
+	if (irq_isolation_active()) {
+		allocated = alloc_cpumask_var(&prog_mask, GFP_KERNEL);
+		if (allocated &&
+		    irq_copy_programmed_affinity(notify->irq, prog_mask))
+			prog = prog_mask;
+	}
+
+	/*
+	 * The notifier reports the requested mask.  The reverse map has to
+	 * use the mask which was programmed instead: runtime IRQ isolation
+	 * may have filtered the request, or relocated it when the request
+	 * lost its last online target.
+	 */
+	rc = cpu_rmap_update(glue->rmap, glue->index, prog);
 	if (rc)
 		pr_warn("irq_cpu_rmap_notify: update failed: %d\n", rc);
+	if (allocated)
+		free_cpumask_var(prog_mask);
 }
 
 /**
diff --git a/net/core/dev.c b/net/core/dev.c
index cd0796a1dbfdf..50d41ad09add9 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -88,6 +88,7 @@
 #include <linux/sockios.h>
 #include <linux/errno.h>
 #include <linux/interrupt.h>
+#include <linux/irq.h>
 #include <linux/if_ether.h>
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
@@ -7593,6 +7594,9 @@ netif_napi_irq_notify(struct irq_affinity_notify *notify,
 		container_of(notify, struct napi_struct, notify);
 #ifdef CONFIG_RFS_ACCEL
 	struct cpu_rmap *rmap = napi->dev->rx_cpu_rmap;
+	const struct cpumask *prog;
+	cpumask_var_t prog_mask;
+	bool allocated;
 	int err;
 #endif
 
@@ -7601,7 +7605,23 @@ netif_napi_irq_notify(struct irq_affinity_notify *notify,
 
 #ifdef CONFIG_RFS_ACCEL
 	if (napi->dev->rx_cpu_rmap_auto) {
-		err = cpu_rmap_update(rmap, napi->napi_rmap_idx, mask);
+		/*
+		 * The notifier reports the requested mask; the reverse map
+		 * has to describe the CPUs the chip is asked to use, which
+		 * runtime IRQ isolation may have filtered or relocated.
+		 */
+		prog = mask;
+		allocated = false;
+		/* See irq_cpu_rmap_notify(). */
+		if (irq_isolation_active()) {
+			allocated = alloc_cpumask_var(&prog_mask, GFP_KERNEL);
+			if (allocated &&
+			    irq_copy_programmed_affinity(napi->irq, prog_mask))
+				prog = prog_mask;
+		}
+		err = cpu_rmap_update(rmap, napi->napi_rmap_idx, prog);
+		if (allocated)
+			free_cpumask_var(prog_mask);
 		if (err)
 			netdev_warn(napi->dev, "RMAP update failed (%d)\n",
 				    err);
@@ -7752,7 +7772,7 @@ static void napi_restore_config(struct napi_struct *n)
 
 	if (n->dev->irq_affinity_auto &&
 	    test_bit(NAPI_STATE_HAS_NOTIFIER, &n->state))
-		irq_set_affinity(n->irq, &n->config->affinity_mask);
+		irq_set_affinity_auto(n->irq, &n->config->affinity_mask);
 
 	/* a NAPI ID might be stored in the config, if so use it. if not, use
 	 * napi_hash_add to generate one for us.
-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-30  3:41 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®