From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796010056; b=klusnKCKuPZGqoWDTnLpmeaUf3ZRncK+u3vN6+seGXuklfsvGLqlKe8lQ5+AMfYIq6Vv M153smSAe0y8C4iY4PMSrQNH7PWL8J12RrJuggZFgFBOuOXPKVLeMkHMN3uGbMkJ/aJnP +KX7/VMnu/cGqkD7l5w2Cq5LFzVekBkhERuzFG6cpXku4g6VVJytvJfDVjIz46YvDoqWE 4V1pPlpCbkwXQndqrwYKcc9wwLVn7PLiCt38jgSGe45SrNsNl9dPUy0hiVdig5A1ajseW TR4x2zn4te7/J7UaOcSvMdq63tSKgrwRa0+q9rGZBy5Go1hEAN+FPC5yTeQ7/44XO1g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796010056; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=JgGRMp5qTI1FEgIIh2pQOl7NhOKJPtBWgRbPf4iuHZY=; b=wy6vi9p/ms6p728ieMePAweTbxwfj438FzZFGn1HvtOYZ7Rmlr9kL8m1whR+SYDQFgW5 xIwCpY5ia6yIHgyFiL9GQG40qeYIGulqM2FRcbzYMgLpbV94QgI0ncs9SuhGlVI49lCrU CNE8qWSszrFsaZ2FXwcshD19HP3EwsWU+4DiVIoqNwhvxMv1dwtgSMsciXVH6HUt3VElO ZMnJXBecikqdrUaEbP5atCpA+NU5i3BEt4Jd4YzVRKwTQzREPNcMR5PadfAZadqldfslp fYlDLlD/cjxb6Mrd5h0ckTzKZ4/FDfPbok0EJ1eMg8wYSrpkBH7hXZmCluZTJ4PMmlQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=b3NU584B; arc=none smtp.remote-ip=192.198.163.19 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 788D6606A31C; Mon, 30 Nov 2026 03:40:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 788D6606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=b3NU584B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796010055; x=1827546055; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=d90bQJ3h8k1+usS6IsrBo8sVh/YR3hQ2+3nZ7eun+0w=; b=b3NU584BBcuuIjSQex4tPrTFCvM6e/i4pq7qIBSxft7Bd3NIRYmJyC6a s/J/jgY5KvJKOzEITjZrZoqTwbPT2NhoFkJHxciLsfQ+ng9+bJEmI/34z kiCezxx8uEI3iizI5UMP0PVCU0J7xK4oSQyVeEs+wpO06+62qbtKoeiTW ORKXEAtt7BVVRbkdWJTCEb0weuf8Iq/7XegpBkjQjlWcBn8LEXfqv6H4c GYvNGhsxEDTjGzOBNw7quswx17UoOjI/1mPugWRkbcSOvHWkXqEboVsIi hodBao1p2CULopRd8P8DTZ8MikGW5x9cGDrcaZqrDtENgf+EWcz4qYuQK A==; X-CSE-ConnectionGUID: N1YAaWogQlyRT+0vkwCA5g== X-CSE-MsgGUID: OT3LsDO6QhalKYXCavpNuw== X-IronPort-AV: E=McAfee;i="6800,10657,11981"; a="4848305" X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="4848305" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 19:40:55 -0800 X-CSE-ConnectionGUID: LTLatAq6TpOfo4h5IWOdQg== X-CSE-MsgGUID: TS2kLwgMTiOrLrImpP9WqQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="12951051" Received: from pankris-x299-ud4-pro.bj.intel.com ([10.238.152.122]) by fmviesa005.fm.intel.com with ESMTP; 29 Nov 2026 19:40:46 -0800 From: Kris Pan To: tglx@kernel.org, maz@kernel.org Cc: mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, x86@kernel.org, peterz@infradead.org, frederic@kernel.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, longman@redhat.com, tj@kernel.org, corbet@lwn.net, linux-doc@vger.kernel.org, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, dwmw2@infradead.org, baolu.lu@linux.intel.com, vasant.hegde@amd.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, linux-hyperv@vger.kernel.org, bhelgaas@google.com, lpieralisi@kernel.org, linux-pci@vger.kernel.org, steve.wahl@hpe.com, bigeasy@linutronix.de, clrkwllms@kernel.org, linux-rt-devel@lists.linux.dev, radu@rendec.net, linux-kernel@vger.kernel.org, Kris Pan Subject: [RFC PATCH 05/10] genirq: Reapply unmanaged IRQ affinity on isolation changes Date: Mon, 30 Nov 2026 11:37:03 +0800 Message-ID: <20261130033709.2704805-6-kris.pan@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261130033709.2704805-1-kris.pan@intel.com> References: <20261130033709.2704805-1-kris.pan@intel.com> X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 + * . + */ +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 +#include +#include +#include #include +#include +#include +#include #include #include #include +#include +#include +#include #include #include #include +#include #include #include #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