Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1670028pxb; Thu, 16 Sep 2021 12:31:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzAjgtlyRS1Q2X9L9nZPMX5ROtCWx3jijb/mEtCJM/c+nFe251s1lQfqZXwVhoHiMsAWj2G X-Received: by 2002:a17:906:7ac5:: with SMTP id k5mr8353046ejo.386.1631820703913; Thu, 16 Sep 2021 12:31:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631820703; cv=none; d=google.com; s=arc-20160816; b=e2MtjNOUqbYnN/3fksN4SaPB4ZHau78hNNt3ZPOBePrwXnRkHOdGHEyqnPYzKih/TY 8k+i5uPBTas9Bp/r9Ku8b4h7/ubR97u3OyTlrECQJp/MMt7y6owp4fSUyFDbWfOzF9ZZ PxvuDBpKUaOSxGS2fdlLbmFhXE22kM51g50A2RFMt0PjHg6FHmqEU/Voie4z+aAGH8Wj UMxoSoreJGcSAybem0yjc3ldyF19Hna+15zlCFD4U/f4OPP1rHH0EKa3R79ZssDwF+xG it/o/glK+fIT7gEdN1FZgtQGbr7q6T0ngvLZ6ihOu82vdMx9i1R/Q5oY20xK0RpJDj+U Si2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=jdH9pjjj7vcL7AHNGRU5LW195zXwmmChS2t98wvuvmc=; b=o9NTrIIsxMG+kqlfLXOar8u3IyFf0SxfX5S8jvZSCou0L2RDn2C1B89STlGTI/DOEt wq7RuQtozqNy09pJw61Yk40dLm0CPj5aPWQLHeNfzuD1VaalNzDvYskgYuf35LT/zPMn j2g1W+K6C5daX+uOsl7gYMfzfvFSMS26ztYVkq6hg3Z1Ey1Q2taY2AmimjH1KV66We69 ncfTxgY2tVapTfc5bVtMkrIpT7LlI6eDHV7BZNEgZKzKg8WT/W8aFgNHHRSYk3hojV0O 9fPxt81lJYlNI0bBAtxM6YopvOXgqUf8No/ll60KRucraDhb8coMieqT8Wfp+ZP/U3Ku zQkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GBTUyJlw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u10si4531124edx.173.2021.09.16.12.31.20; Thu, 16 Sep 2021 12:31:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GBTUyJlw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232918AbhIPQLP (ORCPT + 99 others); Thu, 16 Sep 2021 12:11:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:47282 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233065AbhIPQHX (ORCPT ); Thu, 16 Sep 2021 12:07:23 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C45EE61260; Thu, 16 Sep 2021 16:06:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631808362; bh=nbiyBuqkmhXc8mBRqs8IiGTsvEoSLK5Qk4DzIZOScVI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GBTUyJlwbofdDNh62ibq9B2GEyPAg22bJzsUiU2UGRUlRe/tTeFedJDoDwig7YrHS XUTvwiBiseJgE42l9YCmO+728zagO4unrjfXbpVdTOmldBEs4qnx519b/BZk2oIzmh k8nfgRnGEQIGCPuI5533hszby19lUSmRkuhPYmBA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lucas Nussbaum , stable@kernel.org, Tom Lendacky , Joerg Roedel , Herbert Xu , David Rientjes , Brijesh Singh , Tom Lendacky Subject: [PATCH 5.10 035/306] crypto: ccp - shutdown SEV firmware on kexec Date: Thu, 16 Sep 2021 17:56:20 +0200 Message-Id: <20210916155755.146705807@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155753.903069397@linuxfoundation.org> References: <20210916155753.903069397@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Brijesh Singh commit 5441a07a127f106c9936e4f9fa1a8a93e3f31828 upstream. The commit 97f9ac3db6612 ("crypto: ccp - Add support for SEV-ES to the PSP driver") added support to allocate Trusted Memory Region (TMR) used during the SEV-ES firmware initialization. The TMR gets locked during the firmware initialization and unlocked during the shutdown. While the TMR is locked, access to it is disallowed. Currently, the CCP driver does not shutdown the firmware during the kexec reboot, leaving the TMR memory locked. Register a callback to shutdown the SEV firmware on the kexec boot. Fixes: 97f9ac3db6612 ("crypto: ccp - Add support for SEV-ES to the PSP driver") Reported-by: Lucas Nussbaum Tested-by: Lucas Nussbaum Cc: Cc: Tom Lendacky Cc: Joerg Roedel Cc: Herbert Xu Cc: David Rientjes Signed-off-by: Brijesh Singh Acked-by: Tom Lendacky Signed-off-by: Herbert Xu Signed-off-by: Greg Kroah-Hartman --- drivers/crypto/ccp/sev-dev.c | 49 ++++++++++++++++++++----------------------- drivers/crypto/ccp/sp-pci.c | 12 ++++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -278,6 +278,9 @@ static int __sev_platform_shutdown_locke struct sev_device *sev = psp_master->sev_data; int ret; + if (sev->state == SEV_STATE_UNINIT) + return 0; + ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); if (ret) return ret; @@ -1018,6 +1021,20 @@ e_err: return ret; } +static void sev_firmware_shutdown(struct sev_device *sev) +{ + sev_platform_shutdown(NULL); + + if (sev_es_tmr) { + /* The TMR area was encrypted, flush it from the cache */ + wbinvd_on_all_cpus(); + + free_pages((unsigned long)sev_es_tmr, + get_order(SEV_ES_TMR_SIZE)); + sev_es_tmr = NULL; + } +} + void sev_dev_destroy(struct psp_device *psp) { struct sev_device *sev = psp->sev_data; @@ -1025,6 +1042,8 @@ void sev_dev_destroy(struct psp_device * if (!sev) return; + sev_firmware_shutdown(sev); + if (sev->misc) kref_put(&misc_dev->refcount, sev_exit); @@ -1055,21 +1074,6 @@ void sev_pci_init(void) if (sev_get_api_version()) goto err; - /* - * If platform is not in UNINIT state then firmware upgrade and/or - * platform INIT command will fail. These command require UNINIT state. - * - * In a normal boot we should never run into case where the firmware - * is not in UNINIT state on boot. But in case of kexec boot, a reboot - * may not go through a typical shutdown sequence and may leave the - * firmware in INIT or WORKING state. - */ - - if (sev->state != SEV_STATE_UNINIT) { - sev_platform_shutdown(NULL); - sev->state = SEV_STATE_UNINIT; - } - if (sev_version_greater_or_equal(0, 15) && sev_update_firmware(sev->dev) == 0) sev_get_api_version(); @@ -1114,17 +1118,10 @@ err: void sev_pci_exit(void) { - if (!psp_master->sev_data) - return; - - sev_platform_shutdown(NULL); + struct sev_device *sev = psp_master->sev_data; - if (sev_es_tmr) { - /* The TMR area was encrypted, flush it from the cache */ - wbinvd_on_all_cpus(); + if (!sev) + return; - free_pages((unsigned long)sev_es_tmr, - get_order(SEV_ES_TMR_SIZE)); - sev_es_tmr = NULL; - } + sev_firmware_shutdown(sev); } --- a/drivers/crypto/ccp/sp-pci.c +++ b/drivers/crypto/ccp/sp-pci.c @@ -241,6 +241,17 @@ e_err: return ret; } +static void sp_pci_shutdown(struct pci_dev *pdev) +{ + struct device *dev = &pdev->dev; + struct sp_device *sp = dev_get_drvdata(dev); + + if (!sp) + return; + + sp_destroy(sp); +} + static void sp_pci_remove(struct pci_dev *pdev) { struct device *dev = &pdev->dev; @@ -370,6 +381,7 @@ static struct pci_driver sp_pci_driver = .id_table = sp_pci_table, .probe = sp_pci_probe, .remove = sp_pci_remove, + .shutdown = sp_pci_shutdown, .driver.pm = &sp_pci_pm_ops, };