From patchwork Mon Aug 16 20:24:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 498215 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 5CD95C4320A for ; Mon, 16 Aug 2021 20:24:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 44FCC60F55 for ; Mon, 16 Aug 2021 20:24:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232370AbhHPUZ0 (ORCPT ); Mon, 16 Aug 2021 16:25:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231760AbhHPUZZ (ORCPT ); Mon, 16 Aug 2021 16:25:25 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C0DEC061764 for ; Mon, 16 Aug 2021 13:24:53 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id y185-20020a3764c20000b02903d2c78226ceso7979076qkb.6 for ; Mon, 16 Aug 2021 13:24:53 -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=lAON/zCvnCQ9WkXsqWr7UdkpKpH6eowrQHHqKFOX8Ms=; b=ewJdYFg1tzZ1wizJlezLTaTYBT4aAtZdQcBb2uJlih9sqNHaFRZ8A3YzyMU+5ErrSC BcswvvEckSbQ2DChlC7FHMBSvlYETZGJi+e7GPs4mW6HvhG2+WW75B/VSvqxRfzqyMEI nwSScBipyqbyL7jwK2J+zIsprKJll7Qs1ha7ksb3Q5zZjLkp8e5+8m+btH9lgbBXbgXh b3bNhsiu8Z/gj2s0edL63ITQTAVqvoOdfD3KmZL7oo5zbX/hEyOttPr7vKgknPyAjSWX /1tE5NgbMZ4KoN/UpuUFzMc4owiAcgRCQDjk40X732mmggVhT6yh3lK51F1DEZb1vVLx hbIQ== 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=lAON/zCvnCQ9WkXsqWr7UdkpKpH6eowrQHHqKFOX8Ms=; b=pmtxNRAIzYr5WuLT/V+3Q7B1Js+d2IhKQQqriR4onfqans3oGFA3HZQRe2Sbr4/lav wBk16FyAJnNO7G0qFL5fBRDbRQ0flP9/i6SbkaIDdxhg15MOTW0fj0w7HjFW3x31qFpG g8RmdMS2T8GTa9/gek5+5zIjd0R6o7VYRIfSzyKWs85Bk1G+UwwFEhjjV5J4773X05Cc b+L2F8W81GMbBGll/39E8sytseftsGJWh7K1stm63VKaW7vd7Ii5uBh17nzu4wMbfyQ6 fM/gk/Gy94WJVJm50hG1BTLFpk2p1gWzCIe7g1yax4VPhqCVPSSP44AvVA4hLQI5yJX1 qoiA== X-Gm-Message-State: AOAM530C0/icuLHBdccXuGftl8JbpmFk0tfx2Uwn8m9R1suMCSYOPSQj +lnm07LkuyLP7ErQ1QThRILKYZNjIuWG X-Google-Smtp-Source: ABdhPJwsPia8EyVr5Q9u/4hdukkWSxdyMw6btsqaOSIt+v09BBRasZ6Wx6zQVBgvGvlkSBUrpKSgogEo8y4t X-Received: from mizhang-super.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1071]) (user=mizhang job=sendgmr) by 2002:a05:6214:1382:: with SMTP id g2mr610477qvz.14.1629145492552; Mon, 16 Aug 2021 13:24:52 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 16 Aug 2021 20:24:39 +0000 In-Reply-To: <20210816202441.4098523-1-mizhang@google.com> Message-Id: <20210816202441.4098523-2-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 1/3] KVM: SVM: move sev_decommission to psp driver 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 part of the software layer in psp that allows KVM to manage SEV/ES/SNP enabled VMs. Among the APIs exposed in sev-dev, many of them requires caller (KVM) to understand psp specific data structures. This often ends up with the fact that KVM has to create its own 'wrapper' API to make it easy to use. The following is the pattern: kvm_func(unsigned int handle) { psp_data_structure data; data.handle = handle; psp_func(&data, NULL); } psp_func(psp_data_structure *data, int *error) { sev_do_cmd(data, error); } struct psp_data_structure { u32 handle; }; sev_decommission is one example following the above pattern. Since KVM is the only user for this API and 'handle' is the only data that is meaningful to KVM, simplify the interface by putting the code from kvm function sev_decommission into the psp function sev_guest_decomssion. 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 Suggested-by: Sean Christopherson Signed-off-by: Mingwei Zhang --- arch/x86/kvm/svm/sev.c | 15 ++------------- drivers/crypto/ccp/sev-dev.c | 8 +++++++- include/linux/psp-sev.h | 7 ++++--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 75e0b21ad07c..6a1faf28d973 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -198,17 +198,6 @@ static void sev_asid_free(struct kvm_sev_info *sev) sev->misc_cg = NULL; } -static void sev_decommission(unsigned int handle) -{ - struct sev_data_decommission decommission; - - if (!handle) - return; - - decommission.handle = handle; - sev_guest_decommission(&decommission, NULL); -} - static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) { struct sev_data_deactivate deactivate; @@ -223,7 +212,7 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) sev_guest_deactivate(&deactivate, NULL); up_read(&sev_deactivate_lock); - sev_decommission(handle); + sev_guest_decommission(handle, NULL); } static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) @@ -349,7 +338,7 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) /* Bind ASID to this guest */ ret = sev_bind_asid(kvm, start.handle, error); if (ret) { - sev_decommission(start.handle); + sev_guest_decommission(start.handle, NULL); goto e_free_session; } diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 91808402e0bf..ab9c2c49d612 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -903,8 +903,14 @@ int sev_guest_activate(struct sev_data_activate *data, int *error) } EXPORT_SYMBOL_GPL(sev_guest_activate); -int sev_guest_decommission(struct sev_data_decommission *data, int *error) +int sev_guest_decommission(unsigned int handle, int *error) { + struct sev_data_decommission decommission; + + if (!handle) + return -EINVAL; + + decommission.handle = handle; return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); } EXPORT_SYMBOL_GPL(sev_guest_decommission); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index d48a7192e881..6c0f2f451c89 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -612,17 +612,18 @@ int sev_guest_df_flush(int *error); /** * sev_guest_decommission - perform SEV DECOMMISSION command * - * @decommission: sev_data_decommission structure to be processed + * @handle: sev_data_decommission structure to be processed * @sev_ret: sev command return code * * Returns: * 0 if the sev successfully processed the command + * -%EINVAL if handle is NULL * -%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_decommission(struct sev_data_decommission *data, int *error); +int sev_guest_decommission(unsigned int handle, int *error); void *psp_copy_user_blob(u64 uaddr, u32 len); @@ -637,7 +638,7 @@ static inline int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENODEV; } static inline int -sev_guest_decommission(struct sev_data_decommission *data, int *error) { return -ENODEV; } +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; } 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 From patchwork Mon Aug 16 20:24:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 498214 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 52D04C432BE for ; Mon, 16 Aug 2021 20:25:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3CACD60F22 for ; Mon, 16 Aug 2021 20:25:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232323AbhHPUZa (ORCPT ); Mon, 16 Aug 2021 16:25:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232624AbhHPUZ2 (ORCPT ); Mon, 16 Aug 2021 16:25:28 -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 61D92C061796 for ; Mon, 16 Aug 2021 13:24:56 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id w200-20020a25c7d10000b02905585436b530so17968151ybe.21 for ; Mon, 16 Aug 2021 13:24:56 -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=ytVF9QeV+MabNTM9/rkDa+0RWrj8iBIydz2kXshJL00=; b=MMCS/5/YGI5geXZq2J1jLLt4Rr2E/2TX5tjz8gOWO9lYX2SX4ehLEGWEL0BCaiMiTp agNVxJJtqvBJqsZdRyUCH9xL+Rfm1Wgt2HIdnQxafkxtNdX5OzW9ELX1VQogolDw+fd0 gIvB+lLgEI2cjdp8b+rYJpZwJUCx5ae4gVuC7AQUUmj3vpCUb4UKnaPEV9Y2RNn25Owb /AjWIrNM79gx2JDdilHM3oyRLrQYKhEvDvObLx/uOGTLnVaGjkG8A4nwex/jzuiCYmqm MOgG0JVtklTKjKOR4POYs4nxNuSbn3hZdj5DCTNApYKrx9cHamlBTvElapbzcVROu+AX +DRg== 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=ytVF9QeV+MabNTM9/rkDa+0RWrj8iBIydz2kXshJL00=; b=tubh2kTOpAqvL1fZqaIVNRR+wn0iuca19NGE4hB4vp9fZrc8DOL9O1xYPEVpzXl/rF iOgGZR7FPsd0TNFy/xgNI1RxZcw3AZhzLsqDbJN7xphIByuiQUTaEaedNI30CIloEKZF 8cvDYYXYv69c2ANGjVCoBXuhxFlcuA6pn18WeYMMHD5G/RK6TN3hEeJxiwqUGv/0/aF+ Y6Iy5ZNU7gdSn+/exyliwHadyw8yFVBoGOH0hvHE6FEoGLV0dGg3a9Hr1I2JbqdDP6tN TMg/xvhDoWW38fu7qbvteyLK6WOjIdJPXBaVRQpRiRYYOkHfl1+Kcp4/RtkpWY3KvD12 QDsA== X-Gm-Message-State: AOAM532l0Hjp2CT5VLyBff6erppH8wqG5+itk4VKntiL4kqNXnc4FN81 eB64Xe1N5LRn2WLYzIUE5ZJee1f2gKum X-Google-Smtp-Source: ABdhPJxRDg4XjAziDvXPV2YsT0AuxLyzM1Sc65DpyYo3GcMdJfwDr7nsBXBqr2yHD0PnwXA4/ipBmaPK8jv6 X-Received: from mizhang-super.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1071]) (user=mizhang job=sendgmr) by 2002:a25:2155:: with SMTP id h82mr255114ybh.177.1629145495627; Mon, 16 Aug 2021 13:24:55 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 16 Aug 2021 20:24:41 +0000 In-Reply-To: <20210816202441.4098523-1-mizhang@google.com> Message-Id: <20210816202441.4098523-4-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 3/3] KVM: SVM: move sev_unbind_asid and DF_FLUSH logic into 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 In KVM SEV code, sev_unbind_asid and sev_guest_df_flush needs to be serialized because DEACTIVATE command in PSP may clear the WBINVD indicator and cause DF_FLUSH to fail. This is a PSP level detail that is not necessary to expose to KVM. So put both functions as well as the RWSEM 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 Signed-off-by: Mingwei Zhang --- arch/x86/kvm/svm/sev.c | 35 +++-------------------------------- drivers/crypto/ccp/sev-dev.c | 34 +++++++++++++++++++++++++++++++++- include/linux/psp-sev.h | 19 ++++++++++++++++++- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2a674acb22ce..ecf9da718d21 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -57,7 +57,6 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); #endif /* CONFIG_KVM_AMD_SEV */ static u8 sev_enc_bit; -static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); unsigned int max_sev_asid; static unsigned int min_sev_asid; @@ -84,20 +83,9 @@ static int sev_flush_asids(int min_asid, int max_asid) if (asid > max_asid) return -EBUSY; - /* - * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, - * so it must be guarded. - */ - down_write(&sev_deactivate_lock); - - wbinvd_on_all_cpus(); ret = sev_guest_df_flush(&error); - - up_write(&sev_deactivate_lock); - if (ret) pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); - return ret; } @@ -198,23 +186,6 @@ static void sev_asid_free(struct kvm_sev_info *sev) sev->misc_cg = NULL; } -static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) -{ - struct sev_data_deactivate deactivate; - - if (!handle) - return; - - deactivate.handle = handle; - - /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ - down_read(&sev_deactivate_lock); - sev_guest_deactivate(&deactivate, NULL); - up_read(&sev_deactivate_lock); - - sev_guest_decommission(handle, NULL); -} - static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -329,7 +300,7 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) /* return handle to userspace */ params.handle = start.handle; if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { - sev_unbind_asid(kvm, start.handle); + sev_guest_unbind_asid(start.handle); ret = -EFAULT; goto e_free_session; } @@ -1378,7 +1349,7 @@ static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(struct kvm_sev_receive_start))) { ret = -EFAULT; - sev_unbind_asid(kvm, start.handle); + sev_guest_unbind_asid(start.handle); goto e_free_session; } @@ -1789,7 +1760,7 @@ void sev_vm_destroy(struct kvm *kvm) mutex_unlock(&kvm->lock); - sev_unbind_asid(kvm, sev->handle); + sev_guest_unbind_asid(sev->handle); sev_asid_free(sev); } diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index ef58f007030e..7d53cd954f80 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -33,6 +33,7 @@ #define SEV_FW_NAME_SIZE 64 static DEFINE_MUTEX(sev_cmd_mutex); +static DECLARE_RWSEM(sev_deactivate_lock); static struct sev_misc_dev *misc_dev; static int psp_cmd_timeout = 100; @@ -932,10 +933,41 @@ EXPORT_SYMBOL_GPL(sev_guest_decommission); int sev_guest_df_flush(int *error) { - return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); + int ret; + /* + * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, + * so it must be guarded. + */ + down_write(&sev_deactivate_lock); + + wbinvd_on_all_cpus(); + + ret = sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); + + up_write(&sev_deactivate_lock); + + return ret; } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +void sev_guest_unbind_asid(unsigned int handle) +{ + struct sev_data_deactivate deactivate; + + if (!handle) + return; + + deactivate.handle = handle; + + /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ + down_read(&sev_deactivate_lock); + sev_guest_deactivate(&deactivate, NULL); + up_read(&sev_deactivate_lock); + + sev_guest_decommission(handle, NULL); +} +EXPORT_SYMBOL_GPL(sev_guest_unbind_asid); + static void sev_exit(struct kref *ref) { misc_deregister(&misc_dev->misc); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index be50446ff3f1..09447bce9665 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -580,6 +580,20 @@ int sev_issue_cmd_external_user(struct file *filep, unsigned int id, */ int sev_guest_deactivate(struct sev_data_deactivate *data, int *error); +/** + * sev_guest_unbind_asid - perform SEV DEACTIVATE command with lock held + * + * @handle: handle of the VM to deactivate + * + * 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_unbind_asid(unsigned int handle); + /** * sev_guest_activate - perform SEV ACTIVATE command * @@ -612,7 +626,7 @@ int sev_guest_activate(struct sev_data_activate *data, int *error); int sev_guest_bind_asid(int asid, unsigned int handle, int *error); /** - * sev_guest_df_flush - perform SEV DF_FLUSH command + * sev_guest_df_flush - perform SEV DF_FLUSH command with lock held * * @sev_ret: sev command return code * @@ -656,6 +670,9 @@ sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENO static inline int sev_guest_decommission(unsigned int handle, int *error) { return -ENODEV; } +static inline int +sev_guest_unbind_asid(unsigned int handle) { return -ENODEV; } + static inline int sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; }