Skip to content

Commit 77e89af

Browse files
committed
PCI/MSI: Protect msi_desc::masked for multi-MSI
Multi-MSI uses a single MSI descriptor and there is a single mask register when the device supports per vector masking. To avoid reading back the mask register the value is cached in the MSI descriptor and updates are done by clearing and setting bits in the cache and writing it to the device. But nothing protects msi_desc::masked and the mask register from being modified concurrently on two different CPUs for two different Linux interrupts which belong to the same multi-MSI descriptor. Add a lock to struct device and protect any operation on the mask and the mask register with it. This makes the update of msi_desc::masked unconditional, but there is no place which requires a modification of the hardware register without updating the masked cache. msi_mask_irq() is now an empty wrapper which will be cleaned up in follow up changes. The problem goes way back to the initial support of multi-MSI, but picking the commit which introduced the mask cache is a valid cut off point (2.6.30). Fixes: f2440d9 ("PCI MSI: Refactor interrupt masking code") Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Marc Zyngier <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent d28d4ad commit 77e89af

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

drivers/base/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,7 @@ void device_initialize(struct device *dev)
28372837
device_pm_init(dev);
28382838
set_dev_node(dev, -1);
28392839
#ifdef CONFIG_GENERIC_MSI_IRQ
2840+
raw_spin_lock_init(&dev->msi_lock);
28402841
INIT_LIST_HEAD(&dev->msi_list);
28412842
#endif
28422843
INIT_LIST_HEAD(&dev->links.consumers);

drivers/pci/msi.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,25 @@ static inline __attribute_const__ u32 msi_mask(unsigned x)
143143
* reliably as devices without an INTx disable bit will then generate a
144144
* level IRQ which will never be cleared.
145145
*/
146-
u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
146+
void __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
147147
{
148-
u32 mask_bits = desc->masked;
148+
raw_spinlock_t *lock = &desc->dev->msi_lock;
149+
unsigned long flags;
149150

150151
if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
151-
return 0;
152+
return;
152153

153-
mask_bits &= ~mask;
154-
mask_bits |= flag;
154+
raw_spin_lock_irqsave(lock, flags);
155+
desc->masked &= ~mask;
156+
desc->masked |= flag;
155157
pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
156-
mask_bits);
157-
158-
return mask_bits;
158+
desc->masked);
159+
raw_spin_unlock_irqrestore(lock, flags);
159160
}
160161

161162
static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
162163
{
163-
desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag);
164+
__pci_msi_desc_mask_irq(desc, mask, flag);
164165
}
165166

166167
static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)

include/linux/device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ struct device {
506506
struct dev_pin_info *pins;
507507
#endif
508508
#ifdef CONFIG_GENERIC_MSI_IRQ
509+
raw_spinlock_t msi_lock;
509510
struct list_head msi_list;
510511
#endif
511512
#ifdef CONFIG_DMA_OPS

include/linux/msi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
233233
void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
234234

235235
u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag);
236-
u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag);
236+
void __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag);
237237
void pci_msi_mask_irq(struct irq_data *data);
238238
void pci_msi_unmask_irq(struct irq_data *data);
239239

0 commit comments

Comments
 (0)