[RFT PATCH v2 08/23] x86/sev: Fall back to early page state change code only during boot

From: Ard Biesheuvel
Date: Sun May 04 2025 - 05:54:37 EST


From: Ard Biesheuvel <ardb@xxxxxxxxxx>

The core SEV-SNP page state change code will fall back to the early page
state change code if no boot_ghcb has been assigned yet. This is
unlikely to occur in practice, and it prevents the early code from being
emitted as __init, making it harder to reason about whether or not
startup code may be called at runtime.

So ensure that early_page_state_change() is only called before freeing
initmem, and move the call into a separate function annotated as __ref,
so that the callee can be annotated as __init in a subsequent patch.

Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx>
---
arch/x86/coco/sev/core.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index ac400525de73..fd5cd7d9b5f2 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -501,14 +501,24 @@ static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long
return vaddr;
}

+static bool __ref __early_set_pages_state(unsigned long vaddr,
+ unsigned long npages, int op)
+{
+ /* Use the MSR protocol when a GHCB is not yet available. */
+ if (boot_ghcb || WARN_ON(system_state >= SYSTEM_FREEING_INITMEM))
+ return false;
+
+ early_set_pages_state(vaddr, __pa(vaddr), npages, op);
+ return true;
+}
+
static void set_pages_state(unsigned long vaddr, unsigned long npages, int op)
{
struct snp_psc_desc desc;
unsigned long vaddr_end;

- /* Use the MSR protocol when a GHCB is not available. */
- if (!boot_ghcb)
- return early_set_pages_state(vaddr, __pa(vaddr), npages, op);
+ if (__early_set_pages_state(vaddr, npages, op))
+ return;

vaddr = vaddr & PAGE_MASK;
vaddr_end = vaddr + (npages << PAGE_SHIFT);
--
2.49.0.906.g1f30a19c02-goog