All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info
@ 2020-06-03 23:56 Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 1/6] kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id() Jim Mattson
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

It's been about 6 months since v2. Sorry for the delay. Initially, this
was a single patch to add information to KVM_EXIT_FAIL_ENTRY to help
identify a defective CPU. It has gotten a little more complicated,
since Peter Shier pointed out that the vCPU thread may have migrated
between the time of failure and the KVM exit. Fortunately, the SEV folks
started to make the necessary information available with "last_cpu," but
only on AMD and only with SEV. The current version expands upon that by
making "last_cpu" available in all configurations on AMD and Intel.

v2: Use vcpu->cpu rather than raw_smp_processor_id() (Liran).
v3: Record the last logical processor to run the vCPU thread (Peter).
    Add the "last CPU" information to KVM_EXIT_INTERNAL_ERROR exits as
    well as KVM_EXIT_FAIL_ENTRY [except for "EMULATION" errors].
    (Liran & Paolo).
v4: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu, and set this
    field in vcpu_enter_guest (Sean).

Jim Mattson (6):
  kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id()
  kvm: svm: Always set svm->last_cpu on VMRUN
  kvm: vmx: Add last_cpu to struct vcpu_vmx
  kvm: x86: Add "last CPU" to some KVM_EXIT information
  kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu
  kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest

 Documentation/virt/kvm/api.rst  |  1 +
 arch/x86/include/asm/kvm_host.h |  3 +++
 arch/x86/kvm/svm/sev.c          |  3 +--
 arch/x86/kvm/svm/svm.c          | 13 ++++++-------
 arch/x86/kvm/svm/svm.h          |  3 ---
 arch/x86/kvm/vmx/vmx.c          | 10 ++++++++--
 arch/x86/kvm/x86.c              |  2 ++
 include/uapi/linux/kvm.h        |  2 ++
 8 files changed, 23 insertions(+), 14 deletions(-)

-- 
2.27.0.rc2.251.g90737beb825-goog


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

* [PATCH v4 1/6] kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id()
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
@ 2020-06-03 23:56 ` Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 2/6] kvm: svm: Always set svm->last_cpu on VMRUN Jim Mattson
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

The current logical processor id is cached in vcpu->cpu. Use it
instead of raw_smp_processor_id() when a kvm_vcpu struct is available.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Oliver Upton <oupton@google.com>
---
 arch/x86/kvm/svm/svm.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 9e333b91ff78..f0dd481be435 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2990,21 +2990,18 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 
 static void reload_tss(struct kvm_vcpu *vcpu)
 {
-	int cpu = raw_smp_processor_id();
+	struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
 
-	struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
 	sd->tss_desc->type = 9; /* available 32/64-bit TSS */
 	load_TR_desc();
 }
 
 static void pre_svm_run(struct vcpu_svm *svm)
 {
-	int cpu = raw_smp_processor_id();
-
-	struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
+	struct svm_cpu_data *sd = per_cpu(svm_data, svm->vcpu.cpu);
 
 	if (sev_guest(svm->vcpu.kvm))
-		return pre_sev_run(svm, cpu);
+		return pre_sev_run(svm, svm->vcpu.cpu);
 
 	/* FIXME: handle wraparound of asid_generation */
 	if (svm->asid_generation != sd->asid_generation)
-- 
2.27.0.rc2.251.g90737beb825-goog


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

* [PATCH v4 2/6] kvm: svm: Always set svm->last_cpu on VMRUN
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 1/6] kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id() Jim Mattson
@ 2020-06-03 23:56 ` Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 3/6] kvm: vmx: Add last_cpu to struct vcpu_vmx Jim Mattson
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

Previously, this field was only set when using SEV. Set it for all
vCPU configurations, so that it can be communicated to userspace for
diagnosing potential hardware errors.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Oliver Upton <oupton@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 arch/x86/kvm/svm/sev.c | 1 -
 arch/x86/kvm/svm/svm.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 89f7f3aebd31..aa61d5d1e7f3 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -1184,7 +1184,6 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)
 	    svm->last_cpu == cpu)
 		return;
 
-	svm->last_cpu = cpu;
 	sd->sev_vmcbs[asid] = svm->vmcb;
 	svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
 	mark_dirty(svm->vmcb, VMCB_ASID);
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index f0dd481be435..442dbb763639 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -3394,6 +3394,7 @@ static fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
 	 */
 	x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
 
+	svm->last_cpu = vcpu->cpu;
 	__svm_vcpu_run(svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs);
 
 #ifdef CONFIG_X86_64
-- 
2.27.0.rc2.251.g90737beb825-goog


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

* [PATCH v4 3/6] kvm: vmx: Add last_cpu to struct vcpu_vmx
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 1/6] kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id() Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 2/6] kvm: svm: Always set svm->last_cpu on VMRUN Jim Mattson
@ 2020-06-03 23:56 ` Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 4/6] kvm: x86: Add "last CPU" to some KVM_EXIT information Jim Mattson
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

As we already do in svm, record the last logical processor on which a
vCPU has run, so that it can be communicated to userspace for
potential hardware errors.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Oliver Upton <oupton@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 arch/x86/kvm/vmx/vmx.c | 1 +
 arch/x86/kvm/vmx/vmx.h | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 170cc76a581f..42856970d3b8 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6730,6 +6730,7 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.cr2 != read_cr2())
 		write_cr2(vcpu->arch.cr2);
 
+	vmx->last_cpu = vcpu->cpu;
 	vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
 				   vmx->loaded_vmcs->launched);
 
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 672c28f17e49..8a1e833cf4fb 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -302,6 +302,9 @@ struct vcpu_vmx {
 	u64 ept_pointer;
 
 	struct pt_desc pt_desc;
+
+	/* which host CPU was used for running this vcpu */
+	unsigned int last_cpu;
 };
 
 enum ept_pointers_status {
-- 
2.27.0.rc2.251.g90737beb825-goog


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

* [PATCH v4 4/6] kvm: x86: Add "last CPU" to some KVM_EXIT information
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
                   ` (2 preceding siblings ...)
  2020-06-03 23:56 ` [PATCH v4 3/6] kvm: vmx: Add last_cpu to struct vcpu_vmx Jim Mattson
@ 2020-06-03 23:56 ` Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 5/6] kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu Jim Mattson
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

More often than not, a failed VM-entry in an x86 production
environment is induced by a defective CPU. To help identify the bad
hardware, include the id of the last logical CPU to run a vCPU in the
information provided to userspace on a KVM exit for failed VM-entry or
for KVM internal errors not associated with emulation. The presence of
this additional information is indicated by a new capability,
KVM_CAP_LAST_CPU.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Oliver Upton <oupton@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 Documentation/virt/kvm/api.rst |  1 +
 arch/x86/kvm/svm/svm.c         |  4 +++-
 arch/x86/kvm/vmx/vmx.c         | 10 ++++++++--
 arch/x86/kvm/x86.c             |  1 +
 include/uapi/linux/kvm.h       |  2 ++
 5 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index d280af5345df..17db8b68c165 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -4792,6 +4792,7 @@ hardware_exit_reason.
 		/* KVM_EXIT_FAIL_ENTRY */
 		struct {
 			__u64 hardware_entry_failure_reason;
+			__u32 cpu; /* if KVM_LAST_CPU */
 		} fail_entry;
 
 If exit_reason is KVM_EXIT_FAIL_ENTRY, the vcpu could not be run due
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 442dbb763639..938be4172bab 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2945,6 +2945,7 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
 		kvm_run->fail_entry.hardware_entry_failure_reason
 			= svm->vmcb->control.exit_code;
+		kvm_run->fail_entry.cpu = svm->last_cpu;
 		dump_vmcb(vcpu);
 		return 0;
 	}
@@ -2968,8 +2969,9 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
 		vcpu->run->internal.suberror =
 			KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
-		vcpu->run->internal.ndata = 1;
+		vcpu->run->internal.ndata = 2;
 		vcpu->run->internal.data[0] = exit_code;
+		vcpu->run->internal.data[1] = svm->last_cpu;
 		return 0;
 	}
 
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 42856970d3b8..da5490b94704 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4758,10 +4758,11 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 	    !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
 		vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
-		vcpu->run->internal.ndata = 3;
+		vcpu->run->internal.ndata = 4;
 		vcpu->run->internal.data[0] = vect_info;
 		vcpu->run->internal.data[1] = intr_info;
 		vcpu->run->internal.data[2] = error_code;
+		vcpu->run->internal.data[3] = vmx->last_cpu;
 		return 0;
 	}
 
@@ -5983,6 +5984,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
 		vcpu->run->fail_entry.hardware_entry_failure_reason
 			= exit_reason;
+		vcpu->run->fail_entry.cpu = vmx->last_cpu;
 		return 0;
 	}
 
@@ -5991,6 +5993,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
 		vcpu->run->fail_entry.hardware_entry_failure_reason
 			= vmcs_read32(VM_INSTRUCTION_ERROR);
+		vcpu->run->fail_entry.cpu = vmx->last_cpu;
 		return 0;
 	}
 
@@ -6017,6 +6020,8 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 			vcpu->run->internal.data[3] =
 				vmcs_read64(GUEST_PHYSICAL_ADDRESS);
 		}
+		vcpu->run->internal.data[vcpu->run->internal.ndata++] =
+			vmx->last_cpu;
 		return 0;
 	}
 
@@ -6072,8 +6077,9 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 	vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
 	vcpu->run->internal.suberror =
 			KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
-	vcpu->run->internal.ndata = 1;
+	vcpu->run->internal.ndata = 2;
 	vcpu->run->internal.data[0] = exit_reason;
+	vcpu->run->internal.data[1] = vmx->last_cpu;
 	return 0;
 }
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9e41b5135340..20c420a45847 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3472,6 +3472,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_MSR_PLATFORM_INFO:
 	case KVM_CAP_EXCEPTION_PAYLOAD:
 	case KVM_CAP_SET_GUEST_DEBUG:
+	case KVM_CAP_LAST_CPU:
 		r = 1;
 		break;
 	case KVM_CAP_SYNC_REGS:
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 6721eb563eda..3edbd44d85bf 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -289,6 +289,7 @@ struct kvm_run {
 		/* KVM_EXIT_FAIL_ENTRY */
 		struct {
 			__u64 hardware_entry_failure_reason;
+			__u32 cpu;
 		} fail_entry;
 		/* KVM_EXIT_EXCEPTION */
 		struct {
@@ -1031,6 +1032,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_PPC_SECURE_GUEST 181
 #define KVM_CAP_HALT_POLL 182
 #define KVM_CAP_ASYNC_PF_INT 183
+#define KVM_CAP_LAST_CPU 184
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
-- 
2.27.0.rc2.251.g90737beb825-goog


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

* [PATCH v4 5/6] kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
                   ` (3 preceding siblings ...)
  2020-06-03 23:56 ` [PATCH v4 4/6] kvm: x86: Add "last CPU" to some KVM_EXIT information Jim Mattson
@ 2020-06-03 23:56 ` Jim Mattson
  2020-06-03 23:56 ` [PATCH v4 6/6] kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest Jim Mattson
  2020-06-23  9:29 ` [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Paolo Bonzini
  6 siblings, 0 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

Both the vcpu_vmx structure and the vcpu_svm structure have a
'last_cpu' field. Move the common field into the kvm_vcpu_arch
structure. For clarity, rename it to 'last_vmentry_cpu.'

Suggested-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Oliver Upton <oupton@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 arch/x86/include/asm/kvm_host.h |  3 +++
 arch/x86/kvm/svm/sev.c          |  2 +-
 arch/x86/kvm/svm/svm.c          |  6 +++---
 arch/x86/kvm/svm/svm.h          |  3 ---
 arch/x86/kvm/vmx/vmx.c          | 12 ++++++------
 arch/x86/kvm/vmx/vmx.h          |  3 ---
 6 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 58337a25396a..fa33b0f8028f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -827,6 +827,9 @@ struct kvm_vcpu_arch {
 	/* Flush the L1 Data cache for L1TF mitigation on VMENTER */
 	bool l1tf_flush_l1d;
 
+	/* Host CPU on which VM-entry was most recently attempted */
+	unsigned int last_vmentry_cpu;
+
 	/* AMD MSRC001_0015 Hardware Configuration */
 	u64 msr_hwcr;
 };
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index aa61d5d1e7f3..126d9014635a 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -1181,7 +1181,7 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)
 	 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
 	 */
 	if (sd->sev_vmcbs[asid] == svm->vmcb &&
-	    svm->last_cpu == cpu)
+	    svm->vcpu.arch.last_vmentry_cpu == cpu)
 		return;
 
 	sd->sev_vmcbs[asid] = svm->vmcb;
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 938be4172bab..78b64d1ab7b1 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2945,7 +2945,7 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
 		kvm_run->fail_entry.hardware_entry_failure_reason
 			= svm->vmcb->control.exit_code;
-		kvm_run->fail_entry.cpu = svm->last_cpu;
+		kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
 		dump_vmcb(vcpu);
 		return 0;
 	}
@@ -2971,7 +2971,7 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 			KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
 		vcpu->run->internal.ndata = 2;
 		vcpu->run->internal.data[0] = exit_code;
-		vcpu->run->internal.data[1] = svm->last_cpu;
+		vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
 		return 0;
 	}
 
@@ -3396,7 +3396,7 @@ static fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
 	 */
 	x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
 
-	svm->last_cpu = vcpu->cpu;
+	vcpu->arch.last_vmentry_cpu = vcpu->cpu;
 	__svm_vcpu_run(svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs);
 
 #ifdef CONFIG_X86_64
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 6ac4c00a5d82..613356f85da6 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -158,9 +158,6 @@ struct vcpu_svm {
 	 */
 	struct list_head ir_list;
 	spinlock_t ir_list_lock;
-
-	/* which host CPU was used for running this vcpu */
-	unsigned int last_cpu;
 };
 
 struct svm_cpu_data {
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index da5490b94704..562381073c40 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4762,7 +4762,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 		vcpu->run->internal.data[0] = vect_info;
 		vcpu->run->internal.data[1] = intr_info;
 		vcpu->run->internal.data[2] = error_code;
-		vcpu->run->internal.data[3] = vmx->last_cpu;
+		vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
 		return 0;
 	}
 
@@ -5984,7 +5984,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
 		vcpu->run->fail_entry.hardware_entry_failure_reason
 			= exit_reason;
-		vcpu->run->fail_entry.cpu = vmx->last_cpu;
+		vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
 		return 0;
 	}
 
@@ -5993,7 +5993,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
 		vcpu->run->fail_entry.hardware_entry_failure_reason
 			= vmcs_read32(VM_INSTRUCTION_ERROR);
-		vcpu->run->fail_entry.cpu = vmx->last_cpu;
+		vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
 		return 0;
 	}
 
@@ -6021,7 +6021,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 				vmcs_read64(GUEST_PHYSICAL_ADDRESS);
 		}
 		vcpu->run->internal.data[vcpu->run->internal.ndata++] =
-			vmx->last_cpu;
+			vcpu->arch.last_vmentry_cpu;
 		return 0;
 	}
 
@@ -6079,7 +6079,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 			KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
 	vcpu->run->internal.ndata = 2;
 	vcpu->run->internal.data[0] = exit_reason;
-	vcpu->run->internal.data[1] = vmx->last_cpu;
+	vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
 	return 0;
 }
 
@@ -6736,7 +6736,7 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.cr2 != read_cr2())
 		write_cr2(vcpu->arch.cr2);
 
-	vmx->last_cpu = vcpu->cpu;
+	vcpu->arch.last_vmentry_cpu = vcpu->cpu;
 	vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
 				   vmx->loaded_vmcs->launched);
 
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 8a1e833cf4fb..672c28f17e49 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -302,9 +302,6 @@ struct vcpu_vmx {
 	u64 ept_pointer;
 
 	struct pt_desc pt_desc;
-
-	/* which host CPU was used for running this vcpu */
-	unsigned int last_cpu;
 };
 
 enum ept_pointers_status {
-- 
2.27.0.rc2.251.g90737beb825-goog


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

* [PATCH v4 6/6] kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
                   ` (4 preceding siblings ...)
  2020-06-03 23:56 ` [PATCH v4 5/6] kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu Jim Mattson
@ 2020-06-03 23:56 ` Jim Mattson
  2020-06-23  9:29 ` [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Paolo Bonzini
  6 siblings, 0 replies; 8+ messages in thread
From: Jim Mattson @ 2020-06-03 23:56 UTC (permalink / raw)
  To: kvm, Paolo Bonzini
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson, Jim Mattson

Since this field is now in kvm_vcpu_arch, clean things up a little by
setting it in vendor-agnostic code: vcpu_enter_guest. Note that it
must be set after the call to kvm_x86_ops.run(), since it can't be
updated before pre_sev_run().

Suggested-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Oliver Upton <oupton@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 arch/x86/kvm/svm/svm.c | 1 -
 arch/x86/kvm/vmx/vmx.c | 1 -
 arch/x86/kvm/x86.c     | 1 +
 3 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 78b64d1ab7b1..bc8223df698f 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -3396,7 +3396,6 @@ static fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
 	 */
 	x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
 
-	vcpu->arch.last_vmentry_cpu = vcpu->cpu;
 	__svm_vcpu_run(svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs);
 
 #ifdef CONFIG_X86_64
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 562381073c40..c6eea58b5e66 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6736,7 +6736,6 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.cr2 != read_cr2())
 		write_cr2(vcpu->arch.cr2);
 
-	vcpu->arch.last_vmentry_cpu = vcpu->cpu;
 	vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
 				   vmx->loaded_vmcs->launched);
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 20c420a45847..512db3c39392 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8554,6 +8554,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (hw_breakpoint_active())
 		hw_breakpoint_restore();
 
+	vcpu->arch.last_vmentry_cpu = vcpu->cpu;
 	vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
 
 	vcpu->mode = OUTSIDE_GUEST_MODE;
-- 
2.27.0.rc2.251.g90737beb825-goog


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

* Re: [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info
  2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
                   ` (5 preceding siblings ...)
  2020-06-03 23:56 ` [PATCH v4 6/6] kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest Jim Mattson
@ 2020-06-23  9:29 ` Paolo Bonzini
  6 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2020-06-23  9:29 UTC (permalink / raw)
  To: Jim Mattson, kvm
  Cc: Liran Alon, Oliver Upton, Peter Shier, Sean Christopherson

On 04/06/20 01:56, Jim Mattson wrote:
> It's been about 6 months since v2. Sorry for the delay. Initially, this
> was a single patch to add information to KVM_EXIT_FAIL_ENTRY to help
> identify a defective CPU. It has gotten a little more complicated,
> since Peter Shier pointed out that the vCPU thread may have migrated
> between the time of failure and the KVM exit. Fortunately, the SEV folks
> started to make the necessary information available with "last_cpu," but
> only on AMD and only with SEV. The current version expands upon that by
> making "last_cpu" available in all configurations on AMD and Intel.
> 
> v2: Use vcpu->cpu rather than raw_smp_processor_id() (Liran).
> v3: Record the last logical processor to run the vCPU thread (Peter).
>     Add the "last CPU" information to KVM_EXIT_INTERNAL_ERROR exits as
>     well as KVM_EXIT_FAIL_ENTRY [except for "EMULATION" errors].
>     (Liran & Paolo).
> v4: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu, and set this
>     field in vcpu_enter_guest (Sean).
> 
> Jim Mattson (6):
>   kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id()
>   kvm: svm: Always set svm->last_cpu on VMRUN
>   kvm: vmx: Add last_cpu to struct vcpu_vmx
>   kvm: x86: Add "last CPU" to some KVM_EXIT information
>   kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu
>   kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest
> 
>  Documentation/virt/kvm/api.rst  |  1 +
>  arch/x86/include/asm/kvm_host.h |  3 +++
>  arch/x86/kvm/svm/sev.c          |  3 +--
>  arch/x86/kvm/svm/svm.c          | 13 ++++++-------
>  arch/x86/kvm/svm/svm.h          |  3 ---
>  arch/x86/kvm/vmx/vmx.c          | 10 ++++++++--
>  arch/x86/kvm/x86.c              |  2 ++
>  include/uapi/linux/kvm.h        |  2 ++
>  8 files changed, 23 insertions(+), 14 deletions(-)
> 

Queued, thanks.  (It would have been fine to squash patches 3-5-6
together, too).

Paolo


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

end of thread, other threads:[~2020-06-23  9:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-03 23:56 [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Jim Mattson
2020-06-03 23:56 ` [PATCH v4 1/6] kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id() Jim Mattson
2020-06-03 23:56 ` [PATCH v4 2/6] kvm: svm: Always set svm->last_cpu on VMRUN Jim Mattson
2020-06-03 23:56 ` [PATCH v4 3/6] kvm: vmx: Add last_cpu to struct vcpu_vmx Jim Mattson
2020-06-03 23:56 ` [PATCH v4 4/6] kvm: x86: Add "last CPU" to some KVM_EXIT information Jim Mattson
2020-06-03 23:56 ` [PATCH v4 5/6] kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu Jim Mattson
2020-06-03 23:56 ` [PATCH v4 6/6] kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest Jim Mattson
2020-06-23  9:29 ` [PATCH v4 0/6] Add logical CPU to KVM_EXIT_FAIL_ENTRY info Paolo Bonzini

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.