On Fri, 2025-08-29 at 15:39 -0700, Rick Edgecombe wrote: > > > > Anyways, I think we need to avoid the "synchronous" ioctl path anyways, > > because taking kvm->slots_lock inside vcpu->mutex is gross. AFAICT it's not > > actively problematic today, but it feels like a deadlock waiting to happen. > > > > The other oddity I see is the handling of kvm_tdx->state. I don't see how > > this check in tdx_vcpu_create() is safe: > > > > if (kvm_tdx->state != TD_STATE_INITIALIZED) > > return -EIO; > > > > kvm_arch_vcpu_create() runs without any locks held, Oh, you're right. It's about those fields being set further down in the function based on the results of KVM_TDX_INIT_VM, rather then TDX module locking. The race would show if vCPU creation transitioned to TD_STATE_RUNNABLE in finalized while another vCPU was getting created. Though I'm not sure exactly what would go wrong, the code is wrong enough looking to be worth a fix. > > and so TDX effectively has the same bug that SEV intra-host migration had, > > where an in-flight vCPU creation could race with a VM-wide state transition > > (see commit ecf371f8b02d ("KVM: SVM: Reject SEV{-ES} intra host migration if > > vCPU creation is in-flight"). To fix that, kvm->lock needs to be taken and > > KVM needs to verify there's no in-flight vCPU creation, e.g. so that a vCPU > > doesn't pop up and contend a TDX-Module lock. > > > > We an even define a fancy new CLASS to handle the lock+check => unlock logic > > with guard()-like syntax: > > > > CLASS(tdx_vm_state_guard, guard)(kvm); > > if (IS_ERR(guard)) > > return PTR_ERR(guard); > > > > IIUC, with all of those locks, KVM can KVM_BUG_ON() both TDH_MEM_PAGE_ADD > > and TDH_MR_EXTEND, with no exceptions given for -EBUSY. Attached patches > > are very lightly tested as usual and need to be chunked up, but seem do to > > what I want. > > Ok, the direction seem clear. The patch has an issue, need to debug. Just this: diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index c595d9cb6dcd..e99d07611393 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -2809,7 +2809,7 @@ static int tdx_td_finalize(struct kvm *kvm, struct kvm_tdx_cmd *cmd) static int tdx_get_cmd(void __user *argp, struct kvm_tdx_cmd *cmd) { - if (copy_from_user(cmd, argp, sizeof(cmd))) + if (copy_from_user(cmd, argp, sizeof(*cmd))) return -EFAULT; if (cmd->hw_error)