Linux KVM/arm64 development list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mark Brown" <broonie@kernel.org>
Cc: kvmarm@lists.linux.dev, "Marc Zyngier" <maz@kernel.org>,
	"Oliver Upton" <oupton@kernel.org>
Subject: Re: [PATCH v18 04/14] KVM: arm64: Ensure GCS memory effects are visible
Date: Mon, 03 Aug 2026 23:33:51 +0000	[thread overview]
Message-ID: <20260803233352.6C36B1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260803-arm64-gcs-v18-4-fd8c40f5ad88@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Coherency bug and asynchronous memory corruption risk in VHE mode due to delayed `gcsb_dsync()`.
- [High] Logical ordering and missing synchronization (ISB) violation when executing `gcsb_dsync()` in VHE exit path.
- [Medium] Incorrect memory barrier ordering around `gcsb_dsync()` on guest exit/load paths leaves normal memory accesses unsynchronized.
--

--- Patch [4]: [PATCH v18 04/14] KVM: arm64: Ensure GCS memory effects are visible ---
commit 1407ce4bfae0ac123f8195fde84320f01f5496bb
Author: Mark Brown <broonie@kernel.org>

KVM: arm64: Ensure GCS memory effects are visible

This commit inserts explicit GCSB DSYNC barriers during vCPU state
loading and saving to ensure GCS memory effects are visible. This is
required because the hypervisor cannot rely on implicit barriers in
vCPU context switches.

> diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
> --- a/arch/arm64/kvm/hyp/nvhe/switch.c
> +++ b/arch/arm64/kvm/hyp/nvhe/switch.c

[ ... ]

> @@ -345,6 +351,13 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
>  	 */
>  	dsb(nsh);
>  
> +	/*
> +	 * Ensure any GCS memory effects from the outgoing vCPU are
> +	 * visible elsewhere.
> +	 */
> +	if (ctxt_has_gcs(guest_ctxt))
> +		gcsb_dsync();

[Severity: Medium]
Does this sequence require a DSB after the GCSB DSYNC instruction?

To guarantee that GCS memory effects are globally visible before subsequent
operations like page table unmapping, a DSB is typically required after
the GCSB DSYNC. In __kvm_vcpu_run(), the DSB occurs before it.

> diff --git a/arch/arm64/kvm/hyp/vhe/sysreg-sr.c b/arch/arm64/kvm/hyp/vhe/sysreg-sr.c
> --- a/arch/arm64/kvm/hyp/vhe/sysreg-sr.c
> +++ b/arch/arm64/kvm/hyp/vhe/sysreg-sr.c
> @@ -239,6 +239,13 @@ void __vcpu_load_switch_sysregs(struct kvm_vcpu *vcpu)
>  	if (vcpu_has_nv(vcpu))
>  		dsb(nsh);
>  
> +	/*
> +	 * Ensure any GCS memory effects are visible to the incoming
> +	 * vCPU.
> +	 */
> +	if (ctxt_has_gcs(guest_ctxt))
> +		gcsb_dsync();

[Severity: Medium]
Is another DSB needed here as well?

Similar to the nVHE path, a DSB after the GCSB DSYNC is needed to ensure
memory effects are globally visible. Also, the preceding dsb(nsh) is
conditionally skipped if the vCPU does not have nested virtualization enabled,
which might leave this barrier in __vcpu_load_switch_sysregs() without
preceding protection.

[ ... ]

> @@ -299,5 +306,12 @@ void __vcpu_put_switch_sysregs(struct kvm_vcpu *vcpu)
>  	/* Restore host user state */
>  	__sysreg_restore_user_state(host_ctxt);
>  
> +	/*
> +	 * Ensure any GCS memory effects from the outgoing vCPU are
> +	 * visible elsewhere.
> +	 */
> +	if (ctxt_has_gcs(guest_ctxt))
> +		gcsb_dsync();

[Severity: High]
Could placing this barrier in the lazy vcpu_put() path cause stale memory
reads in the host kernel?

In VHE mode, __vcpu_put_switch_sysregs() executes lazily during vcpu_put().
The host kernel will begin processing guest exits (hypercalls, page faults)
while the vCPU is still loaded. This leaves the CPU's GCS cache with dirty,
unwritten guest data while the host might attempt normal memory reads on that
same memory. 

Does this need to be in the immediate exit path instead?

[Severity: High]
Does this execute the barrier under the wrong GCS context and an undefined
pipeline state?

This gcsb_dsync() happens after __sysreg_restore_user_state(host_ctxt).
Restoring the host user state overwrites SYS_GCSCRE0_EL1 without a subsequent
ISB, violating system register synchronization rules and leaving the pipeline
architecturally undefined when the branch and GCSB DSYNC execute.

Additionally, flushing after tearing down the guest's GCS configuration means
the flush occurs under the host's context. 

Should gcsb_dsync() execute before restoring the host state?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260803-arm64-gcs-v18-0-fd8c40f5ad88@kernel.org?part=4

  reply	other threads:[~2026-08-03 23:33 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 22:56 [PATCH v18 00/14] KVM: arm64: Provide guest support for GCS Mark Brown
2026-08-03 22:56 ` [PATCH v18 01/14] arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled Mark Brown
2026-08-03 22:56 ` [PATCH v18 02/14] KVM: arm64: Fix FGT mapping for HFGITR_EL2.nGCSEPP Mark Brown
2026-08-03 22:56 ` [PATCH v18 03/14] KVM: arm64: Manage GCS access and registers for guests Mark Brown
2026-08-03 23:31   ` sashiko-bot
2026-08-05  9:48   ` Yao Yuan
2026-08-05 12:32     ` Mark Brown
2026-08-06  6:51       ` Yao Yuan
2026-08-06 16:43         ` Mark Brown
2026-08-03 22:56 ` [PATCH v18 04/14] KVM: arm64: Ensure GCS memory effects are visible Mark Brown
2026-08-03 23:33   ` sashiko-bot [this message]
2026-08-06  8:25   ` Yao Yuan
2026-08-03 22:56 ` [PATCH v18 05/14] KVM: arm64: Set PSTATE.EXLOCK when entering an exception Mark Brown
2026-08-06 10:30   ` Yao Yuan
2026-08-03 22:56 ` [PATCH v18 06/14] KVM: arm64: Validate GCS exception lock when emulating ERET Mark Brown
2026-08-03 23:41   ` sashiko-bot
2026-08-03 22:56 ` [PATCH v18 07/14] KVM: arm64: Forward GCS exceptions to nested guests Mark Brown
2026-08-03 22:56 ` [PATCH v18 08/14] KVM: arm64: Enforce EXLOCK for SPSR and ELR Mark Brown
2026-08-03 22:56 ` [PATCH v18 09/14] KVM: arm64: Allow GCS to be enabled for guests Mark Brown
2026-08-03 22:56 ` [PATCH v18 10/14] KVM: selftests: arm64: Add GCS registers to get-reg-list Mark Brown
2026-08-03 22:56 ` [PATCH v18 11/14] KVM: selftests: arm64: Add GCS to set_id_regs Mark Brown
2026-08-03 22:56 ` [PATCH v18 12/14] KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change Mark Brown
2026-08-03 22:56 ` [PATCH v18 13/14] tools: Synchronise the kernel esr.h Mark Brown
2026-08-03 22:56 ` [PATCH v18 14/14] KVM: selftests: arm64: Add GCS EXLOCK exception emulation test Mark Brown

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=20260803233352.6C36B1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=broonie@kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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