Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Michael Roth <michael.roth@amd.com>
To: <linux-crypto@vger.kernel.org>
Cc: <herbert@gondor.apana.org.au>, <davem@davemloft.net>,
	<ackerleytng@google.com>, <thomas.lendacky@amd.com>,
	<ashish.kalra@amd.com>, <aik@amd.com>, <seanjc@google.com>
Subject: [PATCH 4/4] crypto: ccp - Allocate status page outside sev_cmd_mutex in SNP_PLATFORM_STATUS
Date: Wed, 14 Oct 2026 08:20:33 -0500	[thread overview]
Message-ID: <20261014132145.1726322-5-michael.roth@amd.com> (raw)
In-Reply-To: <20261014132145.1726322-1-michael.roth@amd.com>

Allocating memory while holding the global SEV command mutex
(sev_cmd_mutex) unnecessarily blocks concurrent SEV and SNP operations
across the system if page allocation stalls or triggers memory reclaim.

Rename __sev_do_snp_platform_status() to sev_do_snp_platform_status()
and push the sev_cmd_mutex and firmware call down into a new routine so
that allocation/freeing of the status page can be done outside of the
mutex.

Co-developed-by: Ackerley Tng <ackerleytng@google.com>
Signed-off-by: Ackerley Tng <ackerleytng@google.com>
Signed-off-by: Michael Roth <michael.roth@amd.com>
---
 drivers/crypto/ccp/sev-dev.c | 60 +++++++++++++++++++++---------------
 1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
index c8c229a6757c..37b948a81bc9 100644
--- a/drivers/crypto/ccp/sev-dev.c
+++ b/drivers/crypto/ccp/sev-dev.c
@@ -2554,20 +2554,13 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
 	return ret;
 }
 
-static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status,
-					int *error)
+static int __sev_do_snp_platform_status(void *data, int *error, bool *free)
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_data_snp_addr buf;
-	struct page *status_page;
-	void *data;
 	int ret;
 
-	status_page = alloc_page(GFP_KERNEL_ACCOUNT);
-	if (!status_page)
-		return -ENOMEM;
-
-	data = page_address(status_page);
+	*free = true;
 
 	/*
 	 * SNP_PLATFORM_STATUS can be executed in any SNP state. But if executed
@@ -2578,10 +2571,8 @@ static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status,
 		 * Firmware expects the status page to be in Firmware state,
 		 * otherwise it will report an error INVALID_PAGE_STATE.
 		 */
-		if (rmp_mark_pages_firmware(__pa(data), 1, true)) {
-			ret = -EFAULT;
-			goto cleanup;
-		}
+		if (rmp_mark_pages_firmware(__pa(data), 1, true))
+			return -EFAULT;
 	}
 
 	buf.address = __psp_pa(data);
@@ -2591,19 +2582,45 @@ static int __sev_do_snp_platform_status(struct sev_user_data_snp_status *status,
 		/*
 		 * The status page will be in Reclaim state on success, or left
 		 * in Firmware state on failure. Use snp_reclaim_pages() to
-		 * transition either case back to Hypervisor-owned state.
+		 * transition either case back to Hypervisor-owned state. If
+		 * for some reason that fails, it will have been added to the
+		 * leaked pages list, so indicate to the caller to not free
+		 * the page.
 		 */
-		if (snp_reclaim_pages(__pa(data), 1, true))
+		if (snp_reclaim_pages(__pa(data), 1, true)) {
+			*free = false;
 			return -EFAULT;
+		}
 	}
 
+	return ret;
+}
+
+static int sev_do_snp_platform_status(struct sev_user_data_snp_status *status,
+				      int *error)
+{
+	struct page *status_page;
+	void *data;
+	bool free;
+	int ret;
+
+	status_page = alloc_page(GFP_KERNEL_ACCOUNT);
+	if (!status_page)
+		return -ENOMEM;
+
+	data = page_address(status_page);
+
+	scoped_guard(mutex, &sev_cmd_mutex)
+		ret = __sev_do_snp_platform_status(data, error, &free);
+
 	if (ret)
 		goto cleanup;
 
 	memcpy(status, data, sizeof(*status));
 
 cleanup:
-	__free_pages(status_page, 0);
+	if (free)
+		__free_pages(status_page, 0);
 	return ret;
 }
 
@@ -2615,8 +2632,7 @@ static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
 	if (!argp->data)
 		return -EINVAL;
 
-	scoped_guard(mutex, &sev_cmd_mutex)
-		ret = __sev_do_snp_platform_status(&status, &argp->error);
+	ret = sev_do_snp_platform_status(&status, &argp->error);
 	if (ret < 0)
 		return ret;
 
@@ -3114,9 +3130,7 @@ static int get_v1_svn(struct sev_device *sev)
 	struct sev_user_data_snp_status status;
 	int ret, error = 0;
 
-	mutex_lock(&sev_cmd_mutex);
-	ret = __sev_do_snp_platform_status(&status, &error);
-	mutex_unlock(&sev_cmd_mutex);
+	ret = sev_do_snp_platform_status(&status, &error);
 	if (ret < 0)
 		return ret;
 
@@ -3131,9 +3145,7 @@ static int get_v2_svn(struct sev_device *sev)
 	struct sev_snp_tcb_version_turin *tcb;
 	int ret, error = 0;
 
-	mutex_lock(&sev_cmd_mutex);
-	ret = __sev_do_snp_platform_status(&status, &error);
-	mutex_unlock(&sev_cmd_mutex);
+	ret = sev_do_snp_platform_status(&status, &error);
 	if (ret < 0)
 		return ret;
 
-- 
2.43.0


  parent reply	other threads:[~2026-10-14 13:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 13:20 [PATCH 0/4] crypto: ccp - Make sev_cmd_mutex more granular Michael Roth
2026-10-14 13:20 ` [PATCH 1/4] crypto: ccp - Shut down SEV firmware before copying to user Michael Roth
2026-10-14 13:37   ` Tom Lendacky
2026-10-14 13:20 ` [PATCH 2/4] crypto: ccp - Check SNP initialization state after copying user inputs Michael Roth
2026-10-14 13:45   ` Tom Lendacky
2026-10-14 13:20 ` [PATCH 3/4] crypto: ccp - Make usage of sev_cmd_mutex more granular Michael Roth
2026-10-14 13:58   ` Tom Lendacky
2026-10-14 13:20 ` Michael Roth [this message]
2026-10-14 14:04   ` [PATCH 4/4] crypto: ccp - Allocate status page outside sev_cmd_mutex in SNP_PLATFORM_STATUS Tom Lendacky

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=20261014132145.1726322-5-michael.roth@amd.com \
    --to=michael.roth@amd.com \
    --cc=ackerleytng@google.com \
    --cc=aik@amd.com \
    --cc=ashish.kalra@amd.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=seanjc@google.com \
    --cc=thomas.lendacky@amd.com \
    /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