From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C64EC433FE for ; Wed, 15 Sep 2021 17:18:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0AAB36105A for ; Wed, 15 Sep 2021 17:18:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230017AbhIORTT (ORCPT ); Wed, 15 Sep 2021 13:19:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbhIORTS (ORCPT ); Wed, 15 Sep 2021 13:19:18 -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 3D0CBC061574 for ; Wed, 15 Sep 2021 10:17:59 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id y127-20020a623285000000b0043deb9a379dso2169815pfy.19 for ; Wed, 15 Sep 2021 10:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=r8HJUwu+ryJ7nGO/o5ObN7ZG57DnEl80AhMlVx8YKrw=; b=OhxQgrQGX2IxTWw2nx1ea95CmDC6zFPVX/tes5tJfOxJtHTnlR+hdx6SzCTGntvE7A kJbhe9eTq+55s2OK0ma7fUwJh0H+JMVSW9KA97xm10Uq26ObdWpa2Ie0ixVOeWL4ZTXf K9chFqEW6J2dXjyvTRfqMHlo7YBV/0yNOcJ9fl1qfX/wiq+Q8eMXTxzRoAT8Wmclj5Rw E79jdHo2FHBuYVQGLdlEQSpxOcSCOiBMw3ZXtCqbEbkO32y6PkpJbZDxcFi1zxFX00cl 0soJCge27mVw0cUF3m6JuSvtfc1VuRC2HLIXMhgCjtGbSeBmvxURdOw3CwW6HCAEEksO Nwfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=r8HJUwu+ryJ7nGO/o5ObN7ZG57DnEl80AhMlVx8YKrw=; b=c+yjXbFkduht/b4EIACOBky3On+dq+5xTTlmivxHwSKuBYsPboONU04bFCfBCFLLx6 LF4eXN8ULUc2x7c+6uUccoS+ZQ7YytC4ULjtH0U+UnOjqb/H495j3YAmK9HU6/QKlSXY hJeJeYow+dwdrEnhB9nM0fg2I99jM+yW8Dbb6BKpJKxMcgbHhVL+amNu8XDjfUDPUTwM blqLb61dE5tFM792IqHevvnMgcqB0u52A623EZ/LZtCQkO9y9u5wPZ130G/zQe6VChTn ZjkShd3p1mrMbX85mIJ1PvEBXOceGN889l8OhxtuxIgmndPvIh7pYXIeGxgfnOdAeORv MaYw== X-Gm-Message-State: AOAM531IbR6mSpboVkeOcblHdd8uuSxkpJkL72lRKVTlBZVfQoevktYB QjlyrzQDHRqlo/RElkSkGVyYkrrS+z8= X-Google-Smtp-Source: ABdhPJx3oPusIcEI63lSFHTu8CTZnjejnRB+WtLO2ePsPEe2W9YW9GjGOpf+qWWkKbAcn4svKE0grwu4PhI= X-Received: from pgonda1.kir.corp.google.com ([2620:15c:29:204:4d5e:3ba7:52f7:ec45]) (user=pgonda job=sendgmr) by 2002:a17:90a:fd85:: with SMTP id cx5mr9728475pjb.168.1631726278586; Wed, 15 Sep 2021 10:17:58 -0700 (PDT) Date: Wed, 15 Sep 2021 10:17:55 -0700 Message-Id: <20210915171755.3773766-1-pgonda@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog Subject: [PATCH V2] KVM: SEV: Acquire vcpu mutex when updating VMSA From: Peter Gonda To: kvm@vger.kernel.org Cc: Peter Gonda , Marc Orr , Paolo Bonzini , Sean Christopherson , Brijesh Singh , stable@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Adds vcpu mutex guard to the VMSA updating code. Refactors out __sev_launch_update_vmsa() function to deal with per vCPU parts of sev_launch_update_vmsa(). Fixes: ad73109ae7ec ("KVM: SVM: Provide support to launch and run an SEV-ES guest") Signed-off-by: Peter Gonda Cc: Marc Orr Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Brijesh Singh Cc: kvm@vger.kernel.org Cc: stable@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- V2 * Refactor per vcpu work to separate function. * Remove check to skip already initialized VMSAs. * Removed vmsa struct zeroing. --- arch/x86/kvm/svm/sev.c | 53 ++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 75e0b21ad07c..766510fe3abb 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -595,43 +595,50 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) return 0; } -static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, + int *error) { - struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; struct sev_data_launch_update_vmsa vmsa; + struct vcpu_svm *svm = to_svm(vcpu); + int ret; + + /* Perform some pre-encryption checks against the VMSA */ + ret = sev_es_sync_vmsa(svm); + if (ret) + return ret; + + /* + * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of + * the VMSA memory content (i.e it will write the same memory region + * with the guest's key), so invalidate it first. + */ + clflush_cache_range(svm->vmsa, PAGE_SIZE); + + vmsa.reserved = 0; + vmsa.handle = to_kvm_svm(kvm)->sev_info.handle; + vmsa.address = __sme_pa(svm->vmsa); + vmsa.len = PAGE_SIZE; + return sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, error); +} + +static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ struct kvm_vcpu *vcpu; int i, ret; if (!sev_es_guest(kvm)) return -ENOTTY; - vmsa.reserved = 0; - - kvm_for_each_vcpu(i, vcpu, kvm) { - struct vcpu_svm *svm = to_svm(vcpu); - - /* Perform some pre-encryption checks against the VMSA */ - ret = sev_es_sync_vmsa(svm); + kvm_for_each_vcpu(i, vcpu, kvm) { + ret = mutex_lock_killable(&vcpu->mutex); if (ret) return ret; - /* - * The LAUNCH_UPDATE_VMSA command will perform in-place - * encryption of the VMSA memory content (i.e it will write - * the same memory region with the guest's key), so invalidate - * it first. - */ - clflush_cache_range(svm->vmsa, PAGE_SIZE); + ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error); - vmsa.handle = sev->handle; - vmsa.address = __sme_pa(svm->vmsa); - vmsa.len = PAGE_SIZE; - ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, &vmsa, - &argp->error); + mutex_unlock(&vcpu->mutex); if (ret) return ret; - - svm->vcpu.arch.guest_state_protected = true; } return 0; -- 2.33.0.464.g1972c5931b-goog