Skip to contentSkip to conversation

target/arm: Enable KVM PMU partitioning and counter limit

RFC PATCH New Colton Lewis 1 participant 1 message Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 target/arm: Enable KVM PMU partitioning and counter limit

Add CPU properties to configure PMU event counters and KVM ARM64 PMU counter partitioning:
- 'kvm-pmu-partition' (bool, KVM only): enables KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION
- 'pmu-num-counters' (uint32, TCG and KVM): sets the number of general-purpose PMU event counters (PMCR_EL0.N) and configures KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS under KVM

When 'kvm-pmu-partition=on' and 'pmu-num-counters=N' are passed via -cpu under KVM, QEMU enables hardware PMU partitioning and reserves N general-purpose event counters for the guest prior to calling KVM_ARM_VCPU_PMU_V3_INIT.

Note that this patch is an RFC to demonstrate the KVM ARM64 PMU Partitioning uAPI. That is why it aborts immediately if setting the device attributes does not succeed.

---
Companion RFC to the v10 kernel series "ARM64 PMU Partitioning", based on QEMU master (08c9bbb825, v11.2.0-rc1). KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION is attribute 5 (with KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS = 4), matching the v10 uAPI.

Built cleanly (0 warnings) for both native x86_64 and cross-arm64 (aarch64-softmmu with KVM and aarch64-linux-user). Verified with the v10 kernel (7.4.0-rc3+):
- TCG (aarch64-softmmu): pmu-num-counters = 0, 2, default (6), 31, pmu=off, rejection of pmu-num-counters=32 (> 31), and rejection of kvm-pmu-partition=on under TCG.
- KVM (-accel kvm -machine virt,gic-version=3, cross-arm64 binary inside a VHE TCG host with 6 GP counters): -smp 2 boots with kvm-pmu-partition=on (pmu-num-counters = 0, 2, 5) and emulated pmu-num-counters=3, plus negative rejections for missing pmu-num-counters, pmu-num-counters=6 (partitioned), =7 (emulated), and =32 (> 31). Zero host kernel warnings.

Changes since v1:
https://lore.kernel.org/kvmarm/[email protected]/
- Rebase onto QEMU master (08c9bbb825, v11.2.0-rc1).
- Tag patch as RFC until the kernel uAPI is accepted (Peter Maydell).
- Document kvm-pmu-partition (including pmu-num-counters=0 without FEAT_HPMN0) and pmu-num-counters in docs/system/arm/cpu-features.rst (Peter Maydell).
- Move the partitioning toggle into kvm_arm_add_vcpu_properties() as kvm-pmu-partition using DEFINE_PROP_BOOL (Peter Maydell).
- Keep pmu-num-counters general (DEFINE_PROP_UINT32) and wire it up for both TCG (reset_pmcr_el0) and KVM (Peter Maydell).
- Guard the pmu_num_counters override in arm_cpu_realizefn() with #ifndef CONFIG_USER_ONLY so aarch64-linux-user does not zero reset_pmcr_el0.N.
- Wrap commit message lines to 72 columns.

 docs/system/arm/cpu-features.rst | 29 +++++++++++++++++++++
 target/arm/cpu.c                 | 19 ++++++++++++++
 target/arm/cpu.h                 |  4 +++
 target/arm/kvm.c                 | 44 ++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
docs/system/arm/cpu-features.rst +29 -0
index 10b0eff27e..9d95f727be 100644
@@ -217,6 +217,21 @@ the list of KVM VCPU features and their descriptions.
 
   Current valid values are: 0.1, 0.2, 1.0, 1.1, 1.2, and 1.3.
 
+``kvm-pmu-partition``
+  By default kvm-pmu-partition is disabled.
+
+  When kvm-pmu-partition is enabled, KVM partitions the physical PMUv3
+  hardware counters between the guest and host using ``MDCR_EL2.HPMN``,
+  granting the guest direct, untrapped access to its partition of event
+  counters and the cycle counter.  Enabling ``kvm-pmu-partition``
+  requires explicitly configuring the number of guest event counters via
+  ``pmu-num-counters`` to a value strictly less than the number of
+  general-purpose counters implemented by the host PMU (leaving at least
+  one general-purpose counter reserved for host profiling).  On hosts
+  without ``FEAT_HPMN0``, setting ``pmu-num-counters=0`` (cycle counter
+  only) requires at least two general-purpose host counters so physical
+  counter 0 can be reserved and hidden behind traps.
+
 TCG VCPU Features
 =================
 
@@ -495,3 +510,17 @@ Legal values for ``S`` are 30, 34, 36, and 39; the default is 30.
 
 As with ``x-rme``, the ``x-l0gptsz`` property may be renamed or
 removed in some future QEMU release.
+
+PMU CPU Properties
+==================
+
+``pmu``
+  Enable or disable the Performance Monitoring Unit (PMU).
+
+``pmu-num-counters``
+  Set the number of general-purpose event counters exposed by the PMU
+  (encoded in ``PMCR_EL0.N``) for both TCG and KVM guests (between 0 and
+  31 for TCG, or up to the number of counters supported by the host PMU
+  for KVM).  When ``kvm-pmu-partition`` is enabled under KVM, this
+  value must be strictly less than the number of general-purpose
+  counters implemented by the host PMU.
target/arm/cpu.c +19 -0
index dde03ffdd6..81b2b83ce2 100644
@@ -1361,6 +1361,9 @@ static const Property arm_cpu_pmsav7_dregion_property =
             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
                                            pmsav7_dregion,
                                            qdev_prop_uint32, uint32_t);
+static const Property arm_cpu_pmu_num_counters_property =
+            DEFINE_PROP_UINT32("pmu-num-counters", ARMCPU, pmu_num_counters,
+                               UINT32_MAX);
 #endif
 
 static bool arm_get_pmu(Object *obj, Error **errp)
@@ -1628,6 +1631,10 @@ static void arm_cpu_post_init(Object *obj)
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
+#ifndef CONFIG_USER_ONLY
+        qdev_property_add_static(DEVICE(obj),
+                                 &arm_cpu_pmu_num_counters_property);
+#endif
     }
 
     /*
@@ -2250,6 +2257,18 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         unset_feature(env, ARM_FEATURE_PMU);
     }
     if (arm_feature(env, ARM_FEATURE_PMU)) {
+#ifndef CONFIG_USER_ONLY
+        if (cpu->pmu_num_counters != UINT32_MAX) {
+            if (cpu->pmu_num_counters > 31) {
+                error_setg(errp, "pmu-num-counters %" PRIu32
+                           " exceeds maximum of 31", cpu->pmu_num_counters);
+                return;
+            }
+            cpu->isar.reset_pmcr_el0 =
+                (cpu->isar.reset_pmcr_el0 & ~PMCRN_MASK) |
+                (cpu->pmu_num_counters << PMCRN_SHIFT);
+        }
+#endif
         pmu_init(cpu);
 
         if (tcg_enabled() || hvf_enabled()) {
target/arm/cpu.h +4 -0
index b2f393f501..990204d7b6 100644
@@ -1024,6 +1024,10 @@ struct ArchCPU {
     bool has_el3;
     /* CPU has PMU (Performance Monitor Unit) */
     bool has_pmu;
+    /* CPU has PMU Partitioning enabled in KVM mode */
+    bool kvm_pmu_partition;
+    /* Number of PMU event counters (PMCR_EL0.N) */
+    uint32_t pmu_num_counters;
     /* CPU has VFP */
     bool has_vfp;
     /* CPU has 32 VFP registers */
target/arm/kvm.c +44 -0

74 lines not on this page · raw message

base-commit: 08c9bbb825148dec9669f5184ecf18823bb6d631

Signature · 1 line
2.56.1.708.g77570a6a1d-goog