All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zide Chen <zide.chen@intel.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>
Cc: kvm@vger.kernel.org, Jim Mattson <jmattson@google.com>,
	Yosry Ahmed <yosry@kernel.org>, Andi Kleen <ak@linux.intel.com>,
	Stephane Eranian <eranian@google.com>,
	linux-kernel@vger.kernel.org, Mingwei Zhang <mizhang@google.com>,
	Zide Chen <zide.chen@intel.com>,
	Das Sandipan <Sandipan.Das@amd.com>,
	Shukla Manali <Manali.Shukla@amd.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Xudong Hao <xudong.hao@intel.com>
Subject: [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support
Date: Tue, 24 Nov 2026 14:38:43 -0700	[thread overview]
Message-ID: <20261124213901.120572-1-zide.chen@intel.com> (raw)

Activation of the PERF_METRICS feature in hardware requires both
PERF_METRICS_AVAILABLE and the existence of fixed counter 3. However,
the presence of the IA32_PERF_METRICS MSR is architecturally decoupled
from it.

This version enforces the following dependencies between PERF_METRICS
and fixed counter 3 to comply with the SDM:

- Advertise PERF_METRICS and RDPMC_METRICS_CLEAR only when mediated vPMU
  is enabled and host perf reports num_topdown_events != 0.

  Gate on host Topdown events in addition to host fixed counter 3 so
  that this feature is advertised on known CPU models only.

- Gate IA32_PERF_METRICS and RDPMC existence solely on guest
  PERF_CAP_PERF_METRICS (per SDM Vol 4 Table 2-2). Without guest fixed
  counter 3, neither RDMSR nor RDPMC faults (#GP): all metric fields are
  reserved (~0ull), reads (RDMSR and RDPMC) succeed and return 0, writing
  0 succeeds, and only non-zero writes #GP.

  With fixed counter 3, valid metric fields are unmasked based on
  host-provided num_topdown_events.

- Gate IA32_PERF_METRICS, GLOBAL_CTRL, and RDPMC passthrough (and
  hardware context switching) on having both guest PERF_METRICS and fixed
  counter 3. If either is missing, metrics cannot count in hardware;
  accesses remain intercepted and handled cleanly in KVM software
  (e.g., emulated RDPMC succeeds and returns 0).

- Allow setting GLOBAL_CTRL[48] whenever PERF_METRICS is present on
  the guest, because the SDM defines bit 48 as an architectural control
  bit (not reserved) when PERF_METRICS is supported, even if fixed counter
  3 is absent.

- Require guest fixed counter 3 to unmask FIXED_CTR_CTRL[14]
  (METRICS_CLEAR_EN), as bit 14 resides in fixed counter 3's control nibble.
  Without fixed counter 3, bit 14 remains reserved; when enabled with
  fixed counter 3, RDPMC of metrics clears both PERF_METRICS and fixed
  counter 3 in hardware and emulation.

Notes:

A. If the mediated vPMU PMU v5 series [1] lands first:
- Patch 9 ("Mask only OS/USR bits when filtering fixed counters") is
  already included in the PMU v5 series and should be dropped from here.
- In patch 11, the contiguous fixed counter clamp in
  kvm_init_pmu_capability() can be removed, as PMU v5 supports bitmasks.
- The fixed counter 3 checks across the series will be updated to use
  the new PMU v5 helper:
    kvm_is_fixed_pmc_supported(pmu, KVM_FIXED_SLOTS_IDX)

B. Patch 3 only fixes perf_capabilities in the base PMU. Broader base PMU
setup on hybrid hardware running with a single core type (e.g., P/E-core only
SKUs or firmware configs) is orthogonal and left for separate patches.

Patch series summary:
patch 1-5: Prerequisites in host perf
patch 6-9: KVM preparations
patch 10-15: Intel Topdown feature virtualization
patch 16-18: Topdown PERF_METRICS selftests.

Tests: Tested Topdown metrics on ICX (4 metrics), and SPR (8
metrics). Tested RDPMC Metrics Clear on DMR. Tested nested.

Rebased on kvm-x86/next: commit 6bd2905303c5 ("Merge branch 'vmx'")

v10:
- Implement the above dependency policies to comply with the SDM.
- Re-order, consolidation, and new patches:
  * patch 2: do not inflate capabilities under hypervisors.
  * patch 3: refine broken perf capabilities handling in host perf.
  * patch 4: export perf_capabilities from host perf to KVM.
  * patch 7: use host-reported perf_capabilities in VMX instead of rdmsrq().
  * patch 9: move fixed counter filter mask fix into preparation group. (Dapeng)
  * patch 10: intercept unexposed PERF_METRICS before fixed counter 3
    (patch 11) to avoid premature hardware passthrough.
  * Re-order RDPMC emulation (patches 12-13) before PERF_METRICS MSR support
    (patch 14) to eliminate a bisection gap before advertising the capability.
  * Fold v9 patch 11 (reserved bit validation) into patch 14. (Jim)
- Selftests:
  * Moved Topdown metrics tests to a separate test file to avoid bloating
    pmu_counters_test.c.
  * Added coverage for RDPMC Metrics Clear Mode, host MSR access, and
    capability permutations.

v9:
- Reject writes to reserved MSR_PERF_METRICS bits (bits [63:32]).
- Support RDPMC Metrics Clear Mode.
- perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants.
- perf/x86: Expose number of Topdown metric events to KVM.
- Remove the incorrect comment about writing to PERF_METRICS.
- Remove references to Metrics Clear from the changelog of patch 7.

v9: https://lore.kernel.org/kvm/20260918193937.569414-1-zide.chen@intel.com/
v8: https://lore.kernel.org/kvm/20260817183143.226156-1-zide.chen@intel.com/
QEMU: https://lore.kernel.org/qemu-devel/20260604025546.19378-7-zide.chen@intel.com/

[1] https://lore.kernel.org/kvm/20261112203227.289379-1-zide.chen@intel.com/

Dapeng Mi (2):
  KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU
  KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Zide Chen (16):
  perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants
  perf/x86/intel: Do not inflate capabilities for guests
  perf/x86/intel: Track common hybrid capabilities
  perf/x86: Expose perf_capabilities to KVM
  perf/x86: Expose number of Topdown metric events to KVM
  KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events
  KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR
  KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h
  KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters
  KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS
  KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks
  KVM: x86/pmu: Emulate RDPMC on performance metrics
  KVM: x86/pmu: Support RDPMC Metrics Clear Mode
  KVM: selftests: Rework PMU guest measurement asm operands
  KVM: selftests: Move and generalize PMU guest measurement helpers
  KVM: selftests: Add Topdown PERF_METRICS test

 arch/x86/events/core.c                        |   3 +
 arch/x86/events/intel/core.c                  |  39 +-
 arch/x86/include/asm/kvm-x86-pmu-ops.h        |   2 +-
 arch/x86/include/asm/kvm_host.h               |   4 +-
 arch/x86/include/asm/msr-index.h              |   2 +
 arch/x86/include/asm/perf_event.h             |   6 +
 arch/x86/kvm/msrs.c                           |  10 +-
 arch/x86/kvm/pmu.c                            |  60 ++-
 arch/x86/kvm/pmu.h                            |  33 +-
 arch/x86/kvm/svm/pmu.c                        |  14 +-
 arch/x86/kvm/vmx/nested.c                     |   2 +
 arch/x86/kvm/vmx/pmu_intel.c                  | 159 +++++--
 arch/x86/kvm/vmx/pmu_intel.h                  |  12 +-
 arch/x86/kvm/vmx/vmx.c                        |  21 +-
 tools/arch/x86/include/asm/msr-index.h        |   2 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/pmu.h |  60 +++
 .../selftests/kvm/x86/pmu_counters_test.c     |  52 +--
 .../selftests/kvm/x86/pmu_perf_metrics_test.c | 391 ++++++++++++++++++
 19 files changed, 741 insertions(+), 132 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c

--
2.56.1


             reply	other threads:[~2026-11-24 21:53 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 21:38 Zide Chen [this message]
2026-11-24 21:38 ` [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants Zide Chen
2026-12-01  5:39   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 02/18] perf/x86/intel: Do not inflate capabilities for guests Zide Chen
2026-12-01  5:43   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 03/18] perf/x86/intel: Track common hybrid capabilities Zide Chen
2026-12-01  6:01   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 04/18] perf/x86: Expose perf_capabilities to KVM Zide Chen
2026-12-01  6:03   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 05/18] perf/x86: Expose number of Topdown metric events " Zide Chen
2026-11-24 21:38 ` [PATCH v10 06/18] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events Zide Chen
2026-11-24 21:38 ` [PATCH v10 07/18] KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR Zide Chen
2026-12-01  6:07   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 08/18] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h Zide Chen
2026-11-24 21:38 ` [PATCH v10 09/18] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters Zide Chen
2026-12-01  6:15   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS Zide Chen
2026-12-01  6:22   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 11/18] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU Zide Chen
2026-11-24 21:38 ` [PATCH v10 12/18] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks Zide Chen
2026-11-24 21:38 ` [PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics Zide Chen
2026-11-24 21:38 ` [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU Zide Chen
2026-11-24 21:38 ` [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode Zide Chen
2026-12-01  6:50   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 16/18] KVM: selftests: Rework PMU guest measurement asm operands Zide Chen
2026-11-24 21:39 ` [PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers Zide Chen
2026-11-24 21:39 ` [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test Zide Chen
2026-12-01  9:35   ` Mi, Dapeng
2026-12-01 18:21     ` Chen, Zide
2026-12-02  0:27       ` Mi, Dapeng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124213901.120572-1-zide.chen@intel.com \
    --to=zide.chen@intel.com \
    --cc=Manali.Shukla@amd.com \
    --cc=Sandipan.Das@amd.com \
    --cc=ak@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=eranian@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mizhang@google.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=xudong.hao@intel.com \
    --cc=yosry@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.