[PATCH 3/5] KVM: x86/xen: Consult kvm_xen_enabled when checking for Xen MSR writes

Sean Christopherson posted 5 patches 1 year ago
There is a newer version of this series
[PATCH 3/5] KVM: x86/xen: Consult kvm_xen_enabled when checking for Xen MSR writes
Posted by Sean Christopherson 1 year ago
Query kvm_xen_enabled when detecting writes to the Xen hypercall page MSR
so that the check is optimized away in the likely scenario that Xen isn't
enabled for the VM.

Deliberately open code the check instead of using kvm_xen_msr_enabled() in
order to avoid a double load of xen_hvm_config.msr (which is admittedly
rather pointless given the widespread lack of READ_ONCE() usage on the
plethora of vCPU-scoped accesses to kvm->arch.xen state).

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/xen.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h
index e92e06926f76..1e3a913dfb94 100644
--- a/arch/x86/kvm/xen.h
+++ b/arch/x86/kvm/xen.h
@@ -58,6 +58,9 @@ static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
 
 static inline bool kvm_xen_is_hypercall_page_msr(struct kvm *kvm, u32 msr)
 {
+	if (!static_branch_unlikely(&kvm_xen_enabled.key))
+		return false;
+
 	return msr && msr == kvm->arch.xen_hvm_config.msr;
 }
 
-- 
2.48.1.362.g079036d154-goog
Re: [PATCH 3/5] KVM: x86/xen: Consult kvm_xen_enabled when checking for Xen MSR writes
Posted by David Woodhouse 1 year ago
On Fri, 2025-01-31 at 17:13 -0800, Sean Christopherson wrote:
> Query kvm_xen_enabled when detecting writes to the Xen hypercall page MSR
> so that the check is optimized away in the likely scenario that Xen isn't
> enabled for the VM.
> 
> Deliberately open code the check instead of using kvm_xen_msr_enabled() in
> order to avoid a double load of xen_hvm_config.msr (which is admittedly
> rather pointless given the widespread lack of READ_ONCE() usage on the
> plethora of vCPU-scoped accesses to kvm->arch.xen state).
> 
> No functional change intended.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>

Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>

Re: [PATCH 3/5] KVM: x86/xen: Consult kvm_xen_enabled when checking for Xen MSR writes
Posted by Paul Durrant 1 year ago
On 01/02/2025 01:13, Sean Christopherson wrote:
> Query kvm_xen_enabled when detecting writes to the Xen hypercall page MSR
> so that the check is optimized away in the likely scenario that Xen isn't
> enabled for the VM.
> 
> Deliberately open code the check instead of using kvm_xen_msr_enabled() in
> order to avoid a double load of xen_hvm_config.msr (which is admittedly
> rather pointless given the widespread lack of READ_ONCE() usage on the
> plethora of vCPU-scoped accesses to kvm->arch.xen state).
> 
> No functional change intended.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>   arch/x86/kvm/xen.h | 3 +++
>   1 file changed, 3 insertions(+)
> 

Reviewed-by: Paul Durrant <paul@xen.org>