mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Pratik R. Sampat" <prsampat@amd.com>
To: <mcgrof@kernel.org>, <russ.weight@linux.dev>, <dakr@kernel.org>,
	<ashish.kalra@amd.com>, <thomas.lendacky@amd.com>,
	<herbert@gondor.apana.org.au>, <davem@davemloft.net>
Cc: <linux-crypto@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<gregkh@linuxfoundation.org>, <rafael@kernel.org>,
	<chao.gao@intel.com>, <aik@amd.com>, <tycho@kernel.org>,
	<nikunj@amd.com>, <michael.roth@amd.com>, <shansinha@google.com>,
	<prsampat@amd.com>
Subject: [Patch v6 5/7] crypto: ccp - Allow SNP platform data to be queried after SNP INIT
Date: Fri, 13 Nov 2026 17:31:58 +0000	[thread overview]
Message-ID: <d18002ac5818f19302ec411355fdc307ce427984.1794531772.git.prsampat@amd.com> (raw)
In-Reply-To: <cover.1794531772.git.prsampat@amd.com>

In preparation for refreshing the cached SNP platform status and feature
information after a successful firmware live update from
DOWNLOAD_FIRMWARE_EX, allow snp_get_platform_data() to be called while
the SNP firmware is in the INIT state.

Once SNP is initialized the firmware requires the output page of both
commands to be firmware-owned. sev->snp_plat_status cannot satisfy that
as it is embedded in struct sev_device, so stage the SNP_PLATFORM_STATUS
output through a dedicated page. Allocate the output pages of both
commands with __snp_alloc_firmware_pages(), which transitions the page
to firmware-owned when SNP is initialized, and release them with
__snp_free_firmware_pages(), which reclaims them before freeing.
__sev_do_snp_platform_status() is not used as it takes sev_cmd_mutex
itself.

Typically this was only ever exercised during initialization. However
DOWNLOAD_FIRMWARE_EX can change that. Live firmware update can race with
IOCTLs such as SEV_FACTORY_RESET which could cause operations performed
on incorrect cached platform states. Avoid this race by holding
sev_cmd_mutex across the whole of sev_get_api_version(), and split its
body out into __sev_get_api_version_locked() for callers that already
hold the mutex.

Co-developed-by: Tycho Andersen (AMD) <tycho@kernel.org>
Signed-off-by: Tycho Andersen (AMD) <tycho@kernel.org>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Pratik R. Sampat <prsampat@amd.com>
---
v5..v6:
* Since "crypto: ccp - Make usage of sev_cmd_mutex more granular" is
  merged, __sev_do_snp_platform_status() takes a sev_cmd_mutex itself,
  so calling it from snp_get_platform_data() with the mutex held would
  deadlock. Instead, stage the SNP_PLATFORM_STATUS output through a page
  from __snp_alloc_firmware_pages() and issue the command directly, the
  same way as SNP_FEATURE_INFO.

* Introduce __sev_get_api_version_locked variant to be used when the
  firmware is updated under a lock. - Sashiko

v4..v5:
No change
---
 drivers/crypto/ccp/sev-dev.c | 40 ++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
index 888d960f68a9..d8a4da1a29d1 100644
--- a/drivers/crypto/ccp/sev-dev.c
+++ b/drivers/crypto/ccp/sev-dev.c
@@ -1265,15 +1265,20 @@ static int snp_get_platform_data(struct sev_device *sev, int *error)
 	struct page *page;
 	int rc;
 
-	/*
-	 * This function is expected to be called before SNP is
-	 * initialized.
-	 */
-	if (sev->snp_initialized)
-		return -EINVAL;
+	lockdep_assert_held(&sev_cmd_mutex);
+
+	page = __snp_alloc_firmware_pages(GFP_KERNEL, 0, true);
+	if (!page)
+		return -ENOMEM;
+
+	buf.address = __psp_pa(page_address(page));
+	rc = __sev_do_cmd_locked(SEV_CMD_SNP_PLATFORM_STATUS, &buf, error);
+	if (!rc)
+		memcpy(&sev->snp_plat_status, page_address(page),
+		       sizeof(sev->snp_plat_status));
+
+	__snp_free_firmware_pages(page, 0, true);
 
-	buf.address = __psp_pa(&sev->snp_plat_status);
-	rc = sev_do_cmd(SEV_CMD_SNP_PLATFORM_STATUS, &buf, error);
 	if (rc) {
 		dev_err(sev->dev, "SNP PLATFORM_STATUS command failed, ret = %d, error = %#x\n",
 			rc, *error);
@@ -1297,7 +1302,7 @@ static int snp_get_platform_data(struct sev_device *sev, int *error)
 	 * command to ensure structure is 8-byte aligned, and does not
 	 * cross a page boundary.
 	 */
-	page = alloc_page(GFP_KERNEL);
+	page = __snp_alloc_firmware_pages(GFP_KERNEL, 0, true);
 	if (!page)
 		return -ENOMEM;
 
@@ -1306,14 +1311,14 @@ static int snp_get_platform_data(struct sev_device *sev, int *error)
 	snp_feat_info.ecx_in = 0;
 	snp_feat_info.feature_info_paddr = __psp_pa(feat_info);
 
-	rc = sev_do_cmd(SEV_CMD_SNP_FEATURE_INFO, &snp_feat_info, error);
+	rc = __sev_do_cmd_locked(SEV_CMD_SNP_FEATURE_INFO, &snp_feat_info, error);
 	if (!rc)
 		sev->snp_feat_info_0 = *feat_info;
 	else
 		dev_err(sev->dev, "SNP FEATURE_INFO command failed, ret = %d, error = %#x\n",
 			rc, *error);
 
-	__free_page(page);
+	__snp_free_firmware_pages(page, 0, true);
 
 	return rc;
 }
@@ -2115,12 +2120,14 @@ void *psp_copy_user_blob(u64 uaddr, u32 len)
 }
 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
 
-static int sev_get_api_version(void)
+static int __sev_get_api_version_locked(void)
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_user_data_status status;
 	int error = 0, ret;
 
+	lockdep_assert_held(&sev_cmd_mutex);
+
 	/*
 	 * Cache SNP platform status and SNP feature information
 	 * if SNP is available.
@@ -2131,7 +2138,7 @@ static int sev_get_api_version(void)
 			return 1;
 	}
 
-	ret = sev_platform_status(&status, &error);
+	ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &status, &error);
 	if (ret) {
 		dev_err(sev->dev,
 			"SEV: failed to get status. Error: %#x\n", error);
@@ -2148,6 +2155,13 @@ static int sev_get_api_version(void)
 	return 0;
 }
 
+static int sev_get_api_version(void)
+{
+	guard(mutex)(&sev_cmd_mutex);
+
+	return __sev_get_api_version_locked();
+}
+
 static int sev_get_firmware(struct device *dev,
 			    const struct firmware **firmware)
 {
-- 
2.43.0


  parent reply	other threads:[~2026-11-13 17:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 17:31 [Patch v6 0/7] Implement SNP live firmware update support Pratik R. Sampat
2026-11-13 17:31 ` [Patch v6 1/7] firmware_loader: Stop pinning modules on registration Pratik R. Sampat
2026-11-13 17:31 ` [Patch v6 2/7] firmware_loader: Stop pinning parent device per workqueue invocation Pratik R. Sampat
2026-11-13 17:31 ` [Patch v6 4/7] crypto: ccp - Factor out the release of the SEV firmware buffers Pratik R. Sampat
2026-11-13 17:31 ` Pratik R. Sampat [this message]
2026-11-13 17:31 ` [Patch v6 6/7] crypto: ccp - Register with fw_uploader and always fail Pratik R. Sampat
2026-11-13 17:32 ` [Patch v6 7/7] crypto: ccp - Implement SNP Download Firmware EX Pratik R. Sampat

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d18002ac5818f19302ec411355fdc307ce427984.1794531772.git.prsampat@amd.com \
    --to=prsampat@amd.com \
    --cc=aik@amd.com \
    --cc=ashish.kalra@amd.com \
    --cc=chao.gao@intel.com \
    --cc=dakr@kernel.org \
    --cc=davem@davemloft.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=michael.roth@amd.com \
    --cc=nikunj@amd.com \
    --cc=rafael@kernel.org \
    --cc=russ.weight@linux.dev \
    --cc=shansinha@google.com \
    --cc=thomas.lendacky@amd.com \
    --cc=tycho@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®