ARM64 PMU Partitioning
Subsystems (7)
v3 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.
v3:
* Return to using one kernel command line parameter for reserved host counters, but set the default value meaning no partitioning to -1 so 0 isn't ambiguous. Adjust checks for partitioning accordingly.
* Move the function kvm_pmu_partition_supported() back out of the driver to avoid subbing has_vhe in 32-bit ARM code.
* Fix the kernel test robot build failures, one with KVM and no PMU, and one with PMU and no KVM.
* Scan entire series to remove vestigial changes and update comments.
v2:
https://lore.kernel.org/kvm/[email protected]/
v1:
https://lore.kernel.org/kvm/[email protected]/
Colton Lewis (21):
arm64: cpufeature: Add cpucap for HPMN0
arm64: Generate sign macro for sysreg Enums
KVM: arm64: Define PMI{CNTR,FILTR}_EL0 as undef_access
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: Correct kvm_arm_pmu_get_max_counters()
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()
perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
KVM: arm64: Inject recorded guest interrupts
KVM: arm64: Add ioctl to partition the PMU when supported
KVM: arm64: selftests: Add test case for partitioned PMU
Marc Zyngier (1):
KVM: arm64: Cleanup PMU includes
Documentation/virt/kvm/api.rst | 21 + arch/arm/include/asm/arm_pmuv3.h | 38 + arch/arm64/include/asm/arm_pmuv3.h | 61 +- arch/arm64/include/asm/kvm_host.h | 18 +- arch/arm64/include/asm/kvm_pmu.h | 100 +++ arch/arm64/kernel/cpufeature.c | 8 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 22 + arch/arm64/kvm/debug.c | 24 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 233 ++++++ arch/arm64/kvm/pmu-emul.c | 674 +---------------- arch/arm64/kvm/pmu-part.c | 378 ++++++++++ arch/arm64/kvm/pmu.c | 702 ++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 79 +- arch/arm64/tools/cpucaps | 1 + arch/arm64/tools/gen-sysreg.awk | 1 + arch/arm64/tools/sysreg | 6 +- drivers/perf/arm_pmuv3.c | 123 ++- include/linux/perf/arm_pmu.h | 6 + include/linux/perf/arm_pmuv3.h | 14 +- include/uapi/linux/kvm.h | 4 + tools/include/uapi/linux/kvm.h | 2 + .../selftests/kvm/arm64/vpmu_counter_access.c | 62 +- virt/kvm/kvm_main.c | 1 + 24 files changed, 1828 insertions(+), 752 deletions(-) create mode 100644 arch/arm64/kvm/pmu-part.c
base-commit: 79150772457f4d45e38b842d786240c36bb1f97f --
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 1/22 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/tools/cpucaps | 1 + arch/arm64/tools/sysreg | 6 +++--- 3 files changed, 12 insertions(+), 3 deletions(-)
arch/arm64/kernel/cpufeature.c +8 -0
@@ -548,6 +548,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_STRICT ,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 ),
@@ -2896,6 +2897,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature,ARM64_CPUID_FIELDS (ID_AA64MMFR0_EL1 ,FGT ,FGT2 ) }, + { + .desc ="FEAT_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/tools/cpucaps +1 -0
@@ -39,6 +39,7 @@ HAS_GIC_CPUIF_SYSREGS 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
@@ -1531,9 +1531,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.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v3 01/22] arm64: cpufeature: Add cpucap for HPMN0
Quoted 6 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.
I agree it's appropriate to mark ID_AA64DFR0_EL1.HPMN0 as an UnsignedEnum. It follows the usual ID scheme per ARM DDI 0487 L.a section D24.1.3, and zero means not present, so it must be unsigned.
Likewise, the value renames (UNPREDICTABLE => NI and DEF => IMP) look fine to me.
Quoted 1 line
Signed-off-by Colton Lewis <[email protected]>
I have one minor nit below, but either way:
Quoted 19 lines
--- arch/arm64/kernel/cpufeature.c | 8 ++++++++ arch/arm64/tools/cpucaps | 1 + arch/arm64/tools/sysreg | 6 +++--- 3 files changed, 12 insertions(+), 3 deletions(-)arch/arm64/kernel/cpufeature.c +1
-0@@ -548,6 +548,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_STRICT ,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 ),
arch/arm64/kernel/cpufeature.c static const struct arm64_cpu_capabilities arm64_features[] = { · line 2901
Quoted 5 lines
arch/arm64/kernel/cpufeature.c +2
-0@@ -2896,6 +2897,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature,ARM64_CPUID_FIELDS (ID_AA64MMFR0_EL1 ,FGT ,FGT2 ) }, + { + .desc = "FEAT_HPMN0",
Minor nit, but we can drop the "FEAT_" prefix here, for consistency with other features (e.g. E0PD, FPMR).
Mark.
Quoted 40 lines
arch/arm64/kernel/cpufeature.c +5
-0+ .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/tools/cpucaps +1
-0@@ -39,6 +39,7 @@ HAS_GIC_CPUIF_SYSREGS HAS_GIC_PRIO_MASKING HAS_GIC_PRIO_RELAXED_SYNC HAS_HCR_NV1 +HAS_HPMN0 HAS_HCX HAS_LDAPR HAS_LPA2arch/arm64/tools/sysreg +3
-3@@ -1531,9 +1531,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 NISignature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v3 01/22] arm64: cpufeature: Add cpucap for HPMN0
Quoted 12 lines · Mark Rutland
Quoted 6 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.I agree it's appropriate to mark ID_AA64DFR0_EL1.HPMN0 as an UnsignedEnum. It follows the usual ID scheme per ARM DDI 0487 L.a section D24.1.3, and zero means not present, so it must be unsigned.
Quoted 8 lines
Likewise, the value renames (UNPREDICTABLE => NI and DEF => IMP) look fine to me.
Quoted 1 line
Signed-off-by Colton Lewis <[email protected]>I have one minor nit below, but either way:
Acked-by Mark Rutland <[email protected]>
Thank you Mark
Quoted 33 lines
Quoted 23 lines
--- arch/arm64/kernel/cpufeature.c | 8 ++++++++ arch/arm64/tools/cpucaps | 1 + arch/arm64/tools/sysreg | 6 +++--- 3 files changed, 12 insertions(+), 3 deletions(-)arch/arm64/kernel/cpufeature.c +1
-0@@ -548,6 +548,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_STRICT ,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),arch/arm64/kernel/cpufeature.c static const struct arm64_cpu_capabilities · line 2902
Quoted 6 lines
arch/arm64/kernel/cpufeature.c +2
-0@@ -2896,6 +2897,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature, ARM64_CPUID_FIELDS(ID_AA64MMFR0_EL1, FGT, FGT2) }, + { + .desc = "FEAT_HPMN0",Minor nit, but we can drop the "
FEAT_" prefix here, for consistency with other features (e.g. E0PD, FPMR).
Quoted 1 line
Mark.
Will do.
Quoted 39 lines
Quoted 39 lines
arch/arm64/kernel/cpufeature.c +5
-0+ .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/tools/cpucaps +1
-0@@ -39,6 +39,7 @@ HAS_GIC_CPUIF_SYSREGS HAS_GIC_PRIO_MASKING HAS_GIC_PRIO_RELAXED_SYNC HAS_HCR_NV1 +HAS_HPMN0 HAS_HCX HAS_LDAPR HAS_LPA2arch/arm64/tools/sysreg +3
-4@@ -1531,9 +1531,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 --2.50.0.727.gbf7dc18ff4-goog
PATCH v3 2/22 arm64: Generate sign macro for sysreg Enums
In reply to Colton LewisThere's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
--- arch/arm64/tools/gen-sysreg.awk | 1 + 1 file changed, 1 insertion(+)
arch/arm64/tools/gen-sysreg.awk +1 -0
@@ -308,6 +308,7 @@ $1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields parse_bitdef(reg, field, $2) define_field(reg, field, msb, lsb) + define_field_sign(reg, field, "false") next }
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v3 02/22] arm64: Generate sign macro for sysreg Enums
Hi Colton,
Quoted 3 lines · Colton Lewis
There's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
An Enum can be annotated with the field's sign by updating it to UnsignedEnum or SignedEnum. This is explained in [1].
With this change ID_AA64PFR1_EL1.MTE_frac would be marked as unsigned when it should really be considered signed.
Enum 43:40 MTE_frac
0b0000 ASYNC 0b1111 NI
EndEnum
Quoted 11 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 1 + 1 file changed, 1 insertion(+)arch/arm64/tools/gen-sysreg.awk +0
-0
arch/arm64/tools/gen-sysreg.awk $1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields · line 314
Quoted 7 lines
arch/arm64/tools/gen-sysreg.awk +1
-0@@ -308,6 +308,7 @@ $1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields parse_bitdef(reg, field, $2) define_field(reg, field, msb, lsb) + define_field_sign(reg, field, "false") next }
Thanks,
Ben
Re: [PATCH v3 02/22] arm64: Generate sign macro for sysreg Enums
Hi Ben. Thanks for the review.
Quoted 9 lines · Ben Horgan
Hi Colton,
Quoted 3 lines · Colton Lewis
There's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
An Enum can be annotated with the field's sign by updating it to UnsignedEnum or SignedEnum. This is explained in [1].
Quoted 7 lines
With this change ID_AA64PFR1_EL1.MTE_frac would be marked as unsigned when it should really be considered signed.
Enum 43:40
MTE_frac0b0000 ASYNC 0b1111 NIEndEnum
Thanks for the explanation. I made this a separate commit because I considered people might object and HPMN0 is already an UnsignedEnum in my previous commit.
Do you think it would be a good idea to make plain Enums signed by default or should I just remove this commit from the series?
Quoted 25 lines
Quoted 14 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 1 + 1 file changed, 1 insertion(+)arch/arm64/tools/gen-sysreg.awk +0
-0@@ -308,6 +308,7 @@ $1 == "Enum" && (block_current() == "Sysreg" ||
block_current()== "SysregFields
parse_bitdef(reg, field, $2)
define_field(reg, field, msb, lsb)arch/arm64/tools/gen-sysreg.awk $1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields · line 314
Quoted 4 lines
arch/arm64/tools/gen-sysreg.awk +1
-0+ define_field_sign(reg, field, "false") next }Thanks,
Ben
Re: [PATCH v3 02/22] arm64: Generate sign macro for sysreg Enums
Quoted 21 lines · Colton Lewis
Hi Ben. Thanks for the review.
Quoted 9 lines · Ben Horgan
Hi Colton,
Quoted 3 lines · Colton Lewis
There's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
An Enum can be annotated with the field's sign by updating it to UnsignedEnum or SignedEnum. This is explained in [1].
Quoted 7 lines
With this change ID_AA64PFR1_EL1.MTE_frac would be marked as unsigned when it should really be considered signed.
Enum 43:40
MTE_frac0b0000 ASYNC 0b1111 NIEndEnum
Quoted 6 lines
Thanks for the explanation. I made this a separate commit because I considered people might object and HPMN0 is already an UnsignedEnum in my previous commit.
Do you think it would be a good idea to make plain Enums signed by default or should I just remove this commit from the series?
It is presumptive to associate a sign with an enumeration. Generally speaking, the only fields that can do signed / unsigned comparisons are the Feature ID register fields.
So please drop this and only keep the change for HPMN0.
Thanks,
Oliver
Quoted 26 lines
Quoted 26 lines
Quoted 15 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 1 + 1 file changed, 1 insertion(+)arch/arm64/tools/gen-sysreg.awk +0
-0@@ -308,6 +308,7 @@ $1 == "Enum" && (block_current() == "Sysreg" ||
block_current()== "SysregFields
parse_bitdef(reg, field, $2)
define_field(reg, field, msb, lsb)arch/arm64/tools/gen-sysreg.awk $1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields · line 314
Quoted 4 lines
arch/arm64/tools/gen-sysreg.awk +1
-0+ define_field_sign(reg, field, "false") next }Thanks,
Ben
Re: [PATCH v3 02/22] arm64: Generate sign macro for sysreg Enums
Quoted 26 lines · Oliver Upton
Quoted 24 lines · Colton Lewis
Hi Ben. Thanks for the review.
Quoted 9 lines · Ben Horgan
Hi Colton,
Quoted 3 lines · Colton Lewis
There's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
An Enum can be annotated with the field's sign by updating it to UnsignedEnum or SignedEnum. This is explained in [1].
Quoted 7 lines
With this change ID_AA64PFR1_EL1.MTE_frac would be marked as unsigned when it should really be considered signed.
Enum 43:40
MTE_frac0b0000 ASYNC 0b1111 NIEndEnum
Thanks for the explanation. I made this a separate commit because I considered people might object and HPMN0 is already an UnsignedEnum in my previous commit.
Quoted 8 lines
Quoted 2 lines
Do you think it would be a good idea to make plain Enums signed by default or should I just remove this commit from the series?
It is presumptive to associate a sign with an enumeration. Generally speaking, the only fields that can do signed / unsigned comparisons are the Feature ID register fields.
So please drop this and only keep the change for HPMN0.
Done.
Quoted 30 lines
Thanks,
OliverQuoted 27 lines
Quoted 17 lines
Quoted 16 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 1 + 1 file changed, 1 insertion(+)arch/arm64/tools/gen-sysreg.awk +0
-0@@ -308,6 +308,7 @@ $1 == "Enum" && (block_current() == "Sysreg" ||
block_current()== "SysregFields
parse_bitdef(reg, field, $2)
define_field(reg, field, msb, lsb)arch/arm64/tools/gen-sysreg.awk +1
-0+ define_field_sign(reg, field, "false")Quoted 9 lines
Quoted 2 lines
next }Thanks,
Ben
[1]
Marc Zyngier
maintainerRe: [PATCH v3 02/22] arm64: Generate sign macro for sysreg Enums
In reply to Colton LewisQuoted 4 lines · Colton Lewis
There's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
And randomly break unsuspecting cases for which a value is neither signed not unsigned, but an actual *enumeration*.
If you have issues with HPMN0, start by explaining those, because ranting in a commit message doesn't help much.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Mark Rutland
maintainerRe: [PATCH v3 02/22] arm64: Generate sign macro for sysreg Enums
In reply to Colton LewisQuoted 5 lines · Colton Lewis
There's no reason Enums shouldn't be equivalent to UnsignedEnums and explicitly specify they are unsigned. This will avoid the annoyance I had with HPMN0.
Signed-off-by Colton Lewis <[email protected]>
I realise you've already agreed to drop this, so the below is mostly for the sake of the archive.
We deliberately kept Enum separate from SignedEnum and UnsignedEnum as there are enumerated ID fields which have no ordering, and are neither signed nor unsigned (e.g. CTR_EL0.L1Ip).
That was discussed previously at:
https://lore.kernel.org/all/Y5MrVC3d8MPhvshE@FVFF77S0Q05N/
We could consider adding a comment to that effect in gen-sysreg.awk, but that needn't be part of this series.
Mark.
Quoted 19 lines
--- arch/arm64/tools/gen-sysreg.awk | 1 + 1 file changed, 1 insertion(+)arch/arm64/tools/gen-sysreg.awk +1
-0@@ -308,6 +308,7 @@ $1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields parse_bitdef(reg, field, $2) define_field(reg, field, msb, lsb) + define_field_sign(reg, field, "false") next }Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 3/22 KVM: arm64: Define PMI{CNTR,FILTR}_EL0 as undef_access
In reply to Colton LewisBecause KVM isn't fully prepared to support these yet even though the host PMUv3 driver does, define them as undef_access for now.
--- arch/arm64/kvm/sys_regs.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kvm/sys_regs.c +3 -0
@@ -3092,6 +3092,9 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_SVCR), undef_access, reset_val, SVCR, 0, .visibility = sme_visibility }, { SYS_DESC(SYS_FPMR), undef_access, reset_val, FPMR, 0, .visibility = fp8_visibility }, + { SYS_DESC(SYS_PMICNTR_EL0), undef_access }, + { SYS_DESC(SYS_PMICFILTR_EL0), undef_access }, + { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr }, { PMU_SYS_REG(PMCNTENSET_EL0),
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Marc Zyngier
maintainerRe: [PATCH v3 03/22] KVM: arm64: Define PMI{CNTR,FILTR}_EL0 as undef_access
Quoted 14 lines · Colton Lewis
Because KVM isn't fully prepared to support these yet even though the host PMUv3 driver does, define them as
undef_accessfor now.Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/sys_regs.c | 3 +++ 1 file changed, 3 insertions(+)arch/arm64/kvm/sys_regs.c +0
-0
arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 3095
Quoted 4 lines
arch/arm64/kvm/sys_regs.c +1
-0@@ -3092,6 +3092,9 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_SVCR), undef_access, reset_val, SVCR, 0, .visibility = sme_visibility }, { SYS_DESC(SYS_FPMR), undef_access, reset_val, FPMR, 0, .visibility = fp8_visibility }, + { SYS_DESC(SYS_PMICNTR_EL0), undef_access },
$ jq -r --arg FEAT "FEAT_PMUv3_ICNTR" -f ./dumpfeat.jq Features.json (FEAT_PMUv3_ICNTR --> v8Ap8) (FEAT_PMUv3_ICNTR --> FEAT_PMUv3p9) ((FEAT_PMUv3_ICNTR && FEAT_AA64EL2) --> FEAT_FGT2)
If you have FEAT_PMUv3_ICNTR, then you have FEAT_FGT2. If you have FEAT_FGT2, then we already trap and UNDEF PMICNTR_EL0 without any further handling since 4bc0fe0898406 ("KVM: arm64: Add sanitisation for FEAT_FGT2 registers").
arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 3096
Quoted 1 line
arch/arm64/kvm/sys_regs.c +1
-0+ { SYS_DESC(SYS_PMICFILTR_EL0), undef_access },
Same thing.
arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 3100
Quoted 4 lines
arch/arm64/kvm/sys_regs.c +1
-0+ { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr }, { PMU_SYS_REG(PMCNTENSET_EL0),
So none of this is actually required.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v3 03/22] KVM: arm64: Define PMI{CNTR,FILTR}_EL0 as undef_access
Hi Marc. Thanks for the review.
Quoted 37 lines · Marc Zyngier
Quoted 13 lines · Colton Lewis
Because KVM isn't fully prepared to support these yet even though the host PMUv3 driver does, define them as
undef_accessfor now.Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/sys_regs.c | 3 +++ 1 file changed, 3 insertions(+)arch/arm64/kvm/sys_regs.c +0
-0arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = · line 3098
Quoted 7 lines
arch/arm64/kvm/sys_regs.c +1
-0@@ -3092,6 +3092,9 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_SVCR), undef_access, reset_val, SVCR, 0, .visibility = sme_visibility }, { SYS_DESC(SYS_FPMR), undef_access, reset_val, FPMR, 0, .visibility = fp8_visibility }, + { SYS_DESC(SYS_PMICNTR_EL0), undef_access },$ jq -r --arg FEAT "FEAT_PMUv3_ICNTR" -f ./dumpfeat.jq Features.json (FEAT_PMUv3_ICNTR --> v8Ap8) (FEAT_PMUv3_ICNTR --> FEAT_PMUv3p9) ((FEAT_PMUv3_ICNTR && FEAT_AA64EL2) --> FEAT_FGT2)If you have
FEAT_PMUv3_ICNTR, then you haveFEAT_FGT2. If you haveFEAT_FGT2, then we already trap and UNDEFPMICNTR_EL0without any further handling since4bc0fe0898406("KVM: arm64: Add sanitisation forFEAT_FGT2registers").arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 3096
Quoted 1 line
arch/arm64/kvm/sys_regs.c +1
-0+ { SYS_DESC(SYS_PMICFILTR_EL0), undef_access },Same thing.
Quoted 6 lines
Quoted 4 lines
arch/arm64/kvm/sys_regs.c +1
-0+{
PMU_SYS_REG(PMCR_EL0), .access =access_pmcr, .reset =reset_pmcr,
.reg =PMCR_EL0, .get_user =get_pmcr, .set_user =set_pmcr},
{PMU_SYS_REG(PMCNTENSET_EL0),So none of this is actually required.
Thanks for the context. I'll take this patch out.
PATCH v3 4/22 KVM: arm64: Cleanup PMU includes
In reply to Colton LewisFrom: Marc Zyngier <[email protected]>
Reorganize these tangled headers.
* Respect the move defining the interface between KVM and PMU in its own header asm/kvm_pmu.h
* Define an empty struct arm_pmu so it is defined for those interface functions when compiling with CONFIG_KVM but not CONFIG_ARM_PMU
--- arch/arm64/include/asm/arm_pmuv3.h | 2 +- arch/arm64/include/asm/kvm_host.h | 15 +-------------- arch/arm64/include/asm/kvm_pmu.h | 15 +++++++++++++++ arch/arm64/kvm/debug.c | 1 + arch/arm64/kvm/hyp/include/hyp/switch.h | 1 + arch/arm64/kvm/pmu.c | 2 ++ arch/arm64/kvm/sys_regs.c | 1 + include/linux/perf/arm_pmu.h | 5 +++++ virt/kvm/kvm_main.c | 1 + 9 files changed, 28 insertions(+), 15 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +1 -1
@@ -6,7 +6,7 @@#ifndef __ASM_PMUV3_H#define __ASM_PMUV3_H -#include <asm/kvm_host.h> +#include <asm/kvm_pmu.h> #include <asm/cpufeature.h> #include <asm/sysreg.h>
arch/arm64/include/asm/kvm_host.h +1 -14
@@ -14,6 +14,7 @@#include <linux/arm-smccc.h> #include <linux/bitmap.h> #include <linux/types.h> +#include <linux/irq_work.h> #include <linux/jump_label.h> #include <linux/kvm_types.h> #include <linux/maple_tree.h>
@@ -1487,25 +1488,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) {}
arch/arm64/kvm/debug.c +1 -0
@@ -9,6 +9,7 @@#include <linux/kvm_host.h> #include <linux/hw_breakpoint.h> +#include <linux/perf/arm_pmuv3.h> #include <asm/debug-monitors.h> #include <asm/kvm_asm.h>
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -0
@@ -14,6 +14,7 @@#include <linux/kvm_host.h> #include <linux/types.h> #include <linux/jump_label.h> +#include <linux/perf/arm_pmuv3.h> #include <uapi/linux/psci.h> #include <asm/barrier.h>
arch/arm64/kvm/pmu.c +2 -0
@@ -8,6 +8,8 @@#include <linux/perf/arm_pmu.h> #include <linux/perf/arm_pmuv3.h> +#include <asm/kvm_pmu.h> + static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);/*
arch/arm64/kvm/sys_regs.c +1 -0
@@ -18,6 +18,7 @@#include <linux/printk.h> #include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> +#include <linux/perf/arm_pmuv3.h> #include <asm/arm_pmuv3.h> #include <asm/cacheflush.h>
include/linux/perf/arm_pmu.h +5 -0
@@ -187,6 +187,11 @@ void armpmu_free_irq(int irq, int cpu);#define ARMV8_PMU_PDEV_NAME "armv8-pmu" +#else + +struct arm_pmu { +}; +#endif /* CONFIG_ARM_PMU */ #define ARMV8_SPE_PDEV_NAME "arm,spe-v1"
virt/kvm/kvm_main.c +1 -0
@@ -48,6 +48,7 @@#include <linux/lockdep.h> #include <linux/kthread.h> #include <linux/suspend.h> +#include <linux/perf_event.h> #include <asm/processor.h> #include <asm/ioctl.h>
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Mark Rutland
maintainerRe: [PATCH v3 04/22] KVM: arm64: Cleanup PMU includes
Quoted 3 lines · Colton Lewis
From: Marc Zyngier <[email protected]>
Reorganize these tangled headers.
In prior versions of this series, there was some earlier text explaining that there was a circular header dependency. In v2:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
... it said:
| asm/kvm_host.h includes asm/arm_pmu.h which includes perf/arm_pmuv3.h | which includes asm/arm_pmuv3.h which includes asm/kvm_host.h This | causes compilation problems why trying to use anything defined in any | of the headers in any other headers.
Was there some reason for dropping that? It's a bit odd to start without a problem statement.
Quoted 6 lines
* Respect the move defining the interface between KVM and PMU in its own header
asm/kvm_pmu.h* Define an empty struct
arm_pmuso it is defined for those interface functions when compiling withCONFIG_KVMbut notCONFIG_ARM_PMU
Which functions in particular are those? What prevents them from depending on CONFIG_ARM_PMU?
Mark.
Quoted 188 lines
Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/arm_pmuv3.h | 2 +- arch/arm64/include/asm/kvm_host.h | 15 +-------------- arch/arm64/include/asm/kvm_pmu.h | 15 +++++++++++++++ arch/arm64/kvm/debug.c | 1 + arch/arm64/kvm/hyp/include/hyp/switch.h | 1 + arch/arm64/kvm/pmu.c | 2 ++ arch/arm64/kvm/sys_regs.c | 1 + include/linux/perf/arm_pmu.h | 5 +++++ virt/kvm/kvm_main.c | 1 + 9 files changed, 28 insertions(+), 15 deletions(-)arch/arm64/include/asm/arm_pmuv3.h +1
-1@@ -6,7 +6,7 @@#ifndef __ASM_PMUV3_H#define __ASM_PMUV3_H -#include <asm/kvm_host.h> +#include <asm/kvm_pmu.h> #include <asm/cpufeature.h> #include <asm/sysreg.h> arch/arm64/include/asm/kvm_host.h +1
-14@@ -14,6 +14,7 @@#include <linux/arm-smccc.h> #include <linux/bitmap.h> #include <linux/types.h> +#include <linux/irq_work.h> #include <linux/jump_label.h> #include <linux/kvm_types.h> #include <linux/maple_tree.h> @@ -1487,25 +1488,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) {}arch/arm64/kvm/debug.c +1
-0@@ -9,6 +9,7 @@#include <linux/kvm_host.h> #include <linux/hw_breakpoint.h> +#include <linux/perf/arm_pmuv3.h> #include <asm/debug-monitors.h> #include <asm/kvm_asm.h> arch/arm64/kvm/hyp/include/hyp/switch.h +1
-0@@ -14,6 +14,7 @@#include <linux/kvm_host.h> #include <linux/types.h> #include <linux/jump_label.h> +#include <linux/perf/arm_pmuv3.h> #include <uapi/linux/psci.h> #include <asm/barrier.h> arch/arm64/kvm/pmu.c +2
-0@@ -8,6 +8,8 @@#include <linux/perf/arm_pmu.h> #include <linux/perf/arm_pmuv3.h> +#include <asm/kvm_pmu.h> + static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);/* arch/arm64/kvm/sys_regs.c +1
-0@@ -18,6 +18,7 @@#include <linux/printk.h> #include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> +#include <linux/perf/arm_pmuv3.h> #include <asm/arm_pmuv3.h> #include <asm/cacheflush.h> include/linux/perf/arm_pmu.h +5
-0@@ -187,6 +187,11 @@ void armpmu_free_irq(int irq, int cpu);#define ARMV8_PMU_PDEV_NAME "armv8-pmu" +#else + +struct arm_pmu { +}; +#endif /* CONFIG_ARM_PMU */ #define ARMV8_SPE_PDEV_NAME "arm,spe-v1"virt/kvm/kvm_main.c +1
-0@@ -48,6 +48,7 @@#include <linux/lockdep.h> #include <linux/kthread.h> #include <linux/suspend.h> +#include <linux/perf_event.h> #include <asm/processor.h> #include <asm/ioctl.h> Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v3 04/22] KVM: arm64: Cleanup PMU includes
Quoted 13 lines · Mark Rutland
Quoted 3 lines · Colton Lewis
From: Marc Zyngier <[email protected]>
Reorganize these tangled headers.
In prior versions of this series, there was some earlier text explaining that there was a circular header dependency. In v2:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
... it said:
Quoted 7 lines
| asm/kvm_host.h includes asm/arm_pmu.h which includes perf/arm_pmuv3.h | which includes asm/arm_pmuv3.h which includes asm/kvm_host.h This | causes compilation problems why trying to use anything defined in any | of the headers in any other headers.Was there some reason for dropping that? It's a bit odd to start without a problem statement.
When I rebased on top of kvm/queue, that included a patch from Sean that changed the the problem. I thought I wrote a new description but I guess not. I'll include in my next reroll.
The core issue is still that including *all* of kvm_host.h in arm_pmuv3.h is a circular dependency.
asm/kvm_host.h includes asm/kvm_pmu.h includes perf/arm_pmuv3.h includes asm/arm_pmuv3.h includes asm/kvm_host.h
Some other things in this patch are vestigial or needed later, so I should put them in the patches where they are used.
Quoted 10 lines
Quoted 5 lines
* Respect the move defining the interface between KVM and PMU in its own header
asm/kvm_pmu.h* Define an empty struct
arm_pmuso it is defined for those interface functions when compiling withCONFIG_KVMbut notCONFIG_ARM_PMUWhich functions in particular are those? What prevents them from depending on
CONFIG_ARM_PMU?Mark.
Functions I introduce later. So this hunk should be in a later patch if I need to keep it.
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);
The first, at least, has to be called in non-PMU KVM code to check if we can use my ioctl later.
I suppose in that case it's only calling a dummy definition that returns false, so I don't need struct arm_pmu * in the argument and can just use void *
Quoted 193 lines
Quoted 193 lines
Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/arm_pmuv3.h | 2 +- arch/arm64/include/asm/kvm_host.h | 15 +-------------- arch/arm64/include/asm/kvm_pmu.h | 15 +++++++++++++++ arch/arm64/kvm/debug.c | 1 + arch/arm64/kvm/hyp/include/hyp/switch.h | 1 + arch/arm64/kvm/pmu.c | 2 ++ arch/arm64/kvm/sys_regs.c | 1 + include/linux/perf/arm_pmu.h | 5 +++++ virt/kvm/kvm_main.c | 1 + 9 files changed, 28 insertions(+), 15 deletions(-)arch/arm64/include/asm/arm_pmuv3.h +1
-1@@ -6,7 +6,7 @@#ifndef __ASM_PMUV3_H#define __ASM_PMUV3_H -#include <asm/kvm_host.h> +#include <asm/kvm_pmu.h> #include <asm/cpufeature.h> #include <asm/sysreg.h> arch/arm64/include/asm/kvm_host.h +1
-14@@ -14,6 +14,7 @@#include <linux/arm-smccc.h> #include <linux/bitmap.h> #include <linux/types.h> +#include <linux/irq_work.h> #include <linux/jump_label.h> #include <linux/kvm_types.h> #include <linux/maple_tree.h> @@ -1487,25 +1488,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 voidkvm_disable_trbe(void){} static inline void kvm_tracing_set_el1_configuration(u64trfcr_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 voidkvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu){}arch/arm64/kvm/debug.c +1
-0@@ -9,6 +9,7 @@#include <linux/kvm_host.h> #include <linux/hw_breakpoint.h> +#include <linux/perf/arm_pmuv3.h> #include <asm/debug-monitors.h> #include <asm/kvm_asm.h> arch/arm64/kvm/hyp/include/hyp/switch.h +1
-0@@ -14,6 +14,7 @@#include <linux/kvm_host.h> #include <linux/types.h> #include <linux/jump_label.h> +#include <linux/perf/arm_pmuv3.h> #include <uapi/linux/psci.h> #include <asm/barrier.h> arch/arm64/kvm/pmu.c +2
-0@@ -8,6 +8,8 @@#include <linux/perf/arm_pmu.h> #include <linux/perf/arm_pmuv3.h> +#include <asm/kvm_pmu.h> + static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);/* arch/arm64/kvm/sys_regs.c +1
-0@@ -18,6 +18,7 @@#include <linux/printk.h> #include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> +#include <linux/perf/arm_pmuv3.h> #include <asm/arm_pmuv3.h> #include <asm/cacheflush.h> include/linux/perf/arm_pmu.h +5
-0@@ -187,6 +187,11 @@ void armpmu_free_irq(int irq, int cpu);#define ARMV8_PMU_PDEV_NAME "armv8-pmu" +#else + +struct arm_pmu { +}; +#endif /* CONFIG_ARM_PMU */ #define ARMV8_SPE_PDEV_NAME "arm,spe-v1"virt/kvm/kvm_main.c +1
-1@@ -48,6 +48,7 @@#include <linux/lockdep.h> #include <linux/kthread.h> #include <linux/suspend.h> +#include <linux/perf_event.h> #include <asm/processor.h> #include <asm/ioctl.h> --2.50.0.727.gbf7dc18ff4-goog
PATCH v3 5/22 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 | 673 +++++++++++++++++++++++++++++++ 3 files changed, 677 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 +673 -0
686 lines not on this page · raw message
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 6/22 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 at EL1 on the host, partitioning is only allowed in VHE mode. Working on nVHE mode would require a hypercall for every counter access in the driver because the counters reserved for the host by HPMN are only accessible to EL2.
--- arch/arm/include/asm/arm_pmuv3.h | 14 ++++++ arch/arm64/include/asm/arm_pmuv3.h | 5 ++ arch/arm64/include/asm/kvm_pmu.h | 6 +++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-part.c | 23 ++++++++++ drivers/perf/arm_pmuv3.c | 74 +++++++++++++++++++++++++++++- include/linux/perf/arm_pmu.h | 1 + 7 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 arch/arm64/kvm/pmu-part.c
arch/arm/include/asm/arm_pmuv3.h +14 -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;
@@ -228,6 +232,11 @@ static inline bool kvm_set_pmuserenr(u64 val)static inline void kvm_vcpu_pmu_resync_el0 (void ) {} +static inline bool has_vhe (void ) +{ +return false; +} +/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0#define ARMV8_PMU_DFR_VER_V3P1 0x4
@@ -242,6 +251,11 @@ static inline bool pmuv3_implemented(int pmuver) pmuver == ARMV8_PMU_DFR_VER_NI); } +static inline bool is_pmuv3p1 (int pmuver) +{ +return pmuver >=ARMV8_PMU_DFR_VER_V3P1 ; +} +static inline bool is_pmuv3p4 (int pmuver) {return pmuver >=ARMV8_PMU_DFR_VER_V3P4 ;
arch/arm64/include/asm/arm_pmuv3.h +5 -0
@@ -173,6 +173,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 +6 -0
@@ -46,6 +46,7 @@ struct arm_pmu_entry { };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 +116,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
@@ -23,7 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o -kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu-part.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o
arch/arm64/kvm/pmu-part.c new file +23 -0
@@ -0,0 +1,23 @@ +// 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, assumed +* since we are in the PMUv3 driver) +* +* Return: True if partitioning is possible, false otherwise +*/ +bool kvm_pmu_partition_supported (void ) +{ +return kvm_supports_guest_pmuv3 () && +has_vhe (); +}
drivers/perf/arm_pmuv3.c +72 -2
106 lines not on this page · raw message
include/linux/perf/arm_pmu.h +1 -0
@@ -122,6 +122,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.50.0.727.gbf7dc18ff4-goog
Mark Rutland
maintainerRe: [PATCH v3 06/22] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 15 lines · Colton Lewis
For 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_countersto 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'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas the maximum allowable value for HPMN.
Quoted 5 lines
Due to the difficulty this feature would create for the driver running at EL1 on the host, partitioning is only allowed in VHE mode. Working on nVHE mode would require a hypercall for every counter access in the driver because the counters reserved for the host by HPMN are only accessible to EL2.
It would be good if we could elaborate on this last point. When exactly do we intend to configure HPMN (e.g. is that static, dynamic at load/put, or dynamic at finer granularity)?
I ask becuase it's not immediately clear to me how this would break nVHE without also breaking direct userspace access on VHE, unless we flip HPMN dynamically at load/put, and this is only broken in some transient windows on nVHE.
Quoted 8 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 14 ++++++ arch/arm64/include/asm/arm_pmuv3.h | 5 ++ arch/arm64/include/asm/kvm_pmu.h | 6 +++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-part.c | 23 ++++++++++
Maybe I'll contradict Oliver and Marc here (and whatever they say rules), but IMO it'd be nice to spell out "partition" rather than "part" here for clarity.
Mark.
Re: [PATCH v3 06/22] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 22 lines · Mark Rutland
Quoted 15 lines · Colton Lewis
For 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_countersto 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'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas the maximum allowable value for HPMN.Quoted 5 lines
Due to the difficulty this feature would create for the driver running at EL1 on the host, partitioning is only allowed in VHE mode. Working on nVHE mode would require a hypercall for every counter access in the driver because the counters reserved for the host by HPMN are only accessible to EL2.
Quoted 8 lines
It would be good if we could elaborate on this last point. When exactly do we intend to configure HPMN (e.g. is that static, dynamic at load/put, or dynamic at finer granularity)?
I ask becuase it's not immediately clear to me how this would break nVHE without also breaking direct userspace access on VHE, unless we flip HPMN dynamically at load/put, and this is only broken in some transient windows on nVHE.
Agree that KVM's HPMN can only take effect between vcpu_load() / vcpu_put().
The changelog isn't correct regarding the complications of nVHE, though. In order to support a 'partitioned' PMU on nVHE we'd need to explicitly disable guest counters on every exit and reset HPMN to place all counters in the 'first range'. Unless someone has a use case for this stuff on nVHE I'm not too bothered by the VHE-only limitation.
Quoted 9 lines
Quoted 8 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 14 ++++++ arch/arm64/include/asm/arm_pmuv3.h | 5 ++ arch/arm64/include/asm/kvm_pmu.h | 6 +++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-part.c | 23 ++++++++++
Quoted 3 lines
Maybe I'll contradict Oliver and Marc here (and whatever they say rules), but IMO it'd be nice to spell out "partition" rather than "part" here for clarity.
I'm not too big of a fan of the naming here either. I'd prefer something like "pmu-direct". Partitioning is just a side effect of how we're allocating counters currently and most of this implementation could be reused if we pass the entire PMU to the guest in the future.
With that being said -- Colton I'd focus on getting these patches in shape while we figure out what color we want it ;-)
Thanks,
Oliver
Re: [PATCH v3 06/22] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 32 lines · Oliver Upton
Quoted 22 lines · Mark Rutland
Quoted 15 lines · Colton Lewis
For 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_countersto 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'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas the maximum allowable value for HPMN.Quoted 5 lines
Due to the difficulty this feature would create for the driver running at EL1 on the host, partitioning is only allowed in VHE mode. Working on nVHE mode would require a hypercall for every counter access in the driver because the counters reserved for the host by HPMN are only accessible to EL2.
Quoted 8 lines
It would be good if we could elaborate on this last point. When exactly do we intend to configure HPMN (e.g. is that static, dynamic at load/put, or dynamic at finer granularity)?
I ask becuase it's not immediately clear to me how this would break nVHE without also breaking direct userspace access on VHE, unless we flip HPMN dynamically at load/put, and this is only broken in some transient windows on nVHE.
Quoted 8 lines
Agree that KVM's HPMN can only take effect between
vcpu_load()/vcpu_put().The changelog isn't correct regarding the complications of nVHE, though. In order to support a 'partitioned' PMU on nVHE we'd need to explicitly disable guest counters on every exit and reset HPMN to place all counters in the 'first range'. Unless someone has a use case for this stuff on nVHE I'm not too bothered by the VHE-only limitation.
I'll fix this.
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 14 ++++++ arch/arm64/include/asm/arm_pmuv3.h | 5 ++ arch/arm64/include/asm/kvm_pmu.h | 6 +++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-part.c | 23 ++++++++++
Quoted 8 lines
Quoted 3 lines
Maybe I'll contradict Oliver and Marc here (and whatever they say rules), but IMO it'd be nice to spell out "partition" rather than "part" here for clarity.
I'm not too big of a fan of the naming here either. I'd prefer something like "pmu-direct". Partitioning is just a side effect of how we're allocating counters currently and most of this implementation could be reused if we pass the entire PMU to the guest in the future.
Sure.
Quoted 5 lines
With that being said -- Colton I'd focus on getting these patches in shape while we figure out what color we want it ;-)
Thanks,
Oliver
Trust me I'm working on it.
Re: [PATCH v3 06/22] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 65 lines · Colton Lewis
Quoted 32 lines · Oliver Upton
Quoted 22 lines · Mark Rutland
Quoted 15 lines · Colton Lewis
For 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_countersto 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'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas the maximum allowable value for HPMN.Quoted 5 lines
Due to the difficulty this feature would create for the driver running at EL1 on the host, partitioning is only allowed in VHE mode. Working on nVHE mode would require a hypercall for every counter access in the driver because the counters reserved for the host by HPMN are only accessible to EL2.
Quoted 8 lines
It would be good if we could elaborate on this last point. When exactly do we intend to configure HPMN (e.g. is that static, dynamic at load/put, or dynamic at finer granularity)?
I ask becuase it's not immediately clear to me how this would break nVHE without also breaking direct userspace access on VHE, unless we flip HPMN dynamically at load/put, and this is only broken in some transient windows on nVHE.
Quoted 8 lines
Agree that KVM's HPMN can only take effect between
vcpu_load()/vcpu_put().The changelog isn't correct regarding the complications of nVHE, though. In order to support a 'partitioned' PMU on nVHE we'd need to explicitly disable guest counters on every exit and reset HPMN to place all counters in the 'first range'. Unless someone has a use case for this stuff on nVHE I'm not too bothered by the VHE-only limitation.
I'll fix this.
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 14 ++++++ arch/arm64/include/asm/arm_pmuv3.h | 5 ++ arch/arm64/include/asm/kvm_pmu.h | 6 +++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-part.c | 23 ++++++++++Quoted 8 lines
Quoted 3 lines
Maybe I'll contradict Oliver and Marc here (and whatever they say rules), but IMO it'd be nice to spell out "partition" rather than "part" here for clarity.
I'm not too big of a fan of the naming here either. I'd prefer something like "pmu-direct". Partitioning is just a side effect of how we're allocating counters currently and most of this implementation could be reused if we pass the entire PMU to the guest in the future.
Sure.
Quoted 7 lines
Quoted 5 lines
With that being said -- Colton I'd focus on getting these patches in shape while we figure out what color we want it ;-)
Thanks,
OliverTrust me I'm working on it.
I know you are -- this comment wasn't meant to add pressure to you. Just noting that we might bikeshed a bit further on the naming here but I wouldn't worry much about it for the time being.
sed isn't _that_ difficult :)
Thanks,
Oliver
PATCH v3 7/22 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
@@ -513,7 +513,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)
@@ -749,7 +749,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.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v3 07/22] perf: arm_pmuv3: Generalize counter bitmasks
Quoted 4 lines · Colton Lewis
The OVSR bitmasks are valid for enable and interrupt registers as well as overflow registers. Generalize the names.
Signed-off-by Colton Lewis <[email protected]>
FWIW, this looks fine to me, so:
Mark.
Quoted 56 lines
--- 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@@ -513,7 +513,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)@@ -749,7 +749,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.50.0.727.gbf7dc18ff4-goog
Re: [PATCH v3 07/22] perf: arm_pmuv3: Generalize counter bitmasks
Quoted 11 lines · Mark Rutland
Quoted 4 lines · Colton Lewis
The OVSR bitmasks are valid for enable and interrupt registers as well as overflow registers. Generalize the names.
Signed-off-by Colton Lewis <[email protected]>FWIW, this looks fine to me, so:
Acked-by Mark Rutland <[email protected]>Mark.
Thanks.
Quoted 56 lines
Quoted 56 lines
--- 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@@ -513,7 +513,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)@@ -749,7 +749,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
--
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 8/22 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. Partitioning is defined by the MDCR_EL2.HPMN register field and the maximum value KVM can use is saved in cpu_pmu->hpmn_max. The range 0..HPMN-1 is accessible by EL1 and EL0 while HPMN..PMCR.N is reserved for EL2.
Define some functions that take HPMN as an argument and construct mutually exclusive bitmaps for testing which partition a particular counter is in. Note that despite their different 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-part.c | 84 ++++++++++++++++++++++++++++++++ drivers/perf/arm_pmuv3.c | 36 ++++++++++++-- 4 files changed, 158 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 ; +} +static inline bool has_vhe (void ) {
arch/arm64/include/asm/kvm_pmu.h +24 -0
@@ -88,6 +88,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
@@ -220,6 +226,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 (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 ; +} + +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} +#endif #endif
arch/arm64/kvm/pmu-part.c +84 -0
94 lines not on this page · raw message
drivers/perf/arm_pmuv3.c +32 -4
@@ -834,12 +834,18 @@ static void armv8pmu_start(struct arm_pmu *cpu_pmu)kvm_vcpu_pmu_resync_el0 ();/* Enable all counters */ +if (kvm_pmu_is_partitioned (cpu_pmu)) +kvm_pmu_host_counters_enable (); +armv8pmu_pmcr_write (armv8pmu_pmcr_read () |ARMV8_PMU_PMCR_E ); }static void armv8pmu_stop (struct arm_pmu *cpu_pmu) {/* Disable all counters */ +if (kvm_pmu_is_partitioned (cpu_pmu)) +kvm_pmu_host_counters_disable (); +armv8pmu_pmcr_write (armv8pmu_pmcr_read () & ~ARMV8_PMU_PMCR_E ); }
@@ -949,6 +955,7 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,/* Always prefer to place a cycle counter into the cycle counter. */ if ((evtype ==ARMV8_PMUV3_PERFCTR_CPU_CYCLES ) && + !kvm_pmu_is_partitioned (cpu_pmu) && !armv8pmu_event_get_threshold (&event->attr)) {if (!test_and_set_bit (ARMV8_PMU_CYCLE_IDX , cpuc->used_mask))return ARMV8_PMU_CYCLE_IDX ;
@@ -964,6 +971,7 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,* may not know how to handle it. */ if ((evtype ==ARMV8_PMUV3_PERFCTR_INST_RETIRED ) && + !kvm_pmu_is_partitioned (cpu_pmu) && !armv8pmu_event_get_threshold (&event->attr) &&test_bit (ARMV8_PMU_INSTR_IDX , cpu_pmu->cntr_mask) && !armv8pmu_event_want_user_access (event)) {
@@ -975,7 +983,7 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,* Otherwise use events counters */ if (armv8pmu_event_is_chained (event)) -return armv8pmu_get_chain_idx (cpuc, cpu_pmu); +return armv8pmu_get_chain_idx (cpuc, cpu_pmu);else return armv8pmu_get_single_idx (cpuc, cpu_pmu); }
@@ -1067,6 +1075,14 @@ static int armv8pmu_set_event_filter(struct hw_perf_event *event,return 0 ; } +static void armv8pmu_reset_host_counters (struct arm_pmu *cpu_pmu) +{ +int idx; + +for_each_set_bit (idx, cpu_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ) +armv8pmu_write_evcntr (idx,0 ); +} +static void armv8pmu_reset (void *info) {struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
@@ -1074,6 +1090,9 @@ static void armv8pmu_reset(void *info)bitmap_to_arr64 (&mask, cpu_pmu->cntr_mask,ARMPMU_MAX_HWEVENTS ); +if (kvm_pmu_is_partitioned (cpu_pmu)) + mask &=kvm_pmu_host_counter_mask (cpu_pmu); +/* The counter and interrupt enable registers are unknown at reset. */ armv8pmu_disable_counter (mask);armv8pmu_disable_intens (mask);
@@ -1081,11 +1100,20 @@ static void armv8pmu_reset(void *info)/* Clear the counters we flip at guest entry/exit */ kvm_clr_pmu_events (mask); + + pmcr =ARMV8_PMU_PMCR_LC ; +/* -* Initialize & Reset PMNC. Request overflow interrupt for -* 64 bit cycle counter but cheat in armv8pmu_write_counter(). +* Initialize & Reset PMNC. Request overflow interrupt for 64 +* bit cycle counter but cheat in armv8pmu_write_counter(). +* +* When partitioned, there is no single bit to reset only the +* host counters. so reset them individually. */ - pmcr =ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C |ARMV8_PMU_PMCR_LC ; +if (kvm_pmu_is_partitioned (cpu_pmu)) +armv8pmu_reset_host_counters (cpu_pmu); +else + pmcr =ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C ;/* Enable long event counter support where available */ if (armv8pmu_has_long_event (cpu_pmu))
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 9/22 KVM: arm64: Correct kvm_arm_pmu_get_max_counters()
In reply to Colton LewisSince cntr_mask is modified when the PMU is partitioned to remove some bits, make sure the missing counters are added back to get the right total.
--- arch/arm64/kvm/pmu.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu.c +8 -1
@@ -533,6 +533,8 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)u8 kvm_arm_pmu_get_max_counters (struct kvm *kvm) {struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; +u8 counters; +/* * PMUv3 requires that all event counters are capable of counting any
@@ -545,7 +547,12 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)* The arm_pmu->cntr_mask considers the fixed counter(s) as well. * Ignore those and return only the general-purpose counters. */ -return bitmap_weight (arm_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ); + counters =bitmap_weight (arm_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ); + +if (kvm_pmu_is_partitioned (arm_pmu)) + counters += arm_pmu->hpmn_max; + +return counters; }static void kvm_arm_set_nr_counters (struct kvm *kvm,unsigned int nr)
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Marc Zyngier
maintainerRe: [PATCH v3 09/22] KVM: arm64: Correct kvm_arm_pmu_get_max_counters()
Quoted 4 lines · Colton Lewis
Since
cntr_maskis modified when the PMU is partitioned to remove some bits, make sure the missing counters are added back to get the right total.
Please fix the subject of the patch to be more descriptive. It is worded like a bug fix, while it really is only a step in the patch series.
Something like "Take partitioning into account for max number of counters" would go a long way.
Quoted 11 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/pmu.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)arch/arm64/kvm/pmu.c +0
-0
arch/arm64/kvm/pmu.c static bool pmu_irq_is_valid(struct kvm *kvm, int irq) · line 537
Quoted 5 lines
arch/arm64/kvm/pmu.c +2
-0@@ -533,6 +533,8 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)u8 kvm_arm_pmu_get_max_counters (struct kvm *kvm) {struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; +u8 counters; +
nit: superfluous blank line.
arch/arm64/kvm/pmu.c u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) · line 553
Quoted 12 lines
arch/arm64/kvm/pmu.c +4
-1/* * PMUv3 requires that all event counters are capable of counting any @@ -545,7 +547,12 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)* The arm_pmu->cntr_mask considers the fixed counter(s) as well. * Ignore those and return only the general-purpose counters. */ -return bitmap_weight (arm_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ); + counters =bitmap_weight (arm_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ); + +if (kvm_pmu_is_partitioned (arm_pmu)) + counters += arm_pmu->hpmn_max;
Why the check? Why can't we rely on hpmn_max to always give us the correct value?
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v3 09/22] KVM: arm64: Correct kvm_arm_pmu_get_max_counters()
Quoted 7 lines · Marc Zyngier
Quoted 3 lines · Colton Lewis
Since
cntr_maskis modified when the PMU is partitioned to remove some bits, make sure the missing counters are added back to get the right total.
Quoted 6 lines
Please fix the subject of the patch to be more descriptive. It is worded like a bug fix, while it really is only a step in the patch series.
Something like "Take partitioning into account for max number of counters" would go a long way.
Done.
Quoted 5 lines
Quoted 4 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/pmu.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 13 lines
arch/arm64/kvm/pmu.c static bool pmu_irq_is_valid(struct kvm *kvm, int · line 538
Quoted 11 lines
arch/arm64/kvm/pmu.c +2
-0@@ -533,6 +533,8 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq) u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) {struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; +u8 counters; +nit: superfluous blank line.
I'll remove.
Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/pmu.c +0
-1/* * PMUv3 requires that all event counters are capable of counting any @@ -545,7 +547,12 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)* The arm_pmu->cntr_mask considers the fixed counter(s) as well. * Ignore those and return only the general-purpose counters. */ -return bitmap_weight (arm_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS);
Quoted 8 lines
arch/arm64/kvm/pmu.c u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) · line 553
Quoted 5 lines
arch/arm64/kvm/pmu.c +4
-0+ counters =bitmap_weight (arm_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ); + +if (kvm_pmu_is_partitioned (arm_pmu)) + counters += arm_pmu->hpmn_max;Why the check? Why can't we rely on
hpmn_maxto always give us the correct value?
Imagine no partitioning. Then the value of hpmn_max that would be give a correct result here is 0 but 0 is also a valid value (anything from 0 to the number of host counters inclusive) indicating we are partitioned and want to give only the cycle counter to the guest.
My predicate to determine if the PMU is partitoned relies on hpmn_max being a valid value only when the PMU is partitioned.
Making hpmn_max 0 here would require introducing another flag to check partitioning.
Quoted 6 lines
Thanks,
M.--
Without deviation from the norm, progress is not possible.
PATCH v3 10/22 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.
There should be no information leaks between guests as all these registers are context swapped by a later patch in this series.
Untrapped:
* PMCR_EL0
* PMUSERENR_EL0
* PMSELR_EL0
* PMCCNTR_EL0
* PMINTEN_EL0
* PMEVCNTRn_EL0
Trapped:
* PMOVS_EL0
* PMEVTYPERn_EL0
* PMCCFILTR_EL0
* PMICNTR_EL0
* PMICFILTR_EL0
PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.
PMICNTR remains trapped because KVM is not handling that yet.
PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMCIFILTR are the same.
--- arch/arm64/include/asm/kvm_pmu.h | 23 ++++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 58 +++++++++++++++++++++++++ arch/arm64/kvm/pmu-part.c | 32 ++++++++++++++ 3 files changed, 113 insertions(+)
arch/arm64/include/asm/kvm_pmu.h +23 -0
@@ -94,6 +94,21 @@ 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 +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -133,6 +148,14 @@ 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 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/hyp/include/hyp/switch.h +58 -0
70 lines not on this page · raw message
arch/arm64/kvm/pmu-part.c +32 -0
@@ -41,6 +41,38 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) pmu->hpmn_max <= *host_data_ptr (nr_event_counters); } +/** +* kvm_vcpu_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU +* @vcpu: 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` +* +* Return: True if the VCPU PMU is partitioned, false otherwise +*/ +bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu); +} + +/** +* kvm_vcpu_pmu_use_fgt() - Determine if we can use FGT +* @vcpu: Pointer to struct kvm_vcpu +* +* Determine if we can use FGT for direct access to registers. We can +* if capabilities permit the number of guest counters requested. +* +* Return: True if we can use FGT, false otherwise +*/ +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) && +cpus_have_final_cap (ARM64_HAS_FGT ) && + (hpmn !=0 ||cpus_have_final_cap (ARM64_HAS_HPMN0 )); +} +/** * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters * @pmu: Pointer to arm_pmu struct
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Marc Zyngier
maintainerRe: [PATCH v3 10/22] KVM: arm64: Set up FGT for Partitioned PMU
Quoted 24 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.There should be no information leaks between guests as all these registers are context swapped by a later patch in this series.
Untrapped:
*PMCR_EL0
*PMUSERENR_EL0
*PMSELR_EL0
*PMCCNTR_EL0
*PMINTEN_EL0
*PMEVCNTRn_EL0Trapped:
*PMOVS_EL0
*PMEVTYPERn_EL0
*PMCCFILTR_EL0
*PMICNTR_EL0
*PMICFILTR_EL0
Quoted 8 lines
PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.
PMICNTR remains trapped because KVM is not handling that yet.
PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMCIFILTR are the same.
I'd rather you explain why it is safe not to trap the rest.
Quoted 100 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_pmu.h | 23 ++++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 58 +++++++++++++++++++++++++ arch/arm64/kvm/pmu-part.c | 32 ++++++++++++++ 3 files changed, 113 insertions(+)arch/arm64/include/asm/kvm_pmu.h +23
-0@@ -94,6 +94,21 @@ 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 +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure @@ -133,6 +148,14 @@ 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 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/hyp/include/hyp/switch.h +43
-0@@ -191,6 +191,61 @@ static inline bool cpu_has_amu(void) ID_AA64PFR0_EL1_AMU_SHIFT); } +/** +* __activate_pmu_fgt() - Activate fine grain traps for partitioned PMU +* @vcpu: Pointer to struct kvm_vcpu +* +* Clear the most commonly accessed registers for a partitioned +* PMU. Trap the rest. +*/ +static inline void __activate_pmu_fgt (struct kvm_vcpu *vcpu) +{ +struct kvm_cpu_context *hctxt =host_data_ptr (host_ctxt); +struct kvm *kvm =kern_hyp_va (vcpu->kvm); +u64 set; +u64 clr; + + set =HDFGRTR_EL2_PMOVS + |HDFGRTR_EL2_PMCCFILTR_EL0 + | HDFGRTR_EL2_PMEVTYPERn_EL0; + clr =HDFGRTR_EL2_PMUSERENR_EL0 + |HDFGRTR_EL2_PMSELR_EL0 + |HDFGRTR_EL2_PMINTEN + |HDFGRTR_EL2_PMCNTEN + |HDFGRTR_EL2_PMCCNTR_EL0 + | HDFGRTR_EL2_PMEVCNTRn_EL0; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGRTR_EL2 , clr, set); + + set =HDFGWTR_EL2_PMOVS + |HDFGWTR_EL2_PMCCFILTR_EL0 + | HDFGWTR_EL2_PMEVTYPERn_EL0; + clr =HDFGWTR_EL2_PMUSERENR_EL0 + |HDFGWTR_EL2_PMCR_EL0 + |HDFGWTR_EL2_PMSELR_EL0 + |HDFGWTR_EL2_PMINTEN + |HDFGWTR_EL2_PMCNTEN + |HDFGWTR_EL2_PMCCNTR_EL0 + | HDFGWTR_EL2_PMEVCNTRn_EL0; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGWTR_EL2 , clr, set); + +if (!cpus_have_final_cap (ARM64_HAS_FGT2 )) +return ; + + set = HDFGRTR2_EL2_nPMICFILTR_EL0
arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool cpu_has_amu(void) · line 246
Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +10
-0+ | HDFGRTR2_EL2_nPMICNTR_EL0; + clr =0 ; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGRTR2_EL2 , clr, set); + + set = HDFGWTR2_EL2_nPMICFILTR_EL0 + | HDFGWTR2_EL2_nPMICNTR_EL0; + clr =0 ; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGWTR2_EL2 , clr, set);
This feels wrong. There should be one place to populate the FGTs that apply to a guest as set from the host, not two or more.
There is such a construct in the SME series, and maybe you could have a look at it, specially if the trap configuration is this static.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v3 10/22] KVM: arm64: Set up FGT for Partitioned PMU
Quoted 29 lines · Marc Zyngier
Quoted 25 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.There should be no information leaks between guests as all these registers are context swapped by a later patch in this series.
Untrapped:
*PMCR_EL0
*PMUSERENR_EL0
*PMSELR_EL0
*PMCCNTR_EL0
*PMINTEN_EL0
*PMEVCNTRn_EL0Trapped:
*PMOVS_EL0
*PMEVTYPERn_EL0
*PMCCFILTR_EL0
*PMICNTR_EL0
*PMICFILTR_EL0PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.
Quoted 7 lines
Quoted 5 lines
PMICNTR remains trapped because KVM is not handling that yet.
PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMCIFILTR are the same.
I'd rather you explain why it is safe not to trap the rest.
Okay, I will reverse my explanation.
Quoted 115 lines
Quoted 101 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_pmu.h | 23 ++++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 58 +++++++++++++++++++++++++ arch/arm64/kvm/pmu-part.c | 32 ++++++++++++++ 3 files changed, 113 insertions(+)arch/arm64/include/asm/kvm_pmu.h +15
-0@@ -94,6 +94,21 @@ 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 +/* * 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 +8
-0@@ -133,6 +148,14 @@ 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 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/hyp/include/hyp/switch.h +43
-0@@ -191,6 +191,61 @@ static inline bool cpu_has_amu(void) ID_AA64PFR0_EL1_AMU_SHIFT); } +/** +* __activate_pmu_fgt() - Activate fine grain traps for partitioned PMU +* @vcpu: Pointer to struct kvm_vcpu +* +* Clear the most commonly accessed registers for a partitioned +* PMU. Trap the rest. +*/ +static inline void __activate_pmu_fgt (struct kvm_vcpu *vcpu) +{ +struct kvm_cpu_context *hctxt =host_data_ptr (host_ctxt); +struct kvm *kvm =kern_hyp_va (vcpu->kvm); +u64 set; +u64 clr; + + set =HDFGRTR_EL2_PMOVS + |HDFGRTR_EL2_PMCCFILTR_EL0 + | HDFGRTR_EL2_PMEVTYPERn_EL0; + clr =HDFGRTR_EL2_PMUSERENR_EL0 + |HDFGRTR_EL2_PMSELR_EL0 + |HDFGRTR_EL2_PMINTEN + |HDFGRTR_EL2_PMCNTEN + |HDFGRTR_EL2_PMCCNTR_EL0 + | HDFGRTR_EL2_PMEVCNTRn_EL0; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGRTR_EL2 , clr, set); + + set =HDFGWTR_EL2_PMOVS + |HDFGWTR_EL2_PMCCFILTR_EL0 + | HDFGWTR_EL2_PMEVTYPERn_EL0; + clr =HDFGWTR_EL2_PMUSERENR_EL0 + |HDFGWTR_EL2_PMCR_EL0 + |HDFGWTR_EL2_PMSELR_EL0 + |HDFGWTR_EL2_PMINTEN + |HDFGWTR_EL2_PMCNTEN + |HDFGWTR_EL2_PMCCNTR_EL0 + | HDFGWTR_EL2_PMEVCNTRn_EL0; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGWTR_EL2 , clr, set); + +if (!cpus_have_final_cap (ARM64_HAS_FGT2 )) +return ; + + set = HDFGRTR2_EL2_nPMICFILTR_EL0arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool cpu_has_amu(void) · line 246
Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +10
-0+ | HDFGRTR2_EL2_nPMICNTR_EL0; + clr =0 ; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGRTR2_EL2 , clr, set); + + set = HDFGWTR2_EL2_nPMICFILTR_EL0 + | HDFGWTR2_EL2_nPMICNTR_EL0; + clr =0 ; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGWTR2_EL2 , clr, set);This feels wrong. There should be one place to populate the FGTs that apply to a guest as set from the host, not two or more.
Quoted 7 lines
There is such a construct in the SME series, and maybe you could have a look at it, specially if the trap configuration is this static.
M.--
Without deviation from the norm, progress is not possible.
I'm assuming you are referring to Mark Brown's series [1], specifically patches 5 and 18 and I see what you mean.
You are probably thinking configuration should happen from sys_regs.c:kvm_calculate_traps or thereabout and should be setting bits in the existing kvm->arch.fgt array.
Correct me if I'm mistaken.
Marc Zyngier
maintainerRe: [PATCH v3 10/22] KVM: arm64: Set up FGT for Partitioned PMU
Quoted 64 lines · Colton Lewis
Quoted 53 lines · Marc Zyngier
Quoted 36 lines · Colton Lewis
arch/arm64/kvm/hyp/include/hyp/switch.h +36
-0+static inline void __activate_pmu_fgt (struct kvm_vcpu *vcpu) +{ +struct kvm_cpu_context *hctxt =host_data_ptr (host_ctxt); +struct kvm *kvm =kern_hyp_va (vcpu->kvm); +u64 set; +u64 clr; + + set =HDFGRTR_EL2_PMOVS + |HDFGRTR_EL2_PMCCFILTR_EL0 + | HDFGRTR_EL2_PMEVTYPERn_EL0; + clr =HDFGRTR_EL2_PMUSERENR_EL0 + |HDFGRTR_EL2_PMSELR_EL0 + |HDFGRTR_EL2_PMINTEN + |HDFGRTR_EL2_PMCNTEN + |HDFGRTR_EL2_PMCCNTR_EL0 + | HDFGRTR_EL2_PMEVCNTRn_EL0; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGRTR_EL2 , clr, set); + + set =HDFGWTR_EL2_PMOVS + |HDFGWTR_EL2_PMCCFILTR_EL0 + | HDFGWTR_EL2_PMEVTYPERn_EL0; + clr =HDFGWTR_EL2_PMUSERENR_EL0 + |HDFGWTR_EL2_PMCR_EL0 + |HDFGWTR_EL2_PMSELR_EL0 + |HDFGWTR_EL2_PMINTEN + |HDFGWTR_EL2_PMCNTEN + |HDFGWTR_EL2_PMCCNTR_EL0 + | HDFGWTR_EL2_PMEVCNTRn_EL0; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGWTR_EL2 , clr, set); + +if (!cpus_have_final_cap (ARM64_HAS_FGT2 )) +return ; + + set = HDFGRTR2_EL2_nPMICFILTR_EL0arch/arm64/kvm/hyp/include/hyp/switch.h static inline bool cpu_has_amu(void) · line 246
Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +10
-0+ | HDFGRTR2_EL2_nPMICNTR_EL0; + clr =0 ; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGRTR2_EL2 , clr, set); + + set = HDFGWTR2_EL2_nPMICFILTR_EL0 + | HDFGWTR2_EL2_nPMICNTR_EL0; + clr =0 ; + +update_fgt_traps_cs (hctxt, vcpu, kvm,HDFGWTR2_EL2 , clr, set);This feels wrong. There should be one place to populate the FGTs that apply to a guest as set from the host, not two or more.
Quoted 7 lines
There is such a construct in the SME series, and maybe you could have a look at it, specially if the trap configuration is this static.
M.--
Without deviation from the norm, progress is not possible.
Quoted 8 lines
I'm assuming you are referring to Mark Brown's series [1], specifically patches 5 and 18 and I see what you mean.
You are probably thinking configuration should happen from sys_regs.c:kvm_calculate_traps or thereabout and should be setting bits in the existing
kvm->arch.fgtarray.Correct me if I'm mistaken.
I'm saying there should be exactly one place where we write to the individual trap registers, and that the source of these settings should be equally unique when they are immutable in the lifetime of the guest.
That's the existing pattern for most trap configuration, including HCR_EL2, ICH_HCR_EL2, HCRX_EL2, and the FGU configuration that trickles into the actual trap registers, and I want to stick with it if at all possible.
The way it is done in the SME series may be reasonable, but I haven't reviewed this series at all. I'm merely pointing out that similar constructs exist for other features.
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
PATCH v3 11/22 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 delaying when guest writes take effect.
--- arch/arm64/kvm/sys_regs.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +28 -1
@@ -18,6 +18,7 @@#include <linux/printk.h> #include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> +#include <linux/perf/arm_pmu.h> #include <linux/perf/arm_pmuv3.h> #include <asm/arm_pmuv3.h>
@@ -1037,6 +1038,30 @@ 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; + +if (idx ==ARMV8_PMU_CYCLE_IDX ) + eventsel =ARMV8_PMUV3_PERFCTR_CPU_CYCLES ; +else + eventsel = p->regval &kvm_pmu_evtyper_mask (vcpu->kvm); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (eventsel, vcpu->kvm->arch.pmu_filter)) +return false; + +__vcpu_assign_sys_reg (vcpu, reg, eventsel); + +if (idx ==ARMV8_PMU_CYCLE_IDX ) +write_pmccfiltr (eventsel); +else +write_pmevtypern (idx, eventsel); + +return true; +} +static bool access_pmu_evtyper (struct kvm_vcpu *vcpu,struct sys_reg_params *p,const struct sys_reg_desc *r) {
@@ -1063,7 +1088,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.50.0.727.gbf7dc18ff4-goog
PATCH v3 12/22 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.
--- 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
@@ -72,11 +72,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
@@ -1065,14 +1065,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.50.0.727.gbf7dc18ff4-goog
PATCH v3 13/22 KVM: arm64: Writethrough trapped PMOVS 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 delaying when guest writes take effect.
--- 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
@@ -142,6 +142,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
@@ -1173,6 +1173,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) {
@@ -1181,7 +1194,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.50.0.727.gbf7dc18ff4-goog
PATCH v3 14/22 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. Add a handler for those registers in the fast path so we can still get a performance boost from partitioning.
--- arch/arm64/include/asm/arm_pmuv3.h | 37 ++++- arch/arm64/include/asm/kvm_pmu.h | 10 ++ arch/arm64/kvm/hyp/include/hyp/switch.h | 174 ++++++++++++++++++++++++ arch/arm64/kvm/pmu.c | 16 +++ arch/arm64/kvm/sys_regs.c | 16 --- 5 files changed, 236 insertions(+), 17 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +36 -1
@@ -41,6 +41,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 );
@@ -107,21 +117,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);
@@ -162,11 +192,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 +10 -0
@@ -81,6 +81,8 @@ 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);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 );
@@ -214,6 +216,14 @@ 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 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 +174 -0
198 lines not on this page · raw message
arch/arm64/kvm/pmu.c +16 -0
@@ -891,3 +891,19 @@ 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 ); +}
arch/arm64/kvm/sys_regs.c +0 -16
@@ -842,22 +842,6 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)return __vcpu_sys_reg (vcpu, r->reg); } -static 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; -} - -static bool pmu_access_el0_disabled (struct kvm_vcpu *vcpu) -{ -return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_EN ); -} -static bool pmu_write_swinc_el0_disabled (struct kvm_vcpu *vcpu) {return check_pmu_access_disabled (vcpu,ARMV8_PMU_USERENR_SW |ARMV8_PMU_USERENR_EN );
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 15/22 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 maximum setting the host allows (which implies no partition) so hardware enforces that a guest will only see the counters in the guest partition.
With HPMN set, we can now leave the TPM and TPMCR bits unset unless FGT is not available, in which case we need to fall back to that.
Also, if available, set the filtering bits HPMD and HCCD to be extra sure nothing counts at EL2.
--- arch/arm64/include/asm/kvm_pmu.h | 11 ++++++ arch/arm64/kvm/debug.c | 23 ++++++++++--- arch/arm64/kvm/pmu-part.c | 57 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +11 -0
@@ -96,6 +96,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);
@@ -158,6 +161,14 @@ static inline bool kvm_vcpu_pmu_use_fgt(struct kvm_vcpu *vcpu) {return false; } +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 +18 -5
@@ -37,15 +37,28 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)* 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 |= (MDCR_EL2_TPM | -MDCR_EL2_TPMS | -MDCR_EL2_TTRF | + vcpu->arch.mdcr_el2 =FIELD_PREP (MDCR_EL2_HPMN ,kvm_pmu_hpmn (vcpu)); + vcpu->arch.mdcr_el2 |= (MDCR_EL2_TTRF |MDCR_EL2_TPMCR |MDCR_EL2_TDRA |MDCR_EL2_TDOSA ); +if (kvm_vcpu_pmu_is_partitioned (vcpu) + &&is_pmuv3p1 (read_pmuver ())) { +/* +* 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. +*/ + vcpu->arch.mdcr_el2 |=MDCR_EL2_HPMD ; + +if (is_pmuv3p5 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HCCD ; + } + +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)/* Route all software debug exceptions to EL2 */
arch/arm64/kvm/pmu-part.c +57 -0
60 lines not on this page · raw message
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 16/22 KVM: arm64: Account for partitioning in PMCR_EL0 access
In reply to Colton LewisFor some reason unknown to me, KVM allows writes to PMCR_EL0.N even though the architecture specifies that field as RO. Make sure these accesses conform to additional constraints imposed when the PMU is partitioned.
--- arch/arm64/kvm/pmu.c | 2 +- arch/arm64/kvm/sys_regs.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kvm/pmu.c +1 -1
@@ -884,7 +884,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 +3 -1
@@ -1249,7 +1249,9 @@ 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_arm_pmu_get_max_counters (kvm) && + (!kvm_vcpu_pmu_is_partitioned (vcpu) || + new_n <=kvm_pmu_hpmn (vcpu))) kvm->arch.nr_pmu_counters = new_n;mutex_unlock (&kvm->arch.config_lock);
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 17/22 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 the PMU is not partitioned or MDCR_EL2.TPM is set, all PMU registers are trapped so return immediately.
--- arch/arm64/include/asm/kvm_pmu.h | 4 ++ arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/pmu-part.c | 101 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+)
arch/arm64/include/asm/kvm_pmu.h +4 -0
@@ -98,6 +98,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);
@@ -169,6 +171,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
@@ -616,6 +616,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);
@@ -658,6 +659,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/pmu-part.c +101 -0
110 lines not on this page · raw message
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 18/22 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. If the event is filtered, exclude counting at all exception levels before writing the hardware.
--- arch/arm64/kvm/pmu-part.c | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
arch/arm64/kvm/pmu-part.c +43 -0
@@ -196,6 +196,47 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return hpmn; } +/** +* 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 =kvm_pmu_evtyper_mask (vcpu->kvm) + & ~kvm_pmu_event_mask (vcpu->kvm) + & ~ARMV8_PMU_INCLUDE_EL2 ; +u64 evtyper_clr =ARMV8_PMU_INCLUDE_EL2 ; +u8 i; +u64 val; + +for (i =0 ; i < pmu->hpmn_max; i++) { + val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + i); + +if (vcpu->kvm->arch.pmu_filter && + !test_bit (val, 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
@@ -218,6 +259,8 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)if (!kvm_pmu_is_partitioned (pmu) || (vcpu->arch.mdcr_el2 &MDCR_EL2_TPM ))return ; +kvm_pmu_apply_event_filter (vcpu); +for (i =0 ; i < pmu->hpmn_max; i++) { val =__vcpu_sys_reg (vcpu,PMEVCNTR0_EL0 + i);write_pmevcntrn (i, val);
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 19/22 perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
In reply to Colton LewisGuest 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-part.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) {}static inline bool has_vhe (void ) {
arch/arm64/include/asm/kvm_pmu.h +2 -0
@@ -95,6 +95,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);
@@ -291,6 +292,7 @@ static inline u64 kvm_pmu_guest_counter_mask(struct arm_pmu *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-part.c +17 -0
@@ -338,3 +338,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
@@ -750,6 +750,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;
@@ -852,6 +854,7 @@ static void armv8pmu_stop(struct arm_pmu *cpu_pmu)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;
@@ -906,6 +909,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.50.0.727.gbf7dc18ff4-goog
PATCH v3 20/22 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-emul.c | 4 ++-- arch/arm64/kvm/pmu-part.c | 22 +++++++++++++++++++++- arch/arm64/kvm/pmu.c | 6 +++++- 4 files changed, 30 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_pmu.h +2 -0
@@ -86,6 +86,8 @@ bool pmu_access_el0_disabled(struct kvm_vcpu *vcpu);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-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-part.c +21 -1
@@ -279,7 +279,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)write_pmcr (val);/* -* Loading these registers is tricky because of +* Loading these registers is more intricate because of * 1. Applying only the bits for guest counters (indicated by mask) * 2. Setting and clearing are different registers */
@@ -355,3 +355,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.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.50.0.727.gbf7dc18ff4-goog
PATCH v3 21/22 KVM: arm64: Add ioctl to partition the PMU when supported
In reply to Colton LewisAdd KVM_ARM_PMU_PARTITION to enable the partitioned PMU for a given vCPU. Add a corresponding KVM_CAP_ARM_PMU_PARTITION to check for this ability. This capability is allowed on an initialized vCPU where PMUv3 and VHE are supported.
However, because the underlying ability relies on the driver being passed some command line arguments to configure the hardware partition at boot, enabling the partitioned PMU will not be allowed without the underlying driver configuration even though the capability exists.
--- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 20 ++++++++++++++++++++ arch/arm64/kvm/pmu-part.c | 3 ++- include/uapi/linux/kvm.h | 4 ++++ 5 files changed, 50 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +21 -0
@@ -6478,6 +6478,27 @@ the capability to be present. `flags` must currently be zero. +4.144 KVM_ARM_PARTITION_PMU +--------------------------- + +:Capability: KVM_CAP_ARM_PARTITION_PMU +:Architectures: arm64 +:Type: vcpu ioctl +:Parameters: arg[0] is a boolean to enable the partitioned PMU + +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 parameters `arm-pmuv3.partition_pmu=y` and +`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 +that shares the host partition. .. _kvm_run:
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -369,6 +369,9 @@ struct kvm_arch {/* Maximum number of counters for the guest */ u8 nr_pmu_counters; +/* Whether this guest uses the partitioned PMU */ +bool partitioned_pmu_enable; +/* Iterator for idreg debugfs */ u8 idreg_debugfs_iter;
arch/arm64/kvm/arm.c +20 -0
@@ -21,6 +21,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
@@ -38,6 +39,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>
@@ -383,6 +385,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_supported (); +break ;case KVM_CAP_ARM_INJECT_SERROR_ESR : r =cpus_have_final_cap (ARM64_HAS_RAS_EXTN );break ;
@@ -1810,6 +1815,21 @@ long kvm_arch_vcpu_ioctl(struct file *filp,return kvm_arm_vcpu_finalize (vcpu, what); } +case KVM_ARM_PARTITION_PMU : { +bool enable; + +if (unlikely (!kvm_vcpu_initialized (vcpu))) +return -ENOEXEC ; + +if (!kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu)) +return -EPERM ; + +if (copy_from_user (&enable, argp,sizeof (enable))) +return -EFAULT ; + + vcpu->kvm->arch.partitioned_pmu_enable = enable; +return 0 ; + }default : r = -EINVAL ; }
arch/arm64/kvm/pmu-part.c +2 -1
@@ -53,7 +53,8 @@ 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); +return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu) + && vcpu->kvm->arch.partitioned_pmu_enable; }/**
include/uapi/linux/kvm.h +4 -0
@@ -935,6 +935,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_EL2_E2H0 241#define KVM_CAP_RISCV_MP_STATE_RESET 242#define KVM_CAP_GMEM_SHARED_MEM 243 +#define KVM_CAP_ARM_PARTITION_PMU 244struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1413,6 +1414,9 @@ struct kvm_enc_region {#define KVM_GET_SREGS2 _IOR(KVMIO, 0xcc, struct kvm_sregs2)#define KVM_SET_SREGS2 _IOW(KVMIO, 0xcd, struct kvm_sregs2) +/* Available with KVM_CAP_ARM_PARTITION_PMU */ +#define KVM_ARM_PARTITION_PMU _IOWR(KVMIO, 0xce, bool) +#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0)#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1)
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH v3 22/22 KVM: arm64: selftests: Add test case for partitioned PMU
In reply to Colton LewisRun separate a test case for a partitioned PMU in vpmu_counter_access.
An enum is created 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.
Because the test should still succeed even if we are on a machine where we have the capability but the ioctl fails because the driver was never configured properly, use __vcpu_ioctl to avoid checking the return code.
--- tools/include/uapi/linux/kvm.h | 2 + .../selftests/kvm/arm64/vpmu_counter_access.c | 62 +++++++++++++------ 2 files changed, 46 insertions(+), 18 deletions(-)
tools/include/uapi/linux/kvm.h +2 -0
@@ -930,6 +930,7 @@ struct kvm_enable_cap {#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237#define KVM_CAP_X86_GUEST_MODE 238#define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239 +#define KVM_CAP_ARM_PARTITION_PMU 244struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1356,6 +1357,7 @@ struct kvm_vfio_spapr_tce {#define KVM_S390_SET_CMMA_BITS _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log)/* Memory Encryption Commands */ #define KVM_MEMORY_ENCRYPT_OP _IOWR(KVMIO, 0xba, unsigned long) +#define KVM_ARM_PARTITION_PMU _IOWR(KVMIO, 0xce, bool)struct kvm_enc_region {__u64 addr;
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +44 -18
150 lines not on this page · raw message
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog