git: 0e496388ef1b - stable/14 - x86 msi: Enable/disable IDT vectors for MSI groups all at once

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 24 Oct 2023 19:26:01 UTC
The branch stable/14 has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=0e496388ef1bc9001378201bf8af2115d8739500

commit 0e496388ef1bc9001378201bf8af2115d8739500
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-10-20 21:52:38 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-10-24 19:09:23 +0000

    x86 msi: Enable/disable IDT vectors for MSI groups all at once
    
    Unlike MSI-X, when a device uses multiple MSI interrupts, the entire
    group of interrupts are enabled/disabled at once in the relevant PCI
    config register.  Currently, the interrupt code enables the IDT vector
    for each MSI interrupt when a handler is first registered.  If the PCI
    device triggers an MSI interrupt which doesn't yet have a handler,
    this can trigger a panic when the Xrsvd ISR executes rather than
    treating it as a stray device interrupt.
    
    To fix, enable all the IDT vectors for an MSI group when the first
    interrupt handler is configured, and don't disable the IDT vectors
    until the last interrupt handler for the group is torn down.
    
    When migrating an MSI group between CPUs, enable/disable the entire
    group of IDT vectors if at least one interrupt handler is configured
    for the group.
    
    Reported by:    jhay
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D42232
    
    (cherry picked from commit 2d4924892144f653a7a7afba27ed1bf536dd7e51)
---
 sys/x86/x86/msi.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/sys/x86/x86/msi.c b/sys/x86/x86/msi.c
index 7f4d87c09453..246645efbc10 100644
--- a/sys/x86/x86/msi.c
+++ b/sys/x86/x86/msi.c
@@ -120,6 +120,7 @@ struct msi_intsrc {
 	u_int msi_cpu;			/* Local APIC ID. (g) */
 	u_int msi_count:8;		/* Messages in this group. (g) */
 	u_int msi_maxcount:8;		/* Alignment for this group. (g) */
+	u_int msi_enabled:8;		/* Enabled messages in this group. (g) */
 	u_int *msi_irqs;		/* Group's IRQ list. (g) */
 	u_int msi_remap_cookie;
 };
@@ -204,7 +205,12 @@ msi_enable_intr(struct intsrc *isrc)
 {
 	struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
 
-	apic_enable_vector(msi->msi_cpu, msi->msi_vector);
+	msi = msi->msi_first;
+	if (msi->msi_enabled == 0) {
+		for (u_int i = 0; i < msi->msi_count; i++)
+			apic_enable_vector(msi->msi_cpu, msi->msi_vector + i);
+	}
+	msi->msi_enabled++;
 }
 
 static void
@@ -212,7 +218,12 @@ msi_disable_intr(struct intsrc *isrc)
 {
 	struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
 
-	apic_disable_vector(msi->msi_cpu, msi->msi_vector);
+	msi = msi->msi_first;
+	msi->msi_enabled--;
+	if (msi->msi_enabled == 0) {
+		for (u_int i = 0; i < msi->msi_count; i++)
+			apic_disable_vector(msi->msi_cpu, msi->msi_vector + i);
+	}
 }
 
 static int
@@ -277,11 +288,8 @@ msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
 	/* Must be set before BUS_REMAP_INTR as it may call back into MSI. */
 	msi->msi_cpu = apic_id;
 	msi->msi_vector = vector;
-	if (msi->msi_intsrc.is_handlers > 0)
-		apic_enable_vector(msi->msi_cpu, msi->msi_vector);
-	for (i = 1; i < msi->msi_count; i++) {
-		sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
-		if (sib->msi_intsrc.is_handlers > 0)
+	if (msi->msi_enabled > 0) {
+		for (i = 0; i < msi->msi_count; i++)
 			apic_enable_vector(apic_id, vector + i);
 	}
 	error = BUS_REMAP_INTR(device_get_parent(msi->msi_dev), msi->msi_dev,
@@ -317,15 +325,13 @@ msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
 	 * to prevent races where we could miss an interrupt.  If BUS_REMAP_INTR
 	 * failed then we disable and free the new, unused vector(s).
 	 */
-	if (msi->msi_intsrc.is_handlers > 0)
-		apic_disable_vector(old_id, old_vector);
-	apic_free_vector(old_id, old_vector, msi->msi_irq);
-	for (i = 1; i < msi->msi_count; i++) {
-		sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
-		if (sib->msi_intsrc.is_handlers > 0)
+	if (msi->msi_enabled > 0) {
+		for (i = 0; i < msi->msi_count; i++)
 			apic_disable_vector(old_id, old_vector + i);
-		apic_free_vector(old_id, old_vector + i, msi->msi_irqs[i]);
 	}
+	apic_free_vector(old_id, old_vector, msi->msi_irq);
+	for (i = 1; i < msi->msi_count; i++)
+		apic_free_vector(old_id, old_vector + i, msi->msi_irqs[i]);
 	return (error);
 }