[PATCH RFC DNM 07/15] thunderbolt: nhi: Process ring completions in the threaded IRQ

From: Konrad Dybcio

Date: Tue Nov 24 2026 - 15:02:11 EST


The Qualcomm USB4 router uses a shared IRQ for all NHI rings.
Scheduling a work item from its threaded handler adds another delay
before completed RX frames are consumed and replacement buffers are
posted.

Process completed descriptors directly from the threaded IRQ instead.
Harvest all completed frames while holding the NHI and ring locks,
replenish RX rings immediately, then invoke frame callbacks after
dropping the locks. Preserve the existing polling-ring path, which must
continue to mask the interrupt and start polling instead.

This removes the workqueue round trip from the common IRQ path without
calling ring consumers under NHI locking. Since the callbacks no longer
run only from ring->work, have tb_ring_stop() and tb_ring_free()
synchronize with the ring interrupt, so they keep returning only after
all callbacks of the ring have finished.

Switch the single MSI fallback of the PCI glue over to the same threaded
handler, which allows dropping nhi_interrupt_work() and the work item
that backed it.

Based on related work by Wesley Cheng.

Signed-off-by: Konrad Dybcio <konrad.dybcio@xxxxxxxxxxxxxxxx>
---
drivers/thunderbolt/nhi.c | 133 +++++++++++++++++++++++++++++++++++---------
drivers/thunderbolt/nhi.h | 2 +-
drivers/thunderbolt/pci.c | 15 ++---
include/linux/thunderbolt.h | 8 +--
4 files changed, 115 insertions(+), 43 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index e48521482c021c7f5f73349ce16519d6088d3250..bcc2b959e3b1a6b014c13a4938ed6f68cb0d0fd8 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -310,6 +310,41 @@ static void ring_write_descriptors(struct tb_ring *ring, bool notify)
__ring_notify(ring);
}

+/*
+ * ring_harvest() - collect completed frames
+ * @ring: Ring to collect the frames from
+ * @done: List to move the completed frames to
+ *
+ * Moves all completed frames from the ring buffer to @done and writes new
+ * frames to the ring buffer.
+ *
+ * ring->lock is held.
+ */
+static void ring_harvest(struct tb_ring *ring, struct list_head *done)
+{
+ struct ring_frame *frame;
+
+ lockdep_assert_held(&ring->lock);
+
+ while (!ring_empty(ring)) {
+ if (!(ring->descriptors[ring->tail].flags
+ & RING_DESC_COMPLETED))
+ break;
+ frame = list_first_entry(&ring->in_flight, typeof(*frame),
+ list);
+ list_move_tail(&frame->list, done);
+ if (!ring->is_tx) {
+ frame->size = ring->descriptors[ring->tail].length;
+ frame->eof = ring->descriptors[ring->tail].eof;
+ frame->sof = ring->descriptors[ring->tail].sof;
+ frame->flags = ring->descriptors[ring->tail].flags;
+ }
+ frame->ring = ring;
+ ring->tail = (ring->tail + 1) % ring->size;
+ }
+ ring_write_descriptors(ring, true);
+}
+
/*
* ring_work() - progress completed frames
*
@@ -337,22 +372,7 @@ static void ring_work(struct work_struct *work)
goto invoke_callback;
}

- while (!ring_empty(ring)) {
- if (!(ring->descriptors[ring->tail].flags
- & RING_DESC_COMPLETED))
- break;
- frame = list_first_entry(&ring->in_flight, typeof(*frame),
- list);
- list_move_tail(&frame->list, &done);
- if (!ring->is_tx) {
- frame->size = ring->descriptors[ring->tail].length;
- frame->eof = ring->descriptors[ring->tail].eof;
- frame->sof = ring->descriptors[ring->tail].sof;
- frame->flags = ring->descriptors[ring->tail].flags;
- }
- ring->tail = (ring->tail + 1) % ring->size;
- }
- ring_write_descriptors(ring, true);
+ ring_harvest(ring, &done);

invoke_callback:
/* allow callbacks to schedule new work */
@@ -371,6 +391,35 @@ static void ring_work(struct work_struct *work)
wake_up(&ring->wait);
}

+/*
+ * ring_process_completions() - invoke the callbacks of completed frames
+ * @done: Frames collected by ring_harvest(), possibly from several rings
+ *
+ * The frames of each ring are contiguous in @done. Like ring_work(), wake
+ * up the waiters of a ring once the callbacks of all its frames have run.
+ */
+static void ring_process_completions(struct list_head *done)
+{
+ struct ring_frame *frame;
+ struct tb_ring *ring;
+
+ while (!list_empty(done)) {
+ frame = list_first_entry(done, typeof(*frame), list);
+ ring = frame->ring;
+ /*
+ * The callback may reenqueue or delete frame.
+ * Do not hold on to it.
+ */
+ list_del_init(&frame->list);
+ if (frame->callback)
+ frame->callback(ring, frame, false);
+
+ if (list_empty(done) ||
+ list_first_entry(done, typeof(*frame), list)->ring != ring)
+ wake_up(&ring->wait);
+ }
+}
+
int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame, bool more)
{
unsigned long flags;
@@ -819,6 +868,19 @@ static bool tb_ring_empty(struct tb_ring *ring)
return list_empty(&ring->in_flight);
}

+/*
+ * ring_sync_irq() - wait for the interrupt handler of a ring
+ *
+ * nhi_msi_thread() invokes the callbacks of the frames it has harvested
+ * after dropping the locks, so make sure it has finished with any frames
+ * of a ring that has been stopped or dissociated from the NHI.
+ */
+static void ring_sync_irq(struct tb_ring *ring)
+{
+ if (ring->irq > 0)
+ synchronize_irq(ring->irq);
+}
+
/**
* tb_ring_flush() - Waits for a ring to be empty
* @ring: Ring to wait
@@ -881,6 +943,8 @@ void tb_ring_stop(struct tb_ring *ring)
spin_unlock(&ring->lock);
spin_unlock_irq(&ring->nhi->lock);

+ ring_sync_irq(ring);
+
/*
* schedule ring->work to invoke callbacks on all remaining frames.
*/
@@ -906,7 +970,7 @@ void tb_ring_free(struct tb_ring *ring)
spin_lock_irq(&ring->nhi->lock);
/*
* Dissociate the ring from the NHI. This also ensures that
- * nhi_interrupt_work cannot reschedule ring->work.
+ * nhi_msi_thread() cannot harvest its frames anymore.
*/
if (ring->is_tx)
ring->nhi->tx_rings[ring->hop] = NULL;
@@ -919,6 +983,8 @@ void tb_ring_free(struct tb_ring *ring)
}
spin_unlock_irq(&ring->nhi->lock);

+ ring_sync_irq(ring);
+
if (nhi->ops->release_ring_irq)
nhi->ops->release_ring_irq(ring);

@@ -935,8 +1001,8 @@ void tb_ring_free(struct tb_ring *ring)

/*
* ring->work can no longer be scheduled (it is scheduled only
- * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
- * to finish before freeing the ring.
+ * by ring_stop and ring_msix). Wait for it to finish before
+ * freeing the ring.
*/
flush_work(&ring->work);
lockdep_unregister_key(&ring->lock_key);
@@ -1022,16 +1088,18 @@ enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
return (enum nhi_fw_mode)val;
}

-void nhi_interrupt_work(struct work_struct *work)
+irqreturn_t nhi_msi_thread(int irq, void *data)
{
- struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
+ struct tb_nhi *nhi = data;
+ LIST_HEAD(completed_frames);
+ unsigned long flags;
int value = 0; /* Suppress uninitialized usage warning. */
int bit;
int hop = -1;
int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
struct tb_ring *ring;

- spin_lock_irq(&nhi->lock);
+ spin_lock_irqsave(&nhi->lock, flags);

/*
* Starting at REG_RING_NOTIFY_BASE there are three status bitfields
@@ -1073,17 +1141,28 @@ void nhi_interrupt_work(struct work_struct *work)
}

spin_lock(&ring->lock);
- __ring_interrupt(ring);
+ /*
+ * Harvest the completions right away instead of deferring
+ * to ring->work, so that the RX buffers are reposted without
+ * another scheduling delay during a burst.
+ */
+ if (ring->start_poll)
+ __ring_interrupt(ring);
+ else if (ring->running)
+ ring_harvest(ring, &completed_frames);
spin_unlock(&ring->lock);
}
- spin_unlock_irq(&nhi->lock);
+ spin_unlock_irqrestore(&nhi->lock, flags);
+
+ if (!list_empty(&completed_frames))
+ ring_process_completions(&completed_frames);
+
+ return IRQ_HANDLED;
}

irqreturn_t nhi_msi(int irq, void *data)
{
- struct tb_nhi *nhi = data;
- schedule_work(&nhi->interrupt_work);
- return IRQ_HANDLED;
+ return IRQ_WAKE_THREAD;
}

static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 88af9ab9fde7afc8af6da412b668663e67380e53..71f750451aacae6ca2f8cb37bd8ae7fc5a029818 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -31,8 +31,8 @@ int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data);
enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi);
void nhi_enable_int_throttling(struct tb_nhi *nhi);
void nhi_disable_interrupts(struct tb_nhi *nhi);
-void nhi_interrupt_work(struct work_struct *work);
irqreturn_t nhi_msi(int irq, void *data);
+irqreturn_t nhi_msi_thread(int irq, void *data);
irqreturn_t ring_msix(int irq, void *data);
int nhi_probe(struct tb_nhi *nhi);
void nhi_shutdown(struct tb_nhi *nhi);
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 9fe2ef43d8ac8ac867c6e37d7a77518ad2221ed9..6388f77c67ac6d51831667abbffcfb8f52881af6 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -206,14 +206,13 @@ static int nhi_pci_init_msi(struct tb_nhi *nhi)
if (nvec < 0)
return nvec;

- INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
-
irq = pci_irq_vector(pdev, 0);
if (irq < 0)
return irq;

- res = devm_request_irq(&pdev->dev, irq, nhi_msi,
- IRQF_NO_SUSPEND, "thunderbolt", nhi);
+ res = devm_request_threaded_irq(&pdev->dev, irq, nhi_msi,
+ nhi_msi_thread,
+ IRQF_NO_SUSPEND, "thunderbolt", nhi);
if (res)
return res;
}
@@ -311,14 +310,8 @@ static void nhi_pci_release_irq(struct tb_nhi *nhi)
struct tb_nhi_pci *nhi_pci = nhi_to_pci(nhi);
struct pci_dev *pdev = to_pci_dev(nhi->dev);

- /*
- * We have to release the irq before calling flush_work. Otherwise an
- * already executing IRQ handler could call schedule_work again.
- */
- if (!pdev->msix_enabled) {
+ if (!pdev->msix_enabled)
devm_free_irq(nhi->dev, pdev->irq, nhi);
- flush_work(&nhi->interrupt_work);
- }
ida_destroy(&nhi_pci->msix_ida);
}

diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 57502da290805ed6ba627578da060afe290d4137..4faec9246d175d08063b7881ed1d379b9a7bdafd 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -501,7 +501,8 @@ void tb_service_properties_changed(struct tb_service *svc);
/**
* struct tb_nhi - thunderbolt native host interface
* @lock: Must be held during ring creation/destruction. Is acquired by
- * interrupt_work when dispatching interrupts to individual rings.
+ * the interrupt thread when dispatching interrupts to individual
+ * rings.
* @dev: Device associated with this NHI instance
* @ops: NHI specific optional ops
* @iobase: MMIO space of the NHI
@@ -511,8 +512,6 @@ void tb_service_properties_changed(struct tb_service *svc);
* @going_away: The host controller device is about to disappear so when
* this flag is set, avoid touching the hardware anymore.
* @iommu_dma_protection: An IOMMU will isolate external-facing ports.
- * @interrupt_work: Work scheduled to handle ring interrupt when no
- * MSI-X is used.
* @hop_count: Number of rings (end point hops) supported by NHI.
* @quirks: NHI specific quirks if any
* @domain_released: Completed when domain has been fully released
@@ -532,7 +531,6 @@ struct tb_nhi {
u32 *interrupt_mask;
bool going_away;
bool iommu_dma_protection;
- struct work_struct interrupt_work;
u32 hop_count;
unsigned long quirks;
struct completion domain_released;
@@ -639,11 +637,13 @@ enum ring_desc_flags {
* @flags: Flags for the frame (see &enum ring_desc_flags)
* @eof: End of frame protocol defined field
* @sof: Start of frame protocol defined field
+ * @ring: Ring the frame was completed on
*/
struct ring_frame {
dma_addr_t buffer_phy;
ring_cb callback;
struct list_head list;
+ struct tb_ring *ring;
u32 size:12;
u32 flags:12;
u32 eof:4;

--
2.55.0