Add logical CPU to KVM_EXIT_FAIL_ENTRY info
Subsystems (4)
v4 cover Add logical CPU to KVM_EXIT_FAIL_ENTRY info
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(-)
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
PATCH v4 1/6 kvm: svm: Prefer vcpu->cpu to raw_smp_processor_id()
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.
--- arch/x86/kvm/svm/svm.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +3 -6
@@ -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)
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
PATCH v4 2/6 kvm: svm: Always set svm->last_cpu on VMRUN
In reply to Jim MattsonPreviously, 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.
--- arch/x86/kvm/svm/sev.c | 1 - arch/x86/kvm/svm/svm.c | 1 + 2 files changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +0 -1
@@ -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 );
arch/x86/kvm/svm/svm.c +1 -0
@@ -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
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
PATCH v4 3/6 kvm: vmx: Add last_cpu to struct vcpu_vmx
In reply to Jim MattsonAs 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.
--- arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/vmx/vmx.h | 3 +++ 2 files changed, 4 insertions(+)
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -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);
arch/x86/kvm/vmx/vmx.h +3 -0
@@ -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 {
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
PATCH v4 4/6 kvm: x86: Add "last CPU" to some KVM_EXIT information
In reply to Jim MattsonMore 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.
--- 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(-)
Documentation/virt/kvm/api.rst +1 -0
@@ -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
arch/x86/kvm/svm/svm.c +3 -1
@@ -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 ; }
arch/x86/kvm/vmx/vmx.c +8 -2
@@ -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 ; }
arch/x86/kvm/x86.c +1 -0
@@ -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 :
include/uapi/linux/kvm.h +2 -0
@@ -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
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
PATCH v4 5/6 kvm: x86: Move last_cpu into kvm_vcpu_arch as last_vmentry_cpu
In reply to Jim MattsonBoth 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.'
--- 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(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -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; };
arch/x86/kvm/svm/sev.c +1 -1
@@ -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;
arch/x86/kvm/svm/svm.c +3 -3
@@ -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
arch/x86/kvm/svm/svm.h +0 -3
@@ -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 {
arch/x86/kvm/vmx/vmx.c +6 -6
@@ -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);
arch/x86/kvm/vmx/vmx.h +0 -3
@@ -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 {
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
PATCH v4 6/6 kvm: x86: Set last_vmentry_cpu in vcpu_enter_guest
In reply to Jim MattsonSince 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().
--- 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(-)
arch/x86/kvm/svm/svm.c +0 -1
@@ -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
arch/x86/kvm/vmx/vmx.c +0 -1
@@ -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);
arch/x86/kvm/x86.c +1 -0
@@ -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 ;
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
Quoted 25 lines · 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_ENTRYto 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->cpurather thanraw_smp_processor_id()(Liran).
v3: Record the last logical processor to run the vCPU thread (Peter).
Add the "last CPU" information toKVM_EXIT_INTERNAL_ERRORexits as well asKVM_EXIT_FAIL_ENTRY[except for "EMULATION" errors]. (Liran & Paolo).
v4: Movelast_cpuintokvm_vcpu_archaslast_vmentry_cpu, and set this
field invcpu_enter_guest(Sean).Jim Mattson (6):
kvm: svm: Prefervcpu->cputoraw_smp_processor_id()
kvm: svm: Always setsvm->last_cpuon VMRUN
kvm: vmx: Addlast_cputo structvcpu_vmx
kvm: x86: Add "last CPU" to someKVM_EXITinformation
kvm: x86: Movelast_cpuintokvm_vcpu_archaslast_vmentry_cpu
kvm: x86: Setlast_vmentry_cpuinvcpu_enter_guest
Quoted 10 lines
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