From nobody Tue Feb 10 17:08:39 2026 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D95121DE8B9 for ; Mon, 6 Jan 2025 14:25:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736173532; cv=none; b=YSO0Ywk3JwGJQ1/GHV6eoPHVt38GSzmkz0S3+9VQofBxq3ezSJuD1T1GRRKuPY426vNA2/SR/DD6VNgP87ceItvLTjgrP3OeaHExSFUD0GYMRv26nkaI+15aJQgbZAfKLYJmudVFE2KOAcEl2EUEWXWLUS+kWAR7ecKxSh/KUWc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736173532; c=relaxed/simple; bh=Z3hNXFTWsi2VMNGUTZU3m56uqM9TsOxLaJPjr9uPxTg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pE6d5rfp+5UbHguInu2Avu+Y0MUyup9/RFHrjc990Hsg81U+OSZx7TJVj57GbL68/Sjy44WXeSee6mxu5+GUMq2PEYs9MUlgDcZAVkfaLgNc0+SJzfpqRy6EzvgfJD6ufbVCmCa85Z6TsiZClrnFZKyASMR0ExN+Y8Mo7XAnbdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=m82Em2In; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="m82Em2In" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-385e27c75f4so10393093f8f.2 for ; Mon, 06 Jan 2025 06:25:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736173527; x=1736778327; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4tKqXX6ltyMKnYMGbEPboArH4aBYnbWTrcwl9D/Fhgk=; b=m82Em2InnKHrVJOP6/oWjBjnACMQIsGwlYCz6QPRq7Bp3uQiZp11H6VmZCGZ8keUih TXI6osW2hdnxg0Wq9JPyRITE+al3fjlkCZolpeNqHwmUOQ4rB8uieLC/EDT+GmHVAQjf XNwpBGpgkUJjFhGFYllGZVx6eGzDA/rN+BbxTdND/TiOp1qpPuq839gRXe7ebbEut5lb C+bFrki/Rd+FbQmLBy1RUkvIaDP/8yvl/GmP5c6TeVZxN4kpGcgGqUuN94QoH/RxSSon +46HoHMKOmijGO7gowvEZo69Db4lNXtAfvRTI5F3DvcldwZ8oHWId78JjaG7Gjm9dGEf FMUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736173527; x=1736778327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4tKqXX6ltyMKnYMGbEPboArH4aBYnbWTrcwl9D/Fhgk=; b=AicIL32NAOrKrn/EJZSdlDZHqYnKIII5OqR5Uup11Do+ZPHxFB6J5/qtppOnOte7ym pylPXcC9x0DJZdK+adZxhHVslSUynmkmkz0qxGvJ05YRKGBr0qID4MsTKHh2Xs7bUulB WgxhY6JyBxS84AR6VWljSWGiRNudkPV+mehZxBGKM8ZMi83rftZ7z3P31r4tbuWvKpEs EmSsHewqBCTb8I/0e2f6I45kZ+iF5ljIfXshZB5oeU6Y5zuBgXd4mR1d+Xd3ujFVlklY SJQ987B3Yoi0NkWWJT1wlMRMPYyyWFBNpCFhgB5lKYjWHnTqDs5gsLbtGVrnd2HkRzF5 pGBw== X-Forwarded-Encrypted: i=1; AJvYcCXduo/FI3SyP6coZXMNLxIQOFrA3NvM7TvaMw0ChbvxIkwJDVnanrJnsDRCNFwDVqKOfsll8MxQrSCCeFI=@vger.kernel.org X-Gm-Message-State: AOJu0YxRVR2O2XVfd4Aferb3JxcUdPS6ODKHMaSD0Sxp9YF9qIQqhg1l CnKVLIlgEd33Zrq/eAC1bBUjTRgbHRkuZcR2xXirB6ffz17TenmznXEq0B8mzLE= X-Gm-Gg: ASbGncuM4cLoVZgV2eygbXAsfSalaNmmRYbYKNs2/FK3WXJ9UKSoDqy4NmcUAIXiRrT vCcYtLG+E2ZEP0Ce2sNd8DeWUthgMaRIyR2Wm6P02I1jbBRBqlsUOja1SlFmQNrLwBCgcNzVWkx fQXuK+PcFI0ZFqBFxPfdq/i+bnQFluPg34ydTEdkgOlIbV41MJJvq2DNvSNjx/KpDipka8NAUpm BtxQekWKnwsq3dgv7WTtttobNC+c90s+y4HDSxX4muyRnEzDx+HtY03 X-Google-Smtp-Source: AGHT+IHosAoSQ8Y6r5GTgrFTFQzIQO4UbWzlRpVUduo3UPs5moBmKInuymWOSfwWQHVu3cAAbCM3Ag== X-Received: by 2002:a5d:64ac:0:b0:385:effc:a279 with SMTP id ffacd0b85a97d-38a223ff46cmr42420705f8f.58.1736173526999; Mon, 06 Jan 2025 06:25:26 -0800 (PST) Received: from pop-os.. ([145.224.66.180]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4365d116d8fsm577295045e9.17.2025.01.06.06.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 06:25:26 -0800 (PST) From: James Clark To: maz@kernel.org, kvmarm@lists.linux.dev, oliver.upton@linux.dev, suzuki.poulose@arm.com, coresight@lists.linaro.org Cc: James Clark , Joey Gouly , Zenghui Yu , Catalin Marinas , Will Deacon , Mike Leach , Alexander Shishkin , Mark Rutland , Anshuman Khandual , "Rob Herring (Arm)" , James Morse , Mark Brown , Shiqi Liu , Raghavendra Rao Ananta , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v9 5/7] KVM: arm64: coresight: Give TRBE enabled state to KVM Date: Mon, 6 Jan 2025 14:24:40 +0000 Message-Id: <20250106142446.628923-6-james.clark@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250106142446.628923-1-james.clark@linaro.org> References: <20250106142446.628923-1-james.clark@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently in nVHE, KVM has to check if TRBE is enabled on every guest switch even if it was never used. Because it's a debug feature and is more likely to not be used than used, give KVM the TRBE buffer status to allow a much simpler and faster do-nothing path in the hyp. Protected mode now disables trace regardless of TRBE (because trfcr_while_in_guest is always 0), which was not previously done. However, it continues to flush whenever the buffer is enabled regardless of the filter status. This avoids the hypothetical case of a host that had disabled the filter but not flushed which would arise if only doing the flush when the filter was enabled. Signed-off-by: James Clark --- arch/arm64/include/asm/kvm_host.h | 9 +++ arch/arm64/kvm/debug.c | 32 +++++++++- arch/arm64/kvm/hyp/nvhe/debug-sr.c | 63 ++++++++++++-------- drivers/hwtracing/coresight/coresight-trbe.c | 3 + 4 files changed, 79 insertions(+), 28 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index 04cd488f6787..8fdc7b6efc79 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -617,6 +617,8 @@ struct kvm_host_data { #define KVM_HOST_DATA_FLAG_HAS_TRBE 1 #define KVM_HOST_DATA_FLAG_HOST_SVE_ENABLED 2 #define KVM_HOST_DATA_FLAG_HOST_SME_ENABLED 3 +#define KVM_HOST_DATA_FLAG_TRBE_ENABLED 4 +#define KVM_HOST_DATA_FLAG_EL1_TRACING_CONFIGURED 5 unsigned long flags; =20 struct kvm_cpu_context host_ctxt; @@ -662,6 +664,9 @@ struct kvm_host_data { u64 mdcr_el2; } host_debug_state; =20 + /* Guest trace filter value */ + u64 trfcr_while_in_guest; + /* Number of programmable event counters (PMCR_EL0.N) for this CPU */ unsigned int nr_event_counters; =20 @@ -1389,6 +1394,8 @@ static inline bool kvm_pmu_counter_deferred(struct pe= rf_event_attr *attr) void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr); void kvm_clr_pmu_events(u64 clr); bool kvm_set_pmuserenr(u64 val); +void kvm_enable_trbe(void); +void kvm_disable_trbe(void); #else static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *att= r) {} static inline void kvm_clr_pmu_events(u64 clr) {} @@ -1396,6 +1403,8 @@ static inline bool kvm_set_pmuserenr(u64 val) { return false; } +static inline void kvm_enable_trbe(void) {} +static inline void kvm_disable_trbe(void) {} #endif =20 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu); diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index d921e7f7bd59..92a24adadb0e 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -81,9 +81,15 @@ void kvm_init_host_debug_data(void) !(read_sysreg_s(SYS_PMBIDR_EL1) & PMBIDR_EL1_P)) host_data_set_flag(HAS_SPE); =20 - if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffe= r_SHIFT) && - !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P)) - host_data_set_flag(HAS_TRBE); + if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_= SHIFT)) { + /* Force disable trace in protected mode in case of no TRBE */ + if (is_protected_kvm_enabled()) + host_data_set_flag(EL1_TRACING_CONFIGURED); + + if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuff= er_SHIFT) && + !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P)) + host_data_set_flag(HAS_TRBE); + } } =20 /* @@ -219,3 +225,23 @@ void kvm_debug_handle_oslar(struct kvm_vcpu *vcpu, u64= val) kvm_arch_vcpu_load(vcpu, smp_processor_id()); preempt_enable(); } + +void kvm_enable_trbe(void) +{ + if (has_vhe() || is_protected_kvm_enabled() || + WARN_ON_ONCE(preemptible())) + return; + + host_data_set_flag(TRBE_ENABLED); +} +EXPORT_SYMBOL_GPL(kvm_enable_trbe); + +void kvm_disable_trbe(void) +{ + if (has_vhe() || is_protected_kvm_enabled() || + WARN_ON_ONCE(preemptible())) + return; + + host_data_clear_flag(TRBE_ENABLED); +} +EXPORT_SYMBOL_GPL(kvm_disable_trbe); diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/d= ebug-sr.c index 858bb38e273f..2f4a4f5036bb 100644 --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c @@ -51,32 +51,45 @@ static void __debug_restore_spe(u64 pmscr_el1) write_sysreg_el1(pmscr_el1, SYS_PMSCR); } =20 -static void __debug_save_trace(u64 *trfcr_el1) +static void __trace_do_switch(u64 *saved_trfcr, u64 new_trfcr) { - *trfcr_el1 =3D 0; + *saved_trfcr =3D read_sysreg_el1(SYS_TRFCR); + write_sysreg_el1(new_trfcr, SYS_TRFCR); +} =20 - /* Check if the TRBE is enabled */ - if (!(read_sysreg_s(SYS_TRBLIMITR_EL1) & TRBLIMITR_EL1_E)) - return; - /* - * Prohibit trace generation while we are in guest. - * Since access to TRFCR_EL1 is trapped, the guest can't - * modify the filtering set by the host. - */ - *trfcr_el1 =3D read_sysreg_el1(SYS_TRFCR); - write_sysreg_el1(0, SYS_TRFCR); - isb(); - /* Drain the trace buffer to memory */ - tsb_csync(); +static bool __trace_needs_drain(void) +{ + if (is_protected_kvm_enabled() && host_data_test_flag(HAS_TRBE)) + return read_sysreg_s(SYS_TRBLIMITR_EL1) & TRBLIMITR_EL1_E; + + return host_data_test_flag(TRBE_ENABLED); } =20 -static void __debug_restore_trace(u64 trfcr_el1) +static bool __trace_needs_switch(void) { - if (!trfcr_el1) - return; + return host_data_test_flag(TRBE_ENABLED) || + host_data_test_flag(EL1_TRACING_CONFIGURED); +} + +static void __trace_switch_to_guest(void) +{ + /* Unsupported with TRBE so disable */ + if (host_data_test_flag(TRBE_ENABLED)) + *host_data_ptr(trfcr_while_in_guest) =3D 0; + + __trace_do_switch(host_data_ptr(host_debug_state.trfcr_el1), + *host_data_ptr(trfcr_while_in_guest)); =20 - /* Restore trace filter controls */ - write_sysreg_el1(trfcr_el1, SYS_TRFCR); + if (__trace_needs_drain()) { + isb(); + tsb_csync(); + } +} + +static void __trace_switch_to_host(void) +{ + __trace_do_switch(host_data_ptr(trfcr_while_in_guest), + *host_data_ptr(host_debug_state.trfcr_el1)); } =20 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu) @@ -84,9 +97,9 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu) /* Disable and flush SPE data generation */ if (host_data_test_flag(HAS_SPE)) __debug_save_spe(host_data_ptr(host_debug_state.pmscr_el1)); - /* Disable and flush Self-Hosted Trace generation */ - if (host_data_test_flag(HAS_TRBE)) - __debug_save_trace(host_data_ptr(host_debug_state.trfcr_el1)); + + if (__trace_needs_switch()) + __trace_switch_to_guest(); } =20 void __debug_switch_to_guest(struct kvm_vcpu *vcpu) @@ -98,8 +111,8 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *= vcpu) { if (host_data_test_flag(HAS_SPE)) __debug_restore_spe(*host_data_ptr(host_debug_state.pmscr_el1)); - if (host_data_test_flag(HAS_TRBE)) - __debug_restore_trace(*host_data_ptr(host_debug_state.trfcr_el1)); + if (__trace_needs_switch()) + __trace_switch_to_host(); } =20 void __debug_switch_to_host(struct kvm_vcpu *vcpu) diff --git a/drivers/hwtracing/coresight/coresight-trbe.c b/drivers/hwtraci= ng/coresight/coresight-trbe.c index 03d3695ba5aa..a728802d2206 100644 --- a/drivers/hwtracing/coresight/coresight-trbe.c +++ b/drivers/hwtracing/coresight/coresight-trbe.c @@ -17,6 +17,7 @@ =20 #include #include +#include #include =20 #include "coresight-self-hosted-trace.h" @@ -221,6 +222,7 @@ static inline void set_trbe_enabled(struct trbe_cpudata= *cpudata, u64 trblimitr) */ trblimitr |=3D TRBLIMITR_EL1_E; write_sysreg_s(trblimitr, SYS_TRBLIMITR_EL1); + kvm_enable_trbe(); =20 /* Synchronize the TRBE enable event */ isb(); @@ -239,6 +241,7 @@ static inline void set_trbe_disabled(struct trbe_cpudat= a *cpudata) */ trblimitr &=3D ~TRBLIMITR_EL1_E; write_sysreg_s(trblimitr, SYS_TRBLIMITR_EL1); + kvm_disable_trbe(); =20 if (trbe_needs_drain_after_disable(cpudata)) trbe_drain_buffer(); --=20 2.34.1