[RFC PATCH 05/41] KVM: x86/pmu: Register PMI handler for passthrough PMU

From: Xiong Zhang
Date: Fri Jan 26 2024 - 04:31:41 EST


From: Xiong Zhang <xiong.y.zhang@xxxxxxxxx>

Add function to register/unregister PMI handler at KVM module
initialization and destroy time. This allows the host PMU with passthough
capability enabled switch PMI handler at PMU context switch time.

Signed-off-by: Xiong Zhang <xiong.y.zhang@xxxxxxxxx>
Signed-off-by: Mingwei Zhang <mizhang@xxxxxxxxxx>
---
arch/x86/kvm/x86.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2c924075f6f1..4432e736129f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -10611,6 +10611,18 @@ void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
}
EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);

+void kvm_passthrough_pmu_handler(void)
+{
+ struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+
+ if (!vcpu) {
+ pr_warn_once("%s: no running vcpu found!\n", __func__);
+ return;
+ }
+
+ kvm_make_request(KVM_REQ_PMI, vcpu);
+}
+
/*
* Called within kvm->srcu read side.
* Returns 1 to let vcpu_run() continue the guest execution loop without
@@ -13815,6 +13827,7 @@ static int __init kvm_x86_init(void)
{
kvm_mmu_x86_module_init();
mitigate_smt_rsb &= boot_cpu_has_bug(X86_BUG_SMT_RSB) && cpu_smt_possible();
+ kvm_set_vpmu_handler(kvm_passthrough_pmu_handler);
return 0;
}
module_init(kvm_x86_init);
@@ -13825,5 +13838,6 @@ static void __exit kvm_x86_exit(void)
* If module_init() is implemented, module_exit() must also be
* implemented to allow module unload.
*/
+ kvm_set_vpmu_handler(NULL);
}
module_exit(kvm_x86_exit);
--
2.34.1