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 02/10] x86/sev: Switch the CA from per-CPU variables to the per-CPU runtime data
Date: Sun, 6 Dec 2026 05:13:04 +0000	[thread overview]
Message-ID: <c6243672f0ce0733861d668ca1abf9ac328f7e26.1796524449.git.huibo.wang@amd.com> (raw)
In-Reply-To: <cover.1796524449.git.huibo.wang@amd.com>

Nested-call detection needs the SVSM Calling Area's in-use state to be
reachable from the same noinstr code that fetches the CA. Today the CA
is tracked in standalone per-CPU variables (svsm_caa, svsm_caa_pa),
separate from the rest of the per-CPU #VC state in struct
sev_es_runtime_data.

Move the CA state into the runtime data so it lives in one place with
the other #VC state, allowing the in-use flag to be accessed safely from
noinstr context alongside the CA pointer.

No functional change intended.

Signed-off-by: Melody Wang <huibo.wang@amd.com>
---
 arch/x86/coco/sev/core.c      | 46 ++++++++++++++++++++++++-----------
 arch/x86/coco/sev/internal.h  | 25 +++++++++----------
 arch/x86/coco/sev/svsm.c      | 17 ++++++++-----
 arch/x86/coco/sev/vc-handle.c |  4 +--
 4 files changed, 57 insertions(+), 35 deletions(-)

diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index 6a13af13ca59..d4cd7a0d469f 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -386,14 +386,18 @@ static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long
 
 static void set_pages_state(unsigned long vaddr, unsigned long npages, int op)
 {
+	struct caa_state ca_state;
 	struct snp_psc_desc desc;
 	unsigned long vaddr_end;
 
 	/* Use the MSR protocol when a GHCB is not available. */
 	if (!boot_ghcb) {
-		struct psc_desc d = { op, svsm_get_caa(), svsm_get_caa_pa() };
+		struct psc_desc d = { op, __svsm_get_caa(&ca_state), svsm_get_caa_pa() };
 
-		return early_set_pages_state(vaddr, __pa(vaddr), npages, &d);
+		early_set_pages_state(vaddr, __pa(vaddr), npages, &d);
+
+		__svsm_put_caa(&ca_state);
+		return;
 	}
 
 	vaddr = vaddr & PAGE_MASK;
@@ -472,8 +476,9 @@ static int vmgexit_ap_control(u64 event, struct sev_es_save_area *vmsa, u32 apic
 	return ret;
 }
 
-static int snp_set_vmsa(void *va, void *caa, int apic_id, bool make_vmsa)
+static int snp_set_vmsa(void *va, u64 caa_pa, int apic_id, bool make_vmsa)
 {
+	struct caa_state ca_state;
 	int ret;
 
 	if (snp_vmpl) {
@@ -482,13 +487,13 @@ static int snp_set_vmsa(void *va, void *caa, int apic_id, bool make_vmsa)
 
 		local_irq_save(flags);
 
-		call.caa = this_cpu_read(svsm_caa);
+		call.caa = __svsm_get_caa(&ca_state);
 		call.rcx = __pa(va);
 
 		if (make_vmsa) {
 			/* Protocol 0, Call ID 2 */
 			call.rax = SVSM_CORE_CALL(SVSM_CORE_CREATE_VCPU);
-			call.rdx = __pa(caa);
+			call.rdx = caa_pa;
 			call.r8  = apic_id;
 		} else {
 			/* Protocol 0, Call ID 3 */
@@ -497,6 +502,7 @@ static int snp_set_vmsa(void *va, void *caa, int apic_id, bool make_vmsa)
 
 		ret = svsm_perform_call_protocol(&call);
 
+		__svsm_put_caa(&ca_state);
 		local_irq_restore(flags);
 	} else {
 		/*
@@ -521,7 +527,7 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa, int apic_id)
 {
 	int err;
 
-	err = snp_set_vmsa(vmsa, NULL, apic_id, false);
+	err = snp_set_vmsa(vmsa, 0, apic_id, false);
 	if (err)
 		pr_err("clear VMSA page failed (%u), leaking page\n", err);
 	else
@@ -757,8 +763,8 @@ static void *snp_alloc_vmsa_page(int cpu)
 static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip, unsigned int cpu)
 {
 	struct sev_es_save_area *cur_vmsa, *vmsa;
-	struct svsm_ca *caa;
 	u8 sipi_vector;
+	u64 caa_pa;
 	int ret;
 	u64 cr4;
 
@@ -794,7 +800,7 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip, unsigned
 		return -ENOMEM;
 
 	/* If an SVSM is present, the SVSM per-CPU CAA will be !NULL */
-	caa = per_cpu(svsm_caa, cpu);
+	caa_pa = svsm_get_cpu_caa_pa(cpu);
 
 	/* CR4 should maintain the MCE value */
 	cr4 = native_read_cr4() & X86_CR4_MCE;
@@ -856,7 +862,7 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip, unsigned
 	}
 
 	/* Switch the page over to a VMSA page now that it is initialized */
-	ret = snp_set_vmsa(vmsa, caa, apic_id, true);
+	ret = snp_set_vmsa(vmsa, caa_pa, apic_id, true);
 	if (ret) {
 		pr_err("set VMSA page failed (%u)\n", ret);
 		free_page((unsigned long)vmsa);
@@ -959,7 +965,7 @@ int __init sev_es_efi_map_ghcbs_cas(pgd_t *pgd)
 			return 1;
 
 		if (snp_vmpl) {
-			address = per_cpu(svsm_caa_pa, cpu);
+			address = svsm_get_cpu_caa_pa(cpu);
 			if (!address)
 				return 1;
 
@@ -1194,18 +1200,27 @@ static void __init alloc_runtime_data(int cpu)
 	per_cpu(runtime_data, cpu) = data;
 
 	if (snp_vmpl) {
+		struct svsm_ca_runtime_data *svsm_data;
 		struct svsm_ca *caa;
 
 		/* Allocate the SVSM CA page if an SVSM is present */
 		caa = cpu ? memblock_alloc_or_panic(sizeof(*caa), PAGE_SIZE)
 			  : &boot_svsm_ca_page;
 
-		per_cpu(svsm_caa, cpu) = caa;
-		per_cpu(svsm_caa_pa, cpu) = __pa(caa);
+
+		svsm_data = memblock_alloc_node(sizeof(*svsm_data), PAGE_SIZE, cpu_to_node(cpu));
+
+		if (!svsm_data)
+			panic("Can't allocate SVSM CA runtime data");
+
+		svsm_data->ca_page = caa;
+		svsm_data->caa_pa = __pa(caa);
+
+		data->ca_runtime_data = svsm_data;
 	}
 }
 
-static void __init init_ghcb(int cpu)
+static void __init init_ghcb_and_ca(int cpu)
 {
 	struct sev_es_runtime_data *data;
 	int err;
@@ -1221,6 +1236,9 @@ static void __init init_ghcb(int cpu)
 
 	data->ghcb_active = false;
 	data->backup_ghcb_active = false;
+
+	if (snp_vmpl)
+		data->ca_runtime_data->ca_active = false;
 }
 
 void __init sev_es_init_vc_handling(void)
@@ -1249,7 +1267,7 @@ void __init sev_es_init_vc_handling(void)
 	/* Initialize per-cpu GHCB pages */
 	for_each_possible_cpu(cpu) {
 		alloc_runtime_data(cpu);
-		init_ghcb(cpu);
+		init_ghcb_and_ca(cpu);
 	}
 
 	if (snp_vmpl)
diff --git a/arch/x86/coco/sev/internal.h b/arch/x86/coco/sev/internal.h
index 96e89977fc2a..540b1a2b1946 100644
--- a/arch/x86/coco/sev/internal.h
+++ b/arch/x86/coco/sev/internal.h
@@ -95,9 +95,6 @@ DECLARE_PER_CPU(struct sev_es_save_area *, sev_vmsa);
 void early_set_pages_state(unsigned long vaddr, unsigned long paddr,
 			   unsigned long npages, const struct psc_desc *desc);
 
-DECLARE_PER_CPU(struct svsm_ca *, svsm_caa);
-DECLARE_PER_CPU(u64, svsm_caa_pa);
-
 extern u64 boot_svsm_caa_pa;
 
 enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt *ctxt);
@@ -125,20 +122,22 @@ u64 get_hv_features(void);
 
 const struct snp_cpuid_table *snp_cpuid_get_table(void);
 
-static inline struct svsm_ca *svsm_get_caa(void)
+static inline u64 svsm_get_caa_pa(void)
 {
-	if (sev_cfg.use_cas)
-		return this_cpu_read(svsm_caa);
-	else
-		return rip_rel_ptr(&boot_svsm_ca_page);
+	if (!sev_cfg.use_cas)
+		return boot_svsm_caa_pa;
+
+	return this_cpu_read(runtime_data)->ca_runtime_data->caa_pa;
 }
 
-static inline u64 svsm_get_caa_pa(void)
+static inline u64 svsm_get_cpu_caa_pa(int cpu)
 {
-	if (sev_cfg.use_cas)
-		return this_cpu_read(svsm_caa_pa);
-	else
-		return boot_svsm_caa_pa;
+	struct sev_es_runtime_data *data = per_cpu(runtime_data, cpu);
+
+	if (!data || !data->ca_runtime_data)
+		return 0;
+
+	return data->ca_runtime_data->caa_pa;
 }
 
 struct svsm_ca *__svsm_get_caa(struct caa_state *state);
diff --git a/arch/x86/coco/sev/svsm.c b/arch/x86/coco/sev/svsm.c
index 2d493d55ff2e..cd38c6a03a53 100644
--- a/arch/x86/coco/sev/svsm.c
+++ b/arch/x86/coco/sev/svsm.c
@@ -24,9 +24,6 @@ SYM_PIC_ALIAS(boot_svsm_ca_page);
 u64 boot_svsm_caa_pa __ro_after_init;
 SYM_PIC_ALIAS(boot_svsm_caa_pa);
 
-DEFINE_PER_CPU(struct svsm_ca *, svsm_caa);
-DEFINE_PER_CPU(u64, svsm_caa_pa);
-
 static int svsm_perform_ghcb_protocol(struct ghcb *ghcb, struct svsm_call *call)
 {
 	struct es_em_ctxt ctxt;
@@ -67,6 +64,7 @@ static int svsm_perform_ghcb_protocol(struct ghcb *ghcb, struct svsm_call *call)
 
 int svsm_perform_call_protocol(struct svsm_call *call)
 {
+	struct caa_state ca_state;
 	struct ghcb_state state;
 	unsigned long flags;
 	struct ghcb *ghcb;
@@ -80,7 +78,7 @@ int svsm_perform_call_protocol(struct svsm_call *call)
 	 * is fetched, always fetch 'caa' and then issue the SVSM call with
 	 * interrupts disabled. This ensures the correct 'caa' is used.
 	 */
-	call->caa = svsm_get_caa();
+	call->caa = __svsm_get_caa(&ca_state);
 
 	ghcb = __sev_get_ghcb(&state);
 
@@ -90,6 +88,7 @@ int svsm_perform_call_protocol(struct svsm_call *call)
 	} while (ret == -EAGAIN);
 
 	__sev_put_ghcb(&state);
+	__svsm_put_caa(&ca_state);
 
 	native_local_irq_restore(flags);
 
@@ -176,6 +175,7 @@ void svsm_pval_pages(struct snp_psc_desc *desc)
 	unsigned int i, pv_4k_count = 0;
 	struct svsm_pvalidate_call *pc;
 	struct svsm_call call = {};
+	struct caa_state ca_state;
 	unsigned long flags;
 	bool action;
 	u64 pc_pa;
@@ -194,11 +194,13 @@ void svsm_pval_pages(struct snp_psc_desc *desc)
 	 * the SVSM is invoked. This repeats until all entries in the descriptor
 	 * are processed.
 	 */
-	call.caa = svsm_get_caa();
+	call.caa = __svsm_get_caa(&ca_state);
 
 	pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer;
 	pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
 
+	__svsm_put_caa(&ca_state);
+
 	/* Protocol 0, Call ID 1 */
 	call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE);
 	call.rcx = pc_pa;
@@ -272,6 +274,7 @@ int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call,
 			      struct svsm_attest_call *input)
 {
 	struct svsm_attest_call *ac;
+	struct caa_state ca_state;
 	unsigned long flags;
 	u64 attest_call_pa;
 	int ret;
@@ -281,13 +284,15 @@ int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call,
 
 	local_irq_save(flags);
 
-	call->caa = svsm_get_caa();
+	call->caa = __svsm_get_caa(&ca_state);
 
 	ac = (struct svsm_attest_call *)call->caa->svsm_buffer;
 	attest_call_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
 
 	*ac = *input;
 
+	__svsm_put_caa(&ca_state);
+
 	/*
 	 * Set input registers for the request and set RDX and R8 to known
 	 * values in order to detect length values being returned in them.
diff --git a/arch/x86/coco/sev/vc-handle.c b/arch/x86/coco/sev/vc-handle.c
index a233ea5c867e..080b7760aa06 100644
--- a/arch/x86/coco/sev/vc-handle.c
+++ b/arch/x86/coco/sev/vc-handle.c
@@ -345,8 +345,8 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
 	if (write)
 		return ES_OK;
 
-	regs->ax = lower_32_bits(this_cpu_read(svsm_caa_pa));
-	regs->dx = upper_32_bits(this_cpu_read(svsm_caa_pa));
+	regs->ax = lower_32_bits(svsm_get_caa_pa());
+	regs->dx = upper_32_bits(svsm_get_caa_pa());
 
 	return ES_OK;
 }
-- 
2.43.0


  parent reply	other threads:[~2026-12-06  5:13 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 ` Melody Wang [this message]
2026-12-06  5:13 ` [PATCH v7 03/10] x86/sev: Implement backup CA Melody Wang
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=c6243672f0ce0733861d668ca1abf9ac328f7e26.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®