| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/11/08 23:29 | flow | patch-triage |
5mArgs:null Results: {
"EnableConfigs": [
"KVM",
"KVM_INTEL",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_refresh",
"kvm_pmu_set_msr",
"kvm_pmu_get_msr",
"intel_pmu_refresh",
"intel_pmu_set_msr",
"intel_pmu_get_msr",
"amd_pmu_refresh",
"amd_pmu_set_msr",
"kvm_mediated_pmu_load"
],
"KMSANReasoning": "The patch series refactors KVM's virtual Performance Monitoring Unit (vPMU) counter tracking to use bitmasks rather than scalar counter counts, and adds support for Intel PerfMon v5 fixed-counter enumeration (via CPUID 0AH ECX) and MSRs (MSR_CORE_PERF_GLOBAL_STATUS_SET, MSR_CORE_PERF_GLOBAL_INUSE).\n\nAnalysis regarding uninitialized memory:\n1. No uninitialized data leaks to userspace or guests:\n - In `__do_cpuid_func()`, the local unions (`cpuid10_eax`, `cpuid10_edx`, `cpuid_0x80000022_ebx`) are explicitly zero-initialized (`= { }`), and all entry register fields (`eax`, `ebx`, `ecx`, `edx`) are fully assigned before returning.\n - For newly handled MSRs, `MSR_CORE_PERF_GLOBAL_STATUS_SET` explicitly sets `msr_info-\u003edata = 0;` and `MSR_CORE_PERF_GLOBAL_INUSE` computes `data` starting from `0` in `intel_pmu_get_global_inuse()`, with all local variables initialized.\n2. Structure initialization and sizing:\n - `struct kvm_pmu` continues to be zeroed via `memset(pmu, 0, sizeof(*pmu))` in `kvm_pmu_init()`. The new union replacing `all_valid_pmc_idx` with `pmc_exists` / `pmc_exists64` maintains identical size (`X86_PMC_IDX_MAX` bits = 64 bits = `sizeof(u64)`), which is verified at compile-time via `BUILD_BUG_ON`.\n - `kvm_host_pmu` and `kvm_pmu_cap` are static global structures in BSS that are zeroed by default and completely populated during PMU initialization (`kvm_init_pmu_capability()`).\n3. Control flow and indexing:\n - Array accesses into `gp_counters` and `fixed_counters` continue to be bounded by static limits (`KVM_MAX_NR_GP_COUNTERS` and `KVM_MAX_NR_FIXED_COUNTERS`) sanitized by `array_index_nospec()`. Potential bugs would be index out-of-bounds or invalid MSR access bugs, which fall under the scope of KASAN rather than KMSAN.\n\nBecause the changes do not introduce or expose uninitialized memory reads or information leak risks, a dedicated KMSAN fuzzing session is not warranted. Standard KASAN builds are sufficient.",
"NeedsKMSAN": false,
"Reasoning": "The patch series refactors x86 KVM PMU virtualization to support non-contiguous counters and PMU v5, adds emulation for MSR_CORE_PERF_GLOBAL_STATUS_SET and MSR_CORE_PERF_GLOBAL_INUSE, and updates MSR interception logic and AnyThread deprecation handling for both Intel and AMD. This executable code is reachable via standard /dev/kvm ioctls (such as KVM_SET_CPUID2, KVM_SET_MSRS, KVM_GET_MSRS) and guest vCPU execution, making it a viable target for syzkaller fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/11/08 23:29 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit f49f6183a3b22d43cbacabe4487b395224573ec4\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Nov 8 23:29:44 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/virt/kvm/x86/errata.rst b/Documentation/virt/kvm/x86/errata.rst\nindex a9cf0e004651aa..161ba6d1beb483 100644\n--- a/Documentation/virt/kvm/x86/errata.rst\n+++ b/Documentation/virt/kvm/x86/errata.rst\n@@ -84,4 +84,12 @@ memory.\n \n Note, this erratum does not affect AMD CPUs, which fully virtualize CR0.CD in\n hardware, i.e. put the CPU caches into \"no fill\" mode when CR0.CD=1, even when\n-running in the guest.\n\\ No newline at end of file\n+running in the guest.\n+\n+IA32_PERF_GLOBAL_STATUS_RESET\n+-----------------------------\n+To reduce VM-exits on performance-critical paths, KVM passes through writes\n+to IA32_PERF_GLOBAL_STATUS_RESET (0x390) in mediated PMU mode when the guest's\n+counters match hardware, without injecting #GP on unvirtualized bits. Because\n+the MSR is read-as-zero and write-to-clear, this cannot leak host information\n+to the guest.\ndiff --git a/arch/x86/events/core.c b/arch/x86/events/core.c\nindex 8b3ea0adb9655c..990d3be5a07f95 100644\n--- a/arch/x86/events/core.c\n+++ b/arch/x86/events/core.c\n@@ -3156,10 +3156,10 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)\n \t * base PMU holds the correct number of counters for P-cores.\n \t */\n \tcap-\u003eversion\t\t= x86_pmu.version;\n-\tcap-\u003enum_counters_gp\t= x86_pmu_num_counters(NULL);\n-\tcap-\u003enum_counters_fixed\t= x86_pmu_num_counters_fixed(NULL);\n-\tcap-\u003ebit_width_gp\t= cap-\u003enum_counters_gp ? x86_pmu.cntval_bits : 0;\n-\tcap-\u003ebit_width_fixed\t= cap-\u003enum_counters_fixed ? x86_pmu.cntval_bits : 0;\n+\tcap-\u003ecntr_mask64\t= x86_pmu.cntr_mask64;\n+\tcap-\u003efixed_cntr_mask64\t= x86_pmu.fixed_cntr_mask64;\n+\tcap-\u003ebit_width_gp\t= cap-\u003ecntr_mask64 ? x86_pmu.cntval_bits : 0;\n+\tcap-\u003ebit_width_fixed\t= cap-\u003efixed_cntr_mask64 ? x86_pmu.cntval_bits : 0;\n \tcap-\u003eevents_mask\t= (unsigned int)x86_pmu.events_maskl;\n \tcap-\u003eevents_mask_len\t= x86_pmu.events_mask_len;\n \tcap-\u003epebs_ept\t\t= x86_pmu.pebs_ept;\ndiff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h\nindex 0378577d9d8bbf..93f029e1fc98be 100644\n--- a/arch/x86/include/asm/kvm_host.h\n+++ b/arch/x86/include/asm/kvm_host.h\n@@ -575,8 +575,6 @@ struct kvm_pmc {\n \n struct kvm_pmu {\n \tu8 version;\n-\tunsigned nr_arch_gp_counters;\n-\tunsigned nr_arch_fixed_counters;\n \tunsigned available_event_types;\n \tu64 fixed_ctr_ctrl;\n \tu64 fixed_ctr_ctrl_hw;\n@@ -586,7 +584,7 @@ struct kvm_pmu {\n \tu64 counter_bitmask[2];\n \tu64 global_ctrl_rsvd;\n \tu64 global_status_rsvd;\n-\tu64 reserved_bits;\n+\tu64 eventsel_rsvd;\n \tu64 raw_event_mask;\n \tstruct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];\n \tstruct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];\n@@ -600,7 +598,10 @@ struct kvm_pmu {\n \t\tDECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);\n \t\tatomic64_t __reprogram_pmi;\n \t};\n-\tDECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);\n+\tunion {\n+\t\tDECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);\n+\t\tu64 pmc_exists64;\n+\t};\n \tDECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);\n \n \tDECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);\ndiff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h\nindex 3a8e51a0c9e88c..25dda50fbdc1f0 100644\n--- a/arch/x86/include/asm/msr-index.h\n+++ b/arch/x86/include/asm/msr-index.h\n@@ -1242,17 +1242,10 @@\n #define MSR_CORE_PERF_GLOBAL_CTRL\t0x0000038f\n #define MSR_CORE_PERF_GLOBAL_OVF_CTRL\t0x00000390\n #define MSR_CORE_PERF_GLOBAL_STATUS_SET\t0x00000391\n+#define MSR_CORE_PERF_GLOBAL_INUSE\t0x00000392\n \n #define MSR_PERF_METRICS\t\t0x00000329\n \n-/* PERF_GLOBAL_OVF_CTL bits */\n-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT\t55\n-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI\t\t(1ULL \u003c\u003c MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)\n-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT\t\t62\n-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF\t\t\t(1ULL \u003c\u003c MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)\n-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT\t\t63\n-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD\t\t\t(1ULL \u003c\u003c MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)\n-\n /* Geode defined MSRs */\n #define MSR_GEODE_BUSCONT_CONF0\t\t0x00001900\n \ndiff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h\nindex 1eb13673e889f9..2937e8a278915c 100644\n--- a/arch/x86/include/asm/perf_event.h\n+++ b/arch/x86/include/asm/perf_event.h\n@@ -300,8 +300,14 @@ union cpuid_0x80000022_ebx {\n \n struct x86_pmu_capability {\n \tint\t\tversion;\n-\tint\t\tnum_counters_gp;\n-\tint\t\tnum_counters_fixed;\n+\tunion {\n+\t\tu64\tcntr_mask64;\n+\t\tDECLARE_BITMAP(cntr_mask, X86_PMC_IDX_MAX);\n+\t};\n+\tunion {\n+\t\tu64\tfixed_cntr_mask64;\n+\t\tDECLARE_BITMAP(fixed_cntr_mask, X86_PMC_IDX_MAX);\n+\t};\n \tint\t\tbit_width_gp;\n \tint\t\tbit_width_fixed;\n \tunsigned int\tevents_mask;\n@@ -448,6 +454,9 @@ static inline bool is_topdown_idx(int idx)\n #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD\tBIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)\n #define GLOBAL_STATUS_PERF_METRICS_OVF_BIT\t48\n \n+/* Intel IA32_PERF_GLOBAL_INUSE MSR */\n+#define PERF_GLOBAL_INUSE_PMI_INUSE\tBIT_ULL(63)\n+\n #define GLOBAL_CTRL_EN_PERF_METRICS\t\tBIT_ULL(48)\n /*\n * We model guest LBR event tracing as another fixed-mode PMC like BTS.\ndiff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c\nindex adafd6fa2c5427..66e68a86794697 100644\n--- a/arch/x86/kvm/cpuid.c\n+++ b/arch/x86/kvm/cpuid.c\n@@ -1535,18 +1535,29 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)\n \t\t}\n \n \t\teax.split.version_id = kvm_pmu_cap.version;\n-\t\teax.split.num_counters = kvm_pmu_cap.num_counters_gp;\n+\n+\t\t/* Contiguous GP counters only. */\n+\t\teax.split.num_counters =\n+\t\t\tfind_first_zero_bit(kvm_pmu_cap.cntr_mask,\n+\t\t\t\t\t KVM_MAX_NR_GP_COUNTERS);\n \t\teax.split.bit_width = kvm_pmu_cap.bit_width_gp;\n \t\teax.split.mask_length = kvm_pmu_cap.events_mask_len;\n-\t\tedx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;\n+\n+\t\t/* Contiguous fixed counters only. */\n+\t\tedx.split.num_counters_fixed =\n+\t\t\tfind_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,\n+\t\t\t\t\t KVM_MAX_NR_FIXED_COUNTERS);\n \t\tedx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;\n \n-\t\tif (kvm_pmu_cap.version)\n+\t\tif (kvm_pmu_cap.version \u003e= 5)\n \t\t\tedx.split.anythread_deprecated = 1;\n \n \t\tentry-\u003eeax = eax.full;\n \t\tentry-\u003eebx = kvm_pmu_cap.events_mask;\n-\t\tentry-\u003eecx = 0;\n+\t\tif (kvm_pmu_cap.version \u003e= 5)\n+\t\t\tentry-\u003eecx = (u32)kvm_pmu_cap.fixed_cntr_mask64;\n+\t\telse\n+\t\t\tentry-\u003eecx = 0;\n \t\tentry-\u003eedx = edx.full;\n \t\tbreak;\n \t}\n@@ -1903,7 +1914,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)\n \n \t\tcpuid_entry_override(entry, CPUID_8000_0022_EAX);\n \n-\t\tebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;\n+\t\tebx.split.num_core_pmc = hweight64(kvm_pmu_cap.cntr_mask64);\n \t\tentry-\u003eebx = ebx.full;\n \t\tbreak;\n \t}\ndiff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c\nindex e91650af44c207..01fb130a7d24a9 100644\n--- a/arch/x86/kvm/msrs.c\n+++ b/arch/x86/kvm/msrs.c\n@@ -2621,20 +2621,20 @@ static void kvm_probe_msr_to_save(u32 msr_index)\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-\t\t kvm_pmu_cap.num_counters_gp)\n+\t\tif (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_PERFCTR0) \u0026\n+\t\t kvm_pmu_cap.cntr_mask64))\n \t\t\treturn;\n \t\tbreak;\n \tcase MSR_ARCH_PERFMON_EVENTSEL0 ...\n \t MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1:\n-\t\tif (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 \u003e=\n-\t\t kvm_pmu_cap.num_counters_gp)\n+\t\tif (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_EVENTSEL0) \u0026\n+\t\t kvm_pmu_cap.cntr_mask64))\n \t\t\treturn;\n \t\tbreak;\n \tcase MSR_ARCH_PERFMON_FIXED_CTR0 ...\n \t MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1:\n-\t\tif (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 \u003e=\n-\t\t kvm_pmu_cap.num_counters_fixed)\n+\t\tif (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_FIXED_CTR0) \u0026\n+\t\t kvm_pmu_cap.fixed_cntr_mask64))\n \t\t\treturn;\n \t\tbreak;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_CTL:\ndiff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c\nindex eef2f792dc9e80..f949fdbf359c68 100644\n--- a/arch/x86/kvm/pmu.c\n+++ b/arch/x86/kvm/pmu.c\n@@ -20,15 +20,15 @@\n #include \u003casm/perf_event.h\u003e\n #include \u003casm/cpu_device_id.h\u003e\n #include \"x86.h\"\n-#include \"cpuid.h\"\n #include \"lapic.h\"\n #include \"pmu.h\"\n \n /* This is enough to filter the vast majority of currently defined events. */\n #define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300\n \n-/* Unadultered PMU capabilities of the host, i.e. of hardware. */\n-static struct x86_pmu_capability __read_mostly kvm_host_pmu;\n+/* Unadulterated PMU capabilities of the host, i.e. of hardware. */\n+struct x86_pmu_capability __read_mostly kvm_host_pmu;\n+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host_pmu);\n \n /* KVM's PMU capabilities, i.e. the intersection of KVM and hardware support. */\n struct x86_pmu_capability __read_mostly kvm_pmu_cap;\n@@ -155,8 +155,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n \t\t * there are a non-zero number of counters, but fewer than what\n \t\t * is architecturally required.\n \t\t */\n-\t\tif (!kvm_host_pmu.num_counters_gp ||\n-\t\t WARN_ON_ONCE(kvm_host_pmu.num_counters_gp \u003c min_nr_gp_ctrs))\n+\t\tif (!kvm_host_pmu.cntr_mask64 ||\n+\t\t WARN_ON_ONCE(hweight64(kvm_host_pmu.cntr_mask64) \u003c min_nr_gp_ctrs))\n \t\t\tenable_pmu = false;\n \t\telse if (is_intel \u0026\u0026 !kvm_host_pmu.version)\n \t\t\tenable_pmu = false;\n@@ -175,11 +175,21 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n \t}\n \n \tmemcpy(\u0026kvm_pmu_cap, \u0026kvm_host_pmu, sizeof(kvm_host_pmu));\n-\tkvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);\n-\tkvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,\n-\t\t\t\t\t pmu_ops-\u003eMAX_NR_GP_COUNTERS);\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 * PMU v3/v4 has AnyThread support, which KVM cannot safely virtualize.\n+\t * Only expose PMU v5+ for Intel mediated PMU, where AnyThread is\n+\t * deprecated; otherwise, cap at PMU v2.\n+\t */\n+\tif (is_intel \u0026\u0026 enable_mediated_pmu \u0026\u0026 kvm_pmu_cap.version \u003e= 5)\n+\t\tkvm_pmu_cap.version = 5;\n+\telse\n+\t\tkvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);\n+\n+\tkvm_pmu_cap.cntr_mask64 \u0026=\n+\t\tGENMASK_ULL(pmu_ops-\u003eMAX_NR_GP_COUNTERS - 1, 0);\n+\tkvm_pmu_cap.fixed_cntr_mask64 \u0026=\n+\t\tGENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);\n \n \tkvm_pmu_eventsel.INSTRUCTIONS_RETIRED =\n \t\tperf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);\n@@ -557,7 +567,9 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)\n \t\t\tpmc-\u003eeventsel_hw |= pmc-\u003eeventsel \u0026\n \t\t\t\t\t ARCH_PERFMON_EVENTSEL_ENABLE;\n \t} else {\n-\t\tu64 mask = intel_fixed_bits_by_idx(pmc-\u003eidx - KVM_FIXED_PMC_BASE_IDX, 0xf);\n+\t\tu64 mask = intel_fixed_bits_by_idx(pmc-\u003eidx - KVM_FIXED_PMC_BASE_IDX,\n+\t\t\t\t\t\t INTEL_FIXED_0_KERNEL |\n+\t\t\t\t\t\t INTEL_FIXED_0_USER);\n \n \t\tpmu-\u003efixed_ctr_ctrl_hw \u0026= ~mask;\n \t\tif (allowed)\n@@ -787,8 +799,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)\n \t * KVM's capabilities are constrained based on KVM support, i.e. KVM's\n \t * capabilities themselves may be a subset of hardware capabilities.\n \t */\n-\treturn pmu-\u003enr_arch_gp_counters != kvm_host_pmu.num_counters_gp ||\n-\t pmu-\u003enr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed;\n+\treturn kvm_gp_pmc_mask(pmu) != kvm_host_pmu.cntr_mask64 ||\n+\t kvm_fixed_pmc_mask(pmu) != kvm_host_pmu.fixed_cntr_mask64;\n }\n \n bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)\n@@ -855,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:\n \tcase MSR_CORE_PERF_GLOBAL_OVF_CTRL:\n+\tcase MSR_CORE_PERF_GLOBAL_STATUS_SET:\n \t\tmsr_info-\u003edata = 0;\n \t\tbreak;\n \tdefault:\n@@ -921,6 +934,18 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\tif (!msr_info-\u003ehost_initiated)\n \t\t\tpmu-\u003eglobal_status \u0026= ~data;\n \t\tbreak;\n+\tcase MSR_CORE_PERF_GLOBAL_STATUS_SET:\n+\t\t/*\n+\t\t * Bit 63 (CondChgd) is architecturally reserved in STATUS_SET.\n+\t\t * Older CPUs errantly accepted writes without #GP (e.g. SKX95),\n+\t\t * but KVM strictly enforces the architectural spec to ensure\n+\t\t * consistent behavior across migrations.\n+\t\t */\n+\t\tif (data \u0026 (pmu-\u003eglobal_status_rsvd | GLOBAL_STATUS_COND_CHG))\n+\t\t\treturn 1;\n+\t\tif (!msr_info-\u003ehost_initiated)\n+\t\t\tpmu-\u003eglobal_status |= data;\n+\t\tbreak;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:\n \t\tif (!msr_info-\u003ehost_initiated)\n \t\t\tpmu-\u003eglobal_status |= data \u0026 ~pmu-\u003eglobal_status_rsvd;\n@@ -944,7 +969,7 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)\n \tbitmap_zero(pmu-\u003ereprogram_pmi, X86_PMC_IDX_MAX);\n \tbitmap_zero(pmu-\u003epmc_has_mode_specific_enables, X86_PMC_IDX_MAX);\n \n-\tkvm_for_each_pmc(pmu, pmc, i, pmu-\u003eall_valid_pmc_idx) {\n+\tkvm_for_each_pmc(pmu, pmc, i, pmu-\u003epmc_exists) {\n \t\tpmc_stop_counter(pmc);\n \t\tpmc-\u003ecounter = 0;\n \t\tpmc-\u003eemulated_counter = 0;\n@@ -979,18 +1004,20 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)\n \tkvm_pmu_reset(vcpu);\n \n \tpmu-\u003eversion = 0;\n-\tpmu-\u003enr_arch_gp_counters = 0;\n-\tpmu-\u003enr_arch_fixed_counters = 0;\n \tpmu-\u003ecounter_bitmask[KVM_PMC_GP] = 0;\n \tpmu-\u003ecounter_bitmask[KVM_PMC_FIXED] = 0;\n-\tpmu-\u003ereserved_bits = 0xffffffff00200000ull;\n+\t/*\n+\t * KVM is not able to emulate the AnyThread bit due to cross-VM\n+\t * information leakage on SMT cores.\n+\t */\n+\tpmu-\u003eeventsel_rsvd = GENMASK_ULL(63, 32) | ARCH_PERFMON_EVENTSEL_ANY;\n \tpmu-\u003eraw_event_mask = X86_RAW_EVENT_MASK;\n \tpmu-\u003eglobal_ctrl_rsvd = ~0ull;\n \tpmu-\u003eglobal_status_rsvd = ~0ull;\n \tpmu-\u003efixed_ctr_ctrl_rsvd = ~0ull;\n \tpmu-\u003epebs_enable_rsvd = ~0ull;\n \tpmu-\u003epebs_data_cfg_rsvd = ~0ull;\n-\tbitmap_zero(pmu-\u003eall_valid_pmc_idx, X86_PMC_IDX_MAX);\n+\tbitmap_zero(pmu-\u003epmc_exists, X86_PMC_IDX_MAX);\n \n \tif (!vcpu-\u003ekvm-\u003earch.enable_pmu)\n \t\treturn;\n@@ -1004,22 +1031,20 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)\n \t * in the global controls). Emulate that behavior when refreshing the\n \t * PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL.\n \t */\n-\tif (pmu-\u003enr_arch_gp_counters \u0026\u0026\n+\tif (kvm_gp_pmc_mask(pmu) \u0026\u0026\n \t (kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu)))\n-\t\tpmu-\u003eglobal_ctrl = GENMASK_ULL(pmu-\u003enr_arch_gp_counters - 1, 0);\n+\t\tpmu-\u003eglobal_ctrl = kvm_gp_pmc_mask(pmu);\n \n \tif (kvm_vcpu_has_mediated_pmu(vcpu))\n \t\tkvm_pmu_call(write_global_ctrl)(pmu-\u003eglobal_ctrl);\n-\n-\tbitmap_set(pmu-\u003eall_valid_pmc_idx, 0, pmu-\u003enr_arch_gp_counters);\n-\tbitmap_set(pmu-\u003eall_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,\n-\t\t pmu-\u003enr_arch_fixed_counters);\n }\n \n void kvm_pmu_init(struct kvm_vcpu *vcpu)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \n+\tBUILD_BUG_ON(sizeof(pmu-\u003epmc_exists) != sizeof(pmu-\u003epmc_exists64));\n+\n \tmemset(pmu, 0, sizeof(*pmu));\n \tkvm_pmu_call(init)(vcpu);\n }\n@@ -1034,7 +1059,7 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)\n \n \tpmu-\u003eneed_cleanup = false;\n \n-\tbitmap_andnot(bitmask, pmu-\u003eall_valid_pmc_idx,\n+\tbitmap_andnot(bitmask, pmu-\u003epmc_exists,\n \t\t pmu-\u003epmc_in_use, X86_PMC_IDX_MAX);\n \n \tkvm_for_each_pmc(pmu, pmc, i, bitmask) {\n@@ -1322,6 +1347,8 @@ static __always_inline u32 gp_eventsel_msr(u32 idx)\n static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n+\tunsigned long gp_mask = kvm_gp_pmc_mask(pmu);\n \tstruct kvm_pmc *pmc;\n \tu32 i;\n \n@@ -1330,14 +1357,14 @@ static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)\n \t * is intercepted if hardware has counters that aren't visible to the\n \t * guest (KVM will inject #GP as appropriate).\n \t */\n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_gp_counters; i++) {\n+\tkvm_for_each_gp_counter(i, gp_mask) {\n \t\tpmc = \u0026pmu-\u003egp_counters[i];\n \n \t\tif (pmc-\u003ecounter != rdpmc(i))\n \t\t\twrmsrq(gp_counter_msr(i), pmc-\u003ecounter);\n \t\twrmsrq(gp_eventsel_msr(i), pmc-\u003eeventsel_hw);\n \t}\n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_fixed_counters; i++) {\n+\tkvm_for_each_fixed_counter(i, fixed_mask) {\n \t\tpmc = \u0026pmu-\u003efixed_counters[i];\n \n \t\tif (pmc-\u003ecounter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))\n@@ -1380,6 +1407,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)\n static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n+\tunsigned long gp_mask = kvm_gp_pmc_mask(pmu);\n \tstruct kvm_pmc *pmc;\n \tu32 i;\n \n@@ -1387,7 +1416,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)\n \t * Clear selectors and counters to ensure hardware doesn't count using\n \t * guest controls when the host (perf) restores its state.\n \t */\n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_gp_counters; i++) {\n+\tkvm_for_each_gp_counter(i, gp_mask) {\n \t\tpmc = \u0026pmu-\u003egp_counters[i];\n \n \t\tpmc-\u003ecounter = rdpmc(i);\n@@ -1397,7 +1426,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)\n \t\t\twrmsrq(gp_eventsel_msr(i), 0);\n \t}\n \n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_fixed_counters; i++) {\n+\tkvm_for_each_fixed_counter(i, fixed_mask) {\n \t\tpmc = \u0026pmu-\u003efixed_counters[i];\n \n \t\tpmc-\u003ecounter = rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i);\ndiff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h\nindex 090c9bbb74f483..98e6f2443b82ef 100644\n--- a/arch/x86/kvm/pmu.h\n+++ b/arch/x86/kvm/pmu.h\n@@ -88,6 +88,30 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)\n \treturn enable_mediated_pmu \u0026\u0026 vcpu_to_pmu(vcpu)-\u003eversion;\n }\n \n+static inline unsigned long kvm_gp_pmc_mask(struct kvm_pmu *pmu)\n+{\n+\treturn pmu-\u003epmc_exists64 \u0026\n+\t GENMASK_ULL(KVM_MAX_NR_GP_COUNTERS - 1, 0);\n+}\n+\n+static inline unsigned long kvm_fixed_pmc_mask(struct kvm_pmu *pmu)\n+{\n+\treturn (pmu-\u003epmc_exists64 \u003e\u003e KVM_FIXED_PMC_BASE_IDX) \u0026\n+\t GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);\n+}\n+\n+static inline bool kvm_is_gp_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)\n+{\n+\treturn idx \u003c KVM_MAX_NR_GP_COUNTERS \u0026\u0026\n+\t test_bit(idx, pmu-\u003epmc_exists);\n+}\n+\n+static inline bool kvm_is_fixed_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)\n+{\n+\treturn idx \u003c KVM_MAX_NR_FIXED_COUNTERS \u0026\u0026\n+\t test_bit(KVM_FIXED_PMC_BASE_IDX + idx, pmu-\u003epmc_exists);\n+}\n+\n /*\n * KVM tracks all counters in 64-bit bitmaps, with general purpose counters\n * mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0\n@@ -104,11 +128,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)\n */\n static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)\n {\n-\tif (idx \u003c pmu-\u003enr_arch_gp_counters)\n+\tif (kvm_is_gp_pmc_supported(pmu, idx))\n \t\treturn \u0026pmu-\u003egp_counters[idx];\n \n \tidx -= KVM_FIXED_PMC_BASE_IDX;\n-\tif (idx \u003e= 0 \u0026\u0026 idx \u003c pmu-\u003enr_arch_fixed_counters)\n+\tif (kvm_is_fixed_pmc_supported(pmu, idx))\n \t\treturn \u0026pmu-\u003efixed_counters[idx];\n \n \treturn NULL;\n@@ -120,6 +144,17 @@ static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)\n \t\t\tcontinue;\t\t\t\t\\\n \t\telse\t\t\t\t\t\t\\\n \n+/*\n+ * @mask is expected to be a scalar unsigned long derived from pmu-\u003epmc_exists,\n+ * which is already constrained by KVM_MAX_NR_{AMD,INTEL}_{GP,FIXED}_COUNTERS,\n+ * so iteration up to KVM's maximum counter count is safe.\n+ */\n+#define kvm_for_each_gp_counter(i, mask)\t\\\n+\tfor_each_set_bit((i), \u0026(mask), KVM_MAX_NR_GP_COUNTERS)\n+\n+#define kvm_for_each_fixed_counter(i, mask)\t\\\n+\tfor_each_set_bit((i), \u0026(mask), KVM_MAX_NR_FIXED_COUNTERS)\n+\n static inline u64 pmc_bitmask(struct kvm_pmc *pmc)\n {\n \tstruct kvm_pmu *pmu = pmc_to_pmu(pmc);\n@@ -168,9 +203,12 @@ static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,\n static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,\n \t\t\t\t\t u32 base)\n {\n-\tif (msr \u003e= base \u0026\u0026 msr \u003c base + pmu-\u003enr_arch_gp_counters) {\n+\tif (msr \u003e= base \u0026\u0026 msr \u003c base + KVM_MAX_NR_GP_COUNTERS) {\n \t\tu32 index = array_index_nospec(msr - base,\n-\t\t\t\t\t pmu-\u003enr_arch_gp_counters);\n+\t\t\t\t\t KVM_MAX_NR_GP_COUNTERS);\n+\n+\t\tif (!kvm_is_gp_pmc_supported(pmu, index))\n+\t\t\treturn NULL;\n \n \t\treturn \u0026pmu-\u003egp_counters[index];\n \t}\n@@ -183,9 +221,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)\n {\n \tint base = MSR_CORE_PERF_FIXED_CTR0;\n \n-\tif (msr \u003e= base \u0026\u0026 msr \u003c base + pmu-\u003enr_arch_fixed_counters) {\n+\tif (msr \u003e= base \u0026\u0026 msr \u003c base + KVM_MAX_NR_FIXED_COUNTERS) {\n \t\tu32 index = array_index_nospec(msr - base,\n-\t\t\t\t\t pmu-\u003enr_arch_fixed_counters);\n+\t\t\t\t\t KVM_MAX_NR_FIXED_COUNTERS);\n+\n+\t\tif (!kvm_is_fixed_pmc_supported(pmu, index))\n+\t\t\treturn NULL;\n \n \t\treturn \u0026pmu-\u003efixed_counters[index];\n \t}\n@@ -211,6 +252,7 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)\n \treturn !kvm_pmu_call(pmc_is_disabled_in_current_mode)(pmc);\n }\n \n+extern struct x86_pmu_capability kvm_host_pmu;\n extern struct x86_pmu_capability kvm_pmu_cap;\n \n void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);\ndiff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c\nindex c18286545a7acd..fb5a298244ecba 100644\n--- a/arch/x86/kvm/svm/pmu.c\n+++ b/arch/x86/kvm/svm/pmu.c\n@@ -27,12 +27,11 @@ enum pmu_type {\n \n static struct kvm_pmc *amd_pmu_get_pmc(struct kvm_pmu *pmu, int pmc_idx)\n {\n-\tunsigned int num_counters = pmu-\u003enr_arch_gp_counters;\n-\n-\tif (pmc_idx \u003e= num_counters)\n+\tif (!kvm_is_gp_pmc_supported(pmu, pmc_idx))\n \t\treturn NULL;\n \n-\treturn \u0026pmu-\u003egp_counters[array_index_nospec(pmc_idx, num_counters)];\n+\tpmc_idx = array_index_nospec(pmc_idx, KVM_MAX_NR_AMD_GP_COUNTERS);\n+\treturn \u0026pmu-\u003egp_counters[pmc_idx];\n }\n \n static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,\n@@ -77,7 +76,7 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \n-\tif (idx \u003e= pmu-\u003enr_arch_gp_counters)\n+\tif (!kvm_is_gp_pmc_supported(pmu, idx))\n \t\treturn -EINVAL;\n \n \treturn 0;\n@@ -117,7 +116,7 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)\n \t\treturn pmu-\u003eversion \u003e 1;\n \tdefault:\n \t\tif (msr \u003e MSR_F15H_PERF_CTR5 \u0026\u0026\n-\t\t msr \u003c MSR_F15H_PERF_CTL0 + 2 * pmu-\u003enr_arch_gp_counters)\n+\t\t msr \u003c MSR_F15H_PERF_CTL0 + 2 * hweight_long(kvm_gp_pmc_mask(pmu)))\n \t\t\treturn pmu-\u003eversion \u003e 1;\n \t\tbreak;\n \t}\n@@ -163,7 +162,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t/* MSR_EVNTSELn */\n \tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);\n \tif (pmc) {\n-\t\tdata \u0026= ~pmu-\u003ereserved_bits;\n+\t\tdata \u0026= ~pmu-\u003eeventsel_rsvd;\n \t\tif (data != pmc-\u003eeventsel) {\n \t\t\tpmc-\u003eeventsel = data;\n \t\t\tpmc-\u003eeventsel_hw = (data \u0026 ~AMD64_EVENTSEL_HOSTONLY) |\n@@ -184,6 +183,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \n static void amd_pmu_refresh(struct kvm_vcpu *vcpu)\n {\n+\tunsigned int nr_gp_counters = AMD64_NUM_COUNTERS;\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \tunion cpuid_0x80000022_ebx ebx;\n \n@@ -197,31 +197,34 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)\n \t\tBUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||\n \t\t\t x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);\n \t\tebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)-\u003eebx;\n-\t\tpmu-\u003enr_arch_gp_counters = ebx.split.num_core_pmc;\n+\t\tnr_gp_counters = ebx.split.num_core_pmc;\n \t} else if (guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {\n-\t\tpmu-\u003enr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;\n-\t} else {\n-\t\tpmu-\u003enr_arch_gp_counters = AMD64_NUM_COUNTERS;\n+\t\tnr_gp_counters = AMD64_NUM_COUNTERS_CORE;\n \t}\n \n-\tpmu-\u003enr_arch_gp_counters = min_t(unsigned int, pmu-\u003enr_arch_gp_counters,\n-\t\t\t\t\t kvm_pmu_cap.num_counters_gp);\n+\tpmu-\u003epmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) \u0026\n+\t\t\t kvm_pmu_cap.cntr_mask64;\n \n \tif (pmu-\u003eversion \u003e 1) {\n-\t\tpmu-\u003eglobal_ctrl_rsvd = ~(BIT_ULL(pmu-\u003enr_arch_gp_counters) - 1);\n+\t\tpmu-\u003eglobal_ctrl_rsvd = ~pmu-\u003epmc_exists64;\n \t\tpmu-\u003eglobal_status_rsvd = pmu-\u003eglobal_ctrl_rsvd;\n \t}\n \n \tpmu-\u003ecounter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;\n \n-\tpmu-\u003ereserved_bits = 0xfffffff000280000ull;\n+\t/*\n+\t * AMD PerfEvtSeln registers do not support PinControl, and KVM does\n+\t * not emulate AnyThread due to cross-VM information leakage on SMT\n+\t * cores (as on the Intel side).\n+\t */\n+\tpmu-\u003eeventsel_rsvd = GENMASK_ULL(63, 36) | ARCH_PERFMON_EVENTSEL_PIN_CONTROL |\n+\t\t\t ARCH_PERFMON_EVENTSEL_ANY;\n \tif (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) \u0026\u0026 kvm_vcpu_has_mediated_pmu(vcpu))\n-\t\tpmu-\u003ereserved_bits \u0026= ~AMD64_EVENTSEL_HOST_GUEST_MASK;\n+\t\tpmu-\u003eeventsel_rsvd \u0026= ~AMD64_EVENTSEL_HOST_GUEST_MASK;\n \n \tpmu-\u003eraw_event_mask = AMD64_RAW_EVENT_MASK;\n \t/* not applicable to AMD; but clean them to prevent any fall out */\n \tpmu-\u003ecounter_bitmask[KVM_PMC_FIXED] = 0;\n-\tpmu-\u003enr_arch_fixed_counters = 0;\n }\n \n static void amd_pmu_init(struct kvm_vcpu *vcpu)\ndiff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c\nindex 581bc3a23c95a4..eaf3ca63064b45 100644\n--- a/arch/x86/kvm/svm/svm.c\n+++ b/arch/x86/kvm/svm/svm.c\n@@ -761,24 +761,21 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n {\n \tbool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tunsigned long gp_mask = kvm_gp_pmc_mask(pmu);\n \tint i;\n \n \tif (!enable_mediated_pmu)\n \t\treturn;\n \n \t/* Legacy counters are always available for AMD CPUs with a PMU. */\n-\tfor (i = 0; i \u003c min(pmu-\u003enr_arch_gp_counters, AMD64_NUM_COUNTERS); i++)\n+\tfor (i = 0; i \u003c AMD64_NUM_COUNTERS; i++)\n \t\tsvm_set_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,\n-\t\t\t\t\t MSR_TYPE_RW, intercept);\n+\t\t\t\t\t MSR_TYPE_RW, intercept || !test_bit(i, \u0026gp_mask));\n \n \tintercept |= !guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE);\n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_gp_counters; i++)\n+\tfor_each_set_bit(i, kvm_host_pmu.cntr_mask, AMD64_NUM_COUNTERS_CORE)\n \t\tsvm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,\n-\t\t\t\t\t MSR_TYPE_RW, intercept);\n-\n-\tfor ( ; i \u003c kvm_pmu_cap.num_counters_gp; i++)\n-\t\tsvm_enable_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,\n-\t\t\t\t\t MSR_TYPE_RW);\n+\t\t\t\t\t MSR_TYPE_RW, intercept || !test_bit(i, \u0026gp_mask));\n \n \tintercept = kvm_need_perf_global_ctrl_intercept(vcpu);\n \tsvm_set_intercept_for_msr(vcpu, MSR_AMD64_PERF_CNTR_GLOBAL_CTL,\n@@ -5720,9 +5717,8 @@ static __init void svm_set_cpu_caps(void)\n \t\t * access to enough counters to virtualize \"core\" support,\n \t\t * otherwise limit vPMU support to the legacy number of counters.\n \t\t */\n-\t\tif (kvm_pmu_cap.num_counters_gp \u003c AMD64_NUM_COUNTERS_CORE)\n-\t\t\tkvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,\n-\t\t\t\t\t\t\t kvm_pmu_cap.num_counters_gp);\n+\t\tif (hweight64(kvm_pmu_cap.cntr_mask64) \u003c AMD64_NUM_COUNTERS_CORE)\n+\t\t\tkvm_pmu_cap.cntr_mask64 \u0026= GENMASK_ULL(AMD64_NUM_COUNTERS - 1, 0);\n \t\telse\n \t\t\tkvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);\n \ndiff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c\nindex f0d75f5a968cb3..0084e510d896fe 100644\n--- a/arch/x86/kvm/vmx/nested.c\n+++ b/arch/x86/kvm/vmx/nested.c\n@@ -704,6 +704,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \tstruct vcpu_vmx *vmx = to_vmx(vcpu);\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n+\tunsigned long gp_mask = kvm_gp_pmc_mask(pmu);\n \tint i;\n \n \t/*\n@@ -713,12 +715,12 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,\n \tif (!kvm_vcpu_has_mediated_pmu(vcpu))\n \t\treturn;\n \n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_gp_counters; i++) {\n+\tkvm_for_each_gp_counter(i, gp_mask) {\n \t\tnested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i);\n \t\tnested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i);\n \t}\n \n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_fixed_counters; i++)\n+\tkvm_for_each_fixed_counter(i, fixed_mask)\n \t\tnested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i);\n \n \tnested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);\ndiff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c\nindex 70a8c481613569..961d16e7f31f54 100644\n--- a/arch/x86/kvm/vmx/pmu_intel.c\n+++ b/arch/x86/kvm/vmx/pmu_intel.c\n@@ -55,6 +55,25 @@ static struct x86_pmu_lbr *vcpu_to_lbr_records(struct kvm_vcpu *vcpu)\n \n #pragma GCC poison to_vmx\n \n+static u64 intel_pmu_fixed_ctrl_bits(struct kvm_pmu *pmu, u64 bits)\n+{\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n+\tu64 fixed_ctrl_bits = 0;\n+\tint i;\n+\n+\tkvm_for_each_fixed_counter(i, fixed_mask)\n+\t\tfixed_ctrl_bits |= intel_fixed_bits_by_idx(i, bits);\n+\n+\treturn fixed_ctrl_bits;\n+}\n+\n+static u64 intel_pmu_fixed_anythread_mask(struct kvm_pmu *pmu)\n+{\n+\t/* Fixed counter 3 repurposes that bit for METRICS_CLEAR_EN. */\n+\treturn intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD) \u0026\n+\t ~intel_fixed_bits_by_idx(3, INTEL_FIXED_3_METRICS_CLEAR);\n+}\n+\n static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)\n {\n \t/*\n@@ -66,13 +85,14 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)\n \t * hardware, e.g. to ensure the event filter is enforced.\n \t */\n \tu64 old_fixed_ctr_ctrl = pmu-\u003efixed_ctr_ctrl_hw;\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n \tstruct kvm_pmc *pmc;\n \tint i;\n \n \tpmu-\u003efixed_ctr_ctrl = data;\n-\tpmu-\u003efixed_ctr_ctrl_hw = data;\n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_fixed_counters; i++) {\n-\t\tu8 new_ctrl = fixed_ctrl_field(data, i);\n+\tpmu-\u003efixed_ctr_ctrl_hw = data \u0026 ~intel_pmu_fixed_anythread_mask(pmu);\n+\tkvm_for_each_fixed_counter(i, fixed_mask) {\n+\t\tu8 new_ctrl = fixed_ctrl_field(pmu-\u003efixed_ctr_ctrl_hw, i);\n \t\tu8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);\n \n \t\tif (old_ctrl == new_ctrl)\n@@ -107,6 +127,8 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,\n \tif (WARN_ON_ONCE(!pmu-\u003eversion))\n \t\treturn NULL;\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 \t * are supported on all architectural PMUs, i.e. on all virtual PMUs\n@@ -117,23 +139,25 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,\n \t */\n \tswitch (type) {\n \tcase INTEL_RDPMC_FIXED:\n+\t\tif (!kvm_is_fixed_pmc_supported(pmu, idx))\n+\t\t\treturn NULL;\n+\n \t\tcounters = pmu-\u003efixed_counters;\n-\t\tnum_counters = pmu-\u003enr_arch_fixed_counters;\n+\t\tnum_counters = KVM_MAX_NR_INTEL_FIXED_COUNTERS;\n \t\tbitmask = pmu-\u003ecounter_bitmask[KVM_PMC_FIXED];\n \t\tbreak;\n \tcase INTEL_RDPMC_GP:\n+\t\tif (!kvm_is_gp_pmc_supported(pmu, idx))\n+\t\t\treturn NULL;\n+\n \t\tcounters = pmu-\u003egp_counters;\n-\t\tnum_counters = pmu-\u003enr_arch_gp_counters;\n+\t\tnum_counters = KVM_MAX_NR_INTEL_GP_COUNTERS;\n \t\tbitmask = pmu-\u003ecounter_bitmask[KVM_PMC_GP];\n \t\tbreak;\n \tdefault:\n \t\treturn NULL;\n \t}\n \n-\tidx \u0026= INTEL_RDPMC_INDEX_MASK;\n-\tif (idx \u003e= num_counters)\n-\t\treturn NULL;\n-\n \t*mask \u0026= bitmask;\n \treturn \u0026counters[array_index_nospec(idx, num_counters)];\n }\n@@ -192,6 +216,9 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)\n \tcase MSR_CORE_PERF_GLOBAL_OVF_CTRL:\n \tcase MSR_CORE_PERF_FIXED_CTR_CTRL:\n \t\treturn kvm_pmu_has_perf_global_ctrl(pmu);\n+\tcase MSR_CORE_PERF_GLOBAL_STATUS_SET:\n+\tcase MSR_CORE_PERF_GLOBAL_INUSE:\n+\t\treturn pmu-\u003eversion \u003e= 4;\n \tcase MSR_IA32_PEBS_ENABLE:\n \t\tret = vcpu_get_perf_capabilities(vcpu) \u0026 PERF_CAP_PEBS_FORMAT;\n \t\tbreak;\n@@ -339,6 +366,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,\n \treturn true;\n }\n \n+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)\n+{\n+\tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n+\tunsigned long gp_mask = kvm_gp_pmc_mask(pmu);\n+\tbool pmi_inuse = false;\n+\tu64 eventsel, data = 0;\n+\tu32 fixed_ctrl;\n+\tint i;\n+\n+\tkvm_for_each_gp_counter(i, gp_mask) {\n+\t\teventsel = pmu-\u003egp_counters[i].eventsel;\n+\n+\t\tif (eventsel \u0026 ARCH_PERFMON_EVENTSEL_EVENT)\n+\t\t\tdata |= BIT_ULL(i);\n+\t\tpmi_inuse |= !!(eventsel \u0026 ARCH_PERFMON_EVENTSEL_INT);\n+\t}\n+\tkvm_for_each_fixed_counter(i, fixed_mask) {\n+\t\tfixed_ctrl = fixed_ctrl_field(pmu-\u003efixed_ctr_ctrl, i);\n+\n+\t\tif (fixed_ctrl \u0026 (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))\n+\t\t\tdata |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);\n+\t\tpmi_inuse |= !!(fixed_ctrl \u0026 INTEL_FIXED_0_ENABLE_PMI);\n+\t}\n+\tpmi_inuse |= pmu-\u003epebs_enable;\n+\n+\tif (pmi_inuse)\n+\t\tdata |= PERF_GLOBAL_INUSE_PMI_INUSE;\n+\n+\treturn data;\n+}\n+\n static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n@@ -349,6 +408,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_CORE_PERF_GLOBAL_INUSE:\n+\t\tmsr_info-\u003edata = intel_pmu_get_global_inuse(vcpu);\n+\t\tbreak;\n \tcase MSR_IA32_PEBS_ENABLE:\n \t\tmsr_info-\u003edata = pmu-\u003epebs_enable;\n \t\tbreak;\n@@ -388,7 +450,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \tstruct kvm_pmc *pmc;\n \tu32 msr = msr_info-\u003eindex;\n \tu64 data = msr_info-\u003edata;\n-\tu64 reserved_bits, diff;\n+\tu64 eventsel_rsvd, diff;\n \n \tswitch (msr) {\n \tcase MSR_CORE_PERF_FIXED_CTR_CTRL:\n@@ -436,16 +498,16 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\t\tpmc_write_counter(pmc, data);\n \t\t\tbreak;\n \t\t} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {\n-\t\t\treserved_bits = pmu-\u003ereserved_bits;\n+\t\t\teventsel_rsvd = pmu-\u003eeventsel_rsvd;\n \t\t\tif ((pmc-\u003eidx == 2) \u0026\u0026\n \t\t\t (pmu-\u003eraw_event_mask \u0026 HSW_IN_TX_CHECKPOINTED))\n-\t\t\t\treserved_bits ^= HSW_IN_TX_CHECKPOINTED;\n-\t\t\tif (data \u0026 reserved_bits)\n+\t\t\t\teventsel_rsvd ^= HSW_IN_TX_CHECKPOINTED;\n+\t\t\tif (data \u0026 eventsel_rsvd)\n \t\t\t\treturn 1;\n \n \t\t\tif (data != pmc-\u003eeventsel) {\n \t\t\t\tpmc-\u003eeventsel = data;\n-\t\t\t\tpmc-\u003eeventsel_hw = data;\n+\t\t\t\tpmc-\u003eeventsel_hw = data \u0026 ~ARCH_PERFMON_EVENTSEL_ANY;\n \t\t\t\tkvm_pmu_request_counter_reprogram(pmc);\n \t\t\t}\n \t\t\tbreak;\n@@ -489,26 +551,22 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)\n \t * have a known encoding for the associated general purpose event.\n \t */\n \teventsel = perf_get_hw_event_config(fixed_pmc_perf_ids[index]);\n-\tWARN_ON_ONCE(!eventsel \u0026\u0026 index \u003c kvm_pmu_cap.num_counters_fixed);\n+\tWARN_ON_ONCE(!eventsel \u0026\u0026\n+\t\t (kvm_pmu_cap.fixed_cntr_mask64 \u0026 BIT_ULL(index)));\n \treturn eventsel;\n }\n \n-static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)\n-{\n-\tint i;\n-\n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_fixed_counters; i++)\n-\t\tpmu-\u003efixed_ctr_ctrl_rsvd \u0026= ~intel_fixed_bits_by_idx(i, bits);\n-}\n-\n static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \tstruct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);\n \tstruct kvm_cpuid_entry2 *entry;\n+\tu64 fixed_enable_bits = 0;\n \tunion cpuid10_eax eax;\n \tunion cpuid10_edx edx;\n \tu64 perf_capabilities;\n+\tu64 fixed_mask;\n+\tint nr_gp_counters, nr_fixed_counters;\n \tu64 counter_rsvd;\n \n \tif (!lbr_desc)\n@@ -535,8 +593,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\n \tif (!pmu-\u003eversion)\n \t\treturn;\n \n-\tpmu-\u003enr_arch_gp_counters = min_t(int, eax.split.num_counters,\n-\t\t\t\t\t kvm_pmu_cap.num_counters_gp);\n \teax.split.bit_width = min_t(int, eax.split.bit_width,\n \t\t\t\t kvm_pmu_cap.bit_width_gp);\n \tpmu-\u003ecounter_bitmask[KVM_PMC_GP] = BIT_ULL(eax.split.bit_width) - 1;\n@@ -544,11 +600,39 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\n \t\t\t\t kvm_pmu_cap.events_mask_len);\n \tpmu-\u003eavailable_event_types = ~entry-\u003eebx \u0026 (BIT_ULL(eax.split.mask_length) - 1);\n \n+\t/*\n+\t * CPUID.0AH:EDX[4:0] can only enumerate contiguous counters. Clamp the\n+\t * value to the number of contiguous fixed counters supported by KVM.\n+\t */\n+\tnr_fixed_counters = min_t(int, edx.split.num_counters_fixed,\n+\t\t\t\t find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,\n+\t\t\t\t\t\t KVM_MAX_NR_FIXED_COUNTERS));\n+\n+\t/* FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] \u003e i) */\n+\tfixed_mask = BIT_ULL(nr_fixed_counters) - 1;\n+\t/*\n+\t * Do not enable fixed counters via the v5 ECX bitmap if KVM caps\n+\t * PMU version below 5 (e.g. when mediated PMU is disabled or on\n+\t * older hosts), matching KVM's advertised CPUID.0AH:ECX capability.\n+\t */\n+\tif (kvm_pmu_cap.version \u003e= 5 \u0026\u0026 pmu-\u003eversion \u003e= 5)\n+\t\tfixed_mask |= entry-\u003eecx;\n+\tfixed_mask \u0026= kvm_pmu_cap.fixed_cntr_mask64;\n+\n+\t/*\n+\t * CPUID.0AH:EAX[15:8] can only enumerate contiguous counters. Clamp the\n+\t * value to the number of contiguous GP counters supported by KVM.\n+\t */\n+\tnr_gp_counters = min_t(int, eax.split.num_counters,\n+\t\t\t find_first_zero_bit(kvm_pmu_cap.cntr_mask,\n+\t\t\t\t\t\t KVM_MAX_NR_GP_COUNTERS));\n+\tpmu-\u003epmc_exists64 = BIT_ULL(nr_gp_counters) - 1;\n+\n \tentry = kvm_find_cpuid_entry_index(vcpu, 7, 0);\n \tif (entry \u0026\u0026\n \t (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) \u0026\u0026\n \t (entry-\u003eebx \u0026 (X86_FEATURE_HLE|X86_FEATURE_RTM))) {\n-\t\tpmu-\u003ereserved_bits ^= HSW_IN_TX;\n+\t\tpmu-\u003eeventsel_rsvd ^= HSW_IN_TX;\n \t\tpmu-\u003eraw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);\n \t}\n \n@@ -560,47 +644,56 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)\n \t\tlbr_desc-\u003erecords.nr = 0;\n \n \tif (lbr_desc-\u003erecords.nr)\n-\t\tbitmap_set(pmu-\u003eall_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);\n+\t\tbitmap_set(pmu-\u003epmc_exists, INTEL_PMC_IDX_FIXED_VLBR, 1);\n \n \tif (pmu-\u003eversion == 1)\n \t\treturn;\n \n-\tpmu-\u003enr_arch_fixed_counters = min_t(int, edx.split.num_counters_fixed,\n-\t\t\t\t\t kvm_pmu_cap.num_counters_fixed);\n+\tpmu-\u003epmc_exists64 |= fixed_mask \u003c\u003c INTEL_PMC_IDX_FIXED;\n \tedx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,\n \t\t\t\t\t kvm_pmu_cap.bit_width_fixed);\n \tpmu-\u003ecounter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;\n \n-\tintel_pmu_enable_fixed_counter_bits(pmu, INTEL_FIXED_0_KERNEL |\n-\t\t\t\t\t\t INTEL_FIXED_0_USER |\n-\t\t\t\t\t\t INTEL_FIXED_0_ENABLE_PMI);\n+\tfixed_enable_bits |= INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER |\n+\t\t\t INTEL_FIXED_0_ENABLE_PMI;\n \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+\tcounter_rsvd = ~((u64)kvm_gp_pmc_mask(pmu) |\n+\t\t\t ((u64)kvm_fixed_pmc_mask(pmu) \u003c\u003c KVM_FIXED_PMC_BASE_IDX));\n \tpmu-\u003eglobal_ctrl_rsvd = counter_rsvd;\n \n-\t/*\n-\t * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)\n-\t * share reserved bit definitions. The kernel just happens to use\n-\t * OVF_CTRL for the names.\n-\t */\n \tpmu-\u003eglobal_status_rsvd = pmu-\u003eglobal_ctrl_rsvd\n-\t\t\t\u0026 ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF |\n-\t\t\t MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD);\n+\t\t\t\u0026 ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);\n \tif (vmx_pt_mode_is_host_guest())\n-\t\tpmu-\u003eglobal_status_rsvd \u0026=\n-\t\t\t\t~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;\n+\t\tpmu-\u003eglobal_status_rsvd \u0026= ~GLOBAL_STATUS_TRACE_TOPAPMI;\n \n \tif (perf_capabilities \u0026 PERF_CAP_PEBS_FORMAT) {\n \t\tif (perf_capabilities \u0026 PERF_CAP_PEBS_BASELINE) {\n \t\t\tpmu-\u003epebs_enable_rsvd = counter_rsvd;\n-\t\t\tpmu-\u003ereserved_bits \u0026= ~ICL_EVENTSEL_ADAPTIVE;\n+\t\t\tpmu-\u003eeventsel_rsvd \u0026= ~ICL_EVENTSEL_ADAPTIVE;\n \t\t\tpmu-\u003epebs_data_cfg_rsvd = ~0xff00000full;\n-\t\t\tintel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);\n+\t\t\tfixed_enable_bits |= ICL_FIXED_0_ADAPTIVE;\n \t\t} else {\n-\t\t\tpmu-\u003epebs_enable_rsvd = ~(BIT_ULL(pmu-\u003enr_arch_gp_counters) - 1);\n+\t\t\tpmu-\u003epebs_enable_rsvd = ~(u64)kvm_gp_pmc_mask(pmu);\n \t\t}\n \t}\n+\n+\tpmu-\u003efixed_ctr_ctrl_rsvd \u0026=\n+\t\t~intel_pmu_fixed_ctrl_bits(pmu, fixed_enable_bits);\n+\n+\t/*\n+\t * AnyThread counting is not supported by KVM due to cross-VM\n+\t * information leakage concerns on SMT cores. Therefore, AnyThread\n+\t * remains unavailable for PerfMon v3/v4 guests, where AnyThread\n+\t * deprecation is not enumerated.\n+\t *\n+\t * On PerfMon v5+ with ANYTHREAD_DEPRECATION, writes to AnyThread bits\n+\t * are accepted and preserved on RDMSR, while counting behavior is\n+\t * ignored.\n+\t */\n+\tif (pmu-\u003eversion \u003e= 5 \u0026\u0026 edx.split.anythread_deprecated) {\n+\t\tpmu-\u003eeventsel_rsvd \u0026= ~ARCH_PERFMON_EVENTSEL_ANY;\n+\t\tpmu-\u003efixed_ctr_ctrl_rsvd \u0026= ~intel_pmu_fixed_anythread_mask(pmu);\n+\t}\n }\n \n static void intel_pmu_init(struct kvm_vcpu *vcpu)\n@@ -808,8 +901,16 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)\n \ttoggle = pmu-\u003eglobal_status ^ global_status;\n \tif (global_status \u0026 toggle)\n \t\twrmsrq(MSR_CORE_PERF_GLOBAL_OVF_CTRL, global_status \u0026 toggle);\n-\tif (pmu-\u003eglobal_status \u0026 toggle)\n-\t\twrmsrq(MSR_CORE_PERF_GLOBAL_STATUS_SET, pmu-\u003eglobal_status \u0026 toggle);\n+\n+\t/*\n+\t * CondChgd is architecturally reserved in GLOBAL_STATUS_SET. Unless\n+\t * set by malicious userspace, this bit should never be set in\n+\t * pmu-\u003eglobal_status; filter it out defensively to protect against\n+\t * a host #GP.\n+\t */\n+\tif ((pmu-\u003eglobal_status \u0026 toggle) \u0026 ~GLOBAL_STATUS_COND_CHG)\n+\t\twrmsrq(MSR_CORE_PERF_GLOBAL_STATUS_SET,\n+\t\t (pmu-\u003eglobal_status \u0026 toggle) \u0026 ~GLOBAL_STATUS_COND_CHG);\n \n \twrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, pmu-\u003efixed_ctr_ctrl_hw);\n }\ndiff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c\nindex b341af4c6057a7..9db6107943ec30 100644\n--- a/arch/x86/kvm/vmx/vmx.c\n+++ b/arch/x86/kvm/vmx/vmx.c\n@@ -4242,6 +4242,8 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n \tbool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \tstruct vcpu_vmx *vmx = to_vmx(vcpu);\n+\tunsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);\n+\tunsigned long gp_mask = kvm_gp_pmc_mask(pmu);\n \tbool intercept = !has_mediated_pmu;\n \tint i;\n \n@@ -4262,25 +4264,21 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n \n \tvm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);\n \n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_gp_counters; i++) {\n+\tfor_each_set_bit(i, kvm_host_pmu.cntr_mask, INTEL_PMC_MAX_GENERIC) {\n+\t\tbool pmc_intercept = intercept || !test_bit(i, \u0026gp_mask);\n+\n \t\tvmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,\n-\t\t\t\t\t MSR_TYPE_RW, intercept);\n+\t\t\t\t\t MSR_TYPE_RW, pmc_intercept);\n \t\tvmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,\n-\t\t\t\t\t intercept || !fw_writes_is_enabled(vcpu));\n-\t}\n-\tfor ( ; i \u003c kvm_pmu_cap.num_counters_gp; i++) {\n-\t\tvmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,\n-\t\t\t\t\t MSR_TYPE_RW, true);\n-\t\tvmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i,\n-\t\t\t\t\t MSR_TYPE_RW, true);\n+\t\t\t\t\t pmc_intercept || !fw_writes_is_enabled(vcpu));\n \t}\n \n-\tfor (i = 0; i \u003c pmu-\u003enr_arch_fixed_counters; i++)\n-\t\tvmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,\n-\t\t\t\t\t MSR_TYPE_RW, intercept);\n-\tfor ( ; i \u003c kvm_pmu_cap.num_counters_fixed; i++)\n+\tfor_each_set_bit(i, kvm_host_pmu.fixed_cntr_mask, INTEL_PMC_MAX_FIXED) {\n+\t\tbool pmc_intercept = intercept || !test_bit(i, \u0026fixed_mask);\n+\n \t\tvmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,\n-\t\t\t\t\t MSR_TYPE_RW, true);\n+\t\t\t\t\t MSR_TYPE_RW, pmc_intercept);\n+\t}\n \n \tintercept = kvm_need_perf_global_ctrl_intercept(vcpu);\n \tvmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS,\n@@ -8477,7 +8475,7 @@ static unsigned int vmx_handle_intel_pt_intr(void)\n \t\treturn 0;\n \n \tkvm_make_request(KVM_REQ_PMI, vcpu);\n-\t__set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,\n+\t__set_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT,\n \t\t (unsigned long *)\u0026vcpu-\u003earch.pmu.global_status);\n \treturn 1;\n }\ndiff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h\nindex 5898a277a2e01c..ece262d974613f 100644\n--- a/tools/testing/selftests/kvm/include/x86/processor.h\n+++ b/tools/testing/selftests/kvm/include/x86/processor.h\n@@ -836,6 +836,7 @@ static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property)\n \n static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)\n {\n+\tu8 pmu_version;\n \tu32 nr_bits;\n \n \tif (feature.f.reg == KVM_CPUID_EBX) {\n@@ -845,7 +846,10 @@ static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)\n \n \tGUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX);\n \tnr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);\n-\treturn nr_bits \u003e feature.f.bit || this_cpu_has(feature.f);\n+\tpmu_version = this_cpu_property(X86_PROPERTY_PMU_VERSION);\n+\n+\treturn nr_bits \u003e feature.f.bit ||\n+\t (pmu_version \u003e= 5 \u0026\u0026 this_cpu_has(feature.f));\n }\n \n static __always_inline u64 this_cpu_supported_xcr0(void)\ndiff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c\nindex c3e784e16348c3..77d67fa6e3fc5b 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/bitmap.h\u003e\n \n #include \"pmu.h\"\n #include \"processor.h\"\n@@ -631,25 +632,42 @@ static void __test_fixed_counters(struct kvm_vcpu *vcpu, u8 nr_fixed_counters,\n static void test_fixed_counters(u8 pmu_version, u64 perf_capabilities)\n {\n \tu8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);\n+\tunsigned long fixed_subset;\n \tstruct kvm_vcpu **vcpus;\n \tstruct kvm_vm *vm;\n+\tu32 fixed_bitmap;\n \tint i = 0;\n-\tu32 k;\n \tu8 j;\n \n \tpr_info(\"Testing %u fixed counters, PMU version %u, perf_caps = %lx\\n\",\n \t\tnr_fixed_counters, pmu_version, perf_capabilities);\n \n+\tfixed_bitmap = BIT_ULL(nr_fixed_counters) - 1;\n+\tif (pmu_version \u003e= 5)\n+\t\tfixed_bitmap |= kvm_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK);\n \n-\tvm = pmu_vm_create_with_vcpus((nr_fixed_counters + 1) * BIT(nr_fixed_counters),\n+\tvm = pmu_vm_create_with_vcpus(BIT(__builtin_popcount(fixed_bitmap)) + nr_fixed_counters,\n \t\t\t\t guest_test_fixed_counters,\n \t\t\t\t pmu_version, perf_capabilities, \u0026vcpus);\n \n-\tfor (j = 0; j \u003c= nr_fixed_counters; j++) {\n-\t\tfor (k = 0; k \u003c= (BIT(nr_fixed_counters) - 1); k++)\n-\t\t\t__test_fixed_counters(vcpus[i++], j, k);\n+\tfor (fixed_subset = 0; fixed_subset \u003c= fixed_bitmap; fixed_subset++) {\n+\t\tu32 nr_contiguous;\n+\n+\t\t/*\n+\t\t * The loop walks all values from 0 to fixed_bitmap, so skip any\n+\t\t * value that is not a subset of fixed_bitmap.\n+\t\t */\n+\t\tif (fixed_subset \u0026 ~fixed_bitmap)\n+\t\t\tcontinue;\n+\n+\t\tnr_contiguous = find_first_zero_bit(\u0026fixed_subset,\n+\t\t\t\t\t\t MAX_NR_FIXED_COUNTERS);\n+\t\t__test_fixed_counters(vcpus[i++], nr_contiguous, fixed_subset);\n \t}\n \n+\tfor (j = 1; j \u003c= nr_fixed_counters; j++)\n+\t\t__test_fixed_counters(vcpus[i++], j, 0);\n+\n \tpmu_vm_free(vm, vcpus);\n }\n \n"
} |
| 2/1 | 2026/11/08 23:29 | agent | patch-evaluator |
2mModel:core Args: null Results: {
"EnableConfigs": [
"KVM",
"KVM_INTEL",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_refresh",
"kvm_pmu_set_msr",
"kvm_pmu_get_msr",
"intel_pmu_refresh",
"intel_pmu_set_msr",
"intel_pmu_get_msr",
"amd_pmu_refresh",
"amd_pmu_set_msr",
"kvm_mediated_pmu_load"
],
"Reasoning": "The patch series refactors x86 KVM PMU virtualization to support non-contiguous counters and PMU v5, adds emulation for MSR_CORE_PERF_GLOBAL_STATUS_SET and MSR_CORE_PERF_GLOBAL_INUSE, and updates MSR interception logic and AnyThread deprecation handling for both Intel and AMD. This executable code is reachable via standard /dev/kvm ioctls (such as KVM_SET_CPUID2, KVM_SET_MSRS, KVM_GET_MSRS) and guest vCPU execution, making it a viable target for syzkaller fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any '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: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit f49f6183a3b22d43cbacabe4487b395224573ec4
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 8 23:29:44 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/virt/kvm/x86/errata.rst b/Documentation/virt/kvm/x86/errata.rst
index a9cf0e004651aa..161ba6d1beb483 100644
--- a/Documentation/virt/kvm/x86/errata.rst
+++ b/Documentation/virt/kvm/x86/errata.rst
@@ -84,4 +84,12 @@ memory.
Note, this erratum does not affect AMD CPUs, which fully virtualize CR0.CD in
hardware, i.e. put the CPU caches into "no fill" mode when CR0.CD=1, even when
-running in the guest.
\ No newline at end of file
+running in the guest.
+
+IA32_PERF_GLOBAL_STATUS_RESET
+-----------------------------
+To reduce VM-exits on performance-critical paths, KVM passes through writes
+to IA32_PERF_GLOBAL_STATUS_RESET (0x390) in mediated PMU mode when the guest's
+counters match hardware, without injecting #GP on unvirtualized bits. Because
+the MSR is read-as-zero and write-to-clear, this cannot leak host information
+to the guest.
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 8b3ea0adb9655c..990d3be5a07f95 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -3156,10 +3156,10 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
* base PMU holds the correct number of counters for P-cores.
*/
cap->version = x86_pmu.version;
- cap->num_counters_gp = x86_pmu_num_counters(NULL);
- cap->num_counters_fixed = x86_pmu_num_counters_fixed(NULL);
- cap->bit_width_gp = cap->num_counters_gp ? x86_pmu.cntval_bits : 0;
- cap->bit_width_fixed = cap->num_counters_fixed ? x86_pmu.cntval_bits : 0;
+ cap->cntr_mask64 = x86_pmu.cntr_mask64;
+ cap->fixed_cntr_mask64 = x86_pmu.fixed_cntr_mask64;
+ cap->bit_width_gp = cap->cntr_mask64 ? x86_pmu.cntval_bits : 0;
+ cap->bit_width_fixed = cap->fixed_cntr_mask64 ? x86_pmu.cntval_bits : 0;
cap->events_mask = (unsigned int)x86_pmu.events_maskl;
cap->events_mask_len = x86_pmu.events_mask_len;
cap->pebs_ept = x86_pmu.pebs_ept;
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 0378577d9d8bbf..93f029e1fc98be 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -575,8 +575,6 @@ struct kvm_pmc {
struct kvm_pmu {
u8 version;
- unsigned nr_arch_gp_counters;
- unsigned nr_arch_fixed_counters;
unsigned available_event_types;
u64 fixed_ctr_ctrl;
u64 fixed_ctr_ctrl_hw;
@@ -586,7 +584,7 @@ struct kvm_pmu {
u64 counter_bitmask[2];
u64 global_ctrl_rsvd;
u64 global_status_rsvd;
- u64 reserved_bits;
+ u64 eventsel_rsvd;
u64 raw_event_mask;
struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
@@ -600,7 +598,10 @@ struct kvm_pmu {
DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
atomic64_t __reprogram_pmi;
};
- DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
+ union {
+ DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+ u64 pmc_exists64;
+ };
DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 3a8e51a0c9e88c..25dda50fbdc1f0 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -1242,17 +1242,10 @@
#define MSR_CORE_PERF_GLOBAL_CTRL 0x0000038f
#define MSR_CORE_PERF_GLOBAL_OVF_CTRL 0x00000390
#define MSR_CORE_PERF_GLOBAL_STATUS_SET 0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE 0x00000392
#define MSR_PERF_METRICS 0x00000329
-/* PERF_GLOBAL_OVF_CTL bits */
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT 55
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT 62
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT 63
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
-
/* Geode defined MSRs */
#define MSR_GEODE_BUSCONT_CONF0 0x00001900
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index 1eb13673e889f9..2937e8a278915c 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -300,8 +300,14 @@ union cpuid_0x80000022_ebx {
struct x86_pmu_capability {
int version;
- int num_counters_gp;
- int num_counters_fixed;
+ union {
+ u64 cntr_mask64;
+ DECLARE_BITMAP(cntr_mask, X86_PMC_IDX_MAX);
+ };
+ union {
+ u64 fixed_cntr_mask64;
+ DECLARE_BITMAP(fixed_cntr_mask, X86_PMC_IDX_MAX);
+ };
int bit_width_gp;
int bit_width_fixed;
unsigned int events_mask;
@@ -448,6 +454,9 @@ static inline bool is_topdown_idx(int idx)
#define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
#define GLOBAL_STATUS_PERF_METRICS_OVF_BIT 48
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE BIT_ULL(63)
+
#define GLOBAL_CTRL_EN_PERF_METRICS BIT_ULL(48)
/*
* We model guest LBR event tracing as another fixed-mode PMC like BTS.
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index adafd6fa2c5427..66e68a86794697 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1535,18 +1535,29 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
}
eax.split.version_id = kvm_pmu_cap.version;
- eax.split.num_counters = kvm_pmu_cap.num_counters_gp;
+
+ /* Contiguous GP counters only. */
+ eax.split.num_counters =
+ find_first_zero_bit(kvm_pmu_cap.cntr_mask,
+ KVM_MAX_NR_GP_COUNTERS);
eax.split.bit_width = kvm_pmu_cap.bit_width_gp;
eax.split.mask_length = kvm_pmu_cap.events_mask_len;
- edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;
+
+ /* Contiguous fixed counters only. */
+ edx.split.num_counters_fixed =
+ find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
+ KVM_MAX_NR_FIXED_COUNTERS);
edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
- if (kvm_pmu_cap.version)
+ if (kvm_pmu_cap.version >= 5)
edx.split.anythread_deprecated = 1;
entry->eax = eax.full;
entry->ebx = kvm_pmu_cap.events_mask;
- entry->ecx = 0;
+ if (kvm_pmu_cap.version >= 5)
+ entry->ecx = (u32)kvm_pmu_cap.fixed_cntr_mask64;
+ else
+ entry->ecx = 0;
entry->edx = edx.full;
break;
}
@@ -1903,7 +1914,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
cpuid_entry_override(entry, CPUID_8000_0022_EAX);
- ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+ ebx.split.num_core_pmc = hweight64(kvm_pmu_cap.cntr_mask64);
entry->ebx = ebx.full;
break;
}
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index e91650af44c207..01fb130a7d24a9 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -2621,20 +2621,20 @@ static void kvm_probe_msr_to_save(u32 msr_index)
break;
case MSR_ARCH_PERFMON_PERFCTR0 ...
MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
- if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
- kvm_pmu_cap.num_counters_gp)
+ if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_PERFCTR0) &
+ kvm_pmu_cap.cntr_mask64))
return;
break;
case MSR_ARCH_PERFMON_EVENTSEL0 ...
MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1:
- if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >=
- kvm_pmu_cap.num_counters_gp)
+ if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_EVENTSEL0) &
+ kvm_pmu_cap.cntr_mask64))
return;
break;
case MSR_ARCH_PERFMON_FIXED_CTR0 ...
MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1:
- if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >=
- kvm_pmu_cap.num_counters_fixed)
+ if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_FIXED_CTR0) &
+ kvm_pmu_cap.fixed_cntr_mask64))
return;
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e80..f949fdbf359c68 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -20,15 +20,15 @@
#include <asm/perf_event.h>
#include <asm/cpu_device_id.h>
#include "x86.h"
-#include "cpuid.h"
#include "lapic.h"
#include "pmu.h"
/* This is enough to filter the vast majority of currently defined events. */
#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300
-/* Unadultered PMU capabilities of the host, i.e. of hardware. */
-static struct x86_pmu_capability __read_mostly kvm_host_pmu;
+/* Unadulterated PMU capabilities of the host, i.e. of hardware. */
+struct x86_pmu_capability __read_mostly kvm_host_pmu;
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host_pmu);
/* KVM's PMU capabilities, i.e. the intersection of KVM and hardware support. */
struct x86_pmu_capability __read_mostly kvm_pmu_cap;
@@ -155,8 +155,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
* there are a non-zero number of counters, but fewer than what
* is architecturally required.
*/
- if (!kvm_host_pmu.num_counters_gp ||
- WARN_ON_ONCE(kvm_host_pmu.num_counters_gp < min_nr_gp_ctrs))
+ if (!kvm_host_pmu.cntr_mask64 ||
+ WARN_ON_ONCE(hweight64(kvm_host_pmu.cntr_mask64) < min_nr_gp_ctrs))
enable_pmu = false;
else if (is_intel && !kvm_host_pmu.version)
enable_pmu = false;
@@ -175,11 +175,21 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
}
memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
- kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
- kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
- pmu_ops->MAX_NR_GP_COUNTERS);
- kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
- KVM_MAX_NR_FIXED_COUNTERS);
+
+ /*
+ * PMU v3/v4 has AnyThread support, which KVM cannot safely virtualize.
+ * Only expose PMU v5+ for Intel mediated PMU, where AnyThread is
+ * deprecated; otherwise, cap at PMU v2.
+ */
+ if (is_intel && enable_mediated_pmu && kvm_pmu_cap.version >= 5)
+ kvm_pmu_cap.version = 5;
+ else
+ kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+
+ kvm_pmu_cap.cntr_mask64 &=
+ GENMASK_ULL(pmu_ops->MAX_NR_GP_COUNTERS - 1, 0);
+ kvm_pmu_cap.fixed_cntr_mask64 &=
+ GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
@@ -557,7 +567,9 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)
pmc->eventsel_hw |= pmc->eventsel &
ARCH_PERFMON_EVENTSEL_ENABLE;
} else {
- u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX, 0xf);
+ u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX,
+ INTEL_FIXED_0_KERNEL |
+ INTEL_FIXED_0_USER);
pmu->fixed_ctr_ctrl_hw &= ~mask;
if (allowed)
@@ -787,8 +799,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
* KVM's capabilities are constrained based on KVM support, i.e. KVM's
* capabilities themselves may be a subset of hardware capabilities.
*/
- return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp ||
- pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed;
+ return kvm_gp_pmc_mask(pmu) != kvm_host_pmu.cntr_mask64 ||
+ kvm_fixed_pmc_mask(pmu) != kvm_host_pmu.fixed_cntr_mask64;
}
bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
@@ -855,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+ case MSR_CORE_PERF_GLOBAL_STATUS_SET:
msr_info->data = 0;
break;
default:
@@ -921,6 +934,18 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
if (!msr_info->host_initiated)
pmu->global_status &= ~data;
break;
+ case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+ /*
+ * Bit 63 (CondChgd) is architecturally reserved in STATUS_SET.
+ * Older CPUs errantly accepted writes without #GP (e.g. SKX95),
+ * but KVM strictly enforces the architectural spec to ensure
+ * consistent behavior across migrations.
+ */
+ if (data & (pmu->global_status_rsvd | GLOBAL_STATUS_COND_CHG))
+ return 1;
+ if (!msr_info->host_initiated)
+ pmu->global_status |= data;
+ break;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
if (!msr_info->host_initiated)
pmu->global_status |= data & ~pmu->global_status_rsvd;
@@ -944,7 +969,7 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX);
bitmap_zero(pmu->pmc_has_mode_specific_enables, X86_PMC_IDX_MAX);
- kvm_for_each_pmc(pmu, pmc, i, pmu->all_valid_pmc_idx) {
+ kvm_for_each_pmc(pmu, pmc, i, pmu->pmc_exists) {
pmc_stop_counter(pmc);
pmc->counter = 0;
pmc->emulated_counter = 0;
@@ -979,18 +1004,20 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
kvm_pmu_reset(vcpu);
pmu->version = 0;
- pmu->nr_arch_gp_counters = 0;
- pmu->nr_arch_fixed_counters = 0;
pmu->counter_bitmask[KVM_PMC_GP] = 0;
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
- pmu->reserved_bits = 0xffffffff00200000ull;
+ /*
+ * KVM is not able to emulate the AnyThread bit due to cross-VM
+ * information leakage on SMT cores.
+ */
+ pmu->eventsel_rsvd = GENMASK_ULL(63, 32) | ARCH_PERFMON_EVENTSEL_ANY;
pmu->raw_event_mask = X86_RAW_EVENT_MASK;
pmu->global_ctrl_rsvd = ~0ull;
pmu->global_status_rsvd = ~0ull;
pmu->fixed_ctr_ctrl_rsvd = ~0ull;
pmu->pebs_enable_rsvd = ~0ull;
pmu->pebs_data_cfg_rsvd = ~0ull;
- bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
+ bitmap_zero(pmu->pmc_exists, X86_PMC_IDX_MAX);
if (!vcpu->kvm->arch.enable_pmu)
return;
@@ -1004,22 +1031,20 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
* in the global controls). Emulate that behavior when refreshing the
* PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL.
*/
- if (pmu->nr_arch_gp_counters &&
+ if (kvm_gp_pmc_mask(pmu) &&
(kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu)))
- pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0);
+ pmu->global_ctrl = kvm_gp_pmc_mask(pmu);
if (kvm_vcpu_has_mediated_pmu(vcpu))
kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
-
- bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
- bitmap_set(pmu->all_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,
- pmu->nr_arch_fixed_counters);
}
void kvm_pmu_init(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));
+
memset(pmu, 0, sizeof(*pmu));
kvm_pmu_call(init)(vcpu);
}
@@ -1034,7 +1059,7 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
pmu->need_cleanup = false;
- bitmap_andnot(bitmask, pmu->all_valid_pmc_idx,
+ bitmap_andnot(bitmask, pmu->pmc_exists,
pmu->pmc_in_use, X86_PMC_IDX_MAX);
kvm_for_each_pmc(pmu, pmc, i, bitmask) {
@@ -1322,6 +1347,8 @@ static __always_inline u32 gp_eventsel_msr(u32 idx)
static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
struct kvm_pmc *pmc;
u32 i;
@@ -1330,14 +1357,14 @@ static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
* is intercepted if hardware has counters that aren't visible to the
* guest (KVM will inject #GP as appropriate).
*/
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ kvm_for_each_gp_counter(i, gp_mask) {
pmc = &pmu->gp_counters[i];
if (pmc->counter != rdpmc(i))
wrmsrq(gp_counter_msr(i), pmc->counter);
wrmsrq(gp_eventsel_msr(i), pmc->eventsel_hw);
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+ kvm_for_each_fixed_counter(i, fixed_mask) {
pmc = &pmu->fixed_counters[i];
if (pmc->counter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))
@@ -1380,6 +1407,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
struct kvm_pmc *pmc;
u32 i;
@@ -1387,7 +1416,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
* Clear selectors and counters to ensure hardware doesn't count using
* guest controls when the host (perf) restores its state.
*/
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ kvm_for_each_gp_counter(i, gp_mask) {
pmc = &pmu->gp_counters[i];
pmc->counter = rdpmc(i);
@@ -1397,7 +1426,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
wrmsrq(gp_eventsel_msr(i), 0);
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+ kvm_for_each_fixed_counter(i, fixed_mask) {
pmc = &pmu->fixed_counters[i];
pmc->counter = rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i);
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f483..98e6f2443b82ef 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -88,6 +88,30 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
return enable_mediated_pmu && vcpu_to_pmu(vcpu)->version;
}
+static inline unsigned long kvm_gp_pmc_mask(struct kvm_pmu *pmu)
+{
+ return pmu->pmc_exists64 &
+ GENMASK_ULL(KVM_MAX_NR_GP_COUNTERS - 1, 0);
+}
+
+static inline unsigned long kvm_fixed_pmc_mask(struct kvm_pmu *pmu)
+{
+ return (pmu->pmc_exists64 >> KVM_FIXED_PMC_BASE_IDX) &
+ GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
+}
+
+static inline bool kvm_is_gp_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+ return idx < KVM_MAX_NR_GP_COUNTERS &&
+ test_bit(idx, pmu->pmc_exists);
+}
+
+static inline bool kvm_is_fixed_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+ return idx < KVM_MAX_NR_FIXED_COUNTERS &&
+ test_bit(KVM_FIXED_PMC_BASE_IDX + idx, pmu->pmc_exists);
+}
+
/*
* KVM tracks all counters in 64-bit bitmaps, with general purpose counters
* mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0
@@ -104,11 +128,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
*/
static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
{
- if (idx < pmu->nr_arch_gp_counters)
+ if (kvm_is_gp_pmc_supported(pmu, idx))
return &pmu->gp_counters[idx];
idx -= KVM_FIXED_PMC_BASE_IDX;
- if (idx >= 0 && idx < pmu->nr_arch_fixed_counters)
+ if (kvm_is_fixed_pmc_supported(pmu, idx))
return &pmu->fixed_counters[idx];
return NULL;
@@ -120,6 +144,17 @@ static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
continue; \
else \
+/*
+ * @mask is expected to be a scalar unsigned long derived from pmu->pmc_exists,
+ * which is already constrained by KVM_MAX_NR_{AMD,INTEL}_{GP,FIXED}_COUNTERS,
+ * so iteration up to KVM's maximum counter count is safe.
+ */
+#define kvm_for_each_gp_counter(i, mask) \
+ for_each_set_bit((i), &(mask), KVM_MAX_NR_GP_COUNTERS)
+
+#define kvm_for_each_fixed_counter(i, mask) \
+ for_each_set_bit((i), &(mask), KVM_MAX_NR_FIXED_COUNTERS)
+
static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
{
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
@@ -168,9 +203,12 @@ static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,
static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
u32 base)
{
- if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
+ if (msr >= base && msr < base + KVM_MAX_NR_GP_COUNTERS) {
u32 index = array_index_nospec(msr - base,
- pmu->nr_arch_gp_counters);
+ KVM_MAX_NR_GP_COUNTERS);
+
+ if (!kvm_is_gp_pmc_supported(pmu, index))
+ return NULL;
return &pmu->gp_counters[index];
}
@@ -183,9 +221,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
{
int base = MSR_CORE_PERF_FIXED_CTR0;
- if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
+ if (msr >= base && msr < base + KVM_MAX_NR_FIXED_COUNTERS) {
u32 index = array_index_nospec(msr - base,
- pmu->nr_arch_fixed_counters);
+ KVM_MAX_NR_FIXED_COUNTERS);
+
+ if (!kvm_is_fixed_pmc_supported(pmu, index))
+ return NULL;
return &pmu->fixed_counters[index];
}
@@ -211,6 +252,7 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)
return !kvm_pmu_call(pmc_is_disabled_in_current_mode)(pmc);
}
+extern struct x86_pmu_capability kvm_host_pmu;
extern struct x86_pmu_capability kvm_pmu_cap;
void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7acd..fb5a298244ecba 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -27,12 +27,11 @@ enum pmu_type {
static struct kvm_pmc *amd_pmu_get_pmc(struct kvm_pmu *pmu, int pmc_idx)
{
- unsigned int num_counters = pmu->nr_arch_gp_counters;
-
- if (pmc_idx >= num_counters)
+ if (!kvm_is_gp_pmc_supported(pmu, pmc_idx))
return NULL;
- return &pmu->gp_counters[array_index_nospec(pmc_idx, num_counters)];
+ pmc_idx = array_index_nospec(pmc_idx, KVM_MAX_NR_AMD_GP_COUNTERS);
+ return &pmu->gp_counters[pmc_idx];
}
static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
@@ -77,7 +76,7 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
- if (idx >= pmu->nr_arch_gp_counters)
+ if (!kvm_is_gp_pmc_supported(pmu, idx))
return -EINVAL;
return 0;
@@ -117,7 +116,7 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
return pmu->version > 1;
default:
if (msr > MSR_F15H_PERF_CTR5 &&
- msr < MSR_F15H_PERF_CTL0 + 2 * pmu->nr_arch_gp_counters)
+ msr < MSR_F15H_PERF_CTL0 + 2 * hweight_long(kvm_gp_pmc_mask(pmu)))
return pmu->version > 1;
break;
}
@@ -163,7 +162,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
/* MSR_EVNTSELn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
if (pmc) {
- data &= ~pmu->reserved_bits;
+ data &= ~pmu->eventsel_rsvd;
if (data != pmc->eventsel) {
pmc->eventsel = data;
pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
@@ -184,6 +183,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
{
+ unsigned int nr_gp_counters = AMD64_NUM_COUNTERS;
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
union cpuid_0x80000022_ebx ebx;
@@ -197,31 +197,34 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
- pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+ nr_gp_counters = ebx.split.num_core_pmc;
} else if (guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
- pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
- } else {
- pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+ nr_gp_counters = AMD64_NUM_COUNTERS_CORE;
}
- pmu->nr_arch_gp_counters = min_t(unsigned int, pmu->nr_arch_gp_counters,
- kvm_pmu_cap.num_counters_gp);
+ pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
+ kvm_pmu_cap.cntr_mask64;
if (pmu->version > 1) {
- pmu->global_ctrl_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+ pmu->global_ctrl_rsvd = ~pmu->pmc_exists64;
pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
}
pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;
- pmu->reserved_bits = 0xfffffff000280000ull;
+ /*
+ * AMD PerfEvtSeln registers do not support PinControl, and KVM does
+ * not emulate AnyThread due to cross-VM information leakage on SMT
+ * cores (as on the Intel side).
+ */
+ pmu->eventsel_rsvd = GENMASK_ULL(63, 36) | ARCH_PERFMON_EVENTSEL_PIN_CONTROL |
+ ARCH_PERFMON_EVENTSEL_ANY;
if (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) && kvm_vcpu_has_mediated_pmu(vcpu))
- pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
+ pmu->eventsel_rsvd &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
/* not applicable to AMD; but clean them to prevent any fall out */
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
- pmu->nr_arch_fixed_counters = 0;
}
static void amd_pmu_init(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 581bc3a23c95a4..eaf3ca63064b45 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -761,24 +761,21 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
{
bool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
int i;
if (!enable_mediated_pmu)
return;
/* Legacy counters are always available for AMD CPUs with a PMU. */
- for (i = 0; i < min(pmu->nr_arch_gp_counters, AMD64_NUM_COUNTERS); i++)
+ for (i = 0; i < AMD64_NUM_COUNTERS; i++)
svm_set_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,
- MSR_TYPE_RW, intercept);
+ MSR_TYPE_RW, intercept || !test_bit(i, &gp_mask));
intercept |= !guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE);
- for (i = 0; i < pmu->nr_arch_gp_counters; i++)
+ for_each_set_bit(i, kvm_host_pmu.cntr_mask, AMD64_NUM_COUNTERS_CORE)
svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
- MSR_TYPE_RW, intercept);
-
- for ( ; i < kvm_pmu_cap.num_counters_gp; i++)
- svm_enable_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
- MSR_TYPE_RW);
+ MSR_TYPE_RW, intercept || !test_bit(i, &gp_mask));
intercept = kvm_need_perf_global_ctrl_intercept(vcpu);
svm_set_intercept_for_msr(vcpu, MSR_AMD64_PERF_CNTR_GLOBAL_CTL,
@@ -5720,9 +5717,8 @@ static __init void svm_set_cpu_caps(void)
* access to enough counters to virtualize "core" support,
* otherwise limit vPMU support to the legacy number of counters.
*/
- if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
- kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
- kvm_pmu_cap.num_counters_gp);
+ if (hweight64(kvm_pmu_cap.cntr_mask64) < AMD64_NUM_COUNTERS_CORE)
+ kvm_pmu_cap.cntr_mask64 &= GENMASK_ULL(AMD64_NUM_COUNTERS - 1, 0);
else
kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index f0d75f5a968cb3..0084e510d896fe 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -704,6 +704,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
int i;
/*
@@ -713,12 +715,12 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
if (!kvm_vcpu_has_mediated_pmu(vcpu))
return;
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ kvm_for_each_gp_counter(i, gp_mask) {
nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i);
nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i);
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+ kvm_for_each_fixed_counter(i, fixed_mask)
nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i);
nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 70a8c481613569..961d16e7f31f54 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -55,6 +55,25 @@ static struct x86_pmu_lbr *vcpu_to_lbr_records(struct kvm_vcpu *vcpu)
#pragma GCC poison to_vmx
+static u64 intel_pmu_fixed_ctrl_bits(struct kvm_pmu *pmu, u64 bits)
+{
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ u64 fixed_ctrl_bits = 0;
+ int i;
+
+ kvm_for_each_fixed_counter(i, fixed_mask)
+ fixed_ctrl_bits |= intel_fixed_bits_by_idx(i, bits);
+
+ return fixed_ctrl_bits;
+}
+
+static u64 intel_pmu_fixed_anythread_mask(struct kvm_pmu *pmu)
+{
+ /* Fixed counter 3 repurposes that bit for METRICS_CLEAR_EN. */
+ return intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD) &
+ ~intel_fixed_bits_by_idx(3, INTEL_FIXED_3_METRICS_CLEAR);
+}
+
static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
{
/*
@@ -66,13 +85,14 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
* hardware, e.g. to ensure the event filter is enforced.
*/
u64 old_fixed_ctr_ctrl = pmu->fixed_ctr_ctrl_hw;
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
struct kvm_pmc *pmc;
int i;
pmu->fixed_ctr_ctrl = data;
- pmu->fixed_ctr_ctrl_hw = data;
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
- u8 new_ctrl = fixed_ctrl_field(data, i);
+ pmu->fixed_ctr_ctrl_hw = data & ~intel_pmu_fixed_anythread_mask(pmu);
+ kvm_for_each_fixed_counter(i, fixed_mask) {
+ u8 new_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl_hw, i);
u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
if (old_ctrl == new_ctrl)
@@ -107,6 +127,8 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
if (WARN_ON_ONCE(!pmu->version))
return NULL;
+ idx &= INTEL_RDPMC_INDEX_MASK;
+
/*
* General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs
* are supported on all architectural PMUs, i.e. on all virtual PMUs
@@ -117,23 +139,25 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
*/
switch (type) {
case INTEL_RDPMC_FIXED:
+ if (!kvm_is_fixed_pmc_supported(pmu, idx))
+ return NULL;
+
counters = pmu->fixed_counters;
- num_counters = pmu->nr_arch_fixed_counters;
+ num_counters = KVM_MAX_NR_INTEL_FIXED_COUNTERS;
bitmask = pmu->counter_bitmask[KVM_PMC_FIXED];
break;
case INTEL_RDPMC_GP:
+ if (!kvm_is_gp_pmc_supported(pmu, idx))
+ return NULL;
+
counters = pmu->gp_counters;
- num_counters = pmu->nr_arch_gp_counters;
+ num_counters = KVM_MAX_NR_INTEL_GP_COUNTERS;
bitmask = pmu->counter_bitmask[KVM_PMC_GP];
break;
default:
return NULL;
}
- idx &= INTEL_RDPMC_INDEX_MASK;
- if (idx >= num_counters)
- return NULL;
-
*mask &= bitmask;
return &counters[array_index_nospec(idx, num_counters)];
}
@@ -192,6 +216,9 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
case MSR_CORE_PERF_FIXED_CTR_CTRL:
return kvm_pmu_has_perf_global_ctrl(pmu);
+ case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+ case MSR_CORE_PERF_GLOBAL_INUSE:
+ return pmu->version >= 4;
case MSR_IA32_PEBS_ENABLE:
ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
break;
@@ -339,6 +366,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
return true;
}
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+ bool pmi_inuse = false;
+ u64 eventsel, data = 0;
+ u32 fixed_ctrl;
+ int i;
+
+ kvm_for_each_gp_counter(i, gp_mask) {
+ eventsel = pmu->gp_counters[i].eventsel;
+
+ if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+ data |= BIT_ULL(i);
+ pmi_inuse |= !!(eventsel & ARCH_PERFMON_EVENTSEL_INT);
+ }
+ kvm_for_each_fixed_counter(i, fixed_mask) {
+ fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+ if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+ data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);
+ pmi_inuse |= !!(fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI);
+ }
+ pmi_inuse |= pmu->pebs_enable;
+
+ if (pmi_inuse)
+ data |= PERF_GLOBAL_INUSE_PMI_INUSE;
+
+ return data;
+}
+
static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -349,6 +408,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_CORE_PERF_GLOBAL_INUSE:
+ msr_info->data = intel_pmu_get_global_inuse(vcpu);
+ break;
case MSR_IA32_PEBS_ENABLE:
msr_info->data = pmu->pebs_enable;
break;
@@ -388,7 +450,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
struct kvm_pmc *pmc;
u32 msr = msr_info->index;
u64 data = msr_info->data;
- u64 reserved_bits, diff;
+ u64 eventsel_rsvd, diff;
switch (msr) {
case MSR_CORE_PERF_FIXED_CTR_CTRL:
@@ -436,16 +498,16 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
pmc_write_counter(pmc, data);
break;
} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
- reserved_bits = pmu->reserved_bits;
+ eventsel_rsvd = pmu->eventsel_rsvd;
if ((pmc->idx == 2) &&
(pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
- reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
- if (data & reserved_bits)
+ eventsel_rsvd ^= HSW_IN_TX_CHECKPOINTED;
+ if (data & eventsel_rsvd)
return 1;
if (data != pmc->eventsel) {
pmc->eventsel = data;
- pmc->eventsel_hw = data;
+ pmc->eventsel_hw = data & ~ARCH_PERFMON_EVENTSEL_ANY;
kvm_pmu_request_counter_reprogram(pmc);
}
break;
@@ -489,26 +551,22 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
* have a known encoding for the associated general purpose event.
*/
eventsel = perf_get_hw_event_config(fixed_pmc_perf_ids[index]);
- WARN_ON_ONCE(!eventsel && index < kvm_pmu_cap.num_counters_fixed);
+ WARN_ON_ONCE(!eventsel &&
+ (kvm_pmu_cap.fixed_cntr_mask64 & BIT_ULL(index)));
return eventsel;
}
-static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
-{
- int i;
-
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
- pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
-}
-
static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
struct kvm_cpuid_entry2 *entry;
+ u64 fixed_enable_bits = 0;
union cpuid10_eax eax;
union cpuid10_edx edx;
u64 perf_capabilities;
+ u64 fixed_mask;
+ int nr_gp_counters, nr_fixed_counters;
u64 counter_rsvd;
if (!lbr_desc)
@@ -535,8 +593,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
if (!pmu->version)
return;
- pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
- kvm_pmu_cap.num_counters_gp);
eax.split.bit_width = min_t(int, eax.split.bit_width,
kvm_pmu_cap.bit_width_gp);
pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(eax.split.bit_width) - 1;
@@ -544,11 +600,39 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
kvm_pmu_cap.events_mask_len);
pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
+ /*
+ * CPUID.0AH:EDX[4:0] can only enumerate contiguous counters. Clamp the
+ * value to the number of contiguous fixed counters supported by KVM.
+ */
+ nr_fixed_counters = min_t(int, edx.split.num_counters_fixed,
+ find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
+ KVM_MAX_NR_FIXED_COUNTERS));
+
+ /* FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i) */
+ fixed_mask = BIT_ULL(nr_fixed_counters) - 1;
+ /*
+ * Do not enable fixed counters via the v5 ECX bitmap if KVM caps
+ * PMU version below 5 (e.g. when mediated PMU is disabled or on
+ * older hosts), matching KVM's advertised CPUID.0AH:ECX capability.
+ */
+ if (kvm_pmu_cap.version >= 5 && pmu->version >= 5)
+ fixed_mask |= entry->ecx;
+ fixed_mask &= kvm_pmu_cap.fixed_cntr_mask64;
+
+ /*
+ * CPUID.0AH:EAX[15:8] can only enumerate contiguous counters. Clamp the
+ * value to the number of contiguous GP counters supported by KVM.
+ */
+ nr_gp_counters = min_t(int, eax.split.num_counters,
+ find_first_zero_bit(kvm_pmu_cap.cntr_mask,
+ KVM_MAX_NR_GP_COUNTERS));
+ pmu->pmc_exists64 = BIT_ULL(nr_gp_counters) - 1;
+
entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
if (entry &&
(boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
(entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
- pmu->reserved_bits ^= HSW_IN_TX;
+ pmu->eventsel_rsvd ^= HSW_IN_TX;
pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
}
@@ -560,47 +644,56 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
lbr_desc->records.nr = 0;
if (lbr_desc->records.nr)
- bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);
+ bitmap_set(pmu->pmc_exists, INTEL_PMC_IDX_FIXED_VLBR, 1);
if (pmu->version == 1)
return;
- pmu->nr_arch_fixed_counters = min_t(int, edx.split.num_counters_fixed,
- kvm_pmu_cap.num_counters_fixed);
+ pmu->pmc_exists64 |= fixed_mask << INTEL_PMC_IDX_FIXED;
edx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,
kvm_pmu_cap.bit_width_fixed);
pmu->counter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;
- intel_pmu_enable_fixed_counter_bits(pmu, INTEL_FIXED_0_KERNEL |
- INTEL_FIXED_0_USER |
- INTEL_FIXED_0_ENABLE_PMI);
+ fixed_enable_bits |= INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER |
+ INTEL_FIXED_0_ENABLE_PMI;
- counter_rsvd = ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) |
- ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
+ counter_rsvd = ~((u64)kvm_gp_pmc_mask(pmu) |
+ ((u64)kvm_fixed_pmc_mask(pmu) << KVM_FIXED_PMC_BASE_IDX));
pmu->global_ctrl_rsvd = counter_rsvd;
- /*
- * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)
- * share reserved bit definitions. The kernel just happens to use
- * OVF_CTRL for the names.
- */
pmu->global_status_rsvd = pmu->global_ctrl_rsvd
- & ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF |
- MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD);
+ & ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
if (vmx_pt_mode_is_host_guest())
- pmu->global_status_rsvd &=
- ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
+ pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;
if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
pmu->pebs_enable_rsvd = counter_rsvd;
- pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
+ pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
pmu->pebs_data_cfg_rsvd = ~0xff00000full;
- intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
+ fixed_enable_bits |= ICL_FIXED_0_ADAPTIVE;
} else {
- pmu->pebs_enable_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+ pmu->pebs_enable_rsvd = ~(u64)kvm_gp_pmc_mask(pmu);
}
}
+
+ pmu->fixed_ctr_ctrl_rsvd &=
+ ~intel_pmu_fixed_ctrl_bits(pmu, fixed_enable_bits);
+
+ /*
+ * AnyThread counting is not supported by KVM due to cross-VM
+ * information leakage concerns on SMT cores. Therefore, AnyThread
+ * remains unavailable for PerfMon v3/v4 guests, where AnyThread
+ * deprecation is not enumerated.
+ *
+ * On PerfMon v5+ with ANYTHREAD_DEPRECATION, writes to AnyThread bits
+ * are accepted and preserved on RDMSR, while counting behavior is
+ * ignored.
+ */
+ if (pmu->version >= 5 && edx.split.anythread_deprecated) {
+ pmu->eventsel_rsvd &= ~ARCH_PERFMON_EVENTSEL_ANY;
+ pmu->fixed_ctr_ctrl_rsvd &= ~intel_pmu_fixed_anythread_mask(pmu);
+ }
}
static void intel_pmu_init(struct kvm_vcpu *vcpu)
@@ -808,8 +901,16 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
toggle = pmu->global_status ^ global_status;
if (global_status & toggle)
wrmsrq(MSR_CORE_PERF_GLOBAL_OVF_CTRL, global_status & toggle);
- if (pmu->global_status & toggle)
- wrmsrq(MSR_CORE_PERF_GLOBAL_STATUS_SET, pmu->global_status & toggle);
+
+ /*
+ * CondChgd is architecturally reserved in GLOBAL_STATUS_SET. Unless
+ * set by malicious userspace, this bit should never be set in
+ * pmu->global_status; filter it out defensively to protect against
+ * a host #GP.
+ */
+ if ((pmu->global_status & toggle) & ~GLOBAL_STATUS_COND_CHG)
+ wrmsrq(MSR_CORE_PERF_GLOBAL_STATUS_SET,
+ (pmu->global_status & toggle) & ~GLOBAL_STATUS_COND_CHG);
wrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, pmu->fixed_ctr_ctrl_hw);
}
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b341af4c6057a7..9db6107943ec30 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4242,6 +4242,8 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
bool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
bool intercept = !has_mediated_pmu;
int i;
@@ -4262,25 +4264,21 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
vm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ for_each_set_bit(i, kvm_host_pmu.cntr_mask, INTEL_PMC_MAX_GENERIC) {
+ bool pmc_intercept = intercept || !test_bit(i, &gp_mask);
+
vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
- MSR_TYPE_RW, intercept);
+ MSR_TYPE_RW, pmc_intercept);
vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,
- intercept || !fw_writes_is_enabled(vcpu));
- }
- for ( ; i < kvm_pmu_cap.num_counters_gp; i++) {
- vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
- MSR_TYPE_RW, true);
- vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i,
- MSR_TYPE_RW, true);
+ pmc_intercept || !fw_writes_is_enabled(vcpu));
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
- vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
- MSR_TYPE_RW, intercept);
- for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)
+ for_each_set_bit(i, kvm_host_pmu.fixed_cntr_mask, INTEL_PMC_MAX_FIXED) {
+ bool pmc_intercept = intercept || !test_bit(i, &fixed_mask);
+
vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
- MSR_TYPE_RW, true);
+ MSR_TYPE_RW, pmc_intercept);
+ }
intercept = kvm_need_perf_global_ctrl_intercept(vcpu);
vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS,
@@ -8477,7 +8475,7 @@ static unsigned int vmx_handle_intel_pt_intr(void)
return 0;
kvm_make_request(KVM_REQ_PMI, vcpu);
- __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
+ __set_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT,
(unsigned long *)&vcpu->arch.pmu.global_status);
return 1;
}
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 5898a277a2e01c..ece262d974613f 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -836,6 +836,7 @@ static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property)
static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
{
+ u8 pmu_version;
u32 nr_bits;
if (feature.f.reg == KVM_CPUID_EBX) {
@@ -845,7 +846,10 @@ static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
GUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX);
nr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
- return nr_bits > feature.f.bit || this_cpu_has(feature.f);
+ pmu_version = this_cpu_property(X86_PROPERTY_PMU_VERSION);
+
+ return nr_bits > feature.f.bit ||
+ (pmu_version >= 5 && this_cpu_has(feature.f));
}
static __always_inline u64 this_cpu_supported_xcr0(void)
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index c3e784e16348c3..77d67fa6e3fc5b 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/bitmap.h>
#include "pmu.h"
#include "processor.h"
@@ -631,25 +632,42 @@ static void __test_fixed_counters(struct kvm_vcpu *vcpu, u8 nr_fixed_counters,
static void test_fixed_counters(u8 pmu_version, u64 perf_capabilities)
{
u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
+ unsigned long fixed_subset;
struct kvm_vcpu **vcpus;
struct kvm_vm *vm;
+ u32 fixed_bitmap;
int i = 0;
- u32 k;
u8 j;
pr_info("Testing %u fixed counters, PMU version %u, perf_caps = %lx\n",
nr_fixed_counters, pmu_version, perf_capabilities);
+ fixed_bitmap = BIT_ULL(nr_fixed_counters) - 1;
+ if (pmu_version >= 5)
+ fixed_bitmap |= kvm_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK);
- vm = pmu_vm_create_with_vcpus((nr_fixed_counters + 1) * BIT(nr_fixed_counters),
+ vm = pmu_vm_create_with_vcpus(BIT(__builtin_popcount(fixed_bitmap)) + nr_fixed_counters,
guest_test_fixed_counters,
pmu_version, perf_capabilities, &vcpus);
- for (j = 0; j <= nr_fixed_counters; j++) {
- for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
- __test_fixed_counters(vcpus[i++], j, k);
+ for (fixed_subset = 0; fixed_subset <= fixed_bitmap; fixed_subset++) {
+ u32 nr_contiguous;
+
+ /*
+ * The loop walks all values from 0 to fixed_bitmap, so skip any
+ * value that is not a subset of fixed_bitmap.
+ */
+ if (fixed_subset & ~fixed_bitmap)
+ continue;
+
+ nr_contiguous = find_first_zero_bit(&fixed_subset,
+ MAX_NR_FIXED_COUNTERS);
+ __test_fixed_counters(vcpus[i++], nr_contiguous, fixed_subset);
}
+ for (j = 1; j <= nr_fixed_counters; j++)
+ __test_fixed_counters(vcpus[i++], j, 0);
+
pmu_vm_free(vm, vcpus);
}
|
| 3/2 | 2026/11/08 23:29 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24117 output: 37 thoughts: 1486 Thoughts: **KVM x86 PMU Patch Series Analysis: Worthy of Fuzzing** Alright, let's break this down. This patch series is touching a significant chunk of the KVM x86 PMU virtualization infrastructure: the documentation, core files, and a lot of the AMD and Intel-specific PMU code. We're looking at improvements, fixes, and refactoring related to mediated PMU, handling non-contiguous counters, PMU v5 features, the AnyThread deprecation, and MSR management (IA32_PERF_GLOBAL_STATUS_SET/INUSE). This is definitely within my area of expertise. My primary concern, as always, is reachability for fuzzing. Can we get at this code in a standard, virtualized environment? The target architecture is amd64, which is excellent, and syzkaller *extensively* tests `/dev/kvm` on x86. So, the code *should* be reachable. Syzkaller's syscalls cover KVM ioctls like `KVM_CREATE_VM`, `KVM_SET_CPUID2`, `KVM_SET_MSRS`, `KVM_RUN`, and so on. Let's quickly check the guidelines for this kind of evaluation. We're looking for modifications in core subsystems, drivers accessible via virtual buses, or syscall handling. KVM, of course, falls into the syscall handling category via `/dev/kvm` ioctls. We must determine if the code is unreachable. Section 1 states that "REACHABILITY IS THE PRIMARY GATE: Fuzzing can only discover bugs in code that can actually execute in standard virtualized environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap). If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed, regardless of whether it adds assertions or complex logic." Section 2 lists the negative criteria: non-kernel changes, structurally unreachable hardware, and unreachable execution paths. None of these apply here! This is KVM code on x86; it's *meant* to be run in standard QEMU or even with nested virtualization. Specifically, it looks like functions like `kvm_pmu_refresh`, `kvm_pmu_set_msr`, `kvm_pmu_get_msr`, `__do_cpuid_func` (used with `KVM_GET_SUPPORTED_CPUID`), and `kvm_probe_msr_to_save` (used with `KVM_GET_MSR_INDEX_LIST`) are *directly* accessible via ioctls on `/dev/kvm` or VM/vCPU file descriptors, without requiring nested virtualization or even a running vCPU. That's a huge plus for fuzzing coverage. Now, let's get into the specifics of how these functions are being invoked. I'll fire up `grepper` and see exactly which ioctls trigger these functions. Time to dig into the code itself. |
| 4/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "kvm_pmu_refresh",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/cpuid.c=377=void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/cpuid.c-451-\narch/x86/kvm/cpuid.c:452:\tkvm_pmu_refresh(vcpu);\narch/x86/kvm/cpuid.c-453-\n--\narch/x86/kvm/msrs.c=1452=int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n--\narch/x86/kvm/msrs.c-1503-\t\tvcpu-\u003earch.perf_capabilities = data;\narch/x86/kvm/msrs.c:1504:\t\tkvm_pmu_refresh(vcpu);\narch/x86/kvm/msrs.c-1505-\t\tkvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu);\n--\narch/x86/kvm/pmu.c=961=static void kvm_pmu_reset(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/pmu.c-992- */\narch/x86/kvm/pmu.c:993:void kvm_pmu_refresh(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-994-{\n--\narch/x86/kvm/pmu.h=321=int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);\narch/x86/kvm/pmu.h:322:void kvm_pmu_refresh(struct kvm_vcpu *vcpu);\narch/x86/kvm/pmu.h-323-void kvm_pmu_init(struct kvm_vcpu *vcpu);\n"
} |
| 5/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26840 output: 35 thoughts: 330 Thoughts: **My Assessment of KVM/PMU Fuzzing Potential** Okay, so I've mapped out the call paths for `kvm_pmu_refresh`. It's definitely reachable code within KVM on x86, triggered by various entry points: `kvm_vcpu_after_set_cpuid` after `KVM_SET_CPUID2`, `kvm_set_msr_common` via `KVM_SET_MSRS` (specifically `MSR_IA32_PERF_CAPABILITIES`), and then `kvm_pmu_set_msr` and `kvm_pmu_get_msr` which are part of the MSR handling routines triggered by `KVM_GET_MSRS`, `KVM_SET_MSRS`, and even the guest's own rdmsr/wrmsr instructions. Plus, during actual guest execution (the `KVM_RUN` path), we have things like `kvm_pmu_load_guest_pmcs`, `kvm_pmu_put_guest_pmcs`, MSR emulation, intercepts, and handling of rdpmc instructions – all hitting this code. Given this wide reach, and considering syzkaller's heavy fuzzing of KVM, *WorthFuzzing* is definitely true – this is an area prime for exploration. Now, let's get practical. I need to figure out the optimal fuzzing strategy. I need to determine: 1. **Required configurations:** What Kconfig options need to be enabled to expose this code path and maximize its vulnerability surface? 2. **Best FocusSymbols:** Where should syzkaller's efforts be most concentrated to maximize crash likelihood? 3. **Reasoning:** What are my assumptions based on this investigation, and how can I refine this knowledge? Time to dig into the Kconfig files around `arch/x86/kvm/Kconfig`. Let's see what we can find. |
| 6/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "config KVM",
"PathPrefixes": [
"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-47-\narch/x86/kvm/Kconfig:48:config KVM\narch/x86/kvm/Kconfig-49-\ttristate \"Kernel-based Virtual Machine (KVM) support\"\n--\narch/x86/kvm/Kconfig-64-\narch/x86/kvm/Kconfig:65:config KVM_WERROR\narch/x86/kvm/Kconfig-66-\tbool \"Compile KVM with -Werror\"\n--\narch/x86/kvm/Kconfig-78-\narch/x86/kvm/Kconfig:79:config KVM_PRIVATE_MEMATTR_VM\narch/x86/kvm/Kconfig-80-\tbool \"Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)\"\n--\narch/x86/kvm/Kconfig-92-\narch/x86/kvm/Kconfig:93:config KVM_SW_PROTECTED_VM\narch/x86/kvm/Kconfig-94-\tbool \"Enable support for KVM software-protected VMs\"\n--\narch/x86/kvm/Kconfig-104-\narch/x86/kvm/Kconfig:105:config KVM_INTEL\narch/x86/kvm/Kconfig-106-\ttristate \"KVM for Intel (and compatible) processors support\"\n--\narch/x86/kvm/Kconfig-115-\narch/x86/kvm/Kconfig:116:config KVM_INTEL_PROVE_VE\narch/x86/kvm/Kconfig-117- bool \"Check that guests do not receive #VE exceptions\"\n--\narch/x86/kvm/Kconfig=131=config X86_SGX_KVM\n--\narch/x86/kvm/Kconfig-142-\narch/x86/kvm/Kconfig:143:config KVM_INTEL_TDX\narch/x86/kvm/Kconfig-144-\tbool \"Intel Trust Domain Extensions (TDX) support\"\n--\narch/x86/kvm/Kconfig-153-\narch/x86/kvm/Kconfig:154:config KVM_AMD\narch/x86/kvm/Kconfig-155-\ttristate \"KVM for AMD processors support\"\n--\narch/x86/kvm/Kconfig-163-\narch/x86/kvm/Kconfig:164:config KVM_AMD_SEV\narch/x86/kvm/Kconfig-165-\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n--\narch/x86/kvm/Kconfig-179-\narch/x86/kvm/Kconfig:180:config KVM_IOAPIC\narch/x86/kvm/Kconfig-181-\tbool \"I/O APIC, PIC, and PIT emulation\"\n--\narch/x86/kvm/Kconfig-189-\narch/x86/kvm/Kconfig:190:config KVM_SMM\narch/x86/kvm/Kconfig-191-\tbool \"System Management Mode emulation\"\n--\narch/x86/kvm/Kconfig-200-\narch/x86/kvm/Kconfig:201:config KVM_HYPERV\narch/x86/kvm/Kconfig-202-\tbool \"Support for Microsoft Hyper-V emulation\"\n--\narch/x86/kvm/Kconfig-214-\narch/x86/kvm/Kconfig:215:config KVM_XEN\narch/x86/kvm/Kconfig-216-\tbool \"Support for Xen hypercall interface\"\n--\narch/x86/kvm/Kconfig-223-\narch/x86/kvm/Kconfig:224:config KVM_PROVE_MMU\narch/x86/kvm/Kconfig-225-\tbool \"Prove KVM MMU correctness\"\n--\narch/x86/kvm/Kconfig-236-\narch/x86/kvm/Kconfig:237:config KVM_EXTERNAL_WRITE_TRACKING\narch/x86/kvm/Kconfig-238-\tbool\narch/x86/kvm/Kconfig-239-\narch/x86/kvm/Kconfig:240:config KVM_MAX_NR_VCPUS\narch/x86/kvm/Kconfig-241-\tint \"Maximum number of vCPUs per KVM guest\"\n"
} |
| 7/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28933 output: 41 thoughts: 72 Thoughts: Here's my analysis of what I'm looking at, from a low-level perspective: **Investigating KVM's PMU Implementation Across Intel and AMD Architectures** Okay, I'm diving into the KVM code. My primary interest right now lies in the Performance Monitoring Unit (PMU) implementation within the x86 architecture, specifically how it's handled within the KVM hypervisor. I'm focusing on the core PMU code and its architecture-specific backends. My immediate goal is to understand how the PMU is managed for Intel and AMD systems. To achieve this, I need to trace the build process and identify the relevant source files. I'm starting by examining the following files: * `arch/x86/kvm/pmu.c`: This likely contains the core, architecture-agnostic PMU management code within KVM. This should define the common PMU interface and handle basic functionalities. * `arch/x86/kvm/vmx/pmu_intel.c`: This file *should* hold the Intel-specific PMU implementation, leveraging Virtual Machine Extensions (VMX) for hardware virtualization and PMU management. This file's implementation is likely very different from the code in `pmu.c`. * `arch/x86/kvm/svm/pmu.c`: Here's the AMD-specific PMU code. This will utilize Secure Virtual Machine (SVM) extensions for PMU control and virtualization. I expect this implementation to be distinct from the Intel VMX variant, although there may be some functional similarities. Now, to understand how these files are compiled, linked, and incorporated into the KVM module, I need to analyze the KVM `Makefile` within the architecture-specific directory. I'll need to go to `arch/x86/kvm/Makefile` and see how this all fits together. This will expose the build dependencies, object files, and ultimately, how KVM loads the correct PMU backend based on the underlying hardware (Intel or AMD). |
| 8/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "pmu\\.o|pmu_intel\\.o",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/Makefile=6=include $(srctree)/virt/kvm/Makefile.kvm\narch/x86/kvm/Makefile-7-\narch/x86/kvm/Makefile:8:kvm-y\t\t\t+= x86.o emulate.o irq.o lapic.o cpuid.o msrs.o pmu.o regs.o \\\narch/x86/kvm/Makefile-9-\t\t\t mtrr.o debugfs.o mmu/mmu.o mmu/page_track.o mmu/spte.o\n--\narch/x86/kvm/Makefile=15=kvm-$(CONFIG_KVM_SMM)\t+= smm.o\narch/x86/kvm/Makefile-16-\narch/x86/kvm/Makefile:17:kvm-intel-y\t\t+= vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \\\narch/x86/kvm/Makefile-18-\t\t\t vmx/nested.o vmx/posted_intr.o vmx/main.o\n--\narch/x86/kvm/Makefile=22=kvm-intel-$(CONFIG_KVM_INTEL_TDX)\t+= vmx/tdx.o\narch/x86/kvm/Makefile-23-\narch/x86/kvm/Makefile:24:kvm-amd-y\t\t+= svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o\narch/x86/kvm/Makefile-25-\n"
} |
| 9/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29907 output: 32 thoughts: 100 Thoughts: **PMU Module Association and Capability Check** Okay, so I'm looking at how Performance Monitoring Units (PMUs) are handled in the KVM world, specifically regarding module dependencies. I see that the base `kvm-y` build configuration is linked to `pmu.o`. That's the core PMU module, makes sense. But then things get interesting. `kvm-intel-y` links to `vmx/pmu_intel.o` and `kvm-amd-y` links to `svm/pmu.o`. That tells me the specific PMU implementations are linked based on the processor type, Intel's VMX or AMD's SVM. Now, I need to check something important: where is the function `perf_get_x86_pmu_capability` located? I know it deals with determining PMU capabilities for x86 architectures, but I need to understand its relationship to these KVM modules. Let's look at `arch/x86/events/core.c` for that. Finally, just to confirm the build dependencies, I'll quickly glance at the `arch/x86/events/Makefile`. That should solidify how these PMU components are built and linked within the broader x86 kernel. This will hopefully show the linkage between KVM, the x86 architecture and PMU, and the overall process for PMU capabilities. |
| 10/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "core\\.o",
"PathPrefixes": [
"arch/x86/events/"
]
}Results: {
"Output": "arch/x86/events/Makefile-1-# SPDX-License-Identifier: GPL-2.0-only\narch/x86/events/Makefile:2:obj-y\t\t\t\t\t+= core.o probe.o utils.o\narch/x86/events/Makefile-3-obj-$(CONFIG_PERF_EVENTS_INTEL_RAPL)\t+= rapl.o\n--\narch/x86/events/amd/Makefile-1-# SPDX-License-Identifier: GPL-2.0\narch/x86/events/amd/Makefile:2:obj-$(CONFIG_CPU_SUP_AMD)\t\t+= core.o lbr.o\narch/x86/events/amd/Makefile-3-obj-$(CONFIG_PERF_EVENTS_AMD_BRS)\t+= brs.o\n--\narch/x86/events/amd/Makefile=5=obj-$(CONFIG_X86_LOCAL_APIC)\t\t+= ibs.o\narch/x86/events/amd/Makefile:6:obj-$(CONFIG_PERF_EVENTS_AMD_UNCORE)\t+= amd-uncore.o\narch/x86/events/amd/Makefile:7:amd-uncore-objs\t\t\t\t:= uncore.o\narch/x86/events/amd/Makefile-8-ifdef CONFIG_AMD_IOMMU\n--\narch/x86/events/intel/Makefile-1-# SPDX-License-Identifier: GPL-2.0\narch/x86/events/intel/Makefile:2:obj-$(CONFIG_CPU_SUP_INTEL)\t\t+= core.o bts.o\narch/x86/events/intel/Makefile-3-obj-$(CONFIG_CPU_SUP_INTEL)\t\t+= ds.o knc.o\narch/x86/events/intel/Makefile=4=obj-$(CONFIG_CPU_SUP_INTEL)\t\t+= lbr.o p4.o p6.o pt.o\narch/x86/events/intel/Makefile:5:obj-$(CONFIG_PERF_EVENTS_INTEL_UNCORE)\t+= intel-uncore.o\narch/x86/events/intel/Makefile:6:intel-uncore-objs\t\t\t:= uncore.o uncore_nhmex.o uncore_snb.o uncore_snbep.o uncore_discovery.o\narch/x86/events/intel/Makefile-7-obj-$(CONFIG_PERF_EVENTS_INTEL_CSTATE)\t+= intel-cstate.o\n--\narch/x86/events/zhaoxin/Makefile-1-# SPDX-License-Identifier: GPL-2.0\narch/x86/events/zhaoxin/Makefile:2:obj-y\t+= core.o\n"
} |
| 11/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30997 output: 34 thoughts: 59 Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue: **Investigating x86 Performance Event Build Process** Alright, let's see... the `arch/x86/events/Makefile` is clearly the key here, and I see it's got `obj-y += core.o probe.o utils.o` defined, contingent on `PERF_EVENTS` being enabled. So, that tells me the core components for x86 performance event handling are being pulled in. Now, I need to understand when and *how* `PERF_EVENTS` itself gets defined. It's a fundamental conditional, after all. Gotta jump over to `arch/x86/Kconfig` – that's where the architectural features are generally configured. That's where I will find the logic governing when the whole `arch/x86/events` directory, and therefore its associated objects, are brought into the build. That's the next logical step to map this out completely. |
| 12/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "arch/x86/events",
"PathPrefixes": [
"arch/x86/"
]
}Results: {
"Output": "arch/x86/Kconfig=1190=config X86_MCE_INJECT\n--\narch/x86/Kconfig-1197-\narch/x86/Kconfig:1198:source \"arch/x86/events/Kconfig\"\narch/x86/Kconfig-1199-\n"
} |
| 13/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31407 output: 34 thoughts: 16 Thoughts: Here's a summary of my thought process, as requested: **Navigating the x86 Event Configuration Landscape** Okay, so I need to understand something related to event configuration, and based on the context, the x86 architecture is likely the focus. My immediate instinct is to dive into the Kconfig files. This is where the core configuration options are defined for the kernel build. Specifically, I'm thinking about the x86 architecture and its performance events or PMU (Performance Monitoring Unit) features. That directory `arch/x86/events/` is the logical starting point. I expect to find options related to enabling or disabling specific PMU functionalities, defining event types, perhaps even fine-grained control over the hardware counters. The Kconfig file should illuminate the available options and their dependencies. I'm hoping it's well-structured, so I can quickly identify the relevant sections and the purpose of each defined symbol. Let's see what gems are in there! |
| 14/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "menu \"Performance monitoring\"",
"PathPrefixes": [
"arch/x86/events/"
]
}Results: {
"Output": "arch/x86/events/Kconfig-1-# SPDX-License-Identifier: GPL-2.0\narch/x86/events/Kconfig:2:menu \"Performance monitoring\"\narch/x86/events/Kconfig-3-\n"
} |
| 15/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31733 output: 36 thoughts: 16 Thoughts: Okay, let's dive into this. Here's my take on those instructions, as if they're my internal thought process: **Diving into the x86 Event Configuration** Right, let's take a look at `arch/x86/events/Kconfig`. I need to get a handle on how the x86 architecture defines its performance monitoring events. It's a critical part of understanding the underlying hardware performance counters. This Kconfig file will be the starting point to understand the configuration options related to event selection, enabling, and any other x86-specific details that are presented in the kernel's build system. From here I can understand how the available features are presented to the user. Hopefully, it is relatively straightforward and clearly structured. I need to get a high-level view and any hints to the implementation details, from the code, will be useful. |
| 16/2 | 2026/11/08 23:30 | tool | read-file |
0mArgs:{
"File": "arch/x86/events/Kconfig",
"FirstLine": 1,
"LineCount": 40
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0\n 2:\tmenu \"Performance monitoring\"\n 3:\t\n 4:\tconfig PERF_EVENTS_INTEL_UNCORE\n 5:\t\ttristate \"Intel uncore performance events\"\n 6:\t\tdepends on PERF_EVENTS \u0026\u0026 CPU_SUP_INTEL \u0026\u0026 PCI\n 7:\t\tdefault y\n 8:\t\thelp\n 9:\t\t Include support for Intel uncore performance events. These are\n 10:\t\t available on NehalemEX and more modern processors.\n 11:\t\n 12:\tconfig PERF_EVENTS_INTEL_RAPL\n 13:\t\ttristate \"Intel/AMD rapl performance events\"\n 14:\t\tdepends on PERF_EVENTS \u0026\u0026 (CPU_SUP_INTEL || CPU_SUP_AMD) \u0026\u0026 PCI\n 15:\t\tdefault y\n 16:\t\thelp\n 17:\t\t Include support for Intel and AMD rapl performance events for power\n 18:\t\t monitoring on modern processors.\n 19:\t\n 20:\tconfig PERF_EVENTS_INTEL_CSTATE\n 21:\t\ttristate \"Intel cstate performance events\"\n 22:\t\tdepends on PERF_EVENTS \u0026\u0026 CPU_SUP_INTEL \u0026\u0026 PCI\n 23:\t\tdefault y\n 24:\t\thelp\n 25:\t\t Include support for Intel cstate performance events for power\n 26:\t\t monitoring on modern processors.\n 27:\t\n 28:\tconfig PERF_EVENTS_AMD_POWER\n 29:\t\tdepends on PERF_EVENTS \u0026\u0026 CPU_SUP_AMD\n 30:\t\ttristate \"AMD Processor Power Reporting Mechanism\"\n 31:\t\thelp\n 32:\t\t Provide power reporting mechanism support for AMD processors.\n 33:\t\t Currently, it leverages X86_FEATURE_ACC_POWER\n 34:\t\t (CPUID Fn8000_0007_EDX[12]) interface to calculate the\n 35:\t\t average power consumption on Family 15h processors.\n 36:\t\n 37:\tconfig PERF_EVENTS_AMD_UNCORE\n 38:\t\ttristate \"AMD Uncore performance events\"\n 39:\t\tdepends on PERF_EVENTS \u0026\u0026 CPU_SUP_AMD\n 40:\t\tdefault y\n"
} |
| 17/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32547 output: 35 thoughts: 20 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating x86 Event Handling** Alright, let's dive in. My immediate thought is to check `arch/x86/Makefile`. Specifically, I'm zeroing in on the relationship between that Makefile and `arch/x86/events`. I suspect there's probably a build dependency or compilation strategy defined there that relates to how the kernel handles performance monitoring events on x86 architectures. I'm looking for clues about how those events are enabled, how they interact with the build system, and any potential configuration options related to them. This is likely where I'll find the glue that ties the event-related code together and controls its inclusion during the kernel build. I'll need to understand that build flow and see if there are any specific make rules or flags that could be relevant to debugging or modifying the event-related features. |
| 18/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "arch/x86/events",
"PathPrefixes": [
"arch/x86/Makefile"
]
}Results: {
"Output": "no matches"
} |
| 19/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32813 output: 39 thoughts: 20 Thoughts: Here's my thought process, as it stands: **Locating x86 Event Inclusion** Okay, so I need to figure out where the `arch/x86/events` directory gets included within the kernel's build system. This is a standard first step when working with kernel code; understanding the build dependencies is paramount. Knowing the Makefiles that reference this path will give me a clear picture of how the event-related code is incorporated into the overall build process. This will let me identify the relevant configurations, compile flags, and ultimately, the pieces of the kernel that interact with the x86 event subsystem. I need to find the specific targets, dependencies, and potential conditional inclusions. Time to dig in and check where those Makefiles are. |
| 20/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "events/",
"PathPrefixes": [
"arch/x86/Makefile",
"arch/x86/"
]
}Results: {
"Output": "arch/x86/Kbuild=10=obj-y += entry/\narch/x86/Kbuild-11-\narch/x86/Kbuild:12:obj-$(CONFIG_PERF_EVENTS) += events/\narch/x86/Kbuild-13-\n--\narch/x86/Kconfig=1190=config X86_MCE_INJECT\n--\narch/x86/Kconfig-1197-\narch/x86/Kconfig:1198:source \"arch/x86/events/Kconfig\"\narch/x86/Kconfig-1199-\n--\narch/x86/include/asm/mmu_context.h-8-\narch/x86/include/asm/mmu_context.h:9:#include \u003ctrace/events/tlb.h\u003e\narch/x86/include/asm/mmu_context.h-10-\n--\narch/x86/include/asm/xen/hypercall.h-43-\narch/x86/include/asm/xen/hypercall.h:44:#include \u003ctrace/events/xen.h\u003e\narch/x86/include/asm/xen/hypercall.h-45-\n--\narch/x86/kernel/cpu/mce/core.c=64=static DEFINE_MUTEX(mce_sysfs_mutex);\n--\narch/x86/kernel/cpu/mce/core.c-66-#define CREATE_TRACE_POINTS\narch/x86/kernel/cpu/mce/core.c:67:#include \u003ctrace/events/mce.h\u003e\narch/x86/kernel/cpu/mce/core.c-68-\n--\narch/x86/kernel/cpu/resctrl/pseudo_lock.c-23-\narch/x86/kernel/cpu/resctrl/pseudo_lock.c:24:#include \"../../events/perf_event.h\" /* For X86_CONFIG() */\narch/x86/kernel/cpu/resctrl/pseudo_lock.c-25-#include \"internal.h\"\n--\narch/x86/kernel/nmi.c-41-#define CREATE_TRACE_POINTS\narch/x86/kernel/nmi.c:42:#include \u003ctrace/events/nmi.h\u003e\narch/x86/kernel/nmi.c-43-\n--\narch/x86/kernel/paravirt-spinlocks.c-9-#include \u003clinux/jump_label.h\u003e\narch/x86/kernel/paravirt-spinlocks.c:10:#include \u003ctrace/events/lock.h\u003e\narch/x86/kernel/paravirt-spinlocks.c-11-\n--\narch/x86/kernel/process.c-28-#include \u003clinux/static_call.h\u003e\narch/x86/kernel/process.c:29:#include \u003ctrace/events/power.h\u003e\narch/x86/kernel/process.c-30-#include \u003clinux/hw_breakpoint.h\u003e\n--\narch/x86/kvm/fpu.h-7-\narch/x86/kvm/fpu.h:8:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/fpu.h-9-\n--\narch/x86/kvm/hyperv.c-38-#include \u003casm/mshyperv.h\u003e\narch/x86/kvm/hyperv.c:39:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/hyperv.c-40-\n--\narch/x86/kvm/mmu/mmu.c=123=static int max_tdp_level __read_mostly;\n--\narch/x86/kvm/mmu/mmu.c-126-\narch/x86/kvm/mmu/mmu.c:127:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/mmu/mmu.c-128-\n--\narch/x86/kvm/mmu/tdp_mmu.c-11-#include \u003casm/cmpxchg.h\u003e\narch/x86/kvm/mmu/tdp_mmu.c:12:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/mmu/tdp_mmu.c-13-\n--\narch/x86/kvm/svm/svm.c-48-\narch/x86/kvm/svm/svm.c:49:#include \u003ctrace/events/ipi.h\u003e\narch/x86/kvm/svm/svm.c-50-\n--\narch/x86/kvm/vmx/nested.c=5077=void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,\n--\narch/x86/kvm/vmx/nested.c-5156-\t/*\narch/x86/kvm/vmx/nested.c:5157:\t * Drop events/exceptions that were queued for re-injection to L2\narch/x86/kvm/vmx/nested.c-5158-\t * (picked up via vmx_complete_interrupts()), as well as exceptions\narch/x86/kvm/vmx/nested.c-5159-\t * that were pending for L2. Note, this must NOT be hoisted above\narch/x86/kvm/vmx/nested.c:5160:\t * prepare_vmcs12(), events/exceptions queued for re-injection need to\narch/x86/kvm/vmx/nested.c-5161-\t * be captured in vmcs12 (see vmcs12_save_pending_event()).\n--\narch/x86/kvm/vmx/tdx.c-19-#include \"irq.h\"\narch/x86/kvm/vmx/tdx.c:20:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/vmx/tdx.c-21-#include \"trace.h\"\n--\narch/x86/kvm/vmx/vmx.c-53-\narch/x86/kvm/vmx/vmx.c:54:#include \u003ctrace/events/ipi.h\u003e\narch/x86/kvm/vmx/vmx.c-55-\n--\narch/x86/kvm/x86.c-66-\narch/x86/kvm/x86.c:67:#include \u003ctrace/events/ipi.h\u003e\narch/x86/kvm/x86.c:68:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/x86.c-69-\n--\narch/x86/kvm/xen.c-18-\narch/x86/kvm/xen.c:19:#include \u003ctrace/events/kvm.h\u003e\narch/x86/kvm/xen.c-20-#include \u003cxen/interface/xen.h\u003e\n--\narch/x86/mm/fault.c-40-#define CREATE_TRACE_POINTS\narch/x86/mm/fault.c:41:#include \u003ctrace/events/exceptions.h\u003e\narch/x86/mm/fault.c-42-\n--\narch/x86/mm/init.c-36-#define CREATE_TRACE_POINTS\narch/x86/mm/init.c:37:#include \u003ctrace/events/tlb.h\u003e\narch/x86/mm/init.c-38-\n--\narch/x86/xen/mmu_pv.c-57-\narch/x86/xen/mmu_pv.c:58:#include \u003ctrace/events/xen.h\u003e\narch/x86/xen/mmu_pv.c-59-\n--\narch/x86/xen/pmu.c-16-/* x86_pmu.handle_irq definition */\narch/x86/xen/pmu.c:17:#include \"../events/perf_event.h\"\narch/x86/xen/pmu.c-18-\n--\narch/x86/xen/trace.c=12=static const char *xen_hypercall_name(unsigned op)\n--\narch/x86/xen/trace.c-20-#define CREATE_TRACE_POINTS\narch/x86/xen/trace.c:21:#include \u003ctrace/events/xen.h\u003e\n--\narch/x86/xen/xen-ops.h-16-\narch/x86/xen/xen-ops.h:17:#include \u003ctrace/events/xen.h\u003e\narch/x86/xen/xen-ops.h-18-\n"
} |
| 21/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35258 output: 36 thoughts: 91 Thoughts: Here's my thought process, as I see it: **Examining Kernel Build Dependencies for Performance Monitoring in a KVM Environment** Okay, so I'm seeing this line: `obj-$(CONFIG_PERF_EVENTS) += events/` in `arch/x86/Kbuild`. Right, that's telling the build system to include the `events/` directory in the `x86` architecture if `CONFIG_PERF_EVENTS` is enabled. That's for performance events, obviously. Now, I know KVM is in the picture. And KVM relies on, at a bare minimum, `CONFIG_KVM`. I need to be careful, because KVM also requires either `CONFIG_KVM_INTEL` or `CONFIG_KVM_AMD`, depending on the host hardware. And, crucially for what I'm looking at, `CONFIG_PERF_EVENTS` is also a dependency, which makes sense since we're dealing with performance monitoring here. So, where's the best place to confirm all these dependencies? The logical place is within KVM's configuration itself. I'll need to check the Kconfig file for KVM, specifically `arch/x86/kvm/Kconfig` to see the conditions that govern these KVM options. Let's see how those dependencies are set up in detail. |
| 22/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "config KVM$",
"PathPrefixes": [
"arch/x86/kvm/Kconfig"
]
}Results: {
"Output": "arch/x86/kvm/Kconfig=20=config KVM_X86\n--\narch/x86/kvm/Kconfig-47-\narch/x86/kvm/Kconfig:48:config KVM\narch/x86/kvm/Kconfig-49-\ttristate \"Kernel-based Virtual Machine (KVM) support\"\n"
} |
| 23/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35782 output: 38 thoughts: 27 Thoughts: Okay, let's dive into this Kconfig file, specifically the x86 KVM configuration. I need to get a handle on the available options and dependencies within this section. Focusing on lines 20 to 120 seems like a good place to start – that should give me a solid overview without getting lost in the weeds. I'm looking for the core KVM features and their associated knobs and levers. What are the key compilation flags? How are they structured in terms of dependencies? Are there any obvious areas where customization is possible or where there might be architectural constraints? I'll pay close attention to the `bool`, `tristate`, and `select` directives, and how they relate to other configuration symbols, especially those related to CPU features and virtualization extensions. Ultimately, my goal is to understand the flexibility and potential implications of these settings. |
| 24/2 | 2026/11/08 23:30 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/Kconfig",
"FirstLine": 20,
"LineCount": 90
}Results: {
"Contents": " 20:\tconfig KVM_X86\n 21:\t\tdef_tristate KVM if (KVM_INTEL != n || KVM_AMD != n)\n 22:\t\tselect KVM_COMMON\n 23:\t\tselect KVM_ELIDE_TLB_FLUSH_IF_YOUNG\n 24:\t\tselect KVM_MMU_LOCKLESS_AGING\n 25:\t\tselect HAVE_KVM_IRQCHIP\n 26:\t\tselect HAVE_KVM_PFNCACHE\n 27:\t\tselect HAVE_KVM_DIRTY_RING_TSO\n 28:\t\tselect HAVE_KVM_DIRTY_RING_ACQ_REL\n 29:\t\tselect HAVE_KVM_IRQ_BYPASS\n 30:\t\tselect HAVE_KVM_IRQ_ROUTING\n 31:\t\tselect VHOST_TASK\n 32:\t\tselect KVM_ASYNC_PF\n 33:\t\tselect USER_RETURN_NOTIFIER\n 34:\t\tselect SCHED_INFO\n 35:\t\tselect PERF_EVENTS\n 36:\t\tselect GUEST_PERF_EVENTS\n 37:\t\tselect PERF_GUEST_MEDIATED_PMU\n 38:\t\tselect HAVE_KVM_CPU_RELAX_INTERCEPT\n 39:\t\tselect VIRT_XFER_TO_GUEST_WORK\n 40:\t\tselect KVM_GENERIC_DIRTYLOG_READ_PROTECT\n 41:\t\tselect KVM_VFIO\n 42:\t\tselect HAVE_KVM_PM_NOTIFIER if PM\n 43:\t\tselect KVM_GENERIC_HARDWARE_ENABLING\n 44:\t\tselect KVM_GENERIC_PRE_FAULT_MEMORY\n 45:\t\tselect KVM_WERROR if WERROR\n 46:\t\tselect KVM_GUEST_MEMFD if X86_64\n 47:\t\n 48:\tconfig KVM\n 49:\t\ttristate \"Kernel-based Virtual Machine (KVM) support\"\n 50:\t\tdepends on X86_LOCAL_APIC\n 51:\t\thelp\n 52:\t\t Support hosting fully virtualized guest machines using hardware\n 53:\t\t virtualization extensions. You will need a fairly recent\n 54:\t\t processor equipped with virtualization extensions. You will also\n 55:\t\t need to select one or more of the processor modules below.\n 56:\t\n 57:\t\t This module provides access to the hardware capabilities through\n 58:\t\t a character device node named /dev/kvm.\n 59:\t\n 60:\t\t To compile this as a module, choose M here: the module\n 61:\t\t will be called kvm.\n 62:\t\n 63:\t\t If unsure, say N.\n 64:\t\n 65:\tconfig KVM_WERROR\n 66:\t\tbool \"Compile KVM with -Werror\"\n 67:\t\t# Disallow KVM's -Werror if KASAN is enabled, e.g. to guard against\n 68:\t\t# randomized configs from selecting KVM_WERROR=y, which doesn't play\n 69:\t\t# nice with KASAN. KASAN builds generates warnings for the default\n 70:\t\t# FRAME_WARN, i.e. KVM_WERROR=y with KASAN=y requires special tuning.\n 71:\t\t# Building KVM with -Werror and KASAN is still doable via enabling\n 72:\t\t# the kernel-wide WERROR=y.\n 73:\t\tdepends on KVM_X86 \u0026\u0026 ((EXPERT \u0026\u0026 !KASAN) || WERROR)\n 74:\t\thelp\n 75:\t\t Add -Werror to the build flags for KVM.\n 76:\t\n 77:\t\t If in doubt, say \"N\".\n 78:\t\n 79:\tconfig KVM_PRIVATE_MEMATTR_VM\n 80:\t\tbool \"Enable per-VM PRIVATE vs. SHARED attributes (for CoCo VMs)\"\n 81:\t\tdepends on KVM_SW_PROTECTED_VM || KVM_INTEL_TDX || KVM_AMD_SEV\n 82:\t\tselect KVM_VM_MEMORY_ATTRIBUTES\n 83:\t\tdefault KVM_INTEL_TDX || KVM_AMD_SEV\n 84:\t\thelp\n 85:\t\t Enable support for tracking PRIVATE vs. SHARED memory using per-VM\n 86:\t\t memory attributes. Using per-VM attributes is deprecated in favor of\n 87:\t\t tracking PRIVATE state in guest_memfd. Select this if you need to run\n 88:\t\t CoCo VMs using a VMM that doesn't support guest_memfd memory\n 89:\t\t attributes.\n 90:\t\n 91:\t\t If unsure, say Y.\n 92:\t\n 93:\tconfig KVM_SW_PROTECTED_VM\n 94:\t\tbool \"Enable support for KVM software-protected VMs\"\n 95:\t\tdepends on EXPERT\n 96:\t\tdepends on KVM_X86 \u0026\u0026 X86_64\n 97:\t\thelp\n 98:\t\t Enable support for KVM software-protected VMs. Currently, software-\n 99:\t\t protected VMs are purely a development and testing vehicle for\n 100:\t\t KVM_CREATE_GUEST_MEMFD. Attempting to run a \"real\" VM workload as a\n 101:\t\t software-protected VM will fail miserably.\n 102:\t\n 103:\t\t If unsure, say \"N\".\n 104:\t\n 105:\tconfig KVM_INTEL\n 106:\t\ttristate \"KVM for Intel (and compatible) processors support\"\n 107:\t\tdepends on KVM \u0026\u0026 IA32_FEAT_CTL\n 108:\t\tselect X86_FRED if X86_64\n 109:\t\thelp\n"
} |
| 25/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37495 output: 48 thoughts: 164 Thoughts: **Analyzing KVM and Performance Monitoring Configurations** Okay, let's break this down. First, I'm observing a dependency chain here, specifically within the KVM (Kernel-based Virtual Machine) configuration. I see that `KVM_X86` is selecting a few important options: `PERF_EVENTS`, `GUEST_PERF_EVENTS`, and `PERF_GUEST_MEDIATED_PMU`. That's a key starting point because it links performance monitoring, both host and guest, with this mediated PMU functionality. Now, I'm also noticing that `KVM_INTEL` has the dependency `KVM && IA32_FEAT_CTL`. Right, so it requires both base KVM and the IA32 feature control. On the other hand, `KVM_AMD` simply depends on `KVM`. This confirms a hierarchy: `KVM`, then `KVM_INTEL` and `KVM_AMD`. Now, I need to make sure I haven't missed anything. Are there any other config options that directly relate to mediated PMU? I'm going to scan the existing configurations to make sure I haven't overlooked any other relevant settings. Let's quickly check line 37 again... Yep, there it is: `select PERF_GUEST_MEDIATED_PMU`. That reinforces the importance of that specific configuration. I need to make sure I have the full picture, so I'm going to `grep` for `PERF_GUEST_MEDIATED_PMU` and also try `enable_mediated_pmu` to see if there are any other direct mentions or enabling flags that might be relevant. This should help me ensure I fully understand how mediated PMU is being handled within the KVM ecosystem. |
| 26/2 | 2026/11/08 23:30 | tool | grepper |
0mArgs:{
"Expression": "PERF_GUEST_MEDIATED_PMU|enable_mediated_pmu",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/Kconfig=20=config KVM_X86\n--\narch/x86/kvm/Kconfig-36-\tselect GUEST_PERF_EVENTS\narch/x86/kvm/Kconfig:37:\tselect PERF_GUEST_MEDIATED_PMU\narch/x86/kvm/Kconfig-38-\tselect HAVE_KVM_CPU_RELAX_INTERCEPT\n--\narch/x86/kvm/pmu.c=40=module_param(enable_pmu, bool, 0444);\n--\narch/x86/kvm/pmu.c-42-/* Enable/disabled mediated PMU virtualization. */\narch/x86/kvm/pmu.c:43:bool __read_mostly enable_mediated_pmu;\narch/x86/kvm/pmu.c:44:EXPORT_SYMBOL_FOR_KVM_INTERNAL(enable_mediated_pmu);\narch/x86/kvm/pmu.c-45-\n--\narch/x86/kvm/pmu.c=133=void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n--\narch/x86/kvm/pmu.c-164-\narch/x86/kvm/pmu.c:165:\tif (!enable_pmu || !enable_mediated_pmu || !kvm_host_pmu.mediated ||\narch/x86/kvm/pmu.c-166-\t !pmu_ops-\u003eis_mediated_pmu_supported(\u0026kvm_host_pmu))\narch/x86/kvm/pmu.c:167:\t\tenable_mediated_pmu = false;\narch/x86/kvm/pmu.c-168-\narch/x86/kvm/pmu.c:169:\tif (!enable_mediated_pmu)\narch/x86/kvm/pmu.c-170-\t\tpmu_ops-\u003ewrite_global_ctrl = NULL;\n--\narch/x86/kvm/pmu.c-183-\t */\narch/x86/kvm/pmu.c:184:\tif (is_intel \u0026\u0026 enable_mediated_pmu \u0026\u0026 kvm_pmu_cap.version \u003e= 5)\narch/x86/kvm/pmu.c-185-\t\tkvm_pmu_cap.version = 5;\n--\narch/x86/kvm/pmu.h=65=extern bool enable_pmu;\narch/x86/kvm/pmu.h:66:extern bool enable_mediated_pmu;\narch/x86/kvm/pmu.h-67-\n--\narch/x86/kvm/pmu.h=86=static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.h-87-{\narch/x86/kvm/pmu.h:88:\treturn enable_mediated_pmu \u0026\u0026 vcpu_to_pmu(vcpu)-\u003eversion;\narch/x86/kvm/pmu.h-89-}\n--\narch/x86/kvm/svm/svm.c=178=module_param(vnmi, bool, 0444);\narch/x86/kvm/svm/svm.c-179-\narch/x86/kvm/svm/svm.c:180:module_param(enable_mediated_pmu, bool, 0444);\narch/x86/kvm/svm/svm.c-181-\n--\narch/x86/kvm/svm/svm.c=760=static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-766-\narch/x86/kvm/svm/svm.c:767:\tif (!enable_mediated_pmu)\narch/x86/kvm/svm/svm.c-768-\t\treturn;\n--\narch/x86/kvm/vmx/capabilities.h=399=static inline bool vmx_pebs_supported(void)\n--\narch/x86/kvm/vmx/capabilities.h-401-\treturn boot_cpu_has(X86_FEATURE_PEBS) \u0026\u0026 kvm_pmu_cap.pebs_ept \u0026\u0026\narch/x86/kvm/vmx/capabilities.h:402:\t !enable_mediated_pmu;\narch/x86/kvm/vmx/capabilities.h-403-}\n--\narch/x86/kvm/vmx/vmx.c=163=module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);\narch/x86/kvm/vmx/vmx.c-164-\narch/x86/kvm/vmx/vmx.c:165:module_param(enable_mediated_pmu, bool, 0444);\narch/x86/kvm/vmx/vmx.c-166-\n--\narch/x86/kvm/vmx/vmx.c=4238=static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4249-\narch/x86/kvm/vmx/vmx.c:4250:\tif (!enable_mediated_pmu)\narch/x86/kvm/vmx/vmx.c-4251-\t\treturn;\n--\narch/x86/kvm/vmx/vmx.c=4462=void vmx_set_constant_host_state(struct vcpu_vmx *vmx)\n--\narch/x86/kvm/vmx/vmx.c-4549-\t */\narch/x86/kvm/vmx/vmx.c:4550:\tif (enable_mediated_pmu)\narch/x86/kvm/vmx/vmx.c-4551-\t\tvmcs_write64(HOST_IA32_PERF_GLOBAL_CTRL, 0);\n--\narch/x86/kvm/vmx/vmx.c=7887=static __init u64 vmx_get_perf_capabilities(void)\n--\narch/x86/kvm/vmx/vmx.c-7898-\tif (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) \u0026\u0026\narch/x86/kvm/vmx/vmx.c:7899:\t !enable_mediated_pmu) {\narch/x86/kvm/vmx/vmx.c-7900-\t\tx86_perf_get_lbr(\u0026vmx_lbr_caps);\n--\narch/x86/kvm/x86.c=7061=int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)\n--\narch/x86/kvm/x86.c-7183-\t__kvm_register_perf_callbacks(ops-\u003ehandle_intel_pt_intr,\narch/x86/kvm/x86.c:7184:\t\t\t\t enable_mediated_pmu ? kvm_handle_guest_mediated_pmi : NULL);\narch/x86/kvm/x86.c-7185-\n--\narch/x86/kvm/x86.c=9394=int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)\n--\narch/x86/kvm/x86.c-9416-\narch/x86/kvm/x86.c:9417:\tif (enable_mediated_pmu \u0026\u0026 kvm-\u003earch.enable_pmu \u0026\u0026\narch/x86/kvm/x86.c-9418-\t !kvm-\u003earch.created_mediated_pmu) {\n"
} |
| 27/2 | 2026/11/08 23:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40195 output: 41 thoughts: 37 Thoughts: Here's a summary of my thought process, as requested: **Investigating the `enable_mediated_pmu` Module Parameter** Okay, so I'm trying to get a handle on this `enable_mediated_pmu` module parameter. I need to understand its default value – whether it's set to true or false by default. My go-to approach is to dive straight into the source code where it's likely defined and used. Considering this is related to x86 and KVM, `arch/x86/kvm/pmu.c` seems like the most logical place to start looking. I'll need to open that file and quickly scan for the parameter definition and its default initialization. Knowing the default value is crucial for understanding how the system behaves without explicit configuration. Then I'll be able to trace its usage to learn what it controls. |
| 28/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "enable_mediated_pmu =",
"PathPrefixes": [
"arch/x86/kvm/pmu.c"
]
}Results: {
"Output": "arch/x86/kvm/pmu.c=133=void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n--\narch/x86/kvm/pmu.c-166-\t !pmu_ops-\u003eis_mediated_pmu_supported(\u0026kvm_host_pmu))\narch/x86/kvm/pmu.c:167:\t\tenable_mediated_pmu = false;\narch/x86/kvm/pmu.c-168-\n"
} |
| 29/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40609 output: 37 thoughts: 37 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Tracing the Initialization of `enable_mediated_pmu`** Alright, so I need to understand how the `enable_mediated_pmu` variable gets set up within the KVM/VMX or KVM/SVM code. It's time to dive into the architecture-specific initialization, specifically within `arch/x86/kvm/vmx/vmx.c` and `arch/x86/kvm/svm/svm.c`. I expect to find the declaration and the *point* of initialization in either of these locations. I'll need to understand the control flow to figure out the conditions which are being used to determine the initial value. Specifically, I need to know: Is this a configuration-time setting, determined by features detected at boot, or is it a dynamic setting adjusted during runtime by the hypervisor? I have a feeling the initialization is probably feature dependent, tied to something like the availability of certain MSRs or specific CPU capabilities related to PMU virtualization support. I'm guessing that the code will probe for hardware features or check for the host CPU support for some kind of PMU virtualization extensions or MSR support. Now, let's get down to the nitty-gritty and see where it's actually getting assigned its initial value. |
| 30/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "bool enable_mediated_pmu",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/pmu.h=65=extern bool enable_pmu;\narch/x86/kvm/pmu.h:66:extern bool enable_mediated_pmu;\narch/x86/kvm/pmu.h-67-\n"
} |
| 31/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41055 output: 60 thoughts: 28 Thoughts: Here's my take on those thoughts: **Investigating the "enable_mediated_pmu" Parameter** Okay, so I need to get a handle on `enable_mediated_pmu`. My gut feeling is that it's going to be crucial for understanding performance monitoring in a virtualized environment. The fact that it's probably *mediated* suggests some level of control or abstraction being injected between the guest and the hardware PMU (Performance Monitoring Unit). My immediate thought is, where is this actually defined? I'm betting it's going to be in the core virtualization code. Therefore, I'll need to check the relevant files for both VMX and SVM implementations. So, I need to open up `vmx/vmx.c` and `svm/svm.c` and see what's what. These are the two primary virtualization technologies I'll be working with, and I'll need to understand how each handles this parameter. Let's see if it's a simple boolean, a complex structure, or something else entirely. The context and usage in those files will tell me everything. This will be the first step to figuring out how the mediated PMU impacts performance and if it will affect the guest's performance metrics. |
| 32/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "enable_mediated_pmu",
"PathPrefixes": [
"arch/x86/kvm/vmx/vmx.c",
"arch/x86/kvm/svm/svm.c"
]
}Results: {
"Output": "arch/x86/kvm/svm/svm.c=178=module_param(vnmi, bool, 0444);\narch/x86/kvm/svm/svm.c-179-\narch/x86/kvm/svm/svm.c:180:module_param(enable_mediated_pmu, bool, 0444);\narch/x86/kvm/svm/svm.c-181-\n--\narch/x86/kvm/svm/svm.c=760=static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-766-\narch/x86/kvm/svm/svm.c:767:\tif (!enable_mediated_pmu)\narch/x86/kvm/svm/svm.c-768-\t\treturn;\n--\narch/x86/kvm/vmx/vmx.c=163=module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);\narch/x86/kvm/vmx/vmx.c-164-\narch/x86/kvm/vmx/vmx.c:165:module_param(enable_mediated_pmu, bool, 0444);\narch/x86/kvm/vmx/vmx.c-166-\n--\narch/x86/kvm/vmx/vmx.c=4238=static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4249-\narch/x86/kvm/vmx/vmx.c:4250:\tif (!enable_mediated_pmu)\narch/x86/kvm/vmx/vmx.c-4251-\t\treturn;\n--\narch/x86/kvm/vmx/vmx.c=4462=void vmx_set_constant_host_state(struct vcpu_vmx *vmx)\n--\narch/x86/kvm/vmx/vmx.c-4549-\t */\narch/x86/kvm/vmx/vmx.c:4550:\tif (enable_mediated_pmu)\narch/x86/kvm/vmx/vmx.c-4551-\t\tvmcs_write64(HOST_IA32_PERF_GLOBAL_CTRL, 0);\n--\narch/x86/kvm/vmx/vmx.c=7887=static __init u64 vmx_get_perf_capabilities(void)\n--\narch/x86/kvm/vmx/vmx.c-7898-\tif (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) \u0026\u0026\narch/x86/kvm/vmx/vmx.c:7899:\t !enable_mediated_pmu) {\narch/x86/kvm/vmx/vmx.c-7900-\t\tx86_perf_get_lbr(\u0026vmx_lbr_caps);\n"
} |
| 33/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42242 output: 41 thoughts: 644 Thoughts: **Analyzing the KVM PMU Patch: A Deep Dive** Okay, so I'm looking at this patch and my initial thought is, even though `enable_mediated_pmu` defaults to false, the code changes here are going to impact both the emulated PMU and the mediated PMU paths. That's the first critical point. This means I need to be thorough. I started by listing key KVM files that handle PMU interactions. I made sure to check: `cpuid.c` because of how counter numbers are calculated, specifically `__do_cpuid_func` which seems to use `find_first_zero_bit`, and masks such as `fixed_cntr_mask`, which are always good places to check when looking at PMU configurations. Then, of course, I checked `msrs.c` looking for `kvm_probe_msr_to_save`, paying close attention to `cntr_mask64` and `fixed_cntr_mask64`. Then, I checked `pmu.c`, which contains the core functions: `kvm_init_pmu_capability`, `kvm_pmu_reset`, `kvm_pmu_refresh`, `kvm_pmu_init`, `kvm_pmu_cleanup`, and the MSR access functions `kvm_pmu_get_msr` and `kvm_pmu_set_msr`, as well as the guest PMC handling functions `kvm_pmu_load_guest_pmcs` and `kvm_pmu_put_guest_pmcs`. I also made sure to look at the architecture-specific PMU implementations: `svm/pmu.c` for AMD, with its `amd_pmu_get_pmc`, `amd_check_rdpmc_early`, `amd_is_valid_msr`, `amd_pmu_set_msr`, and `amd_pmu_refresh`, and `vmx/pmu_intel.c` for Intel with `reprogram_fixed_counters`, `intel_rdpmc_ecx_to_pmc`, `intel_is_valid_msr`, `intel_pmu_get_msr`, `intel_pmu_set_msr`, `intel_pmu_refresh`, and `intel_pmu_get_global_inuse`. My experience tells me all these functions will be called regardless when a guest vCPU is configured with PMU, or when the MSRs are accessed, or CPUID is configured. Of course, `enable_mediated_pmu=true` will also enable the mediated PMU. Now, my focus shifts to the patch itself. To understand the patch's immediate impact, I've got to identify the specific functions that were added or modified directly. This means checking the FocusSymbols, then I will make a list of functions touched by the diff. I've already noted `perf_get_x86_pmu_capability` in `arch/x86/events/core.c`. Then, I also found that `__do_cpuid_func` in `arch/x86/kvm/cpuid.c` (KVM CPUID handling) looks touched. That's interesting, and also that it's a static inline function. Because of that, I need to check its callers, which I think are `kvm_dev_ioctl_get_supported_cpuid` and `kvm_vcpu_ioctl_get_cpuid2`. Okay, I'll need to check `__do_cpuid_func` in `arch/x86/kvm/cpuid.c`. That's where I will start, to see what impact the changes here have. |
| 34/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "__do_cpuid_func",
"PathPrefixes": [
"arch/x86/kvm/cpuid.c"
]
}Results: {
"Output": "arch/x86/kvm/cpuid.c=1330=static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,\n--\narch/x86/kvm/cpuid.c-1342-\tcase 0x40000000:\narch/x86/kvm/cpuid.c:1343:\t\t/* Hypervisor leaves are always synthesized by __do_cpuid_func. */\narch/x86/kvm/cpuid.c-1344-\t\treturn entry;\n--\narch/x86/kvm/cpuid.c-1347-\t\t/*\narch/x86/kvm/cpuid.c:1348:\t\t * 0x80000021 is sometimes synthesized by __do_cpuid_func, which\narch/x86/kvm/cpuid.c-1349-\t\t * would result in out-of-bounds calls to do_host_cpuid.\n--\narch/x86/kvm/cpuid.c=1373=static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, u32 index,\n--\narch/x86/kvm/cpuid.c-1429-\narch/x86/kvm/cpuid.c:1430:static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)\narch/x86/kvm/cpuid.c-1431-{\n--\narch/x86/kvm/cpuid.c-1438-\narch/x86/kvm/cpuid.c:1439:static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)\narch/x86/kvm/cpuid.c-1440-{\n--\narch/x86/kvm/cpuid.c=1947=static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,\n--\narch/x86/kvm/cpuid.c-1950-\tif (type == KVM_GET_EMULATED_CPUID)\narch/x86/kvm/cpuid.c:1951:\t\treturn __do_cpuid_func_emulated(array, func);\narch/x86/kvm/cpuid.c-1952-\narch/x86/kvm/cpuid.c:1953:\treturn __do_cpuid_func(array, func);\narch/x86/kvm/cpuid.c-1954-}\n"
} |
| 35/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44450 output: 40 thoughts: 30 Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind the user is an expert: **Investigating `kvm_dev_ioctl_get_cpuid`'s Call Chain** Right, let's get down to brass tacks. I need to quickly ascertain the call hierarchy, specifically if `kvm_dev_ioctl_get_cpuid` is a direct caller of `do_cpuid_func`. I need to figure out the exact relationship between the two. This is standard operating procedure, especially when dealing with KVM, and CPUID requests. Understanding this quickly is crucial to debugging or analyzing the execution path. Let's see if this code path is the culprit. |
| 36/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "do_cpuid_func",
"PathPrefixes": [
"arch/x86/kvm/cpuid.c"
]
}Results: {
"Output": "arch/x86/kvm/cpuid.c=1330=static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,\n--\narch/x86/kvm/cpuid.c-1342-\tcase 0x40000000:\narch/x86/kvm/cpuid.c:1343:\t\t/* Hypervisor leaves are always synthesized by __do_cpuid_func. */\narch/x86/kvm/cpuid.c-1344-\t\treturn entry;\n--\narch/x86/kvm/cpuid.c-1347-\t\t/*\narch/x86/kvm/cpuid.c:1348:\t\t * 0x80000021 is sometimes synthesized by __do_cpuid_func, which\narch/x86/kvm/cpuid.c-1349-\t\t * would result in out-of-bounds calls to do_host_cpuid.\n--\narch/x86/kvm/cpuid.c=1373=static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, u32 index,\n--\narch/x86/kvm/cpuid.c-1429-\narch/x86/kvm/cpuid.c:1430:static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)\narch/x86/kvm/cpuid.c-1431-{\n--\narch/x86/kvm/cpuid.c-1438-\narch/x86/kvm/cpuid.c:1439:static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)\narch/x86/kvm/cpuid.c-1440-{\n--\narch/x86/kvm/cpuid.c-1946-\narch/x86/kvm/cpuid.c:1947:static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,\narch/x86/kvm/cpuid.c-1948-\t\t\t unsigned int type)\n--\narch/x86/kvm/cpuid.c-1950-\tif (type == KVM_GET_EMULATED_CPUID)\narch/x86/kvm/cpuid.c:1951:\t\treturn __do_cpuid_func_emulated(array, func);\narch/x86/kvm/cpuid.c-1952-\narch/x86/kvm/cpuid.c:1953:\treturn __do_cpuid_func(array, func);\narch/x86/kvm/cpuid.c-1954-}\n--\narch/x86/kvm/cpuid.c=1958=static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,\n--\narch/x86/kvm/cpuid.c-1968-\narch/x86/kvm/cpuid.c:1969:\tr = do_cpuid_func(array, func, type);\narch/x86/kvm/cpuid.c-1970-\tif (r)\n--\narch/x86/kvm/cpuid.c-1974-\tfor (func = func + 1; func \u003c= limit; ++func) {\narch/x86/kvm/cpuid.c:1975:\t\tr = do_cpuid_func(array, func, type);\narch/x86/kvm/cpuid.c-1976-\t\tif (r)\n"
} |
| 37/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 45646 output: 40 thoughts: 14 Thoughts: Okay, let's dive into this. Here's my understanding, presented as a first-person account, focusing on what I'm thinking about the provided context: **Analyzing the `get_cpuid_func` Call** Alright, I need to understand what's happening with that `get_cpuid_func` call. It's obviously central to some process here, and I'm interested in the specifics. I know how CPUID works generally, so I need to pinpoint exactly *how* it's being leveraged in this code. My first question is, what's being passed to it? Are we looking for specific feature flags, vendor information, or something else entirely? The context will be key. I'll need to trace where `get_cpuid_func` is defined and see how its inputs are used. Are there conditional branches based on the CPUID results? That would be typical and what I would expect. Knowing the calling conventions, especially if it is being targeted at certain processor architectures, would be very helpful. Is it being used to enable certain optimizations or disable them based on CPU capabilities? Is this even the full code or is there more context? I'll probably be looking for a dependency graph of related function calls to see how the results are used, especially as it relates to system-level calls. All in all, I need to figure out the purpose of this call, its impact on the code's behavior, and ideally, what decisions are made based on the data it provides. |
| 38/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "get_cpuid_func",
"PathPrefixes": [
"arch/x86/kvm/cpuid.c"
]
}Results: {
"Output": "arch/x86/kvm/cpuid.c=1947=static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,\n--\narch/x86/kvm/cpuid.c-1957-\narch/x86/kvm/cpuid.c:1958:static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,\narch/x86/kvm/cpuid.c-1959-\t\t\t unsigned int type)\n--\narch/x86/kvm/cpuid.c=2010=int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,\n--\narch/x86/kvm/cpuid.c-2037-\tfor (i = 0; i \u003c ARRAY_SIZE(funcs); i++) {\narch/x86/kvm/cpuid.c:2038:\t\tr = get_cpuid_func(\u0026array, funcs[i], type);\narch/x86/kvm/cpuid.c-2039-\t\tif (r)\n"
} |
| 39/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46319 output: 36 thoughts: 211 Thoughts: **Investigating KVM PMU Symbol Visibility** Okay, so I'm looking at these KVM PMU functions and need to understand their visibility. The initial focus is on whether they are declared static or not. `kvm_dev_ioctl_get_cpuid` – that one is definitely non-static, which is what I expected. Now, what about some of the PMU-specific functions? Let me see... `kvm_pmu_refresh` is also non-static; it's declared as `void kvm_pmu_refresh(struct kvm_vcpu *vcpu)`. Same story for `kvm_pmu_set_msr`, also non-static, `int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)`. And the same is true of `kvm_pmu_get_msr`. But hold on, there's `intel_pmu_refresh`. Hmm, it appears to be a *static* function. Interesting. So, `intel_pmu_refresh` is static, which suggests it might have limited scope. I need to figure out where it's being used to understand its full implications. That means I need to check how it's referenced. Let's look at `intel_pmu_ops` to understand where `intel_pmu_refresh` is used. |
| 40/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "intel_pmu_ops",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/pmu.h=334=bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu);\narch/x86/kvm/pmu.h-335-\narch/x86/kvm/pmu.h:336:extern struct kvm_pmu_ops intel_pmu_ops;\narch/x86/kvm/pmu.h-337-extern struct kvm_pmu_ops amd_pmu_ops;\n--\narch/x86/kvm/vmx/main.c=1155=struct kvm_x86_init_ops vt_init_ops __initdata = {\n--\narch/x86/kvm/vmx/main.c-1159-\t.runtime_ops = \u0026vt_x86_ops,\narch/x86/kvm/vmx/main.c:1160:\t.pmu_ops = \u0026intel_pmu_ops,\narch/x86/kvm/vmx/main.c-1161-\t.nested_ops = \u0026vmx_nested_ops,\n--\narch/x86/kvm/vmx/pmu_intel.c=918=static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/pmu_intel.c-937-\narch/x86/kvm/vmx/pmu_intel.c:938:struct kvm_pmu_ops intel_pmu_ops __initdata = {\narch/x86/kvm/vmx/pmu_intel.c-939-\t.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,\n"
} |
| 41/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47345 output: 47 thoughts: 21 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing intel\_pmu\_ops and amd\_pmu\_ops Structures** Alright, let's dive into this. My initial thought process is to get a holistic view. I need to understand the landscape of `intel_pmu_ops` and `amd_pmu_ops`. I'm immediately thinking, "Okay, what are all the fields within these structures? What's the complete set of capabilities exposed via these specific interfaces?" I want to ensure I haven't missed anything crucial, so I'll be meticulously checking each member. This isn't just a surface-level scan; I'm digging for design choices and potential areas for performance optimization or, conversely, any limitations that could impact our work. Understanding the complete function pointers will be key. I'll be looking for commonality, differences, and, ultimately, how these architectures are trying to accomplish the same goals using potentially very different underlying hardware features. |
| 42/2 | 2026/11/08 23:31 | tool | grepper |
0mArgs:{
"Expression": "struct kvm_pmu_ops (intel|amd)_pmu_ops",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/pmu.h=334=bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu);\narch/x86/kvm/pmu.h-335-\narch/x86/kvm/pmu.h:336:extern struct kvm_pmu_ops intel_pmu_ops;\narch/x86/kvm/pmu.h:337:extern struct kvm_pmu_ops amd_pmu_ops;\narch/x86/kvm/pmu.h-338-#endif /* __KVM_X86_PMU_H */\n--\narch/x86/kvm/svm/pmu.c=276=static bool amd_pmc_is_disabled_in_current_mode(struct kvm_pmc *pmc)\n--\narch/x86/kvm/svm/pmu.c-306-\narch/x86/kvm/svm/pmu.c:307:struct kvm_pmu_ops amd_pmu_ops __initdata = {\narch/x86/kvm/svm/pmu.c-308-\t.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,\n--\narch/x86/kvm/vmx/pmu_intel.c=918=static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/pmu_intel.c-937-\narch/x86/kvm/vmx/pmu_intel.c:938:struct kvm_pmu_ops intel_pmu_ops __initdata = {\narch/x86/kvm/vmx/pmu_intel.c-939-\t.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,\n"
} |
| 43/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48132 output: 46 thoughts: 31 Thoughts: Okay, let's dive into this. I'm going to pull up `arch/x86/kvm/vmx/pmu_intel.c` and focus around line 938. My initial thought is likely around the area of handling performance monitoring units in Intel's VMX context. Considering the file and location, I'd expect to see code related to the Intel PMU's interaction with the Virtual Machine Extensions (VMX) technology, specifically in the context of KVM. I'll be looking for how the code manages the PMU state when entering and exiting VMX mode, handling event selection, counter configuration, and perhaps interrupt generation or interception related to PMU events within the guest. Understanding the specifics of this area is critical for performance analysis and monitoring inside virtualized environments; I'm expecting to see details on how the hardware resources are exposed or emulated. I'll be looking for clues on things like event filtering, counter overflow handling, and how guest PMU access is controlled and possibly virtualized for security or resource allocation. The specifics of the PMU virtualization will be a key point of interest. |
| 44/2 | 2026/11/08 23:31 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/vmx/pmu_intel.c",
"FirstLine": 935,
"LineCount": 30
}Results: {
"Contents": " 935:\t\t\twrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, 0);\n 936:\t}\n 937:\t\n 938:\tstruct kvm_pmu_ops intel_pmu_ops __initdata = {\n 939:\t\t.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,\n 940:\t\t.msr_idx_to_pmc = intel_msr_idx_to_pmc,\n 941:\t\t.is_valid_msr = intel_is_valid_msr,\n 942:\t\t.get_msr = intel_pmu_get_msr,\n 943:\t\t.set_msr = intel_pmu_set_msr,\n 944:\t\t.refresh = intel_pmu_refresh,\n 945:\t\t.init = intel_pmu_init,\n 946:\t\t.reset = intel_pmu_reset,\n 947:\t\t.deliver_pmi = intel_pmu_deliver_pmi,\n 948:\t\t.cleanup = intel_pmu_cleanup,\n 949:\t\n 950:\t\t.is_mediated_pmu_supported = intel_pmu_is_mediated_pmu_supported,\n 951:\t\t.mediated_load = intel_mediated_pmu_load,\n 952:\t\t.mediated_put = intel_mediated_pmu_put,\n 953:\t\t.write_global_ctrl = intel_pmu_write_global_ctrl,\n 954:\t\n 955:\t\t.EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT,\n 956:\t\t.MAX_NR_GP_COUNTERS = KVM_MAX_NR_INTEL_GP_COUNTERS,\n 957:\t\t.MIN_NR_GP_COUNTERS = 1,\n 958:\t\n 959:\t\t.PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL,\n 960:\t\t.GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0,\n 961:\t\t.GP_COUNTER_BASE = MSR_IA32_PMC0,\n 962:\t\t.FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,\n 963:\t\t.MSR_STRIDE = 1,\n 964:\t};\n"
} |
| 45/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49082 output: 43 thoughts: 29 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person: **Diving into the AMD PMU Implementation** Alright, let's get down to brass tacks. I'm thinking we should zero in on `arch/x86/kvm/svm/pmu.c`. Specifically, I'm interested in the area around line 307. I'm assuming we're dealing with the AMD Secure Virtual Machine's Performance Monitoring Unit (PMU) implementation there, and that location likely handles some crucial aspects. I'm probably looking for something related to event configuration, VM-Exit handling, or perhaps the interaction between the guest's PMU settings and the host's. It could be where the hypervisor is mediating access to the hardware counters, or maybe the code that's responsible for properly emulating or virtualizing the PMU registers. Ultimately, I'm expecting to find a piece of the puzzle that I can analyze to enhance or understand a facet of the current state of PMU virtualization on KVM/SVM. |
| 46/2 | 2026/11/08 23:31 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/pmu.c",
"FirstLine": 305,
"LineCount": 30
}Results: {
"Contents": " 305:\t}\n 306:\t\n 307:\tstruct kvm_pmu_ops amd_pmu_ops __initdata = {\n 308:\t\t.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,\n 309:\t\t.msr_idx_to_pmc = amd_msr_idx_to_pmc,\n 310:\t\t.check_rdpmc_early = amd_check_rdpmc_early,\n 311:\t\t.is_valid_msr = amd_is_valid_msr,\n 312:\t\t.get_msr = amd_pmu_get_msr,\n 313:\t\t.set_msr = amd_pmu_set_msr,\n 314:\t\t.refresh = amd_pmu_refresh,\n 315:\t\t.init = amd_pmu_init,\n 316:\t\t.pmc_is_disabled_in_current_mode = amd_pmc_is_disabled_in_current_mode,\n 317:\t\n 318:\t\t.is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported,\n 319:\t\t.mediated_load = amd_mediated_pmu_load,\n 320:\t\t.mediated_put = amd_mediated_pmu_put,\n 321:\t\n 322:\t\t.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,\n 323:\t\t.MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,\n 324:\t\t.MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,\n 325:\t\n 326:\t\t.PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL,\n 327:\t\t.GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0,\n 328:\t\t.GP_COUNTER_BASE = MSR_F15H_PERF_CTR0,\n 329:\t\t.FIXED_COUNTER_BASE = 0,\n 330:\t\t.MSR_STRIDE = 2,\n 331:\t};\n"
} |
| 47/2 | 2026/11/08 23:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 49975 output: 45 thoughts: 2391 Thoughts: **PMU Virtualization Symbol Analysis** Alright, let's break this down. My goal is to identify suitable symbols for fuzzing focus, and that means avoiding inline-able or ubiquitous functions, and target specific logic. Looking at this list of functions – `intel_pmu_refresh`, `amd_pmu_refresh`, etc. – it's all about PMU (Performance Monitoring Unit) virtualization, and that's precisely what I'm interested in. I need to make sure I am focusing on what's changed and avoiding being diluted by code I can't target with the fuzzer. First, the key distinction: I *know* that static functions in `.c` files, unlike static inline functions in headers, generally have distinct symbols in the kernel, especially with KALLSYMS enabled, which syzkaller typically does. But I still need to be careful about compile-time inlining – that's the enemy of accurate symbol targeting. Now, let’s go through these functions systematically. I’ve checked the source, and found the following: - `kvm_pmu_refresh`, `kvm_pmu_set_msr`, `kvm_pmu_get_msr`, and `kvm_mediated_pmu_load` are declared as non-static global symbols, so they are *definitely* distinct symbols. That's good. - `perf_get_x86_pmu_capability` is also a non-static global symbol. This is called only once, but may be worth including. - `intel_pmu_refresh`, `intel_pmu_set_msr`, `amd_pmu_refresh`, and `amd_pmu_set_msr` are function pointers assigned to `intel_pmu_ops` and `amd_pmu_ops`. Excellent! Because their addresses are taken, the compiler *cannot* inline them. These are guaranteed symbols in `kvm-intel.ko` / `kvm-amd.ko` or `vmlinux`. So, the key functions I am focused on for analysis, are: 1. `kvm_pmu_refresh` 2. `kvm_pmu_set_msr` 3. `kvm_pmu_get_msr` 4. `intel_pmu_refresh` 5. `intel_pmu_set_msr` 6. `intel_pmu_get_msr` 7. `amd_pmu_refresh` 8. `amd_pmu_set_msr` 9. `intel_mediated_pmu_load` 10. `intel_pmu_get_global_inuse` (This one is used only in intel\_pmu\_get\_msr; I need to make sure that the fact that `intel_pmu_get_msr` cannot be inlined, means that `intel_pmu_get_global_inuse` won't be either. These are not ubiquitous, they *directly* relate to PMU virtualization logic. They're good candidates for focus. `perf_get_x86_pmu_capability` is used for PMU capability initialization and may be relevant to the fuzzing focus. Now I should examine how `kvm_probe_msr_to_save` in `arch/x86/kvm/msrs.c` is used, to see if it makes the list. |
| 48/2 | 2026/11/08 23:32 | tool | grepper |
0mArgs:{
"Expression": "kvm_probe_msr_to_save",
"PathPrefixes": [
"arch/x86/kvm/msrs.c"
]
}Results: {
"Output": "arch/x86/kvm/msrs.c=2564=static void kvm_probe_feature_msr(u32 msr_index)\n--\narch/x86/kvm/msrs.c-2573-\narch/x86/kvm/msrs.c:2574:static void kvm_probe_msr_to_save(u32 msr_index)\narch/x86/kvm/msrs.c-2575-{\n--\narch/x86/kvm/msrs.c=2681=void kvm_init_msr_lists(void)\n--\narch/x86/kvm/msrs.c-2692-\tfor (i = 0; i \u003c ARRAY_SIZE(msrs_to_save_base); i++)\narch/x86/kvm/msrs.c:2693:\t\tkvm_probe_msr_to_save(msrs_to_save_base[i]);\narch/x86/kvm/msrs.c-2694-\n--\narch/x86/kvm/msrs.c-2696-\t\tfor (i = 0; i \u003c ARRAY_SIZE(msrs_to_save_pmu); i++)\narch/x86/kvm/msrs.c:2697:\t\t\tkvm_probe_msr_to_save(msrs_to_save_pmu[i]);\narch/x86/kvm/msrs.c-2698-\t}\n"
} |
| 49/2 | 2026/11/08 23:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53579 output: 47 thoughts: 84 Thoughts: Okay, here's my summary, tailored for an expert audience, as if I'm thinking through this process: **Diving into MSR Interception in KVM** Alright, so `kvm_probe_msr_to_save` is exclusively part of module initialization – specifically, it's called within `kvm_init_msr_lists`. That's a crucial starting point. Now, to understand the broader picture, I need to investigate how performance monitoring unit (PMU) MSRs are managed and potentially intercepted within the AMD-V (SVM) and Intel-VT (VMX) contexts. My focus shifts to the functions `svm_recalc_pmu_msr_intercepts` and `vmx_recalc_pmu_msr_intercepts`. I need to figure out how these functions interact with the MSR interception logic. The key questions in my mind are: are they *called* directly, or is their address being captured and perhaps used in a function pointer or other indirect way? Understanding the call paths, and specifically if there is a 'jump-table' like usage pattern, will shed light on the design's flexibility. This investigation should show me what other areas are involved in the overall architecture. |
| 50/2 | 2026/11/08 23:32 | tool | grepper |
0mArgs:{
"Expression": "(svm|vmx)_recalc_pmu_msr_intercepts",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/svm.c=755=void svm_vcpu_free_msrpm(void *msrpm)\n--\narch/x86/kvm/svm/svm.c-759-\narch/x86/kvm/svm/svm.c:760:static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-761-{\n--\narch/x86/kvm/svm/svm.c=791=static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-858-\narch/x86/kvm/svm/svm.c:859:\tsvm_recalc_pmu_msr_intercepts(vcpu);\narch/x86/kvm/svm/svm.c-860-\n--\narch/x86/kvm/vmx/vmx.c=4222=void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4237-\narch/x86/kvm/vmx/vmx.c:4238:static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/vmx.c-4239-{\n--\narch/x86/kvm/vmx/vmx.c=4292=static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4357-\narch/x86/kvm/vmx/vmx.c:4358:\tvmx_recalc_pmu_msr_intercepts(vcpu);\narch/x86/kvm/vmx/vmx.c-4359-\n"
} |
| 51/2 | 2026/11/08 23:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 54521 output: 38 thoughts: 62 Thoughts: Okay, let's dive into this. I need to quickly refresh my understanding of how these MSR intercept recalculation functions work in the KVM context. My initial thought is to confirm how the core functions `svm_recalc_msr_intercepts` and `vmx_recalc_msr_intercepts` are actually *used*. My gut feeling is they are accessed via `kvm_x86_ops`, but I need to definitively verify that. **MSR Intercept Recalculation Flow Check** So, let's cut right to the chase: I need to quickly see if my intuition about those functions being called through `kvm_x86_ops` is correct. The most efficient way to do that right now is to just grep the source code for the term `recalc_msr_intercepts`. That should quickly uncover all the call sites and confirm the `kvm_x86_ops` usage or reveal any other unexpected pathways. That will tell me everything I need to know about the control flow. Let's see what the grep turns up. |
| 52/2 | 2026/11/08 23:32 | tool | grepper |
0mArgs:{
"Expression": "recalc_msr_intercepts",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/svm/sev.c=4746=int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)\n--\narch/x86/kvm/svm/sev.c-4769-\narch/x86/kvm/svm/sev.c:4770:void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/sev.c-4771-{\n--\narch/x86/kvm/svm/svm.c=760=static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-790-\narch/x86/kvm/svm/svm.c:791:static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-792-{\n--\narch/x86/kvm/svm/svm.c-856-\tif (is_sev_es_guest(vcpu))\narch/x86/kvm/svm/svm.c:857:\t\tsev_es_recalc_msr_intercepts(vcpu);\narch/x86/kvm/svm/svm.c-858-\n--\narch/x86/kvm/svm/svm.c=1129=static void svm_recalc_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-1131-\tsvm_recalc_instruction_intercepts(vcpu);\narch/x86/kvm/svm/svm.c:1132:\tsvm_recalc_msr_intercepts(vcpu);\narch/x86/kvm/svm/svm.c-1133-}\n--\narch/x86/kvm/svm/svm.h=988=int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);\narch/x86/kvm/svm/svm.h:989:void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu);\narch/x86/kvm/svm/svm.h-990-void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);\n--\narch/x86/kvm/vmx/vmx.c=4238=static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4291-\narch/x86/kvm/vmx/vmx.c:4292:static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)\narch/x86/kvm/vmx/vmx.c-4293-{\n--\narch/x86/kvm/vmx/vmx.c=4372=void vmx_recalc_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4374-\tvmx_recalc_instruction_intercepts(vcpu);\narch/x86/kvm/vmx/vmx.c:4375:\tvmx_recalc_msr_intercepts(vcpu);\narch/x86/kvm/vmx/vmx.c-4376-}\n"
} |
| 53/2 | 2026/11/08 23:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 55801 output: 44 thoughts: 35 Thoughts: Okay, here's my summary, taking on the persona you requested: **Deconstructing the KVM PMU Guest PMCs** Alright, let's dive in. I need to understand how the KVM kernel module handles Performance Monitoring Counters (PMCs) for guests. Specifically, I'm going to follow the call flow of `kvm_pmu_load_guest_pmcs` and `kvm_pmu_put_guest_pmcs`. These are the functions responsible for, from what I can infer, loading the guest's PMC configuration into the hardware and then saving it back out, respectively. I need to understand what state these functions are operating on, where that state is kept, and the implications for guest performance, context switching, and potential virtualization overhead. I'm especially interested in how they integrate with the overall KVM architecture and how they interact with the underlying hardware's PMU. I'll need to look at the data structures passed to these functions, see how they're used, and figure out how they relate to the virtual PMC definitions in the guest's context. I also want to see the locking mechanisms involved. I'm betting that concurrent access to PMCs is a challenge. I need to know the details! |
| 54/2 | 2026/11/08 23:32 | tool | grepper |
0mArgs:{
"Expression": "kvm_pmu_(load|put)_guest_pmcs",
"PathPrefixes": [
"arch/x86/kvm/"
]
}Results: {
"Output": "arch/x86/kvm/pmu.c=1342=static __always_inline u32 gp_eventsel_msr(u32 idx)\n--\narch/x86/kvm/pmu.c-1346-\narch/x86/kvm/pmu.c:1347:static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-1348-{\n--\narch/x86/kvm/pmu.c=1375=void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/pmu.c-1401-\narch/x86/kvm/pmu.c:1402:\tkvm_pmu_load_guest_pmcs(vcpu);\narch/x86/kvm/pmu.c-1403-\n--\narch/x86/kvm/pmu.c-1406-\narch/x86/kvm/pmu.c:1407:static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-1408-{\n--\narch/x86/kvm/pmu.c=1438=void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/pmu.c-1451-\narch/x86/kvm/pmu.c:1452:\tkvm_pmu_put_guest_pmcs(vcpu);\narch/x86/kvm/pmu.c-1453-\n"
} |
| 55/2 | 2026/11/08 23:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 56616 output: 51 thoughts: 321 Thoughts: **Analyzing KVM PMU Functionality and Potential Nesting Implications** Okay, let's break this down. I see we have several key functions related to Performance Monitoring Units (PMUs) within the KVM (Kernel-based Virtual Machine) infrastructure, specifically for x86 architectures. I'm noting that `kvm_mediated_pmu_load` and `kvm_mediated_pmu_put`, which are non-static and reside in `arch/x86/kvm/pmu.c`, seem central. Also in that file, we have `kvm_pmu_refresh`, `kvm_pmu_set_msr`, and `kvm_pmu_get_msr` - all fundamental interactions with the PMU. I'm starting to build a picture of how these core functions might be structured. Now, digging deeper into vendor-specific implementations, I'm finding distinct functions for Intel and AMD. Specifically, `intel_pmu_refresh`, `intel_pmu_set_msr`, `intel_pmu_get_msr`, `intel_rdpmc_ecx_to_pmc`, and `intel_mediated_pmu_load` are Intel-specific, housed in `arch/x86/kvm/vmx/pmu_intel.c`. Similarly, for AMD, we have `amd_pmu_refresh`, `amd_pmu_set_msr`, `amd_pmu_get_pmc`, and `amd_check_rdpmc_early` in `arch/x86/kvm/svm/pmu.c`. So it appears that the core PMU operations are abstracted via the kvm calls, but the details are handled by specific implementations. I'm particularly interested in how these are potentially impacted by nested virtualization. Let me check the modifications. Specifically, I need to focus on `nested_vmx_merge_pmu_msr_bitmaps` which, after reviewing `arch/x86/kvm/vmx/nested.c`, appears to have been updated to utilize `kvm_for_each_gp_counter` and `kvm_for_each_fixed_counter`. This suggests an effort to streamline or improve the handling of PMU MSRs in a nested VMX context. I need to understand the implications of those changes. Let's see who's calling it... |
| 56/2 | 2026/11/08 23:32 | tool | grepper |
0mArgs:{
"Expression": "nested_vmx_merge_pmu_msr_bitmaps",
"PathPrefixes": [
"arch/x86/kvm/vmx/nested.c"
]
}Results: {
"Output": "arch/x86/kvm/vmx/nested.c=675=static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx,\n--\narch/x86/kvm/vmx/nested.c-700-\narch/x86/kvm/vmx/nested.c:701:static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,\narch/x86/kvm/vmx/nested.c-702-\t\t\t\t\t unsigned long *msr_bitmap_l1,\n--\narch/x86/kvm/vmx/nested.c=735=static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/vmx/nested.c-845-\narch/x86/kvm/vmx/nested.c:846:\tnested_vmx_merge_pmu_msr_bitmaps(vcpu, msr_bitmap_l1, msr_bitmap_l0);\narch/x86/kvm/vmx/nested.c-847-\n"
} |
| 57/2 | 2026/11/08 23:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57827 output: 285 thoughts: 1624 Thoughts: **KVM PMU Virtualization Analysis for Fuzzing** Alright, let's break this down. First, I need to confirm the required kernel configs. We're dealing with KVM, so we definitely need `KVM`, `KVM_INTEL`, and `KVM_AMD`. No other obvious configs jump out, but let's be thorough. I should check the dependencies of these, just in case. `KVM_INTEL` depends on `KVM` and `IA32_FEAT_CTL`, which is always enabled on X86, so that's covered. `KVM_AMD` only depends on `KVM`. What about KVM itself? It needs `VIRTUALIZATION` and `X86_LOCAL_APIC`. So, the core configs remain `KVM`, `KVM_INTEL`, and `KVM_AMD`. And `KVM_X86` selects `PERF_EVENTS` too. Now, the critical part: identifying the *right* `FocusSymbols`. I need to pick specific symbols, ones that are directly tied to the modified code and not ubiquitous, core functions. So, let's look at the key functions that are touched by the patch. * `kvm_pmu_refresh`: This one's important, as it reconfigures the guest PMU counters based on CPUID updates or MSR writes. It handles bitmasks and such. * `kvm_pmu_set_msr`: This deals with writing to the newly added `MSR_CORE_PERF_GLOBAL_STATUS_SET` and AMD global status registers, overflow control, etc. * `kvm_pmu_get_msr`: Reading from the status MSRs. * `intel_pmu_refresh`, `intel_pmu_set_msr`, `intel_pmu_get_msr`: The Intel-specific PMU counterparts, also key. * `amd_pmu_refresh`, `amd_pmu_set_msr`: The AMD-specific PMU counterparts, also key. * `kvm_mediated_pmu_load`, `kvm_mediated_pmu_put`: Mediated PMU helper functions. I also see that `intel_rdpmc_ecx_to_pmc` and `amd_check_rdpmc_early` are touched, due to the new `kvm_is_gp_pmc_supported`/`kvm_is_fixed_pmc_supported` check. `intel_pmu_get_msr` calls `intel_pmu_get_global_inuse` when reading `MSR_CORE_PERF_GLOBAL_INUSE`. Now, the million-dollar question: are any of these lifecycle hot-paths? No. These are all specific to KVM's performance monitoring unit virtualization and aren't generic allocators, page faults, locks, or core scheduling functions. They are *ideal* targets for fuzzing. Let's quickly check the output format requirements for `set-results`. We need `WorthFuzzing`, `Reasoning`, `FocusSymbols` (as an array), and `EnableConfigs` (as an array). Okay, quick sanity check. This patch: (1) changes scalar counts to bitmaps/masks for non-contiguous PMCs and PMU v5; (2) implements Intel PerfMon v5 CPUID enumeration and deprecation handling; (3) adds MSR support for `MSR_CORE_PERF_GLOBAL_STATUS_SET` and `MSR_CORE_PERF_GLOBAL_INUSE`; (4) enforces architectural reserved bit checking; (5) updates MSR interception recalculation; (6) fixes mediated PMU loading/saving. Can this code be reached in standard virtualized environments? Absolutely. KVM is accessible via `/dev/kvm` and through the execution of guest vCPUs, so it's a good target for syzkaller. Configs: `KVM`, `KVM_INTEL`, `KVM_AMD`. Focus Symbols: `kvm_pmu_refresh`, `kvm_pmu_set_msr`, `kvm_pmu_get_msr`, `intel_pmu_refresh`, `intel_pmu_set_msr`, `intel_pmu_get_msr`, `amd_pmu_refresh`, `amd_pmu_set_msr`, `kvm_mediated_pmu_load`. Time to confirm those symbol names are valid and non-static. * `kvm_pmu_refresh`: in ` |
| 58/2 | 2026/11/08 23:32 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"KVM",
"KVM_INTEL",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_refresh",
"kvm_pmu_set_msr",
"kvm_pmu_get_msr",
"intel_pmu_refresh",
"intel_pmu_set_msr",
"intel_pmu_get_msr",
"amd_pmu_refresh",
"amd_pmu_set_msr",
"kvm_mediated_pmu_load"
],
"Reasoning": "The patch series refactors x86 KVM PMU virtualization to support non-contiguous counters and PMU v5, adds emulation for MSR_CORE_PERF_GLOBAL_STATUS_SET and MSR_CORE_PERF_GLOBAL_INUSE, and updates MSR interception logic and AnyThread deprecation handling for both Intel and AMD. This executable code is reachable via standard /dev/kvm ioctls (such as KVM_SET_CPUID2, KVM_SET_MSRS, KVM_GET_MSRS) and guest vCPU execution, making it a viable target for syzkaller fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"KVM",
"KVM_INTEL",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_refresh",
"kvm_pmu_set_msr",
"kvm_pmu_get_msr",
"intel_pmu_refresh",
"intel_pmu_set_msr",
"intel_pmu_get_msr",
"amd_pmu_refresh",
"amd_pmu_set_msr",
"kvm_mediated_pmu_load"
],
"Reasoning": "The patch series refactors x86 KVM PMU virtualization to support non-contiguous counters and PMU v5, adds emulation for MSR_CORE_PERF_GLOBAL_STATUS_SET and MSR_CORE_PERF_GLOBAL_INUSE, and updates MSR interception logic and AnyThread deprecation handling for both Intel and AMD. This executable code is reachable via standard /dev/kvm ioctls (such as KVM_SET_CPUID2, KVM_SET_MSRS, KVM_GET_MSRS) and guest vCPU execution, making it a viable target for syzkaller fuzzing.",
"WorthFuzzing": true
} |
| 59/1 | 2026/11/08 23:32 | action | If |
2mArgs:{
"WorthFuzzing": true
}Results: null |
| 60/2 | 2026/11/08 23:32 | agent | kmsan-evaluator |
2mModel:core Args: null Results: {
"KMSANReasoning": "The patch series refactors KVM's virtual Performance Monitoring Unit (vPMU) counter tracking to use bitmasks rather than scalar counter counts, and adds support for Intel PerfMon v5 fixed-counter enumeration (via CPUID 0AH ECX) and MSRs (MSR_CORE_PERF_GLOBAL_STATUS_SET, MSR_CORE_PERF_GLOBAL_INUSE).\n\nAnalysis regarding uninitialized memory:\n1. No uninitialized data leaks to userspace or guests:\n - In `__do_cpuid_func()`, the local unions (`cpuid10_eax`, `cpuid10_edx`, `cpuid_0x80000022_ebx`) are explicitly zero-initialized (`= { }`), and all entry register fields (`eax`, `ebx`, `ecx`, `edx`) are fully assigned before returning.\n - For newly handled MSRs, `MSR_CORE_PERF_GLOBAL_STATUS_SET` explicitly sets `msr_info-\u003edata = 0;` and `MSR_CORE_PERF_GLOBAL_INUSE` computes `data` starting from `0` in `intel_pmu_get_global_inuse()`, with all local variables initialized.\n2. Structure initialization and sizing:\n - `struct kvm_pmu` continues to be zeroed via `memset(pmu, 0, sizeof(*pmu))` in `kvm_pmu_init()`. The new union replacing `all_valid_pmc_idx` with `pmc_exists` / `pmc_exists64` maintains identical size (`X86_PMC_IDX_MAX` bits = 64 bits = `sizeof(u64)`), which is verified at compile-time via `BUILD_BUG_ON`.\n - `kvm_host_pmu` and `kvm_pmu_cap` are static global structures in BSS that are zeroed by default and completely populated during PMU initialization (`kvm_init_pmu_capability()`).\n3. Control flow and indexing:\n - Array accesses into `gp_counters` and `fixed_counters` continue to be bounded by static limits (`KVM_MAX_NR_GP_COUNTERS` and `KVM_MAX_NR_FIXED_COUNTERS`) sanitized by `array_index_nospec()`. Potential bugs would be index out-of-bounds or invalid MSR access bugs, which fall under the scope of KASAN rather than KMSAN.\n\nBecause the changes do not introduce or expose uninitialized memory reads or information leak risks, a dedicated KMSAN fuzzing session is not warranted. Standard KASAN builds are sufficient.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
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: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit f49f6183a3b22d43cbacabe4487b395224573ec4
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 8 23:29:44 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/virt/kvm/x86/errata.rst b/Documentation/virt/kvm/x86/errata.rst
index a9cf0e004651aa..161ba6d1beb483 100644
--- a/Documentation/virt/kvm/x86/errata.rst
+++ b/Documentation/virt/kvm/x86/errata.rst
@@ -84,4 +84,12 @@ memory.
Note, this erratum does not affect AMD CPUs, which fully virtualize CR0.CD in
hardware, i.e. put the CPU caches into "no fill" mode when CR0.CD=1, even when
-running in the guest.
\ No newline at end of file
+running in the guest.
+
+IA32_PERF_GLOBAL_STATUS_RESET
+-----------------------------
+To reduce VM-exits on performance-critical paths, KVM passes through writes
+to IA32_PERF_GLOBAL_STATUS_RESET (0x390) in mediated PMU mode when the guest's
+counters match hardware, without injecting #GP on unvirtualized bits. Because
+the MSR is read-as-zero and write-to-clear, this cannot leak host information
+to the guest.
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 8b3ea0adb9655c..990d3be5a07f95 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -3156,10 +3156,10 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
* base PMU holds the correct number of counters for P-cores.
*/
cap->version = x86_pmu.version;
- cap->num_counters_gp = x86_pmu_num_counters(NULL);
- cap->num_counters_fixed = x86_pmu_num_counters_fixed(NULL);
- cap->bit_width_gp = cap->num_counters_gp ? x86_pmu.cntval_bits : 0;
- cap->bit_width_fixed = cap->num_counters_fixed ? x86_pmu.cntval_bits : 0;
+ cap->cntr_mask64 = x86_pmu.cntr_mask64;
+ cap->fixed_cntr_mask64 = x86_pmu.fixed_cntr_mask64;
+ cap->bit_width_gp = cap->cntr_mask64 ? x86_pmu.cntval_bits : 0;
+ cap->bit_width_fixed = cap->fixed_cntr_mask64 ? x86_pmu.cntval_bits : 0;
cap->events_mask = (unsigned int)x86_pmu.events_maskl;
cap->events_mask_len = x86_pmu.events_mask_len;
cap->pebs_ept = x86_pmu.pebs_ept;
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 0378577d9d8bbf..93f029e1fc98be 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -575,8 +575,6 @@ struct kvm_pmc {
struct kvm_pmu {
u8 version;
- unsigned nr_arch_gp_counters;
- unsigned nr_arch_fixed_counters;
unsigned available_event_types;
u64 fixed_ctr_ctrl;
u64 fixed_ctr_ctrl_hw;
@@ -586,7 +584,7 @@ struct kvm_pmu {
u64 counter_bitmask[2];
u64 global_ctrl_rsvd;
u64 global_status_rsvd;
- u64 reserved_bits;
+ u64 eventsel_rsvd;
u64 raw_event_mask;
struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
@@ -600,7 +598,10 @@ struct kvm_pmu {
DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
atomic64_t __reprogram_pmi;
};
- DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
+ union {
+ DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+ u64 pmc_exists64;
+ };
DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 3a8e51a0c9e88c..25dda50fbdc1f0 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -1242,17 +1242,10 @@
#define MSR_CORE_PERF_GLOBAL_CTRL 0x0000038f
#define MSR_CORE_PERF_GLOBAL_OVF_CTRL 0x00000390
#define MSR_CORE_PERF_GLOBAL_STATUS_SET 0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE 0x00000392
#define MSR_PERF_METRICS 0x00000329
-/* PERF_GLOBAL_OVF_CTL bits */
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT 55
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT 62
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT 63
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
-
/* Geode defined MSRs */
#define MSR_GEODE_BUSCONT_CONF0 0x00001900
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index 1eb13673e889f9..2937e8a278915c 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -300,8 +300,14 @@ union cpuid_0x80000022_ebx {
struct x86_pmu_capability {
int version;
- int num_counters_gp;
- int num_counters_fixed;
+ union {
+ u64 cntr_mask64;
+ DECLARE_BITMAP(cntr_mask, X86_PMC_IDX_MAX);
+ };
+ union {
+ u64 fixed_cntr_mask64;
+ DECLARE_BITMAP(fixed_cntr_mask, X86_PMC_IDX_MAX);
+ };
int bit_width_gp;
int bit_width_fixed;
unsigned int events_mask;
@@ -448,6 +454,9 @@ static inline bool is_topdown_idx(int idx)
#define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
#define GLOBAL_STATUS_PERF_METRICS_OVF_BIT 48
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE BIT_ULL(63)
+
#define GLOBAL_CTRL_EN_PERF_METRICS BIT_ULL(48)
/*
* We model guest LBR event tracing as another fixed-mode PMC like BTS.
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index adafd6fa2c5427..66e68a86794697 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1535,18 +1535,29 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
}
eax.split.version_id = kvm_pmu_cap.version;
- eax.split.num_counters = kvm_pmu_cap.num_counters_gp;
+
+ /* Contiguous GP counters only. */
+ eax.split.num_counters =
+ find_first_zero_bit(kvm_pmu_cap.cntr_mask,
+ KVM_MAX_NR_GP_COUNTERS);
eax.split.bit_width = kvm_pmu_cap.bit_width_gp;
eax.split.mask_length = kvm_pmu_cap.events_mask_len;
- edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;
+
+ /* Contiguous fixed counters only. */
+ edx.split.num_counters_fixed =
+ find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
+ KVM_MAX_NR_FIXED_COUNTERS);
edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
- if (kvm_pmu_cap.version)
+ if (kvm_pmu_cap.version >= 5)
edx.split.anythread_deprecated = 1;
entry->eax = eax.full;
entry->ebx = kvm_pmu_cap.events_mask;
- entry->ecx = 0;
+ if (kvm_pmu_cap.version >= 5)
+ entry->ecx = (u32)kvm_pmu_cap.fixed_cntr_mask64;
+ else
+ entry->ecx = 0;
entry->edx = edx.full;
break;
}
@@ -1903,7 +1914,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
cpuid_entry_override(entry, CPUID_8000_0022_EAX);
- ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+ ebx.split.num_core_pmc = hweight64(kvm_pmu_cap.cntr_mask64);
entry->ebx = ebx.full;
break;
}
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index e91650af44c207..01fb130a7d24a9 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -2621,20 +2621,20 @@ static void kvm_probe_msr_to_save(u32 msr_index)
break;
case MSR_ARCH_PERFMON_PERFCTR0 ...
MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
- if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
- kvm_pmu_cap.num_counters_gp)
+ if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_PERFCTR0) &
+ kvm_pmu_cap.cntr_mask64))
return;
break;
case MSR_ARCH_PERFMON_EVENTSEL0 ...
MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1:
- if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >=
- kvm_pmu_cap.num_counters_gp)
+ if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_EVENTSEL0) &
+ kvm_pmu_cap.cntr_mask64))
return;
break;
case MSR_ARCH_PERFMON_FIXED_CTR0 ...
MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1:
- if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >=
- kvm_pmu_cap.num_counters_fixed)
+ if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_FIXED_CTR0) &
+ kvm_pmu_cap.fixed_cntr_mask64))
return;
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e80..f949fdbf359c68 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -20,15 +20,15 @@
#include <asm/perf_event.h>
#include <asm/cpu_device_id.h>
#include "x86.h"
-#include "cpuid.h"
#include "lapic.h"
#include "pmu.h"
/* This is enough to filter the vast majority of currently defined events. */
#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300
-/* Unadultered PMU capabilities of the host, i.e. of hardware. */
-static struct x86_pmu_capability __read_mostly kvm_host_pmu;
+/* Unadulterated PMU capabilities of the host, i.e. of hardware. */
+struct x86_pmu_capability __read_mostly kvm_host_pmu;
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host_pmu);
/* KVM's PMU capabilities, i.e. the intersection of KVM and hardware support. */
struct x86_pmu_capability __read_mostly kvm_pmu_cap;
@@ -155,8 +155,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
* there are a non-zero number of counters, but fewer than what
* is architecturally required.
*/
- if (!kvm_host_pmu.num_counters_gp ||
- WARN_ON_ONCE(kvm_host_pmu.num_counters_gp < min_nr_gp_ctrs))
+ if (!kvm_host_pmu.cntr_mask64 ||
+ WARN_ON_ONCE(hweight64(kvm_host_pmu.cntr_mask64) < min_nr_gp_ctrs))
enable_pmu = false;
else if (is_intel && !kvm_host_pmu.version)
enable_pmu = false;
@@ -175,11 +175,21 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
}
memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
- kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
- kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
- pmu_ops->MAX_NR_GP_COUNTERS);
- kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
- KVM_MAX_NR_FIXED_COUNTERS);
+
+ /*
+ * PMU v3/v4 has AnyThread support, which KVM cannot safely virtualize.
+ * Only expose PMU v5+ for Intel mediated PMU, where AnyThread is
+ * deprecated; otherwise, cap at PMU v2.
+ */
+ if (is_intel && enable_mediated_pmu && kvm_pmu_cap.version >= 5)
+ kvm_pmu_cap.version = 5;
+ else
+ kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+
+ kvm_pmu_cap.cntr_mask64 &=
+ GENMASK_ULL(pmu_ops->MAX_NR_GP_COUNTERS - 1, 0);
+ kvm_pmu_cap.fixed_cntr_mask64 &=
+ GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
@@ -557,7 +567,9 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)
pmc->eventsel_hw |= pmc->eventsel &
ARCH_PERFMON_EVENTSEL_ENABLE;
} else {
- u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX, 0xf);
+ u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX,
+ INTEL_FIXED_0_KERNEL |
+ INTEL_FIXED_0_USER);
pmu->fixed_ctr_ctrl_hw &= ~mask;
if (allowed)
@@ -787,8 +799,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
* KVM's capabilities are constrained based on KVM support, i.e. KVM's
* capabilities themselves may be a subset of hardware capabilities.
*/
- return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp ||
- pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed;
+ return kvm_gp_pmc_mask(pmu) != kvm_host_pmu.cntr_mask64 ||
+ kvm_fixed_pmc_mask(pmu) != kvm_host_pmu.fixed_cntr_mask64;
}
bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
@@ -855,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+ case MSR_CORE_PERF_GLOBAL_STATUS_SET:
msr_info->data = 0;
break;
default:
@@ -921,6 +934,18 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
if (!msr_info->host_initiated)
pmu->global_status &= ~data;
break;
+ case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+ /*
+ * Bit 63 (CondChgd) is architecturally reserved in STATUS_SET.
+ * Older CPUs errantly accepted writes without #GP (e.g. SKX95),
+ * but KVM strictly enforces the architectural spec to ensure
+ * consistent behavior across migrations.
+ */
+ if (data & (pmu->global_status_rsvd | GLOBAL_STATUS_COND_CHG))
+ return 1;
+ if (!msr_info->host_initiated)
+ pmu->global_status |= data;
+ break;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
if (!msr_info->host_initiated)
pmu->global_status |= data & ~pmu->global_status_rsvd;
@@ -944,7 +969,7 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX);
bitmap_zero(pmu->pmc_has_mode_specific_enables, X86_PMC_IDX_MAX);
- kvm_for_each_pmc(pmu, pmc, i, pmu->all_valid_pmc_idx) {
+ kvm_for_each_pmc(pmu, pmc, i, pmu->pmc_exists) {
pmc_stop_counter(pmc);
pmc->counter = 0;
pmc->emulated_counter = 0;
@@ -979,18 +1004,20 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
kvm_pmu_reset(vcpu);
pmu->version = 0;
- pmu->nr_arch_gp_counters = 0;
- pmu->nr_arch_fixed_counters = 0;
pmu->counter_bitmask[KVM_PMC_GP] = 0;
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
- pmu->reserved_bits = 0xffffffff00200000ull;
+ /*
+ * KVM is not able to emulate the AnyThread bit due to cross-VM
+ * information leakage on SMT cores.
+ */
+ pmu->eventsel_rsvd = GENMASK_ULL(63, 32) | ARCH_PERFMON_EVENTSEL_ANY;
pmu->raw_event_mask = X86_RAW_EVENT_MASK;
pmu->global_ctrl_rsvd = ~0ull;
pmu->global_status_rsvd = ~0ull;
pmu->fixed_ctr_ctrl_rsvd = ~0ull;
pmu->pebs_enable_rsvd = ~0ull;
pmu->pebs_data_cfg_rsvd = ~0ull;
- bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
+ bitmap_zero(pmu->pmc_exists, X86_PMC_IDX_MAX);
if (!vcpu->kvm->arch.enable_pmu)
return;
@@ -1004,22 +1031,20 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
* in the global controls). Emulate that behavior when refreshing the
* PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL.
*/
- if (pmu->nr_arch_gp_counters &&
+ if (kvm_gp_pmc_mask(pmu) &&
(kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu)))
- pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0);
+ pmu->global_ctrl = kvm_gp_pmc_mask(pmu);
if (kvm_vcpu_has_mediated_pmu(vcpu))
kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
-
- bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
- bitmap_set(pmu->all_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,
- pmu->nr_arch_fixed_counters);
}
void kvm_pmu_init(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));
+
memset(pmu, 0, sizeof(*pmu));
kvm_pmu_call(init)(vcpu);
}
@@ -1034,7 +1059,7 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
pmu->need_cleanup = false;
- bitmap_andnot(bitmask, pmu->all_valid_pmc_idx,
+ bitmap_andnot(bitmask, pmu->pmc_exists,
pmu->pmc_in_use, X86_PMC_IDX_MAX);
kvm_for_each_pmc(pmu, pmc, i, bitmask) {
@@ -1322,6 +1347,8 @@ static __always_inline u32 gp_eventsel_msr(u32 idx)
static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
struct kvm_pmc *pmc;
u32 i;
@@ -1330,14 +1357,14 @@ static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
* is intercepted if hardware has counters that aren't visible to the
* guest (KVM will inject #GP as appropriate).
*/
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ kvm_for_each_gp_counter(i, gp_mask) {
pmc = &pmu->gp_counters[i];
if (pmc->counter != rdpmc(i))
wrmsrq(gp_counter_msr(i), pmc->counter);
wrmsrq(gp_eventsel_msr(i), pmc->eventsel_hw);
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+ kvm_for_each_fixed_counter(i, fixed_mask) {
pmc = &pmu->fixed_counters[i];
if (pmc->counter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))
@@ -1380,6 +1407,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
struct kvm_pmc *pmc;
u32 i;
@@ -1387,7 +1416,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
* Clear selectors and counters to ensure hardware doesn't count using
* guest controls when the host (perf) restores its state.
*/
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ kvm_for_each_gp_counter(i, gp_mask) {
pmc = &pmu->gp_counters[i];
pmc->counter = rdpmc(i);
@@ -1397,7 +1426,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
wrmsrq(gp_eventsel_msr(i), 0);
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+ kvm_for_each_fixed_counter(i, fixed_mask) {
pmc = &pmu->fixed_counters[i];
pmc->counter = rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i);
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f483..98e6f2443b82ef 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -88,6 +88,30 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
return enable_mediated_pmu && vcpu_to_pmu(vcpu)->version;
}
+static inline unsigned long kvm_gp_pmc_mask(struct kvm_pmu *pmu)
+{
+ return pmu->pmc_exists64 &
+ GENMASK_ULL(KVM_MAX_NR_GP_COUNTERS - 1, 0);
+}
+
+static inline unsigned long kvm_fixed_pmc_mask(struct kvm_pmu *pmu)
+{
+ return (pmu->pmc_exists64 >> KVM_FIXED_PMC_BASE_IDX) &
+ GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
+}
+
+static inline bool kvm_is_gp_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+ return idx < KVM_MAX_NR_GP_COUNTERS &&
+ test_bit(idx, pmu->pmc_exists);
+}
+
+static inline bool kvm_is_fixed_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+ return idx < KVM_MAX_NR_FIXED_COUNTERS &&
+ test_bit(KVM_FIXED_PMC_BASE_IDX + idx, pmu->pmc_exists);
+}
+
/*
* KVM tracks all counters in 64-bit bitmaps, with general purpose counters
* mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0
@@ -104,11 +128,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
*/
static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
{
- if (idx < pmu->nr_arch_gp_counters)
+ if (kvm_is_gp_pmc_supported(pmu, idx))
return &pmu->gp_counters[idx];
idx -= KVM_FIXED_PMC_BASE_IDX;
- if (idx >= 0 && idx < pmu->nr_arch_fixed_counters)
+ if (kvm_is_fixed_pmc_supported(pmu, idx))
return &pmu->fixed_counters[idx];
return NULL;
@@ -120,6 +144,17 @@ static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
continue; \
else \
+/*
+ * @mask is expected to be a scalar unsigned long derived from pmu->pmc_exists,
+ * which is already constrained by KVM_MAX_NR_{AMD,INTEL}_{GP,FIXED}_COUNTERS,
+ * so iteration up to KVM's maximum counter count is safe.
+ */
+#define kvm_for_each_gp_counter(i, mask) \
+ for_each_set_bit((i), &(mask), KVM_MAX_NR_GP_COUNTERS)
+
+#define kvm_for_each_fixed_counter(i, mask) \
+ for_each_set_bit((i), &(mask), KVM_MAX_NR_FIXED_COUNTERS)
+
static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
{
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
@@ -168,9 +203,12 @@ static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,
static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
u32 base)
{
- if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
+ if (msr >= base && msr < base + KVM_MAX_NR_GP_COUNTERS) {
u32 index = array_index_nospec(msr - base,
- pmu->nr_arch_gp_counters);
+ KVM_MAX_NR_GP_COUNTERS);
+
+ if (!kvm_is_gp_pmc_supported(pmu, index))
+ return NULL;
return &pmu->gp_counters[index];
}
@@ -183,9 +221,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
{
int base = MSR_CORE_PERF_FIXED_CTR0;
- if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
+ if (msr >= base && msr < base + KVM_MAX_NR_FIXED_COUNTERS) {
u32 index = array_index_nospec(msr - base,
- pmu->nr_arch_fixed_counters);
+ KVM_MAX_NR_FIXED_COUNTERS);
+
+ if (!kvm_is_fixed_pmc_supported(pmu, index))
+ return NULL;
return &pmu->fixed_counters[index];
}
@@ -211,6 +252,7 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)
return !kvm_pmu_call(pmc_is_disabled_in_current_mode)(pmc);
}
+extern struct x86_pmu_capability kvm_host_pmu;
extern struct x86_pmu_capability kvm_pmu_cap;
void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7acd..fb5a298244ecba 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -27,12 +27,11 @@ enum pmu_type {
static struct kvm_pmc *amd_pmu_get_pmc(struct kvm_pmu *pmu, int pmc_idx)
{
- unsigned int num_counters = pmu->nr_arch_gp_counters;
-
- if (pmc_idx >= num_counters)
+ if (!kvm_is_gp_pmc_supported(pmu, pmc_idx))
return NULL;
- return &pmu->gp_counters[array_index_nospec(pmc_idx, num_counters)];
+ pmc_idx = array_index_nospec(pmc_idx, KVM_MAX_NR_AMD_GP_COUNTERS);
+ return &pmu->gp_counters[pmc_idx];
}
static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
@@ -77,7 +76,7 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
- if (idx >= pmu->nr_arch_gp_counters)
+ if (!kvm_is_gp_pmc_supported(pmu, idx))
return -EINVAL;
return 0;
@@ -117,7 +116,7 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
return pmu->version > 1;
default:
if (msr > MSR_F15H_PERF_CTR5 &&
- msr < MSR_F15H_PERF_CTL0 + 2 * pmu->nr_arch_gp_counters)
+ msr < MSR_F15H_PERF_CTL0 + 2 * hweight_long(kvm_gp_pmc_mask(pmu)))
return pmu->version > 1;
break;
}
@@ -163,7 +162,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
/* MSR_EVNTSELn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
if (pmc) {
- data &= ~pmu->reserved_bits;
+ data &= ~pmu->eventsel_rsvd;
if (data != pmc->eventsel) {
pmc->eventsel = data;
pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
@@ -184,6 +183,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
{
+ unsigned int nr_gp_counters = AMD64_NUM_COUNTERS;
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
union cpuid_0x80000022_ebx ebx;
@@ -197,31 +197,34 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
- pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+ nr_gp_counters = ebx.split.num_core_pmc;
} else if (guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
- pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
- } else {
- pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+ nr_gp_counters = AMD64_NUM_COUNTERS_CORE;
}
- pmu->nr_arch_gp_counters = min_t(unsigned int, pmu->nr_arch_gp_counters,
- kvm_pmu_cap.num_counters_gp);
+ pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
+ kvm_pmu_cap.cntr_mask64;
if (pmu->version > 1) {
- pmu->global_ctrl_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+ pmu->global_ctrl_rsvd = ~pmu->pmc_exists64;
pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
}
pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;
- pmu->reserved_bits = 0xfffffff000280000ull;
+ /*
+ * AMD PerfEvtSeln registers do not support PinControl, and KVM does
+ * not emulate AnyThread due to cross-VM information leakage on SMT
+ * cores (as on the Intel side).
+ */
+ pmu->eventsel_rsvd = GENMASK_ULL(63, 36) | ARCH_PERFMON_EVENTSEL_PIN_CONTROL |
+ ARCH_PERFMON_EVENTSEL_ANY;
if (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) && kvm_vcpu_has_mediated_pmu(vcpu))
- pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
+ pmu->eventsel_rsvd &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
/* not applicable to AMD; but clean them to prevent any fall out */
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
- pmu->nr_arch_fixed_counters = 0;
}
static void amd_pmu_init(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 581bc3a23c95a4..eaf3ca63064b45 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -761,24 +761,21 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
{
bool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
int i;
if (!enable_mediated_pmu)
return;
/* Legacy counters are always available for AMD CPUs with a PMU. */
- for (i = 0; i < min(pmu->nr_arch_gp_counters, AMD64_NUM_COUNTERS); i++)
+ for (i = 0; i < AMD64_NUM_COUNTERS; i++)
svm_set_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,
- MSR_TYPE_RW, intercept);
+ MSR_TYPE_RW, intercept || !test_bit(i, &gp_mask));
intercept |= !guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE);
- for (i = 0; i < pmu->nr_arch_gp_counters; i++)
+ for_each_set_bit(i, kvm_host_pmu.cntr_mask, AMD64_NUM_COUNTERS_CORE)
svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
- MSR_TYPE_RW, intercept);
-
- for ( ; i < kvm_pmu_cap.num_counters_gp; i++)
- svm_enable_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
- MSR_TYPE_RW);
+ MSR_TYPE_RW, intercept || !test_bit(i, &gp_mask));
intercept = kvm_need_perf_global_ctrl_intercept(vcpu);
svm_set_intercept_for_msr(vcpu, MSR_AMD64_PERF_CNTR_GLOBAL_CTL,
@@ -5720,9 +5717,8 @@ static __init void svm_set_cpu_caps(void)
* access to enough counters to virtualize "core" support,
* otherwise limit vPMU support to the legacy number of counters.
*/
- if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
- kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
- kvm_pmu_cap.num_counters_gp);
+ if (hweight64(kvm_pmu_cap.cntr_mask64) < AMD64_NUM_COUNTERS_CORE)
+ kvm_pmu_cap.cntr_mask64 &= GENMASK_ULL(AMD64_NUM_COUNTERS - 1, 0);
else
kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index f0d75f5a968cb3..0084e510d896fe 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -704,6 +704,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
int i;
/*
@@ -713,12 +715,12 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
if (!kvm_vcpu_has_mediated_pmu(vcpu))
return;
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ kvm_for_each_gp_counter(i, gp_mask) {
nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i);
nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i);
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+ kvm_for_each_fixed_counter(i, fixed_mask)
nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i);
nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 70a8c481613569..961d16e7f31f54 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -55,6 +55,25 @@ static struct x86_pmu_lbr *vcpu_to_lbr_records(struct kvm_vcpu *vcpu)
#pragma GCC poison to_vmx
+static u64 intel_pmu_fixed_ctrl_bits(struct kvm_pmu *pmu, u64 bits)
+{
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ u64 fixed_ctrl_bits = 0;
+ int i;
+
+ kvm_for_each_fixed_counter(i, fixed_mask)
+ fixed_ctrl_bits |= intel_fixed_bits_by_idx(i, bits);
+
+ return fixed_ctrl_bits;
+}
+
+static u64 intel_pmu_fixed_anythread_mask(struct kvm_pmu *pmu)
+{
+ /* Fixed counter 3 repurposes that bit for METRICS_CLEAR_EN. */
+ return intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD) &
+ ~intel_fixed_bits_by_idx(3, INTEL_FIXED_3_METRICS_CLEAR);
+}
+
static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
{
/*
@@ -66,13 +85,14 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
* hardware, e.g. to ensure the event filter is enforced.
*/
u64 old_fixed_ctr_ctrl = pmu->fixed_ctr_ctrl_hw;
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
struct kvm_pmc *pmc;
int i;
pmu->fixed_ctr_ctrl = data;
- pmu->fixed_ctr_ctrl_hw = data;
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
- u8 new_ctrl = fixed_ctrl_field(data, i);
+ pmu->fixed_ctr_ctrl_hw = data & ~intel_pmu_fixed_anythread_mask(pmu);
+ kvm_for_each_fixed_counter(i, fixed_mask) {
+ u8 new_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl_hw, i);
u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
if (old_ctrl == new_ctrl)
@@ -107,6 +127,8 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
if (WARN_ON_ONCE(!pmu->version))
return NULL;
+ idx &= INTEL_RDPMC_INDEX_MASK;
+
/*
* General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs
* are supported on all architectural PMUs, i.e. on all virtual PMUs
@@ -117,23 +139,25 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
*/
switch (type) {
case INTEL_RDPMC_FIXED:
+ if (!kvm_is_fixed_pmc_supported(pmu, idx))
+ return NULL;
+
counters = pmu->fixed_counters;
- num_counters = pmu->nr_arch_fixed_counters;
+ num_counters = KVM_MAX_NR_INTEL_FIXED_COUNTERS;
bitmask = pmu->counter_bitmask[KVM_PMC_FIXED];
break;
case INTEL_RDPMC_GP:
+ if (!kvm_is_gp_pmc_supported(pmu, idx))
+ return NULL;
+
counters = pmu->gp_counters;
- num_counters = pmu->nr_arch_gp_counters;
+ num_counters = KVM_MAX_NR_INTEL_GP_COUNTERS;
bitmask = pmu->counter_bitmask[KVM_PMC_GP];
break;
default:
return NULL;
}
- idx &= INTEL_RDPMC_INDEX_MASK;
- if (idx >= num_counters)
- return NULL;
-
*mask &= bitmask;
return &counters[array_index_nospec(idx, num_counters)];
}
@@ -192,6 +216,9 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
case MSR_CORE_PERF_FIXED_CTR_CTRL:
return kvm_pmu_has_perf_global_ctrl(pmu);
+ case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+ case MSR_CORE_PERF_GLOBAL_INUSE:
+ return pmu->version >= 4;
case MSR_IA32_PEBS_ENABLE:
ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
break;
@@ -339,6 +366,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
return true;
}
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+ bool pmi_inuse = false;
+ u64 eventsel, data = 0;
+ u32 fixed_ctrl;
+ int i;
+
+ kvm_for_each_gp_counter(i, gp_mask) {
+ eventsel = pmu->gp_counters[i].eventsel;
+
+ if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+ data |= BIT_ULL(i);
+ pmi_inuse |= !!(eventsel & ARCH_PERFMON_EVENTSEL_INT);
+ }
+ kvm_for_each_fixed_counter(i, fixed_mask) {
+ fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+ if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+ data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);
+ pmi_inuse |= !!(fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI);
+ }
+ pmi_inuse |= pmu->pebs_enable;
+
+ if (pmi_inuse)
+ data |= PERF_GLOBAL_INUSE_PMI_INUSE;
+
+ return data;
+}
+
static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -349,6 +408,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_CORE_PERF_GLOBAL_INUSE:
+ msr_info->data = intel_pmu_get_global_inuse(vcpu);
+ break;
case MSR_IA32_PEBS_ENABLE:
msr_info->data = pmu->pebs_enable;
break;
@@ -388,7 +450,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
struct kvm_pmc *pmc;
u32 msr = msr_info->index;
u64 data = msr_info->data;
- u64 reserved_bits, diff;
+ u64 eventsel_rsvd, diff;
switch (msr) {
case MSR_CORE_PERF_FIXED_CTR_CTRL:
@@ -436,16 +498,16 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
pmc_write_counter(pmc, data);
break;
} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
- reserved_bits = pmu->reserved_bits;
+ eventsel_rsvd = pmu->eventsel_rsvd;
if ((pmc->idx == 2) &&
(pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
- reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
- if (data & reserved_bits)
+ eventsel_rsvd ^= HSW_IN_TX_CHECKPOINTED;
+ if (data & eventsel_rsvd)
return 1;
if (data != pmc->eventsel) {
pmc->eventsel = data;
- pmc->eventsel_hw = data;
+ pmc->eventsel_hw = data & ~ARCH_PERFMON_EVENTSEL_ANY;
kvm_pmu_request_counter_reprogram(pmc);
}
break;
@@ -489,26 +551,22 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
* have a known encoding for the associated general purpose event.
*/
eventsel = perf_get_hw_event_config(fixed_pmc_perf_ids[index]);
- WARN_ON_ONCE(!eventsel && index < kvm_pmu_cap.num_counters_fixed);
+ WARN_ON_ONCE(!eventsel &&
+ (kvm_pmu_cap.fixed_cntr_mask64 & BIT_ULL(index)));
return eventsel;
}
-static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
-{
- int i;
-
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
- pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
-}
-
static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
struct kvm_cpuid_entry2 *entry;
+ u64 fixed_enable_bits = 0;
union cpuid10_eax eax;
union cpuid10_edx edx;
u64 perf_capabilities;
+ u64 fixed_mask;
+ int nr_gp_counters, nr_fixed_counters;
u64 counter_rsvd;
if (!lbr_desc)
@@ -535,8 +593,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
if (!pmu->version)
return;
- pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
- kvm_pmu_cap.num_counters_gp);
eax.split.bit_width = min_t(int, eax.split.bit_width,
kvm_pmu_cap.bit_width_gp);
pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(eax.split.bit_width) - 1;
@@ -544,11 +600,39 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
kvm_pmu_cap.events_mask_len);
pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
+ /*
+ * CPUID.0AH:EDX[4:0] can only enumerate contiguous counters. Clamp the
+ * value to the number of contiguous fixed counters supported by KVM.
+ */
+ nr_fixed_counters = min_t(int, edx.split.num_counters_fixed,
+ find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
+ KVM_MAX_NR_FIXED_COUNTERS));
+
+ /* FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i) */
+ fixed_mask = BIT_ULL(nr_fixed_counters) - 1;
+ /*
+ * Do not enable fixed counters via the v5 ECX bitmap if KVM caps
+ * PMU version below 5 (e.g. when mediated PMU is disabled or on
+ * older hosts), matching KVM's advertised CPUID.0AH:ECX capability.
+ */
+ if (kvm_pmu_cap.version >= 5 && pmu->version >= 5)
+ fixed_mask |= entry->ecx;
+ fixed_mask &= kvm_pmu_cap.fixed_cntr_mask64;
+
+ /*
+ * CPUID.0AH:EAX[15:8] can only enumerate contiguous counters. Clamp the
+ * value to the number of contiguous GP counters supported by KVM.
+ */
+ nr_gp_counters = min_t(int, eax.split.num_counters,
+ find_first_zero_bit(kvm_pmu_cap.cntr_mask,
+ KVM_MAX_NR_GP_COUNTERS));
+ pmu->pmc_exists64 = BIT_ULL(nr_gp_counters) - 1;
+
entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
if (entry &&
(boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
(entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
- pmu->reserved_bits ^= HSW_IN_TX;
+ pmu->eventsel_rsvd ^= HSW_IN_TX;
pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
}
@@ -560,47 +644,56 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
lbr_desc->records.nr = 0;
if (lbr_desc->records.nr)
- bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);
+ bitmap_set(pmu->pmc_exists, INTEL_PMC_IDX_FIXED_VLBR, 1);
if (pmu->version == 1)
return;
- pmu->nr_arch_fixed_counters = min_t(int, edx.split.num_counters_fixed,
- kvm_pmu_cap.num_counters_fixed);
+ pmu->pmc_exists64 |= fixed_mask << INTEL_PMC_IDX_FIXED;
edx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,
kvm_pmu_cap.bit_width_fixed);
pmu->counter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;
- intel_pmu_enable_fixed_counter_bits(pmu, INTEL_FIXED_0_KERNEL |
- INTEL_FIXED_0_USER |
- INTEL_FIXED_0_ENABLE_PMI);
+ fixed_enable_bits |= INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER |
+ INTEL_FIXED_0_ENABLE_PMI;
- counter_rsvd = ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) |
- ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
+ counter_rsvd = ~((u64)kvm_gp_pmc_mask(pmu) |
+ ((u64)kvm_fixed_pmc_mask(pmu) << KVM_FIXED_PMC_BASE_IDX));
pmu->global_ctrl_rsvd = counter_rsvd;
- /*
- * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)
- * share reserved bit definitions. The kernel just happens to use
- * OVF_CTRL for the names.
- */
pmu->global_status_rsvd = pmu->global_ctrl_rsvd
- & ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF |
- MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD);
+ & ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
if (vmx_pt_mode_is_host_guest())
- pmu->global_status_rsvd &=
- ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
+ pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;
if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
pmu->pebs_enable_rsvd = counter_rsvd;
- pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
+ pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
pmu->pebs_data_cfg_rsvd = ~0xff00000full;
- intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
+ fixed_enable_bits |= ICL_FIXED_0_ADAPTIVE;
} else {
- pmu->pebs_enable_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+ pmu->pebs_enable_rsvd = ~(u64)kvm_gp_pmc_mask(pmu);
}
}
+
+ pmu->fixed_ctr_ctrl_rsvd &=
+ ~intel_pmu_fixed_ctrl_bits(pmu, fixed_enable_bits);
+
+ /*
+ * AnyThread counting is not supported by KVM due to cross-VM
+ * information leakage concerns on SMT cores. Therefore, AnyThread
+ * remains unavailable for PerfMon v3/v4 guests, where AnyThread
+ * deprecation is not enumerated.
+ *
+ * On PerfMon v5+ with ANYTHREAD_DEPRECATION, writes to AnyThread bits
+ * are accepted and preserved on RDMSR, while counting behavior is
+ * ignored.
+ */
+ if (pmu->version >= 5 && edx.split.anythread_deprecated) {
+ pmu->eventsel_rsvd &= ~ARCH_PERFMON_EVENTSEL_ANY;
+ pmu->fixed_ctr_ctrl_rsvd &= ~intel_pmu_fixed_anythread_mask(pmu);
+ }
}
static void intel_pmu_init(struct kvm_vcpu *vcpu)
@@ -808,8 +901,16 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
toggle = pmu->global_status ^ global_status;
if (global_status & toggle)
wrmsrq(MSR_CORE_PERF_GLOBAL_OVF_CTRL, global_status & toggle);
- if (pmu->global_status & toggle)
- wrmsrq(MSR_CORE_PERF_GLOBAL_STATUS_SET, pmu->global_status & toggle);
+
+ /*
+ * CondChgd is architecturally reserved in GLOBAL_STATUS_SET. Unless
+ * set by malicious userspace, this bit should never be set in
+ * pmu->global_status; filter it out defensively to protect against
+ * a host #GP.
+ */
+ if ((pmu->global_status & toggle) & ~GLOBAL_STATUS_COND_CHG)
+ wrmsrq(MSR_CORE_PERF_GLOBAL_STATUS_SET,
+ (pmu->global_status & toggle) & ~GLOBAL_STATUS_COND_CHG);
wrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, pmu->fixed_ctr_ctrl_hw);
}
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b341af4c6057a7..9db6107943ec30 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4242,6 +4242,8 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
bool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
+ unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+ unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
bool intercept = !has_mediated_pmu;
int i;
@@ -4262,25 +4264,21 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
vm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);
- for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ for_each_set_bit(i, kvm_host_pmu.cntr_mask, INTEL_PMC_MAX_GENERIC) {
+ bool pmc_intercept = intercept || !test_bit(i, &gp_mask);
+
vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
- MSR_TYPE_RW, intercept);
+ MSR_TYPE_RW, pmc_intercept);
vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,
- intercept || !fw_writes_is_enabled(vcpu));
- }
- for ( ; i < kvm_pmu_cap.num_counters_gp; i++) {
- vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
- MSR_TYPE_RW, true);
- vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i,
- MSR_TYPE_RW, true);
+ pmc_intercept || !fw_writes_is_enabled(vcpu));
}
- for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
- vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
- MSR_TYPE_RW, intercept);
- for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)
+ for_each_set_bit(i, kvm_host_pmu.fixed_cntr_mask, INTEL_PMC_MAX_FIXED) {
+ bool pmc_intercept = intercept || !test_bit(i, &fixed_mask);
+
vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
- MSR_TYPE_RW, true);
+ MSR_TYPE_RW, pmc_intercept);
+ }
intercept = kvm_need_perf_global_ctrl_intercept(vcpu);
vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS,
@@ -8477,7 +8475,7 @@ static unsigned int vmx_handle_intel_pt_intr(void)
return 0;
kvm_make_request(KVM_REQ_PMI, vcpu);
- __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
+ __set_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT,
(unsigned long *)&vcpu->arch.pmu.global_status);
return 1;
}
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 5898a277a2e01c..ece262d974613f 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -836,6 +836,7 @@ static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property)
static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
{
+ u8 pmu_version;
u32 nr_bits;
if (feature.f.reg == KVM_CPUID_EBX) {
@@ -845,7 +846,10 @@ static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
GUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX);
nr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
- return nr_bits > feature.f.bit || this_cpu_has(feature.f);
+ pmu_version = this_cpu_property(X86_PROPERTY_PMU_VERSION);
+
+ return nr_bits > feature.f.bit ||
+ (pmu_version >= 5 && this_cpu_has(feature.f));
}
static __always_inline u64 this_cpu_supported_xcr0(void)
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index c3e784e16348c3..77d67fa6e3fc5b 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/bitmap.h>
#include "pmu.h"
#include "processor.h"
@@ -631,25 +632,42 @@ static void __test_fixed_counters(struct kvm_vcpu *vcpu, u8 nr_fixed_counters,
static void test_fixed_counters(u8 pmu_version, u64 perf_capabilities)
{
u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
+ unsigned long fixed_subset;
struct kvm_vcpu **vcpus;
struct kvm_vm *vm;
+ u32 fixed_bitmap;
int i = 0;
- u32 k;
u8 j;
pr_info("Testing %u fixed counters, PMU version %u, perf_caps = %lx\n",
nr_fixed_counters, pmu_version, perf_capabilities);
+ fixed_bitmap = BIT_ULL(nr_fixed_counters) - 1;
+ if (pmu_version >= 5)
+ fixed_bitmap |= kvm_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK);
- vm = pmu_vm_create_with_vcpus((nr_fixed_counters + 1) * BIT(nr_fixed_counters),
+ vm = pmu_vm_create_with_vcpus(BIT(__builtin_popcount(fixed_bitmap)) + nr_fixed_counters,
guest_test_fixed_counters,
pmu_version, perf_capabilities, &vcpus);
- for (j = 0; j <= nr_fixed_counters; j++) {
- for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
- __test_fixed_counters(vcpus[i++], j, k);
+ for (fixed_subset = 0; fixed_subset <= fixed_bitmap; fixed_subset++) {
+ u32 nr_contiguous;
+
+ /*
+ * The loop walks all values from 0 to fixed_bitmap, so skip any
+ * value that is not a subset of fixed_bitmap.
+ */
+ if (fixed_subset & ~fixed_bitmap)
+ continue;
+
+ nr_contiguous = find_first_zero_bit(&fixed_subset,
+ MAX_NR_FIXED_COUNTERS);
+ __test_fixed_counters(vcpus[i++], nr_contiguous, fixed_subset);
}
+ for (j = 1; j <= nr_fixed_counters; j++)
+ __test_fixed_counters(vcpus[i++], j, 0);
+
pmu_vm_free(vm, vcpus);
}
|
| 61/3 | 2026/11/08 23:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23329 output: 22 thoughts: 1401 Thoughts: **KMSAN Evaluation: KVM x86 vPMU Refactor** Okay, let's get down to brass tacks. I need to figure out if this patch series, which refactors the KVM x86 vPMU support, warrants a dedicated KMSAN fuzzing session. The guiding principle is clear: if KASAN, LOCKDEP, or any other standard bug detectors can catch the issue, we don't need KMSAN. We only invoke KMSAN if the code *introduces or exposes* uninitialized memory reads or information leaks that KMSAN *specifically* detects. I'm focusing on those critical KMSAN scenarios: kernel structures leaking uninitialized data to userspace (via the usual suspects like `copy_to_user`), branching logic that depends on potentially uninitialized variables, allocation of complex structures with potentially uninitialized fields, or potentially uninitialized buffer access facilitated through bounds checks. If the patch primarily touches issues like out-of-bounds access, use-after-free, or locking problems, KMSAN isn't the primary tool, KASAN and the others will handle them. Also, if everything is properly initialized or zeroed, we're good. Now, let's dive into the code. This patch series touches a fair number of files: Documentation, core x86 event handling, KVM headers, CPUID and MSR handling, and some selftests. The core of it seems to be refactoring the performance monitoring unit (PMU) within KVM. It's moving from counter counts to bitmasks, adding support for new CPUID features, and handling specific MSRs. I need to meticulously examine each file, keeping a laser focus on where data is potentially exposed to userspace or where uninitialized reads could sneak in. The first file of interest, `arch/x86/events/core.c`, is the perfect starting point. The `perf_get_x86_pmu_capability` function, specifically, is intriguing. It populates a `struct x86_pmu_capability`. That structure is defined in `arch/x86/include/asm/perf_event.h`. This structure is where the refactoring from counts to bitmasks is happening. Now I'm worried: is this `struct x86_pmu_capability` ever *directly* copied to userspace? That's the critical question. If so, and if any of the new bitmasks aren't properly initialized before being exposed, that's a classic KMSAN scenario. Now, I'm going to fire up the grepper to confirm my suspicion... |
| 62/3 | 2026/11/08 23:32 | tool | grepper |
0mArgs:{
"Expression": "x86_pmu_capability"
}Results: {
"Output": "arch/x86/events/core.c=3135=unsigned long perf_arch_misc_flags(struct pt_regs *regs)\n--\narch/x86/events/core.c-3143-\narch/x86/events/core.c:3144:void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)\narch/x86/events/core.c-3145-{\n--\narch/x86/events/core.c-3167-}\narch/x86/events/core.c:3168:EXPORT_SYMBOL_FOR_KVM(perf_get_x86_pmu_capability);\narch/x86/events/core.c-3169-\n--\narch/x86/include/asm/perf_event.h=287=union cpuid_0x80000022_ebx {\n--\narch/x86/include/asm/perf_event.h-300-\narch/x86/include/asm/perf_event.h:301:struct x86_pmu_capability {\narch/x86/include/asm/perf_event.h-302-\tint\t\tversion;\n--\narch/x86/include/asm/perf_event.h=767=struct x86_pmu_lbr {\n--\narch/x86/include/asm/perf_event.h-774-\narch/x86/include/asm/perf_event.h:775:extern void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap);\narch/x86/include/asm/perf_event.h-776-extern u64 perf_get_hw_event_config(int hw_event);\n--\narch/x86/include/asm/perf_event.h=779=extern int x86_perf_rdpmc_index(struct perf_event *event);\narch/x86/include/asm/perf_event.h-780-#else\narch/x86/include/asm/perf_event.h:781:static inline void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)\narch/x86/include/asm/perf_event.h-782-{\n--\narch/x86/kvm/pmu.c-29-/* Unadulterated PMU capabilities of the host, i.e. of hardware. */\narch/x86/kvm/pmu.c:30:struct x86_pmu_capability __read_mostly kvm_host_pmu;\narch/x86/kvm/pmu.c-31-EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host_pmu);\n--\narch/x86/kvm/pmu.c-33-/* KVM's PMU capabilities, i.e. the intersection of KVM and hardware support. */\narch/x86/kvm/pmu.c:34:struct x86_pmu_capability __read_mostly kvm_pmu_cap;\narch/x86/kvm/pmu.c-35-EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_pmu_cap);\n--\narch/x86/kvm/pmu.c=133=void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n--\narch/x86/kvm/pmu.c-147-\t} else {\narch/x86/kvm/pmu.c:148:\t\tperf_get_x86_pmu_capability(\u0026kvm_host_pmu);\narch/x86/kvm/pmu.c-149-\t}\n--\narch/x86/kvm/pmu.h=26=struct kvm_pmu_ops {\n--\narch/x86/kvm/pmu.h-40-\narch/x86/kvm/pmu.h:41:\tbool (*is_mediated_pmu_supported)(struct x86_pmu_capability *host_pmu);\narch/x86/kvm/pmu.h-42-\tvoid (*mediated_load)(struct kvm_vcpu *vcpu);\n--\narch/x86/kvm/pmu.h=237=static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)\n--\narch/x86/kvm/pmu.h-254-\narch/x86/kvm/pmu.h:255:extern struct x86_pmu_capability kvm_host_pmu;\narch/x86/kvm/pmu.h:256:extern struct x86_pmu_capability kvm_pmu_cap;\narch/x86/kvm/pmu.h-257-\n--\narch/x86/kvm/svm/pmu.c=230=static void amd_pmu_init(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/pmu.c-244-\narch/x86/kvm/svm/pmu.c:245:static bool amd_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_pmu)\narch/x86/kvm/svm/pmu.c-246-{\n--\narch/x86/kvm/vmx/pmu_intel.c=846=void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu)\n--\narch/x86/kvm/vmx/pmu_intel.c-865-\narch/x86/kvm/vmx/pmu_intel.c:866:static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_pmu)\narch/x86/kvm/vmx/pmu_intel.c-867-{\n"
} |
| 63/3 | 2026/11/08 23:32 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 26863 output: 41 thoughts: 80 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into x86 PMU Capability Initialization** Okay, so `x86_pmu_capability` is apparently an internal detail, mainly a communication channel between the `perf` tool and the KVM (Kernel-based Virtual Machine) infrastructure. My task now is to figure out *how* the `kvm_host_pmu` component is initialized, and how that relates to this capability. It seems logical to start by examining how the capability is set and used in the KVM itself. Let's dig into that `kvm_init_pmu_capability` function, specifically looking at the relevant section – lines 130 to 190 in `arch/x86/kvm/pmu.c`. That should give me the core logic for the capability setup. I'm expecting to see the initialization of `kvm_host_pmu`, configuration based on the hardware's capabilities, or perhaps some interaction with the host's PMU (Performance Monitoring Unit) configuration. That file location is where I'll find my answers. This is where I'll see how KVM interacts with the PMU. |
| 64/3 | 2026/11/08 23:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/pmu.c",
"FirstLine": 130,
"LineCount": 60
}Results: {
"Contents": " 130:\t#undef __KVM_X86_PMU_OP\n 131:\t}\n 132:\t\n 133:\tvoid kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n 134:\t{\n 135:\t\tbool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;\n 136:\t\tint min_nr_gp_ctrs = pmu_ops-\u003eMIN_NR_GP_COUNTERS;\n 137:\t\n 138:\t\t/*\n 139:\t\t * Hybrid PMUs don't play nice with virtualization without careful\n 140:\t\t * configuration by userspace, and KVM's APIs for reporting supported\n 141:\t\t * vPMU features do not account for hybrid PMUs. Disable vPMU support\n 142:\t\t * for hybrid PMUs until KVM gains a way to let userspace opt-in.\n 143:\t\t */\n 144:\t\tif (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) {\n 145:\t\t\tenable_pmu = false;\n 146:\t\t\tmemset(\u0026kvm_host_pmu, 0, sizeof(kvm_host_pmu));\n 147:\t\t} else {\n 148:\t\t\tperf_get_x86_pmu_capability(\u0026kvm_host_pmu);\n 149:\t\t}\n 150:\t\n 151:\t\tif (enable_pmu) {\n 152:\t\t\t/*\n 153:\t\t\t * WARN if perf did NOT disable hardware PMU if the number of\n 154:\t\t\t * architecturally required GP counters aren't present, i.e. if\n 155:\t\t\t * there are a non-zero number of counters, but fewer than what\n 156:\t\t\t * is architecturally required.\n 157:\t\t\t */\n 158:\t\t\tif (!kvm_host_pmu.cntr_mask64 ||\n 159:\t\t\t WARN_ON_ONCE(hweight64(kvm_host_pmu.cntr_mask64) \u003c min_nr_gp_ctrs))\n 160:\t\t\t\tenable_pmu = false;\n 161:\t\t\telse if (is_intel \u0026\u0026 !kvm_host_pmu.version)\n 162:\t\t\t\tenable_pmu = false;\n 163:\t\t}\n 164:\t\n 165:\t\tif (!enable_pmu || !enable_mediated_pmu || !kvm_host_pmu.mediated ||\n 166:\t\t !pmu_ops-\u003eis_mediated_pmu_supported(\u0026kvm_host_pmu))\n 167:\t\t\tenable_mediated_pmu = false;\n 168:\t\n 169:\t\tif (!enable_mediated_pmu)\n 170:\t\t\tpmu_ops-\u003ewrite_global_ctrl = NULL;\n 171:\t\n 172:\t\tif (!enable_pmu) {\n 173:\t\t\tmemset(\u0026kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));\n 174:\t\t\treturn;\n 175:\t\t}\n 176:\t\n 177:\t\tmemcpy(\u0026kvm_pmu_cap, \u0026kvm_host_pmu, sizeof(kvm_host_pmu));\n 178:\t\n 179:\t\t/*\n 180:\t\t * PMU v3/v4 has AnyThread support, which KVM cannot safely virtualize.\n 181:\t\t * Only expose PMU v5+ for Intel mediated PMU, where AnyThread is\n 182:\t\t * deprecated; otherwise, cap at PMU v2.\n 183:\t\t */\n 184:\t\tif (is_intel \u0026\u0026 enable_mediated_pmu \u0026\u0026 kvm_pmu_cap.version \u003e= 5)\n 185:\t\t\tkvm_pmu_cap.version = 5;\n 186:\t\telse\n 187:\t\t\tkvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);\n 188:\t\n 189:\t\tkvm_pmu_cap.cntr_mask64 \u0026=\n"
} |
| 65/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28341 output: 40 thoughts: 42 Thoughts: Here's my summary, as if I were thinking these thoughts: **Dissecting x86 PMU Capability Detection** Okay, so I'm diving into this `perf_get_x86_pmu_capability` function. Seems like the key to understanding how we're determining the capabilities of the x86 Performance Monitoring Unit. Right, let's pull up the source code for this, specifically `arch/x86/events/core.c`. I know from experience that the critical sections usually revolve around the implementation-specific details. So, I need to focus on this section around line 3144. Time to see what's going on under the hood and understand what it's trying to get at. Let's see how the architecture handles the PMU-related event and counter capabilities, and what kind of checks are performed. |
| 66/3 | 2026/11/08 23:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/events/core.c",
"FirstLine": 3144,
"LineCount": 40
}Results: {
"Contents": "3144:\tvoid perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)\n3145:\t{\n3146:\t\t/* This API doesn't currently support enumerating hybrid PMUs. */\n3147:\t\tif (WARN_ON_ONCE(cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) ||\n3148:\t\t !x86_pmu_initialized()) {\n3149:\t\t\tmemset(cap, 0, sizeof(*cap));\n3150:\t\t\treturn;\n3151:\t\t}\n3152:\t\n3153:\t\t/*\n3154:\t\t * Note, hybrid CPU models get tracked as having hybrid PMUs even when\n3155:\t\t * all E-cores are disabled via BIOS. When E-cores are disabled, the\n3156:\t\t * base PMU holds the correct number of counters for P-cores.\n3157:\t\t */\n3158:\t\tcap-\u003eversion\t\t= x86_pmu.version;\n3159:\t\tcap-\u003ecntr_mask64\t= x86_pmu.cntr_mask64;\n3160:\t\tcap-\u003efixed_cntr_mask64\t= x86_pmu.fixed_cntr_mask64;\n3161:\t\tcap-\u003ebit_width_gp\t= cap-\u003ecntr_mask64 ? x86_pmu.cntval_bits : 0;\n3162:\t\tcap-\u003ebit_width_fixed\t= cap-\u003efixed_cntr_mask64 ? x86_pmu.cntval_bits : 0;\n3163:\t\tcap-\u003eevents_mask\t= (unsigned int)x86_pmu.events_maskl;\n3164:\t\tcap-\u003eevents_mask_len\t= x86_pmu.events_mask_len;\n3165:\t\tcap-\u003epebs_ept\t\t= x86_pmu.pebs_ept;\n3166:\t\tcap-\u003emediated\t\t= !!(pmu.capabilities \u0026 PERF_PMU_CAP_MEDIATED_VPMU);\n3167:\t}\n3168:\tEXPORT_SYMBOL_FOR_KVM(perf_get_x86_pmu_capability);\n3169:\t\n3170:\tu64 perf_get_hw_event_config(int hw_event)\n3171:\t{\n3172:\t\tint max = x86_pmu.max_events;\n3173:\t\n3174:\t\tif (hw_event \u003c max)\n3175:\t\t\treturn x86_pmu.event_map(array_index_nospec(hw_event, max));\n3176:\t\n3177:\t\treturn 0;\n3178:\t}\n3179:\tEXPORT_SYMBOL_FOR_KVM(perf_get_hw_event_config);\n"
} |
| 67/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29358 output: 23 thoughts: 354 Thoughts: **My Analysis of Potential Uninitialized Memory** Okay, so I'm looking at this `perf_get_x86_pmu_capability` function, and something's bugging me. I noticed it doesn't do a `memset` on the `cap` structure on the non-error path. That's unusual, and a potential source of problems. Wait a minute... `kvm_host_pmu` is defined as a global variable, `__read_mostly`, so it's statically zero-initialized in the BSS section. So, in this specific case, it might be okay. But that doesn't mean all usages are safe. I need to investigate further. What about other callers of this function, or other members of `x86_pmu_capability`? I'd better check the actual definition of `struct x86_pmu_capability` in `arch/x86/include/asm/perf_event.h`. Okay, here we go: it includes `version`, then a couple of unions using `cntr_mask64` or a bitmask, then `bit_width_gp`, `bit_width_fixed`, `events_mask`, `events_mask_len`, and some boolean fields. Ah, but hold on... I see that bitmap definition using `X86_PMC_IDX_MAX`. I'm not familiar with that macro off the top of my head. I better search for the definition of `X86_PMC_IDX_MAX`. That's where I need to focus my attention. It's a key piece of information that will determine whether this is a real problem or not. |
| 68/3 | 2026/11/08 23:33 | tool | grepper |
0mArgs:{
"Expression": "X86_PMC_IDX_MAX"
}Results: {
"Output": "\nFull output is too long, showing 200 out of 457 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (13 files in total):\narch/x86/events/amd/core.c:7\narch/x86/events/core.c:20\narch/x86/events/intel/core.c:8\narch/x86/events/intel/ds.c:5\narch/x86/events/intel/knc.c:1\narch/x86/events/intel/lbr.c:1\narch/x86/events/intel/p4.c:7\narch/x86/events/perf_event.h:23\narch/x86/events/zhaoxin/core.c:1\narch/x86/include/asm/kvm_host.h:6\narch/x86/include/asm/perf_event.h:3\narch/x86/kvm/pmu.c:13\narch/x86/kvm/pmu.h:2\n\narch/x86/events/amd/core.c=292=static u64 amd_pmu_event_map(int hw_event)\n--\narch/x86/events/amd/core.c-308- */\narch/x86/events/amd/core.c:309:static unsigned int event_offsets[X86_PMC_IDX_MAX] __read_mostly;\narch/x86/events/amd/core.c:310:static unsigned int count_offsets[X86_PMC_IDX_MAX] __read_mostly;\narch/x86/events/amd/core.c-311-\n--\narch/x86/events/amd/core.c=425=static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,\n--\narch/x86/events/amd/core.c-438-\t */\narch/x86/events/amd/core.c:439:\tfor_each_set_bit(i, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/amd/core.c-440-\t\tstruct perf_event *tmp = event;\n--\narch/x86/events/amd/core.c=537=static struct amd_nb *amd_alloc_nb(int cpu)\n--\narch/x86/events/amd/core.c-550-\t */\narch/x86/events/amd/core.c:551:\tfor_each_set_bit(i, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/amd/core.c-552-\t\t__set_bit(i, nb-\u003eevent_constraints[i].idxmsk);\n--\narch/x86/events/amd/core.c=725=static void amd_pmu_check_overflow(void)\n--\narch/x86/events/amd/core.c-743-\t */\narch/x86/events/amd/core.c:744:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/amd/core.c-745-\t\tif (!test_bit(idx, cpuc-\u003eactive_mask))\n--\narch/x86/events/amd/core.c=757=static void __amd_pmu_enable_all(void)\n--\narch/x86/events/amd/core.c-761-\narch/x86/events/amd/core.c:762:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/amd/core.c-763-\t\t/* only activate events which are marked as active */\n--\narch/x86/events/amd/core.c=956=static int amd_pmu_v2_handle_irq(struct pt_regs *regs)\n--\narch/x86/events/amd/core.c-996-\narch/x86/events/amd/core.c:997:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/amd/core.c-998-\t\tif (!test_bit(idx, cpuc-\u003eactive_mask))\n--\narch/x86/events/core.c=219=static bool reserve_pmc_hardware(void)\n--\narch/x86/events/core.c-223-\narch/x86/events/core.c:224:\tfor_each_set_bit(i, (unsigned long *)\u0026cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-225-\t\tif (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))\n--\narch/x86/events/core.c-228-\narch/x86/events/core.c:229:\tfor_each_set_bit(i, (unsigned long *)\u0026cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-230-\t\tif (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))\n--\narch/x86/events/core.c-239-\t\trelease_evntsel_nmi(x86_pmu_config_addr(i));\narch/x86/events/core.c:240:\ti = X86_PMC_IDX_MAX;\narch/x86/events/core.c-241-\n--\narch/x86/events/core.c=250=static void release_pmc_hardware(void)\n--\narch/x86/events/core.c-254-\narch/x86/events/core.c:255:\tfor_each_set_bit(i, (unsigned long *)\u0026cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-256-\t\trelease_perfctr_nmi(x86_pmu_event_addr(i));\n--\narch/x86/events/core.c=268=bool check_hw_exists(unsigned long *cntr_mask,\n--\narch/x86/events/core.c-279-\t */\narch/x86/events/core.c:280:\tfor_each_set_bit(i, cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-281-\t\treg = x86_pmu_config_addr(i);\n--\narch/x86/events/core.c-298-\t\t\tgoto msr_fail;\narch/x86/events/core.c:299:\t\tfor_each_set_bit(i, fixed_cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-300-\t\t\tif (val \u0026 (0x03ULL \u003c\u003c i*4)) {\n--\narch/x86/events/core.c=703=void x86_pmu_disable_all(void)\n--\narch/x86/events/core.c-707-\narch/x86/events/core.c:708:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-709-\t\tstruct hw_perf_event *hwc = \u0026cpuc-\u003eevents[idx]-\u003ehw;\n--\narch/x86/events/core.c=760=void x86_pmu_enable_all(int added)\n--\narch/x86/events/core.c-764-\narch/x86/events/core.c:765:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-766-\t\tstruct hw_perf_event *hwc = \u0026cpuc-\u003eevents[idx]-\u003ehw;\n--\narch/x86/events/core.c=816=struct sched_state {\n--\narch/x86/events/core.c-824-\narch/x86/events/core.c:825:/* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */\narch/x86/events/core.c-826-#define\tSCHED_STATES_MAX\t2\n--\narch/x86/events/core.c=893=static bool __perf_sched_find_counter(struct perf_sched *sched)\n--\narch/x86/events/core.c-907-\t\tidx = INTEL_PMC_IDX_FIXED;\narch/x86/events/core.c:908:\t\tfor_each_set_bit_from(idx, c-\u003eidxmsk, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-909-\t\t\tu64 mask = BIT_ULL(idx);\n--\narch/x86/events/core.c=1008=int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)\n--\narch/x86/events/core.c-1028-\narch/x86/events/core.c:1029:\tfor (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i \u003c n; i++) {\narch/x86/events/core.c-1030-\t\tc = cpuc-\u003eevent_constraint[i];\n--\narch/x86/events/core.c=1325=static void x86_pmu_enable(struct pmu *pmu)\n--\narch/x86/events/core.c-1414-\narch/x86/events/core.c:1415:DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);\narch/x86/events/core.c-1416-\n--\narch/x86/events/core.c=1493=static int x86_pmu_add(struct perf_event *event, int flags)\n--\narch/x86/events/core.c-1496-\tstruct hw_perf_event *hwc;\narch/x86/events/core.c:1497:\tint assign[X86_PMC_IDX_MAX];\narch/x86/events/core.c-1498-\tint n, n0, ret;\n--\narch/x86/events/core.c=1574=void perf_event_print_debug(void)\n--\narch/x86/events/core.c-1615-\narch/x86/events/core.c:1616:\tfor_each_set_bit(idx, cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-1617-\t\trdmsrq(x86_pmu_config_addr(idx), pmc_ctrl);\n--\narch/x86/events/core.c-1628-\t}\narch/x86/events/core.c:1629:\tfor_each_set_bit(idx, fixed_cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-1630-\t\trdmsrq(x86_pmu_fixed_ctr_addr(idx), pmc_count);\n--\narch/x86/events/core.c=1720=int x86_pmu_handle_irq(struct pt_regs *regs)\n--\narch/x86/events/core.c-1740-\narch/x86/events/core.c:1741:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-1742-\t\tif (!test_bit(idx, cpuc-\u003eactive_mask))\n--\narch/x86/events/core.c=2370=static int x86_pmu_commit_txn(struct pmu *pmu)\n--\narch/x86/events/core.c-2372-\tstruct cpu_hw_events *cpuc = this_cpu_ptr(\u0026cpu_hw_events);\narch/x86/events/core.c:2373:\tint assign[X86_PMC_IDX_MAX];\narch/x86/events/core.c-2374-\tint n, ret;\n--\narch/x86/events/core.c=2572=void perf_clear_dirty_counters(void)\n--\narch/x86/events/core.c-2580-\narch/x86/events/core.c:2581:\tif (bitmap_empty(cpuc-\u003edirty, X86_PMC_IDX_MAX))\narch/x86/events/core.c-2582-\t\treturn;\narch/x86/events/core.c-2583-\narch/x86/events/core.c:2584:\tfor_each_set_bit(i, cpuc-\u003edirty, X86_PMC_IDX_MAX) {\narch/x86/events/core.c-2585-\t\tif (i \u003e= INTEL_PMC_IDX_FIXED) {\n--\narch/x86/events/core.c-2595-\narch/x86/events/core.c:2596:\tbitmap_zero(cpuc-\u003edirty, X86_PMC_IDX_MAX);\narch/x86/events/core.c-2597-}\n--\narch/x86/events/intel/core.c=3617=static void intel_pmu_acr_late_setup(struct cpu_hw_events *cpuc)\n--\narch/x86/events/intel/core.c-3648-\t\t\tevent-\u003ehw.config1 = 0;\narch/x86/events/intel/core.c:3649:\t\t\tfor_each_set_bit(bit, (unsigned long *)\u0026event-\u003eattr.config2, X86_PMC_IDX_MAX) {\narch/x86/events/intel/core.c-3650-\t\t\t\tidx = i + bit;\n--\narch/x86/events/intel/core.c=3773=static void x86_pmu_handle_guest_pebs(struct pt_regs *regs,\n--\narch/x86/events/intel/core.c-3787-\narch/x86/events/intel/core.c:3788:\tfor_each_set_bit(bit, (unsigned long *)\u0026guest_pebs_idxs, X86_PMC_IDX_MAX) {\narch/x86/events/intel/core.c-3789-\t\tevent = cpuc-\u003eevents[bit];\n--\narch/x86/events/intel/core.c=3801=static int handle_pmi_common(struct pt_regs *regs, u64 status)\n--\narch/x86/events/intel/core.c-3909-\narch/x86/events/intel/core.c:3910:\tfor_each_set_bit(bit, (unsigned long *)\u0026status, X86_PMC_IDX_MAX) {\narch/x86/events/intel/core.c-3911-\t\tstruct perf_event *event = cpuc-\u003eevents[bit];\n--\narch/x86/events/intel/core.c=4417=intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,\n--\narch/x86/events/intel/core.c-4479-\tw = c-\u003eweight;\narch/x86/events/intel/core.c:4480:\tfor_each_set_bit(i, c-\u003eidxmsk, X86_PMC_IDX_MAX) {\narch/x86/events/intel/core.c-4481-\t\t/*\n--\narch/x86/events/intel/core.c=4517=intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,\n--\narch/x86/events/intel/core.c-4531-\t WARN_ON_ONCE(!(c1-\u003eflags \u0026 PERF_X86_EVENT_DYNAMIC));\narch/x86/events/intel/core.c:4532:\t\tbitmap_copy(c1-\u003eidxmsk, c2-\u003eidxmsk, X86_PMC_IDX_MAX);\narch/x86/events/intel/core.c-4533-\t\tc1-\u003eweight = c2-\u003eweight;\n--\narch/x86/events/intel/core.c=5408=static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr, void *data)\n--\narch/x86/events/intel/core.c-5413-\narch/x86/events/intel/core.c:5414:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/intel/core.c-5415-\t\tstruct perf_event *event = cpuc-\u003eevents[idx];\n--\narch/x86/events/intel/core.c=5442=static void core_pmu_enable_all(int added)\n--\narch/x86/events/intel/core.c-5446-\narch/x86/events/intel/core.c:5447:\tfor_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {\narch/x86/events/intel/core.c-5448-\t\tstruct hw_perf_event *hwc = \u0026cpuc-\u003eevents[idx]-\u003ehw;\n--\narch/x86/events/intel/core.c=5938=int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu)\n--\narch/x86/events/intel/core.c-5948-\tif (x86_pmu.flags \u0026 (PMU_FL_EXCL_CNTRS | PMU_FL_TFA | PMU_FL_DYN_CONSTRAINT)) {\narch/x86/events/intel/core.c:5949:\t\tsize_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);\narch/x86/events/intel/core.c-5950-\n--\narch/x86/events/intel/ds.c=1788=u64 intel_get_arch_pebs_data_config(struct perf_event *event)\n--\narch/x86/events/intel/ds.c-1793-\narch/x86/events/intel/ds.c:1794:\tif (WARN_ON(event-\u003ehw.idx \u003c 0 || event-\u003ehw.idx \u003e= X86_PMC_IDX_MAX))\narch/x86/events/intel/ds.c-1795-\t\treturn 0;\n--\narch/x86/events/intel/ds.c=2993=static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, u64 mask)\n--\n\n"
} |
| 69/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35191 output: 25 thoughts: 38 Thoughts: Okay, let's dive into this. Here's my thought process on this: **Investigating the Definition of `X86_PMC_IDX_MAX`** Alright, I'm working with some x86 performance counters, and I need to figure out the maximum index for these Programmed Measure Counters (PMCs). My first thought is to determine what the macro `X86_PMC_IDX_MAX` represents. Where is it defined? I'll quickly need to ascertain its value, because I can't proceed without knowing the upper limit on these PMCs. A quick `grep` for `#define X86_PMC_IDX_MAX` should do the trick. That should pinpoint where this is defined in the source code. |
| 70/3 | 2026/11/08 23:33 | tool | grepper |
0mArgs:{
"Expression": "#define X86_PMC_IDX_MAX"
}Results: {
"Output": "arch/x86/include/asm/perf_event.h-14-\narch/x86/include/asm/perf_event.h:15:#define X86_PMC_IDX_MAX\t\t\t\t\t 64\narch/x86/include/asm/perf_event.h-16-\n"
} |
| 71/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35500 output: 42 thoughts: 1084 Thoughts: **Analyzing KVM's CPUID and MSR Handling for Performance Counters** Okay, so I'm looking at how KVM exposes performance counter capabilities via CPUID and MSRs. The first thing that jumps out is the hardcoded maximum number of performance counters, `X86_PMC_IDX_MAX`, which is 64. On amd64, `DECLARE_BITMAP(cntr_mask, 64)` expands to `unsigned long cntr_mask[1]`, an 8-byte array – 64 bits – and `u64 cntr_mask64` is also 8 bytes, or 64 bits. Now, in `struct kvm_pmu`, there's this `union` containing both a bitmap (`DECLARE_BITMAP`) and a `u64`, and the `BUILD_BUG_ON` ensures they are the same size. So, the kernel uses either the bitmap or a 64-bit integer, for the same underlying 64 bits representing the availability of the performance counters. Now, I'm interested in what happens when a user-space program or a guest tries to access performance counter information via CPUID or MSRs, and seeing how the kernel sets these values. Let's look at the CPUID handling in `arch/x86/kvm/cpuid.c`. Specifically, around line 1530 in `__do_cpuid_func`. I see that the code uses `kvm_pmu_cap.version` to signal the presence of performance counters. The number of general-purpose counters reported in CPUID leaf 0x00000004 is determined by `find_first_zero_bit` on `kvm_pmu_cap.cntr_mask`. This suggests that only contiguous GP counters are supported, where the first available counter index is returned. This is interesting: even if the hardware has counters that are not contiguous, the KVM interface presents them as a contiguous set. Also, fixed counters use `find_first_zero_bit` on `kvm_pmu_cap.fixed_cntr_mask`. Looking further, for leaf 0x80000022, the number of core PMCs (`ebx.split.num_core_pmc`) now seems to be derived using `hweight64` on `kvm_pmu_cap.cntr_mask64`. This means the code is now reporting the number of *set* bits in the bitmap, *not* the contiguous available counters. Why the change? Is this behavior consistent? This means we are looking at the *actual* number of available counters! The `ecx` register for CPUID leaf 0x4 contains the 64-bit mask for fixed counters, if the version is 5 or greater. This seems like it can be used to set the fixed-function counters in the guest. So, I need to make sure I understand how all these values are populated and how they correlate to the underlying hardware capabilities. I need to make certain all the registers, like `eax`, `edx`, and `ebx`, as well as `entry->...` are fully initialized and consistent with the intended presentation of the performance counter information to the guest. It's crucial for the virtualized environment to expose the right information! |
| 72/3 | 2026/11/08 23:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/cpuid.c",
"FirstLine": 1515,
"LineCount": 45
}Results: {
"Contents": "1515:\t\t\t\tentry-\u003eebx = 0;\n1516:\t\t\t}\n1517:\t\t\tif (max_idx \u003e= 2) {\n1518:\t\t\t\tentry = do_host_cpuid(array, function, 2);\n1519:\t\t\t\tif (!entry)\n1520:\t\t\t\t\tgoto out;\n1521:\t\n1522:\t\t\t\tcpuid_entry_override(entry, CPUID_7_2_EDX);\n1523:\t\t\t\tentry-\u003eecx = 0;\n1524:\t\t\t\tentry-\u003eebx = 0;\n1525:\t\t\t\tentry-\u003eeax = 0;\n1526:\t\t\t}\n1527:\t\t\tbreak;\n1528:\t\tcase 0xa: { /* Architectural Performance Monitoring */\n1529:\t\t\tunion cpuid10_eax eax = { };\n1530:\t\t\tunion cpuid10_edx edx = { };\n1531:\t\n1532:\t\t\tif (!enable_pmu || !cpu_feature_enabled(X86_FEATURE_ARCH_PERFMON)) {\n1533:\t\t\t\tentry-\u003eeax = entry-\u003eebx = entry-\u003eecx = entry-\u003eedx = 0;\n1534:\t\t\t\tbreak;\n1535:\t\t\t}\n1536:\t\n1537:\t\t\teax.split.version_id = kvm_pmu_cap.version;\n1538:\t\n1539:\t\t\t/* Contiguous GP counters only. */\n1540:\t\t\teax.split.num_counters =\n1541:\t\t\t\tfind_first_zero_bit(kvm_pmu_cap.cntr_mask,\n1542:\t\t\t\t\t\t KVM_MAX_NR_GP_COUNTERS);\n1543:\t\t\teax.split.bit_width = kvm_pmu_cap.bit_width_gp;\n1544:\t\t\teax.split.mask_length = kvm_pmu_cap.events_mask_len;\n1545:\t\n1546:\t\t\t/* Contiguous fixed counters only. */\n1547:\t\t\tedx.split.num_counters_fixed =\n1548:\t\t\t\tfind_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,\n1549:\t\t\t\t\t\t KVM_MAX_NR_FIXED_COUNTERS);\n1550:\t\t\tedx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;\n1551:\t\n1552:\t\t\tif (kvm_pmu_cap.version \u003e= 5)\n1553:\t\t\t\tedx.split.anythread_deprecated = 1;\n1554:\t\n1555:\t\t\tentry-\u003eeax = eax.full;\n1556:\t\t\tentry-\u003eebx = kvm_pmu_cap.events_mask;\n1557:\t\t\tif (kvm_pmu_cap.version \u003e= 5)\n1558:\t\t\t\tentry-\u003eecx = (u32)kvm_pmu_cap.fixed_cntr_mask64;\n1559:\t\t\telse\n"
} |
| 73/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38150 output: 42 thoughts: 122 Thoughts: Okay, let's break this down.
**Analyzing CPUID Function Handling**
Right, so I'm looking at how CPUID functions are being managed, specifically with `eax` and `edx`. It appears that `eax` and `edx` are both zeroed out at the start, using this `union` approach: `union cpuid10_eax eax = { };` and `union cpuid10_edx edx = { };`. Seems like a safe and clean initialization. Then, I can see that `entry->eax`, `entry->ebx`, `entry->ecx`, and `entry->edx` are all being explicitly assigned values. This is good; it gives me fine-grained control and I can track the changes.
My main focus right now is on function `0x80000022`. I need to understand the specifics of how this function is handled. I need to dig into the implementation details to fully comprehend the implications here. I should jump over to `arch/x86/kvm/cpuid.c`, specifically lines 1900-1925, to check exactly how it's being handled.
|
| 74/3 | 2026/11/08 23:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/cpuid.c",
"FirstLine": 1900,
"LineCount": 25
}Results: {
"Contents": "1900:\t\t\telse\n1901:\t\t\t\tentry-\u003eebx = 0;\n1902:\t\n1903:\t\t\tcpuid_entry_override(entry, CPUID_8000_0021_ECX);\n1904:\t\t\tbreak;\n1905:\t\t/* AMD Extended Performance Monitoring and Debug */\n1906:\t\tcase 0x80000022: {\n1907:\t\t\tunion cpuid_0x80000022_ebx ebx = { };\n1908:\t\n1909:\t\t\tentry-\u003eecx = entry-\u003eedx = 0;\n1910:\t\t\tif (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) {\n1911:\t\t\t\tentry-\u003eeax = entry-\u003eebx = 0;\n1912:\t\t\t\tbreak;\n1913:\t\t\t}\n1914:\t\n1915:\t\t\tcpuid_entry_override(entry, CPUID_8000_0022_EAX);\n1916:\t\n1917:\t\t\tebx.split.num_core_pmc = hweight64(kvm_pmu_cap.cntr_mask64);\n1918:\t\t\tentry-\u003eebx = ebx.full;\n1919:\t\t\tbreak;\n1920:\t\t}\n1921:\t\t/*Add support for Centaur's CPUID instruction*/\n1922:\t\tcase 0xC0000000:\n1923:\t\t\t/*Just support up to 0xC0000004 now*/\n1924:\t\t\tentry-\u003eeax = min(entry-\u003eeax, 0xC0000004);\n"
} |
| 75/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39036 output: 45 thoughts: 1651 Thoughts: **Analyzing KVM's Performance Monitoring Unit (PMU) MSR Handling**
Okay, let's break down this MSR access logic. I see `union cpuid_0x80000022_ebx ebx = { };`, which is zero-initialized, good. Now, let's get into the heart of the matter – MSR handling, specifically concerning `MSR_CORE_PERF_GLOBAL_STATUS_SET` and `MSR_CORE_PERF_GLOBAL_INUSE`.
In `kvm_pmu_get_msr` within `pmu.c`, the `MSR_CORE_PERF_GLOBAL_STATUS_SET` case is handled by explicitly returning `msr_info->data = 0`. This is straightforward. However, `MSR_CORE_PERF_GLOBAL_INUSE` in `intel_pmu_get_msr` is more interesting, as it calls `intel_pmu_get_global_inuse`.
`intel_pmu_get_global_inuse` calculates the "in-use" status. `pmi_inuse` is initialized to `false`, and `data` to `0`. The code then iterates through general-purpose (GP) and fixed counters, examining `eventsel` and `fixed_ctrl` values retrieved from the `struct kvm_pmu`. Within these loops, `data` is bitwise OR'd with the counter index if the relevant event flags are set (event or PMI interrupt). Also, `pmi_inuse` is set to `true` if an event selects interrupts (`ARCH_PERFMON_EVENTSEL_INT`) or, in the case of fixed counters, `INTEL_FIXED_0_ENABLE_PMI` is set, or if PEBS is enabled. Finally, `PERF_GLOBAL_INUSE_PMI_INUSE` is OR'd into `data` if `pmi_inuse` is true and returned.
The critical point here is the initialization and state of `pmu`. I need to ensure its initialized correctly. I recall from earlier, `kvm_pmu_init` uses `memset(pmu, 0, sizeof(*pmu))` to zero it out, and `kvm_pmu_reset` then further clears bitmask members within `pmu`. This is good; the structure is zeroed initially, which simplifies tracking the state of various counters. Each `pmc` within `pmu` is also initialized to `0`.
Now, let's move to `kvm_probe_msr_to_save`. I need to confirm the conditions for selecting the msrs to save. The code checks the validity of MSR indexes, such as `MSR_ARCH_PERFMON_PERFCTR0`, using bitmasks in `kvm_pmu_cap`. Originally it checked against `num_counters_gp`, but now it check the bitmask, which is a better approach.
I need to confirm the state of `kvm_pmu_cap`. This appears to control which performance counters are valid based on the feature sets supported by the hardware.
|
| 76/3 | 2026/11/08 23:33 | tool | grepper |
0mArgs:{
"Expression": "kvm_probe_msr_to_save",
"PathPrefixes": [
"arch/x86/kvm/msrs.c"
]
}Results: {
"Output": "arch/x86/kvm/msrs.c=2564=static void kvm_probe_feature_msr(u32 msr_index)\n--\narch/x86/kvm/msrs.c-2573-\narch/x86/kvm/msrs.c:2574:static void kvm_probe_msr_to_save(u32 msr_index)\narch/x86/kvm/msrs.c-2575-{\n--\narch/x86/kvm/msrs.c=2681=void kvm_init_msr_lists(void)\n--\narch/x86/kvm/msrs.c-2692-\tfor (i = 0; i \u003c ARRAY_SIZE(msrs_to_save_base); i++)\narch/x86/kvm/msrs.c:2693:\t\tkvm_probe_msr_to_save(msrs_to_save_base[i]);\narch/x86/kvm/msrs.c-2694-\n--\narch/x86/kvm/msrs.c-2696-\t\tfor (i = 0; i \u003c ARRAY_SIZE(msrs_to_save_pmu); i++)\narch/x86/kvm/msrs.c:2697:\t\t\tkvm_probe_msr_to_save(msrs_to_save_pmu[i]);\narch/x86/kvm/msrs.c-2698-\t}\n"
} |
| 77/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41823 output: 42 thoughts: 33 Thoughts: Okay, let's dive into this. Here's how I'd approach this task, keeping in mind my own expert understanding: **Analyzing MSR Handling in `arch/x86/kvm/msrs.c`** Alright, the objective is to understand what's happening around lines 2574 and 2681 in `arch/x86/kvm/msrs.c`. I need to zero in on what MSR-related logic is being implemented within this area of the code, so that I have a better understanding of how the MSR values are handled by KVM. I'll need to consider what's happening during MSR access. The specifics of those lines will tell a tale. Is this related to a specific CPU model, a particular feature, or a general MSR handling mechanism? Are these lines related to a write operation, or a read, or possibly even both? I'm anticipating looking at the control flow around these lines. What functions are being called, what data structures are being manipulated? Are we looking at a hot path for frequent MSR accesses, or more of an edge case? Once I determine what functionality is provided at these lines, I can then determine what's happening. My aim is to extract the critical pieces of the puzzle. I'll need to know what kind of MSRs are involved, which guest VM context, and what the overall intention of this code is. I'm focusing on the *why* as much as the *what*. |
| 78/3 | 2026/11/08 23:33 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/msrs.c",
"FirstLine": 2570,
"LineCount": 55
}Results: {
"Contents": "2570:\t\n2571:\t\tmsr_based_features[num_msr_based_features++] = msr_index;\n2572:\t}\n2573:\t\n2574:\tstatic void kvm_probe_msr_to_save(u32 msr_index)\n2575:\t{\n2576:\t\tu64 dummy;\n2577:\t\n2578:\t\tif (rdmsrq_safe(msr_index, \u0026dummy))\n2579:\t\t\treturn;\n2580:\t\n2581:\t\t/*\n2582:\t\t * Even MSRs that are valid in the host may not be exposed to guests in\n2583:\t\t * some cases.\n2584:\t\t */\n2585:\t\tswitch (msr_index) {\n2586:\t\tcase MSR_IA32_BNDCFGS:\n2587:\t\t\tif (!kvm_mpx_supported())\n2588:\t\t\t\treturn;\n2589:\t\t\tbreak;\n2590:\t\tcase MSR_TSC_AUX:\n2591:\t\t\tif (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) \u0026\u0026\n2592:\t\t\t !kvm_cpu_cap_has(X86_FEATURE_RDPID))\n2593:\t\t\t\treturn;\n2594:\t\t\tbreak;\n2595:\t\tcase MSR_IA32_UMWAIT_CONTROL:\n2596:\t\t\tif (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))\n2597:\t\t\t\treturn;\n2598:\t\t\tbreak;\n2599:\t\tcase MSR_IA32_RTIT_CTL:\n2600:\t\tcase MSR_IA32_RTIT_STATUS:\n2601:\t\t\tif (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))\n2602:\t\t\t\treturn;\n2603:\t\t\tbreak;\n2604:\t\tcase MSR_IA32_RTIT_CR3_MATCH:\n2605:\t\t\tif (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||\n2606:\t\t\t !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))\n2607:\t\t\t\treturn;\n2608:\t\t\tbreak;\n2609:\t\tcase MSR_IA32_RTIT_OUTPUT_BASE:\n2610:\t\tcase MSR_IA32_RTIT_OUTPUT_MASK:\n2611:\t\t\tif (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||\n2612:\t\t\t (!intel_pt_validate_hw_cap(PT_CAP_topa_output) \u0026\u0026\n2613:\t\t\t !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))\n2614:\t\t\t\treturn;\n2615:\t\t\tbreak;\n2616:\t\tcase MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:\n2617:\t\t\tif (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) ||\n2618:\t\t\t (msr_index - MSR_IA32_RTIT_ADDR0_A \u003e=\n2619:\t\t\t intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))\n2620:\t\t\t\treturn;\n2621:\t\t\tbreak;\n2622:\t\tcase MSR_ARCH_PERFMON_PERFCTR0 ...\n2623:\t\t MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:\n2624:\t\t\tif (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_PERFCTR0) \u0026\n"
} |
| 79/3 | 2026/11/08 23:33 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43231 output: 21 thoughts: 998 Thoughts: **Analyzing PMU MSR Intercepts in KVM** Okay, so I'm looking into how KVM handles MSR intercepts related to Performance Monitoring Units (PMUs). Specifically, I'm trying to understand how the MSRs are saved and intercepted on both AMD (SVM) and Intel (VMX) hosts. First, `kvm_probe_msr_to_save` is pretty straightforward: it just appends the MSR index to `msrs_to_save`. The logic seems focused on checking a bit within `kvm_pmu_cap.cntr_mask64`, using `BIT_ULL()`. This bitmask probably indicates which performance counters are potentially relevant to save. Now, let's dive into the core of the interception logic. I'm focusing on `svm_recalc_pmu_msr_intercepts` and `vmx_recalc_pmu_msr_intercepts`. In the SVM (AMD) path, the `gp_mask` variable, calculated with `kvm_gp_pmc_mask(pmu)`, seems crucial. This mask likely represents a guest's ability to access generic performance counters. The code iterates through the available generic counters. The `test_bit(i, &gp_mask)` call is critical. It's checking if the guest *actually* has access to a particular counter, before setting an intercept. AMD's code uses `AMD64_NUM_COUNTERS`, and `AMD64_NUM_COUNTERS_CORE`, with the indexes `i` being within the range of 0 to 5. So, this mask is operating on 64 bits. The VMX (Intel) path is a bit more involved. Here, there are two masks: `fixed_mask` (from `kvm_fixed_pmc_mask(pmu)`) and `gp_mask` (from `kvm_gp_pmc_mask(pmu)`), which have a similar function as on AMD, i.e. describing the available counters and whether the guest has access. The code then uses `for_each_set_bit` to iterate through the counters. Intel's code uses `INTEL_PMC_MAX_GENERIC` and `INTEL_PMC_MAX_FIXED`, and these values are critical here. I'll need to check their definitions to understand the bounds of the loops. The important thing is: If either the guest doesn't have mediated PMU access or doesn't have access to this particular counter, an intercept is set. The code also considers `fw_writes_is_enabled(vcpu)` when setting intercepts for performance counters. This part needs further review. Essentially, on both AMD and Intel, the decision to intercept an MSR relies on: 1. Whether mediated PMU is enabled. 2. If the guest has access to the particular counter. 3. (On Intel) Whether firmware writes are enabled, in the case of the IA32_PMC* MSRs. I need to confirm the definitions of `INTEL_PMC_MAX_GENERIC` and `INTEL_PMC_MAX_FIXED` to fully understand the scope of the Intel code, and to assess whether any optimizations are possible. |
| 80/3 | 2026/11/08 23:34 | tool | grepper |
0mArgs:{
"Expression": "INTEL_PMC_MAX_"
}Results: {
"Output": "arch/x86/events/intel/core.c=3720=static void intel_pmu_reset(void)\n--\narch/x86/events/intel/core.c-3735-\narch/x86/events/intel/core.c:3736:\tfor_each_set_bit(idx, cntr_mask, INTEL_PMC_MAX_GENERIC) {\narch/x86/events/intel/core.c-3737-\t\twrmsrq_safe(x86_pmu_config_addr(idx), 0ull);\n--\narch/x86/events/intel/core.c-3739-\t}\narch/x86/events/intel/core.c:3740:\tfor_each_set_bit(idx, fixed_cntr_mask, INTEL_PMC_MAX_FIXED) {\narch/x86/events/intel/core.c-3741-\t\twrmsrq_safe(x86_pmu_fixed_ctr_addr(idx), 0ull);\n--\narch/x86/events/intel/core.c=6009=static void intel_pmu_check_counters_mask(u64 *cntr_mask,\n--\narch/x86/events/intel/core.c-6015-\tbit = fls64(*cntr_mask);\narch/x86/events/intel/core.c:6016:\tif (bit \u003e INTEL_PMC_MAX_GENERIC) {\narch/x86/events/intel/core.c-6017-\t\tWARN(1, KERN_ERR \"hw perf events %d \u003e max(%d), clipping!\",\narch/x86/events/intel/core.c:6018:\t\t bit, INTEL_PMC_MAX_GENERIC);\narch/x86/events/intel/core.c:6019:\t\t*cntr_mask \u0026= GENMASK_ULL(INTEL_PMC_MAX_GENERIC - 1, 0);\narch/x86/events/intel/core.c-6020-\t}\n--\narch/x86/events/intel/core.c-6023-\tbit = fls64(*fixed_cntr_mask);\narch/x86/events/intel/core.c:6024:\tif (bit \u003e INTEL_PMC_MAX_FIXED) {\narch/x86/events/intel/core.c-6025-\t\tWARN(1, KERN_ERR \"hw perf events fixed %d \u003e max(%d), clipping!\",\narch/x86/events/intel/core.c:6026:\t\t bit, INTEL_PMC_MAX_FIXED);\narch/x86/events/intel/core.c:6027:\t\t*fixed_cntr_mask \u0026= GENMASK_ULL(INTEL_PMC_MAX_FIXED - 1, 0);\narch/x86/events/intel/core.c-6028-\t}\n--\narch/x86/events/intel/core.c=6135=static void intel_pmu_check_dyn_constr(struct pmu *pmu,\n--\narch/x86/events/intel/core.c-6138-{\narch/x86/events/intel/core.c:6139:\tu64 gp_mask = GENMASK_ULL(INTEL_PMC_MAX_GENERIC - 1, 0);\narch/x86/events/intel/core.c-6140-\tenum dyn_constr_type i;\n--\narch/x86/events/intel/core.c=7976=__init int intel_pmu_init(void)\n--\narch/x86/events/intel/core.c-8898-\t\t\t */\narch/x86/events/intel/core.c:8899:\t\t\tif (fls64(x86_pmu.fixed_cntr_mask64) \u003e INTEL_PMC_MAX_FIXED)\narch/x86/events/intel/core.c:8900:\t\t\t\tx86_pmu.fixed_cntr_mask64 \u0026= GENMASK_ULL(INTEL_PMC_MAX_FIXED - 1, 0);\narch/x86/events/intel/core.c-8901-\t\t\tintel_v5_gen_event_constraints[fls64(x86_pmu.fixed_cntr_mask64)].weight = -1;\n--\narch/x86/events/intel/ds.c=2374=static inline void __setup_pebs_counter_group(struct cpu_hw_events *cpuc,\n--\narch/x86/events/intel/ds.c-2380-\narch/x86/events/intel/ds.c:2381:\tfor_each_set_bit(bit, (unsigned long *)\u0026cntr-\u003ecntr, INTEL_PMC_MAX_GENERIC) {\narch/x86/events/intel/ds.c-2382-\t\tintel_perf_event_update_pmc(cpuc-\u003eevents[bit], *(u64 *)next_record);\n--\narch/x86/events/intel/ds.c-2385-\narch/x86/events/intel/ds.c:2386:\tfor_each_set_bit(bit, (unsigned long *)\u0026cntr-\u003efixed, INTEL_PMC_MAX_FIXED) {\narch/x86/events/intel/ds.c-2387-\t\t/* The slots event will be handled with perf_metric later */\n--\narch/x86/events/intel/p4.c=1368=__init int p4_pmu_init(void)\n--\narch/x86/events/intel/p4.c-1373-\t/* If we get stripped -- indexing fails */\narch/x86/events/intel/p4.c:1374:\tBUILD_BUG_ON(ARCH_P4_MAX_CCCR \u003e INTEL_PMC_MAX_GENERIC);\narch/x86/events/intel/p4.c-1375-\n--\narch/x86/include/asm/perf_event.h-10-\narch/x86/include/asm/perf_event.h:11:#define INTEL_PMC_MAX_GENERIC\t\t\t\t 32\narch/x86/include/asm/perf_event.h:12:#define INTEL_PMC_MAX_FIXED\t\t\t\t 16\narch/x86/include/asm/perf_event.h-13-#define INTEL_PMC_IDX_FIXED\t\t\t\t 32\n--\narch/x86/kvm/vmx/vmx.c=4238=static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/vmx/vmx.c-4266-\narch/x86/kvm/vmx/vmx.c:4267:\tfor_each_set_bit(i, kvm_host_pmu.cntr_mask, INTEL_PMC_MAX_GENERIC) {\narch/x86/kvm/vmx/vmx.c-4268-\t\tbool pmc_intercept = intercept || !test_bit(i, \u0026gp_mask);\n--\narch/x86/kvm/vmx/vmx.c-4275-\narch/x86/kvm/vmx/vmx.c:4276:\tfor_each_set_bit(i, kvm_host_pmu.fixed_cntr_mask, INTEL_PMC_MAX_FIXED) {\narch/x86/kvm/vmx/vmx.c-4277-\t\tbool pmc_intercept = intercept || !test_bit(i, \u0026fixed_mask);\n"
} |
| 81/3 | 2026/11/08 23:34 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46934 output: 23 thoughts: 641 Thoughts: **My Analysis of Performance Monitor Unit (PMU) Configuration** Okay, so I'm looking at how we're setting up the PMU counters. It's good to keep in mind the underlying hardware limitations. `INTEL_PMC_MAX_GENERIC` is 32 and `INTEL_PMC_MAX_FIXED` is 16. Those are definitely within the 64-bit bounds. That's a sanity check, but it's important. Now, let's dive into what `intel_pmu_refresh` is doing. It's handling the details of discovering and configuring counters. I see it's dealing with the CPUID information, specifically `CPUID.0AH`, which tells us what the hardware supports. There's some clamping going on, that makes sense. It looks like we're determining the number of fixed counters (`nr_fixed_counters`) based on `edx.split.num_counters_fixed` from the CPUID and then taking the minimum with what KVM is advertising, represented by `kvm_pmu_cap.fixed_cntr_mask`. The `find_first_zero_bit` function is a clever way to figure out the number of contiguous counters that KVM supports. Then, we construct a `fixed_mask`, accounting for potential version-specific capabilities using the v5 ECX bitmap. Similar logic is applied to the general-purpose counters with `nr_gp_counters` and `kvm_pmu_cap.cntr_mask`. We calculate `pmu->pmc_exists64` using `BIT_ULL` to represent which counters exist. Then, there's a version check and if the PMU version is 1, the function returns. Otherwise, we combine the fixed and general-purpose counter masks using `pmu->pmc_exists64 |= fixed_mask << INTEL_PMC_IDX_FIXED`. Makes sense. But wait a minute... what if `nr_gp_counters` happens to be 64? And this calculation works on 64 bit integers. That looks like we could support 64 general-purpose counters, but... let's check what `KVM_MAX_NR_GP_COUNTERS` is actually defined as. I know from experience it *shouldn't* be 64... Okay, let's look at `kvm_host.h`. Ah, here it is: `#define KVM_MAX_NR_GP_COUNTERS 8` and `#define KVM_MAX_NR_FIXED_COUNTERS 8`. We're definitely clamping, but I need to make sure this is what is actually implemented here. Let's verify that quickly, since that's a *major* potential bug. |
| 82/3 | 2026/11/08 23:34 | tool | grepper |
0mArgs:{
"Expression": "#define KVM_MAX_NR_"
}Results: {
"Output": "arch/x86/include/asm/kvm_host.h=530=struct kvm_pmc {\n--\narch/x86/include/asm/kvm_host.h-565-#define KVM_MAX(a, b)\t((a) \u003e= (b) ? (a) : (b))\narch/x86/include/asm/kvm_host.h:566:#define KVM_MAX_NR_INTEL_GP_COUNTERS\t8\narch/x86/include/asm/kvm_host.h:567:#define KVM_MAX_NR_AMD_GP_COUNTERS\t6\narch/x86/include/asm/kvm_host.h:568:#define KVM_MAX_NR_GP_COUNTERS\t\tKVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \\\narch/x86/include/asm/kvm_host.h-569-\t\t\t\t\t\tKVM_MAX_NR_AMD_GP_COUNTERS)\narch/x86/include/asm/kvm_host.h-570-\narch/x86/include/asm/kvm_host.h:571:#define KVM_MAX_NR_INTEL_FIXED_COUNTERS\t3\narch/x86/include/asm/kvm_host.h:572:#define KVM_MAX_NR_AMD_FIXED_COUNTERS\t0\narch/x86/include/asm/kvm_host.h:573:#define KVM_MAX_NR_FIXED_COUNTERS\tKVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \\\narch/x86/include/asm/kvm_host.h-574-\t\t\t\t\t\tKVM_MAX_NR_AMD_FIXED_COUNTERS)\n--\narch/x86/kvm/msrs.c=31=struct kvm_x86_msr_filter {\n--\narch/x86/kvm/msrs.c-41- */\narch/x86/kvm/msrs.c:42:#define KVM_MAX_NR_USER_RETURN_MSRS 16\narch/x86/kvm/msrs.c-43-\n--\ninclude/linux/kvm_host.h-88-#ifndef KVM_MAX_NR_ADDRESS_SPACES\ninclude/linux/kvm_host.h:89:#define KVM_MAX_NR_ADDRESS_SPACES\t1\ninclude/linux/kvm_host.h-90-#endif\n"
} |
| 83/3 | 2026/11/08 23:34 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48842 output: 23 thoughts: 4106 Thoughts: **KMSAN Audit of KVM Performance Monitor Code** Alright, let's break this down systematically. My primary concern here is always uninitialized memory reads that could expose sensitive data or, worse, lead to security vulnerabilities. Given the nature of the code dealing with performance counters, a KMSAN audit is vital. First, I examined the constants, `KVM_MAX_NR_GP_COUNTERS`, `KVM_MAX_NR_FIXED_COUNTERS`, and the bitwise operations based on those. The goal is always to see if calculations based on these values can lead to out-of-bounds array access. So far, the bit manipulations look sound. The code properly uses bitmasks and ensures that counter indices used in array indexing are within the declared bounds. The checks with `nr_gp_counters >= 64` are also safe, due to the bitfield nature of `num_core_pmc` in the relevant CPUID data. The use of `kvm_pmu_cap.cntr_mask64` seems correct in limiting the number of counters AMD can use, and it is safe, so no issue there. Next, I dove into `amd_pmu_refresh`, `intel_rdpmc_ecx_to_pmc`, `get_gp_pmc`, `get_fixed_pmc`, `amd_pmu_get_pmc`, `reprogram_fixed_counters`, and `intel_mediated_pmu_load`. I focused on where counter indices are derived and used. Specifically, the use of `kvm_is_gp_pmc_supported`, `kvm_is_fixed_pmc_supported`, and the ubiquitous `array_index_nospec` are critical. These functions are designed to prevent out-of-bounds access. The `array_index_nospec` is used appropriately after the support is checked, ensuring the code cannot access an array out-of-bounds. Now, for potential KMSAN issues, I went through the four KMSAN criteria. 1. **Kernel Structures to Userspace:** I scrutinized the data transferred to userspace, specifically through CPUID entries (e.g., `KVM_GET_SUPPORTED_CPUID`). The code clearly initializes every field of `entry` ( `eax`, `ebx`, `ecx`, `edx` ) before returning them. For MSRs (e.g., `KVM_GET_MSRS`), the `msr_info->data` is populated properly. In fact, all data copied back to userspace via MSRs are properly initialized or have no sensitive data. 2. **Conditional Logic/Branching:** I've carefully reviewed where variables and structure fields are used in conditional logic. `kvm_pmu_cap` is initialized at boot, and `pmu` structs are zero-initialized with `memset` during allocation, which is good practice. I confirmed that newly introduced struct fields ( `eventsel_rsvd`, and `pmc_exists64` from the union ) are also explicitly initialized. `pmc_exists` is initialized to zero, and `eventsel_rsvd` are initialized with appropriate values. The data dependencies and initialization of the `kvm_pmu` struct are fine. 3. **Complex Data Structure Initialization:** There are no new complex data structures allocated where uninitialized fields could be a problem. 4. **Bounds Checks:** The use of `array_index_nospec` and the explicit checks before using those indices were all correct. The shift in bound checks looks stronger, with no way to allow access to uninitialized bytes of existing buffers. Overall, I'm confident that the current code does not have any obvious memory safety problems relating to uninitialized memory. I'll need to stay vigilant for future changes, but based on the review so far, it appears the developers were very thoughtful about memory safety and potential KMSAN violations. |
| 84/3 | 2026/11/08 23:34 | tool | grepper |
0mArgs:{
"Expression": "kvm_init_pmu_capability"
}Results: {
"Output": "arch/x86/kvm/pmu.c=117=void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)\n--\narch/x86/kvm/pmu.c-132-\narch/x86/kvm/pmu.c:133:void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\narch/x86/kvm/pmu.c-134-{\n--\narch/x86/kvm/pmu.h=256=extern struct x86_pmu_capability kvm_pmu_cap;\narch/x86/kvm/pmu.h-257-\narch/x86/kvm/pmu.h:258:void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);\narch/x86/kvm/pmu.h-259-\n--\narch/x86/kvm/x86.c=7061=int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)\n--\narch/x86/kvm/x86.c-7142-\narch/x86/kvm/x86.c:7143:\tkvm_init_pmu_capability(ops-\u003epmu_ops);\narch/x86/kvm/x86.c-7144-\n"
} |
| 85/3 | 2026/11/08 23:34 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 54214 output: 623 thoughts: 1285 Thoughts: **KMSAN Determination for KVM vPMU Patch** Okay, let's break this down. The `kvm_init_pmu_capability` function is called during `kvm_x86_vendor_init`. Before this, `kvm_host_pmu` and `kvm_pmu_cap` exist as BSS globals. This means they start as zero-initialized, and are fully populated later. I need to rigorously check any MSR accesses and CPUID handling within this context. Specifically, I need to focus on `MSR_CORE_PERF_GLOBAL_STATUS_SET` within `kvm_pmu_set_msr`. The code here explicitly checks for the architecturally reserved `GLOBAL_STATUS_COND_CHG` bit and returns an error (1) if it's set, unless the write is initiated by the host. Otherwise it updates `pmu->global_status`, which is a `u64`. I want to make sure this is sound. Now, what about the read-only `MSR_CORE_PERF_GLOBAL_INUSE` MSR? It's handled by `intel_pmu_get_msr`, returning a value from `intel_pmu_get_global_inuse(vcpu)`. The access is gated by `intel_is_valid_msr`, which checks `pmu->version >= 4`. Any write attempts will correctly generate a #GP. I reviewed the `Documentation/virt/kvm/x86/errata.rst` to see what I might be missing. The documentation mentions the pass-through of writes to `IA32_PERF_GLOBAL_STATUS_RESET`. This is just documentation; the hardware pass-through behavior is already implemented, read as zero and write to clear. No host information can be leaked. I briefly glanced at the test files in `tools/testing/selftests`, but realized those are userspace tests and not relevant here. The key here is understanding the distinction between KASAN and KMSAN. Standard KASAN builds catch out-of-bounds accesses, use-after-free, double frees, invalid frees, locking issues, and various memory corruption problems. KMSAN, on the other hand, detects reads of uninitialized memory and potential kernel-to-user memory info-leaks. The rule is: *if* KASAN, LOCKDEP, or other standard bug detectors can catch a bug, *don't* use KMSAN, as it's a significant resource overhead. I need to be sure that the changes in the patch introduce *only* potential issues detectable by KMSAN and not KASAN. Looking at the code changes, it appears to be entirely focused on the KVM vPMU logic. This involves replacing counter counts with bitmaps (`cntr_mask64`, `fixed_cntr_mask64`, `pmc_exists`), supporting architectural PerfMon v5 fixed counter bitmaps, emulating MSRs, and refactoring MSR intercepts for mediated PMU on VMX and SVM. Critically, all modified or added structures – `kvm_pmu`, `x86_pmu_capability`, CPUID unions, and local variables – are either zero-initialized or explicitly initialized *before* use. Any MSR or CPUID values returned to userspace are fully computed and initialized. There's no introduction of uninitialized memory usage, info-leaks, or uninitialized fields being copied to userspace. The risks here relate to bounds checking within the bitmap operations, the intercept logic (which can cause out-of-bounds array access), and register validation (which might lead to incorrect CPUID flags or unexpected guest #GP). All of these bugs will be caught by KASAN or standard testing/bug detectors. Therefore, `NeedsKMSAN = false`. I am confident that these changes do not introduce risks specifically addressed by KMSAN and thus would not be a productive use of resources. I'll now prepare the reasoning, contrasting KASAN and KMSAN as described above, for the `set-results` tool and mark `NeedsKMSAN: false`. |
| 86/3 | 2026/11/08 23:35 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series refactors KVM's virtual Performance Monitoring Unit (vPMU) counter tracking to use bitmasks rather than scalar counter counts, and adds support for Intel PerfMon v5 fixed-counter enumeration (via CPUID 0AH ECX) and MSRs (MSR_CORE_PERF_GLOBAL_STATUS_SET, MSR_CORE_PERF_GLOBAL_INUSE).\n\nAnalysis regarding uninitialized memory:\n1. No uninitialized data leaks to userspace or guests:\n - In `__do_cpuid_func()`, the local unions (`cpuid10_eax`, `cpuid10_edx`, `cpuid_0x80000022_ebx`) are explicitly zero-initialized (`= { }`), and all entry register fields (`eax`, `ebx`, `ecx`, `edx`) are fully assigned before returning.\n - For newly handled MSRs, `MSR_CORE_PERF_GLOBAL_STATUS_SET` explicitly sets `msr_info-\u003edata = 0;` and `MSR_CORE_PERF_GLOBAL_INUSE` computes `data` starting from `0` in `intel_pmu_get_global_inuse()`, with all local variables initialized.\n2. Structure initialization and sizing:\n - `struct kvm_pmu` continues to be zeroed via `memset(pmu, 0, sizeof(*pmu))` in `kvm_pmu_init()`. The new union replacing `all_valid_pmc_idx` with `pmc_exists` / `pmc_exists64` maintains identical size (`X86_PMC_IDX_MAX` bits = 64 bits = `sizeof(u64)`), which is verified at compile-time via `BUILD_BUG_ON`.\n - `kvm_host_pmu` and `kvm_pmu_cap` are static global structures in BSS that are zeroed by default and completely populated during PMU initialization (`kvm_init_pmu_capability()`).\n3. Control flow and indexing:\n - Array accesses into `gp_counters` and `fixed_counters` continue to be bounded by static limits (`KVM_MAX_NR_GP_COUNTERS` and `KVM_MAX_NR_FIXED_COUNTERS`) sanitized by `array_index_nospec()`. Potential bugs would be index out-of-bounds or invalid MSR access bugs, which fall under the scope of KASAN rather than KMSAN.\n\nBecause the changes do not introduce or expose uninitialized memory reads or information leak risks, a dedicated KMSAN fuzzing session is not warranted. Standard KASAN builds are sufficient.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series refactors KVM's virtual Performance Monitoring Unit (vPMU) counter tracking to use bitmasks rather than scalar counter counts, and adds support for Intel PerfMon v5 fixed-counter enumeration (via CPUID 0AH ECX) and MSRs (MSR_CORE_PERF_GLOBAL_STATUS_SET, MSR_CORE_PERF_GLOBAL_INUSE).\n\nAnalysis regarding uninitialized memory:\n1. No uninitialized data leaks to userspace or guests:\n - In `__do_cpuid_func()`, the local unions (`cpuid10_eax`, `cpuid10_edx`, `cpuid_0x80000022_ebx`) are explicitly zero-initialized (`= { }`), and all entry register fields (`eax`, `ebx`, `ecx`, `edx`) are fully assigned before returning.\n - For newly handled MSRs, `MSR_CORE_PERF_GLOBAL_STATUS_SET` explicitly sets `msr_info-\u003edata = 0;` and `MSR_CORE_PERF_GLOBAL_INUSE` computes `data` starting from `0` in `intel_pmu_get_global_inuse()`, with all local variables initialized.\n2. Structure initialization and sizing:\n - `struct kvm_pmu` continues to be zeroed via `memset(pmu, 0, sizeof(*pmu))` in `kvm_pmu_init()`. The new union replacing `all_valid_pmc_idx` with `pmc_exists` / `pmc_exists64` maintains identical size (`X86_PMC_IDX_MAX` bits = 64 bits = `sizeof(u64)`), which is verified at compile-time via `BUILD_BUG_ON`.\n - `kvm_host_pmu` and `kvm_pmu_cap` are static global structures in BSS that are zeroed by default and completely populated during PMU initialization (`kvm_init_pmu_capability()`).\n3. Control flow and indexing:\n - Array accesses into `gp_counters` and `fixed_counters` continue to be bounded by static limits (`KVM_MAX_NR_GP_COUNTERS` and `KVM_MAX_NR_FIXED_COUNTERS`) sanitized by `array_index_nospec()`. Potential bugs would be index out-of-bounds or invalid MSR access bugs, which fall under the scope of KASAN rather than KMSAN.\n\nBecause the changes do not introduce or expose uninitialized memory reads or information leak risks, a dedicated KMSAN fuzzing session is not warranted. Standard KASAN builds are sufficient.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|