Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp819454rdf; Fri, 3 Nov 2023 17:04:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IECzB+OHu6T+uZzBL6Cu0sdC5Gx4F8OSSrLrxCbxtHoObsbwwN91UcSEai69K6iMPmA3W1b X-Received: by 2002:a05:6e02:1d07:b0:359:6e44:46ec with SMTP id i7-20020a056e021d0700b003596e4446ecmr1886967ila.18.1699056279879; Fri, 03 Nov 2023 17:04:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699056279; cv=none; d=google.com; s=arc-20160816; b=WVCpr+IF+C7om4ujtA7cwLzOMnFzHqszjieF/m0+aaJSUoAy+mc2EHDVuSkEtbOtXR oxwN+Rwm35kW4YbClrVHcIV2zKOys/CgBSoDkb0GnfR5JRMHa4XfqPJme/Q2hTFWueoe 4oKmqbIE151wT8L7+rPbQbbs9ciip+7osOYa3Wd2IF79jufPxfVyyt+hDR1zXzVtN0Ta av9Yp8Lcc0YiJe4WmmuMdO0lxCRF1AqZopFPx9mw4zud0x10bnJ8Qlbgd/vt410x34pW 6hKTa11hNhOZTyT4kEuhwmh7x9pjFPVyNUY9PdSh8Lv5/5c5dVPSso/R1FLROotEQWtR lBVQ== 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=yilGYXp68Gbkah/X+aiEqo69eD6DcRs1k7jq3pnaJG0=; fh=z/UQYx9FUD9Bz9JyYrmi2ijAScp29gktKr8lejSUuXU=; b=tiNGv2Zm4SxyeIRDcJNpWRLegRl1YB1/dSviez3hUvHFL8sVYiN+kGBcgVZOrGn2bM XwKIRx17OC6OOBPVAhezg88Z9QEBNKBqnyHpBwtME5Vu1D91miaaSEr6/8oxhF1QVaWL kYlZGF9P05KftlShuDzfKkilzo7QFiuZj77WSErGv+C/Eet+0HTN14DMzGOXkprZL0r+ +9qn5c8Q3PP9Se/nEfBGhudHju/ily0VrPA2tgKc9LxXjz67TLn4EmzLcIB1FnCEfH5x LJqoWwU4rfISC9aOKMXCG9E8LfJYEiNkbbCJJh44mELrTVdhfphLP+rd0ucOvugJYb+u 6N4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PrhYXGRQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id cl20-20020a056a02099400b005b975375d70si2813225pgb.331.2023.11.03.17.04.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 17:04:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PrhYXGRQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (Postfix) with ESMTP id A6E658208F21; Fri, 3 Nov 2023 17:04:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232662AbjKDAER (ORCPT + 99 others); Fri, 3 Nov 2023 20:04:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231765AbjKDADl (ORCPT ); Fri, 3 Nov 2023 20:03:41 -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 074A91736 for ; Fri, 3 Nov 2023 17:03:19 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da39d244087so3392665276.3 for ; Fri, 03 Nov 2023 17:03:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699056199; x=1699660999; 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=yilGYXp68Gbkah/X+aiEqo69eD6DcRs1k7jq3pnaJG0=; b=PrhYXGRQXDy2Ufld/nmOej2eX0/rRRKB0FLcxie6dNxN4DNLBqb756aOgohALGvpgn NDo95V+7d7ZtI9iT5SWw32bMVk22ylN1S3QKup6zovh8lXgBAvuP8NZtAfKi0gXJNjrt u//eorY5smk8dIHKZU3wzVTVcnqh1k1ZJlbequh/iFH40LW1rC4nGUeaKyrSKbyHKQXH Jh6JfDF/oIHs9kaTMz3TC+7MN+cyKlebh6AYsmUsLvtRgkS9+7qmotRNJR8ysk7zCDZd Ja8TjC6YEeyoI3avxBoPNLk7PAcGmaLJ8DOzxqt9Oue9KcbJ0Mmf2aiQ6uYe/EPk4XU9 DRIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699056199; x=1699660999; 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=yilGYXp68Gbkah/X+aiEqo69eD6DcRs1k7jq3pnaJG0=; b=nyZnLvhnPz6X0R7QtiPd2pmVwtAHm+1QzWvN9x4DP0cpwq8kfLceL8E0xBse/bfBcK b9huJJGr9FPjFiaV8Ti8qeY2BfSezhk4E011mMj3+uXlLmN6OxilQAY9MDykT3WQR0Ko kfC7OIuGmfKewqV2U39R6yc5F5mskloroVVChAmIoigBa6rPYzGoW9GUiXwQQdv0QjY1 cG3Fe3Rtfj9TPYr7THrSBpWAkH3kGQ5xTmgSdKMpIADybX6mrQNV3hI7bjaa3JisucTT mICOX24a9eERVNSw2U/anJc2nWLcc8cKxYyX0cYr9rSTtYcQRblbTPIyTLZ2ShDCXYLU jxaw== X-Gm-Message-State: AOJu0Yz/NjVxq2PRyHLZRNv5Hq+Y0sOE+M2zAZqr+9FpWttHlNzd5/nN dnU4uYSjnJ/+Pn+s6uPguqD86l/+ETw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:565:b0:da0:c979:fd70 with SMTP id a5-20020a056902056500b00da0c979fd70mr474880ybt.9.1699056199021; Fri, 03 Nov 2023 17:03:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 3 Nov 2023 17:02:38 -0700 In-Reply-To: <20231104000239.367005-1-seanjc@google.com> Mime-Version: 1.0 References: <20231104000239.367005-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231104000239.367005-21-seanjc@google.com> Subject: [PATCH v6 20/20] KVM: selftests: Test PMC virtualization with forced emulation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jinrong Liang , Like Xu , Jim Mattson , Aaron Lewis 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,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 fry.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 (fry.vger.email [0.0.0.0]); Fri, 03 Nov 2023 17:04:34 -0700 (PDT) Extend the PMC counters test to use forced emulation to verify that KVM emulates counter events for instructions retired and branches retired. Force emulation for only a subset of the measured code to test that KVM does the right thing when mixing perf events with emulated events. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/pmu_counters_test.c | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index e6cf76d3499b..c66cf92cc9cc 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -21,6 +21,7 @@ static uint8_t kvm_pmu_version; static bool kvm_has_perf_caps; +static bool is_forced_emulation_enabled; static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code, @@ -34,6 +35,7 @@ static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, vcpu_init_descriptor_tables(*vcpu); sync_global_to_guest(vm, kvm_pmu_version); + sync_global_to_guest(vm, is_forced_emulation_enabled); /* * Set PERF_CAPABILITIES before PMU version as KVM disallows enabling @@ -138,37 +140,50 @@ static void guest_assert_event_count(uint8_t idx, * If CLFUSH{,OPT} is supported, flush the cacheline containing (at least) the * start of the loop to force LLC references and misses, i.e. to allow testing * that those events actually count. + * + * If forced emulation is enabled (and specified), force emulation on a subset + * of the measured code to verify that KVM correctly emulates instructions and + * branches retired events in conjunction with hardware also counting said + * events. */ -#define GUEST_MEASURE_EVENT(_msr, _value, clflush) \ +#define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP) \ do { \ __asm__ __volatile__("wrmsr\n\t" \ clflush "\n\t" \ "mfence\n\t" \ "1: mov $" __stringify(NUM_BRANCHES) ", %%ecx\n\t" \ - "loop .\n\t" \ - "mov %%edi, %%ecx\n\t" \ - "xor %%eax, %%eax\n\t" \ - "xor %%edx, %%edx\n\t" \ + FEP "loop .\n\t" \ + FEP "mov %%edi, %%ecx\n\t" \ + FEP "xor %%eax, %%eax\n\t" \ + FEP "xor %%edx, %%edx\n\t" \ "wrmsr\n\t" \ :: "a"((uint32_t)_value), "d"(_value >> 32), \ "c"(_msr), "D"(_msr) \ ); \ } while (0) +#define GUEST_TEST_EVENT(_idx, _event, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ +do { \ + wrmsr(pmc_msr, 0); \ + \ + if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt 1f", FEP); \ + else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush 1f", FEP); \ + else \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \ + \ + guest_assert_event_count(_idx, _event, _pmc, _pmc_msr); \ +} while (0) + static void __guest_test_arch_event(uint8_t idx, struct kvm_x86_pmu_feature event, uint32_t pmc, uint32_t pmc_msr, uint32_t ctrl_msr, uint64_t ctrl_msr_value) { - wrmsr(pmc_msr, 0); + GUEST_TEST_EVENT(idx, event, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, ""); - if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) - GUEST_MEASURE_EVENT(ctrl_msr, ctrl_msr_value, "clflushopt 1f"); - else if (this_cpu_has(X86_FEATURE_CLFLUSH)) - GUEST_MEASURE_EVENT(ctrl_msr, ctrl_msr_value, "clflush 1f"); - else - GUEST_MEASURE_EVENT(ctrl_msr, ctrl_msr_value, "nop"); - - guest_assert_event_count(idx, event, pmc, pmc_msr); + if (is_forced_emulation_enabled) + GUEST_TEST_EVENT(idx, event, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, KVM_FEP); } #define X86_PMU_FEATURE_NULL \ @@ -544,6 +559,7 @@ int main(int argc, char *argv[]) kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM); + is_forced_emulation_enabled = kvm_is_forced_emulation_enabled(); test_intel_counters(); -- 2.42.0.869.gea05f2083d-goog