[PATCH] KVM: arm64: VHE: Initialize PMSCR_EL1

Alexandru Elisei alexandru.elisei at arm.com
Wed Nov 6 04:26:54 PST 2024


According to the pseudocode for StatisticalProfilingEnabled() from Arm
DDI0487K.a, PMSCR_EL1 controls profiling at EL1 and EL0:

- PMSCR_EL1.E1SPE controls profiling at EL1.
- PMSCR_EL1.E0SPE controls profiling at EL0 if HCR_EL2.TGE=0. KVM always
  clears HCR_EL2.TGE when running a VM.

When profiling is enabled in the host, and the host is running in nVHE mode
(HCR_EL2.E2H=0), KVM clears PMSCR_EL1.{E1SPE,E0SPE} before jumping into the
guest.

When profiling is enabled in the host, and the host is running at EL2
(HCR_EL2.E2H=1), KVM will not touch PMSCR_EL1.{E1SPE,E0SPE} before jumping
into the guest. PMSCR_EL1.{E1SPE,E0SPE} reset to an architecturally UNKNOWN
value, which means it might be possible that KVM unintentionally profiles
the guest when is running in VHE mode.

Clear PMSCR_EL1.{E1SPE,E0SPE} when setting up VHE mode to keep the
behaviour consistent and predictable.

Signed-off-by: Alexandru Elisei <alexandru.elisei at arm.com>
---

Tested on the model, by setting the PMSCR_EL1.E1SPE and E0SPE bits in
__init_el2_debug to simulate a system where they reset to 1. Without the
patch, when the host is running at EL2, and the user is profiling the
kvmtool process, I can see records taken at EL1:

# perf record -e arm_spe// -- ./lkvm-static run -c2 -m512 -k Image -d disk -p earlycon

With this patch, those records disappear; and the size of perf.data has
been more than halved.

 arch/arm64/kernel/hyp-stub.S | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/arm64/kernel/hyp-stub.S b/arch/arm64/kernel/hyp-stub.S
index 65f76064c86b..df63f329d400 100644
--- a/arch/arm64/kernel/hyp-stub.S
+++ b/arch/arm64/kernel/hyp-stub.S
@@ -117,6 +117,8 @@ SYM_CODE_START_LOCAL(__finalise_el2)
 	bic	x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
 	bic	x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
 	msr	mdcr_el2, x0
+	// Disable profiling when running a virtual machine
+	msr_s	SYS_PMSCR_EL12, xzr
 
 	// Transfer the MM state from EL1 to EL2
 	mrs_s	x0, SYS_TCR_EL12
-- 
2.47.0




More information about the linux-arm-kernel mailing list