[PATCH v22 04/16] KVM: arm64: Ensure GCS memory effects are visible

From: Mark Brown

Date: Mon Nov 30 2026 - 11:43:26 EST


In order to allow efficient implementation of GCS implementations are
permitted to do GCS specific caching, with barriers implicit in stack
switch operations and GCSB DSYNC as an explicit barrier. Since we do
not use GCS in the hypervisor or host kernel the hypervisor cannot rely
on any barriers being present in vCPU context switches other than those
it explicitly inserts. Add explicit barriers when saving vCPU state,
unless we're doing it due to a nested EL transition in which case there
is no need since the guest will not change.

Reviewed-by: Yuan Yao <yaoyuan@xxxxxxxxxxxxxxxxx>
Reviewed-by: Leonardo Bras <leo.bras@xxxxxxx>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
Signed-off-by: Mark Brown <broonie@xxxxxxxxxx>
---
arch/arm64/kvm/arm.c | 18 ++++++++++++++++++
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 +++++++
2 files changed, 25 insertions(+)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index a148e80d0aef..2064cff8e92a 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -826,6 +826,16 @@ static void vhe_vcpu_put(struct kvm_vcpu *vcpu)
if (vcpu_has_nv(vcpu))
kvm_vcpu_put_hw_mmu(vcpu);

+ /*
+ * Ensure any GCS memory effects from the outgoing vCPU are
+ * visible elsewhere, nested guests transitioning ELs do not
+ * need this.
+ */
+ if (kvm_has_gcs(vcpu->kvm) &&
+ !vcpu_get_flag(vcpu, IN_NESTED_ERET) &&
+ !vcpu_get_flag(vcpu, IN_NESTED_EXCEPTION))
+ gcsb_dsync();
+
vcpu_put_mmu(vcpu);
}

@@ -836,6 +846,14 @@ static void nvhe_vcpu_put(struct kvm_vcpu *vcpu)
kvm_timer_vcpu_put(vcpu);
kvm_vgic_put(vcpu);
kvm_vcpu_pmu_restore_host(vcpu);
+
+ /*
+ * Ensure any GCS memory effects from the outgoing vCPU are
+ * visible elsewhere.
+ */
+ if (kvm_has_gcs(vcpu->kvm))
+ gcsb_dsync();
+
vcpu_put_mmu(vcpu);
}

diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 0c023ee0cf42..18030dd0dc0a 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -871,6 +871,13 @@ DEFINE_KVM_HOST_HCALL0(void, __pkvm_vcpu_put)
sync_hyp_vcpu_state(hyp_vcpu);
}

+ /*
+ * Ensure any GCS memory effects from the outgoing vCPU are
+ * visible elsewhere even if the host skips syncing.
+ */
+ if (kvm_has_gcs(hyp_vcpu->vcpu.kvm))
+ gcsb_dsync();
+
pkvm_put_hyp_vcpu(hyp_vcpu);
}
}

--
2.47.3