Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4766025rwb; Tue, 8 Aug 2023 13:32:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH0S+jJFJ9niIljb12rMwes2B6dqZ/NkPey9eUufolXZaW2jZqmkm/Gm2hfesW8KHboChYz X-Received: by 2002:a17:902:7442:b0:1bb:ed65:5e0d with SMTP id e2-20020a170902744200b001bbed655e0dmr776335plt.56.1691526742825; Tue, 08 Aug 2023 13:32:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691526742; cv=none; d=google.com; s=arc-20160816; b=FZOvJIS6F6prjRpcd9W3g1YkghNVTpp3mudcROXZF/8pW7gGtFuLi0B18fQ0803wQt CrNUrvxy96tW2S1iuawTA6BcwU/gVBCODZoJoeDM6jUoHCNvppjfb69UZbWVR84F+Db0 2TNdkVpUw7CfXecjJ1ibo6yUyqAcWuxCv7rMYQP24MdcnFI4dYsp1Cekk9R3diyUqWDb QFspM3g967YjO5dEAIQtSIljbWrPaDBqiUSoORN2Rhw3u1xOnLiloRhZCIDieVshGr3D 2HChqC6/ewKAo4epcvQLuTkbfoke6yW7qzqHd6BIGNk+US4ENSMFDLRsZs4JIlF6p1lu B0pw== 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=Sl46gIrQH5BQRbWoDO856XXYNIRYgg3I1FK+D8djIyM=; fh=hFhuNU6hGwY9RQG5GoeicdPhLH628OAjjnvGlP4WSgo=; b=PQhRyGnnpESyrzMp4XNc2yQl+SUh/krKohiaNI03FnCu398nR6toWltTH4qhY1rSeJ sDjy2fEA1EiRKrfUui+h490ki1mIYEaJV07/sWgq5LK3qSV9Ddo4GK6MZamxOenVNWF5 h3PDhWcAXfXDV5gaqLm9bs/X4mJ1nGSqp6/Xtyll4feVcHnIw5DanynLm6FL/lwj7qsE ceHnPR7Bv7EtFMUqtEykZRU5jZQYIJgy9y1aDDV+tp3J47uB1eM9mL+cpeu0c28kUFdX MR0gPF30dkJ0DW0KpCAfm0oNCCh1BSx++OIwv7MlAotbtqp401nzdVcdXAACOtWuY7g/ X59Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q63cCxax; 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 d7-20020a170902cec700b001bbb56b34e4si5266349plg.331.2023.08.08.13.32.09; Tue, 08 Aug 2023 13:32:22 -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=Q63cCxax; 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 S235392AbjHHTrM (ORCPT + 99 others); Tue, 8 Aug 2023 15:47:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236679AbjHHTq5 (ORCPT ); Tue, 8 Aug 2023 15:46:57 -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 733DC4C06D for ; Tue, 8 Aug 2023 09:50:55 -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 8BEEE62437 for ; Tue, 8 Aug 2023 08:27:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CAAF9C433C7; Tue, 8 Aug 2023 08:27:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691483239; bh=8vZjx++7vc/gL3iItvttMwpeYTizvuwArhclxzBwQt8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Q63cCxaxLP7BLXtt+k92+Za9fDEGw5P76MDkKXbe5kzu9Nn4NKj7bevIgqdXTT9Bm GnX/iKMSYi1jTtZ3ecGXZtdm/FLFU17S8d2c4IXYYJzJkUqLaSAqzK8ViLThDVOfwt cRHaEzRGP+UbhLEYHaPn/4eZdG/aBOdBslSFr7YQacpyU9spnI3ERPE9VOlzv8u7Cb H3I2MQgc5q0OzUJI2lmtym0uxOxwPaDNAzGIx/1RhHwRc+PEoHpATQMROm35ez7qod M/S5utYzPQRmgt5UYdICpWxHRrv0XsUH7t2oi1A2jzM0gIj/yAeq5BAEb+e+lMhaoi PQGAIiw05BPqg== Received: from ip-185-104-136-29.ptr.icomera.net ([185.104.136.29] 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 1qTI3g-00365O-4h; Tue, 08 Aug 2023 09:27:16 +0100 Date: Tue, 08 Aug 2023 09:27:20 +0100 Message-ID: <87350uq6qf.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 1/3] arm64: KVM: Add support for exclude_guest and exclude_host for ETM In-Reply-To: <20230804101317.460697-2-james.clark@arm.com> References: <20230804101317.460697-1-james.clark@arm.com> <20230804101317.460697-2-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: 185.104.136.29 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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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:11 +0100, James Clark wrote: > > Add an interface for the Coresight driver to use to set the current > exclude settings for the current CPU. This will be used to configure > TRFCR_EL1. Can you start by stating the problem? There is *some* rationale in the cover letter, but not enough to get the full picture. Specially if you haven't looked at the trace subsystem in the past... 7 years or so. > > The settings must be copied to the vCPU before each run in the same > way that PMU events are because the per-cpu struct isn't accessible in > protected mode. I'm pretty sure that for protected guests, we'd like to disable tracing altogether (debug mode excepted). > > This is only needed for nVHE, otherwise it works automatically with How about hVHE, which uses VHE at EL2 only? Doesn't it require the same treatment? > TRFCR_EL{1,2}. Unfortunately it can't be gated on CONFIG_CORESIGHT > because Coresight can be built as a module. It can however be gated on > CONFIG_PERF_EVENTS because that is required by Coresight. Why does it need to be gated *at all*? We need this for the PMU because of the way we call into the perf subsystem, but I don't see anything like that here. In general, conditional compilation sucks, and I'd like to avoid it as much as possible. > > Signed-off-by: James Clark > --- > arch/arm64/include/asm/kvm_host.h | 10 ++++++- > arch/arm64/kvm/Makefile | 1 + > arch/arm64/kvm/arm.c | 1 + > arch/arm64/kvm/etm.c | 48 +++++++++++++++++++++++++++++++ > include/kvm/etm.h | 43 +++++++++++++++++++++++++++ > 5 files changed, 102 insertions(+), 1 deletion(-) > create mode 100644 arch/arm64/kvm/etm.c > create mode 100644 include/kvm/etm.h > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index d7b1403a3fb2..f33262217c84 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -35,6 +35,7 @@ > #include > #include > #include > +#include > > #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS > > @@ -500,7 +501,7 @@ struct kvm_vcpu_arch { > u8 cflags; > > /* Input flags to the hypervisor code, potentially cleared after use */ > - u8 iflags; > + u16 iflags; If you make the iflags bigger, what ripple effect does it have on the alignment of the other data structures? Consider reordering things if it helps filling holes. > > /* State flags for kernel bookkeeping, unused by the hypervisor code */ > u8 sflags; > @@ -541,6 +542,9 @@ struct kvm_vcpu_arch { > u64 pmscr_el1; > /* Self-hosted trace */ > u64 trfcr_el1; > + /* exclude_guest settings for nVHE */ > + struct kvm_etm_event etm_event; > + Spurious blank line. More importantly, how is that related to the trfcr_el1 field just above? > } host_debug_state; > > /* VGIC state */ > @@ -713,6 +717,8 @@ struct kvm_vcpu_arch { > #define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6)) > /* vcpu running in HYP context */ > #define VCPU_HYP_CONTEXT __vcpu_single_flag(iflags, BIT(7)) > +/* Save TRFCR and apply exclude_guest rules */ > +#define DEBUG_STATE_SAVE_TRFCR __vcpu_single_flag(iflags, BIT(8)) > > /* SVE enabled for host EL0 */ > #define HOST_SVE_ENABLED __vcpu_single_flag(sflags, BIT(0)) > @@ -1096,6 +1102,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu); > void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr); > void kvm_clr_pmu_events(u32 clr); > bool kvm_set_pmuserenr(u64 val); > +void kvm_set_etm_events(struct perf_event_attr *attr); > +void kvm_clr_etm_events(void); > #else > static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {} > static inline void kvm_clr_pmu_events(u32 clr) {} > diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile > index c0c050e53157..0faff57423c4 100644 > --- a/arch/arm64/kvm/Makefile > +++ b/arch/arm64/kvm/Makefile > @@ -23,6 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ > vgic/vgic-its.o vgic/vgic-debug.o > > kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o > +kvm-$(CONFIG_PERF_EVENTS) += etm.o > > always-y := hyp_constants.h hyp-constants.s > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index b1a9d47fb2f3..7bd5975328a3 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -952,6 +952,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > kvm_vgic_flush_hwstate(vcpu); > > kvm_pmu_update_vcpu_events(vcpu); > + kvm_etm_update_vcpu_events(vcpu); > > /* > * Ensure we set mode to IN_GUEST_MODE after we disable > diff --git a/arch/arm64/kvm/etm.c b/arch/arm64/kvm/etm.c > new file mode 100644 > index 000000000000..359c37745de2 > --- /dev/null > +++ b/arch/arm64/kvm/etm.c > @@ -0,0 +1,48 @@ > +// SPDX-License-Identifier: GPL-2.0-only > + > +#include > + > +#include > + > +static DEFINE_PER_CPU(struct kvm_etm_event, kvm_etm_events); > + > +struct kvm_etm_event *kvm_get_etm_event(void) > +{ > + return this_cpu_ptr(&kvm_etm_events); > +} > + > +void kvm_etm_set_events(struct perf_event_attr *attr) > +{ > + struct kvm_etm_event *etm_event; > + > + /* > + * Exclude guest option only requires extra work with nVHE. > + * Otherwise it works automatically with TRFCR_EL{1,2} > + */ > + if (has_vhe()) > + return; > + > + etm_event = kvm_get_etm_event(); > + > + etm_event->exclude_guest = attr->exclude_guest; > + etm_event->exclude_host = attr->exclude_host; > + etm_event->exclude_kernel = attr->exclude_kernel; > + etm_event->exclude_user = attr->exclude_user; > +} > +EXPORT_SYMBOL_GPL(kvm_etm_set_events); > + > +void kvm_etm_clr_events(void) > +{ > + struct kvm_etm_event *etm_event; > + > + if (has_vhe()) > + return; > + > + etm_event = kvm_get_etm_event(); > + > + etm_event->exclude_guest = false; > + etm_event->exclude_host = false; > + etm_event->exclude_kernel = false; > + etm_event->exclude_user = false; > +} > +EXPORT_SYMBOL_GPL(kvm_etm_clr_events); Does it really need its own compilation unit if we were to build it at all times? > diff --git a/include/kvm/etm.h b/include/kvm/etm.h > new file mode 100644 > index 000000000000..95c4809fa2b0 > --- /dev/null > +++ b/include/kvm/etm.h > @@ -0,0 +1,43 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > + > +#ifndef __KVM_DEBUG_H > +#define __KVM_DEBUG_H > + > +struct perf_event_attr; > +struct kvm_vcpu; > + > +#if IS_ENABLED(CONFIG_KVM) && IS_ENABLED(CONFIG_PERF_EVENTS) > + > +struct kvm_etm_event { > + bool exclude_host; > + bool exclude_guest; > + bool exclude_kernel; > + bool exclude_user; > +}; > + > +struct kvm_etm_event *kvm_get_etm_event(void); > +void kvm_etm_clr_events(void); > +void kvm_etm_set_events(struct perf_event_attr *attr); > + > +/* > + * Updates the vcpu's view of the etm events for this cpu. Must be > + * called before every vcpu run after disabling interrupts, to ensure > + * that an interrupt cannot fire and update the structure. > + */ > +#define kvm_etm_update_vcpu_events(vcpu) \ > + do { \ > + if (!has_vhe() && vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR)) \ > + vcpu->arch.host_debug_state.etm_event = *kvm_get_etm_event(); \ > + } while (0) > + Why is it a macro and not a function, which would avoid exposing kvm_get_etm_event? Thanks, M. -- Without deviation from the norm, progress is not possible.