Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1566467rda; Mon, 23 Oct 2023 17:27:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHhZ9Aph3WL6gzdQnwaIMr6lihirvgj4nM0CbzsiU1yKLd0Fnl8c7t35PEaouvUe2mzJlPz X-Received: by 2002:a17:90b:f98:b0:27d:3e90:9ee1 with SMTP id ft24-20020a17090b0f9800b0027d3e909ee1mr9384962pjb.23.1698107269720; Mon, 23 Oct 2023 17:27:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698107269; cv=none; d=google.com; s=arc-20160816; b=COl9QR+RS3omJLntE2fHA+7owwDrwqjQfg5yYMTHIKHl++qGVX7+2nOvxNrvpxFPZj w8Ws9K/Nz9BD6mZMhU2STQG9fnwnhbybT6jB3w5gIKW1gHy8YO2rlLyAa+YeIG7ESvQe 3oA/Etm6GUC02WjTr8Q4dLt/RFBgX7rL4DVX86snEStxLJ8JNz/Lpcy946jijpeilowZ uoDXsCHFioWHjuEJRB+fCgFdtZRoHf32xDa4lngtaJp1jF471C32XBDuEUE8X7L65fCz dOPnKWAhLf+kGEJ9F6t4UVT1luaxZ3T8Mit19nGJEIrHQR2GR4zXdfZEiyCyKbqv5/7B 02wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=0U+dLPR8elz3+cGklc6/3G8uITKyjqrZ5KLIjMTZ3q8=; fh=D//PLXZS63mgWSQM2kcZZyW+2k64a7rh3i2k12EZPR4=; b=uPCy7oQW8wg9nxM6iPE/wq7oKiO1LFCYXDJI3FCQrExF6AtlahLS0yqeOsCUgtRbEh j6ynH5VXqyINa6j2JrgcxrsCUUfgjhxzZ/iT7VuVcx049sqhkXO2RLlVkgtFic/3xIOn xD6PJYYiiq60bIpvjRjY8FUzn+P58Sv+uUHpkL8LWR1mY4Qe4TGneitSkx08/5reYkz9 h1IMV6oHKbNFSwELJyBUp63vIMhUrRUN2MI8yeJqdfSWhTFKGGBwdLJQeeTe4JoIQBzG OZ/3lizJC+kuao10hdrjnS+e2iD4R6NiqkkhNYw3jw0TuEiSQOCSU+Jmv6uGqrDXWjw7 PCRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yU1A3L10; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id nv9-20020a17090b1b4900b002791a73ab07si7740907pjb.54.2023.10.23.17.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 17:27:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yU1A3L10; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 2EED280310F2; Mon, 23 Oct 2023 17:27:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231939AbjJXA1V (ORCPT + 99 others); Mon, 23 Oct 2023 20:27:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231913AbjJXA1D (ORCPT ); Mon, 23 Oct 2023 20:27:03 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8D0B10F3 for ; Mon, 23 Oct 2023 17:26:51 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1c9c939cc94so28740485ad.1 for ; Mon, 23 Oct 2023 17:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698107211; x=1698712011; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=0U+dLPR8elz3+cGklc6/3G8uITKyjqrZ5KLIjMTZ3q8=; b=yU1A3L10BSMTA6J2kZ593aTlbJ7631hanoC4gNl2rTP6fQ7wgVbTPa3+ExDiRICWDp qMgaeRkd0SBl/TBpLh2yAyfp+iCgibWfqpkoUjE+gOr9roCdicYQYl6L6B71U2Z30OvF kCTea/DCcpUykKLj7n7gPgoLa6MJBPlGtgoS914YjvDNy98Tg+8zacG7GMlKuq+BrgwD JNdZ+aTz7e+WM5Xlc0XXK93E487SjI7AlRwTmFaPeUDr0zzh/npVktOJpGFirTBfrRTE Rod8OZtUCrH+wT5+qbFA2SVBuRsPBeu07qBozr01n2g0MnlfI629RKOwVc7mHC8uoEbR /K0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698107211; x=1698712011; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0U+dLPR8elz3+cGklc6/3G8uITKyjqrZ5KLIjMTZ3q8=; b=YA7zSnJj1sTm2PinT2h6ast30nbRq/hPsYnJzknQ52n2cnYlZHB50C2sIWBMPZLkJ6 gnH+iR7+RcEIWA5aeITylUCIjVONg1iIkS7dwffFZbi6AIPrK2+TfTPC2WRY+uJriBr9 sHuSskzx+OZpNHfiXY3fbje+WAwSMBqAk5WCksQ2S3yJA3rxZjqmhnGYMzGNl5FOOxJ0 OzoBcAffmdJMQVN/bCeehTYHF5LHadCsL9xTQgISmdqE8DN2z8UHnXsG1oqdhxqGkTne DAqcwEmyQndEzW9AyBKmHVyw7/++9TDshX3VmaH5iv+wPFYQTz9MtO5m6HMuBrIPsAY6 g6Ig== X-Gm-Message-State: AOJu0Yw0fyg+uyP1OikILuFasAQvdGMIpky+7zWuKePgy4Enf5RrNx2G hFyyCOoj2an6OnsqLyQM27riBE6FWJY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:64c2:b0:1cb:de60:874c with SMTP id y2-20020a17090264c200b001cbde60874cmr37994pli.12.1698107211277; Mon, 23 Oct 2023 17:26:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 23 Oct 2023 17:26:28 -0700 In-Reply-To: <20231024002633.2540714-1-seanjc@google.com> Mime-Version: 1.0 References: <20231024002633.2540714-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231024002633.2540714-9-seanjc@google.com> Subject: [PATCH v5 08/13] KVM: selftests: Test Intel PMU architectural events on gp counters From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jinrong Liang , Like Xu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 23 Oct 2023 17:27:46 -0700 (PDT) From: Jinrong Liang Add test cases to check if different Architectural events are available after it's marked as unavailable via CPUID. It covers vPMU event filtering logic based on Intel CPUID, which is a complement to pmu_event_filter. According to Intel SDM, the number of architectural events is reported through CPUID.0AH:EAX[31:24] and the architectural event x is supported if EBX[x]=0 && EAX[31:24]>x. Co-developed-by: Like Xu Signed-off-by: Like Xu Signed-off-by: Jinrong Liang Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/pmu_counters_test.c | 189 ++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/pmu_counters_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index ed1c17cabc07..4c024fb845b4 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -82,6 +82,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_test TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test +TEST_GEN_PROGS_x86_64 += x86_64/pmu_counters_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c new file mode 100644 index 000000000000..2a6336b994d5 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023, Tencent, Inc. + */ + +#define _GNU_SOURCE /* for program_invocation_short_name */ +#include + +#include "pmu.h" +#include "processor.h" + +/* Guest payload for any performance counter counting */ +#define NUM_BRANCHES 10 + +static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, + void *guest_code) +{ + struct kvm_vm *vm; + + vm = vm_create_with_one_vcpu(vcpu, guest_code); + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(*vcpu); + + return vm; +} + +static void run_vcpu(struct kvm_vcpu *vcpu) +{ + struct ucall uc; + + do { + vcpu_run(vcpu); + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + break; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unexpected ucall: %lu", uc.cmd); + } + } while (uc.cmd != UCALL_DONE); +} + +static bool pmu_is_intel_event_stable(uint8_t idx) +{ + switch (idx) { + case INTEL_ARCH_CPU_CYCLES: + case INTEL_ARCH_INSTRUCTIONS_RETIRED: + case INTEL_ARCH_REFERENCE_CYCLES: + case INTEL_ARCH_BRANCHES_RETIRED: + return true; + default: + return false; + } +} + +static void guest_measure_pmu_v1(struct kvm_x86_pmu_feature event, + uint32_t counter_msr, uint32_t nr_gp_counters) +{ + uint8_t idx = event.f.bit; + unsigned int i; + + for (i = 0; i < nr_gp_counters; i++) { + wrmsr(counter_msr + i, 0); + wrmsr(MSR_P6_EVNTSEL0 + i, ARCH_PERFMON_EVENTSEL_OS | + ARCH_PERFMON_EVENTSEL_ENABLE | intel_pmu_arch_events[idx]); + __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES})); + + if (pmu_is_intel_event_stable(idx)) + GUEST_ASSERT_EQ(this_pmu_has(event), !!_rdpmc(i)); + + wrmsr(MSR_P6_EVNTSEL0 + i, ARCH_PERFMON_EVENTSEL_OS | + !ARCH_PERFMON_EVENTSEL_ENABLE | + intel_pmu_arch_events[idx]); + wrmsr(counter_msr + i, 0); + __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES})); + + if (pmu_is_intel_event_stable(idx)) + GUEST_ASSERT(!_rdpmc(i)); + } + + GUEST_DONE(); +} + +static void guest_measure_loop(uint8_t idx) +{ + const struct { + struct kvm_x86_pmu_feature gp_event; + } intel_event_to_feature[] = { + [INTEL_ARCH_CPU_CYCLES] = { X86_PMU_FEATURE_CPU_CYCLES }, + [INTEL_ARCH_INSTRUCTIONS_RETIRED] = { X86_PMU_FEATURE_INSNS_RETIRED }, + [INTEL_ARCH_REFERENCE_CYCLES] = { X86_PMU_FEATURE_REFERENCE_CYCLES }, + [INTEL_ARCH_LLC_REFERENCES] = { X86_PMU_FEATURE_LLC_REFERENCES }, + [INTEL_ARCH_LLC_MISSES] = { X86_PMU_FEATURE_LLC_MISSES }, + [INTEL_ARCH_BRANCHES_RETIRED] = { X86_PMU_FEATURE_BRANCH_INSNS_RETIRED }, + [INTEL_ARCH_BRANCHES_MISPREDICTED] = { X86_PMU_FEATURE_BRANCHES_MISPREDICTED }, + }; + + uint32_t nr_gp_counters = this_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS); + uint32_t pmu_version = this_cpu_property(X86_PROPERTY_PMU_VERSION); + struct kvm_x86_pmu_feature gp_event; + uint32_t counter_msr; + unsigned int i; + + if (rdmsr(MSR_IA32_PERF_CAPABILITIES) & PMU_CAP_FW_WRITES) + counter_msr = MSR_IA32_PMC0; + else + counter_msr = MSR_IA32_PERFCTR0; + + gp_event = intel_event_to_feature[idx].gp_event; + TEST_ASSERT_EQ(idx, gp_event.f.bit); + + if (pmu_version < 2) { + guest_measure_pmu_v1(gp_event, counter_msr, nr_gp_counters); + return; + } + + for (i = 0; i < nr_gp_counters; i++) { + wrmsr(counter_msr + i, 0); + wrmsr(MSR_P6_EVNTSEL0 + i, ARCH_PERFMON_EVENTSEL_OS | + ARCH_PERFMON_EVENTSEL_ENABLE | + intel_pmu_arch_events[idx]); + + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, BIT_ULL(i)); + __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES})); + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0); + + if (pmu_is_intel_event_stable(idx)) + GUEST_ASSERT_EQ(this_pmu_has(gp_event), !!_rdpmc(i)); + } + + GUEST_DONE(); +} + +static void test_arch_events_cpuid(uint8_t i, uint8_t j, uint8_t idx) +{ + uint8_t arch_events_unavailable_mask = BIT_ULL(j); + uint8_t arch_events_bitmap_size = BIT_ULL(i); + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_measure_loop); + + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH, + arch_events_bitmap_size); + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_EVENTS_MASK, + arch_events_unavailable_mask); + + vcpu_args_set(vcpu, 1, idx); + + run_vcpu(vcpu); + + kvm_vm_free(vm); +} + +static void test_intel_arch_events(void) +{ + uint8_t idx, i, j; + + for (idx = 0; idx < NR_INTEL_ARCH_EVENTS; idx++) { + /* + * A brute force iteration of all combinations of values is + * likely to exhaust the limit of the single-threaded thread + * fd nums, so it's test by iterating through all valid + * single-bit values. + */ + for (i = 0; i < NR_INTEL_ARCH_EVENTS; i++) { + for (j = 0; j < NR_INTEL_ARCH_EVENTS; j++) + test_arch_events_cpuid(i, j, idx); + } + } +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(get_kvm_param_bool("enable_pmu")); + + 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); + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM)); + + test_intel_arch_events(); + + return 0; +} -- 2.42.0.758.gaed0368e0e-goog