AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/07/28 17:41 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": [
    "KVM",
    "KVM_INTEL",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "kvm_pmu_rdpmc",
    "intel_emulate_rdpmc",
    "amd_emulate_rdpmc",
    "intel_pmu_get_msr",
    "intel_pmu_set_msr",
    "intel_pmu_refresh",
    "intel_pmu_reset",
    "intel_mediated_pmu_load",
    "intel_mediated_pmu_put",
    "kvm_need_rdpmc_intercept"
  ],
  "Reasoning": "The patch modifies KVM's PMU implementation on x86 to support MSR_PERF_METRICS and fixed counter 3 (Topdown slots). It also refactors RDPMC emulation and adds checks for KVM RDPMC passthrough support. This is core virtualization logic reachable from the guest when accessing PMU MSRs or executing RDPMC, and should be fuzzed.",
  "WorthFuzzing": true
}

1/1 2026/07/28 17:41 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit d1f77d992311dff5dcf3abd258343910c039cae6\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Tue Jul 28 17:41:14 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h\nindex 4a223c2793e3f..4b50ed058aed3 100644\n--- a/arch/x86/include/asm/kvm-x86-pmu-ops.h\n+++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h\n@@ -13,7 +13,7 @@\n  * KVM_X86_PMU_OP_OPTIONAL() can be used for those functions that can have\n  * a NULL definition.\n  */\n-KVM_X86_PMU_OP(rdpmc_ecx_to_pmc)\n+KVM_X86_PMU_OP(emulate_rdpmc)\n KVM_X86_PMU_OP(msr_idx_to_pmc)\n KVM_X86_PMU_OP_OPTIONAL(check_rdpmc_early)\n KVM_X86_PMU_OP(is_valid_msr)\ndiff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h\nindex b517257a63153..2a92a75af7e65 100644\n--- a/arch/x86/include/asm/kvm_host.h\n+++ b/arch/x86/include/asm/kvm_host.h\n@@ -347,6 +347,7 @@ struct kvm_host_values {\n \tu64 xss;\n \tu64 s_cet;\n \tu64 arch_capabilities;\n+\tu64 perf_capabilities;\n };\n extern struct kvm_host_values kvm_host;\n \n@@ -615,7 +616,7 @@ struct kvm_pmc {\n #define KVM_MAX_NR_GP_COUNTERS\t\tKVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \\\n \t\t\t\t\t\tKVM_MAX_NR_AMD_GP_COUNTERS)\n \n-#define KVM_MAX_NR_INTEL_FIXED_COUNTERS\t3\n+#define KVM_MAX_NR_INTEL_FIXED_COUNTERS\t4\n #define KVM_MAX_NR_AMD_FIXED_COUNTERS\t0\n #define KVM_MAX_NR_FIXED_COUNTERS\tKVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \\\n \t\t\t\t\t\tKVM_MAX_NR_AMD_FIXED_COUNTERS)\n@@ -635,6 +636,7 @@ struct kvm_pmu {\n \tu64 global_status_rsvd;\n \tu64 reserved_bits;\n \tu64 raw_event_mask;\n+\tu64 perf_metrics;\n \tstruct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];\n \tstruct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];\n \ndiff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h\nindex 18c4be75e9271..fdcaeb6c8352a 100644\n--- a/arch/x86/include/asm/msr-index.h\n+++ b/arch/x86/include/asm/msr-index.h\n@@ -331,6 +331,7 @@\n #define PERF_CAP_PEBS_FORMAT\t\t0xf00\n #define PERF_CAP_FW_WRITES\t\tBIT_ULL(13)\n #define PERF_CAP_PEBS_BASELINE\t\tBIT_ULL(14)\n+#define PERF_CAP_PERF_METRICS\t\tBIT_ULL(15)\n #define PERF_CAP_PEBS_TIMING_INFO\tBIT_ULL(17)\n #define PERF_CAP_PEBS_MASK\t\t(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \\\n \t\t\t\t\t PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \\\ndiff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c\nindex c230b18d87e38..c751a8dbd45dc 100644\n--- a/arch/x86/kvm/msrs.c\n+++ b/arch/x86/kvm/msrs.c\n@@ -228,9 +228,9 @@ static const u32 msrs_to_save_base[] = {\n \n static const u32 msrs_to_save_pmu[] = {\n \tMSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,\n-\tMSR_ARCH_PERFMON_FIXED_CTR0 + 2,\n+\tMSR_ARCH_PERFMON_FIXED_CTR2, MSR_ARCH_PERFMON_FIXED_CTR3,\n \tMSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,\n-\tMSR_CORE_PERF_GLOBAL_CTRL,\n+\tMSR_CORE_PERF_GLOBAL_CTRL, MSR_PERF_METRICS,\n \tMSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,\n \n \t/* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */\n@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)\n \t\t     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))\n \t\t\treturn;\n \t\tbreak;\n+\tcase MSR_PERF_METRICS:\n+\t\tif (!(kvm_caps.supported_perf_cap \u0026 PERF_CAP_PERF_METRICS))\n+\t\t\treturn;\n+\t\tbreak;\n \tcase MSR_ARCH_PERFMON_PERFCTR0 ...\n \t     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:\n \t\tif (msr_index - MSR_ARCH_PERFMON_PERFCTR0 \u003e=\n@@ -2688,7 +2692,7 @@ void kvm_init_msr_lists(void)\n {\n \tunsigned i;\n \n-\tBUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,\n+\tBUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 4,\n \t\t\t \"Please update the fixed PMCs in msrs_to_save_pmu[]\");\n \n \tnum_msrs_to_save = 0;\ndiff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c\nindex 62d0ed99ebe98..4d8354c6e7d81 100644\n--- a/arch/x86/kvm/pmu.c\n+++ b/arch/x86/kvm/pmu.c\n@@ -19,6 +19,7 @@\n #include \u003clinux/moduleparam.h\u003e\n #include \u003casm/perf_event.h\u003e\n #include \u003casm/cpu_device_id.h\u003e\n+#include \u003casm/cpuid/api.h\u003e\n #include \"x86.h\"\n #include \"cpuid.h\"\n #include \"lapic.h\"\n@@ -76,6 +77,21 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] = {\n \t{}\n };\n \n+/*\n+ * CPUs whose RDPMC encodings have been audited for KVM RDPMC\n+ * passthrough support.\n+ */\n+static const struct x86_cpu_id kvm_rdpmc_known_cpus[] = {\n+\tX86_MATCH_VFM(INTEL_ATOM_DARKMONT_X, NULL),\n+\tX86_MATCH_VFM(INTEL_LUNARLAKE_M, NULL),\n+\tX86_MATCH_VFM(INTEL_NOVALAKE, NULL),\n+\tX86_MATCH_VFM(INTEL_NOVALAKE_L, NULL),\n+\tX86_MATCH_VFM(INTEL_PANTHERLAKE_L, NULL),\n+\tX86_MATCH_VFM(INTEL_PANTHERLAKE_R, NULL),\n+\tX86_MATCH_VFM(INTEL_WILDCATLAKE_L, NULL),\n+\t{}\n+};\n+\n /* NOTE:\n  * - Each perf counter is defined as \"struct kvm_pmc\";\n  * - There are two types of perf counters: general purpose (gp) and fixed.\n@@ -99,7 +115,8 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] = {\n  *        all perf counters (both gp and fixed). The mapping relationship\n  *        between pmc and perf counters is as the following:\n  *        * Intel: [0 .. KVM_MAX_NR_INTEL_GP_COUNTERS-1] \u003c=\u003e gp counters\n- *                 [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + 2] \u003c=\u003e fixed\n+ *                 [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX +\n+ *                  KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1] \u003c=\u003e fixed\n  *        * AMD:   [0 .. AMD64_NUM_COUNTERS-1] and, for families 15H\n  *          and later, [0 .. AMD64_NUM_COUNTERS_CORE-1] \u003c=\u003e gp counters\n  */\n@@ -134,6 +151,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n {\n \tbool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;\n \tint min_nr_gp_ctrs = pmu_ops-\u003eMIN_NR_GP_COUNTERS;\n+\tunion cpuid10_edx edx;\n+\tu32 eax, ebx, ecx;\n \n \t/*\n \t * Hybrid PMUs don't play nice with virtualization without careful\n@@ -181,6 +200,19 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n \tkvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,\n \t\t\t\t\t     KVM_MAX_NR_FIXED_COUNTERS);\n \n+\t/*\n+\t * Currently, KVM doesn't support non-contiguous fixed counters; make\n+\t * sure only contiguous ones are retained in kvm_pmu_cap.\n+\t */\n+\tif (kvm_host_pmu.version \u003e= 5) {\n+\t\tcpuid(0xa, \u0026eax, \u0026ebx, \u0026ecx, \u0026edx.full);\n+\t\tif (kvm_pmu_cap.num_counters_fixed \u003e edx.split.num_counters_fixed)\n+\t\t\tkvm_pmu_cap.num_counters_fixed = edx.split.num_counters_fixed;\n+\t}\n+\n+\tif (!enable_mediated_pmu \u0026\u0026 kvm_pmu_cap.num_counters_fixed \u003e 3)\n+\t\tkvm_pmu_cap.num_counters_fixed = 3;\n+\n \tkvm_pmu_eventsel.INSTRUCTIONS_RETIRED =\n \t\tperf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);\n \tkvm_pmu_eventsel.BRANCH_INSTRUCTIONS_RETIRED =\n@@ -752,8 +784,6 @@ static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\n int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n-\tstruct kvm_pmc *pmc;\n-\tu64 mask = ~0ull;\n \n \tif (!pmu-\u003eversion)\n \t\treturn 1;\n@@ -761,17 +791,12 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\n \tif (is_vmware_backdoor_pmc(idx))\n \t\treturn kvm_pmu_rdpmc_vmware(vcpu, idx, data);\n \n-\tpmc = kvm_pmu_call(rdpmc_ecx_to_pmc)(vcpu, idx, \u0026mask);\n-\tif (!pmc)\n-\t\treturn 1;\n-\n \tif (!kvm_is_cr4_bit_set(vcpu, X86_CR4_PCE) \u0026\u0026\n \t    (kvm_x86_call(get_cpl)(vcpu) != 0) \u0026\u0026\n \t    kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))\n \t\treturn 1;\n \n-\t*data = pmc_read_counter(pmc) \u0026 mask;\n-\treturn 0;\n+\treturn kvm_pmu_call(emulate_rdpmc)(vcpu, idx, data);\n }\n \n static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)\n@@ -797,6 +822,26 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)\n }\n EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);\n \n+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)\n+{\n+\treturn (kvm_host.perf_capabilities \u0026 PERF_CAP_PERF_METRICS) \u0026\u0026\n+\t\t!kvm_vcpu_has_perf_metrics(vcpu);\n+}\n+\n+static bool kvm_rdpmc_encoding_supported(void)\n+{\n+\t/* KVM understands all RDPMC encodings prior to PMU v6. */\n+\tif (kvm_host_pmu.version \u003c 6)\n+\t\treturn true;\n+\n+\t/*\n+\t * Future PMU v6 implementations and future PMU versions require RDPMC\n+\t * interception until their RDPMC encodings are audited and supported\n+\t * by KVM.\n+\t */\n+\treturn x86_match_cpu(kvm_rdpmc_known_cpus);\n+}\n+\n bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n@@ -808,7 +853,11 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)\n \tif (enable_vmware_backdoor)\n \t\treturn true;\n \n+\tif (!kvm_rdpmc_encoding_supported())\n+\t\treturn true;\n+\n \treturn kvm_need_any_pmc_intercept(vcpu) ||\n+\t       kvm_need_perf_metrics_intercept(vcpu) ||\n \t       pmu-\u003ecounter_bitmask[KVM_PMC_GP] != (BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) ||\n \t       pmu-\u003ecounter_bitmask[KVM_PMC_FIXED] != (BIT_ULL(kvm_host_pmu.bit_width_fixed) - 1);\n }\ndiff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h\nindex a5821d7c87f93..cdbefda844b97 100644\n--- a/arch/x86/kvm/pmu.h\n+++ b/arch/x86/kvm/pmu.h\n@@ -24,8 +24,8 @@\n #define KVM_FIXED_PMC_BASE_IDX INTEL_PMC_IDX_FIXED\n \n struct kvm_pmu_ops {\n-\tstruct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,\n-\t\tunsigned int idx, u64 *mask);\n+\tint (*emulate_rdpmc)(struct kvm_vcpu *vcpu, unsigned int idx,\n+\t\t\t     u64 *data);\n \tstruct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr);\n \tint (*check_rdpmc_early)(struct kvm_vcpu *vcpu, unsigned int idx);\n \tbool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);\n@@ -271,6 +271,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu)\n \t\t\t\t  X86_PMC_IDX_MAX);\n }\n \n+static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu)\n+{\n+\tif (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM))\n+\t\treturn 0;\n+\n+\treturn vcpu-\u003earch.perf_capabilities;\n+}\n+\n+static inline bool kvm_vcpu_has_perf_metrics(struct kvm_vcpu *vcpu)\n+{\n+\treturn kvm_vcpu_get_perf_caps(vcpu) \u0026 PERF_CAP_PERF_METRICS;\n+}\n+\n void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);\n int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);\n int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);\ndiff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c\nindex c18286545a7ac..0517fd4bbcd7a 100644\n--- a/arch/x86/kvm/svm/pmu.c\n+++ b/arch/x86/kvm/svm/pmu.c\n@@ -84,10 +84,15 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)\n }\n \n /* idx is the ECX register of RDPMC instruction */\n-static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,\n-\tunsigned int idx, u64 *mask)\n+static int amd_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 *data)\n {\n-\treturn amd_pmu_get_pmc(vcpu_to_pmu(vcpu), idx);\n+\tstruct kvm_pmc *pmc = amd_pmu_get_pmc(vcpu_to_pmu(vcpu), idx);\n+\n+\tif (!pmc)\n+\t\treturn 1;\n+\n+\t*data = pmc_read_counter(pmc);\n+\treturn 0;\n }\n \n static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr)\n@@ -302,7 +307,7 @@ static bool amd_pmc_is_disabled_in_current_mode(struct kvm_pmc *pmc)\n }\n \n struct kvm_pmu_ops amd_pmu_ops __initdata = {\n-\t.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,\n+\t.emulate_rdpmc = amd_emulate_rdpmc,\n \t.msr_idx_to_pmc = amd_msr_idx_to_pmc,\n \t.check_rdpmc_early = amd_check_rdpmc_early,\n \t.is_valid_msr = amd_is_valid_msr,\ndiff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c\nindex 0635e92471c89..1aa3b29359544 100644\n--- a/arch/x86/kvm/vmx/nested.c\n+++ b/arch/x86/kvm/vmx/nested.c\n@@ -688,6 +688,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,\n \tnested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);\n \tnested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);\n \tnested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);\n+\n+\tnested_vmx_merge_msr_bitmaps_rw(MSR_PERF_METRICS);\n }\n \n /*\ndiff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c\nindex a73a9515d96cd..d1c0e36954c1c 100644\n--- a/arch/x86/kvm/vmx/pmu_intel.c\n+++ b/arch/x86/kvm/vmx/pmu_intel.c\n@@ -30,6 +30,7 @@\n  */\n #define INTEL_RDPMC_GP\t\t0\n #define INTEL_RDPMC_FIXED\tINTEL_PMC_FIXED_RDPMC_BASE\n+#define INTEL_RDPMC_METRICS\tINTEL_PMC_FIXED_RDPMC_METRICS\n \n #define INTEL_RDPMC_TYPE_MASK\tGENMASK(31, 16)\n #define INTEL_RDPMC_INDEX_MASK\tGENMASK(15, 0)\n@@ -84,14 +85,13 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)\n \t}\n }\n \n-static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,\n-\t\t\t\t\t    unsigned int idx, u64 *mask)\n+static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx,\n+\t\t\t       u64 *data)\n {\n \tunsigned int type = idx \u0026 INTEL_RDPMC_TYPE_MASK;\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n-\tstruct kvm_pmc *counters;\n+\tstruct kvm_pmc *counters, *pmc;\n \tunsigned int num_counters;\n-\tu64 bitmask;\n \n \t/*\n \t * The encoding of ECX for RDPMC is different for architectural versus\n@@ -104,7 +104,9 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,\n \t * as KVM doesn't support such PMUs.\n \t */\n \tif (WARN_ON_ONCE(!pmu-\u003eversion))\n-\t\treturn NULL;\n+\t\treturn 1;\n+\n+\tidx \u0026= INTEL_RDPMC_INDEX_MASK;\n \n \t/*\n \t * General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs\n@@ -118,23 +120,34 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,\n \tcase INTEL_RDPMC_FIXED:\n \t\tcounters = pmu-\u003efixed_counters;\n \t\tnum_counters = pmu-\u003enr_arch_fixed_counters;\n-\t\tbitmask = pmu-\u003ecounter_bitmask[KVM_PMC_FIXED];\n \t\tbreak;\n \tcase INTEL_RDPMC_GP:\n \t\tcounters = pmu-\u003egp_counters;\n \t\tnum_counters = pmu-\u003enr_arch_gp_counters;\n-\t\tbitmask = pmu-\u003ecounter_bitmask[KVM_PMC_GP];\n \t\tbreak;\n+\tcase INTEL_RDPMC_METRICS:\n+\t\tif (!kvm_vcpu_has_perf_metrics(vcpu))\n+\t\t\treturn 1;\n+\n+\t\t/*\n+\t\t * The index in ECX[15:0] is implementation specific, but no\n+\t\t * platform currently supports a non-zero index.\n+\t\t */\n+\t\tif (idx)\n+\t\t\treturn 1;\n+\n+\t\t*data = pmu-\u003eperf_metrics;\n+\t\treturn 0;\n \tdefault:\n-\t\treturn NULL;\n+\t\treturn 1;\n \t}\n \n-\tidx \u0026= INTEL_RDPMC_INDEX_MASK;\n \tif (idx \u003e= num_counters)\n-\t\treturn NULL;\n+\t\treturn 1;\n \n-\t*mask \u0026= bitmask;\n-\treturn \u0026counters[array_index_nospec(idx, num_counters)];\n+\tpmc = \u0026counters[array_index_nospec(idx, num_counters)];\n+\t*data = pmc_read_counter(pmc);\n+\treturn 0;\n }\n \n static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)\n@@ -188,14 +201,16 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)\n \tswitch (msr) {\n \tcase MSR_CORE_PERF_FIXED_CTR_CTRL:\n \t\treturn kvm_pmu_has_perf_global_ctrl(pmu);\n+\tcase MSR_PERF_METRICS:\n+\t\treturn kvm_vcpu_has_perf_metrics(vcpu);\n \tcase MSR_IA32_PEBS_ENABLE:\n-\t\tret = vcpu_get_perf_capabilities(vcpu) \u0026 PERF_CAP_PEBS_FORMAT;\n+\t\tret = kvm_vcpu_get_perf_caps(vcpu) \u0026 PERF_CAP_PEBS_FORMAT;\n \t\tbreak;\n \tcase MSR_IA32_DS_AREA:\n \t\tret = guest_cpu_cap_has(vcpu, X86_FEATURE_DS);\n \t\tbreak;\n \tcase MSR_PEBS_DATA_CFG:\n-\t\tperf_capabilities = vcpu_get_perf_capabilities(vcpu);\n+\t\tperf_capabilities = kvm_vcpu_get_perf_caps(vcpu);\n \t\tret = (perf_capabilities \u0026 PERF_CAP_PEBS_BASELINE) \u0026\u0026\n \t\t\t((perf_capabilities \u0026 PERF_CAP_PEBS_FORMAT) \u003e 3);\n \t\tbreak;\n@@ -345,6 +360,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \tcase MSR_CORE_PERF_FIXED_CTR_CTRL:\n \t\tmsr_info-\u003edata = pmu-\u003efixed_ctr_ctrl;\n \t\tbreak;\n+\tcase MSR_PERF_METRICS:\n+\t\tmsr_info-\u003edata = pmu-\u003eperf_metrics;\n+\t\tbreak;\n \tcase MSR_IA32_PEBS_ENABLE:\n \t\tmsr_info-\u003edata = pmu-\u003epebs_enable;\n \t\tbreak;\n@@ -394,6 +412,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\tif (pmu-\u003efixed_ctr_ctrl != data)\n \t\t\treprogram_fixed_counters(pmu, data);\n \t\tbreak;\n+\tcase MSR_PERF_METRICS:\n+\t\t/*\n+\t\t * Bits [63:32] are ignored on hardware that supports only\n+\t\t * level-1 metrics, but may be valid on hardware that supports\n+\t\t * level-2 metrics. Preserve guest writes verbatim.\n+\t\t */\n+\t\tpmu-\u003eperf_metrics = data;\n+\t\tbreak;\n \tcase MSR_IA32_PEBS_ENABLE:\n \t\tif (data \u0026 pmu-\u003epebs_enable_rsvd)\n \t\t\treturn 1;\n@@ -464,11 +490,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n  * different perf_event is already utilizing the requested counter, but the end\n  * result is the same (ignoring the fact that using a general purpose counter\n  * will likely exacerbate counter contention).\n- *\n- * Forcibly inlined to allow asserting on @index at build time, and there should\n- * never be more than one user.\n  */\n-static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)\n+static u64 intel_get_fixed_pmc_eventsel(unsigned int index)\n {\n \tconst enum perf_hw_id fixed_pmc_perf_ids[] = {\n \t\t[0] = PERF_COUNT_HW_INSTRUCTIONS,\n@@ -477,8 +500,13 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)\n \t};\n \tu64 eventsel;\n \n-\tBUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_perf_ids) != KVM_MAX_NR_INTEL_FIXED_COUNTERS);\n-\tBUILD_BUG_ON(index \u003e= KVM_MAX_NR_INTEL_FIXED_COUNTERS);\n+\t/*\n+\t * Fixed counters 3 and above don't have a corresponding generic\n+\t * hardware perf event, and KVM does not intend to emulate them on\n+\t * non-mediated vPMU.\n+\t */\n+\tif (index \u003e= ARRAY_SIZE(fixed_pmc_perf_ids))\n+\t\treturn 0;\n \n \t/*\n \t * Yell if perf reports support for a fixed counter but perf doesn't\n@@ -548,7 +576,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\n \t\tpmu-\u003eraw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);\n \t}\n \n-\tperf_capabilities = vcpu_get_perf_capabilities(vcpu);\n+\tperf_capabilities = kvm_vcpu_get_perf_caps(vcpu);\n \tif (intel_pmu_lbr_is_compatible(vcpu) \u0026\u0026\n \t    (perf_capabilities \u0026 PERF_CAP_LBR_FMT))\n \t\tmemcpy(\u0026lbr_desc-\u003erecords, \u0026vmx_lbr_caps, sizeof(vmx_lbr_caps));\n@@ -574,6 +602,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\n \tcounter_rsvd = ~((BIT_ULL(pmu-\u003enr_arch_gp_counters) - 1) |\n \t\t\t ((BIT_ULL(pmu-\u003enr_arch_fixed_counters) - 1) \u003c\u003c KVM_FIXED_PMC_BASE_IDX));\n \tpmu-\u003eglobal_ctrl_rsvd = counter_rsvd;\n+\tif (perf_capabilities \u0026 PERF_CAP_PERF_METRICS)\n+\t\tpmu-\u003eglobal_ctrl_rsvd \u0026= ~GLOBAL_CTRL_EN_PERF_METRICS;\n \n \t/*\n \t * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)\n@@ -630,6 +660,9 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)\n \n static void intel_pmu_reset(struct kvm_vcpu *vcpu)\n {\n+\tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\n+\tpmu-\u003eperf_metrics = 0;\n \tintel_pmu_release_guest_lbr_event(vcpu);\n }\n \n@@ -801,6 +834,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \tu64 global_status, toggle;\n \n+\tif (kvm_vcpu_has_perf_metrics(vcpu))\n+\t\twrmsrq(MSR_PERF_METRICS, pmu-\u003eperf_metrics);\n+\n \trdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, global_status);\n \ttoggle = pmu-\u003eglobal_status ^ global_status;\n \tif (global_status \u0026 toggle)\n@@ -829,10 +865,23 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)\n \t */\n \tif (pmu-\u003efixed_ctr_ctrl_hw)\n \t\twrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, 0);\n+\n+\tif (kvm_vcpu_has_perf_metrics(vcpu)) {\n+\t\tpmu-\u003eperf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);\n+\t\t/*\n+\t\t * The SDM requires restoring fixed counter 3 before\n+\t\t * PERF_METRICS. However, this path writes 0 to PERF_METRICS\n+\t\t * before fixed counter 3. For this all-zero case, the\n+\t\t * resulting hardware state is therefore the same regardless\n+\t\t * of write order.\n+\t\t */\n+\t\tif (pmu-\u003eperf_metrics)\n+\t\t\twrmsrq(MSR_PERF_METRICS, 0);\n+\t}\n }\n \n struct kvm_pmu_ops intel_pmu_ops __initdata = {\n-\t.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,\n+\t.emulate_rdpmc = intel_emulate_rdpmc,\n \t.msr_idx_to_pmc = intel_msr_idx_to_pmc,\n \t.is_valid_msr = intel_is_valid_msr,\n \t.get_msr = intel_pmu_get_msr,\ndiff --git a/arch/x86/kvm/vmx/pmu_intel.h b/arch/x86/kvm/vmx/pmu_intel.h\nindex 5d9357640aa18..afdbbc9991d6d 100644\n--- a/arch/x86/kvm/vmx/pmu_intel.h\n+++ b/arch/x86/kvm/vmx/pmu_intel.h\n@@ -6,17 +6,9 @@\n \n #include \"cpuid.h\"\n \n-static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu)\n-{\n-\tif (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM))\n-\t\treturn 0;\n-\n-\treturn vcpu-\u003earch.perf_capabilities;\n-}\n-\n static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu)\n {\n-\treturn (vcpu_get_perf_capabilities(vcpu) \u0026 PERF_CAP_FW_WRITES) != 0;\n+\treturn (kvm_vcpu_get_perf_caps(vcpu) \u0026 PERF_CAP_FW_WRITES) != 0;\n }\n \n bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu);\ndiff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c\nindex 3681d565f1778..25d70645f9ac6 100644\n--- a/arch/x86/kvm/vmx/vmx.c\n+++ b/arch/x86/kvm/vmx/vmx.c\n@@ -4273,6 +4273,10 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n \t\t\t\t  MSR_TYPE_RW, intercept);\n \tvmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_OVF_CTRL,\n \t\t\t\t  MSR_TYPE_RW, intercept);\n+\n+\tintercept = !has_mediated_pmu || !kvm_vcpu_has_perf_metrics(vcpu);\n+\tvmx_set_intercept_for_msr(vcpu, MSR_PERF_METRICS,\n+\t\t\t\t  MSR_TYPE_RW, intercept);\n }\n \n static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)\n@@ -8050,14 +8054,10 @@ void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)\n static __init u64 vmx_get_perf_capabilities(void)\n {\n \tu64 perf_cap = PERF_CAP_FW_WRITES;\n-\tu64 host_perf_cap = 0;\n \n \tif (!enable_pmu)\n \t\treturn 0;\n \n-\tif (boot_cpu_has(X86_FEATURE_PDCM))\n-\t\trdmsrq(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);\n-\n \tif (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) \u0026\u0026\n \t    !enable_mediated_pmu) {\n \t\tx86_perf_get_lbr(\u0026vmx_lbr_caps);\n@@ -8070,11 +8070,11 @@ static __init u64 vmx_get_perf_capabilities(void)\n \t\tif (!vmx_lbr_caps.has_callstack)\n \t\t\tmemset(\u0026vmx_lbr_caps, 0, sizeof(vmx_lbr_caps));\n \t\telse if (vmx_lbr_caps.nr)\n-\t\t\tperf_cap |= host_perf_cap \u0026 PERF_CAP_LBR_FMT;\n+\t\t\tperf_cap |= kvm_host.perf_capabilities \u0026 PERF_CAP_LBR_FMT;\n \t}\n \n \tif (vmx_pebs_supported()) {\n-\t\tperf_cap |= host_perf_cap \u0026 PERF_CAP_PEBS_MASK;\n+\t\tperf_cap |= kvm_host.perf_capabilities \u0026 PERF_CAP_PEBS_MASK;\n \n \t\t/*\n \t\t * Disallow adaptive PEBS as it is functionally broken, can be\n@@ -8099,6 +8099,9 @@ static __init u64 vmx_get_perf_capabilities(void)\n \t\tperf_cap \u0026= ~PERF_CAP_PEBS_BASELINE;\n \t}\n \n+\tif (enable_mediated_pmu)\n+\t\tperf_cap |= kvm_host.perf_capabilities \u0026 PERF_CAP_PERF_METRICS;\n+\n \treturn perf_cap;\n }\n \ndiff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c\nindex 0626e835e9eb7..e0114f248a9ef 100644\n--- a/arch/x86/kvm/x86.c\n+++ b/arch/x86/kvm/x86.c\n@@ -7033,6 +7033,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)\n \tif (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))\n \t\trdmsrq(MSR_IA32_ARCH_CAPABILITIES, kvm_host.arch_capabilities);\n \n+\tif (boot_cpu_has(X86_FEATURE_PDCM))\n+\t\trdmsrq_safe(MSR_IA32_PERF_CAPABILITIES,\n+\t\t\t    \u0026kvm_host.perf_capabilities);\n+\n \tWARN_ON_ONCE(kvm_nr_uret_msrs);\n \n \tr = ops-\u003ehardware_setup();\ndiff --git a/tools/arch/x86/include/asm/msr-index.h b/tools/arch/x86/include/asm/msr-index.h\nindex eff29645719bc..e7745e2cd543a 100644\n--- a/tools/arch/x86/include/asm/msr-index.h\n+++ b/tools/arch/x86/include/asm/msr-index.h\n@@ -331,6 +331,7 @@\n #define PERF_CAP_PEBS_FORMAT\t\t0xf00\n #define PERF_CAP_FW_WRITES\t\tBIT_ULL(13)\n #define PERF_CAP_PEBS_BASELINE\t\tBIT_ULL(14)\n+#define PERF_CAP_PERF_METRICS\t\tBIT_ULL(15)\n #define PERF_CAP_PEBS_TIMING_INFO\tBIT_ULL(17)\n #define PERF_CAP_PEBS_MASK\t\t(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \\\n \t\t\t\t\t PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \\\ndiff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h\nindex 608ed83d7c6a6..6c19503e0bb7d 100644\n--- a/tools/testing/selftests/kvm/include/x86/pmu.h\n+++ b/tools/testing/selftests/kvm/include/x86/pmu.h\n@@ -52,6 +52,9 @@\n /* Fixed PMC controls, Intel only. */\n #define FIXED_PMC_GLOBAL_CTRL_ENABLE(_idx)\tBIT_ULL((32 + (_idx)))\n \n+/* PERF_METRICS enable, Intel only. */\n+#define PERF_METRICS_GLOBAL_CTRL_ENABLE\t\tBIT_ULL(48)\n+\n #define FIXED_PMC_KERNEL\t\t\tBIT_ULL(0)\n #define FIXED_PMC_USER\t\t\t\tBIT_ULL(1)\n #define FIXED_PMC_ANYTHREAD\t\t\tBIT_ULL(2)\ndiff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c\nindex dc6afac3aa919..8294a0089a85a 100644\n--- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c\n+++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c\n@@ -3,6 +3,7 @@\n  * Copyright (C) 2023, Tencent, Inc.\n  */\n #include \u003cx86intrin.h\u003e\n+#include \u003clinux/bitfield.h\u003e\n \n #include \"pmu.h\"\n #include \"processor.h\"\n@@ -241,17 +242,20 @@ do {\t\t\t\t\t\t\t\t\t\t\\\n \t);\t\t\t\t\t\t\t\t\t\\\n } while (0)\n \n-#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP)\t\t\\\n+#define GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP)\t\t\t\t\\\n do {\t\t\t\t\t\t\t\t\t\t\\\n-\twrmsr(_pmc_msr, 0);\t\t\t\t\t\t\t\\\n-\t\t\t\t\t\t\t\t\t\t\\\n \tif (this_cpu_has(X86_FEATURE_CLFLUSHOPT))\t\t\t\t\\\n \t\tGUEST_MEASURE_EVENT(_ctrl_msr, _value, \"clflushopt %[m]\", FEP);\t\\\n \telse if (this_cpu_has(X86_FEATURE_CLFLUSH))\t\t\t\t\\\n \t\tGUEST_MEASURE_EVENT(_ctrl_msr, _value, \"clflush  %[m]\", FEP);\t\\\n \telse\t\t\t\t\t\t\t\t\t\\\n \t\tGUEST_MEASURE_EVENT(_ctrl_msr, _value, \"nop\", FEP);\t\t\\\n-\t\t\t\t\t\t\t\t\t\t\\\n+} while (0)\n+\n+#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP)\t\t\\\n+do {\t\t\t\t\t\t\t\t\t\t\\\n+\twrmsr(_pmc_msr, 0);\t\t\t\t\t\t\t\\\n+\tGUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP);\t\t\t\t\\\n \tguest_assert_event_count(_idx, _pmc, _pmc_msr);\t\t\t\t\\\n } while (0)\n \n@@ -361,7 +365,7 @@ static void test_arch_events(u8 pmu_version, u64 perf_capabilities,\n  * other than PMCs in the future.\n  */\n #define MAX_NR_GP_COUNTERS\t8\n-#define MAX_NR_FIXED_COUNTERS\t3\n+#define MAX_NR_FIXED_COUNTERS\t4\n \n #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector)\t\t\\\n __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector,\t\t\t\\\n@@ -580,11 +584,81 @@ static void test_fixed_counters(u8 pmu_version, u64 perf_capabilities,\n \tkvm_vm_free(vm);\n }\n \n+static void __guest_test_perf_metrics(void)\n+{\n+\tint retiring, bad_spec, fe_bound, be_bound, sum;\n+\tu64 global_ctrl, metrics;\n+\n+\tif ((guest_get_pmu_version() \u003c 2) ||\t/* Does guest have GLOBAL_CTRL? */\n+\t    !this_cpu_has(X86_FEATURE_PDCM) ||\n+\t    !(rdmsr(MSR_IA32_PERF_CAPABILITIES) \u0026 PERF_CAP_PERF_METRICS))\n+\t\treturn;\n+\n+\twrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);\n+\twrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);\n+\twrmsr(MSR_PERF_METRICS, 0);\n+\n+\t/* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */\n+\twrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));\n+\tglobal_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |\n+\t\t      PERF_METRICS_GLOBAL_CTRL_ENABLE;\n+\n+\tGUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, \"\");\n+\n+\t/* Check test results. */\n+\tmetrics = rdmsr(MSR_PERF_METRICS);\n+\tretiring = FIELD_GET(GENMASK_ULL(7, 0), metrics);\n+\tbad_spec = FIELD_GET(GENMASK_ULL(15, 8), metrics);\n+\tfe_bound = FIELD_GET(GENMASK_ULL(23, 16), metrics);\n+\tbe_bound = FIELD_GET(GENMASK_ULL(31, 24), metrics);\n+\n+\t/*\n+\t * Be conservative: the measured payload definitely retires work, so\n+\t * Retiring should be non-zero.\n+\t */\n+\tGUEST_ASSERT_NE(metrics, 0ULL);\n+\tGUEST_ASSERT_NE(retiring, 0ULL);\n+\n+\t/*\n+\t * Each level-1 Topdown metric is an integer fraction of 0xff.\n+\t * A +/-3 error margin is chosen for a loose sanity check.\n+\t */\n+\tsum = retiring + bad_spec + fe_bound + be_bound;\n+\tGUEST_ASSERT(sum \u003e= 0xfc \u0026\u0026 sum \u003c= 0x102);\n+\n+\t/* Sanity check after PERF_METRICS disabled. */\n+\t__asm__ __volatile__(\"loop .\" : \"+c\"((int){NUM_LOOPS}));\n+\tGUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);\n+\twrmsr(MSR_PERF_METRICS, 0xdeaddead);\n+\n+\tguest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0xdeaddead);\n+}\n+\n+static void guest_test_perf_metrics(void)\n+{\n+\t__guest_test_perf_metrics();\n+\tGUEST_DONE();\n+}\n+\n+static void test_perf_metrics(u8 pmu_version, u64 perf_capabilities)\n+{\n+\tstruct kvm_vcpu *vcpu;\n+\tstruct kvm_vm *vm;\n+\n+\tvm = pmu_vm_create_with_one_vcpu(\u0026vcpu, guest_test_perf_metrics,\n+\t\t\t\t\t pmu_version, perf_capabilities);\n+\n+\trun_vcpu(vcpu);\n+\n+\tkvm_vm_free(vm);\n+}\n+\n static void test_intel_counters(void)\n {\n \tu8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);\n \tu8 nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);\n \tu8 pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);\n+\tu64 advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);\n \tunsigned int i;\n \tu8 v, j;\n \tu32 k;\n@@ -592,6 +666,7 @@ static void test_intel_counters(void)\n \tconst u64 perf_caps[] = {\n \t\t0,\n \t\tPMU_CAP_FW_WRITES,\n+\t\tPERF_CAP_PERF_METRICS,\n \t};\n \n \t/*\n@@ -649,6 +724,10 @@ static void test_intel_counters(void)\n \t\t\tif (!kvm_has_perf_caps \u0026\u0026 perf_caps[i])\n \t\t\t\tcontinue;\n \n+\t\t\t/* Ignore unsupported features. */\n+\t\t\tif (perf_caps[i] \u0026 ~advertised_perf_caps)\n+\t\t\t\tcontinue;\n+\n \t\t\tpr_info(\"Testing arch events, PMU version %u, perf_caps = %lx\\n\",\n \t\t\t\tv, perf_caps[i]);\n \n@@ -675,6 +754,11 @@ static void test_intel_counters(void)\n \t\t\t\tfor (k = 0; k \u003c= (BIT(nr_fixed_counters) - 1); k++)\n \t\t\t\t\ttest_fixed_counters(v, perf_caps[i], j, k);\n \t\t\t}\n+\n+\t\t\tpr_info(\"Testing Perf Metrics, PMU version %u, perf_caps = %lx\\n\",\n+\t\t\t\tv, perf_caps[i]);\n+\n+\t\t\ttest_perf_metrics(v, perf_caps[i]);\n \t\t}\n \t}\n }\n"
}

2/1 2026/07/28 17:41 agent patch-evaluator
0m Model:
best-expensive

Args:
null

Results:
{
  "EnableConfigs": [
    "KVM",
    "KVM_INTEL",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "kvm_pmu_rdpmc",
    "intel_emulate_rdpmc",
    "amd_emulate_rdpmc",
    "intel_pmu_get_msr",
    "intel_pmu_set_msr",
    "intel_pmu_refresh",
    "intel_pmu_reset",
    "intel_mediated_pmu_load",
    "intel_mediated_pmu_put",
    "kvm_need_rdpmc_intercept"
  ],
  "Reasoning": "The patch modifies KVM's PMU implementation on x86 to support MSR_PERF_METRICS and fixed counter 3 (Topdown slots). It also refactors RDPMC emulation and adds checks for KVM RDPMC passthrough support. This is core virtualization logic reachable from the guest when accessing PMU MSRs or executing RDPMC, and should be fuzzed.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer.
Your job is to review a provided patch series and determine
if it makes functional changes to the kernel that should be fuzzed.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively
use your code access tools to examine the actual source code and confirm any assumptions.

Return WorthFuzzing=false if the patch only contains:
- Modifications to Documentation/, Kconfig files, or code comments.
- Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints.
- Changes to numeric constants or macros that do not functionally alter execution flow.
- Code paths that are impossible to reach in virtualized environments like GCE or QEMU,
  even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim).
- Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed,
  ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU.
- Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove)
  that are executed only during PCI hot-unplug or sysfs driver unbind operations.

If it modifies reachable core kernel logic, drivers, or architectures, use your code search
tools to verify the code can be executed, then return WorthFuzzing=true.

When returning WorthFuzzing=true, you MUST ALSO:
1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols.
   Avoid listing generic hot-path functions to prevent skewed test distributions.
   Prefer non-static, non-inlined API entrypoint functions over internal static helper functions
   (which are inlined by the compiler and do not have distinct symbol addresses).
2. Identify any specific CONFIG_ options required to properly test this new/modified feature.
   Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions.
   Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs
   needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs
   output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit d1f77d992311dff5dcf3abd258343910c039cae6
Author: syz-cluster <triage@syzkaller.com>
Date:   Tue Jul 28 17:41:14 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h
index 4a223c2793e3f..4b50ed058aed3 100644
--- a/arch/x86/include/asm/kvm-x86-pmu-ops.h
+++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h
@@ -13,7 +13,7 @@
  * KVM_X86_PMU_OP_OPTIONAL() can be used for those functions that can have
  * a NULL definition.
  */
-KVM_X86_PMU_OP(rdpmc_ecx_to_pmc)
+KVM_X86_PMU_OP(emulate_rdpmc)
 KVM_X86_PMU_OP(msr_idx_to_pmc)
 KVM_X86_PMU_OP_OPTIONAL(check_rdpmc_early)
 KVM_X86_PMU_OP(is_valid_msr)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index b517257a63153..2a92a75af7e65 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -347,6 +347,7 @@ struct kvm_host_values {
 	u64 xss;
 	u64 s_cet;
 	u64 arch_capabilities;
+	u64 perf_capabilities;
 };
 extern struct kvm_host_values kvm_host;
 
@@ -615,7 +616,7 @@ struct kvm_pmc {
 #define KVM_MAX_NR_GP_COUNTERS		KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
 						KVM_MAX_NR_AMD_GP_COUNTERS)
 
-#define KVM_MAX_NR_INTEL_FIXED_COUNTERS	3
+#define KVM_MAX_NR_INTEL_FIXED_COUNTERS	4
 #define KVM_MAX_NR_AMD_FIXED_COUNTERS	0
 #define KVM_MAX_NR_FIXED_COUNTERS	KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \
 						KVM_MAX_NR_AMD_FIXED_COUNTERS)
@@ -635,6 +636,7 @@ struct kvm_pmu {
 	u64 global_status_rsvd;
 	u64 reserved_bits;
 	u64 raw_event_mask;
+	u64 perf_metrics;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
 
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 18c4be75e9271..fdcaeb6c8352a 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT		0xf00
 #define PERF_CAP_FW_WRITES		BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE		BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS		BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO	BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK		(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
 					 PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index c230b18d87e38..c751a8dbd45dc 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -228,9 +228,9 @@ static const u32 msrs_to_save_base[] = {
 
 static const u32 msrs_to_save_pmu[] = {
 	MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
-	MSR_ARCH_PERFMON_FIXED_CTR0 + 2,
+	MSR_ARCH_PERFMON_FIXED_CTR2, MSR_ARCH_PERFMON_FIXED_CTR3,
 	MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
-	MSR_CORE_PERF_GLOBAL_CTRL,
+	MSR_CORE_PERF_GLOBAL_CTRL, MSR_PERF_METRICS,
 	MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,
 
 	/* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */
@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
 			return;
 		break;
+	case MSR_PERF_METRICS:
+		if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS))
+			return;
+		break;
 	case MSR_ARCH_PERFMON_PERFCTR0 ...
 	     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
 		if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
@@ -2688,7 +2692,7 @@ void kvm_init_msr_lists(void)
 {
 	unsigned i;
 
-	BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,
+	BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 4,
 			 "Please update the fixed PMCs in msrs_to_save_pmu[]");
 
 	num_msrs_to_save = 0;
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 62d0ed99ebe98..4d8354c6e7d81 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -19,6 +19,7 @@
 #include <linux/moduleparam.h>
 #include <asm/perf_event.h>
 #include <asm/cpu_device_id.h>
+#include <asm/cpuid/api.h>
 #include "x86.h"
 #include "cpuid.h"
 #include "lapic.h"
@@ -76,6 +77,21 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] = {
 	{}
 };
 
+/*
+ * CPUs whose RDPMC encodings have been audited for KVM RDPMC
+ * passthrough support.
+ */
+static const struct x86_cpu_id kvm_rdpmc_known_cpus[] = {
+	X86_MATCH_VFM(INTEL_ATOM_DARKMONT_X, NULL),
+	X86_MATCH_VFM(INTEL_LUNARLAKE_M, NULL),
+	X86_MATCH_VFM(INTEL_NOVALAKE, NULL),
+	X86_MATCH_VFM(INTEL_NOVALAKE_L, NULL),
+	X86_MATCH_VFM(INTEL_PANTHERLAKE_L, NULL),
+	X86_MATCH_VFM(INTEL_PANTHERLAKE_R, NULL),
+	X86_MATCH_VFM(INTEL_WILDCATLAKE_L, NULL),
+	{}
+};
+
 /* NOTE:
  * - Each perf counter is defined as "struct kvm_pmc";
  * - There are two types of perf counters: general purpose (gp) and fixed.
@@ -99,7 +115,8 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] = {
  *        all perf counters (both gp and fixed). The mapping relationship
  *        between pmc and perf counters is as the following:
  *        * Intel: [0 .. KVM_MAX_NR_INTEL_GP_COUNTERS-1] <=> gp counters
- *                 [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + 2] <=> fixed
+ *                 [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX +
+ *                  KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1] <=> fixed
  *        * AMD:   [0 .. AMD64_NUM_COUNTERS-1] and, for families 15H
  *          and later, [0 .. AMD64_NUM_COUNTERS_CORE-1] <=> gp counters
  */
@@ -134,6 +151,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 {
 	bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
 	int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS;
+	union cpuid10_edx edx;
+	u32 eax, ebx, ecx;
 
 	/*
 	 * Hybrid PMUs don't play nice with virtualization without careful
@@ -181,6 +200,19 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 	kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
 					     KVM_MAX_NR_FIXED_COUNTERS);
 
+	/*
+	 * Currently, KVM doesn't support non-contiguous fixed counters; make
+	 * sure only contiguous ones are retained in kvm_pmu_cap.
+	 */
+	if (kvm_host_pmu.version >= 5) {
+		cpuid(0xa, &eax, &ebx, &ecx, &edx.full);
+		if (kvm_pmu_cap.num_counters_fixed > edx.split.num_counters_fixed)
+			kvm_pmu_cap.num_counters_fixed = edx.split.num_counters_fixed;
+	}
+
+	if (!enable_mediated_pmu && kvm_pmu_cap.num_counters_fixed > 3)
+		kvm_pmu_cap.num_counters_fixed = 3;
+
 	kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
 		perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
 	kvm_pmu_eventsel.BRANCH_INSTRUCTIONS_RETIRED =
@@ -752,8 +784,6 @@ static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
-	struct kvm_pmc *pmc;
-	u64 mask = ~0ull;
 
 	if (!pmu->version)
 		return 1;
@@ -761,17 +791,12 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
 	if (is_vmware_backdoor_pmc(idx))
 		return kvm_pmu_rdpmc_vmware(vcpu, idx, data);
 
-	pmc = kvm_pmu_call(rdpmc_ecx_to_pmc)(vcpu, idx, &mask);
-	if (!pmc)
-		return 1;
-
 	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_PCE) &&
 	    (kvm_x86_call(get_cpl)(vcpu) != 0) &&
 	    kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))
 		return 1;
 
-	*data = pmc_read_counter(pmc) & mask;
-	return 0;
+	return kvm_pmu_call(emulate_rdpmc)(vcpu, idx, data);
 }
 
 static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
@@ -797,6 +822,26 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+	return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+		!kvm_vcpu_has_perf_metrics(vcpu);
+}
+
+static bool kvm_rdpmc_encoding_supported(void)
+{
+	/* KVM understands all RDPMC encodings prior to PMU v6. */
+	if (kvm_host_pmu.version < 6)
+		return true;
+
+	/*
+	 * Future PMU v6 implementations and future PMU versions require RDPMC
+	 * interception until their RDPMC encodings are audited and supported
+	 * by KVM.
+	 */
+	return x86_match_cpu(kvm_rdpmc_known_cpus);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -808,7 +853,11 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 	if (enable_vmware_backdoor)
 		return true;
 
+	if (!kvm_rdpmc_encoding_supported())
+		return true;
+
 	return kvm_need_any_pmc_intercept(vcpu) ||
+	       kvm_need_perf_metrics_intercept(vcpu) ||
 	       pmu->counter_bitmask[KVM_PMC_GP] != (BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) ||
 	       pmu->counter_bitmask[KVM_PMC_FIXED] != (BIT_ULL(kvm_host_pmu.bit_width_fixed) - 1);
 }
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index a5821d7c87f93..cdbefda844b97 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -24,8 +24,8 @@
 #define KVM_FIXED_PMC_BASE_IDX INTEL_PMC_IDX_FIXED
 
 struct kvm_pmu_ops {
-	struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
-		unsigned int idx, u64 *mask);
+	int (*emulate_rdpmc)(struct kvm_vcpu *vcpu, unsigned int idx,
+			     u64 *data);
 	struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr);
 	int (*check_rdpmc_early)(struct kvm_vcpu *vcpu, unsigned int idx);
 	bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
@@ -271,6 +271,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu)
 				  X86_PMC_IDX_MAX);
 }
 
+static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu)
+{
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM))
+		return 0;
+
+	return vcpu->arch.perf_capabilities;
+}
+
+static inline bool kvm_vcpu_has_perf_metrics(struct kvm_vcpu *vcpu)
+{
+	return kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PERF_METRICS;
+}
+
 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
 int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7ac..0517fd4bbcd7a 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -84,10 +84,15 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
 }
 
 /* idx is the ECX register of RDPMC instruction */
-static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
-	unsigned int idx, u64 *mask)
+static int amd_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 *data)
 {
-	return amd_pmu_get_pmc(vcpu_to_pmu(vcpu), idx);
+	struct kvm_pmc *pmc = amd_pmu_get_pmc(vcpu_to_pmu(vcpu), idx);
+
+	if (!pmc)
+		return 1;
+
+	*data = pmc_read_counter(pmc);
+	return 0;
 }
 
 static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr)
@@ -302,7 +307,7 @@ static bool amd_pmc_is_disabled_in_current_mode(struct kvm_pmc *pmc)
 }
 
 struct kvm_pmu_ops amd_pmu_ops __initdata = {
-	.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,
+	.emulate_rdpmc = amd_emulate_rdpmc,
 	.msr_idx_to_pmc = amd_msr_idx_to_pmc,
 	.check_rdpmc_early = amd_check_rdpmc_early,
 	.is_valid_msr = amd_is_valid_msr,
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 0635e92471c89..1aa3b29359544 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -688,6 +688,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
 	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);
 	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);
+
+	nested_vmx_merge_msr_bitmaps_rw(MSR_PERF_METRICS);
 }
 
 /*
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index a73a9515d96cd..d1c0e36954c1c 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -30,6 +30,7 @@
  */
 #define INTEL_RDPMC_GP		0
 #define INTEL_RDPMC_FIXED	INTEL_PMC_FIXED_RDPMC_BASE
+#define INTEL_RDPMC_METRICS	INTEL_PMC_FIXED_RDPMC_METRICS
 
 #define INTEL_RDPMC_TYPE_MASK	GENMASK(31, 16)
 #define INTEL_RDPMC_INDEX_MASK	GENMASK(15, 0)
@@ -84,14 +85,13 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
 	}
 }
 
-static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
-					    unsigned int idx, u64 *mask)
+static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx,
+			       u64 *data)
 {
 	unsigned int type = idx & INTEL_RDPMC_TYPE_MASK;
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
-	struct kvm_pmc *counters;
+	struct kvm_pmc *counters, *pmc;
 	unsigned int num_counters;
-	u64 bitmask;
 
 	/*
 	 * The encoding of ECX for RDPMC is different for architectural versus
@@ -104,7 +104,9 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 	 * as KVM doesn't support such PMUs.
 	 */
 	if (WARN_ON_ONCE(!pmu->version))
-		return NULL;
+		return 1;
+
+	idx &= INTEL_RDPMC_INDEX_MASK;
 
 	/*
 	 * General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs
@@ -118,23 +120,34 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 	case INTEL_RDPMC_FIXED:
 		counters = pmu->fixed_counters;
 		num_counters = pmu->nr_arch_fixed_counters;
-		bitmask = pmu->counter_bitmask[KVM_PMC_FIXED];
 		break;
 	case INTEL_RDPMC_GP:
 		counters = pmu->gp_counters;
 		num_counters = pmu->nr_arch_gp_counters;
-		bitmask = pmu->counter_bitmask[KVM_PMC_GP];
 		break;
+	case INTEL_RDPMC_METRICS:
+		if (!kvm_vcpu_has_perf_metrics(vcpu))
+			return 1;
+
+		/*
+		 * The index in ECX[15:0] is implementation specific, but no
+		 * platform currently supports a non-zero index.
+		 */
+		if (idx)
+			return 1;
+
+		*data = pmu->perf_metrics;
+		return 0;
 	default:
-		return NULL;
+		return 1;
 	}
 
-	idx &= INTEL_RDPMC_INDEX_MASK;
 	if (idx >= num_counters)
-		return NULL;
+		return 1;
 
-	*mask &= bitmask;
-	return &counters[array_index_nospec(idx, num_counters)];
+	pmc = &counters[array_index_nospec(idx, num_counters)];
+	*data = pmc_read_counter(pmc);
+	return 0;
 }
 
 static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)
@@ -188,14 +201,16 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
 		return kvm_pmu_has_perf_global_ctrl(pmu);
+	case MSR_PERF_METRICS:
+		return kvm_vcpu_has_perf_metrics(vcpu);
 	case MSR_IA32_PEBS_ENABLE:
-		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
+		ret = kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PEBS_FORMAT;
 		break;
 	case MSR_IA32_DS_AREA:
 		ret = guest_cpu_cap_has(vcpu, X86_FEATURE_DS);
 		break;
 	case MSR_PEBS_DATA_CFG:
-		perf_capabilities = vcpu_get_perf_capabilities(vcpu);
+		perf_capabilities = kvm_vcpu_get_perf_caps(vcpu);
 		ret = (perf_capabilities & PERF_CAP_PEBS_BASELINE) &&
 			((perf_capabilities & PERF_CAP_PEBS_FORMAT) > 3);
 		break;
@@ -345,6 +360,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
 		msr_info->data = pmu->fixed_ctr_ctrl;
 		break;
+	case MSR_PERF_METRICS:
+		msr_info->data = pmu->perf_metrics;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		msr_info->data = pmu->pebs_enable;
 		break;
@@ -394,6 +412,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (pmu->fixed_ctr_ctrl != data)
 			reprogram_fixed_counters(pmu, data);
 		break;
+	case MSR_PERF_METRICS:
+		/*
+		 * Bits [63:32] are ignored on hardware that supports only
+		 * level-1 metrics, but may be valid on hardware that supports
+		 * level-2 metrics. Preserve guest writes verbatim.
+		 */
+		pmu->perf_metrics = data;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		if (data & pmu->pebs_enable_rsvd)
 			return 1;
@@ -464,11 +490,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  * different perf_event is already utilizing the requested counter, but the end
  * result is the same (ignoring the fact that using a general purpose counter
  * will likely exacerbate counter contention).
- *
- * Forcibly inlined to allow asserting on @index at build time, and there should
- * never be more than one user.
  */
-static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
+static u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 {
 	const enum perf_hw_id fixed_pmc_perf_ids[] = {
 		[0] = PERF_COUNT_HW_INSTRUCTIONS,
@@ -477,8 +500,13 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 	};
 	u64 eventsel;
 
-	BUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_perf_ids) != KVM_MAX_NR_INTEL_FIXED_COUNTERS);
-	BUILD_BUG_ON(index >= KVM_MAX_NR_INTEL_FIXED_COUNTERS);
+	/*
+	 * Fixed counters 3 and above don't have a corresponding generic
+	 * hardware perf event, and KVM does not intend to emulate them on
+	 * non-mediated vPMU.
+	 */
+	if (index >= ARRAY_SIZE(fixed_pmc_perf_ids))
+		return 0;
 
 	/*
 	 * Yell if perf reports support for a fixed counter but perf doesn't
@@ -548,7 +576,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
 	}
 
-	perf_capabilities = vcpu_get_perf_capabilities(vcpu);
+	perf_capabilities = kvm_vcpu_get_perf_caps(vcpu);
 	if (intel_pmu_lbr_is_compatible(vcpu) &&
 	    (perf_capabilities & PERF_CAP_LBR_FMT))
 		memcpy(&lbr_desc->records, &vmx_lbr_caps, sizeof(vmx_lbr_caps));
@@ -574,6 +602,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	counter_rsvd = ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) |
 			 ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
 	pmu->global_ctrl_rsvd = counter_rsvd;
+	if (perf_capabilities & PERF_CAP_PERF_METRICS)
+		pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
 
 	/*
 	 * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)
@@ -630,6 +660,9 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
 
 static void intel_pmu_reset(struct kvm_vcpu *vcpu)
 {
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+	pmu->perf_metrics = 0;
 	intel_pmu_release_guest_lbr_event(vcpu);
 }
 
@@ -801,6 +834,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	u64 global_status, toggle;
 
+	if (kvm_vcpu_has_perf_metrics(vcpu))
+		wrmsrq(MSR_PERF_METRICS, pmu->perf_metrics);
+
 	rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, global_status);
 	toggle = pmu->global_status ^ global_status;
 	if (global_status & toggle)
@@ -829,10 +865,23 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)
 	 */
 	if (pmu->fixed_ctr_ctrl_hw)
 		wrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
+
+	if (kvm_vcpu_has_perf_metrics(vcpu)) {
+		pmu->perf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);
+		/*
+		 * The SDM requires restoring fixed counter 3 before
+		 * PERF_METRICS. However, this path writes 0 to PERF_METRICS
+		 * before fixed counter 3. For this all-zero case, the
+		 * resulting hardware state is therefore the same regardless
+		 * of write order.
+		 */
+		if (pmu->perf_metrics)
+			wrmsrq(MSR_PERF_METRICS, 0);
+	}
 }
 
 struct kvm_pmu_ops intel_pmu_ops __initdata = {
-	.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,
+	.emulate_rdpmc = intel_emulate_rdpmc,
 	.msr_idx_to_pmc = intel_msr_idx_to_pmc,
 	.is_valid_msr = intel_is_valid_msr,
 	.get_msr = intel_pmu_get_msr,
diff --git a/arch/x86/kvm/vmx/pmu_intel.h b/arch/x86/kvm/vmx/pmu_intel.h
index 5d9357640aa18..afdbbc9991d6d 100644
--- a/arch/x86/kvm/vmx/pmu_intel.h
+++ b/arch/x86/kvm/vmx/pmu_intel.h
@@ -6,17 +6,9 @@
 
 #include "cpuid.h"
 
-static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu)
-{
-	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM))
-		return 0;
-
-	return vcpu->arch.perf_capabilities;
-}
-
 static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu)
 {
-	return (vcpu_get_perf_capabilities(vcpu) & PERF_CAP_FW_WRITES) != 0;
+	return (kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_FW_WRITES) != 0;
 }
 
 bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 3681d565f1778..25d70645f9ac6 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4273,6 +4273,10 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 				  MSR_TYPE_RW, intercept);
 	vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
 				  MSR_TYPE_RW, intercept);
+
+	intercept = !has_mediated_pmu || !kvm_vcpu_has_perf_metrics(vcpu);
+	vmx_set_intercept_for_msr(vcpu, MSR_PERF_METRICS,
+				  MSR_TYPE_RW, intercept);
 }
 
 static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
@@ -8050,14 +8054,10 @@ void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 static __init u64 vmx_get_perf_capabilities(void)
 {
 	u64 perf_cap = PERF_CAP_FW_WRITES;
-	u64 host_perf_cap = 0;
 
 	if (!enable_pmu)
 		return 0;
 
-	if (boot_cpu_has(X86_FEATURE_PDCM))
-		rdmsrq(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
-
 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) &&
 	    !enable_mediated_pmu) {
 		x86_perf_get_lbr(&vmx_lbr_caps);
@@ -8070,11 +8070,11 @@ static __init u64 vmx_get_perf_capabilities(void)
 		if (!vmx_lbr_caps.has_callstack)
 			memset(&vmx_lbr_caps, 0, sizeof(vmx_lbr_caps));
 		else if (vmx_lbr_caps.nr)
-			perf_cap |= host_perf_cap & PERF_CAP_LBR_FMT;
+			perf_cap |= kvm_host.perf_capabilities & PERF_CAP_LBR_FMT;
 	}
 
 	if (vmx_pebs_supported()) {
-		perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;
+		perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PEBS_MASK;
 
 		/*
 		 * Disallow adaptive PEBS as it is functionally broken, can be
@@ -8099,6 +8099,9 @@ static __init u64 vmx_get_perf_capabilities(void)
 		perf_cap &= ~PERF_CAP_PEBS_BASELINE;
 	}
 
+	if (enable_mediated_pmu)
+		perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS;
+
 	return perf_cap;
 }
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0626e835e9eb7..e0114f248a9ef 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7033,6 +7033,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
 		rdmsrq(MSR_IA32_ARCH_CAPABILITIES, kvm_host.arch_capabilities);
 
+	if (boot_cpu_has(X86_FEATURE_PDCM))
+		rdmsrq_safe(MSR_IA32_PERF_CAPABILITIES,
+			    &kvm_host.perf_capabilities);
+
 	WARN_ON_ONCE(kvm_nr_uret_msrs);
 
 	r = ops->hardware_setup();
diff --git a/tools/arch/x86/include/asm/msr-index.h b/tools/arch/x86/include/asm/msr-index.h
index eff29645719bc..e7745e2cd543a 100644
--- a/tools/arch/x86/include/asm/msr-index.h
+++ b/tools/arch/x86/include/asm/msr-index.h
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT		0xf00
 #define PERF_CAP_FW_WRITES		BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE		BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS		BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO	BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK		(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
 					 PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h
index 608ed83d7c6a6..6c19503e0bb7d 100644
--- a/tools/testing/selftests/kvm/include/x86/pmu.h
+++ b/tools/testing/selftests/kvm/include/x86/pmu.h
@@ -52,6 +52,9 @@
 /* Fixed PMC controls, Intel only. */
 #define FIXED_PMC_GLOBAL_CTRL_ENABLE(_idx)	BIT_ULL((32 + (_idx)))
 
+/* PERF_METRICS enable, Intel only. */
+#define PERF_METRICS_GLOBAL_CTRL_ENABLE		BIT_ULL(48)
+
 #define FIXED_PMC_KERNEL			BIT_ULL(0)
 #define FIXED_PMC_USER				BIT_ULL(1)
 #define FIXED_PMC_ANYTHREAD			BIT_ULL(2)
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index dc6afac3aa919..8294a0089a85a 100644
--- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2023, Tencent, Inc.
  */
 #include <x86intrin.h>
+#include <linux/bitfield.h>
 
 #include "pmu.h"
 #include "processor.h"
@@ -241,17 +242,20 @@ do {										\
 	);									\
 } while (0)
 
-#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP)		\
+#define GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP)				\
 do {										\
-	wrmsr(_pmc_msr, 0);							\
-										\
 	if (this_cpu_has(X86_FEATURE_CLFLUSHOPT))				\
 		GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt %[m]", FEP);	\
 	else if (this_cpu_has(X86_FEATURE_CLFLUSH))				\
 		GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush  %[m]", FEP);	\
 	else									\
 		GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP);		\
-										\
+} while (0)
+
+#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP)		\
+do {										\
+	wrmsr(_pmc_msr, 0);							\
+	GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP);				\
 	guest_assert_event_count(_idx, _pmc, _pmc_msr);				\
 } while (0)
 
@@ -361,7 +365,7 @@ static void test_arch_events(u8 pmu_version, u64 perf_capabilities,
  * other than PMCs in the future.
  */
 #define MAX_NR_GP_COUNTERS	8
-#define MAX_NR_FIXED_COUNTERS	3
+#define MAX_NR_FIXED_COUNTERS	4
 
 #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector)		\
 __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector,			\
@@ -580,11 +584,81 @@ static void test_fixed_counters(u8 pmu_version, u64 perf_capabilities,
 	kvm_vm_free(vm);
 }
 
+static void __guest_test_perf_metrics(void)
+{
+	int retiring, bad_spec, fe_bound, be_bound, sum;
+	u64 global_ctrl, metrics;
+
+	if ((guest_get_pmu_version() < 2) ||	/* Does guest have GLOBAL_CTRL? */
+	    !this_cpu_has(X86_FEATURE_PDCM) ||
+	    !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+		return;
+
+	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+	wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+	wrmsr(MSR_PERF_METRICS, 0);
+
+	/* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+	wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+	global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
+		      PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+	GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+	/* Check test results. */
+	metrics = rdmsr(MSR_PERF_METRICS);
+	retiring = FIELD_GET(GENMASK_ULL(7, 0), metrics);
+	bad_spec = FIELD_GET(GENMASK_ULL(15, 8), metrics);
+	fe_bound = FIELD_GET(GENMASK_ULL(23, 16), metrics);
+	be_bound = FIELD_GET(GENMASK_ULL(31, 24), metrics);
+
+	/*
+	 * Be conservative: the measured payload definitely retires work, so
+	 * Retiring should be non-zero.
+	 */
+	GUEST_ASSERT_NE(metrics, 0ULL);
+	GUEST_ASSERT_NE(retiring, 0ULL);
+
+	/*
+	 * Each level-1 Topdown metric is an integer fraction of 0xff.
+	 * A +/-3 error margin is chosen for a loose sanity check.
+	 */
+	sum = retiring + bad_spec + fe_bound + be_bound;
+	GUEST_ASSERT(sum >= 0xfc && sum <= 0x102);
+
+	/* Sanity check after PERF_METRICS disabled. */
+	__asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
+	GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);
+	wrmsr(MSR_PERF_METRICS, 0xdeaddead);
+
+	guest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0xdeaddead);
+}
+
+static void guest_test_perf_metrics(void)
+{
+	__guest_test_perf_metrics();
+	GUEST_DONE();
+}
+
+static void test_perf_metrics(u8 pmu_version, u64 perf_capabilities)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_perf_metrics,
+					 pmu_version, perf_capabilities);
+
+	run_vcpu(vcpu);
+
+	kvm_vm_free(vm);
+}
+
 static void test_intel_counters(void)
 {
 	u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
 	u8 nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
 	u8 pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
+	u64 advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
 	unsigned int i;
 	u8 v, j;
 	u32 k;
@@ -592,6 +666,7 @@ static void test_intel_counters(void)
 	const u64 perf_caps[] = {
 		0,
 		PMU_CAP_FW_WRITES,
+		PERF_CAP_PERF_METRICS,
 	};
 
 	/*
@@ -649,6 +724,10 @@ static void test_intel_counters(void)
 			if (!kvm_has_perf_caps && perf_caps[i])
 				continue;
 
+			/* Ignore unsupported features. */
+			if (perf_caps[i] & ~advertised_perf_caps)
+				continue;
+
 			pr_info("Testing arch events, PMU version %u, perf_caps = %lx\n",
 				v, perf_caps[i]);
 
@@ -675,6 +754,11 @@ static void test_intel_counters(void)
 				for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
 					test_fixed_counters(v, perf_caps[i], j, k);
 			}
+
+			pr_info("Testing Perf Metrics, PMU version %u, perf_caps = %lx\n",
+				v, perf_caps[i]);
+
+			test_perf_metrics(v, perf_caps[i]);
 		}
 	}
 }

3/2 2026/07/28 17:41 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 15117
							output: 135
							thoughts: 425
						

Thoughts:
**Fuzzing Target Identification: KVM PMU Patch**

Okay, so I've got this patch here that's making changes to the KVM x86 PMU implementation. Specifically, it's about adding support for `MSR_PERF_METRICS` and enabling fixed counter 3, which is key for those Topdown slots. Interesting. It also looks like they've refactored `rdpmc_ecx_to_pmc` into `emulate_rdpmc`, and added `kvm_rdpmc_known_cpus` for checking RDPMC passthrough CPU support. The real meat of the changes seem to be in `arch/x86/kvm/vmx/pmu_intel.c` to handle the `INTEL_RDPMC_METRICS` part. And, of course, there's a new test added in `tools/testing/selftests/kvm/x86/pmu_counters_test.c` to validate it all.

Now, the question is, is this reachable? Absolutely. When a guest starts messing with PMU MSRs or executing RDPMC instructions, this code will be hit. This is core virtualization logic, exactly the kind of thing that can bite you if it's not rock solid. This is definitely worth a fuzzing campaign.

Let's nail down the critical symbols. Okay, it seems that the important code that is being worked on is:
*   `kvm_pmu_rdpmc`
*   `kvm_need_perf_global_ctrl_intercept`
*   `kvm_need_rdpmc_intercept`
*   `amd_emulate_rdpmc`
*   `intel_emulate_rdpmc`
*   `intel_pmu_get_msr`
*   `intel_pmu_set_msr`
*   `intel_pmu_refresh`
*   `intel_pmu_reset`
*   `intel_mediated_pmu_load`
*   `intel_mediated_pmu_put`
*   `vmx_get_perf_capabilities`

Alright, the main focus should be on `arch/x86/kvm/pmu.c` and `arch/x86/kvm/vmx/pmu_intel.c`. I need to dig into those, understand the new logic, and start thinking about crafting some clever test cases to expose any vulnerabilities. This looks like a promising target.



4/2 2026/07/28 17:41 tool grepper
0m Args:
{
  "Expression": "^(int|void|bool|u64|struct kvm_pmc \\*)\\s+(kvm_pmu_rdpmc|kvm_need_rdpmc_intercept|amd_emulate_rdpmc|intel_emulate_rdpmc|intel_pmu_get_msr|intel_pmu_set_msr|intel_pmu_refresh|intel_pmu_reset|intel_mediated_pmu_load|intel_mediated_pmu_put|vmx_get_perf_capabilities)"
}

Results:
{
  "Output": "arch/x86/kvm/pmu.c=761=static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\n--\narch/x86/kvm/pmu.c-783-\narch/x86/kvm/pmu.c:784:int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\narch/x86/kvm/pmu.c-785-{\n--\narch/x86/kvm/pmu.c=831=static bool kvm_rdpmc_encoding_supported(void)\n--\narch/x86/kvm/pmu.c-844-\narch/x86/kvm/pmu.c:845:bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-846-{\n--\narch/x86/kvm/pmu.h=287=void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);\narch/x86/kvm/pmu.h:288:int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);\narch/x86/kvm/pmu.h-289-int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);\n--\narch/x86/kvm/pmu.h=304=bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu);\narch/x86/kvm/pmu.h:305:bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu);\narch/x86/kvm/pmu.h-306-\n"
}

5/2 2026/07/28 17:41 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 16748
							output: 94
							thoughts: 18
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Analyzing Potential Emulation Entry Points**

Okay, so I need to dig into this. My initial thought is to use `grep`. I want to look for functions that might be related to emulating the `rdpmc` instruction, or anything else that smacks of similar emulation logic. Specifically, I'll be looking for `intel_emulate_rdpmc` or other function names that could be involved in handling instructions not directly supported by the current architecture, particularly within an Intel context. I need to understand how these instructions are trapped, emulated, or handled in software, especially when virtualization or security features come into play. It's a key starting point for understanding performance monitoring in a potentially restricted environment.



6/2 2026/07/28 17:41 tool grepper
0m Args:
{
  "Expression": "(amd_emulate_rdpmc|intel_emulate_rdpmc|intel_pmu_get_msr|intel_pmu_set_msr|intel_pmu_refresh|intel_pmu_reset|intel_mediated_pmu_load|intel_mediated_pmu_put|vmx_get_perf_capabilities)"
}

Results:
{
  "Output": "arch/x86/events/intel/core.c=3436=static u64 intel_pmu_update(struct perf_event *event)\n--\narch/x86/events/intel/core.c-3443-\narch/x86/events/intel/core.c:3444:static void intel_pmu_reset(void)\narch/x86/events/intel/core.c-3445-{\n--\narch/x86/events/intel/core.c=3689=static int intel_pmu_handle_irq(struct pt_regs *regs)\n--\narch/x86/events/intel/core.c-3734-\t\t}\narch/x86/events/intel/core.c:3735:\t\tintel_pmu_reset();\narch/x86/events/intel/core.c-3736-\t\tgoto done;\n--\narch/x86/kvm/svm/pmu.c=76=static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)\n--\narch/x86/kvm/svm/pmu.c-86-/* idx is the ECX register of RDPMC instruction */\narch/x86/kvm/svm/pmu.c:87:static int amd_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 *data)\narch/x86/kvm/svm/pmu.c-88-{\n--\narch/x86/kvm/svm/pmu.c=309=struct kvm_pmu_ops amd_pmu_ops __initdata = {\narch/x86/kvm/svm/pmu.c:310:\t.emulate_rdpmc = amd_emulate_rdpmc,\narch/x86/kvm/svm/pmu.c-311-\t.msr_idx_to_pmc = amd_msr_idx_to_pmc,\n--\narch/x86/kvm/vmx/pmu_intel.c=58=static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)\n--\narch/x86/kvm/vmx/pmu_intel.c-87-\narch/x86/kvm/vmx/pmu_intel.c:88:static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx,\narch/x86/kvm/vmx/pmu_intel.c-89-\t\t\t       u64 *data)\n--\narch/x86/kvm/vmx/pmu_intel.c=314=static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/vmx/pmu_intel.c-352-\narch/x86/kvm/vmx/pmu_intel.c:353:static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\narch/x86/kvm/vmx/pmu_intel.c-354-{\n--\narch/x86/kvm/vmx/pmu_intel.c-398-\narch/x86/kvm/vmx/pmu_intel.c:399:static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\narch/x86/kvm/vmx/pmu_intel.c-400-{\n--\narch/x86/kvm/vmx/pmu_intel.c=520=static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)\n--\narch/x86/kvm/vmx/pmu_intel.c-527-\narch/x86/kvm/vmx/pmu_intel.c:528:static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/pmu_intel.c-529-{\n--\narch/x86/kvm/vmx/pmu_intel.c=632=static void intel_pmu_init(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/pmu_intel.c-660-\narch/x86/kvm/vmx/pmu_intel.c:661:static void intel_pmu_reset(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/pmu_intel.c-662-{\n--\narch/x86/kvm/vmx/pmu_intel.c=826=static void intel_pmu_write_global_ctrl(u64 global_ctrl)\n--\narch/x86/kvm/vmx/pmu_intel.c-831-\narch/x86/kvm/vmx/pmu_intel.c:832:static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/pmu_intel.c-833-{\n--\narch/x86/kvm/vmx/pmu_intel.c-849-\narch/x86/kvm/vmx/pmu_intel.c:850:static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/pmu_intel.c-851-{\n--\narch/x86/kvm/vmx/pmu_intel.c=883=struct kvm_pmu_ops intel_pmu_ops __initdata = {\narch/x86/kvm/vmx/pmu_intel.c:884:\t.emulate_rdpmc = intel_emulate_rdpmc,\narch/x86/kvm/vmx/pmu_intel.c-885-\t.msr_idx_to_pmc = intel_msr_idx_to_pmc,\narch/x86/kvm/vmx/pmu_intel.c-886-\t.is_valid_msr = intel_is_valid_msr,\narch/x86/kvm/vmx/pmu_intel.c:887:\t.get_msr = intel_pmu_get_msr,\narch/x86/kvm/vmx/pmu_intel.c:888:\t.set_msr = intel_pmu_set_msr,\narch/x86/kvm/vmx/pmu_intel.c:889:\t.refresh = intel_pmu_refresh,\narch/x86/kvm/vmx/pmu_intel.c-890-\t.init = intel_pmu_init,\narch/x86/kvm/vmx/pmu_intel.c:891:\t.reset = intel_pmu_reset,\narch/x86/kvm/vmx/pmu_intel.c-892-\t.deliver_pmi = intel_pmu_deliver_pmi,\n--\narch/x86/kvm/vmx/pmu_intel.c-895-\t.is_mediated_pmu_supported = intel_pmu_is_mediated_pmu_supported,\narch/x86/kvm/vmx/pmu_intel.c:896:\t.mediated_load = intel_mediated_pmu_load,\narch/x86/kvm/vmx/pmu_intel.c:897:\t.mediated_put = intel_mediated_pmu_put,\narch/x86/kvm/vmx/pmu_intel.c-898-\t.write_global_ctrl = intel_pmu_write_global_ctrl,\n--\narch/x86/kvm/vmx/vmx.c=7992=void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-8053-\narch/x86/kvm/vmx/vmx.c:8054:static __init u64 vmx_get_perf_capabilities(void)\narch/x86/kvm/vmx/vmx.c-8055-{\n--\narch/x86/kvm/vmx/vmx.c=8108=static __init void vmx_set_cpu_caps(void)\n--\narch/x86/kvm/vmx/vmx.c-8129-\t\tkvm_cpu_cap_clear(X86_FEATURE_PDCM);\narch/x86/kvm/vmx/vmx.c:8130:\tkvm_caps.supported_perf_cap = vmx_get_perf_capabilities();\narch/x86/kvm/vmx/vmx.c-8131-\n"
}

7/2 2026/07/28 17:41 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 19353
							output: 34
							thoughts: 224
						

Thoughts:
**My Assessment of the KVM Patch's Fuzzing Potential**

Okay, so this patch is touching KVM's PMU implementation, specifically adding support for `MSR_PERF_METRICS` and the fixed counter 3. That immediately grabs my attention because modifying the Performance Monitoring Unit is always a good target. It also impacts `rdpmc` emulation, which is a known attack surface, so there's double the potential here.

Since this code is functional and can be accessed from the guest, it is definitely `WorthFuzzing=true`. I need to remember that.

My key focus symbols for this analysis are: `kvm_pmu_rdpmc`, `intel_emulate_rdpmc`, `amd_emulate_rdpmc`, `intel_pmu_get_msr`, `intel_pmu_set_msr`, `intel_pmu_refresh`, `intel_pmu_reset`, `intel_mediated_pmu_load`, `intel_mediated_pmu_put`, and `kvm_need_rdpmc_intercept`. Those are the entry points and the related emulation and management functions that I need to keep in mind.

I'm also going to double-check that `KVM`, `KVM_INTEL`, and `KVM_AMD` are correctly enabled in the configuration because I need to make sure that the intended code paths are active.



8/2 2026/07/28 17:41 tool grepper
0m Args:
{
  "Expression": "config KVM",
  "PathPrefix": "arch/x86/kvm/Kconfig"
}

Results:
{
  "Output": "arch/x86/kvm/Kconfig=18=if VIRTUALIZATION\narch/x86/kvm/Kconfig-19-\narch/x86/kvm/Kconfig:20:config KVM_X86\narch/x86/kvm/Kconfig-21-\tdef_tristate KVM if (KVM_INTEL != n || KVM_AMD != n)\n--\narch/x86/kvm/Kconfig-51-\narch/x86/kvm/Kconfig:52:config KVM\narch/x86/kvm/Kconfig-53-\ttristate \"Kernel-based Virtual Machine (KVM) support\"\n--\narch/x86/kvm/Kconfig-68-\narch/x86/kvm/Kconfig:69:config KVM_WERROR\narch/x86/kvm/Kconfig-70-\tbool \"Compile KVM with -Werror\"\n--\narch/x86/kvm/Kconfig-82-\narch/x86/kvm/Kconfig:83:config KVM_SW_PROTECTED_VM\narch/x86/kvm/Kconfig-84-\tbool \"Enable support for KVM software-protected VMs\"\n--\narch/x86/kvm/Kconfig-95-\narch/x86/kvm/Kconfig:96:config KVM_INTEL\narch/x86/kvm/Kconfig-97-\ttristate \"KVM for Intel (and compatible) processors support\"\n--\narch/x86/kvm/Kconfig-106-\narch/x86/kvm/Kconfig:107:config KVM_INTEL_PROVE_VE\narch/x86/kvm/Kconfig-108-        bool \"Check that guests do not receive #VE exceptions\"\n--\narch/x86/kvm/Kconfig=122=config X86_SGX_KVM\n--\narch/x86/kvm/Kconfig-133-\narch/x86/kvm/Kconfig:134:config KVM_INTEL_TDX\narch/x86/kvm/Kconfig-135-\tbool \"Intel Trust Domain Extensions (TDX) support\"\n--\narch/x86/kvm/Kconfig-145-\narch/x86/kvm/Kconfig:146:config KVM_AMD\narch/x86/kvm/Kconfig-147-\ttristate \"KVM for AMD processors support\"\n--\narch/x86/kvm/Kconfig-155-\narch/x86/kvm/Kconfig:156:config KVM_AMD_SEV\narch/x86/kvm/Kconfig-157-\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n--\narch/x86/kvm/Kconfig-171-\narch/x86/kvm/Kconfig:172:config KVM_IOAPIC\narch/x86/kvm/Kconfig-173-\tbool \"I/O APIC, PIC, and PIT emulation\"\n--\narch/x86/kvm/Kconfig-181-\narch/x86/kvm/Kconfig:182:config KVM_SMM\narch/x86/kvm/Kconfig-183-\tbool \"System Management Mode emulation\"\n--\narch/x86/kvm/Kconfig-192-\narch/x86/kvm/Kconfig:193:config KVM_HYPERV\narch/x86/kvm/Kconfig-194-\tbool \"Support for Microsoft Hyper-V emulation\"\n--\narch/x86/kvm/Kconfig-206-\narch/x86/kvm/Kconfig:207:config KVM_XEN\narch/x86/kvm/Kconfig-208-\tbool \"Support for Xen hypercall interface\"\n--\narch/x86/kvm/Kconfig-215-\narch/x86/kvm/Kconfig:216:config KVM_PROVE_MMU\narch/x86/kvm/Kconfig-217-\tbool \"Prove KVM MMU correctness\"\n--\narch/x86/kvm/Kconfig-228-\narch/x86/kvm/Kconfig:229:config KVM_EXTERNAL_WRITE_TRACKING\narch/x86/kvm/Kconfig-230-\tbool\narch/x86/kvm/Kconfig-231-\narch/x86/kvm/Kconfig:232:config KVM_MAX_NR_VCPUS\narch/x86/kvm/Kconfig-233-\tint \"Maximum number of vCPUs per KVM guest\"\n"
}

9/2 2026/07/28 17:41 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 21135
							output: 241
							thoughts: 491
						

Thoughts:
**Analyzing KVM PMU Symbols for Optimization**

Okay, so I'm looking at these KVM configurations: KVM, KVM_INTEL, and KVM_AMD. My focus is on these symbols related to PMU handling: `kvm_pmu_rdpmc`, `intel_emulate_rdpmc`, `amd_emulate_rdpmc`, `intel_pmu_get_msr`, `intel_pmu_set_msr`, `intel_pmu_refresh`, `intel_pmu_reset`, `intel_mediated_pmu_load`, `intel_mediated_pmu_put`, and `kvm_need_rdpmc_intercept`.

The goal, as I understand it, is to prefer non-static, non-inlined API entrypoints to internal static helpers. In other words, we don't want the compiler inlining functions because it makes them harder to reference.

My initial thought was to verify if `intel_emulate_rdpmc` and `amd_emulate_rdpmc` are static, and, yes, they are. But wait, the documentation says...  "Prefer non-static, non-inlined API entrypoint functions over internal static helper functions." This got me thinking.

Then I realized that `intel_emulate_rdpmc` and `amd_emulate_rdpmc`, even though declared 'static', are actually assigned to a function pointer within the `struct kvm_pmu_ops`. Because their addresses are taken this way, they *cannot* be fully inlined. The compiler has to provide a distinct symbol address for them. Therefore, even though they're 'static' in declaration, they satisfy the requirement.

I double-checked `kvm_pmu_rdpmc` as well. It's exported, so that's good. And similarly, `intel_pmu_get_msr` and `intel_pmu_set_msr` are also assigned to function pointers within `kvm_pmu_ops`.

So, the definitive list of symbols of interest, after this review, remains the same as I initially identified: `kvm_pmu_rdpmc`, `intel_emulate_rdpmc`, `amd_emulate_rdpmc`, `intel_pmu_get_msr`, `intel_pmu_set_msr`, `intel_pmu_refresh`, `intel_pmu_reset`, `intel_mediated_pmu_load`, `intel_mediated_pmu_put`, and `kvm_need_rdpmc_intercept`.

I'm confident in this assessment.  Time to "set-results".



10/2 2026/07/28 17:41 tool set-results
0m Args:
{
  "EnableConfigs": [
    "KVM",
    "KVM_INTEL",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "kvm_pmu_rdpmc",
    "intel_emulate_rdpmc",
    "amd_emulate_rdpmc",
    "intel_pmu_get_msr",
    "intel_pmu_set_msr",
    "intel_pmu_refresh",
    "intel_pmu_reset",
    "intel_mediated_pmu_load",
    "intel_mediated_pmu_put",
    "kvm_need_rdpmc_intercept"
  ],
  "Reasoning": "The patch modifies KVM's PMU implementation on x86 to support MSR_PERF_METRICS and fixed counter 3 (Topdown slots). It also refactors RDPMC emulation and adds checks for KVM RDPMC passthrough support. This is core virtualization logic reachable from the guest when accessing PMU MSRs or executing RDPMC, and should be fuzzed.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "KVM",
    "KVM_INTEL",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "kvm_pmu_rdpmc",
    "intel_emulate_rdpmc",
    "amd_emulate_rdpmc",
    "intel_pmu_get_msr",
    "intel_pmu_set_msr",
    "intel_pmu_refresh",
    "intel_pmu_reset",
    "intel_mediated_pmu_load",
    "intel_mediated_pmu_put",
    "kvm_need_rdpmc_intercept"
  ],
  "Reasoning": "The patch modifies KVM's PMU implementation on x86 to support MSR_PERF_METRICS and fixed counter 3 (Topdown slots). It also refactors RDPMC emulation and adds checks for KVM RDPMC passthrough support. This is core virtualization logic reachable from the guest when accessing PMU MSRs or executing RDPMC, and should be fuzzed.",
  "WorthFuzzing": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)