All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kris Pan" <kris.pan@intel.com>
Cc: linux-hyperv@vger.kernel.org, linux-pci@vger.kernel.org,
	linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation
Date: Tue, 01 Dec 2026 03:41:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-242461@kernel.org> (raw)
In-Reply-To: <20261130033709.2704805-7-kris.pan@intel.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] genirq: missed thread affinity update in
  irq_isolation_needs_reapply()
- [Medium] genirq: stale thread and notifier affinity in
  irq_move_masked_irq()
- [Medium] genirq: broken fallback in irq_thread_check_affinity() on
  false filter
- [Medium] genirq: redundant affinity notifier work in
  irq_move_masked_irq()
- [Medium] genirq: unconditional IRQ thread wakeups on pending affinity
  moves
- [Medium] genirq: programmed affinity mismatch after -ENOSPC in
  irq_do_set_affinity()
--

commit 1aff5a02a459cf6c403f218b2e8e9313e0d75920
Author: Kris Pan <kris.pan@intel.com>

genirq: Align affinity notifiers and IRQ threads with isolation

This commit adds irq_copy_programmed_affinity() for consumers that need
the runtime-isolation-filtered affinity mask, keeps irq_affinity_notify()
reporting the requested mask, and updates IRQ thread affinity when a move
is pending or trapped on a runtime-isolated CPU.

> 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);
> +}

[Severity: Medium]
Can irq_copy_programmed_affinity() return a mask that excludes the CPU
actually programmed when irq_do_set_affinity() falls back on -ENOSPC?

When chip->irq_set_affinity() returns -ENOSPC on the mask produced by
irq_isolation_select_mask(), irq_do_set_affinity() retries with
irq_isolation_widen_mask(), irq_isolation_widen_mask_full(), and finally
the unfiltered requested mask.  If one of those retries succeeds,
irq_copy_programmed_affinity() still returns the narrower
irq_isolation_select_mask() result that failed vector allocation.  In the
same situation, if irq_do_set_affinity() fell back to a runtime-isolated
CPU in the request, irq_thread_check_affinity() sees trapped == true and
uses irq_isolation_select_mask() to move the IRQ thread onto the
non-isolated CPUs where vector allocation failed, away from the effective
CPU where the interrupt fires.

> 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);

[Severity: Medium]
Can irq_isolation_needs_reapply() cause irq_reapply_affinity() to return
early and skip irq_set_thread_affinity(desc) for threaded IRQs without an
affinity notifier?

kernel/irq/isolation.c:irq_isolation_needs_reapply() {
    ...
	if (irq_affinity_is_auto(desc)) {
		if (irq_isolation_active())
			trapped = cpumask_empty(eff) ||
				  housekeeping_runtime_isolated_intersects(eff) ||
    ...
	}

	restored = irq_reapply_restores_target(changed, affinity);

	notify_changed = desc->affinity_notify &&
			 (cpumask_intersects(changed, affinity) ||
			  (irq_move_pending(data) &&
			   cpumask_intersects(changed,
					      irq_desc_get_pending_mask(desc))));
    ...
}

In irq_thread_check_affinity(), whenever the effective mask is on a
runtime-isolated CPU (for example CPU 0), the IRQ thread escapes to the
filtered target in desc->pending_mask or desc->irq_common_data.affinity
(say {CPU 1, CPU 2}), even when the affinity was set explicitly
(!irq_affinity_is_auto(desc)) and desc->affinity_notify is NULL.  If a
later partition change isolates CPU 1 (changed = {CPU 1}),
irq_isolation_needs_reapply() sees trapped == false, restored == false,
notify_changed == false, and unaffected == true (because changed does not
intersect the old effective CPU 0), so irq_reapply_affinity() returns
before calling irq_set_thread_affinity(desc) and leaves the IRQ thread
allowed to run on the newly isolated CPU 1.  Likewise, because restored
only checks desc->irq_common_data.affinity, de-isolating a CPU that is in
desc->pending_mask will not update the IRQ thread when
desc->affinity_notify is NULL.

> @@ -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);

[Severity: Medium]
Does calling irq_set_thread_affinity(desc) unconditionally on every
pending move in irq_set_affinity_locked() and irq_reapply_affinity()
wake the IRQ kthreads even when the effective CPU is not isolated?

irq_set_thread_affinity() does not just set IRQTF_AFFINITY; it also calls
wake_up_process() on every action thread.  When the current effective
mask m does not intersect a runtime-isolated CPU (or when
irq_isolation_update() just disabled irq_isolation_key before calling
irq_isolation_reapply_all()), the woken kthread enters
irq_thread_check_affinity(), sees !trapped, and runs a no-op
set_cpus_allowed_ptr(current, m), only to be woken again when the pending
move completes.

[ ... ]

> @@ -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);

[Severity: Medium]
What happens in this fallback when the effective affinity mask m is empty
or when the IRQ is non-balancing or managed?

On a filterable chip where cpumask_empty(m) is true, if target initially
contains both an isolated CPU and a housekeeping CPU, trapped is true and
irq_isolation_select_mask() narrows the thread to the housekeeping CPU.
When that isolated CPU is later de-isolated (or target is updated
synchronously to a mask without isolated CPUs),
irq_isolation_select_mask() returns false, clean is false because pending
is false (or !trapped is taken if the last isolated partition was
removed), and mask is copied from the empty mask m.
set_cpus_allowed_ptr(current, mask) then fails with -EINVAL, leaving the
IRQ thread permanently pinned to the old filtered CPU subset.

In addition, the clean check only tests
irq_isolation_can_filter(&desc->irq_data) without checking
irqd_can_balance(&desc->irq_data) && !irqd_affinity_is_managed(&desc->irq_data)
as irq_isolation_select_mask() does, so a pending move on an
IRQF_NOBALANCING or managed IRQ whose target has no runtime-isolated CPUs
will widen the thread affinity to target & cpu_online_mask instead of
keeping m.

> 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);
>  }

[Severity: Medium]
If the deferred move in irq_move_masked_irq() is dropped or fails with an
error other than -EBUSY, do the IRQ threads and affinity notifier
consumers stay stuck on the discarded pending_mask?

When a move is deferred in irq_set_affinity_locked() or
irq_reapply_affinity(), the IRQ threads and desc->affinity_notify are
immediately updated from desc->pending_mask while
desc->irq_common_data.affinity keeps the previous mask.  If
desc->pending_mask no longer intersects cpu_online_mask by the time
irq_move_masked_irq() runs, or if irq_do_set_affinity() fails with an
error such as -ENOSPC, irq_move_masked_irq() clears desc->pending_mask
without calling irq_set_thread_affinity(desc) or scheduling
desc->affinity_notify, leaving both the threads and notifier consumers
diverged from desc->irq_common_data.affinity.

[Severity: Medium]
In the success case (!ret), is scheduling desc->affinity_notify a second
time from irq_move_masked_irq() redundant?

When the deferred move was queued, irq_set_affinity_locked() or
irq_reapply_affinity() already scheduled the notifier, and both
irq_affinity_notify() and irq_copy_programmed_affinity() already use
desc->pending_mask while irq_move_pending(&desc->irq_data) is true.  Once
irq_do_set_affinity() copies desc->pending_mask into
desc->irq_common_data.affinity and clears IRQD_SETAFFINITY_PENDING,
irq_affinity_notify() and irq_copy_programmed_affinity() produce the
exact same masks as before, so this schedules duplicate notifier work from
hardirq context on every deferred move completion.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130033709.2704805-1-kris.pan@intel.com?part=6

  reply	other threads:[~2026-12-01  3:41 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  3:36 [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs Kris Pan
2026-11-30  3:36 ` [RFC PATCH 01/10] genirq: Add IRQCHIP_AFFINITY_FILTERABLE flag Kris Pan
2026-11-30  3:37 ` [RFC PATCH 02/10] sched/isolation: Add runtime isolation helpers Kris Pan
2026-11-30  3:37 ` [RFC PATCH 03/10] genirq: Track automatic vs. explicit affinity provenance Kris Pan
2026-11-30  3:37 ` [RFC PATCH 04/10] genirq: Filter unmanaged IRQ affinity against runtime isolation Kris Pan
2026-11-30  3:37 ` [RFC PATCH 05/10] genirq: Reapply unmanaged IRQ affinity on isolation changes Kris Pan
2026-12-01  3:41   ` sashiko-bot
2026-11-30  3:37 ` [RFC PATCH 06/10] genirq: Align affinity notifiers and IRQ threads with isolation Kris Pan
2026-12-01  3:41   ` sashiko-bot [this message]
2026-11-30  3:37 ` [RFC PATCH 07/10] genirq: Handle runtime isolation across hotplug and suspend Kris Pan
2026-12-01  3:41   ` sashiko-bot
2026-11-30  3:37 ` [RFC PATCH 08/10] x86/apic: Support runtime IRQ isolation Kris Pan
2026-12-01  3:41   ` sashiko-bot
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
2026-12-01  3:41   ` sashiko-bot
2026-12-01  3:41 ` [RFC PATCH 00/10] genirq/cpuset: Evict unmanaged IRQs from runtime-isolated CPUs sashiko-bot
2026-12-01  5:13 ` Kris Pan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-242461@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kris.pan@intel.com \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.