Re: [PATCH v19 101/130] KVM: TDX: handle ept violation/misconfig exit

From: Chao Gao
Date: Mon May 06 2024 - 03:22:04 EST


On Wed, May 01, 2024 at 09:54:07AM -0700, Reinette Chatre wrote:
>diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
>index 499c6cd9633f..ba81e6f68c97 100644
>--- a/arch/x86/kvm/vmx/tdx.c
>+++ b/arch/x86/kvm/vmx/tdx.c
>@@ -1305,11 +1305,20 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
> } else {
> exit_qual = tdexit_exit_qual(vcpu);
> if (exit_qual & EPT_VIOLATION_ACC_INSTR) {
>+ union tdx_exit_reason exit_reason = to_tdx(vcpu)->exit_reason;
>+
>+ /*
>+ * Instruction fetch in TD from shared memory
>+ * causes a #PF.
>+ */

It is better to hoist this comment above the if-statement.

/*
* Instruction fetch in TD from shared memory never causes EPT
* violation. Warn if such an EPT violation occurs as the CPU
* probably is buggy.
*/
if (exit_qual & EPT_VIOLATION_ACC_INSTR) {
...
}


but, I am wondering why KVM needs to perform this check. I prefer to drop this
check if KVM doesn't rely on it to handle EPT violation correctly.

> pr_warn("kvm: TDX instr fetch to shared GPA = 0x%lx @ RIP = 0x%lx\n",
> tdexit_gpa(vcpu), kvm_rip_read(vcpu));

how about using vcpu_unimpl()? it is a wrapper for printing such a message.

>- vcpu->run->exit_reason = KVM_EXIT_EXCEPTION;
>- vcpu->run->ex.exception = PF_VECTOR;
>- vcpu->run->ex.error_code = exit_qual;
>+ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
>+ vcpu->run->internal.suberror =
>+ KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
>+ vcpu->run->internal.ndata = 2;
>+ vcpu->run->internal.data[0] = exit_reason.full;
>+ vcpu->run->internal.data[1] = exit_qual;
> return 0;
> }
> }
>
>Reinette
>
>[1] https://github.com/kvm-x86/linux/blob/next/arch/x86/kvm/vmx/vmx.c#L6587
>[2] https://github.com/kvm-x86/linux/blob/next/arch/x86/kvm/svm/svm.c#L3436