Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp841364pxb; Tue, 14 Sep 2021 09:50:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcD2QjGLBnkAeU+hjtZZSHgKlcYfe5KskBxTQuWIh/RQjHW9JPFpVMw1i3yJ1NwjlSFR76 X-Received: by 2002:a05:6638:37a5:: with SMTP id w37mr15517378jal.74.1631638212004; Tue, 14 Sep 2021 09:50:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631638211; cv=none; d=google.com; s=arc-20160816; b=J3D+FOJEi1P0tC3wvw77K3dQLzGaljbqcc0fY9++TYV9lyDs+e5XxJ+/NZ3i4wOCmg hXQiFghD/7oZcFJRCQq5DCDv6zLTky57LRbEBtjNr2Rgs6rCQoQFeDB7tb2jdlWpkjNf ENa3tG8pCZQu8iXbRz/p5/uO/JnXEO9kIIcpCo5uxWO/BM0mJn2vppC1YVoQ3Lp1LUY/ Bog6jLYCSKEIGhHw6JiGzQV7lKEEB89D2pR3lJ0KAAfQMGc86rlSrm9gOYuKgEn5l74o xIQb/807T2r3gHKPm5JmLn2jN63VmgYOri8xmhB0hXBWZ7rP18m+VRtwnTMDCvr4mrJ1 NDSg== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=+f6h/X5NIPeStfEfpLAEhC/0Xz4xmxvZvD1XBnS1esg=; b=xsEliiraQfppIn92eMikwM/FJWDFyZPvTrVpThJgTQ9LFuoJTQM1FJEwlHndBbmUux r0qzobPDsFpgHhPJM/iZ2iGDJPU4FnoZenGDdZxKuseD4vYnHOhLE64+A0BYUOUIW6A0 /h60koBaC7BowLHhNc3QW/p7aTsQ/8FnsfDYjuwAiMVRHTBL9NX2RN41Tg+4z+xv1SzF w5K+C5bxa45R26l0Ywn3QofA/mbYhRyXGSOpDt5gN4OQqXBWp4TqdJWtCJP9EHp9cgE2 ZHTNchYrHslHfeZZI3T7bqf+b+6RJ/wrEC4a6uds28uOzhvv1ERrmvaqyTj6gKfZIWIw ZzPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=s25Zrxmc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x3si10807845ile.168.2021.09.14.09.49.56; Tue, 14 Sep 2021 09:50:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=s25Zrxmc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230270AbhINQs4 (ORCPT + 99 others); Tue, 14 Sep 2021 12:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230109AbhINQsw (ORCPT ); Tue, 14 Sep 2021 12:48:52 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28A37C061574 for ; Tue, 14 Sep 2021 09:47:35 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id o130-20020a62cd88000000b004053c6c1765so8647229pfg.6 for ; Tue, 14 Sep 2021 09:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=+f6h/X5NIPeStfEfpLAEhC/0Xz4xmxvZvD1XBnS1esg=; b=s25ZrxmcmGz46Jr/zJj31XUIp6vh8x7CNYXs1W93DsaANB2p8yRydA+24LZeQNFtkP xwi+cF9htLs/1Mi9F/lNEjN/NbjKL3Kg48DEehxiqSXRdRtVSQt9IMRV4GlaRjgUJiLd aoaFfwGW4Uow2Aeq0vLzTw2ariB9L/A0jcuMCvCiHTwf/b0Ii/FXnBtJMvZt1rP9ypUr 9B3ra/OvJvoV1MvCeVLMxComyVq4xizkGgboFdUsMidwkkf/+LAr2690ulJUthiAnSi3 EZMSWLVrV8WXdnk8EB8OtqNmblBn2dl8b0cMBhJQbYiLPixKfZEUVQoh4y11FBh45Vv4 Qf/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=+f6h/X5NIPeStfEfpLAEhC/0Xz4xmxvZvD1XBnS1esg=; b=a1hskv0QKdMTJ6Sosk8MnnGfFRxx0rmle0ok0NthpTaCEaXq4Sqp2f2zxsbKiFjxeR 1qm0EB+r1WHRAKQCFC+thnpnhyWawE/qoBOIV6/+kElNn4rX9J/YRHUHM9U+V7sDQGtp uHc6cDTplAKSzms+l1NQSP9wu8XgMMfwkLi1wh4RZprlLYOrr/aKxW/XxusvyOJ3lw2k oFuO0qMrEo7YfHJNFR8FDOMOwB29L+ebD+9T7FFx/UHWB/Y4yVP22Muv0a025Ga1Gs7P RBfqifYGNzE0JwTw57fdheGKJi8dHaQa4/++bnpM4lWFQ2E9iR6A4mPyHrOpF0J4s1IC AH+Q== X-Gm-Message-State: AOAM532n55v9siCNGb+bWC19g0WigBbuxoktKYI4oROR+GPAI5hebmL2 vrjC64gVOWpArqUz0wBsjPK8OyW7c9Y= X-Received: from pgonda1.kir.corp.google.com ([2620:15c:29:204:b358:1f40:79d5:ab23]) (user=pgonda job=sendgmr) by 2002:a17:90b:4a84:: with SMTP id lp4mr3205571pjb.34.1631638054604; Tue, 14 Sep 2021 09:47:34 -0700 (PDT) Date: Tue, 14 Sep 2021 09:47:25 -0700 In-Reply-To: <20210914164727.3007031-1-pgonda@google.com> Message-Id: <20210914164727.3007031-3-pgonda@google.com> Mime-Version: 1.0 References: <20210914164727.3007031-1-pgonda@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH 2/4 V8] KVM: SEV: Add support for SEV-ES intra host migration From: Peter Gonda To: kvm@vger.kernel.org Cc: Peter Gonda , Marc Orr , Paolo Bonzini , Sean Christopherson , David Rientjes , "Dr . David Alan Gilbert" , Brijesh Singh , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For SEV-ES to work with intra host migration the VMSAs, GHCB metadata, and other SEV-ES info needs to be preserved along with the guest's memory. Signed-off-by: Peter Gonda Reviewed-by: Marc Orr Cc: Marc Orr Cc: Paolo Bonzini Cc: Sean Christopherson Cc: David Rientjes Cc: Dr. David Alan Gilbert Cc: Brijesh Singh Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: Joerg Roedel Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- arch/x86/kvm/svm/sev.c | 53 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6fc1935b52ea..321b55654f36 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1576,6 +1576,51 @@ static void sev_migrate_from(struct kvm_sev_info *dst, list_replace_init(&src->regions_list, &dst->regions_list); } +static int sev_es_migrate_from(struct kvm *dst, struct kvm *src) +{ + int i; + struct kvm_vcpu *dst_vcpu, *src_vcpu; + struct vcpu_svm *dst_svm, *src_svm; + + if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus)) + return -EINVAL; + + kvm_for_each_vcpu(i, src_vcpu, src) { + if (!src_vcpu->arch.guest_state_protected) + return -EINVAL; + } + + kvm_for_each_vcpu(i, src_vcpu, src) { + src_svm = to_svm(src_vcpu); + dst_vcpu = dst->vcpus[i]; + dst_vcpu = kvm_get_vcpu(dst, i); + dst_svm = to_svm(dst_vcpu); + + /* + * Transfer VMSA and GHCB state to the destination. Nullify and + * clear source fields as appropriate, the state now belongs to + * the destination. + */ + dst_vcpu->vcpu_id = src_vcpu->vcpu_id; + dst_svm->vmsa = src_svm->vmsa; + src_svm->vmsa = NULL; + dst_svm->ghcb = src_svm->ghcb; + src_svm->ghcb = NULL; + dst_svm->vmcb->control.ghcb_gpa = src_svm->vmcb->control.ghcb_gpa; + dst_svm->ghcb_sa = src_svm->ghcb_sa; + src_svm->ghcb_sa = NULL; + dst_svm->ghcb_sa_len = src_svm->ghcb_sa_len; + src_svm->ghcb_sa_len = 0; + dst_svm->ghcb_sa_sync = src_svm->ghcb_sa_sync; + src_svm->ghcb_sa_sync = false; + dst_svm->ghcb_sa_free = src_svm->ghcb_sa_free; + src_svm->ghcb_sa_free = false; + } + to_kvm_svm(src)->sev_info.es_active = false; + + return 0; +} + int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) { struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info; @@ -1604,7 +1649,7 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) if (ret) goto out_fput; - if (!sev_guest(source_kvm) || sev_es_guest(source_kvm)) { + if (!sev_guest(source_kvm)) { ret = -EINVAL; goto out_source; } @@ -1615,6 +1660,12 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) if (ret) goto out_source_vcpu; + if (sev_es_guest(source_kvm)) { + ret = sev_es_migrate_from(kvm, source_kvm); + if (ret) + goto out_source_vcpu; + } + sev_migrate_from(dst_sev, &to_kvm_svm(source_kvm)->sev_info); kvm_for_each_vcpu (i, vcpu, source_kvm) { kvm_vcpu_reset(vcpu, /* init_event= */ false); -- 2.33.0.309.g3052b89438-goog