[PATCH v2 5/5] irqchip/riscv-imsic: Use the recorded temporary MSI ID at completion

From: Zhanpeng Zhang

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


The IRQ core can replace the IRQ chip before entering the domain
free callback. If the old CPU finishes a move in that interval, the
chip flags no longer describe how the device's MSI was programmed.
The old CPU can consequently leave the temporary ID's pending bit
set and a later owner of an otherwise free ID can receive it.

Use the temporary ID recorded before programming the device instead
of looking up the IRQ descriptor. If another IRQ or another move is
using that ID, sample its pending bit without clearing it. Otherwise
consume the pending bit before releasing the old vector.

Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
drivers/irqchip/irq-riscv-imsic-state.c | 49 +++++++++++++++----------
1 file changed, 29 insertions(+), 20 deletions(-)

diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
index 840e83fde..d4db4a481 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.c
+++ b/drivers/irqchip/irq-riscv-imsic-state.c
@@ -65,7 +65,7 @@ const struct imsic_global_config *imsic_get_global_config(void)
}
EXPORT_SYMBOL_GPL(imsic_get_global_config);

-static bool __imsic_eix_read_clear(unsigned long id, bool pend)
+static bool __imsic_eix_read(unsigned long id, bool pend, bool clear)
{
unsigned long isel, imask;

@@ -74,17 +74,20 @@ static bool __imsic_eix_read_clear(unsigned long id, bool pend)
isel += pend ? IMSIC_EIP0 : IMSIC_EIE0;
imask = BIT(id & (__riscv_xlen - 1));

- return !!(imsic_csr_read_clear(isel, imask) & imask);
+ if (clear)
+ return !!(imsic_csr_read_clear(isel, imask) & imask);
+
+ return !!(imsic_csr_read(isel) & imask);
}

static inline bool __imsic_id_read_clear_enabled(unsigned long id)
{
- return __imsic_eix_read_clear(id, false);
+ return __imsic_eix_read(id, false, true);
}

static inline bool __imsic_id_read_clear_pending(unsigned long id)
{
- return __imsic_eix_read_clear(id, true);
+ return __imsic_eix_read(id, true, true);
}

void __imsic_eix_update(unsigned long base_id, unsigned long num_id, bool pend, bool val)
@@ -147,10 +150,29 @@ static bool imsic_tmp_id_shared(struct imsic_local_priv *lpriv,
return false;
}

+static void imsic_forward_pending_tmp_msi(struct imsic_local_priv *lpriv,
+ struct imsic_vector *src,
+ struct imsic_vector *dst)
+{
+ struct imsic_local_config *local;
+ u16 tmp_id = READ_ONCE(src->move_tmp_id);
+
+ if (!tmp_id || !__imsic_eix_read(tmp_id, true, false))
+ return;
+
+ /* Leave a shared pending bit set for its other users. */
+ if (!imsic_tmp_id_shared(lpriv, src, tmp_id) &&
+ !__imsic_id_read_clear_pending(tmp_id))
+ return;
+
+ local = per_cpu_ptr(imsic->global.local, dst->cpu);
+ writel_relaxed(dst->local_id, local->msi_va);
+}
+
static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
{
- struct imsic_local_config *tlocal, *mlocal;
- struct imsic_vector *vec, *tvec, *mvec;
+ struct imsic_local_config *mlocal;
+ struct imsic_vector *vec, *mvec;
bool ret = true;
int i;

@@ -218,20 +240,7 @@ 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)) {
- /* Retrigger temporary vector if it was already in-use */
- if (READ_ONCE(tvec->enable)) {
- tlocal = per_cpu_ptr(imsic->global.local, tvec->cpu);
- writel_relaxed(tvec->local_id, tlocal->msi_va);
- }
-
- mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);
- writel_relaxed(mvec->local_id, mlocal->msi_va);
- }
+ imsic_forward_pending_tmp_msi(lpriv, vec, mvec);

if (__imsic_id_read_clear_pending(vec->local_id)) {
mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);