nvme-pci: dynamic interrupt moderation
Subsystems (6)
cover nvme-pci: dynamic interrupt moderation
From: Keith Busch <[email protected]>
Just adding another nvme hybrid interrupt and polling mechanism to the lists. There's some recent alternatives mentioned in the links below, but I'm trying one last time to leverage the DIM framework. Performance wise, this at least doesn't seem any worse than anything else proposed, though I've only been able to test on mid-level hardware so far.
This one here as some irq prep patches that help some when transitioning from irq to polling for MSI-x enabled NVMe completions. These should be generally applicable, so isn't necessarily tied to how it gets used in the last patch if someone else finds it useful too.
Links:
[1] https://lore.kernel.org/linux-nvme/[email protected]/
[2] https://lore.kernel.org/linux-nvme/[email protected]/
Keith Busch (3):
genirq: Add disable_irq_nowait()
PCI/MSI: Implement irq_mask_nowait() for MSI-X
nvme-pci: hybrid irq+polling
drivers/irqchip/irq-msi-lib.c | 1 + drivers/nvme/host/Kconfig | 2 + drivers/nvme/host/pci.c | 624 ++++++++++++++++++++++++++++++++-- drivers/nvme/host/trace.c | 1 + drivers/nvme/host/trace.h | 26 ++ drivers/pci/msi/irqdomain.c | 6 + drivers/pci/msi/msi.h | 8 +- include/linux/interrupt.h | 1 + include/linux/irq.h | 4 + kernel/irq/chip.c | 31 ++ kernel/irq/internals.h | 1 + kernel/irq/manage.c | 21 ++ 12 files changed, 698 insertions(+), 28 deletions(-)
Signature · 1 line
2.52.0
RFC PATCH 1/3 genirq: Add disable_irq_nowait()
From: Keith Busch <[email protected]>
Drivers that switch between interrupt and polled completions don't need to wait for the mask to reach 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_nowait() callback, falling back to irq_mask(), and disable_irq_nowait() to mask through it right away rather than lazily.
--- include/linux/interrupt.h | 1 + include/linux/irq.h | 4 ++++ kernel/irq/chip.c | 31 +++++++++++++++++++++++++++++++ kernel/irq/internals.h | 1 + kernel/irq/manage.c | 21 +++++++++++++++++++++ 5 files changed, 58 insertions(+)
include/linux/interrupt.h +1 -0
@@ -231,6 +231,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_nowait (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 +4 -0
@@ -455,6 +455,9 @@ 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_nowait: mask an interrupt source without waiting for the +* hardware to acknowledge it (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 +508,7 @@ struct irq_chip {void (*irq_ack)(struct irq_data *data);void (*irq_mask)(struct irq_data *data); +void (*irq_mask_nowait)(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
@@ -399,6 +399,37 @@ void irq_disable(struct irq_desc *desc)__irq_disable (desc,irq_settings_disable_unlazy (desc)); } +static void mask_irq_nowait (struct irq_desc *desc) +{ +if (irqd_irq_masked (&desc->irq_data)) +return ; + +if (desc->irq_data.chip->irq_mask_nowait) { + desc->irq_data.chip->irq_mask_nowait (&desc->irq_data); +irq_state_set_masked (desc); + }else { +mask_irq (desc); + } +} + +/** +* irq_disable_nowait - Mark interrupt disabled and mask it without waiting +* @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_nowait (struct irq_desc *desc) +{ +if (desc->irq_data.chip->irq_disable) { +__irq_disable (desc, true); +return ; + } +irq_state_set_disabled (desc); +mask_irq_nowait (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
@@ -98,6 +98,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_nowait (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
@@ -705,6 +705,27 @@ void disable_irq_nosync(unsigned int irq) }EXPORT_SYMBOL (disable_irq_nosync); +/** +* disable_irq_nowait - mask an irq without waiting for 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 wait for the hardware to acknowledge the +* mask. 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_nowait (unsigned int irq) +{ + scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { +if (!scoped_irqdesc->depth++) +irq_disable_nowait (scoped_irqdesc); + } +} +EXPORT_SYMBOL_GPL (disable_irq_nowait); +/** * disable_irq - disable an irq and wait for completion * @irq: Interrupt to disable
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [RFC PATCH 1/3] genirq: Add disable_irq_nowait()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
From: Keith Busch <[email protected]>
Drivers that switch between interrupt and polled completions don't need to wait for the mask to reach the device; the disabled state already keeps their handler from running and a late interrupt is replayed on enable. Add an optional
irq_chipirq_mask_nowait()callback, falling back toirq_mask(), anddisable_irq_nowait()to mask through it right away rather than lazily.Signed-off-by Keith Busch <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 2/3 PCI/MSI: Implement irq_mask_nowait() for MSI-x
In reply to Keith BuschFrom: Keith Busch <[email protected]>
Skip the read-back that flushes the MSI-x mask write. Parent domains that mask on behalf of PCI/MSI continue using their own irq_mask().
--- drivers/irqchip/irq-msi-lib.c | 1 + drivers/pci/msi/irqdomain.c | 6 ++++++ drivers/pci/msi/msi.h | 8 +++++++- 3 files changed, 14 insertions(+), 1 deletion(-)
drivers/irqchip/irq-msi-lib.c +1 -0
@@ -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_nowait =NULL ; chip->irq_unmask = irq_chip_unmask_parent; }
drivers/pci/msi/irqdomain.c +6 -0
@@ -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_nowait_msix (struct irq_data *data) +{ +pci_msix_mask_nowait (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_nowait = pci_irq_mask_nowait_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 +7 -1
@@ -40,10 +40,16 @@ 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_nowait (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_nowait (desc);/* Flush write to device */ readl (desc->pci.mask_base); }
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [RFC PATCH 2/3] PCI/MSI: Implement irq_mask_nowait() for MSI-x
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 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 continue using their own
irq_mask().Signed-off-by Keith Busch <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [RFC PATCH 2/3] PCI/MSI: Implement irq_mask_nowait() for MSI-x
In reply to Keith Busch[+cc Luigi, cover letter at
https://lore.kernel.org/all/[email protected]]
Quoted 6 lines · 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 continue using their own
irq_mask().Signed-off-by Keith Busch <[email protected]>
I assume Thomas will review/ack the pci/msi/ parts.
Cc'd Luigi since I know he's interested in interrupt moderation in general.
Quoted 67 lines
--- drivers/irqchip/irq-msi-lib.c | 1 + drivers/pci/msi/irqdomain.c | 6 ++++++ drivers/pci/msi/msi.h | 8 +++++++- 3 files changed, 14 insertions(+), 1 deletion(-)drivers/irqchip/irq-msi-lib.c +1
-0@@ -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_nowait =NULL ; chip->irq_unmask = irq_chip_unmask_parent; }drivers/pci/msi/irqdomain.c +6
-0@@ -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_nowait_msix (struct irq_data *data) +{ +pci_msix_mask_nowait (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_nowait = pci_irq_mask_nowait_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 +7
-1@@ -40,10 +40,16 @@ 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_nowait (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_nowait (desc);/* Flush write to device */ readl (desc->pci.mask_base); }Signature · 1 line
2.52.0
Thomas Gleixner
maintainerRe: [RFC PATCH 2/3] PCI/MSI: Implement irq_mask_nowait() for MSI-x
Quoted 6 lines · Bjorn Helgaas
Quoted 4 lines
Skip the read-back that flushes the MSI-x mask write. Parent domains that mask on behalf of PCI/MSI continue using their own
irq_mask().Signed-off-by Keith Busch <[email protected]>I assume Thomas will review/ack the pci/msi/ parts.
Yes. I actually suggested that "nowait" mechanism to Keith at LPC.
Though I hate the name of that callback because irq_mask() does not really wait. It guarantees that the mask store has reached the device which means that after that point no further interrupts will be sent by the device (assuming sane hardware). That means it's a synchronous operation which involves flushing the store, so maybe irq_mask_noflush() or irq_mask_async() might be more accurate and descriptive.
Quoted 2 lines
Cc'd Luigi since I know he's interested in interrupt moderation in general.
Correct. I'm curious whether the interrupt frequency based moderation mechanism Luigi is proposing does solve the NVME issues as well or can be combined with some device specific hint mechanism.
That said, Luigi's approach should benefit from the "nowait" mechanism as well.
Luigi?
Thanks,
tglx
Keith Busch
maintainerRe: [RFC PATCH 2/3] PCI/MSI: Implement irq_mask_nowait() for MSI-x
Quoted 8 lines · Thomas Gleixner
Quoted 6 lines · Bjorn Helgaas
Quoted 4 lines
Skip the read-back that flushes the MSI-x mask write. Parent domains that mask on behalf of PCI/MSI continue using their own
irq_mask().Signed-off-by Keith Busch <[email protected]>I assume Thomas will review/ack the pci/msi/ parts.
Quoted 8 lines
Yes. I actually suggested that "nowait" mechanism to Keith at LPC.
Though I hate the name of that callback because
irq_mask()does not really wait. It guarantees that the mask store has reached the device which means that after that point no further interrupts will be sent by the device (assuming sane hardware). That means it's a synchronous operation which involves flushing the store, so maybeirq_mask_noflush()orirq_mask_async()might be more accurate and descriptive.
Right thank you, I misremembered your suggested name. I am totally fine with "noflush", and I've made that change in my local branch. I'll just wait a bit for any other feedback before the next version.
Quoted 15 lines
Quoted 2 lines
Cc'd Luigi since I know he's interested in interrupt moderation in general.
Correct. I'm curious whether the interrupt frequency based moderation mechanism Luigi is proposing does solve the NVME issues as well or can be combined with some device specific hint mechanism.
That said, Luigi's approach should benefit from the "nowait" mechanism as well.
Luigi?
Thanks,
tglx
Re: [RFC PATCH 2/3] PCI/MSI: Implement irq_mask_nowait() for MSI-x
In reply to Thomas GleixnerQuoted 10 lines · Thomas Gleixner
Quoted 6 lines · Bjorn Helgaas
Quoted 4 lines
Skip the read-back that flushes the MSI-x mask write. Parent domains that mask on behalf of PCI/MSI continue using their own
irq_mask().Signed-off-by Keith Busch <[email protected]>I assume Thomas will review/ack the pci/msi/ parts.
Yes. I actually suggested that "nowait" mechanism to Keith at LPC.
The topic also came up around last December. The readback wastes 1000-1500ns under load. I hacked a conditional in pci_msix_mask()
https://lore.kernel.org/all/[email protected]/
and you suggested a callback irq_mask_dev calling pci_irq_mask_msix_lazy()
https://lore.kernel.org/all/87jyxqiuta.ffs@tglx/
As usual naming is the hard part...
The mechanism is definitely useful (with moderation the mask is a frequent operation, so saving the extra 1-2us per call is measurable). I was a bit worried about adding yet another callback, but there are already 30 or so,
hence one or two more does not make much of a difference.
On passing, on ARM this will not help unless we partially revert the optimization
that skips PCIe if the interrupt has MSI_FLAG_PCI_MSI_MASK_PARENT
https://lore.kernel.org/all/[email protected]/
Quoted 7 lines
Though I hate the name of that callback because
irq_mask()does not really wait. It guarantees that the mask store has reached the device which means that after that point no further interrupts will be sent by the device (assuming sane hardware). That means it's a synchronous operation which involves flushing the store, so maybeirq_mask_noflush()orirq_mask_async()might be more accurate and descriptive.
Quoted 6 lines
Quoted 2 lines
Cc'd Luigi since I know he's interested in interrupt moderation in general.
Correct. I'm curious whether the interrupt frequency based moderation mechanism Luigi is proposing does solve the NVME issues as well or can be combined with some device specific hint mechanism.
I really think we need both. It depends on what the issue is:
- for hardware level problems (backpressure on PCIe from the interrupt dispatch pipeline)
we need to actually disable interrupts at the PCIe sources, and
address the problem at
a global SoC level, not on the single device (NICs, NVME, VFs used by VMs...). I honestly believe something global like GSIM is the best answer,
because the alternative
of having all drivers implement moderation and coordinate the delays
would be hard to manage.
- for pure CPU overload due to e.g. excessive interrupts from a single source, GSIM can help a bit (e.g. preventing livelock, or cutting some
10-20% interrupt overhead),
but driver specific solutions have a lot more visibility on what is happening
and may possibly do a better job. As an example,
Fengnan's solution not only moderates (or does timed polling, GSIM
has that too),
but can also dynamically switch completion processing between
hardirq and softirq.
https://lore.kernel.org/linux-nvme/[email protected]/
cheers
luigi
Quoted 9 lines
That said, Luigi's approach should benefit from the "nowait" mechanism as well.
Luigi?
Thanks,
tglx
RFC PATCH 3/3 nvme-pci: hybrid irq+polling
In reply to Keith BuschFrom: Keith Busch <[email protected]>
High depth workloads can trigger interrupt storms that would be better off polled. This proposal uses the DIM framework to adjust the completion method from interrupt driven to polling based on the workload.
--- drivers/nvme/host/Kconfig | 2 + drivers/nvme/host/pci.c | 624 ++++++++++++++++++++++++++++++++++++-- drivers/nvme/host/trace.c | 1 + drivers/nvme/host/trace.h | 26 ++ 4 files changed, 626 insertions(+), 27 deletions(-)
drivers/nvme/host/Kconfig +2 -0
@@ -6,6 +6,8 @@ config BLK_DEV_NVME tristate "NVM Express block device" depends on PCI && BLOCK select NVME_CORE + select DIMLIB + select IRQ_POLL help The NVM Express driver is for solid state drives directly connected to the PCI or PCI Express bus. If you know you
drivers/nvme/host/pci.c +597 -27
820 lines not on this page · raw message
drivers/nvme/host/trace.c +1 -0
@@ -498,3 +498,4 @@ const char *nvme_trace_disk_name(struct trace_seq *p, char *name) }EXPORT_TRACEPOINT_SYMBOL_GPL (nvme_sq); +EXPORT_TRACEPOINT_SYMBOL_GPL (nvme_dim);
drivers/nvme/host/trace.h +26 -0
@@ -161,6 +161,32 @@ TRACE_EVENT(nvme_sq, ) ); +TRACE_EVENT (nvme_dim, + TP_PROTO(u16 qid, int cpms, int epms, int cpe_ratio, u8 profile, + unsigned int sleep_us), +TP_ARGS (qid, cpms, epms, cpe_ratio, profile, sleep_us), +TP_STRUCT__entry ( + __field(u16, qid) + __field(int, cpms) + __field(int, epms) + __field(int, cpe_ratio) + __field(u8, profile) + __field(unsigned int, sleep_us) + ), +TP_fast_assign ( + __entry->qid = qid; + __entry->cpms = cpms; + __entry->epms = epms; + __entry->cpe_ratio = cpe_ratio; + __entry->profile = profile; + __entry->sleep_us = sleep_us; + ), +TP_printk ("qid=%u cpms=%d epms=%d cpe_ratio=%d profile=%u sleep_us=%u" , + __entry->qid, __entry->cpms, __entry->epms, + __entry->cpe_ratio, __entry->profile, __entry->sleep_us + ) +); +#endif /* _TRACE_NVME_H */ #undef TRACE_INCLUDE_PATH
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [RFC PATCH 3/3] nvme-pci: hybrid irq+polling
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kconfig: BLK_DEV_NVME selects DIMLIB without NET dependency --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kconfig: BLK_DEV_NVME selects DIMLIB without NET dependency
--
commit a8cd25c467c1c40e704c2bd340830c637e44f89b
Author: Keith Busch <[email protected]>
nvme-pci: hybrid irq+polling
This commit introduces hybrid irq+polling for nvme-pci using the DIM framework to adjust completion methods dynamically based on the workload.
drivers/nvme/host/Kconfig config BLK_DEV_NVME · line 9
Quoted 9 lines
drivers/nvme/host/Kconfig +1
-0@@ -6,6 +6,8 @@ config BLK_DEV_NVME tristate "NVM Express block device" depends on PCI && BLOCK select NVME_CORE + select DIMLIB
[Severity: Medium]
Since DIMLIB is defined in lib/Kconfig with a dependency on NET, does unconditionally selecting it here risk Kconfig unmet direct dependency warnings and potential build failures on configurations where CONFIG_NET is disabled?
Quoted 4 lines
drivers/nvme/host/Kconfig +1
-0+ select IRQ_POLL help The NVM Express driver is for solid state drives directly connected to the PCI or PCI Express bus. If you know you
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Hi Keith:
I ran some quick tests using your patch on the PS1010 and SN861. For 4K random reads with QD=8, both drives showed a performance drop of about 19%.
With QD=64/128, there was an improvement of roughly 10–20%.
I think the reason is that the current logic still relies on the QD depth to determine whether to switch, but a high QD doesn't necessarily mean that using the poll mode will be more efficient.
As for DIM, I tried using it based on my V2 patch, but the result was that I either had to add several new DIM functions to perform similar checks, or I still ended up with a performance regression.
Here is detail data
https://docs.google.com/spreadsheets/d/1VOR0xB5yk-z6IMwwRvp8R0yd4k_tMi_wI27Pl-dEQVg/edit?gid=789521759#gid=789521759
Based on the results, it doesn’t perform as well as my previous patch:
https://docs.google.com/spreadsheets/d/1rer4Nl5HHxSO-D3NrrHuHSfpR9s1kLhpLjzh7h7Mbmw/edit?gid=875909538#gid=875909538
Quoted 38 lines · Keith Busch
From: Keith Busch <[email protected]>
Just adding another nvme hybrid interrupt and polling mechanism to the lists. There's some recent alternatives mentioned in the links below, but I'm trying one last time to leverage the DIM framework. Performance wise, this at least doesn't seem any worse than anything else proposed, though I've only been able to test on mid-level hardware so far.
This one here as some irq prep patches that help some when transitioning from irq to polling for MSI-x enabled NVMe completions. These should be generally applicable, so isn't necessarily tied to how it gets used in the last patch if someone else finds it useful too.
Links:
[1] https://lore.kernel.org/linux-nvme/[email protected]/
[2] https://lore.kernel.org/linux-nvme/[email protected]/
Keith Busch (3):
genirq: Adddisable_irq_nowait()
PCI/MSI: Implementirq_mask_nowait()for MSI-X
nvme-pci: hybrid irq+pollingdrivers/irqchip/irq-msi-lib.c | 1 + drivers/nvme/host/Kconfig | 2 + drivers/nvme/host/pci.c | 624 ++++++++++++++++++++++++++++++++-- drivers/nvme/host/trace.c | 1 + drivers/nvme/host/trace.h | 26 ++ drivers/pci/msi/irqdomain.c | 6 + drivers/pci/msi/msi.h | 8 +- include/linux/interrupt.h | 1 + include/linux/irq.h | 4 + kernel/irq/chip.c | 31 ++ kernel/irq/internals.h | 1 + kernel/irq/manage.c | 21 ++ 12 files changed, 698 insertions(+), 28 deletions(-)
Hi Keith:
I made some modifications to DIM and then implemented a solution using the DIM framework.
Based on current testing, the results are now comparable to those of the previous patch.
Here are the preliminary test results:
https://docs.google.com/spreadsheets/d/1e8LQPnaDhmv50XlAM3XxI0TMQkhbk0bTZwlJP1Hzzdk/edit?gid=188992110#gid=188992110
If you agree with this approach, I’ll continue testing and optimizing.
Our design uses two DIM profiles: IRQ and polling at a fixed 10 µs interval, comparing the CQ’s own completion rate.
The patch is attached.
It's just a proof-of-concept to prove that the DIM framework can also achieve good results, most of the code was written by AI.
The current code logic is a bit complex and needs to be simplified a bit.
[1]
https://lore.kernel.org/linux-nvme/[email protected]/
Thanks.
Quoted 24 lines · Keith Busch
From: Keith Busch <[email protected]>
Just adding another nvme hybrid interrupt and polling mechanism to the lists. There's some recent alternatives mentioned in the links below, but I'm trying one last time to leverage the DIM framework. Performance wise, this at least doesn't seem any worse than anything else proposed, though I've only been able to test on mid-level hardware so far.
This one here as some irq prep patches that help some when transitioning from irq to polling for MSI-x enabled NVMe completions. These should be generally applicable, so isn't necessarily tied to how it gets used in the last patch if someone else finds it useful too.
Links:
[1] https://lore.kernel.org/linux-nvme/[email protected]/
[2] https://lore.kernel.org/linux-nvme/[email protected]/
Keith Busch (3):
genirq: Adddisable_irq_nowait()
PCI/MSI: Implementirq_mask_nowait()for MSI-X
nvme-pci: hybrid irq+polling
Quoted 14 lines
drivers/irqchip/irq-msi-lib.c | 1 + drivers/nvme/host/Kconfig | 2 + drivers/nvme/host/pci.c | 624 ++++++++++++++++++++++++++++++++-- drivers/nvme/host/trace.c | 1 + drivers/nvme/host/trace.h | 26 ++ drivers/pci/msi/irqdomain.c | 6 + drivers/pci/msi/msi.h | 8 +- include/linux/interrupt.h | 1 + include/linux/irq.h | 4 + kernel/irq/chip.c | 31 ++ kernel/irq/internals.h | 1 + kernel/irq/manage.c | 21 ++ 12 files changed, 698 insertions(+), 28 deletions(-)
From ab88b22a6f0ced47fa7ced19b24a846520e449ef Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Mon, 19 Oct 2026 11:03:45 +0000
Subject: [RFC PATCH 1/4] lib/dim: allow DIMLIB without CONFIG_NET
Only net_dim.c needs networking: it manages net_device moderation profiles. dim.c and rdma_dim.c are generic, yet DIMLIB as a whole depends on NET, so a block driver cannot select it on a NET=n kernel.
Drop the NET dependency from DIMLIB and build net_dim.o only when CONFIG_NET is enabled. All net_dim users already depend on NET.
--- lib/Kconfig | 1 - lib/dim/Makefile | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-)
lib/Kconfig +0 -1
@@ -475,7 +475,6 @@ config SIGNATURE config DIMLIB tristate - depends on NET help Dynamic Interrupt Moderation library. Implements an algorithm for dynamically changing CQ moderation values
lib/dim/Makefile +2 -1
@@ -4,4 +4,5 @@ obj-$(CONFIG_DIMLIB) += dimlib.o -dimlib-y := dim.o net_dim.o rdma_dim.o +dimlib-y := dim.o rdma_dim.o +dimlib-$(CONFIG_NET) += net_dim.o
Signature · 1 line
2.39.5
From af0e78ec947217a0995cddc42fce6526b015c159 Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Mon, 19 Oct 2026 11:03:46 +0000
Subject: [RFC PATCH 2/4] lib/dim: factor the tuning state machine out of
net_dim
net_dim_decision() walks a profile table: it steps towards the better profile, turns when a step does not help, parks on top, and parks tired after too many steps. Only the statistics comparison and the number of profiles are specific to networking.
Move the state machine into dim.c as dim_decide(), which takes the comparison as a callback and the number of profiles as an argument, so that other drivers can reuse the DIM search with their own measurements and profile tables. The tired limit becomes twice the number of profiles, which is unchanged for net_dim. net_dim() now calls dim_decide() with net_dim_stats_compare() and NET_DIM_PARAMS_NUM_PROFILES; no functional change.
--- include/linux/dim.h | 21 +++++++++++ lib/dim/dim.c | 88 +++++++++++++++++++++++++++++++++++++++++++ lib/dim/net_dim.c | 91 ++------------------------------------------- 3 files changed, 112 insertions(+), 88 deletions(-)
include/linux/dim.h +21 -0
@@ -341,6 +341,27 @@ void dim_park_on_top(struct dim *dim);*/ void dim_park_tired (struct dim *dim); +/** +* dim_decide - one tuning decision on a new statistics sample +* @dim: DIM context +* @curr_stats: statistics of the sample just measured +* @compare: callback returning %DIM_STATS_BETTER, +* %DIM_STATS_SAME or %DIM_STATS_WORSE, given @dim and both samples +* @nprofiles: number of profiles, from 1 to 127 +* +* Run the DIM tuning state machine used by net_dim: step towards the +* better profile, turn when a step does not help, park on top, and park +* tired after 2 * @nprofiles steps without settling. The caller serializes +* access to @dim and applies the new profile. profile_ix must be below +* @nprofiles; a tired parked context must have a nonzero tired count. +* +* Return: true if profile_ix changed. +*/ +bool dim_decide (struct dim *dim,struct dim_stats *curr_stats, +int (*compare)(struct dim *dim,struct dim_stats *curr, +struct dim_stats *prev), +u8 nprofiles); +/** * dim_calc_stats - calculate the difference between two samples * @start: start sample
lib/dim/dim.c +88 -0
94 lines not on this page · raw message
lib/dim/net_dim.c +3 -88
109 lines not on this page · raw message
Signature · 1 line
2.39.5
From db6ef734309963486a7d9640850bb71e4bd5168a Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Wed, 19 Aug 2026 15:31:27 +0800
Subject: [RFC PATCH 3/4] nvme-pci: return completion count from nvme_poll_cq
Return the number of completions consumed by nvme_poll_cq() instead of a boolean. Existing callers still treat zero vs non-zero as before; nvme_poll() keeps returning 0/1 via a bool local.
Extract nvme_irq_complete_batch() from nvme_irq() so the adaptive handler in the next patch can reuse the batch completion path.
--- drivers/nvme/host/pci.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/nvme/host/pci.c +15 -10
@@ -1606,13 +1606,12 @@ static inline void nvme_update_cq_head(struct nvme_queue *nvmeq) } } -static inline bool nvme_poll_cq (struct nvme_queue *nvmeq, -struct io_comp_batch *iob) +static inline unsigned int nvme_poll_cq (struct nvme_queue *nvmeq, +struct io_comp_batch *iob) { -bool found = false; +unsigned int found =0 ;while (nvme_cqe_pending (nvmeq)) { - found = true;/* * load-load control dependency between phase and the rest of * the cqe requires a full read memory barrier
@@ -1620,6 +1619,7 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,dma_rmb ();nvme_handle_cqe (nvmeq, iob, nvmeq->cq_head);nvme_update_cq_head (nvmeq); + found++; }if (found)
@@ -1627,17 +1627,22 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,return found; } +static irqreturn_t nvme_irq_complete_batch (struct io_comp_batch *iob, +unsigned int completions) +{ +if (!completions) +return IRQ_NONE ; +if (!rq_list_empty (&iob->req_list)) +nvme_pci_complete_batch (iob); +return IRQ_HANDLED ; +} +static irqreturn_t nvme_irq (int irq,void *data) {struct nvme_queue *nvmeq = data;DEFINE_IO_COMP_BATCH (iob); -if (nvme_poll_cq (nvmeq, &iob)) { -if (!rq_list_empty (&iob.req_list)) -nvme_pci_complete_batch (&iob); -return IRQ_HANDLED ; - } -return IRQ_NONE ; +return nvme_irq_complete_batch (&iob,nvme_poll_cq (nvmeq, &iob)); }static irqreturn_t nvme_irq_check (int irq,void *data)
Signature · 1 line
2.39.5
From 0b70853ac7e5d9dcc3d39714e0e55729f6533cda Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Wed, 21 Oct 2026 16:32:42 +0800
Subject: [RFC PATCH 4/4] nvme-pci: add per-queue adaptive interrupt polling
At high IOPS, handling an interrupt for each completion competes with I/O submission for CPU time. Periodic polling can amortize this work, but it can delay completions and reduce throughput at low queue depth.
Let each eligible I/O queue use DIM to choose between interrupts and polling every 10 us, using its own completion rate. Queues on the same IRQ CPU share a pinned timer. The timer only schedules irq_poll work; completion processing and DIM decisions stay under the queue lock.
Measure 4096 completions per sample and compare rates with a 3% band. After a step, remeasure the old mode and compare the new sample against the mean of both old samples (A/B/A), unless the difference exceeds 12%. During a search, comparable rates favor interrupts unless both samples reach 100 completions per millisecond. This tie preference is not an admission threshold for polling.
While parked, follow slow drift with a 1/8 EWMA and restart exploration after three consecutive deviations in the same direction. Return to interrupts when a polling sample stalls; reset measurements after a physical mode change or an IRQ CPU change without disturbing peers.
Join polling after completing the interrupt batch and disabling the Linux IRQ. Release irq_poll ownership before enabling the IRQ again, including when a full-budget callback has to defer the transition. Serialize queue stops with CPU offline and synchronize IRQ and poll callbacks before freeing queue state.
The use_adaptive_irq_polling module parameter defaults to off. The per-controller adaptive_irq_polling sysfs attribute changes the policy at runtime after freezing namespace I/O. Only dedicated MSI-X I/O queues with hardirq handlers on non-RT kernels and timer resolution at most 10 us are eligible. Threaded handlers remain interrupt driven.
This policy optimizes a local completion-rate estimate, not total CPU cost or tail latency. Concurrent searches on shared CPUs can affect the estimate, and per-queue decisions do not guarantee the best total throughput when many queues contend for one CPU.
--- Documentation/ABI/testing/sysfs-nvme | 20 + drivers/nvme/host/Kconfig | 2 + drivers/nvme/host/pci.c | 642 +++++++++++++++++++++++++-- 3 files changed, 638 insertions(+), 26 deletions(-)
Documentation/ABI/testing/sysfs-nvme +20 -0
@@ -11,3 +11,23 @@ Description: (REPLACETLSPSK) with the target. After a reauthentication the value returned by tls_configured_key will be the new serial. + +What: /sys/class/nvme/nvmeX/adaptive_irq_polling +Date: August 2026 +KernelVersion: 7.3 +Contact: Linux NVMe mailing list <[email protected]> +Description: + Set the adaptive IRQ polling policy (0 or 1) for eligible + I/O queues of one PCI NVMe controller. Reading returns the + policy, not whether a queue is currently polling. Changing it + freezes the namespace request queues and waits for outstanding + namespace I/O. Writes fail with EBUSY unless the controller is + live. + + Eligible queues use dedicated MSI-X vectors on non-RT kernels, + with handlers running in hardirq context and timer resolution + at most 10 us. Forced-threaded handlers (threadirqs) stay in + IRQ mode. The attribute is unavailable on PREEMPT_RT kernels + or with use_threaded_interrupts; otherwise it reports policy + even when no queue is eligible. The module parameter supplies + only the initial policy for new controllers.
drivers/nvme/host/Kconfig +2 -0
@@ -5,6 +5,8 @@ config NVME_CORE config BLK_DEV_NVME tristate "NVM Express block device" depends on PCI && BLOCK + select DIMLIB + select IRQ_POLL select NVME_CORE help The NVM Express driver is for solid state drives directly
drivers/nvme/host/pci.c +616 -26
867 lines not on this page · raw message
Signature · 1 line
2.39.5
Christoph Hellwig
maintainerQuoted 10 lines · Fengnan
Hi Keith:
I made some modifications to DIM and then implemented a solution using the DIM framework.
Based on current testing, the results are now comparable to those of the previous patch.
Here are the preliminary test results:
https://docs.google.com/spreadsheets/d/1e8LQPnaDhmv50XlAM3XxI0TMQkhbk0bTZwlJP1Hzzdk/edit?gid=188992110#gid=188992110If you agree with this approach, I’ll continue testing and optimizing.
І can't speak for Keith, but I'd really love to see this going forward!
Keith Busch
maintainerQuoted 10 lines · Christoph Hellwig
Quoted 9 lines · Fengnan
Hi Keith:
I made some modifications to DIM and then implemented a solution using the DIM framework.
Based on current testing, the results are now comparable to those of the previous patch.
Here are the preliminary test results:
https://docs.google.com/spreadsheets/d/1e8LQPnaDhmv50XlAM3XxI0TMQkhbk0bTZwlJP1Hzzdk/edit?gid=188992110#gid=188992110
Quoted 3 lines
Quoted 1 line
If you agree with this approach, I’ll continue testing and optimizing.
І can't speak for Keith, but I'd really love to see this going forward!
Same! This is a great problem to solve, but I'm just sorry I don't have more cycles to work on the cause. I can quickly respin the msix noflush pre-patches to address the naming issues tglx mentioned, and we can take nvme in whichever direction has the best performance.
Quoted 18 lines
From: "Keith Busch"<[email protected]>
Date: Wed, Oct 28, 2026, 23:15
Subject: Re: [RFC PATCH 0/3] nvme-pci: dynamic interrupt moderation
To: "Christoph Hellwig"<[email protected]>
Cc: "Fengnan"<[email protected]>, "Keith Busch"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Fengnan
Hi Keith:
I made some modifications to DIM and then implemented a solution using the DIM framework.
Based on current testing, the results are now comparable to those of the previous patch.
Here are the preliminary test results:
https://docs.google.com/spreadsheets/d/1e8LQPnaDhmv50XlAM3XxI0TMQkhbk0bTZwlJP1Hzzdk/edit?gid=188992110#gid=188992110
If you agree with this approach, I’ll continue testing and optimizing.
Quoted 8 lines
Quoted 2 lines
І can't speak for Keith, but I'd really love to see this going forward!
Same! This is a great problem to solve, but I'm just sorry I don't have more cycles to work on the cause. I can quickly respin the msix noflush pre-patches to address the naming issues tglx mentioned, and we can take nvme in whichever direction has the best performance.
That sounds good.
I'm currently working on continuously optimizing and testing both approaches (using DIM and not using DIM).
I plan to update the code and add new data again next Monday.
Thanks.
Quoted 27 lines · changfengnan
Quoted 18 lines
From: "Keith Busch"<[email protected]>
Date: Wed, Oct 28, 2026, 23:15
Subject: Re: [RFC PATCH 0/3] nvme-pci: dynamic interrupt moderation
To: "Christoph Hellwig"<[email protected]>
Cc: "Fengnan"<[email protected]>, "Keith Busch"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Fengnan
Hi Keith:
I made some modifications to DIM and then implemented a solution using the DIM framework.
Based on current testing, the results are now comparable to those of the previous patch.
Here are the preliminary test results:
https://docs.google.com/spreadsheets/d/1e8LQPnaDhmv50XlAM3XxI0TMQkhbk0bTZwlJP1Hzzdk/edit?gid=188992110#gid=188992110
If you agree with this approach, I’ll continue testing and optimizing.Quoted 8 lines
Quoted 2 lines
І can't speak for Keith, but I'd really love to see this going forward!
Same! This is a great problem to solve, but I'm just sorry I don't have more cycles to work on the cause. I can quickly respin the msix noflush pre-patches to address the naming issues tglx mentioned, and we can take nvme in whichever direction has the best performance.
Quoted 6 lines
That sounds good.
I'm currently working on continuously optimizing and testing both approaches (using DIM and not using DIM).
I plan to update the code and add new data again next Monday.Thanks.
Hi Keith and Christoph,
I've prepared two implementations of adaptive IRQ polling: one reusing the DIM tuning state machine and one using an NVMe-specific policy.
I tested both with a 158-case fio matrix and additional latency and CPU-contention tests. The results show broadly similar throughput gains over their respective OFF baselines in the tested workloads.
Test data:
[1] Non-DIM version:
https://docs.google.com/spreadsheets/d/17kmXH9Zkr2jrN-19BwaInSx53HwOdRPJFrZ-Hjlk0_Y/edit?gid=300100#gid=300100
[2] DIM version:
https://docs.google.com/spreadsheets/d/18JADnBXWAGkffY_3LsPJ6ftA9DGH6qpNbKvP-lQWLnE/edit?gid=300200#gid=300200
Both implementations are opt-in and choose between IRQ completion and timer-driven polling per I/O queue. They evaluate samples of at least 32,768 completions and use a target polling interval of 10 us. A polling trial must improve the completion rate by more than 1%. Once successful, polling can continue while its rate stays within roughly 1% of the saved IRQ baseline, subject to a completion-progress guard.
Neither implementation uses I/O latency as a tuning metric.
The non-DIM implementation uses explicit rules inside the NVMe driver. It admits polling trials at an IRQ completion rate of about 100k completions/s per queue and applies a fixed completion-count backoff after three consecutive failures.
The DIM implementation uses IRQ and POLL as two profiles. It explores polling without that fixed admission threshold and requires a trial to gain more than both 1% and one completion/ms. While parked on IRQ, it increases the wait between trials from 1 to 2, 4, 8 and then 16 valid IRQ samples. It also discards stale samples and stops the polling timer when the queue drains.
During development, I felt that the version with dim required more rules to achieve a similar effect. So, I still prefer the version without dim because it’s simpler, easier to understand, and easier to maintain.
Which approach would you prefer?
All the patches are in the attachment.
From ab88b22a6f0ced47fa7ced19b24a846520e449ef Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Mon, 19 Oct 2026 11:03:45 +0000
Subject: [PATCH DIM 1/4] lib/dim: allow DIMLIB without CONFIG_NET
Only net_dim.c needs networking: it manages net_device moderation profiles. dim.c and rdma_dim.c are generic, yet DIMLIB as a whole depends on NET, so a block driver cannot select it on a NET=n kernel.
Drop the NET dependency from DIMLIB and build net_dim.o only when CONFIG_NET is enabled. All net_dim users already depend on NET.
--- lib/Kconfig | 1 - lib/dim/Makefile | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-)
lib/Kconfig +0 -1
@@ -475,7 +475,6 @@ config SIGNATURE config DIMLIB tristate - depends on NET help Dynamic Interrupt Moderation library. Implements an algorithm for dynamically changing CQ moderation values
lib/dim/Makefile +2 -1
@@ -4,4 +4,5 @@ obj-$(CONFIG_DIMLIB) += dimlib.o -dimlib-y := dim.o net_dim.o rdma_dim.o +dimlib-y := dim.o rdma_dim.o +dimlib-$(CONFIG_NET) += net_dim.o
From af0e78ec947217a0995cddc42fce6526b015c159 Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Mon, 19 Oct 2026 11:03:46 +0000
Subject: [PATCH DIM 2/4] lib/dim: factor the tuning state machine out of net_dim
net_dim_decision() walks a profile table: it steps towards the better profile, turns when a step does not help, parks on top, and parks tired after too many steps. Only the statistics comparison and the number of profiles are specific to networking.
Move the state machine into dim.c as dim_decide(), which takes the comparison as a callback and the number of profiles as an argument, so that other drivers can reuse the DIM search with their own measurements and profile tables. The tired limit becomes twice the number of profiles, which is unchanged for net_dim. net_dim() now calls dim_decide() with net_dim_stats_compare() and NET_DIM_PARAMS_NUM_PROFILES; no functional change.
--- include/linux/dim.h | 21 +++++++++++ lib/dim/dim.c | 88 +++++++++++++++++++++++++++++++++++++++++++ lib/dim/net_dim.c | 91 ++------------------------------------------- 3 files changed, 112 insertions(+), 88 deletions(-)
include/linux/dim.h +21 -0
@@ -341,6 +341,27 @@ void dim_park_on_top(struct dim *dim);*/ void dim_park_tired (struct dim *dim); +/** +* dim_decide - one tuning decision on a new statistics sample +* @dim: DIM context +* @curr_stats: statistics of the sample just measured +* @compare: callback returning %DIM_STATS_BETTER, +* %DIM_STATS_SAME or %DIM_STATS_WORSE, given @dim and both samples +* @nprofiles: number of profiles, from 1 to 127 +* +* Run the DIM tuning state machine used by net_dim: step towards the +* better profile, turn when a step does not help, park on top, and park +* tired after 2 * @nprofiles steps without settling. The caller serializes +* access to @dim and applies the new profile. profile_ix must be below +* @nprofiles; a tired parked context must have a nonzero tired count. +* +* Return: true if profile_ix changed. +*/ +bool dim_decide (struct dim *dim,struct dim_stats *curr_stats, +int (*compare)(struct dim *dim,struct dim_stats *curr, +struct dim_stats *prev), +u8 nprofiles); +/** * dim_calc_stats - calculate the difference between two samples * @start: start sample
lib/dim/dim.c +88 -0
94 lines not on this page · raw message
lib/dim/net_dim.c +3 -88
109 lines not on this page · raw message
From db6ef734309963486a7d9640850bb71e4bd5168a Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Wed, 19 Aug 2026 15:31:27 +0800
Subject: [PATCH DIM 3/4] nvme-pci: return completion count from nvme_poll_cq
Return the number of completions consumed by nvme_poll_cq() instead of a boolean. Existing callers still treat zero vs non-zero as before; nvme_poll() keeps returning 0/1 via a bool local.
Extract nvme_irq_complete_batch() from nvme_irq() so the adaptive handler in the next patch can reuse the batch completion path.
--- drivers/nvme/host/pci.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/nvme/host/pci.c +15 -10
@@ -1606,13 +1606,12 @@ static inline void nvme_update_cq_head(struct nvme_queue *nvmeq) } } -static inline bool nvme_poll_cq (struct nvme_queue *nvmeq, -struct io_comp_batch *iob) +static inline unsigned int nvme_poll_cq (struct nvme_queue *nvmeq, +struct io_comp_batch *iob) { -bool found = false; +unsigned int found =0 ;while (nvme_cqe_pending (nvmeq)) { - found = true;/* * load-load control dependency between phase and the rest of * the cqe requires a full read memory barrier
@@ -1620,6 +1619,7 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,dma_rmb ();nvme_handle_cqe (nvmeq, iob, nvmeq->cq_head);nvme_update_cq_head (nvmeq); + found++; }if (found)
@@ -1627,17 +1627,22 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,return found; } +static irqreturn_t nvme_irq_complete_batch (struct io_comp_batch *iob, +unsigned int completions) +{ +if (!completions) +return IRQ_NONE ; +if (!rq_list_empty (&iob->req_list)) +nvme_pci_complete_batch (iob); +return IRQ_HANDLED ; +} +static irqreturn_t nvme_irq (int irq,void *data) {struct nvme_queue *nvmeq = data;DEFINE_IO_COMP_BATCH (iob); -if (nvme_poll_cq (nvmeq, &iob)) { -if (!rq_list_empty (&iob.req_list)) -nvme_pci_complete_batch (&iob); -return IRQ_HANDLED ; - } -return IRQ_NONE ; +return nvme_irq_complete_batch (&iob,nvme_poll_cq (nvmeq, &iob)); }static irqreturn_t nvme_irq_check (int irq,void *data)
From e4d1e421995bb8bc00b06a324e831eaa911cfc85 Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Fri, 30 Oct 2026 01:47:12 +0000
Subject: [PATCH DIM 4/4] nvme-pci: add DIM-based adaptive interrupt polling
Frequent completion interrupts compete with command submission for CPU time at high IOPS. Use the common DIM search to let each eligible I/O queue choose IRQ completion or periodic polling.
Compare 32768-CQE samples using two profiles. A trial must gain more than 1% and more than one CQE/ms to replace its starting profile; ties keep that profile. Keep a selected POLL profile unless its rate falls more than 1% below the saved IRQ reference. Space IRQ-to-POLL trials by 1, 2, 4, 8 and then 16 valid IRQ samples. Allow 128 CQEs of cumulative lag, discard samples older than one second, and reject POLL samples with no completions reaped by the poller.
Use a pinned hard hrtimer and irq_poll with a 10 us period and a 64-CQE budget. Poll only while commands remain, and join the current CQ owner before changing policy or tearing down the queue. Keep the feature off by default and limit it to non-threaded I/O queues with dedicated MSI-X vectors, outside PREEMPT_RT and with adequate timer resolution. The module parameter sets the initial policy; sysfs changes it while namespace I/O is frozen and drained.
The linked 158-case matrix compares adaptive polling disabled (OFF) and enabled (ON) on the same kernel build, using paired ABBA runs with libaio and io_uring. Median per-case paired IOPS changes for QD16-128 per job are shown below (12 cases per row; multi-device tests use 16 drives):
multi-device 4k randread, numjobs=8 +92.3% multi-device 4k mixed, numjobs=8 +49.1% single-device 4k randwrite, numjobs=1 +40.1%
multi-device 4k mixed, numjobs=1 +4.3%
--- Documentation/ABI/testing/sysfs-nvme | 17 ++ drivers/nvme/host/Kconfig | 2 + drivers/nvme/host/pci.c | 423 ++++++++++++++++++++++++++- 3 files changed, 435 insertions(+), 7 deletions(-)
Documentation/ABI/testing/sysfs-nvme +17 -0
@@ -11,3 +11,20 @@ Description: (REPLACETLSPSK) with the target. After a reauthentication the value returned by tls_configured_key will be the new serial. + +What: /sys/class/nvme/nvmeX/adaptive_irq_polling +Date: August 2026 +KernelVersion: 7.3 +Contact: Linux NVMe mailing list <[email protected]> +Description: + Set the adaptive IRQ polling policy (0 or 1) for eligible + I/O queues of one PCI NVMe controller. Reading returns the + policy, not whether a queue is currently polling. Changing it + freezes the namespace request queues and waits for outstanding + namespace I/O. Writes fail with EBUSY unless the controller is + live. + + Eligible queues use non-threaded MSI-X with a dedicated vector. + The attribute is unavailable with threaded interrupts. The + module parameter supplies only the initial policy for new + controllers.
drivers/nvme/host/Kconfig +2 -0
@@ -6,6 +6,8 @@ config BLK_DEV_NVME tristate "NVM Express block device" depends on PCI && BLOCK select NVME_CORE + select DIMLIB + select IRQ_POLL help The NVM Express driver is for solid state drives directly connected to the PCI or PCI Express bus. If you know you
drivers/nvme/host/pci.c +416 -7
537 lines not on this page · raw message
From 9e6aad1e528bf376b8113c2bf07445d4f5130e22 Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Wed, 19 Aug 2026 15:31:27 +0800
Subject: [PATCH non-DIM 1/2] nvme-pci: return completion count from nvme_poll_cq
Return the number of completions consumed by nvme_poll_cq() instead of a boolean. Existing callers still treat zero vs non-zero as before; nvme_poll() keeps returning 0/1 via a bool local.
Extract nvme_irq_complete_batch() from nvme_irq() so the adaptive handler in the next patch can reuse the batch completion path.
--- drivers/nvme/host/pci.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/nvme/host/pci.c +15 -10
@@ -1606,13 +1606,12 @@ static inline void nvme_update_cq_head(struct nvme_queue *nvmeq) } } -static inline bool nvme_poll_cq (struct nvme_queue *nvmeq, -struct io_comp_batch *iob) +static inline unsigned int nvme_poll_cq (struct nvme_queue *nvmeq, +struct io_comp_batch *iob) { -bool found = false; +unsigned int found =0 ;while (nvme_cqe_pending (nvmeq)) { - found = true;/* * load-load control dependency between phase and the rest of * the cqe requires a full read memory barrier
@@ -1620,6 +1619,7 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,dma_rmb ();nvme_handle_cqe (nvmeq, iob, nvmeq->cq_head);nvme_update_cq_head (nvmeq); + found++; }if (found)
@@ -1627,17 +1627,22 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,return found; } +static irqreturn_t nvme_irq_complete_batch (struct io_comp_batch *iob, +unsigned int completions) +{ +if (!completions) +return IRQ_NONE ; +if (!rq_list_empty (&iob->req_list)) +nvme_pci_complete_batch (iob); +return IRQ_HANDLED ; +} +static irqreturn_t nvme_irq (int irq,void *data) {struct nvme_queue *nvmeq = data;DEFINE_IO_COMP_BATCH (iob); -if (nvme_poll_cq (nvmeq, &iob)) { -if (!rq_list_empty (&iob.req_list)) -nvme_pci_complete_batch (&iob); -return IRQ_HANDLED ; - } -return IRQ_NONE ; +return nvme_irq_complete_batch (&iob,nvme_poll_cq (nvmeq, &iob)); }static irqreturn_t nvme_irq_check (int irq,void *data)
From 09a7d5e606bcb24875de2cd18660e8ea68eef75d Mon Sep 17 00:00:00 2001
From: Fengnan Chang <[email protected]>
Date: Wed, 19 Aug 2026 15:34:52 +0800
Subject: [PATCH non-DIM 2/2] nvme-pci: add adaptive interrupt polling
Frequent completion interrupts compete with command submission for CPU time at high IOPS. Let each eligible I/O queue try periodic polling and keep it when the measured completion rate justifies it.
Sample at least 32768 IRQ completions and try polling when their average interval is at most 10 us. Require more than 1% rate gain in the first poll window; later windows may take up to 1% longer than the saved IRQ baseline. Allow 128 completion intervals of cumulative lag within a window. After three consecutive failures, wait 524288 IRQ completions before measuring again.
Use a pinned hard hrtimer and irq_poll with a 10 us target period and a 64-CQE budget. Serialize CQ access and stop polling before policy changes or teardown. Keep the feature off by default and limit it to non-threaded I/O queues with dedicated MSI-X vectors. The module parameter sets the initial controller policy; sysfs changes it while namespace I/O is frozen and drained.
The linked 158-case matrix compares adaptive polling disabled (OFF) and enabled (ON) on the same kernel build, using paired ABBA runs with libaio and io_uring. Median per-case paired IOPS changes for QD16-128 per job are shown below (12 cases per row; multi-device tests use 16 drives):
multi-device 4k randread, numjobs=8 +92.0% multi-device 4k mixed, numjobs=8 +49.1% single-device 4k randwrite, numjobs=1 +38.4%
multi-device 4k mixed, numjobs=1 +3.5%
--- Documentation/ABI/testing/sysfs-nvme | 17 + drivers/nvme/host/Kconfig | 1 + drivers/nvme/host/pci.c | 503 ++++++++++++++++++++++++++- 3 files changed, 506 insertions(+), 15 deletions(-)
Documentation/ABI/testing/sysfs-nvme +17 -0
@@ -11,3 +11,20 @@ Description: (REPLACETLSPSK) with the target. After a reauthentication the value returned by tls_configured_key will be the new serial. + +What: /sys/class/nvme/nvmeX/adaptive_irq_polling +Date: August 2026 +KernelVersion: 7.3 +Contact: Linux NVMe mailing list <[email protected]> +Description: + Set the adaptive IRQ polling policy (0 or 1) for eligible + I/O queues of one PCI NVMe controller. Reading returns the + policy, not whether a queue is currently polling. Changing it + freezes the namespace request queues and waits for outstanding + namespace I/O. Writes fail with EBUSY unless the controller is + live. + + Eligible queues use non-threaded MSI-X with a dedicated vector. + The attribute is unavailable with threaded interrupts. The + module parameter supplies only the initial policy for new + controllers.
drivers/nvme/host/Kconfig +1 -0
@@ -5,6 +5,7 @@ config NVME_CORE config BLK_DEV_NVME tristate "NVM Express block device" depends on PCI && BLOCK + select IRQ_POLL select NVME_CORE help The NVM Express driver is for solid state drives directly
drivers/nvme/host/pci.c +488 -15
664 lines not on this page · raw message
在 02/11/26 20:22, Fengnan 写道:
Hi Keith:
Any comments?
Quoted 84 lines · changfengnan
Quoted 27 lines
Quoted 18 lines
From: "Keith Busch"<[email protected]>
Date: Wed, Oct 28, 2026, 23:15
Subject: Re: [RFC PATCH 0/3] nvme-pci: dynamic interrupt moderation
To: "Christoph Hellwig"<[email protected]>
Cc: "Fengnan"<[email protected]>, "Keith Busch"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Fengnan
Hi Keith:
I made some modifications to DIM and then implemented a solution using the DIM framework.
Based on current testing, the results are now comparable to those of the previous patch.
Here are the preliminary test results:
https://docs.google.com/spreadsheets/d/1e8LQPnaDhmv50XlAM3XxI0TMQkhbk0bTZwlJP1Hzzdk/edit?gid=188992110#gid=188992110
If you agree with this approach, I’ll continue testing and optimizing.Quoted 8 lines
Quoted 2 lines
І can't speak for Keith, but I'd really love to see this going forward!
Same! This is a great problem to solve, but I'm just sorry I don't have more cycles to work on the cause. I can quickly respin the msix noflush pre-patches to address the naming issues tglx mentioned, and we can take nvme in whichever direction has the best performance.Quoted 6 lines
That sounds good.
I'm currently working on continuously optimizing and testing both approaches (using DIM and not using DIM).
I plan to update the code and add new data again next Monday.Thanks.
Hi Keith and Christoph,
I've prepared two implementations of adaptive IRQ polling: one reusing the DIM tuning state machine and one using an NVMe-specific policy.
I tested both with a 158-case fio matrix and additional latency and CPU-contention tests. The results show broadly similar throughput gains over their respective OFF baselines in the tested workloads.
Test data:
[1] Non-DIM version:
https://docs.google.com/spreadsheets/d/17kmXH9Zkr2jrN-19BwaInSx53HwOdRPJFrZ-Hjlk0_Y/edit?gid=300100#gid=300100[2] DIM version:
https://docs.google.com/spreadsheets/d/18JADnBXWAGkffY_3LsPJ6ftA9DGH6qpNbKvP-lQWLnE/edit?gid=300200#gid=300200Both implementations are opt-in and choose between IRQ completion and timer-driven polling per I/O queue. They evaluate samples of at least 32,768 completions and use a target polling interval of 10 us. A polling trial must improve the completion rate by more than 1%. Once successful, polling can continue while its rate stays within roughly 1% of the saved IRQ baseline, subject to a completion-progress guard.
Neither implementation uses I/O latency as a tuning metric.
The non-DIM implementation uses explicit rules inside the NVMe driver. It admits polling trials at an IRQ completion rate of about 100k completions/s per queue and applies a fixed completion-count backoff after three consecutive failures.
The DIM implementation uses IRQ and POLL as two profiles. It explores polling without that fixed admission threshold and requires a trial to gain more than both 1% and one completion/ms. While parked on IRQ, it increases the wait between trials from 1 to 2, 4, 8 and then 16 valid IRQ samples. It also discards stale samples and stops the polling timer when the queue drains.
During development, I felt that the version with
dimrequired more rules to achieve a similar effect. So, I still prefer the version withoutdimbecause it’s simpler, easier to understand, and easier to maintain.Which approach would you prefer?
All the patches are in the attachment.