[PATCH 6/6] genirq: soft_moderation: implement per-driver defaults (nvme and vfio)

Luigi Rizzo posted 6 patches 2 months, 4 weeks ago
There is a newer version of this series
[PATCH 6/6] genirq: soft_moderation: implement per-driver defaults (nvme and vfio)
Posted by Luigi Rizzo 2 months, 4 weeks ago
Introduce helpers to implement per-driver module parameters to enable
moderation at boot/probe time.

As an example, use the helpers in nvme and vfio drivers.

To test, boot a kernel with

${driver}.soft_moderation=1 # enables moderation.

and verify with "cat /proc/irq/soft_moderation" that
the counters increase.

Change-Id: Iaad4110977deb96df845501895e0043bd93fc350
---
 drivers/nvme/host/pci.c           |  3 +++
 drivers/vfio/pci/vfio_pci_intrs.c |  3 +++
 include/linux/interrupt.h         | 13 +++++++++++++
 kernel/irq/irq_moderation.c       | 11 +++++++++++
 4 files changed, 30 insertions(+)

diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index 72fb675a696f4..b9d7bce30061f 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -72,6 +72,8 @@
 static_assert(MAX_PRP_RANGE / NVME_CTRL_PAGE_SIZE <=
 	(1 /* prp1 */ + NVME_MAX_NR_DESCRIPTORS * PRPS_PER_PAGE));
 
+DEFINE_IRQ_MODERATION_MODE_PARAMETER;
+
 static int use_threaded_interrupts;
 module_param(use_threaded_interrupts, int, 0444);
 
@@ -1989,6 +1991,7 @@ static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
 		result = queue_request_irq(nvmeq);
 		if (result < 0)
 			goto release_sq;
+		IRQ_MODERATION_SET_DEFAULT_MODE(pci_irq_vector(to_pci_dev(dev->dev), vector));
 	}
 
 	set_bit(NVMEQ_ENABLED, &nvmeq->flags);
diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c
index 30d3e921cb0de..e54d88cfe601d 100644
--- a/drivers/vfio/pci/vfio_pci_intrs.c
+++ b/drivers/vfio/pci/vfio_pci_intrs.c
@@ -22,6 +22,8 @@
 
 #include "vfio_pci_priv.h"
 
+DEFINE_IRQ_MODERATION_MODE_PARAMETER;
+
 struct vfio_pci_irq_ctx {
 	struct vfio_pci_core_device	*vdev;
 	struct eventfd_ctx		*trigger;
@@ -317,6 +319,7 @@ static int vfio_intx_enable(struct vfio_pci_core_device *vdev,
 		vfio_irq_ctx_free(vdev, ctx, 0);
 		return ret;
 	}
+	IRQ_MODERATION_SET_DEFAULT_MODE(pdev->irq);
 
 	return 0;
 }
diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
index 007201c8db6dd..c7d68d8ec49d7 100644
--- a/include/linux/interrupt.h
+++ b/include/linux/interrupt.h
@@ -879,12 +879,25 @@ void irq_moderation_init_fields(struct irq_desc *desc);
 /* add/remove /proc/irq/NN/soft_moderation */
 void irq_moderation_procfs_entry(struct irq_desc *desc, umode_t umode);
 
+/* helpers for per-driver moderation mode settings */
+#define DEFINE_IRQ_MODERATION_MODE_PARAMETER		\
+	static bool soft_moderation;			\
+	module_param(soft_moderation, bool, 0644);	\
+	MODULE_PARM_DESC(soft_moderation, "0: off, 1: disable_irq")
+
+void irq_moderation_set_mode(int irq, bool mode);
+#define IRQ_MODERATION_SET_DEFAULT_MODE(_irq)		\
+	irq_moderation_set_mode(_irq, READ_ONCE(soft_moderation))
+
 #else   /* empty stubs to avoid conditional compilation */
 
 static inline void irq_moderation_percpu_init(void) {}
 static inline void irq_moderation_init_fields(struct irq_desc *desc) {}
 static inline void irq_moderation_procfs_entry(struct irq_desc *desc, umode_t umode) {};
 
+#define DEFINE_IRQ_MODERATION_MODE_PARAMETER
+#define IRQ_MODERATION_SET_DEFAULT_MODE(_irq)
+
 #endif
 
 /*
diff --git a/kernel/irq/irq_moderation.c b/kernel/irq/irq_moderation.c
index 672e161ecf29e..3b3962dae33d1 100644
--- a/kernel/irq/irq_moderation.c
+++ b/kernel/irq/irq_moderation.c
@@ -83,6 +83,10 @@ static bool enable_posted_msi;
  *
  *    echo "on" > /proc/irq/NN/soft_moderation # use "off" to disable
  *
+ * For selected drivers, the default can also be supplied via module parameters
+ *
+ *	${DRIVER}.soft_moderation=1
+ *
  * === MONITORING ===
  *
  * cat /proc/irq/soft_moderation shows per-CPU and global statistics.
@@ -302,6 +306,13 @@ static void set_moderation_mode(struct irq_desc *desc, bool mode)
 	}
 }
 
+/* irq_to_desc is not exported. Wrap it in this function for a specific use. */
+void irq_moderation_set_mode(int irq, bool mode)
+{
+	set_moderation_mode(irq_to_desc(irq), mode);
+}
+EXPORT_SYMBOL(irq_moderation_set_mode);
+
 #pragma clang diagnostic error "-Wformat"
 /* Print statistics */
 static int moderation_show(struct seq_file *p, void *v)
-- 
2.51.2.1041.gc1ab5b90ca-goog
Re: [PATCH 6/6] genirq: soft_moderation: implement per-driver defaults (nvme and vfio)
Posted by Thomas Gleixner 2 months, 3 weeks ago
On Wed, Nov 12 2025 at 19:24, Luigi Rizzo wrote:
> Introduce helpers to implement per-driver module parameters to enable
> moderation at boot/probe time.
>
> As an example, use the helpers in nvme and vfio drivers.
>
> To test, boot a kernel with
>
> ${driver}.soft_moderation=1 # enables moderation.
>
> and verify with "cat /proc/irq/soft_moderation" that
> the counters increase.
>
> Change-Id: Iaad4110977deb96df845501895e0043bd93fc350
> ---
>  drivers/nvme/host/pci.c           |  3 +++
>  drivers/vfio/pci/vfio_pci_intrs.c |  3 +++
>  include/linux/interrupt.h         | 13 +++++++++++++
>  kernel/irq/irq_moderation.c       | 11 +++++++++++

Again a pile of randomly picked files. The proper way is documented:

   1) Add infrastructure first

   2) Use infrastructure in drivers, one patch per subsystem

> diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
> index 72fb675a696f4..b9d7bce30061f 100644
> --- a/drivers/nvme/host/pci.c
> +++ b/drivers/nvme/host/pci.c
> @@ -72,6 +72,8 @@
>  static_assert(MAX_PRP_RANGE / NVME_CTRL_PAGE_SIZE <=
>  	(1 /* prp1 */ + NVME_MAX_NR_DESCRIPTORS * PRPS_PER_PAGE));
>  
> +DEFINE_IRQ_MODERATION_MODE_PARAMETER;
> +
>  static int use_threaded_interrupts;
>  module_param(use_threaded_interrupts, int, 0444);
>  
> @@ -1989,6 +1991,7 @@ static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
>  		result = queue_request_irq(nvmeq);
>  		if (result < 0)
>  			goto release_sq;
> +		IRQ_MODERATION_SET_DEFAULT_MODE(pci_irq_vector(to_pci_dev(dev->dev), vector));

What's the point of this IRQ_MODERATION_SET_DEFAULT_MODE() wrapper?

Thanks,

        tglx
Re: [PATCH 6/6] genirq: soft_moderation: implement per-driver defaults (nvme and vfio)
Posted by Luigi Rizzo 2 months, 3 weeks ago
On Thu, Nov 13, 2025 at 11:18 AM Thomas Gleixner <tglx@linutronix.de> wrote:
>
> On Wed, Nov 12 2025 at 19:24, Luigi Rizzo wrote:
...
> > --- a/drivers/nvme/host/pci.c
> > +++ b/drivers/nvme/host/pci.c
> > @@ -72,6 +72,8 @@
> >  static_assert(MAX_PRP_RANGE / NVME_CTRL_PAGE_SIZE <=
> >       (1 /* prp1 */ + NVME_MAX_NR_DESCRIPTORS * PRPS_PER_PAGE));
> >
> > +DEFINE_IRQ_MODERATION_MODE_PARAMETER;
> > +
> >  static int use_threaded_interrupts;
> >  module_param(use_threaded_interrupts, int, 0444);
> >
> > @@ -1989,6 +1991,7 @@ static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
> >               result = queue_request_irq(nvmeq);
> >               if (result < 0)
> >                       goto release_sq;
> > +             IRQ_MODERATION_SET_DEFAULT_MODE(pci_irq_vector(to_pci_dev(dev->dev), vector));
>
> What's the point of this IRQ_MODERATION_SET_DEFAULT_MODE() wrapper?

I wanted to avoid exposing the module parameter name,
so that adding support in a driver is mechanical and we can
change the name in one place for all (during this review or later).

Ideally I would have preferred some generic parameter
irq_moderation.enable_on="nvme vfio ..."
to set defaults without having to patch individual drivers.
But I have not found a way to go from the irq_desc to the driver's name
(desc->name is often renamed especially for NICs)

cheers
luigi