All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: Alexey Kardashevskiy <aik@amd.com>, x86@kernel.org
Cc: linux-kernel@vger.kernel.org,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Nikunj A Dadhania <nikunj@amd.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Brijesh Singh <brijesh.singh@amd.com>,
	Ashish Kalra <ashish.kalra@amd.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Michael Roth <michael.roth@amd.com>,
	Kuppuswamy Sathyanarayanan
	<sathyanarayanan.kuppuswamy@linux.intel.com>,
	Liam Merwick <liam.merwick@oracle.com>
Subject: Re: [PATCH 2/4] x86/sev: Allocate request in TSC_INFO_REQ on stack
Date: Mon, 5 May 2025 09:03:37 -0700	[thread overview]
Message-ID: <13dc0d80-5d7f-40ce-be82-8d0f3eb24a1a@intel.com> (raw)
In-Reply-To: <20250505141238.4179623-3-aik@amd.com>

[-- Attachment #1: Type: text/plain, Size: 778 bytes --]

On 5/5/25 07:12, Alexey Kardashevskiy wrote:
> Allocate a 88 byte request structure on stack and skip needless
> kzalloc/kfree.

Could you maybe take a closer look at _all_ of these rather than poking
at them one at a time?

snp_guest_request_ioctl, for example, looks to be ~32 bytes. Why fix
'struct snp_guest_req' and leave an even worse offender?

Or, maybe just be done with it and convert them all over to __free().
Yeah, some of them don't need to be kmalloc(), but kmalloc()s are cheap
and consistency is nice, like in the attached patch.

It also wouldn't be awful to mix stack and kmalloc() allocations,
especially when the freeing semantics are the same for stack and
__free()-annotated allocations.

But it would be really nice to completely eliminate the goto mess.

[-- Attachment #2: snp_get_tsc_info-__free.patch --]
[-- Type: text/x-patch, Size: 2388 bytes --]

diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index b0c1a7a57497..618eaae190e2 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -3174,41 +3174,32 @@ EXPORT_SYMBOL_GPL(snp_send_guest_request);
 
 static int __init snp_get_tsc_info(void)
 {
-	struct snp_guest_request_ioctl *rio;
-	struct snp_tsc_info_resp *tsc_resp;
-	struct snp_tsc_info_req *tsc_req;
-	struct snp_msg_desc *mdesc;
-	struct snp_guest_req *req;
+	struct snp_guest_request_ioctl *rio __free(kfree) = NULL;
+	struct snp_tsc_info_resp *tsc_resp  __free(kfree_sensitive) = NULL;
+	struct snp_tsc_info_req *tsc_req    __free(kfree) = NULL;
+	struct snp_guest_req *req	    __free(kfree) = NULL;
+	struct snp_msg_desc *mdesc	    __free(snp_msg_free) = NULL;
 	int rc = -ENOMEM;
 
-	tsc_req = kzalloc(sizeof(*tsc_req), GFP_KERNEL);
-	if (!tsc_req)
-		return rc;
-
 	/*
 	 * The intermediate response buffer is used while decrypting the
 	 * response payload. Make sure that it has enough space to cover
 	 * the authtag.
 	 */
 	tsc_resp = kzalloc(sizeof(*tsc_resp) + AUTHTAG_LEN, GFP_KERNEL);
-	if (!tsc_resp)
-		goto e_free_tsc_req;
-
+	tsc_req = kzalloc(sizeof(*tsc_req), GFP_KERNEL);
 	req = kzalloc(sizeof(*req), GFP_KERNEL);
-	if (!req)
-		goto e_free_tsc_resp;
-
 	rio = kzalloc(sizeof(*rio), GFP_KERNEL);
-	if (!rio)
-		goto e_free_req;
+	if (!(tsc_resp && tsc_req && req && rio))
+		return rc;
 
 	mdesc = snp_msg_alloc();
 	if (IS_ERR_OR_NULL(mdesc))
-		goto e_free_rio;
+		return rc;
 
 	rc = snp_msg_init(mdesc, snp_vmpl);
 	if (rc)
-		goto e_free_mdesc;
+		return rc;
 
 	req->msg_version = MSG_HDR_VER;
 	req->msg_type = SNP_MSG_TSC_INFO_REQ;
@@ -3221,7 +3212,7 @@ static int __init snp_get_tsc_info(void)
 
 	rc = snp_send_guest_request(mdesc, req, rio);
 	if (rc)
-		goto e_request;
+		return rc;
 
 	pr_debug("%s: response status 0x%x scale 0x%llx offset 0x%llx factor 0x%x\n",
 		 __func__, tsc_resp->status, tsc_resp->tsc_scale, tsc_resp->tsc_offset,
@@ -3235,20 +3226,6 @@ static int __init snp_get_tsc_info(void)
 		rc = -EIO;
 	}
 
-e_request:
-	/* The response buffer contains sensitive data, explicitly clear it. */
-	memzero_explicit(tsc_resp, sizeof(*tsc_resp) + AUTHTAG_LEN);
-e_free_mdesc:
-	snp_msg_free(mdesc);
-e_free_rio:
-	kfree(rio);
-e_free_req:
-	kfree(req);
- e_free_tsc_resp:
-	kfree(tsc_resp);
-e_free_tsc_req:
-	kfree(tsc_req);
-
 	return rc;
 }
 

  parent reply	other threads:[~2025-05-05 16:03 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-05 14:12 [PATCH 0/4] x86/sev: Rework SNP Guest Request Alexey Kardashevskiy
2025-05-05 14:12 ` [PATCH 1/4] virt: sev-guest: Contain snp_guest_request_ioctl in sev-guest Alexey Kardashevskiy
2025-05-05 15:11   ` Dionna Amalie Glaze
2025-05-05 14:12 ` [PATCH 2/4] x86/sev: Allocate request in TSC_INFO_REQ on stack Alexey Kardashevskiy
2025-05-05 15:13   ` Dionna Amalie Glaze
2025-05-05 16:03   ` Dave Hansen [this message]
2025-05-06  2:05     ` Alexey Kardashevskiy
2025-05-05 14:12 ` [PATCH 3/4] x86/sev: Document requirement for linear mapping of Guest Request buffers Alexey Kardashevskiy
2025-05-05 15:18   ` Dionna Amalie Glaze
2025-05-05 14:12 ` [PATCH 4/4] x86/sev: Drop unnecessary parameter in snp_issue_guest_request Alexey Kardashevskiy
2025-05-05 15:19   ` Dionna Amalie Glaze
2025-05-06 18:55 ` [PATCH 0/4] x86/sev: Rework SNP Guest Request Tom Lendacky
2025-06-05  2:40   ` Alexey Kardashevskiy
2025-06-06 12:57     ` Borislav Petkov

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=13dc0d80-5d7f-40ce-be82-8d0f3eb24a1a@intel.com \
    --to=dave.hansen@intel.com \
    --cc=aik@amd.com \
    --cc=ardb@kernel.org \
    --cc=ashish.kalra@amd.com \
    --cc=bp@alien8.de \
    --cc=brijesh.singh@amd.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=liam.merwick@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=mingo@redhat.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=sathyanarayanan.kuppuswamy@linux.intel.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.