Skip to contentSkip to conversation

KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised

PATCH In review Jia He 2 participants 4 messages Mar 22, 2025

Subsystems (2)
lore
Share & tools
lore

Jia He

author
#0 lore raw

PATCH 1/1 KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised

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.

---
 arch/arm64/kvm/pmu.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/pmu.c +7 -0
index 7169c1a24dd6..e39c48d12b81 100644
@@ -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.
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 4 lines · Jia He

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?

Quoted 1 line

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

s/landed in/booted from/

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu)

Quoted 11 lines
---
 arch/arm64/kvm/pmu.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/pmu.c +0 -0
index 7169c1a24dd6..e39c48d12b81 100644
@@ -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.

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu) · line 232

Quoted 6 lines
arch/arm64/kvm/pmu.c +3 -0
 {
 	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.

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu) · line 234

Quoted 2 lines
arch/arm64/kvm/pmu.c +2 -0
+	 */
+	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.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#2 lore raw

Hi Marc,

Quoted 16 lines

-----Original Message-----
From: Marc Zyngier <[email protected]>
Sent: Saturday, March 22, 2025 6:08 PM
To: Justin He <[email protected]>
Cc: Oliver Upton <[email protected]>; linux-arm- [email protected]; [email protected]; Joey Gouly <[email protected]>; Suzuki Poulose <[email protected]>; Zenghui Yu <[email protected]>; Catalin Marinas <[email protected]>; Will Deacon <[email protected]>; linux- [email protected]
Subject: Re: [PATCH] KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised

Quoted 1 line · Jia He
Quoted 8 lines
Quoted 4 lines

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?

Sorry for the confusion --- I should explain more clearly.
I noticed the usage of host_data_ptr in Colton Lewis's RFC patch [1]. After applying the patch, the guest VM fails to boot.Upon investigating the root cause, I found that host_data_ptr can trigger a kernel panic if KVM is not initialized.

[1] https://patchwork.kernel.org/project/kvm/patch/[email protected]/

Quoted 18 lines
Quoted 1 line

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

s/landed in/booted from/

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu)

Quoted 11 lines
---
 arch/arm64/kvm/pmu.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/pmu.c index renamed from arch/arm64/kvm/pmu.c +0 -0

7169c1a24dd6..e39c48d12b81 100644

arch/arm64/kvm/pmu.c +0 -0
@@ -227,6 +227,13 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)

Huh:

Quoted 7 lines
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.

Sorry for the mistake, the patch is based on Colton Lewis's series. I’ll need to respin it if you're interested in the fix 😊

Quoted 9 lines

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu) · line 232

Quoted 6 lines
arch/arm64/kvm/pmu.c +3 -0
 {
 	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.

Quoted 7 lines

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu) · line 234

Quoted 2 lines
arch/arm64/kvm/pmu.c +2 -0
+	 */
+	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.

Indeed, would is_hyp_mode_available() be a proper replacement for is_kvm_arm_initialised() here?
Or should we add a prevention condition in host_data_ptr instead?

Quoted 2 lines

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

---
Cheers,
Justin He(Jia He)

maintainer
#3 lore raw
Quoted 24 lines · Justin He

Hi Marc,

Quoted 21 lines

-----Original Message-----
From: Marc Zyngier <[email protected]>
Sent: Saturday, March 22, 2025 6:08 PM
To: Justin He <[email protected]>
Cc: Oliver Upton <[email protected]>; linux-arm- [email protected]; [email protected]; Joey Gouly <[email protected]>; Suzuki Poulose <[email protected]>; Zenghui Yu <[email protected]>; Catalin Marinas <[email protected]>; Will Deacon <[email protected]>; linux- [email protected]
Subject: Re: [PATCH] KVM: arm64: pmu: Avoid initializing KVM PMU when KVM is not initialised

Quoted 5 lines · Jia He

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

Quoted 8 lines
Quoted 3 lines

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

Sorry for the confusion --- I should explain more clearly.
I noticed the usage of host_data_ptr in Colton Lewis's RFC patch [1]. After applying the patch, the guest VM fails to boot.Upon investigating the root cause, I found that host_data_ptr can trigger a kernel panic if KVM is not initialized.

How relevant is this for upstream?

Quoted 24 lines

[1] https://patchwork.kernel.org/project/kvm/patch/[email protected]/

Quoted 22 lines
Quoted 1 line

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

s/landed in/booted from/

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu)

Quoted 11 lines
---
 arch/arm64/kvm/pmu.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/pmu.c index renamed from arch/arm64/kvm/pmu.c +0 -0

7169c1a24dd6..e39c48d12b81 100644

arch/arm64/kvm/pmu.c +0 -0
@@ -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
Quoted 5 lines
Quoted 3 lines

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.

Sorry for the mistake, the patch is based on Colton Lewis's series. I’ll need to respin it if you're interested in the fix 😊

I'm interested in a patch if:

- the patch applies to an upstream or a kvmarm tag
- the patch fixes *something* in the upstream code
- the commit message accurately describes the problem

Otherwise, no, thank you.

Quoted 16 lines
Quoted 9 lines

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu) · line 232

Quoted 6 lines
arch/arm64/kvm/pmu.c +3 -0
 {
 	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.

Quoted 7 lines

arch/arm64/kvm/pmu.c void kvm_host_pmu_init(struct arm_pmu *pmu) · line 234

Quoted 2 lines
arch/arm64/kvm/pmu.c +2 -0
+	 */
+	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.

Quoted 2 lines

Indeed, would is_hyp_mode_available() be a proper replacement for is_kvm_arm_initialised() here?

Looks more appropriate, at least for upstream.

Quoted 1 line

Or should we add a prevention condition in host_data_ptr instead?

I can't comment about a series that I haven't reviewed and that is not yet targeting upstream. But using host_data_ptr() from outside of KVM is plain wrong, as it falls into the same trap as above. Checking for random init flags is not going to help with that.

Instead of posting random patches that make no sense for upstream, why don't you instead reply to Colton's series outlining your concerns and findings? It sounds to me like a more useful way to make progress.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.