Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

virtio-pci: harden INTX interrupts

This patch tries to make sure the virtio interrupt handler for INTX
won't be called after a reset and before virtio_device_ready(). We
can't use IRQF_NO_AUTOEN since we're using shared interrupt
(IRQF_SHARED). So this patch tracks the INTX enabling status in a new
intx_soft_enabled variable and toggle it during in
vp_disable/enable_vectors(). The INTX interrupt handler will check
intx_soft_enabled before processing the actual interrupt.

Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/r/20211019070152.8236-6-jasowang@redhat.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Jason Wang and committed by
Michael S. Tsirkin
080cd7c3 9e35276a

+22 -2
+21 -2
drivers/virtio/virtio_pci_common.c
··· 30 30 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 31 31 int i; 32 32 33 - if (vp_dev->intx_enabled) 33 + if (vp_dev->intx_enabled) { 34 + /* 35 + * The below synchronize() guarantees that any 36 + * interrupt for this line arriving after 37 + * synchronize_irq() has completed is guaranteed to see 38 + * intx_soft_enabled == false. 39 + */ 40 + WRITE_ONCE(vp_dev->intx_soft_enabled, false); 34 41 synchronize_irq(vp_dev->pci_dev->irq); 42 + } 35 43 36 44 for (i = 0; i < vp_dev->msix_vectors; ++i) 37 45 disable_irq(pci_irq_vector(vp_dev->pci_dev, i)); ··· 51 43 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 52 44 int i; 53 45 54 - if (vp_dev->intx_enabled) 46 + if (vp_dev->intx_enabled) { 47 + disable_irq(vp_dev->pci_dev->irq); 48 + /* 49 + * The above disable_irq() provides TSO ordering and 50 + * as such promotes the below store to store-release. 51 + */ 52 + WRITE_ONCE(vp_dev->intx_soft_enabled, true); 53 + enable_irq(vp_dev->pci_dev->irq); 55 54 return; 55 + } 56 56 57 57 for (i = 0; i < vp_dev->msix_vectors; ++i) 58 58 enable_irq(pci_irq_vector(vp_dev->pci_dev, i)); ··· 112 96 { 113 97 struct virtio_pci_device *vp_dev = opaque; 114 98 u8 isr; 99 + 100 + if (!READ_ONCE(vp_dev->intx_soft_enabled)) 101 + return IRQ_NONE; 115 102 116 103 /* reading the ISR has the effect of also clearing it so it's very 117 104 * important to save off the value. */
+1
drivers/virtio/virtio_pci_common.h
··· 63 63 /* MSI-X support */ 64 64 int msix_enabled; 65 65 int intx_enabled; 66 + bool intx_soft_enabled; 66 67 cpumask_var_t *msix_affinity_masks; 67 68 /* Name strings for interrupts. This size should be enough, 68 69 * and I'm too lazy to allocate each name separately. */