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

Xiong Zhang posted 41 patches 2 years ago
[RFC PATCH 05/41] KVM: x86/pmu: Register PMI handler for passthrough PMU
Posted by Xiong Zhang 2 years ago
From: Xiong Zhang <xiong.y.zhang@intel.com>

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@intel.com>
Signed-off-by: Mingwei Zhang <mizhang@google.com>
---
 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
Re: [RFC PATCH 05/41] KVM: x86/pmu: Register PMI handler for passthrough PMU
Posted by Sean Christopherson 1 year, 10 months ago
On Fri, Jan 26, 2024, Xiong Zhang wrote:
> From: Xiong Zhang <xiong.y.zhang@intel.com>
> 
> 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@intel.com>
> Signed-off-by: Mingwei Zhang <mizhang@google.com>
> ---
>  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)

s/pmu/pmi, and this needs a verb.  Maybe kvm_handle_guest_pmi()?  Definitely
open to other names.

> +{
> +	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
> +
> +	if (!vcpu) {
> +		pr_warn_once("%s: no running vcpu found!\n", __func__);

Unless I misunderstand the code, this can/should be a full WARN_ON_ONCE.  If a
PMI skids all the way past vcpu_put(), we've got big problems.
 
> +		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);

Hmm, a few patches late, but the "kvm" scope is weird.  This calls a core x86
function, not a KVM function.

And to reduce exports and copy+paste, what about something like this?

void x86_set_kvm_irq_handler(u8 vector, void (*handler)(void))
{
	if (!handler)
		handler = dummy_handler;

	if (vector == POSTED_INTR_WAKEUP_VECTOR)
		kvm_posted_intr_wakeup_handler = handler;
	else if (vector == KVM_GUEST_PMI_VECTOR)
		kvm_guest_pmi_handler = handler;
	else
		WARN_ON_ONCE(1);

	if (handler == dummy_handler)
		synchronize_rcu();
}
EXPORT_SYMBOL_GPL(x86_set_kvm_irq_handler);
Re: [RFC PATCH 05/41] KVM: x86/pmu: Register PMI handler for passthrough PMU
Posted by Zhang, Xiong Y 1 year, 10 months ago

On 4/12/2024 3:07 AM, Sean Christopherson wrote:
> On Fri, Jan 26, 2024, Xiong Zhang wrote:
>> From: Xiong Zhang <xiong.y.zhang@intel.com>
>>
>> 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@intel.com>
>> Signed-off-by: Mingwei Zhang <mizhang@google.com>
>> ---
>>  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)
> 
> s/pmu/pmi, and this needs a verb.  Maybe kvm_handle_guest_pmi()?  Definitely
> open to other names.
kvm_handle_guest_pmi() is ok. 
> 
>> +{
>> +	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
>> +
>> +	if (!vcpu) {
>> +		pr_warn_once("%s: no running vcpu found!\n", __func__);
> 
> Unless I misunderstand the code, this can/should be a full WARN_ON_ONCE.  If a
> PMI skids all the way past vcpu_put(), we've got big problems.
yes, it is big problems and user should be noticed.
>  
>> +		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);
> 
> Hmm, a few patches late, but the "kvm" scope is weird.  This calls a core x86
> function, not a KVM function.
> 
> And to reduce exports and copy+paste, what about something like this?
> 
> void x86_set_kvm_irq_handler(u8 vector, void (*handler)(void))
> {
> 	if (!handler)
> 		handler = dummy_handler;
> 
> 	if (vector == POSTED_INTR_WAKEUP_VECTOR)
> 		kvm_posted_intr_wakeup_handler = handler;
> 	else if (vector == KVM_GUEST_PMI_VECTOR)
> 		kvm_guest_pmi_handler = handler;
> 	else
> 		WARN_ON_ONCE(1);
> 
> 	if (handler == dummy_handler)
> 		synchronize_rcu();
> }
> EXPORT_SYMBOL_GPL(x86_set_kvm_irq_handler);
Good suggestion. Follow it in next version.