From: Vincent Donnefort <vdonnefort@google.com>
To: Fuad Tabba <fuad.tabba@linux.dev>
Cc: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
kernel-team@android.com, linux-kernel@vger.kernel.org,
Oculytic <oculytic@gmail.com>, Fuad Tabba <tabba@google.com>
Subject: Re: [PATCH 2/3] KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests
Date: Tue, 27 Oct 2026 11:05:15 +0000 [thread overview]
Message-ID: <auCFa2VPEmxx2ZcC@google.com> (raw)
In-Reply-To: <20261027081147.498183-3-fuad.tabba@linux.dev>
On Tue, Oct 27, 2026 at 08:11:46AM +0000, Fuad Tabba wrote:
> From: Oliver Upton <oliver.upton@linux.dev>
>
> Before every run, the host records in vcpu->arch.pmu.events which perf
> counters to disable and enable around guest entry, and
> __pmu_switch_to_guest() applies them at EL2. Since commit be66e67f1750
> ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()"),
> protected mode runs the hyp vCPU instead, and flush_hyp_vcpu() never
> copies the events into it. So the counters backing a non-protected
> guest's PMU are never enabled, and every counter the guest reads stays
> at 0, the cycle counter included. The host's counters that exclude
> guests keep counting the guest as well.
>
> Copy the events into the hyp vCPU of a non-protected guest on every run.
> The events only exist with CONFIG_HW_PERF_EVENTS, so the copy is built
> under it. Like mdcr_el2, they're host-controlled and never reach a
> protected guest.
>
> Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
> Link: https://lore.kernel.org/all/20240305184840.636212-2-oliver.upton@linux.dev/
> Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
> [Fuad: non-protected copy only, tested on the hyp vCPU, under
> CONFIG_HW_PERF_EVENTS, new message]
> Co-developed-by: Fuad Tabba <fuad.tabba@linux.dev>
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
> ---
> arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> index d715aa942577e..28a292deecb64 100644
> --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> @@ -690,6 +690,16 @@ static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)
> host_vcpu->arch.external_debug_state = hyp_vcpu->vcpu.arch.external_debug_state;
> }
>
> +#ifdef CONFIG_HW_PERF_EVENTS
> +static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu)
> +{
> + if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
> + hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events;
> +}
> +#else
> +static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu) {}
> +#endif
nit: Could save a couple of lines by just doing?
#ifdef CONFIG_HW_PERF_EVENTS
if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events;
#endif
> +
> static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
> {
> struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
> @@ -698,6 +708,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
>
> fpsimd_sve_flush();
> flush_debug_state(hyp_vcpu);
> + flush_pmu_state(hyp_vcpu);
>
> /*
> * If we deal with a non-protected guest and the state is potentially
> --
> 2.47.3
>
Reviewed-by: Vincent Donnefort <vdonnefort@google.com>
Tested-by: Vincent Donnefort <vdonnefort@google.com>
--
Vincent
next prev parent reply other threads:[~2026-10-27 11:05 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 8:11 [PATCH 0/3] KVM: arm64: Fix PMU handling in pKVM Fuad Tabba
2026-10-27 8:11 ` [PATCH 1/3] KVM: arm64: Synchronise kvm_host_data before host deprivileging Fuad Tabba
2026-10-27 8:11 ` [PATCH 2/3] KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests Fuad Tabba
2026-10-27 11:05 ` Vincent Donnefort [this message]
2026-10-28 11:01 ` Fuad Tabba
2026-10-27 8:11 ` [PATCH 3/3] KVM: arm64: Disable host PMU counters for protected guests Fuad Tabba
2026-10-27 9:32 ` Marc Zyngier
2026-10-27 9:41 ` Fuad Tabba
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=auCFa2VPEmxx2ZcC@google.com \
--to=vdonnefort@google.com \
--cc=catalin.marinas@arm.com \
--cc=fuad.tabba@linux.dev \
--cc=joey.gouly@arm.com \
--cc=kernel-team@android.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oculytic@gmail.com \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/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.