All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Ankur Arora <ankur.a.arora@oracle.com>,
	Joao Martins <joao.m.martins@oracle.com>,
	Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	Sean Christopherson <seanjc@google.com>,
	graf@amazon.com, iaslan@amazon.de, pdurrant@amazon.com,
	aagch@amazon.com, fandree@amazon.com, hch@infradead.org
Subject: [PATCH v6 06/19] KVM: x86/xen: Add kvm_xen_enabled static key
Date: Wed,  3 Feb 2021 15:01:01 +0000	[thread overview]
Message-ID: <20210203150114.920335-7-dwmw2@infradead.org> (raw)
In-Reply-To: <20210203150114.920335-1-dwmw2@infradead.org>

From: David Woodhouse <dwmw@amazon.co.uk>

The code paths for Xen support are all fairly lightweight but if we hide
them behind this, they're even *more* lightweight for any system which
isn't actually hosting Xen guests.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
 arch/x86/kvm/x86.c |  2 ++
 arch/x86/kvm/xen.c | 17 +++++++++++++++++
 arch/x86/kvm/xen.h | 10 ++++++++--
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a74ae5f70bdc..d98e08faea23 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7953,6 +7953,7 @@ void kvm_arch_exit(void)
 	kvm_mmu_module_exit();
 	free_percpu(user_return_msrs);
 	kmem_cache_destroy(x86_fpu_cache);
+	WARN_ON(static_branch_unlikely(&kvm_xen_enabled.key));
 }
 
 static int __kvm_vcpu_halt(struct kvm_vcpu *vcpu, int state, int reason)
@@ -10529,6 +10530,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
 	kvm_mmu_uninit_vm(kvm);
 	kvm_page_track_cleanup(kvm);
+	kvm_xen_destroy_vm(kvm);
 	kvm_hv_destroy_vm(kvm);
 }
 
diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index b52549fc6dbc..7d03d918e595 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -16,6 +16,8 @@
 
 #include "trace.h"
 
+DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ);
+
 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
 {
 	struct kvm *kvm = vcpu->kvm;
@@ -93,10 +95,25 @@ int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
 	     xhc->blob_size_32 || xhc->blob_size_64))
 		return -EINVAL;
 
+	mutex_lock(&kvm->lock);
+
+	if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
+		static_branch_inc(&kvm_xen_enabled.key);
+	else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
+		static_branch_slow_dec_deferred(&kvm_xen_enabled);
+
 	memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
+
+	mutex_unlock(&kvm->lock);
 	return 0;
 }
 
+void kvm_xen_destroy_vm(struct kvm *kvm)
+{
+	if (kvm->arch.xen_hvm_config.msr)
+		static_branch_slow_dec_deferred(&kvm_xen_enabled);
+}
+
 static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
 {
 	kvm_rax_write(vcpu, result);
diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h
index 28e9c9892628..ec3d8f6d0ef5 100644
--- a/arch/x86/kvm/xen.h
+++ b/arch/x86/kvm/xen.h
@@ -9,14 +9,20 @@
 #ifndef __ARCH_X86_KVM_XEN_H__
 #define __ARCH_X86_KVM_XEN_H__
 
+#include <linux/jump_label_ratelimit.h>
+
+extern struct static_key_false_deferred kvm_xen_enabled;
+
 int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data);
 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc);
+void kvm_xen_destroy_vm(struct kvm *kvm);
 
 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
 {
-	return kvm->arch.xen_hvm_config.flags &
-		KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL;
+	return static_branch_unlikely(&kvm_xen_enabled.key) &&
+		(kvm->arch.xen_hvm_config.flags &
+		 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
 }
 
 #endif /* __ARCH_X86_KVM_XEN_H__ */
-- 
2.29.2


  parent reply	other threads:[~2021-02-03 15:02 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-03 15:00 [PATCH v6 00/19] KVM: Add minimal support for Xen HVM guests David Woodhouse
2021-02-03 15:00 ` [PATCH v6 01/19] KVM: x86/xen: fix Xen hypercall page msr handling David Woodhouse
2021-02-03 15:00 ` [PATCH v6 02/19] KVM: x86/xen: Fix __user pointer handling for hypercall page installation David Woodhouse
2021-02-03 15:00 ` [PATCH v6 03/19] KVM: x86/xen: intercept xen hypercalls if enabled David Woodhouse
2021-02-05 15:30   ` kernel test robot
2021-02-03 15:00 ` [PATCH v6 04/19] KVM: x86/xen: Fix coexistence of Xen and Hyper-V hypercalls David Woodhouse
2021-02-03 15:01 ` [PATCH v6 05/19] KVM: x86/xen: Move KVM_XEN_HVM_CONFIG handling to xen.c David Woodhouse
2021-02-03 15:01 ` David Woodhouse [this message]
2021-02-03 15:01 ` [PATCH v6 07/19] KVM: x86/xen: add KVM_XEN_HVM_SET_ATTR/KVM_XEN_HVM_GET_ATTR David Woodhouse
2021-02-03 15:01 ` [PATCH v6 08/19] KVM: x86/xen: latch long_mode when hypercall page is set up David Woodhouse
2021-02-03 15:01 ` [PATCH v6 09/19] KVM: x86/xen: add definitions of compat_shared_info, compat_vcpu_info David Woodhouse
2021-02-03 15:01 ` [PATCH v6 10/19] KVM: x86/xen: register shared_info page David Woodhouse
2021-02-03 15:01 ` [PATCH v6 11/19] xen: add wc_sec_hi to struct shared_info David Woodhouse
2021-02-03 15:01 ` [PATCH v6 12/19] KVM: x86/xen: update wallclock region David Woodhouse
2021-02-03 15:01 ` [PATCH v6 13/19] KVM: x86/xen: Add KVM_XEN_VCPU_SET_ATTR/KVM_XEN_VCPU_GET_ATTR David Woodhouse
2021-02-03 15:01 ` [PATCH v6 14/19] KVM: x86/xen: register vcpu info David Woodhouse
2021-02-03 15:01 ` [PATCH v6 15/19] KVM: x86/xen: setup pvclock updates David Woodhouse
2021-02-03 15:01 ` [PATCH v6 16/19] KVM: x86/xen: register vcpu time info region David Woodhouse
2021-02-03 15:01 ` [PATCH v6 17/19] KVM: x86/xen: Add event channel interrupt vector upcall David Woodhouse
2021-02-03 15:01 ` [PATCH v6 18/19] KVM: x86: declare Xen HVM shared info capability and add test case David Woodhouse
2021-02-03 16:38   ` Paolo Bonzini
2021-02-03 17:55     ` David Woodhouse
2021-02-03 15:01 ` [PATCH v6 19/19] KVM: Add documentation for Xen hypercall and shared_info updates David Woodhouse
2021-02-03 16:17   ` Paolo Bonzini
2021-02-03 16:59 ` [PATCH v6 00/19] KVM: Add minimal support for Xen HVM guests Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210203150114.920335-7-dwmw2@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=aagch@amazon.com \
    --cc=ankur.a.arora@oracle.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=fandree@amazon.com \
    --cc=graf@amazon.com \
    --cc=hch@infradead.org \
    --cc=iaslan@amazon.de \
    --cc=joao.m.martins@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=pdurrant@amazon.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.