Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp875748lqc; Fri, 8 Mar 2024 14:39:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCULHCLjSJe4FF7EaJsVIpUJrDVXyuj58cW7TKj+WPNTOvQ2fOf5W+TK2LNjPpRHvIRan1Jxfg4/seEDXL/UFPKl/wedqDDmr2YPdEKCnA== X-Google-Smtp-Source: AGHT+IHXmEKaO1jhIwjK/Q7GYqWAz07XgZNnBwO7J9z6yn0sYH1buq48eMgLtz4KCN2t6gauzATo X-Received: by 2002:a05:6808:15aa:b0:3c2:1262:e04a with SMTP id t42-20020a05680815aa00b003c21262e04amr546342oiw.1.1709937550652; Fri, 08 Mar 2024 14:39:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709937550; cv=pass; d=google.com; s=arc-20160816; b=0fl2snAAdOxVAF5O994p6WTda9+HSPUMyXGfjFCpp+l/96+q2YcPc8EkSlhvJEPxnt ZJreQtfgksSK6vCXFMy83nF82pSlRD0cMeY1YsGt93bsNVK+TAzt/PHhlfAJomDjlp6x paUHBT9mugZqKLMhrMKQJMXjpnSvqBAtC0xiT5IdI9GuYSvlCbIdaPfK63WvQo/iEE7G GNuyX/octgChsay/bWBkYYLFQGyVY+RFa9Pjup4Aq9DZtrcWMze081lba5HhZ0cYYP2j EgyKgh7melQ0uupiDUOZ/9oXRuzlY7Wvkb5im8o+DH1by21X82wsJGe40JXGfk5RqgL7 +i8w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=Wk7WezRe+PQVWxoR7gEA577W4P7MYaIjcJoEoxf0Qyg=; fh=Qf6aa3eDUNpXX8UVNaArLaRhxGQdcr+AX4QPowcEa7g=; b=el2JBsWOjq4VlAZUpNg+BGGsRB1TZeXYiBIe0yJo/3+FocW/7qP9RhX4NRwJijrDKP lHOe9f062sxAQORCfdis879xKUdexJErLgYTcC0aWHzMyzQ/Aj035Ldzb50bf3CQj0Ab vuKEv8LLL+78HClFgKze/1TFLcVfpY5N5s5PpoBbluar0dI4G3ES5iJHP1k7ulg17R3g bY18QaXs1i8z+lX1v8rkwORr/qGDQuFXlnC9XhcWjY25DoM0b/0tnRF3flX57t/jCs6J ZsI6jlTZTpV+3vVdCMWCbDX4Q7bhZ9+6DFlQ2DQgP5X3HJCg6azW94JjjdFwSUQDQqP0 gzhQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qmM625sV; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-97662-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97662-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 18-20020a630e52000000b005dc50f61543si370539pgo.427.2024.03.08.14.39.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 14:39:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97662-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qmM625sV; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-97662-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97662-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 503FFB20E9B for ; Fri, 8 Mar 2024 22:39:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2366F4DA1D; Fri, 8 Mar 2024 22:37:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qmM625sV" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B384B4E1BC for ; Fri, 8 Mar 2024 22:37:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709937440; cv=none; b=YZ9DBXG/j9GT5txUatUoFOhxxR9EYP+ZsDCMFW6ifbJElV0WbBweYeNkB94NJG6UGHVN1ftu+zkI7KtwwFwBoC5GhpG35GDfv58xcx4gpdTdk1Z9aDuRC5fHZAKhAiQBQB4eQESBm7pOXub0JcmIEaYvGlTGkby9pCvW1OCjAMU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709937440; c=relaxed/simple; bh=Cx7f0LVK3agk27XVlL40f1u6TZkbMfoIz9OO1MplwA8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Raw52dsT/tVd0Zs8cZKhkDVZqKLw4Wjd9kjrbqV4yMAy1StI7+bKGljTUZ+esSU2xy4TNOyOE/dMmHRzzb4+eU/+bZaAApZS/LEwEWjQQLy335sx6yfvoep+sN70T4b5acKSXn7JmygBQ9YfYCNpUFjvf5jaQeFT0H8LMMy92vM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qmM625sV; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60a0890f2cdso15236277b3.1 for ; Fri, 08 Mar 2024 14:37:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709937437; x=1710542237; 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=Wk7WezRe+PQVWxoR7gEA577W4P7MYaIjcJoEoxf0Qyg=; b=qmM625sV/JqZDDgDZuEn4n+emjohCMS8mdug4+HlKH2i4wCc3tKdJDO0PRFoajjHTe lt6y1hro7zH36/RpOsXkDW23MFEsGZ2llP+EfG/0YL0+W/+371YNkijRkY1hGT0hSrD9 oOoOSU3zvgq8LHgUVF1em3x+4g6Ec+DS3N2Xko5lpIYZ+tv/PO+HIT3C3rfrUJYC2PUR OL+rUJ1bjAWVQ0P/UajvnlL8nICyfeVC8hgShNRejwwZaMguVitlgulpYv6ZnLmvxv7y OerSGxMKC7++3ZYm3IqpMmwPlvnxnNbukF1jyYbEKJVO5RIb7NPvyLAli2OXn8Hh3Lj9 ZzqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709937437; x=1710542237; 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=Wk7WezRe+PQVWxoR7gEA577W4P7MYaIjcJoEoxf0Qyg=; b=bv852bw+S0E/LjGZ5rxEfxtL59MxyfE2Zf6D/kSshBr9gSOmxDX9TFxxz0bBTuKCrm eGs/7JeB7U70Ei2+KP6zAkkI7fWRgiDa1xdAUuFvzFimyTifjU7uggDoBG7bjjnl9h/q ATPl34iHiL82ZoKgYa77Zl8qNsYLhANsfM+qTqv4XyqEB6pbrjxxCIYYU6bkFBIyJ8nc KJNmkftWkixDPAgKqVXfK/LtIPZ/F66HI8VV+2B3vgc0W+JDTAguok4HPCjdI1oiY3Te D07a+nOR36zBgDuFcx0JhAt5okA0nKH9PO1eKYOwHw74Qm/lxenKtIUQRhnSaMcFs5ud 8LQw== X-Forwarded-Encrypted: i=1; AJvYcCUR6d0YC++YNRM5L4HJA9JbPPXrLbuak9hIEV/f49QlvUIBkZc0wL7C4DQ+TzeWAEwEBkSZHr+5aAD9GhJW9tvuz9euzvcgPpy7ehH0 X-Gm-Message-State: AOJu0Ywiu9zf6jFvenFe08izufTryAqAguL28BzOelzVWdAa1BrWYbOh cNVe8e+rs7fXTjbGOaIZ8QX4WEettE2Wx3RYHBLCCAkx155HY1xij7GK79qunP1HRDLjyHoZW5z y2w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:4f43:0:b0:609:fcd2:2749 with SMTP id d64-20020a814f43000000b00609fcd22749mr150967ywb.4.1709937436831; Fri, 08 Mar 2024 14:37:16 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 14:36:58 -0800 In-Reply-To: <20240308223702.1350851-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240308223702.1350851-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240308223702.1350851-6-seanjc@google.com> Subject: [GIT PULL] KVM: x86: PMU changes for 6.9 From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Christopherson Content-Type: text/plain; charset="UTF-8" Lots of PMU fixes and cleanups, along with related selftests. The most notable fix is to *not* disallow the use of fixed counters and event encodings just because the CPU doesn't report support for the matching architectural event encoding. Note, the selftests changes have several annoying conflicts with "the" selftests pull request that you'll also receive from me. I recommend merging that one first, as I found it slightly easier to resolve the conflicts in that order. P.S. I expect to send another PMU related pull request of 3-4 fixes at some point during the merge window. But they're all small and urgent (if we had a few more weeks for 6.8, I'd have tried to squeeze them into 6.8). The following changes since commit 41bccc98fb7931d63d03f326a746ac4d429c1dd3: Linux 6.8-rc2 (2024-01-28 17:01:12 -0800) are available in the Git repository at: https://github.com/kvm-x86/linux.git tags/kvm-x86-pmu-6.9 for you to fetch changes up to 812d432373f629eb8d6cb696ea6804fca1534efa: KVM: x86/pmu: Explicitly check NMI from guest to reducee false positives (2024-02-26 15:57:22 -0800) ---------------------------------------------------------------- KVM x86 PMU changes for 6.9: - Fix several bugs where KVM speciously prevents the guest from utilizing fixed counters and architectural event encodings based on whether or not guest CPUID reports support for the _architectural_ encoding. - Fix a variety of bugs in KVM's emulation of RDPMC, e.g. for "fast" reads, priority of VMX interception vs #GP, PMC types in architectural PMUs, etc. - Add a selftest to verify KVM correctly emulates RDMPC, counter availability, and a variety of other PMC-related behaviors that depend on guest CPUID, i.e. are difficult to validate via KVM-Unit-Tests. - Zero out PMU metadata on AMD if the virtual PMU is disabled to avoid wasting cycles, e.g. when checking if a PMC event needs to be synthesized when skipping an instruction. - Optimize triggering of emulated events, e.g. for "count instructions" events when skipping an instruction, which yields a ~10% performance improvement in VM-Exit microbenchmarks when a vPMU is exposed to the guest. - Tighten the check for "PMI in guest" to reduce false positives if an NMI arrives in the host while KVM is handling an IRQ VM-Exit. ---------------------------------------------------------------- Dapeng Mi (1): KVM: selftests: Test top-down slots event in x86's pmu_counters_test Jinrong Liang (7): KVM: selftests: Add vcpu_set_cpuid_property() to set properties KVM: selftests: Add pmu.h and lib/pmu.c for common PMU assets KVM: selftests: Test Intel PMU architectural events on gp counters KVM: selftests: Test Intel PMU architectural events on fixed counters KVM: selftests: Test consistency of CPUID with num of gp counters KVM: selftests: Test consistency of CPUID with num of fixed counters KVM: selftests: Add functional test for Intel's fixed PMU counters Like Xu (1): KVM: x86/pmu: Explicitly check NMI from guest to reducee false positives Sean Christopherson (32): KVM: x86/pmu: Always treat Fixed counters as available when supported KVM: x86/pmu: Allow programming events that match unsupported arch events KVM: x86/pmu: Remove KVM's enumeration of Intel's architectural encodings KVM: x86/pmu: Setup fixed counters' eventsel during PMU initialization KVM: x86/pmu: Get eventsel for fixed counters from perf KVM: x86/pmu: Don't ignore bits 31:30 for RDPMC index on AMD KVM: x86/pmu: Prioritize VMX interception over #GP on RDPMC due to bad index KVM: x86/pmu: Apply "fast" RDPMC only to Intel PMUs KVM: x86/pmu: Disallow "fast" RDPMC for architectural Intel PMUs KVM: x86/pmu: Treat "fixed" PMU type in RDPMC as index as a value, not flag KVM: x86/pmu: Explicitly check for RDPMC of unsupported Intel PMC types KVM: selftests: Drop the "name" param from KVM_X86_PMU_FEATURE() KVM: selftests: Extend {kvm,this}_pmu_has() to support fixed counters KVM: selftests: Expand PMU counters test to verify LLC events KVM: selftests: Add a helper to query if the PMU module param is enabled KVM: selftests: Add helpers to read integer module params KVM: selftests: Query module param to detect FEP in MSR filtering test KVM: selftests: Move KVM_FEP macro into common library header KVM: selftests: Test PMC virtualization with forced emulation KVM: selftests: Add a forced emulation variation of KVM_ASM_SAFE() KVM: selftests: Add helpers for safe and safe+forced RDMSR, RDPMC, and XGETBV KVM: selftests: Extend PMU counters test to validate RDPMC after WRMSR KVM: x86/pmu: Zero out PMU metadata on AMD if PMU is disabled KVM: x86/pmu: Add common define to capture fixed counters offset KVM: x86/pmu: Move pmc_idx => pmc translation helper to common code KVM: x86/pmu: Snapshot and clear reprogramming bitmap before reprogramming KVM: x86/pmu: Add macros to iterate over all PMCs given a bitmap KVM: x86/pmu: Process only enabled PMCs when emulating events in software KVM: x86/pmu: Snapshot event selectors that KVM emulates in software KVM: x86/pmu: Expand the comment about what bits are check emulating events KVM: x86/pmu: Check eventsel first when emulating (branch) insns retired KVM: x86/pmu: Avoid CPL lookup if PMC enabline for USER and KERNEL is the same arch/x86/include/asm/kvm-x86-pmu-ops.h | 4 +- arch/x86/include/asm/kvm_host.h | 11 +- arch/x86/kvm/emulate.c | 2 +- arch/x86/kvm/kvm_emulate.h | 2 +- arch/x86/kvm/pmu.c | 163 ++++-- arch/x86/kvm/pmu.h | 57 +- arch/x86/kvm/svm/pmu.c | 22 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 222 +++----- arch/x86/kvm/x86.c | 15 +- arch/x86/kvm/x86.h | 6 - tools/testing/selftests/kvm/Makefile | 2 + .../testing/selftests/kvm/include/kvm_util_base.h | 4 + tools/testing/selftests/kvm/include/x86_64/pmu.h | 97 ++++ .../selftests/kvm/include/x86_64/processor.h | 148 +++-- tools/testing/selftests/kvm/lib/kvm_util.c | 62 ++- tools/testing/selftests/kvm/lib/x86_64/pmu.c | 31 ++ tools/testing/selftests/kvm/lib/x86_64/processor.c | 15 +- .../selftests/kvm/x86_64/pmu_counters_test.c | 620 +++++++++++++++++++++ .../selftests/kvm/x86_64/pmu_event_filter_test.c | 143 ++--- .../kvm/x86_64/smaller_maxphyaddr_emulation_test.c | 2 +- .../selftests/kvm/x86_64/userspace_msr_exit_test.c | 29 +- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 2 +- 23 files changed, 1262 insertions(+), 399 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/pmu.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/pmu.c create mode 100644 tools/testing/selftests/kvm/x86_64/pmu_counters_test.c