All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Akihiko Odaki" <odaki@rsg.ci.i.u-tokyo.ac.jp>
Cc: kvm@vger.kernel.org, "Oliver Upton" <oupton@kernel.org>,
	kvmarm@lists.linux.dev, "Marc Zyngier" <maz@kernel.org>
Subject: Re: [PATCH v9 06/16] KVM: arm64: PMU: Recreate events after MDCR_EL2 changes
Date: Mon, 20 Jul 2026 05:57:05 +0000	[thread overview]
Message-ID: <20260720055705.A33B61F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260720-hybrid-v9-6-2b713ca1b5dc@rsg.ci.i.u-tokyo.ac.jp>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Medium] Modifying PMCR_EL0.LC or PMCR_EL0.LP does not recreate perf events, leading to stale sample periods for active counters.
- [Medium] Userspace modification of PMEVTYPERn_EL0 or PMCCFILTR_EL0 via KVM_SET_ONE_REG does not trigger a PMU reload or recreate the backing perf_event.
--

--- Patch [6]: [PATCH v9 06/16] KVM: arm64: PMU: Recreate events after MDCR_EL2 changes ---
commit 55bb4c8121d4871eb781b7b2feafd0970dbee364
Author: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>

KVM: arm64: PMU: Recreate events after MDCR_EL2 changes

MDCR_EL2.HPMN changes which counters are reserved for EL2 and thus which
enable control, event filter, and overflow width apply. Route guest writes,
userspace writes, and reset through a common helper to mark the vCPU's
perf events for recreation when HPMN, HPMD, or HLP changes.

> diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
> --- a/arch/arm64/kvm/pmu-emul.c
> +++ b/arch/arm64/kvm/pmu-emul.c

[ ... ]

> @@ -606,6 +609,21 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
>  	}
>  }
>  
> +void kvm_pmu_handle_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val)
> +{
> +	u64 changed = old ^ val;
> +
> +	/*
> +	 * HPMN determines which counters HPMD and HLP apply to. Changes to
> +	 * these fields require new perf event filters and sample periods.
> +	 */
> +	if (changed & MDCR_EL2_PMU_RECREATE_MASK)
> +		vcpu->arch.pmu.events_need_recreate = true;
> +
> +	if (changed & (MDCR_EL2_HPME | MDCR_EL2_PMU_RECREATE_MASK))
> +		kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
> +}

[Severity: Medium]
This is a pre-existing issue, but modifying PMCR_EL0.LC or PMCR_EL0.LP
doesn't seem to recreate perf events.

Guest writes to PMCR_EL0 in kvm_pmu_handle_pmcr() only trigger a PMU reload
if the E bit changes, and never mark events for recreation when LC or LP
change. This also applies to userspace KVM_SET_ONE_REG writes to PMCR_EL0 via
set_pmcr(), which request a reload but fail to set
events_need_recreate = true.

Because PMCR_EL0.LC and LP determine whether the PMU counters overflow at 32
or 64 bits, does this leave the events using stale sample periods, leading
to incorrect performance statistics and mistimed overflow interrupts?

> diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> --- a/arch/arm64/kvm/sys_regs.c
> +++ b/arch/arm64/kvm/sys_regs.c

[ ... ]

> @@ -3741,7 +3751,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
>  	EL2_REG_FILTERED(SCTLR2_EL2, access_vm_reg, reset_val, 0,
>  			 sctlr2_el2_visibility),
>  	EL2_REG_VNCR(HCR_EL2, reset_hcr, 0),
> -	EL2_REG(MDCR_EL2, access_mdcr, reset_mdcr, 0),
> +	SYS_REG_USER_FILTER(MDCR_EL2, access_mdcr, reset_mdcr, 0,
> +			    NULL, set_mdcr, el2_visibility),

[Severity: Medium]
This is also a pre-existing issue, but userspace modifications of
PMEVTYPERn_EL0 or PMCCFILTR_EL0 via KVM_SET_ONE_REG don't appear to trigger a
PMU reload or recreate the backing perf_event.

Because the macros defining PMEVTYPERn_EL0 and PMCCFILTR_EL0 in
sys_reg_descs lack a .set_user accessor, kvm_sys_reg_set_user() falls back to
__vcpu_assign_sys_reg() without requesting a PMU reload or event recreation.

If userspace modifies a vCPU's PMU event type registers after the vCPU has
already run, does the PMU continue counting the old events, leading to
incorrect statistics?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260720-hybrid-v9-0-2b713ca1b5dc@rsg.ci.i.u-tokyo.ac.jp?part=6

  reply	other threads:[~2026-07-20  5:57 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-20  5:37 [PATCH v9 00/16] KVM: arm64: PMU: Use multiple host PMUs Akihiko Odaki
2026-07-20  5:37 ` [PATCH v9 01/16] KVM: arm64: Serialize repeated vCPU initialization Akihiko Odaki
2026-07-20  5:37 ` [PATCH v9 02/16] KVM: arm64: PMU: Stop updating MDCR_EL2.HPMN Akihiko Odaki
2026-07-20  5:58   ` sashiko-bot
2026-07-20  5:37 ` [PATCH v9 03/16] KVM: arm64: PMU: Freeze counter count after first run Akihiko Odaki
2026-07-20  5:37 ` [PATCH v9 04/16] KVM: arm64: selftests: Test SET_NR_COUNTERS " Akihiko Odaki
2026-07-20  6:08   ` sashiko-bot
2026-07-20  5:37 ` [PATCH v9 05/16] KVM: arm64: PMU: Keep implemented counter mask EL-independent Akihiko Odaki
2026-07-20  5:53   ` sashiko-bot
2026-07-20  5:38 ` [PATCH v9 06/16] KVM: arm64: PMU: Recreate events after MDCR_EL2 changes Akihiko Odaki
2026-07-20  5:57   ` sashiko-bot [this message]
2026-07-20  5:38 ` [PATCH v9 07/16] tools headers: Use u* types for bitfield helpers Akihiko Odaki
2026-07-20  5:38 ` [PATCH v9 08/16] KVM: arm64: selftests: Cover PMU state in MDCR_EL2 Akihiko Odaki
2026-07-20  5:38 ` [PATCH v9 09/16] arm64: errata: Require Apple IMPDEF PMUv3 traps on all CPUs Akihiko Odaki
2026-07-20  6:01   ` sashiko-bot
2026-07-20  5:38 ` [PATCH v9 10/16] KVM: arm64: Don't clear vcpu->cpu in kvm_arch_vcpu_put() Akihiko Odaki
2026-07-20  5:38 ` [PATCH v9 11/16] KVM: arm64: PMU: Protect the list of PMUs with RCU Akihiko Odaki
2026-07-20  5:38 ` [PATCH v9 12/16] KVM: arm64: PMU: Pass the pPMU to kvm_map_pmu_event() Akihiko Odaki
2026-07-20  6:03   ` sashiko-bot
2026-07-20  5:38 ` [PATCH v9 13/16] KVM: arm64: PMU: Pass the target CPU to kvm_pmu_probe_armpmu() Akihiko Odaki
2026-07-20  5:38 ` [PATCH v9 14/16] KVM: arm64: PMU: Implement fixed-counters-only emulation Akihiko Odaki
2026-07-20  6:17   ` sashiko-bot
2026-07-20  5:38 ` [PATCH v9 15/16] KVM: arm64: PMU: Introduce FIXED_COUNTERS_ONLY Akihiko Odaki
2026-07-20  5:38 ` [PATCH v9 16/16] 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=20260720055705.A33B61F00A3A@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.