[PATCH v2 4/5] irqchip/riscv-imsic: Finish vector retirement before IRQ reuse

From: Zhanpeng Zhang

Date: Thu Dec 03 2026 - 07:32:25 EST


The IMSIC chained handler can read a Linux IRQ number before teardown
and look up its descriptor after the IRQ core has reused that number.
synchronize_irq() waits for the child's interrupt handler, but it does
not cover the IMSIC handler between the load and the lookup.

Clear the IRQ mapping and retire both vectors of a cancelled move on
their owning CPUs. Wait for the local sync callback on each online
owner, with no vector locks held, before returning from IRQ-domain
free. The callback runs with interrupts disabled, so the owner has
either finished dispatching the old number or will read the cleared
mapping. Return each vector ID only after clearing its hardware state.

An offline CPU flushes dirty vectors at startup. If it has already
flushed them but is not yet online for IPIs, let its local timer pick
up the retirement.

Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
drivers/irqchip/irq-riscv-imsic-state.c | 79 +++++++++++++++++++++++--
drivers/irqchip/irq-riscv-imsic-state.h | 1 +
2 files changed, 74 insertions(+), 6 deletions(-)

diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
index cc27fd624..840e83fde 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.c
+++ b/drivers/irqchip/irq-riscv-imsic-state.c
@@ -531,26 +531,83 @@ static void imsic_vector_free(struct imsic_vector *vec)
raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
}

+static void imsic_vector_retire(struct imsic_local_priv *lpriv, struct imsic_vector *vec)
+{
+ lockdep_assert_held(&lpriv->lock);
+
+ WRITE_ONCE(vec->irq, 0);
+ WRITE_ONCE(vec->enable, false);
+ WRITE_ONCE(vec->move_prev, NULL);
+ vec->retiring = true;
+ /* Publish cancellation to the destination's local sync. */
+ smp_store_release(&vec->move_next, NULL);
+ bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
+}
+
+static void imsic_vector_sync_local(void *unused)
+{
+ imsic_local_sync_all(false);
+}
+
+static void imsic_vector_sync_cpu(unsigned int cpu)
+{
+#ifdef CONFIG_SMP
+ struct imsic_local_priv *lpriv = per_cpu_ptr(imsic->lpriv, cpu);
+ unsigned long flags;
+
+ /*
+ * Complete retirement and wait for any IMSIC interrupt already
+ * dispatching the old IRQ number before the IRQ core can reuse it.
+ * The chained handler and this callback run with interrupts disabled.
+ */
+ if (!smp_call_function_single(cpu, imsic_vector_sync_local, NULL, true))
+ return;
+
+ /*
+ * An offline CPU flushes its vectors on startup. If IMSIC startup
+ * has already run but the CPU is not yet online for IPIs, use the
+ * local timer to pick up changes made after the startup flush.
+ */
+ raw_spin_lock_irqsave(&lpriv->lock, flags);
+ if (lpriv->started)
+ __imsic_local_timer_start(lpriv, cpu);
+ raw_spin_unlock_irqrestore(&lpriv->lock, flags);
+#else
+ imsic_vector_sync_local(NULL);
+#endif
+}
+
void imsic_vector_free_irq(struct imsic_vector *vec)
{
struct imsic_vector *prev = READ_ONCE(vec->move_prev);
struct imsic_local_priv *lpriv;
unsigned long flags;
+ unsigned int cpu = vec->cpu;
+ int prev_cpu = -1;

+ /*
+ * Stop the old vector from forwarding to this one before retiring
+ * it. Check the link under the old CPU's lock: the old slot may
+ * already have completed its move and been reused.
+ */
if (prev) {
lpriv = per_cpu_ptr(imsic->lpriv, prev->cpu);
raw_spin_lock_irqsave(&lpriv->lock, flags);
- /* The previous slot may have been reused after completion. */
if (prev->move_next == vec) {
- prev->retiring = true;
- WRITE_ONCE(prev->irq, 0);
- WRITE_ONCE(prev->move_next, NULL);
- __imsic_remote_sync(lpriv, prev->cpu);
+ imsic_vector_retire(lpriv, prev);
+ prev_cpu = prev->cpu;
}
raw_spin_unlock_irqrestore(&lpriv->lock, flags);
}

- imsic_vector_free(vec);
+ lpriv = per_cpu_ptr(imsic->lpriv, cpu);
+ raw_spin_lock_irqsave(&lpriv->lock, flags);
+ imsic_vector_retire(lpriv, vec);
+ raw_spin_unlock_irqrestore(&lpriv->lock, flags);
+
+ if (prev_cpu >= 0)
+ imsic_vector_sync_cpu(prev_cpu);
+ imsic_vector_sync_cpu(cpu);
}

static void __init imsic_local_cleanup(void)
@@ -620,8 +677,17 @@ fail_local_cleanup:

void imsic_state_online(void)
{
+#ifdef CONFIG_SMP
+ struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
+#endif
unsigned long flags;

+#ifdef CONFIG_SMP
+ raw_spin_lock_irqsave(&lpriv->lock, flags);
+ lpriv->started = true;
+ raw_spin_unlock_irqrestore(&lpriv->lock, flags);
+#endif
+
raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
irq_matrix_online(imsic->matrix);
raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
@@ -639,6 +705,7 @@ void imsic_state_offline(void)
struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);

raw_spin_lock_irqsave(&lpriv->lock, flags);
+ lpriv->started = false;
WARN_ON_ONCE(timer_delete_sync_try(&lpriv->timer) < 0);
raw_spin_unlock_irqrestore(&lpriv->lock, flags);
#endif
diff --git a/drivers/irqchip/irq-riscv-imsic-state.h b/drivers/irqchip/irq-riscv-imsic-state.h
index cc0766b77..171268dc7 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.h
+++ b/drivers/irqchip/irq-riscv-imsic-state.h
@@ -38,6 +38,7 @@ struct imsic_local_priv {
#ifdef CONFIG_SMP
/* Local timer for synchronization */
struct timer_list timer;
+ bool started;
#endif

/* Local vector table */