From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F3351DEFD7 for ; Wed, 6 Nov 2024 13:51:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730901082; cv=none; b=ngIYxB2xWXi8m+EpqhoYpGQspHtk4h9MHPfLbxdd3taCD0kkSQyWuBgYLe1cJWoaIW+gXvNEIJDN3f28az+DjsnVxhpEZyT9b2H8d34QKYtB1akNfXpKyaNcCYOphI8Lif0dLPR1UJ8Ywx22JSvj/nJImEcbS0PAXMyBl9PVLXM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730901082; c=relaxed/simple; bh=y/kCWH56lsq8A5L3Dd12k34Q3XqtL+M0Zi9k/4KqAgo=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=H7os/X/2rhSuCEEH6zpYfz+M0kbrU9k3deH2fHDZx1/QdTYkUj8gyeszXnCxLnxuSu49EcFoyORP6maCnpQTYc7f8fal54mY6+PQr3bQ+EWdCNdXHqdFo63dKynfRd6lh0G4vPvDPkO+3NPnmdP3m8ufGzdCOft6S2TdrU8AOvc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U7pgdlHe; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="U7pgdlHe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 390A5C4CECC; Wed, 6 Nov 2024 13:51:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730901082; bh=y/kCWH56lsq8A5L3Dd12k34Q3XqtL+M0Zi9k/4KqAgo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=U7pgdlHehhSwfyDfGPEb8I13dcH8pTfRtl/c+ZpjCQXklQ5acmNzabphTVbH9D42a akwUygIy8m1YsuvSrm0Tl5W+HraQ2H5sOjq6dL5Mu6HMBdL8Yhnm5s0H80lwJdwRyT /q5ZcIDoNQKVF1zGHhTM8ipS44ZUTeojsx1mPHZInLU7QU7KcKyZdEEGaBEGE/RFEq Eyxu//qVaNXmf2FCWuq9mi4O5esJc+gcrd1G02RxDuHRSLGqu5C7beI7/iK46iTDo7 BW+CqTkGeQKHlPfWIr8ImxsEYcw12uFxL/PCu1fShXrZMbRbyyeoqYMBCstrL0ARw8 yC1tFnb/oIVUw== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1t8gRM-00AOGG-7G; Wed, 06 Nov 2024 13:51:20 +0000 Date: Wed, 06 Nov 2024 13:51:19 +0000 Message-ID: <868qtw1ne0.wl-maz@kernel.org> From: Marc Zyngier To: Alexandru Elisei Cc: oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, catalin.marinas@arm.com, will@kernel.org Subject: Re: [PATCH] KVM: arm64: VHE: Initialize PMSCR_EL1 In-Reply-To: <20241106122654.38234-1-alexandru.elisei@arm.com> References: <20241106122654.38234-1-alexandru.elisei@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.4 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: alexandru.elisei@arm.com, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, catalin.marinas@arm.com, will@kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Wed, 06 Nov 2024 12:26:54 +0000, Alexandru Elisei wrote: > > 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 > --- > > 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 ... resulting in an early crash on anything that doesn't have SPE. That's indeed "consistent and predictable" :-). > > // Transfer the MM state from EL1 to EL2 > mrs_s x0, SYS_TCR_EL12 I find it pretty odd to hide something that is squarely guest state in the hyp stubs, and I'd rather see something like this (untested): diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 48cafb65d6acf..806f25a8753ed 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -2139,8 +2139,12 @@ static void cpu_hyp_init_features(void) cpu_set_hyp_vector(); kvm_arm_init_debug(); - if (is_kernel_in_hyp_mode()) + if (is_kernel_in_hyp_mode()) { + if (SYS_FIELD_GET(ID_AA64DFR0_EL1, PMSVer, + read_sysreg(id_aa64dfr0_el1))) + write_sysreg_el1(0, SYS_PMSCR); kvm_timer_init_vhe(); + } if (vgic_present) kvm_vgic_init_cpu_hardware(); Thanks, M. -- Without deviation from the norm, progress is not possible.