From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEBF0EB64D7 for ; Wed, 28 Jun 2023 21:02:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231919AbjF1VCI (ORCPT ); Wed, 28 Jun 2023 17:02:08 -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-Google-Smtp-Source: ACHHUZ4yhY/CGZwUbA5ja/LiKlFUteS+fedo4a1ySqmTMe6ZqhcSkqg6JlDBiIW5EjkYfmEnMdCWBPXaniU= 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" 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.