From patchwork Mon Oct 24 22:46:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 10421 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp723080wru; Mon, 24 Oct 2022 17:28:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4CrKpVyhCtzvf6TIGsgR6HLTanqhpXFqv9ScAQJ7jsE5zOyQyuaQ+Qr2emtaOSrhZIobx8 X-Received: by 2002:a05:6402:270b:b0:45d:61cd:73cc with SMTP id y11-20020a056402270b00b0045d61cd73ccmr33408822edd.136.1666657680054; Mon, 24 Oct 2022 17:28:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666657680; cv=none; d=google.com; s=arc-20160816; b=iL1KPc4dz9ciU6Lhp6UYH4EIPTekS1T+udCJYgs9iLUjEryVH+eySLZoCUtzPfkm0/ 6evYqfszgMcld/9y090t6hFk0jZQN+K3cgzBSvI5pKbaAaPBJyRDtXx6IGjvDWNDwyn1 kD6UAMAchm/apa8/8U4aPZO2xWlPRUIK+fQkYIeuAjPjCkzwo4gCYrGLKGdA5DMSgg4K XtgsW5DD4c/K0cNt3Xe0AzLv7Rv45YjAh51ZE/rwOJHkeanx5gtJabANN5tRKoDS/qX4 H9h2Qx29o9+QdPFky1Pza/vI3j3QxqK8CgOIlzmySgjr71Odao4jJdYM/FkrsBFHw1f5 Xhkg== 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=m+6LJgKl0wRgTRLkq4f9veim6S7HXx9ZKXbkLf1zrmQ=; b=IT2pUMHuHqoMSdAFsW8tqwl8ncF1jB7DeQHv2ppVDs9HbNQ0VL3z97icvSz8AuHbC0 rlT48+W6OJYWRCkBIsO4vkQaeqFlBk1vBp6+10SvSBTP7SkStkvz4whZ/JmAqqBY8UYk NLukEPs04Aa8o6YXQBLXKy3j+6Tl0OlmgPUGbt+uIkLV97X5+c7d2nm2U5ZjZ4cEogKd rdFW5CMQEsbyxpSigPZ749v01nKxbmdLOmLuS7sCWxHghqf1KG1J/yPsObqhcTlYe908 fDTvVCq5AKqGv3TvhyVzUxRYD9YErFwY8Q0DwmTVkmwz4MHOiynPc1uQcbAYg+jvrNqj McMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qAquYynk; 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 ho33-20020a1709070ea100b0079914497afcsi1360150ejc.581.2022.10.24.17.27.36; Mon, 24 Oct 2022 17:28:00 -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=qAquYynk; 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 S231272AbiJYAX4 (ORCPT + 99 others); Mon, 24 Oct 2022 20:23:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229981AbiJYAX0 (ORCPT ); Mon, 24 Oct 2022 20:23:26 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80B4812D805 for ; Mon, 24 Oct 2022 15:47:02 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id oo18-20020a17090b1c9200b0020bdba475afso9534960pjb.4 for ; Mon, 24 Oct 2022 15:47:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=m+6LJgKl0wRgTRLkq4f9veim6S7HXx9ZKXbkLf1zrmQ=; b=qAquYynksnnVrt4f8uMTPnplyMcJL6gGvw6BcX9ETlFFAXWc7XeOmnjigS6Hk0RpJM GkCMlq1TCF5o28LCAPZ/RuFkgTw9BdSBEt+bB7lbM52ppb6/Yt+jFvxxbnjjHWjcSGrh VJskB3Gdm/nLTkw0sS7BvyHSiL7wCAKdIYi1qs0i4ppvBISk+DnFFu00la9g6j+qws5a fU8qzKJnoDartK3Pz7Sv7YvzJnvp7lA8bng0iZ+KH6an/Hg4Yl4gJ+huobq7QOG6AbGZ WqvhLddmwlZqkHd8u97quSJZEYRPRm8j8QMlyLpL1/B134pcwlnlm0jCJiIg82Dgakep hQvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=m+6LJgKl0wRgTRLkq4f9veim6S7HXx9ZKXbkLf1zrmQ=; b=OWN1HgyUXLRXjJH0iRpe51f98cZbyz4R0hiUrqwrONN57n2j5ZK3khVudp1UBHCqof I/u25VoDo4Tm00RpVnuvq2JPUVzRFaveHomoufT0Tk6tGrynwq0l7ddkJza44eZUh9pG h6cX9DxbshXPtD9WL+0tUH2vdBTUEh01MpmFtECrP8iUmwlO1/j1xTRvwiduVNDlH92V 1JEgpbCO9D24wkLAdC/PSZJkBAIeMVoWHnLAn+bGcL/4jbP9dBtKsuP6aal//j+wm4f5 DTDbNE9MEeGUd7Tu1M1/BZ5+1NCL+oVgR9OvnxjoJbFv4+c7V/i9/nZP4qjBGJeMTxoz 6mWA== X-Gm-Message-State: ACrzQf2/XqYMs5A1EBvhxWKa7tOlOIL8TbTaJZsJvlT+1N5a0MG3CthT PwYZmnhlpK5FXnB4XUnYu+jvLVQX4oAV2IIvPZg9oBedoNULzeL1ckhDPaeMA9OIU63GBUZpZ4r 4WS70tGqwaKgh69jTjPw66kecG/CKKwzMcwRfvF89nv41iuW+F94YahCBvg1QwvXXXy0Lxy8DoI H3Bve1peE= X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:902:ced0:b0:17f:92d6:f5ec with SMTP id d16-20020a170902ced000b0017f92d6f5ecmr35114513plg.34.1666651622229; Mon, 24 Oct 2022 15:47:02 -0700 (PDT) Date: Mon, 24 Oct 2022 22:46:54 +0000 In-Reply-To: <20221024224657.2917482-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221024224657.2917482-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221024224657.2917482-2-dionnaglaze@google.com> Subject: [PATCH v4 1/4] ccp: Name -1 return value as SEV_RET_NO_FW_CALL From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Thomas Lendacky , Paolo Bonzini , Joerg Roedel , Ingo Molnar , Andy Lutomirsky X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,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=ham 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?1747617243386445085?= X-GMAIL-MSGID: =?utf-8?q?1747617243386445085?= The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called. To make this protocol unambiguous, we add a constant naming the return value. From: Peter Gonda Cc: Thomas Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Ingo Molnar Cc: Andy Lutomirsky Signed-off-by: Dionna Glaze Reviewed-by: Tom Lendacky --- drivers/crypto/ccp/sev-dev.c | 2 +- include/uapi/linux/psp-sev.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 06fc7156c04f..97eb3544ab36 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error) { struct psp_device *psp = psp_master; struct sev_device *sev; - int rc = 0, psp_ret = -1; + int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; int (*init_function)(int *error); if (!psp || !psp->sev_data) diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..fb61e083d42e 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -36,6 +36,13 @@ enum { * SEV Firmware status code */ typedef enum { + /* + * This error code is not in the SEV spec but is added to convey that + * there was an error that prevented the SEV Firmware from being called. + * This is (u32)-1 since the firmware error code part of EXIT_INFO_2 is + * the lower 32 bits. + */ + SEV_RET_NO_FW_CALL = 0xffffffff, SEV_RET_SUCCESS = 0, SEV_RET_INVALID_PLATFORM_STATE, SEV_RET_INVALID_GUEST_STATE, From patchwork Mon Oct 24 22:46:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 10419 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp723020wru; Mon, 24 Oct 2022 17:28:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6AUghqvfNRNwnbvR65Klh7WPf9te66ph+ns2uZWYsMRSNKTfT22flIEbImUX5KBVZVSBfV X-Received: by 2002:a17:907:8a23:b0:78d:a24e:a9aa with SMTP id sc35-20020a1709078a2300b0078da24ea9aamr30780308ejc.404.1666657680419; Mon, 24 Oct 2022 17:28:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666657680; cv=none; d=google.com; s=arc-20160816; b=tvLsZvZ9xGQPOCU5d3tB8nDRbvDMukB4/E+YtSJZDEQXAn7APdfwSuz4EIDNhOJHL5 vwnCx7NvXb7DL5MZ3Md+2b7M/nK8nBLiwvAfAgK5fcLtVCpsCvbHmjgkOg5sw+6G7IKQ hSpx4O/uYiXJE5s28g/XPQ4/DUIygd5xYUVE/L7/cpfIKqjDDhZufH18guj0wtX/Dp25 iP9Ni2MHkVXWieGYLiO+U49sDyUVC+l6Ze44gL6rj11x7BSOhZGbQMw6M4T93HD1JqcT 3vJ51/UkWZK9a39iApyX/pyTvxTGtkxBsHT+cekflGfHrFc55UWhfpBb2cgRAG0eeIJh ATag== 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=GW6bKm1YVuify4X/GJIm5kxchqqZUANlG9YPqARl8Y4=; b=Nlsad1kLuOivFVMMPGV/NdX9OLIc+96JokoJcsYRdlAPRZQ0RHj2eWzaBnhUQgrnC0 s45a2HdclLjTRQJrvvYVZ0I+wsYl6HoYb6rwdc9plXJGFPpDzJTq0kT0Y1uQg+cKhQ0/ jeIMHExXWf8xth9sAUqim8oqgK5NdRDdw0KpREjabe+xzesC4Ezxt85KkeWA71tUmneK /xrl0yorUR1Sx/8z/gn2Db7Mtjp4Itgto0NvqScvI8M59oC5Ph2VCUKrmwa/VTC8hhbG TGPzIXhqziwEjEcDgjCQ4emNMwPwk5x96VyGN5gMj1tkbyKTClzAQJR/Q/6v+H01X1SM W5Gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=GXi04bfY; 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 dd21-20020a1709069b9500b00781a32fd847si1257724ejc.917.2022.10.24.17.27.36; Mon, 24 Oct 2022 17:28:00 -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=GXi04bfY; 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 S231297AbiJYAYB (ORCPT + 99 others); Mon, 24 Oct 2022 20:24:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230181AbiJYAX1 (ORCPT ); Mon, 24 Oct 2022 20:23:27 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CAAA140E42 for ; Mon, 24 Oct 2022 15:47:04 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id p24-20020a63f458000000b0043cd718c49dso5321307pgk.15 for ; Mon, 24 Oct 2022 15:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GW6bKm1YVuify4X/GJIm5kxchqqZUANlG9YPqARl8Y4=; b=GXi04bfYEXBKhCuN7uwjEYmDrIoZzYqOX2Mvi1/Sbp7GVhLP2729ISTmTNHgq0UPzD QdkUUp8oNaiED1z1HJmLGcsjXwRIlgWHHfNP2aQPtyKmKObqdKVA5b/8/pphKUQfxNy4 pLKYdKUvy/a9AY38lRSlZeB74GTCNNMRqe11mTvN6Tz3X6msV3tHKGTD5n96WaKTr+fg KNBtBHUgWhKJb25HDSr5v10AO0ZKUgcK1mr5QQE1lpOfeVUaDjmyTHxBMdx4hXRpGVfc 88iEq6MMZwAiixYehQff7Foc5VlkXny6vlMpCdd9ZMD65zU4wXG0DS6S7tyf/8Wcvab1 MgNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=GW6bKm1YVuify4X/GJIm5kxchqqZUANlG9YPqARl8Y4=; b=eAEngaaO5hNWJeESoVypdfUr9D/MVN9xPOFqcMWTWGe0xkD7U7MpfflSF96ighDtH0 EFP5KtIBSgYlwL6A/CQqCm4FOvVMaKtJACf74KVIsPmWcQO+RDaP/no0LEAtJbwgIMab 6iPC9FCx/7kHukO0QNtUt++mstRmOWA4V+YSLONO/SJOWYBRjRYtzt9SOI4DunjDB0Fk Gki7lAcKI/KM++spotxKjQuCUlhIHlrPw+H9MCshKFxj2+4C+nfU4zRxHPUkr+CEgZt6 AXwDcn2C1BS3yco/VMJI0434dijCGZ45PNhcs4Nc9ySF7DIKW5M9EUBRMFs4hST9fPB1 BgAQ== X-Gm-Message-State: ACrzQf0KD9wE7FIG22pF7AFxVVkHrJGNHV3zAcFoYK0vNXF54fH5xLc/ zYKKoHhisTcFw58gYpeXKyXF5pKkGIa212c6qmumy/BirzU6Z8L+pYiBb7FLZd6PyF9aT45NT2U UOgKJB0517zPdF1KySaSMI5/UFWxXLIuDgyYT05BpYP/As0Gdur/J0VKP7jHXcs0WtGVdfZ5nTw 4lHE8GdsU= X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:90b:1e51:b0:212:d06b:b5fa with SMTP id pi17-20020a17090b1e5100b00212d06bb5famr21668968pjb.171.1666651623675; Mon, 24 Oct 2022 15:47:03 -0700 (PDT) Date: Mon, 24 Oct 2022 22:46:55 +0000 In-Reply-To: <20221024224657.2917482-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221024224657.2917482-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221024224657.2917482-3-dionnaglaze@google.com> Subject: [PATCH v4 2/4] x86/sev: Change snp_guest_issue_request's fw_err From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,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=ham 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?1747617243919862193?= X-GMAIL-MSGID: =?utf-8?q?1747617243919862193?= The GHCB specification declares that the firmware error value for a guest request will be stored in the lower 32 bits of EXIT_INFO_2. The upper 32 bits are for the VMM's own error code. The fw_err argument is thus a misnomer, and callers will need access to all 64 bits. The type of unsigned long also causes problems, since sw_exit_info2 is u64 (unsigned long long) vs the argument's previous unsigned long*. The signature change requires the follow-up change to drivers/virt/coco/sev-guest to use the new expected type in order to compile. The firmware might not even be called, so we bookend the call with the no firmware call error and clearing the error. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs") Signed-off-by: Dionna Glaze Reviewed-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/kernel/sev.c | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..8ebd78b6a57c 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -196,7 +196,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages); void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); +int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, u64 *exitinfo2); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -217,7 +217,7 @@ static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, - unsigned long *fw_err) + u64 *exitinfo2) { return -ENOTTY; } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a428c62330d3..148f17cb07b5 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -2175,7 +2176,7 @@ static int __init init_sev_config(char *str) } __setup("sev=", init_sev_config); -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err) +int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, u64 *exitinfo2) { struct ghcb_state state; struct es_em_ctxt ctxt; @@ -2186,9 +2187,11 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - if (!fw_err) + if (!exitinfo2) return -EINVAL; + *exitinfo2 = SEV_RET_NO_FW_CALL; + /* * __sev_get_ghcb() needs to run with IRQs disabled because it is using * a per-CPU GHCB. @@ -2212,14 +2215,13 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned if (ret) goto e_put; + *exitinfo2 = ghcb->save.sw_exit_info_2; if (ghcb->save.sw_exit_info_2) { /* Number of expected pages are returned in RBX */ if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST && ghcb->save.sw_exit_info_2 == SNP_GUEST_REQ_INVALID_LEN) input->data_npages = ghcb_get_rbx(ghcb); - *fw_err = ghcb->save.sw_exit_info_2; - ret = -EIO; } From patchwork Mon Oct 24 22:46:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 10420 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp723048wru; Mon, 24 Oct 2022 17:28:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7c7j2jXN4FczVsGfdqfgB+G4Z/JOuO00iJGYIb/A9Mg+uY/1/4Iop1FY3z6uswu9lQeGLD X-Received: by 2002:a05:6402:380a:b0:451:ae08:7a6c with SMTP id es10-20020a056402380a00b00451ae087a6cmr32193541edb.161.1666657685439; Mon, 24 Oct 2022 17:28:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666657685; cv=none; d=google.com; s=arc-20160816; b=NB4DJxPUIjWogIDiRBS9NPNvgeAQyBy59KwwiLDBk3bjen8X8rP3AUKoGlfI3e9rgZ JAfd6HyjLlZHalAaLDjP7rUS8Rb3hqCaw6xL03GBuop4+v3hk6l4o1ZE1denP9mYM9kh d/vxiVxx5YhGdXnocQUuq3TbgD4p5Of6K8PZ0djgzW1Gpwj28SV4AA9brHBHTed8exf0 6UXaOMMANTCLHSupZsVZpWhB8X/RnmbIrUPp8A6Y++Sl+l5MJHz/BA1h7gtI7RiYfbIA zsxhvFc0NLPzGqd2iXbOv03Qb8Ttvw6skK+DZPsrw9igGoaSw58HONQxsoGkt34Wf3Js b6Dw== 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=t50s+W2sjRzmfaqPHM8BWoKuylKuq2NrCynpVhPS9Gk=; b=CxOZBZD2wnLTN4cZhU1t7RpVBrQ6AsFz1EZqbp08h933u37as7gikqTlemCMD+mA7t 82zQzZCr0pFBLLvZAP6RPD5lBx52IkuGg1Q1ZjsoURxqVspxRchf+Ke5Q0WWE27PCXsf Ja+q6zr7och2EmhQFb3WX4n220A35cowWfpHKAqrA+RQUg7mO103NlFHSVrnXz7sI/I3 0KMbIFxRyU6uYOEhz5KzPRlZTyFOdPZ4mYPtSZTu3Ob+tgAx9Nqfi1TCsn1IvBE5u5fP zJ5DCgcUDneunqYzQGtLm+BDm1vIIVChH8arPP9O7eKfRF5LnhCGQ1oDfamFADkoBFPL bJEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=VukVbYSP; 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 bt9-20020a0564020a4900b00461f10cb543si1113851edb.154.2022.10.24.17.27.42; Mon, 24 Oct 2022 17:28:05 -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=VukVbYSP; 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 S229615AbiJYAYR (ORCPT + 99 others); Mon, 24 Oct 2022 20:24:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230336AbiJYAX2 (ORCPT ); Mon, 24 Oct 2022 20:23:28 -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 6760FCC827 for ; Mon, 24 Oct 2022 15:47:06 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id cb7-20020a056a00430700b00561b86e0265so5287228pfb.13 for ; Mon, 24 Oct 2022 15:47:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=t50s+W2sjRzmfaqPHM8BWoKuylKuq2NrCynpVhPS9Gk=; b=VukVbYSP3OxCt2Aov1s0MXfB7/bJ8+HJOcG+qkdTnvVwyqSeEysxQibuYGQUZ3GZHb LQo/uctV2e8+FXtRqm1w3rlwt5gYx58CGxPrKqoR0yeaP8JfVnpR+nP6OXi9wVTzBBl9 AXKCdAMTHbE1mKJISj2iUiRLdw6PTThAzbg+o+a8LaRJc82/oAFNbu6ylNV6osG+qHp8 m5CycDkb++iVbaZA3ZFPyLWI3Z7GFGvKnDD+R/CQ2yo1ag9cs5xDXCH6qE743Q8S1mOq S/kRVHwrJWzq6x1OLj+u3ATxd/9s8S1RZ3/NisX1JirkpZVY76wYTLHgmfhH45U6kaxx 3HAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=t50s+W2sjRzmfaqPHM8BWoKuylKuq2NrCynpVhPS9Gk=; b=Bul4N3EFuhyyXyM0v7Gbm1d9wFFgTUVjuIKWIHKOuppIg2F7Bz4nkx+J5o4POFYv3l HYnOtvcWFn3OKCabFBUFiqc2aTg8cydtGotepVr3ywQdg8v8idvrP6oOOo/Bf4+OD1Hx FqFoHRM0cLfN7GPcPnsKqrYMkS7t0vLU2Yqay4avMQ9195DYH4f/HON/Ev13b+HkGzEc 936fs5uRUjEVUHI0VHmTPG99aj8ZZ07YyDx0YtJYwCadf502dnuizn04jYhuI1mV9iz6 blKSsvjcEgzARRO85DZfwQ322W2OovDd5zV3qvFuqc5yUrXsu9MjSnjDuSiQ99N9eE6L H0tw== X-Gm-Message-State: ACrzQf09eT6O4CMoDHFdqin1H9KQ/hzLVQprOQjK28iSmSaKlYSpPgjj yjX3a+Rw/cjFPLv+x7DjALe8zPxdu0iDUV51U+aE0+D/Il00jy9enBD6RpdVHjSkrnp7Yn0HEWt 3Zcqgx9EsHJeTIl/NyLftAJjZ2ZoHM4BKFFF1VuwwogUF0qLKIQQghOcMN2UepqRMwxq9mr55J9 wtW/Hipn4= X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:90a:4811:b0:20a:fee1:8f69 with SMTP id a17-20020a17090a481100b0020afee18f69mr4038806pjh.0.1666651625380; Mon, 24 Oct 2022 15:47:05 -0700 (PDT) Date: Mon, 24 Oct 2022 22:46:56 +0000 In-Reply-To: <20221024224657.2917482-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221024224657.2917482-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221024224657.2917482-4-dionnaglaze@google.com> Subject: [PATCH v4 3/4] virt/coco/sev-guest: Remove err in handle_guest_request From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,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=ham 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?1747617249225465112?= X-GMAIL-MSGID: =?utf-8?q?1747617249225465112?= The err variable may not be set in the call to snp_issue_guest_request, yet it is unconditionally written back to fw_err if fw_err is non-null. This is undefined behavior, and currently returns uninitialized kernel stack memory to user space. The fw_err argument is better to just pass through to snp_issue_guest_request, so we do that. Since the issue_request's signature has changed fw_err to exitinfo2, we change the argument name here. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Fixes: fce96cf04430 ("virt: Add SEV-SNP guest driver") Signed-off-by: Dionna Glaze Reviewed-by: Tom Lendacky --- drivers/virt/coco/sev-guest/sev-guest.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index f422f9c58ba7..0508c2f46f6b 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -303,9 +303,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, int msg_ver, u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err) + u32 resp_sz, __u64 *exitinfo2) { - unsigned long err; u64 seqno; int rc; @@ -322,9 +321,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in return rc; /* Call firmware to process the request */ - rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err); - if (fw_err) - *fw_err = err; + rc = snp_issue_guest_request(exit_code, &snp_dev->input, exitinfo2); if (rc) return rc; From patchwork Mon Oct 24 22:46:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 10422 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp723248wru; Mon, 24 Oct 2022 17:28:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM58qONtvyvmFTFrEfQ5QV8SHEFRefJdyRYys22FCwjz/eKNwaK/EOtaLFMD8KkRnsULiFx7 X-Received: by 2002:a17:907:a46:b0:782:1c1c:8141 with SMTP id be6-20020a1709070a4600b007821c1c8141mr29796658ejc.549.1666657718821; Mon, 24 Oct 2022 17:28:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666657718; cv=none; d=google.com; s=arc-20160816; b=AgY5syC5C5OyA+RkjhVK9jLtIf893rOhTS8VeqdLbenyE870IgZvf7sbhdSmdgFls0 vlPqrZZzKNyZRzT11bXIGw7Ge7yC2+/YfxZVG0VCMtfsdcWWxv7Z651Lh/gdhKSdQa02 uMruB8vr5dFleIYGNg2blW0mP+26WEcfPMr6jPAUK++4PpWDJwG+NVyQFGUyMZZB4P4u JFM8CPKOIXou+Un230Gx3Vqcc8p4TJdgdLIzB2KMpssPb2SbIyrSI+5ue90EgTEdTMXt BQHgP0kSrsrkTo2rIWbpiONp3gI5o8VL8I52r0zjmrMBRSNllT4wE4I/uH8BK9VaeIC+ go4Q== 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=5UySGAj5El7d7bvSQvGw/2eB/1FL6CpCJxbhgnpFQJc=; b=SwgFVfsBdWm0JWSnlZKB5ROG+B1XMQTJRDyLKmoRuTx1rHYVCUIuUJZ4+KFdIh16QO EuBfS488fxqmSRhdtwLdU56MJ6g16HeUcLwpQqzUGXuClvQ175EeLc6W59l3ZDd5FYDc 6DRDtdnzlpsM70EnGtvSrP522ryIyVHvcYRPaeJjQBH97F3Z/ckPt3W0FNo711k/+Ma9 EY1GCz1Lyys62taKrYEITSV3WO1iEIALnh05jFQDtNHcMUbP5ur+uGrd3AIdRkALSQvB YhrV5RllwjVQOl0iVvhdABoIXdJ+wF49LuyuvAP/6k+7Yr+99gePohEq3d6lxMo7m3yC vLwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=k7ZDEpqk; 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 x20-20020aa7dad4000000b00461d28ac266si988393eds.579.2022.10.24.17.28.13; Mon, 24 Oct 2022 17:28:38 -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=k7ZDEpqk; 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 S230058AbiJYA0z (ORCPT + 99 others); Mon, 24 Oct 2022 20:26:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231236AbiJYAXb (ORCPT ); Mon, 24 Oct 2022 20:23:31 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B47E12344F for ; Mon, 24 Oct 2022 15:47:10 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id k16-20020a170902c41000b00184987e3d09so7218067plk.21 for ; Mon, 24 Oct 2022 15:47:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5UySGAj5El7d7bvSQvGw/2eB/1FL6CpCJxbhgnpFQJc=; b=k7ZDEpqkI4w+l5J7SzI6WdHE/uuaSYc6fTnSOufNEgPWxzVQNBJGDb4WXsfKDw3rM8 Wc/ZL09g8yPC+FWCfNqOE7hoVC/iXLiGHRBIDqRWbeLhv8CFkr3gLjhmleyNs8qO0Oho TNUb4pZqrFTSrD46Oif2JROxuOtySQmKdGJx7k2kgJuUySl3zzxSkF7Yb9yUDoOLGgop hCp7LRM4ed72phO6/8uPKMfWkwCWHoclvDynGFPxqAlLZabD8//W+npxqFYA2gvqzovK t09UWI+ypkQkCGYsrq/Vb83z9WGCV46kkHBL0rXWNv3lHUvCEvWOgN1k+IvxU8Cs/8Y4 2ndg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=5UySGAj5El7d7bvSQvGw/2eB/1FL6CpCJxbhgnpFQJc=; b=Fh1bhkwNYLrmA8KgOXLrgmmdqiY27dao1fGe+pboD1ib1l7ufDArFUM1I4ZT7hcr4R YyXXBD5zU7Y1fhVojD8AsrrBLoLkF0YIMc8flr78mX2o6UBZ7WbKLDmrhU1BQ9P7XLcS 4PsRT9cZtf6xUaPLV4j5BALqY+wl/3WqrXspN0jAGZBtc2GZ1nj+b//6KYG94JjvOU9b LVhfUEqLBJHN4gHjLR+E2vQAh1O9ywFbAe1LMfMy7byuDo8+iDbA4TeCZk9YVs0ayPb5 AxyyqzlyvuihocyR4Yeg+ICGOqTl78p013lPrHj+EhY1dxlJ9Aiw5P73zoSWC+OjVmsr gJIw== X-Gm-Message-State: ACrzQf0LDMnu8NOMS639Ogn4psz5f/F51WkCTJcN6rxHMWYGJxCzUtDW 0gtQGctZRFh5I2m+rXcBFU+7Av0lhpMgQBfGdWNooBk0kStWuZK2msVQpPuCf3/d++6MRoXXQGw MPoucvdVTid4kq9S8P4UZGkVll1D1jcvnyXfcp3CmgIcuzLvFW48N3cBv3tbxkTmxLAsSnh4tmp y0+dRE9UA= X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:903:120e:b0:179:fe49:a952 with SMTP id l14-20020a170903120e00b00179fe49a952mr35474105plh.21.1666651627333; Mon, 24 Oct 2022 15:47:07 -0700 (PDT) Date: Mon, 24 Oct 2022 22:46:57 +0000 In-Reply-To: <20221024224657.2917482-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221024224657.2917482-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221024224657.2917482-5-dionnaglaze@google.com> Subject: [PATCH v4 4/4] virt/coco/sev-guest: interpret VMM errors from guest request From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,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=ham 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?1747617284332731941?= X-GMAIL-MSGID: =?utf-8?q?1747617284332731941?= The GHCB specification states that the upper 32 bits of exitinfo2 are for the VMM's error codes. The sev-guest ABI has already locked in that the fw_err status of the input will be 64 bits, and that BIT_ULL(32) means that the extended guest request's data buffer was too small, so we have to keep that ABI. We can still interpret the upper 32 bits of exitinfo2 for the user anyway in case the request gets throttled. For safety, since the encryption algorithm in GHCBv2 is AES_GCM, we cannot return to user space without having completed the request with the current sequence number. If we were to return and the guest were to make another request but with different message contents, then that would be IV reuse. When throttled, the driver will reschedule itself and then try again. The ioctl may block indefinitely, but that has always been the case when deferring these requests to the host. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Signed-off-by: Dionna Glaze --- drivers/virt/coco/sev-guest/sev-guest.c | 54 ++++++++++++++++++++++--- include/uapi/linux/sev-guest.h | 18 ++++++++- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 0508c2f46f6b..dfde777933b6 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -48,12 +49,22 @@ struct snp_guest_dev { struct snp_req_data input; u32 *os_area_msg_seqno; u8 *vmpck; + + struct ratelimit_state rs; }; static u32 vmpck_id; module_param(vmpck_id, uint, 0444); MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP."); +static int rate_hz = 2; +module_param(rate_hz, uint, 0444); +MODULE_PARM_DESC(vmpck_id, "The rate limit frequency to limit requests to."); + +static int rate_burst = 1; +module_param(rate_burst, uint, 0444); +MODULE_PARM_DESC(rate_burst, "The rate limit burst amount to limit requests to."); + /* Mutex to serialize the shared buffer access and command handling. */ static DEFINE_MUTEX(snp_cmd_mutex); @@ -305,9 +316,12 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in u8 type, void *req_buf, size_t req_sz, void *resp_buf, u32 resp_sz, __u64 *exitinfo2) { + unsigned int vmm_err; u64 seqno; int rc; + might_resched(); + /* Get message sequence and verify that its a non-zero */ seqno = snp_get_msg_seqno(snp_dev); if (!seqno) @@ -320,9 +334,35 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in if (rc) return rc; +retry: + /* + * Rate limit commands internally since the host can also throttle, and + * we don't want to create a tight request spin that could end up + * getting this VM throttled more heavily. + */ + if (!__ratelimit(&snp_dev->rs)) { + cond_resched(); + goto retry; + } /* Call firmware to process the request */ rc = snp_issue_guest_request(exit_code, &snp_dev->input, exitinfo2); + vmm_err = *exitinfo2 >> SNP_GUEST_VMM_ERR_SHIFT; + /* + * The host may return EBUSY if the request has been throttled. + * We retry in the driver to avoid returning and reusing the message + * sequence number on a different message. + */ + if (vmm_err == SNP_GUEST_VMM_ERR_BUSY) { + cond_resched(); + goto retry; + } + + if (vmm_err && vmm_err != SNP_GUEST_VMM_ERR_INVALID_LEN) { + pr_err("sev-guest: host returned unknown error code: %d\n", + vmm_err); + return -EINVAL; + } if (rc) return rc; @@ -375,7 +415,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version, SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data, - resp_len, &arg->fw_err); + resp_len, &arg->exitinfo2); if (rc) goto e_free; @@ -415,7 +455,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version, SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len, - &arg->fw_err); + &arg->exitinfo2); if (rc) return rc; @@ -477,10 +517,10 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques snp_dev->input.data_npages = npages; ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg->msg_version, SNP_MSG_REPORT_REQ, &req.data, - sizeof(req.data), resp->data, resp_len, &arg->fw_err); + sizeof(req.data), resp->data, resp_len, &arg->exitinfo2); /* If certs length is invalid then copy the returned length */ - if (arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) { + if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) { req.certs_len = snp_dev->input.data_npages << PAGE_SHIFT; if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req))) @@ -515,7 +555,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long if (copy_from_user(&input, argp, sizeof(input))) return -EFAULT; - input.fw_err = 0xff; + input.exitinfo2 = SEV_RET_NO_FW_CALL; /* Message version must be non-zero */ if (!input.msg_version) @@ -546,7 +586,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long mutex_unlock(&snp_cmd_mutex); - if (input.fw_err && copy_to_user(argp, &input, sizeof(input))) + if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input))) return -EFAULT; return ret; @@ -696,6 +736,8 @@ static int __init sev_guest_probe(struct platform_device *pdev) if (ret) goto e_free_cert_data; + ratelimit_state_init(&snp_dev->rs, rate_hz * HZ, rate_burst); + dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id); return 0; diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index 256aaeff7e65..8e4144aa78c9 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -52,8 +52,15 @@ struct snp_guest_request_ioctl { __u64 req_data; __u64 resp_data; - /* firmware error code on failure (see psp-sev.h) */ - __u64 fw_err; + /* bits[63:32]: VMM error code, bits[31:0] firmware error code (see psp-sev.h) */ + union { + __u64 exitinfo2; + __u64 fw_err; /* Name deprecated in favor of others */ + struct { + __u32 fw_error; + __u32 vmm_error; + }; + }; }; struct snp_ext_report_req { @@ -77,4 +84,11 @@ struct snp_ext_report_req { /* Get SNP extended report as defined in the GHCB specification version 2. */ #define SNP_GET_EXT_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x2, struct snp_guest_request_ioctl) +/* Guest message request EXIT_INFO_2 constants */ +#define SNP_GUEST_FW_ERR_MASK GENMASK_ULL(31, 0) +#define SNP_GUEST_VMM_ERR_SHIFT 32 + +#define SNP_GUEST_VMM_ERR_INVALID_LEN 1 +#define SNP_GUEST_VMM_ERR_BUSY 2 + #endif /* __UAPI_LINUX_SEV_GUEST_H_ */