Re: [PATCH v9 10/16] KVM: arm64: Don't clear vcpu->cpu in kvm_arch_vcpu_put()

From: Fuad Tabba

Date: Wed Jul 29 2026 - 13:32:42 EST


On Mon, 20 Jul 2026 at 06:39, Akihiko Odaki
<odaki@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> From: Oliver Upton <oupton@xxxxxxxxxx>
>
> commit e9b152cb957c ("arm/arm64: kvm: Set vcpu->cpu to -1 on vcpu_put")
> reset vcpu->cpu in order for the VGIC to determine if there was any vCPU
> running at the time of access. The VGIC has gone through an entire
> rewrite since then, and with commit 7d450e282171 ("KVM: arm/arm64:
> vgic-new: Add userland access to VGIC dist registers") the user
> accessors just grab all vCPU mutexes instead.
>
> Drop this remaining vestige such that kvm_arch_vcpu_load() can properly
> detect a CPU migration. While at it, rework kvm_reset_vcpu() to do a
> much more pedantic check that the provided vCPU is actually what's
> running on the present CPU.
>
> Signed-off-by: Oliver Upton <oupton@xxxxxxxxxx>
> Signed-off-by: Akihiko Odaki <odaki@xxxxxxxxxxxxxxxxxxxxxx>

Reviewed-by: Fuad Tabba <fuad.tabba@xxxxxxxxx>

Cheers,
/fuad

> ---
> arch/arm64/kvm/arm.c | 1 -
> arch/arm64/kvm/reset.c | 16 +++++++++++-----
> 2 files changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index a06ea39f57b1..72ce79ce4d5e 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c
> @@ -749,7 +749,6 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
> kvm_arm_vmid_clear_active();
>
> vcpu_clear_on_unsupported_cpu(vcpu);
> - vcpu->cpu = -1;
> }
>
> static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
> diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c
> index b963fd975aac..6bfb21f00444 100644
> --- a/arch/arm64/kvm/reset.c
> +++ b/arch/arm64/kvm/reset.c
> @@ -190,7 +190,8 @@ static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
> void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
> {
> struct vcpu_reset_state reset_state;
> - bool loaded;
> + struct kvm_vcpu *running;
> + bool loaded = false;
> u32 pstate;
>
> spin_lock(&vcpu->arch.mp_state_lock);
> @@ -198,10 +199,16 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
> vcpu->arch.reset_state.reset = false;
> spin_unlock(&vcpu->arch.mp_state_lock);
>
> - preempt_disable();
> - loaded = (vcpu->cpu != -1);
> - if (loaded)
> + guard(preempt)();
> +
> + running = kvm_get_running_vcpu();
> + if (running) {
> + if (KVM_BUG_ON(running != vcpu, vcpu->kvm))
> + return;
> +
> + loaded = true;
> kvm_arch_vcpu_put(vcpu);
> + }
>
> if (!kvm_arm_vcpu_sve_finalized(vcpu)) {
> if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
> @@ -269,7 +276,6 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
>
> if (loaded)
> kvm_arch_vcpu_load(vcpu, smp_processor_id());
> - preempt_enable();
> }
>
> u32 kvm_get_pa_bits(struct kvm *kvm)
>
> --
> 2.55.0
>
>