KVM: x86: Rework kvm_x86_ops.vcpu_pre_run() into .vcpu_needs_initialization()

Rework .vcpu_pre_run() into a more specific .vcpu_needs_initialization() to
consolidate the SNP and TDX control flows, and to eliminate the potentially
confusing almost-collision between svm_vcpu_pre_run() and pre_sev_run(): the
former is SEV specific, but is pre-KVM_RUN, whereas the latter is pre-VMRUN.

Link: https://patch.msgid.link/20260731173340.2644656-4-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson 2026-07-31 10:33:37 -07:00
parent 281a4e5f10
commit 9fd7a4433d
10 changed files with 21 additions and 33 deletions

View File

@ -63,7 +63,7 @@ KVM_X86_OP_OPTIONAL(flush_remote_tlbs_range)
#endif
KVM_X86_OP(flush_tlb_gva)
KVM_X86_OP(flush_tlb_guest)
KVM_X86_OP(vcpu_pre_run)
KVM_X86_OP_OPTIONAL_RET0(vcpu_needs_initialization)
KVM_X86_OP(vcpu_run)
KVM_X86_OP(handle_exit)
KVM_X86_OP(skip_emulated_instruction)

View File

@ -1584,7 +1584,7 @@ struct kvm_x86_ops {
*/
void (*flush_tlb_guest)(struct kvm_vcpu *vcpu);
int (*vcpu_pre_run)(struct kvm_vcpu *vcpu);
bool (*vcpu_needs_initialization)(struct kvm_vcpu *vcpu);
enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu,
u64 run_flags);
int (*handle_exit)(struct kvm_vcpu *vcpu,

View File

@ -3530,6 +3530,11 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
__sev_es_unmap_ghcb(svm);
}
bool sev_vcpu_needs_initialization(struct kvm_vcpu *vcpu)
{
return to_kvm_sev_info(vcpu->kvm)->need_init;
}
int pre_sev_run(struct vcpu_svm *svm, int cpu)
{
struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);

View File

@ -4387,16 +4387,6 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)
svm_complete_interrupts(vcpu);
}
static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu)
{
#ifdef CONFIG_KVM_AMD_SEV
if (to_kvm_sev_info(vcpu->kvm)->need_init)
return -EINVAL;
#endif
return 1;
}
static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
{
struct vcpu_svm *svm = to_svm(vcpu);
@ -5368,7 +5358,6 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
.flush_tlb_gva = svm_flush_tlb_gva,
.flush_tlb_guest = svm_flush_tlb_guest,
.vcpu_pre_run = svm_vcpu_pre_run,
.vcpu_run = svm_vcpu_run,
.handle_exit = svm_handle_exit,
.skip_emulated_instruction = svm_skip_emulated_instruction,
@ -5426,6 +5415,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
#endif
#ifdef CONFIG_KVM_AMD_SEV
.vcpu_needs_initialization = sev_vcpu_needs_initialization,
.dev_get_attr = sev_dev_get_attr,
.mem_enc_ioctl = sev_mem_enc_ioctl,
.mem_enc_register_region = sev_mem_enc_register_region,

View File

@ -978,6 +978,7 @@ void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_are
void sev_es_unmap_ghcb(struct vcpu_svm *svm);
#ifdef CONFIG_KVM_AMD_SEV
bool sev_vcpu_needs_initialization(struct kvm_vcpu *vcpu);
int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp);
int sev_mem_enc_register_region(struct kvm *kvm,
struct kvm_enc_region *range);

View File

@ -140,12 +140,10 @@ static void vt_vcpu_put(struct kvm_vcpu *vcpu)
vmx_vcpu_put(vcpu);
}
static int vt_vcpu_pre_run(struct kvm_vcpu *vcpu)
static bool vt_vcpu_needs_initialization(struct kvm_vcpu *vcpu)
{
if (is_td_vcpu(vcpu))
return tdx_vcpu_pre_run(vcpu);
return vmx_vcpu_pre_run(vcpu);
return is_td_vcpu(vcpu) &&
tdx_vcpu_needs_initialization(vcpu);
}
static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
@ -949,7 +947,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
.flush_tlb_gva = vt_op(flush_tlb_gva),
.flush_tlb_guest = vt_op(flush_tlb_guest),
.vcpu_pre_run = vt_op(vcpu_pre_run),
.vcpu_needs_initialization = vt_op_tdx_only(vcpu_needs_initialization),
.vcpu_run = vt_op(vcpu_run),
.handle_exit = vt_op(handle_exit),
.skip_emulated_instruction = vmx_skip_emulated_instruction,

View File

@ -907,13 +907,10 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)
tdx->state = VCPU_TD_STATE_UNINITIALIZED;
}
int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu)
bool tdx_vcpu_needs_initialization(struct kvm_vcpu *vcpu)
{
if (unlikely(to_tdx(vcpu)->state != VCPU_TD_STATE_INITIALIZED ||
to_kvm_tdx(vcpu->kvm)->state != TD_STATE_RUNNABLE))
return -EINVAL;
return 1;
return to_tdx(vcpu)->state != VCPU_TD_STATE_INITIALIZED ||
to_kvm_tdx(vcpu->kvm)->state != TD_STATE_RUNNABLE;
}
static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu)

View File

@ -6109,11 +6109,6 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
return 1;
}
int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu)
{
return 1;
}
/*
* Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
* exiting, so only get here on cpu with PAUSE-Loop-Exiting.

View File

@ -21,7 +21,6 @@ int vmx_vm_init(struct kvm *kvm);
void vmx_vm_destroy(struct kvm *kvm);
int vmx_vcpu_precreate(struct kvm *kvm);
int vmx_vcpu_create(struct kvm_vcpu *vcpu);
int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu);
fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags);
void vmx_vcpu_free(struct kvm_vcpu *vcpu);
void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);
@ -138,7 +137,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);
void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);
void tdx_vcpu_free(struct kvm_vcpu *vcpu);
void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu);
bool tdx_vcpu_needs_initialization(struct kvm_vcpu *vcpu);
fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags);
void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
void tdx_vcpu_put(struct kvm_vcpu *vcpu);

View File

@ -8862,12 +8862,15 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcpu)
!kvm_apic_init_sipi_allowed(vcpu))
return -EINVAL;
if (kvm_x86_call(vcpu_needs_initialization)(vcpu))
return -EINVAL;
if (kvm_x86_call(unhandleable_emulation_required)(vcpu)) {
kvm_prepare_emulation_failure_exit(vcpu);
return 0;
}
return kvm_x86_call(vcpu_pre_run)(vcpu);
return 1;
}
int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)