From nobody Thu Sep 18 10:19:09 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DC52C352A1 for ; Wed, 7 Dec 2022 01:02:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229680AbiLGBCc (ORCPT ); Tue, 6 Dec 2022 20:02:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229684AbiLGBC2 (ORCPT ); Tue, 6 Dec 2022 20:02:28 -0500 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 574A44F18E for ; Tue, 6 Dec 2022 17:02:27 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id d3-20020a056a0010c300b005728633819aso14053316pfu.8 for ; Tue, 06 Dec 2022 17:02:27 -0800 (PST) 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=Kq4nsNsu12aUA8GuDAR6Rk/S1sw8FFUkbgn/FDY4oJQ=; b=jHhlxV/UdgCOLo3JvbhKjdSCPKfo+9YiKMvILOwzfOeo+1FL0arji+cdZXGjyGCoxB M36Mkfxno3gFNpEf4awKk+CQ6qVw/+bPB9N3JN7odi6kusKJ8pMqmLoTFH9hxcnRsdl6 lBG+/2HEsd9cwAnxSD2pSZT/LKUNUXn/GcXYbDPLpFZ8z8bk1r8bxD3gx9bszDl+gPst xRs8ai9nkPxFjAFwtWY2MqlhWQsLn7BG8YMcnELOd5ziGzib4Kj6KqCSf2NJOmwP2TOM 7PLlqgNyqF3z7iuoect4es25mOXHQeCRutMjntilg60uY0KsLhwrUTGi9uV+m0oaynkI UMkw== 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=Kq4nsNsu12aUA8GuDAR6Rk/S1sw8FFUkbgn/FDY4oJQ=; b=l1oQkxw+lxtNXr+IRJHRULuLLCTFSSmmHVnrl1bGpjs+koHn9EqF6/uUzKzZCV0pxU 9/5Gy/TSjFM3W7Do8MKOtZFUDj9LbszNo3TSj1PzpVpy6Q0lVrDfDb+hXzg2MdY5Xh0N 2ksCcfDYywFULZ1SYPp6x1JlbvdxHE4HXf9v7dWcZ0mceBtQjUYH2LgjIFCuP1P5zeLU 1pqaJIiQ9Jh7BVq/KwvGzfdqY1/9hUA3Pqgu56J8d+f4GYY4ao25lpkzw2eu89wGGCJN 6mufmyHsgKXkJruhkUnQZyTJHWPWnJDx6ktF1Jf9QcsNWEk0Qj7ti9KGjpoXR1bfUcTY Lf3w== X-Gm-Message-State: ANoB5pl8sYUBV8jv7r5G2pHuPuhK+rVVFZ4UtwAI33azhnOlXCAebJ5H 6BN1w7EnCSnn/skFTC6t014qfmyed+Tu3kRPTdSFsgNaJ77M4KsIH5KKfEZubvKtuoPPgjVj3NH No9gnHEQXqt7NTpHW0P6PgrMNqyNMiSXGkZW+H4dTulg/3iK1EXNvi5e3GYPo4ooU/Pw+eH5uxj JpssOroQE= X-Google-Smtp-Source: AA0mqf7KPhbYWHDviXrCu4S/UJq0eb4mK4Ff0kr7Wz7VyQepAfO88MjO7fplHFKp5jjSMC9Zds9mF0OxKsOTXNqQhQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a05:6a00:1f0f:b0:56e:7424:bc0f with SMTP id be15-20020a056a001f0f00b0056e7424bc0fmr81486148pfb.11.1670374946793; Tue, 06 Dec 2022 17:02:26 -0800 (PST) Date: Wed, 7 Dec 2022 01:02:07 +0000 In-Reply-To: <20221207010210.2563293-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221207010210.2563293-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221207010210.2563293-2-dionnaglaze@google.com> Subject: [PATCH v9 1/4] crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Peter Gonda , Thomas Lendacky , Paolo Bonzini , Joerg Roedel , Ingo Molnar , Andy Lutomirsky , John Allen , Herbert Xu , "David S. Miller" , Borislav Petkov , Dionna Glaze Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Peter Gonda The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called. To make this protocol unambiguous, the value is named SEV_RET_NO_FW_CALL. Cc: Thomas Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Ingo Molnar Cc: Andy Lutomirsky Cc: John Allen Cc: Herbert Xu Cc: "David S. Miller" Cc: Borislav Petkov Signed-off-by: Peter Gonda Signed-off-by: Dionna Glaze Acked-by: Tom Lendacky --- Documentation/virt/coco/sev-guest.rst | 2 +- drivers/crypto/ccp/sev-dev.c | 22 ++++++++++++++-------- include/uapi/linux/psp-sev.h | 7 +++++++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coc= o/sev-guest.rst index bf593e88cfd9..e76393e389eb 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -41,7 +41,7 @@ The guest ioctl should be issued on a file descriptor of = the /dev/sev-guest devi The ioctl accepts struct snp_user_guest_request. The input and output stru= cture is specified through the req_data and resp_data field respectively. If the io= ctl fails to execute due to a firmware error, then fw_err code will be set otherwise= the -fw_err will be set to 0x00000000000000ff. +fw_err will be set to 0x00000000ffffffff, i.e., the lower 32-bits are -1. =20 The firmware checks that the message sequence counter is one greater than the guests message sequence counter. If guest driver fails to increment me= ssage diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 06fc7156c04f..ac205f78a595 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -440,12 +440,19 @@ static int __sev_init_ex_locked(int *error) return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); } =20 +static inline int __sev_do_init_locked(int *psp_ret) +{ + if (sev_init_ex_buffer) + return __sev_init_ex_locked(psp_ret); + else + return __sev_init_locked(psp_ret); +} + static int __sev_platform_init_locked(int *error) { struct psp_device *psp =3D psp_master; struct sev_device *sev; - int rc =3D 0, psp_ret =3D -1; - int (*init_function)(int *error); + int rc =3D 0, psp_ret =3D SEV_RET_NO_FW_CALL; =20 if (!psp || !psp->sev_data) return -ENODEV; @@ -456,15 +463,12 @@ static int __sev_platform_init_locked(int *error) return 0; =20 if (sev_init_ex_buffer) { - init_function =3D __sev_init_ex_locked; rc =3D sev_read_init_ex_file(); if (rc) return rc; - } else { - init_function =3D __sev_init_locked; } =20 - rc =3D init_function(&psp_ret); + rc =3D __sev_do_init_locked(&psp_ret); if (rc && psp_ret =3D=3D SEV_RET_SECURE_DATA_INVALID) { /* * Initialization command returned an integrity check failure @@ -473,9 +477,11 @@ static int __sev_platform_init_locked(int *error) * initialization function should succeed by replacing the state * with a reset state. */ - dev_err(sev->dev, "SEV: retrying INIT command because of SECURE_DATA_INV= ALID error. Retrying once to reset PSP SEV state."); - rc =3D init_function(&psp_ret); + dev_err(sev->dev, +"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying= once to reset PSP SEV state."); + rc =3D __sev_do_init_locked(&psp_ret); } + if (error) *error =3D psp_ret; =20 diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..e8cfb8bde0d7 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. + * The SEV API error codes are 16 bits, so the -1 value will not overlap + * with possible values from the specification. + */ + SEV_RET_NO_FW_CALL =3D -1, SEV_RET_SUCCESS =3D 0, SEV_RET_INVALID_PLATFORM_STATE, SEV_RET_INVALID_GUEST_STATE, --=20 2.39.0.rc0.267.gcb52ba06e7-goog From nobody Thu Sep 18 10:19:09 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5806C3A5A7 for ; Wed, 7 Dec 2022 01:02:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229507AbiLGBCh (ORCPT ); Tue, 6 Dec 2022 20:02:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229748AbiLGBCc (ORCPT ); Tue, 6 Dec 2022 20:02:32 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8540B4F18B for ; Tue, 6 Dec 2022 17:02:30 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id m2-20020a17090a730200b0021020cce6adso18762pjk.3 for ; Tue, 06 Dec 2022 17:02:30 -0800 (PST) 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=GMGURl9LxXOJnwuiD/Ha/hCLoLpmM+oX4OEPqCyeymM=; b=UnmJAc0+Og3g47jm5MGaU3e4YuK2OhTwtsGRAnE+9pvMlfCuaTzH7EZE7WcSBRX0Wh AKLocDxsjvMLxJJkLhsGKnNDEx1JQWlzXUWnR61gNweuPVmV1CzHsx4+uUV4/OnyJO0s MLMNsP1LAQA8Cxn+sBx0w9M5RouMTpSjk21NvueI3YPQMpCHuGvJXnlp2rZDFYP8VeTz Mqlj1ni5AQ4UNXQadNqMHpmCXmZKiEN3nBvCualsOb/PBMliLpe4hOxRrudAeL6vX6VC mBp0DxpFy1jKjHmy29BB1UOiMIxKMG3eJJV9hTsQVYNV/kSHCmoPNYJ8sb1zMOKoZqC0 CHlw== 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=GMGURl9LxXOJnwuiD/Ha/hCLoLpmM+oX4OEPqCyeymM=; b=NGYcBwwl3uP1mblqrSLOI9YD75O0YYi/9aXcQqaw3ugvP9DhCI8JBTFHI4iB/4vsMu QtlY8WHI2+0/LjAhhw6NZ5ITcBOgWlw5q4KnUIGowKcrijsTer+HoQRA4viQh1sEgRte pfmfWl2ZBnk+3L1xw1Lo5SEMRRVuXeAk+PEOzGYsoFq107p0+lfyv3W5XVugiYy3V9U9 GWzwboppmdctthGlGNbsYHmai0xROo1Egn1puO/n0Q5Rro8OnY10aihp3zDUN8FxmfSE 4z8kQrAoorGopLu3DJ7ml8gWeRygnv/ywWYnvVKSMCqxMd9MqHHxJAxN1LTplK+owKMs 6qCQ== X-Gm-Message-State: ANoB5plyYI2Mw8cp1xetcmMyjKejvLCzTBEjugV0AfLCsWxvtpPQ9gaO t+ytMtj4sSVoiIyMf/nHSTdYXPF+f1UAZ4bT4n0LeabogNa6GvtttRCifY1JtVA8b3Kkw2yrE0E kF7tZCxJZ5h+hXx8QrXiwXCLXDruQR/Ffiq8AporzRAgmBICydrXuIK0DRglvvJ2SUgdgu/YZGm QzgyWdq9k= X-Google-Smtp-Source: AA0mqf6qnUoKXk5tirgCkojnoRDceiVqwr4B00hVzZ9eumlk28+jh+JCxJkTyE8o26JIiCFIwgzT1AOM9ltZJKSC5g== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a65:6385:0:b0:478:e68b:7969 with SMTP id h5-20020a656385000000b00478e68b7969mr3710242pgv.574.1670374949949; Tue, 06 Dec 2022 17:02:29 -0800 (PST) Date: Wed, 7 Dec 2022 01:02:08 +0000 In-Reply-To: <20221207010210.2563293-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221207010210.2563293-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221207010210.2563293-3-dionnaglaze@google.com> Subject: [PATCH v9 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 , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Venu Busireddy , Michael Roth , "Kirill A. Shutemov" , Michael Sterritt Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 the call is bookended 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 Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Venu Busireddy Cc: Michael Roth Cc: "Kirill A. Shutemov" Cc: Michael Sterritt Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs") Reviewed-by: Tom Lendacky Reviewed-by: Borislav Petkov Signed-off-by: Dionna Glaze --- 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..05de34d10d89 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, unsign= ed 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, uns= igned 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_da= ta *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 =20 #include #include @@ -2175,7 +2176,7 @@ static int __init init_sev_config(char *str) } __setup("sev=3D", init_sev_config); =20 -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, uns= igned 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 sn= p_req_data *input, unsigned if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; =20 - if (!fw_err) + if (!exitinfo2) return -EINVAL; =20 + *exitinfo2 =3D 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 s= np_req_data *input, unsigned if (ret) goto e_put; =20 + *exitinfo2 =3D ghcb->save.sw_exit_info_2; if (ghcb->save.sw_exit_info_2) { /* Number of expected pages are returned in RBX */ if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && ghcb->save.sw_exit_info_2 =3D=3D SNP_GUEST_REQ_INVALID_LEN) input->data_npages =3D ghcb_get_rbx(ghcb); =20 - *fw_err =3D ghcb->save.sw_exit_info_2; - ret =3D -EIO; } =20 --=20 2.39.0.rc0.267.gcb52ba06e7-goog From nobody Thu Sep 18 10:19:09 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87B96C3A5A7 for ; Wed, 7 Dec 2022 01:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229843AbiLGBCt (ORCPT ); Tue, 6 Dec 2022 20:02:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229821AbiLGBCj (ORCPT ); Tue, 6 Dec 2022 20:02:39 -0500 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 B9BB7BC6 for ; Tue, 6 Dec 2022 17:02:34 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id o31-20020a17090a0a2200b00219a2904952so38173pjo.0 for ; Tue, 06 Dec 2022 17:02:34 -0800 (PST) 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=LYb5Kkl7KCSyA25o8WI4Ic+8SfxAdiy9vbY0soW+D/8=; b=Pi2ZKrH8sVupiZlbc+zhoHxcKrCy9Td9o6yADcsSdTeQ0fbjZ+F/PZxeKNtHfFKZGY ya1GLHf7PmNpSghZ/GPX9Ckgrb7ovCtW8nKRE7sej87O/ZIC6c4GJWOQMqjTJNcm6gHi UdQy46PkhThDKH+wx7eWq0OeGcGL1LLc/h+wzh/QsRQ2X0RKtlho0JmQP+eZcxVeJM7h tbiHXnSgIFf89/MJk69ZIzWPhTSShdVexyk9q42ZCpqQC0vreRf5QBBGQyTpS8ZIzMcf HmlC/+ydM+hLQsGT+iSfJc6UU4p/H47/btHjSc7l9DjstB2Seb8YCA4MhPDyKuBsJ57c eyXw== 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=LYb5Kkl7KCSyA25o8WI4Ic+8SfxAdiy9vbY0soW+D/8=; b=aVpudRnFh1/Uf05xGLXmZUrNcAcPDmq/N7CTG8uHLe0Ed1QhWm7VJQ4/hJp8b4W34Y DmROyYk2KHm2MFNIl9bpRL9iGpVbXZg8di+Y5DVQuqfAL9NN+uRmA6lfLdSocowMnf+Y ysF64HB0iEgMMFFSSuSFQiv60jMIwaPhyDD/dhA7ty91Ew5942tvAQ1YI9P1ZNmulmm1 a9PsYsm9dGYwCWDXZzbR3AUuWkYAIwqVGCt2S14HeticaiZ6oiKmPTicgf/FB0+D2VZo J8/dknYXDLhtdcjBiyV0+H7Tk0LQnt9It1G93NPYqRzXd0UgCQPrUroJvP3U41KIKmER YwPQ== X-Gm-Message-State: ANoB5pmpFsQebPL1tICr9ULq3mx0iZkGjra9G9Pec+T7LFn1blNNqINM T2A1eGK5Ho/BzyRPP1H7k2a+mD3LrJhMDEVYEpehpOIAUPA775rdOAtlIMLlp+M/d6vJzm/ikCt PLfW5Hf+4EtfiPdp2XJ3qO2Xj9PuAbn1OyQjXE7XXzpQlnec84y7zli7uS/EsGWzcD8PbteoYf1 mh2COWhxI= X-Google-Smtp-Source: AA0mqf79S2a8pHYPXB39OoGONxOXjRLJO03JVGSUEE07nKwCBsq1o57fflyv7qPwTVPzLRSSDqh70drKt+BYeGLeLg== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:902:9004:b0:17c:9a37:72fb with SMTP id a4-20020a170902900400b0017c9a3772fbmr83968713plp.82.1670374953834; Tue, 06 Dec 2022 17:02:33 -0800 (PST) Date: Wed, 7 Dec 2022 01:02:09 +0000 In-Reply-To: <20221207010210.2563293-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221207010210.2563293-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221207010210.2563293-4-dionnaglaze@google.com> Subject: [PATCH v9 3/4] virt: 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 , Borislav Petkov , Haowen Bai , Liam Merwick , Yang Yingliang Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 that's done by passing along the ioctl argument. This removes the need for an argument to handle_guest_request. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Cc: Borislav Petkov Cc: Haowen Bai Cc: Liam Merwick Cc: Yang Yingliang Fixes: fce96cf04430 ("virt: Add SEV-SNP guest driver") Reviewed-by: Tom Lendacky Signed-off-by: Dionna Glaze --- drivers/virt/coco/sev-guest/sev-guest.c | 35 ++++++++++++------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 1ea6d2e5b218..4a2a0a02985f 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -320,11 +320,11 @@ static int enc_payload(struct snp_guest_dev *snp_dev,= u64 seqno, int version, u8 return __enc_payload(snp_dev, req, payload, sz); } =20 -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_co= de, int msg_ver, +static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_co= de, + struct snp_guest_request_ioctl *arg, u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err) + u32 resp_sz) { - unsigned long err; u64 seqno; int rc; =20 @@ -336,7 +336,8 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); =20 /* Encrypt the userspace provided payload */ - rc =3D enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc =3D enc_payload(snp_dev, seqno, arg->msg_version, type, req_buf, + req_sz); if (rc) return rc; =20 @@ -346,7 +347,8 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in * sequence number must be incremented or the VMPCK must be deleted to * prevent reuse of the IV. */ - rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, &err); + rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, + &arg->fw_err); =20 /* * If the extended guest request fails due to having too small of a @@ -368,24 +370,22 @@ 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 =3D snp_issue_guest_request(exit_code, &snp_dev->input, &err); + rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, + &arg->fw_err); =20 /* * Override the error to inform callers the given extended * request buffer size was too small and give the caller the * required buffer size. */ - err =3D SNP_GUEST_REQ_INVALID_LEN; + arg->fw_err =3D SNP_GUEST_REQ_INVALID_LEN; snp_dev->input.data_npages =3D certs_npages; } =20 - if (fw_err) - *fw_err =3D err; - if (rc) { dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", - rc, *fw_err); + rc, arg->fw_err); goto disable_vmpck; } =20 @@ -432,9 +432,9 @@ static int get_report(struct snp_guest_dev *snp_dev, st= ruct snp_guest_request_io if (!resp) return -ENOMEM; =20 - rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_= version, + rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data, - resp_len, &arg->fw_err); + resp_len); if (rc) goto e_free; =20 @@ -472,9 +472,8 @@ static int get_derived_key(struct snp_guest_dev *snp_de= v, struct snp_guest_reque if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) return -EFAULT; =20 - rc =3D 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); + rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, + SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len); if (rc) return rc; =20 @@ -534,9 +533,9 @@ static int get_ext_report(struct snp_guest_dev *snp_dev= , struct snp_guest_reques return -ENOMEM; =20 snp_dev->input.data_npages =3D npages; - ret =3D handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg-= >msg_version, + ret =3D handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg, SNP_MSG_REPORT_REQ, &req.data, - sizeof(req.data), resp->data, resp_len, &arg->fw_err); + sizeof(req.data), resp->data, resp_len); =20 /* If certs length is invalid then copy the returned length */ if (arg->fw_err =3D=3D SNP_GUEST_REQ_INVALID_LEN) { --=20 2.39.0.rc0.267.gcb52ba06e7-goog From nobody Thu Sep 18 10:19:09 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A80CC352A1 for ; Wed, 7 Dec 2022 01:02:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229909AbiLGBCz (ORCPT ); Tue, 6 Dec 2022 20:02:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229740AbiLGBCp (ORCPT ); Tue, 6 Dec 2022 20:02:45 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 305FD65AE for ; Tue, 6 Dec 2022 17:02:39 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id e202-20020a2550d3000000b006f9d739c724so17271018ybb.6 for ; Tue, 06 Dec 2022 17:02:39 -0800 (PST) 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=qY65PLc6zHsJD/cqwYCmdutorWBhwj9IJtq2nl4qVh0=; b=N5JtHkFJu7EcrIkzhRX8/1J/XJJxYi8LLh2X9EJO+I5Bp9wooXQ+/J56glpbSKKHJY TiIkF68szGjOggKORR16V6ZfGu4b+cbrxQ9Jfu8eN3LoXwfSnCi4W5l2e+KHPeXnBIyt 6fN4qkFNL/Jg2ntVUA/ajNI0wNapuKVqheulg/rOwVuUR4L61aD04Cdw6sblC71tDgYn J89lf8e7miOheJ7q/p0ukdsn0U8/xD0M7c2sOX/vV1JmcjE7UlvdbKmZ4ebtJdNSDXjn B6X/L81oP0qxNl+TKEdv1qLj+JFkgo4tWqiZid0ie7XcfMmKb82chnksBp7QedpL5Y5q uPOw== 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=qY65PLc6zHsJD/cqwYCmdutorWBhwj9IJtq2nl4qVh0=; b=1TzmCJj4/ocIjGCSVOI73kQOwqCNMV3E3uIvsl30KG3UK2W/HW6Ot1WP/mhvW9otQ7 UBC37yhhF3BdLk+ZsZqBWAVWA9nqIpQ7hbX2GDMbMKWsVvBnnQqqy1bqeNuvjHHj/WnJ AfuBbMB4MdWT94fpUnxdMW40DuBfDSFIuT0tOCO9z2QtjdUBxRpUVvZgewkwRTPmtir/ Tl/S3ZQVoLRMG/WKQUkDvw2JB3ZKL1fg906ZZbwrckzB6/8mZmpPK48XYTC98fnk5znz blEMMA/sFEQu4vksVQTiMWsHrHd9e9uuJwwqzqBSzVIAxFzVX5dFWMf/LFdXzkPPMoIM y10g== X-Gm-Message-State: ANoB5pkgfP5ufXoVYdR3WdEEk5gTns0W2vzdBvjQ5aVMQcM2spoEPbxk EcRXryWafaHFMqgOHo8+BPg/Grnp5b9hGH8Gmy0zNFCqlqP0DZQrkXLTdcdOrdZoXovgQ5WDPnJ hOnmT3+7j84aOubdOuvvM/goR7DymqB5r7G5NlFQJOaQDxwdF9E+diFjxyn5CHsI5hJSdeBDdqi CWttR195s= X-Google-Smtp-Source: AA0mqf4nARsrbJ+BCZvEiNFaJC5TVn0q06r3enoph2iD8nzvZ8Qtq8gNSGhkVpLKa6Q2oRHUrKDwZAoBvWMSSn1DcA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a25:391:0:b0:706:d51a:bf7e with SMTP id 139-20020a250391000000b00706d51abf7emr3049290ybd.416.1670374958257; Tue, 06 Dec 2022 17:02:38 -0800 (PST) Date: Wed, 7 Dec 2022 01:02:10 +0000 In-Reply-To: <20221207010210.2563293-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221207010210.2563293-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221207010210.2563293-5-dionnaglaze@google.com> Subject: [PATCH v9 4/4] virt: 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 , Peter Gonda , Borislav Petkov , Tom Lendacky , Liam Merwick , Yang Yingliang , Haowen Bai Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Add BIT_ULL(33) for "host refused the request for throttling reasons". The driver interprets 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, control must remain in the kernel to complete the request with the current sequence number. Returning without finishing the request allows the the guest to make another request but with different message contents. This is IV reuse, and breaks cryptographic protections. When throttled, the driver will reschedule itself and then try again after sleeping half its ratelimit time to avoid a big wait queue. The ioctl may block indefinitely, but that has always been the case when deferring these requests to the host. Cc: Tom Lendacky Cc: Peter Gonda Cc: Borislav Petkov Cc: Tom Lendacky Cc: Liam Merwick Cc: Yang Yingliang Cc: Haowen Bai Reviewed-by: Tom Lendacky Signed-off-by: Dionna Glaze --- Documentation/virt/coco/sev-guest.rst | 21 +++++++---- drivers/virt/coco/sev-guest/sev-guest.c | 49 ++++++++++++++++++++----- include/uapi/linux/sev-guest.h | 18 ++++++++- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coc= o/sev-guest.rst index e76393e389eb..fd8bf833f1cd 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -37,11 +37,12 @@ along with a description: the return value. General error numbers (-ENOMEM, -EINVAL) are not detailed, but errors with specific meanings are. =20 -The guest ioctl should be issued on a file descriptor of the /dev/sev-gues= t device. -The ioctl accepts struct snp_user_guest_request. The input and output stru= cture is -specified through the req_data and resp_data field respectively. If the io= ctl fails -to execute due to a firmware error, then fw_err code will be set otherwise= the -fw_err will be set to 0x00000000ffffffff, i.e., the lower 32-bits are -1. +The guest ioctl should be issued on a file descriptor of the +/dev/sev-guest device. The ioctl accepts struct +snp_user_guest_request. The input and output structure is specified +through the req_data and resp_data field respectively. If the ioctl +fails to execute due to a firmware error, then the fw_error code will +be set, otherwise fw_error will be set to -1. =20 The firmware checks that the message sequence counter is one greater than the guests message sequence counter. If guest driver fails to increment me= ssage @@ -57,8 +58,14 @@ counter (e.g. counter overflow), then -EIO will be retur= ned. __u64 req_data; __u64 resp_data; =20 - /* 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; + struct { + __u32 fw_error; + __u32 vmm_error; + }; + }; }; =20 2.1 SNP_GET_REPORT diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 4a2a0a02985f..6f62b90de4da 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; }; =20 static u32 vmpck_id; module_param(vmpck_id, uint, 0444); MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with th= e PSP."); =20 +static int rate_s =3D 1; +module_param(rate_s, int, 0444); +MODULE_PARM_DESC(rate_s, "The rate limit interval in seconds to limit requ= ests to."); + +static int rate_burst =3D 2; +module_param(rate_burst, int, 0444); +MODULE_PARM_DESC(rate_burst, "The rate limit burst amount to limit request= s to."); + /* Mutex to serialize the shared buffer access and command handling. */ static DEFINE_MUTEX(snp_cmd_mutex); =20 @@ -341,6 +352,16 @@ static int handle_guest_request(struct snp_guest_dev *= snp_dev, u64 exit_code, if (rc) return rc; =20 +retry: + /* + * Rate limit commands internally since the host can also throttle, and + * the guest shouldn't create a tight request spin that could end up + * getting this VM throttled more heavily. + */ + if (!__ratelimit(&snp_dev->rs)) { + schedule_timeout_interruptible((rate_s * HZ) / 2); + goto retry; + } /* * Call firmware to process the request. In this function the encrypted * message enters shared memory with the host. So after this call the @@ -348,7 +369,15 @@ static int handle_guest_request(struct snp_guest_dev *= snp_dev, u64 exit_code, * prevent reuse of the IV. */ rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, - &arg->fw_err); + &arg->exitinfo2); + + /* + * 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 (arg->vmm_error =3D=3D SNP_GUEST_VMM_ERR_BUSY) + goto retry; =20 /* * If the extended guest request fails due to having too small of a @@ -357,7 +386,7 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, * and thus avoid IV reuse. */ if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && - err =3D=3D SNP_GUEST_REQ_INVALID_LEN) { + arg->vmm_error =3D=3D SNP_GUEST_VMM_ERR_INVALID_LEN) { const unsigned int certs_npages =3D snp_dev->input.data_npages; =20 exit_code =3D SVM_VMGEXIT_GUEST_REQUEST; @@ -371,21 +400,21 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, * user as an ioctl() return code. */ rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, - &arg->fw_err); + &arg->exitinfo2); =20 /* * Override the error to inform callers the given extended * request buffer size was too small and give the caller the * required buffer size. */ - arg->fw_err =3D SNP_GUEST_REQ_INVALID_LEN; + arg->vmm_error =3D SNP_GUEST_VMM_ERR_INVALID_LEN; snp_dev->input.data_npages =3D certs_npages; } =20 if (rc) { dev_alert(snp_dev->dev, - "Detected error from ASP request. rc: %d, fw_err: %llu\n", - rc, arg->fw_err); + "Detected error from ASP request. rc: %d, exitinfo2: %llu\n", + rc, arg->exitinfo2); goto disable_vmpck; } =20 @@ -538,7 +567,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev= , struct snp_guest_reques sizeof(req.data), resp->data, resp_len); =20 /* If certs length is invalid then copy the returned length */ - if (arg->fw_err =3D=3D SNP_GUEST_REQ_INVALID_LEN) { + if (arg->vmm_error =3D=3D SNP_GUEST_VMM_ERR_INVALID_LEN) { req.certs_len =3D snp_dev->input.data_npages << PAGE_SHIFT; =20 if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req))) @@ -573,7 +602,7 @@ static long snp_guest_ioctl(struct file *file, unsigned= int ioctl, unsigned long if (copy_from_user(&input, argp, sizeof(input))) return -EFAULT; =20 - input.fw_err =3D 0xff; + input.exitinfo2 =3D SEV_RET_NO_FW_CALL; =20 /* Message version must be non-zero */ if (!input.msg_version) @@ -604,7 +633,7 @@ static long snp_guest_ioctl(struct file *file, unsigned= int ioctl, unsigned long =20 mutex_unlock(&snp_cmd_mutex); =20 - if (input.fw_err && copy_to_user(argp, &input, sizeof(input))) + if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input))) return -EFAULT; =20 return ret; @@ -754,6 +783,8 @@ static int __init sev_guest_probe(struct platform_devic= e *pdev) if (ret) goto e_free_cert_data; =20 + ratelimit_state_init(&snp_dev->rs, rate_s * HZ, rate_burst); + dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck= _id); return 0; =20 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; =20 - /* 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-s= ev.h) */ + union { + __u64 exitinfo2; + __u64 fw_err; /* Name deprecated in favor of others */ + struct { + __u32 fw_error; + __u32 vmm_error; + }; + }; }; =20 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_g= uest_request_ioctl) =20 +/* 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_ */ --=20 2.39.0.rc0.267.gcb52ba06e7-goog