Linux KVM/arm64 development list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Akihiko Odaki" <odaki@rsg.ci.i.u-tokyo.ac.jp>
Cc: "Marc Zyngier" <maz@kernel.org>,
	kvm@vger.kernel.org, "Oliver Upton" <oupton@kernel.org>,
	kvmarm@lists.linux.dev
Subject: Re: [PATCH v10 07/20] KVM: arm64: PMU: Recreate events after reset
Date: Mon, 14 Sep 2026 12:14:33 +0000	[thread overview]
Message-ID: <20260914121433.C4CE21F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260914-hybrid-v10-7-92788053a691@rsg.ci.i.u-tokyo.ac.jp>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Deferred PMU event recreation corrupts PMU counter reset values by writing back stale perf_event counts into freshly reset system registers.
--

commit a8710b2a9555c20c3e81b98a0509dda9d3d93f2e
Author: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>

KVM: arm64: PMU: Recreate events after reset

This commit defers the destruction and recreation of PMU events until the
vCPU thread runs, setting a flag during system register reset to stop and
reprogram the backing perf events.

> diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
> index a5ccb6ae44d5f..8530cc907b56c 100644
> --- a/arch/arm64/kvm/pmu-emul.c
> +++ b/arch/arm64/kvm/pmu-emul.c
[ ... ]
> @@ -900,7 +906,16 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
>  
>  void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
>  {
> +	struct kvm_pmu *pmu = &vcpu->arch.pmu;
>  	u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
> +	int i;
> +
> +	if (pmu->events_need_recreate) {
> +		for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
> +			kvm_pmu_stop_counter(kvm_vcpu_idx_to_pmc(vcpu, i));

[Severity: High]
Does this corrupt the architectural reset state of the PMU counters?

If a guest VM triggers a warm reset, kvm_reset_sys_regs() resets the PMU
counters to their architectural reset state and then calls
kvm_pmu_request_recreate().

On the next vCPU entry, kvm_vcpu_reload_pmu() iterates over all PMCs and calls
kvm_pmu_stop_counter(). However, kvm_pmu_stop_counter() is designed for dynamic
disablement. It preserves the counter by reading the current sysreg value,
adding the accumulated perf_event count, and writing the sum back to the
sysreg:

arch/arm64/kvm/pmu-emul.c:kvm_pmu_stop_counter() {
    ...
    val = kvm_pmu_get_pmc_value(pmc);
    reg = counter_index_to_reg(pmc->idx);
    __vcpu_assign_sys_reg(vcpu, reg, val);
    ...
}

Will this sequence overwrite the freshly reset counter value with the sum of
the reset value and the stale perf_event count from the previous run?

> +
> +		pmu->events_need_recreate = false;
> +	}
>  
>  	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, mask);
>  	__vcpu_rmw_sys_reg(vcpu, PMINTENSET_EL1, &=, mask);
> diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> index 42b6705bc21d1..1a57e07cec9a4 100644
> --- a/arch/arm64/kvm/sys_regs.c
> +++ b/arch/arm64/kvm/sys_regs.c
> @@ -5390,7 +5390,7 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
>  	set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags);
>  
>  	if (kvm_vcpu_has_pmu(vcpu))
> -		kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
> +		kvm_pmu_request_recreate(vcpu);
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260914-hybrid-v10-0-92788053a691@rsg.ci.i.u-tokyo.ac.jp?part=7

  reply	other threads:[~2026-09-14 12:14 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14 11:40 [PATCH v10 00/20] KVM: arm64: PMU: Use multiple host PMUs Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 01/20] KVM: arm64: Serialize repeated vCPU initialization Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 02/20] KVM: arm64: PMU: Stop updating MDCR_EL2.HPMN Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 03/20] KVM: arm64: PMU: Freeze counter count after first run Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 04/20] KVM: arm64: selftests: Test SET_NR_COUNTERS " Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 05/20] KVM: arm64: PMU: Keep implemented counter mask EL-independent Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 06/20] KVM: arm64: PMU: Preserve EL2 bitmap state during migration Akihiko Odaki
2026-09-14 12:24   ` sashiko-bot
2026-09-14 11:40 ` [PATCH v10 07/20] KVM: arm64: PMU: Recreate events after reset Akihiko Odaki
2026-09-14 12:14   ` sashiko-bot [this message]
2026-09-14 11:40 ` [PATCH v10 08/20] KVM: arm64: PMU: Recreate events after MDCR_EL2 changes Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 09/20] KVM: arm64: PMU: Recreate events after PMCR_EL0 changes Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 10/20] KVM: arm64: PMU: Recreate events after userspace event writes Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 11/20] tools headers: Use u* types for bitfield helpers Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 12/20] KVM: arm64: selftests: Cover PMU state in MDCR_EL2 Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 13/20] arm64: errata: Require Apple IMPDEF PMUv3 traps on all CPUs Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 14/20] KVM: arm64: Don't clear vcpu->cpu in kvm_arch_vcpu_put() Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 15/20] KVM: arm64: PMU: Protect the list of PMUs with RCU Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 16/20] KVM: arm64: PMU: Pass the pPMU to kvm_map_pmu_event() Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 17/20] KVM: arm64: PMU: Pass the target CPU to kvm_pmu_probe_armpmu() Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 18/20] KVM: arm64: PMU: Implement fixed-counters-only emulation Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 19/20] KVM: arm64: PMU: Introduce FIXED_COUNTERS_ONLY Akihiko Odaki
2026-09-14 11:40 ` [PATCH v10 20/20] KVM: arm64: selftests: Test PMU_V3_FIXED_COUNTERS_ONLY Akihiko Odaki

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=20260914121433.C4CE21F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
    --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