RISC-V: KVM: Move HGEI[E|P] CSR access to IMSIC virtualization
authorAnup Patel <apatel@ventanamicro.com>
Mon, 7 Jul 2025 03:53:44 +0000 (09:23 +0530)
committerAnup Patel <anup@brainfault.org>
Fri, 11 Jul 2025 13:03:27 +0000 (18:33 +0530)
commit4cec89db80ba81fa4524c6449c0494b8ae08eeb0
tree9448c55a27133375d90f08d745a5d677fa36b4bb
parent57f576e860d3a3582edb5064db9f0c95558ca5f4
RISC-V: KVM: Move HGEI[E|P] CSR access to IMSIC virtualization

Currently, the common AIA functions kvm_riscv_vcpu_aia_has_interrupts()
and kvm_riscv_aia_wakeon_hgei() lookup HGEI line using an array of VCPU
pointers before accessing HGEI[E|P] CSR which is slow and prone to race
conditions because there is a separate per-hart lock for the VCPU pointer
array and a separate per-VCPU rwlock for IMSIC VS-file (including HGEI
line) used by the VCPU. Due to these race conditions, it is observed
on QEMU RISC-V host that Guest VCPUs sleep in WFI and never wakeup even
with interrupt pending in the IMSIC VS-file because VCPUs were waiting
for HGEI wakeup on the wrong host CPU.

The IMSIC virtualization already keeps track of the HGEI line and the
associated IMSIC VS-file used by each VCPU so move the HGEI[E|P] CSR
access to IMSIC virtualization so that costly HGEI line lookup can be
avoided and likelihood of race-conditions when updating HGEI[E|P] CSR
is also reduced.

Reviewed-by: Atish Patra <atishp@rivosinc.com>
Tested-by: Atish Patra <atishp@rivosinc.com>
Tested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Fixes: 3385339296d1 ("RISC-V: KVM: Use IMSIC guest files when available")
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Nutty Liu <liujingqi@lanxincomputing.com>
Link: https://lore.kernel.org/r/20250707035345.17494-3-apatel@ventanamicro.com
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/include/asm/kvm_aia.h
arch/riscv/include/asm/kvm_host.h
arch/riscv/kvm/aia.c
arch/riscv/kvm/aia_imsic.c
arch/riscv/kvm/vcpu.c