From patchwork Fri Apr 7 20:19:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 81025 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp514281vqo; Fri, 7 Apr 2023 13:33:04 -0700 (PDT) X-Google-Smtp-Source: AKy350bie7PF6Ka+N0rYRO24cZMljkqm8qlVxQkf5Ya58TbYq4ziI2s9iK9TQWBKGhQwVBEWTVNi X-Received: by 2002:a05:6402:7c3:b0:4fb:b09e:4c8b with SMTP id u3-20020a05640207c300b004fbb09e4c8bmr3301965edy.37.1680899583825; Fri, 07 Apr 2023 13:33:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680899583; cv=none; d=google.com; s=arc-20160816; b=KwRZ9WMn9ju+XScRZKmCprQhudHTrTvFIptlkJrp9LvNw9uTEVwYyMnGNpxJHbX2kh tJZCZPcAg9vhyrO0SFObW8n5qwRG0xvnxuHZ2+NpI0w3GVdBe05FZByI9YSkd6lL+C3c RovVJYA5dMWh6F9GR0JgRfRn/MC1eR1jMckDAB13zDCjjC3qpprGuIEdQnpsEruKxU79 QKsLeAGQYw9cNxicwmv5du7fGDoZ4TbMBpr+8agQBs6SDsdHC4+8jaHVsXR/gWMHsTp+ WX6XkzabrsZcIvwezAnMZ387fLjc//exKP3D35RVee/WM1fJHrEr+Ak3Ues636pLq64C fZMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=GgRFXdpbsewXFN8nfe13N4oAoHTkIOTXjaNF6DLenHQ=; b=GqGUvlVbR0dVCzcQMWSm/Fy7fKj43v76Zu47NLiA/xIqs1NuvRHog617k2J3zITCwg lSlyM8kAdhb/vFeL3o4edCWIRjAhEasMobmKnlE2TbYhlKK0Yxx1KX8/SzzLKt2kkxL5 j1ulbR5eTSreulCJ5wX/Ao61lwHwY2h3GCrJ0k5cjBi6MN524Er0yrmBgrzos+TU5YHR 4YMZZgLQtvJWVfcLHSE6d2gdlnjK6X3vHyA4Xv0t/C3rUVVkBDQVMc/xNVBO6Vy+2wwX 7cfS/WDtp5OjCq/ZKq9XSGsBdWEWGQLBQtrW9KfdDXbg0lirgff2/Blxe/nR568hzZfu c5nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bUPKu4nh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u17-20020a17090626d100b00936255a28b0si3400920ejc.865.2023.04.07.13.32.39; Fri, 07 Apr 2023 13:33:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bUPKu4nh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230445AbjDGUUO (ORCPT + 99 others); Fri, 7 Apr 2023 16:20:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230446AbjDGUUK (ORCPT ); Fri, 7 Apr 2023 16:20:10 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A190FC66B for ; Fri, 7 Apr 2023 13:20:04 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-632389f445cso1050b3a.0 for ; Fri, 07 Apr 2023 13:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680898804; x=1683490804; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GgRFXdpbsewXFN8nfe13N4oAoHTkIOTXjaNF6DLenHQ=; b=bUPKu4nhvU6AIpMyXIiHt43tbAVl8CQ5zUEZSFuYtVCkd015VQ7DKPwcFXmkmH17As lVLq79uj/NQdWNkY26hN2EggIFXZpzKO7MNJr65yXQcHcpHpQeTfnrxw50qg9C4gx8RL iy6eCReaQSIdPkYWlJzdeM1yXiqFLrTJzBWQ+KyLOH2z0fuAXahHqDM3ShgTW6UQ8eiS 71szTjXJlV/MBpQ6pN2g2pLgUqRonwbez8iewZcmNKFaJDdvBEolIbXNt2Ioa/Qw1pE1 lRrvA7nz+YB0FqnzFEDqCWuFkMZ4mLINvwECc8AohsUCtkcFdtPYhDRkIHP8oomf5Drr OrxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680898804; x=1683490804; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GgRFXdpbsewXFN8nfe13N4oAoHTkIOTXjaNF6DLenHQ=; b=ueetEMczomD0UuiVggyZQPxVo68R856gQTmBsBtQMaWpyKGumQKiXT2yJVYKeRTUAj W/CQKFg9UoEzeBvovagQrE5yKKkOr2puXs6PxB8HBJqzzNgtbKdpSAbRRP7CBBr2BoMW xtHayZUdTx8goELwswX9of0D4wLQN9yFzbnKDYC4YKhAc2npLL9qHPyElC1zWJ/3iTfI 6YlJhg2x6O+ZfyxQF8eJE3CWLfon0muvO9g9o95ONU06rmRjlaAOF+x47iURMupl2XvP 28r+S3C0q9ozl4i8f95aLDWLCerCWUI8zUiTNHIwyKQmuYJjUCXXj3J8R4sV/JVLRMGY xwag== X-Gm-Message-State: AAQBX9cl0rlbJXVfIxStr//f3QoBqz3J31SGRsLEzHCYOIPFKnM5xfCu KPjmlAhFfzbw8LzOoTCf7wAWOKaBQw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:6a00:2d20:b0:627:d4fa:6a9c with SMTP id fa32-20020a056a002d2000b00627d4fa6a9cmr1663271pfb.6.1680898803826; Fri, 07 Apr 2023 13:20:03 -0700 (PDT) Date: Fri, 7 Apr 2023 20:19:19 +0000 In-Reply-To: <20230407201921.2703758-1-sagis@google.com> Mime-Version: 1.0 References: <20230407201921.2703758-1-sagis@google.com> X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230407201921.2703758-4-sagis@google.com> Subject: [RFC PATCH 3/5] KVM: TDX: Add base implementation for tdx_vm_move_enc_context_from From: Sagi Shahar To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Cc: Sean Christopherson , Paolo Bonzini , Isaku Yamahata , Erdem Aktas , David Matlack , Kai Huang , Zhi Wang , Chao Peng , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Sagi Shahar X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762550961834756924?= X-GMAIL-MSGID: =?utf-8?q?1762550961834756924?= This should mostly match the logic in sev_vm_move_enc_context_from. Signed-off-by: Sagi Shahar --- arch/x86/kvm/vmx/main.c | 10 +++++++ arch/x86/kvm/vmx/tdx.c | 56 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/x86_ops.h | 5 ++++ 4 files changed, 73 insertions(+) diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 5b64fe5404958..9d5d0ac465bf6 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -979,6 +979,14 @@ static int vt_vcpu_mem_enc_ioctl(struct kvm_vcpu *vcpu, void __user *argp) return tdx_vcpu_ioctl(vcpu, argp); } +static int vt_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) +{ + if (!is_td(kvm)) + return -ENOTTY; + + return tdx_vm_move_enc_context_from(kvm, source_fd); +} + #define VMX_REQUIRED_APICV_INHIBITS \ ( \ BIT(APICV_INHIBIT_REASON_DISABLE)| \ @@ -1141,6 +1149,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .dev_mem_enc_ioctl = tdx_dev_ioctl, .mem_enc_ioctl = vt_mem_enc_ioctl, .vcpu_mem_enc_ioctl = vt_vcpu_mem_enc_ioctl, + + .vm_move_enc_context_from = vt_move_enc_context_from, }; struct kvm_x86_init_ops vt_init_ops __initdata = { diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 8af7e4e81c860..0999a6d827c99 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -2826,3 +2826,59 @@ int __init tdx_init(void) INIT_LIST_HEAD(&per_cpu(associated_tdvcpus, cpu)); return 0; } + +static __always_inline bool tdx_guest(struct kvm *kvm) +{ + struct kvm_tdx *tdx_kvm = to_kvm_tdx(kvm); + + return tdx_kvm->finalized; +} + +static int tdx_migrate_from(struct kvm *dst, struct kvm *src) +{ + return -EINVAL; +} + +int tdx_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) +{ + struct kvm_tdx *dst_tdx = to_kvm_tdx(kvm); + struct file *src_kvm_file; + struct kvm_tdx *src_tdx; + struct kvm *src_kvm; + int ret; + + src_kvm_file = fget(source_fd); + if (!file_is_kvm(src_kvm_file)) { + ret = -EBADF; + goto out_fput; + } + src_kvm = src_kvm_file->private_data; + src_tdx = to_kvm_tdx(src_kvm); + + ret = pre_move_enc_context_from(kvm, src_kvm, + &dst_tdx->migration_in_progress, + &src_tdx->migration_in_progress); + if (ret) + goto out_fput; + + if (tdx_guest(kvm) || !tdx_guest(src_kvm)) { + ret = -EINVAL; + goto out_post; + } + + ret = tdx_migrate_from(kvm, src_kvm); + if (ret) + goto out_post; + + kvm_vm_dead(src_kvm); + ret = 0; + +out_post: + post_move_enc_context_from(kvm, src_kvm, + &dst_tdx->migration_in_progress, + &src_tdx->migration_in_progress); +out_fput: + if (src_kvm_file) + fput(src_kvm_file); + return ret; +} diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index 71818c5001862..21b7e710be1fd 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -24,6 +24,8 @@ struct kvm_tdx { atomic_t tdh_mem_track; u64 tsc_offset; + + atomic_t migration_in_progress; }; union tdx_exit_reason { diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index d049e0c72ed0c..275f5d75e9bf1 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -187,6 +187,8 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp); void tdx_flush_tlb(struct kvm_vcpu *vcpu); int tdx_sept_tlb_remote_flush(struct kvm *kvm); void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); + +int tdx_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd); #else static inline int tdx_init(void) { return 0; }; static inline int tdx_hardware_setup(struct kvm_x86_ops *x86_ops) { return -ENOSYS; } @@ -241,6 +243,9 @@ static inline int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp) { ret static inline void tdx_flush_tlb(struct kvm_vcpu *vcpu) {} static inline int tdx_sept_tlb_remote_flush(struct kvm *kvm) { return 0; } static inline void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level) {} + +static inline int tdx_vm_move_enc_context_from(struct kvm *kvm, u + nsigned int source_fd) { return -EOPNOTSUPP; } #endif #if defined(CONFIG_INTEL_TDX_HOST) && defined(CONFIG_KVM_SMM)