x86/sev: Remove redundant ghcbs_initialized checks around __sev_{get,put}_ghcb()
PATCH 1/1 x86/sev: Remove redundant ghcbs_initialized checks around __sev_{get,put}_ghcb()
After commit 3645eb7e3915 ("x86/fred: Fix early boot failures on SEV-ES/SNP guests"), __sev_{get,put}_ghcb() handle the early-boot GHCB fallback internally, making the ghcbs_initialized guards in __set_pages_state() and svsm_perform_call_protocol() redundant.
Remove these redundant conditionals and call __sev_{get,put}_ghcb() unconditionally.
Also initialize state->ghcb to NULL in the early-boot path of __sev_get_ghcb() so that the ghcb_state is well-defined for all callers, even though __sev_put_ghcb() currently returns early before reading it.
No functional change intended.
--- arch/x86/coco/sev/core.c | 8 ++------ arch/x86/coco/sev/noinstr.c | 4 +++- arch/x86/coco/sev/svsm.c | 10 ++-------- 3 files changed, 7 insertions(+), 15 deletions(-)
arch/x86/coco/sev/core.c +2 -6
@@ -367,17 +367,13 @@ static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned longlocal_irq_save (flags); -if (sev_cfg.ghcbs_initialized) - ghcb =__sev_get_ghcb (&state); -else - ghcb = boot_ghcb; + ghcb =__sev_get_ghcb (&state);/* Invoke the hypervisor to perform the page state changes */ if (!ghcb ||vmgexit_psc (ghcb, data))sev_es_terminate (SEV_TERM_SET_LINUX ,GHCB_TERM_PSC ); -if (sev_cfg.ghcbs_initialized) -__sev_put_ghcb (&state); +__sev_put_ghcb (&state);local_irq_restore (flags);
arch/x86/coco/sev/noinstr.c +3 -1
@@ -121,8 +121,10 @@ noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)WARN_ON (!irqs_disabled ()); -if (!sev_cfg.ghcbs_initialized) +if (!sev_cfg.ghcbs_initialized) { + state->ghcb =NULL ;return boot_ghcb; + } data =this_cpu_read (runtime_data); ghcb = &data->ghcb_page;
arch/x86/coco/sev/svsm.c +2 -8
@@ -74,20 +74,14 @@ int svsm_perform_call_protocol(struct svsm_call *call) flags =native_local_irq_save (); -if (sev_cfg.ghcbs_initialized) - ghcb =__sev_get_ghcb (&state); -else if (boot_ghcb) - ghcb = boot_ghcb; -else - ghcb =NULL ; + ghcb =__sev_get_ghcb (&state);do { ret = ghcb ?svsm_perform_ghcb_protocol (ghcb, call) :__pi_svsm_perform_msr_protocol (call); }while (ret == -EAGAIN ); -if (sev_cfg.ghcbs_initialized) -__sev_put_ghcb (&state); +__sev_put_ghcb (&state);native_local_irq_restore (flags);
Signature · 1 line
2.48.1
Quoted 8 lines · Nikunj A Dadhania
After commit
3645eb7e3915("x86/fred: Fix early boot failures on SEV-ES/SNP guests"), __sev_{get,put}_ghcb() handle the early-boot GHCB fallback internally, making theghcbs_initializedguards in__set_pages_state()andsvsm_perform_call_protocol()redundant.Remove these redundant conditionals and call __sev_{get,put}_ghcb() unconditionally.
Quoted 8 lines
Also initialize
state->ghcbto NULL in the early-boot path of__sev_get_ghcb()so that theghcb_stateis well-defined for all callers, even though__sev_put_ghcb()currently returns early before reading it.No functional change intended.
Suggested-by Tom Lendacky <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>
Quoted 73 lines
--- arch/x86/coco/sev/core.c | 8 ++------ arch/x86/coco/sev/noinstr.c | 4 +++- arch/x86/coco/sev/svsm.c | 10 ++-------- 3 files changed, 7 insertions(+), 15 deletions(-)arch/x86/coco/sev/core.c +2
-6@@ -367,17 +367,13 @@ static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned longlocal_irq_save (flags); -if (sev_cfg.ghcbs_initialized) - ghcb =__sev_get_ghcb (&state); -else - ghcb = boot_ghcb; + ghcb =__sev_get_ghcb (&state);/* Invoke the hypervisor to perform the page state changes */ if (!ghcb ||vmgexit_psc (ghcb, data))sev_es_terminate (SEV_TERM_SET_LINUX ,GHCB_TERM_PSC ); -if (sev_cfg.ghcbs_initialized) -__sev_put_ghcb (&state); +__sev_put_ghcb (&state);local_irq_restore (flags);arch/x86/coco/sev/noinstr.c +3
-1@@ -121,8 +121,10 @@ noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)WARN_ON (!irqs_disabled ()); -if (!sev_cfg.ghcbs_initialized) +if (!sev_cfg.ghcbs_initialized) { + state->ghcb =NULL ;return boot_ghcb; + } data =this_cpu_read (runtime_data); ghcb = &data->ghcb_page;arch/x86/coco/sev/svsm.c +2
-8@@ -74,20 +74,14 @@ int svsm_perform_call_protocol(struct svsm_call *call) flags =native_local_irq_save (); -if (sev_cfg.ghcbs_initialized) - ghcb =__sev_get_ghcb (&state); -else if (boot_ghcb) - ghcb = boot_ghcb; -else - ghcb =NULL ; + ghcb =__sev_get_ghcb (&state);do { ret = ghcb ?svsm_perform_ghcb_protocol (ghcb, call) :__pi_svsm_perform_msr_protocol (call); }while (ret == -EAGAIN ); -if (sev_cfg.ghcbs_initialized) -__sev_put_ghcb (&state); +__sev_put_ghcb (&state);native_local_irq_restore (flags);
The following commit has been merged into the x86/sev branch of tip:
The following commit has been merged into the x86/sev branch of tip:
Commit-ID: 9d8460a1c7a6b0f2dc6302e5d0f31d4e8c2a7913 Gitweb: https://git.kernel.org/tip/9d8460a1c7a6b0f2dc6302e5d0f31d4e8c2a7913 Author: Nikunj A Dadhania <[email protected]> AuthorDate: Mon, 18 May 2026 10:22:30 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Tue, 19 May 2026 13:31:29 -07:00
x86/sev: Remove redundant ghcbs_initialized checks around __sev_{get,put}_ghcb()
After
3645eb7e3915 ("x86/fred: Fix early boot failures on SEV-ES/SNP guests"),
__sev_{get,put}_ghcb() handle the early-boot GHCB fallback internally, making the ghcbs_initialized guards in __set_pages_state() and svsm_perform_call_protocol() redundant.
Remove them.
Also initialize state->ghcb to NULL in the early-boot path of __sev_get_ghcb() so that the ghcb_state is well-defined for all callers, even though __sev_put_ghcb() currently returns early before reading it.
No functional change intended.
--- arch/x86/coco/sev/core.c | 8 ++------ arch/x86/coco/sev/noinstr.c | 4 +++- arch/x86/coco/sev/svsm.c | 10 ++-------- 3 files changed, 7 insertions(+), 15 deletions(-)
arch/x86/coco/sev/core.c +2 -6
@@ -367,17 +367,13 @@ static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned longlocal_irq_save (flags); -if (sev_cfg.ghcbs_initialized) - ghcb =__sev_get_ghcb (&state); -else - ghcb = boot_ghcb; + ghcb =__sev_get_ghcb (&state);/* Invoke the hypervisor to perform the page state changes */ if (!ghcb ||vmgexit_psc (ghcb, data))sev_es_terminate (SEV_TERM_SET_LINUX ,GHCB_TERM_PSC ); -if (sev_cfg.ghcbs_initialized) -__sev_put_ghcb (&state); +__sev_put_ghcb (&state);local_irq_restore (flags);
arch/x86/coco/sev/noinstr.c +3 -1
@@ -121,8 +121,10 @@ noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)WARN_ON (!irqs_disabled ()); -if (!sev_cfg.ghcbs_initialized) +if (!sev_cfg.ghcbs_initialized) { + state->ghcb =NULL ;return boot_ghcb; + } data =this_cpu_read (runtime_data); ghcb = &data->ghcb_page;
arch/x86/coco/sev/svsm.c +2 -8
@@ -74,20 +74,14 @@ int svsm_perform_call_protocol(struct svsm_call *call) flags =native_local_irq_save (); -if (sev_cfg.ghcbs_initialized) - ghcb =__sev_get_ghcb (&state); -else if (boot_ghcb) - ghcb = boot_ghcb; -else - ghcb =NULL ; + ghcb =__sev_get_ghcb (&state);do { ret = ghcb ?svsm_perform_ghcb_protocol (ghcb, call) :__pi_svsm_perform_msr_protocol (call); }while (ret == -EAGAIN ); -if (sev_cfg.ghcbs_initialized) -__sev_put_ghcb (&state); +__sev_put_ghcb (&state);native_local_irq_restore (flags);