Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+55 -6
+1
arch/x86/include/asm/kvm-x86-ops.h
··· 128 128 KVM_X86_OP_OPTIONAL(dev_get_attr) 129 129 KVM_X86_OP_OPTIONAL(mem_enc_ioctl) 130 130 KVM_X86_OP_OPTIONAL(vcpu_mem_enc_ioctl) 131 + KVM_X86_OP_OPTIONAL(vcpu_mem_enc_unlocked_ioctl) 131 132 KVM_X86_OP_OPTIONAL(mem_enc_register_region) 132 133 KVM_X86_OP_OPTIONAL(mem_enc_unregister_region) 133 134 KVM_X86_OP_OPTIONAL(vm_copy_enc_context_from)
+1
arch/x86/include/asm/kvm_host.h
··· 1914 1914 int (*dev_get_attr)(u32 group, u64 attr, u64 *val); 1915 1915 int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp); 1916 1916 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); 1917 1918 int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp); 1918 1919 int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp); 1919 1920 int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
+9
arch/x86/kvm/vmx/main.c
··· 831 831 return tdx_vcpu_ioctl(vcpu, argp); 832 832 } 833 833 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 + 834 842 static int vt_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, 835 843 bool is_private) 836 844 { ··· 1013 1005 1014 1006 .mem_enc_ioctl = vt_op_tdx_only(mem_enc_ioctl), 1015 1007 .vcpu_mem_enc_ioctl = vt_op_tdx_only(vcpu_mem_enc_ioctl), 1008 + .vcpu_mem_enc_unlocked_ioctl = vt_op_tdx_only(vcpu_mem_enc_unlocked_ioctl), 1016 1009 1017 1010 .gmem_max_mapping_level = vt_op_tdx_only(gmem_max_mapping_level) 1018 1011 };
+36 -6
arch/x86/kvm/vmx/tdx.c
··· 3181 3181 return ret; 3182 3182 } 3183 3183 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 + 3184 3220 int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp) 3185 3221 { 3186 3222 struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); ··· 3231 3195 return ret; 3232 3196 3233 3197 switch (cmd.id) { 3234 - case KVM_TDX_INIT_VCPU: 3235 - ret = tdx_vcpu_init(vcpu, &cmd); 3236 - break; 3237 - case KVM_TDX_INIT_MEM_REGION: 3238 - ret = tdx_vcpu_init_mem_region(vcpu, &cmd); 3239 - break; 3240 3198 case KVM_TDX_GET_CPUID: 3241 3199 ret = tdx_vcpu_get_cpuid(vcpu, &cmd); 3242 3200 break;
+1
arch/x86/kvm/vmx/x86_ops.h
··· 149 149 int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr); 150 150 151 151 int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp); 152 + int tdx_vcpu_unlocked_ioctl(struct kvm_vcpu *vcpu, void __user *argp); 152 153 153 154 void tdx_flush_tlb_current(struct kvm_vcpu *vcpu); 154 155 void tdx_flush_tlb_all(struct kvm_vcpu *vcpu);
+7
arch/x86/kvm/x86.c
··· 7243 7243 long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl, 7244 7244 unsigned long arg) 7245 7245 { 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 + 7246 7253 return -ENOIOCTLCMD; 7247 7254 } 7248 7255