Skip to content

Commit 873f10c

Browse files
fangyu0809avpatel
authored andcommitted
RISC-V: KVM: Read HGEIP CSR on the correct cpu
When executing kvm_riscv_vcpu_aia_has_interrupts, the vCPU may have migrated and the IMSIC VS-file have not been updated yet, currently the HGEIP CSR should be read from the imsic->vsfile_cpu ( the pCPU before migration ) via on_each_cpu_mask, but this will trigger an IPI call and repeated IPI within a period of time is expensive in a many-core systems. Just let the vCPU execute and update the correct IMSIC VS-file via kvm_riscv_vcpu_aia_imsic_update may be a simple solution. Fixes: 4cec89d ("RISC-V: KVM: Move HGEI[E|P] CSR access to IMSIC virtualization") Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com> Reviewed-by: Guo Ren <guoren@kernel.org> Reviewed-by: Anup Patel <anup@brainfault.org> Tested-by: Anup Patel <anup@brainfault.org> Link: https://lore.kernel.org/r/20251016012659.82998-1-fangyu.yu@linux.alibaba.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent ea138a6 commit 873f10c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

arch/riscv/kvm/aia_imsic.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,20 @@ bool kvm_riscv_vcpu_aia_imsic_has_interrupt(struct kvm_vcpu *vcpu)
689689
*/
690690

691691
read_lock_irqsave(&imsic->vsfile_lock, flags);
692-
if (imsic->vsfile_cpu > -1)
693-
ret = !!(csr_read(CSR_HGEIP) & BIT(imsic->vsfile_hgei));
692+
if (imsic->vsfile_cpu > -1) {
693+
/*
694+
* This function is typically called from kvm_vcpu_block() via
695+
* kvm_arch_vcpu_runnable() upon WFI trap. The kvm_vcpu_block()
696+
* can be preempted and the blocking VCPU might resume on a
697+
* different CPU. This means it is possible that current CPU
698+
* does not match the imsic->vsfile_cpu hence this function
699+
* must check imsic->vsfile_cpu before accessing HGEIP CSR.
700+
*/
701+
if (imsic->vsfile_cpu != vcpu->cpu)
702+
ret = true;
703+
else
704+
ret = !!(csr_read(CSR_HGEIP) & BIT(imsic->vsfile_hgei));
705+
}
694706
read_unlock_irqrestore(&imsic->vsfile_lock, flags);
695707

696708
return ret;

0 commit comments

Comments
 (0)