mirror of
https://github.com/raspberrypi/linux.git
synced 2025-12-20 00:31:51 +00:00
During a module removal, kvm_exit invokes arch specific disable call which disables AIA. However, we invoke aia_exit before kvm_exit resulting in the following warning. KVM kernel module can't be inserted afterwards due to inconsistent state of IRQ. [25469.031389] percpu IRQ 31 still enabled on CPU0! [25469.031732] WARNING: CPU: 3 PID: 943 at kernel/irq/manage.c:2476 __free_percpu_irq+0xa2/0x150 [25469.031804] Modules linked in: kvm(-) [25469.031848] CPU: 3 UID: 0 PID: 943 Comm: rmmod Not tainted 6.14.0-rc5-06947-g91c763118f47-dirty #2 [25469.031905] Hardware name: riscv-virtio,qemu (DT) [25469.031928] epc : __free_percpu_irq+0xa2/0x150 [25469.031976] ra : __free_percpu_irq+0xa2/0x150 [25469.032197] epc : ffffffff8007db1e ra : ffffffff8007db1e sp : ff2000000088bd50 [25469.032241] gp : ffffffff8131cef8 tp : ff60000080b96400 t0 : ff2000000088baf8 [25469.032285] t1 : fffffffffffffffc t2 : 5249207570637265 s0 : ff2000000088bd90 [25469.032329] s1 : ff60000098b21080 a0 : 037d527a15eb4f00 a1 : 037d527a15eb4f00 [25469.032372] a2 : 0000000000000023 a3 : 0000000000000001 a4 : ffffffff8122dbf8 [25469.032410] a5 : 0000000000000fff a6 : 0000000000000000 a7 : ffffffff8122dc10 [25469.032448] s2 : ff60000080c22eb0 s3 : 0000000200000022 s4 : 000000000000001f [25469.032488] s5 : ff60000080c22e00 s6 : ffffffff80c351c0 s7 : 0000000000000000 [25469.032582] s8 : 0000000000000003 s9 : 000055556b7fb490 s10: 00007ffff0e12fa0 [25469.032621] s11: 00007ffff0e13e9a t3 : ffffffff81354ac7 t4 : ffffffff81354ac7 [25469.032664] t5 : ffffffff81354ac8 t6 : ffffffff81354ac7 [25469.032698] status: 0000000200000100 badaddr: ffffffff8007db1e cause: 0000000000000003 [25469.032738] [<ffffffff8007db1e>] __free_percpu_irq+0xa2/0x150 [25469.032797] [<ffffffff8007dbfc>] free_percpu_irq+0x30/0x5e [25469.032856] [<ffffffff013a57dc>] kvm_riscv_aia_exit+0x40/0x42 [kvm] [25469.033947] [<ffffffff013b4e82>] cleanup_module+0x10/0x32 [kvm] [25469.035300] [<ffffffff8009b150>] __riscv_sys_delete_module+0x18e/0x1fc [25469.035374] [<ffffffff8000c1ca>] syscall_handler+0x3a/0x46 [25469.035456] [<ffffffff809ec9a4>] do_trap_ecall_u+0x72/0x134 [25469.035536] [<ffffffff809f5e18>] handle_exception+0x148/0x156 Invoke aia_exit and other arch specific cleanup functions after kvm_exit so that disable gets a chance to be called first before exit. Fixes:54e43320c2("RISC-V: KVM: Initial skeletal support for AIA") Fixes:eded6754f3("riscv: KVM: add basic support for host vs guest profiling") Signed-off-by: Atish Patra <atishp@rivosinc.com> Reviewed-by: Anup Patel <anup@brainfault.org> Reviewed-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/20250317-kvm_exit_fix-v1-1-aa5240c5dbd2@rivosinc.com Signed-off-by: Anup Patel <anup@brainfault.org>
180 lines
3.6 KiB
C
180 lines
3.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright (C) 2019 Western Digital Corporation or its affiliates.
|
|
*
|
|
* Authors:
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
*/
|
|
|
|
#include <linux/errno.h>
|
|
#include <linux/err.h>
|
|
#include <linux/module.h>
|
|
#include <linux/kvm_host.h>
|
|
#include <asm/cpufeature.h>
|
|
#include <asm/kvm_nacl.h>
|
|
#include <asm/sbi.h>
|
|
|
|
long kvm_arch_dev_ioctl(struct file *filp,
|
|
unsigned int ioctl, unsigned long arg)
|
|
{
|
|
return -EINVAL;
|
|
}
|
|
|
|
int kvm_arch_enable_virtualization_cpu(void)
|
|
{
|
|
int rc;
|
|
|
|
rc = kvm_riscv_nacl_enable();
|
|
if (rc)
|
|
return rc;
|
|
|
|
csr_write(CSR_HEDELEG, KVM_HEDELEG_DEFAULT);
|
|
csr_write(CSR_HIDELEG, KVM_HIDELEG_DEFAULT);
|
|
|
|
/* VS should access only the time counter directly. Everything else should trap */
|
|
csr_write(CSR_HCOUNTEREN, 0x02);
|
|
|
|
csr_write(CSR_HVIP, 0);
|
|
|
|
kvm_riscv_aia_enable();
|
|
|
|
return 0;
|
|
}
|
|
|
|
void kvm_arch_disable_virtualization_cpu(void)
|
|
{
|
|
kvm_riscv_aia_disable();
|
|
|
|
/*
|
|
* After clearing the hideleg CSR, the host kernel will receive
|
|
* spurious interrupts if hvip CSR has pending interrupts and the
|
|
* corresponding enable bits in vsie CSR are asserted. To avoid it,
|
|
* hvip CSR and vsie CSR must be cleared before clearing hideleg CSR.
|
|
*/
|
|
csr_write(CSR_VSIE, 0);
|
|
csr_write(CSR_HVIP, 0);
|
|
csr_write(CSR_HEDELEG, 0);
|
|
csr_write(CSR_HIDELEG, 0);
|
|
|
|
kvm_riscv_nacl_disable();
|
|
}
|
|
|
|
static void kvm_riscv_teardown(void)
|
|
{
|
|
kvm_riscv_aia_exit();
|
|
kvm_riscv_nacl_exit();
|
|
kvm_unregister_perf_callbacks();
|
|
}
|
|
|
|
static int __init riscv_kvm_init(void)
|
|
{
|
|
int rc;
|
|
char slist[64];
|
|
const char *str;
|
|
|
|
if (!riscv_isa_extension_available(NULL, h)) {
|
|
kvm_info("hypervisor extension not available\n");
|
|
return -ENODEV;
|
|
}
|
|
|
|
if (sbi_spec_is_0_1()) {
|
|
kvm_info("require SBI v0.2 or higher\n");
|
|
return -ENODEV;
|
|
}
|
|
|
|
if (!sbi_probe_extension(SBI_EXT_RFENCE)) {
|
|
kvm_info("require SBI RFENCE extension\n");
|
|
return -ENODEV;
|
|
}
|
|
|
|
rc = kvm_riscv_nacl_init();
|
|
if (rc && rc != -ENODEV)
|
|
return rc;
|
|
|
|
kvm_riscv_gstage_mode_detect();
|
|
|
|
kvm_riscv_gstage_vmid_detect();
|
|
|
|
rc = kvm_riscv_aia_init();
|
|
if (rc && rc != -ENODEV) {
|
|
kvm_riscv_nacl_exit();
|
|
return rc;
|
|
}
|
|
|
|
kvm_info("hypervisor extension available\n");
|
|
|
|
if (kvm_riscv_nacl_available()) {
|
|
rc = 0;
|
|
slist[0] = '\0';
|
|
if (kvm_riscv_nacl_sync_csr_available()) {
|
|
if (rc)
|
|
strcat(slist, ", ");
|
|
strcat(slist, "sync_csr");
|
|
rc++;
|
|
}
|
|
if (kvm_riscv_nacl_sync_hfence_available()) {
|
|
if (rc)
|
|
strcat(slist, ", ");
|
|
strcat(slist, "sync_hfence");
|
|
rc++;
|
|
}
|
|
if (kvm_riscv_nacl_sync_sret_available()) {
|
|
if (rc)
|
|
strcat(slist, ", ");
|
|
strcat(slist, "sync_sret");
|
|
rc++;
|
|
}
|
|
if (kvm_riscv_nacl_autoswap_csr_available()) {
|
|
if (rc)
|
|
strcat(slist, ", ");
|
|
strcat(slist, "autoswap_csr");
|
|
rc++;
|
|
}
|
|
kvm_info("using SBI nested acceleration with %s\n",
|
|
(rc) ? slist : "no features");
|
|
}
|
|
|
|
switch (kvm_riscv_gstage_mode()) {
|
|
case HGATP_MODE_SV32X4:
|
|
str = "Sv32x4";
|
|
break;
|
|
case HGATP_MODE_SV39X4:
|
|
str = "Sv39x4";
|
|
break;
|
|
case HGATP_MODE_SV48X4:
|
|
str = "Sv48x4";
|
|
break;
|
|
case HGATP_MODE_SV57X4:
|
|
str = "Sv57x4";
|
|
break;
|
|
default:
|
|
return -ENODEV;
|
|
}
|
|
kvm_info("using %s G-stage page table format\n", str);
|
|
|
|
kvm_info("VMID %ld bits available\n", kvm_riscv_gstage_vmid_bits());
|
|
|
|
if (kvm_riscv_aia_available())
|
|
kvm_info("AIA available with %d guest external interrupts\n",
|
|
kvm_riscv_aia_nr_hgei);
|
|
|
|
kvm_register_perf_callbacks(NULL);
|
|
|
|
rc = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
|
|
if (rc) {
|
|
kvm_riscv_teardown();
|
|
return rc;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
module_init(riscv_kvm_init);
|
|
|
|
static void __exit riscv_kvm_exit(void)
|
|
{
|
|
kvm_exit();
|
|
|
|
kvm_riscv_teardown();
|
|
}
|
|
module_exit(riscv_kvm_exit);
|