Re: [PATCH v2 01/31] KVM: x86: hyper-v: Resurrect dedicated KVM_REQ_HV_TLB_FLUSH flag

From: Sean Christopherson
Date: Thu Apr 07 2022 - 14:02:49 EST


On Thu, Apr 07, 2022, Vitaly Kuznetsov wrote:
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index e9647614dc8c..3c54f6804b7b 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3341,7 +3341,12 @@ void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu)
> if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
> kvm_vcpu_flush_tlb_current(vcpu);
>
> - if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu))
> + if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) {
> + kvm_vcpu_flush_tlb_guest(vcpu);
> + kvm_clear_request(KVM_REQ_HV_TLB_FLUSH, vcpu);
> + }
> +
> + if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
> kvm_vcpu_flush_tlb_guest(vcpu);

It'd be slightly more performant to do:

if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) {
kvm_vcpu_flush_tlb_guest(vcpu);
kvm_clear_request(KVM_REQ_HV_TLB_FLUSH, vcpu);
} else if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) {
kvm_hv_vcpu_flush_tlb(vcpu);
}

And then when the code becomes


if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) {
kvm_vcpu_flush_tlb_guest(vcpu);
if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
kvm_hv_vcpu_empty_flush_tlb(vcpu);
} else if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) {
kvm_hv_vcpu_flush_tlb(vcpu);
}

the elif will help unsuspecting readers see that the HV_TLB_FLUSH request is
cleared by kvm_check_request() in the TLB_FLUSH_GUEST path.

The elif could result in having to bail from VM-Entry if the request becomes
pending after the check/clear inside TLB_FLUSH_GUEST, but that should be a very
rare case.

> }
> EXPORT_SYMBOL_GPL(kvm_service_local_tlb_flush_requests);
> --
> 2.35.1
>