[PATCH] KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised

Jia He posted 1 patch 9 months ago
arch/arm64/kvm/pmu.c | 7 +++++++
1 file changed, 7 insertions(+)
[PATCH] KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised
Posted by Jia He 9 months ago
Currently, `kvm_host_pmu_init()` does not check if KVM has been
successfully initialized before proceeding. This can lead to unintended
behavior if the function is called in an environment where KVM is not
available, e.g., kernel is landed in EL1.

Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/pmu.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/arm64/kvm/pmu.c b/arch/arm64/kvm/pmu.c
index 7169c1a24dd6..e39c48d12b81 100644
--- a/arch/arm64/kvm/pmu.c
+++ b/arch/arm64/kvm/pmu.c
@@ -227,6 +227,13 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)
 {
 	struct arm_pmu_entry *entry;
 
+	/*
+	 * Prevent unintended behavior where KVM is not available or not
+	 * successfully initialised, e.g., kernel is landed in EL1.
+	 */
+	if (!is_kvm_arm_initialised())
+		return;
+
 	/*
 	 * Check the sanitised PMU version for the system, as KVM does not
 	 * support implementations where PMUv3 exists on a subset of CPUs.
-- 
2.34.1
Re: [PATCH] KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised
Posted by Marc Zyngier 9 months ago
On Sat, 22 Mar 2025 03:51:15 +0000,
Jia He <justin.he@arm.com> wrote:
> 
> Currently, `kvm_host_pmu_init()` does not check if KVM has been
> successfully initialized before proceeding. This can lead to unintended
> behavior if the function is called in an environment where KVM is not

Which unintended behaviour? Other than the pointless allocation of a
tiny amount of memory? Does anything really go wrong?

> available, e.g., kernel is landed in EL1.

s/landed in/booted from/

> 
> Signed-off-by: Jia He <justin.he@arm.com>
> ---
>  arch/arm64/kvm/pmu.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/arch/arm64/kvm/pmu.c b/arch/arm64/kvm/pmu.c
> index 7169c1a24dd6..e39c48d12b81 100644
> --- a/arch/arm64/kvm/pmu.c
> +++ b/arch/arm64/kvm/pmu.c
> @@ -227,6 +227,13 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)

Huh:

maz@valley-girl:~/hot-poop/arm-platforms$ git grep -l kvm_host_pmu_init
arch/arm64/kvm/pmu-emul.c
drivers/perf/arm_pmu.c
include/linux/perf/arm_pmu.h

Amusingly, arch/arm64/kvm/pmu.c is nowhere to be seen in this list.
I have no idea what this patch applies to, but that's neither 6.13,
the current upstream, nor kvmarm/next.

>  {
>  	struct arm_pmu_entry *entry;
>  
> +	/*
> +	 * Prevent unintended behavior where KVM is not available or not
> +	 * successfully initialised, e.g., kernel is landed in EL1.

Same comment here.

> +	 */
> +	if (!is_kvm_arm_initialised())

This is definitely the wrong thing to check for, as it relies on the
probe ordering between the PMU drivers and KVM. Relying on that is not
acceptable.

If you're worried about a kernel booted at EL1, then check for that.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.