From patchwork Mon Mar 13 12:58:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 68829 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1177510wrd; Mon, 13 Mar 2023 06:13:04 -0700 (PDT) X-Google-Smtp-Source: AK7set9gPoXB4m/pp4RwoOXA7JtO0pPAnp8IgF4OHuHIM6m/X85EqJAM341j2IhHWBW0UgsetM3T X-Received: by 2002:a92:1a43:0:b0:317:99d0:8ad1 with SMTP id z3-20020a921a43000000b0031799d08ad1mr10474782ill.21.1678713184062; Mon, 13 Mar 2023 06:13:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678713184; cv=none; d=google.com; s=arc-20160816; b=WcBtH815PFRDSylFQpT9JlwsugliQFmP3OMGkSkdK6d+IdRCYFrc2/dETov2oRjv0c e+0w77aPjW3H3DR3qIdkT1jM16Xv0BDdZLuSYhxH9Hh1L8sAr4hhUj47RpUidwOq/t1i ELLj3o1DilydD0yfN3aA/nIZhx/uHQCc/sLx6cTyf+n+PXv5tziAvIOJ2O9hcXKUcTzu WdACWFBEZ7UJvfPI5g8JZv9FDfvL8uFgT7VTvbpRDEUjrOS7uHgQDTJ9y30YH/uKYDz3 rue3dhegzdlrSNsAGoxCnECKXFeh0Ph3cTQJqHUeDBb72xxKfNi8GbzP0j7gRoLWbbAe Pk4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=klVnixmbKKQXzPOSoXudW5Vityk7cxQ7t6Ur6l65nwg=; b=NC1aVm2fS5iq79VZ599mXBmQ9H1TAKLgM8GHZdbN/95XaSPrhA3DgLqW9CfjpPVp90 B6nYdCn/KPTxIn78tmdxDSfoKZ8MwrTMRN11jDGcjrnY816ZHN3yks0TZ0ZX6S/2jz84 gb7qADtFJu5gr8Z52vYRMji1B8otXmZJ4Urmr5Zkgy9EuE21BlpfMyF9vN696kcE+c4L UC6qgq22NXGg+XHZdDHjxyFR1rBKWXBbIqSRIEbhclMfvBNyJ2iN5W973LJ+V6YhHRvY 2HYb7UPWUz3evgnHgDyuaPyCtiNppL5xRF+m0Md7oS3Lr7CUdzm84+PRRaxpSwawo9HU 1GTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TXUVF1Cz; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=nDbehQCs; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 18-20020a92c652000000b0032305aa6e98si3455429ill.205.2023.03.13.06.12.49; Mon, 13 Mar 2023 06:13:04 -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=@linutronix.de header.s=2020 header.b=TXUVF1Cz; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=nDbehQCs; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230409AbjCMM6p (ORCPT + 99 others); Mon, 13 Mar 2023 08:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230346AbjCMM6X (ORCPT ); Mon, 13 Mar 2023 08:58:23 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98BF6B455; Mon, 13 Mar 2023 05:58:16 -0700 (PDT) Date: Mon, 13 Mar 2023 12:58:10 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1678712291; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=klVnixmbKKQXzPOSoXudW5Vityk7cxQ7t6Ur6l65nwg=; b=TXUVF1CzACRczBIjFrWMaBwKS4Cl3ODfddQYqIZfZoCwxnClw/gkkN7kRq6NCQh2J+BhkC 0XZWXrEwfjn8xRttWtxP0/JU/PLQ7ybKmeZGsaequl1NyVy4pJQwY/ToPQ6H69h+tD/YnZ BkG6fnrUrOafqR3qpFsgMTC0JOctrzCtmab1t4GR12Le1MGd4/T4lDtn2ooZ3ttCG2F7ST fMyAn8tOM68StY4Nute22m7GG2Z6pWfwaYwI8bQPQwgdaVbuDydo/THpa7OMfPIzboKnMo 8RuEq9ZU4QlAWf6lqmTAeZ3rTZqeUeBwgpJyr8h80Knc403Ew/4VwD6NF2GSpg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1678712291; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=klVnixmbKKQXzPOSoXudW5Vityk7cxQ7t6Ur6l65nwg=; b=nDbehQCsodhDqfS/nqYgLzW5ijVBuS5emBNI7ot+YbNqu5PoqIK7xJBVlSJqYVUc3ZmpCy 3WjmqJJi/TPp6GDA== From: "tip-bot2 for Borislav Petkov (AMD)" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] virt/coco/sev-guest: Simplify extended guest request handling Cc: "Borislav Petkov (AMD)" , Tom Lendacky , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230307192449.24732-4-bp@alien8.de> References: <20230307192449.24732-4-bp@alien8.de> MIME-Version: 1.0 Message-ID: <167871229078.5837.2530695833715446239.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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?1759742389395223996?= X-GMAIL-MSGID: =?utf-8?q?1760258355684567080?= The following commit has been merged into the x86/urgent branch of tip: Commit-ID: 970ab823743fb54b42002ec76c51481f67436444 Gitweb: https://git.kernel.org/tip/970ab823743fb54b42002ec76c51481f67436444 Author: Borislav Petkov (AMD) AuthorDate: Wed, 15 Feb 2023 11:39:41 +01:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 13 Mar 2023 11:27:10 +01:00 virt/coco/sev-guest: Simplify extended guest request handling Return a specific error code - -ENOSPC - to signal the too small cert data buffer instead of checking exit code and exitinfo2. While at it, hoist the *fw_err assignment in snp_issue_guest_request() so that a proper error value is returned to the callers. [ Tom: check override_err instead of err. ] Signed-off-by: Borislav Petkov (AMD) Signed-off-by: Tom Lendacky Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230307192449.24732-4-bp@alien8.de --- arch/x86/kernel/sev.c | 11 +++--- drivers/virt/coco/sev-guest/sev-guest.c | 48 +++++++++++++----------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index c644c34..6a3e142 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2209,15 +2209,16 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned if (ret) goto e_put; + *fw_err = 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) + 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; + ret = -ENOSPC; + } else { + ret = -EIO; + } } e_put: diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index ed5d6ae..e61db0b 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -322,7 +322,8 @@ 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 *fw_err) { - unsigned long err; + unsigned long err, override_err = 0; + unsigned int override_npages = 0; u64 seqno; int rc; @@ -338,6 +339,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in if (rc) return rc; +retry_request: /* * Call firmware to process the request. In this function the encrypted * message enters shared memory with the host. So after this call the @@ -346,17 +348,24 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in */ rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err); - /* - * If the extended guest request fails due to having too small of a - * certificate data buffer, retry the same guest request without the - * extended data request in order to increment the sequence number - * and thus avoid IV reuse. - */ - if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST && - err == SNP_GUEST_REQ_INVALID_LEN) { - const unsigned int certs_npages = snp_dev->input.data_npages; + switch (rc) { + case -ENOSPC: + /* + * If the extended guest request fails due to having too + * small of a certificate data buffer, retry the same + * guest request without the extended data request in + * order to increment the sequence number and thus avoid + * IV reuse. + */ + override_npages = snp_dev->input.data_npages; + exit_code = SVM_VMGEXIT_GUEST_REQUEST; - exit_code = SVM_VMGEXIT_GUEST_REQUEST; + /* + * Override the error to inform callers the given extended + * request buffer size was too small and give the caller the + * required buffer size. + */ + override_err = SNP_GUEST_REQ_INVALID_LEN; /* * If this call to the firmware succeeds, the sequence number can @@ -366,15 +375,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in * of the VMPCK and the error code being propagated back to the * user as an ioctl() return code. */ - rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err); - - /* - * Override the error to inform callers the given extended - * request buffer size was too small and give the caller the - * required buffer size. - */ - err = SNP_GUEST_REQ_INVALID_LEN; - snp_dev->input.data_npages = certs_npages; + goto retry_request; } /* @@ -386,7 +387,10 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in snp_inc_msg_seqno(snp_dev); if (fw_err) - *fw_err = err; + *fw_err = override_err ?: err; + + if (override_npages) + snp_dev->input.data_npages = override_npages; /* * If an extended guest request was issued and the supplied certificate @@ -394,7 +398,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in * prevent IV reuse. If the standard request was successful, return -EIO * back to the caller as would have originally been returned. */ - if (!rc && err == SNP_GUEST_REQ_INVALID_LEN) + if (!rc && override_err == SNP_GUEST_REQ_INVALID_LEN) return -EIO; if (rc) {