[PATCH 2/2] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer systems

Reiji Watanabe
2023-06-10T06:16:27Z

Disallow userspace from configuring vPMU for guests on systems
where the PMUVer is not uniform across all PEs.
KVM has not been advertising PMUv3 to the guests with vPMU on
such systems anyway, and such systems would be extremely
uncommon and unlikely to even use KVM.

Signed-off-by: Reiji Watanabe <reijiw@google.com>
---
arch/arm64/kvm/arm.c | 1 +
arch/arm64/kvm/pmu-emul.c | 3 ---
include/kvm/arm_pmu.h | 10 ++++++++++
3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 14391826241c..21901fbd6e4d 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1960,6 +1960,7 @@ static int __init init_subsystems(void)
goto out;

kvm_register_perf_callbacks(NULL);
+ kvm_arm_set_support_pmu_v3();

out:
if (err)
diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index 5d2903f52a5f..45b84cf22026 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -684,9 +684,6 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)
entry->arm_pmu = pmu;
list_add_tail(&entry->entry, &arm_pmus);

- if (list_is_singular(&arm_pmus))
- static_branch_enable(&kvm_arm_pmu_available);
-
out_unlock:
mutex_unlock(&arm_pmus_lock);
}
diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h
index eef17de966da..af1fe2b53fbb 100644
--- a/include/kvm/arm_pmu.h
+++ b/include/kvm/arm_pmu.h
@@ -105,6 +105,14 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);

u8 kvm_arm_pmu_get_pmuver_limit(void);

+static inline void kvm_arm_set_support_pmu_v3(void)
+{
+ u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
+
+ if (pmu_v3_is_supported(pmuver))
+ static_branch_enable(&kvm_arm_pmu_available);
+}
+
#else
struct kvm_pmu {
};
@@ -114,6 +122,8 @@ static inline bool kvm_arm_support_pmu_v3(void)
return false;
}

+static inline void kvm_arm_set_support_pmu_v3(void) {};
+
#define kvm_arm_pmu_irq_initialized(v) (false)
static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu,
u64 select_idx)