[PATCH] irqchip/riscv-imsic: Fix vector lifetime during IRQ migration
From: Zhanpeng Zhang
Date: Wed Dec 02 2026 - 06:49:20 EST
When fio runs in a guest with oversubscribed vCPUs and a VFIO
passthrough device, the host can panic on a NULL pointer dereference in
irq_can_move_in_process_context() due to a race between IRQ-domain
teardown and IMSIC vector move completion.
Teardown can free the IRQ descriptor and release the destination vector
while the old CPU still has a move to complete. The old CPU can then
look up an IRQ number with no descriptor or forward a pending MSI to a
vector that another IRQ has already taken over.
Save the temporary MSI ID when setting affinity so move completion can
use it without looking up irq_data. During teardown, validate and
cancel the move under the old CPU's lock. Clear the retiring vectors'
enable and pending bits before returning their IDs to the matrix.
Clear the temporary pending bit if no other IRQ or move uses that ID.
Wait for the online vector owners before returning from the IRQ-domain
free callback. This also lets any chained handler that has read the old
IRQ number finish dispatching it before the IRQ core can reuse the
number. Offline CPUs finish cleanup when they come online.
During CPU offlining, complete the move on the outgoing CPU before
clearing the reverse link so pending MSIs can reach the active vector.
Fixes: 896f8e436f99 ("irqchip/riscv-imsic: Special handling for non-atomic device MSI update")
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
drivers/irqchip/irq-riscv-imsic-early.c | 5 +-
drivers/irqchip/irq-riscv-imsic-platform.c | 18 +-
drivers/irqchip/irq-riscv-imsic-state.c | 192 +++++++++++++++++----
drivers/irqchip/irq-riscv-imsic-state.h | 8 +-
4 files changed, 176 insertions(+), 47 deletions(-)
diff --git a/drivers/irqchip/irq-riscv-imsic-early.c b/drivers/irqchip/irq-riscv-imsic-early.c
index 06726a062..77e6cc849 100644
--- a/drivers/irqchip/irq-riscv-imsic-early.c
+++ b/drivers/irqchip/irq-riscv-imsic-early.c
@@ -95,6 +95,7 @@ static void imsic_handle_irq(struct irq_desc *desc)
struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
struct irq_chip *chip = irq_desc_get_chip(desc);
unsigned long local_id;
+ unsigned int irq;
/*
* Process pending local synchronization instead of waiting
@@ -118,7 +119,9 @@ static void imsic_handle_irq(struct irq_desc *desc)
continue;
}
- generic_handle_irq(lpriv->vectors[local_id].irq);
+ irq = READ_ONCE(lpriv->vectors[local_id].irq);
+ if (irq)
+ generic_handle_irq(irq);
}
chained_irq_exit(chip, desc);
diff --git a/drivers/irqchip/irq-riscv-imsic-platform.c b/drivers/irqchip/irq-riscv-imsic-platform.c
index 5f72b2a80..cb31379e1 100644
--- a/drivers/irqchip/irq-riscv-imsic-platform.c
+++ b/drivers/irqchip/irq-riscv-imsic-platform.c
@@ -103,6 +103,7 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
{
struct imsic_vector *old_vec, *new_vec;
struct imsic_vector tmp_vec;
+ bool move_via_tmp;
/*
* Requirements for the downstream irqdomains (or devices):
@@ -151,8 +152,9 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
* Based on the above, __imsic_local_sync() must check pending
* status of both old MSI data and new MSI data on the old CPU.
*/
- if (!irq_can_move_in_process_context(d) &&
- new_vec->local_id != old_vec->local_id) {
+ move_via_tmp = !irq_can_move_in_process_context(d) &&
+ new_vec->local_id != old_vec->local_id;
+ if (move_via_tmp) {
/* Setup temporary vector */
tmp_vec.cpu = old_vec->cpu;
tmp_vec.local_id = new_vec->local_id;
@@ -171,7 +173,7 @@ static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask
irq_data_update_effective_affinity(d, cpumask_of(new_vec->cpu));
/* Move state of the old vector to the new vector */
- imsic_vector_move(old_vec, new_vec);
+ imsic_vector_move(old_vec, new_vec, move_via_tmp);
return IRQ_SET_MASK_OK_DONE;
}
@@ -193,18 +195,10 @@ static void imsic_irq_force_complete_move(struct irq_data *d)
if (mvec->cpu != cpu)
return;
- /*
- * The best we can do is force cleanup the old IMSIC vector.
- *
- * The challenges over here are same as x86 vector domain so
- * refer to the comments in irq_force_complete_move() function
- * implemented at arch/x86/kernel/apic/vector.c.
- */
-
/* Force cleanup in-flight move */
pr_info("IRQ fixup: irq %d move in progress, old vector cpu %d local_id %d\n",
d->irq, mvec->cpu, mvec->local_id);
- imsic_vector_force_move_cleanup(vec);
+ imsic_vector_force_move_cleanup(vec, mvec);
}
#endif
diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
index 01620373b..818731772 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.c
+++ b/drivers/irqchip/irq-riscv-imsic-state.c
@@ -125,6 +125,39 @@ void __imsic_eix_update(unsigned long base_id, unsigned long num_id, bool pend,
}
}
+/* The owning CPU's local lock is held with interrupts disabled. */
+static void imsic_vector_release(struct imsic_vector *vec)
+{
+ WRITE_ONCE(vec->irq, 0);
+ vec->move_tmp_id = 0;
+ raw_spin_lock(&imsic->matrix_lock);
+ irq_matrix_free(imsic->matrix, vec->cpu, vec->local_id, false);
+ raw_spin_unlock(&imsic->matrix_lock);
+}
+
+static void imsic_cancel_tmp_msi(struct imsic_local_priv *lpriv, u16 tmp_id)
+{
+ unsigned int i;
+
+ lockdep_assert_held(&lpriv->lock);
+
+ /*
+ * The pending bit has no source information. Leave it alone if
+ * another IRQ owns the ID or another move still uses it.
+ */
+ if (!tmp_id || lpriv->vectors[tmp_id].irq)
+ return;
+
+ for_each_set_bit(i, lpriv->dirty_bitmap, imsic->global.nr_ids + 1) {
+ struct imsic_vector *vec = &lpriv->vectors[i];
+
+ if (vec->move_next && vec->move_tmp_id == tmp_id)
+ return;
+ }
+
+ __imsic_id_read_clear_pending(tmp_id);
+}
+
static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
{
struct imsic_local_config *tlocal, *mlocal;
@@ -139,6 +172,17 @@ static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
goto skip;
vec = &lpriv->vectors[i];
+ if (vec->retiring) {
+ __imsic_id_clear_enable(i);
+ __imsic_id_read_clear_pending(i);
+ imsic_cancel_tmp_msi(lpriv, vec->move_tmp_id);
+ vec->retiring = false;
+ /* Order the CSR writes before making this ID reusable. */
+ wmb();
+ imsic_vector_release(vec);
+ goto skip;
+ }
+
if (READ_ONCE(vec->enable))
__imsic_id_set_enable(i);
else
@@ -151,10 +195,11 @@ static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
mvec = READ_ONCE(vec->move_prev);
if (mvec) {
/*
- * If the old vector has not been updated then
- * try again in the next sync-up call.
+ * Wait only for this move. The old slot may have
+ * been reused for a different IRQ.
*/
- if (READ_ONCE(mvec->move_next)) {
+ /* Pair with the release of move_next on the old CPU. */
+ if (smp_load_acquire(&mvec->move_next) == vec) {
ret = false;
continue;
}
@@ -177,11 +222,9 @@ static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
*
* For details, see imsic_irq_set_affinity().
*/
- tvec = vec->local_id == mvec->local_id ?
- NULL : &lpriv->vectors[mvec->local_id];
-
- if (tvec && !irq_can_move_in_process_context(irq_get_irq_data(vec->irq)) &&
- __imsic_id_read_clear_pending(tvec->local_id)) {
+ if (vec->move_tmp_id &&
+ __imsic_id_read_clear_pending(vec->move_tmp_id)) {
+ tvec = &lpriv->vectors[vec->move_tmp_id];
/* Retrigger temporary vector if it was already in-use */
if (READ_ONCE(tvec->enable)) {
tlocal = per_cpu_ptr(imsic->global.local, tvec->cpu);
@@ -197,8 +240,11 @@ static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
writel_relaxed(mvec->local_id, mlocal->msi_va);
}
- WRITE_ONCE(vec->move_next, NULL);
- imsic_vector_free(vec);
+ /* Order the retriggers before publishing move completion. */
+ wmb();
+ /* Pair with the acquire of move_next on the new CPU. */
+ smp_store_release(&vec->move_next, NULL);
+ imsic_vector_release(vec);
}
skip:
@@ -334,26 +380,27 @@ void imsic_vector_unmask(struct imsic_vector *vec)
raw_spin_unlock(&lpriv->lock);
}
-void imsic_vector_force_move_cleanup(struct imsic_vector *vec)
+void imsic_vector_force_move_cleanup(struct imsic_vector *vec, struct imsic_vector *prev)
{
- struct imsic_local_priv *lpriv;
- struct imsic_vector *mvec;
+ struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
unsigned long flags;
- lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
+ /* This runs on the outgoing CPU. Forward pending MSI before freeing. */
raw_spin_lock_irqsave(&lpriv->lock, flags);
+ if (prev->move_next == vec)
+ __imsic_local_sync(lpriv);
+ raw_spin_unlock_irqrestore(&lpriv->lock, flags);
- mvec = READ_ONCE(vec->move_prev);
+ lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
+ raw_spin_lock_irqsave(&lpriv->lock, flags);
WRITE_ONCE(vec->move_prev, NULL);
- if (mvec)
- imsic_vector_free(mvec);
-
raw_spin_unlock_irqrestore(&lpriv->lock, flags);
}
static bool imsic_vector_move_update(struct imsic_local_priv *lpriv,
struct imsic_vector *vec, bool is_old_vec,
- bool new_enable, struct imsic_vector *move_vec)
+ bool new_enable, struct imsic_vector *move_vec,
+ bool move_via_tmp)
{
unsigned long flags;
bool enabled;
@@ -363,10 +410,12 @@ static bool imsic_vector_move_update(struct imsic_local_priv *lpriv,
/* Update enable and move details */
enabled = READ_ONCE(vec->enable);
WRITE_ONCE(vec->enable, new_enable);
- if (is_old_vec)
+ if (is_old_vec) {
+ vec->move_tmp_id = move_via_tmp ? move_vec->local_id : 0;
WRITE_ONCE(vec->move_next, move_vec);
- else
+ } else {
WRITE_ONCE(vec->move_prev, move_vec);
+ }
/* Mark the vector as dirty and synchronize */
bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
@@ -377,7 +426,8 @@ static bool imsic_vector_move_update(struct imsic_local_priv *lpriv,
return enabled;
}
-void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_vec)
+void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_vec,
+ bool move_via_tmp)
{
struct imsic_local_priv *old_lpriv, *new_lpriv;
bool enabled;
@@ -399,8 +449,9 @@ void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_ve
* interrupt on the old vector while device was being moved
* to the new vector.
*/
- enabled = imsic_vector_move_update(old_lpriv, old_vec, true, false, new_vec);
- imsic_vector_move_update(new_lpriv, new_vec, false, enabled, old_vec);
+ enabled = imsic_vector_move_update(old_lpriv, old_vec, true, false, new_vec,
+ move_via_tmp);
+ imsic_vector_move_update(new_lpriv, new_vec, false, enabled, old_vec, false);
}
#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
@@ -451,22 +502,89 @@ struct imsic_vector *imsic_vector_alloc(unsigned int irq, const struct cpumask *
lpriv = per_cpu_ptr(imsic->lpriv, cpu);
vec = &lpriv->vectors[local_id];
- vec->irq = irq;
- vec->enable = false;
- vec->move_next = NULL;
- vec->move_prev = NULL;
+ raw_spin_lock_irqsave(&lpriv->lock, flags);
+ WRITE_ONCE(vec->irq, irq);
+ raw_spin_unlock_irqrestore(&lpriv->lock, flags);
return vec;
}
+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);
+ WRITE_ONCE(vec->move_next, NULL);
+ vec->retiring = true;
+ 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) != -ENXIO)
+ 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(struct imsic_vector *vec)
{
+ struct imsic_vector *prev = READ_ONCE(vec->move_prev);
+ struct imsic_local_priv *lpriv;
+ unsigned int cpu = vec->cpu;
unsigned long flags;
+ int prev_cpu = -1;
- raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
- vec->irq = 0;
- irq_matrix_free(imsic->matrix, vec->cpu, vec->local_id, false);
- raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
+ /*
+ * 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);
+ if (prev->move_next == vec) {
+ imsic_vector_retire(lpriv, prev);
+ prev_cpu = prev->cpu;
+ }
+ raw_spin_unlock_irqrestore(&lpriv->lock, flags);
+ }
+
+ 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)
@@ -536,8 +654,17 @@ static int __init imsic_local_init(void)
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);
@@ -555,6 +682,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 189829b86..46d8ec92e 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.h
+++ b/drivers/irqchip/irq-riscv-imsic-state.h
@@ -21,7 +21,9 @@ struct imsic_vector {
/* Details saved by driver in the vector */
unsigned int irq;
/* Details accessed using local lock held */
+ u16 move_tmp_id;
bool enable;
+ bool retiring;
struct imsic_vector *move_next;
struct imsic_vector *move_prev;
};
@@ -36,6 +38,7 @@ struct imsic_local_priv {
#ifdef CONFIG_SMP
/* Local timer for synchronization */
struct timer_list timer;
+ bool started;
#endif
/* Local vector table */
@@ -88,8 +91,9 @@ static inline struct imsic_vector *imsic_vector_get_move(struct imsic_vector *ve
return READ_ONCE(vec->move_prev);
}
-void imsic_vector_force_move_cleanup(struct imsic_vector *vec);
-void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_vec);
+void imsic_vector_force_move_cleanup(struct imsic_vector *vec, struct imsic_vector *prev);
+void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_vec,
+ bool move_via_tmp);
struct imsic_vector *imsic_vector_alloc(unsigned int irq, const struct cpumask *mask);
void imsic_vector_free(struct imsic_vector *vector);
base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
--
2.50.1 (Apple Git-155)