ARM64 PMU Partitioning
Subsystems (7)
v5 cover ARM64 PMU Partitioning
This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
The long duration between v4 and v5 is due to time spent on this project being monopolized preparing this feature for internal production. As a result, there are too many improvements to fully list here, but I will cover the notable ones.
v5:
* Rebase onto v6.18-rc7. This required pulling some reorganization patches from Anish and Sean that were dependencies from previous versions based on kvm/queue but never made it to upstream.
* Ensure FGTs (fine-grained traps) are correctly programmed at vCPU load using kvm_vcpu_load_fgt() and helpers introduced by Oliver Upton.
* Cleanly separate concerns of whether the partitioned PMU is enabled for the guest and whether FGT should be enabled. This allows that the capability can be VM-scoped while the implementation detail of whether FGT and context switching are in effect can remain vCPU-scoped.
* Shrink the uAPI change. Instead of a cap and corresponding ioctl, the feature can be controlled by just a cap with an argument. The cap is now also VM-scoped and enforces ordering that it should be decided before vCPUs are created. Whether the cap is enabled is now tracked by the new flag KVM_ARCH_ARM_PARTITIONED_PMU_ENABLED.
* Improve log messages when partitioning in the PMUv3 driver.
* Introduce a global variable armv8pmu_hpmn_max in the PMUv3 driver so KVM code can read if a value was set before the PMU is probed. This is needed to properly test if we have the capability before vCPUs are created.
* Make it possible for a VMM to filter the HPMN0 feature bit.
* Fix event filter problems with PMEVTYPER handling in writethrough_pmevtyper() and kvm_pmu_apply_event_filter() by using kvm_pmu_event_mask() in the right spots. And if an event is filtered, write the physical register with the appropriate exclude bits set but keep the virtual register exactly what the guest wrote.
* Fix register access problems with the PMU register fast path handler by lifting some static PMU access checks from sys_regs.c to use them in the fast path too and make bit masking more strict for better ARM compliance.
* Fix the readability and logic of programming the MDCR_EL2 register when entering the guest. Make sure to set the HPME bit to allow host counters to count guest events. Set TPM and TPMCR by default and clear them if partitioning is enabled rather than the previous inverted logic of leaving them clear and setting them if partitioning is not enabled. Make the HPMN field computation more clear.
* As part of lazy context switching, do a load when the guest is switching to physical access to ensure any previous writes that only reached the virtual registers reach the physical ones as well and are not clobbered by the next vcpu_put().
* Other fixes and improvements that are too small to mention or left out from my personal notes.
v4:
https://lore.kernel.org/kvmarm/[email protected]/
v3:
https://lore.kernel.org/kvm/[email protected]/
v2:
https://lore.kernel.org/kvm/[email protected]/
v1:
https://lore.kernel.org/kvm/[email protected]/
[1] https://gitlab.com/qemu-project/kvm-forum/-/raw/main/_attachments/2025/Optimizing__itvHkhc.pdf
[2] https://www.youtube.com/watch?v=YRzZ8jMIA6M&list=PLW3ep1uCIRfxwmllXTOA2txfDWN6vUOHp&index=9
Anish Ghulati (1):
KVM: arm64: Move arm_{psci,hypercalls}.h to an internal KVM path
Colton Lewis (20):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Introduce method to partition the PMU
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Keep out of guest counter partition
KVM: arm64: Set up FGT for Partitioned PMU
KVM: arm64: Writethrough trapped PMEVTYPER register
KVM: arm64: Use physical PMSELR for PMXEVTYPER if partitioned
KVM: arm64: Writethrough trapped PMOVS register
KVM: arm64: Write fast path PMU register handlers
KVM: arm64: Setup MDCR_EL2 to handle a partitioned PMU
KVM: arm64: Account for partitioning in PMCR_EL0 access
KVM: arm64: Context swap Partitioned PMU guest registers
KVM: arm64: Enforce PMU event filter at vcpu_load()
KVM: arm64: Implement lazy PMU context swaps
perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
KVM: arm64: Inject recorded guest interrupts
KVM: arm64: Add KVM_CAP to partition the PMU
KVM: selftests: Add find_bit to KVM library
KVM: arm64: selftests: Add test case for partitioned PMU
Marc Zyngier (1):
KVM: arm64: Reorganize PMU includes
Sean Christopherson (2):
KVM: arm64: Include KVM headers to get forward declarations
KVM: arm64: Move ARM specific headers in include/kvm to arch directory
Documentation/virt/kvm/api.rst | 24 +
arch/arm/include/asm/arm_pmuv3.h | 28 +
arch/arm64/include/asm/arm_pmuv3.h | 61 +-
.../arm64/include/asm/kvm_arch_timer.h | 2 +
arch/arm64/include/asm/kvm_host.h | 24 +-
.../arm64/include/asm/kvm_pmu.h | 142 ++++
arch/arm64/include/asm/kvm_types.h | 7 +-
.../arm64/include/asm/kvm_vgic.h | 0
arch/arm64/kernel/cpufeature.c | 8 +
arch/arm64/kvm/Makefile | 2 +-
arch/arm64/kvm/arch_timer.c | 5 +-
arch/arm64/kvm/arm.c | 23 +-
{include => arch/arm64}/kvm/arm_hypercalls.h | 0
{include => arch/arm64}/kvm/arm_psci.h | 0
arch/arm64/kvm/config.c | 34 +-
arch/arm64/kvm/debug.c | 31 +-
arch/arm64/kvm/guest.c | 2 +-
arch/arm64/kvm/handle_exit.c | 2 +-
arch/arm64/kvm/hyp/Makefile | 6 +-
arch/arm64/kvm/hyp/include/hyp/switch.h | 211 ++++-
arch/arm64/kvm/hyp/nvhe/switch.c | 4 +-
arch/arm64/kvm/hyp/vhe/switch.c | 4 +-
arch/arm64/kvm/hypercalls.c | 4 +-
arch/arm64/kvm/pmu-direct.c | 464 +++++++++++
arch/arm64/kvm/pmu-emul.c | 678 +---------------
arch/arm64/kvm/pmu.c | 726 ++++++++++++++++++
arch/arm64/kvm/psci.c | 4 +-
arch/arm64/kvm/pvtime.c | 2 +-
arch/arm64/kvm/reset.c | 3 +-
arch/arm64/kvm/sys_regs.c | 110 +--
arch/arm64/kvm/trace_arm.h | 2 +-
arch/arm64/kvm/trng.c | 2 +-
arch/arm64/kvm/vgic/vgic-debug.c | 2 +-
arch/arm64/kvm/vgic/vgic-init.c | 2 +-
arch/arm64/kvm/vgic/vgic-irqfd.c | 2 +-
arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 +-
arch/arm64/kvm/vgic/vgic-mmio-v2.c | 2 +-
arch/arm64/kvm/vgic/vgic-mmio-v3.c | 2 +-
arch/arm64/kvm/vgic/vgic-mmio.c | 4 +-
arch/arm64/kvm/vgic/vgic-v2.c | 2 +-
arch/arm64/kvm/vgic/vgic-v3-nested.c | 3 +-
arch/arm64/kvm/vgic/vgic-v3.c | 2 +-
arch/arm64/kvm/vgic/vgic-v5.c | 2 +-
arch/arm64/tools/cpucaps | 1 +
arch/arm64/tools/sysreg | 6 +-
drivers/perf/arm_pmuv3.c | 137 +++-
include/linux/perf/arm_pmu.h | 1 +
include/linux/perf/arm_pmuv3.h | 14 +-
include/uapi/linux/kvm.h | 1 +
tools/include/uapi/linux/kvm.h | 1 +
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/arm64/vpmu_counter_access.c | 77 +-
tools/testing/selftests/kvm/lib/find_bit.c | 1 +
53 files changed, 2049 insertions(+), 831 deletions(-)
rename include/kvm/arm_arch_timer.h => arch/arm64/include/asm/kvm_arch_timer.h (98%)
rename include/kvm/arm_pmu.h => arch/arm64/include/asm/kvm_pmu.h (61%)
rename include/kvm/arm_vgic.h => arch/arm64/include/asm/kvm_vgic.h (100%)
rename {include => arch/arm64}/kvm/arm_hypercalls.h (100%)
rename {include => arch/arm64}/kvm/arm_psci.h (100%)
create mode 100644 arch/arm64/kvm/pmu-direct.c
create mode 100644 tools/testing/selftests/kvm/lib/find_bit.cbase-commit: ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d --
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 1/24 arm64: cpufeature: Add cpucap for HPMN0
Add a capability for FEAT_HPMN0, whether MDCR_EL2.HPMN can specify 0 counters reserved for the guest.
This required changing HPMN0 to an UnsignedEnum in tools/sysreg because otherwise not all the appropriate macros are generated to add it to arm64_cpu_capabilities_arm64_features.
--- arch/arm64/kernel/cpufeature.c | 8 ++++++++ arch/arm64/kvm/sys_regs.c | 3 ++- arch/arm64/tools/cpucaps | 1 + arch/arm64/tools/sysreg | 6 +++--- 4 files changed, 14 insertions(+), 4 deletions(-)
arch/arm64/kernel/cpufeature.c +8 -0
@@ -555,6 +555,7 @@ static const struct arm64_ftr_bits ftr_id_mmfr0[] = { };static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = { +ARM64_FTR_BITS (FTR_HIDDEN ,FTR_NONSTRICT ,FTR_LOWER_SAFE ,ID_AA64DFR0_EL1_HPMN0_SHIFT ,4 ,0 ),S_ARM64_FTR_BITS (FTR_HIDDEN ,FTR_STRICT ,FTR_LOWER_SAFE , ID_AA64DFR0_EL1_DoubleLock_SHIFT,4 ,0 ),ARM64_FTR_BITS (FTR_HIDDEN ,FTR_NONSTRICT ,FTR_LOWER_SAFE , ID_AA64DFR0_EL1_PMSVer_SHIFT,4 ,0 ),ARM64_FTR_BITS (FTR_HIDDEN ,FTR_STRICT ,FTR_LOWER_SAFE , ID_AA64DFR0_EL1_CTX_CMPs_SHIFT,4 ,0 ),
@@ -2898,6 +2899,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature,ARM64_CPUID_FIELDS (ID_AA64MMFR0_EL1 ,FGT ,FGT2 ) }, + { + .desc ="HPMN0" , + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .capability = ARM64_HAS_HPMN0, + .matches = has_cpuid_feature, +ARM64_CPUID_FIELDS (ID_AA64DFR0_EL1 ,HPMN0 ,IMP ) + }, #ifdef CONFIG_ARM64_SME { .desc = "Scalable Matrix Extension",
arch/arm64/kvm/sys_regs.c +2 -1
@@ -3214,7 +3214,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { ID_AA64DFR0_EL1_DoubleLock_MASK | ID_AA64DFR0_EL1_WRPs_MASK | ID_AA64DFR0_EL1_PMUVer_MASK | - ID_AA64DFR0_EL1_DebugVer_MASK), + ID_AA64DFR0_EL1_DebugVer_MASK | + ID_AA64DFR0_EL1_HPMN0_MASK),ID_SANITISED (ID_AA64DFR1_EL1 ),ID_UNALLOCATED (5 ,2 ), ID_UNALLOCATED(5,3),
arch/arm64/tools/cpucaps +1 -0
@@ -41,6 +41,7 @@ HAS_GICV5_LEGACY HAS_GIC_PRIO_MASKING HAS_GIC_PRIO_RELAXED_SYNC HAS_HCR_NV1 +HAS_HPMN0 HAS_HCX HAS_LDAPR HAS_LPA2
arch/arm64/tools/sysreg +3 -3
@@ -1666,9 +1666,9 @@ EndEnum EndSysreg Sysreg ID_AA64DFR0_EL1 3 0 0 5 0 -Enum 63:60 HPMN0 - 0b0000 UNPREDICTABLE - 0b0001 DEF +UnsignedEnum 63:60 HPMN0 + 0b0000 NI + 0b0001 IMP EndEnum UnsignedEnum 59:56 ExtTrcBuff 0b0000 NI
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Re: [PATCH v5 01/24] arm64: cpufeature: Add cpucap for HPMN0
Quoted 9 lines · Colton Lewis
Add a capability for
FEAT_HPMN0, whether MDCR_EL2.HPMN can specify 0 counters reserved for the guest.This required changing HPMN0 to an UnsignedEnum in
tools/sysregbecause otherwise not all the appropriate macros are generated to add it toarm64_cpu_capabilities_arm64_features.Acked-by Mark Rutland <[email protected]>Signed-off-by Colton Lewis <[email protected]>
Re: [PATCH v5 01/24] arm64: cpufeature: Add cpucap for HPMN0
Quoted 4 lines · Suzuki K Poulose
Quoted 2 lines · Colton Lewis
Add a capability for
FEAT_HPMN0, whether MDCR_EL2.HPMN can specify 0 counters reserved for the guest.
Quoted 8 lines
Quoted 6 lines
This required changing HPMN0 to an UnsignedEnum in
tools/sysregbecause otherwise not all the appropriate macros are generated to add it toarm64_cpu_capabilities_arm64_features.Acked-by Mark Rutland <[email protected]>Signed-off-by Colton Lewis <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>
Thanks Suzuki!
PATCH v5 2/24 KVM: arm64: Move arm_{psci,hypercalls}.h to an internal KVM path
In reply to Colton LewisFrom: Anish Ghulati <[email protected]>
Move arm_hypercalls.h and arm_psci.h into arch/arm64/kvm now that KVM no longer supports 32-bit ARM, i.e. now that there's no reason to make the hypercall and PSCI APIs "public".
Message-ID: <[email protected]>
---
arch/arm64/kvm/arm.c | 5 +++--
{include => arch/arm64}/kvm/arm_hypercalls.h | 0
{include => arch/arm64}/kvm/arm_psci.h | 0
arch/arm64/kvm/guest.c | 2 +-
arch/arm64/kvm/handle_exit.c | 2 +-
arch/arm64/kvm/hyp/Makefile | 6 +++---
arch/arm64/kvm/hyp/include/hyp/switch.h | 4 ++--
arch/arm64/kvm/hyp/nvhe/switch.c | 4 ++--
arch/arm64/kvm/hyp/vhe/switch.c | 4 ++--
arch/arm64/kvm/hypercalls.c | 4 ++--
arch/arm64/kvm/psci.c | 4 ++--
arch/arm64/kvm/pvtime.c | 2 +-
arch/arm64/kvm/trng.c | 2 +-
13 files changed, 20 insertions(+), 19 deletions(-)
rename {include => arch/arm64}/kvm/arm_hypercalls.h (100%)
rename {include => arch/arm64}/kvm/arm_psci.h (100%)arch/arm64/kvm/arm.c +3 -2
@@ -41,9 +41,10 @@#include <asm/kvm_ptrauth.h> #include <asm/sections.h> -#include <kvm/arm_hypercalls.h> #include <kvm/arm_pmu.h> -#include <kvm/arm_psci.h> + +#include "arm_hypercalls.h" +#include "arm_psci.h" #include "sys_regs.h"
arch/arm64/kvm/arm_hypercalls.h renamed from include/kvm/arm_hypercalls.h +0 -0
arch/arm64/kvm/arm_psci.h renamed from include/kvm/arm_psci.h +0 -0
arch/arm64/kvm/guest.c +1 -1
@@ -18,7 +18,6 @@#include <linux/string.h> #include <linux/vmalloc.h> #include <linux/fs.h> -#include <kvm/arm_hypercalls.h> #include <asm/cputype.h> #include <linux/uaccess.h> #include <asm/fpsimd.h>
@@ -27,6 +26,7 @@#include <asm/kvm_nested.h> #include <asm/sigcontext.h> +#include "arm_hypercalls.h" #include "trace.h" const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
arch/arm64/kvm/handle_exit.c +1 -1
@@ -22,7 +22,7 @@#include <asm/stacktrace/nvhe.h> #include <asm/traps.h> -#include <kvm/arm_hypercalls.h> +#include "arm_hypercalls.h" #define CREATE_TRACE_POINTS #include "trace_handle_exit.h"
arch/arm64/kvm/hyp/Makefile +3 -3
@@ -3,8 +3,8 @@ # Makefile for Kernel-based Virtual Machine module, HYP part # -incdir := $(src)/include -subdir-asflags-y := -I$(incdir) -subdir-ccflags-y := -I$(incdir) +hyp_includes := -I$(src)/include -I$(srctree)/arch/arm64/kvm +subdir-asflags-y := $(hyp_includes) +subdir-ccflags-y := $(hyp_includes) obj-$(CONFIG_KVM) += vhe/ nvhe/ pgtable.o
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
@@ -16,8 +16,6 @@#include <linux/jump_label.h> #include <uapi/linux/psci.h> -#include <kvm/arm_psci.h> -#include <asm/barrier.h> #include <asm/cpufeature.h> #include <asm/extable.h>
@@ -32,6 +30,8 @@#include <asm/processor.h> #include <asm/traps.h> +#include "arm_psci.h" +struct kvm_exception_table_entry {int insn, fixup; };
arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
@@ -13,8 +13,6 @@#include <linux/jump_label.h> #include <uapi/linux/psci.h> -#include <kvm/arm_psci.h> -#include <asm/barrier.h> #include <asm/cpufeature.h> #include <asm/kprobes.h>
@@ -28,6 +26,8 @@#include <nvhe/mem_protect.h> +#include "arm_psci.h" +/* Non-VHE specific context */ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
arch/arm64/kvm/hyp/vhe/switch.c +2 -2
@@ -13,8 +13,6 @@#include <linux/percpu.h> #include <uapi/linux/psci.h> -#include <kvm/arm_psci.h> -#include <asm/barrier.h> #include <asm/cpufeature.h> #include <asm/kprobes.h>
@@ -28,6 +26,8 @@#include <asm/thread_info.h> #include <asm/vectors.h> +#include "arm_psci.h" +/* VHE specific context */ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
arch/arm64/kvm/hypercalls.c +2 -2
@@ -6,8 +6,8 @@#include <asm/kvm_emulate.h> -#include <kvm/arm_hypercalls.h> -#include <kvm/arm_psci.h> +#include "arm_hypercalls.h" +#include "arm_psci.h" #define KVM_ARM_SMCCC_STD_FEATURES \ GENMASK(KVM_REG_ARM_STD_BMAP_BIT_COUNT - 1, 0)
arch/arm64/kvm/psci.c +2 -2
@@ -13,8 +13,8 @@#include <asm/cputype.h> #include <asm/kvm_emulate.h> -#include <kvm/arm_psci.h> -#include <kvm/arm_hypercalls.h> +#include "arm_hypercalls.h" +#include "arm_psci.h" /* * This is an implementation of the Power State Coordination Interface
arch/arm64/kvm/pvtime.c +1 -1
@@ -8,7 +8,7 @@#include <asm/kvm_mmu.h> #include <asm/pvclock-abi.h> -#include <kvm/arm_hypercalls.h> +#include "arm_hypercalls.h" void kvm_update_stolen_time (struct kvm_vcpu *vcpu) {
arch/arm64/kvm/trng.c +1 -1
@@ -6,7 +6,7 @@#include <asm/kvm_emulate.h> -#include <kvm/arm_hypercalls.h> +#include "arm_hypercalls.h" #define ARM_SMCCC_TRNG_VERSION_1_0 0x10000UL
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 3/24 KVM: arm64: Include KVM headers to get forward declarations
In reply to Colton LewisFrom: Sean Christopherson <[email protected]>
Include include/uapi/linux/kvm.h and include/linux/kvm_types.h in ARM's public arm_arch_timer.h and arm_pmu.h headers to get forward declarations of things like "struct kvm_vcpu" and "struct kvm_device_attr", which are referenced but never declared (neither file includes *any* KVM headers).
The missing includes don't currently cause problems because of the order of includes in parent files, but that order is largely arbitrary and is subject to change, e.g. a future commit will move the ARM specific headers to arch/arm64/include/asm and reorder parent includes to maintain alphabetic ordering.
Message-ID: <[email protected]>
--- include/kvm/arm_arch_timer.h | 2 ++ include/kvm/arm_pmu.h | 2 ++ 2 files changed, 4 insertions(+)
include/kvm/arm_arch_timer.h +2 -0
@@ -7,6 +7,8 @@#ifndef __ASM_ARM_KVM_ARCH_TIMER_H#define __ASM_ARM_KVM_ARCH_TIMER_H +#include <linux/kvm.h> +#include <linux/kvm_types.h> #include <linux/clocksource.h> #include <linux/hrtimer.h>
include/kvm/arm_pmu.h +2 -0
@@ -7,6 +7,8 @@#ifndef __ASM_ARM_KVM_PMU_H#define __ASM_ARM_KVM_PMU_H +#include <linux/kvm.h> +#include <linux/kvm_types.h> #include <linux/perf_event.h> #include <linux/perf/arm_pmuv3.h>
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 4/24 KVM: arm64: Move ARM specific headers in include/kvm to arch directory
In reply to Colton LewisFrom: Sean Christopherson <[email protected]>
Move kvm/arm_{arch_timer,pmu,vgic}.h to arch/arm64/include/asm and drop the "arm" prefix from all file names. Now that KVM no longer supports 32-bit ARM, there is no reason to expose ARM specific headers to other architectures beyond arm64.
Message-ID: <[email protected]>
--- .../arm64/include/asm/kvm_arch_timer.h | 0 arch/arm64/include/asm/kvm_host.h | 7 +++---- include/kvm/arm_pmu.h => arch/arm64/include/asm/kvm_pmu.h | 0 .../kvm/arm_vgic.h => arch/arm64/include/asm/kvm_vgic.h | 0 arch/arm64/kvm/arch_timer.c | 5 ++--- arch/arm64/kvm/arm.c | 3 +-- arch/arm64/kvm/pmu-emul.c | 4 ++-- arch/arm64/kvm/reset.c | 3 +-- arch/arm64/kvm/trace_arm.h | 2 +- arch/arm64/kvm/vgic/vgic-debug.c | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- arch/arm64/kvm/vgic/vgic-irqfd.c | 2 +- arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 +- arch/arm64/kvm/vgic/vgic-mmio-v2.c | 2 +- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 2 +- arch/arm64/kvm/vgic/vgic-mmio.c | 4 ++-- arch/arm64/kvm/vgic/vgic-v2.c | 2 +- arch/arm64/kvm/vgic/vgic-v3-nested.c | 3 +-- arch/arm64/kvm/vgic/vgic-v3.c | 2 +- arch/arm64/kvm/vgic/vgic-v5.c | 2 +- 20 files changed, 22 insertions(+), 27 deletions(-) rename include/kvm/arm_arch_timer.h => arch/arm64/include/asm/kvm_arch_timer.h (100%) rename include/kvm/arm_pmu.h => arch/arm64/include/asm/kvm_pmu.h (100%) rename include/kvm/arm_vgic.h => arch/arm64/include/asm/kvm_vgic.h (100%)
arch/arm64/include/asm/kvm_arch_timer.h renamed from include/kvm/arm_arch_timer.h +0 -0
arch/arm64/include/asm/kvm_host.h +3 -4
@@ -26,17 +26,16 @@#include <asm/daifflags.h> #include <asm/fpsimd.h> #include <asm/kvm.h> +#include <asm/kvm_arch_timer.h> #include <asm/kvm_asm.h> +#include <asm/kvm_pmu.h> +#include <asm/kvm_vgic.h> #include <asm/vncr_mapping.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED#define KVM_HALT_POLL_NS_DEFAULT 500000 -#include <kvm/arm_vgic.h> -#include <kvm/arm_arch_timer.h> -#include <kvm/arm_pmu.h> -#define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS#define KVM_VCPU_MAX_FEATURES 9
arch/arm64/include/asm/kvm_pmu.h renamed from include/kvm/arm_pmu.h +0 -0
arch/arm64/include/asm/kvm_vgic.h renamed from include/kvm/arm_vgic.h +0 -0
arch/arm64/kvm/arch_timer.c +2 -3
@@ -14,12 +14,11 @@#include <clocksource/arm_arch_timer.h> #include <asm/arch_timer.h> +#include <asm/kvm_arch_timer.h> #include <asm/kvm_emulate.h> #include <asm/kvm_hyp.h> #include <asm/kvm_nested.h> - -#include <kvm/arm_vgic.h> -#include <kvm/arm_arch_timer.h> +#include <asm/kvm_vgic.h> #include "trace.h"
arch/arm64/kvm/arm.c +1 -2
@@ -38,11 +38,10 @@#include <asm/kvm_mmu.h> #include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> +#include <asm/kvm_pmu.h> #include <asm/kvm_ptrauth.h> #include <asm/sections.h> -#include <kvm/arm_pmu.h> -#include "arm_hypercalls.h" #include "arm_psci.h"
arch/arm64/kvm/pmu-emul.c +2 -2
@@ -12,8 +12,8 @@#include <linux/perf/arm_pmu.h> #include <linux/uaccess.h> #include <asm/kvm_emulate.h> -#include <kvm/arm_pmu.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_pmu.h> +#include <asm/kvm_vgic.h> #define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0)
arch/arm64/kvm/reset.c +1 -2
@@ -17,12 +17,11 @@#include <linux/string.h> #include <linux/types.h> -#include <kvm/arm_arch_timer.h> -#include <asm/cpufeature.h> #include <asm/cputype.h> #include <asm/fpsimd.h> #include <asm/ptrace.h> +#include <asm/kvm_arch_timer.h> #include <asm/kvm_arm.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h>
arch/arm64/kvm/trace_arm.h +1 -1
@@ -3,7 +3,7 @@#define _TRACE_ARM_ARM64_KVM_H#include <asm/kvm_emulate.h> -#include <kvm/arm_arch_timer.h> +#include <asm/kvm_arch_timer.h> #include <linux/tracepoint.h> #undef TRACE_SYSTEM
arch/arm64/kvm/vgic/vgic-debug.c +1 -1
@@ -9,7 +9,7 @@#include <linux/interrupt.h> #include <linux/kvm_host.h> #include <linux/seq_file.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include <asm/kvm_mmu.h> #include "vgic.h"
arch/arm64/kvm/vgic/vgic-init.c +1 -1
@@ -7,7 +7,7 @@#include <linux/interrupt.h> #include <linux/cpu.h> #include <linux/kvm_host.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h> #include "vgic.h"
arch/arm64/kvm/vgic/vgic-irqfd.c +1 -1
@@ -6,7 +6,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <trace/events/kvm.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include "vgic.h" /*
arch/arm64/kvm/vgic/vgic-kvm-device.c +1 -1
@@ -7,7 +7,7 @@*/ #include <linux/irqchip/arm-gic-v3.h> #include <linux/kvm_host.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include <linux/uaccess.h> #include <asm/kvm_mmu.h> #include <asm/cputype.h>
arch/arm64/kvm/vgic/vgic-mmio-v2.c +1 -1
@@ -9,7 +9,7 @@#include <linux/nospec.h> #include <kvm/iodev.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include "vgic.h" #include "vgic-mmio.h"
arch/arm64/kvm/vgic/vgic-mmio-v3.c +1 -1
@@ -9,11 +9,11 @@#include <linux/kvm_host.h> #include <linux/interrupt.h> #include <kvm/iodev.h> -#include <kvm/arm_vgic.h> #include <asm/kvm_emulate.h> #include <asm/kvm_arm.h> #include <asm/kvm_mmu.h> +#include <asm/kvm_vgic.h> #include "vgic.h" #include "vgic-mmio.h"
arch/arm64/kvm/vgic/vgic-mmio.c +2 -2
@@ -10,8 +10,8 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <kvm/iodev.h> -#include <kvm/arm_arch_timer.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_arch_timer.h> +#include <asm/kvm_vgic.h> #include "vgic.h" #include "vgic-mmio.h"
arch/arm64/kvm/vgic/vgic-v2.c +1 -1
@@ -6,7 +6,7 @@#include <linux/irqchip/arm-gic.h> #include <linux/kvm.h> #include <linux/kvm_host.h> -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include <asm/kvm_mmu.h> #include "vgic.h"
arch/arm64/kvm/vgic/vgic-v3-nested.c +1 -2
@@ -7,11 +7,10 @@#include <linux/io.h> #include <linux/uaccess.h> -#include <kvm/arm_vgic.h> -#include <asm/kvm_arm.h> #include <asm/kvm_emulate.h> #include <asm/kvm_nested.h> +#include <asm/kvm_vgic.h> #include "vgic.h"
arch/arm64/kvm/vgic/vgic-v3.c +1 -1
@@ -7,10 +7,10 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <linux/string_choices.h> -#include <kvm/arm_vgic.h> #include <asm/kvm_hyp.h> #include <asm/kvm_mmu.h> #include <asm/kvm_asm.h> +#include <asm/kvm_vgic.h> #include "vgic.h"
arch/arm64/kvm/vgic/vgic-v5.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only -#include <kvm/arm_vgic.h> +#include <asm/kvm_vgic.h> #include <linux/irqchip/arm-vgic-info.h> #include "vgic.h"
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 5/24 KVM: arm64: Reorganize PMU includes
In reply to Colton LewisFrom: Marc Zyngier <[email protected]>
Including *all* of asm/kvm_host.h in asm/arm_pmuv3.h is a bad idea because that is much more than arm_pmuv3.h logically needs and creates a circular dependency that makes it easy to introduce compiler errors when editing this code.
asm/kvm_host.h includes asm/kvm_pmu.h includes perf/arm_pmuv3.h includes asm/arm_pmuv3.h includes asm/kvm_host.h
Reorganize the PMU includes to be more sane. In particular:
* Remove the circular dependency by removing the kvm_host.h include from asm/arm_pmuv3.h since 99% of it isn't needed.
* Move the remaining tiny bit of KVM/PMU interface from kvm_host.h into kvm_pmu.h
* Conditionally on ARM64, include the more targeted kvm_pmu.h directly in the arm_pmuv3.c driver.
--- arch/arm64/include/asm/arm_pmuv3.h | 2 -- arch/arm64/include/asm/kvm_host.h | 14 -------------- arch/arm64/include/asm/kvm_pmu.h | 15 +++++++++++++++ drivers/perf/arm_pmuv3.c | 5 +++++ 4 files changed, 20 insertions(+), 16 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +0 -2
@@ -6,8 +6,6 @@#ifndef __ASM_PMUV3_H#define __ASM_PMUV3_H -#include <asm/kvm_host.h> -#include <asm/cpufeature.h> #include <asm/sysreg.h>
arch/arm64/include/asm/kvm_host.h +0 -14
@@ -1410,25 +1410,11 @@ void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu);void kvm_arch_vcpu_ctxsync_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_put_fp (struct kvm_vcpu *vcpu); -static inline bool kvm_pmu_counter_deferred (struct perf_event_attr *attr) -{ -return (!has_vhe () && attr->exclude_host); -} -#ifdef CONFIG_KVM -void kvm_set_pmu_events (u64 set,struct perf_event_attr *attr); -void kvm_clr_pmu_events (u64 clr); -bool kvm_set_pmuserenr (u64 val);void kvm_enable_trbe (void );void kvm_disable_trbe (void );void kvm_tracing_set_el1_configuration (u64 trfcr_while_in_guest);#else -static inline void kvm_set_pmu_events (u64 set,struct perf_event_attr *attr) {} -static inline void kvm_clr_pmu_events (u64 clr) {} -static inline bool kvm_set_pmuserenr (u64 val) -{ -return false; -}static inline void kvm_enable_trbe (void ) {}static inline void kvm_disable_trbe (void ) {}static inline void kvm_tracing_set_el1_configuration (u64 trfcr_while_in_guest) {}
arch/arm64/include/asm/kvm_pmu.h +15 -0
@@ -11,9 +11,15 @@#include <linux/kvm_types.h> #include <linux/perf_event.h> #include <linux/perf/arm_pmuv3.h> +#include <linux/perf/arm_pmu.h> #define KVM_ARMV8_PMU_MAX_COUNTERS 32 +#define kvm_pmu_counter_deferred (attr) \ + ({ \ + !has_vhe() && (attr)->exclude_host; \ + }) + #if IS_ENABLED(CONFIG_HW_PERF_EVENTS) && IS_ENABLED(CONFIG_KVM) struct kvm_pmc { u8 idx; /* index into the pmu->pmc array */
@@ -68,6 +74,9 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu,int kvm_arm_pmu_v3_enable (struct kvm_vcpu *vcpu);struct kvm_pmu_events *kvm_get_pmu_events (void ); +void kvm_set_pmu_events (u64 set,struct perf_event_attr *attr); +void kvm_clr_pmu_events (u64 clr); +bool kvm_set_pmuserenr (u64 val);void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_resync_el0 (void );
@@ -161,6 +170,12 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)#define kvm_vcpu_has_pmu (vcpu) ({ false; })static inline void kvm_pmu_update_vcpu_events (struct kvm_vcpu *vcpu) {} +static inline void kvm_set_pmu_events (u64 set,struct perf_event_attr *attr) {} +static inline void kvm_clr_pmu_events (u64 clr) {} +static inline bool kvm_set_pmuserenr (u64 val) +{ +return false; +}static inline void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_reload_pmu (struct kvm_vcpu *vcpu) {}
drivers/perf/arm_pmuv3.c +5 -0
@@ -9,6 +9,11 @@*/ #include <asm/irq_regs.h> + +#if defined(CONFIG_ARM64 ) +#include <asm/kvm_pmu.h> +#endif +#include <asm/perf_event.h> #include <asm/virt.h>
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 6/24 KVM: arm64: Reorganize PMU functions
In reply to Colton LewisA lot of functions in pmu-emul.c aren't specific to the emulated PMU implementation. Move them to the more appropriate pmu.c file where shared PMU functions should live.
--- arch/arm64/include/asm/kvm_pmu.h | 3 + arch/arm64/kvm/pmu-emul.c | 672 +----------------------------- arch/arm64/kvm/pmu.c | 675 +++++++++++++++++++++++++++++++ 3 files changed, 679 insertions(+), 671 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +3 -0
@@ -51,13 +51,16 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);void kvm_pmu_set_counter_value_user (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);u64 kvm_pmu_implemented_counter_mask (struct kvm_vcpu *vcpu); +u64 kvm_pmu_hyp_counter_mask (struct kvm_vcpu *vcpu);u64 kvm_pmu_accessible_counter_mask (struct kvm_vcpu *vcpu); +u32 kvm_pmu_event_mask (struct kvm *kvm);u64 kvm_pmu_get_pmceid (struct kvm_vcpu *vcpu,bool pmceid1);void kvm_pmu_vcpu_init (struct kvm_vcpu *vcpu);void kvm_pmu_vcpu_destroy (struct kvm_vcpu *vcpu);void kvm_pmu_reprogram_counter_mask (struct kvm_vcpu *vcpu,u64 val);void kvm_pmu_flush_hwstate (struct kvm_vcpu *vcpu);void kvm_pmu_sync_hwstate (struct kvm_vcpu *vcpu); +bool kvm_pmu_overflow_status (struct kvm_vcpu *vcpu);bool kvm_pmu_should_notify_user (struct kvm_vcpu *vcpu);void kvm_pmu_update_run (struct kvm_vcpu *vcpu);void kvm_pmu_software_increment (struct kvm_vcpu *vcpu,u64 val);
arch/arm64/kvm/pmu-emul.c +1 -671
724 lines not on this page · raw message
arch/arm64/kvm/pmu.c +675 -0
686 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 7/24 perf: arm_pmuv3: Introduce method to partition the PMU
In reply to Colton LewisFor PMUv3, the register field MDCR_EL2.HPMN partitiones the PMU counters into two ranges where counters 0..HPMN-1 are accessible by EL1 and, if allowed, EL0 while counters HPMN..N are only accessible by EL2.
Create module parameter reserved_host_counters to reserve a number of counters for the host. This number is set at boot because the perf subsystem assumes the number of counters will not change after the PMU is probed.
Introduce the function armv8pmu_partition() to modify the PMU driver's cntr_mask of available counters to exclude the counters being reserved for the guest and record reserved_guest_counters as the maximum allowable value for HPMN.
Due to the difficulty this feature would create for the driver running in nVHE mode, partitioning is only allowed in VHE mode. In order to support a partitioning on nVHE we'd need to explicitly disable guest counters on every exit and reset HPMN to place all counters in the first range.
--- arch/arm/include/asm/arm_pmuv3.h | 4 ++ arch/arm64/include/asm/arm_pmuv3.h | 5 ++ arch/arm64/include/asm/kvm_pmu.h | 8 +++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-direct.c | 22 +++++++++ drivers/perf/arm_pmuv3.c | 78 +++++++++++++++++++++++++++++- include/linux/perf/arm_pmu.h | 1 + 7 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 arch/arm64/kvm/pmu-direct.c
arch/arm/include/asm/arm_pmuv3.h +4 -0
@@ -221,6 +221,10 @@ static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)return false; } +static inline bool kvm_pmu_partition_supported (void ) +{ +return false; +}static inline bool kvm_set_pmuserenr (u64 val) {return false;
arch/arm64/include/asm/arm_pmuv3.h +5 -0
@@ -171,6 +171,11 @@ static inline bool pmuv3_implemented(int pmuver) pmuver == ID_AA64DFR0_EL1_PMUVer_NI); } +static inline bool is_pmuv3p1 (int pmuver) +{ +return pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P1; +} +static inline bool is_pmuv3p4 (int pmuver) {return pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P4;
arch/arm64/include/asm/kvm_pmu.h +8 -0
@@ -45,7 +45,10 @@ struct arm_pmu_entry {struct arm_pmu *arm_pmu; }; +extern int armv8pmu_hpmn_max; +bool kvm_supports_guest_pmuv3 (void ); +bool kvm_pmu_partition_supported (void );#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);
@@ -115,6 +118,11 @@ static inline bool kvm_supports_guest_pmuv3(void)return false; } +static inline bool kvm_pmu_partition_supported (void ) +{ +return false; +} +#define kvm_arm_pmu_irq_initialized (v) (false)static inline u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx)
arch/arm64/kvm/Makefile +1 -1
@@ -26,7 +26,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \ vgic/vgic-v5.o -kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu-direct.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o
arch/arm64/kvm/pmu-direct.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2025 Google LLC +* Author: Colton Lewis <[email protected]> +*/ + +#include <linux/kvm_host.h> + +#include <asm/kvm_pmu.h> + +/** +* kvm_pmu_partition_supported() - Determine if partitioning is possible +* +* Partitioning is only supported in VHE mode with PMUv3 +* +* Return: True if partitioning is possible, false otherwise +*/ +bool kvm_pmu_partition_supported (void ) +{ +return has_vhe () && +system_supports_pmuv3 (); +}
drivers/perf/arm_pmuv3.c +76 -2
110 lines not on this page · raw message
include/linux/perf/arm_pmu.h +1 -0
@@ -128,6 +128,7 @@ struct arm_pmu {/* Only to be used by ACPI probing code */ unsigned long acpi_cpuid; +int hpmn_max;/* MDCR_EL2.HPMN: counter partition pivot */ };#define to_arm_pmu (p) (container_of(p, struct arm_pmu, pmu))
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 8/24 perf: arm_pmuv3: Generalize counter bitmasks
In reply to Colton LewisThe OVSR bitmasks are valid for enable and interrupt registers as well as overflow registers. Generalize the names.
--- drivers/perf/arm_pmuv3.c | 4 ++-- include/linux/perf/arm_pmuv3.h | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-)
drivers/perf/arm_pmuv3.c +2 -2
@@ -546,7 +546,7 @@ static u64 armv8pmu_pmcr_n_read(void)static int armv8pmu_has_overflowed (u64 pmovsr) { -return !!(pmovsr &ARMV8_PMU_OVERFLOWED_MASK ); +return !!(pmovsr &ARMV8_PMU_CNT_MASK_ALL ); }static int armv8pmu_counter_has_overflowed (u64 pmnc,int idx)
@@ -782,7 +782,7 @@ static u64 armv8pmu_getreset_flags(void) value =read_pmovsclr ();/* Write to clear flags */ - value &=ARMV8_PMU_OVERFLOWED_MASK ; + value &=ARMV8_PMU_CNT_MASK_ALL ;write_pmovsclr (value);return value;
include/linux/perf/arm_pmuv3.h +7 -7
@@ -224,14 +224,14 @@ ARMV8_PMU_PMCR_LC | ARMV8_PMU_PMCR_LP)/* -* PMOVSR: counters overflow flag status reg +* Counter bitmask layouts for overflow, enable, and interrupts */ -#define ARMV8_PMU_OVSR_P GENMASK(30, 0) -#define ARMV8_PMU_OVSR_C BIT(31) -#define ARMV8_PMU_OVSR_F BIT_ULL(32)/* arm64 only */ -/* Mask for writable bits is both P and C fields */ -#define ARMV8_PMU_OVERFLOWED_MASK (ARMV8_PMU_OVSR_P | ARMV8_PMU_OVSR_C | \ - ARMV8_PMU_OVSR_F) +#define ARMV8_PMU_CNT_MASK_P GENMASK(30, 0) +#define ARMV8_PMU_CNT_MASK_C BIT(31) +#define ARMV8_PMU_CNT_MASK_F BIT_ULL(32)/* arm64 only */ +#define ARMV8_PMU_CNT_MASK_ALL (ARMV8_PMU_CNT_MASK_P | \ + ARMV8_PMU_CNT_MASK_C | \ + ARMV8_PMU_CNT_MASK_F)/* * PMXEVTYPER: Event selection reg
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 9/24 perf: arm_pmuv3: Keep out of guest counter partition
In reply to Colton LewisIf the PMU is partitioned, keep the driver out of the guest counter partition and only use the host counter partition.
Define some functions that determine whether the PMU is partitioned and construct mutually exclusive bitmaps for testing which partition a particular counter is in. Note that despite their separate position in the bitmap, the cycle and instruction counters are always in the guest partition.
--- arch/arm/include/asm/arm_pmuv3.h | 18 +++++++ arch/arm64/include/asm/kvm_pmu.h | 24 +++++++++ arch/arm64/kvm/pmu-direct.c | 86 ++++++++++++++++++++++++++++++++ drivers/perf/arm_pmuv3.c | 41 +++++++++++++-- 4 files changed, 165 insertions(+), 4 deletions(-)
arch/arm/include/asm/arm_pmuv3.h +18 -0
@@ -231,6 +231,24 @@ static inline bool kvm_set_pmuserenr(u64 val) }static inline void kvm_vcpu_pmu_resync_el0 (void ) {} +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} + +static inline bool kvm_pmu_is_partitioned (struct arm_pmu *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} +/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0
arch/arm64/include/asm/kvm_pmu.h +24 -0
@@ -90,6 +90,12 @@ void kvm_vcpu_pmu_resync_el0(void);#define kvm_vcpu_has_pmu (vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3)) +bool kvm_pmu_is_partitioned (struct arm_pmu *pmu); +u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu); +u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu); +void kvm_pmu_host_counters_enable (void ); +void kvm_pmu_host_counters_disable (void ); +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -222,6 +228,24 @@ static inline bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idstatic inline void kvm_pmu_nested_transition (struct kvm_vcpu *vcpu) {} +static inline bool kvm_pmu_is_partitioned (void *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (void *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (void *pmu) +{ +return ~0 ; +} + +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} +#endif #endif
arch/arm64/kvm/pmu-direct.c +86 -0
96 lines not on this page · raw message
drivers/perf/arm_pmuv3.c +37 -4
97 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
kernel test robot
build botRe: [PATCH v5 09/24] perf: arm_pmuv3: Keep out of guest counter partition
kernel test robot noticed the following build warnings:
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20251210-055309 base: ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d patch link: https://lore.kernel.org/r/20251209205121.1871534-10-coltonlewis%40google.com patch subject: [PATCH v5 09/24] perf: arm_pmuv3: Keep out of guest counter partition config: arm64-defconfig (https://download.01.org/0day-ci/archive/20251211/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251211/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: arch/arm64/kvm/pmu-direct.c:75 function parameter 'pmu' not described in 'kvm_pmu_guest_counter_mask'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v5 09/24] perf: arm_pmuv3: Keep out of guest counter partition
Quoted 14 lines · kernel test robot
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on
ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d]url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20251210-055309 base: ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d patch link: https://lore.kernel.org/r/20251209205121.1871534-10-coltonlewis%40google.com patch subject: [PATCH v5 09/24] perf: arm_pmuv3: Keep out of guest counter partition config: arm64-defconfig (https://download.01.org/0day-ci/archive/20251211/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251211/[email protected]/reproduce)
Quoted 8 lines
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All warnings (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
Warning:
arch/arm64/kvm/pmu-direct.c:75function parameter 'pmu' not described in 'kvm_pmu_guest_counter_mask'
Just a missing argument description in the comment. Fixed
Quoted 3 lines
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
PATCH v5 10/24 KVM: arm64: Set up FGT for Partitioned PMU
In reply to Colton LewisIn order to gain the best performance benefit from partitioning the PMU, utilize fine grain traps (FEAT_FGT and FEAT_FGT2) to avoid trapping common PMU register accesses by the guest to remove that overhead.
Untrapped:
* PMCR_EL0
* PMUSERENR_EL0
* PMSELR_EL0
* PMCCNTR_EL0
* PMCNTEN_EL0
* PMINTEN_EL1
* PMEVCNTRn_EL0
These are safe to untrap because writing MDCR_EL2.HPMN as this series will do limits the effect of writes to any of these registers to the partition of counters 0..HPMN-1. Reads from these registers will not leak information from between guests as all these registers are context swapped by a later patch in this series. Reads from these registers also do not leak any information about the host's hardware beyond what is promised by PMUv3.
Trapped:
* PMOVS_EL0
* PMEVTYPERn_EL0
* PMCCFILTR_EL0
* PMICNTR_EL0
* PMICFILTR_EL0
* PMCEIDn_EL0
* PMMIR_EL1
PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.
PMICNTR and PMIFILTR remain trapped because KVM is not handling them yet.
PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMCIFILTR are special cases of the same.
PMCEIDn and PMMIR remain trapped because they can leak information specific to the host hardware implementation.
NOTE: This patch temporarily forces kvm_vcpu_pmu_is_partitioned() to be false to prevent partial feature activation for easier debugging.
--- arch/arm64/include/asm/kvm_pmu.h | 33 ++++++++++++++++++++++ arch/arm64/kvm/config.c | 34 ++++++++++++++++++++-- arch/arm64/kvm/pmu-direct.c | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +33 -0
@@ -96,6 +96,23 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +#if !defined(__KVM_NVHE_HYPERVISOR__) +bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu); +bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu); +#else +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} + +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif +u64 kvm_pmu_fgt_bits (void ); +u64 kvm_pmu_fgt2_bits (void ); +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -135,6 +152,22 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, {return 0 ; } +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline u64 kvm_pmu_fgt_bits (void ) +{ +return 0 ; +} +static inline u64 kvm_pmu_fgt2_bits (void ) +{ +return 0 ; +}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
arch/arm64/kvm/config.c +31 -3
@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_pmu.h> #include <asm/kvm_nested.h> #include <asm/sysreg.h>
@@ -1489,12 +1490,39 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu) *vcpu_fgt (vcpu,HFGWTR_EL2 ) |=HFGWTR_EL2_TCR_EL1 ; } +static void __compute_hdfgrtr (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGRTR_EL2 ); + +if (kvm_vcpu_pmu_use_fgt (vcpu)) + *vcpu_fgt (vcpu,HDFGRTR_EL2 ) |=kvm_pmu_fgt_bits (); +} +static void __compute_hdfgwtr (struct kvm_vcpu *vcpu) {__compute_fgt (vcpu,HDFGWTR_EL2 );if (is_hyp_ctxt (vcpu)) *vcpu_fgt (vcpu,HDFGWTR_EL2 ) |=HDFGWTR_EL2_MDSCR_EL1 ; + +if (kvm_vcpu_pmu_use_fgt (vcpu)) + *vcpu_fgt (vcpu,HDFGWTR_EL2 ) |=kvm_pmu_fgt_bits (); +} + +static void __compute_hdfgrtr2 (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGRTR2_EL2 ); + +if (kvm_vcpu_pmu_use_fgt (vcpu)) + *vcpu_fgt (vcpu,HDFGRTR2_EL2 ) |=kvm_pmu_fgt2_bits (); +} + +static void __compute_hdfgwtr2 (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGWTR2_EL2 ); + +if (kvm_vcpu_pmu_use_fgt (vcpu)) + *vcpu_fgt (vcpu,HDFGWTR2_EL2 ) |=kvm_pmu_fgt2_bits (); }void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu)
@@ -1505,7 +1533,7 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HFGRTR_EL2 );__compute_hfgwtr (vcpu);__compute_fgt (vcpu,HFGITR_EL2 ); -__compute_fgt (vcpu,HDFGRTR_EL2 ); +__compute_hdfgrtr (vcpu);__compute_hdfgwtr (vcpu);__compute_fgt (vcpu,HAFGRTR_EL2 );
@@ -1515,6 +1543,6 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HFGRTR2_EL2 );__compute_fgt (vcpu,HFGWTR2_EL2 );__compute_fgt (vcpu,HFGITR2_EL2 ); -__compute_fgt (vcpu,HDFGRTR2_EL2 ); -__compute_fgt (vcpu,HDFGWTR2_EL2 ); +__compute_hdfgrtr2 (vcpu); +__compute_hdfgwtr2 (vcpu); }
arch/arm64/kvm/pmu-direct.c +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 10/24] KVM: arm64: Set up FGT for Partitioned PMU
Hi Colton,
Quoted 118 lines · Colton Lewis
In order to gain the best performance benefit from partitioning the PMU, utilize fine grain traps (
FEAT_FGTandFEAT_FGT2) to avoid trapping common PMU register accesses by the guest to remove that overhead.Untrapped:
*PMCR_EL0
*PMUSERENR_EL0
*PMSELR_EL0
*PMCCNTR_EL0
*PMCNTEN_EL0
*PMINTEN_EL1
*PMEVCNTRn_EL0These are safe to untrap because writing MDCR_EL2.HPMN as this series will do limits the effect of writes to any of these registers to the partition of counters 0..HPMN-1. Reads from these registers will not leak information from between guests as all these registers are context swapped by a later patch in this series. Reads from these registers also do not leak any information about the host's hardware beyond what is promised by PMUv3.
Trapped:
*PMOVS_EL0
*PMEVTYPERn_EL0
*PMCCFILTR_EL0
*PMICNTR_EL0
*PMICFILTR_EL0
*PMCEIDn_EL0
*PMMIR_EL1PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.
PMICNTR and PMIFILTR remain trapped because KVM is not handling them yet.
PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMCIFILTR are special cases of the same.
PMCEIDn and PMMIR remain trapped because they can leak information specific to the host hardware implementation.
NOTE: This patch temporarily forces
kvm_vcpu_pmu_is_partitioned()to be false to prevent partial feature activation for easier debugging.Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_pmu.h | 33 ++++++++++++++++++++++ arch/arm64/kvm/config.c | 34 ++++++++++++++++++++-- arch/arm64/kvm/pmu-direct.c | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pmu.h +33
-0@@ -96,6 +96,23 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +#if !defined(__KVM_NVHE_HYPERVISOR__) +bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu); +bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu); +#else +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} + +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif +u64 kvm_pmu_fgt_bits (void ); +u64 kvm_pmu_fgt2_bits (void ); +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure @@ -135,6 +152,22 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, {return 0 ; } +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline u64 kvm_pmu_fgt_bits (void ) +{ +return 0 ; +} +static inline u64 kvm_pmu_fgt2_bits (void ) +{ +return 0 ; +}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,arch/arm64/kvm/config.c +1
-0@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_pmu.h> #include <asm/kvm_nested.h> #include <asm/sysreg.h>
arch/arm64/kvm/config.c static void __compute_hfgwtr(struct kvm_vcpu *vcpu) · line 1498
Quoted 9 lines
arch/arm64/kvm/config.c +6
-0@@ -1489,12 +1490,39 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu) *vcpu_fgt (vcpu,HFGWTR_EL2 ) |=HFGWTR_EL2_TCR_EL1 ; } +static void __compute_hdfgrtr (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGRTR_EL2 ); + +if (kvm_vcpu_pmu_use_fgt (vcpu)) + *vcpu_fgt (vcpu,HDFGRTR_EL2 ) |=kvm_pmu_fgt_bits ();
Couple of suggestions. I'd rather see this conditioned on kvm_vcpu_pmu_is_partitioned() and get rid of the FGT predicate. After all, kvm_vcpu_load_fgt() already checks for the presence of FEAT_FGT first.
Additionally, I'd prefer that the trap configuration is inline instead of done in a helper in some other file. Centralizing the FGT configuration here was very much intentional.
The other reason for doing this is kvm_pmu_fgt_bits() assumes a 'positive' trap polarity, even though there are several cases where FGTs have a 'negative' priority (i.e. 0 => trap).
Thanks,
Oliver
Re: [PATCH v5 10/24] KVM: arm64: Set up FGT for Partitioned PMU
Hi Oliver. Thanks for the speedy review.
Quoted 127 lines · Oliver Upton
Hi Colton,
Quoted 123 lines · Colton Lewis
In order to gain the best performance benefit from partitioning the PMU, utilize fine grain traps (
FEAT_FGTandFEAT_FGT2) to avoid trapping common PMU register accesses by the guest to remove that overhead.Untrapped:
*PMCR_EL0
*PMUSERENR_EL0
*PMSELR_EL0
*PMCCNTR_EL0
*PMCNTEN_EL0
*PMINTEN_EL1
*PMEVCNTRn_EL0These are safe to untrap because writing MDCR_EL2.HPMN as this series will do limits the effect of writes to any of these registers to the partition of counters 0..HPMN-1. Reads from these registers will not leak information from between guests as all these registers are context swapped by a later patch in this series. Reads from these registers also do not leak any information about the host's hardware beyond what is promised by PMUv3.
Trapped:
*PMOVS_EL0
*PMEVTYPERn_EL0
*PMCCFILTR_EL0
*PMICNTR_EL0
*PMICFILTR_EL0
*PMCEIDn_EL0
*PMMIR_EL1PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.
PMICNTR and PMIFILTR remain trapped because KVM is not handling them yet.
PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMCIFILTR are special cases of the same.
PMCEIDn and PMMIR remain trapped because they can leak information specific to the host hardware implementation.
NOTE: This patch temporarily forces
kvm_vcpu_pmu_is_partitioned()to be false to prevent partial feature activation for easier debugging.Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_pmu.h | 33 ++++++++++++++++++++++ arch/arm64/kvm/config.c | 34 ++++++++++++++++++++-- arch/arm64/kvm/pmu-direct.c | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pmu.h +17
-0@@ -96,6 +96,23 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +#if !defined(__KVM_NVHE_HYPERVISOR__) +bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu); +bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu); +#else +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} + +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif +u64 kvm_pmu_fgt_bits (void ); +u64 kvm_pmu_fgt2_bits (void ); +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
arch/arm64/include/asm/kvm_pmu.h +16
-0@@ -135,6 +152,22 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, {return 0 ; } +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline u64 kvm_pmu_fgt_bits (void ) +{ +return 0 ; +} +static inline u64 kvm_pmu_fgt2_bits (void ) +{ +return 0 ; +}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {}static inline void kvm_pmu_set_counter_value_user(struct
kvm_vcpu*vcpu,arch/arm64/kvm/config.c +1
-0@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_pmu.h> #include <asm/kvm_nested.h> #include <asm/sysreg.h> @@ -1489,12 +1490,39 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu) *vcpu_fgt (vcpu,HFGWTR_EL2 ) |=HFGWTR_EL2_TCR_EL1 ; }
Quoted 11 lines
arch/arm64/kvm/config.c static void __compute_hfgwtr(struct kvm_vcpu *vcpu) · line 1498
Quoted 6 lines
arch/arm64/kvm/config.c +6
-0+static void __compute_hdfgrtr (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGRTR_EL2 ); + +if (kvm_vcpu_pmu_use_fgt (vcpu)) + *vcpu_fgt (vcpu,HDFGRTR_EL2 ) |=kvm_pmu_fgt_bits ();Couple of suggestions. I'd rather see this conditioned on
kvm_vcpu_pmu_is_partitioned()and get rid of the FGT predicate. After all,kvm_vcpu_load_fgt()already checks for the presence ofFEAT_FGTfirst.
I can use that here. I'll still need the use_fgt predicate when programming MDCR_EL2 so I'll introduce in the later patch.
Quoted 3 lines
Additionally, I'd prefer that the trap configuration is inline instead of done in a helper in some other file. Centralizing the FGT configuration here was very much intentional.
Sure I can centralize it.
Quoted 3 lines
The other reason for doing this is
kvm_pmu_fgt_bits()assumes a 'positive' trap polarity, even though there are several cases where FGTs have a 'negative' priority (i.e. 0 => trap).
For the bits I was concerned with they all had positive polarity, except for the dedicated instruction counter. (Side note: Why would ARM do this?)
IIRC the FGT setup I plugged into in previous versions of the patch had some icky macros that accounted for polarity. They were confusing and I didn't like the effort to understand them.
Is there a good reason not to adopt a convetion that 1 => trap for kernel code? Reversing the negative polarities immediately before write could be easy: Have a bitmap of the negative polarity bits to xor with the traps we actually want.
A^0 = A (positive polarity bits)
A^1 = ~A (negative polarity bits)
Quoted 2 lines
Thanks,
Oliver
Oliver Upton
maintainerRe: [PATCH v5 10/24] KVM: arm64: Set up FGT for Partitioned PMU
Quoted 7 lines · Colton Lewis
Quoted 3 lines
The other reason for doing this is
kvm_pmu_fgt_bits()assumes a 'positive' trap polarity, even though there are several cases where FGTs have a 'negative' priority (i.e. 0 => trap).For the bits I was concerned with they all had positive polarity, except for the dedicated instruction counter. (Side note: Why would ARM do this?)
Old software on new hardware, you don't want the guest to magically get access to things it shouldn't.
Quoted 3 lines
IIRC the FGT setup I plugged into in previous versions of the patch had some icky macros that accounted for polarity. They were confusing and I didn't like the effort to understand them.
I'm guessing you're referring to the undef infrastructure (FGUs), which is a meaningfully load-bearing part of KVM.
Quoted 4 lines
Is there a good reason not to adopt a convetion that 1 => trap for kernel code? Reversing the negative polarities immediately before write could be easy: Have a bitmap of the negative polarity bits to xor with the traps we actually want.
This *significantly* muddies the water around FGTs. I quite like that the current representation matches the architecture. NV forces KVM to deal with the native representations anyway.
Thanks,
Oliver
PATCH v5 11/24 KVM: arm64: Writethrough trapped PMEVTYPER register
In reply to Colton LewisWith FGT in place, the remaining trapped registers need to be written through to the underlying physical registers as well as the virtual ones. Failing to do this means guest writes will not take effect when expected.
For the PMEVTYPER register, take care to enforce KVM's PMU event filter. Do that by setting the bits to exclude EL1 and EL0 when an event is not present in the filter and clearing the bit to include EL2 always.
Note the virtual register is always assigned the value specified by the guest to hide the setting of those bits.
--- arch/arm64/kvm/sys_regs.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +33 -1
@@ -1166,6 +1166,36 @@ static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,return true; } +static bool writethrough_pmevtyper (struct kvm_vcpu *vcpu,struct sys_reg_params *p, +u64 reg,u64 idx) +{ +u64 eventsel; +u64 val = p->regval; +u64 evtyper_set =ARMV8_PMU_EXCLUDE_EL0 | +ARMV8_PMU_EXCLUDE_EL1 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; + +__vcpu_assign_sys_reg (vcpu, reg, val); + +if (idx ==ARMV8_PMU_CYCLE_IDX ) + eventsel =ARMV8_PMUV3_PERFCTR_CPU_CYCLES ; +else + eventsel = val &kvm_pmu_event_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (eventsel, vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; + +if (idx ==ARMV8_PMU_CYCLE_IDX ) +write_pmccfiltr (val); +else +write_pmevtypern (idx, val); + +return true; +} +static bool access_pmu_evtyper (struct kvm_vcpu *vcpu,struct sys_reg_params *p,const struct sys_reg_desc *r) {
@@ -1192,7 +1222,9 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p,if (!pmu_counter_idx_valid (vcpu, idx))return false; -if (p->is_write) { +if (kvm_vcpu_pmu_is_partitioned (vcpu) && p->is_write) { +writethrough_pmevtyper (vcpu, p, reg, idx); + }else if (p->is_write) {kvm_pmu_set_counter_event_type (vcpu, p->regval, idx);kvm_vcpu_pmu_restore_guest (vcpu); }else {
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
kernel test robot
build botRe: [PATCH v5 11/24] KVM: arm64: Writethrough trapped PMEVTYPER register
kernel test robot noticed the following build errors:
Hi Colton,
kernel test robot noticed the following build errors:
[auto build test ERROR on ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20251210-055309 base: ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d patch link: https://lore.kernel.org/r/20251209205121.1871534-12-coltonlewis%40google.com patch subject: [PATCH v5 11/24] KVM: arm64: Writethrough trapped PMEVTYPER register config: arm64-randconfig-001-20251210 (https://download.01.org/0day-ci/archive/20251211/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 14.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251211/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
arch/arm64/kvm/sys_regs.c: In function 'writethrough_pmevtyper':
>> arch/arm64/kvm/sys_regs.c:1183:34: error: implicit declaration of function 'kvm_pmu_event_mask'; did you mean 'kvm_pmu_evtyper_mask'? [-Wimplicit-function-declaration]
1183 | eventsel = val & kvm_pmu_event_mask(vcpu->kvm);
| ^~~~~~~~~~~~~~~~~~
| kvm_pmu_evtyper_maskvim +1183 arch/arm64/kvm/sys_regs.c
1168
1169 static bool writethrough_pmevtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
1170 u64 reg, u64 idx)
1171 {
1172 u64 eventsel;
1173 u64 val = p->regval;
1174 u64 evtyper_set = ARMV8_PMU_EXCLUDE_EL0 |
1175 ARMV8_PMU_EXCLUDE_EL1;
1176 u64 evtyper_clr = ARMV8_PMU_INCLUDE_EL2;
1177
1178 __vcpu_assign_sys_reg(vcpu, reg, val);
1179
1180 if (idx == ARMV8_PMU_CYCLE_IDX)
1181 eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
1182 else
> 1183 eventsel = val & kvm_pmu_event_mask(vcpu->kvm);
1184
1185 if (vcpu->kvm->arch.pmu_filter &&
1186 !test_bit(eventsel, vcpu->kvm->arch.pmu_filter))
1187 val |= evtyper_set;
1188
1189 val &= ~evtyper_clr;
1190
1191 if (idx == ARMV8_PMU_CYCLE_IDX)
1192 write_pmccfiltr(val);
1193 else
1194 write_pmevtypern(idx, val);
1195
1196 return true;
1197 }
1198
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v5 11/24] KVM: arm64: Writethrough trapped PMEVTYPER register
Quoted 19 lines · kernel test robot
Hi Colton,
kernel test robot noticed the following build errors:
[auto build test ERROR on
ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d]url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20251210-055309 base: ac3fd01e4c1efce8f2c054cdeb2ddd2fc0fb150d patch link: https://lore.kernel.org/r/20251209205121.1871534-12-coltonlewis%40google.com patch subject: [PATCH v5 11/24] KVM: arm64: Writethrough trapped PMEVTYPER register config: arm64-randconfig-001-20251210 (https://download.01.org/0day-ci/archive/20251211/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 14.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251211/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
Quoted 7 lines
All errors (new ones prefixed by >>):
arch/arm64/kvm/sys_regs.c: In function 'writethrough_pmevtyper':Quoted 1 line
Quoted 1 line
arch/arm64/kvm/sys_regs.c:1183:34: error: implicit declaration of function 'kvm_pmu_event_mask'; did you mean 'kvm_pmu_evtyper_mask'? [-Wimplicit-function-declaration]1183 | eventsel = val & kvm_pmu_event_mask(vcpu->kvm); | ^~~~~~~~~~~~~~~~~~ | kvm_pmu_evtyper_mask
Caused by missing a stub definition for kvm_pmu_event_mask when reorganizing headers in patch 6: Reorganize PMU functions
Fixed
Quoted 38 lines
vim +1183
arch/arm64/kvm/sys_regs.c1168
1169 static bool writethrough_pmevtyper(structkvm_vcpu*vcpu, structsys_reg_params*p,
1170 u64 reg, u64 idx)1171 { 1172 u64 eventsel; 1173 u64 val = p->regval; 1174 u64 evtyper_set = ARMV8_PMU_EXCLUDE_EL0 | 1175 ARMV8_PMU_EXCLUDE_EL1; 1176 u64 evtyper_clr = ARMV8_PMU_INCLUDE_EL2; 1177 1178 __vcpu_assign_sys_reg(vcpu, reg, val); 1179 1180 if (idx == ARMV8_PMU_CYCLE_IDX) 1181 eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES; 1182 elseQuoted 1 line
1183 eventsel = val &
kvm_pmu_event_mask(vcpu->kvm);1184
1185 if (vcpu->kvm->arch.pmu_filter&&
1186 !test_bit(eventsel,vcpu->kvm->arch.pmu_filter))1187 val |= evtyper_set; 1188 1189 val &= ~evtyper_clr; 1190 1191 if (idx == ARMV8_PMU_CYCLE_IDX) 1192 write_pmccfiltr(val); 1193 else 1194 write_pmevtypern(idx, val); 1195 1196 return true; 1197 } 1198--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
PATCH v5 12/24 KVM: arm64: Use physical PMSELR for PMXEVTYPER if partitioned
In reply to Colton LewisBecause PMXEVTYPER is trapped and PMSELR is not, it is not appropriate to use the virtual PMSELR register when it could be outdated and lead to an invalid write. Use the physical register when partitioned.
--- arch/arm64/include/asm/arm_pmuv3.h | 7 ++++++- arch/arm64/kvm/sys_regs.c | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +6 -1
@@ -70,11 +70,16 @@ static inline u64 read_pmcr(void)return read_sysreg (pmcr_el0); } -static inline void write_pmselr (u32 val) +static inline void write_pmselr (u64 val) {write_sysreg (val, pmselr_el0); } +static inline u64 read_pmselr (void ) +{ +return read_sysreg (pmselr_el0); +} +static inline void write_pmccntr (u64 val) {write_sysreg (val, pmccntr_el0);
arch/arm64/kvm/sys_regs.c +7 -2
@@ -1199,14 +1199,19 @@ static bool writethrough_pmevtyper(struct kvm_vcpu *vcpu, struct sys_reg_paramsstatic bool access_pmu_evtyper (struct kvm_vcpu *vcpu,struct sys_reg_params *p,const struct sys_reg_desc *r) { -u64 idx, reg; +u64 idx, reg, pmselr;if (pmu_access_el0_disabled (vcpu))return false;if (r->CRn ==9 && r->CRm ==13 && r->Op2 ==1 ) {/* PMXEVTYPER_EL0 */ - idx =SYS_FIELD_GET (PMSELR_EL0 ,SEL ,__vcpu_sys_reg (vcpu,PMSELR_EL0 )); +if (kvm_vcpu_pmu_is_partitioned (vcpu)) + pmselr =read_pmselr (); +else + pmselr =__vcpu_sys_reg (vcpu,PMSELR_EL0 ); + + idx =SYS_FIELD_GET (PMSELR_EL0 ,SEL , pmselr); reg =PMEVTYPER0_EL0 + idx; }else if (r->CRn ==14 && (r->CRm &12 ) ==12 ) { idx = ((r->CRm &3 ) <<3 ) | (r->Op2 &7 );
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 12/24] KVM: arm64: Use physical PMSELR for PMXEVTYPER if partitioned
Quoted 43 lines · Colton Lewis
Because PMXEVTYPER is trapped and PMSELR is not, it is not appropriate to use the virtual PMSELR register when it could be outdated and lead to an invalid write. Use the physical register when partitioned.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/arm_pmuv3.h | 7 ++++++- arch/arm64/kvm/sys_regs.c | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-)arch/arm64/include/asm/arm_pmuv3.h +6
-1@@ -70,11 +70,16 @@ static inline u64 read_pmcr(void)return read_sysreg (pmcr_el0); } -static inline void write_pmselr (u32 val) +static inline void write_pmselr (u64 val) {write_sysreg (val, pmselr_el0); } +static inline u64 read_pmselr (void ) +{ +return read_sysreg (pmselr_el0); +} +static inline void write_pmccntr (u64 val) {write_sysreg (val, pmccntr_el0);arch/arm64/kvm/sys_regs.c +1
-1@@ -1199,14 +1199,19 @@ static bool writethrough_pmevtyper(struct kvm_vcpu *vcpu, struct sys_reg_paramsstatic bool access_pmu_evtyper (struct kvm_vcpu *vcpu,struct sys_reg_params *p,const struct sys_reg_desc *r) { -u64 idx, reg; +u64 idx, reg, pmselr;
arch/arm64/kvm/sys_regs.c static bool writethrough_pmevtyper(struct kvm_vcpu *vcpu, struct sys_reg_params · line 1212
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +4
-1if (pmu_access_el0_disabled (vcpu))return false;if (r->CRn ==9 && r->CRm ==13 && r->Op2 ==1 ) {/* PMXEVTYPER_EL0 */ - idx =SYS_FIELD_GET (PMSELR_EL0 ,SEL ,__vcpu_sys_reg (vcpu,PMSELR_EL0 )); +if (kvm_vcpu_pmu_is_partitioned (vcpu)) + pmselr =read_pmselr (); +else + pmselr =__vcpu_sys_reg (vcpu,PMSELR_EL0 );
This isn't preemption safe. Nor should the "if (partitioned) do X else do Y" get open-coded throughout the shop.
I would rather this be handled with a prepatory patch that provides generic PMU register accessors to the rest of KVM (e.g. vcpu_read_pmu_reg() / vcpu_write_pmu_reg()). Internally those helpers can locate the vCPU's PMU registers (emulated, partitioned in-memory, partitioned in-CPU).
Thanks,
Oliver
Re: [PATCH v5 12/24] KVM: arm64: Use physical PMSELR for PMXEVTYPER if partitioned
Quoted 49 lines · Oliver Upton
Quoted 47 lines · Colton Lewis
Because PMXEVTYPER is trapped and PMSELR is not, it is not appropriate to use the virtual PMSELR register when it could be outdated and lead to an invalid write. Use the physical register when partitioned.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/arm_pmuv3.h | 7 ++++++- arch/arm64/kvm/sys_regs.c | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-)arch/arm64/include/asm/arm_pmuv3.h +6
-1@@ -70,11 +70,16 @@ static inline u64 read_pmcr(void)return read_sysreg (pmcr_el0); } -static inline void write_pmselr (u32 val) +static inline void write_pmselr (u64 val) {write_sysreg (val, pmselr_el0); } +static inline u64 read_pmselr (void ) +{ +return read_sysreg (pmselr_el0); +} +static inline void write_pmccntr (u64 val) {write_sysreg (val, pmccntr_el0);arch/arm64/kvm/sys_regs.c +0
-0@@ -1199,14 +1199,19 @@ static bool writethrough_pmevtyper(struct
kvm_vcpu*vcpu, structsys_reg_params
static bool access_pmu_evtyper(structkvm_vcpu*vcpu, structsys_reg_params*p,
const structsys_reg_desc*r)
{arch/arm64/kvm/sys_regs.c +1
-1-u64 idx, reg; +u64 idx, reg, pmselr;if (pmu_access_el0_disabled (vcpu))return false;
Quoted 11 lines
arch/arm64/kvm/sys_regs.c static bool writethrough_pmevtyper(struct kvm_vcpu *vcpu, struct sys_reg_params · line 1212
Quoted 8 lines
if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 1) { /* PMXEVTYPER_EL0 */arch/arm64/kvm/sys_regs.c +0
-1- idx = SYS_FIELD_GET(PMSELR_EL0, SEL, __vcpu_sys_reg(vcpu,
PMSELR_EL0));arch/arm64/kvm/sys_regs.c +4
-0+if (kvm_vcpu_pmu_is_partitioned (vcpu)) + pmselr =read_pmselr (); +else + pmselr =__vcpu_sys_reg (vcpu,PMSELR_EL0 );This isn't preemption safe. Nor should the "if (partitioned) do X else do Y" get open-coded throughout the shop.
Okay. I had not known the register access stuff wasn't. I'll fix that.
Quoted 5 lines
I would rather this be handled with a prepatory patch that provides generic PMU register accessors to the rest of KVM (e.g.
vcpu_read_pmu_reg()/vcpu_write_pmu_reg()). Internally those helpers can locate the vCPU's PMU registers (emulated, partitioned in-memory, partitioned in-CPU).
That seems like a good idea.
Quoted 2 lines
Thanks,
Oliver
PATCH v5 13/24 KVM: arm64: Writethrough trapped PMOVS register
In reply to Colton LewisBecause PMOVS remains trapped, it needs to be written through when partitioned to affect PMU hardware when expected.
--- arch/arm64/include/asm/arm_pmuv3.h | 10 ++++++++++ arch/arm64/kvm/sys_regs.c | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-)
arch/arm64/include/asm/arm_pmuv3.h +10 -0
@@ -140,6 +140,16 @@ static inline u64 read_pmicfiltr(void)return read_sysreg_s (SYS_PMICFILTR_EL0 ); } +static inline void write_pmovsset (u64 val) +{ +write_sysreg (val, pmovsset_el0); +} + +static inline u64 read_pmovsset (void ) +{ +return read_sysreg (pmovsset_el0); +} +static inline void write_pmovsclr (u64 val) {write_sysreg (val, pmovsclr_el0);
arch/arm64/kvm/sys_regs.c +16 -1
@@ -1307,6 +1307,19 @@ static bool access_pminten(struct kvm_vcpu *vcpu, struct sys_reg_params *p,return true; } +static void writethrough_pmovs (struct kvm_vcpu *vcpu,struct sys_reg_params *p,bool set) +{ +u64 mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (set) { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, (p->regval & mask)); +write_pmovsset (p->regval & mask); + }else { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, ~(p->regval & mask)); +write_pmovsclr (p->regval & mask); + } +} +static bool access_pmovs (struct kvm_vcpu *vcpu,struct sys_reg_params *p,const struct sys_reg_desc *r) {
@@ -1315,7 +1328,9 @@ static bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p,if (pmu_access_el0_disabled (vcpu))return false; -if (p->is_write) { +if (kvm_vcpu_pmu_is_partitioned (vcpu) && p->is_write) { +writethrough_pmovs (vcpu, p, r->CRm &0x2 ); + }else if (p->is_write) {if (r->CRm &0x2 )/* accessing PMOVSSET_EL0 */ __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, (p->regval & mask));
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 13/24] KVM: arm64: Writethrough trapped PMOVS register
Quoted 39 lines · Colton Lewis
Because PMOVS remains trapped, it needs to be written through when partitioned to affect PMU hardware when expected.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/arm_pmuv3.h | 10 ++++++++++ arch/arm64/kvm/sys_regs.c | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-)arch/arm64/include/asm/arm_pmuv3.h +10
-0@@ -140,6 +140,16 @@ static inline u64 read_pmicfiltr(void)return read_sysreg_s (SYS_PMICFILTR_EL0 ); } +static inline void write_pmovsset (u64 val) +{ +write_sysreg (val, pmovsset_el0); +} + +static inline u64 read_pmovsset (void ) +{ +return read_sysreg (pmovsset_el0); +} +static inline void write_pmovsclr (u64 val) {write_sysreg (val, pmovsclr_el0);arch/arm64/kvm/sys_regs.c +1
-0@@ -1307,6 +1307,19 @@ static bool access_pminten(struct kvm_vcpu *vcpu, struct sys_reg_params *p,return true; } +static void writethrough_pmovs (struct kvm_vcpu *vcpu,struct sys_reg_params *p,bool set)
arch/arm64/kvm/sys_regs.c static bool access_pminten(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 1320
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10
-0+{ +u64 mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (set) { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, (p->regval & mask)); +write_pmovsset (p->regval & mask); + }else { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, ~(p->regval & mask)); +write_pmovsclr (p->regval & mask); + }
There's only ever a single canonical guest view of a register. Either it has been loaded onto the CPU or it is in memory, writing the value to two different locations is odd. What guarantees the guest context is on the CPU currently? And what about preemption?
Thanks,
Oliver
Re: [PATCH v5 13/24] KVM: arm64: Writethrough trapped PMOVS register
Quoted 49 lines · Oliver Upton
Quoted 48 lines · Colton Lewis
Because PMOVS remains trapped, it needs to be written through when partitioned to affect PMU hardware when expected.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/arm_pmuv3.h | 10 ++++++++++ arch/arm64/kvm/sys_regs.c | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-)arch/arm64/include/asm/arm_pmuv3.h +10
-0@@ -140,6 +140,16 @@ static inline u64 read_pmicfiltr(void)return read_sysreg_s (SYS_PMICFILTR_EL0 ); } +static inline void write_pmovsset (u64 val) +{ +write_sysreg (val, pmovsset_el0); +} + +static inline u64 read_pmovsset (void ) +{ +return read_sysreg (pmovsset_el0); +} +static inline void write_pmovsclr (u64 val) {write_sysreg (val, pmovsclr_el0);arch/arm64/kvm/sys_regs.c +8
-0@@ -1307,6 +1307,19 @@ static bool access_pminten(struct kvm_vcpu *vcpu,struct sys_reg_params *p, return true; } +static void writethrough_pmovs (struct kvm_vcpu *vcpu,struct sys_reg_params *p,bool set) +{ +u64 mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (set) { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, (p->regval & mask)); +write_pmovsset (p->regval & mask); + }else {
Quoted 8 lines
arch/arm64/kvm/sys_regs.c static bool access_pminten(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 1320
Quoted 3 lines
arch/arm64/kvm/sys_regs.c +3
-0+ __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, ~(p->regval & mask)); +write_pmovsclr (p->regval & mask); + }There's only ever a single canonical guest view of a register. Either it has been loaded onto the CPU or it is in memory, writing the value to two different locations is odd. What guarantees the guest context is on the CPU currently? And what about preemption?
My thinking here was pmovs is trapped so the "canonical" view is in memory, but the guest still expects it to have an effect immediately.
Otherwise we would have to wait until the next load before the value makes it to hardware. Are you okay with that latency? I'm not sure how well that's going to work. Consider PMEVTYPER as an example. If I don't write it to hardware immediately, a guest may expect a counter to start counting as soon as it is written, but if it's only in memory the counter won't start until the next load.
Echoing discussion on the previous patch, I wasn't aware preemption was possible while servicing these register writes. I'll figure out how to account for that.
PATCH v5 14/24 KVM: arm64: Write fast path PMU register handlers
In reply to Colton LewisWe may want a partitioned PMU but not have FEAT_FGT to untrap the specific registers that would normally be untrapped. Add a handler for those registers in the fast path so we can still get a performance boost from partitioning.
The idea is to handle traps for all the PMU registers quickly by writing through to the hardware as possible instead of hooking into the emulated vPMU as the standard handlers in sys_regs.c do. Since context switching will not happen without FGT, make sure to write both physical and virtual registers so they stay in sync.
To assist with that, fill of the gaps in arm_pmuv3.h for helper functions to read and write PMU registers and lift some of the access checking functions from sys_regs.c to pmu.c
--- arch/arm64/include/asm/arm_pmuv3.h | 37 ++++- arch/arm64/include/asm/kvm_pmu.h | 25 +++ arch/arm64/kvm/hyp/include/hyp/switch.h | 201 ++++++++++++++++++++++++ arch/arm64/kvm/pmu.c | 40 +++++ arch/arm64/kvm/sys_regs.c | 41 +---- 5 files changed, 303 insertions(+), 41 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +36 -1
@@ -39,6 +39,16 @@ static inline unsigned long read_pmevtypern(int n)return 0 ; } +static inline void write_pmxevcntr (u64 val) +{ +write_sysreg (val, pmxevcntr_el0); +} + +static inline u64 read_pmxevcntr (void ) +{ +return read_sysreg (pmxevcntr_el0); +} +static inline unsigned long read_pmmir (void ) {return read_cpuid (PMMIR_EL1 );
@@ -105,21 +115,41 @@ static inline void write_pmcntenset(u64 val)write_sysreg (val, pmcntenset_el0); } +static inline u64 read_pmcntenset (void ) +{ +return read_sysreg (pmcntenset_el0); +} +static inline void write_pmcntenclr (u64 val) {write_sysreg (val, pmcntenclr_el0); } +static inline u64 read_pmcntenclr (void ) +{ +return read_sysreg (pmcntenclr_el0); +} +static inline void write_pmintenset (u64 val) {write_sysreg (val, pmintenset_el1); } +static inline u64 read_pmintenset (void ) +{ +return read_sysreg (pmintenset_el1); +} +static inline void write_pmintenclr (u64 val) {write_sysreg (val, pmintenclr_el1); } +static inline u64 read_pmintenclr (void ) +{ +return read_sysreg (pmintenclr_el1); +} +static inline void write_pmccfiltr (u64 val) {write_sysreg (val, pmccfiltr_el0);
@@ -160,11 +190,16 @@ static inline u64 read_pmovsclr(void)return read_sysreg (pmovsclr_el0); } -static inline void write_pmuserenr (u32 val) +static inline void write_pmuserenr (u64 val) {write_sysreg (val, pmuserenr_el0); } +static inline u64 read_pmuserenr (void ) +{ +return read_sysreg (pmuserenr_el0); +} +static inline void write_pmuacr (u64 val) {write_sysreg_s (val,SYS_PMUACR_EL1 );
arch/arm64/include/asm/kvm_pmu.h +25 -0
@@ -83,6 +83,11 @@ struct kvm_pmu_events *kvm_get_pmu_events(void);void kvm_set_pmu_events (u64 set,struct perf_event_attr *attr);void kvm_clr_pmu_events (u64 clr);bool kvm_set_pmuserenr (u64 val); +bool check_pmu_access_disabled (struct kvm_vcpu *vcpu,u64 flags); +bool pmu_access_el0_disabled (struct kvm_vcpu *vcpu); +bool pmu_access_event_counter_el0_disabled (struct kvm_vcpu *vcpu); +bool pmu_access_cycle_counter_el0_disabled (struct kvm_vcpu *vcpu); +bool pmu_counter_idx_valid (struct kvm_vcpu *vcpu,u64 idx);void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_resync_el0 (void );
@@ -226,6 +231,26 @@ static inline bool kvm_set_pmuserenr(u64 val) {return false; } +static inline bool check_pmu_access_disabled (struct kvm_vcpu *vcpu,u64 flags) +{ +return false; +} +static inline bool pmu_access_el0_disabled (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool pmu_access_event_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool pmu_access_cycle_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool pmu_counter_idx_valid (struct kvm_vcpu *vcpu,u64 idx) +{ +return false; +}static inline void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_reload_pmu (struct kvm_vcpu *vcpu) {}
arch/arm64/kvm/hyp/include/hyp/switch.h +201 -0
225 lines not on this page · raw message
arch/arm64/kvm/pmu.c +40 -0
@@ -884,3 +884,43 @@ u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)return u64_replace_bits (pmcr, n,ARMV8_PMU_PMCR_N ); } + +bool check_pmu_access_disabled (struct kvm_vcpu *vcpu,u64 flags) +{ +u64 reg =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +bool enabled = (reg & flags) ||vcpu_mode_priv (vcpu); + +if (!enabled) +kvm_inject_undefined (vcpu); + +return !enabled; +} + +bool pmu_access_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_EN ); +} + +bool pmu_counter_idx_valid (struct kvm_vcpu *vcpu,u64 idx) +{ +u64 pmcr, val; + + pmcr =kvm_vcpu_read_pmcr (vcpu); + val =FIELD_GET (ARMV8_PMU_PMCR_N , pmcr); +if (idx >= val && idx !=ARMV8_PMU_CYCLE_IDX ) { +kvm_inject_undefined (vcpu); +return false; + } + +return true; +} + +bool pmu_access_cycle_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_CR |ARMV8_PMU_USERENR_EN ); +} + +bool pmu_access_event_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_ER |ARMV8_PMU_USERENR_EN ); +}
arch/arm64/kvm/sys_regs.c +1 -40
64 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 14/24] KVM: arm64: Write fast path PMU register handlers
Quoted 71 lines · Colton Lewis
arch/arm64/include/asm/arm_pmuv3.h +31
-1@@ -39,6 +39,16 @@ static inline unsigned long read_pmevtypern(int n)return 0 ; } +static inline void write_pmxevcntr (u64 val) +{ +write_sysreg (val, pmxevcntr_el0); +} + +static inline u64 read_pmxevcntr (void ) +{ +return read_sysreg (pmxevcntr_el0); +} +static inline unsigned long read_pmmir (void ) {return read_cpuid (PMMIR_EL1 );@@ -105,21 +115,41 @@ static inline void write_pmcntenset(u64 val)write_sysreg (val, pmcntenset_el0); } +static inline u64 read_pmcntenset (void ) +{ +return read_sysreg (pmcntenset_el0); +} +static inline void write_pmcntenclr (u64 val) {write_sysreg (val, pmcntenclr_el0); } +static inline u64 read_pmcntenclr (void ) +{ +return read_sysreg (pmcntenclr_el0); +} +static inline void write_pmintenset (u64 val) {write_sysreg (val, pmintenset_el1); } +static inline u64 read_pmintenset (void ) +{ +return read_sysreg (pmintenset_el1); +} +static inline void write_pmintenclr (u64 val) {write_sysreg (val, pmintenclr_el1); } +static inline u64 read_pmintenclr (void ) +{ +return read_sysreg (pmintenclr_el1); +} +static inline void write_pmccfiltr (u64 val) {write_sysreg (val, pmccfiltr_el0);@@ -160,11 +190,16 @@ static inline u64 read_pmovsclr(void)return read_sysreg (pmovsclr_el0); } -static inline void write_pmuserenr (u32 val) +static inline void write_pmuserenr (u64 val) {write_sysreg (val, pmuserenr_el0);
arch/arm64/include/asm/arm_pmuv3.h static inline u64 read_pmovsclr(void) · line 205
Quoted 10 lines
arch/arm64/include/asm/arm_pmuv3.h +5
-0} +static inline u64 read_pmuserenr (void ) +{ +return read_sysreg (pmuserenr_el0); +} +static inline void write_pmuacr (u64 val) {write_sysreg_s (val,SYS_PMUACR_EL1 );
I wouldn't bother with adding accessors to the PMUv3 driver header, this gets a bit confusing when the 32-bit counterpart lacks matching definitions. Additionally, the part of KVM you're modifying is very much an "internal" part meant to run in a not-quite-kernel context.
Considering this, I'd prefer KVM directly accessed the PMU registers to avoid the possibility of taking some instrumented codepath in the future.
arch/arm64/include/asm/arm_pmuv3.h
Quoted 2 lines
arch/arm64/include/asm/arm_pmuv3.h +2
-0+if (!kvm_vcpu_pmu_is_partitioned (vcpu) + ||pmu_access_el0_disabled (vcpu))
Always put operators on the preceding line for line continuations.
Also, don't call pmu_access_el0_disabled() from here. It can potentially do a full emulated exception entry even though the vCPU is in an extremely inconsistent state (i.e. haven't returned to kernel context yet). Isn't the current value for PMUSERENR_EL0 on the CPU instead of in the vCPU's saved context anyway?
The fast-path accessors really need to be *just* accessors, reading state from the CPU in the unfortunate situation that a TPM trap has been forced upon us.
arch/arm64/include/asm/arm_pmuv3.h
Quoted 12 lines
arch/arm64/include/asm/arm_pmuv3.h +12
-0+case SYS_PMXEVCNTR_EL0 : + idx =FIELD_GET (PMSELR_EL0_SEL ,read_pmselr ()); + +if (pmu_access_event_counter_el0_disabled (vcpu)) +return false; + +if (!pmu_counter_idx_valid (vcpu, idx)) +return false; + + ret =handle_pmu_reg (vcpu, &p,PMEVCNTR0_EL0 + idx, rt, val, + &read_pmxevcntr, &write_pmxevcntr); +break ;
It is a bit odd to handle the muxing for finding the in-memory value yet using the selector-based register for hardware.
Quoted 8 lines
arch/arm64/include/asm/arm_pmuv3.h +8
-0+ case SYS_PMEVCNTRn_EL0(0) ... SYS_PMEVCNTRn_EL0(30): + idx = ((p.CRm &3 ) <<3 ) | (p.Op2 &7 ); + +if (pmu_access_event_counter_el0_disabled (vcpu)) +return false; + +if (!pmu_counter_idx_valid (vcpu, idx)) +return false;
arch/arm64/include/asm/arm_pmuv3.h
Quoted 10 lines
arch/arm64/include/asm/arm_pmuv3.h +10
-0+ +if (p.is_write) { +write_pmevcntrn (idx, val); +__vcpu_assign_sys_reg (vcpu,PMEVCNTR0_EL0 + idx, val); + }else { +vcpu_set_reg (vcpu, rt,read_pmevcntrn (idx)); + } + + ret = true; +break ;
Can't both of these cases share a helper once you've worked out the index? Same for PMEVTYPERn_EL0.
Quoted 14 lines
arch/arm64/include/asm/arm_pmuv3.h +10
-0+default : + ret = false; + } + +if (ret) +__kvm_skip_instr (vcpu); + +return ret; +} +static inline bool kvm_hyp_handle_sysreg (struct kvm_vcpu *vcpu,u64 *exit_code) { if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
arch/arm64/include/asm/arm_pmuv3.h static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code) · line 988
Quoted 6 lines
arch/arm64/include/asm/arm_pmuv3.h +3
-0@@ -785,6 +983,9 @@ static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)if (kvm_handle_cntxct (vcpu))return true; +if (kvm_hyp_handle_pmu_regs (vcpu)) +return true; +
Since the whole partitioned PMU feature is constrained to VHE-only you should call this from kvm_hyp_handle_sysreg_vhe().
Quoted 30 lines
arch/arm64/include/asm/arm_pmuv3.h +30
-0+ +bool check_pmu_access_disabled (struct kvm_vcpu *vcpu,u64 flags) +{ +u64 reg =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +bool enabled = (reg & flags) ||vcpu_mode_priv (vcpu); + +if (!enabled) +kvm_inject_undefined (vcpu); + +return !enabled; +} + +bool pmu_access_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_EN ); +} + +bool pmu_counter_idx_valid (struct kvm_vcpu *vcpu,u64 idx) +{ +u64 pmcr, val; + + pmcr =kvm_vcpu_read_pmcr (vcpu); + val =FIELD_GET (ARMV8_PMU_PMCR_N , pmcr); +if (idx >= val && idx !=ARMV8_PMU_CYCLE_IDX ) { +kvm_inject_undefined (vcpu); +return false; + } + +return true; +}
arch/arm64/include/asm/arm_pmuv3.h static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
Quoted 10 lines
arch/arm64/include/asm/arm_pmuv3.h +10
-0+ +bool pmu_access_cycle_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_CR |ARMV8_PMU_USERENR_EN ); +} + +bool pmu_access_event_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_ER |ARMV8_PMU_USERENR_EN ); +}
Refactorings need to happen in a separate patch.
Thanks,
Oliver
Re: [PATCH v5 14/24] KVM: arm64: Write fast path PMU register handlers
Quoted 88 lines · Oliver Upton
Quoted 71 lines · Colton Lewis
arch/arm64/include/asm/arm_pmuv3.h +31
-1@@ -39,6 +39,16 @@ static inline unsigned long read_pmevtypern(int n)return 0 ; } +static inline void write_pmxevcntr (u64 val) +{ +write_sysreg (val, pmxevcntr_el0); +} + +static inline u64 read_pmxevcntr (void ) +{ +return read_sysreg (pmxevcntr_el0); +} +static inline unsigned long read_pmmir (void ) {return read_cpuid (PMMIR_EL1 );@@ -105,21 +115,41 @@ static inline void write_pmcntenset(u64 val)write_sysreg (val, pmcntenset_el0); } +static inline u64 read_pmcntenset (void ) +{ +return read_sysreg (pmcntenset_el0); +} +static inline void write_pmcntenclr (u64 val) {write_sysreg (val, pmcntenclr_el0); } +static inline u64 read_pmcntenclr (void ) +{ +return read_sysreg (pmcntenclr_el0); +} +static inline void write_pmintenset (u64 val) {write_sysreg (val, pmintenset_el1); } +static inline u64 read_pmintenset (void ) +{ +return read_sysreg (pmintenset_el1); +} +static inline void write_pmintenclr (u64 val) {write_sysreg (val, pmintenclr_el1); } +static inline u64 read_pmintenclr (void ) +{ +return read_sysreg (pmintenclr_el1); +} +static inline void write_pmccfiltr (u64 val) {write_sysreg (val, pmccfiltr_el0);@@ -160,11 +190,16 @@ static inline u64 read_pmovsclr(void)return read_sysreg (pmovsclr_el0); } -static inline void write_pmuserenr (u32 val) +static inline void write_pmuserenr (u64 val) {write_sysreg (val, pmuserenr_el0);arch/arm64/include/asm/arm_pmuv3.h static inline u64 read_pmovsclr(void) · line 205
Quoted 10 lines
arch/arm64/include/asm/arm_pmuv3.h +5
-0} +static inline u64 read_pmuserenr (void ) +{ +return read_sysreg (pmuserenr_el0); +} +static inline void write_pmuacr (u64 val) {write_sysreg_s (val,SYS_PMUACR_EL1 );I wouldn't bother with adding accessors to the PMUv3 driver header, this gets a bit confusing when the 32-bit counterpart lacks matching definitions. Additionally, the part of KVM you're modifying is very much an "internal" part meant to run in a not-quite-kernel context.
Quoted 3 lines
Considering this, I'd prefer KVM directly accessed the PMU registers to avoid the possibility of taking some instrumented codepath in the future.
Will do.
Quoted 11 lines
arch/arm64/include/asm/arm_pmuv3.h
Quoted 2 lines
arch/arm64/include/asm/arm_pmuv3.h +2
-0+if (!kvm_vcpu_pmu_is_partitioned (vcpu) + ||pmu_access_el0_disabled (vcpu))Always put operators on the preceding line for line continuations.
Also, don't call
pmu_access_el0_disabled()from here. It can potentially do a full emulated exception entry even though the vCPU is in an extremely inconsistent state (i.e. haven't returned to kernel context yet). Isn't the current value forPMUSERENR_EL0on the CPU instead of in the vCPU's saved context anyway?
Quoted 3 lines
The fast-path accessors really need to be *just* accessors, reading state from the CPU in the unfortunate situation that a TPM trap has been forced upon us.
Understood.
Quoted 7 lines
Quoted 7 lines
arch/arm64/include/asm/arm_pmuv3.h +7
-0+case SYS_PMXEVCNTR_EL0 : + idx =FIELD_GET (PMSELR_EL0_SEL ,read_pmselr ()); + +if (pmu_access_event_counter_el0_disabled (vcpu)) +return false; + +if (!pmu_counter_idx_valid (vcpu, idx))
Quoted 8 lines
arch/arm64/include/asm/arm_pmuv3.h
Quoted 5 lines
arch/arm64/include/asm/arm_pmuv3.h +5
-0+return false; + + ret =handle_pmu_reg (vcpu, &p,PMEVCNTR0_EL0 + idx, rt, val, + &read_pmxevcntr, &write_pmxevcntr); +break ;It is a bit odd to handle the muxing for finding the in-memory value yet using the selector-based register for hardware.
Agreed
Quoted 13 lines
Quoted 13 lines
arch/arm64/include/asm/arm_pmuv3.h +13
-0+ case SYS_PMEVCNTRn_EL0(0) ... SYS_PMEVCNTRn_EL0(30): + idx = ((p.CRm &3 ) <<3 ) | (p.Op2 &7 ); + +if (pmu_access_event_counter_el0_disabled (vcpu)) +return false; + +if (!pmu_counter_idx_valid (vcpu, idx)) +return false; + +if (p.is_write) { +write_pmevcntrn (idx, val); +__vcpu_assign_sys_reg (vcpu,PMEVCNTR0_EL0 + idx, val); + }else {
Quoted 8 lines
arch/arm64/include/asm/arm_pmuv3.h
Quoted 5 lines
arch/arm64/include/asm/arm_pmuv3.h +5
-0+vcpu_set_reg (vcpu, rt,read_pmevcntrn (idx)); + } + + ret = true; +break ;Can't both of these cases share a helper once you've worked out the index? Same for
PMEVTYPERn_EL0.
Agreed
Quoted 19 lines
Quoted 18 lines
arch/arm64/include/asm/arm_pmuv3.h +10
-0+default : + ret = false; + } + +if (ret) +__kvm_skip_instr (vcpu); + +return ret; +} + static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64
*exit_code)
{
if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM)&&arch/arm64/include/asm/arm_pmuv3.h +0
-0@@ -785,6 +983,9 @@ static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)if (kvm_handle_cntxct (vcpu))return true;
Quoted 6 lines
arch/arm64/include/asm/arm_pmuv3.h static inline bool kvm_hyp_handle_sysreg(struct · line 988
Quoted 3 lines
arch/arm64/include/asm/arm_pmuv3.h +3
-0+if (kvm_hyp_handle_pmu_regs (vcpu)) +return true; +Since the whole partitioned PMU feature is constrained to VHE-only you should call this from
kvm_hyp_handle_sysreg_vhe().
Will do.
Quoted 36 lines
Quoted 36 lines
arch/arm64/include/asm/arm_pmuv3.h +34
-0+ +bool check_pmu_access_disabled (struct kvm_vcpu *vcpu,u64 flags) +{ +u64 reg =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +bool enabled = (reg & flags) ||vcpu_mode_priv (vcpu); + +if (!enabled) +kvm_inject_undefined (vcpu); + +return !enabled; +} + +bool pmu_access_el0_disabled (struct kvm_vcpu *vcpu) +{ +return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_EN ); +} + +bool pmu_counter_idx_valid (struct kvm_vcpu *vcpu,u64 idx) +{ +u64 pmcr, val; + + pmcr =kvm_vcpu_read_pmcr (vcpu); + val =FIELD_GET (ARMV8_PMU_PMCR_N , pmcr); +if (idx >= val && idx !=ARMV8_PMU_CYCLE_IDX ) { +kvm_inject_undefined (vcpu); +return false; + } + +return true; +} + +bool pmu_access_cycle_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ + return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_CR |
ARMV8_PMU_USERENR_EN);
+}
Quoted 8 lines
Quoted 6 lines
arch/arm64/include/asm/arm_pmuv3.h +4
-0+ +bool pmu_access_event_counter_el0_disabled (struct kvm_vcpu *vcpu) +{ + return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_ER |
ARMV8_PMU_USERENR_EN);
+}Refactorings need to happen in a separate patch.
Will do
Quoted 2 lines
Thanks,
Oliver
PATCH v5 15/24 KVM: arm64: Setup MDCR_EL2 to handle a partitioned PMU
In reply to Colton LewisSetup MDCR_EL2 to handle a partitioned PMU. That means calculate an appropriate value for HPMN instead of the default maximum setting the host allows (which implies no partition) so hardware enforces that a guest will only see the counters in the guest partition.
Setting HPMN to a non default value means the global enable bit for the host counters is now MDCR_EL2.HPME instead of the usual PMCR_EL0.E. Enable the HPME bit to allow the host to count guest events. Since HPME only has an effect when HPMN is set which we only do for the guest, it is correct to enable it unconditionally here.
Unset the TPM and TPMCR bits, which trap all PMU accesses, if FGT (fine grain trapping) is being used.
If available, set the filtering bits HPMD and HCCD to be extra sure nothing in the guest counts at EL2.
--- arch/arm64/include/asm/kvm_pmu.h | 11 ++++++ arch/arm64/kvm/debug.c | 29 ++++++++++++-- arch/arm64/kvm/pmu-direct.c | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +11 -0
@@ -101,6 +101,9 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu); +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu); +#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu);
@@ -173,6 +176,14 @@ static inline u64 kvm_pmu_fgt2_bits(void) {return 0 ; } +static inline u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +static inline u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu) +{ +return 0 ; +}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
arch/arm64/kvm/debug.c +26 -3
@@ -36,20 +36,43 @@ static int cpu_has_spe(u64 dfr0)*/ static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) { +int hpmn =kvm_pmu_hpmn (vcpu); +preempt_disable ();/* * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK * to disable guest access to the profiling and trace buffers */ - vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN , - *host_data_ptr (nr_event_counters)); + + vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN , hpmn); vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |MDCR_EL2_TPMS |MDCR_EL2_TTRF |MDCR_EL2_TPMCR |MDCR_EL2_TDRA | -MDCR_EL2_TDOSA ); +MDCR_EL2_TDOSA | +MDCR_EL2_HPME ); + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) { +/* +* Filtering these should be redundant because we trap +* all the TYPER and FILTR registers anyway and ensure +* they filter EL2, but set the bits if they are here. +*/ +if (is_pmuv3p1 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HPMD ; +if (is_pmuv3p5 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HCCD ; + +/* +* Take out the coarse grain traps if we are using +* fine grain traps. +*/ +if (kvm_vcpu_pmu_use_fgt (vcpu)) + vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM |MDCR_EL2_TPMCR ); + + }/* Is the VM being debugged by userspace? */ if (vcpu->guest_debug)
arch/arm64/kvm/pmu-direct.c +65 -0
68 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 15/24] KVM: arm64: Setup MDCR_EL2 to handle a partitioned PMU
Quoted 112 lines · Colton Lewis
Setup
MDCR_EL2to handle a partitioned PMU. That means calculate an appropriate value for HPMN instead of the default maximum setting the host allows (which implies no partition) so hardware enforces that a guest will only see the counters in the guest partition.Setting HPMN to a non default value means the global enable bit for the host counters is now MDCR_EL2.HPME instead of the usual PMCR_EL0.E. Enable the HPME bit to allow the host to count guest events. Since HPME only has an effect when HPMN is set which we only do for the guest, it is correct to enable it unconditionally here.
Unset the TPM and TPMCR bits, which trap all PMU accesses, if FGT (fine grain trapping) is being used.
If available, set the filtering bits HPMD and HCCD to be extra sure nothing in the guest counts at EL2.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_pmu.h | 11 ++++++ arch/arm64/kvm/debug.c | 29 ++++++++++++-- arch/arm64/kvm/pmu-direct.c | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pmu.h +11
-0@@ -101,6 +101,9 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu); +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu); +#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu);@@ -173,6 +176,14 @@ static inline u64 kvm_pmu_fgt2_bits(void) {return 0 ; } +static inline u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +static inline u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu) +{ +return 0 ; +}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,arch/arm64/kvm/debug.c +26
-3@@ -36,20 +36,43 @@ static int cpu_has_spe(u64 dfr0)*/ static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) { +int hpmn =kvm_pmu_hpmn (vcpu); +preempt_disable ();/* * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK * to disable guest access to the profiling and trace buffers */ - vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN , - *host_data_ptr (nr_event_counters)); + + vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN , hpmn); vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |MDCR_EL2_TPMS |MDCR_EL2_TTRF |MDCR_EL2_TPMCR |MDCR_EL2_TDRA | -MDCR_EL2_TDOSA ); +MDCR_EL2_TDOSA | +MDCR_EL2_HPME ); + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) { +/* +* Filtering these should be redundant because we trap +* all the TYPER and FILTR registers anyway and ensure +* they filter EL2, but set the bits if they are here. +*/ +if (is_pmuv3p1 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HPMD ; +if (is_pmuv3p5 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HCCD ; + +/* +* Take out the coarse grain traps if we are using +* fine grain traps. +*/ +if (kvm_vcpu_pmu_use_fgt (vcpu)) + vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM |MDCR_EL2_TPMCR ); + + }/* Is the VM being debugged by userspace? */ if (vcpu->guest_debug)arch/arm64/kvm/pmu-direct.c +0
-0@@ -154,3 +154,68 @@ void kvm_pmu_host_counters_disable(void) mdcr &= ~MDCR_EL2_HPME ;write_sysreg (mdcr, mdcr_el2); }
<snip>
Quoted 54 lines
arch/arm64/kvm/pmu-direct.c +54
-0+/** +* kvm_pmu_guest_num_counters() - Number of counters to show to guest +* @vcpu: Pointer to struct kvm_vcpu +* +* Calculate the number of counters to show to the guest via +* PMCR_EL0.N, making sure to respect the maximum the host allows, +* which is hpmn_max if partitioned and host_max otherwise. +* +* Return: Valid value for PMCR_EL0.N +*/ +u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu) +{ +u8 nr_cnt = vcpu->kvm->arch.nr_pmu_counters; +int hpmn_max = armv8pmu_hpmn_max; +u8 host_max = *host_data_ptr (nr_event_counters); + +if (vcpu->kvm->arch.arm_pmu) + hpmn_max = vcpu->kvm->arch.arm_pmu->hpmn_max; + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) { +if (nr_cnt <= hpmn_max && nr_cnt <= host_max) +return nr_cnt; +if (hpmn_max <= host_max) +return hpmn_max; + } + +if (nr_cnt <= host_max) +return nr_cnt; + +return host_max; +} + +/** +* kvm_pmu_hpmn() - Calculate HPMN field value +* @vcpu: Pointer to struct kvm_vcpu +* +* Calculate the appropriate value to set for MDCR_EL2.HPMN. If +* partitioned, this is the number of counters set for the guest if +* supported, falling back to hpmn_max if needed. If we are not +* partitioned or can't set the implied HPMN value, fall back to the +* host value. +* +* Return: A valid HPMN value +*/ +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu) +{ +u8 nr_guest_cnt =kvm_pmu_guest_num_counters (vcpu); +int nr_guest_cnt_max = armv8pmu_hpmn_max; +u8 nr_host_cnt_max = *host_data_ptr (nr_event_counters); + +if (vcpu->kvm->arch.arm_pmu) + nr_guest_cnt_max = vcpu->kvm->arch.arm_pmu->hpmn_max; + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) {
arch/arm64/kvm/pmu-direct.c void kvm_pmu_host_counters_disable(void) · line 221
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+if (cpus_have_final_cap (ARM64_HAS_HPMN0 )) +return nr_guest_cnt; +else if (nr_guest_cnt >0 ) +return nr_guest_cnt; +else if (nr_guest_cnt_max >0 ) +return nr_guest_cnt_max; + } + +return nr_host_cnt_max; +}
</snip>
I find all of this rather confusing. It seems like you're dealing with sanitizing kvm->arch.nr_pmu_counters vs. the underlying implementation. I'm not sure why you need to do that, I would expect that we reject unsupported values at the time of the ioctl.
The only thing you do need to handle is if the vCPU has migrated to an "unsupported" CPU, for which we already have supporting helpers. I'm too lazy to fetch the Arm ARM and cite architecture but I'm pretty sure an out-of-range HPMN has UNPREDICTABLE behavior.
I think you just need to move the vcpu_set_unsupported_cpu() call earlier in kvm_arch_vcpu_load().
Taking all that into consideration:
u8 kvm_mdcr_hpmn(struct kvm_vcpu *vcpu)
{
u8 nr_counters = *host_data_ptr(nr_event_counters);
if (!kvm_vcpu_pmu_is_partitioned(vcpu) || vcpu_on_unsupported_cpu(vcpu))
return nr_counters;
return vcpu->kvm->arch.nr_pmu_counters;
}Thanks,
Oliver
Re: [PATCH v5 15/24] KVM: arm64: Setup MDCR_EL2 to handle a partitioned PMU
Quoted 183 lines · Oliver Upton
Quoted 112 lines · Colton Lewis
Setup
MDCR_EL2to handle a partitioned PMU. That means calculate an appropriate value for HPMN instead of the default maximum setting the host allows (which implies no partition) so hardware enforces that a guest will only see the counters in the guest partition.Setting HPMN to a non default value means the global enable bit for the host counters is now MDCR_EL2.HPME instead of the usual PMCR_EL0.E. Enable the HPME bit to allow the host to count guest events. Since HPME only has an effect when HPMN is set which we only do for the guest, it is correct to enable it unconditionally here.
Unset the TPM and TPMCR bits, which trap all PMU accesses, if FGT (fine grain trapping) is being used.
If available, set the filtering bits HPMD and HCCD to be extra sure nothing in the guest counts at EL2.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_pmu.h | 11 ++++++ arch/arm64/kvm/debug.c | 29 ++++++++++++-- arch/arm64/kvm/pmu-direct.c | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pmu.h +11
-0@@ -101,6 +101,9 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu); +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu); +#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu);@@ -173,6 +176,14 @@ static inline u64 kvm_pmu_fgt2_bits(void) {return 0 ; } +static inline u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +static inline u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu) +{ +return 0 ; +}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,arch/arm64/kvm/debug.c +26
-3@@ -36,20 +36,43 @@ static int cpu_has_spe(u64 dfr0)*/ static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) { +int hpmn =kvm_pmu_hpmn (vcpu); +preempt_disable ();/* * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK * to disable guest access to the profiling and trace buffers */ - vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN , - *host_data_ptr (nr_event_counters)); + + vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN , hpmn); vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |MDCR_EL2_TPMS |MDCR_EL2_TTRF |MDCR_EL2_TPMCR |MDCR_EL2_TDRA | -MDCR_EL2_TDOSA ); +MDCR_EL2_TDOSA | +MDCR_EL2_HPME ); + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) { +/* +* Filtering these should be redundant because we trap +* all the TYPER and FILTR registers anyway and ensure +* they filter EL2, but set the bits if they are here. +*/ +if (is_pmuv3p1 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HPMD ; +if (is_pmuv3p5 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HCCD ; + +/* +* Take out the coarse grain traps if we are using +* fine grain traps. +*/ +if (kvm_vcpu_pmu_use_fgt (vcpu)) + vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM |MDCR_EL2_TPMCR ); + + }/* Is the VM being debugged by userspace? */ if (vcpu->guest_debug)arch/arm64/kvm/pmu-direct.c +0
-0@@ -154,3 +154,68 @@ void kvm_pmu_host_counters_disable(void) mdcr &= ~MDCR_EL2_HPME ;write_sysreg (mdcr, mdcr_el2); }<snip>
Quoted 54 lines
arch/arm64/kvm/pmu-direct.c +54
-0+/** +* kvm_pmu_guest_num_counters() - Number of counters to show to guest +* @vcpu: Pointer to struct kvm_vcpu +* +* Calculate the number of counters to show to the guest via +* PMCR_EL0.N, making sure to respect the maximum the host allows, +* which is hpmn_max if partitioned and host_max otherwise. +* +* Return: Valid value for PMCR_EL0.N +*/ +u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu) +{ +u8 nr_cnt = vcpu->kvm->arch.nr_pmu_counters; +int hpmn_max = armv8pmu_hpmn_max; +u8 host_max = *host_data_ptr (nr_event_counters); + +if (vcpu->kvm->arch.arm_pmu) + hpmn_max = vcpu->kvm->arch.arm_pmu->hpmn_max; + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) { +if (nr_cnt <= hpmn_max && nr_cnt <= host_max) +return nr_cnt; +if (hpmn_max <= host_max) +return hpmn_max; + } + +if (nr_cnt <= host_max) +return nr_cnt; + +return host_max; +} + +/** +* kvm_pmu_hpmn() - Calculate HPMN field value +* @vcpu: Pointer to struct kvm_vcpu +* +* Calculate the appropriate value to set for MDCR_EL2.HPMN. If +* partitioned, this is the number of counters set for the guest if +* supported, falling back to hpmn_max if needed. If we are not +* partitioned or can't set the implied HPMN value, fall back to the +* host value. +* +* Return: A valid HPMN value +*/ +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu) +{ +u8 nr_guest_cnt =kvm_pmu_guest_num_counters (vcpu); +int nr_guest_cnt_max = armv8pmu_hpmn_max; +u8 nr_host_cnt_max = *host_data_ptr (nr_event_counters); + +if (vcpu->kvm->arch.arm_pmu) + nr_guest_cnt_max = vcpu->kvm->arch.arm_pmu->hpmn_max; + +if (kvm_vcpu_pmu_is_partitioned (vcpu)) {arch/arm64/kvm/pmu-direct.c void kvm_pmu_host_counters_disable(void) · line 221
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+if (cpus_have_final_cap (ARM64_HAS_HPMN0 )) +return nr_guest_cnt; +else if (nr_guest_cnt >0 ) +return nr_guest_cnt; +else if (nr_guest_cnt_max >0 ) +return nr_guest_cnt_max; + } + +return nr_host_cnt_max; +}</snip>
Quoted 4 lines
I find all of this rather confusing. It seems like you're dealing with sanitizing
kvm->arch.nr_pmu_countersvs. the underlying implementation. I'm not sure why you need to do that, I would expect that we reject unsupported values at the time of the ioctl.
I agree it makes more sense to do the validating at the ioctl. I'll do that.
Quoted 4 lines
The only thing you do need to handle is if the vCPU has migrated to an "unsupported" CPU, for which we already have supporting helpers. I'm too lazy to fetch the Arm ARM and cite architecture but I'm pretty sure an out-of-range HPMN has UNPREDICTABLE behavior.
It does.
Quoted 6 lines
I think you just need to move the
vcpu_set_unsupported_cpu()call earlier inkvm_arch_vcpu_load().Taking all that into consideration:
u8
kvm_mdcr_hpmn(struct kvm_vcpu *vcpu)
Quoted 8 lines
{
u8 nr_counters = *host_data_ptr(nr_event_counters);
if (!kvm_vcpu_pmu_is_partitioned(vcpu) || vcpu_on_unsupported_cpu(vcpu))
return nr_counters;
return vcpu->kvm->arch.nr_pmu_counters;
}Something like that will probably work. But I also need to account for if arch.nr_pmu_counters is 0 and we don't have HPMN0, which is also unpredictable. I don't think I can reject that at the ioctl because we still want to support that case. Is there any way to handle that through vcpu_on_unsupported_cpu()?
Quoted 2 lines
Thanks,
Oliver
PATCH v5 16/24 KVM: arm64: Account for partitioning in PMCR_EL0 access
In reply to Colton LewisMake sure reads and writes to PMCR_EL0 conform to additional constraints imposed when the PMU is partitioned.
--- arch/arm64/kvm/pmu.c | 2 +- arch/arm64/kvm/sys_regs.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/pmu.c +1 -1
@@ -877,7 +877,7 @@ u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)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; +u64 n =kvm_pmu_guest_num_counters (vcpu);if (vcpu_has_nv (vcpu) && !vcpu_is_el2 (vcpu)) n =FIELD_GET (MDCR_EL2_HPMN ,__vcpu_sys_reg (vcpu,MDCR_EL2 ));
arch/arm64/kvm/sys_regs.c +1 -1
@@ -1360,7 +1360,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,*/ if (!kvm_vm_has_ran_once (kvm) && !vcpu_has_nv (vcpu) && - new_n <=kvm_arm_pmu_get_max_counters (kvm)) + new_n <=kvm_pmu_hpmn (vcpu)) kvm->arch.nr_pmu_counters = new_n;mutex_unlock (&kvm->arch.config_lock);
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 16/24] KVM: arm64: Account for partitioning in PMCR_EL0 access
Quoted 14 lines · Colton Lewis
Make sure reads and writes to
PMCR_EL0conform to additional constraints imposed when the PMU is partitioned.Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/pmu.c | 2 +- arch/arm64/kvm/sys_regs.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)arch/arm64/kvm/pmu.c +0
-0
arch/arm64/kvm/pmu.c u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu) · line 880
Quoted 5 lines
arch/arm64/kvm/pmu.c +1
-1@@ -877,7 +877,7 @@ u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)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; +u64 n =kvm_pmu_guest_num_counters (vcpu);
Why can't the value of vcpu->kvm->arch.nr_pmu_counters be trusted?
arch/arm64/kvm/pmu.c static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1364
Quoted 7 lines
arch/arm64/kvm/pmu.c +1
-1@@ -1360,7 +1360,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,*/ if (!kvm_vm_has_ran_once (kvm) && !vcpu_has_nv (vcpu) && - new_n <=kvm_arm_pmu_get_max_counters (kvm)) + new_n <=kvm_pmu_hpmn (vcpu)) kvm->arch.nr_pmu_counters = new_n;
This is the legacy UAPI for setting the number of PMU counters by writing to PMCR_EL0.N.
The 'partitioned' implementation should take a dependency on the SET_NR_COUNTERS attribute and reject attempts to change the value of PMCR_EL0.N. Just like nested.
Thanks,
Oliver
Re: [PATCH v5 16/24] KVM: arm64: Account for partitioning in PMCR_EL0 access
Quoted 10 lines · Oliver Upton
Quoted 8 lines · Colton Lewis
Make sure reads and writes to
PMCR_EL0conform to additional constraints imposed when the PMU is partitioned.Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/pmu.c | 2 +- arch/arm64/kvm/sys_regs.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 13 lines
arch/arm64/kvm/pmu.c u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu · line 881
Quoted 11 lines
arch/arm64/kvm/pmu.c +1
-1@@ -877,7 +877,7 @@ u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)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; +u64 n =kvm_pmu_guest_num_counters (vcpu);Why can't the value of
vcpu->kvm->arch.nr_pmu_countersbe trusted?
Similar to discussion on a previous patch, I need to move some validation to the ioctl where it's set.
Quoted 12 lines
arch/arm64/kvm/pmu.c static int set_pmcr(struct kvm_vcpu *vcpu, const · line 1365
Quoted 8 lines
arch/arm64/kvm/pmu.c +1
-1@@ -1360,7 +1360,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, conststruct sys_reg_desc *r, */ if (!kvm_vm_has_ran_once (kvm) && !vcpu_has_nv (vcpu) && - new_n <=kvm_arm_pmu_get_max_counters (kvm)) + new_n <=kvm_pmu_hpmn (vcpu)) kvm->arch.nr_pmu_counters = new_n;This is the legacy UAPI for setting the number of PMU counters by writing to PMCR_EL0.N.
Quoted 3 lines
The 'partitioned' implementation should take a dependency on the
SET_NR_COUNTERSattribute and reject attempts to change the value of PMCR_EL0.N. Just like nested.
Good! I hated making writes to PMCR_EL0.N work and only did it because of selftests assuming it did.
Quoted 2 lines
Thanks,
Oliver
PATCH v5 17/24 KVM: arm64: Context swap Partitioned PMU guest registers
In reply to Colton LewisSave and restore newly untrapped registers that can be directly accessed by the guest when the PMU is partitioned.
* PMEVCNTRn_EL0
* PMCCNTR_EL0
* PMICNTR_EL0
* PMUSERENR_EL0
* PMSELR_EL0
* PMCR_EL0
* PMCNTEN_EL0
* PMINTEN_EL1
If we know we are not using FGT (that is, trapping everything), then return immediately. Either the PMU is not partitioned, or it is but all register writes are being written through the VCPU fields to hardware, so all values are fresh.
Since we are taking over context switching, avoid the writes to PMSELR_EL0 and PMUSERENR_EL0 that would normally occur in __{,de}activate_traps_common()
--- arch/arm64/include/asm/kvm_pmu.h | 4 + arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/hyp/include/hyp/switch.h | 4 +- arch/arm64/kvm/pmu-direct.c | 112 ++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +4 -0
@@ -103,6 +103,8 @@ void kvm_pmu_host_counters_disable(void);u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu);u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu); +void kvm_pmu_load (struct kvm_vcpu *vcpu); +void kvm_pmu_put (struct kvm_vcpu *vcpu);#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);
@@ -184,6 +186,8 @@ static inline u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) {return 0 ; } +static inline void kvm_pmu_load (struct kvm_vcpu *vcpu) {} +static inline void kvm_pmu_put (struct kvm_vcpu *vcpu) {}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
arch/arm64/kvm/arm.c +2 -0
@@ -629,6 +629,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_vhe (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_pmu_load (vcpu);if (kvm_arm_is_pvtime_enabled (&vcpu->arch))kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);
@@ -671,6 +672,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)kvm_timer_vcpu_put (vcpu);kvm_vgic_put (vcpu);kvm_vcpu_pmu_restore_host (vcpu); +kvm_pmu_put (vcpu);if (vcpu_has_nv (vcpu))kvm_vcpu_put_hw_mmu (vcpu);kvm_arm_vmid_clear_active ();
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
@@ -311,7 +311,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)* counter, which could make a PMXEVCNTR_EL0 access UNDEF at * EL1 instead of being trapped to EL2. */ -if (system_supports_pmuv3 ()) { +if (system_supports_pmuv3 () && !kvm_vcpu_pmu_is_partitioned (vcpu)) {write_sysreg (0 , pmselr_el0);ctxt_sys_reg (hctxt,PMUSERENR_EL0 ) =read_sysreg (pmuserenr_el0);
@@ -340,7 +340,7 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)struct kvm_cpu_context *hctxt =host_data_ptr (host_ctxt);write_sysreg (0 , hstr_el2); -if (system_supports_pmuv3 ()) { +if (system_supports_pmuv3 () && !kvm_vcpu_pmu_is_partitioned (vcpu)) {write_sysreg (ctxt_sys_reg (hctxt,PMUSERENR_EL0 ), pmuserenr_el0);vcpu_clear_flag (vcpu,PMUSERENR_ON_CPU ); }
arch/arm64/kvm/pmu-direct.c +112 -0
121 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 17/24] KVM: arm64: Context swap Partitioned PMU guest registers
Quoted 5 lines · Colton Lewis
arch/arm64/kvm/pmu-direct.c +5
-0+/** +* kvm_pmu_load() - Load untrapped PMU registers +* @vcpu: Pointer to struct kvm_vcpu +* +* Load all untrapped PMU registers from the VCPU into the PCPU. Mask
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 238
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+* to only bits belonging to guest-reserved counters and leave +* host-reserved counters alone in bitmask registers. +*/ +void kvm_pmu_load (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu; +u64 mask; +u8 i; +u64 val; +
Assert that preemption is disabled.
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 244
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+/* +* If we aren't using FGT then we are trapping everything +* anyway, so no need to bother with the swap. +*/ +if (!kvm_vcpu_pmu_use_fgt (vcpu)) +return ;
Uhh... Then how do events count in this case?
The absence of FEAT_FGT shouldn't affect the residence of the guest PMU context. We just need to handle the extra traps, ideally by reading the PMU registers directly as a fast path exit handler.
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 257
Quoted 12 lines
arch/arm64/kvm/pmu-direct.c +12
-0+ pmu = vcpu->kvm->arch.arm_pmu; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =__vcpu_sys_reg (vcpu,PMEVCNTR0_EL0 + i); +write_pmevcntrn (i, val); + } + + val =__vcpu_sys_reg (vcpu,PMCCNTR_EL0 ); +write_pmccntr (val); + + val =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +write_pmuserenr (val);
What about the host's value for PMUSERENR?
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 260
Quoted 2 lines
arch/arm64/kvm/pmu-direct.c +2
-0+ val =__vcpu_sys_reg (vcpu,PMSELR_EL0 ); +write_pmselr (val);
PMSELR_EL0 needs to be switched late, e.g. at sysreg_restore_guest_state_vhe(). Even though the host doesn't currently use the selector-based accessor, I'd prefer we not load things that'd affect the host context until we're about to enter the guest.
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+/* Save only the stateful writable bits. */ + val =__vcpu_sys_reg (vcpu,PMCR_EL0 ); + mask =ARMV8_PMU_PMCR_MASK & + ~(ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C ); +write_pmcr (val & mask); + +/* +* When handling these: +* 1. Apply only the bits for guest counters (indicated by mask) +* 2. Use the different registers for set and clear
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 281
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+*/ + mask =kvm_pmu_guest_counter_mask (pmu); + + val =__vcpu_sys_reg (vcpu,PMCNTENSET_EL0 ); +write_pmcntenset (val & mask); +write_pmcntenclr (~val & mask); + + val =__vcpu_sys_reg (vcpu,PMINTENSET_EL1 ); +write_pmintenset (val & mask); +write_pmintenclr (~val & mask);
Is this safe? What happens if we put the PMU into an overflow condition?
Quoted 41 lines
arch/arm64/kvm/pmu-direct.c +41
-0+} + +/** +* kvm_pmu_put() - Put untrapped PMU registers +* @vcpu: Pointer to struct kvm_vcpu +* +* Put all untrapped PMU registers from the VCPU into the PCPU. Mask +* to only bits belonging to guest-reserved counters and leave +* host-reserved counters alone in bitmask registers. +*/ +void kvm_pmu_put (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu; +u64 mask; +u8 i; +u64 val; + +/* +* If we aren't using FGT then we are trapping everything +* anyway, so no need to bother with the swap. +*/ +if (!kvm_vcpu_pmu_use_fgt (vcpu)) +return ; + + pmu = vcpu->kvm->arch.arm_pmu; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =read_pmevcntrn (i); +__vcpu_assign_sys_reg (vcpu,PMEVCNTR0_EL0 + i, val); + } + + val =read_pmccntr (); +__vcpu_assign_sys_reg (vcpu,PMCCNTR_EL0 , val); + + val =read_pmuserenr (); +__vcpu_assign_sys_reg (vcpu,PMUSERENR_EL0 , val); + + val =read_pmselr (); +__vcpu_assign_sys_reg (vcpu,PMSELR_EL0 , val); + + val =read_pmcr ();
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 332
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+__vcpu_assign_sys_reg (vcpu,PMCR_EL0 , val); + +/* Mask these to only save the guest relevant bits. */ + mask =kvm_pmu_guest_counter_mask (pmu); + + val =read_pmcntenset (); +__vcpu_assign_sys_reg (vcpu,PMCNTENSET_EL0 , val & mask); + + val =read_pmintenset (); +__vcpu_assign_sys_reg (vcpu,PMINTENSET_EL1 , val & mask);
What if the PMU is in an overflow state at this point?
Thanks,
Oliver
Re: [PATCH v5 17/24] KVM: arm64: Context swap Partitioned PMU guest registers
Quoted 10 lines · Oliver Upton
Quoted 9 lines · Colton Lewis
arch/arm64/kvm/pmu-direct.c +9
-0+/** +* kvm_pmu_load() - Load untrapped PMU registers +* @vcpu: Pointer to struct kvm_vcpu +* +* Load all untrapped PMU registers from the VCPU into the PCPU. Mask +* to only bits belonging to guest-reserved counters and leave +* host-reserved counters alone in bitmask registers. +*/ +void kvm_pmu_load (struct kvm_vcpu *vcpu)
Quoted 8 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 238
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+{ +struct arm_pmu *pmu; +u64 mask; +u8 i; +u64 val; +Assert that preemption is disabled.
Will do.
Quoted 9 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 244
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+/* +* If we aren't using FGT then we are trapping everything +* anyway, so no need to bother with the swap. +*/ +if (!kvm_vcpu_pmu_use_fgt (vcpu)) +return ;Uhh... Then how do events count in this case?
Quoted 3 lines
The absence of
FEAT_FGTshouldn't affect the residence of the guest PMU context. We just need to handle the extra traps, ideally by reading the PMU registers directly as a fast path exit handler.
Agreed. Yeah I fixed this in my internal backports but looks like I skipped incorperating the fix here.
Quoted 14 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 257
Quoted 12 lines
arch/arm64/kvm/pmu-direct.c +12
-0+ pmu = vcpu->kvm->arch.arm_pmu; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =__vcpu_sys_reg (vcpu,PMEVCNTR0_EL0 + i); +write_pmevcntrn (i, val); + } + + val =__vcpu_sys_reg (vcpu,PMCCNTR_EL0 ); +write_pmccntr (val); + + val =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +write_pmuserenr (val);What about the host's value for PMUSERENR?
Quoted 7 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 260
Quoted 2 lines
arch/arm64/kvm/pmu-direct.c +2
-0+ val =__vcpu_sys_reg (vcpu,PMSELR_EL0 ); +write_pmselr (val);
PMSELR_EL0needs to be switched late, e.g. atsysreg_restore_guest_state_vhe(). Even though the host doesn't currently use the selector-based accessor, I'd prefer we not load things that'd affect the host context until we're about to enter the guest.
There's a spot in __activate_traps_common() where the host value for PMUSERENR is saved and PMSELR is zeroed. I stopped that branch when partitioning because it was clobbering my loaded values, but I can modify instead to handle these things as they should be handled.
Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/pmu-direct.c +14
-0+/* Save only the stateful writable bits. */ + val =__vcpu_sys_reg (vcpu,PMCR_EL0 ); + mask =ARMV8_PMU_PMCR_MASK & + ~(ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C ); +write_pmcr (val & mask); + +/* +* When handling these: +* 1. Apply only the bits for guest counters (indicated by mask) +* 2. Use the different registers for set and clear +*/ + mask =kvm_pmu_guest_counter_mask (pmu); + + val =__vcpu_sys_reg (vcpu,PMCNTENSET_EL0 );
Quoted 8 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 281
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+write_pmcntenset (val & mask); +write_pmcntenclr (~val & mask); + + val =__vcpu_sys_reg (vcpu,PMINTENSET_EL1 ); +write_pmintenset (val & mask); +write_pmintenclr (~val & mask);Is this safe? What happens if we put the PMU into an overflow condition?
It gets handled by the host same as any other PMU interrupt. Though I remember from our conversation you don't want the latency of an additional interrupt so I can handle that here.
Quoted 45 lines
Quoted 45 lines
arch/arm64/kvm/pmu-direct.c +45
-0+} + +/** +* kvm_pmu_put() - Put untrapped PMU registers +* @vcpu: Pointer to struct kvm_vcpu +* +* Put all untrapped PMU registers from the VCPU into the PCPU. Mask +* to only bits belonging to guest-reserved counters and leave +* host-reserved counters alone in bitmask registers. +*/ +void kvm_pmu_put (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu; +u64 mask; +u8 i; +u64 val; + +/* +* If we aren't using FGT then we are trapping everything +* anyway, so no need to bother with the swap. +*/ +if (!kvm_vcpu_pmu_use_fgt (vcpu)) +return ; + + pmu = vcpu->kvm->arch.arm_pmu; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =read_pmevcntrn (i); +__vcpu_assign_sys_reg (vcpu,PMEVCNTR0_EL0 + i, val); + } + + val =read_pmccntr (); +__vcpu_assign_sys_reg (vcpu,PMCCNTR_EL0 , val); + + val =read_pmuserenr (); +__vcpu_assign_sys_reg (vcpu,PMUSERENR_EL0 , val); + + val =read_pmselr (); +__vcpu_assign_sys_reg (vcpu,PMSELR_EL0 , val); + + val =read_pmcr (); +__vcpu_assign_sys_reg (vcpu,PMCR_EL0 , val); + +/* Mask these to only save the guest relevant bits. */ + mask =kvm_pmu_guest_counter_mask (pmu);
Quoted 8 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 332
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+ + val =read_pmcntenset (); +__vcpu_assign_sys_reg (vcpu,PMCNTENSET_EL0 , val & mask); + + val =read_pmintenset (); +__vcpu_assign_sys_reg (vcpu,PMINTENSET_EL1 , val & mask);What if the PMU is in an overflow state at this point?
Is this a separate concern from the point above? It gets loaded back that way and the normal interrupt machinery handles it.
PATCH v5 18/24 KVM: arm64: Enforce PMU event filter at vcpu_load()
In reply to Colton LewisThe KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value. If the event is filtered, exclude counting at all exception levels before writing the hardware.
--- arch/arm64/kvm/pmu-direct.c | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
arch/arm64/kvm/pmu-direct.c +44 -0
@@ -221,6 +221,49 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return nr_host_cnt_max; } +/** +* kvm_pmu_apply_event_filter() +* @vcpu: Pointer to vcpu struct +* +* To uphold the guarantee of the KVM PMU event filter, we must ensure +* no counter counts if the event is filtered. Accomplish this by +* filtering all exception levels if the event is filtered. +*/ +static void kvm_pmu_apply_event_filter (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 evtyper_set =ARMV8_PMU_EXCLUDE_EL0 | +ARMV8_PMU_EXCLUDE_EL1 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; +u8 i; +u64 val; +u64 evsel; + +if (!pmu) +return ; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + i); + evsel = val &kvm_pmu_event_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (evsel, vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmevtypern (i, val); + } + + val =__vcpu_sys_reg (vcpu,PMCCFILTR_EL0 ); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (ARMV8_PMUV3_PERFCTR_CPU_CYCLES , vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmccfiltr (val); +} +/** * kvm_pmu_load() - Load untrapped PMU registers * @vcpu: Pointer to struct kvm_vcpu
@@ -244,6 +287,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)return ; pmu = vcpu->kvm->arch.arm_pmu; +kvm_pmu_apply_event_filter (vcpu);for (i =0 ; i < pmu->hpmn_max; i++) { val =__vcpu_sys_reg (vcpu,PMEVCNTR0_EL0 + i);
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 18/24] KVM: arm64: Enforce PMU event filter at vcpu_load()
Quoted 51 lines · Colton Lewis
The KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value. If the event is filtered, exclude counting at all exception levels before writing the hardware.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/pmu-direct.c | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)arch/arm64/kvm/pmu-direct.c +32
-0@@ -221,6 +221,49 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return nr_host_cnt_max; } +/** +* kvm_pmu_apply_event_filter() +* @vcpu: Pointer to vcpu struct +* +* To uphold the guarantee of the KVM PMU event filter, we must ensure +* no counter counts if the event is filtered. Accomplish this by +* filtering all exception levels if the event is filtered. +*/ +static void kvm_pmu_apply_event_filter (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 evtyper_set =ARMV8_PMU_EXCLUDE_EL0 | +ARMV8_PMU_EXCLUDE_EL1 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; +u8 i; +u64 val; +u64 evsel; + +if (!pmu) +return ; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + i); + evsel = val &kvm_pmu_event_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (evsel, vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmevtypern (i, val); + }
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 265
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+ + val =__vcpu_sys_reg (vcpu,PMCCFILTR_EL0 ); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (ARMV8_PMUV3_PERFCTR_CPU_CYCLES , vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmccfiltr (val); +}
This doesn't work for nested. I agree that the hardware value of PMEVTYPERn_EL0 needs to be under KVM control, but depending on whether or not we're in a hyp context the meaning of the EL1 filtering bit changes. Have a look at kvm_pmu_create_perf_event().
Thanks,
Oliver
Re: [PATCH v5 18/24] KVM: arm64: Enforce PMU event filter at vcpu_load()
Quoted 60 lines · Oliver Upton
Quoted 59 lines · Colton Lewis
The KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value. If the event is filtered, exclude counting at all exception levels before writing the hardware.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/pmu-direct.c | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)arch/arm64/kvm/pmu-direct.c +39
-0@@ -221,6 +221,49 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return nr_host_cnt_max; } +/** +* kvm_pmu_apply_event_filter() +* @vcpu: Pointer to vcpu struct +* +* To uphold the guarantee of the KVM PMU event filter, we must ensure +* no counter counts if the event is filtered. Accomplish this by +* filtering all exception levels if the event is filtered. +*/ +static void kvm_pmu_apply_event_filter (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 evtyper_set =ARMV8_PMU_EXCLUDE_EL0 | +ARMV8_PMU_EXCLUDE_EL1 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; +u8 i; +u64 val; +u64 evsel; + +if (!pmu) +return ; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + i); + evsel = val &kvm_pmu_event_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (evsel, vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmevtypern (i, val); + } + + val =__vcpu_sys_reg (vcpu,PMCCFILTR_EL0 ); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (ARMV8_PMUV3_PERFCTR_CPU_CYCLES , vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; +
Quoted 8 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 265
Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3
-0+ val &= ~evtyper_clr; +write_pmccfiltr (val); +}This doesn't work for nested. I agree that the hardware value of
PMEVTYPERn_EL0needs to be under KVM control, but depending on whether or not we're in a hyp context the meaning of the EL1 filtering bit changes. Have a look atkvm_pmu_create_perf_event().
That's where I got the ideas for this code. I'll look at the handling for hyp context too.
Quoted 2 lines
Thanks,
Oliver
Oliver Upton
maintainerRe: [PATCH v5 18/24] KVM: arm64: Enforce PMU event filter at vcpu_load()
In reply to Colton LewisRe-reading this patch...
Quoted 4 lines · Colton Lewis
The KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value.
Just directly state that this is guarding against userspace programming an unsupported event ID.
Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +4
-0+static void kvm_pmu_apply_event_filter (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; + u64 evtyper_set = ARMV8_PMU_EXCLUDE_EL0 |
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 245
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+ARMV8_PMU_EXCLUDE_EL1 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; +u8 i; +u64 val; +u64 evsel; + +if (!pmu) +return ; + +for (i =0 ; i < pmu->hpmn_max; i++) {
Iterate the bitmask of counters and you'll handle the cycle counter 'for free'.
<snip>
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 254
Quoted 9 lines
arch/arm64/kvm/pmu-direct.c +9
-0+ val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + i); + evsel = val &kvm_pmu_event_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (evsel, vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmevtypern (i, val);
</snip>
This all needs to be shared with writethrough_pmevtyper() instead of open-coding the same thing.
Thanks,
Oliver
Re: [PATCH v5 18/24] KVM: arm64: Enforce PMU event filter at vcpu_load()
Quoted 10 lines · Oliver Upton
Re-reading this patch...
Quoted 4 lines · Colton Lewis
The KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value.
Just directly state that this is guarding against userspace programming an unsupported event ID.
Sure
Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/pmu-direct.c +9
-0+static void kvm_pmu_apply_event_filter (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 evtyper_set =ARMV8_PMU_EXCLUDE_EL0 | +ARMV8_PMU_EXCLUDE_EL1 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; +u8 i; +u64 val; +u64 evsel;
Quoted 8 lines
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 245
Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +5
-0+ +if (!pmu) +return ; + +for (i =0 ; i < pmu->hpmn_max; i++) {Iterate the bitmask of counters and you'll handle the cycle counter 'for free'.
Will do.
Quoted 14 lines
<snip>
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 254
Quoted 9 lines
arch/arm64/kvm/pmu-direct.c +9
-0+ val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + i); + evsel = val &kvm_pmu_event_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (evsel, vcpu->kvm->arch.pmu_filter)) + val |= evtyper_set; + + val &= ~evtyper_clr; +write_pmevtypern (i, val);</snip>
Quoted 2 lines
This all needs to be shared with
writethrough_pmevtyper()instead of open-coding the same thing.
Will do.
Quoted 2 lines
Thanks,
Oliver
PATCH v5 19/24 KVM: arm64: Implement lazy PMU context swaps
In reply to Colton LewisSince many guests will never touch the PMU, they need not pay the cost of context swapping those registers.
Use an enum to implement a simple state machine for PMU register access. The PMU either accesses registers virtually or physically.
Virtual access implies all PMU registers are trapped coarsely by MDCR_EL2.TPM and therefore do not need to be context swapped. Physical access implies some registers are untrapped through FGT and do need to be context swapped. All vCPUs do virtual access by default and transition to physical if the PMU is partitioned and the guest actually tries a PMU access.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_pmu.h | 4 ++++ arch/arm64/include/asm/kvm_types.h | 7 ++++++- arch/arm64/kvm/debug.c | 2 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 2 ++ arch/arm64/kvm/pmu-direct.c | 21 +++++++++++++++++++++ arch/arm64/kvm/pmu.c | 7 +++++++ arch/arm64/kvm/sys_regs.c | 4 ++++ 8 files changed, 46 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1373,6 +1373,7 @@ static inline bool kvm_system_needs_idmapped_vectors(void)return cpus_have_final_cap (ARM64_SPECTRE_V3A ); } +void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu);void kvm_init_host_debug_data (void );void kvm_debug_init_vhe (void );void kvm_vcpu_load_debug (struct kvm_vcpu *vcpu);
arch/arm64/include/asm/kvm_pmu.h +4 -0
@@ -38,6 +38,7 @@ struct kvm_pmu {int irq_num;bool created;bool irq_level; +enum vcpu_pmu_register_access access; };struct arm_pmu_entry {
@@ -106,6 +107,8 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu);void kvm_pmu_load (struct kvm_vcpu *vcpu);void kvm_pmu_put (struct kvm_vcpu *vcpu); +void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu); +#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu);
@@ -188,6 +191,7 @@ static inline u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) }static inline void kvm_pmu_load (struct kvm_vcpu *vcpu) {}static inline void kvm_pmu_put (struct kvm_vcpu *vcpu) {} +static inline void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu) {}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
arch/arm64/include/asm/kvm_types.h +6 -1
@@ -4,5 +4,10 @@#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40 -#endif /* _ASM_ARM64_KVM_TYPES_H */ +enum vcpu_pmu_register_access { +VCPU_PMU_ACCESS_UNSET , +VCPU_PMU_ACCESS_VIRTUAL , +VCPU_PMU_ACCESS_PHYSICAL , +}; +#endif /* _ASM_ARM64_KVM_TYPES_H */
arch/arm64/kvm/debug.c +1 -1
@@ -34,7 +34,7 @@ static int cpu_has_spe(u64 dfr0)* - Self-hosted Trace Filter controls (MDCR_EL2_TTRF) * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB) */ -static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) +void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) {int hpmn =kvm_pmu_hpmn (vcpu);
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -0
@@ -963,6 +963,8 @@ static bool kvm_hyp_handle_pmu_regs(struct kvm_vcpu *vcpu)if (ret)__kvm_skip_instr (vcpu); +kvm_pmu_set_physical_access (vcpu); +return ret; }
arch/arm64/kvm/pmu-direct.c +21 -0
@@ -73,6 +73,7 @@ bool kvm_vcpu_pmu_use_fgt(struct kvm_vcpu *vcpu)u8 hpmn = vcpu->kvm->arch.nr_pmu_counters;return kvm_vcpu_pmu_is_partitioned (vcpu) && + vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_PHYSICAL &&cpus_have_final_cap (ARM64_HAS_FGT ) && (hpmn !=0 ||cpus_have_final_cap (ARM64_HAS_HPMN0 )); }
@@ -92,6 +93,26 @@ u64 kvm_pmu_fgt2_bits(void) | HDFGRTR2_EL2_nPMICNTR_EL0; } +/** +* kvm_pmu_set_physical_access() +* @vcpu: Pointer to vcpu struct +* +* Reconfigure the guest for physical access of PMU hardware if +* allowed. This means reconfiguring mdcr_el2 and loading the vCPU +* state onto hardware. +* +*/ + +void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_pmu_is_partitioned (vcpu) + && vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_VIRTUAL ) { + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_PHYSICAL ; +kvm_arm_setup_mdcr_el2 (vcpu); +kvm_pmu_load (vcpu); + } +} +/** * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters * @pmu: Pointer to arm_pmu struct
arch/arm64/kvm/pmu.c +7 -0
@@ -471,6 +471,12 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)return 0 ; } +static void kvm_pmu_register_init (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_UNSET ) + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_VIRTUAL ; +} +static int kvm_arm_pmu_v3_init (struct kvm_vcpu *vcpu) {if (irqchip_in_kernel (vcpu->kvm)) {
@@ -496,6 +502,7 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)init_irq_work (&vcpu->arch.pmu.overflow_work, kvm_pmu_perf_overflow_notify_vcpu); +kvm_pmu_register_init (vcpu); vcpu->arch.pmu.created = true;return 0 ; }
arch/arm64/kvm/sys_regs.c +4 -0
@@ -1197,6 +1197,8 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, p->regval =__vcpu_sys_reg (vcpu, reg); } +kvm_pmu_set_physical_access (vcpu); +return true; }
@@ -1302,6 +1304,8 @@ static bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p, p->regval =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); } +kvm_pmu_set_physical_access (vcpu); +return true; }
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 19/24] KVM: arm64: Implement lazy PMU context swaps
arch/arm64/include/asm/kvm_types.h line 11
Quoted 5 lines · Colton Lewis
arch/arm64/include/asm/kvm_types.h +5
-0+enum vcpu_pmu_register_access { +VCPU_PMU_ACCESS_UNSET , +VCPU_PMU_ACCESS_VIRTUAL , +VCPU_PMU_ACCESS_PHYSICAL , +};
This is confusing. Even when the guest is accessing registers directly on the CPU I'd still call that "hardware assisted virtualization" and not "physical".
Quoted 51 lines
arch/arm64/include/asm/kvm_types.h +1
-0+#endif /* _ASM_ARM64_KVM_TYPES_H */ arch/arm64/kvm/debug.c +1
-1@@ -34,7 +34,7 @@ static int cpu_has_spe(u64 dfr0)* - Self-hosted Trace Filter controls (MDCR_EL2_TTRF) * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB) */ -static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) +void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) {int hpmn =kvm_pmu_hpmn (vcpu);arch/arm64/kvm/hyp/include/hyp/switch.h +2
-0@@ -963,6 +963,8 @@ static bool kvm_hyp_handle_pmu_regs(struct kvm_vcpu *vcpu)if (ret)__kvm_skip_instr (vcpu); +kvm_pmu_set_physical_access (vcpu); +return ret; }arch/arm64/kvm/pmu-direct.c +9
-0@@ -73,6 +73,7 @@ bool kvm_vcpu_pmu_use_fgt(struct kvm_vcpu *vcpu)u8 hpmn = vcpu->kvm->arch.nr_pmu_counters;return kvm_vcpu_pmu_is_partitioned (vcpu) && + vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_PHYSICAL &&cpus_have_final_cap (ARM64_HAS_FGT ) && (hpmn !=0 ||cpus_have_final_cap (ARM64_HAS_HPMN0 )); }@@ -92,6 +93,26 @@ u64 kvm_pmu_fgt2_bits(void) | HDFGRTR2_EL2_nPMICNTR_EL0; } +/** +* kvm_pmu_set_physical_access() +* @vcpu: Pointer to vcpu struct +* +* Reconfigure the guest for physical access of PMU hardware if +* allowed. This means reconfiguring mdcr_el2 and loading the vCPU +* state onto hardware. +*
arch/arm64/kvm/pmu-direct.c u64 kvm_pmu_fgt2_bits(void) · line 113
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+*/ + +void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_pmu_is_partitioned (vcpu) + && vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_VIRTUAL ) { + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_PHYSICAL ; +kvm_arm_setup_mdcr_el2 (vcpu); +kvm_pmu_load (vcpu); + }
It isn't immediately obvious how this guards against preemption.
Also, the general approach for these context-loading situations is to do a full load/put on the vCPU rather than a directed load.
arch/arm64/kvm/pmu-direct.c u64 kvm_pmu_fgt2_bits(void) · line 117
Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +4
-0+static void kvm_pmu_register_init (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_UNSET ) + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_VIRTUAL ;
This is confusing. The zero value of the enum should be consistent with the "unloaded" state.
Quoted 14 lines
arch/arm64/kvm/sys_regs.c +2
-0@@ -1197,6 +1197,8 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, p->regval =__vcpu_sys_reg (vcpu, reg); } +kvm_pmu_set_physical_access (vcpu); +return true; }
arch/arm64/kvm/sys_regs.c static bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 1310
Quoted 7 lines
arch/arm64/kvm/sys_regs.c +2
-0@@ -1302,6 +1304,8 @@ static bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p, p->regval =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); } +kvm_pmu_set_physical_access (vcpu); +return true; }
Aren't there a ton of other registers the guest may access before these two? Having generic PMU register accessors would allow you to manage residence of PMU registers from a single spot.
Thanks,
Oliver
Re: [PATCH v5 19/24] KVM: arm64: Implement lazy PMU context swaps
Quoted 10 lines · Oliver Upton
arch/arm64/include/asm/kvm_types.h line 11
Quoted 5 lines · Colton Lewis
arch/arm64/include/asm/kvm_types.h +5
-0+enum vcpu_pmu_register_access { +VCPU_PMU_ACCESS_UNSET , +VCPU_PMU_ACCESS_VIRTUAL , +VCPU_PMU_ACCESS_PHYSICAL , +};This is confusing. Even when the guest is accessing registers directly on the CPU I'd still call that "hardware assisted virtualization" and not "physical".
It was what I thought described the access pattern. Do you have another naming suggestion?
Quoted 65 lines
Quoted 52 lines
arch/arm64/include/asm/kvm_types.h +1
-0+#endif /* _ASM_ARM64_KVM_TYPES_H */ arch/arm64/kvm/debug.c +1
-1@@ -34,7 +34,7 @@ static int cpu_has_spe(u64 dfr0)* - Self-hosted Trace Filter controls (MDCR_EL2_TTRF) * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB) */ -static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) +void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) {int hpmn =kvm_pmu_hpmn (vcpu);arch/arm64/kvm/hyp/include/hyp/switch.h +2
-0@@ -963,6 +963,8 @@ static bool kvm_hyp_handle_pmu_regs(struct kvm_vcpu *vcpu)if (ret)__kvm_skip_instr (vcpu); +kvm_pmu_set_physical_access (vcpu); +return ret; }arch/arm64/kvm/pmu-direct.c +9
-0@@ -73,6 +73,7 @@ bool kvm_vcpu_pmu_use_fgt(struct kvm_vcpu *vcpu)u8 hpmn = vcpu->kvm->arch.nr_pmu_counters;return kvm_vcpu_pmu_is_partitioned (vcpu) && + vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_PHYSICAL &&cpus_have_final_cap (ARM64_HAS_FGT ) && (hpmn !=0 ||cpus_have_final_cap (ARM64_HAS_HPMN0 )); }@@ -92,6 +93,26 @@ u64 kvm_pmu_fgt2_bits(void) | HDFGRTR2_EL2_nPMICNTR_EL0; } +/** +* kvm_pmu_set_physical_access() +* @vcpu: Pointer to vcpu struct +* +* Reconfigure the guest for physical access of PMU hardware if +* allowed. This means reconfiguring mdcr_el2 and loading the vCPU +* state onto hardware. +* arch/arm64/kvm/pmu-direct.c u64 kvm_pmu_fgt2_bits(void) · line 113
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+*/ + +void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_pmu_is_partitioned (vcpu) + && vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_VIRTUAL ) { + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_PHYSICAL ; +kvm_arm_setup_mdcr_el2 (vcpu); +kvm_pmu_load (vcpu); + }It isn't immediately obvious how this guards against preemption.
Quoted 2 lines
Also, the general approach for these context-loading situations is to do a full load/put on the vCPU rather than a directed load.
Understood. Will fix.
Quoted 7 lines
arch/arm64/kvm/pmu-direct.c u64 kvm_pmu_fgt2_bits(void) · line 117
Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +4
-0+static void kvm_pmu_register_init (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_UNSET ) + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_VIRTUAL ;This is confusing. The zero value of the enum should be consistent with the "unloaded" state.
That's the way I initially wrote it but it had a problem on a different kernel. I forget the exact issue, but I never saw the problem on upstream so I'm happy to go back to it.
Quoted 19 lines
Quoted 18 lines
arch/arm64/kvm/sys_regs.c +2
-0@@ -1197,6 +1197,8 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, p->regval =__vcpu_sys_reg (vcpu, reg); } +kvm_pmu_set_physical_access (vcpu); +return true; }@@ -1302,6 +1304,8 @@ static bool access_pmovs(struct kvm_vcpu *vcpu,struct sys_reg_params *p, p->regval = __vcpu_sys_reg(vcpu, PMOVSSET_EL0); }
Quoted 8 lines
arch/arm64/kvm/sys_regs.c static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 1203
Quoted 4 lines
arch/arm64/kvm/sys_regs.c +2
-0+kvm_pmu_set_physical_access (vcpu); +return true; }Aren't there a ton of other registers the guest may access before these two? Having generic PMU register accessors would allow you to manage residence of PMU registers from a single spot.
Yes but these are the only two that use the old trap handlers. I also call set_physical_access from my fast path handler that handles all the other registers when partitioned.
Agree on having some generic accessors which you mention in an earlier patch.
Oliver Upton
maintainerRe: [PATCH v5 19/24] KVM: arm64: Implement lazy PMU context swaps
Quoted 13 lines · Colton Lewis
Quoted 10 lines · Oliver Upton
arch/arm64/include/asm/kvm_types.h line 11
Quoted 5 lines · Colton Lewis
arch/arm64/include/asm/kvm_types.h +5
-0+enum vcpu_pmu_register_access { +VCPU_PMU_ACCESS_UNSET , +VCPU_PMU_ACCESS_VIRTUAL , +VCPU_PMU_ACCESS_PHYSICAL , +};This is confusing. Even when the guest is accessing registers directly on the CPU I'd still call that "hardware assisted virtualization" and not "physical".
Quoted 2 lines
It was what I thought described the access pattern. Do you have another naming suggestion?
PMU_STATE_FREE,
PMU_STATE_GUEST_OWNED,
Quoted 9 lines
Quoted 8 lines
arch/arm64/kvm/sys_regs.c static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 1203
Quoted 4 lines
arch/arm64/kvm/sys_regs.c +2
-0+kvm_pmu_set_physical_access (vcpu); +return true; }Aren't there a ton of other registers the guest may access before these two? Having generic PMU register accessors would allow you to manage residence of PMU registers from a single spot.
Quoted 3 lines
Yes but these are the only two that use the old trap handlers. I also call
set_physical_accessfrom my fast path handler that handles all the other registers when partitioned.
The fast path accessors should only be accessing state already loaded on the CPU. If the guest's PMU context isn't loaded on the CPU then it should return to a kernel context and do a full put/load on the vCPU.
I'm not seeing how this all fits together but for lazy loading to work correctly you need to evaluate the state of the vPMU at vcpu_load(). If there exists an enabled PMC, set PMU_STATE_GUEST_OWNED and load it upfront.
Otherwise, default to PMU_STATE_FREE until the next register access and this whole thing resets when the vCPU is scheduled out. I had suggested to you a while back that you should follow a similar model to the debug registers, this is how they behave.
Thanks,
Oliver
PATCH v5 20/24 perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
In reply to Colton LewisBecause ARM hardware is not yet capable of direct interrupt injection into guests, guest counters will still trigger interrupts that need to be handled by the host PMU interrupt handler. Clear the overflow flags in hardware to handle the interrupt as normal, but record which guest overflow flags were set in the virtual overflow register for later injecting the interrupt into the guest.
--- arch/arm/include/asm/arm_pmuv3.h | 6 ++++++ arch/arm64/include/asm/kvm_pmu.h | 2 ++ arch/arm64/kvm/pmu-direct.c | 17 +++++++++++++++++ drivers/perf/arm_pmuv3.c | 9 +++++++++ 4 files changed, 34 insertions(+)
arch/arm/include/asm/arm_pmuv3.h +6 -0
@@ -180,6 +180,11 @@ static inline void write_pmintenset(u32 val)write_sysreg (val,PMINTENSET ); } +static inline u32 read_pmintenset (void ) +{ +return read_sysreg (PMINTENSET ); +} +static inline void write_pmintenclr (u32 val) {write_sysreg (val,PMINTENCLR );
@@ -249,6 +254,7 @@ static inline u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu)return ~0 ; } +static inline void kvm_pmu_handle_guest_irq (u64 govf) {}/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0
arch/arm64/include/asm/kvm_pmu.h +2 -0
@@ -101,6 +101,7 @@ u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu);u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +void kvm_pmu_handle_guest_irq (u64 govf);u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu);u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu);
@@ -322,6 +323,7 @@ static inline u64 kvm_pmu_guest_counter_mask(void *pmu)static inline void kvm_pmu_host_counters_enable (void ) {}static inline void kvm_pmu_host_counters_disable (void ) {} +static inline void kvm_pmu_handle_guest_irq (u64 govf) {}#endif
arch/arm64/kvm/pmu-direct.c +17 -0
@@ -396,3 +396,20 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu) val =read_pmintenset ();__vcpu_assign_sys_reg (vcpu,PMINTENSET_EL1 , val & mask); } + +/** +* kvm_pmu_handle_guest_irq() - Record IRQs in guest counters +* @govf: Bitmask of guest overflowed counters +* +* Record IRQs from overflows in guest-reserved counters in the VCPU +* register for the guest to clear later. +*/ +void kvm_pmu_handle_guest_irq (u64 govf) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); + +if (!vcpu) +return ; + + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf); +}
drivers/perf/arm_pmuv3.c +9 -0
@@ -783,6 +783,8 @@ static u64 armv8pmu_getreset_flags(void)/* Write to clear flags */ value &=ARMV8_PMU_CNT_MASK_ALL ; +/* Only reset interrupt enabled counters. */ + value &=read_pmintenset ();write_pmovsclr (value);return value;
@@ -904,6 +906,7 @@ static void read_branch_records(struct pmu_hw_events *cpuc,static irqreturn_t armv8pmu_handle_irq (struct arm_pmu *cpu_pmu) {u64 pmovsr; +u64 govf;struct perf_sample_data data;struct pmu_hw_events *cpuc =this_cpu_ptr (cpu_pmu->hw_events);struct pt_regs *regs;
@@ -961,6 +964,12 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)*/ perf_event_overflow (event, &data, regs); } + + govf = pmovsr &kvm_pmu_guest_counter_mask (cpu_pmu); + +if (kvm_pmu_is_partitioned (cpu_pmu) && govf) +kvm_pmu_handle_guest_irq (govf); +armv8pmu_start (cpu_pmu);return IRQ_HANDLED ;
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 20/24] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
Quoted 1 line · Colton Lewis
Because ARM hardware is not yet capable of direct interrupt injection
PPI injection, it can do LPIs just fine.
drivers/perf/arm_pmuv3.c static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu) · line 972
Quoted 10 lines
drivers/perf/arm_pmuv3.c +6
-0@@ -961,6 +964,12 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)*/ perf_event_overflow (event, &data, regs); } + + govf = pmovsr &kvm_pmu_guest_counter_mask (cpu_pmu); + +if (kvm_pmu_is_partitioned (cpu_pmu) && govf) +kvm_pmu_handle_guest_irq (govf); +
The state ownership of this whole interaction is very odd. I would much rather that KVM have full ownership of the range of counters while the guest is loaded. By that I mean the PMUv3 driver only clears overflows on PMCs that it owns and KVM will do the same on the back of the IRQ.
Similarly, KVM should be leaving the "guest" range of counters in a non-overflow condition at vcpu_put().
Thanks,
Oliver
Re: [PATCH v5 20/24] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
Quoted 4 lines · Oliver Upton
Quoted 1 line · Colton Lewis
Because ARM hardware is not yet capable of direct interrupt injection
PPI injection, it can do LPIs just fine.
Clarification noted. I will update the message.
Quoted 8 lines
Quoted 8 lines
drivers/perf/arm_pmuv3.c +3
-0@@ -961,6 +964,12 @@ static irqreturn_t armv8pmu_handle_irq(structarm_pmu *cpu_pmu) */ perf_event_overflow (event, &data, regs); } + + govf = pmovsr &kvm_pmu_guest_counter_mask (cpu_pmu); +
Quoted 8 lines
drivers/perf/arm_pmuv3.c static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu) · line 972
Quoted 3 lines
drivers/perf/arm_pmuv3.c +3
-0+if (kvm_pmu_is_partitioned (cpu_pmu) && govf) +kvm_pmu_handle_guest_irq (govf); +The state ownership of this whole interaction is very odd. I would much rather that KVM have full ownership of the range of counters while the guest is loaded. By that I mean the PMUv3 driver only clears overflows on PMCs that it owns and KVM will do the same on the back of the IRQ.
If I'm understanding correctly this is a code location concern, because the host driver has to handle the interrupt in this function or a callee of this function.
I will do it that way. It would be duplicating a small amount of logic in kvm_pmu_handle_guest_irq() but I see your reasoning.
Quoted 2 lines
Similarly, KVM should be leaving the "guest" range of counters in a non-overflow condition at
vcpu_put().
Noted from your comments on kvm_pmu_put()
Quoted 2 lines
Thanks,
Oliver
PATCH v5 21/24 KVM: arm64: Inject recorded guest interrupts
In reply to Colton LewisWhen we re-enter the VM after handling a PMU interrupt, calculate whether it was any of the guest counters that overflowed and inject an interrupt into the guest if so.
--- arch/arm64/include/asm/kvm_pmu.h | 2 ++ arch/arm64/kvm/pmu-direct.c | 20 ++++++++++++++++++++ arch/arm64/kvm/pmu-emul.c | 4 ++-- arch/arm64/kvm/pmu.c | 6 +++++- 4 files changed, 29 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +2 -0
@@ -92,6 +92,8 @@ bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx);void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_resync_el0 (void ); +bool kvm_pmu_emul_overflow_status (struct kvm_vcpu *vcpu); +bool kvm_pmu_part_overflow_status (struct kvm_vcpu *vcpu);#define kvm_vcpu_has_pmu (vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
arch/arm64/kvm/pmu-direct.c +20 -0
@@ -413,3 +413,23 @@ void kvm_pmu_handle_guest_irq(u64 govf) __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf); } + +/** +* kvm_pmu_part_overflow_status() - Determine if any guest counters have overflowed +* @vcpu: Ponter to struct kvm_vcpu +* +* Determine if any guest counters have overflowed and therefore an +* IRQ needs to be injected into the guest. +* +* Return: True if there was an overflow, false otherwise +*/ +bool kvm_pmu_part_overflow_status (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 mask =kvm_pmu_guest_counter_mask (pmu); +u64 pmovs =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +u64 pmint =read_pmintenset (); +u64 pmcr =read_pmcr (); + +return (pmcr &ARMV8_PMU_PMCR_E ) && (mask & pmovs & pmint); +}
arch/arm64/kvm/pmu-emul.c +2 -2
@@ -268,7 +268,7 @@ void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val)* counter where the values of the global enable control, PMOVSSET_EL0[n], and * PMINTENSET_EL1[n] are all 1. */ -bool kvm_pmu_overflow_status (struct kvm_vcpu *vcpu) +bool kvm_pmu_emul_overflow_status (struct kvm_vcpu *vcpu) {u64 reg =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 );
@@ -405,7 +405,7 @@ static void kvm_pmu_perf_overflow(struct perf_event *perf_event,kvm_pmu_counter_increment (vcpu,BIT (idx +1 ),ARMV8_PMUV3_PERFCTR_CHAIN ); -if (kvm_pmu_overflow_status (vcpu)) { +if (kvm_pmu_emul_overflow_status (vcpu)) {kvm_make_request (KVM_REQ_IRQ_PENDING , vcpu);if (!in_nmi ())
arch/arm64/kvm/pmu.c +5 -1
@@ -407,7 +407,11 @@ static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)struct kvm_pmu *pmu = &vcpu->arch.pmu;bool overflow; - overflow =kvm_pmu_overflow_status (vcpu); +if (kvm_vcpu_pmu_is_partitioned (vcpu)) + overflow =kvm_pmu_part_overflow_status (vcpu); +else + overflow =kvm_pmu_emul_overflow_status (vcpu); +if (pmu->irq_level == overflow)return ;
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 21/24] KVM: arm64: Inject recorded guest interrupts
In no situation should KVM be injecting a "recorded" IRQ. The overflow condition of the PMU is well defined in the architecture and we should implement *exactly* that.
Quoted 6 lines · Colton Lewis
arch/arm64/kvm/pmu-direct.c +6
-0+/** +* kvm_pmu_part_overflow_status() - Determine if any guest counters have overflowed +* @vcpu: Ponter to struct kvm_vcpu +* +* Determine if any guest counters have overflowed and therefore an +* IRQ needs to be injected into the guest.
arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 govf) · line 432
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+* +* Return: True if there was an overflow, false otherwise +*/ +bool kvm_pmu_part_overflow_status (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 mask =kvm_pmu_guest_counter_mask (pmu); +u64 pmovs =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +u64 pmint =read_pmintenset (); +u64 pmcr =read_pmcr ();
How do we know that the vPMU has been loaded on the CPU at this point?
arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 govf) · line 435
Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3
-0+ +return (pmcr &ARMV8_PMU_PMCR_E ) && (mask & pmovs & pmint); +}
I'd rather reuse kvm_pmu_overflow_status(), relying on the accessors to abstract away the implementation / location of the guest PMU context.
Thanks,
Oliver
Re: [PATCH v5 21/24] KVM: arm64: Inject recorded guest interrupts
Quoted 3 lines · Oliver Upton
In no situation should KVM be injecting a "recorded" IRQ. The overflow condition of the PMU is well defined in the architecture and we should implement *exactly* that.
When I say "record" I just meant "updating the virtual overflow register to reflect an overflow".
Do you think I'm not implementing the condition correctly in kvm_pmu_part_overflow_status()?
Quoted 12 lines · Colton Lewis
Quoted 11 lines
+/**
+ *kvm_pmu_part_overflow_status()- Determine if any guest counters have overflowed
+ * @vcpu: Ponter to structkvm_vcpu
+ *
+ * Determine if any guest counters have overflowed and therefore an
+ * IRQ needs to be injected into the guest.
+ *
+ * Return: True if there was an overflow, false otherwise
+ */
+boolkvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu)
Quoted 8 lines
arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 govf) · line 432
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 mask =kvm_pmu_guest_counter_mask (pmu); +u64 pmovs =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +u64 pmint =read_pmintenset (); +u64 pmcr =read_pmcr ();How do we know that the vPMU has been loaded on the CPU at this point?
Because this is only called by kvm_pmu_update_state which is only called by kvm_pmu_update_state <- kvm_pmu_{flush,sync}_hwstate <- kvm_arch_vcpu_ioctl_run after a vcpu_load.
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 govf) · line 435
Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3
-0+ +return (pmcr &ARMV8_PMU_PMCR_E ) && (mask & pmovs & pmint); +}I'd rather reuse
kvm_pmu_overflow_status(), relying on the accessors to abstract away the implementation / location of the guest PMU context.
Agreed on making some generic accessors.
Quoted 2 lines
Thanks,
Oliver
Oliver Upton
maintainerRe: [PATCH v5 21/24] KVM: arm64: Inject recorded guest interrupts
Quoted 8 lines · Colton Lewis
Quoted 3 lines · Oliver Upton
In no situation should KVM be injecting a "recorded" IRQ. The overflow condition of the PMU is well defined in the architecture and we should implement *exactly* that.
When I say "record" I just meant "updating the virtual overflow register to reflect an overflow".
Right, consider changing the shortlog to read more along the lines of "detect overflows for partitioned PMU" or similar.
Quoted 21 lines
Quoted 12 lines · Colton Lewis
Quoted 11 lines
+/**
+ *kvm_pmu_part_overflow_status()- Determine if any guest counters have overflowed
+ * @vcpu: Ponter to structkvm_vcpu
+ *
+ * Determine if any guest counters have overflowed and therefore an
+ * IRQ needs to be injected into the guest.
+ *
+ * Return: True if there was an overflow, false otherwise
+ */
+boolkvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu)Quoted 8 lines
arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 govf) · line 432
Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6
-0+{ +struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu; +u64 mask =kvm_pmu_guest_counter_mask (pmu); +u64 pmovs =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +u64 pmint =read_pmintenset (); +u64 pmcr =read_pmcr ();How do we know that the vPMU has been loaded on the CPU at this point?
Quoted 3 lines
Because this is only called by
kvm_pmu_update_statewhich is only called bykvm_pmu_update_state<- kvm_pmu_{flush,sync}_hwstate <-kvm_arch_vcpu_ioctl_runafter avcpu_load.
That's assuming the PMU is loaded eagerly which I thought we agreed it would not be.
Thanks,
Oliver
PATCH v5 22/24 KVM: arm64: Add KVM_CAP to partition the PMU
In reply to Colton LewisAdd KVM_CAP_ARM_PARTITION_PMU to enable the partitioned PMU for a given VM. This capability is allowed where PMUv3 and VHE are supported and the host driver was configured with arm_pmuv3.reserved_host_counters.
Ordering is enforced such that this must be called before the vCPUs are created to avoid the possibility the guest is already using one PMU configuration that gets switched out from under it.
The enabled capability is tracked by the new flagKVM_ARCH_FLAG_PARTITIONED_PMU_ENABLED.
--- Documentation/virt/kvm/api.rst | 24 +++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/asm/kvm_pmu.h | 9 ++++++++ arch/arm64/kvm/arm.c | 15 +++++++++++++ arch/arm64/kvm/pmu-direct.c | 35 ++++++++++++++++++++++++++++--- include/uapi/linux/kvm.h | 1 + 6 files changed, 83 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +24 -0
@@ -8703,6 +8703,30 @@ This capability indicate to the userspace whether a PFNMAP memory region can be safely mapped as cacheable. This relies on the presence of force write back (FWB) feature support on the hardware. + +7.245 KVM_CAP_ARM_PARTITION_PMU +------------------------------------- + +:Architectures: arm64 +:Target: VM +:Parameters: arg[0] is a boolean that enables or disables the capability +:Returns: 0 on success + -EPERM if host doesn't support + -EBUSY if vPMU was already created + +This API controls the PMU implementation used for VMs. The capability +is only available if the host PMUv3 driver was configured for +partitioning via the module parameter +``arm-pmuv3.reserved_guest_counters=[0-$NR_COUNTERS]``. When enabled, +VMs are configured to have direct hardware access to the most +frequently used registers for the counters configured by the +aforementioned module parameters, bypassing the KVM traps in the +standard emulated PMU implementation and reducing overhead of any +guest software that uses PMU capabilities such as ``perf``. + +If the host driver was configured for partitioning but the partitioned +PMU is disabled through this interface, the VM will use the legacy PMU + 8. Other capabilities. ======================
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -349,6 +349,8 @@ struct kvm_arch {#define KVM_ARCH_FLAG_GUEST_HAS_SVE 9/* MIDR_EL1, REVIDR_EL1, and AIDR_EL1 are writable from userspace */ #define KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS 10 +/* Partitioned PMU Enabled */ +#define KVM_ARCH_FLAG_PARTITION_PMU_ENABLED 11unsigned long flags;/* VM-wide vCPU feature set */
arch/arm64/include/asm/kvm_pmu.h +9 -0
@@ -111,6 +111,8 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu);void kvm_pmu_put (struct kvm_vcpu *vcpu);void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu); +bool kvm_pmu_partition_ready (void ); +void kvm_pmu_partition_enable (struct kvm *kvm,bool enable);#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);
@@ -327,6 +329,13 @@ static inline void kvm_pmu_host_counters_enable(void) {}static inline void kvm_pmu_host_counters_disable (void ) {}static inline void kvm_pmu_handle_guest_irq (u64 govf) {} +static inline bool kvm_pmu_partition_ready (void ) +{ +return false; +} + +static inline void kvm_pmu_partition_enable (struct kvm *kvm,bool enable) {} +#endif #endif
arch/arm64/kvm/arm.c +15 -0
@@ -20,6 +20,7 @@#include <linux/irqbypass.h> #include <linux/sched/stat.h> #include <linux/psci.h> +#include <linux/perf/arm_pmu.h> #include <trace/events/kvm.h> #define CREATE_TRACE_POINTS
@@ -37,6 +38,7 @@#include <asm/kvm_emulate.h> #include <asm/kvm_mmu.h> #include <asm/kvm_nested.h> +#include <asm/kvm_pmu.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_pmu.h> #include <asm/kvm_ptrauth.h>
@@ -132,6 +134,16 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_PARTITION_PMU : +if (kvm->created_vcpus) { + r = -EBUSY ; + }else if (!kvm_pmu_partition_ready ()) { + r = -EPERM ; + }else { + r =0 ; +kvm_pmu_partition_enable (kvm, cap->args[0 ]); + } +break ;default :break ; }
@@ -388,6 +400,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PMU_V3 : r =kvm_supports_guest_pmuv3 ();break ; +case KVM_CAP_ARM_PARTITION_PMU : + r =kvm_pmu_partition_ready (); +break ;case KVM_CAP_ARM_INJECT_SERROR_ESR : r =cpus_have_final_cap (ARM64_HAS_RAS_EXTN );break ;
arch/arm64/kvm/pmu-direct.c +32 -3
@@ -45,8 +45,8 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) }/** -* kvm_vcpu_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU -* @vcpu: Pointer to kvm_vcpu struct +* kvm_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU +* @kvm: Pointer to kvm_vcpu struct * * Determine if given VCPU has a partitioned PMU by extracting that * field and passing it to :c:func:`kvm_pmu_is_partitioned`
@@ -56,7 +56,36 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) {return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu) && - false; +test_bit (KVM_ARCH_FLAG_PARTITION_PMU_ENABLED , &vcpu->kvm->arch.flags); +} + +/** +* kvm_pmu_partition_ready() - If we can enable/disable partition +* +* Return: true if allowed, false otherwise. +*/ +bool kvm_pmu_partition_ready (void ) +{ +return kvm_pmu_partition_supported () && +kvm_supports_guest_pmuv3 () && + armv8pmu_hpmn_max >-1 ; +} + +/** +* kvm_pmu_partition_enable() - Enable/disable partition flag +* @kvm: Pointer to vcpu +* @enable: Whether to enable or disable +* +* If we want to enable the partition, the guest is free to grab +* hardware by accessing PMU registers. Otherwise, the host maintains +* control. +*/ +void kvm_pmu_partition_enable (struct kvm *kvm,bool enable) +{ +if (enable) +set_bit (KVM_ARCH_FLAG_PARTITION_PMU_ENABLED , &kvm->arch.flags); +else +clear_bit (KVM_ARCH_FLAG_PARTITION_PMU_ENABLED , &kvm->arch.flags); }/**
include/uapi/linux/kvm.h +1 -0
@@ -963,6 +963,7 @@ struct kvm_enable_cap {#define KVM_CAP_RISCV_MP_STATE_RESET 242#define KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED 243#define KVM_CAP_GUEST_MEMFD_FLAGS 244 +#define KVM_CAP_ARM_PARTITION_PMU 245struct kvm_irq_routing_irqchip {__u32 irqchip;
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerRe: [PATCH v5 22/24] KVM: arm64: Add KVM_CAP to partition the PMU
Documentation/virt/kvm/api.rst This capability indicate to the userspace whether a PFNMAP memory region · line 8709
Quoted 4 lines · Colton Lewis
Documentation/virt/kvm/api.rst +4
-0+ +7.245 KVM_CAP_ARM_PARTITION_PMU +------------------------------------- +
Why can't this be a vCPU attribute similar to the other vPMU controls? Making the UAPI consistent will make it easier for userspace to reason about it.
Better yet, we could make the UAPI such that userspace selects a PMU implementation and the partitioned-ness of the PMU at the same time.
Quoted 18 lines
arch/arm64/kvm/arm.c +10
-0@@ -132,6 +134,16 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_PARTITION_PMU : +if (kvm->created_vcpus) { + r = -EBUSY ; + }else if (!kvm_pmu_partition_ready ()) { + r = -EPERM ; + }else { + r =0 ; +kvm_pmu_partition_enable (kvm, cap->args[0 ]); + } +break ;default :break ; }
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 404
Quoted 5 lines
arch/arm64/kvm/arm.c +2
-0@@ -388,6 +400,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PMU_V3 : r =kvm_supports_guest_pmuv3 ();break ; +case KVM_CAP_ARM_PARTITION_PMU : + r =kvm_pmu_partition_ready ();
"ready" is very confusing in this context, as KVM will never be ready to support the feature on a system w/o the prerequisites.
Thanks,
Oliver
Re: [PATCH v5 22/24] KVM: arm64: Add KVM_CAP to partition the PMU
Quoted 9 lines · Oliver Upton
Documentation/virt/kvm/api.rst This capability indicate to the userspace whether a PFNMAP memory region · line 8709
Quoted 4 lines · Colton Lewis
Documentation/virt/kvm/api.rst +4
-0+ +7.245 KVM_CAP_ARM_PARTITION_PMU +------------------------------------- +Why can't this be a vCPU attribute similar to the other vPMU controls? Making the UAPI consistent will make it easier for userspace to reason about it.
I'm confused by the inconsistency of using a vCPU attribute for something we want to affect the whole VM.
But I'll do a vCPU attribute if you want.
Quoted 2 lines
Better yet, we could make the UAPI such that userspace selects a PMU implementation and the partitioned-ness of the PMU at the same time.
Sounds good.
Quoted 19 lines
Quoted 19 lines
arch/arm64/kvm/arm.c +10
-0@@ -132,6 +134,16 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_PARTITION_PMU : +if (kvm->created_vcpus) { + r = -EBUSY ; + }else if (!kvm_pmu_partition_ready ()) { + r = -EPERM ; + }else { + r =0 ; +kvm_pmu_partition_enable (kvm, cap->args[0 ]); + } +break ;default :break ; }@@ -388,6 +400,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Quoted 8 lines
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 404
Quoted 5 lines
arch/arm64/kvm/arm.c +2
-0case KVM_CAP_ARM_PMU_V3 : r =kvm_supports_guest_pmuv3 ();break ; +case KVM_CAP_ARM_PARTITION_PMU : + r =kvm_pmu_partition_ready ();"ready" is very confusing in this context, as KVM will never be ready to support the feature on a system w/o the prerequisites.
That was a last minute addition. I'll change the name to something better.
Quoted 2 lines
Thanks,
Oliver
PATCH v5 23/24 KVM: selftests: Add find_bit to KVM library
In reply to Colton LewisSome selftests have a dependency on find_bit and weren't compiling separately without it, so I've added it to the KVM library here using the same method as files like rbtree.c.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/lib/find_bit.c | 1 + 2 files changed, 2 insertions(+) create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -5,6 +5,7 @@ all: LIBKVM += lib/assert.c LIBKVM += lib/elf.c +LIBKVM += lib/find_bit.c LIBKVM += lib/guest_modes.c LIBKVM += lib/io.c LIBKVM += lib/kvm_util.c
tools/testing/selftests/kvm/lib/find_bit.c new file +1 -0
@@ -0,0 +1 @@ +#include "../../../../lib/find_bit.c"
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
PATCH v5 24/24 KVM: arm64: selftests: Add test case for partitioned PMU
In reply to Colton LewisRerun all tests for a partitioned PMU in vpmu_counter_access.
Create an enum specifying whether we are testing the emulated or partitioned PMU and all the test functions are modified to take the implementation as an argument and make the difference in setup appropriately.
--- tools/include/uapi/linux/kvm.h | 1 + .../selftests/kvm/arm64/vpmu_counter_access.c | 77 ++++++++++++++----- 2 files changed, 57 insertions(+), 21 deletions(-)
tools/include/uapi/linux/kvm.h +1 -0
@@ -963,6 +963,7 @@ struct kvm_enable_cap {#define KVM_CAP_RISCV_MP_STATE_RESET 242#define KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED 243#define KVM_CAP_GUEST_MEMFD_FLAGS 244 +#define KVM_CAP_ARM_PARTITION_PMU 245struct kvm_irq_routing_irqchip {__u32 irqchip;
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +56 -21
176 lines not on this page · raw message
Signature · 1 line
2.52.0.239.gd5f0c6e74e-goog
Oliver Upton
maintainerQuoted 5 lines · Colton Lewis
This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.
Quoted 7 lines
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
The long duration between v4 and v5 is due to time spent on this project being monopolized preparing this feature for internal production. As a result, there are too many improvements to fully list here, but I will cover the notable ones.
Thanks for reposting. I think there's still quite a bit of ground to cover on the KVM side of this, but I would definitely appreciate it if someone with more context on the perf side of things could chime in.
Will, IIRC you had some thoughts around counter allocation, right?
Best,
Oliver
Will Deacon
maintainerQuoted 14 lines · Oliver Upton
Quoted 5 lines · Colton Lewis
This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.
Quoted 7 lines
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
The long duration between v4 and v5 is due to time spent on this project being monopolized preparing this feature for internal production. As a result, there are too many improvements to fully list here, but I will cover the notable ones.
Quoted 5 lines
Thanks for reposting. I think there's still quite a bit of ground to cover on the KVM side of this, but I would definitely appreciate it if someone with more context on the perf side of things could chime in.
Will, IIRC you had some thoughts around counter allocation, right?
Right, I was hoping that the host counter reservation could be more dynamic than a cmdline option. Perf already has support for pinning events to a CPU, so the concept of some counters being unavailable shouldn't be too much for the driver to handle. You might just need to create some fake pinned events so that perf code understands what is happening.
Will
Quoted 19 lines · Will Deacon
Quoted 17 lines · Oliver Upton
Quoted 5 lines · Colton Lewis
This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.
Quoted 7 lines
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
The long duration between v4 and v5 is due to time spent on this project being monopolized preparing this feature for internal production. As a result, there are too many improvements to fully list here, but I will cover the notable ones.
Thanks for reposting. I think there's still quite a bit of ground to cover on the KVM side of this, but I would definitely appreciate it if someone with more context on the perf side of things could chime in.
Quoted 8 lines
Quoted 1 line
Will, IIRC you had some thoughts around counter allocation, right?
Right, I was hoping that the host counter reservation could be more dynamic than a cmdline option. Perf already has support for pinning events to a CPU, so the concept of some counters being unavailable shouldn't be too much for the driver to handle. You might just need to create some fake pinned events so that perf code understands what is happening.
Thanks Will. I have a few followup questions:
1. Are you suggesting this be done whenever we enter a guest so the host always has access to the full range in host context? That would be the most dynamic.
2. How should we handle the possibility a real event already occupies a counter wanted by the guest? Is there a good way to create our fake pinned events then force a reschedule so perf moves the real events out of the way?
3. Is there an existing fake event type that tells perf not to touch hardware?
4. Can you point to any example code that already does something like this?
Quoted 41 lines · Colton Lewis
Quoted 19 lines · Will Deacon
Quoted 17 lines · Oliver Upton
Quoted 5 lines · Colton Lewis
This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.
Quoted 7 lines
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
The long duration between v4 and v5 is due to time spent on this project being monopolized preparing this feature for internal production. As a result, there are too many improvements to fully list here, but I will cover the notable ones.
Thanks for reposting. I think there's still quite a bit of ground to cover on the KVM side of this, but I would definitely appreciate it if someone with more context on the perf side of things could chime in.
Quoted 8 lines
Quoted 1 line
Will, IIRC you had some thoughts around counter allocation, right?
Right, I was hoping that the host counter reservation could be more dynamic than a cmdline option. Perf already has support for pinning events to a CPU, so the concept of some counters being unavailable shouldn't be too much for the driver to handle. You might just need to create some fake pinned events so that perf code understands what is happening.
Thanks Will. I have a few followup questions:
1. Are you suggesting this be done whenever we enter a guest so the host always has access to the full range in host context? That would be the most dynamic.
2. How should we handle the possibility a real event already occupies a counter wanted by the guest? Is there a good way to create our fake pinned events then force a reschedule so perf moves the real events out of the way?
Quoted 5 lines
3. Is there an existing fake event type that tells perf not to touch hardware?
4. Can you point to any example code that already does something like this?
Thank you Will and Mark for meeting with me to discuss things in person.
Here's my main takeaways so the list can comment:
Will's initial idea doesn't work because there is no way for KVM to pin counters in a way that takes priority over counters pinned by the host and therefore guarantee reservation.
An alternate idea I am proposing is to call the perf core sched_in/sched_out functionality during vcpu_load/vcpu_put when guest counters need to be reserved/unreserved.
That means having perf vacate all the host counters temporarily, modifying the arm_pmu.cntr_mask to add/remove the appropriate counters, then having perf schedule all host events back on the new set. Perf is capable of doing that without any significant changes.
This is simple and should work because arm_pmu.cntr_mask is already accessible from the vcpu struct and modifying it is already how the existing boot-time counter reservation works.
There are some tradeoffs to this approach that will need further consideration. The first is how to handle event groups. Perf allows events to be grouped such that they must all be scheduled in at once. If the host has a larger group than the number of counters available while the vcpu is loaded, then it simply won't be able to schedule that group in for that time period. Another is whether it will be acceptable performance-wise to put perf sched_in/sched_out in vcpu_load/vcpu_put. I'm unsure how much delay that would add to those paths.
Absent strong objections, I will be posting a series using this method.
Another idea that was not discussed that I had later is a middle approach that is less dynamic but gives the user control over when the perf sched_in/sched_out happens. Expose the existing boot-time parameter as writable in sysfs and do the sched_out/modify mask/sched_in when that is written rather than in vcpu_load.