From nobody Mon Nov 23 04:28:00 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1795114428; cv=none; d=zohomail.com; s=zohoarc; b=VYml6rJW815JzsIkc6DsTi95jwJpAQUjmR0cjBM/3YpEJAWN1y45r7brvpTXfjj0QCFKq4cuJlAJE4fj4ifOaUHI+7M5k5e0i1SY5+93f3OYYGpcFzh02/nDBnZLFNgcSyiQ6P8II6X/7U1e0+j16GZg9JJ9jf6pdmqVEJIJZbU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795114428; h=Content-Type:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=TH5vitwFPm9bGuJwkWFGWD1DGO5XNv6nZY+HFNZ0AOQ=; b=NU9+WXetLzKqunK7BdMZxlI/lMviS6cTKtlQjdCICVVbszY2V/LkKnXgraPG5c1pfcF+6UlW2TZYfsYOI7L2pdBz7hcs/WhLLAirz1oBrXrmynlJFXW2SOvjdcIzA89OBc3VIcUuggEFg8MQ+wor3MGuCNWQGkCx8G4GHalXX/8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795114428537789.7517682093187; Thu, 19 Nov 2026 10:53:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU7Fn-0005q1-Ca; Thu, 19 Nov 2026 13:53:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3hEX_agsKCmsLXUcXWUNfRbPXXPUN.LXVZNVd-MNeNUWXWPWd.XaP@flex--coltonlewis.bounces.google.com>) id 1xU7Fj-0005pW-8e for qemu-devel@nongnu.org; Thu, 19 Nov 2026 13:52:59 -0500 Received: from mail-oi1-x247.google.com ([2607:f8b0:4864:20::247]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3hEX_agsKCmsLXUcXWUNfRbPXXPUN.LXVZNVd-MNeNUWXWPWd.XaP@flex--coltonlewis.bounces.google.com>) id 1xU7Fh-0002RB-02 for qemu-devel@nongnu.org; Thu, 19 Nov 2026 13:52:58 -0500 Received: by mail-oi1-x247.google.com with SMTP id 5614622812f47-574f8578e48so157040b6e.2 for ; Thu, 19 Nov 2026 10:52:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795114373; x=1795719173; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=TH5vitwFPm9bGuJwkWFGWD1DGO5XNv6nZY+HFNZ0AOQ=; b=VZNKQqf3GvT++C/lltn5G0iHuQoCGC40DuHFpJxaBL2wDnB8jCklTF4rIConiPc9bE qBMuuXFfzamgNFRBPjpP4h6QMwFURGLU3YyvOGArznpK3YLyEjpGWW+fFs712Kq+pwSI sPr+GpfFF2WFXxVXAhlMUoSe+wUoF+N9rb8fGu6+l9i/WaQk3bYlEFGm3d7uVORLOCye kJbMYRzkhMWR2M/2h82H8EdWDhpo/cxPlr5XXRYODRTRCEHaddz3u53d5EQt064jBPGq KGTsYEKmfMV4xjKWWyYPKvHtgC3Z0z7V17G3bXyC7l1W+cGkrpXd863O1EpmM815RKwD 65pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795114373; x=1795719173; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=TH5vitwFPm9bGuJwkWFGWD1DGO5XNv6nZY+HFNZ0AOQ=; b=dImMP5+yU2H4YIKPHylgJ1KqYSUSPFBdaL9aw+2NOuaJ7L34Vir/w58e2y3B8bKqJ3 /f9tzr8dTVdbv+EzpSrcOyqc20MrX677fInm4qrqppndU0QtqVVK1kjSU/UtsgXLJQGD mx5nDcIHWOHinIaTHIQf2ET4imtF3xBFw7eEkWAD5OPGOfeSPfGuQ5xYPU9O/8TL6zz6 HzcXNLP5+G5sHBctt8ZkAOKTnZwcUI4UJppXithSg82QrUEHN0zGnGvi0DRlPJ2y3xg9 dqC1kAOipH28pJnTIyHMS22+iygBhJU6GXoBwbw23TCimmSe9AZ8aa1hXb7E7nTFsnFV Lwcg== X-Forwarded-Encrypted: i=1; AL+hErEozklC9iHfWLEsHB8C2k4WeukmWCI56H8pCSG8zMD9xR79Tdnipx+vwQErn2h2mmB9ddDHA1g03JM3@nongnu.org X-Gm-Message-State: APuU4Z8qEIV3lowb8GwleJ4muuF4vu0eixbI//nluGlO0xhWrO5cUf2x cl0e1NDht24EFXkFV1+DaBTHyEY1pvh7tFvJyvmIag8NRiGzNBx6U0F2DRNwp2bEg84yQ9GttSw OwxM+1+mwUnyARclglbjDEv27iA== X-Received: from ilmy14.prod.google.com ([2002:a92:d80e:0:b0:50c:2841:18fc]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:1528:b0:4d6:90d3:e185 with SMTP id 5614622812f47-57a0b645ee5mr1052075b6e.46.1795114372340; Thu, 19 Nov 2026 10:52:52 -0800 (PST) Date: Thu, 19 Nov 2026 18:52:51 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261119185251.638036-1-coltonlewis@google.com> Subject: [RFC PATCH v2] target/arm: Enable KVM PMU partitioning and counter limit From: Colton Lewis To: Peter Maydell Cc: Paolo Bonzini , Pierrick Bouvier , Marc Zyngier , Oliver Upton , Will Deacon , James Clark , Mingwei Zhang , qemu-arm@nongnu.org, qemu-devel@nongnu.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::247; envelope-from=3hEX_agsKCmsLXUcXWUNfRbPXXPUN.LXVZNVd-MNeNUWXWPWd.XaP@flex--coltonlewis.bounces.google.com; helo=mail-oi1-x247.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1795114430602158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3Don' and 'pmu-num-counters=3DN' 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. Signed-off-by: Colton Lewis --- 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` =3D 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` =3D 0, 2, default (6), 31, `pmu=3Doff`, rejection of `pmu-num-counters=3D32` (> 31), and rejection of `kvm-pmu-partition=3Don` under TCG. - KVM (`-accel kvm -machine virt,gic-version=3D3`, cross-arm64 binary inside a VHE TCG host with 6 GP counters): `-smp 2` boots with `kvm-pmu-partition=3Don` (`pmu-num-counters` =3D 0, 2, 5) and emulated `pmu-num-counters=3D3`, plus negative rejections for missing `pmu-num-counters`, `pmu-num-counters=3D6` (partitioned), `=3D7` (emulated), and `=3D32` (> 31). Zero host kernel warnings. Changes since v1: https://lore.kernel.org/kvmarm/20260924173051.2117637-1-coltonlewis@google.= com/ - 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=3D0` 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(+) diff --git a/docs/system/arm/cpu-features.rst b/docs/system/arm/cpu-feature= s.rst index 10b0eff27e..9d95f727be 100644 --- a/docs/system/arm/cpu-features.rst +++ b/docs/system/arm/cpu-features.rst @@ -217,6 +217,21 @@ the list of KVM VCPU features and their descriptions. =20 Current valid values are: 0.1, 0.2, 1.0, 1.1, 1.2, and 1.3. =20 +``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=3D0`` (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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -495,3 +510,17 @@ Legal values for ``S`` are 30, 34, 36, and 39; the def= ault is 30. =20 As with ``x-rme``, the ``x-l0gptsz`` property may be renamed or removed in some future QEMU release. + +PMU CPU Properties +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +``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. diff --git a/target/arm/cpu.c b/target/arm/cpu.c index dde03ffdd6..81b2b83ce2 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -1361,6 +1361,9 @@ static const Property arm_cpu_pmsav7_dregion_property= =3D DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU, pmsav7_dregion, qdev_prop_uint32, uint32_t); +static const Property arm_cpu_pmu_num_counters_property =3D + DEFINE_PROP_UINT32("pmu-num-counters", ARMCPU, pmu_num_counter= s, + UINT32_MAX); #endif =20 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 =3D 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 } =20 /* @@ -2250,6 +2257,18 @@ static void arm_cpu_realizefn(DeviceState *dev, Erro= r **errp) unset_feature(env, ARM_FEATURE_PMU); } if (arm_feature(env, ARM_FEATURE_PMU)) { +#ifndef CONFIG_USER_ONLY + if (cpu->pmu_num_counters !=3D 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 =3D + (cpu->isar.reset_pmcr_el0 & ~PMCRN_MASK) | + (cpu->pmu_num_counters << PMCRN_SHIFT); + } +#endif pmu_init(cpu); =20 if (tcg_enabled() || hvf_enabled()) { diff --git a/target/arm/cpu.h b/target/arm/cpu.h index b2f393f501..990204d7b6 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -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 */ diff --git a/target/arm/kvm.c b/target/arm/kvm.c index df86ad387c..a712c5a1d1 100644 --- a/target/arm/kvm.c +++ b/target/arm/kvm.c @@ -37,6 +37,7 @@ #include "gdbstub/enums.h" #include "hw/core/boards.h" #include "hw/core/irq.h" +#include "hw/core/qdev-properties.h" #include "qapi/visitor.h" #include "qemu/log.h" #include "hw/acpi/acpi.h" @@ -608,6 +609,9 @@ static void kvm_set_psci_version(Object *obj, const cha= r *value, Error **errp) cpu->psci_version =3D PSCI_VERSION(maj, min); } =20 +static const Property arm_cpu_kvm_pmu_partition_property =3D + DEFINE_PROP_BOOL("kvm-pmu-partition", ARMCPU, kvm_pmu_partition, false= ); + /* KVM VCPU properties should be prefixed with "kvm-". */ void kvm_arm_add_vcpu_properties(ARMCPU *cpu) { @@ -624,6 +628,14 @@ void kvm_arm_add_vcpu_properties(ARMCPU *cpu) "will be counted."); } =20 + if (arm_feature(env, ARM_FEATURE_PMU)) { + qdev_property_add_static(DEVICE(obj), + &arm_cpu_kvm_pmu_partition_property); + object_property_set_description(obj, "kvm-pmu-partition", + "Set on to enable KVM PMU counter " + "partitioning."); + } + cpu->kvm_steal_time =3D ON_OFF_AUTO_AUTO; object_property_add_bool(obj, "kvm-steal-time", kvm_steal_time_get, kvm_steal_time_set); @@ -1913,6 +1925,10 @@ static bool kvm_arm_set_device_attr(ARMCPU *cpu, str= uct kvm_device_attr *attr, return true; } =20 +#ifndef KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION +#define KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION 5 +#endif + void kvm_arm_pmu_init(ARMCPU *cpu) { struct kvm_device_attr attr =3D { @@ -1923,6 +1939,34 @@ void kvm_arm_pmu_init(ARMCPU *cpu) if (!cpu->has_pmu) { return; } + + if (cpu->kvm_pmu_partition) { + uint32_t partition =3D 1; + struct kvm_device_attr part_attr =3D { + .group =3D KVM_ARM_VCPU_PMU_V3_CTRL, + .attr =3D KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION, + .addr =3D (intptr_t)&partition, + }; + + if (!kvm_arm_set_device_attr(cpu, &part_attr, "PMU partition")) { + error_report("failed to enable KVM PMU partition"); + abort(); + } + } + + if (cpu->pmu_num_counters !=3D UINT32_MAX) { + struct kvm_device_attr cnt_attr =3D { + .group =3D KVM_ARM_VCPU_PMU_V3_CTRL, + .attr =3D KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS, + .addr =3D (intptr_t)&cpu->pmu_num_counters, + }; + + if (!kvm_arm_set_device_attr(cpu, &cnt_attr, "PMU nr_counters")) { + error_report("failed to set KVM PMU nr_counters"); + abort(); + } + } + if (!kvm_arm_set_device_attr(cpu, &attr, "PMU")) { error_report("failed to init PMU"); abort(); base-commit: 08c9bbb825148dec9669f5184ecf18823bb6d631 --=20 2.56.1.708.g77570a6a1d-goog