From: Colton Lewis <coltonlewis@google.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Russell King <linux@armlinux.org.uk>,
Jonathan Corbet <corbet@lwn.net>,
Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Shuah Khan <skhan@linuxfoundation.org>,
James Clark <james.clark@linaro.org>,
Alexandru Elisei <alexandru.elisei@arm.com>,
Mingwei Zhang <mizhang@google.com>,
Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com>,
kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
kvm@vger.kernel.org, linux-perf-users@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v10 30/33] KVM: arm64: Support zero event counters on Partitioned PMU without HPMN0
Date: Thu, 19 Nov 2026 18:52:12 +0000 [thread overview]
Message-ID: <20261119185215.632334-31-coltonlewis@google.com> (raw)
In-Reply-To: <20261119185215.632334-1-coltonlewis@google.com>
Programming MDCR_EL2.HPMN to 0 is CONSTRAINED UNPREDICTABLE on CPUs that
do not implement FEAT_HPMN0, preventing userspace from configuring a
cycle-counter-only Partitioned PMU guest (nr_pmu_counters == 0) on those
platforms if HPMN is set directly from nr_pmu_counters.
Support nr_pmu_counters == 0 without FEAT_HPMN0 whenever the PMU has at
least two general-purpose counters by setting physical MDCR_EL2.HPMN = 1
(via kvm_arm_pmu_part_hpmn()), reserving and quiescing physical counter 0
alongside PMCCNTR_EL0 on vCPU load so host perf uses counters 1..N-1,
keeping MDCR_EL2.TPMCR set so trapped PMCR_EL0 reads report N == 0, and
enabling fine-grained traps on PMEVCNTRn_EL0, PMCNTEN, PMINTEN, and
PMSWINC_EL0 so counter 0 remains completely hidden from the guest while
PMCCNTR_EL0 accesses stay untrapped on FEAT_FGT hosts.
Signed-off-by: Colton Lewis <coltonlewis@google.com>
---
arch/arm64/kvm/config.c | 16 ++++++++++----
arch/arm64/kvm/debug.c | 12 ++++++-----
arch/arm64/kvm/pmu-direct.c | 42 ++++++++++++++++++++++++++++++-------
arch/arm64/kvm/pmu.c | 15 +++++++++++--
include/kvm/arm_pmu.h | 6 ++++++
5 files changed, 73 insertions(+), 18 deletions(-)
diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index 0ec74e45fdbf6..bbe940f6266aa 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -1791,6 +1791,8 @@ static void __compute_hdfgrtr(struct kvm_vcpu *vcpu)
__compute_fgt(vcpu, HDFGRTR_EL2);
if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+ unsigned int n = vcpu->kvm->arch.nr_pmu_counters;
+ bool hide_gp0 = kvm_arm_pmu_part_hpmn(n) != n;
u64 set = HDFGRTR_EL2_PMOVS |
HDFGRTR_EL2_PMCCFILTR_EL0 |
HDFGRTR_EL2_PMEVTYPERn_EL0 |
@@ -1801,10 +1803,12 @@ static void __compute_hdfgrtr(struct kvm_vcpu *vcpu)
(__vcpu_sys_reg(vcpu, MDCR_EL2) & MDCR_EL2_TPM))
set |= HDFGRTR_EL2_PMUSERENR_EL0;
- if (kvm_pmu_hyp_counter_mask(vcpu))
+ if (kvm_pmu_hyp_counter_mask(vcpu) || hide_gp0)
set |= HDFGRTR_EL2_PMCNTEN;
- if (kvm_pmu_part_ovf32_mask(vcpu))
+ if (kvm_pmu_part_ovf32_mask(vcpu) || hide_gp0)
set |= HDFGRTR_EL2_PMEVCNTRn_EL0;
+ if (hide_gp0)
+ set |= HDFGRTR_EL2_PMINTEN;
*vcpu_fgt(vcpu, HDFGRTR_EL2) |= set & ~hdfgrtr_masks.res0;
}
@@ -1818,6 +1822,8 @@ static void __compute_hdfgwtr(struct kvm_vcpu *vcpu)
*vcpu_fgt(vcpu, HDFGWTR_EL2) |= HDFGWTR_EL2_MDSCR_EL1;
if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+ unsigned int n = vcpu->kvm->arch.nr_pmu_counters;
+ bool hide_gp0 = kvm_arm_pmu_part_hpmn(n) != n;
u64 set = HDFGWTR_EL2_PMOVS |
HDFGWTR_EL2_PMCCFILTR_EL0 |
HDFGWTR_EL2_PMEVTYPERn_EL0;
@@ -1825,10 +1831,12 @@ static void __compute_hdfgwtr(struct kvm_vcpu *vcpu)
if (is_hyp_ctxt(vcpu))
set |= HDFGWTR_EL2_PMUSERENR_EL0;
- if (kvm_pmu_hyp_counter_mask(vcpu))
+ if (kvm_pmu_hyp_counter_mask(vcpu) || hide_gp0)
set |= HDFGWTR_EL2_PMCNTEN | HDFGWTR_EL2_PMSWINC_EL0;
- if (kvm_pmu_part_ovf32_mask(vcpu))
+ if (kvm_pmu_part_ovf32_mask(vcpu) || hide_gp0)
set |= HDFGWTR_EL2_PMEVCNTRn_EL0;
+ if (hide_gp0)
+ set |= HDFGWTR_EL2_PMINTEN;
*vcpu_fgt(vcpu, HDFGWTR_EL2) |= set & ~hdfgwtr_masks.res0;
}
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index b89f46cb59955..73d157f5b8f6f 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -58,11 +58,11 @@ void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
if (kvm_pmu_is_partitioned(vcpu->kvm) && vcpu->arch.pmu.loaded_on_cpu) {
u8 nr_host_cntr = *host_data_ptr(nr_event_counters);
u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters;
+ u8 guest_hpmn = kvm_arm_pmu_part_hpmn(nr_guest_cntr);
if (cpumask_test_cpu(smp_processor_id(),
vcpu->kvm->arch.supported_cpus) &&
- nr_guest_cntr < nr_host_cntr &&
- (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
+ guest_hpmn < nr_host_cntr) {
u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, read_sysreg(mdcr_el2));
bool hpme;
@@ -81,14 +81,16 @@ void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
* Take out the coarse grain traps if we are using
* fine grain traps and enforce counter access with
* HPMN. Keep TPMCR when L1 sub-partitions its counters
- * so PMCR_EL0 reads and writes trap for multiplexing.
+ * or when guest_hpmn > nr_guest_cntr (!FEAT_HPMN0 with
+ * 0 event counters) so PMCR_EL0 accesses trap.
*/
mdcr &= ~MDCR_EL2_HPMN;
- mdcr |= FIELD_PREP(MDCR_EL2_HPMN, nr_guest_cntr);
+ mdcr |= FIELD_PREP(MDCR_EL2_HPMN, guest_hpmn);
if (cpus_have_final_cap(ARM64_HAS_FGT)) {
mdcr &= ~MDCR_EL2_TPM;
- if (!kvm_pmu_hyp_counter_mask(vcpu))
+ if (!kvm_pmu_hyp_counter_mask(vcpu) &&
+ guest_hpmn == nr_guest_cntr)
mdcr &= ~MDCR_EL2_TPMCR;
}
}
diff --git a/arch/arm64/kvm/pmu-direct.c b/arch/arm64/kvm/pmu-direct.c
index 6382233e54c71..7b8f0a69641bc 100644
--- a/arch/arm64/kvm/pmu-direct.c
+++ b/arch/arm64/kvm/pmu-direct.c
@@ -89,6 +89,22 @@ static u64 kvm_vcpu_pmu_guest_counter_mask(struct kvm_vcpu *vcpu)
return 0;
}
+static u64 kvm_vcpu_pmu_reserved_counter_mask(struct kvm_vcpu *vcpu)
+{
+ if (vcpu && kvm_pmu_is_partitioned(vcpu->kvm)) {
+ u64 mask = ARMV8_PMU_CNT_MASK_C;
+ unsigned int hpmn =
+ kvm_arm_pmu_part_hpmn(vcpu->kvm->arch.nr_pmu_counters);
+
+ if (hpmn > 0)
+ mask |= GENMASK_ULL(hpmn - 1, 0);
+
+ return mask;
+ }
+
+ return 0;
+}
+
static void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu);
/**
@@ -372,8 +388,12 @@ static u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
if (!kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5))
pmcr &= ~ARMV8_PMU_PMCR_LP;
- if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu))
- n = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
+ if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu)) {
+ u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
+
+ if (hpmn < n)
+ n = hpmn;
+ }
return u64_replace_bits(pmcr, n, ARMV8_PMU_PMCR_N);
}
@@ -686,6 +706,7 @@ static void kvm_pmu_apply_event_filter(struct kvm_vcpu *vcpu)
static void __kvm_pmu_load(struct kvm_vcpu *vcpu, bool reserve_counters)
{
struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu;
+ unsigned long reserved_counters;
unsigned long guest_counters;
unsigned long flags;
unsigned long daif;
@@ -710,20 +731,27 @@ static void __kvm_pmu_load(struct kvm_vcpu *vcpu, bool reserve_counters)
return;
}
+ reserved_counters = kvm_vcpu_pmu_reserved_counter_mask(vcpu);
guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
if (reserve_counters)
- kvm_pmu_set_guest_counters(pmu, guest_counters);
+ kvm_pmu_set_guest_counters(pmu, reserved_counters);
daif = local_daif_save();
/*
* Ensure guest counters, interrupts, and overflow flags are quiesced
* in hardware before lowering MDCR_EL2.HPMN and marking the partition
- * live on this CPU.
+ * live on this CPU. If counter 0 is reserved solely to satisfy HPMN=1
+ * without FEAT_HPMN0, scrub and filter it so it cannot count or leak.
*/
- write_sysreg(guest_counters, pmcntenclr_el0);
- write_sysreg(guest_counters, pmintenclr_el1);
- write_sysreg(guest_counters, pmovsclr_el0);
+ write_sysreg(reserved_counters, pmcntenclr_el0);
+ write_sysreg(reserved_counters, pmintenclr_el1);
+ write_sysreg(reserved_counters, pmovsclr_el0);
+ if (reserved_counters & ~guest_counters) {
+ write_pmevcntrn(0, 0);
+ write_pmevtypern(0, ARMV8_PMU_EXCLUDE_EL0 |
+ ARMV8_PMU_EXCLUDE_EL1);
+ }
isb();
vcpu->arch.pmu.loaded_on_cpu = true;
diff --git a/arch/arm64/kvm/pmu.c b/arch/arm64/kvm/pmu.c
index 9d4ad4b7bd8d5..02a1968bd6cb1 100644
--- a/arch/arm64/kvm/pmu.c
+++ b/arch/arm64/kvm/pmu.c
@@ -914,6 +914,13 @@ u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)
return mask & ~kvm_pmu_hyp_counter_mask(vcpu);
}
+unsigned int kvm_arm_pmu_part_hpmn(unsigned int n)
+{
+ if (!n && !cpus_have_final_cap(ARM64_HAS_HPMN0))
+ return 1;
+ return n;
+}
+
/**
* kvm_vcpu_read_pmcr - Read PMCR_EL0 register for the vCPU
* @vcpu: The vcpu pointer
@@ -923,8 +930,12 @@ u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
u64 pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
u64 n = vcpu->kvm->arch.nr_pmu_counters;
- if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu))
- n = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
+ if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu)) {
+ u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
+
+ if (hpmn < n)
+ n = hpmn;
+ }
return u64_replace_bits(pmcr, n, ARMV8_PMU_PMCR_N);
}
diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h
index 0fe4f593d3f9a..bebf981855040 100644
--- a/include/kvm/arm_pmu.h
+++ b/include/kvm/arm_pmu.h
@@ -137,6 +137,7 @@ u8 kvm_arm_pmu_get_pmuver_limit(void);
u64 kvm_pmu_evtyper_mask(struct kvm *kvm);
int kvm_arm_set_default_pmu(struct kvm *kvm);
u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm);
+unsigned int kvm_arm_pmu_part_hpmn(unsigned int n);
u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu);
bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idx);
@@ -277,6 +278,11 @@ static inline u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
return 0;
}
+static inline unsigned int kvm_arm_pmu_part_hpmn(unsigned int n)
+{
+ return n;
+}
+
static inline u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
{
return 0;
--
2.56.1.708.g77570a6a1d-goog
next prev parent reply other threads:[~2026-11-19 18:52 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 18:51 [PATCH v10 00/33] ARM64 PMU Partitioning Colton Lewis
2026-11-19 18:51 ` [PATCH v10 01/33] arm64: cpufeature: Add cpucap for HPMN0 Colton Lewis
2026-11-19 18:51 ` [PATCH v10 02/33] KVM: arm64: Reorganize PMU includes Colton Lewis
2026-11-19 18:51 ` [PATCH v10 03/33] KVM: arm64: Reorganize PMU functions Colton Lewis
2026-11-19 18:51 ` [PATCH v10 04/33] KVM: arm64: Use nr_pmu_counters in kvm_pmu_implemented_counter_mask() Colton Lewis
2026-11-19 18:51 ` [PATCH v10 05/33] KVM: arm64: Ignore HPMN for userspace PMU register access Colton Lewis
2026-11-19 18:51 ` [PATCH v10 06/33] perf: arm_pmuv3: Generalize counter bitmasks Colton Lewis
2026-11-19 18:51 ` [PATCH v10 07/33] perf: arm_pmuv3: Move counter allocation mask to per-CPU struct pmu_hw_events Colton Lewis
2026-11-19 18:51 ` [PATCH v10 08/33] perf: arm_pmuv3: Check cntr_mask before using pmccntr Colton Lewis
2026-11-19 18:51 ` [PATCH v10 09/33] perf: arm_pmuv3: Allocate counter indices from high to low Colton Lewis
2026-11-19 18:51 ` [PATCH v10 10/33] perf: arm_pmuv3: Expose number of general-purpose counters in sysfs Colton Lewis
2026-11-19 18:51 ` [PATCH v10 11/33] KVM: arm64: Add initial scaffolding for Partitioned PMU Colton Lewis
2026-11-19 18:51 ` [PATCH v10 12/33] KVM: arm64: Set up FGT " Colton Lewis
2026-11-19 18:51 ` [PATCH v10 13/33] KVM: arm64: Make __vcpu_rmw_sys_reg() atomic Colton Lewis
2026-11-19 18:51 ` [PATCH v10 14/33] KVM: arm64: Add Partitioned PMU register trap handlers Colton Lewis
2026-11-19 18:51 ` [PATCH v10 15/33] KVM: arm64: Construct mdcr_el2 in a local variable in kvm_arm_setup_mdcr_el2() Colton Lewis
2026-11-19 18:51 ` [PATCH v10 16/33] KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU Colton Lewis
2026-11-19 18:51 ` [PATCH v10 17/33] KVM: arm64: Context swap Partitioned PMU guest registers Colton Lewis
2026-11-19 18:52 ` [PATCH v10 18/33] KVM: arm64: Enforce PMU event filter at vcpu_load() Colton Lewis
2026-11-19 18:52 ` [PATCH v10 19/33] perf: Add perf_pmu_resched_update() Colton Lewis
2026-11-19 18:52 ` [PATCH v10 20/33] perf/core: Prioritize kernel-owned pinned events when scheduling groups Colton Lewis
2026-11-19 18:52 ` [PATCH v10 21/33] KVM: arm64: Allow kvm_vcpu_pmu_resync_el0() to resync filters in process context Colton Lewis
2026-11-19 18:52 ` [PATCH v10 22/33] KVM: arm64: Apply dynamic guest counter reservations Colton Lewis
2026-11-19 18:52 ` [PATCH v10 23/33] KVM: arm64: Implement lazy PMU context swaps Colton Lewis
2026-11-19 18:52 ` [PATCH v10 24/33] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters Colton Lewis
2026-11-19 18:52 ` [PATCH v10 25/33] KVM: arm64: Detect overflows for the Partitioned PMU Colton Lewis
2026-11-19 18:52 ` [PATCH v10 26/33] KVM: arm64: Multiplex PMCR_EL0.E and MDCR_EL2.HPME for sub-partitioned L1 Colton Lewis
2026-11-19 18:52 ` [PATCH v10 27/33] KVM: arm64: Emulate mismatched LP and HLP overflow widths " Colton Lewis
2026-11-19 18:52 ` [PATCH v10 28/33] KVM: arm64: Emulate trapped PMSWINC_EL0 writes for Partitioned PMU Colton Lewis
2026-11-19 18:52 ` [PATCH v10 29/33] KVM: arm64: Forward FGT traps for PMXEVTYPER_EL0 and PMXEVCNTR_EL0 Colton Lewis
2026-11-19 18:52 ` Colton Lewis [this message]
2026-11-19 18:52 ` [PATCH v10 31/33] KVM: arm64: Add vCPU device attr to partition the PMU Colton Lewis
2026-11-19 18:52 ` [PATCH v10 32/33] KVM: selftests: Add find_bit to KVM library Colton Lewis
2026-11-19 18:52 ` [PATCH v10 33/33] KVM: arm64: selftests: Add test case for Partitioned PMU Colton Lewis
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=20261119185215.632334-31-coltonlewis@google.com \
--to=coltonlewis@google.com \
--cc=alexandru.elisei@arm.com \
--cc=catalin.marinas@arm.com \
--cc=corbet@lwn.net \
--cc=fuad.tabba@linux.dev \
--cc=gankulkarni@os.amperecomputing.com \
--cc=james.clark@linaro.org \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=mingo@redhat.com \
--cc=mizhang@google.com \
--cc=oupton@kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=skhan@linuxfoundation.org \
--cc=suzuki.poulose@arm.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®