kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v6 0/2] KVM: x86/xen: update Xen CPUID Leaf 4
@ 2022-12-20 13:40 Paul Durrant
  2022-12-20 13:40 ` [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit Paul Durrant
  2022-12-20 13:40 ` [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Paul Durrant
  0 siblings, 2 replies; 13+ messages in thread
From: Paul Durrant @ 2022-12-20 13:40 UTC (permalink / raw)
  To: x86, kvm, linux-kernel
  Cc: Paul Durrant, Borislav Petkov, Dave Hansen, David Woodhouse,
	H. Peter Anvin, Ingo Molnar, Paolo Bonzini, Sean Christopherson,
	Thomas Gleixner

Patch #2 was the original patch. It has been expended to a series in v6.

Paul Durrant (2):
  KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also
    capture limit
  KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if
    present

 arch/x86/include/asm/kvm_host.h |  8 +++++++-
 arch/x86/kvm/cpuid.c            | 17 ++++++++++-------
 arch/x86/kvm/x86.c              |  1 +
 arch/x86/kvm/xen.c              | 26 ++++++++++++++++++++++++++
 arch/x86/kvm/xen.h              |  7 +++++++
 5 files changed, 51 insertions(+), 8 deletions(-)
---
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
-- 
2.20.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit
  2022-12-20 13:40 [PATCH v6 0/2] KVM: x86/xen: update Xen CPUID Leaf 4 Paul Durrant
@ 2022-12-20 13:40 ` Paul Durrant
  2023-01-03 16:20   ` David Woodhouse
  2023-01-04 19:34   ` Sean Christopherson
  2022-12-20 13:40 ` [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Paul Durrant
  1 sibling, 2 replies; 13+ messages in thread
From: Paul Durrant @ 2022-12-20 13:40 UTC (permalink / raw)
  To: x86, kvm, linux-kernel
  Cc: Paul Durrant, Sean Christopherson, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	David Woodhouse

A sunsequent patch will need to acquire the CPUID leaf range for emulated
Xen so explicitly pass the signature of the hypervisor we're interested in
to the new function. Also introduce a new kvm_hypervisor_cpuid structure
so we can neatly store both the base and limit leaf indices.

Signed-off-by: Paul Durrant <pdurrant@amazon.com>
---
Cc: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: David Woodhouse <dwmw2@infradead.org>

v6:
 - New in this version
---
 arch/x86/include/asm/kvm_host.h |  7 ++++++-
 arch/x86/kvm/cpuid.c            | 15 ++++++++-------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index f35f1ff4427b..ff201ad35551 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -710,6 +710,11 @@ struct kvm_queued_exception {
 	bool has_payload;
 };
 
+struct kvm_hypervisor_cpuid {
+	u32 base;
+	u32 limit;
+};
+
 struct kvm_vcpu_arch {
 	/*
 	 * rip and regs accesses must go through
@@ -826,7 +831,7 @@ struct kvm_vcpu_arch {
 
 	int cpuid_nent;
 	struct kvm_cpuid_entry2 *cpuid_entries;
-	u32 kvm_cpuid_base;
+	struct kvm_hypervisor_cpuid kvm_cpuid;
 
 	u64 reserved_gpa_bits;
 	int maxphyaddr;
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 0b5bf013fcb8..2468720f8d84 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -180,12 +180,13 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2
 	return 0;
 }
 
-static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
+static void kvm_update_hypervisor_cpuid(struct kvm_vcpu *vcpu, const char *hypervisor_signature,
+					struct kvm_hypervisor_cpuid *hypervisor_cpuid)
 {
 	u32 function;
 	struct kvm_cpuid_entry2 *entry;
 
-	vcpu->arch.kvm_cpuid_base = 0;
+	memset(hypervisor_cpuid, 0, sizeof(*hypervisor_cpuid));
 
 	for_each_possible_hypervisor_cpuid_base(function) {
 		entry = kvm_find_cpuid_entry(vcpu, function);
@@ -197,9 +198,9 @@ static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
 			signature[1] = entry->ecx;
 			signature[2] = entry->edx;
 
-			BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
-			if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
-				vcpu->arch.kvm_cpuid_base = function;
+			if (!memcmp(signature, hypervisor_signature, sizeof(signature))) {
+				hypervisor_cpuid->base = function;
+				hypervisor_cpuid->limit = entry->eax;
 				break;
 			}
 		}
@@ -209,7 +210,7 @@ static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
 static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu,
 					      struct kvm_cpuid_entry2 *entries, int nent)
 {
-	u32 base = vcpu->arch.kvm_cpuid_base;
+	u32 base = vcpu->arch.kvm_cpuid.base;
 
 	if (!base)
 		return NULL;
@@ -439,7 +440,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
 	vcpu->arch.cpuid_entries = e2;
 	vcpu->arch.cpuid_nent = nent;
 
-	kvm_update_kvm_cpuid_base(vcpu);
+	kvm_update_hypervisor_cpuid(vcpu, KVM_SIGNATURE, &vcpu->arch.kvm_cpuid);
 	kvm_vcpu_after_set_cpuid(vcpu);
 
 	return 0;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present
  2022-12-20 13:40 [PATCH v6 0/2] KVM: x86/xen: update Xen CPUID Leaf 4 Paul Durrant
  2022-12-20 13:40 ` [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit Paul Durrant
@ 2022-12-20 13:40 ` Paul Durrant
  2023-01-03 16:20   ` David Woodhouse
  2023-01-04 19:40   ` Sean Christopherson
  1 sibling, 2 replies; 13+ messages in thread
From: Paul Durrant @ 2022-12-20 13:40 UTC (permalink / raw)
  To: x86, kvm, linux-kernel
  Cc: Paul Durrant, Sean Christopherson, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, David Woodhouse

The scaling information in subleaf 1 should match the values set by KVM in
the 'vcpu_info' sub-structure 'time_info' (a.k.a. pvclock_vcpu_time_info)
which is shared with the guest, but is not directly available to the VMM.
The offset values are not set since a TSC offset is already applied.
The TSC frequency should also be set in sub-leaf 2.

Signed-off-by: Paul Durrant <pdurrant@amazon.com>
---
Cc: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: David Woodhouse <dwmw2@infradead.org>

v6:
 - Stash Xen cpuid base and limit values when cpuid is set
 - Re-name kvm_xen_setup_tsc_info() to kvm_xen_update_tsc_info()

v5:
 - Drop the caching of the CPUID entry pointers and only update the
   sub-leaves if the CPU frequency has actually changed

v4:
 - Update commit comment

v3:
 - Add leaf limit check in kvm_xen_set_cpuid()

v2:
 - Make sure sub-leaf pointers are NULLed if the time leaf is removed
---
 arch/x86/include/asm/kvm_host.h | 11 ++++++-----
 arch/x86/kvm/cpuid.c            |  2 ++
 arch/x86/kvm/x86.c              |  1 +
 arch/x86/kvm/xen.c              | 26 ++++++++++++++++++++++++++
 arch/x86/kvm/xen.h              |  7 +++++++
 5 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index ff201ad35551..44329594bdf7 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -678,6 +678,11 @@ struct kvm_vcpu_hv {
 	} nested;
 };
 
+struct kvm_hypervisor_cpuid {
+	u32 base;
+	u32 limit;
+};
+
 /* Xen HVM per vcpu emulation context */
 struct kvm_vcpu_xen {
 	u64 hypercall_rip;
@@ -698,6 +703,7 @@ struct kvm_vcpu_xen {
 	struct hrtimer timer;
 	int poll_evtchn;
 	struct timer_list poll_timer;
+	struct kvm_hypervisor_cpuid cpuid;
 };
 
 struct kvm_queued_exception {
@@ -710,11 +716,6 @@ struct kvm_queued_exception {
 	bool has_payload;
 };
 
-struct kvm_hypervisor_cpuid {
-	u32 base;
-	u32 limit;
-};
-
 struct kvm_vcpu_arch {
 	/*
 	 * rip and regs accesses must go through
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 2468720f8d84..e661413ddf8d 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -25,6 +25,7 @@
 #include "mmu.h"
 #include "trace.h"
 #include "pmu.h"
+#include "xen.h"
 
 /*
  * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
@@ -441,6 +442,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
 	vcpu->arch.cpuid_nent = nent;
 
 	kvm_update_hypervisor_cpuid(vcpu, KVM_SIGNATURE, &vcpu->arch.kvm_cpuid);
+	kvm_update_hypervisor_cpuid(vcpu, XEN_SIGNATURE, &vcpu->arch.xen.cpuid);
 	kvm_vcpu_after_set_cpuid(vcpu);
 
 	return 0;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index fd6c01a39312..60acc55f0c00 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3158,6 +3158,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
 				   &vcpu->hv_clock.tsc_shift,
 				   &vcpu->hv_clock.tsc_to_system_mul);
 		vcpu->hw_tsc_khz = tgt_tsc_khz;
+		kvm_xen_update_tsc_info(v);
 	}
 
 	vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index d7af40240248..46297521791c 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -22,6 +22,9 @@
 #include <xen/interface/event_channel.h>
 #include <xen/interface/sched.h>
 
+#include <asm/xen/cpuid.h>
+
+#include "cpuid.h"
 #include "trace.h"
 
 static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm);
@@ -2061,6 +2064,29 @@ void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
 	del_timer_sync(&vcpu->arch.xen.poll_timer);
 }
 
+void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *entry;
+	u32 function;
+
+	if (!vcpu->arch.xen.cpuid.base)
+		return;
+
+	function = vcpu->arch.xen.cpuid.base | XEN_CPUID_LEAF(3);
+	if (function > vcpu->arch.xen.cpuid.limit)
+		return;
+
+	entry = kvm_find_cpuid_entry_index(vcpu, function, 1);
+	if (entry) {
+		entry->ecx = vcpu->arch.hv_clock.tsc_to_system_mul;
+		entry->edx = vcpu->arch.hv_clock.tsc_shift;
+	}
+
+	entry = kvm_find_cpuid_entry_index(vcpu, function, 2);
+	if (entry)
+		entry->eax = vcpu->arch.hw_tsc_khz;
+}
+
 void kvm_xen_init_vm(struct kvm *kvm)
 {
 	idr_init(&kvm->arch.xen.evtchn_ports);
diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h
index ea33d80a0c51..88dd085e10f8 100644
--- a/arch/x86/kvm/xen.h
+++ b/arch/x86/kvm/xen.h
@@ -32,6 +32,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe,
 int kvm_xen_setup_evtchn(struct kvm *kvm,
 			 struct kvm_kernel_irq_routing_entry *e,
 			 const struct kvm_irq_routing_entry *ue);
+void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu);
 
 static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
 {
@@ -135,6 +136,10 @@ static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
 {
 	return false;
 }
+
+static inline void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
+{
+}
 #endif
 
 int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
@@ -143,6 +148,8 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
 #include <asm/xen/interface.h>
 #include <xen/interface/vcpu.h>
 
+#define XEN_SIGNATURE "XenVMMXenVMM"
+
 void kvm_xen_update_runstate(struct kvm_vcpu *vcpu, int state);
 
 static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit
  2022-12-20 13:40 ` [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit Paul Durrant
@ 2023-01-03 16:20   ` David Woodhouse
  2023-01-04 19:34   ` Sean Christopherson
  1 sibling, 0 replies; 13+ messages in thread
From: David Woodhouse @ 2023-01-03 16:20 UTC (permalink / raw)
  To: Paul Durrant, x86, kvm, linux-kernel
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen

[-- Attachment #1: Type: text/plain, Size: 4729 bytes --]

On Tue, 2022-12-20 at 13:40 +0000, Paul Durrant wrote:
> A sunsequent patch will need to acquire the CPUID leaf range for emulated
> Xen so explicitly pass the signature of the hypervisor we're interested in
> to the new function. Also introduce a new kvm_hypervisor_cpuid structure
> so we can neatly store both the base and limit leaf indices.
> 
> Signed-off-by: Paul Durrant <pdurrant@amazon.com>
> ---

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

> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: David Woodhouse <dwmw2@infradead.org>
> 
> v6:
>  - New in this version
> ---
>  arch/x86/include/asm/kvm_host.h |  7 ++++++-
>  arch/x86/kvm/cpuid.c            | 15 ++++++++-------
>  2 files changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index f35f1ff4427b..ff201ad35551 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -710,6 +710,11 @@ struct kvm_queued_exception {
>         bool has_payload;
>  };
>  
> +struct kvm_hypervisor_cpuid {
> +       u32 base;
> +       u32 limit;
> +};
> +
>  struct kvm_vcpu_arch {
>         /*
>          * rip and regs accesses must go through
> @@ -826,7 +831,7 @@ struct kvm_vcpu_arch {
>  
>         int cpuid_nent;
>         struct kvm_cpuid_entry2 *cpuid_entries;
> -       u32 kvm_cpuid_base;
> +       struct kvm_hypervisor_cpuid kvm_cpuid;
>  
>         u64 reserved_gpa_bits;
>         int maxphyaddr;
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 0b5bf013fcb8..2468720f8d84 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -180,12 +180,13 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2
>         return 0;
>  }
>  
> -static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
> +static void kvm_update_hypervisor_cpuid(struct kvm_vcpu *vcpu, const char *hypervisor_signature,
> +                                       struct kvm_hypervisor_cpuid *hypervisor_cpuid)
>  {
>         u32 function;
>         struct kvm_cpuid_entry2 *entry;
>  
> -       vcpu->arch.kvm_cpuid_base = 0;
> +       memset(hypervisor_cpuid, 0, sizeof(*hypervisor_cpuid));
>  
>         for_each_possible_hypervisor_cpuid_base(function) {
>                 entry = kvm_find_cpuid_entry(vcpu, function);
> @@ -197,9 +198,9 @@ static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
>                         signature[1] = entry->ecx;
>                         signature[2] = entry->edx;
>  
> -                       BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
> -                       if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
> -                               vcpu->arch.kvm_cpuid_base = function;
> +                       if (!memcmp(signature, hypervisor_signature, sizeof(signature))) {
> +                               hypervisor_cpuid->base = function;
> +                               hypervisor_cpuid->limit = entry->eax;
>                                 break;
>                         }
>                 }
> @@ -209,7 +210,7 @@ static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
>  static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu,
>                                               struct kvm_cpuid_entry2 *entries, int nent)
>  {
> -       u32 base = vcpu->arch.kvm_cpuid_base;
> +       u32 base = vcpu->arch.kvm_cpuid.base;
>  
>         if (!base)
>                 return NULL;
> @@ -439,7 +440,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
>         vcpu->arch.cpuid_entries = e2;
>         vcpu->arch.cpuid_nent = nent;
>  
> -       kvm_update_kvm_cpuid_base(vcpu);
> +       kvm_update_hypervisor_cpuid(vcpu, KVM_SIGNATURE, &vcpu->arch.kvm_cpuid);
>         kvm_vcpu_after_set_cpuid(vcpu);
>  
>         return 0;


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present
  2022-12-20 13:40 ` [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Paul Durrant
@ 2023-01-03 16:20   ` David Woodhouse
  2023-01-04 19:40   ` Sean Christopherson
  1 sibling, 0 replies; 13+ messages in thread
From: David Woodhouse @ 2023-01-03 16:20 UTC (permalink / raw)
  To: Paul Durrant, x86, kvm, linux-kernel
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin

[-- Attachment #1: Type: text/plain, Size: 7120 bytes --]

On Tue, 2022-12-20 at 13:40 +0000, Paul Durrant wrote:
> The scaling information in subleaf 1 should match the values set by KVM in
> the 'vcpu_info' sub-structure 'time_info' (a.k.a. pvclock_vcpu_time_info)
> which is shared with the guest, but is not directly available to the VMM.
> The offset values are not set since a TSC offset is already applied.
> The TSC frequency should also be set in sub-leaf 2.
> 
> Signed-off-by: Paul Durrant <pdurrant@amazon.com>

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

> ---
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Cc: David Woodhouse <dwmw2@infradead.org>
> 
> v6:
>  - Stash Xen cpuid base and limit values when cpuid is set
>  - Re-name kvm_xen_setup_tsc_info() to kvm_xen_update_tsc_info()
> 
> v5:
>  - Drop the caching of the CPUID entry pointers and only update the
>    sub-leaves if the CPU frequency has actually changed
> 
> v4:
>  - Update commit comment
> 
> v3:
>  - Add leaf limit check in kvm_xen_set_cpuid()
> 
> v2:
>  - Make sure sub-leaf pointers are NULLed if the time leaf is removed
> ---
>  arch/x86/include/asm/kvm_host.h | 11 ++++++-----
>  arch/x86/kvm/cpuid.c            |  2 ++
>  arch/x86/kvm/x86.c              |  1 +
>  arch/x86/kvm/xen.c              | 26 ++++++++++++++++++++++++++
>  arch/x86/kvm/xen.h              |  7 +++++++
>  5 files changed, 42 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index ff201ad35551..44329594bdf7 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -678,6 +678,11 @@ struct kvm_vcpu_hv {
>         } nested;
>  };
>  
> +struct kvm_hypervisor_cpuid {
> +       u32 base;
> +       u32 limit;
> +};
> +
>  /* Xen HVM per vcpu emulation context */
>  struct kvm_vcpu_xen {
>         u64 hypercall_rip;
> @@ -698,6 +703,7 @@ struct kvm_vcpu_xen {
>         struct hrtimer timer;
>         int poll_evtchn;
>         struct timer_list poll_timer;
> +       struct kvm_hypervisor_cpuid cpuid;
>  };
>  
>  struct kvm_queued_exception {
> @@ -710,11 +716,6 @@ struct kvm_queued_exception {
>         bool has_payload;
>  };
>  
> -struct kvm_hypervisor_cpuid {
> -       u32 base;
> -       u32 limit;
> -};
> -
>  struct kvm_vcpu_arch {
>         /*
>          * rip and regs accesses must go through
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 2468720f8d84..e661413ddf8d 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -25,6 +25,7 @@
>  #include "mmu.h"
>  #include "trace.h"
>  #include "pmu.h"
> +#include "xen.h"
>  
>  /*
>   * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
> @@ -441,6 +442,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
>         vcpu->arch.cpuid_nent = nent;
>  
>         kvm_update_hypervisor_cpuid(vcpu, KVM_SIGNATURE, &vcpu->arch.kvm_cpuid);
> +       kvm_update_hypervisor_cpuid(vcpu, XEN_SIGNATURE, &vcpu->arch.xen.cpuid);
>         kvm_vcpu_after_set_cpuid(vcpu);
>  
>         return 0;
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index fd6c01a39312..60acc55f0c00 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3158,6 +3158,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
>                                    &vcpu->hv_clock.tsc_shift,
>                                    &vcpu->hv_clock.tsc_to_system_mul);
>                 vcpu->hw_tsc_khz = tgt_tsc_khz;
> +               kvm_xen_update_tsc_info(v);
>         }
>  
>         vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
> diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
> index d7af40240248..46297521791c 100644
> --- a/arch/x86/kvm/xen.c
> +++ b/arch/x86/kvm/xen.c
> @@ -22,6 +22,9 @@
>  #include <xen/interface/event_channel.h>
>  #include <xen/interface/sched.h>
>  
> +#include <asm/xen/cpuid.h>
> +
> +#include "cpuid.h"
>  #include "trace.h"
>  
>  static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm);
> @@ -2061,6 +2064,29 @@ void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
>         del_timer_sync(&vcpu->arch.xen.poll_timer);
>  }
>  
> +void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
> +{
> +       struct kvm_cpuid_entry2 *entry;
> +       u32 function;
> +
> +       if (!vcpu->arch.xen.cpuid.base)
> +               return;
> +
> +       function = vcpu->arch.xen.cpuid.base | XEN_CPUID_LEAF(3);
> +       if (function > vcpu->arch.xen.cpuid.limit)
> +               return;
> +
> +       entry = kvm_find_cpuid_entry_index(vcpu, function, 1);
> +       if (entry) {
> +               entry->ecx = vcpu->arch.hv_clock.tsc_to_system_mul;
> +               entry->edx = vcpu->arch.hv_clock.tsc_shift;
> +       }
> +
> +       entry = kvm_find_cpuid_entry_index(vcpu, function, 2);
> +       if (entry)
> +               entry->eax = vcpu->arch.hw_tsc_khz;
> +}
> +
>  void kvm_xen_init_vm(struct kvm *kvm)
>  {
>         idr_init(&kvm->arch.xen.evtchn_ports);
> diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h
> index ea33d80a0c51..88dd085e10f8 100644
> --- a/arch/x86/kvm/xen.h
> +++ b/arch/x86/kvm/xen.h
> @@ -32,6 +32,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe,
>  int kvm_xen_setup_evtchn(struct kvm *kvm,
>                          struct kvm_kernel_irq_routing_entry *e,
>                          const struct kvm_irq_routing_entry *ue);
> +void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu);
>  
>  static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
>  {
> @@ -135,6 +136,10 @@ static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
>  {
>         return false;
>  }
> +
> +static inline void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
> +{
> +}
>  #endif
>  
>  int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
> @@ -143,6 +148,8 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
>  #include <asm/xen/interface.h>
>  #include <xen/interface/vcpu.h>
>  
> +#define XEN_SIGNATURE "XenVMMXenVMM"
> +
>  void kvm_xen_update_runstate(struct kvm_vcpu *vcpu, int state);
>  
>  static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit
  2022-12-20 13:40 ` [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit Paul Durrant
  2023-01-03 16:20   ` David Woodhouse
@ 2023-01-04 19:34   ` Sean Christopherson
  2023-01-05 10:38     ` Paul Durrant
  1 sibling, 1 reply; 13+ messages in thread
From: Sean Christopherson @ 2023-01-04 19:34 UTC (permalink / raw)
  To: Paul Durrant
  Cc: x86, kvm, linux-kernel, Paolo Bonzini, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, David Woodhouse

On Tue, Dec 20, 2022, Paul Durrant wrote:
> A sunsequent patch will need to acquire the CPUID leaf range for emulated
> Xen so explicitly pass the signature of the hypervisor we're interested in
> to the new function. Also introduce a new kvm_hypervisor_cpuid structure
> so we can neatly store both the base and limit leaf indices.
> 
> Signed-off-by: Paul Durrant <pdurrant@amazon.com>
> ---
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: David Woodhouse <dwmw2@infradead.org>
> 
> v6:
>  - New in this version
> ---
>  arch/x86/include/asm/kvm_host.h |  7 ++++++-
>  arch/x86/kvm/cpuid.c            | 15 ++++++++-------
>  2 files changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index f35f1ff4427b..ff201ad35551 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -710,6 +710,11 @@ struct kvm_queued_exception {
>  	bool has_payload;
>  };
>  
> +struct kvm_hypervisor_cpuid {
> +	u32 base;
> +	u32 limit;
> +};

Probably makes sense to place this above "struct kvm_vcpu_xen" right away to
avoid the (very minor) churn.

>  struct kvm_vcpu_arch {
>  	/*
>  	 * rip and regs accesses must go through
> @@ -826,7 +831,7 @@ struct kvm_vcpu_arch {
>  
>  	int cpuid_nent;
>  	struct kvm_cpuid_entry2 *cpuid_entries;
> -	u32 kvm_cpuid_base;
> +	struct kvm_hypervisor_cpuid kvm_cpuid;
>  
>  	u64 reserved_gpa_bits;
>  	int maxphyaddr;
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 0b5bf013fcb8..2468720f8d84 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -180,12 +180,13 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2
>  	return 0;
>  }
>  
> -static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
> +static void kvm_update_hypervisor_cpuid(struct kvm_vcpu *vcpu, const char *hypervisor_signature,

Please wrap.  The 80 char limit is a soft limit, but should still be honored unless
there's a good reason to run over.

I also vote to name the param "sig" to keep line lengths short.

> +					struct kvm_hypervisor_cpuid *hypervisor_cpuid)

Since the struct is a 64-bit value, what about making this a pure getter that
returns a copy?

static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu,
							    const char *sig)
{
	struct kvm_hypervisor_cpuid cpuid = {};
	struct kvm_cpuid_entry2 *entry;
	u32 function;

	for_each_possible_hypervisor_cpuid_base(cpuid.base) {
		entry = kvm_find_cpuid_entry(vcpu, function);

		if (entry) {
			u32 signature[3];

			signature[0] = entry->ebx;
			signature[1] = entry->ecx;
			signature[2] = entry->edx;

			if (!memcmp(signature, sig, sizeof(signature))) {
				cpuid.base = function;
				cpuid.limit = entry->eax;
				break;
			}
		}
	}

	return cpuid;
}


	vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
	vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present
  2022-12-20 13:40 ` [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Paul Durrant
  2023-01-03 16:20   ` David Woodhouse
@ 2023-01-04 19:40   ` Sean Christopherson
  2023-01-04 20:09     ` David Woodhouse
  1 sibling, 1 reply; 13+ messages in thread
From: Sean Christopherson @ 2023-01-04 19:40 UTC (permalink / raw)
  To: Paul Durrant
  Cc: x86, kvm, linux-kernel, Paolo Bonzini, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin,
	David Woodhouse

On Tue, Dec 20, 2022, Paul Durrant wrote:
> @@ -143,6 +148,8 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
>  #include <asm/xen/interface.h>
>  #include <xen/interface/vcpu.h>
>  
> +#define XEN_SIGNATURE "XenVMMXenVMM"

arch/x86/include/asm/xen/hypervisor.h also open codes the signature.  Rather than
add a KVM-specific define, what about putting in xen/cpuid.h?  (I've had a version
of this series sitting in my todo pile for far too long, sorry).

--
From: Sean Christopherson <seanjc@google.com>
Date: Mon, 11 Jul 2022 15:18:42 -0700
Subject: [PATCH] xen: Add a #define to provide Xen's CPUID signature as a
 string

Add XEN_SIGNATURE instead of open coding it in xen_cpuid_base() so
that KVM can reuse the definition when querying a VM's CPUID.

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/include/asm/xen/cpuid.h      | 1 +
 arch/x86/include/asm/xen/hypervisor.h | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/xen/cpuid.h b/arch/x86/include/asm/xen/cpuid.h
index 6daa9b0c8d11..38f1bd153f42 100644
--- a/arch/x86/include/asm/xen/cpuid.h
+++ b/arch/x86/include/asm/xen/cpuid.h
@@ -49,6 +49,7 @@
  * EBX-EDX: "XenVMMXenVMM" signature, allowing positive identification
  *      of a Xen host.
  */
+#define XEN_SIGNATURE     "XenVMMXenVMM"
 #define XEN_CPUID_SIGNATURE_EBX 0x566e6558 /* "XenV" */
 #define XEN_CPUID_SIGNATURE_ECX 0x65584d4d /* "MMXe" */
 #define XEN_CPUID_SIGNATURE_EDX 0x4d4d566e /* "nVMM" */
diff --git a/arch/x86/include/asm/xen/hypervisor.h b/arch/x86/include/asm/xen/hypervisor.h
index 16f548a661cf..32ff6583b3d9 100644
--- a/arch/x86/include/asm/xen/hypervisor.h
+++ b/arch/x86/include/asm/xen/hypervisor.h
@@ -37,10 +37,11 @@ extern struct shared_info *HYPERVISOR_shared_info;
 extern struct start_info *xen_start_info;
 
 #include <asm/processor.h>
+#include <asm/xen/cpuid.h>
 
 static inline uint32_t xen_cpuid_base(void)
 {
-	return hypervisor_cpuid_base("XenVMMXenVMM", 2);
+	return hypervisor_cpuid_base(XEN_SIGNATURE, 2);
 }
 
 struct pci_dev;

base-commit: 91dc252b0dbb6879e4067f614df1e397fec532a1
-- 

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present
  2023-01-04 19:40   ` Sean Christopherson
@ 2023-01-04 20:09     ` David Woodhouse
  2023-01-04 20:20       ` Sean Christopherson
  0 siblings, 1 reply; 13+ messages in thread
From: David Woodhouse @ 2023-01-04 20:09 UTC (permalink / raw)
  To: Sean Christopherson, Paul Durrant
  Cc: x86, kvm, linux-kernel, Paolo Bonzini, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin

[-- Attachment #1: Type: text/plain, Size: 461 bytes --]

On Wed, 2023-01-04 at 19:40 +0000, Sean Christopherson wrote:
> 
> arch/x86/include/asm/xen/hypervisor.h also open codes the signature.  Rather than
> add a KVM-specific define, what about putting in xen/cpuid.h?  (I've had a version
> of this series sitting in my todo pile for far too long, sorry).

xen/cpuid.h is an external header imported from Xen itself so in
general I'd prefer to avoid modifying it unless we also send the
changes upstream.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present
  2023-01-04 20:09     ` David Woodhouse
@ 2023-01-04 20:20       ` Sean Christopherson
  2023-01-04 21:03         ` David Woodhouse
  0 siblings, 1 reply; 13+ messages in thread
From: Sean Christopherson @ 2023-01-04 20:20 UTC (permalink / raw)
  To: David Woodhouse
  Cc: Paul Durrant, x86, kvm, linux-kernel, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin

On Wed, Jan 04, 2023, David Woodhouse wrote:
> On Wed, 2023-01-04 at 19:40 +0000, Sean Christopherson wrote:
> > 
> > arch/x86/include/asm/xen/hypervisor.h also open codes the signature.  Rather than
> > add a KVM-specific define, what about putting in xen/cpuid.h?  (I've had a version
> > of this series sitting in my todo pile for far too long, sorry).
> 
> xen/cpuid.h is an external header imported from Xen itself so in
> general I'd prefer to avoid modifying it unless we also send the
> changes upstream.

*sigh*  Fool me once...

Can we shove it into arch/x86/include/asm/xen/hypervisor.h?  Or is including
that in KVM too confusing/ugly?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present
  2023-01-04 20:20       ` Sean Christopherson
@ 2023-01-04 21:03         ` David Woodhouse
  0 siblings, 0 replies; 13+ messages in thread
From: David Woodhouse @ 2023-01-04 21:03 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paul Durrant, x86, kvm, linux-kernel, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin



On 4 January 2023 20:20:55 GMT, Sean Christopherson <seanjc@google.com> wrote:
>On Wed, Jan 04, 2023, David Woodhouse wrote:
>> On Wed, 2023-01-04 at 19:40 +0000, Sean Christopherson wrote:
>> > 
>> > arch/x86/include/asm/xen/hypervisor.h also open codes the signature.  Rather than
>> > add a KVM-specific define, what about putting in xen/cpuid.h?  (I've had a version
>> > of this series sitting in my todo pile for far too long, sorry).
>> 
>> xen/cpuid.h is an external header imported from Xen itself so in
>> general I'd prefer to avoid modifying it unless we also send the
>> changes upstream.
>
>*sigh*  Fool me once...
>
>Can we shove it into arch/x86/include/asm/xen/hypervisor.h?  Or is including
>that in KVM too confusing/ugly?

Maybe, if that's our own header. We do include a bunch of stuff intended for use by Xen guests, in arch/x86/kvm/xen.c to provide Xen support. It's only the 32-bit compat bits we define for ourselves within arch/x86/kvm/xen.h.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit
  2023-01-04 19:34   ` Sean Christopherson
@ 2023-01-05 10:38     ` Paul Durrant
  2023-01-05 18:09       ` Sean Christopherson
  0 siblings, 1 reply; 13+ messages in thread
From: Paul Durrant @ 2023-01-05 10:38 UTC (permalink / raw)
  To: Sean Christopherson, Paul Durrant
  Cc: x86, kvm, linux-kernel, Paolo Bonzini, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, David Woodhouse

On 04/01/2023 19:34, Sean Christopherson wrote:
> On Tue, Dec 20, 2022, Paul Durrant wrote:
>> A sunsequent patch will need to acquire the CPUID leaf range for emulated
>> Xen so explicitly pass the signature of the hypervisor we're interested in
>> to the new function. Also introduce a new kvm_hypervisor_cpuid structure
>> so we can neatly store both the base and limit leaf indices.
>>
>> Signed-off-by: Paul Durrant <pdurrant@amazon.com>
>> ---
>> Cc: Sean Christopherson <seanjc@google.com>
>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>> Cc: Thomas Gleixner <tglx@linutronix.de>
>> Cc: Ingo Molnar <mingo@redhat.com>
>> Cc: Borislav Petkov <bp@alien8.de>
>> Cc: Dave Hansen <dave.hansen@linux.intel.com>
>> Cc: David Woodhouse <dwmw2@infradead.org>
>>
>> v6:
>>   - New in this version
>> ---
>>   arch/x86/include/asm/kvm_host.h |  7 ++++++-
>>   arch/x86/kvm/cpuid.c            | 15 ++++++++-------
>>   2 files changed, 14 insertions(+), 8 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
>> index f35f1ff4427b..ff201ad35551 100644
>> --- a/arch/x86/include/asm/kvm_host.h
>> +++ b/arch/x86/include/asm/kvm_host.h
>> @@ -710,6 +710,11 @@ struct kvm_queued_exception {
>>   	bool has_payload;
>>   };
>>   
>> +struct kvm_hypervisor_cpuid {
>> +	u32 base;
>> +	u32 limit;
>> +};
> 
> Probably makes sense to place this above "struct kvm_vcpu_xen" right away to
> avoid the (very minor) churn.
> 

Sure.

>>   struct kvm_vcpu_arch {
>>   	/*
>>   	 * rip and regs accesses must go through
>> @@ -826,7 +831,7 @@ struct kvm_vcpu_arch {
>>   
>>   	int cpuid_nent;
>>   	struct kvm_cpuid_entry2 *cpuid_entries;
>> -	u32 kvm_cpuid_base;
>> +	struct kvm_hypervisor_cpuid kvm_cpuid;
>>   
>>   	u64 reserved_gpa_bits;
>>   	int maxphyaddr;
>> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
>> index 0b5bf013fcb8..2468720f8d84 100644
>> --- a/arch/x86/kvm/cpuid.c
>> +++ b/arch/x86/kvm/cpuid.c
>> @@ -180,12 +180,13 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2
>>   	return 0;
>>   }
>>   
>> -static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
>> +static void kvm_update_hypervisor_cpuid(struct kvm_vcpu *vcpu, const char *hypervisor_signature,
> 
> Please wrap.  The 80 char limit is a soft limit, but should still be honored unless
> there's a good reason to run over.

Ok.

> 
> I also vote to name the param "sig" to keep line lengths short.
> 
>> +					struct kvm_hypervisor_cpuid *hypervisor_cpuid)
> 
> Since the struct is a 64-bit value, what about making this a pure getter that
> returns a copy?
> 
> static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu,
> 							    const char *sig)
> {
> 	struct kvm_hypervisor_cpuid cpuid = {};
> 	struct kvm_cpuid_entry2 *entry;
> 	u32 function;
> 
> 	for_each_possible_hypervisor_cpuid_base(cpuid.base) {
> 		entry = kvm_find_cpuid_entry(vcpu, function);
> 
> 		if (entry) {
> 			u32 signature[3];
> 
> 			signature[0] = entry->ebx;
> 			signature[1] = entry->ecx;
> 			signature[2] = entry->edx;
> 
> 			if (!memcmp(signature, sig, sizeof(signature))) {
> 				cpuid.base = function;
> 				cpuid.limit = entry->eax;
> 				break;
> 			}
> 		}
> 	}
> 
> 	return cpuid;
> }
> 
> 
> 	vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
> 	vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);

Yes, if that's preferable then no problem.

   Paul


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit
  2023-01-05 10:38     ` Paul Durrant
@ 2023-01-05 18:09       ` Sean Christopherson
  2023-01-06  9:20         ` Paul Durrant
  0 siblings, 1 reply; 13+ messages in thread
From: Sean Christopherson @ 2023-01-05 18:09 UTC (permalink / raw)
  To: Paul Durrant
  Cc: Paul Durrant, x86, kvm, linux-kernel, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	David Woodhouse

On Thu, Jan 05, 2023, Paul Durrant wrote:
> On 04/01/2023 19:34, Sean Christopherson wrote:
> > Since the struct is a 64-bit value, what about making this a pure getter that
> > returns a copy?
> > 
> > static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu,
> > 							    const char *sig)
> > {
> > 	struct kvm_hypervisor_cpuid cpuid = {};
> > 	struct kvm_cpuid_entry2 *entry;
> > 	u32 function;
> > 
> > 	for_each_possible_hypervisor_cpuid_base(cpuid.base) {
> > 		entry = kvm_find_cpuid_entry(vcpu, function);
> > 
> > 		if (entry) {
> > 			u32 signature[3];
> > 
> > 			signature[0] = entry->ebx;
> > 			signature[1] = entry->ecx;
> > 			signature[2] = entry->edx;
> > 
> > 			if (!memcmp(signature, sig, sizeof(signature))) {
> > 				cpuid.base = function;
> > 				cpuid.limit = entry->eax;
> > 				break;
> > 			}
> > 		}
> > 	}
> > 
> > 	return cpuid;
> > }
> > 
> > 
> > 	vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
> > 	vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);
> 
> Yes, if that's preferable then no problem.

I like it (obviously), but it's probably worth waiting a few days to see what
others think before posting a new version.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit
  2023-01-05 18:09       ` Sean Christopherson
@ 2023-01-06  9:20         ` Paul Durrant
  0 siblings, 0 replies; 13+ messages in thread
From: Paul Durrant @ 2023-01-06  9:20 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paul Durrant, x86, kvm, linux-kernel, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	David Woodhouse

On 05/01/2023 18:09, Sean Christopherson wrote:
> On Thu, Jan 05, 2023, Paul Durrant wrote:
>> On 04/01/2023 19:34, Sean Christopherson wrote:
>>> Since the struct is a 64-bit value, what about making this a pure getter that
>>> returns a copy?
>>>
>>> static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu,
>>> 							    const char *sig)
>>> {
>>> 	struct kvm_hypervisor_cpuid cpuid = {};
>>> 	struct kvm_cpuid_entry2 *entry;
>>> 	u32 function;
>>>
>>> 	for_each_possible_hypervisor_cpuid_base(cpuid.base) {
>>> 		entry = kvm_find_cpuid_entry(vcpu, function);
>>>
>>> 		if (entry) {
>>> 			u32 signature[3];
>>>
>>> 			signature[0] = entry->ebx;
>>> 			signature[1] = entry->ecx;
>>> 			signature[2] = entry->edx;
>>>
>>> 			if (!memcmp(signature, sig, sizeof(signature))) {
>>> 				cpuid.base = function;
>>> 				cpuid.limit = entry->eax;
>>> 				break;
>>> 			}
>>> 		}
>>> 	}
>>>
>>> 	return cpuid;
>>> }
>>>
>>>
>>> 	vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
>>> 	vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);
>>
>> Yes, if that's preferable then no problem.
> 
> I like it (obviously), but it's probably worth waiting a few days to see what
> others think before posting a new version.

I think it's cleaner too, and I already did the typing so I may as well 
post. I don't think anyone else has expressed any strong opinions on the 
code either way.

Paul

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-01-06  9:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-20 13:40 [PATCH v6 0/2] KVM: x86/xen: update Xen CPUID Leaf 4 Paul Durrant
2022-12-20 13:40 ` [PATCH v6 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit Paul Durrant
2023-01-03 16:20   ` David Woodhouse
2023-01-04 19:34   ` Sean Christopherson
2023-01-05 10:38     ` Paul Durrant
2023-01-05 18:09       ` Sean Christopherson
2023-01-06  9:20         ` Paul Durrant
2022-12-20 13:40 ` [PATCH v6 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Paul Durrant
2023-01-03 16:20   ` David Woodhouse
2023-01-04 19:40   ` Sean Christopherson
2023-01-04 20:09     ` David Woodhouse
2023-01-04 20:20       ` Sean Christopherson
2023-01-04 21:03         ` David Woodhouse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).