From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2C02ED44D77 for ; Wed, 6 Nov 2024 14:08:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ySGwEOhA4TSs6o+A4LmoHn6MMY7nNG1kD54UC5YnIUA=; b=bo6VMnXMYtBx8FG9AJCc0PN7Nl BkyNmgck1ag2sK4L4Aq/wYwwwJH0Dcf8rAOfOD/fGlENJuAmobTDUlF1jC5wjL7Sl4Qhvo0WgiT9g yR2/h557SMBGfsq2YVNhbKtB5LW7IDul08IwdDNL6rVGOiWmYxaqKXDA2f32LrAdaVfpsoPt8u370 LDKPIuUOuwwpbwJEGhCpMTykItjBQ4QvG0X6/9z1bKIdkINxalBjCOyGRuwDeQQsasQP1bi4167gg KgZYbYa9T9w2fBMKwwE9ieM2MYSTo72BmlZK99DhaOGNcDxw5zuCbAGE1l9r2eSQb12ugabmO1+Ut Mz+D7Q6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t8ghZ-00000003XeL-21fP; Wed, 06 Nov 2024 14:08:05 +0000 Received: from nyc.source.kernel.org ([147.75.193.91]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t8gRP-00000003Uff-1NSl for linux-arm-kernel@lists.infradead.org; Wed, 06 Nov 2024 13:51:24 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id A05A8A43D04; Wed, 6 Nov 2024 13:49:27 +0000 (UTC) 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) 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241106_055123_522746_E3E0DF17 X-CRM114-Status: GOOD ( 30.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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.