From patchwork Wed Sep 18 20:53:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 829496 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 339A66FC5 for ; Wed, 18 Sep 2024 20:54:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692841; cv=none; b=u+XDH8kPFnw9CNxaaTJ10sYYCxmSf9Y/vMB4DuDUMy13nB9WZU5wcnnhmWneOVBu5E/465xQLM7wmpvWeX1gPwtziddJfhmlVvhusdjV0jF2lOUV1uRLiw5BVEDTKDuTp8dd1BG9LPH0vQcIeCEmiB+Q0CqU9axiPzrRjIHTt7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692841; c=relaxed/simple; bh=7LFbPeFjkQq1UTJ8lEzVj85jrHSO5Ur9v/Y+l3mM/6A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CHC5PRhM4ZJONpaSfMG+fSPWXpLu//KythAR/CmAEE0ld1usy0vNQozvlVIo2xJvclnOp8G9Ca63ji1zE3cRXi2BwJzZUKAWjPdehN7sgBI5S0+QCped3rtk1rBM8AV3rYXNo00hErfAdEYeXQ4h/LU+7l9nj++eudQjdr2m2Yk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aTlvEM/I; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aTlvEM/I" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e02fff66a83so387818276.0 for ; Wed, 18 Sep 2024 13:53:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726692839; x=1727297639; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8rJ/KpP7ZLjH/baI+Q4VTgii8fM80xVyJoccolaGcXA=; b=aTlvEM/I8pCaQl8i/1OmC4g4gkSXYcpZlslWxUhWtYTDfGfjGRQb1zxEyVl6KgEk+k VfTE69P1955Qkmyc/WQHANhsb8ujWKbDFrmbIBv3BKH3Wfa/2MZAe7Zx632BfbUCRCTZ or/OHZq1VxZO0cbT8PrMFjedQJqKwX4DFFZ3SKlG/ns7rj44sbv4k9nz6lnpZ0cr9Hks L41kaJeehLWKjfjGoIyZCOEHHHnZo6Acdu3xCiop+kpIzG5FdYiLIHuASpTv0p/BQbT1 Jx9F9Fi7PAw1lLP8SJA/FHwT1eABd6neOJ6qBAonXdyD9IU6+4wlJwM967qkBCIyzKjg VkIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726692839; x=1727297639; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8rJ/KpP7ZLjH/baI+Q4VTgii8fM80xVyJoccolaGcXA=; b=j+oKEgM6sDVv/imnzffHCfsAESTyDyR9fm5isyCGpsz8u/8suyBh3h9HtEI/2pbaxd HrpBK2q/cbQVKAyl1zOsznm1SgJyhNupNXNLRd8MaopgXkOOTC8TXFK/CBeii/3yGsxl XdQiuI81JqzxMCg6wtoEdpviAOzqtZmYD5KKDsxBJxSiWpPdmFATWM9UjF4exIbPcD8u AzDLsycu/gb9HeqXbxNW/4JQaYxYEHjp5gcSB4IOvMW19dgfFXkuHEFx7TWP6B5UsvDW 2XEeKdH9mhicED9Mccg6pG2jJlsgpImgj06iA39hFLVPPt2jbTZi9bTZFExKoxDPHWU/ rtKA== X-Forwarded-Encrypted: i=1; AJvYcCXrP//e6/r8+YMvfj235sJ+1zrCAQY5VuIW1tAyYvNq5xJyka09NP5vJJosgmG+HSp6R5kgC5BDYzBuN1PtIaY=@vger.kernel.org X-Gm-Message-State: AOJu0YwtL9V2JFifzA+yDHR/ycAztVkTVTbJC6XALhFV0FSoOIIEDDAV P5ZedyGv1j5zEQLAIQgQ80JHSQswOzJYN+wnDKQFDOBf/QEz95CmM6/fQYLMFo2rlrXFabXy48c i2ofxfeNWOF3WMIOml+VRwQ== X-Google-Smtp-Source: AGHT+IE19c7uHgFQDSF5lDBOr6e84MO4hTZGPdwklurCeZKa3gXSTOztaJSR5dLNDju6LDGeGsq93ws0AB9ttpRl9Q== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:11b:3898:ac11:fa18]) (user=coltonlewis job=sendgmr) by 2002:a25:f304:0:b0:e11:7105:956a with SMTP id 3f1490d57ef6-e1d9db9c23fmr34188276.3.1726692839006; Wed, 18 Sep 2024 13:53:59 -0700 (PDT) Date: Wed, 18 Sep 2024 20:53:14 +0000 In-Reply-To: <20240918205319.3517569-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240918205319.3517569-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog Message-ID: <20240918205319.3517569-2-coltonlewis@google.com> Subject: [PATCH v2 1/6] KVM: x86: selftests: Fix typos in macro variable use From: Colton Lewis To: kvm@vger.kernel.org Cc: Mingwei Zhang , Jinrong Liang , Jim Mattson , Aaron Lewis , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Colton Lewis Without the leading underscore, these variables are referencing a variable in the calling scope. It only worked before by accident because all calling scopes had a variable with the right name. Fixes: cd34fd8c758e ("KVM: selftests: Test PMC virtualization with forced emulation") Signed-off-by: Colton Lewis --- tools/testing/selftests/kvm/x86_64/pmu_counters_test.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index 698cb36989db..0e305e43a93b 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -174,7 +174,7 @@ do { \ #define GUEST_TEST_EVENT(_idx, _event, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ do { \ - wrmsr(pmc_msr, 0); \ + wrmsr(_pmc_msr, 0); \ \ if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt .", FEP); \ @@ -331,9 +331,9 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ expect_gp ? "#GP" : "no fault", msr, vector) \ #define GUEST_ASSERT_PMC_VALUE(insn, msr, val, expected) \ - __GUEST_ASSERT(val == expected_val, \ + __GUEST_ASSERT(val == expected, \ "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \ - msr, expected_val, val); + msr, expected, val); static void guest_test_rdpmc(uint32_t rdpmc_idx, bool expect_success, uint64_t expected_val) From patchwork Wed Sep 18 20:53:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 829495 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 178821CB33D for ; Wed, 18 Sep 2024 20:54:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692844; cv=none; b=YE48Qasy9F8yG3vkRVVFVI592UA8FhVHUaE/kiNk0VFCLor0ZK0Moq0X8BQG8ED6N1bdSzSQw9SgFiRBOVS91JfZGc1DlUdDaE461u5kQI2RD1B3itl1fGV5hTyiTtVTWTFO5NE1loue92G5pmBIVsKXoT86yKBs02Kpt00pS54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692844; c=relaxed/simple; bh=p6MzaJUynW/JjkmeZKB5mW65nL3/Igk6OCMaHic7+aw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YzhQU652tpUC+wOjFu31hQodwYA3iQPn/VyjW/fMHUStu0tXGhtitBuJe2QoLxUsgIQ8ZU8V/1NOkqXuwFPhgxXsQSX/E4tXyRCmmg7F5N4vRJRsWcbPpj5Kc9CUTwcTU7as8yUAujO4xrQTCIbfPBuumWLBytGvOeX74SWooDQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SxvwbFWt; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SxvwbFWt" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-69a0536b23aso2267477b3.3 for ; Wed, 18 Sep 2024 13:54:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726692841; x=1727297641; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YwPgtOW129JrsQoACWdjEIl4s6WZlvFOhv3Du6OF+8U=; b=SxvwbFWtq/Snn8Opm0acT0QfMfIdgN1dq0W8sk1O7KOrYEPCwyEkmuiW1kTIvWw/Jp IMRiq8DB8T6ptwGLcouwfxB7Pm1aAMVjXQ4bR6RUOt355LAGotp0yGzwhpg0vup8qAa+ gglnsrl+GV7uDLG3i6+pq0MB1HZJt5DueTbO1QL9oW9EX4v/f8TTd3wR0efUPv2je4H+ ioinA2e/al6rHbRaVJ+A2HW3zD0JUCLioD/CD6B+ngTJeQLJaEBakg8Dpg/aBVPK5INy /tpcIfym8tvprxLN9qXEld4aQPprCAf38yxdCp69+C3878R0NSsAEnqQfmIMjShKV3eu 7RQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726692841; x=1727297641; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YwPgtOW129JrsQoACWdjEIl4s6WZlvFOhv3Du6OF+8U=; b=hOVoC7O8uonj/uD3sWBUbHC1oYQXnNNd4MnmsAu5yaijPnlbJOd+Buxt6io8cgB7LZ mYfiXzj9WYN1G+Bko+HUs/b64wLvbpsP9zO/h9Mte4RcBASUlt8VrcvaUDi30fNsbwEW y5cwAFyYijA0B0JgEJ7D86nrZWkxtSTRP32QvPwlm1nSsqrhyGAzBLMgqTIi2YSNkWce Fqnuoht+BttnnZNLzRHtyY4Kut9ib4+jIaYGZx9kkRARcs7i27yKWzhPwtmUGWMf34gG RSuVSG1Wlo7vDcyMDQ093c5/TQTRW/RK7FmMXY7dzdmqRI8tgocSAqXZVsIiZun7sw12 h4cQ== X-Forwarded-Encrypted: i=1; AJvYcCWqgYK1F+4M9Wvyq/eS9JuLtWvNaiDzQC1PA/UArQcPkqGUA9qSgvEKjBPHHd/9nVZw9394NFvITOs2peYZm7M=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1pIbv5qN3Z9TrwiGyeXJ7GPyxtxbQgaGuXdRFy1Yp7J8ViBSY 1zzx4kTstcwjeXkjQl2VkyPiQcAD6FCNPN8xiB6IT4WPfgWVwNaJYOCCoeQp+ZRGX/pG/9WblTC 3tpDicswJ5Dj5iDJiJBYlHw== X-Google-Smtp-Source: AGHT+IGRPIBH0lJ/zYqTw1ktDVZvUIdvA2LFZQYoQbGDgG/n34gqx1ZHStAZ7pNzlmKV4Cl93mXbQJCAfZuocHz/Dw== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:11b:3898:ac11:fa18]) (user=coltonlewis job=sendgmr) by 2002:a81:cb07:0:b0:6db:e107:74cb with SMTP id 00721157ae682-6dbe1077640mr6957727b3.3.1726692840998; Wed, 18 Sep 2024 13:54:00 -0700 (PDT) Date: Wed, 18 Sep 2024 20:53:16 +0000 In-Reply-To: <20240918205319.3517569-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240918205319.3517569-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog Message-ID: <20240918205319.3517569-4-coltonlewis@google.com> Subject: [PATCH v2 3/6] KVM: x86: selftests: Set up AMD VM in pmu_counters_test From: Colton Lewis To: kvm@vger.kernel.org Cc: Mingwei Zhang , Jinrong Liang , Jim Mattson , Aaron Lewis , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Colton Lewis Branch in main() depending on if the CPU is Intel or AMD. They are subject to vastly different requirements because the AMD PMU lacks many properties defined by the Intel PMU including the entire CPUID 0xa function where Intel stores all the PMU properties. AMD lacks this as well as any consistent notion of PMU versions as Intel does. Every feature is a separate flag and they aren't the same features as Intel. Set up a VM for testing core AMD counters and ensure proper CPUID features are set. Signed-off-by: Colton Lewis --- .../selftests/kvm/x86_64/pmu_counters_test.c | 104 ++++++++++++++---- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index 0e305e43a93b..5b240585edc5 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -30,10 +30,21 @@ #define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS) +/* + * Limit testing to MSRs that are actually defined by Intel (in the SDM). MSRs + * that aren't defined counter MSRs *probably* don't exist, but there's no + * guarantee that currently undefined MSR indices won't be used for something + * other than PMCs in the future. + */ +#define MAX_NR_GP_COUNTERS 8 +#define MAX_NR_FIXED_COUNTERS 3 +#define AMD_NR_CORE_COUNTERS 4 +#define AMD_NR_CORE_EXT_COUNTERS 6 + static uint8_t kvm_pmu_version; static bool kvm_has_perf_caps; -static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, +static struct kvm_vm *intel_pmu_vm_create(struct kvm_vcpu **vcpu, void *guest_code, uint8_t pmu_version, uint64_t perf_capabilities) @@ -303,7 +314,7 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities, if (!pmu_version) return; - vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_arch_events, + vm = intel_pmu_vm_create(&vcpu, guest_test_arch_events, pmu_version, perf_capabilities); vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH, @@ -316,15 +327,6 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities, kvm_vm_free(vm); } -/* - * Limit testing to MSRs that are actually defined by Intel (in the SDM). MSRs - * that aren't defined counter MSRs *probably* don't exist, but there's no - * guarantee that currently undefined MSR indices won't be used for something - * other than PMCs in the future. - */ -#define MAX_NR_GP_COUNTERS 8 -#define MAX_NR_FIXED_COUNTERS 3 - #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector) \ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ "Expected %s on " #insn "(0x%x), got vector %u", \ @@ -463,7 +465,7 @@ static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities, struct kvm_vcpu *vcpu; struct kvm_vm *vm; - vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_gp_counters, + vm = intel_pmu_vm_create(&vcpu, guest_test_gp_counters, pmu_version, perf_capabilities); vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_GP_COUNTERS, @@ -530,7 +532,7 @@ static void test_fixed_counters(uint8_t pmu_version, uint64_t perf_capabilities, struct kvm_vcpu *vcpu; struct kvm_vm *vm; - vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_fixed_counters, + vm = intel_pmu_vm_create(&vcpu, guest_test_fixed_counters, pmu_version, perf_capabilities); vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK, @@ -627,18 +629,78 @@ static void test_intel_counters(void) } } -int main(int argc, char *argv[]) +static uint8_t nr_core_counters(void) { - TEST_REQUIRE(kvm_is_pmu_enabled()); + uint8_t nr_counters = kvm_cpu_property(X86_PROPERTY_NUM_PERF_CTR_CORE); + bool core_ext = kvm_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + + if (nr_counters != 0) + return nr_counters; + + if (core_ext) + return AMD_NR_CORE_EXT_COUNTERS; - TEST_REQUIRE(host_cpu_is_intel); - TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); - TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); + return AMD_NR_CORE_COUNTERS; - kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); - kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM); +} + +static void guest_test_core_counters(void) +{ + GUEST_DONE(); +} + +static void test_core_counters(void) +{ + uint8_t nr_counters = nr_core_counters(); + bool core_ext = kvm_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + bool perfmon_v2 = kvm_cpu_has(X86_FEATURE_PERFMON_V2); + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + for (uint8_t ce = 0; ce <= core_ext; ce++) { + for (uint8_t pm = 0; pm <= perfmon_v2; pm++) { + for (uint8_t nc = 0; nc <= nr_counters; nc++) { + vm = vm_create_with_one_vcpu(&vcpu, guest_test_core_counters); + + if (nc) + vcpu_set_cpuid_property( + vcpu, X86_PROPERTY_NUM_PERF_CTR_CORE, nc); + if (ce) + vcpu_set_cpuid_feature( + vcpu, X86_FEATURE_PERF_CTR_EXT_CORE); + if (pm) + vcpu_set_cpuid_feature( + vcpu, X86_FEATURE_PERFMON_V2); + + pr_info("Testing core counters: CoreExt = %u, PerfMonV2 = %u, NumCounters = %u\n", + ce, pm, nc); + run_vcpu(vcpu); + + kvm_vm_free(vm); + } + } + } +} + +static void test_amd_counters(void) +{ + test_core_counters(); +} - test_intel_counters(); +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_is_pmu_enabled()); + + if (host_cpu_is_intel) { + TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); + TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0); + kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); + kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM); + test_intel_counters(); + } else if (host_cpu_is_amd) { + /* AMD CPUs don't have the same properties to look at. */ + test_amd_counters(); + } return 0; } From patchwork Wed Sep 18 20:53:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colton Lewis X-Patchwork-Id: 829494 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 5E1E11CBE9D for ; Wed, 18 Sep 2024 20:54:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692846; cv=none; b=j6hgd8KZHxb1nejW6OfDWCtYqTxzf9Kuk576r2o7O+8K3rAdM/DC9YPMsoGPSzc7kVY1IssVs89Mc6ZfQcpELPoSzJwLsYtcHWpdMte4TiyAneiYMTpmSsEapEhGC2t7FkRcuGWL2w0opgh/dLpc3h6tZtcSOQUKDAdKxCzFx54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726692846; c=relaxed/simple; bh=EhE7fT+kvYR1Q8z0VYUdV57MUeryDUN91qCH81aB3uA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Vy+D72Qt2TFyZJXiymHYWPN1ihDYBRCdEgjPigDJ5jgrJiuL80Y1l/pfo/K9Zp8SQaYv4+6ipoGnANfTOKKQpShbg1fm7rlqYANJ7M/nmR2vwTY3Ne/Bl9NxokKZNd6Wz4JjNm59N7NOmREYKZLarALIknmXPVad1X7czRndxlM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xUY/Koia; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xUY/Koia" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1aa529e30eso334556276.1 for ; Wed, 18 Sep 2024 13:54:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726692843; x=1727297643; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5LjPPMrNjWPhkrbIBVeS2QTzariP8KiUDD2AXY1srT0=; b=xUY/KoiaYu1WcZptKlHhPCDe7aKzCN5E3fMjLVG5KMSw5yU0uyrfnd1r6+7mPKSRUd cHIj7jYWihdLEOg2kifSO7zYS5jfaYWXAIEx/oLEJ2I0xDcqRUaOHkA2NvSdNy4dw7Kd 6Kd35x+uyfDNtGWA0/G+E3NCx+qbxFJGxjtt9LAqShBsPdyAKhYkS/tyJcO4+hQFtqQl h6F37R+fnMn/hZYP45RyHFEqPwZh4cky3hGtcheJZLTvtH6JJjGKPp4SfN5J3GyUU758 5QztmkgSiLCDX8gpSaRDiJDC6clfYnaQmNVxmNhKTSvQzaPqT5Tx2EFMzfdQ364n72Mt 0MkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726692843; x=1727297643; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5LjPPMrNjWPhkrbIBVeS2QTzariP8KiUDD2AXY1srT0=; b=owcAUG0L3YyUjP1Koyco6eGzmlQAy10RCKNvogA9wjku2Mz5/+TVynIaNVEkckMMvv mMgbNFwBGDUjddbKCT2A6uNOicbsfYqk9LEgOfsIwdmXMy7vrpWOTLvDai2jKKHMC5ko RZO80U9gxiyNeEnMWBLBUQBtJ2IkkcSRjSKGRUuLMVXGsqh9JHOUDdU60gqNcp69DOAe KiVHPJKkbRnsCJJjZKYhuB9iWp1K1ED++erBtf81KdDplYz3XApgovs+KAiu8lcb0k5U xbuDMLDkxR43ZBgkTzw0idlgs5c/CfapxyqOmmrxLRGb0XadZsLsxi0bqow7F8ZoiwbE xUGQ== X-Forwarded-Encrypted: i=1; AJvYcCU/aDsQdKDNzkb4QmdWIEJZT7Ywe/gaDQbBniLMhCN1W/cHAI/TSYCN4AQgWOG3JJ7hwPVoP3EW0R9YebbmumY=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5tQgMkJDG3iysTH+0dtsEcq7KeVu2RMPw0Q/s6XV9i9u9sjCs igKRyTZld0zXq0a/xI/5lz300S4pNvWDp/CXK+cbn7UYyDlssjkDLNm94bfW3wDpzyxdQpCD4Om xboSThlAsRymfCc6aA7uGtQ== X-Google-Smtp-Source: AGHT+IFsuKcr5FzVL+z7aQg1GQc1dsi2r2Svsejd/GzBtLLxOhu7F6DPwsXM3Vd9fngcGMgwH7awUkawVaVTnLnOpw== X-Received: from coltonlewis-kvm.c.googlers.com ([fda3:e722:ac3:cc00:11b:3898:ac11:fa18]) (user=coltonlewis job=sendgmr) by 2002:a25:3602:0:b0:e1d:2043:da46 with SMTP id 3f1490d57ef6-e1daff6d90bmr44632276.3.1726692843155; Wed, 18 Sep 2024 13:54:03 -0700 (PDT) Date: Wed, 18 Sep 2024 20:53:18 +0000 In-Reply-To: <20240918205319.3517569-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240918205319.3517569-1-coltonlewis@google.com> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog Message-ID: <20240918205319.3517569-6-coltonlewis@google.com> Subject: [PATCH v2 5/6] KVM: x86: selftests: Test core events From: Colton Lewis To: kvm@vger.kernel.org Cc: Mingwei Zhang , Jinrong Liang , Jim Mattson , Aaron Lewis , Sean Christopherson , Paolo Bonzini , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Colton Lewis Test events on core counters by iterating through every combination of events in amd_pmu_zen_events with every core counter. For each combination, calculate the appropriate register addresses for the event selection/control register and the counter register. The base addresses and layout schemes change depending on whether we have the CoreExt feature. To do the testing, reuse GUEST_TEST_EVENT to run a standard known workload. Decouple it from guest_assert_event_count (now guest_assert_intel_event_count) to generalize to AMD. Then assert the most specific detail that can be reasonably known about the counter result. Exact count is defined and known for some events and for other events merely asserted to be nonzero. Note on exact counts: AMD counts one more branch than Intel for the same workload. Though I can't confirm a reason, the only thing it could be is the boundary of the loop instruction being counted differently. Presumably, when the counter reaches 0 and execution continues to the next instruction, AMD counts this as a branch and Intel doesn't. Signed-off-by: Colton Lewis --- .../selftests/kvm/x86_64/pmu_counters_test.c | 87 ++++++++++++++++--- 1 file changed, 77 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index 79ca7d608e00..cf2941cc7c4c 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -29,6 +29,9 @@ /* Total number of instructions retired within the measured section. */ #define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS) +/* AMD counting one extra branch. Probably at loop boundary condition. */ +#define NUM_BRANCH_INSNS_RETIRED_AMD (NUM_LOOPS+1) +#define NUM_INSNS_RETIRED_AMD (NUM_INSNS_RETIRED+1) /* * Limit testing to MSRs that are actually defined by Intel (in the SDM). MSRs @@ -109,7 +112,7 @@ static uint8_t guest_get_pmu_version(void) * Sanity check that in all cases, the event doesn't count when it's disabled, * and that KVM correctly emulates the write of an arbitrary value. */ -static void guest_assert_event_count(uint8_t idx, +static void guest_assert_intel_event_count(uint8_t idx, struct kvm_x86_pmu_feature event, uint32_t pmc, uint32_t pmc_msr) { @@ -151,6 +154,33 @@ static void guest_assert_event_count(uint8_t idx, GUEST_ASSERT_EQ(_rdpmc(pmc), 0xdead); } +static void guest_assert_amd_event_count(uint8_t evt_idx, uint8_t cnt_idx, uint32_t pmc_msr) +{ + uint64_t count; + uint64_t count_pmc; + + count = rdmsr(pmc_msr); + count_pmc = _rdpmc(cnt_idx); + GUEST_ASSERT_EQ(count, count_pmc); + + switch (evt_idx) { + case AMD_ZEN_CORE_CYCLES_INDEX: + GUEST_ASSERT_NE(count, 0); + break; + case AMD_ZEN_INSTRUCTIONS_INDEX: + GUEST_ASSERT_EQ(count, NUM_INSNS_RETIRED_AMD); + break; + case AMD_ZEN_BRANCHES_INDEX: + GUEST_ASSERT_EQ(count, NUM_BRANCH_INSNS_RETIRED_AMD); + break; + case AMD_ZEN_BRANCH_MISSES_INDEX: + GUEST_ASSERT_NE(count, 0); + break; + default: + break; + } + +} /* * Enable and disable the PMC in a monolithic asm blob to ensure that the * compiler can't insert _any_ code into the measured sequence. Note, ECX @@ -183,28 +213,29 @@ do { \ ); \ } while (0) -#define GUEST_TEST_EVENT(_idx, _event, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ +#define GUEST_TEST_EVENT(_pmc_msr, _ctrl_msr, _ctrl_value, FEP) \ do { \ wrmsr(_pmc_msr, 0); \ \ if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt .", FEP); \ + GUEST_MEASURE_EVENT(_ctrl_msr, _ctrl_value, "clflushopt .", FEP); \ else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush .", FEP); \ + GUEST_MEASURE_EVENT(_ctrl_msr, _ctrl_value, "clflush .", FEP); \ else \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \ - \ - guest_assert_event_count(_idx, _event, _pmc, _pmc_msr); \ + GUEST_MEASURE_EVENT(_ctrl_msr, _ctrl_value, "nop", FEP); \ } while (0) static void __guest_test_arch_event(uint8_t idx, struct kvm_x86_pmu_feature event, uint32_t pmc, uint32_t pmc_msr, uint32_t ctrl_msr, uint64_t ctrl_msr_value) { - GUEST_TEST_EVENT(idx, event, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, ""); + GUEST_TEST_EVENT(pmc_msr, ctrl_msr, ctrl_msr_value, ""); + guest_assert_intel_event_count(idx, event, pmc, pmc_msr); - if (is_forced_emulation_enabled) - GUEST_TEST_EVENT(idx, event, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, KVM_FEP); + if (is_forced_emulation_enabled) { + GUEST_TEST_EVENT(pmc_msr, ctrl_msr, ctrl_msr_value, KVM_FEP); + guest_assert_intel_event_count(idx, event, pmc, pmc_msr); + } } #define X86_PMU_FEATURE_NULL \ @@ -697,9 +728,45 @@ static void guest_test_rdwr_core_counters(void) } } +static void __guest_test_core_event(uint8_t event_idx, uint8_t counter_idx) +{ + /* One fortunate area of actual compatibility! This register + * layout is the same for both AMD and Intel. + */ + uint64_t eventsel = ARCH_PERFMON_EVENTSEL_OS | + ARCH_PERFMON_EVENTSEL_ENABLE | + amd_pmu_zen_events[event_idx]; + bool core_ext = this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE); + uint64_t esel_msr_base = core_ext ? MSR_F15H_PERF_CTL : MSR_K7_EVNTSEL0; + uint64_t cnt_msr_base = core_ext ? MSR_F15H_PERF_CTR : MSR_K7_PERFCTR0; + uint64_t msr_step = core_ext ? 2 : 1; + uint64_t esel_msr = esel_msr_base + msr_step * counter_idx; + uint64_t cnt_msr = cnt_msr_base + msr_step * counter_idx; + + GUEST_TEST_EVENT(cnt_msr, esel_msr, eventsel, ""); + guest_assert_amd_event_count(event_idx, counter_idx, cnt_msr); + + if (is_forced_emulation_enabled) { + GUEST_TEST_EVENT(cnt_msr, esel_msr, eventsel, KVM_FEP); + guest_assert_amd_event_count(event_idx, counter_idx, cnt_msr); + } + +} + +static void guest_test_core_events(void) +{ + uint8_t nr_counters = guest_nr_core_counters(); + + for (uint8_t i = 0; i < NR_AMD_ZEN_EVENTS; i++) { + for (uint8_t j = 0; j < nr_counters; j++) + __guest_test_core_event(i, j); + } +} + static void guest_test_core_counters(void) { guest_test_rdwr_core_counters(); + guest_test_core_events(); GUEST_DONE(); }