public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Luigi Rizzo <lrizzo@google.com>,
	jacob.jun.pan@linux.intel.com, lrizzo@google.com,
	rizzo.unipi@gmail.com, seanjc@google.com
Cc: a.manzanares@samsung.com, acme@kernel.org, axboe@kernel.dk,
	baolu.lu@linux.intel.com, bp@alien8.de, dan.j.williams@intel.com,
	dave.hansen@intel.com, guang.zeng@intel.com, helgaas@kernel.org,
	hpa@zytor.com, iommu@lists.linux.dev, jim.harris@samsung.com,
	joro@8bytes.org, kevin.tian@intel.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, maz@kernel.org, mingo@redhat.com,
	oliver.sang@intel.com, paul.e.luse@intel.com,
	peterz@infradead.org, robert.hoo.linux@gmail.com,
	robin.murphy@arm.com, x86@kernel.org
Subject: Re: [PATCH v3  00/12] Coalesced Interrupt Delivery with posted MSI
Date: Mon, 24 Nov 2025 19:59:35 +0100	[thread overview]
Message-ID: <87a50bi7e0.ffs@tglx> (raw)
In-Reply-To: <20251124104836.3685533-1-lrizzo@google.com>

On Mon, Nov 24 2025 at 10:48, Luigi Rizzo wrote:
> I think there is an inherent race condition when intremap=posted_msi
> and the IRQ subsystem resends pending interrupts via __apic_send_IPI().
>
> In detail:
> intremap=posted_msi does not process vectors for which the
> corresponding bit in the PIR register is set.
>
> Now say that, for whatever reason, the IRQ infrastructure intercepts
> an interrupt marking it as PENDING. . handle_edge_irq() and many other
> places in kernel/irq have sections of code like this:
>
>     if (!irq_may_run(desc)) {
>         desc->istate |= IRQS_PENDING;
> 	mask_ack_irq(desc);
> 	goto out_unlock;
>     }
>
> Then eventually check_irq_resend() will try to resend pending interrupts
>
>     desc->istate &= ~IRQS_PENDING;
>     if (!try_retrigger(desc))
>         err = irq_sw_resend(desc);
>
> try_retrigger() on x86 eventually calls apic_retrigger_irq() which
> uses __apic_send_IPI(). Unfortunately the latter does not seem to
> set the 'vector' bit in the PIR (nor sends the POSTED_MSI interrupt)
> thus potentially causing a lost interrupt unless there is some other
> spontaneous interrupt coming from the device.

It sends an IPI to the actual vector, which invokes the handler
directly. That works only once because the remap interrupt chip does not
issue an EOI, so the vector becomes stale.... Clearly nobody ever tested
that code.

> I could verify the stall (forcing the path that sets IRQS_PENDING),
> and could verify that the patch below fixes the problem

You can do the same with

    echo trigger > /sys/kernel/debug/irq/irqs/$IRQNR

>  static int apic_retrigger_irq(struct irq_data *irqd)
>  {
>         struct apic_chip_data *apicd = apic_chip_data(irqd);
>         unsigned long flags;
> +       uint vec;
>
>         raw_spin_lock_irqsave(&vector_lock, flags);
> +       vec = apicd->vector;
> +       if (posted_msi_supported() &&
> +           vec >= FIRST_EXTERNAL_VECTOR && vec < FIRST_SYSTEM_VECTOR) {
> +               struct pi_desc *pid = per_cpu_ptr(&posted_msi_pi_desc, apicd->cpu);

Won't build with CONFIG_X86_POSTED_MSI=n

> +               set_bit(vec, (unsigned long *)pid->pir64);
> +               __apic_send_IPI(apicd->cpu, POSTED_MSI_NOTIFICATION_VECTOR);

That's wrong as it affects all interrupts and not just the MSI ones
which are going through the posted vector. As the interrupt chip of
those other ones is different, that retriggered handler would issue two
EOIs, which is not what we want.

That's what the interrupt hierarchy is for.

While that _is_ solvable, there is another issue which is not solvable
by that at all.

If there is ever a stray interrupt on such a vector, then the related
APIC ISR bit becomes stale due to the lack of EOI, which means all
vectors below that vector are never serviced again. Unlikely to happen,
but if it happens it's not debuggable at all.

So instead of playing games with the PIR, this can be actually solved
for both cases. See below.

Thanks,

        tglx
---
 arch/x86/include/asm/irq_remapping.h |    7 +++++++
 arch/x86/kernel/irq.c                |   28 +++++++++++++++++++++++-----
 drivers/iommu/intel/irq_remapping.c  |    8 ++++----
 3 files changed, 34 insertions(+), 9 deletions(-)

--- a/arch/x86/include/asm/irq_remapping.h
+++ b/arch/x86/include/asm/irq_remapping.h
@@ -87,4 +87,11 @@ static inline void panic_if_irq_remap(co
 }
 
 #endif /* CONFIG_IRQ_REMAP */
+
+#ifdef CONFIG_X86_POSTED_MSI
+void intel_ack_posted_msi_irq(struct irq_data *irqd);
+#else
+#define intel_ack_posted_msi_irq(irqd)	irq_move_irq(irdq)
+#endif
+
 #endif /* __X86_IRQ_REMAPPING_H */
--- a/arch/x86/kernel/irq.c
+++ b/arch/x86/kernel/irq.c
@@ -396,6 +396,7 @@ DEFINE_IDTENTRY_SYSVEC_SIMPLE(sysvec_kvm
 
 /* Posted Interrupt Descriptors for coalesced MSIs to be posted */
 DEFINE_PER_CPU_ALIGNED(struct pi_desc, posted_msi_pi_desc);
+static DEFINE_PER_CPU_CACHE_HOT(bool, posted_msi_handler_active);
 
 void intel_posted_msi_init(void)
 {
@@ -413,6 +414,24 @@ void intel_posted_msi_init(void)
 	this_cpu_write(posted_msi_pi_desc.ndst, destination);
 }
 
+void intel_ack_posted_msi_irq(struct irq_data *irqd)
+{
+	irq_move_irq(irqd);
+	/*
+	 * Handle the rare case that irq_retrigger() raised the actual
+	 * assigned vector on the target CPU, which means that it was not
+	 * invoked via the posted MSI handler below. In that case APIC EOI
+	 * is required as otherwise the ISR entry becomes stale and lower
+	 * priority interrupts are never going to be delivered after that.
+	 *
+	 * If the posted handler invoked the device interrupt handler then
+	 * the EOI would be premature because it would acknowledge the
+	 * posted vector.
+	 */
+	if (unlikely(!this_cpu_read(posted_msi_handler_active)))
+		apic_eoi();
+}
+
 static __always_inline bool handle_pending_pir(unsigned long *pir, struct pt_regs *regs)
 {
 	unsigned long pir_copy[NR_PIR_WORDS];
@@ -439,12 +458,10 @@ static __always_inline bool handle_pendi
  */
 DEFINE_IDTENTRY_SYSVEC(sysvec_posted_msi_notification)
 {
+	struct pi_desc *pid = this_cpu_ptr(&posted_msi_pi_desc);
 	struct pt_regs *old_regs = set_irq_regs(regs);
-	struct pi_desc *pid;
-	int i = 0;
-
-	pid = this_cpu_ptr(&posted_msi_pi_desc);
 
+	this_cpu_write(posted_msi_handler_active, true);
 	inc_irq_stat(posted_msi_notification_count);
 	irq_enter();
 
@@ -453,7 +470,7 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_posted_msi
 	 * after clearing the outstanding notification bit. Hence, at most
 	 * MAX_POSTED_MSI_COALESCING_LOOP - 1 loops are executed here.
 	 */
-	while (++i < MAX_POSTED_MSI_COALESCING_LOOP) {
+	for (int i = 1; i < MAX_POSTED_MSI_COALESCING_LOOP; i++) {
 		if (!handle_pending_pir(pid->pir, regs))
 			break;
 	}
@@ -473,6 +490,7 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_posted_msi
 
 	apic_eoi();
 	irq_exit();
+	this_cpu_write(posted_msi_handler_active, false);
 	set_irq_regs(old_regs);
 }
 #endif /* X86_POSTED_MSI */
--- a/drivers/iommu/intel/irq_remapping.c
+++ b/drivers/iommu/intel/irq_remapping.c
@@ -1303,17 +1303,17 @@ static struct irq_chip intel_ir_chip = {
  *	irq_enter();
  *		handle_edge_irq()
  *			irq_chip_ack_parent()
- *				irq_move_irq(); // No EOI
+ *				intel_ack_posted_msi_irq(); // No EOI
  *			handle_irq_event()
  *				driver_handler()
  *		handle_edge_irq()
  *			irq_chip_ack_parent()
- *				irq_move_irq(); // No EOI
+ *				intel_ack_posted_msi_irq(); // No EOI
  *			handle_irq_event()
  *				driver_handler()
  *		handle_edge_irq()
  *			irq_chip_ack_parent()
- *				irq_move_irq(); // No EOI
+ *				intel_ack_posted_msi_irq(); // No EOI
  *			handle_irq_event()
  *				driver_handler()
  *	apic_eoi()
@@ -1322,7 +1322,7 @@ static struct irq_chip intel_ir_chip = {
  */
 static struct irq_chip intel_ir_chip_post_msi = {
 	.name			= "INTEL-IR-POST",
-	.irq_ack		= irq_move_irq,
+	.irq_ack		= intel_ack_posted_msi_irq,
 	.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,


  reply	other threads:[~2025-11-24 18:59 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-23 17:41 [PATCH v3 00/12] Coalesced Interrupt Delivery with posted MSI Jacob Pan
2024-04-23 17:41 ` [PATCH v3 01/12] KVM: VMX: Move posted interrupt descriptor out of vmx code Jacob Pan
2024-04-23 17:41 ` [PATCH v3 02/12] x86/irq: Unionize PID.PIR for 64bit access w/o casting Jacob Pan
2024-04-23 17:41 ` [PATCH v3 03/12] x86/irq: Remove bitfields in posted interrupt descriptor Jacob Pan
2024-05-01  7:29   ` Oliver Sang
2024-04-23 17:41 ` [PATCH v3 04/12] x86/irq: Add a Kconfig option for posted MSI Jacob Pan
2024-04-23 17:41 ` [PATCH v3 05/12] x86/irq: Reserve a per CPU IDT vector for posted MSIs Jacob Pan
2024-04-23 17:41 ` [PATCH v3 06/12] x86/irq: Set up per host CPU posted interrupt descriptors Jacob Pan
2024-04-23 17:41 ` [PATCH v3 07/12] x86/irq: Factor out calling ISR from common_interrupt Jacob Pan
2024-04-23 17:41 ` [PATCH v3 08/12] x86/irq: Install posted MSI notification handler Jacob Pan
2024-04-23 17:41 ` [PATCH v3 09/12] x86/irq: Factor out common code for checking pending interrupts Jacob Pan
2024-04-23 17:41 ` [PATCH v3 10/12] x86/irq: Extend checks for pending vectors to posted interrupts Jacob Pan
2024-04-23 17:41 ` [PATCH v3 11/12] iommu/vt-d: Make posted MSI an opt-in cmdline option Jacob Pan
2024-04-23 17:41 ` [PATCH v3 12/12] iommu/vt-d: Enable posted mode for device MSIs Jacob Pan
2025-11-24 10:48 ` [PATCH v3 00/12] Coalesced Interrupt Delivery with posted MSI Luigi Rizzo
2025-11-24 18:59   ` Thomas Gleixner [this message]
2025-11-24 22:40     ` Luigi Rizzo

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=87a50bi7e0.ffs@tglx \
    --to=tglx@linutronix.de \
    --cc=a.manzanares@samsung.com \
    --cc=acme@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=baolu.lu@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=guang.zeng@intel.com \
    --cc=helgaas@kernel.org \
    --cc=hpa@zytor.com \
    --cc=iommu@lists.linux.dev \
    --cc=jacob.jun.pan@linux.intel.com \
    --cc=jim.harris@samsung.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lrizzo@google.com \
    --cc=maz@kernel.org \
    --cc=mingo@redhat.com \
    --cc=oliver.sang@intel.com \
    --cc=paul.e.luse@intel.com \
    --cc=peterz@infradead.org \
    --cc=rizzo.unipi@gmail.com \
    --cc=robert.hoo.linux@gmail.com \
    --cc=robin.murphy@arm.com \
    --cc=seanjc@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

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

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