Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19861864rwd; Wed, 28 Jun 2023 15:45:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5XD98IcN1lWhbhYozQW2vg4mPyWRAc3MkIQ3D6VMe8Mcqf6qf7CHMJoGjmj7A+yXORBLDv X-Received: by 2002:a05:6a20:3956:b0:110:f66c:a0e1 with SMTP id r22-20020a056a20395600b00110f66ca0e1mr2930895pzg.14.1687992300281; Wed, 28 Jun 2023 15:45:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687992300; cv=none; d=google.com; s=arc-20160816; b=b6SD+F0astGbeJFJ4iWoe+sKtnAd4EhbRCl3nRtFOiAi3TA240nc8XhcfyrGNjDOZ+ ksYyIRZeJJc7HKkYoNAC40uwZs8Fnge0a6XZOvPptXi9u39HeESEPBSf3UOz22cvBz8L xKAokx8yJ6ef/cWn7Tx2VYz3WyX4VxsZ/pNjnbby1YbS1LS8jgaOBEX3NoG1JuevEOlp Tlo7j0PZr4vSki4mpEg4cP+m/miexJje8VgpuO535ToJlT2wIjfk1dD8cQ58hyQGnxiQ B2ERUym1iZ/3CablYgQrer7TBB4SWYMpLgWS/lepzpHJw+TwdfA3dl0ZxutqaDDV8f4W +2aw== 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:dkim-signature; bh=u1lKcqn9ySILuHAuk8KKZF1RwSNsgXAEhY3643E2F/0=; fh=3mmcG8TVUIjEBT0msOXYur9a6a1f1kr4PdgcqMax7lU=; b=b8DFphv3qw/D8Qq7dL7AFIApeeSLYl23WcfFuEqviq0uVdLfqRAgjfMlCOgnx+9lGO OuxQSIs8J/jF+TaATq+tRQrwqJA38V7wNtz/BLylv6oj1tXp+HwIdO5mQLEYhGkD82kk I+YheT0Fx+Xl7Fq3hcTk+e/txp7H27sFnxSgOJeX4du9rV/b+6a1/crTyv6NT2SDW+2J vT+eO83LlERh9JAdtU9yxtjxm/Py1Dh7TfRWBpebYacxcAjkMnqGeHE1sg+cmTD/bEf3 Aabz8FSTWW44WQ68dZZ6w7dJkzRF2b/ptyvOEThdBpFc3RbzafZZh43pwVuoeHmOEIHS wJoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=BRo0KRb5; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cj22-20020a056a00299600b006634db9e11dsi4891021pfb.313.2023.06.28.15.44.48; Wed, 28 Jun 2023 15:45:00 -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=@google.com header.s=20221208 header.b=BRo0KRb5; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231758AbjF1V4Y (ORCPT + 99 others); Wed, 28 Jun 2023 17:56:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbjF1Vzz (ORCPT ); Wed, 28 Jun 2023 17:55:55 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 119342D7B for ; Wed, 28 Jun 2023 14:55:33 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c361777c7f7so44503276.0 for ; Wed, 28 Jun 2023 14:55:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687989332; x=1690581332; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=u1lKcqn9ySILuHAuk8KKZF1RwSNsgXAEhY3643E2F/0=; b=BRo0KRb5NEu7qjfuG+yJrx9L3lM9o2xl8qUFEVLG5EUlUs/qioeXBSoR5z6LDsE9ZY gPZeFxMbvifz00kbpJFWCMCfrf0Q2RJeMmsar6q3K0K3QpreHsH1twT4OjsNeoa/V6nb fdG6vrSspxz8fm6cCBqkCku4lf8VOMgCbv92Y/5ASoAwzLCQqBmFBMhWAa051ABRI1Dj PeA6D9aKqlwVebLuTluwqPwHVv9Zv6YZ1CiieJ+93pViWelJi2ZW7YGRgzc+l0o12+iC AmAPZP1GopOS9vB10hpjCpvo1ILZwLdWjf8G1dczq05oasD5dK449wgeNvZRdK8aioVK KbuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687989332; x=1690581332; 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=u1lKcqn9ySILuHAuk8KKZF1RwSNsgXAEhY3643E2F/0=; b=eaNFzwgZhd0EqLC3BVgrui9qPN19VJeHROouvB3M2Nhbu9q/Y/KLeDi59ogILJNffV zyOnOM2qfTztJ8HC6jNBcg59bqDsDhcqtVA+yFBboRnLRg3THvXx7QCx+sCH6AqshCTe QYHtEKaUOEMPbIcRkghNy4fzC4oaC/RAsZmF+EtNqn8vJfFTVkpJAq9dNV3qAZD/C5PR ZLQMvih3kVxPSPQ2yZ+0QrrlHuGUDxADQ+jZkS7pA0z/P3In7oM90kLpzdfV4n84YZb6 cggDcyo+LblA5prH9dfmeCqnmEb9QOsJ2G8UQov3l4d/c9utjqp/uUm7FEtIfYI27k04 YEwg== X-Gm-Message-State: ABy/qLYbFdVr+fmLQkwEKTPv9VcHmP5rkByU2MpwsC3jI3ImvHEa5JMZ F6rcP/j9kFwzp/4p7kSH+ccP1ro+VLQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1105:b0:c17:b9e7:4c2c with SMTP id o5-20020a056902110500b00c17b9e74c2cmr40499ybu.6.1687989332365; Wed, 28 Jun 2023 14:55:32 -0700 (PDT) Date: Wed, 28 Jun 2023 14:55:30 -0700 In-Reply-To: <20230608113420.14695-3-cloudliang@tencent.com> Mime-Version: 1.0 References: <20230608113420.14695-1-cloudliang@tencent.com> <20230608113420.14695-3-cloudliang@tencent.com> Message-ID: Subject: Re: [PATCH 2/2] KVM: selftests: Add PEBS test for MSR_IA32_PERF_CAPABILITIES From: Sean Christopherson To: Jinrong Liang Cc: Paolo Bonzini , Wanpeng Li , Like Xu , Jinrong Liang , linux-kselftest@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 08, 2023, Jinrong Liang wrote: > From: Jinrong Liang > > This commit adds a PEBS test that verifies all possible combinations > of PEBS-related bits in MSR_IA32_PERF_CAPABILITIES. This comprehensive > test ensures the accuracy of the PEBS feature. > > Signed-off-by: Jinrong Liang > --- > .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 71 +++++++++++++++++++ > 1 file changed, 71 insertions(+) > > diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c > index 02903084598f..c1b1ba44bc26 100644 > --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c > +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c > @@ -21,6 +21,12 @@ > > #define MAX_LINEAR_ADDR_MASK GENMASK_ULL(15, 8) > #define ADDR_OFS_BIT 8 > +#define PMU_CAP_LBR_FMT 0x3f > +#define PMU_CAP_SMM_FREEZE BIT_ULL(12) > +#define PMU_CAP_FW_WRITES BIT_ULL(13) > +#define PMU_CAP_PERF_METRICS_AVAILABLE BIT_ULL(PERF_CAP_METRICS_IDX) > +#define PMU_CAP_PEBS_OUTPUT_PT_AVAIL BIT_ULL(PERF_CAP_PT_IDX) > +#define PMU_CAP_PEBS_ALL (PERF_CAP_PEBS_MASK | PMU_CAP_PEBS_OUTPUT_PT_AVAIL) > > union perf_capabilities { > struct { > @@ -331,6 +337,70 @@ static void test_ds_area_noncanonical_address(union perf_capabilities host_cap) > kvm_vm_free(vm); > } > > +static void test_pebs_bit_combinations(union perf_capabilities host_cap) > +{ > + int ret; Reverse xmas tree. > + uint64_t pebs_val, val; > + struct kvm_vcpu *vcpu; > + struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL); It's kinda silly, but I think it makes sense to wait until after all of the TEST_REQUIRE()s to create the VM+vCPU. > + > + TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 1); > + TEST_REQUIRE(host_cap.capabilities & PERF_CAP_PEBS_FORMAT); > + TEST_REQUIRE(vcpu_get_msr(vcpu, MSR_IA32_MISC_ENABLE) & > + MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL); > + > + /* > + * Test if PEBS_REC_FMT is set and the value is the same as host, > + * the other PEBS bits are allowed to be set only if they are the > + * same as host. > + */ > + pebs_val = host_cap.capabilities & PMU_CAP_PEBS_ALL; > + > + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, pebs_val); > + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), > + (u64)pebs_val); This cast shouldn't be necessary. And if you're going to split lines... ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities & PMU_CAP_PEBS_ALL); Though isn't that flawed? E.g. will fail if MSR_IA32_PERF_CAPABILITIES has non-PEBS bits set. I think what you want is something like: guest_perf_caps = vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES); ASSERT_EQ(guest_perf_caps & PMU_CAP_PEBS_ALL, host_cap.capabilities & PMU_CAP_PEBS_ALL); > + > + /* Test all PEBS bit combinations. */ > + for (val = 0x0; val <= (~0ul & PMU_CAP_PEBS_ALL); val++) { > + /* Skips values that are not related to PEBS. */ > + if (val & (PMU_CAP_LBR_FMT | PMU_CAP_SMM_FREEZE | > + PMU_CAP_FW_WRITES | PMU_CAP_PERF_METRICS_AVAILABLE)) Align things by their scope, i.e. if (val & (PMU_CAP_LBR_FMT | PMU_CAP_SMM_FREEZE PMU_CAP_FW_WRITES | PMU_CAP_PERF_METRICS_AVAILABLE)) But even better would be to look for !PEBS, not some other values where it's not clear they exhaustively cover all !PEBS value. E.g. can't this be? if (val & ~PMU_CAP_PEBS_ALL) continue; > + continue; > + > + /* > + * Test that value of PEBS is rejected when the KVM doesn't Just "KVM", not "the KVM". > + * supports Intel PT. > + */ > + if ((val & PMU_CAP_PEBS_OUTPUT_PT_AVAIL) && > + (!(host_cap.capabilities & PMU_CAP_PEBS_OUTPUT_PT_AVAIL))) { > + ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); > + TEST_ASSERT(!ret, "Bad PEBS auxiliary bits = 0x%lx didn't fail", val); > + > + continue; > + } > + > + /* > + * Test that value of PEBS is rejected when carrying I don't quite follow what you mean by "carrying". Do you mean a non-zero value? > + * PEBS_REC_FMT if the value of PEBS is not equal to host. > + */ > + if ((val & PERF_CAP_PEBS_FORMAT) && val != pebs_val) { > + ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); > + TEST_ASSERT(!ret, "Bad PEBS auxiliary bits = 0x%lx didn't fail", val); > + > + continue; > + } > + > + /* > + * Test that PEBS bits can be written simultaneously or > + * independently if PEBS_REC_FMT is not carried. > + */ > + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val); > + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), val); > + } > + > + kvm_vm_free(vm); > +}