Skip to contentSkip to conversation

genirq, PCI/MSI: mask without flushing

PATCH In review Keith Busch 4 participants 10 messages Nov 6 – Nov 10

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover genirq, PCI/MSI: mask without flushing

From: Keith Busch <[email protected]>

This adds a new irq api, disable_irq_noflush(), backed by an optional irq_chip irq_mask_noflush() callback, and implemented for PCI MSI-X. It writes the mask without reading back, and is for callers where an extra interrupt after returning is harmless compared to the added latency of flushing the masking write to the device.

There are no users in this series; the hybrid irq/polling work from nvme is expected to use it when switching between MSI-X interrupts and polling, so these are posted separately as prep patches that are hopefully generally useful.

RFC with the nvme usage:
https://lore.kernel.org/linux-nvme/[email protected]/

Changes since v2:
- Just the commit log to use the preferred "MSI-X"
Changes since v1:
- Keep the channel offline check guarding the flush read in pci_msix_mask() (Sashiko)

v1: https://lore.kernel.org/all/[email protected]/

Keith Busch (2):
genirq: Add disable_irq_noflush()
PCI/MSI: Implement irq_mask_noflush() for MSI-X

 drivers/irqchip/irq-msi-lib.c |  1 +
 drivers/pci/msi/irqdomain.c   |  6 ++++++
 drivers/pci/msi/msi.h         | 10 ++++++++--
 include/linux/interrupt.h     |  1 +
 include/linux/irq.h           |  3 +++
 kernel/irq/chip.c             | 31 +++++++++++++++++++++++++++++++
 kernel/irq/internals.h        |  1 +
 kernel/irq/manage.c           | 21 +++++++++++++++++++++
 8 files changed, 72 insertions(+), 2 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v3 1/2 genirq: Add disable_irq_noflush()

From: Keith Busch <[email protected]>

Drivers that switch between interrupt and polled completions don't need the mask write flushed to the device; the disabled state already keeps their handler from running and a late interrupt is replayed on enable. Add an optional irq_chip::irq_mask_noflush() callback, falling back to irq_mask(), and disable_irq_noflush() to mask through it right away rather than lazily.

---
 include/linux/interrupt.h |  1 +
 include/linux/irq.h       |  3 +++
 kernel/irq/chip.c         | 31 +++++++++++++++++++++++++++++++
 kernel/irq/internals.h    |  1 +
 kernel/irq/manage.c       | 21 +++++++++++++++++++++
 5 files changed, 57 insertions(+)
include/linux/interrupt.h +1 -0
index ee2dd5349697d..6cfa1f8f3d400 100644
@@ -233,6 +233,7 @@ extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
 
 bool irq_has_action(unsigned int irq);
 extern void disable_irq_nosync(unsigned int irq);
+extern void disable_irq_noflush(unsigned int irq);
 extern bool disable_hardirq(unsigned int irq);
 extern void disable_irq(unsigned int irq);
 extern void disable_percpu_irq(unsigned int irq);
include/linux/irq.h +3 -0
index f485369b1b4f7..3a428dad6666e 100644
@@ -455,6 +455,8 @@ static inline irq_hw_number_t irqd_to_hwirq(struct irq_data *d)
  * @irq_disable:	disable the interrupt
  * @irq_ack:		start of a new interrupt
  * @irq_mask:		mask an interrupt source
+ * @irq_mask_noflush:	mask an interrupt source without flushing the write
+ *			to the hardware (optional, falls back to @irq_mask)
  * @irq_mask_ack:	ack and mask an interrupt source
  * @irq_unmask:		unmask an interrupt source
  * @irq_eoi:		end of interrupt
@@ -505,6 +507,7 @@ struct irq_chip {
 
 	void		(*irq_ack)(struct irq_data *data);
 	void		(*irq_mask)(struct irq_data *data);
+	void		(*irq_mask_noflush)(struct irq_data *data);
 	void		(*irq_mask_ack)(struct irq_data *data);
 	void		(*irq_unmask)(struct irq_data *data);
 	void		(*irq_eoi)(struct irq_data *data);
kernel/irq/chip.c +31 -0
index de754db414d1d..28285d7274e69 100644
@@ -399,6 +399,37 @@ void irq_disable(struct irq_desc *desc)
 	__irq_disable(desc, irq_settings_disable_unlazy(desc));
 }
 
+static void mask_irq_noflush(struct irq_desc *desc)
+{
+	if (irqd_irq_masked(&desc->irq_data))
+		return;
+
+	if (desc->irq_data.chip->irq_mask_noflush) {
+		desc->irq_data.chip->irq_mask_noflush(&desc->irq_data);
+		irq_state_set_masked(desc);
+	} else {
+		mask_irq(desc);
+	}
+}
+
+/**
+ * irq_disable_noflush - Mark interrupt disabled and mask it without flushing
+ * @desc:	irq descriptor which should be disabled
+ *
+ * Like irq_disable() with lazy disable turned off, but the hardware may
+ * still deliver an interrupt after this returns. The flow handler deals
+ * with that the same way it does for a lazily disabled interrupt.
+ */
+void irq_disable_noflush(struct irq_desc *desc)
+{
+	if (desc->irq_data.chip->irq_disable) {
+		__irq_disable(desc, true);
+		return;
+	}
+	irq_state_set_disabled(desc);
+	mask_irq_noflush(desc);
+}
+
 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
 {
 	if (desc->irq_data.chip->irq_enable)
kernel/irq/internals.h +1 -0
index 703a062be8783..7995970babb77 100644
@@ -101,6 +101,7 @@ extern void irq_startup_managed(struct irq_desc *desc);
 extern void irq_shutdown(struct irq_desc *desc);
 extern void irq_shutdown_and_deactivate(struct irq_desc *desc);
 extern void irq_disable(struct irq_desc *desc);
+extern void irq_disable_noflush(struct irq_desc *desc);
 extern void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu);
 extern void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu);
 extern void mask_irq(struct irq_desc *desc);
kernel/irq/manage.c +21 -0
index ec3ecb0e48cec..152dabea3102b 100644
@@ -706,6 +706,27 @@ void disable_irq_nosync(unsigned int irq)
 }
 EXPORT_SYMBOL(disable_irq_nosync);
 
+/**
+ * disable_irq_noflush - mask an irq without flushing the mask to the hardware
+ * @irq: Interrupt to disable
+ *
+ * Like disable_irq_nosync(), but masks the interrupt at the chip right away
+ * instead of lazily, and does not flush the mask write to the hardware. An
+ * interrupt that still arrives is held pending and replayed by enable_irq().
+ * Use this when an extra interrupt is cheaper than flushing the mask, e.g.
+ * switching from interrupts to polling.
+ *
+ * This function may be called from IRQ context.
+ */
+void disable_irq_noflush(unsigned int irq)
+{
+	scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) {
+		if (!scoped_irqdesc->depth++)
+			irq_disable_noflush(scoped_irqdesc);
+	}
+}
+EXPORT_SYMBOL_GPL(disable_irq_noflush);
+
 /**
  * disable_irq - disable an irq and wait for completion
  * @irq: Interrupt to disable
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v3 1/2] genirq: Add disable_irq_noflush()

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

From: Keith Busch <[email protected]>

Drivers that switch between interrupt and polled completions don't need the mask write flushed to the device; the disabled state already keeps their handler from running and a late interrupt is replayed on enable. Add an optional irq_chip::irq_mask_noflush() callback, falling back to irq_mask(), and disable_irq_noflush() to mask through it right away rather than lazily.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v3 2/2 PCI/MSI: Implement irq_mask_noflush() for MSI-X

In reply to Keith Busch

From: Keith Busch <[email protected]>

Skip the read-back that flushes the MSI-X mask write. Parent domains that mask on behalf of PCI/MSI keep using their own irq_mask().

---
 drivers/irqchip/irq-msi-lib.c |  1 +
 drivers/pci/msi/irqdomain.c   |  6 ++++++
 drivers/pci/msi/msi.h         | 10 ++++++++--
 3 files changed, 15 insertions(+), 2 deletions(-)
drivers/irqchip/irq-msi-lib.c +1 -0
index 45e0ed3134ce1..97bc95d592122 100644
@@ -126,6 +126,7 @@ bool msi_lib_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 	 */
 	if (info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT) {
 		chip->irq_mask		= irq_chip_mask_parent;
+		chip->irq_mask_noflush	= NULL;
 		chip->irq_unmask	= irq_chip_unmask_parent;
 	}
 
drivers/pci/msi/irqdomain.c +6 -0
index 6e65f0f44112e..2b9a5a0c305fb 100644
@@ -162,6 +162,11 @@ static void pci_irq_mask_msix(struct irq_data *data)
 	pci_msix_mask(irq_data_get_msi_desc(data));
 }
 
+static void pci_irq_mask_noflush_msix(struct irq_data *data)
+{
+	pci_msix_mask_noflush(irq_data_get_msi_desc(data));
+}
+
 static void pci_irq_unmask_msix(struct irq_data *data)
 {
 	pci_msix_unmask(irq_data_get_msi_desc(data));
@@ -182,6 +187,7 @@ static const struct msi_domain_template pci_msix_template = {
 		.irq_startup		= pci_irq_startup_msix,
 		.irq_shutdown		= pci_irq_shutdown_msix,
 		.irq_mask		= pci_irq_mask_msix,
+		.irq_mask_noflush	= pci_irq_mask_noflush_msix,
 		.irq_unmask		= pci_irq_unmask_msix,
 		.irq_write_msi_msg	= pci_msi_domain_write_msg,
 		.flags			= IRQCHIP_ONESHOT_SAFE,
drivers/pci/msi/msi.h +8 -2
index f987cf897264e..c40d24e5ca901 100644
@@ -44,14 +44,20 @@ static inline void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl)
 		writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
 }
 
-static inline void pci_msix_mask(struct msi_desc *desc)
+/* Posted write only: the device may still send an interrupt after return */
+static inline void pci_msix_mask_noflush(struct msi_desc *desc)
 {
 	desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
+	pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
+}
+
+static inline void pci_msix_mask(struct msi_desc *desc)
+{
+	pci_msix_mask_noflush(desc);
 
 	if (pci_channel_offline(msi_desc_to_pci_dev(desc)))
 		return;
 
-	pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
 	/* Flush write to device */
 	readl(desc->pci.mask_base);
 }
Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH v3 2/2] PCI/MSI: Implement irq_mask_noflush() for MSI-X

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

From: Keith Busch <[email protected]>

Skip the read-back that flushes the MSI-X mask write. Parent domains that mask on behalf of PCI/MSI keep using their own irq_mask().

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw

Re: [PATCH v3 2/2] PCI/MSI: Implement irq_mask_noflush() for MSI-X

In reply to Keith Busch

drivers/pci/msi/msi.h static inline void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl) · line 56

Quoted 11 lines · Keith Busch
drivers/pci/msi/msi.h +8 -1
-static inline void pci_msix_mask(struct msi_desc *desc)
+/* Posted write only: the device may still send an interrupt after return */
+static inline void pci_msix_mask_noflush(struct msi_desc *desc)
 {
 	desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
+	pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
+}
+
+static inline void pci_msix_mask(struct msi_desc *desc)
+{
+	pci_msix_mask_noflush(desc);

That's before the offline check now, which looks more than wrong.

Quoted 7 lines
drivers/pci/msi/msi.h +0 -1
 	if (pci_channel_offline(msi_desc_to_pci_dev(desc)))
 		return;
 
-	pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
 	/* Flush write to device */
 	readl(desc->pci.mask_base);
 }
maintainer
#6 lore raw

Re: [PATCH v3 2/2] PCI/MSI: Implement irq_mask_noflush() for MSI-X

Quoted 14 lines · Thomas Gleixner

drivers/pci/msi/msi.h static inline void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl) · line 56

Quoted 11 lines · Keith Busch
drivers/pci/msi/msi.h +8 -1
-static inline void pci_msix_mask(struct msi_desc *desc)
+/* Posted write only: the device may still send an interrupt after return */
+static inline void pci_msix_mask_noflush(struct msi_desc *desc)
 {
 	desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
+	pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
+}
+
+static inline void pci_msix_mask(struct msi_desc *desc)
+{
+	pci_msix_mask_noflush(desc);

That's before the offline check now, which looks more than wrong.

This is done on purpose: pci_msix_write_vector_ctrl() already has the same offline check, so nothing actually gets written, same as before.

And regardless of whether it's offline or not, we have to set the PCI_MSIX_ENTRY_CTRL_MASKBIT in the cached msxi_ctrl. That's common to both paths, so it lives in the noflush helper to avoid duplicating it.

maintainer
#7 lore raw

Re: [PATCH v3 2/2] PCI/MSI: Implement irq_mask_noflush() for MSI-X

Quoted 16 lines · Keith Busch
Quoted 14 lines · Thomas Gleixner

drivers/pci/msi/msi.h static inline void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl) · line 56

Quoted 11 lines · Keith Busch
drivers/pci/msi/msi.h +8 -1
-static inline void pci_msix_mask(struct msi_desc *desc)
+/* Posted write only: the device may still send an interrupt after return */
+static inline void pci_msix_mask_noflush(struct msi_desc *desc)
 {
 	desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
+	pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
+}
+
+static inline void pci_msix_mask(struct msi_desc *desc)
+{
+	pci_msix_mask_noflush(desc);

That's before the offline check now, which looks more than wrong.

Quoted 6 lines

This is done on purpose: pci_msix_write_vector_ctrl() already has the same offline check, so nothing actually gets written, same as before.

And regardless of whether it's offline or not, we have to set the PCI_MSIX_ENTRY_CTRL_MASKBIT in the cached msxi_ctrl. That's common to both paths, so it lives in the noflush helper to avoid duplicating it.

Indeed, but that's an unreadable mess TBH.

Can we please change that to have two functions here?

pci_msix_write_vector_ctrl()
plus
pci_msix_write_vector_ctrl_and_flush()

and fixup the related call sites?

Thanks,

tglx

maintainer
#8 lore raw

Re: [PATCH v3 2/2] PCI/MSI: Implement irq_mask_noflush() for MSI-X

Quoted 9 lines · Thomas Gleixner
Quoted 6 lines · Keith Busch

This is done on purpose: pci_msix_write_vector_ctrl() already has the same offline check, so nothing actually gets written, same as before.

And regardless of whether it's offline or not, we have to set the PCI_MSIX_ENTRY_CTRL_MASKBIT in the cached msxi_ctrl. That's common to both paths, so it lives in the noflush helper to avoid duplicating it.

Indeed, but that's an unreadable mess TBH.

No disagreement there! I was just going for a minimal diff, but we can refactor a bit on the way. I'll take a stab at it.

I also don't really get the rationale for putting the check in write_vector. The commit message says the checks are to avoid having to deal with errors, but posted writes don't produce errors, so not sure what it's actually guarding against.

Quoted 7 lines

Can we please change that to have two functions here?

pci_msix_write_vector_ctrl()
plus
pci_msix_write_vector_ctrl_and_flush()

and fixup the related call sites?

maintainer
#9 lore raw

Re: [PATCH v3 2/2] PCI/MSI: Implement irq_mask_noflush() for MSI-X

Quoted 5 lines · Keith Busch
Quoted 4 lines · Thomas Gleixner
Quoted 3 lines · Keith Busch

This is done on purpose: pci_msix_write_vector_ctrl() already has the same offline check, so nothing actually gets written, same as before.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

And regardless of whether it's offline or not, we have to set the PCI_MSIX_ENTRY_CTRL_MASKBIT in the cached msxi_ctrl. That's common to both paths, so it lives in the noflush helper to avoid duplicating it.

Indeed, but that's an unreadable mess TBH.

No disagreement there! I was just going for a minimal diff, but we can refactor a bit on the way. I'll take a stab at it.

Yes, please. The minimal diff approch is what creates the most problems down the road.

Quoted 4 lines

I also don't really get the rationale for putting the check in write_vector. The commit message says the checks are to avoid having to deal with errors, but posted writes don't produce errors, so not sure what it's actually guarding against.

To me the explaination made sense and there were no objections within month from any of the people involved. I'm not a DPC (or whatever is involved) expert. I just work here.

I've Cc'ed the author of the patch for you as that might be a better way to answer your question, no?

Thanks,

tglx