From patchwork Mon Aug 16 20:24:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 497718 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.3 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 1F9FAC432BE for ; Mon, 16 Aug 2021 20:24:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F388C60F5C for ; Mon, 16 Aug 2021 20:24:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232554AbhHPUZ2 (ORCPT ); Mon, 16 Aug 2021 16:25:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232346AbhHPUZ0 (ORCPT ); Mon, 16 Aug 2021 16:25:26 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D49A8C061764 for ; Mon, 16 Aug 2021 13:24:54 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id p71-20020a25424a0000b029056092741626so17852220yba.19 for ; Mon, 16 Aug 2021 13:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=OaovW+VBocncb082CQVnEflU35hVdinYVYmJKw4413A=; b=D7dhyu//8TUnnI0T+zsJwLWKcS8FeoX35kk5eu/gi9PZde3ChNzoygFpGKSrb+4uCK PEJKvri5CRU9N0JP4UpBNfF4Tce7WAwrBBdWMWG/1X3+1B3j+/S6DI3ZIgD03AV9Lgnf 66E5+BuO0+DqPLafB6wT66tITiRTLrjbAjlVhhBLh/F1fhZIRRge06UuLYqQQlaRNDWK PxqtqMLBNixDGRTILPE/N3q+6k2KzH/Nnp7zGQNX9BWv2fsLD8FrEhG4FsYQT9Y/F2LR PUgIwG/U6/x+TTMcxQujy304/K3IboPGL6AInOHyWFJt83+lsHTjEpT1ywieeC/c38sg a8fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=OaovW+VBocncb082CQVnEflU35hVdinYVYmJKw4413A=; b=YT4w659lHIgnT9W9u4Kl4BXRucWAlwkPocDufOOkjRyUowE8aW/FhVI/x73j8yHVuk e2OPwODJ7oaeiuI5u6gqM4pBW7RYPth1CvdRkNBLi4alnkugs8VOGlUqcmQwawBFVbCp tf2JQ6vsjgyJMdBHfVceQfoH+3VjzMkBhRzsWG8rULAu/O8Lm1UrjZDnP52NLsjSIYO+ NjeO4JifFP0zP4sOJW6llt0I9eHvhnDXu7HOt/lMtmOVbhIrR4hhEYeQlttjvhwtSIBE MR8oPru2kdWOv0lylRvPBMZrowtrVaa835LS2ip1iYUWoDKXuuwafXxnOY77/F7e6uY5 S3nQ== X-Gm-Message-State: AOAM5331M7LhmSJMO8VvI99w6PeAqNJ5sYErjRfgszWiAmKM6uYg1QUh N+bzHdvlTUyXzfYnMW2GJ/sTJUnQ+50k X-Google-Smtp-Source: ABdhPJxIMqz27nTJUgfga7qLDAHIDa6ONMo3oIPLOxj5qe0jtmjpu/OkkRKcFSovMz+qrpB61Jqk6yW6P2dU X-Received: from mizhang-super.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1071]) (user=mizhang job=sendgmr) by 2002:a25:d691:: with SMTP id n139mr279100ybg.27.1629145494110; Mon, 16 Aug 2021 13:24:54 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 16 Aug 2021 20:24:40 +0000 In-Reply-To: <20210816202441.4098523-1-mizhang@google.com> Message-Id: <20210816202441.4098523-3-mizhang@google.com> Mime-Version: 1.0 References: <20210816202441.4098523-1-mizhang@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH 2/3] KVM: SVM: move sev_bind_asid to psp From: Mingwei Zhang To: Paolo Bonzini , Brijesh Singh , Tom Lendacky , John Allen Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Alper Gun , Borislav Petkov , David Rienjes , Marc Orr , Peter Gonda , Vipin Sharma , Mingwei Zhang Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org ccp/sev-dev.c is the software layer in psp that allows KVM to manage SEV/ES/SNP enabled VMs. Since psp API provides only primitive sev command invocation, KVM has to do extra processing that are specific only to psp with KVM level wrapper function. sev_bind_asid is such a KVM function that literally wraps around sev_guest_activate in psp with extra steps like psp data structure creation and error processing: invoking sev_guest_decommission on activation failure. Adding sev_guest_decommission is essentially required on all sev_bin_asid call sites. This is error prone and in fact the upstream code in KVM still have an issue on sev_receive_start where sev_guest_decommission is missing. Since sev_bind_asid code logic is purely psp specific, putting it into psp layer should make it more robust, since KVM code does not have to worry about error handling of asid binding failure. So replace the KVM pointer in sev_bind_asid with primitive arguments: asid and handle; slightly change the name to sev_guest_bind_asid make it consistent with other psp APIs; add the error handling code inside sev_guest_bind_asid and; put it into the sev-dev.c. Cc: Alper Gun Cc: Borislav Petkov Cc: Brijesh Singh Cc: David Rienjes Cc: Marc Orr Cc: John Allen Cc: Peter Gonda Cc: Sean Christopherson Cc: Tom Lendacky Cc: Vipin Sharma Fixes: af43cbbf954b ("KVM: SVM: Add support for KVM_SEV_RECEIVE_START command") Signed-off-by: Mingwei Zhang --- arch/x86/kvm/svm/sev.c | 23 ++++------------------- drivers/crypto/ccp/sev-dev.c | 15 +++++++++++++++ include/linux/psp-sev.h | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6a1faf28d973..2a674acb22ce 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -252,20 +252,6 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } -static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) -{ - struct sev_data_activate activate; - int asid = sev_get_asid(kvm); - int ret; - - /* activate ASID on the given handle */ - activate.handle = handle; - activate.asid = asid; - ret = sev_guest_activate(&activate, error); - - return ret; -} - static int __sev_issue_cmd(int fd, int id, void *data, int *error) { struct fd f; @@ -336,11 +322,9 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free_session; /* Bind ASID to this guest */ - ret = sev_bind_asid(kvm, start.handle, error); - if (ret) { - sev_guest_decommission(start.handle, NULL); + ret = sev_guest_bind_asid(sev_get_asid(kvm), start.handle, error); + if (ret) goto e_free_session; - } /* return handle to userspace */ params.handle = start.handle; @@ -1385,7 +1369,8 @@ static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free_session; /* Bind ASID to this guest */ - ret = sev_bind_asid(kvm, start.handle, error); + ret = sev_guest_bind_asid(sev_get_asid(kvm), start.handle, error); + if (ret) goto e_free_session; diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index ab9c2c49d612..ef58f007030e 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -903,6 +903,21 @@ int sev_guest_activate(struct sev_data_activate *data, int *error) } EXPORT_SYMBOL_GPL(sev_guest_activate); +int sev_guest_bind_asid(int asid, unsigned int handle, int *error) +{ + struct sev_data_activate activate; + int ret; + + /* activate ASID on the given handle */ + activate.handle = handle; + activate.asid = asid; + ret = sev_guest_activate(&activate, error); + if (ret) + sev_guest_decommission(handle, NULL); + return ret; +} +EXPORT_SYMBOL_GPL(sev_guest_bind_asid); + int sev_guest_decommission(unsigned int handle, int *error) { struct sev_data_decommission decommission; diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 6c0f2f451c89..be50446ff3f1 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -595,6 +595,22 @@ int sev_guest_deactivate(struct sev_data_deactivate *data, int *error); */ int sev_guest_activate(struct sev_data_activate *data, int *error); +/** + * sev_guest_bind_asid - bind an ASID with VM and does decommission on failure + * + * @asid: current ASID of the VM + * @handle: handle of the VM to retrieve status + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_bind_asid(int asid, unsigned int handle, int *error); + /** * sev_guest_df_flush - perform SEV DF_FLUSH command * @@ -643,6 +659,9 @@ sev_guest_decommission(unsigned int handle, int *error) { return -ENODEV; } static inline int sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; } +static inline int +sev_guest_bind_asid(int asid, unsigned int handle, int *error) { return -ENODEV; } + static inline int sev_guest_df_flush(int *error) { return -ENODEV; } static inline int