Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp7585897rwp; Tue, 18 Jul 2023 18:43:32 -0700 (PDT) X-Google-Smtp-Source: APBJJlGlZt/23jyTs9zyr6vXEq/UZqSbasmFsEY4xgkYljPKfQxxWy7wJMOt9sCMAIsPUm/N3JV9 X-Received: by 2002:a05:6358:7207:b0:134:e603:116e with SMTP id h7-20020a056358720700b00134e603116emr1555833rwa.6.1689731012205; Tue, 18 Jul 2023 18:43:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689731012; cv=none; d=google.com; s=arc-20160816; b=S7aIptbQ7BPIqKzn9K1jiaQUEp92dJV/c8ClchMH8LLP4P3WL5O7/ar7l9WQXAVtiV 0yto7SR2ga6jxe+3ISBWxGH+WZfGDwhi5bqFn7wfVOr7uuQOtn63ElZfSobYTD5jhVgd hq5Daa5bJwZFxpFMrGAs/i/Iof18AL5FsD8d+cPcv3gX17MJSCies4VrEJON4I1blC/t VTRQjHOmv9i/3qrbEpUMvJLRWMGX33wWXkWFG00up4VUXuJOpG9r7IuTLZc+IkFqHa3M JH8PowntO1nOAPWh9i6F5YbEs8bbd1p7q/TAVifGVknOoesWjftPae6Wm55zWpUdzK5c 3R6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=r98SwIuCR5wk/UZFxeuXqHCnCC/0/cUGmeyzvrJ3y8c=; fh=UqO/PQpNCau320ueX0+iYlM7UKorOr3THVbNOLSoXhE=; b=tzNCEyGnaMOkd6B5YmT2gEBscZjIuBRdPR4XmLlMoJxKq9LuvsmhbvpHImGrsFutbn r5cGuBP29cJXWgTM5RUFe9N82ALm03SsXvjp8LCBuQUcH11UaRc/SyKRDVPdS6JMhOOr vBoFlyNhU6Uk5Npji3N5NdVqw6CxMzcANXxHl9NksmHV+xI8dTaIFZ5c8seFai3QELPE 5jWrWeBD10dcYgSEaAPGDLEf6fdMOUDL2QJNBV1SgL8p+Usd970iU6tG+qfZJL/ykHWS xpliYEXjxMtxlpE/DCHxklDiu7PiaSri9xQCqnpu5Tasx9JdvT06UKgXmmnIHGq7eI6x 5JsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=onilTHiC; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h24-20020a635318000000b005573ca1a657si2408786pgb.118.2023.07.18.18.43.19; Tue, 18 Jul 2023 18:43:32 -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=@gmail.com header.s=20221208 header.b=onilTHiC; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229695AbjGSBVS (ORCPT + 99 others); Tue, 18 Jul 2023 21:21:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjGSBVR (ORCPT ); Tue, 18 Jul 2023 21:21:17 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDC4D12F; Tue, 18 Jul 2023 18:21:15 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6862842a028so912597b3a.0; Tue, 18 Jul 2023 18:21:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689729675; x=1690334475; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=r98SwIuCR5wk/UZFxeuXqHCnCC/0/cUGmeyzvrJ3y8c=; b=onilTHiCvorh1QZPOIKwt3VUjJh9H3ifKDcHKb14k8+2ePGO62RRT/N7D992SR599Q g4ZukZanowJVtgp8NdBrr4PNb1EFg3XLAVWq+uL5+3IXXVOcHG7al5HpTzH8VIto5tZy BV/IF495RZ7RbrjbMWjmQGNuwZD67jefo1bkvhkfVxwnFw1gAtMHpiPo1uug6Zx0xzoV DoHbi+uB+iyVXwbjXxQKu55GaT3j1zznZDKIY8iOiG0eWzbgxAfpxl7Iuot3QX23cJjA yithyi7UJy1kQDLmMetgsqGYKqlTBpIApGn+uFwjD8JyTyt/RPTqopn8SMFE9KwezUXd BZpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689729675; x=1690334475; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=r98SwIuCR5wk/UZFxeuXqHCnCC/0/cUGmeyzvrJ3y8c=; b=SOBNrMqjFzzWooTI3o55cIXN0rZ7vFhzDP7Pwd3XSVlhj+StyvU2ARUexTqECg398s 0lOaHuMPjUoZPPbSHXArlDGD7rR1Q097mwqqbUskdvvBMeZ0WB06r1GbaCwjZE4YOB2d Y+iq7Xh6Z13IpD4R2TaIwUZIusOjAK8MThX2PyHl5i7ktKTSeBzcCOwztqzDT5WCRfw2 1iMYeskrcDno244TDwOEwZAngxN2mqN2JnzUQ32TWtKS6+cjTwTKqGWRn3pIGXtGvDPM eu/T7PXOKW+ERnnxpw5kt0UP9gnuyByDqe3sijIS+daRZ2cDfEOX7bWiFAU7U9efJOiz 0g6Q== X-Gm-Message-State: ABy/qLY+uSt7wEcGSpnm0Q2nxsyed+SUA1zVx6Cn+IFpe5jpRO+nszAj Em2UPPFi9qykJVf92KPqW/E= X-Received: by 2002:a05:6a00:1487:b0:67a:c810:3cb0 with SMTP id v7-20020a056a00148700b0067ac8103cb0mr1451882pfu.3.1689729675004; Tue, 18 Jul 2023 18:21:15 -0700 (PDT) Received: from localhost ([192.55.54.50]) by smtp.gmail.com with ESMTPSA id i5-20020aa787c5000000b0067aea93af40sm2109331pfo.2.2023.07.18.18.21.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 18:21:14 -0700 (PDT) Date: Tue, 18 Jul 2023 18:21:13 -0700 From: Isaku Yamahata To: Jinrong Liang Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , Shuah Khan , Aaron Lewis , David Matlack , Vishal Annapurve , Wanpeng Li , Like Xu , Jinrong Liang , linux-kselftest@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com Subject: Re: [PATCH v4 5/6] KVM: selftests: Test if event filter meets expectations on fixed counters Message-ID: <20230719012113.GE25699@ls.amr.corp.intel.com> References: <20230717062343.3743-1-cloudliang@tencent.com> <20230717062343.3743-6-cloudliang@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230717062343.3743-6-cloudliang@tencent.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Mon, Jul 17, 2023 at 02:23:42PM +0800, Jinrong Liang wrote: > From: Jinrong Liang > > Add tests to cover that pmu event_filter works as expected when > it's applied to fixed performance counters, even if there is none > fixed counter exists (e.g. Intel guest pmu version=1 or AMD guest). > > Signed-off-by: Jinrong Liang > --- > .../kvm/x86_64/pmu_event_filter_test.c | 80 +++++++++++++++++++ > 1 file changed, 80 insertions(+) > > diff --git a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c > index 63f85f583ef8..1872b848f734 100644 > --- a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c > +++ b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c > @@ -27,6 +27,7 @@ > #define ARCH_PERFMON_BRANCHES_RETIRED 5 > > #define NUM_BRANCHES 42 > +#define INTEL_PMC_IDX_FIXED 32 > > /* Matches KVM_PMU_EVENT_FILTER_MAX_EVENTS in pmu.c */ > #define MAX_FILTER_EVENTS 300 > @@ -805,6 +806,84 @@ static void test_filter_ioctl(struct kvm_vcpu *vcpu) > TEST_ASSERT(!r, "Masking non-existent fixed counters should be allowed"); > } > > +static void intel_run_fixed_counter_guest_code(uint8_t fixed_ctr_idx) > +{ > + for (;;) { > + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0); > + wrmsr(MSR_CORE_PERF_FIXED_CTR0 + fixed_ctr_idx, 0); > + > + /* Only OS_EN bit is enabled for fixed counter[idx]. */ > + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, BIT_ULL(4 * fixed_ctr_idx)); > + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, > + BIT_ULL(INTEL_PMC_IDX_FIXED + fixed_ctr_idx)); > + __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES})); > + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0); > + > + GUEST_SYNC(rdmsr(MSR_CORE_PERF_FIXED_CTR0 + fixed_ctr_idx)); > + } > +} > + > +static uint64_t test_with_fixed_counter_filter(struct kvm_vcpu *vcpu, > + uint32_t action, uint32_t bitmap) > +{ > + struct __kvm_pmu_event_filter f = { > + .action = action, > + .fixed_counter_bitmap = bitmap, > + }; > + do_vcpu_set_pmu_event_filter(vcpu, &f); > + > + return run_vcpu_to_sync(vcpu); > +} > + > +static void __test_fixed_counter_bitmap(struct kvm_vcpu *vcpu, uint8_t idx, > + uint8_t nr_fixed_counters) > +{ > + unsigned int i; > + uint32_t bitmap; > + uint64_t count; > + > + TEST_ASSERT(nr_fixed_counters < sizeof(bitmap), sizeof(bitmap) * 8? > + "Invalid nr_fixed_counters"); > + > + /* > + * Check the fixed performance counter can count normally when KVM > + * userspace doesn't set any pmu filter. > + */ > + count = run_vcpu_to_sync(vcpu); > + TEST_ASSERT(count, "Unexpected count value: %ld\n", count); > + > + for (i = 0; i < BIT(nr_fixed_counters); i++) { > + bitmap = BIT(i); > + count = test_with_fixed_counter_filter(vcpu, KVM_PMU_EVENT_ALLOW, > + bitmap); > + ASSERT_EQ(!!count, !!(bitmap & BIT(idx))); > + > + count = test_with_fixed_counter_filter(vcpu, KVM_PMU_EVENT_DENY, > + bitmap); > + ASSERT_EQ(!!count, !(bitmap & BIT(idx))); > + } > +} > + > +static void test_fixed_counter_bitmap(void) > +{ > + uint8_t nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); > + struct kvm_vm *vm; > + struct kvm_vcpu *vcpu; > + uint8_t idx; > + > + /* > + * Check that pmu_event_filter works as expected when it's applied to > + * fixed performance counters. > + */ > + for (idx = 0; idx < nr_fixed_counters; idx++) { > + vm = vm_create_with_one_vcpu(&vcpu, > + intel_run_fixed_counter_guest_code); > + vcpu_args_set(vcpu, 1, idx); > + __test_fixed_counter_bitmap(vcpu, idx, nr_fixed_counters); > + kvm_vm_free(vm); > + } > +} > + > int main(int argc, char *argv[]) > { > void (*guest_code)(void); > @@ -848,6 +927,7 @@ int main(int argc, char *argv[]) > kvm_vm_free(vm); > > test_pmu_config_disable(guest_code); > + test_fixed_counter_bitmap(); > > return 0; > } > -- > 2.39.3 > -- Isaku Yamahata