Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19773129rwd; Wed, 28 Jun 2023 14:10:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6r3vdUxXEsMvxjr5RGAQZvuExxpSJT8Nt2rpsMTTMgFRGII50jJuoCOpTLDpPtva2UD3cK X-Received: by 2002:a05:6402:292:b0:51d:d1a6:c512 with SMTP id l18-20020a056402029200b0051dd1a6c512mr1654746edv.6.1687986628100; Wed, 28 Jun 2023 14:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687986628; cv=none; d=google.com; s=arc-20160816; b=Vcnogyxn3DomqMZk6xbMLw3ih/KurrDaaV1CuNkfCLPxepcnLzjVVZe9Kpq96Wb6o5 Useuh3WrTX/pZMrEEGI+hoYHHbCoSjQGx50kLxZDqzO5w6nt4Dgip9O4SHkZYYBnWGbj nrUa15s9jTAX/qkqoTAwupWqWavb5TOI0APEUk2cx6DUZdibThoC0unxbcUMneiQoAmG JEN3Y7QQo4MNvoB18XDEk7257KqOtrYCpLZL9JYDccbWhQBMPp/qNzYKW5r5rn3EGeL2 JLRlDD6e6NMhojeEVusZNxxroLPmBZUg9fv8Lb0Swe7mcAyqUs6JRp4xsAltE+y5KCs5 /HSQ== 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=zAdU4qXPSUkOZjySjJgyPMDSkwNgAW9dwq8obwhFF/E=; fh=NZar1lo+0Rhg/9fJqaygQT5vsrjdPJfNAdYFy36OmiM=; b=EsNm0SiH7qxwgphxY+RvLa4G3zy3B0QX2nlyWvncyh19Z1uiwROUz1mwosSISetdlh /ViSWy1ot/WIy+dPveKxVdUVVylauf1DQQyHDQUjthYjYTTR8yGGQk9wWobcG43IQc0i 5mZWUf4f/p1xrX3c7b6yVjQz3/7LVQUlnzugo41siFqynyD6s184HCmRir+/49YwHJ5R HcVFt+BsBf1tcfNxm3tK6d/PLvrI76BJg32+LJT3OCv7KCXE4/paW5+8xSUx9cO+nU1k QQQq50563gvM3qmUW4MyMEgpGsTlQrMHyONWkVEt8XLSyJzi2ZOk97HmddAhGAZDKU5D Q3KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=pyVELHnW; 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 c19-20020aa7c753000000b0051dd3314051si771676eds.188.2023.06.28.14.10.03; Wed, 28 Jun 2023 14:10:28 -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=pyVELHnW; 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 S231901AbjF1VCE (ORCPT + 99 others); Wed, 28 Jun 2023 17:02:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231324AbjF1VBy (ORCPT ); Wed, 28 Jun 2023 17:01:54 -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 AF8E0199 for ; Wed, 28 Jun 2023 14:01:53 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c337c5c56ecso132127276.3 for ; Wed, 28 Jun 2023 14:01:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687986113; x=1690578113; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zAdU4qXPSUkOZjySjJgyPMDSkwNgAW9dwq8obwhFF/E=; b=pyVELHnWd+/RblILdNwfILju2YBbjtM18OHPwCsDW0+ne4lrWD/ThAjecXrI/QuyNJ p4t2BoQUD/sPFmA3DRGODaHLu/ufFEuc11P+duNwBtpjAOedje2yqH9pmSRXFXlVSYQu HhirY+9XukPAGtwiNN37dBui3xsmpezX183zo1m1L2ZTG+eGXenzj3WQEMla5RfaJ4Dc 6ClcEBPtz2yFDdjD6RDqwMlYI8DhoS60xkQL/kxAvJNFvWWUu56C5sj8A/2qvlhpFWuM FSvd5tCGaKz54EiUehYk2BF9mfgNfpiejvH/lf1Ud+yVNtV2TlPGFv1GcB/RB88uN+f+ /lPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687986113; x=1690578113; 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=zAdU4qXPSUkOZjySjJgyPMDSkwNgAW9dwq8obwhFF/E=; b=ir7JkhiYCM9R7TeCRKbWiftsUWWxcDB0+HzBITBcgxfMn8y+PkdP0xoif6v1yHNX2t NIBVqZJt9yMoXiWw9EhCYlWFwfU0DpPxS9tzeBs32FfmtP7tzs7VDx9ZClcW664fjXd0 pAAV4E2r3Vmf7DgkxgvoFxYnNYW8lcfdE/DAoylz3MilBa+NbGyjTNLUK9WbD9qKEuKL ReomLYb5DzONtL3BeWPvnexnEn3B5J1C5D0IrYRowsabh56SNMUQYtdoBw791hb+76ep rpHC9pRWVpHbxq92XvwXGckLD/PEF+m9czpYdokYiI19h1wXRL7JnP8HyytKqbZytedX Lx0g== X-Gm-Message-State: AC+VfDwl7rWIsAPix2KYX4eJQdiZg00R1pZqc3chXmyBBVLGMmcziZx9 YFwk7O7raIlQKDpUUm+cUq9yuRNfANs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:4f1:b0:c15:cbd1:60d6 with SMTP id w17-20020a05690204f100b00c15cbd160d6mr3625214ybs.5.1687986112965; Wed, 28 Jun 2023 14:01:52 -0700 (PDT) Date: Wed, 28 Jun 2023 14:01:51 -0700 In-Reply-To: <20230530134248.23998-7-cloudliang@tencent.com> Mime-Version: 1.0 References: <20230530134248.23998-1-cloudliang@tencent.com> <20230530134248.23998-7-cloudliang@tencent.com> Message-ID: Subject: Re: [PATCH v2 6/8] KVM: selftests: Test consistency of CPUID with num of fixed counters From: Sean Christopherson To: Jinrong Liang Cc: Paolo Bonzini , Like Xu , David Matlack , Aaron Lewis , Vitaly Kuznetsov , Wanpeng Li , Jinrong Liang , 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=ham 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 Tue, May 30, 2023, Jinrong Liang wrote: > From: Jinrong Liang > > Add test to check if non-existent counters can be accessed in guest after > determining the number of Intel generic performance counters by CPUID. > Per SDM, fixed-function performance counter 'i' is supported if ECX[i] || > (EDX[4:0] > i). KVM doesn't emulate more counters than it can support. > > Co-developed-by: Like Xu > Signed-off-by: Like Xu > Signed-off-by: Jinrong Liang > --- > .../kvm/x86_64/pmu_basic_functionality_test.c | 42 +++++++++++++++++++ > 1 file changed, 42 insertions(+) > > diff --git a/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c b/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c > index 116437ac2095..e19f8c2774c5 100644 > --- a/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c > +++ b/tools/testing/selftests/kvm/x86_64/pmu_basic_functionality_test.c > @@ -228,10 +228,46 @@ static void test_oob_gp_counter(uint8_t eax_gp_num, uint8_t offset, > kvm_vm_free(vm); > } > > +static void intel_test_oob_fixed_ctr(uint8_t edx_fix_num, > + uint32_t fixed_bitmask, uint64_t expected) > +{ > + struct kvm_vm *vm; > + struct kvm_vcpu *vcpu; > + struct kvm_cpuid_entry2 *entry; > + uint8_t idx = edx_fix_num; > + bool visible; > + uint64_t msr_val; > + > + vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_wr_and_rd_msrs); > + > + entry = vcpu_get_cpuid_entry(vcpu, 0xa); > + entry->ecx = fixed_bitmask; > + entry->edx = (entry->edx & ~FIXED_CTR_NUM_MASK) | edx_fix_num; > + vcpu_set_cpuid(vcpu); > + > + /* Per Intel SDM, FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i). */ > + visible = (entry->ecx & BIT_ULL(idx) || > + ((entry->edx & FIXED_CTR_NUM_MASK) > idx)); Add a helper (in pmu.h) for this one too. > + /* KVM doesn't emulate more fixed counters than it can support. */ > + if (idx >= X86_INTEL_MAX_FIXED_CTR_NUM) > + visible = false; > + > + vcpu_args_set(vcpu, 4, MSR_CORE_PERF_FIXED_CTR0, 0xffff, idx, 1); > + if (!visible) Curly braces need around a multi-line statement. > + while (run_vcpu(vcpu, &msr_val) != UCALL_DONE) > + TEST_ASSERT(msr_val == expected, > + "Unexpected when testing fixed counter num."); ASSERT_EQ() will print the expected versus actually for you.