Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4586155rwb; Tue, 8 Aug 2023 10:31:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8L73tYoKtIbgVmBEISE29aH3JrXtEUvkAyJy5ZYur6fOIJpSVDDAOW0UjdhQ3Lzq7t5zc X-Received: by 2002:a17:902:e5d1:b0:1b8:8b72:fa28 with SMTP id u17-20020a170902e5d100b001b88b72fa28mr387119plf.58.1691515890075; Tue, 08 Aug 2023 10:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691515890; cv=none; d=google.com; s=arc-20160816; b=nRHof612l+iBgX1EO+3Z1PDMEP/HkCmJYf5GgsWGB7n/T84xB7W2y8FcEKg4pCKlRy UW2ID7HhBe9fBixCmHLoqXbTh2TVTQIZ7Q4jk0+vw0+p5nfbH5h1vu++qqvvwzrontc4 Qsmlv9qwANmeWInhyfuOTumYor11K54DWQkolx+1xNk6m4aKDZ0lxJ2BVAWWWPvyy5RT KhWj9/81ezUfx+f+st+M7AijGov1RbDF7N080DO4dKa0hI+WcfEwe6e1f+22B3lCU8Ru JiQ8M38TOzjvTtIYo7ej55Ll8KTC8kv6gBO7034ZBidL4BYhv3PVZ1zzW5fzXD0aZJ+D Omvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=tnw3TjYRK4hP2XTg+KuCg913mHw9QdbSQd9Qi7ACuLA=; fh=hFhuNU6hGwY9RQG5GoeicdPhLH628OAjjnvGlP4WSgo=; b=bvzRjTQlNiE3m9x4naeQHlqZ9SSFSRtjngd22S3g4y7hx6La99NsJRvv5J/MmrqW8W UfgMSx8dB/MfwJlJ8Zs3xIZIWDsxW8Odr091vmZ0WF7mMGTkY5/LAYZyEq2aL7WWptEr NZpUDKH0cObGxwOZnwBPtmzwrjSZnFwibH5d+AS+Xg1KL/1YQOBfVFnWynFtvZDbae84 5Q3yVg8T8bPmYx5ZUkFJbD2P5CPxrm79SDysu+byNjmGbnZxNo363JRrBqAXFpSpBcR1 l2jNkfEXDBU7A78ZdTLgQrsKWFNAmyK06xqa/OeP8D+zJgwTMv9DHiJtQnmDcZJbgXGC n8IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=f0PqxTwg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n9-20020a170902e54900b001b9d8ea026fsi7715557plf.485.2023.08.08.10.31.17; Tue, 08 Aug 2023 10:31:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=f0PqxTwg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232858AbjHHQ3r (ORCPT + 99 others); Tue, 8 Aug 2023 12:29:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232739AbjHHQ2e (ORCPT ); Tue, 8 Aug 2023 12:28:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 109C512520 for ; Tue, 8 Aug 2023 08:51:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 880AB624C0 for ; Tue, 8 Aug 2023 11:04:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB25BC433C7; Tue, 8 Aug 2023 11:04:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691492654; bh=DvFRtMOY4iagkM51r9J4zb/PTZkURRwsD+PlGC3bhCQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=f0PqxTwgl8WsCHtX6Gsfios7cRzmexMQGyXxFqIvsasO6WhgwyKOsQIGNYUWy9Q0Q WFWIZp7AoBkeuetrWWQDu3JhDAkHH+HweDSyEZWwuhKMdVJq4Nlri8/e/QvyxpLiIG RTWQHAwHatWRR4/8g+/ImbUYL0euknmk67PiPnhag8u/Q988jiwJd1uCae2ysYBOR1 kYd2BUtnUq4ElPR8ONnIxo13uYX/dMyyV4l8vVDWu1dJV6hDkWM1bTB/JBTKdnaI82 tgjlVVtO7V2UeOLm/93dm00MtjQNGvwE2LW9HrzE1Zwbq8nQnYAAHYTAqWnOkBTsOQ I7B40TIGjvj2Q== Received: from [104.132.45.110] (helo=wait-a-minute.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 1qTKVY-00372P-FV; Tue, 08 Aug 2023 12:04:12 +0100 Date: Tue, 08 Aug 2023 12:04:19 +0100 Message-ID: <871qgdre18.wl-maz@kernel.org> From: Marc Zyngier To: James Clark Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mike Leach , Leo Yan , Alexander Shishkin , Anshuman Khandual , Rob Herring , linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH 2/3] arm64: KVM: Support exclude_guest for Coresight trace in nVHE In-Reply-To: <20230804101317.460697-3-james.clark@arm.com> References: <20230804101317.460697-1-james.clark@arm.com> <20230804101317.460697-3-james.clark@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/28.2 (x86_64-pc-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: 104.132.45.110 X-SA-Exim-Rcpt-To: james.clark@arm.com, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, mike.leach@linaro.org, leo.yan@linaro.org, alexander.shishkin@linux.intel.com, anshuman.khandual@arm.com, robh@kernel.org, linux-kernel@vger.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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 04 Aug 2023 11:13:12 +0100, James Clark wrote: > > Currently trace will always be generated in nVHE as long as TRBE isn't > being used. To allow filtering out guest trace, re-apply the filter > rules before switching to the guest. > > The TRFCR restore function remains the same. > > Signed-off-by: James Clark > --- > arch/arm64/kvm/debug.c | 7 ++++ > arch/arm64/kvm/hyp/nvhe/debug-sr.c | 56 +++++++++++++++++++++++++++--- > 2 files changed, 59 insertions(+), 4 deletions(-) > > diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c > index 8725291cb00a..ebb4db20a859 100644 > --- a/arch/arm64/kvm/debug.c > +++ b/arch/arm64/kvm/debug.c > @@ -335,10 +335,17 @@ void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) > if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) && > !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P)) > vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE); > + /* > + * Save TRFCR on nVHE if FEAT_TRF exists. This will be done in cases > + * where DEBUG_STATE_SAVE_TRBE doesn't completely disable trace. > + */ > + if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) > + vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRFCR); > } > > void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) > { > vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); > vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); > + vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR); > } > diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c > index 4558c02eb352..0e8c85b29b92 100644 > --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c > +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c > @@ -51,13 +51,17 @@ static void __debug_restore_spe(u64 pmscr_el1) > write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1); > } > > -static void __debug_save_trace(u64 *trfcr_el1) > +/* > + * Save TRFCR and disable trace completely if TRBE is being used. Return true > + * if trace was disabled. > + */ > +static bool __debug_save_trace(u64 *trfcr_el1) > { > *trfcr_el1 = 0; > > /* Check if the TRBE is enabled */ > if (!(read_sysreg_s(SYS_TRBLIMITR_EL1) & TRBLIMITR_EL1_E)) > - return; > + return false; While you're refactoring this code, please move the zeroing of *trfcr_el1 under the if statement. > /* > * Prohibit trace generation while we are in guest. > * Since access to TRFCR_EL1 is trapped, the guest can't > @@ -68,6 +72,8 @@ static void __debug_save_trace(u64 *trfcr_el1) > isb(); > /* Drain the trace buffer to memory */ > tsb_csync(); > + > + return true; > } > > static void __debug_restore_trace(u64 trfcr_el1) > @@ -79,14 +85,55 @@ static void __debug_restore_trace(u64 trfcr_el1) > write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1); > } > > +#if IS_ENABLED(CONFIG_PERF_EVENTS) As previously stated, just always compile this. There shouldn't be anything here that's so large that it becomes a candidate for exclusion. Hell, even the whole of NV+pKVM are permanent features, even of most people won't use *any* of that. > +static inline void __debug_save_trfcr(struct kvm_vcpu *vcpu) > +{ > + u64 trfcr; > + struct kvm_etm_event etm_event = vcpu->arch.host_debug_state.etm_event; > + > + /* No change if neither are excluded */ > + if (!etm_event.exclude_guest && !etm_event.exclude_host) { > + /* Zeroing prevents restoring a stale value */ > + vcpu->arch.host_debug_state.trfcr_el1 = 0; I find this "zero means do nothing" part very odd. I can see it is already done, but I really dislike this sort of assumption to avoid writing to a register. I'd really prefer we track another version of TRFCR_EL1, compare host and guest, and decide to avoid writing if they are equal. At least, it would be readable. And in the end, expressing *everything* in terms of the register would really help, instead of the exclude_* stuff that has no place in the low-level arch code. Thanks, M. -- Without deviation from the norm, progress is not possible.