Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Congkai Tan" <congkai@amazon.com>
Cc: kvmarm@lists.linux.dev, "Oliver Upton" <oupton@kernel.org>,
	"Marc Zyngier" <maz@kernel.org>,
	kvm@vger.kernel.org
Subject: Re: [PATCH v2 1/3] KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature
Date: Thu, 02 Jul 2026 19:29:17 +0000	[thread overview]
Message-ID: <20260702192918.303A01F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260702190421.420992-2-congkai@amazon.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Silent truncation of PMU event counter states and PMCR_EL0.N during VCPU sysreg restoration before PMU initialization.
--

commit 7651e4c8e75621127e5fba611a9aa7f3257fa4b2
Author: Congkai Tan <congkai@amazon.com>

KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature

This patch introduces the KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature, preventing
KVM from creating a default PMU on initialization. Userspace must instead
explicitly select a PMU via KVM_ARM_VCPU_PMU_V3_SET_PMU before the first
KVM_RUN.

> diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
> index e1860acae641f..1f24169505a91 100644
> --- a/arch/arm64/kvm/pmu-emul.c
> +++ b/arch/arm64/kvm/pmu-emul.c

[ ... ]

> @@ -1021,6 +1024,14 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
>  {
>  	struct arm_pmu *arm_pmu = kvm->arch.arm_pmu;
>  
> +	/*
> +	 * Under KVM_ARM_VCPU_PMU_V3_STRICT no PMU exists until userspace sets
> +	 * one, so this can be reached before arm_pmu is set. Report no
> +	 * counters in that case.
> +	 */
> +	if (!arm_pmu)
> +		return 0;
> +

[Severity: High]
Could returning 0 here before the PMU is explicitly selected cause silent
truncation of PMU event counter states during live migration?

If a VMM restores PMU sysregs via KVM_SET_ONE_REG before selecting the PMU
via KVM_ARM_VCPU_PMU_V3_SET_PMU, set_pmcr() in arch/arm64/kvm/sys_regs.c
evaluates:

arch/arm64/kvm/sys_regs.c:set_pmcr() {
    if (!kvm_vm_has_ran_once(kvm) &&
        !vcpu_has_nv(vcpu)        &&
        new_n <= kvm_arm_pmu_get_max_counters(kvm))
        kvm->arch.nr_pmu_counters = new_n;
    ...
}

Because kvm_arm_pmu_get_max_counters() now returns 0, the bounds check fails.
set_pmcr() then leaves nr_pmu_counters at 0 without returning an error to
userspace.

Consequently, when set_pmreg() executes:

arch/arm64/kvm/sys_regs.c:set_pmreg() {
    ...
    u64 mask = kvm_pmu_accessible_counter_mask(vcpu);
    __vcpu_assign_sys_reg(vcpu, r->reg, val & mask);
}

The mask evaluates to only the cycle counter since nr_pmu_counters is 0.
This silently zeroes out the restored event counter bits for registers like
PMOVSSET_EL0, PMINTENSET_EL1, and PMCNTENSET_EL0.

Should the KVM API reject this out-of-order restoration sequence with an
error like -EINVAL, rather than silently discarding the VMM's restored
PMCR_EL0.N and event counter states?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260702190421.420992-1-congkai@amazon.com?part=1

  reply	other threads:[~2026-07-02 19:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02 19:04 [PATCH v2 0/3] KVM: arm64: Expose PMMIR_EL1.SLOTS to guests Congkai Tan
2026-07-02 19:04 ` [PATCH v2 1/3] KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature Congkai Tan
2026-07-02 19:29   ` sashiko-bot [this message]
2026-07-02 19:04 ` [PATCH v2 2/3] KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI Congkai Tan
2026-07-02 19:04 ` [PATCH v2 3/3] KVM: arm64: Advertise STALL_SLOT* in PMCEID1 " Congkai Tan

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=20260702192918.303A01F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=congkai@amazon.com \
    --cc=kvm@vger.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