mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Melody Wang <huibo.wang@amd.com>
To: <x86@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Tom Lendacky <thomas.lendacky@amd.com>, <ardb@kernel.org>,
	Melody Wang <huibo.wang@amd.com>
Subject: [PATCH v7 03/10] x86/sev: Implement backup CA
Date: Sun, 6 Dec 2026 05:13:05 +0000	[thread overview]
Message-ID: <66925daa93c05c5f2e23828f0af2c70aac5fbf4c.1796524449.git.huibo.wang@amd.com> (raw)
In-Reply-To: <cover.1796524449.git.huibo.wang@amd.com>

Give each CPU a backup CA so an interrupting call can save and restore
the in-use CA around its own call. If both CAs are already in use there
is no safe way to proceed, so panic, as the GHCB code does.

No functional change for guests running without an SVSM.

Signed-off-by: Melody Wang <huibo.wang@amd.com>
---
 arch/x86/coco/sev/core.c     |  4 +++-
 arch/x86/coco/sev/internal.h | 10 ++++++++++
 arch/x86/coco/sev/noinstr.c  | 19 +++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index d4cd7a0d469f..91a3ff5b1f27 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -1237,8 +1237,10 @@ static void __init init_ghcb_and_ca(int cpu)
 	data->ghcb_active = false;
 	data->backup_ghcb_active = false;
 
-	if (snp_vmpl)
+	if (snp_vmpl) {
 		data->ca_runtime_data->ca_active = false;
+		data->ca_runtime_data->backup_ca_active = false;
+	}
 }
 
 void __init sev_es_init_vc_handling(void)
diff --git a/arch/x86/coco/sev/internal.h b/arch/x86/coco/sev/internal.h
index 540b1a2b1946..8f5dd898b244 100644
--- a/arch/x86/coco/sev/internal.h
+++ b/arch/x86/coco/sev/internal.h
@@ -21,6 +21,15 @@ struct svsm_ca_runtime_data {
 	struct svsm_ca *ca_page;
 	u64 caa_pa;
 
+	/*
+	 * Reserve one page per CPU as backup storage for CA.
+	 * It is needed when an NMI happens while the SVSM call uses the real
+	 * CA, and the NMI handler itself is causing another SVSM call. In
+	 * that case the CA content of the first SVSM call needs to be backed up
+	 * and restored.
+	 */
+	struct svsm_ca backup_ca;
+
 	/*
 	 * Mark the per-cpu CAs as in-use to detect a nested SVSM call.
 	 * There is no need for it to be atomic, because nothing is written to
@@ -35,6 +44,7 @@ struct svsm_ca_runtime_data {
 	 * already.
 	 */
 	bool ca_active;
+	bool backup_ca_active;
 };
 
 /* #VC handler runtime per-CPU data */
diff --git a/arch/x86/coco/sev/noinstr.c b/arch/x86/coco/sev/noinstr.c
index fb178ee1e60a..9e85f64f07ac 100644
--- a/arch/x86/coco/sev/noinstr.c
+++ b/arch/x86/coco/sev/noinstr.c
@@ -215,6 +215,24 @@ noinstr struct svsm_ca *__svsm_get_caa(struct caa_state *state)
 
 	if (unlikely(data->ca_active)) {
 		/* CA is already in use - save its contents */
+		if (unlikely(data->backup_ca_active)) {
+			/*
+			 * Backup-CA is also already in use. There is no way
+			 * to continue here so just kill the machine.
+			 */
+			data->ca_active        = false;
+			data->backup_ca_active = false;
+
+			instrumentation_begin();
+			panic("CA and backup CA already in use. Giving up");
+			instrumentation_end();
+		}
+
+		/* Mark backup_ca active before writing to it */
+		data->backup_ca_active = true;
+
+		state->caa = &data->backup_ca;
+
 		/* Backup CAA content */
 		*state->caa = *caa;
 	} else {
@@ -242,6 +260,7 @@ noinstr void __svsm_put_caa(struct caa_state *state)
 		/* Restore CAA from backup */
 		*caa = *state->caa;
 
+		data->backup_ca_active = false;
 		state->caa = NULL;
 	} else {
 		data->ca_active = false;
-- 
2.43.0


  parent reply	other threads:[~2026-12-06  5:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  5:13 [PATCH v7 00/10] Alternate Injection: Secure Interrupt Delivery for SEV-SNP Guests - Guest Support Melody Wang
2026-12-06  5:13 ` [PATCH v7 01/10] x86/sev: Add per-CPU CA runtime data and CA get/put helpers Melody Wang
2026-12-06  5:13 ` [PATCH v7 02/10] x86/sev: Switch the CA from per-CPU variables to the per-CPU runtime data Melody Wang
2026-12-06  5:13 ` Melody Wang [this message]
2026-12-06  5:13 ` [PATCH v7 04/10] x86/sev: Add a config option for Alternate Injection Melody Wang
2026-12-06  5:13 ` [PATCH v7 05/10] x86/apic: Add an SVSM APIC driver Melody Wang
2026-12-06  5:13 ` [PATCH v7 06/10] x86/sev: Route unsupported APIC register accesses to the hypervisor APIC emulation Melody Wang
2026-12-06  5:13 ` [PATCH v7 07/10] x86/sev: Add a function to contain all SEV-specific setup operations Melody Wang
2026-12-06  5:13 ` [PATCH v7 08/10] x86/sev: Register Alternate Injection protocol during OVMF boot Melody Wang
2026-12-06  5:13 ` [PATCH v7 09/10] x86/sev: Allow the guest to configure interrupt vectors for the hypervisor Melody Wang
2026-12-06  5:13 ` [PATCH v7 10/10] x86/sev: Indicate that Alternate Injection is supported in the guest Melody Wang

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=66925daa93c05c5f2e23828f0af2c70aac5fbf4c.1796524449.git.huibo.wang@amd.com \
    --to=huibo.wang@amd.com \
    --cc=ardb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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 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®