Skip to content

Commit 94428e3

Browse files
committed
KVM: TDX: Convert INIT_MEM_REGION and INIT_VCPU to "unlocked" vCPU ioctl
Handle the KVM_TDX_INIT_MEM_REGION and KVM_TDX_INIT_VCPU vCPU sub-ioctls in the unlocked variant, i.e. outside of vcpu->mutex, in anticipation of taking kvm->lock along with all other vCPU mutexes, at which point the sub-ioctls _must_ start without vcpu->mutex held. No functional change intended. Reviewed-by: Kai Huang <kai.huang@intel.com> Co-developed-by: Yan Zhao <yan.y.zhao@intel.com> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Kai Huang <kai.huang@intel.com> Link: https://patch.msgid.link/20251030200951.3402865-24-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 59d5c1e commit 94428e3

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ KVM_X86_OP(enable_smi_window)
128128
KVM_X86_OP_OPTIONAL(dev_get_attr)
129129
KVM_X86_OP_OPTIONAL(mem_enc_ioctl)
130130
KVM_X86_OP_OPTIONAL(vcpu_mem_enc_ioctl)
131+
KVM_X86_OP_OPTIONAL(vcpu_mem_enc_unlocked_ioctl)
131132
KVM_X86_OP_OPTIONAL(mem_enc_register_region)
132133
KVM_X86_OP_OPTIONAL(mem_enc_unregister_region)
133134
KVM_X86_OP_OPTIONAL(vm_copy_enc_context_from)

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ struct kvm_x86_ops {
19141914
int (*dev_get_attr)(u32 group, u64 attr, u64 *val);
19151915
int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp);
19161916
int (*vcpu_mem_enc_ioctl)(struct kvm_vcpu *vcpu, void __user *argp);
1917+
int (*vcpu_mem_enc_unlocked_ioctl)(struct kvm_vcpu *vcpu, void __user *argp);
19171918
int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp);
19181919
int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp);
19191920
int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd);

arch/x86/kvm/vmx/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,14 @@ static int vt_vcpu_mem_enc_ioctl(struct kvm_vcpu *vcpu, void __user *argp)
831831
return tdx_vcpu_ioctl(vcpu, argp);
832832
}
833833

834+
static int vt_vcpu_mem_enc_unlocked_ioctl(struct kvm_vcpu *vcpu, void __user *argp)
835+
{
836+
if (!is_td_vcpu(vcpu))
837+
return -EINVAL;
838+
839+
return tdx_vcpu_unlocked_ioctl(vcpu, argp);
840+
}
841+
834842
static int vt_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn,
835843
bool is_private)
836844
{
@@ -1005,6 +1013,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
10051013

10061014
.mem_enc_ioctl = vt_op_tdx_only(mem_enc_ioctl),
10071015
.vcpu_mem_enc_ioctl = vt_op_tdx_only(vcpu_mem_enc_ioctl),
1016+
.vcpu_mem_enc_unlocked_ioctl = vt_op_tdx_only(vcpu_mem_enc_unlocked_ioctl),
10081017

10091018
.gmem_max_mapping_level = vt_op_tdx_only(gmem_max_mapping_level)
10101019
};

arch/x86/kvm/vmx/tdx.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,6 +3181,42 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c
31813181
return ret;
31823182
}
31833183

3184+
int tdx_vcpu_unlocked_ioctl(struct kvm_vcpu *vcpu, void __user *argp)
3185+
{
3186+
struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm);
3187+
struct kvm_tdx_cmd cmd;
3188+
int r;
3189+
3190+
r = tdx_get_cmd(argp, &cmd);
3191+
if (r)
3192+
return r;
3193+
3194+
if (!is_hkid_assigned(kvm_tdx) || kvm_tdx->state == TD_STATE_RUNNABLE)
3195+
return -EINVAL;
3196+
3197+
if (mutex_lock_killable(&vcpu->mutex))
3198+
return -EINTR;
3199+
3200+
vcpu_load(vcpu);
3201+
3202+
switch (cmd.id) {
3203+
case KVM_TDX_INIT_MEM_REGION:
3204+
r = tdx_vcpu_init_mem_region(vcpu, &cmd);
3205+
break;
3206+
case KVM_TDX_INIT_VCPU:
3207+
r = tdx_vcpu_init(vcpu, &cmd);
3208+
break;
3209+
default:
3210+
r = -ENOIOCTLCMD;
3211+
break;
3212+
}
3213+
3214+
vcpu_put(vcpu);
3215+
3216+
mutex_unlock(&vcpu->mutex);
3217+
return r;
3218+
}
3219+
31843220
int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp)
31853221
{
31863222
struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm);
@@ -3195,12 +3231,6 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp)
31953231
return ret;
31963232

31973233
switch (cmd.id) {
3198-
case KVM_TDX_INIT_VCPU:
3199-
ret = tdx_vcpu_init(vcpu, &cmd);
3200-
break;
3201-
case KVM_TDX_INIT_MEM_REGION:
3202-
ret = tdx_vcpu_init_mem_region(vcpu, &cmd);
3203-
break;
32043234
case KVM_TDX_GET_CPUID:
32053235
ret = tdx_vcpu_get_cpuid(vcpu, &cmd);
32063236
break;

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ int tdx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
149149
int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
150150

151151
int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp);
152+
int tdx_vcpu_unlocked_ioctl(struct kvm_vcpu *vcpu, void __user *argp);
152153

153154
void tdx_flush_tlb_current(struct kvm_vcpu *vcpu);
154155
void tdx_flush_tlb_all(struct kvm_vcpu *vcpu);

arch/x86/kvm/x86.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7243,6 +7243,13 @@ static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp)
72437243
long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
72447244
unsigned long arg)
72457245
{
7246+
struct kvm_vcpu *vcpu = filp->private_data;
7247+
void __user *argp = (void __user *)arg;
7248+
7249+
if (ioctl == KVM_MEMORY_ENCRYPT_OP &&
7250+
kvm_x86_ops.vcpu_mem_enc_unlocked_ioctl)
7251+
return kvm_x86_call(vcpu_mem_enc_unlocked_ioctl)(vcpu, argp);
7252+
72467253
return -ENOIOCTLCMD;
72477254
}
72487255

0 commit comments

Comments
 (0)