Skip to content

Commit 6b5b368

Browse files
Marc Zyngierbonzini
authored andcommitted
KVM: arm64: Turn kvm_arm_support_pmu_v3() into a static key
We currently find out about the presence of a HW PMU (or the handling of that PMU by perf, which amounts to the same thing) in a fairly roundabout way, by checking the number of counters available to perf. That's good enough for now, but we will soon need to find about about that on paths where perf is out of reach (in the world switch). Instead, let's turn kvm_arm_support_pmu_v3() into a static key. Signed-off-by: Marc Zyngier <maz@kernel.org> Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com> Link: https://lore.kernel.org/r/20210209114844.3278746-2-maz@kernel.org Message-Id: <20210305185254.3730990-5-maz@kernel.org> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent c4b000c commit 6b5b368

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎arch/arm64/kvm/perf.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <asm/kvm_emulate.h>
1313

14+
DEFINE_STATIC_KEY_FALSE(kvm_arm_pmu_available);
15+
1416
static int kvm_is_in_guest(void)
1517
{
1618
return kvm_get_running_vcpu() != NULL;
@@ -48,6 +50,14 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = {
4850

4951
int kvm_perf_init(void)
5052
{
53+
/*
54+
* Check if HW_PERF_EVENTS are supported by checking the number of
55+
* hardware performance counters. This could ensure the presence of
56+
* a physical PMU and CONFIG_PERF_EVENT is selected.
57+
*/
58+
if (IS_ENABLED(CONFIG_ARM_PMU) && perf_num_counters() > 0)
59+
static_branch_enable(&kvm_arm_pmu_available);
60+
5161
return perf_register_guest_info_callbacks(&kvm_guest_cbs);
5262
}
5363

‎arch/arm64/kvm/pmu-emul.c‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -823,16 +823,6 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
823823
return val & mask;
824824
}
825825

826-
bool kvm_arm_support_pmu_v3(void)
827-
{
828-
/*
829-
* Check if HW_PERF_EVENTS are supported by checking the number of
830-
* hardware performance counters. This could ensure the presence of
831-
* a physical PMU and CONFIG_PERF_EVENT is selected.
832-
*/
833-
return (perf_num_counters() > 0);
834-
}
835-
836826
int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
837827
{
838828
if (!kvm_vcpu_has_pmu(vcpu))

‎include/kvm/arm_pmu.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#define ARMV8_PMU_CYCLE_IDX (ARMV8_PMU_MAX_COUNTERS - 1)
1414
#define ARMV8_PMU_MAX_COUNTER_PAIRS ((ARMV8_PMU_MAX_COUNTERS + 1) >> 1)
1515

16+
DECLARE_STATIC_KEY_FALSE(kvm_arm_pmu_available);
17+
18+
static __always_inline bool kvm_arm_support_pmu_v3(void)
19+
{
20+
return static_branch_likely(&kvm_arm_pmu_available);
21+
}
22+
1623
#ifdef CONFIG_HW_PERF_EVENTS
1724

1825
struct kvm_pmc {
@@ -47,7 +54,6 @@ void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val);
4754
void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val);
4855
void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
4956
u64 select_idx);
50-
bool kvm_arm_support_pmu_v3(void);
5157
int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu,
5258
struct kvm_device_attr *attr);
5359
int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu,
@@ -87,7 +93,6 @@ static inline void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) {}
8793
static inline void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) {}
8894
static inline void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu,
8995
u64 data, u64 select_idx) {}
90-
static inline bool kvm_arm_support_pmu_v3(void) { return false; }
9196
static inline int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu,
9297
struct kvm_device_attr *attr)
9398
{

0 commit comments

Comments
 (0)