Skip to contentSkip to conversation

perf: arm_pmuv3: Remove cyclical dependency with kvm_host.h

PATCH In review Colton Lewis 2 participants 6 messages Feb 4, 2025 – Feb 5, 2025

Subsystems (5)
Arm (32-bit) KVM arm64 KVM arm64 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf: arm_pmuv3: Remove cyclical dependency with kvm_host.h

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 confusing compilation problems when trying to use anything in the chain.

Break the cycle by taking asm/kvm_host.h out of asm/arm_pmuv3.h because asm/kvm_host.h is huge and we only need a few functions from it. Move the required declarations to asm/arm_pmuv3.h.

---
 arch/arm64/include/asm/arm_pmuv3.h | 24 ++++++++++++++++++++++--
 arch/arm64/include/asm/kvm_host.h  | 18 ------------------
 include/kvm/arm_pmu.h              |  1 -
 3 files changed, 22 insertions(+), 21 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +22 -2
index 8a777dec8d88..89fd6abb7da6 100644
@@ -6,11 +6,31 @@
 #ifndef __ASM_PMUV3_H
 #define __ASM_PMUV3_H
 
-#include <asm/kvm_host.h>
-
 #include <asm/cpufeature.h>
 #include <asm/sysreg.h>
 
+#include <linux/perf_event.h>
+
+#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);
+#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;
+}
+#endif
+
+static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
+{
+	return (!has_vhe() && attr->exclude_host);
+}
+
+void kvm_vcpu_pmu_resync_el0(void);
+
 #define RETURN_READ_PMEVCNTRN(n) \
 	return read_sysreg(pmevcntr##n##_el0)
 static inline unsigned long read_pmevcntrn(int n)
arch/arm64/include/asm/kvm_host.h +0 -18
index e18e9244d17a..3eeb762944c9 100644
@@ -1362,28 +1362,10 @@ 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);
-}
-
 /* Flags for host debug state */
 void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
 
-#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);
-#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;
-}
-#endif
-
 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu);
 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu);
 
include/kvm/arm_pmu.h +0 -1
index 147bd3ee4f7b..2c78b1b1a9bb 100644
@@ -74,7 +74,6 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu);
 struct kvm_pmu_events *kvm_get_pmu_events(void);
 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);
 
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))

base-commit: 5bc55a333a2f7316b58edc7573e8e893f7acb532

Signature · 1 line
2.48.1.362.g079036d154-goog
#1 lore raw

PATCH 2/2 perf: arm_pmuv3: Uninvert dependency between {asm,perf}/arm_pmuv3.h

perf/arm_pmuv3.h includes asm/arm_pmuv3.h at the bottom of the file. This counterintiutive decision was presumably made so asm/arm_pmuv3.h would be included everywhere perf/arm_pmuv3.h was even though the actual dependency relationship goes the other way because asm/arm_pmuv3.h depends on the PMEVN_SWITCH macro that was presumably put there to avoid duplicating it in the asm files for arm and arm64.

Extract the relevant macro to its own file to avoid this unusual structure so it may be included in the asm headers without worrying about ordering issues.

---
 arch/arm/include/asm/arm_pmuv3.h   |  2 ++
 arch/arm64/include/asm/arm_pmuv3.h |  1 +
 include/linux/perf/arm_pmuv3.h     | 49 ++-------------------------
 include/linux/perf/pmevn_switch.h  | 54 ++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 47 deletions(-)
 create mode 100644 include/linux/perf/pmevn_switch.h
arch/arm/include/asm/arm_pmuv3.h +2 -0
index 2ec0e5e83fc9..a39277b6a365 100644
@@ -9,6 +9,8 @@
 #include <asm/cp15.h>
 #include <asm/cputype.h>
 
+#include <linux/perf/pmevn_switch.h>
+
 #define PMCCNTR			__ACCESS_CP15_64(0, c9)
 
 #define PMCR			__ACCESS_CP15(c9,  0, c12, 0)
arch/arm64/include/asm/arm_pmuv3.h +1 -0
index 89fd6abb7da6..e22fb26b6169 100644
@@ -10,6 +10,7 @@
 #include <asm/sysreg.h>
 
 #include <linux/perf_event.h>
+#include <linux/perf/pmevn_switch.h>
 
 #ifdef CONFIG_KVM
 void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr);
include/linux/perf/arm_pmuv3.h +2 -47

59 lines not on this page · raw message

include/linux/perf/pmevn_switch.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.48.1.362.g079036d154-goog
#2 lore raw

Re: [PATCH 2/2] perf: arm_pmuv3: Uninvert dependency between {asm,perf}/arm_pmuv3.h

Quoted 7 lines · Colton Lewis

perf/arm_pmuv3.h includes asm/arm_pmuv3.h at the bottom of the file. This counterintiutive decision was presumably made so asm/arm_pmuv3.h would be included everywhere perf/arm_pmuv3.h was even though the actual dependency relationship goes the other way because asm/arm_pmuv3.h depends on the PMEVN_SWITCH macro that was presumably put there to avoid duplicating it in the asm files for arm and arm64.

Quoted 5 lines

Extract the relevant macro to its own file to avoid this unusual structure so it may be included in the asm headers without worrying about ordering issues.

Is the intention of this change to allow asm/arm_pmuv3.h to be directly included? If yes, what's the issue with using perf/arm_pmuv3.h?

We already use definitions from the non-arch header in KVM anyway...

Signature · 2 lines
Thanks,
Oliver
#3 lore raw

Re: [PATCH 2/2] perf: arm_pmuv3: Uninvert dependency between {asm,perf}/arm_pmuv3.h

Quoted 8 lines · Oliver Upton
Quoted 6 lines · Colton Lewis

perf/arm_pmuv3.h includes asm/arm_pmuv3.h at the bottom of the file. This counterintiutive decision was presumably made so asm/arm_pmuv3.h would be included everywhere perf/arm_pmuv3.h was even though the actual dependency relationship goes the other way because asm/arm_pmuv3.h depends on the PMEVN_SWITCH macro that was presumably put there to avoid duplicating it in the asm files for arm and arm64.

Quoted 8 lines
Quoted 5 lines

Extract the relevant macro to its own file to avoid this unusual structure so it may be included in the asm headers without worrying about ordering issues.

Is the intention of this change to allow asm/arm_pmuv3.h to be directly included? If yes, what's the issue with using perf/arm_pmuv3.h?

That isn't the primary intent, but it's a good side effect. Headers that can't be directly included violate people's expectations.

Quoted 1 line

We already use definitions from the non-arch header in KVM anyway...

My intention here was just reorganizing a counterintuitive use of headers. The arch header depends on definitions in the non-arch header even though the inclusion relationship is the other way around.

But this patch doesn't matter as much as fixing the cyclical dependency.

#4 lore raw
In reply to Colton Lewis

Hi Colton,

Quoted 10 lines · Colton Lewis

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 confusing compilation problems when trying to use anything in the chain.

Break the cycle by taking asm/kvm_host.h out of asm/arm_pmuv3.h because asm/kvm_host.h is huge and we only need a few functions from it. Move the required declarations to asm/arm_pmuv3.h.

Please do not move KVM namespaced functions into non-KVM headers. Having a separate header for KVM<->PMUv3 driver interfaces is probably the right thing to do, especially since you're going to be adding more with partitioned PMU support.

Signature · 2 lines
Thanks,
Oliver
#5 lore raw
Quoted 12 lines · Oliver Upton

Hi Colton,

Quoted 8 lines · Colton Lewis

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 confusing compilation problems when trying to use anything in the chain.

Break the cycle by taking asm/kvm_host.h out of asm/arm_pmuv3.h because asm/kvm_host.h is huge and we only need a few functions from it. Move the required declarations to asm/arm_pmuv3.h.

Quoted 6 lines
Quoted 1 line

Please do not move KVM namespaced functions into non-KVM headers. Having a separate header for KVM<->PMUv3 driver interfaces is probably the right thing to do, especially since you're going to be adding more with partitioned PMU support.

That seems like a good idea to me.