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…@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