Re: [PATCH v10 13/33] KVM: arm64: Make __vcpu_rmw_sys_reg() atomic
From: Oliver Upton
Date: Tue Dec 01 2026 - 20:08:16 EST
On Thu, Nov 19, 2026 at 06:51:55PM +0000, Colton Lewis wrote:
> Use try_cmpxchg_local() in __vcpu_rmw_sys_reg() so read-modify-write
> updates to vCPU system registers are atomic with respect to local
> interrupts and GICv3 pseudo-NMIs. This prevents lost updates when a PMU
> overflow handler (which may run as a pseudo-NMI that is not masked by
> local_irq_save()) updates PMOVSSET_EL0 concurrently with vCPU register
> accesses.
>
> Signed-off-by: Colton Lewis <coltonlewis@xxxxxxxxxx>
> ---
> arch/arm64/include/asm/kvm_host.h | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index 89683a690828e..103c25001c0d2 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -1313,12 +1313,14 @@ u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
> #define __vcpu_rmw_sys_reg(v, r, op, val) \
> do { \
> const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \
> - u64 __v = ctxt_sys_reg(ctxt, (r)); \
> - __v op (val); \
> - if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \
> - __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \
> - \
> - ctxt_sys_reg(ctxt, (r)) = __v; \
> + u64 __old = READ_ONCE(ctxt_sys_reg(ctxt, (r))); \
> + u64 __new; \
> + do { \
> + __new = __old; \
> + __new op (val); \
> + if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \
> + __new = kvm_vcpu_apply_reg_masks((v), (r), __new); \
> + } while (!try_cmpxchg_local(__ctxt_sys_reg(ctxt, (r)), &__old, __new)); \
Urgh, my strong preference is that we avoid upgrading all RMW patterns
to atomics. I'm guessing you only need this for the overflow flags?
If that's the case then we should just special-case the handling of the
overflow flags, stuffing them in an atomic that can be drained from the
vCPU outside of an IRQ.
You'd need to make sure those flags are architectural before re-entering
the guest or vcpu_put().
Thanks,
Oliver