From nobody Sat Feb 22 08:34:30 2025 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 C63DC1E9B19 for ; Tue, 7 Jan 2025 11:34:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736249679; cv=none; b=goJg8peT672AbkjIJbp1hptgwDCppxvaiLaSOB4QAvgdlRkGGRXeWdAnGxwspf9JdOxJLrFIRyMcYUt9Aap7KqN0VFIPgGf9lwKhQMUpG3AU/AwK9BV33yeNLA1UEML3KY4YqGP6r+xEwydWHhvo4DBsP9AXxh8vux237gtkRC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736249679; c=relaxed/simple; bh=iOXWpao8tAMomenHhnLXm2BgavMdVLScmJoiwFvtpWU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q1CEjZuZXkBrUxTnPfTQ1bjPrxeOK/j+PYFnOHcBUGCPoSBt5EHuCNr8VoAFBHVa1SnRtsFqiASw7OFXSGjXDKkRB02vTnZHIq8PsmCPmdLDJ6o6m2X1W+aWTdGkuwVhakciY0v35dWaYDA7UjHC6mLWLyjHbGuTv8+IciC1svw= 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=UIsLBGy3; arc=none smtp.client-ip=209.85.221.41 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="UIsLBGy3" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-385ddcfc97bso13008365f8f.1 for ; Tue, 07 Jan 2025 03:34:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736249673; x=1736854473; 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=uEwlmUKHFzSaBhYRka1iF8EthtWsBcWlAWnCafzs3Pg=; b=UIsLBGy36c1GXERF68btofX1P/Wynwk7XTZIu3JsANPlB0AJs9FqWt6JfEo/61OFib r9LBTt2Vy+fjbJK5trvHf8aaWauKYEaY+4825Bu/IQ/o5IVaHk5JOQyjc8zfy5/l/t5i xLZm+aXW7xaCi5vH/k2BtX9WH7jCa5xrQRNRgTEY6bbQwBEhrYVAS7effN7hFe4IJZ/g HdKKvd9EWbQhKF+4anbU0JjI/N5xD38kgbiFbtewuR0J6/LOtBsyScO1g4s958Q4eaID qdLpmjWD8EGSSzqhNF1BhjnkpMPNmRCrAeFFgrT5aiNkxijhhEECtydeVnFcgMXD7wo7 qs8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736249673; x=1736854473; 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=uEwlmUKHFzSaBhYRka1iF8EthtWsBcWlAWnCafzs3Pg=; b=kaSRH1VfpDc5lj0AWdmntyJckSZTGO+ny1WZ37GirYLYaRxTfsITywqYmAmI5sPsmy MtDtkXsrBWySnXrKHm6plgJpTsZv2IkuRbd+75GzMi2B5CkPKRyboRRptIkGrbvp2q17 uO6vmBJDJHuiS2NlOHOe4M6EijXyeVhEPKQ+OvmYoJPhgykaZ28fQQQI+aXe6UYhMqso ibWtuXhm7MDnRNDulxSDvkINIEnCUdDO+hHs49Ria+VOkDQiFIywfFrZuI/sxs/1hy1P vY9qPRBZzc7oBgJszrlX2+QXL23ZFEYnDQmNkgujN0afn+UPRw1JYAbbzgj7l9iohGjN WqhA== X-Forwarded-Encrypted: i=1; AJvYcCVm4kIZQjw39X8LePg54mDHAETWS5TLiYVDgRr3GbBlIJysJqbGxkmb1J8HsmOzjnu5dJrHmdzrxUiV6iE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3ZkKmatUqe2Uo8yh8EESJLvKgc/eqlhdmORjmzzf7IiVwGsjR Snu6aQE2mqUwjPsFydu1dH/mhEI6Z7UineBKbhywesk5qINpbOERvdxuPTRrbdw= X-Gm-Gg: ASbGncvKIKR031hDHetJx8usCIBAoaug7PTO//XJohonts0ZliNJzVsBBMmRMMs4y5k ZTHFYUdq5LN+ce1JXqgaaO7vk8H5WrTuwAXi9SOsMTASA8bUdlfKotu4RbEHvYvvMqx0rWnmL7l bi57yWSnVUBWu8T8JKvPVGiVjYV5LwSnurgHkM/vJ0TCNllaczTx3WC1gbmhGjdNkStK1+owm/K ttBVVUaqb53B85dUo8fpLdcIsOjVeMRz7oxyAWFNdscvXgQ44iOkYm8 X-Google-Smtp-Source: AGHT+IHbyZC/6R2NLq2cscP6k/cxmIqPHv5zu3Yng+oav3cmo3eJA6Bdt7S3W1BSgUHa3jUWyRCpEw== X-Received: by 2002:a5d:47af:0:b0:385:f10a:335 with SMTP id ffacd0b85a97d-38a221face5mr50014560f8f.21.1736249673009; Tue, 07 Jan 2025 03:34:33 -0800 (PST) Received: from pop-os.. ([145.224.66.180]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c89e1acsm50299218f8f.68.2025.01.07.03.34.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 03:34:32 -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 , Shiqi Liu , James Morse , Mark Brown , Fuad Tabba , "Rob Herring (Arm)" , Raghavendra Rao Ananta , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 03/10] KVM: arm64: Track presence of SPE/TRBE in kvm_host_data instead of vCPU Date: Tue, 7 Jan 2025 11:32:40 +0000 Message-Id: <20250107113252.260631-4-james.clark@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250107113252.260631-1-james.clark@linaro.org> References: <20250107113252.260631-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" From: Oliver Upton Add flags to kvm_host_data to track if SPE/TRBE is present + programmable on a per-CPU basis. Set the flags up at init rather than vcpu_load() as the programmability of these buffers is unlikely to change. Reviewed-by: James Clark Tested-by: James Clark Signed-off-by: Oliver Upton Link: https://lore.kernel.org/r/20241219224116.3941496-4-oliver.upton@linux= .dev Signed-off-by: Marc Zyngier --- arch/arm64/include/asm/kvm_host.h | 19 +++++++++------- arch/arm64/kvm/arm.c | 3 --- arch/arm64/kvm/debug.c | 36 ++++++++---------------------- arch/arm64/kvm/hyp/nvhe/debug-sr.c | 8 +++---- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index 064f5dfca7f4..fb252d540850 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -610,6 +610,10 @@ struct cpu_sve_state { * field. */ struct kvm_host_data { +#define KVM_HOST_DATA_FLAG_HAS_SPE 0 +#define KVM_HOST_DATA_FLAG_HAS_TRBE 1 + unsigned long flags; + struct kvm_cpu_context host_ctxt; =20 /* @@ -911,10 +915,6 @@ struct kvm_vcpu_arch { #define EXCEPT_AA64_EL2_SERR __vcpu_except_flags(7) /* Guest debug is live */ #define DEBUG_DIRTY __vcpu_single_flag(iflags, BIT(4)) -/* Save SPE context if active */ -#define DEBUG_STATE_SAVE_SPE __vcpu_single_flag(iflags, BIT(5)) -/* Save TRBE context if active */ -#define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6)) =20 /* SVE enabled for host EL0 */ #define HOST_SVE_ENABLED __vcpu_single_flag(sflags, BIT(0)) @@ -1310,6 +1310,13 @@ DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_ho= st_data); &this_cpu_ptr_hyp_sym(kvm_host_data)->f) #endif =20 +#define host_data_test_flag(flag) \ + (test_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags))) +#define host_data_set_flag(flag) \ + set_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)) +#define host_data_clear_flag(flag) \ + clear_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)) + /* Check whether the FP regs are owned by the guest */ static inline bool guest_owns_fp_regs(void) { @@ -1370,10 +1377,6 @@ static inline bool kvm_pmu_counter_deferred(struct p= erf_event_attr *attr) return (!has_vhe() && attr->exclude_host); } =20 -/* Flags for host debug state */ -void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu); -void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu); - #ifdef CONFIG_KVM void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr); void kvm_clr_pmu_events(u64 clr); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index ab1bf9ccf385..3822774840e1 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -617,15 +617,12 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cp= u) =20 vcpu_set_pauth_traps(vcpu); =20 - kvm_arch_vcpu_load_debug_state_flags(vcpu); - if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus)) vcpu_set_on_unsupported_cpu(vcpu); } =20 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) { - kvm_arch_vcpu_put_debug_state_flags(vcpu); kvm_arch_vcpu_put_fp(vcpu); if (has_vhe()) kvm_vcpu_put_vhe(vcpu); diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index d8ea6fe6a2a2..1ee2fd765b62 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -294,40 +294,22 @@ void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) } } =20 -void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) +void kvm_init_host_debug_data(void) { - u64 dfr0; + u64 dfr0 =3D read_sysreg(id_aa64dfr0_el1); + + if (cpuid_feature_extract_signed_field(dfr0, ID_AA64DFR0_EL1_PMUVer_SHIFT= ) > 0) + *host_data_ptr(nr_event_counters) =3D FIELD_GET(ARMV8_PMU_PMCR_N, + read_sysreg(pmcr_el0)); =20 - /* For VHE, there is nothing to do */ if (has_vhe()) return; =20 - dfr0 =3D read_sysreg(id_aa64dfr0_el1); - /* - * If SPE is present on this CPU and is available at current EL, - * we may need to check if the host state needs to be saved. - */ if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_PMSVer_SHI= FT) && - !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(PMBIDR_EL1_P_SHIFT))) - vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE); + !(read_sysreg_s(SYS_PMBIDR_EL1) & PMBIDR_EL1_P)) + host_data_set_flag(HAS_SPE); =20 - /* Check if we have TRBE implemented and available at the host */ if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffe= r_SHIFT) && !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P)) - vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE); -} - -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); -} - -void kvm_init_host_debug_data(void) -{ - u64 dfr0 =3D read_sysreg(id_aa64dfr0_el1); - - if (cpuid_feature_extract_signed_field(dfr0, ID_AA64DFR0_EL1_PMUVer_SHIFT= ) > 0) - *host_data_ptr(nr_event_counters) =3D FIELD_GET(ARMV8_PMU_PMCR_N, - read_sysreg(pmcr_el0)); + host_data_set_flag(HAS_TRBE); } diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/d= ebug-sr.c index 1e2a26d0196e..858bb38e273f 100644 --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c @@ -82,10 +82,10 @@ static void __debug_restore_trace(u64 trfcr_el1) void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu) { /* Disable and flush SPE data generation */ - if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE)) + 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 (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE)) + if (host_data_test_flag(HAS_TRBE)) __debug_save_trace(host_data_ptr(host_debug_state.trfcr_el1)); } =20 @@ -96,9 +96,9 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu) =20 void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu) { - if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE)) + if (host_data_test_flag(HAS_SPE)) __debug_restore_spe(*host_data_ptr(host_debug_state.pmscr_el1)); - if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE)) + if (host_data_test_flag(HAS_TRBE)) __debug_restore_trace(*host_data_ptr(host_debug_state.trfcr_el1)); } =20 --=20 2.34.1