| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/10 10:39 | flow | patch-triage |
3mArgs:null Results: {
"EnableConfigs": [
"KVM",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_get_msr",
"kvm_pmu_set_msr",
"kvm_pmu_rdpmc",
"kvm_pmu_refresh",
"kvm_mediated_pmu_load",
"kvm_mediated_pmu_put",
"enter_svm_guest_mode",
"nested_svm_vmexit",
"svm_leave_nested"
],
"KMSANReasoning": "The patch introduces PMC (Performance Monitoring Counter) virtualization support for AMD SVM in KVM. It modifies the `vmcb_save_area` structure to include a `pmc` array and global control/status fields, which replaces previously reserved bytes. The `vmcb_save_area` is copied to userspace via the `KVM_GET_NESTED_STATE` ioctl. However, the underlying VMCB pages (e.g., `vmcb01` and `vmcb02`) are allocated using `snp_safe_alloc_page()`, which explicitly uses `__GFP_ZERO` to zero-initialize the entire page. Furthermore, `struct vmcb_save_area` is marked as `__packed`, preventing any implicit padding bytes. The patch also adds logic to read and write these fields, but since the memory is zero-allocated and explicitly managed, there is no risk of uninitialized memory being leaked to userspace or used in conditional branches. Standard KASAN and other bug detectors are sufficient to catch any potential memory corruption or out-of-bounds access bugs introduced by these changes.",
"NeedsKMSAN": false,
"Reasoning": "The patch introduces hardware-switched PMU state for AMD SVM guests, adding support for PMC virtualization. This involves significant functional changes to KVM's PMU handling, MSR virtualization, and nested virtualization transitions. These changes are reachable from guest execution and should be heavily fuzzed.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/10 10:39 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit b1b55dd3c5a9465f0f9a50de4b59854f8eb681d2\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Mon Aug 10 10:39:40 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h\nindex 30bb8fdf2a8b5..b551ba381b6e2 100644\n--- a/arch/x86/include/asm/cpufeatures.h\n+++ b/arch/x86/include/asm/cpufeatures.h\n@@ -384,6 +384,7 @@\n #define X86_FEATURE_VMCBCLEAN\t\t(15*32+ 5) /* \"vmcb_clean\" VMCB clean bits support */\n #define X86_FEATURE_FLUSHBYASID\t\t(15*32+ 6) /* \"flushbyasid\" Flush-by-ASID support */\n #define X86_FEATURE_DECODEASSISTS\t(15*32+ 7) /* \"decodeassists\" Decode Assists support */\n+#define X86_FEATURE_PERFCTR_VIRT\t(15*32+ 8) /* PMC virtualization support */\n #define X86_FEATURE_PAUSEFILTER\t\t(15*32+10) /* \"pausefilter\" Filtered pause intercept */\n #define X86_FEATURE_PFTHRESHOLD\t\t(15*32+12) /* \"pfthreshold\" Pause filter threshold */\n #define X86_FEATURE_AVIC\t\t(15*32+13) /* \"avic\" Virtual Interrupt Controller */\ndiff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h\nindex 4a223c2793e3f..4a617162d4c08 100644\n--- a/arch/x86/include/asm/kvm-x86-pmu-ops.h\n+++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h\n@@ -29,6 +29,9 @@ KVM_X86_PMU_OP_OPTIONAL_RET0(pmc_is_disabled_in_current_mode)\n KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl)\n KVM_X86_PMU_OP(mediated_load)\n KVM_X86_PMU_OP(mediated_put)\n+\n+KVM_X86_PMU_OP_OPTIONAL_RET0(get_vendor_state)\n+KVM_X86_PMU_OP_OPTIONAL_RET0(set_vendor_state)\n #endif\n \n #undef KVM_X86_PMU_OP\ndiff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h\nindex 283847619ff88..338df11e2a4df 100644\n--- a/arch/x86/include/asm/kvm_host.h\n+++ b/arch/x86/include/asm/kvm_host.h\n@@ -715,6 +715,16 @@ enum kvm_only_cpuid_leafs {\n \tNKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,\n };\n \n+/*\n+ * Track how the guest PMU state is saved and restored, as this can be done\n+ * either by software or, if capable, by hardware. When hardware manages the\n+ * guest PMU state, the state resides in a vendor save area.\n+ */\n+#define KVM_MEDIATED_PMU_CAP_HW_SWITCHED\tBIT(0)\n+#define KVM_MEDIATED_PMU_CAP_HW_FILTERED\tBIT(1)\n+#define KVM_MEDIATED_PMU_CAP_VALID\t\t(KVM_MEDIATED_PMU_CAP_HW_SWITCHED\t| \\\n+\t\t\t\t\t\t KVM_MEDIATED_PMU_CAP_HW_FILTERED)\n+\n struct kvm_vcpu_arch {\n \t/*\n \t * rip and regs accesses must go through\n@@ -1284,6 +1294,7 @@ struct kvm_arch {\n \tbool bus_lock_detection_enabled;\n \tbool enable_pmu;\n \tbool created_mediated_pmu;\n+\tu32 mediated_pmu_caps;\n \n \tu32 notify_window;\n \tu32 notify_vmexit_flags;\ndiff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h\nindex aa63431ba92c3..e1c8a333e86fa 100644\n--- a/arch/x86/include/asm/svm.h\n+++ b/arch/x86/include/asm/svm.h\n@@ -247,6 +247,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {\n \n #define SVM_MISC2_ENABLE_V_LBR\tBIT_ULL(0)\n #define SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE\tBIT_ULL(1)\n+#define SVM_MISC2_ENABLE_V_PMC\tBIT_ULL(3)\n \n #define SVM_TSC_RATIO_RSVD\t0xffffff0000000000ULL\n #define SVM_TSC_RATIO_MIN\t0x0000000000000001ULL\n@@ -338,7 +339,12 @@ struct vmcb_save_area {\n \tu8 cpl;\n \tu8 reserved_0xcc[4];\n \tu64 efer;\n-\tu8 reserved_0xd8[112];\n+\tu8 reserved_0xd8[8];\n+\tstruct {\n+\t\tu64 perf_ctl;\n+\t\tu64 perf_ctr;\n+\t} __packed pmc[6];\n+\tu8 reserved_0x140[8];\n \tu64 cr4;\n \tu64 cr3;\n \tu64 cr0;\n@@ -346,7 +352,9 @@ struct vmcb_save_area {\n \tu64 dr6;\n \tu64 rflags;\n \tu64 rip;\n-\tu8 reserved_0x180[88];\n+\tu8 reserved_0x180[72];\n+\tu64 perf_cntr_global_status;\n+\tu64 perf_cntr_global_control;\n \tu64 rsp;\n \tu64 s_cet;\n \tu64 ssp;\n@@ -575,6 +583,7 @@ static inline void __unused_size_checks(void)\n \tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xa0);\n \tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xcc);\n \tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xd8);\n+\tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x140);\n \tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x180);\n \tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x248);\n \tBUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x298);\ndiff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c\nindex ddb022cb203a2..ce4f984bc6e16 100644\n--- a/arch/x86/kvm/cpuid.c\n+++ b/arch/x86/kvm/cpuid.c\n@@ -1217,6 +1217,7 @@ void kvm_initialize_cpu_caps(void)\n \t\tVENDOR_F(VGIF),\n \t\tVENDOR_F(VNMI),\n \t\tVENDOR_F(SVME_ADDR_CHK),\n+\t\tVENDOR_F(PERFCTR_VIRT),\n \t);\n \n \tkvm_cpu_cap_init(CPUID_8000_001F_EAX,\ndiff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c\nindex a7d60c8785cd4..4c918aa6d6f37 100644\n--- a/arch/x86/kvm/pmu.c\n+++ b/arch/x86/kvm/pmu.c\n@@ -130,6 +130,24 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)\n #undef __KVM_X86_PMU_OP\n }\n \n+static void kvm_pmu_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)\n+{\n+\tif (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\treturn;\n+\n+\tif (kvm_pmu_call(get_vendor_state)(vcpu, msr))\n+\t\tkvm_pmu_warn_vendor_state(msr);\n+}\n+\n+static void kvm_pmu_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)\n+{\n+\tif (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\treturn;\n+\n+\tif (kvm_pmu_call(set_vendor_state)(vcpu, msr))\n+\t\tkvm_pmu_warn_vendor_state(msr);\n+}\n+\n void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n {\n \tbool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;\n@@ -166,8 +184,11 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)\n \t !pmu_ops-\u003eis_mediated_pmu_supported(\u0026kvm_host_pmu))\n \t\tenable_mediated_pmu = false;\n \n-\tif (!enable_mediated_pmu)\n+\tif (!enable_mediated_pmu) {\n \t\tpmu_ops-\u003ewrite_global_ctrl = NULL;\n+\t\tpmu_ops-\u003eget_vendor_state = NULL;\n+\t\tpmu_ops-\u003eset_vendor_state = NULL;\n+\t}\n \n \tif (!enable_pmu) {\n \t\tmemset(\u0026kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));\n@@ -197,6 +218,27 @@ void kvm_handle_guest_mediated_pmi(void)\n \tkvm_make_request(KVM_REQ_PMI, vcpu);\n }\n \n+static __always_inline u32 fixed_counter_msr(u32 idx)\n+{\n+\treturn kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;\n+}\n+\n+static __always_inline u32 gp_counter_msr(u32 idx)\n+{\n+\treturn kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;\n+}\n+\n+static __always_inline u32 gp_eventsel_msr(u32 idx)\n+{\n+\treturn kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;\n+}\n+\n+static __always_inline u32 pmc_counter_msr(struct kvm_pmc *pmc)\n+{\n+\treturn pmc_is_gp(pmc) ? gp_counter_msr(pmc-\u003eidx) :\n+\t\t\t\tfixed_counter_msr(pmc-\u003eidx - KVM_FIXED_PMC_BASE_IDX);\n+}\n+\n static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)\n {\n \tstruct kvm_pmu *pmu = pmc_to_pmu(pmc);\n@@ -550,18 +592,23 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)\n {\n \tbool allowed = pmc_is_locally_enabled(pmc) \u0026\u0026 pmc_is_event_allowed(pmc);\n \tstruct kvm_pmu *pmu = pmc_to_pmu(pmc);\n+\tstruct kvm_vcpu *vcpu = pmc-\u003evcpu;\n \n \tif (pmc_is_gp(pmc)) {\n \t\tpmc-\u003eeventsel_hw \u0026= ~ARCH_PERFMON_EVENTSEL_ENABLE;\n \t\tif (allowed)\n \t\t\tpmc-\u003eeventsel_hw |= pmc-\u003eeventsel \u0026\n \t\t\t\t\t ARCH_PERFMON_EVENTSEL_ENABLE;\n+\n+\t\tkvm_pmu_set_vendor_state(vcpu, gp_eventsel_msr(pmc-\u003eidx));\n \t} else {\n \t\tu64 mask = intel_fixed_bits_by_idx(pmc-\u003eidx - KVM_FIXED_PMC_BASE_IDX, 0xf);\n \n \t\tpmu-\u003efixed_ctr_ctrl_hw \u0026= ~mask;\n \t\tif (allowed)\n \t\t\tpmu-\u003efixed_ctr_ctrl_hw |= pmu-\u003efixed_ctr_ctrl \u0026 mask;\n+\n+\t\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.FIXED_COUNTER_CTRL);\n \t}\n }\n \n@@ -769,6 +816,8 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\n \t kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))\n \t\treturn 1;\n \n+\tkvm_pmu_get_vendor_state(vcpu, pmc_counter_msr(pmc));\n+\n \t*data = pmc_read_counter(pmc) \u0026 mask;\n \treturn 0;\n }\n@@ -852,10 +901,12 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \tswitch (msr) {\n \tcase MSR_CORE_PERF_GLOBAL_STATUS:\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:\n+\t\tkvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n \t\tmsr_info-\u003edata = pmu-\u003eglobal_status;\n \t\tbreak;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_CTL:\n \tcase MSR_CORE_PERF_GLOBAL_CTRL:\n+\t\tkvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);\n \t\tmsr_info-\u003edata = pmu-\u003eglobal_ctrl;\n \t\tbreak;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:\n@@ -895,6 +946,7 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\t\treturn 1;\n \n \t\tpmu-\u003eglobal_status = data;\n+\t\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n \t\tbreak;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_CTL:\n \t\tdata \u0026= ~pmu-\u003eglobal_ctrl_rsvd;\n@@ -914,6 +966,8 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\t */\n \t\tif (kvm_vcpu_has_mediated_pmu(vcpu))\n \t\t\tkvm_pmu_call(write_global_ctrl)(data);\n+\n+\t\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);\n \t\tbreak;\n \tcase MSR_CORE_PERF_GLOBAL_OVF_CTRL:\n \t\t/*\n@@ -924,12 +978,18 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\t\treturn 1;\n \t\tfallthrough;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:\n-\t\tif (!msr_info-\u003ehost_initiated)\n+\t\tif (!msr_info-\u003ehost_initiated) {\n+\t\t\tkvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n \t\t\tpmu-\u003eglobal_status \u0026= ~data;\n+\t\t\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n+\t\t}\n \t\tbreak;\n \tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:\n-\t\tif (!msr_info-\u003ehost_initiated)\n+\t\tif (!msr_info-\u003ehost_initiated) {\n+\t\t\tkvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n \t\t\tpmu-\u003eglobal_status |= data \u0026 ~pmu-\u003eglobal_status_rsvd;\n+\t\t\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n+\t\t}\n \t\tbreak;\n \tdefault:\n \t\tkvm_pmu_mark_pmc_in_use(vcpu, msr_info-\u003eindex);\n@@ -1017,6 +1077,8 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)\n \tif (kvm_vcpu_has_mediated_pmu(vcpu))\n \t\tkvm_pmu_call(write_global_ctrl)(pmu-\u003eglobal_ctrl);\n \n+\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_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@@ -1139,6 +1201,9 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,\n \tif (bitmap_empty(event_pmcs, X86_PMC_IDX_MAX))\n \t\treturn;\n \n+\tkvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);\n+\tkvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n+\n \tif (!kvm_pmu_has_perf_global_ctrl(pmu))\n \t\tbitmap_copy(bitmap, event_pmcs, X86_PMC_IDX_MAX);\n \telse if (!bitmap_and(bitmap, event_pmcs,\n@@ -1147,11 +1212,17 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,\n \n \tidx = srcu_read_lock(\u0026vcpu-\u003ekvm-\u003esrcu);\n \tkvm_for_each_pmc(pmu, pmc, i, bitmap) {\n+\t\tkvm_pmu_get_vendor_state(vcpu, pmc_counter_msr(pmc));\n+\n \t\tif (!pmc_is_event_allowed(pmc) || !cpl_is_matched(pmc))\n \t\t\tcontinue;\n \n \t\tkvm_pmu_incr_counter(pmc);\n+\t\tkvm_pmu_set_vendor_state(vcpu, pmc_counter_msr(pmc));\n \t}\n+\n+\tkvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);\n+\n \tsrcu_read_unlock(\u0026vcpu-\u003ekvm-\u003esrcu, idx);\n }\n \n@@ -1310,21 +1381,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)\n \treturn r;\n }\n \n-static __always_inline u32 fixed_counter_msr(u32 idx)\n-{\n-\treturn kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;\n-}\n-\n-static __always_inline u32 gp_counter_msr(u32 idx)\n-{\n-\treturn kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;\n-}\n-\n-static __always_inline u32 gp_eventsel_msr(u32 idx)\n-{\n-\treturn kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;\n-}\n-\n static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n@@ -1361,6 +1417,10 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)\n \n \tperf_load_guest_context();\n \n+\t/* Guest PMU state is restored by hardware */\n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\treturn;\n+\n \t/*\n \t * Explicitly clear PERF_GLOBAL_CTRL, as \"loading\" the guest's context\n \t * disables all individual counters (if any were enabled), but doesn't\n@@ -1420,6 +1480,12 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)\n \n \tlockdep_assert_irqs_disabled();\n \n+\t/* Guest PMU state is saved by hardware */\n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) {\n+\t\tperf_put_guest_context();\n+\t\treturn;\n+\t}\n+\n \t/*\n \t * Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's\n \t * atomically cleared on VM-Exit, i.e. doesn't need to be clear here.\ndiff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h\nindex 090c9bbb74f48..42d33072ece90 100644\n--- a/arch/x86/kvm/pmu.h\n+++ b/arch/x86/kvm/pmu.h\n@@ -42,14 +42,18 @@ struct kvm_pmu_ops {\n \tvoid (*mediated_load)(struct kvm_vcpu *vcpu);\n \tvoid (*mediated_put)(struct kvm_vcpu *vcpu);\n \tvoid (*write_global_ctrl)(u64 global_ctrl);\n+\tint (*get_vendor_state)(struct kvm_vcpu *vcpu, u32 msr);\n+\tint (*set_vendor_state)(struct kvm_vcpu *vcpu, u32 msr);\n \n \tconst u64 EVENTSEL_EVENT;\n \tconst int MAX_NR_GP_COUNTERS;\n \tconst int MIN_NR_GP_COUNTERS;\n \n \tconst u32 PERF_GLOBAL_CTRL;\n+\tconst u32 PERF_GLOBAL_STATUS;\n \tconst u32 GP_EVENTSEL_BASE;\n \tconst u32 GP_COUNTER_BASE;\n+\tconst u32 FIXED_COUNTER_CTRL;\n \tconst u32 FIXED_COUNTER_BASE;\n \tconst u32 MSR_STRIDE;\n };\n@@ -88,6 +92,31 @@ 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 bool kvm_vcpu_has_mediated_pmu_caps(struct kvm_vcpu *vcpu, u32 caps)\n+{\n+\treturn kvm_vcpu_has_mediated_pmu(vcpu) \u0026\u0026\n+\t !!(vcpu-\u003ekvm-\u003earch.mediated_pmu_caps \u0026 caps);\n+}\n+\n+#define kvm_set_mediated_pmu_caps(kvm, caps)\t\t\t\t\\\n+do {\t\t\t\t\t\t\t\t\t\\\n+\tBUILD_BUG_ON(!__builtin_constant_p(caps) ||\t\t\t\\\n+\t\t ((caps) \u0026 ~KVM_MEDIATED_PMU_CAP_VALID));\t\t\\\n+\t(kvm)-\u003earch.mediated_pmu_caps |= (caps);\t\t\t\\\n+} while (0)\n+\n+#define kvm_clr_mediated_pmu_caps(kvm, caps)\t\t\t\t\\\n+do {\t\t\t\t\t\t\t\t\t\\\n+\tBUILD_BUG_ON(!__builtin_constant_p(caps) ||\t\t\t\\\n+\t\t ((caps) \u0026 ~KVM_MEDIATED_PMU_CAP_VALID));\t\t\\\n+\t(kvm)-\u003earch.mediated_pmu_caps \u0026= ~(caps);\t\t\t\\\n+} while (0)\n+\n+static inline void kvm_pmu_warn_vendor_state(u32 msr)\n+{\n+\tWARN_ONCE(1, \"accessing unsupported vendor save slot for MSR 0x%x\\n\", msr);\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\ndiff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c\nindex 73f37b050d0a0..a9f9caeb78406 100644\n--- a/arch/x86/kvm/svm/nested.c\n+++ b/arch/x86/kvm/svm/nested.c\n@@ -605,6 +605,7 @@ static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,\n \tto-\u003eg_pat = from-\u003eg_pat;\n \n \tsvm_copy_lbrs(to, from);\n+\tsvm_copy_pmcs(to, from);\n }\n \n void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,\n@@ -741,6 +742,17 @@ static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vcpu)\n \t\t(to_svm(vcpu)-\u003enested.ctl.misc_ctl2 \u0026 SVM_MISC2_ENABLE_V_LBR);\n }\n \n+static bool nested_vmcb12_has_vpmc(struct kvm_vcpu *vcpu)\n+{\n+\t/*\n+\t * Since nested AVIC is not supported, L2 PMIs can only be delivered\n+\t * via VNMI, so make it a hard requirement.\n+\t */\n+\treturn guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_VIRT) \u0026\u0026\n+\t\tnested_vnmi_enabled(to_svm(vcpu)) \u0026\u0026\n+\t\t(to_svm(vcpu)-\u003enested.ctl.misc_ctl2 \u0026 SVM_MISC2_ENABLE_V_PMC);\n+}\n+\n static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)\n {\n \tstruct vmcb_ctrl_area_cached *control = \u0026svm-\u003enested.ctl;\n@@ -823,6 +835,11 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)\n \t}\n \tvmcb_mark_dirty(vmcb02, VMCB_LBR);\n \tsvm_update_lbrv(\u0026svm-\u003evcpu);\n+\n+\tif (nested_vmcb12_has_vpmc(vcpu))\n+\t\tsvm_copy_pmcs(\u0026vmcb02-\u003esave, save);\n+\telse if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\tsvm_copy_pmcs(\u0026vmcb02-\u003esave, \u0026vmcb01-\u003esave);\n }\n \n static inline bool is_evtinj_soft(u32 evtinj)\n@@ -863,7 +880,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)\n \n \t/* Enter Guest-Mode */\n \tenter_guest_mode(vcpu);\n-\tsvm_pmu_handle_nested_transition(svm);\n \n \t/*\n \t * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,\n@@ -986,6 +1002,10 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)\n \n \t/* SVM_MISC2_ENABLE_V_LBR is controlled by svm_update_lbrv() */\n \n+\t/* L0 uses hardware-switched mode for both vmcb01 and vmcb02 */\n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\tvmcb02-\u003econtrol.misc_ctl2 |= SVM_MISC2_ENABLE_V_PMC;\n+\n \tif (!nested_vmcb_needs_vls_intercept(svm))\n \t\tvmcb02-\u003econtrol.misc_ctl2 |= SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE;\n \n@@ -1065,6 +1085,9 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)\n \tnested_vmcb02_prepare_control(svm);\n \tnested_vmcb02_prepare_save(svm);\n \n+\tif (!nested_vmcb12_has_vpmc(vcpu))\n+\t\tsvm_pmu_handle_nested_transition(svm);\n+\n \tret = nested_svm_load_cr3(\u0026svm-\u003evcpu, svm-\u003enested.save.cr3,\n \t\t\t\t nested_npt_enabled(svm), from_vmrun);\n \tif (ret)\n@@ -1228,6 +1251,9 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_save,\n \t\tsvm_copy_lbrs(to_save, from_save);\n \t\tto_save-\u003edbgctl \u0026= ~DEBUGCTL_RESERVED_BITS;\n \t}\n+\n+\tif (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_VIRT))\n+\t\tsvm_copy_pmcs(to_save, from_save);\n }\n \n void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)\n@@ -1300,6 +1326,9 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)\n \tif (nested_vmcb12_has_lbrv(vcpu))\n \t\tsvm_copy_lbrs(\u0026vmcb12-\u003esave, \u0026vmcb02-\u003esave);\n \n+\tif (nested_vmcb12_has_vpmc(vcpu))\n+\t\tsvm_copy_pmcs(\u0026vmcb12-\u003esave, \u0026vmcb02-\u003esave);\n+\n \tvmcb12-\u003econtrol.event_inj\t = 0;\n \tvmcb12-\u003econtrol.event_inj_err\t = 0;\n \tvmcb12-\u003econtrol.int_ctl = svm-\u003enested.ctl.int_ctl;\n@@ -1325,7 +1354,9 @@ void nested_svm_vmexit(struct vcpu_svm *svm)\n \n \t/* Exit Guest-Mode */\n \tleave_guest_mode(vcpu);\n-\tsvm_pmu_handle_nested_transition(svm);\n+\n+\tif (!nested_vmcb12_has_vpmc(vcpu))\n+\t\tsvm_pmu_handle_nested_transition(svm);\n \n \tsvm-\u003enested.vmcb12_gpa = 0;\n \n@@ -1381,6 +1412,10 @@ void nested_svm_vmexit(struct vcpu_svm *svm)\n \n \tsvm_update_lbrv(vcpu);\n \n+\tif (!nested_vmcb12_has_vpmc(vcpu) \u0026\u0026\n+\t kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\tsvm_copy_pmcs(\u0026vmcb01-\u003esave, \u0026vmcb02-\u003esave);\n+\n \tif (vnmi) {\n \t\tif (vmcb02-\u003econtrol.int_ctl \u0026 V_NMI_BLOCKING_MASK)\n \t\t\tvmcb01-\u003econtrol.int_ctl |= V_NMI_BLOCKING_MASK;\n@@ -1545,7 +1580,8 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)\n \t\t * into PMU state from arbitrary contexts (e.g. to avoid using\n \t\t * stale state).\n \t\t */\n-\t\t__svm_pmu_handle_nested_transition(svm, true);\n+\t\tif (!nested_vmcb12_has_vpmc(vcpu))\n+\t\t\t__svm_pmu_handle_nested_transition(svm, true);\n \n \t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\n \n@@ -2074,6 +2110,9 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n \n \tnested_vmcb02_prepare_control(svm);\n \n+\tif (!nested_vmcb12_has_vpmc(vcpu))\n+\t\tsvm_pmu_handle_nested_transition(svm);\n+\n \t/*\n \t * Any previously restored state (e.g. KVM_SET_SREGS) would mark fields\n \t * dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.\ndiff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c\nindex c18286545a7ac..7d3a151b6cdee 100644\n--- a/arch/x86/kvm/svm/pmu.c\n+++ b/arch/x86/kvm/svm/pmu.c\n@@ -125,6 +125,89 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)\n \treturn amd_msr_idx_to_pmc(vcpu, msr);\n }\n \n+static u32 amd_pmu_adjust_msr_idx(struct kvm_vcpu *vcpu, u32 msr)\n+{\n+\tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\n+\tif (!guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE) \u0026\u0026\n+\t msr \u003e= MSR_F15H_PERF_CTL0 \u0026\u0026\n+\t msr \u003c= MSR_F15H_PERF_CTR0 + 2 * pmu-\u003enr_arch_gp_counters)\n+\t\tmsr = ((msr \u0026 0x1) ? MSR_K7_PERFCTR0 : MSR_K7_EVNTSEL0) +\n+\t\t ((msr - MSR_F15H_PERF_CTL0) / 2);\n+\n+\treturn msr;\n+}\n+\n+static int amd_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)\n+{\n+\tstruct vmcb_save_area *save = \u0026to_svm(vcpu)-\u003evmcb-\u003esave;\n+\tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tstruct kvm_pmc *pmc;\n+\n+\t/* MSR_PERF_CNTR_GLOBAL_* */\n+\tswitch (msr) {\n+\tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:\n+\t\tpmu-\u003eglobal_status = save-\u003eperf_cntr_global_status;\n+\t\treturn 0;\n+\tcase MSR_AMD64_PERF_CNTR_GLOBAL_CTL:\n+\t\tpmu-\u003eglobal_ctrl = save-\u003eperf_cntr_global_control;\n+\t\treturn 0;\n+\t}\n+\n+\tmsr = amd_pmu_adjust_msr_idx(vcpu, msr);\n+\n+\t/* MSR_PERFCTRn */\n+\tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);\n+\tif (pmc) {\n+\t\tpmc-\u003ecounter = save-\u003epmc[pmc-\u003eidx].perf_ctr;\n+\t\treturn 0;\n+\t}\n+\n+\t/* MSR_EVNTSELn */\n+\tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);\n+\tif (pmc) {\n+\t\tpmc-\u003eeventsel_hw = save-\u003epmc[pmc-\u003eidx].perf_ctl;\n+\t\treturn 0;\n+\t}\n+\n+\treturn 1;\n+}\n+\n+static int amd_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)\n+{\n+\tstruct vmcb_save_area *save = \u0026to_svm(vcpu)-\u003evmcb-\u003esave;\n+\tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tstruct kvm_pmc *pmc;\n+\n+\t/* MSR_PERF_CNTR_GLOBAL_* */\n+\tswitch (msr) {\n+\tcase MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:\n+\t\tsave-\u003eperf_cntr_global_status = pmu-\u003eglobal_status;\n+\t\treturn 0;\n+\tcase MSR_AMD64_PERF_CNTR_GLOBAL_CTL:\n+\t\tsave-\u003eperf_cntr_global_control = pmu-\u003eglobal_ctrl;\n+\t\treturn 0;\n+\t}\n+\n+\tmsr = amd_pmu_adjust_msr_idx(vcpu, msr);\n+\n+\t/* MSR_PERFCTRn */\n+\tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);\n+\tif (pmc) {\n+\t\tsave-\u003epmc[pmc-\u003eidx].perf_ctr = pmc-\u003ecounter \u0026 pmc_bitmask(pmc);\n+\t\treturn 0;\n+\t}\n+\n+\t/* MSR_EVNTSELn */\n+\tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);\n+\tif (pmc) {\n+\t\tsave-\u003epmc[pmc-\u003eidx].perf_ctl = pmc-\u003eeventsel_hw;\n+\t\treturn 0;\n+\t}\n+\n+\treturn 1;\n+}\n+\n static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n {\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n@@ -134,6 +217,10 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t/* MSR_PERFCTRn */\n \tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);\n \tif (pmc) {\n+\t\tif (msr_info-\u003ehost_initiated \u0026\u0026\n+\t\t kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) \u0026\u0026\n+\t\t amd_get_vendor_state(vcpu, msr))\n+\t\t\tkvm_pmu_warn_vendor_state(msr);\n \t\tmsr_info-\u003edata = pmc_read_counter(pmc);\n \t\treturn 0;\n \t}\n@@ -158,6 +245,10 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \tpmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);\n \tif (pmc) {\n \t\tpmc_write_counter(pmc, data);\n+\t\tif (msr_info-\u003ehost_initiated \u0026\u0026\n+\t\t kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) \u0026\u0026\n+\t\t amd_set_vendor_state(vcpu, msr))\n+\t\t\tkvm_pmu_warn_vendor_state(msr);\n \t\treturn 0;\n \t}\n \t/* MSR_EVNTSELn */\n@@ -174,6 +265,9 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n \t\t\telse\n \t\t\t\t__clear_bit(pmc-\u003eidx, pmu-\u003epmc_has_mode_specific_enables);\n \n+\t\t\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\t\t\tpmc-\u003eeventsel_hw = data;\n+\n \t\t\tkvm_pmu_request_counter_reprogram(pmc);\n \t\t}\n \t\treturn 0;\n@@ -239,6 +333,24 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu)\n \t}\n }\n \n+static void amd_pmu_reset(struct kvm_vcpu *vcpu)\n+{\n+\tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n+\tstruct vmcb_save_area *save = \u0026to_svm(vcpu)-\u003evmcb-\u003esave;\n+\tint i;\n+\n+\tif (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\treturn;\n+\n+\tfor (i = 0; i \u003c pmu-\u003enr_arch_gp_counters; i++) {\n+\t\tsave-\u003epmc[i].perf_ctl = 0;\n+\t\tsave-\u003epmc[i].perf_ctr = 0;\n+\t}\n+\n+\tsave-\u003eperf_cntr_global_control = 0;\n+\tsave-\u003eperf_cntr_global_status = 0;\n+}\n+\n static bool amd_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_pmu)\n {\n \treturn host_pmu-\u003eversion \u003e= 2;\n@@ -311,18 +423,23 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {\n \t.refresh = amd_pmu_refresh,\n \t.init = amd_pmu_init,\n \t.pmc_is_disabled_in_current_mode = amd_pmc_is_disabled_in_current_mode,\n+\t.reset = amd_pmu_reset,\n \n \t.is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported,\n \t.mediated_load = amd_mediated_pmu_load,\n \t.mediated_put = amd_mediated_pmu_put,\n+\t.get_vendor_state = amd_get_vendor_state,\n+\t.set_vendor_state = amd_set_vendor_state,\n \n \t.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,\n \t.MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,\n \t.MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,\n \n \t.PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL,\n+\t.PERF_GLOBAL_STATUS = MSR_AMD64_PERF_CNTR_GLOBAL_STATUS,\n \t.GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0,\n \t.GP_COUNTER_BASE = MSR_F15H_PERF_CTR0,\n+\t.FIXED_COUNTER_CTRL = 0,\n \t.FIXED_COUNTER_BASE = 0,\n \t.MSR_STRIDE = 2,\n };\ndiff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c\nindex fcb41dfde4c02..8e8cd4feb16a0 100644\n--- a/arch/x86/kvm/svm/sev.c\n+++ b/arch/x86/kvm/svm/sev.c\n@@ -534,6 +534,9 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,\n \tif (snp_active)\n \t\tsev-\u003evmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE;\n \n+\tif (es_active)\n+\t\tkvm_clr_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED);\n+\n \tret = sev_asid_new(sev, vm_type);\n \tif (ret)\n \t\tgoto e_no_asid;\ndiff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c\nindex 9d607b98bd067..2023d38bba634 100644\n--- a/arch/x86/kvm/svm/svm.c\n+++ b/arch/x86/kvm/svm/svm.c\n@@ -179,6 +179,9 @@ module_param(vnmi, bool, 0444);\n \n module_param(enable_mediated_pmu, bool, 0444);\n \n+bool vpmc = true;\n+module_param(vpmc, bool, 0444);\n+\n static bool __ro_after_init svm_gp_erratum_intercept = true;\n \n static u8 rsm_ins_bytes[] = \"\\x0f\\xaa\";\n@@ -1267,6 +1270,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)\n \n \tsvm_hv_init_vmcb(vmcb);\n \n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\tcontrol-\u003emisc_ctl2 |= SVM_MISC2_ENABLE_V_PMC;\n+\n \tkvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu);\n \n \tvmcb_mark_all_dirty(vmcb);\n@@ -3563,6 +3569,30 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)\n \t \"excp_from:\", save-\u003elast_excp_from,\n \t \"excp_to:\", save-\u003elast_excp_to);\n \n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) {\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_ctl0:\", save-\u003epmc[0].perf_ctl,\n+\t\t \"perf_ctr0:\", save-\u003epmc[0].perf_ctr);\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_ctl1:\", save-\u003epmc[1].perf_ctl,\n+\t\t \"perf_ctr1:\", save-\u003epmc[1].perf_ctr);\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_ctl2:\", save-\u003epmc[2].perf_ctl,\n+\t\t \"perf_ctr2:\", save-\u003epmc[2].perf_ctr);\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_ctl3:\", save-\u003epmc[3].perf_ctl,\n+\t\t \"perf_ctr3:\", save-\u003epmc[3].perf_ctr);\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_ctl4:\", save-\u003epmc[4].perf_ctl,\n+\t\t \"perf_ctr4:\", save-\u003epmc[4].perf_ctr);\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_ctl5:\", save-\u003epmc[5].perf_ctl,\n+\t\t \"perf_ctr5:\", save-\u003epmc[5].perf_ctr);\n+\t\tpr_err(\"%-15s %016llx %-13s %016llx\\n\",\n+\t\t \"perf_cntr_global_control:\", save-\u003eperf_cntr_global_control,\n+\t\t \"perf_cntr_global_status:\", save-\u003eperf_cntr_global_status);\n+\t}\n+\n \tif (is_sev_es_guest(vcpu)) {\n \t\tstruct sev_es_save_area *vmsa = (struct sev_es_save_area *)save;\n \n@@ -4452,6 +4482,14 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, unsigned enter_fl\n \n \tamd_clear_divider();\n \n+\t/*\n+\t * On #VMEXIT, PerfCntrGlobalCtl goes back to its reset state since\n+\t * its save slot is of Swap Type C. All enable bits are set but PMC\n+\t * virtualization requires them to be cleared before VMRUN.\n+\t */\n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n+\t\twrmsrq(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0);\n+\n \tif (is_sev_es_guest(vcpu))\n \t\t__svm_sev_es_vcpu_run(svm, enter_flags,\n \t\t\t\t sev_es_host_save_area(sd));\n@@ -4618,7 +4656,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)\n \n \tkvm_clear_available_registers(vcpu, SVM_REGS_LAZY_LOAD_SET);\n \n-\tif (!msr_write_intercepted(svm, MSR_AMD64_PERF_CNTR_GLOBAL_CTL))\n+\tif (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) \u0026\u0026\n+\t !msr_write_intercepted(svm, MSR_AMD64_PERF_CNTR_GLOBAL_CTL))\n \t\trdmsrq(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, vcpu_to_pmu(vcpu)-\u003eglobal_ctrl);\n \n \ttrace_kvm_exit(vcpu, KVM_ISA_SVM);\n@@ -5288,6 +5327,9 @@ static void svm_vm_destroy(struct kvm *kvm)\n \n static int svm_vm_init(struct kvm *kvm)\n {\n+\tif (vpmc)\n+\t\tkvm_set_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED);\n+\n \tsev_vm_init(kvm);\n \n \tif (!pause_filter_count || !pause_filter_thresh)\n@@ -5541,6 +5583,10 @@ static __init void svm_set_cpu_caps(void)\n \t\tif (vnmi)\n \t\t\tkvm_cpu_cap_set(X86_FEATURE_VNMI);\n \n+\t\t/* Nested AVIC is not supported, so VNMI must be enabled */\n+\t\tif (vpmc \u0026\u0026 vnmi)\n+\t\t\tkvm_cpu_cap_set(X86_FEATURE_PERFCTR_VIRT);\n+\n \t\t/* Nested VM can receive #VMEXIT instead of triggering #GP */\n \t\tkvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);\n \t}\n@@ -5734,6 +5780,16 @@ static __init int svm_hardware_setup(void)\n \tif (!enable_pmu)\n \t\tpr_info(\"PMU virtualization is disabled\\n\");\n \n+\t/*\n+\t * PMC virtualization does not raise host PMIs that need to be injected.\n+\t * Instead, it requires VNMI or AVIC for guest PMI delivery. AVIC can,\n+\t * however, get inhibited so make VNMI the hard requirement.\n+\t */\n+\tvpmc = vpmc \u0026\u0026 vnmi \u0026\u0026 enable_mediated_pmu \u0026\u0026\n+\t cpu_feature_enabled(X86_FEATURE_PERFCTR_VIRT);\n+\tif (vpmc)\n+\t\tpr_info(\"PMC virtualization supported\\n\");\n+\n \tsvm_set_cpu_caps();\n \n \tkvm_caps.inapplicable_quirks \u0026= ~KVM_X86_QUIRK_CD_NW_CLEARED;\ndiff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h\nindex 66b44b54608e8..3db6b73e81634 100644\n--- a/arch/x86/kvm/svm/svm.h\n+++ b/arch/x86/kvm/svm/svm.h\n@@ -53,6 +53,7 @@ extern int vgif;\n extern bool intercept_smi;\n extern bool vnmi;\n extern int lbrv;\n+extern bool vpmc;\n \n extern int tsc_aux_uret_slot __ro_after_init;\n \n@@ -174,6 +175,12 @@ struct vmcb_save_area_cached {\n \tu64 br_to;\n \tu64 last_excp_from;\n \tu64 last_excp_to;\n+\tstruct {\n+\t\tu64 perf_ctl;\n+\t\tu64 perf_ctr;\n+\t} pmc[6];\n+\tu64 perf_cntr_global_status;\n+\tu64 perf_cntr_global_control;\n };\n \n struct vmcb_ctrl_area_cached {\n@@ -805,6 +812,18 @@ do {\t\t\t\t\t\t\t\t\\\n \t(to)-\u003elast_excp_to\t= (from)-\u003elast_excp_to;\t\t\\\n } while (0)\n \n+#define svm_copy_pmcs(to, from)\t\t\t\t\t\t\t\\\n+do {\t\t\t\t\t\t\t\t\t\t\\\n+\tint i;\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\t\\\n+\t(to)-\u003eperf_cntr_global_control = (from)-\u003eperf_cntr_global_control;\t\\\n+\t(to)-\u003eperf_cntr_global_status = (from)-\u003eperf_cntr_global_status;\t\\\n+\tfor (i = 0; i \u003c kvm_pmu_cap.num_counters_gp; i++) {\t\t\t\\\n+\t\t(to)-\u003epmc[i].perf_ctl = (from)-\u003epmc[i].perf_ctl;\t\t\\\n+\t\t(to)-\u003epmc[i].perf_ctr = (from)-\u003epmc[i].perf_ctr;\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\\\n+} while (0)\n+\n void svm_vcpu_free_msrpm(void *msrpm);\n void svm_enable_lbrv(struct kvm_vcpu *vcpu);\n void svm_update_lbrv(struct kvm_vcpu *vcpu);\ndiff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c\nindex bfa8612fb4508..d0723814410e7 100644\n--- a/arch/x86/kvm/vmx/pmu_intel.c\n+++ b/arch/x86/kvm/vmx/pmu_intel.c\n@@ -853,8 +853,10 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = {\n \t.MIN_NR_GP_COUNTERS = 1,\n \n \t.PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL,\n+\t.PERF_GLOBAL_STATUS = MSR_CORE_PERF_GLOBAL_STATUS,\n \t.GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0,\n \t.GP_COUNTER_BASE = MSR_IA32_PMC0,\n+\t.FIXED_COUNTER_CTRL = MSR_CORE_PERF_FIXED_CTR_CTRL,\n \t.FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,\n \t.MSR_STRIDE = 1,\n };\ndiff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c\nindex e3bfe6aca1a0e..7d8c032c1897e 100644\n--- a/arch/x86/kvm/vmx/vmx.c\n+++ b/arch/x86/kvm/vmx/vmx.c\n@@ -7383,7 +7383,8 @@ static void vmx_refresh_guest_perf_global_control(struct kvm_vcpu *vcpu)\n \tstruct kvm_pmu *pmu = vcpu_to_pmu(vcpu);\n \tstruct vcpu_vmx *vmx = to_vmx(vcpu);\n \n-\tif (msr_write_intercepted(vmx, MSR_CORE_PERF_GLOBAL_CTRL))\n+\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) ||\n+\t msr_write_intercepted(vmx, MSR_CORE_PERF_GLOBAL_CTRL))\n \t\treturn;\n \n \tif (!cpu_has_save_perf_global_ctrl()) {\ndiff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c\nindex d94b59140c452..4287982638b6e 100644\n--- a/arch/x86/kvm/x86.c\n+++ b/arch/x86/kvm/x86.c\n@@ -9326,6 +9326,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)\n \t\tif (irqchip_in_kernel(kvm)) {\n \t\t\tr = perf_create_mediated_pmu();\n \t\t\tif (r) {\n+\t\t\t\tkvm-\u003earch.mediated_pmu_caps = 0;\n \t\t\t\tpr_warn_ratelimited(PERF_MEDIATED_PMU_MSG);\n \t\t\t\treturn r;\n \t\t\t}\n@@ -9334,6 +9335,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)\n \t\t\tkvm-\u003earch.enable_pmu = false;\n \t\t}\n \t}\n+\n \treturn 0;\n }\n \n"
} |
| 2/1 | 2026/08/10 10:39 | agent | patch-evaluator |
1mModel:best-expensive Args: null Results: {
"EnableConfigs": [
"KVM",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_get_msr",
"kvm_pmu_set_msr",
"kvm_pmu_rdpmc",
"kvm_pmu_refresh",
"kvm_mediated_pmu_load",
"kvm_mediated_pmu_put",
"enter_svm_guest_mode",
"nested_svm_vmexit",
"svm_leave_nested"
],
"Reasoning": "The patch introduces hardware-switched PMU state for AMD SVM guests, adding support for PMC virtualization. This involves significant functional changes to KVM's PMU handling, MSR virtualization, and nested virtualization transitions. These changes are reachable from guest execution and should be heavily fuzzed.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit b1b55dd3c5a9465f0f9a50de4b59854f8eb681d2
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Aug 10 10:39:40 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
index 30bb8fdf2a8b5..b551ba381b6e2 100644
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -384,6 +384,7 @@
#define X86_FEATURE_VMCBCLEAN (15*32+ 5) /* "vmcb_clean" VMCB clean bits support */
#define X86_FEATURE_FLUSHBYASID (15*32+ 6) /* "flushbyasid" Flush-by-ASID support */
#define X86_FEATURE_DECODEASSISTS (15*32+ 7) /* "decodeassists" Decode Assists support */
+#define X86_FEATURE_PERFCTR_VIRT (15*32+ 8) /* PMC virtualization support */
#define X86_FEATURE_PAUSEFILTER (15*32+10) /* "pausefilter" Filtered pause intercept */
#define X86_FEATURE_PFTHRESHOLD (15*32+12) /* "pfthreshold" Pause filter threshold */
#define X86_FEATURE_AVIC (15*32+13) /* "avic" Virtual Interrupt Controller */
diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h
index 4a223c2793e3f..4a617162d4c08 100644
--- a/arch/x86/include/asm/kvm-x86-pmu-ops.h
+++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h
@@ -29,6 +29,9 @@ KVM_X86_PMU_OP_OPTIONAL_RET0(pmc_is_disabled_in_current_mode)
KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl)
KVM_X86_PMU_OP(mediated_load)
KVM_X86_PMU_OP(mediated_put)
+
+KVM_X86_PMU_OP_OPTIONAL_RET0(get_vendor_state)
+KVM_X86_PMU_OP_OPTIONAL_RET0(set_vendor_state)
#endif
#undef KVM_X86_PMU_OP
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 283847619ff88..338df11e2a4df 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -715,6 +715,16 @@ enum kvm_only_cpuid_leafs {
NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
};
+/*
+ * Track how the guest PMU state is saved and restored, as this can be done
+ * either by software or, if capable, by hardware. When hardware manages the
+ * guest PMU state, the state resides in a vendor save area.
+ */
+#define KVM_MEDIATED_PMU_CAP_HW_SWITCHED BIT(0)
+#define KVM_MEDIATED_PMU_CAP_HW_FILTERED BIT(1)
+#define KVM_MEDIATED_PMU_CAP_VALID (KVM_MEDIATED_PMU_CAP_HW_SWITCHED | \
+ KVM_MEDIATED_PMU_CAP_HW_FILTERED)
+
struct kvm_vcpu_arch {
/*
* rip and regs accesses must go through
@@ -1284,6 +1294,7 @@ struct kvm_arch {
bool bus_lock_detection_enabled;
bool enable_pmu;
bool created_mediated_pmu;
+ u32 mediated_pmu_caps;
u32 notify_window;
u32 notify_vmexit_flags;
diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h
index aa63431ba92c3..e1c8a333e86fa 100644
--- a/arch/x86/include/asm/svm.h
+++ b/arch/x86/include/asm/svm.h
@@ -247,6 +247,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
#define SVM_MISC2_ENABLE_V_LBR BIT_ULL(0)
#define SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE BIT_ULL(1)
+#define SVM_MISC2_ENABLE_V_PMC BIT_ULL(3)
#define SVM_TSC_RATIO_RSVD 0xffffff0000000000ULL
#define SVM_TSC_RATIO_MIN 0x0000000000000001ULL
@@ -338,7 +339,12 @@ struct vmcb_save_area {
u8 cpl;
u8 reserved_0xcc[4];
u64 efer;
- u8 reserved_0xd8[112];
+ u8 reserved_0xd8[8];
+ struct {
+ u64 perf_ctl;
+ u64 perf_ctr;
+ } __packed pmc[6];
+ u8 reserved_0x140[8];
u64 cr4;
u64 cr3;
u64 cr0;
@@ -346,7 +352,9 @@ struct vmcb_save_area {
u64 dr6;
u64 rflags;
u64 rip;
- u8 reserved_0x180[88];
+ u8 reserved_0x180[72];
+ u64 perf_cntr_global_status;
+ u64 perf_cntr_global_control;
u64 rsp;
u64 s_cet;
u64 ssp;
@@ -575,6 +583,7 @@ static inline void __unused_size_checks(void)
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xa0);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xcc);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xd8);
+ BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x140);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x180);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x248);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x298);
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index ddb022cb203a2..ce4f984bc6e16 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1217,6 +1217,7 @@ void kvm_initialize_cpu_caps(void)
VENDOR_F(VGIF),
VENDOR_F(VNMI),
VENDOR_F(SVME_ADDR_CHK),
+ VENDOR_F(PERFCTR_VIRT),
);
kvm_cpu_cap_init(CPUID_8000_001F_EAX,
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index a7d60c8785cd4..4c918aa6d6f37 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -130,6 +130,24 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)
#undef __KVM_X86_PMU_OP
}
+static void kvm_pmu_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
+ if (kvm_pmu_call(get_vendor_state)(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
+}
+
+static void kvm_pmu_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
+ if (kvm_pmu_call(set_vendor_state)(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
+}
+
void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
{
bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
@@ -166,8 +184,11 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
!pmu_ops->is_mediated_pmu_supported(&kvm_host_pmu))
enable_mediated_pmu = false;
- if (!enable_mediated_pmu)
+ if (!enable_mediated_pmu) {
pmu_ops->write_global_ctrl = NULL;
+ pmu_ops->get_vendor_state = NULL;
+ pmu_ops->set_vendor_state = NULL;
+ }
if (!enable_pmu) {
memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));
@@ -197,6 +218,27 @@ void kvm_handle_guest_mediated_pmi(void)
kvm_make_request(KVM_REQ_PMI, vcpu);
}
+static __always_inline u32 fixed_counter_msr(u32 idx)
+{
+ return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 gp_counter_msr(u32 idx)
+{
+ return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 gp_eventsel_msr(u32 idx)
+{
+ return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 pmc_counter_msr(struct kvm_pmc *pmc)
+{
+ return pmc_is_gp(pmc) ? gp_counter_msr(pmc->idx) :
+ fixed_counter_msr(pmc->idx - KVM_FIXED_PMC_BASE_IDX);
+}
+
static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)
{
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
@@ -550,18 +592,23 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)
{
bool allowed = pmc_is_locally_enabled(pmc) && pmc_is_event_allowed(pmc);
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
+ struct kvm_vcpu *vcpu = pmc->vcpu;
if (pmc_is_gp(pmc)) {
pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
if (allowed)
pmc->eventsel_hw |= pmc->eventsel &
ARCH_PERFMON_EVENTSEL_ENABLE;
+
+ kvm_pmu_set_vendor_state(vcpu, gp_eventsel_msr(pmc->idx));
} else {
u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX, 0xf);
pmu->fixed_ctr_ctrl_hw &= ~mask;
if (allowed)
pmu->fixed_ctr_ctrl_hw |= pmu->fixed_ctr_ctrl & mask;
+
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.FIXED_COUNTER_CTRL);
}
}
@@ -769,6 +816,8 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))
return 1;
+ kvm_pmu_get_vendor_state(vcpu, pmc_counter_msr(pmc));
+
*data = pmc_read_counter(pmc) & mask;
return 0;
}
@@ -852,10 +901,12 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
switch (msr) {
case MSR_CORE_PERF_GLOBAL_STATUS:
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
msr_info->data = pmu->global_status;
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
case MSR_CORE_PERF_GLOBAL_CTRL:
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);
msr_info->data = pmu->global_ctrl;
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
@@ -895,6 +946,7 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
return 1;
pmu->global_status = data;
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
data &= ~pmu->global_ctrl_rsvd;
@@ -914,6 +966,8 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
*/
if (kvm_vcpu_has_mediated_pmu(vcpu))
kvm_pmu_call(write_global_ctrl)(data);
+
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);
break;
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
/*
@@ -924,12 +978,18 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
return 1;
fallthrough;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
- if (!msr_info->host_initiated)
+ if (!msr_info->host_initiated) {
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
pmu->global_status &= ~data;
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+ }
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
- if (!msr_info->host_initiated)
+ if (!msr_info->host_initiated) {
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
pmu->global_status |= data & ~pmu->global_status_rsvd;
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+ }
break;
default:
kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
@@ -1017,6 +1077,8 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
if (kvm_vcpu_has_mediated_pmu(vcpu))
kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_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);
@@ -1139,6 +1201,9 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,
if (bitmap_empty(event_pmcs, X86_PMC_IDX_MAX))
return;
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+
if (!kvm_pmu_has_perf_global_ctrl(pmu))
bitmap_copy(bitmap, event_pmcs, X86_PMC_IDX_MAX);
else if (!bitmap_and(bitmap, event_pmcs,
@@ -1147,11 +1212,17 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,
idx = srcu_read_lock(&vcpu->kvm->srcu);
kvm_for_each_pmc(pmu, pmc, i, bitmap) {
+ kvm_pmu_get_vendor_state(vcpu, pmc_counter_msr(pmc));
+
if (!pmc_is_event_allowed(pmc) || !cpl_is_matched(pmc))
continue;
kvm_pmu_incr_counter(pmc);
+ kvm_pmu_set_vendor_state(vcpu, pmc_counter_msr(pmc));
}
+
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+
srcu_read_unlock(&vcpu->kvm->srcu, idx);
}
@@ -1310,21 +1381,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)
return r;
}
-static __always_inline u32 fixed_counter_msr(u32 idx)
-{
- return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
-static __always_inline u32 gp_counter_msr(u32 idx)
-{
- return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
-static __always_inline u32 gp_eventsel_msr(u32 idx)
-{
- return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -1361,6 +1417,10 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
perf_load_guest_context();
+ /* Guest PMU state is restored by hardware */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
/*
* Explicitly clear PERF_GLOBAL_CTRL, as "loading" the guest's context
* disables all individual counters (if any were enabled), but doesn't
@@ -1420,6 +1480,12 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)
lockdep_assert_irqs_disabled();
+ /* Guest PMU state is saved by hardware */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) {
+ perf_put_guest_context();
+ return;
+ }
+
/*
* Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's
* atomically cleared on VM-Exit, i.e. doesn't need to be clear here.
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f48..42d33072ece90 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -42,14 +42,18 @@ struct kvm_pmu_ops {
void (*mediated_load)(struct kvm_vcpu *vcpu);
void (*mediated_put)(struct kvm_vcpu *vcpu);
void (*write_global_ctrl)(u64 global_ctrl);
+ int (*get_vendor_state)(struct kvm_vcpu *vcpu, u32 msr);
+ int (*set_vendor_state)(struct kvm_vcpu *vcpu, u32 msr);
const u64 EVENTSEL_EVENT;
const int MAX_NR_GP_COUNTERS;
const int MIN_NR_GP_COUNTERS;
const u32 PERF_GLOBAL_CTRL;
+ const u32 PERF_GLOBAL_STATUS;
const u32 GP_EVENTSEL_BASE;
const u32 GP_COUNTER_BASE;
+ const u32 FIXED_COUNTER_CTRL;
const u32 FIXED_COUNTER_BASE;
const u32 MSR_STRIDE;
};
@@ -88,6 +92,31 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
return enable_mediated_pmu && vcpu_to_pmu(vcpu)->version;
}
+static inline bool kvm_vcpu_has_mediated_pmu_caps(struct kvm_vcpu *vcpu, u32 caps)
+{
+ return kvm_vcpu_has_mediated_pmu(vcpu) &&
+ !!(vcpu->kvm->arch.mediated_pmu_caps & caps);
+}
+
+#define kvm_set_mediated_pmu_caps(kvm, caps) \
+do { \
+ BUILD_BUG_ON(!__builtin_constant_p(caps) || \
+ ((caps) & ~KVM_MEDIATED_PMU_CAP_VALID)); \
+ (kvm)->arch.mediated_pmu_caps |= (caps); \
+} while (0)
+
+#define kvm_clr_mediated_pmu_caps(kvm, caps) \
+do { \
+ BUILD_BUG_ON(!__builtin_constant_p(caps) || \
+ ((caps) & ~KVM_MEDIATED_PMU_CAP_VALID)); \
+ (kvm)->arch.mediated_pmu_caps &= ~(caps); \
+} while (0)
+
+static inline void kvm_pmu_warn_vendor_state(u32 msr)
+{
+ WARN_ONCE(1, "accessing unsupported vendor save slot for MSR 0x%x\n", msr);
+}
+
/*
* 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
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 73f37b050d0a0..a9f9caeb78406 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -605,6 +605,7 @@ static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
to->g_pat = from->g_pat;
svm_copy_lbrs(to, from);
+ svm_copy_pmcs(to, from);
}
void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
@@ -741,6 +742,17 @@ static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vcpu)
(to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR);
}
+static bool nested_vmcb12_has_vpmc(struct kvm_vcpu *vcpu)
+{
+ /*
+ * Since nested AVIC is not supported, L2 PMIs can only be delivered
+ * via VNMI, so make it a hard requirement.
+ */
+ return guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_VIRT) &&
+ nested_vnmi_enabled(to_svm(vcpu)) &&
+ (to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_PMC);
+}
+
static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
{
struct vmcb_ctrl_area_cached *control = &svm->nested.ctl;
@@ -823,6 +835,11 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
}
vmcb_mark_dirty(vmcb02, VMCB_LBR);
svm_update_lbrv(&svm->vcpu);
+
+ if (nested_vmcb12_has_vpmc(vcpu))
+ svm_copy_pmcs(&vmcb02->save, save);
+ else if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ svm_copy_pmcs(&vmcb02->save, &vmcb01->save);
}
static inline bool is_evtinj_soft(u32 evtinj)
@@ -863,7 +880,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
/* Enter Guest-Mode */
enter_guest_mode(vcpu);
- svm_pmu_handle_nested_transition(svm);
/*
* Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,
@@ -986,6 +1002,10 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
/* SVM_MISC2_ENABLE_V_LBR is controlled by svm_update_lbrv() */
+ /* L0 uses hardware-switched mode for both vmcb01 and vmcb02 */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ vmcb02->control.misc_ctl2 |= SVM_MISC2_ENABLE_V_PMC;
+
if (!nested_vmcb_needs_vls_intercept(svm))
vmcb02->control.misc_ctl2 |= SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE;
@@ -1065,6 +1085,9 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)
nested_vmcb02_prepare_control(svm);
nested_vmcb02_prepare_save(svm);
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ svm_pmu_handle_nested_transition(svm);
+
ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
nested_npt_enabled(svm), from_vmrun);
if (ret)
@@ -1228,6 +1251,9 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
svm_copy_lbrs(to_save, from_save);
to_save->dbgctl &= ~DEBUGCTL_RESERVED_BITS;
}
+
+ if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_VIRT))
+ svm_copy_pmcs(to_save, from_save);
}
void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
@@ -1300,6 +1326,9 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)
if (nested_vmcb12_has_lbrv(vcpu))
svm_copy_lbrs(&vmcb12->save, &vmcb02->save);
+ if (nested_vmcb12_has_vpmc(vcpu))
+ svm_copy_pmcs(&vmcb12->save, &vmcb02->save);
+
vmcb12->control.event_inj = 0;
vmcb12->control.event_inj_err = 0;
vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
@@ -1325,7 +1354,9 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
/* Exit Guest-Mode */
leave_guest_mode(vcpu);
- svm_pmu_handle_nested_transition(svm);
+
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ svm_pmu_handle_nested_transition(svm);
svm->nested.vmcb12_gpa = 0;
@@ -1381,6 +1412,10 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
svm_update_lbrv(vcpu);
+ if (!nested_vmcb12_has_vpmc(vcpu) &&
+ kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ svm_copy_pmcs(&vmcb01->save, &vmcb02->save);
+
if (vnmi) {
if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK)
vmcb01->control.int_ctl |= V_NMI_BLOCKING_MASK;
@@ -1545,7 +1580,8 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
* into PMU state from arbitrary contexts (e.g. to avoid using
* stale state).
*/
- __svm_pmu_handle_nested_transition(svm, true);
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ __svm_pmu_handle_nested_transition(svm, true);
svm_switch_vmcb(svm, &svm->vmcb01);
@@ -2074,6 +2110,9 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
nested_vmcb02_prepare_control(svm);
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ svm_pmu_handle_nested_transition(svm);
+
/*
* Any previously restored state (e.g. KVM_SET_SREGS) would mark fields
* dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7ac..7d3a151b6cdee 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -125,6 +125,89 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
return amd_msr_idx_to_pmc(vcpu, msr);
}
+static u32 amd_pmu_adjust_msr_idx(struct kvm_vcpu *vcpu, u32 msr)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+ if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE) &&
+ msr >= MSR_F15H_PERF_CTL0 &&
+ msr <= MSR_F15H_PERF_CTR0 + 2 * pmu->nr_arch_gp_counters)
+ msr = ((msr & 0x1) ? MSR_K7_PERFCTR0 : MSR_K7_EVNTSEL0) +
+ ((msr - MSR_F15H_PERF_CTL0) / 2);
+
+ return msr;
+}
+
+static int amd_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ struct kvm_pmc *pmc;
+
+ /* MSR_PERF_CNTR_GLOBAL_* */
+ switch (msr) {
+ case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+ pmu->global_status = save->perf_cntr_global_status;
+ return 0;
+ case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+ pmu->global_ctrl = save->perf_cntr_global_control;
+ return 0;
+ }
+
+ msr = amd_pmu_adjust_msr_idx(vcpu, msr);
+
+ /* MSR_PERFCTRn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
+ if (pmc) {
+ pmc->counter = save->pmc[pmc->idx].perf_ctr;
+ return 0;
+ }
+
+ /* MSR_EVNTSELn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
+ if (pmc) {
+ pmc->eventsel_hw = save->pmc[pmc->idx].perf_ctl;
+ return 0;
+ }
+
+ return 1;
+}
+
+static int amd_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ struct kvm_pmc *pmc;
+
+ /* MSR_PERF_CNTR_GLOBAL_* */
+ switch (msr) {
+ case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+ save->perf_cntr_global_status = pmu->global_status;
+ return 0;
+ case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+ save->perf_cntr_global_control = pmu->global_ctrl;
+ return 0;
+ }
+
+ msr = amd_pmu_adjust_msr_idx(vcpu, msr);
+
+ /* MSR_PERFCTRn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
+ if (pmc) {
+ save->pmc[pmc->idx].perf_ctr = pmc->counter & pmc_bitmask(pmc);
+ return 0;
+ }
+
+ /* MSR_EVNTSELn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
+ if (pmc) {
+ save->pmc[pmc->idx].perf_ctl = pmc->eventsel_hw;
+ return 0;
+ }
+
+ return 1;
+}
+
static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -134,6 +217,10 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
/* MSR_PERFCTRn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
+ if (msr_info->host_initiated &&
+ kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) &&
+ amd_get_vendor_state(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
msr_info->data = pmc_read_counter(pmc);
return 0;
}
@@ -158,6 +245,10 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
pmc_write_counter(pmc, data);
+ if (msr_info->host_initiated &&
+ kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) &&
+ amd_set_vendor_state(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
return 0;
}
/* MSR_EVNTSELn */
@@ -174,6 +265,9 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
else
__clear_bit(pmc->idx, pmu->pmc_has_mode_specific_enables);
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ pmc->eventsel_hw = data;
+
kvm_pmu_request_counter_reprogram(pmc);
}
return 0;
@@ -239,6 +333,24 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu)
}
}
+static void amd_pmu_reset(struct kvm_vcpu *vcpu)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
+ int i;
+
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
+ for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ save->pmc[i].perf_ctl = 0;
+ save->pmc[i].perf_ctr = 0;
+ }
+
+ save->perf_cntr_global_control = 0;
+ save->perf_cntr_global_status = 0;
+}
+
static bool amd_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_pmu)
{
return host_pmu->version >= 2;
@@ -311,18 +423,23 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {
.refresh = amd_pmu_refresh,
.init = amd_pmu_init,
.pmc_is_disabled_in_current_mode = amd_pmc_is_disabled_in_current_mode,
+ .reset = amd_pmu_reset,
.is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported,
.mediated_load = amd_mediated_pmu_load,
.mediated_put = amd_mediated_pmu_put,
+ .get_vendor_state = amd_get_vendor_state,
+ .set_vendor_state = amd_set_vendor_state,
.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,
.MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,
.MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,
.PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL,
+ .PERF_GLOBAL_STATUS = MSR_AMD64_PERF_CNTR_GLOBAL_STATUS,
.GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0,
.GP_COUNTER_BASE = MSR_F15H_PERF_CTR0,
+ .FIXED_COUNTER_CTRL = 0,
.FIXED_COUNTER_BASE = 0,
.MSR_STRIDE = 2,
};
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index fcb41dfde4c02..8e8cd4feb16a0 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -534,6 +534,9 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,
if (snp_active)
sev->vmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE;
+ if (es_active)
+ kvm_clr_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED);
+
ret = sev_asid_new(sev, vm_type);
if (ret)
goto e_no_asid;
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 9d607b98bd067..2023d38bba634 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -179,6 +179,9 @@ module_param(vnmi, bool, 0444);
module_param(enable_mediated_pmu, bool, 0444);
+bool vpmc = true;
+module_param(vpmc, bool, 0444);
+
static bool __ro_after_init svm_gp_erratum_intercept = true;
static u8 rsm_ins_bytes[] = "\x0f\xaa";
@@ -1267,6 +1270,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
svm_hv_init_vmcb(vmcb);
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ control->misc_ctl2 |= SVM_MISC2_ENABLE_V_PMC;
+
kvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu);
vmcb_mark_all_dirty(vmcb);
@@ -3563,6 +3569,30 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)
"excp_from:", save->last_excp_from,
"excp_to:", save->last_excp_to);
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) {
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl0:", save->pmc[0].perf_ctl,
+ "perf_ctr0:", save->pmc[0].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl1:", save->pmc[1].perf_ctl,
+ "perf_ctr1:", save->pmc[1].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl2:", save->pmc[2].perf_ctl,
+ "perf_ctr2:", save->pmc[2].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl3:", save->pmc[3].perf_ctl,
+ "perf_ctr3:", save->pmc[3].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl4:", save->pmc[4].perf_ctl,
+ "perf_ctr4:", save->pmc[4].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl5:", save->pmc[5].perf_ctl,
+ "perf_ctr5:", save->pmc[5].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_cntr_global_control:", save->perf_cntr_global_control,
+ "perf_cntr_global_status:", save->perf_cntr_global_status);
+ }
+
if (is_sev_es_guest(vcpu)) {
struct sev_es_save_area *vmsa = (struct sev_es_save_area *)save;
@@ -4452,6 +4482,14 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, unsigned enter_fl
amd_clear_divider();
+ /*
+ * On #VMEXIT, PerfCntrGlobalCtl goes back to its reset state since
+ * its save slot is of Swap Type C. All enable bits are set but PMC
+ * virtualization requires them to be cleared before VMRUN.
+ */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ wrmsrq(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0);
+
if (is_sev_es_guest(vcpu))
__svm_sev_es_vcpu_run(svm, enter_flags,
sev_es_host_save_area(sd));
@@ -4618,7 +4656,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
kvm_clear_available_registers(vcpu, SVM_REGS_LAZY_LOAD_SET);
- if (!msr_write_intercepted(svm, MSR_AMD64_PERF_CNTR_GLOBAL_CTL))
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) &&
+ !msr_write_intercepted(svm, MSR_AMD64_PERF_CNTR_GLOBAL_CTL))
rdmsrq(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, vcpu_to_pmu(vcpu)->global_ctrl);
trace_kvm_exit(vcpu, KVM_ISA_SVM);
@@ -5288,6 +5327,9 @@ static void svm_vm_destroy(struct kvm *kvm)
static int svm_vm_init(struct kvm *kvm)
{
+ if (vpmc)
+ kvm_set_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED);
+
sev_vm_init(kvm);
if (!pause_filter_count || !pause_filter_thresh)
@@ -5541,6 +5583,10 @@ static __init void svm_set_cpu_caps(void)
if (vnmi)
kvm_cpu_cap_set(X86_FEATURE_VNMI);
+ /* Nested AVIC is not supported, so VNMI must be enabled */
+ if (vpmc && vnmi)
+ kvm_cpu_cap_set(X86_FEATURE_PERFCTR_VIRT);
+
/* Nested VM can receive #VMEXIT instead of triggering #GP */
kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
}
@@ -5734,6 +5780,16 @@ static __init int svm_hardware_setup(void)
if (!enable_pmu)
pr_info("PMU virtualization is disabled\n");
+ /*
+ * PMC virtualization does not raise host PMIs that need to be injected.
+ * Instead, it requires VNMI or AVIC for guest PMI delivery. AVIC can,
+ * however, get inhibited so make VNMI the hard requirement.
+ */
+ vpmc = vpmc && vnmi && enable_mediated_pmu &&
+ cpu_feature_enabled(X86_FEATURE_PERFCTR_VIRT);
+ if (vpmc)
+ pr_info("PMC virtualization supported\n");
+
svm_set_cpu_caps();
kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED;
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 66b44b54608e8..3db6b73e81634 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -53,6 +53,7 @@ extern int vgif;
extern bool intercept_smi;
extern bool vnmi;
extern int lbrv;
+extern bool vpmc;
extern int tsc_aux_uret_slot __ro_after_init;
@@ -174,6 +175,12 @@ struct vmcb_save_area_cached {
u64 br_to;
u64 last_excp_from;
u64 last_excp_to;
+ struct {
+ u64 perf_ctl;
+ u64 perf_ctr;
+ } pmc[6];
+ u64 perf_cntr_global_status;
+ u64 perf_cntr_global_control;
};
struct vmcb_ctrl_area_cached {
@@ -805,6 +812,18 @@ do { \
(to)->last_excp_to = (from)->last_excp_to; \
} while (0)
+#define svm_copy_pmcs(to, from) \
+do { \
+ int i; \
+ \
+ (to)->perf_cntr_global_control = (from)->perf_cntr_global_control; \
+ (to)->perf_cntr_global_status = (from)->perf_cntr_global_status; \
+ for (i = 0; i < kvm_pmu_cap.num_counters_gp; i++) { \
+ (to)->pmc[i].perf_ctl = (from)->pmc[i].perf_ctl; \
+ (to)->pmc[i].perf_ctr = (from)->pmc[i].perf_ctr; \
+ } \
+} while (0)
+
void svm_vcpu_free_msrpm(void *msrpm);
void svm_enable_lbrv(struct kvm_vcpu *vcpu);
void svm_update_lbrv(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index bfa8612fb4508..d0723814410e7 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -853,8 +853,10 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = {
.MIN_NR_GP_COUNTERS = 1,
.PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL,
+ .PERF_GLOBAL_STATUS = MSR_CORE_PERF_GLOBAL_STATUS,
.GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0,
.GP_COUNTER_BASE = MSR_IA32_PMC0,
+ .FIXED_COUNTER_CTRL = MSR_CORE_PERF_FIXED_CTR_CTRL,
.FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,
.MSR_STRIDE = 1,
};
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index e3bfe6aca1a0e..7d8c032c1897e 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7383,7 +7383,8 @@ static void vmx_refresh_guest_perf_global_control(struct kvm_vcpu *vcpu)
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
- if (msr_write_intercepted(vmx, MSR_CORE_PERF_GLOBAL_CTRL))
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) ||
+ msr_write_intercepted(vmx, MSR_CORE_PERF_GLOBAL_CTRL))
return;
if (!cpu_has_save_perf_global_ctrl()) {
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index d94b59140c452..4287982638b6e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9326,6 +9326,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
if (irqchip_in_kernel(kvm)) {
r = perf_create_mediated_pmu();
if (r) {
+ kvm->arch.mediated_pmu_caps = 0;
pr_warn_ratelimited(PERF_MEDIATED_PMU_MSG);
return r;
}
@@ -9334,6 +9335,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
kvm->arch.enable_pmu = false;
}
}
+
return 0;
}
|
| 3/2 | 2026/08/10 10:39 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 17419 output: 45 thoughts: 1330 Thoughts: **Analyzing the KVM Patch for AMD SVM PMU Virtualization** Okay, so I'm looking at this patch for KVM, specifically focusing on the AMD SVM (Secure Virtual Machine) side. It's adding support for PMC virtualization, which is essentially Performance Monitoring Counter virtualization. I see a pretty broad set of changes, so it's worth taking a deeper look. First, I see the usual suspects: new feature flags in `cpufeatures.h`, vendor-specific state functions declared, and capabilities being added to `kvm_host.h`. The core of the change seems to be in `svm.h`, where they're adding the `SVM_MISC2_ENABLE_V_PMC` flag and expanding the `vmcb_save_area` to include arrays for the PMCs themselves along with global status and control registers. This indicates that hardware switching is key, not just the usual software emulation or filtering. The CPUID exposure of `PERFCTR_VIRT` is expected, so the guest can know the feature is there. The real meat of the patch, from my perspective, is how they're handling the vendor state transitions in `pmu.c` and specifically how the SVM code in `svm/pmu.c` implements `amd_get_vendor_state`, `amd_set_vendor_state`, and `amd_pmu_reset`. This is where the AMD-specific PMU state management comes into play. The nested virtualization support in `svm/nested.c` is also going to be critical, especially with the addition of VPMC. Also, worth noting the ES (Encrypted State) interaction. I notice that Intel PMU code has also been touched. It's good to see that there is this kind of consistency in changes, as that makes code more maintainable. The patch seems to be introducing hardware-switched PMU state for guests. That's a big deal. Given the complexity and the hardware interaction, I need to see if the changes are properly guarded to make sure they are not accidentally enabled in non-AMD SVM configurations. Now, let's zero in on key symbols for further analysis and fuzzing. I've identified several functions of interest: * The vendor state functions: `kvm_pmu_get_vendor_state`, `kvm_pmu_set_vendor_state`, `amd_get_vendor_state`, `amd_set_vendor_state`, and `amd_pmu_reset`. * Functions related to nested virtualization: `nested_vmcb12_has_vpmc`, `enter_svm_guest_mode`, `nested_svm_vmexit`, and `svm_leave_nested`. * Global KVM PMU functions: `kvm_pmu_rdpmc`, `kvm_pmu_get_msr`, `kvm_pmu_set_msr`, `kvm_pmu_refresh`, `kvm_mediated_pmu_load`, and `kvm_mediated_pmu_put`. The MSR read/write and the refresh functions will be high-value. I want to check these functions more closely, especially the ones in `pmu.c` and `svm/nested.c`, since they seem central to the vendor state and nested virtualization aspects. Looks like `kvm_pmu_get_msr` and `kvm_pmu_set_msr` *have* been modified, along with the others in the global list, so that's good. I'll check all the modifications in the `pmu.c` in order to see how the vendor state is getting set. |
| 4/2 | 2026/08/10 10:39 | tool | grepper |
0mArgs:{
"Expression": "^int kvm_pmu_get_msr",
"PathPrefix": "arch/x86/kvm/pmu.c"
}Results: {
"Output": "arch/x86/kvm/pmu.c=887=static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)\n--\narch/x86/kvm/pmu.c-895-\narch/x86/kvm/pmu.c:896:int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\narch/x86/kvm/pmu.c-897-{\n"
} |
| 5/2 | 2026/08/10 10:39 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 19739 output: 45 thoughts: 34 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing KVM PMU MSR Access** Right, so `kvm_pmu_get_msr` is indeed global, and that immediately raises a few questions in my mind. Let me quickly pivot and take a look at `kvm_pmu_set_msr`. Understanding how reads are handled is important, but how writes are managed is also crucial for a comprehensive picture. I need to see if the write path shares the same global scope. My immediate focus is on potential race conditions and concurrency issues if both read and write MSR access rely on this global aspect. I want to see if the write operations are appropriately synchronized. I'm especially interested in whether any locking mechanisms are in place, and if so, how they relate to the global nature of this access. This is going to be key to understanding the performance characteristics and reliability of the PMU interactions within the KVM environment. |
| 6/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^int kvm_pmu_set_msr",
"PathPrefix": "arch/x86/kvm/pmu.c"
}Results: {
"Output": "arch/x86/kvm/pmu.c=896=int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n--\narch/x86/kvm/pmu.c-923-\narch/x86/kvm/pmu.c:924:int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\narch/x86/kvm/pmu.c-925-{\n"
} |
| 7/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 20180 output: 44 thoughts: 27 Thoughts: Okay, here's my attempt at summarizing those thoughts from a first-person, expert perspective, focusing on the assumed knowledge of a fellow expert: **Investigating KVM Mediated PMU Handling** Right, let's dive into this. The first thing that pops into my mind is a quick double-check on how KVM is handling the Programmed Monitoring Unit (PMU) load and put operations. I want to make sure the interactions with `kvm_mediated_pmu_load` and `kvm_mediated_pmu_put` are as efficient and correct as I expect. It's crucial we revisit these entry points, given the work we're doing. A quick sanity check of those two specific functions feels necessary before we get too deep down this path. I suspect there may be a corner case lurking somewhere, or perhaps a performance bottleneck that needs to be addressed. Ultimately, I need to confirm whether our mediated PMU handling still makes sense. |
| 8/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^void kvm_mediated_pmu_load",
"PathPrefix": "arch/x86/kvm/pmu.c"
}Results: {
"Output": "arch/x86/kvm/pmu.c=1384=static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/pmu.c-1409-\narch/x86/kvm/pmu.c:1410:void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-1411-{\n"
} |
| 9/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 20594 output: 44 thoughts: 12 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Delving into KVM-Mediated PMU Management** Alright, so the focus seems to be on a specific function: `kvm_mediated_pmu_put`. My mind immediately jumps to the implications within a KVM (Kernel-based Virtual Machine) environment, specifically concerning how we're managing Performance Monitoring Units (PMUs). Given the name, `put` likely refers to releasing or relinquishing control, or perhaps removing an allocation. This suggests we're dealing with a system where PMU resources are mediated, meaning there's some form of controlled sharing or virtualization of these hardware performance counters. The `kvm_` prefix indicates this mediation happens within the KVM framework. This makes me wonder about the specific architecture. Are we discussing Intel's PT (Processor Trace) capabilities, AMD's IBS (Instruction Based Sampling), or some other PMU feature that KVM is handling? Are we talking about a simple assignment of PMU counters to virtual machines, or something more complex like multiplexing or event filtering? The crucial aspect is the *how* and *when* this `put` operation is executed. Is this part of guest context switching? Host context switching? Is it triggered by specific events within the guest, or perhaps a host-initiated reclaim of resources? What synchronization primitives are involved? Given my experience, I'd anticipate locks and potentially RCU (Read-Copy-Update) mechanisms to ensure atomicity and prevent race conditions when manipulating the PMU state. I'd be looking closely for potential performance bottlenecks here, especially in highly contested PMU environments. Ultimately, I'd need to consider the impact on guest performance, and also the overhead on the host. We are, after all, aiming for a truly performant virtualization solution. |
| 10/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^void kvm_mediated_pmu_put",
"PathPrefix": "arch/x86/kvm/pmu.c"
}Results: {
"Output": "arch/x86/kvm/pmu.c=1446=static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/pmu.c-1474-\narch/x86/kvm/pmu.c:1475:void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-1476-{\n"
} |
| 11/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 21183 output: 44 thoughts: 15 Thoughts: Okay, let's dive into this. Here's my thought process on checking `kvm_pmu_rdpmc`: **Analyzing the `kvm_pmu_rdpmc` Function** Alright, so the task at hand is to scrutinize `kvm_pmu_rdpmc`. My initial line of thinking is, given my expertise, to immediately consider its context within the KVM framework. This function is likely responsible for interacting with Performance Monitoring Units (PMUs) within a virtualized environment. The core function is likely involved in reading performance counters from within a guest VM. The primary questions that leap to mind are: * **Security:** How is this function secured? Given that we're talking about reading potentially sensitive hardware performance data, access control is paramount. I need to understand what checks are in place to prevent malicious guests from accessing counters they shouldn't, potentially leaking information about the host or other VMs. Are there any known vulnerabilities related to PMU access in KVM? Have there been any recent fixes or patches addressing such concerns? * **Performance:** What is the performance impact of using `kvm_pmu_rdpmc`? Reading PMU counters is typically not a zero-cost operation, especially across a virtualization boundary. I'll need to consider how this function is optimized and how it fits into the overall performance profile of a virtual machine. Does it introduce significant overhead? Does it utilize any hardware virtualization features (e.g., vPMU) to mitigate overhead? * **Implementation:** How is it implemented under the hood? I need to understand the mechanism. Is it direct hardware access or mediated through some form of hypervisor abstraction? Does it use the standard `rdpmc` instruction? Are there any subtleties related to the differences in PMU behavior between the host and guest architectures? * **Context:** What are the various contexts from which this function can be invoked? Are there any preconditions which have to be met before the call to function is made? What are the implications if there is some error? Are there any side-effects to bear in mind? * **Error handling:** I'll want to review the error handling. How does this function deal with failures (e.g., invalid counter configuration, insufficient permissions, unavailable PMUs)? Does it return error codes, throw exceptions, or log messages? How robust is the error handling? Does it have any implications for reliability or security? I'll need to dig into the source code to get the answers to these questions. I'll probably look for any related documentation too, such as the KVM documentation, and any relevant mailing list discussions or bug reports, to get a handle on the current state and any known issues. I should also check commit history to see if there were any recent changes or fixes that might be relevant. My focus will be on understanding the security, performance and implementation of the code. |
| 12/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^int kvm_pmu_rdpmc",
"PathPrefix": "arch/x86/kvm/pmu.c"
}Results: {
"Output": "arch/x86/kvm/pmu.c=775=static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\n--\narch/x86/kvm/pmu.c-797-\narch/x86/kvm/pmu.c:798:int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)\narch/x86/kvm/pmu.c-799-{\n"
} |
| 13/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 22008 output: 43 thoughts: 13 Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind I'm speaking to a fellow expert: **Investigating `enter_svm_guest_mode`** Alright, time to dive in. Let's start by looking into the `enter_svm_guest_mode` function. Given the name, I'm immediately thinking this is the entry point for transitioning into an SVM guest execution context. I'll need to understand the initial setup, any parameter validation, the steps involved in configuring the processor for guest operation (like setting control registers, saving/restoring state, etc.), and the ultimate mechanism used to actually jump into the guest code. I'll be looking for any potential vulnerabilities or subtle bugs that might exist. Specifically, I'll be looking for any potential for an unexpected return, and how any associated exceptions will be handled. The devil is always in the details with this kind of low-level code, and I know from experience that even seemingly insignificant oversights can have big consequences. Let's see what we've got. |
| 14/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^int enter_svm_guest_mode",
"PathPrefix": "arch/x86/kvm/svm/nested.c"
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1041=static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)\n--\narch/x86/kvm/svm/nested.c-1052-\narch/x86/kvm/svm/nested.c:1053:int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)\narch/x86/kvm/svm/nested.c-1054-{\n"
} |
| 15/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 22451 output: 42 thoughts: 12 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Nested SVM VMEXIT Analysis** Alright, time to dig into `nested_svm_vmexit`. It's that critical juncture where the nested virtualization gets, well, nested. I need to be precise here. I want to trace the execution flow through this thing. Specifically, I'm thinking about the implications on the control flow. We have the guest OS, then the hypervisor, and then the outer hypervisor/host OS and if there are *multiple* layers of hypervisors, where does this code fall into the picture? What state information is being captured, restored, and switched? I'm anticipating potential issues with register preservation and context switching overhead. This whole thing has the potential to become a performance bottleneck if not implemented extremely carefully, so I want to know all the nuances. This particular structure could reveal the secrets to performance optimizations or expose potential security holes. I need to understand how the VM-Exit handler processes that structure, to verify whether the architecture's protections are sufficient. Let's see how well it's been handled with all the layers involved, and ensure the state is handled correctly. |
| 16/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^void nested_svm_vmexit",
"PathPrefix": "arch/x86/kvm/svm/nested.c"
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1275=static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1345-\narch/x86/kvm/svm/nested.c:1346:void nested_svm_vmexit(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-1347-{\n"
} |
| 17/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 22894 output: 41 thoughts: 11 Thoughts: Okay, here's my summarized thought process, presented as if I'm thinking it: **Checking `svm_leave_nested`** Alright, time to get to work. Let's see... the task at hand is definitely centered around the `svm_leave_nested` function. I need to refresh my memory on what that particular piece of code handles. It's obviously a crucial component of the current workflow. I need to quickly recall the context in which this function is utilized – is it related to cross-validation, hyperparameter tuning, model evaluation, or some combination of these? Where does it fit into the broader scheme? Let's quickly review its definition or any associated documentation to ensure I have a firm understanding. The goal is to fully grasp its role so I can contribute effectively to the task ahead. Let's dig in. |
| 18/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "^void svm_leave_nested",
"PathPrefix": "arch/x86/kvm/svm/nested.c"
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1540=void svm_free_nested(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1565-\narch/x86/kvm/svm/nested.c:1566:void svm_leave_nested(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1567-{\n"
} |
| 19/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 23253 output: 36 thoughts: 229 Thoughts: **Analyzing the AMD PMC Virtualization Patch** Okay, so this patch is all about adding support for AMD Performance Monitor Counter (PMC) virtualization within KVM. My mind immediately jumps to a few key symbols that will be at the heart of the implementation: `kvm_pmu_get_msr`, `kvm_pmu_set_msr`, `kvm_pmu_rdpmc`, and functions related to mediated PMU access like `kvm_mediated_pmu_load` and `kvm_mediated_pmu_put`. I'll need to understand how these integrate with the SVM (Secure Virtual Machine) environment, so `enter_svm_guest_mode`, `nested_svm_vmexit`, and `svm_leave_nested` are also critical. Now, let's look at the Kconfig. I need to figure out the build configurations. The code resides in `arch/x86/kvm/`, so I'll start there. The core configs are `KVM` and `KVM_AMD`. Anything new added? No, it doesn't look like any new configs were added. Right, so I'll also need to make sure that the typical PMU-related configurations are enabled - usually, `PERF_EVENTS`. The next step is to check if `KVM_AMD` has any specific dependencies in `arch/x86/kvm/Kconfig`. I'll need to verify that everything is set correctly for this AMD PMU virtualization to function properly. |
| 20/2 | 2026/08/10 10:40 | tool | grepper |
0mArgs:{
"Expression": "config KVM_AMD",
"PathPrefix": "arch/x86/kvm/Kconfig"
}Results: {
"Output": "arch/x86/kvm/Kconfig=134=config KVM_INTEL_TDX\n--\narch/x86/kvm/Kconfig-145-\narch/x86/kvm/Kconfig:146:config KVM_AMD\narch/x86/kvm/Kconfig-147-\ttristate \"KVM for AMD processors support\"\n--\narch/x86/kvm/Kconfig-155-\narch/x86/kvm/Kconfig:156:config KVM_AMD_SEV\narch/x86/kvm/Kconfig-157-\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n"
} |
| 21/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 24029 output: 39 thoughts: 12 Thoughts: Here's my take on the dependencies of `KVM_AMD`, thinking as a seasoned expert: **Dependencies of KVM_AMD: A Quick Mental Audit** Okay, time to get a handle on the `KVM_AMD` dependencies. First, I need to mentally walk through the layers. Obviously, this is going to rely on a solid foundation from the kernel itself; I'm assuming that the base KVM framework is in place. But within the AMD context, what specific AMD features or hardware capabilities are we leaning on? We're talking virtualization extensions, right? Things like nested paging, extended page tables (EPT), and any specific CPU features that are essential for AMD's Secure Virtual Machine (SVM) implementation. I also need to consider the interplay with the host's CPU features. Are there any particular microcode revisions or firmware requirements that `KVM_AMD` necessitates to function correctly and securely? That's a critical area, especially with the ever-evolving nature of AMD hardware. How does this fit in the general scope of security? Then there are drivers and modules. Are there any specific AMD-provided or community-developed drivers that `KVM_AMD` depends on for optimal performance or feature enablement? I need to be aware of how they might interact with other subsystems like the memory management unit or the interrupt controller. And let's not forget the build process and configuration options. What other kernel modules or build-time flags need to be enabled alongside `KVM_AMD` to ensure a fully functional and optimized virtualisation environment? What kind of kernel config dependencies might limit its usefulness on certain hardware, maybe on older AMD models? I'm picturing a mental matrix – dependencies across CPU, kernel, and any required firmware or drivers. Let's make sure nothing's getting overlooked. |
| 22/2 | 2026/08/10 10:40 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/Kconfig",
"FirstLine": 146,
"LineCount": 20
}Results: {
"Contents": " 146:\tconfig KVM_AMD\n 147:\t\ttristate \"KVM for AMD processors support\"\n 148:\t\tdepends on KVM \u0026\u0026 (CPU_SUP_AMD || CPU_SUP_HYGON)\n 149:\t\thelp\n 150:\t\t Provides support for KVM on AMD processors equipped with the AMD-V\n 151:\t\t (SVM) extensions.\n 152:\t\n 153:\t\t To compile this as a module, choose M here: the module\n 154:\t\t will be called kvm-amd.\n 155:\t\n 156:\tconfig KVM_AMD_SEV\n 157:\t\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n 158:\t\tdefault y\n 159:\t\tdepends on KVM_AMD \u0026\u0026 X86_64\n 160:\t\tdepends on CRYPTO_DEV_SP_PSP \u0026\u0026 !(KVM_AMD=y \u0026\u0026 CRYPTO_DEV_CCP_DD=m)\n 161:\t\tselect ARCH_HAS_CC_PLATFORM\n 162:\t\tselect KVM_GENERIC_MEMORY_ATTRIBUTES\n 163:\t\tselect HAVE_KVM_ARCH_GMEM_CONVERT\n 164:\t\tselect HAVE_KVM_ARCH_GMEM_RECLAIM\n 165:\t\tselect HAVE_KVM_ARCH_GMEM_INVALIDATE\n"
} |
| 23/2 | 2026/08/10 10:40 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 24818 output: 42 thoughts: 149 Thoughts: **My Assessment of the KVM Fuzzing Target** Alright, let's take a look at this. The core configurations I need to verify are definitely `KVM` and `KVM_AMD`. They seem to be the foundation for the fuzzing, and based on the provided changes, this looks like a worthwhile target for deeper investigation. I need to focus my efforts on these critical symbols: `kvm_pmu_get_msr`, `kvm_pmu_set_msr`, `kvm_pmu_rdpmc`, `kvm_mediated_pmu_load`, `kvm_mediated_pmu_put`, `enter_svm_guest_mode`, `nested_svm_vmexit`, and `svm_leave_nested`. These are obviously the key areas where potential vulnerabilities might reside. They cover various PMU interactions and the critical SVM/nested SVM transition points. I'll need to develop fuzzing strategies specifically tailored to expose issues within these functions. Before I get too deep into the core functions, I should do a quick spot check on `kvm_pmu_refresh`. It's possible some subtle bug might live there. I always want to make sure I am starting from the most complete picture. |
| 24/2 | 2026/08/10 10:41 | tool | grepper |
0mArgs:{
"Expression": "^void kvm_pmu_refresh",
"PathPrefix": "arch/x86/kvm/pmu.c"
}Results: {
"Output": "arch/x86/kvm/pmu.c=1002=static void kvm_pmu_reset(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/pmu.c-1033- */\narch/x86/kvm/pmu.c:1034:void kvm_pmu_refresh(struct kvm_vcpu *vcpu)\narch/x86/kvm/pmu.c-1035-{\n"
} |
| 25/2 | 2026/08/10 10:41 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 25410 output: 199 thoughts: 374 Thoughts: **My Analysis of KVM PMU Focus Symbols** Okay, so I'm thinking about the key KVM functions related to Performance Monitoring Units (PMUs) that are worth focusing on. `kvm_pmu_refresh` is definitely a good starting point – it's a core function, so I'll include that. Now, let's look at `kvm_pmu_rdpmc`. This one is interesting; it's called during the guest's execution of the `rdpmc` instruction, so it's clearly involved in PMU access. I need to verify that it's not a mere wrapper and that it does contain some actual logic. Then there are `kvm_pmu_get_msr` and `kvm_pmu_set_msr`, called when the guest reads and writes MSRs. Those are crucial. Additionally, `kvm_mediated_pmu_load` and `kvm_mediated_pmu_put` handle vCPU context switches, so they are a must. And since we're diving deep, let's not forget the nested virtualization transitions. The functions `enter_svm_guest_mode`, `nested_svm_vmexit`, and `svm_leave_nested` are relevant in nested virtualization environments. So, the definitive list to include will be: * `kvm_pmu_get_msr` * `kvm_pmu_set_msr` * `kvm_pmu_rdpmc` * `kvm_pmu_refresh` * `kvm_mediated_pmu_load` * `kvm_mediated_pmu_put` * `enter_svm_guest_mode` * `nested_svm_vmexit` * `svm_leave_nested` Regarding configurations, `KVM` and `KVM_AMD` are essential, that should be enough. I'll double-check whether we need `PERF_EVENTS` enabled, but I suspect `KVM` and `KVM_AMD` cover it. Alright, that covers the essentials. Time to `set-results`. |
| 26/2 | 2026/08/10 10:41 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"KVM",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_get_msr",
"kvm_pmu_set_msr",
"kvm_pmu_rdpmc",
"kvm_pmu_refresh",
"kvm_mediated_pmu_load",
"kvm_mediated_pmu_put",
"enter_svm_guest_mode",
"nested_svm_vmexit",
"svm_leave_nested"
],
"Reasoning": "The patch introduces hardware-switched PMU state for AMD SVM guests, adding support for PMC virtualization. This involves significant functional changes to KVM's PMU handling, MSR virtualization, and nested virtualization transitions. These changes are reachable from guest execution and should be heavily fuzzed.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"KVM",
"KVM_AMD"
],
"FocusSymbols": [
"kvm_pmu_get_msr",
"kvm_pmu_set_msr",
"kvm_pmu_rdpmc",
"kvm_pmu_refresh",
"kvm_mediated_pmu_load",
"kvm_mediated_pmu_put",
"enter_svm_guest_mode",
"nested_svm_vmexit",
"svm_leave_nested"
],
"Reasoning": "The patch introduces hardware-switched PMU state for AMD SVM guests, adding support for PMC virtualization. This involves significant functional changes to KVM's PMU handling, MSR virtualization, and nested virtualization transitions. These changes are reachable from guest execution and should be heavily fuzzed.",
"WorthFuzzing": true
} |
| 27/1 | 2026/08/10 10:41 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 28/2 | 2026/08/10 10:41 | agent | kmsan-evaluator |
1mModel:best-expensive Args: null Results: {
"KMSANReasoning": "The patch introduces PMC (Performance Monitoring Counter) virtualization support for AMD SVM in KVM. It modifies the `vmcb_save_area` structure to include a `pmc` array and global control/status fields, which replaces previously reserved bytes. The `vmcb_save_area` is copied to userspace via the `KVM_GET_NESTED_STATE` ioctl. However, the underlying VMCB pages (e.g., `vmcb01` and `vmcb02`) are allocated using `snp_safe_alloc_page()`, which explicitly uses `__GFP_ZERO` to zero-initialize the entire page. Furthermore, `struct vmcb_save_area` is marked as `__packed`, preventing any implicit padding bytes. The patch also adds logic to read and write these fields, but since the memory is zero-allocated and explicitly managed, there is no risk of uninitialized memory being leaked to userspace or used in conditional branches. Standard KASAN and other bug detectors are sufficient to catch any potential memory corruption or out-of-bounds access bugs introduced by these changes.",
"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: For your convenience, here is the diff of the changes:
commit b1b55dd3c5a9465f0f9a50de4b59854f8eb681d2
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Aug 10 10:39:40 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
index 30bb8fdf2a8b5..b551ba381b6e2 100644
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -384,6 +384,7 @@
#define X86_FEATURE_VMCBCLEAN (15*32+ 5) /* "vmcb_clean" VMCB clean bits support */
#define X86_FEATURE_FLUSHBYASID (15*32+ 6) /* "flushbyasid" Flush-by-ASID support */
#define X86_FEATURE_DECODEASSISTS (15*32+ 7) /* "decodeassists" Decode Assists support */
+#define X86_FEATURE_PERFCTR_VIRT (15*32+ 8) /* PMC virtualization support */
#define X86_FEATURE_PAUSEFILTER (15*32+10) /* "pausefilter" Filtered pause intercept */
#define X86_FEATURE_PFTHRESHOLD (15*32+12) /* "pfthreshold" Pause filter threshold */
#define X86_FEATURE_AVIC (15*32+13) /* "avic" Virtual Interrupt Controller */
diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h
index 4a223c2793e3f..4a617162d4c08 100644
--- a/arch/x86/include/asm/kvm-x86-pmu-ops.h
+++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h
@@ -29,6 +29,9 @@ KVM_X86_PMU_OP_OPTIONAL_RET0(pmc_is_disabled_in_current_mode)
KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl)
KVM_X86_PMU_OP(mediated_load)
KVM_X86_PMU_OP(mediated_put)
+
+KVM_X86_PMU_OP_OPTIONAL_RET0(get_vendor_state)
+KVM_X86_PMU_OP_OPTIONAL_RET0(set_vendor_state)
#endif
#undef KVM_X86_PMU_OP
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 283847619ff88..338df11e2a4df 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -715,6 +715,16 @@ enum kvm_only_cpuid_leafs {
NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
};
+/*
+ * Track how the guest PMU state is saved and restored, as this can be done
+ * either by software or, if capable, by hardware. When hardware manages the
+ * guest PMU state, the state resides in a vendor save area.
+ */
+#define KVM_MEDIATED_PMU_CAP_HW_SWITCHED BIT(0)
+#define KVM_MEDIATED_PMU_CAP_HW_FILTERED BIT(1)
+#define KVM_MEDIATED_PMU_CAP_VALID (KVM_MEDIATED_PMU_CAP_HW_SWITCHED | \
+ KVM_MEDIATED_PMU_CAP_HW_FILTERED)
+
struct kvm_vcpu_arch {
/*
* rip and regs accesses must go through
@@ -1284,6 +1294,7 @@ struct kvm_arch {
bool bus_lock_detection_enabled;
bool enable_pmu;
bool created_mediated_pmu;
+ u32 mediated_pmu_caps;
u32 notify_window;
u32 notify_vmexit_flags;
diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h
index aa63431ba92c3..e1c8a333e86fa 100644
--- a/arch/x86/include/asm/svm.h
+++ b/arch/x86/include/asm/svm.h
@@ -247,6 +247,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
#define SVM_MISC2_ENABLE_V_LBR BIT_ULL(0)
#define SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE BIT_ULL(1)
+#define SVM_MISC2_ENABLE_V_PMC BIT_ULL(3)
#define SVM_TSC_RATIO_RSVD 0xffffff0000000000ULL
#define SVM_TSC_RATIO_MIN 0x0000000000000001ULL
@@ -338,7 +339,12 @@ struct vmcb_save_area {
u8 cpl;
u8 reserved_0xcc[4];
u64 efer;
- u8 reserved_0xd8[112];
+ u8 reserved_0xd8[8];
+ struct {
+ u64 perf_ctl;
+ u64 perf_ctr;
+ } __packed pmc[6];
+ u8 reserved_0x140[8];
u64 cr4;
u64 cr3;
u64 cr0;
@@ -346,7 +352,9 @@ struct vmcb_save_area {
u64 dr6;
u64 rflags;
u64 rip;
- u8 reserved_0x180[88];
+ u8 reserved_0x180[72];
+ u64 perf_cntr_global_status;
+ u64 perf_cntr_global_control;
u64 rsp;
u64 s_cet;
u64 ssp;
@@ -575,6 +583,7 @@ static inline void __unused_size_checks(void)
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xa0);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xcc);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xd8);
+ BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x140);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x180);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x248);
BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x298);
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index ddb022cb203a2..ce4f984bc6e16 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1217,6 +1217,7 @@ void kvm_initialize_cpu_caps(void)
VENDOR_F(VGIF),
VENDOR_F(VNMI),
VENDOR_F(SVME_ADDR_CHK),
+ VENDOR_F(PERFCTR_VIRT),
);
kvm_cpu_cap_init(CPUID_8000_001F_EAX,
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index a7d60c8785cd4..4c918aa6d6f37 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -130,6 +130,24 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)
#undef __KVM_X86_PMU_OP
}
+static void kvm_pmu_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
+ if (kvm_pmu_call(get_vendor_state)(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
+}
+
+static void kvm_pmu_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
+ if (kvm_pmu_call(set_vendor_state)(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
+}
+
void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
{
bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
@@ -166,8 +184,11 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
!pmu_ops->is_mediated_pmu_supported(&kvm_host_pmu))
enable_mediated_pmu = false;
- if (!enable_mediated_pmu)
+ if (!enable_mediated_pmu) {
pmu_ops->write_global_ctrl = NULL;
+ pmu_ops->get_vendor_state = NULL;
+ pmu_ops->set_vendor_state = NULL;
+ }
if (!enable_pmu) {
memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));
@@ -197,6 +218,27 @@ void kvm_handle_guest_mediated_pmi(void)
kvm_make_request(KVM_REQ_PMI, vcpu);
}
+static __always_inline u32 fixed_counter_msr(u32 idx)
+{
+ return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 gp_counter_msr(u32 idx)
+{
+ return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 gp_eventsel_msr(u32 idx)
+{
+ return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 pmc_counter_msr(struct kvm_pmc *pmc)
+{
+ return pmc_is_gp(pmc) ? gp_counter_msr(pmc->idx) :
+ fixed_counter_msr(pmc->idx - KVM_FIXED_PMC_BASE_IDX);
+}
+
static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)
{
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
@@ -550,18 +592,23 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)
{
bool allowed = pmc_is_locally_enabled(pmc) && pmc_is_event_allowed(pmc);
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
+ struct kvm_vcpu *vcpu = pmc->vcpu;
if (pmc_is_gp(pmc)) {
pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
if (allowed)
pmc->eventsel_hw |= pmc->eventsel &
ARCH_PERFMON_EVENTSEL_ENABLE;
+
+ kvm_pmu_set_vendor_state(vcpu, gp_eventsel_msr(pmc->idx));
} else {
u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX, 0xf);
pmu->fixed_ctr_ctrl_hw &= ~mask;
if (allowed)
pmu->fixed_ctr_ctrl_hw |= pmu->fixed_ctr_ctrl & mask;
+
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.FIXED_COUNTER_CTRL);
}
}
@@ -769,6 +816,8 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))
return 1;
+ kvm_pmu_get_vendor_state(vcpu, pmc_counter_msr(pmc));
+
*data = pmc_read_counter(pmc) & mask;
return 0;
}
@@ -852,10 +901,12 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
switch (msr) {
case MSR_CORE_PERF_GLOBAL_STATUS:
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
msr_info->data = pmu->global_status;
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
case MSR_CORE_PERF_GLOBAL_CTRL:
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);
msr_info->data = pmu->global_ctrl;
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
@@ -895,6 +946,7 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
return 1;
pmu->global_status = data;
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
data &= ~pmu->global_ctrl_rsvd;
@@ -914,6 +966,8 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
*/
if (kvm_vcpu_has_mediated_pmu(vcpu))
kvm_pmu_call(write_global_ctrl)(data);
+
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);
break;
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
/*
@@ -924,12 +978,18 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
return 1;
fallthrough;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
- if (!msr_info->host_initiated)
+ if (!msr_info->host_initiated) {
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
pmu->global_status &= ~data;
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+ }
break;
case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
- if (!msr_info->host_initiated)
+ if (!msr_info->host_initiated) {
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
pmu->global_status |= data & ~pmu->global_status_rsvd;
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+ }
break;
default:
kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
@@ -1017,6 +1077,8 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
if (kvm_vcpu_has_mediated_pmu(vcpu))
kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_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);
@@ -1139,6 +1201,9 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,
if (bitmap_empty(event_pmcs, X86_PMC_IDX_MAX))
return;
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL);
+ kvm_pmu_get_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+
if (!kvm_pmu_has_perf_global_ctrl(pmu))
bitmap_copy(bitmap, event_pmcs, X86_PMC_IDX_MAX);
else if (!bitmap_and(bitmap, event_pmcs,
@@ -1147,11 +1212,17 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,
idx = srcu_read_lock(&vcpu->kvm->srcu);
kvm_for_each_pmc(pmu, pmc, i, bitmap) {
+ kvm_pmu_get_vendor_state(vcpu, pmc_counter_msr(pmc));
+
if (!pmc_is_event_allowed(pmc) || !cpl_is_matched(pmc))
continue;
kvm_pmu_incr_counter(pmc);
+ kvm_pmu_set_vendor_state(vcpu, pmc_counter_msr(pmc));
}
+
+ kvm_pmu_set_vendor_state(vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS);
+
srcu_read_unlock(&vcpu->kvm->srcu, idx);
}
@@ -1310,21 +1381,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)
return r;
}
-static __always_inline u32 fixed_counter_msr(u32 idx)
-{
- return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
-static __always_inline u32 gp_counter_msr(u32 idx)
-{
- return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
-static __always_inline u32 gp_eventsel_msr(u32 idx)
-{
- return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -1361,6 +1417,10 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
perf_load_guest_context();
+ /* Guest PMU state is restored by hardware */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
/*
* Explicitly clear PERF_GLOBAL_CTRL, as "loading" the guest's context
* disables all individual counters (if any were enabled), but doesn't
@@ -1420,6 +1480,12 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)
lockdep_assert_irqs_disabled();
+ /* Guest PMU state is saved by hardware */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) {
+ perf_put_guest_context();
+ return;
+ }
+
/*
* Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's
* atomically cleared on VM-Exit, i.e. doesn't need to be clear here.
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f48..42d33072ece90 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -42,14 +42,18 @@ struct kvm_pmu_ops {
void (*mediated_load)(struct kvm_vcpu *vcpu);
void (*mediated_put)(struct kvm_vcpu *vcpu);
void (*write_global_ctrl)(u64 global_ctrl);
+ int (*get_vendor_state)(struct kvm_vcpu *vcpu, u32 msr);
+ int (*set_vendor_state)(struct kvm_vcpu *vcpu, u32 msr);
const u64 EVENTSEL_EVENT;
const int MAX_NR_GP_COUNTERS;
const int MIN_NR_GP_COUNTERS;
const u32 PERF_GLOBAL_CTRL;
+ const u32 PERF_GLOBAL_STATUS;
const u32 GP_EVENTSEL_BASE;
const u32 GP_COUNTER_BASE;
+ const u32 FIXED_COUNTER_CTRL;
const u32 FIXED_COUNTER_BASE;
const u32 MSR_STRIDE;
};
@@ -88,6 +92,31 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
return enable_mediated_pmu && vcpu_to_pmu(vcpu)->version;
}
+static inline bool kvm_vcpu_has_mediated_pmu_caps(struct kvm_vcpu *vcpu, u32 caps)
+{
+ return kvm_vcpu_has_mediated_pmu(vcpu) &&
+ !!(vcpu->kvm->arch.mediated_pmu_caps & caps);
+}
+
+#define kvm_set_mediated_pmu_caps(kvm, caps) \
+do { \
+ BUILD_BUG_ON(!__builtin_constant_p(caps) || \
+ ((caps) & ~KVM_MEDIATED_PMU_CAP_VALID)); \
+ (kvm)->arch.mediated_pmu_caps |= (caps); \
+} while (0)
+
+#define kvm_clr_mediated_pmu_caps(kvm, caps) \
+do { \
+ BUILD_BUG_ON(!__builtin_constant_p(caps) || \
+ ((caps) & ~KVM_MEDIATED_PMU_CAP_VALID)); \
+ (kvm)->arch.mediated_pmu_caps &= ~(caps); \
+} while (0)
+
+static inline void kvm_pmu_warn_vendor_state(u32 msr)
+{
+ WARN_ONCE(1, "accessing unsupported vendor save slot for MSR 0x%x\n", msr);
+}
+
/*
* 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
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 73f37b050d0a0..a9f9caeb78406 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -605,6 +605,7 @@ static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
to->g_pat = from->g_pat;
svm_copy_lbrs(to, from);
+ svm_copy_pmcs(to, from);
}
void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
@@ -741,6 +742,17 @@ static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vcpu)
(to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR);
}
+static bool nested_vmcb12_has_vpmc(struct kvm_vcpu *vcpu)
+{
+ /*
+ * Since nested AVIC is not supported, L2 PMIs can only be delivered
+ * via VNMI, so make it a hard requirement.
+ */
+ return guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_VIRT) &&
+ nested_vnmi_enabled(to_svm(vcpu)) &&
+ (to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_PMC);
+}
+
static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
{
struct vmcb_ctrl_area_cached *control = &svm->nested.ctl;
@@ -823,6 +835,11 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
}
vmcb_mark_dirty(vmcb02, VMCB_LBR);
svm_update_lbrv(&svm->vcpu);
+
+ if (nested_vmcb12_has_vpmc(vcpu))
+ svm_copy_pmcs(&vmcb02->save, save);
+ else if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ svm_copy_pmcs(&vmcb02->save, &vmcb01->save);
}
static inline bool is_evtinj_soft(u32 evtinj)
@@ -863,7 +880,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
/* Enter Guest-Mode */
enter_guest_mode(vcpu);
- svm_pmu_handle_nested_transition(svm);
/*
* Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,
@@ -986,6 +1002,10 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
/* SVM_MISC2_ENABLE_V_LBR is controlled by svm_update_lbrv() */
+ /* L0 uses hardware-switched mode for both vmcb01 and vmcb02 */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ vmcb02->control.misc_ctl2 |= SVM_MISC2_ENABLE_V_PMC;
+
if (!nested_vmcb_needs_vls_intercept(svm))
vmcb02->control.misc_ctl2 |= SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE;
@@ -1065,6 +1085,9 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)
nested_vmcb02_prepare_control(svm);
nested_vmcb02_prepare_save(svm);
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ svm_pmu_handle_nested_transition(svm);
+
ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
nested_npt_enabled(svm), from_vmrun);
if (ret)
@@ -1228,6 +1251,9 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
svm_copy_lbrs(to_save, from_save);
to_save->dbgctl &= ~DEBUGCTL_RESERVED_BITS;
}
+
+ if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_VIRT))
+ svm_copy_pmcs(to_save, from_save);
}
void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
@@ -1300,6 +1326,9 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)
if (nested_vmcb12_has_lbrv(vcpu))
svm_copy_lbrs(&vmcb12->save, &vmcb02->save);
+ if (nested_vmcb12_has_vpmc(vcpu))
+ svm_copy_pmcs(&vmcb12->save, &vmcb02->save);
+
vmcb12->control.event_inj = 0;
vmcb12->control.event_inj_err = 0;
vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
@@ -1325,7 +1354,9 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
/* Exit Guest-Mode */
leave_guest_mode(vcpu);
- svm_pmu_handle_nested_transition(svm);
+
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ svm_pmu_handle_nested_transition(svm);
svm->nested.vmcb12_gpa = 0;
@@ -1381,6 +1412,10 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
svm_update_lbrv(vcpu);
+ if (!nested_vmcb12_has_vpmc(vcpu) &&
+ kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ svm_copy_pmcs(&vmcb01->save, &vmcb02->save);
+
if (vnmi) {
if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK)
vmcb01->control.int_ctl |= V_NMI_BLOCKING_MASK;
@@ -1545,7 +1580,8 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
* into PMU state from arbitrary contexts (e.g. to avoid using
* stale state).
*/
- __svm_pmu_handle_nested_transition(svm, true);
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ __svm_pmu_handle_nested_transition(svm, true);
svm_switch_vmcb(svm, &svm->vmcb01);
@@ -2074,6 +2110,9 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
nested_vmcb02_prepare_control(svm);
+ if (!nested_vmcb12_has_vpmc(vcpu))
+ svm_pmu_handle_nested_transition(svm);
+
/*
* Any previously restored state (e.g. KVM_SET_SREGS) would mark fields
* dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7ac..7d3a151b6cdee 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -125,6 +125,89 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
return amd_msr_idx_to_pmc(vcpu, msr);
}
+static u32 amd_pmu_adjust_msr_idx(struct kvm_vcpu *vcpu, u32 msr)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+ if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE) &&
+ msr >= MSR_F15H_PERF_CTL0 &&
+ msr <= MSR_F15H_PERF_CTR0 + 2 * pmu->nr_arch_gp_counters)
+ msr = ((msr & 0x1) ? MSR_K7_PERFCTR0 : MSR_K7_EVNTSEL0) +
+ ((msr - MSR_F15H_PERF_CTL0) / 2);
+
+ return msr;
+}
+
+static int amd_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ struct kvm_pmc *pmc;
+
+ /* MSR_PERF_CNTR_GLOBAL_* */
+ switch (msr) {
+ case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+ pmu->global_status = save->perf_cntr_global_status;
+ return 0;
+ case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+ pmu->global_ctrl = save->perf_cntr_global_control;
+ return 0;
+ }
+
+ msr = amd_pmu_adjust_msr_idx(vcpu, msr);
+
+ /* MSR_PERFCTRn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
+ if (pmc) {
+ pmc->counter = save->pmc[pmc->idx].perf_ctr;
+ return 0;
+ }
+
+ /* MSR_EVNTSELn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
+ if (pmc) {
+ pmc->eventsel_hw = save->pmc[pmc->idx].perf_ctl;
+ return 0;
+ }
+
+ return 1;
+}
+
+static int amd_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr)
+{
+ struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ struct kvm_pmc *pmc;
+
+ /* MSR_PERF_CNTR_GLOBAL_* */
+ switch (msr) {
+ case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+ save->perf_cntr_global_status = pmu->global_status;
+ return 0;
+ case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+ save->perf_cntr_global_control = pmu->global_ctrl;
+ return 0;
+ }
+
+ msr = amd_pmu_adjust_msr_idx(vcpu, msr);
+
+ /* MSR_PERFCTRn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
+ if (pmc) {
+ save->pmc[pmc->idx].perf_ctr = pmc->counter & pmc_bitmask(pmc);
+ return 0;
+ }
+
+ /* MSR_EVNTSELn */
+ pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
+ if (pmc) {
+ save->pmc[pmc->idx].perf_ctl = pmc->eventsel_hw;
+ return 0;
+ }
+
+ return 1;
+}
+
static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -134,6 +217,10 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
/* MSR_PERFCTRn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
+ if (msr_info->host_initiated &&
+ kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) &&
+ amd_get_vendor_state(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
msr_info->data = pmc_read_counter(pmc);
return 0;
}
@@ -158,6 +245,10 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
pmc_write_counter(pmc, data);
+ if (msr_info->host_initiated &&
+ kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) &&
+ amd_set_vendor_state(vcpu, msr))
+ kvm_pmu_warn_vendor_state(msr);
return 0;
}
/* MSR_EVNTSELn */
@@ -174,6 +265,9 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
else
__clear_bit(pmc->idx, pmu->pmc_has_mode_specific_enables);
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ pmc->eventsel_hw = data;
+
kvm_pmu_request_counter_reprogram(pmc);
}
return 0;
@@ -239,6 +333,24 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu)
}
}
+static void amd_pmu_reset(struct kvm_vcpu *vcpu)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+ struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
+ int i;
+
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ return;
+
+ for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ save->pmc[i].perf_ctl = 0;
+ save->pmc[i].perf_ctr = 0;
+ }
+
+ save->perf_cntr_global_control = 0;
+ save->perf_cntr_global_status = 0;
+}
+
static bool amd_pmu_is_mediated_pmu_supported(struct x86_pmu_capability *host_pmu)
{
return host_pmu->version >= 2;
@@ -311,18 +423,23 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {
.refresh = amd_pmu_refresh,
.init = amd_pmu_init,
.pmc_is_disabled_in_current_mode = amd_pmc_is_disabled_in_current_mode,
+ .reset = amd_pmu_reset,
.is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported,
.mediated_load = amd_mediated_pmu_load,
.mediated_put = amd_mediated_pmu_put,
+ .get_vendor_state = amd_get_vendor_state,
+ .set_vendor_state = amd_set_vendor_state,
.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,
.MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,
.MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,
.PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL,
+ .PERF_GLOBAL_STATUS = MSR_AMD64_PERF_CNTR_GLOBAL_STATUS,
.GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0,
.GP_COUNTER_BASE = MSR_F15H_PERF_CTR0,
+ .FIXED_COUNTER_CTRL = 0,
.FIXED_COUNTER_BASE = 0,
.MSR_STRIDE = 2,
};
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index fcb41dfde4c02..8e8cd4feb16a0 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -534,6 +534,9 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,
if (snp_active)
sev->vmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE;
+ if (es_active)
+ kvm_clr_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED);
+
ret = sev_asid_new(sev, vm_type);
if (ret)
goto e_no_asid;
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 9d607b98bd067..2023d38bba634 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -179,6 +179,9 @@ module_param(vnmi, bool, 0444);
module_param(enable_mediated_pmu, bool, 0444);
+bool vpmc = true;
+module_param(vpmc, bool, 0444);
+
static bool __ro_after_init svm_gp_erratum_intercept = true;
static u8 rsm_ins_bytes[] = "\x0f\xaa";
@@ -1267,6 +1270,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
svm_hv_init_vmcb(vmcb);
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ control->misc_ctl2 |= SVM_MISC2_ENABLE_V_PMC;
+
kvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu);
vmcb_mark_all_dirty(vmcb);
@@ -3563,6 +3569,30 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)
"excp_from:", save->last_excp_from,
"excp_to:", save->last_excp_to);
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) {
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl0:", save->pmc[0].perf_ctl,
+ "perf_ctr0:", save->pmc[0].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl1:", save->pmc[1].perf_ctl,
+ "perf_ctr1:", save->pmc[1].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl2:", save->pmc[2].perf_ctl,
+ "perf_ctr2:", save->pmc[2].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl3:", save->pmc[3].perf_ctl,
+ "perf_ctr3:", save->pmc[3].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl4:", save->pmc[4].perf_ctl,
+ "perf_ctr4:", save->pmc[4].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_ctl5:", save->pmc[5].perf_ctl,
+ "perf_ctr5:", save->pmc[5].perf_ctr);
+ pr_err("%-15s %016llx %-13s %016llx\n",
+ "perf_cntr_global_control:", save->perf_cntr_global_control,
+ "perf_cntr_global_status:", save->perf_cntr_global_status);
+ }
+
if (is_sev_es_guest(vcpu)) {
struct sev_es_save_area *vmsa = (struct sev_es_save_area *)save;
@@ -4452,6 +4482,14 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, unsigned enter_fl
amd_clear_divider();
+ /*
+ * On #VMEXIT, PerfCntrGlobalCtl goes back to its reset state since
+ * its save slot is of Swap Type C. All enable bits are set but PMC
+ * virtualization requires them to be cleared before VMRUN.
+ */
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))
+ wrmsrq(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0);
+
if (is_sev_es_guest(vcpu))
__svm_sev_es_vcpu_run(svm, enter_flags,
sev_es_host_save_area(sd));
@@ -4618,7 +4656,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
kvm_clear_available_registers(vcpu, SVM_REGS_LAZY_LOAD_SET);
- if (!msr_write_intercepted(svm, MSR_AMD64_PERF_CNTR_GLOBAL_CTL))
+ if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) &&
+ !msr_write_intercepted(svm, MSR_AMD64_PERF_CNTR_GLOBAL_CTL))
rdmsrq(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, vcpu_to_pmu(vcpu)->global_ctrl);
trace_kvm_exit(vcpu, KVM_ISA_SVM);
@@ -5288,6 +5327,9 @@ static void svm_vm_destroy(struct kvm *kvm)
static int svm_vm_init(struct kvm *kvm)
{
+ if (vpmc)
+ kvm_set_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED);
+
sev_vm_init(kvm);
if (!pause_filter_count || !pause_filter_thresh)
@@ -5541,6 +5583,10 @@ static __init void svm_set_cpu_caps(void)
if (vnmi)
kvm_cpu_cap_set(X86_FEATURE_VNMI);
+ /* Nested AVIC is not supported, so VNMI must be enabled */
+ if (vpmc && vnmi)
+ kvm_cpu_cap_set(X86_FEATURE_PERFCTR_VIRT);
+
/* Nested VM can receive #VMEXIT instead of triggering #GP */
kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
}
@@ -5734,6 +5780,16 @@ static __init int svm_hardware_setup(void)
if (!enable_pmu)
pr_info("PMU virtualization is disabled\n");
+ /*
+ * PMC virtualization does not raise host PMIs that need to be injected.
+ * Instead, it requires VNMI or AVIC for guest PMI delivery. AVIC can,
+ * however, get inhibited so make VNMI the hard requirement.
+ */
+ vpmc = vpmc && vnmi && enable_mediated_pmu &&
+ cpu_feature_enabled(X86_FEATURE_PERFCTR_VIRT);
+ if (vpmc)
+ pr_info("PMC virtualization supported\n");
+
svm_set_cpu_caps();
kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED;
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 66b44b54608e8..3db6b73e81634 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -53,6 +53,7 @@ extern int vgif;
extern bool intercept_smi;
extern bool vnmi;
extern int lbrv;
+extern bool vpmc;
extern int tsc_aux_uret_slot __ro_after_init;
@@ -174,6 +175,12 @@ struct vmcb_save_area_cached {
u64 br_to;
u64 last_excp_from;
u64 last_excp_to;
+ struct {
+ u64 perf_ctl;
+ u64 perf_ctr;
+ } pmc[6];
+ u64 perf_cntr_global_status;
+ u64 perf_cntr_global_control;
};
struct vmcb_ctrl_area_cached {
@@ -805,6 +812,18 @@ do { \
(to)->last_excp_to = (from)->last_excp_to; \
} while (0)
+#define svm_copy_pmcs(to, from) \
+do { \
+ int i; \
+ \
+ (to)->perf_cntr_global_control = (from)->perf_cntr_global_control; \
+ (to)->perf_cntr_global_status = (from)->perf_cntr_global_status; \
+ for (i = 0; i < kvm_pmu_cap.num_counters_gp; i++) { \
+ (to)->pmc[i].perf_ctl = (from)->pmc[i].perf_ctl; \
+ (to)->pmc[i].perf_ctr = (from)->pmc[i].perf_ctr; \
+ } \
+} while (0)
+
void svm_vcpu_free_msrpm(void *msrpm);
void svm_enable_lbrv(struct kvm_vcpu *vcpu);
void svm_update_lbrv(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index bfa8612fb4508..d0723814410e7 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -853,8 +853,10 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = {
.MIN_NR_GP_COUNTERS = 1,
.PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL,
+ .PERF_GLOBAL_STATUS = MSR_CORE_PERF_GLOBAL_STATUS,
.GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0,
.GP_COUNTER_BASE = MSR_IA32_PMC0,
+ .FIXED_COUNTER_CTRL = MSR_CORE_PERF_FIXED_CTR_CTRL,
.FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,
.MSR_STRIDE = 1,
};
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index e3bfe6aca1a0e..7d8c032c1897e 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7383,7 +7383,8 @@ static void vmx_refresh_guest_perf_global_control(struct kvm_vcpu *vcpu)
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
struct vcpu_vmx *vmx = to_vmx(vcpu);
- if (msr_write_intercepted(vmx, MSR_CORE_PERF_GLOBAL_CTRL))
+ if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED) ||
+ msr_write_intercepted(vmx, MSR_CORE_PERF_GLOBAL_CTRL))
return;
if (!cpu_has_save_perf_global_ctrl()) {
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index d94b59140c452..4287982638b6e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9326,6 +9326,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
if (irqchip_in_kernel(kvm)) {
r = perf_create_mediated_pmu();
if (r) {
+ kvm->arch.mediated_pmu_caps = 0;
pr_warn_ratelimited(PERF_MEDIATED_PMU_MSG);
return r;
}
@@ -9334,6 +9335,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
kvm->arch.enable_pmu = false;
}
}
+
return 0;
}
|
| 29/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 17416 output: 40 thoughts: 731 Thoughts: **PMC Virtualization for AMD SVM in KVM: A Deep Dive** Okay, so this patch series is tackling Performance Monitoring Counter (PMC) virtualization for AMD's Secure Virtual Machine (SVM) within KVM. My first thought is: finally! We've needed this for a while to allow better performance analysis in nested virtualization scenarios. It's adding `X86_FEATURE_PERFCTR_VIRT`, which is a key flag, but the core of it seems to be centered around modifying the VMCB (Virtual Machine Control Block) save area. Specifically, the addition of a `pmc` array ( `perf_ctl`, `perf_ctr` ) and the inclusion of `perf_cntr_global_status` and `perf_cntr_global_control` within the `vmcb_save_area` structure is where my attention is drawn. This means these registers, critical for PMC functionality, are being carefully managed across VM exits and entries. I need to understand the implications of this. I'm wondering if this will cause compatibility problems. The introduction of `get_vendor_state` and `set_vendor_state` into the `kvm_pmu_ops` structure is another important change. It's allowing KVM to interact with these new PMC state elements. This suggests a dedicated mechanism for handling vendor-specific details related to PMCs within the SVM environment. The modification of `svm_copy_pmcs` to explicitly handle copying these fields between VMCBs is expected. This is the glue that keeps things consistent as VMs are switched. The addition of `KVM_MEDIATED_PMU_CAP_HW_SWITCHED` and `KVM_MEDIATED_PMU_CAP_HW_FILTERED` is for capability reporting. I'll need to remember those for later. But then, the `vmcb_save_area`... this is where things get interesting. The code snippet with the updated structure raised a concern: is this data exposed to userspace? The VMCB is a shared structure with the hardware, so I'm thinking the modification to it could have wider implications. I had a quick thought about `KVM_GET_NESTED_STATE` and `KVM_SET_NESTED_STATE` potentially copying the VMCB contents to userspace. I need to be sure the PMC data is handled safely. I need to confirm if `vmcb_save_area` is actually exposed to userspace. Ah, `struct kvm_nested_state` has `data`, which contains VMCB12. I recall `nested_svm_vmexit_update_vmcb12` copying `vmcb02->save` to `vmcb12->save`. So, `vmcb12` is in guest memory (L1 guest memory), not directly copied to userspace by KVM. That's good, but how does the nested state get exposed, which is needed by L1? Wait, `KVM_GET_NESTED_STATE` is used for SVM and it copies `vmcb12`? I need to check the code in `arch/x86/kvm/svm/nested.c` for `KVM_GET_NESTED_STATE`. This is a critical point. I can't have raw, sensitive PMC information leaking. I'll get the code from there to confirm the exact copy operations. This is a potential attack vector if not handled carefully, so I need to audit this path closely. |
| 30/3 | 2026/08/10 10:41 | tool | grepper |
0mArgs:{
"Expression": "KVM_GET_NESTED_STATE",
"PathPrefix": "arch/x86/kvm/svm/"
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1982=static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-2059-\t * validated, because (a) it may have been clobbered by SMM before\narch/x86/kvm/svm/nested.c:2060:\t * KVM_GET_NESTED_STATE, and (b) it is not loaded at emulated\narch/x86/kvm/svm/nested.c-2061-\t * #VMEXIT.\n"
} |
| 31/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 19102 output: 38 thoughts: 14 Thoughts: Okay, let's dive into this `svm_get_nested_state` function. My initial thought is to understand its purpose within the larger SVM framework. I need to quickly grasp what constitutes a "nested state" in the context of an SVM. Is this related to some form of hierarchical model, perhaps dealing with multi-class classification or structured output prediction? Or is it more fundamental, maybe concerned with the internal workings of the optimization process itself, like handling nested loops or conditional branches within the solver? My experience tells me this will likely be more involved than a simple getter function. It's probably responsible for extracting and potentially managing complex internal data structures. Think about the support vectors, decision functions, model parameters, and optimization history. A "nested state" could be a way to encapsulate all of that, or a subset, for a specific sub-problem or stage in the SVM's training or prediction phase. I need to quickly assess the level of abstraction this function offers. Is it intended for debugging, model inspection, or perhaps facilitating some form of online learning or transfer learning? The naming convention suggests it’s not meant for everyday use, perhaps reserved for advanced users. It's likely hidden from the user and only used internally. I'm now thinking about the potential implications for performance. Retrieving nested states can be expensive. We need to be wary of making unnecessary calls to such a function, especially during the crucial stages of model training or real-time inference. Efficient memory management and carefully considered data copying strategies will be essential if this function is used. My assumption is that the state information returned is likely a read-only copy, unless we are specifically dealing with some form of internal data modification. Finally, I need to start asking some questions. What is the API of this function? What are the input parameters, and what information do they expose? What does the return value look like? Is it a complex structure, a pointer to some internal object, or some combination of both? Getting the specifics of this function will be essential for a complete understanding of its behavior and place in the whole system. |
| 32/3 | 2026/08/10 10:41 | tool | grepper |
0mArgs:{
"Expression": "svm_get_nested_state",
"PathPrefix": "arch/x86/kvm/svm/"
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1877=static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,\n--\narch/x86/kvm/svm/nested.c-1911-\narch/x86/kvm/svm/nested.c:1912:static int svm_get_nested_state(struct kvm_vcpu *vcpu,\narch/x86/kvm/svm/nested.c-1913-\t\t\t\tstruct kvm_nested_state __user *user_kvm_nested_state,\n--\narch/x86/kvm/svm/nested.c=1982=static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-2145-\narch/x86/kvm/svm/nested.c:2146:static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-2147-{\n--\narch/x86/kvm/svm/nested.c=2199=struct kvm_x86_nested_ops svm_nested_ops __initdata = {\n--\narch/x86/kvm/svm/nested.c-2204-\t.triple_fault = nested_svm_triple_fault,\narch/x86/kvm/svm/nested.c:2205:\t.get_nested_state_pages = svm_get_nested_state_pages,\narch/x86/kvm/svm/nested.c:2206:\t.get_state = svm_get_nested_state,\narch/x86/kvm/svm/nested.c-2207-\t.set_state = svm_set_nested_state,\n--\narch/x86/kvm/svm/svm.c=4504=static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)\n--\narch/x86/kvm/svm/svm.c-4670-\t *\narch/x86/kvm/svm/svm.c:4671:\t * FIXME: Rework svm_get_nested_state() to not pull data from the\narch/x86/kvm/svm/svm.c-4672-\t * cache (except for maybe int_ctl).\n"
} |
| 33/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 20230 output: 43 thoughts: 13 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Decoding `svm_get_nested_state`** Alright, the mental gears are already turning. `svm_get_nested_state`... that name immediately pings a few key concepts in my head. It sounds like we're dealing with a state retrieval function within the context of a Support Vector Machine (SVM). The "nested" part strongly suggests that we're not just looking at the overall SVM model state, but diving deeper into the substructures or components that make it up. Are we talking about retrieving the parameters of individual kernels, the alpha values associated with support vectors, the bias terms, the states of any optimization processes used, or maybe the state of intermediate calculations? The precise context would influence what I expect to see. Is this for debugging? Model serialization/deserialization? Model interpretation? Understanding the *purpose* of getting this nested state is critical. If it's for inspecting an SVM that's behaving strangely, I'll be looking for clues about the feature space transformations or the values of the dual coefficients. If it's part of a model persistence strategy, I'll be more concerned with the format and completeness of the data being captured. Essentially, I'm thinking: what *exactly* is "nested" here? What level of granularity are we operating at? The answer to that question will inform my entire line of reasoning. I need to figure out the level of abstraction we're working at to understand how to fully grok the functionality. |
| 34/3 | 2026/08/10 10:41 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 1912,
"LineCount": 70
}Results: {
"Contents": "1912:\tstatic int svm_get_nested_state(struct kvm_vcpu *vcpu,\n1913:\t\t\t\t\tstruct kvm_nested_state __user *user_kvm_nested_state,\n1914:\t\t\t\t\tu32 user_data_size)\n1915:\t{\n1916:\t\tstruct vcpu_svm *svm;\n1917:\t\tstruct vmcb_control_area *ctl;\n1918:\t\tunsigned long r;\n1919:\t\tstruct kvm_nested_state kvm_state = {\n1920:\t\t\t.flags = 0,\n1921:\t\t\t.format = KVM_STATE_NESTED_FORMAT_SVM,\n1922:\t\t\t.size = sizeof(kvm_state),\n1923:\t\t};\n1924:\t\tstruct vmcb __user *user_vmcb = (struct vmcb __user *)\n1925:\t\t\t\u0026user_kvm_nested_state-\u003edata.svm[0];\n1926:\t\n1927:\t\tif (!vcpu)\n1928:\t\t\treturn kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;\n1929:\t\n1930:\t\tsvm = to_svm(vcpu);\n1931:\t\n1932:\t\tif (user_data_size \u003c kvm_state.size)\n1933:\t\t\tgoto out;\n1934:\t\n1935:\t\t/* First fill in the header and copy it out. */\n1936:\t\tif (is_guest_mode(vcpu)) {\n1937:\t\t\tkvm_state.hdr.svm.vmcb_pa = svm-\u003enested.vmcb12_gpa;\n1938:\t\t\tkvm_state.hdr.svm.gpat = 0;\n1939:\t\t\tif (l2_has_separate_pat(vcpu))\n1940:\t\t\t\tkvm_state.hdr.svm.gpat = svm-\u003evmcb-\u003esave.g_pat;\n1941:\t\t\tkvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;\n1942:\t\t\tkvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;\n1943:\t\n1944:\t\t\tif (vcpu-\u003earch.nested_run_pending)\n1945:\t\t\t\tkvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;\n1946:\t\t}\n1947:\t\n1948:\t\tif (gif_set(svm))\n1949:\t\t\tkvm_state.flags |= KVM_STATE_NESTED_GIF_SET;\n1950:\t\n1951:\t\tif (copy_to_user(user_kvm_nested_state, \u0026kvm_state, sizeof(kvm_state)))\n1952:\t\t\treturn -EFAULT;\n1953:\t\n1954:\t\tif (!is_guest_mode(vcpu))\n1955:\t\t\tgoto out;\n1956:\t\n1957:\t\t/*\n1958:\t\t * Copy over the full size of the VMCB rather than just the size\n1959:\t\t * of the structs.\n1960:\t\t */\n1961:\t\tif (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))\n1962:\t\t\treturn -EFAULT;\n1963:\t\n1964:\t\tctl = kzalloc_obj(*ctl);\n1965:\t\tif (!ctl)\n1966:\t\t\treturn -ENOMEM;\n1967:\t\n1968:\t\tnested_copy_vmcb_cache_to_control(ctl, \u0026svm-\u003enested.ctl);\n1969:\t\tr = copy_to_user(\u0026user_vmcb-\u003econtrol, ctl,\n1970:\t\t\t\t sizeof(user_vmcb-\u003econtrol));\n1971:\t\tkfree(ctl);\n1972:\t\tif (r)\n1973:\t\t\treturn -EFAULT;\n1974:\t\n1975:\t\tif (copy_to_user(\u0026user_vmcb-\u003esave, \u0026svm-\u003evmcb01.ptr-\u003esave,\n1976:\t\t\t\t sizeof(user_vmcb-\u003esave)))\n1977:\t\t\treturn -EFAULT;\n1978:\tout:\n1979:\t\treturn kvm_state.size;\n1980:\t}\n1981:\t\n"
} |
| 35/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 21780 output: 35 thoughts: 408 Thoughts: **Analyzing the Impact of the `vmcb_save_area` Structure Modification** Okay, so `svm_get_nested_state` is clearly responsible for copying the save area, `svm->vmcb01.ptr->save`, to the `user_vmcb->save` structure. `user_vmcb->save`, from the looks of it, is a `struct vmcb_save_area`, as expected. Now, here's where it gets interesting – this patch modified `struct vmcb_save_area`. I need to understand the implications of this change. Looking at the diff, they've added performance monitoring counters within the save area! Specifically, the addition of a `pmc[6]` array of `perf_ctl` and `perf_ctr` pairs, and two registers, `perf_cntr_global_status` and `perf_cntr_global_control`. This is a significant change, given that the old version had substantial reserved space in that area. The next step is to trace back the origin of `vmcb01.ptr->save` to see how it is handled initially. This would be allocated by `svm_vcpu_create`, so that is the next function to look at. From there, I need to know how the allocation takes place, and how the changes to the `struct vmcb_save_area` structure will need to be accounted for. |
| 36/3 | 2026/08/10 10:41 | tool | grepper |
0mArgs:{
"Expression": "vmcb01",
"PathPrefix": "arch/x86/kvm/svm/"
}Results: {
"Output": "\nFull output is too long, showing 500 out of 517 lines.\nUse more precise expression if possible.\n\narch/x86/kvm/svm/avic.c=174=static void avic_activate_vmcb(struct vcpu_svm *svm)\narch/x86/kvm/svm/avic.c-175-{\narch/x86/kvm/svm/avic.c:176:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/avic.c-177-\tstruct kvm_vcpu *vcpu = \u0026svm-\u003evcpu;\n--\narch/x86/kvm/svm/avic.c=233=static void avic_deactivate_vmcb(struct vcpu_svm *svm)\narch/x86/kvm/svm/avic.c-234-{\narch/x86/kvm/svm/avic.c:235:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/avic.c-236-\n--\narch/x86/kvm/svm/avic.c=1191=void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/avic.c-1193-\tstruct vcpu_svm *svm = to_svm(vcpu);\narch/x86/kvm/svm/avic.c:1194:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/avic.c-1195-\n--\narch/x86/kvm/svm/nested.c=99=static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-107-\t/*\narch/x86/kvm/svm/nested.c:108:\t * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,\narch/x86/kvm/svm/nested.c-109-\t * when called via KVM_SET_NESTED_STATE, that state may _not_ match current\n--\narch/x86/kvm/svm/nested.c-111-\t */\narch/x86/kvm/svm/nested.c:112:\tkvm_init_shadow_npt_mmu(vcpu, svm-\u003evmcb01.ptr-\u003esave.cr4,\narch/x86/kvm/svm/nested.c:113:\t\t\t\tsvm-\u003evmcb01.ptr-\u003esave.efer,\narch/x86/kvm/svm/nested.c-114-\t\t\t\tsvm-\u003enested.ctl.nested_cr3,\n--\narch/x86/kvm/svm/nested.c=141=void nested_vmcb02_recalc_intercepts(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-144-\tstruct vmcb *vmcb02 = svm-\u003enested.vmcb02.ptr;\narch/x86/kvm/svm/nested.c:145:\tstruct vmcb *vmcb01 = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/nested.c-146-\tunsigned int i;\n--\narch/x86/kvm/svm/nested.c-153-\tfor (i = 0; i \u003c MAX_INTERCEPT; i++)\narch/x86/kvm/svm/nested.c:154:\t\tvmcb02-\u003econtrol.intercepts[i] = vmcb01-\u003econtrol.intercepts[i];\narch/x86/kvm/svm/nested.c-155-\n--\narch/x86/kvm/svm/nested.c-167-\t\tvmcb_clr_intercept(\u0026vmcb02-\u003econtrol, INTERCEPT_CR8_WRITE);\narch/x86/kvm/svm/nested.c:168:\t\tif (!(vmcb01-\u003esave.rflags \u0026 X86_EFLAGS_IF))\narch/x86/kvm/svm/nested.c-169-\t\t\tvmcb_clr_intercept(\u0026vmcb02-\u003econtrol, INTERCEPT_VINTR);\n--\narch/x86/kvm/svm/nested.c-203- * This array (and its actual size) holds the set of offsets (indexing by chunk\narch/x86/kvm/svm/nested.c:204: * size) to process when merging vmcb12's MSRPM with vmcb01's MSRPM. Note, the\narch/x86/kvm/svm/nested.c:205: * set of MSRs for which interception is disabled in vmcb01 is per-vCPU, e.g.\narch/x86/kvm/svm/nested.c-206- * based on CPUID features. This array only tracks MSRs that *might* be passed\n--\narch/x86/kvm/svm/nested.c=756=static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-760-\tbool new_vmcb12 = false;\narch/x86/kvm/svm/nested.c:761:\tstruct vmcb *vmcb01 = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/nested.c-762-\tstruct vmcb *vmcb02 = svm-\u003enested.vmcb02.ptr;\n--\narch/x86/kvm/svm/nested.c-833-\t} else {\narch/x86/kvm/svm/nested.c:834:\t\tsvm_copy_lbrs(\u0026vmcb02-\u003esave, \u0026vmcb01-\u003esave);\narch/x86/kvm/svm/nested.c-835-\t}\n--\narch/x86/kvm/svm/nested.c-841-\telse if (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\narch/x86/kvm/svm/nested.c:842:\t\tsvm_copy_pmcs(\u0026vmcb02-\u003esave, \u0026vmcb01-\u003esave);\narch/x86/kvm/svm/nested.c-843-}\n--\narch/x86/kvm/svm/nested.c=869=static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-870-{\narch/x86/kvm/svm/nested.c:871:\tu32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;\narch/x86/kvm/svm/nested.c-872-\tu32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;\n--\narch/x86/kvm/svm/nested.c-875-\tstruct vmcb *vmcb02 = svm-\u003enested.vmcb02.ptr;\narch/x86/kvm/svm/nested.c:876:\tstruct vmcb *vmcb01 = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/nested.c-877-\tstruct kvm_vcpu *vcpu = \u0026svm-\u003evcpu;\n--\narch/x86/kvm/svm/nested.c-892-\telse\narch/x86/kvm/svm/nested.c:893:\t\tint_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);\narch/x86/kvm/svm/nested.c-894-\narch/x86/kvm/svm/nested.c-895-\tif (vnmi) {\narch/x86/kvm/svm/nested.c:896:\t\tif (vmcb01-\u003econtrol.int_ctl \u0026 V_NMI_PENDING_MASK) {\narch/x86/kvm/svm/nested.c-897-\t\t\tsvm-\u003evcpu.arch.nmi_pending++;\n--\narch/x86/kvm/svm/nested.c-906-\t/*\narch/x86/kvm/svm/nested.c:907:\t * Copied from vmcb01. msrpm_base can be overwritten later.\narch/x86/kvm/svm/nested.c-908-\t *\n--\narch/x86/kvm/svm/nested.c-914-\t */\narch/x86/kvm/svm/nested.c:915:\tvmcb02-\u003econtrol.misc_ctl = vmcb01-\u003econtrol.misc_ctl \u0026 (SVM_MISC_ENABLE_NP | SVM_MISC_ENABLE_GMET);\narch/x86/kvm/svm/nested.c:916:\tvmcb02-\u003econtrol.iopm_base_pa = vmcb01-\u003econtrol.iopm_base_pa;\narch/x86/kvm/svm/nested.c:917:\tvmcb02-\u003econtrol.msrpm_base_pa = vmcb01-\u003econtrol.msrpm_base_pa;\narch/x86/kvm/svm/nested.c-918-\tvmcb_mark_dirty(vmcb02, VMCB_PERM_MAP);\n--\narch/x86/kvm/svm/nested.c-928-\t * scenario, where a buslock happened in L1 before VMRUN, the bus lock\narch/x86/kvm/svm/nested.c:929:\t * firmly happened on an instruction in the past. Even if vmcb01's\narch/x86/kvm/svm/nested.c-930-\t * counter is still '1', (because the guilty instruction got patched),\n--\narch/x86/kvm/svm/nested.c-951-\narch/x86/kvm/svm/nested.c:952:\t/* Use vmcb01 MMU and format if guest does not use nNPT */\narch/x86/kvm/svm/nested.c-953-\tif (nested_npt_enabled(svm)) {\n--\narch/x86/kvm/svm/nested.c-971-\t\t(vmcb12_ctrl-\u003eint_ctl \u0026 int_ctl_vmcb12_bits) |\narch/x86/kvm/svm/nested.c:972:\t\t(vmcb01-\u003econtrol.int_ctl \u0026 int_ctl_vmcb01_bits);\narch/x86/kvm/svm/nested.c-973-\n--\narch/x86/kvm/svm/nested.c-1004-\narch/x86/kvm/svm/nested.c:1005:\t/* L0 uses hardware-switched mode for both vmcb01 and vmcb02 */\narch/x86/kvm/svm/nested.c-1006-\tif (kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\n--\narch/x86/kvm/svm/nested.c=1053=int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)\n--\narch/x86/kvm/svm/nested.c-1081-\narch/x86/kvm/svm/nested.c:1082:\tnested_svm_copy_common_state(svm-\u003evmcb01.ptr, svm-\u003enested.vmcb02.ptr);\narch/x86/kvm/svm/nested.c-1083-\n--\narch/x86/kvm/svm/nested.c=1136=int nested_svm_vmrun(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1140-\tu64 vmcb12_gpa;\narch/x86/kvm/svm/nested.c:1141:\tstruct vmcb *vmcb01 = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/nested.c-1142-\n--\narch/x86/kvm/svm/nested.c-1187-\t/*\narch/x86/kvm/svm/nested.c:1188:\t * Since vmcb01 is not in use, we can use it to store some of the L1\narch/x86/kvm/svm/nested.c-1189-\t * state.\narch/x86/kvm/svm/nested.c-1190-\t */\narch/x86/kvm/svm/nested.c:1191:\tvmcb01-\u003esave.efer = vcpu-\u003earch.efer;\narch/x86/kvm/svm/nested.c:1192:\tvmcb01-\u003esave.cr0 = kvm_read_cr0(vcpu);\narch/x86/kvm/svm/nested.c:1193:\tvmcb01-\u003esave.cr4 = vcpu-\u003earch.cr4;\narch/x86/kvm/svm/nested.c:1194:\tvmcb01-\u003esave.rflags = kvm_get_rflags(vcpu);\narch/x86/kvm/svm/nested.c:1195:\tvmcb01-\u003esave.rip = kvm_rip_read(vcpu);\narch/x86/kvm/svm/nested.c-1196-\narch/x86/kvm/svm/nested.c-1197-\tif (!npt_enabled)\narch/x86/kvm/svm/nested.c:1198:\t\tvmcb01-\u003esave.cr3 = kvm_read_cr3(vcpu);\narch/x86/kvm/svm/nested.c-1199-\n--\narch/x86/kvm/svm/nested.c=1346=void nested_svm_vmexit(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1348-\tstruct kvm_vcpu *vcpu = \u0026svm-\u003evcpu;\narch/x86/kvm/svm/nested.c:1349:\tstruct vmcb *vmcb01 = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/nested.c-1350-\tstruct vmcb *vmcb02 = svm-\u003enested.vmcb02.ptr;\n--\narch/x86/kvm/svm/nested.c-1376-\narch/x86/kvm/svm/nested.c:1377:\tnested_svm_copy_common_state(svm-\u003enested.vmcb02.ptr, svm-\u003evmcb01.ptr);\narch/x86/kvm/svm/nested.c-1378-\n--\narch/x86/kvm/svm/nested.c-1381-\tif (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS))\narch/x86/kvm/svm/nested.c:1382:\t\tvmcb01-\u003econtrol.erap_ctl |= ERAP_CONTROL_CLEAR_RAP;\narch/x86/kvm/svm/nested.c-1383-\narch/x86/kvm/svm/nested.c:1384:\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\narch/x86/kvm/svm/nested.c-1385-\narch/x86/kvm/svm/nested.c-1386-\t/*\narch/x86/kvm/svm/nested.c:1387:\t * Rules for synchronizing int_ctl bits from vmcb02 to vmcb01:\narch/x86/kvm/svm/nested.c-1388-\t *\n--\narch/x86/kvm/svm/nested.c-1393-\t * KVM re-requests an interrupt window if necessary, which implicitly\narch/x86/kvm/svm/nested.c:1394:\t * copies this bits from vmcb02 to vmcb01.\narch/x86/kvm/svm/nested.c-1395-\t *\n--\narch/x86/kvm/svm/nested.c-1397-\t * is stored in vmcb02, but its value doesn't need to be copied from/to\narch/x86/kvm/svm/nested.c:1398:\t * vmcb01 because it is copied from/to the virtual APIC's TPR register\narch/x86/kvm/svm/nested.c-1399-\t * on each VM entry/exit.\n--\narch/x86/kvm/svm/nested.c-1402-\t * V_GIF. However, GIF is architecturally clear on each VM exit, thus\narch/x86/kvm/svm/nested.c:1403:\t * there is no need to copy V_GIF from vmcb02 to vmcb01.\narch/x86/kvm/svm/nested.c-1404-\t */\n--\narch/x86/kvm/svm/nested.c-1408-\tif (!nested_vmcb12_has_lbrv(vcpu)) {\narch/x86/kvm/svm/nested.c:1409:\t\tsvm_copy_lbrs(\u0026vmcb01-\u003esave, \u0026vmcb02-\u003esave);\narch/x86/kvm/svm/nested.c:1410:\t\tvmcb_mark_dirty(vmcb01, VMCB_LBR);\narch/x86/kvm/svm/nested.c-1411-\t}\n--\narch/x86/kvm/svm/nested.c-1416-\t kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED))\narch/x86/kvm/svm/nested.c:1417:\t\tsvm_copy_pmcs(\u0026vmcb01-\u003esave, \u0026vmcb02-\u003esave);\narch/x86/kvm/svm/nested.c-1418-\n--\narch/x86/kvm/svm/nested.c-1420-\t\tif (vmcb02-\u003econtrol.int_ctl \u0026 V_NMI_BLOCKING_MASK)\narch/x86/kvm/svm/nested.c:1421:\t\t\tvmcb01-\u003econtrol.int_ctl |= V_NMI_BLOCKING_MASK;\narch/x86/kvm/svm/nested.c-1422-\t\telse\narch/x86/kvm/svm/nested.c:1423:\t\t\tvmcb01-\u003econtrol.int_ctl \u0026= ~V_NMI_BLOCKING_MASK;\narch/x86/kvm/svm/nested.c-1424-\n--\narch/x86/kvm/svm/nested.c-1426-\t\t\tvcpu-\u003earch.nmi_pending--;\narch/x86/kvm/svm/nested.c:1427:\t\t\tvmcb01-\u003econtrol.int_ctl |= V_NMI_PENDING_MASK;\narch/x86/kvm/svm/nested.c-1428-\t\t} else {\narch/x86/kvm/svm/nested.c:1429:\t\t\tvmcb01-\u003econtrol.int_ctl \u0026= ~V_NMI_PENDING_MASK;\narch/x86/kvm/svm/nested.c-1430-\t\t}\n--\narch/x86/kvm/svm/nested.c-1437-\tsvm_set_gif(svm, false);\narch/x86/kvm/svm/nested.c:1438:\tvmcb01-\u003econtrol.exit_int_info = 0;\narch/x86/kvm/svm/nested.c-1439-\narch/x86/kvm/svm/nested.c-1440-\tsvm-\u003evcpu.arch.tsc_offset = svm-\u003evcpu.arch.l1_tsc_offset;\narch/x86/kvm/svm/nested.c:1441:\tif (vmcb01-\u003econtrol.tsc_offset != svm-\u003evcpu.arch.tsc_offset) {\narch/x86/kvm/svm/nested.c:1442:\t\tvmcb01-\u003econtrol.tsc_offset = svm-\u003evcpu.arch.tsc_offset;\narch/x86/kvm/svm/nested.c:1443:\t\tvmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);\narch/x86/kvm/svm/nested.c-1444-\t}\n--\narch/x86/kvm/svm/nested.c-1454-\t/*\narch/x86/kvm/svm/nested.c:1455:\t * Restore processor state that had been saved in vmcb01\narch/x86/kvm/svm/nested.c-1456-\t */\narch/x86/kvm/svm/nested.c:1457:\tkvm_set_rflags(vcpu, vmcb01-\u003esave.rflags);\narch/x86/kvm/svm/nested.c:1458:\tsvm_set_efer(vcpu, vmcb01-\u003esave.efer);\narch/x86/kvm/svm/nested.c:1459:\tsvm_set_cr0(vcpu, vmcb01-\u003esave.cr0 | X86_CR0_PE);\narch/x86/kvm/svm/nested.c:1460:\tsvm_set_cr4(vcpu, vmcb01-\u003esave.cr4);\narch/x86/kvm/svm/nested.c:1461:\tkvm_rax_write_raw(vcpu, vmcb01-\u003esave.rax);\narch/x86/kvm/svm/nested.c:1462:\tkvm_rsp_write(vcpu, vmcb01-\u003esave.rsp);\narch/x86/kvm/svm/nested.c:1463:\tkvm_rip_write(vcpu, vmcb01-\u003esave.rip);\narch/x86/kvm/svm/nested.c-1464-\n--\narch/x86/kvm/svm/nested.c-1471-\narch/x86/kvm/svm/nested.c:1472:\tif (nested_svm_load_cr3(vcpu, vmcb01-\u003esave.cr3, false, true))\narch/x86/kvm/svm/nested.c-1473-\t\tkvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);\n--\narch/x86/kvm/svm/nested.c-1492-\t */\narch/x86/kvm/svm/nested.c:1493:\tif (unlikely(vmcb01-\u003esave.rflags \u0026 X86_EFLAGS_TF))\narch/x86/kvm/svm/nested.c-1494-\t\tkvm_queue_exception(\u0026(svm-\u003evcpu), DB_VECTOR);\n--\narch/x86/kvm/svm/nested.c=1540=void svm_free_nested(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1544-\narch/x86/kvm/svm/nested.c:1545:\tif (WARN_ON_ONCE(svm-\u003evmcb != svm-\u003evmcb01.ptr))\narch/x86/kvm/svm/nested.c:1546:\t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\narch/x86/kvm/svm/nested.c-1547-\n--\narch/x86/kvm/svm/nested.c=1566=void svm_leave_nested(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1585-\narch/x86/kvm/svm/nested.c:1586:\t\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\narch/x86/kvm/svm/nested.c-1587-\n--\narch/x86/kvm/svm/nested.c=1832=int nested_svm_exit_special(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1844-\narch/x86/kvm/svm/nested.c:1845:\t\tif (svm-\u003evmcb01.ptr-\u003econtrol.intercepts[INTERCEPT_EXCEPTION] \u0026\narch/x86/kvm/svm/nested.c-1846-\t\t excp_bits)\n--\narch/x86/kvm/svm/nested.c=1912=static int svm_get_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-1974-\narch/x86/kvm/svm/nested.c:1975:\tif (copy_to_user(\u0026user_vmcb-\u003esave, \u0026svm-\u003evmcb01.ptr-\u003esave,\narch/x86/kvm/svm/nested.c-1976-\t\t\t sizeof(user_vmcb-\u003esave)))\n--\narch/x86/kvm/svm/nested.c=1982=static int svm_set_nested_state(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-2084-\t * vmcb12.control, which will be combined with L1 and stored into\narch/x86/kvm/svm/nested.c:2085:\t * vmcb02, and the L1 save state which we store in vmcb01.\narch/x86/kvm/svm/nested.c-2086-\t * L2 registers if needed are moved from the current VMCB to VMCB02.\n--\narch/x86/kvm/svm/nested.c-2091-\telse\narch/x86/kvm/svm/nested.c:2092:\t\tsvm-\u003enested.vmcb02.ptr-\u003esave = svm-\u003evmcb01.ptr-\u003esave;\narch/x86/kvm/svm/nested.c-2093-\n--\narch/x86/kvm/svm/nested.c-2102-\narch/x86/kvm/svm/nested.c:2103:\tsvm_copy_vmrun_state(\u0026svm-\u003evmcb01.ptr-\u003esave, save);\narch/x86/kvm/svm/nested.c-2104-\tnested_copy_vmcb_control_to_cache(svm, ctl);\n--\narch/x86/kvm/svm/nested.c-2117-\t * Any previously restored state (e.g. KVM_SET_SREGS) would mark fields\narch/x86/kvm/svm/nested.c:2118:\t * dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.\narch/x86/kvm/svm/nested.c-2119-\t */\n--\narch/x86/kvm/svm/sev.c=4757=static void sev_es_init_vmcb(struct vcpu_svm *svm, bool init_event)\n--\narch/x86/kvm/svm/sev.c-4759-\tstruct kvm_sev_info *sev = to_kvm_sev_info(svm-\u003evcpu.kvm);\narch/x86/kvm/svm/sev.c:4760:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/sev.c-4761-\n--\narch/x86/kvm/svm/svm.c=639=static void set_dr_intercepts(struct vcpu_svm *svm)\narch/x86/kvm/svm/svm.c-640-{\narch/x86/kvm/svm/svm.c:641:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.c-642-\n--\narch/x86/kvm/svm/svm.c=663=static void clr_dr_intercepts(struct vcpu_svm *svm)\narch/x86/kvm/svm/svm.c-664-{\narch/x86/kvm/svm/svm.c:665:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.c-666-\n--\narch/x86/kvm/svm/svm.c=1016=static void svm_write_tsc_offset(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-1019-\narch/x86/kvm/svm/svm.c:1020:\tsvm-\u003evmcb01.ptr-\u003econtrol.tsc_offset = vcpu-\u003earch.l1_tsc_offset;\narch/x86/kvm/svm/svm.c-1021-\tsvm-\u003evmcb-\u003econtrol.tsc_offset = vcpu-\u003earch.tsc_offset;\n--\narch/x86/kvm/svm/svm.c=1124=static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)\n--\narch/x86/kvm/svm/svm.c-1126-\tstruct vcpu_svm *svm = to_svm(vcpu);\narch/x86/kvm/svm/svm.c:1127:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.c-1128-\tstruct vmcb_control_area *control = \u0026vmcb-\u003econtrol;\n--\narch/x86/kvm/svm/svm.c=1316=static int svm_vcpu_create(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-1318-\tstruct vcpu_svm *svm;\narch/x86/kvm/svm/svm.c:1319:\tstruct page *vmcb01_page;\narch/x86/kvm/svm/svm.c-1320-\tint err;\n--\narch/x86/kvm/svm/svm.c-1325-\terr = -ENOMEM;\narch/x86/kvm/svm/svm.c:1326:\tvmcb01_page = snp_safe_alloc_page();\narch/x86/kvm/svm/svm.c:1327:\tif (!vmcb01_page)\narch/x86/kvm/svm/svm.c-1328-\t\tgoto out;\n--\narch/x86/kvm/svm/svm.c-1346-\narch/x86/kvm/svm/svm.c:1347:\tsvm-\u003evmcb01.ptr = page_address(vmcb01_page);\narch/x86/kvm/svm/svm.c:1348:\tsvm-\u003evmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) \u003c\u003c PAGE_SHIFT);\narch/x86/kvm/svm/svm.c:1349:\tsvm_switch_vmcb(svm, \u0026svm-\u003evmcb01);\narch/x86/kvm/svm/svm.c-1350-\n--\narch/x86/kvm/svm/svm.c-1357-error_free_vmcb_page:\narch/x86/kvm/svm/svm.c:1358:\t__free_page(vmcb01_page);\narch/x86/kvm/svm/svm.c-1359-out:\n--\narch/x86/kvm/svm/svm.c=1363=static void svm_vcpu_free(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-1373-\narch/x86/kvm/svm/svm.c:1374:\t__free_page(__sme_pa_to_page(svm-\u003evmcb01.pa));\narch/x86/kvm/svm/svm.c-1375-\tsvm_vcpu_free_msrpm(svm-\u003emsrpm);\n--\narch/x86/kvm/svm/svm.c=1587=static void svm_clear_vintr(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/svm.c-1593-\tif (is_guest_mode(\u0026svm-\u003evcpu)) {\narch/x86/kvm/svm/svm.c:1594:\t\tsvm-\u003evmcb01.ptr-\u003econtrol.int_ctl \u0026= ~V_IRQ_INJECTION_BITS_MASK;\narch/x86/kvm/svm/svm.c-1595-\n--\narch/x86/kvm/svm/svm.c=1608=static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)\n--\narch/x86/kvm/svm/svm.c-1610-\tstruct vmcb_save_area *save = \u0026to_svm(vcpu)-\u003evmcb-\u003esave;\narch/x86/kvm/svm/svm.c:1611:\tstruct vmcb_save_area *save01 = \u0026to_svm(vcpu)-\u003evmcb01.ptr-\u003esave;\narch/x86/kvm/svm/svm.c-1612-\n--\narch/x86/kvm/svm/svm.c=2237=static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)\n--\narch/x86/kvm/svm/svm.c-2261-\tif (vmload) {\narch/x86/kvm/svm/svm.c:2262:\t\tsvm_copy_vmloadsave_state(svm-\u003evmcb01.ptr, vmcb12);\narch/x86/kvm/svm/svm.c-2263-\t\tsvm-\u003esysenter_eip_hi = 0;\n--\narch/x86/kvm/svm/svm.c-2265-\t} else {\narch/x86/kvm/svm/svm.c:2266:\t\tsvm_copy_vmloadsave_state(vmcb12, svm-\u003evmcb01.ptr);\narch/x86/kvm/svm/svm.c-2267-\t}\n--\narch/x86/kvm/svm/svm.c=2828=static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)\n--\narch/x86/kvm/svm/svm.c-2844-\tcase MSR_STAR:\narch/x86/kvm/svm/svm.c:2845:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.star;\narch/x86/kvm/svm/svm.c-2846-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c-2848-\tcase MSR_LSTAR:\narch/x86/kvm/svm/svm.c:2849:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.lstar;\narch/x86/kvm/svm/svm.c-2850-\t\tbreak;\narch/x86/kvm/svm/svm.c-2851-\tcase MSR_CSTAR:\narch/x86/kvm/svm/svm.c:2852:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.cstar;\narch/x86/kvm/svm/svm.c-2853-\t\tbreak;\narch/x86/kvm/svm/svm.c-2854-\tcase MSR_GS_BASE:\narch/x86/kvm/svm/svm.c:2855:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.gs.base;\narch/x86/kvm/svm/svm.c-2856-\t\tbreak;\narch/x86/kvm/svm/svm.c-2857-\tcase MSR_FS_BASE:\narch/x86/kvm/svm/svm.c:2858:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.fs.base;\narch/x86/kvm/svm/svm.c-2859-\t\tbreak;\narch/x86/kvm/svm/svm.c-2860-\tcase MSR_KERNEL_GS_BASE:\narch/x86/kvm/svm/svm.c:2861:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.kernel_gs_base;\narch/x86/kvm/svm/svm.c-2862-\t\tbreak;\narch/x86/kvm/svm/svm.c-2863-\tcase MSR_SYSCALL_MASK:\narch/x86/kvm/svm/svm.c:2864:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.sfmask;\narch/x86/kvm/svm/svm.c-2865-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c-2867-\tcase MSR_IA32_SYSENTER_CS:\narch/x86/kvm/svm/svm.c:2868:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.sysenter_cs;\narch/x86/kvm/svm/svm.c-2869-\t\tbreak;\narch/x86/kvm/svm/svm.c-2870-\tcase MSR_IA32_SYSENTER_EIP:\narch/x86/kvm/svm/svm.c:2871:\t\tmsr_info-\u003edata = (u32)svm-\u003evmcb01.ptr-\u003esave.sysenter_eip;\narch/x86/kvm/svm/svm.c-2872-\t\tif (guest_cpuid_is_intel_compatible(vcpu))\n--\narch/x86/kvm/svm/svm.c-2875-\tcase MSR_IA32_SYSENTER_ESP:\narch/x86/kvm/svm/svm.c:2876:\t\tmsr_info-\u003edata = svm-\u003evmcb01.ptr-\u003esave.sysenter_esp;\narch/x86/kvm/svm/svm.c-2877-\t\tif (guest_cpuid_is_intel_compatible(vcpu))\n--\narch/x86/kvm/svm/svm.c=2991=static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)\n--\narch/x86/kvm/svm/svm.c-3045-\t\tif (npt_enabled) {\narch/x86/kvm/svm/svm.c:3046:\t\t\tvmcb_set_gpat(svm-\u003evmcb01.ptr, data);\narch/x86/kvm/svm/svm.c-3047-\t\t\tif (is_guest_mode(vcpu) \u0026\u0026 !l2_has_separate_pat(vcpu))\n--\narch/x86/kvm/svm/svm.c-3089-\tcase MSR_STAR:\narch/x86/kvm/svm/svm.c:3090:\t\tsvm-\u003evmcb01.ptr-\u003esave.star = data;\narch/x86/kvm/svm/svm.c-3091-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c-3093-\tcase MSR_LSTAR:\narch/x86/kvm/svm/svm.c:3094:\t\tsvm-\u003evmcb01.ptr-\u003esave.lstar = data;\narch/x86/kvm/svm/svm.c-3095-\t\tbreak;\narch/x86/kvm/svm/svm.c-3096-\tcase MSR_CSTAR:\narch/x86/kvm/svm/svm.c:3097:\t\tsvm-\u003evmcb01.ptr-\u003esave.cstar = data;\narch/x86/kvm/svm/svm.c-3098-\t\tbreak;\narch/x86/kvm/svm/svm.c-3099-\tcase MSR_GS_BASE:\narch/x86/kvm/svm/svm.c:3100:\t\tsvm-\u003evmcb01.ptr-\u003esave.gs.base = data;\narch/x86/kvm/svm/svm.c-3101-\t\tbreak;\narch/x86/kvm/svm/svm.c-3102-\tcase MSR_FS_BASE:\narch/x86/kvm/svm/svm.c:3103:\t\tsvm-\u003evmcb01.ptr-\u003esave.fs.base = data;\narch/x86/kvm/svm/svm.c-3104-\t\tbreak;\narch/x86/kvm/svm/svm.c-3105-\tcase MSR_KERNEL_GS_BASE:\narch/x86/kvm/svm/svm.c:3106:\t\tsvm-\u003evmcb01.ptr-\u003esave.kernel_gs_base = data;\narch/x86/kvm/svm/svm.c-3107-\t\tbreak;\narch/x86/kvm/svm/svm.c-3108-\tcase MSR_SYSCALL_MASK:\narch/x86/kvm/svm/svm.c:3109:\t\tsvm-\u003evmcb01.ptr-\u003esave.sfmask = data;\narch/x86/kvm/svm/svm.c-3110-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c-3112-\tcase MSR_IA32_SYSENTER_CS:\narch/x86/kvm/svm/svm.c:3113:\t\tsvm-\u003evmcb01.ptr-\u003esave.sysenter_cs = data;\narch/x86/kvm/svm/svm.c-3114-\t\tbreak;\narch/x86/kvm/svm/svm.c-3115-\tcase MSR_IA32_SYSENTER_EIP:\narch/x86/kvm/svm/svm.c:3116:\t\tsvm-\u003evmcb01.ptr-\u003esave.sysenter_eip = (u32)data;\narch/x86/kvm/svm/svm.c-3117-\t\t/*\n--\narch/x86/kvm/svm/svm.c-3126-\tcase MSR_IA32_SYSENTER_ESP:\narch/x86/kvm/svm/svm.c:3127:\t\tsvm-\u003evmcb01.ptr-\u003esave.sysenter_esp = (u32)data;\narch/x86/kvm/svm/svm.c-3128-\t\tsvm-\u003esysenter_esp_hi = guest_cpuid_is_intel_compatible(vcpu) ? (data \u003e\u003e 32) : 0;\n--\narch/x86/kvm/svm/svm.c-3131-\t\tsvm-\u003evmcb-\u003esave.s_cet = data;\narch/x86/kvm/svm/svm.c:3132:\t\tvmcb_mark_dirty(svm-\u003evmcb01.ptr, VMCB_CET);\narch/x86/kvm/svm/svm.c-3133-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c-3135-\t\tsvm-\u003evmcb-\u003esave.isst_addr = data;\narch/x86/kvm/svm/svm.c:3136:\t\tvmcb_mark_dirty(svm-\u003evmcb01.ptr, VMCB_CET);\narch/x86/kvm/svm/svm.c-3137-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c-3139-\t\tsvm-\u003evmcb-\u003esave.ssp = data;\narch/x86/kvm/svm/svm.c:3140:\t\tvmcb_mark_dirty(svm-\u003evmcb01.ptr, VMCB_CET);\narch/x86/kvm/svm/svm.c-3141-\t\tbreak;\n--\narch/x86/kvm/svm/svm.c=3427=static void dump_vmcb(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-3431-\tstruct vmcb_save_area *save = \u0026svm-\u003evmcb-\u003esave;\narch/x86/kvm/svm/svm.c:3432:\tstruct vmcb_save_area *save01 = \u0026svm-\u003evmcb01.ptr-\u003esave;\narch/x86/kvm/svm/svm.c-3433-\tchar *vm_type;\n--\narch/x86/kvm/svm/svm.c=4069=bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-4079-\t\tif ((svm-\u003enested.ctl.int_ctl \u0026 V_INTR_MASKING_MASK)\narch/x86/kvm/svm/svm.c:4080:\t\t ? !(svm-\u003evmcb01.ptr-\u003esave.rflags \u0026 X86_EFLAGS_IF)\narch/x86/kvm/svm/svm.c-4081-\t\t : !(kvm_get_rflags(vcpu) \u0026 X86_EFLAGS_IF))\n--\narch/x86/kvm/svm/svm.c=5034=static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram)\n--\narch/x86/kvm/svm/svm.c-5075-\narch/x86/kvm/svm/svm.c:5076:\tsvm_copy_vmrun_state(m_save.map.hva + 0x400, \u0026svm-\u003evmcb01.ptr-\u003esave);\narch/x86/kvm/svm/svm.c-5077-\treturn 0;\n--\narch/x86/kvm/svm/svm.c=5080=static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)\n--\narch/x86/kvm/svm/svm.c-5115-\narch/x86/kvm/svm/svm.c:5116:\tsvm_copy_vmrun_state(\u0026svm-\u003evmcb01.ptr-\u003esave, m_save.map.hva + 0x400);\narch/x86/kvm/svm/svm.c-5117-\n--\narch/x86/kvm/svm/svm.c-5121-\narch/x86/kvm/svm/svm.c:5122:\tvmcb_mark_all_dirty(svm-\u003evmcb01.ptr);\narch/x86/kvm/svm/svm.c-5123-\n--\narch/x86/kvm/svm/svm.h=219=struct svm_nested_state {\n--\narch/x86/kvm/svm/svm.h-228-\t * The MSR permissions map used for vmcb02, which is the merge result\narch/x86/kvm/svm/svm.h:229:\t * of vmcb01 and vmcb12\narch/x86/kvm/svm/svm.h-230-\t */\n--\narch/x86/kvm/svm/svm.h=287=struct vcpu_svm {\n--\narch/x86/kvm/svm/svm.h-290-\tstruct vmcb *vmcb;\narch/x86/kvm/svm/svm.h:291:\tstruct kvm_vmcb_info vmcb01;\narch/x86/kvm/svm/svm.h-292-\tstruct kvm_vmcb_info *current_vmcb;\n--\narch/x86/kvm/svm/svm.h=555=static inline void svm_mark_intercepts_dirty(struct vcpu_svm *svm)\narch/x86/kvm/svm/svm.h-556-{\narch/x86/kvm/svm/svm.h:557:\tvmcb_mark_dirty(svm-\u003evmcb01.ptr, VMCB_INTERCEPTS);\narch/x86/kvm/svm/svm.h-558-\n--\narch/x86/kvm/svm/svm.h-560-\t * If L2 is active, recalculate the intercepts for vmcb02 to account\narch/x86/kvm/svm/svm.h:561:\t * for the changes made to vmcb01. All intercept configuration is done\narch/x86/kvm/svm/svm.h:562:\t * for vmcb01 and then propagated to vmcb02 to combine KVM's intercepts\narch/x86/kvm/svm/svm.h-563-\t * with L1's intercepts (from the vmcb12 snapshot).\n--\narch/x86/kvm/svm/svm.h=569=static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)\narch/x86/kvm/svm/svm.h-570-{\narch/x86/kvm/svm/svm.h:571:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.h-572-\n--\narch/x86/kvm/svm/svm.h=579=static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)\narch/x86/kvm/svm/svm.h-580-{\narch/x86/kvm/svm/svm.h:581:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.h-582-\n--\narch/x86/kvm/svm/svm.h=589=static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)\narch/x86/kvm/svm/svm.h-590-{\narch/x86/kvm/svm/svm.h:591:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.h-592-\n--\narch/x86/kvm/svm/svm.h=598=static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)\narch/x86/kvm/svm/svm.h-599-{\narch/x86/kvm/svm/svm.h:600:\tstruct vmcb *vmcb = svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.h-601-\n--\narch/x86/kvm/svm/svm.h=618=static inline struct vmcb *get_vgif_vmcb(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/svm.h-625-\telse\narch/x86/kvm/svm/svm.h:626:\t\treturn svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.h-627-}\n--\narch/x86/kvm/svm/svm.h=689=static inline struct vmcb *get_vnmi_vmcb_l1(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/svm.h-696-\telse\narch/x86/kvm/svm/svm.h:697:\t\treturn svm-\u003evmcb01.ptr;\narch/x86/kvm/svm/svm.h-698-}\n--\narch/x86/kvm/svm/vmenter.S-10-\n\n"
} |
| 37/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 36142 output: 38 thoughts: 53 Thoughts: Okay, here's my take on those thoughts, framed as my own: **Page Allocation and Safety Concerns** Alright, so the code starts with `vmcb01_page = snp_safe_alloc_page();`. Right off the bat, that `snp_safe_alloc_page` catches my eye. It's assigning to `vmcb01_page`, which I assume is a critical control page, likely for a Secure Nested Paging (SNP) context. The name *suggests* it's "safe," but you *know* assumptions in security are dangerous. My immediate thought is, "Okay, a page is being allocated. But is it zeroed? Does `snp_safe_alloc_page` guarantee that the contents are initialized?" This is absolutely crucial for SNP, or any secure system, to prevent information leakage from previous uses of that memory. We can't have stale data lingering in there. So, instead of just assuming, I need to verify what that function *actually* does. Time to go dig into the definition of `snp_safe_alloc_page`. I'm heading there now. Let's see if the code is actually doing what I expect. |
| 38/3 | 2026/08/10 10:41 | tool | grepper |
0mArgs:{
"Expression": "snp_safe_alloc_page",
"PathPrefix": "arch/x86/kvm/svm/"
}Results: {
"Output": "arch/x86/kvm/svm/nested.c=1515=int svm_allocate_nested(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1521-\narch/x86/kvm/svm/nested.c:1522:\tvmcb02_page = snp_safe_alloc_page();\narch/x86/kvm/svm/nested.c-1523-\tif (!vmcb02_page)\n--\narch/x86/kvm/svm/sev.c=4849=int sev_vcpu_create(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/sev.c-4862-\t */\narch/x86/kvm/svm/sev.c:4863:\tvmsa_page = snp_safe_alloc_page();\narch/x86/kvm/svm/sev.c-4864-\tif (!vmsa_page)\n--\narch/x86/kvm/svm/sev.c=4935=void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)\n--\narch/x86/kvm/svm/sev.c-4971-\narch/x86/kvm/svm/sev.c:4972:struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)\narch/x86/kvm/svm/sev.c-4973-{\n--\narch/x86/kvm/svm/svm.c=614=static int svm_cpu_init(int cpu)\n--\narch/x86/kvm/svm/svm.c-620-\tmemset(sd, 0, sizeof(struct svm_cpu_data));\narch/x86/kvm/svm/svm.c:621:\tsave_area_page = snp_safe_alloc_page_node(cpu_to_node(cpu), GFP_KERNEL);\narch/x86/kvm/svm/svm.c-622-\tif (!save_area_page)\n--\narch/x86/kvm/svm/svm.c=1316=static int svm_vcpu_create(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/svm.c-1325-\terr = -ENOMEM;\narch/x86/kvm/svm/svm.c:1326:\tvmcb01_page = snp_safe_alloc_page();\narch/x86/kvm/svm/svm.c-1327-\tif (!vmcb01_page)\n--\narch/x86/kvm/svm/svm.c=5342=static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/svm.c-5343-{\narch/x86/kvm/svm/svm.c:5344:\tstruct page *page = snp_safe_alloc_page();\narch/x86/kvm/svm/svm.c-5345-\n--\narch/x86/kvm/svm/svm.h=1010=int sev_handle_vmgexit(struct kvm_vcpu *vcpu);\n--\narch/x86/kvm/svm/svm.h-1013-\narch/x86/kvm/svm/svm.h:1014:struct page *snp_safe_alloc_page_node(int node, gfp_t gfp);\narch/x86/kvm/svm/svm.h:1015:static inline struct page *snp_safe_alloc_page(void)\narch/x86/kvm/svm/svm.h-1016-{\narch/x86/kvm/svm/svm.h:1017:\treturn snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT);\narch/x86/kvm/svm/svm.h-1018-}\n--\narch/x86/kvm/svm/svm.h=1037=void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa);\narch/x86/kvm/svm/svm.h-1038-#else\narch/x86/kvm/svm/svm.h:1039:static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)\narch/x86/kvm/svm/svm.h-1040-{\n--\narch/x86/kvm/svm/svm.h-1043-\narch/x86/kvm/svm/svm.h:1044:static inline struct page *snp_safe_alloc_page(void)\narch/x86/kvm/svm/svm.h-1045-{\narch/x86/kvm/svm/svm.h:1046:\treturn snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT);\narch/x86/kvm/svm/svm.h-1047-}\n"
} |
| 39/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 37847 output: 43 thoughts: 32 Thoughts: Okay, let's dive into this. Here's my thought process on tackling this particular function, `snp_safe_alloc_page_node`, within the context of KVM/SVM's SEV implementation, which is located in `arch/x86/kvm/svm/sev.c`. **Diving into `snp_safe_alloc_page_node`** Alright, I need to get a clear picture of this allocation function. Since I'm in the x86/KVM/SVM space and we're dealing with SEV, my immediate assumptions are that this is related to secure memory allocation, likely for guest virtual machines running under Secure Encrypted Virtualization (SEV). I'll want to understand how it ensures safe page allocation within the context of the SEV's security features. I'm going to look specifically for any aspects that relate to the encryption keys or integrity protection, maybe even the specific flags passed in. I'm guessing we'll be dealing with mechanisms to prevent page leaks or unauthorized access within the encrypted realm. My instinct says I should be checking how it interacts with the underlying hardware's memory encryption capabilities, especially with the host kernel's MMU settings. I'm looking for the "how" and the "why" of the security considerations. I also should be comparing to any other similar allocation calls around this area and how this one differentiates. |
| 40/3 | 2026/08/10 10:41 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/sev.c",
"FirstLine": 4972,
"LineCount": 20
}Results: {
"Contents": "4972:\tstruct page *snp_safe_alloc_page_node(int node, gfp_t gfp)\n4973:\t{\n4974:\t\tunsigned long pfn;\n4975:\t\tstruct page *p;\n4976:\t\n4977:\t\tif (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))\n4978:\t\t\treturn alloc_pages_node(node, gfp | __GFP_ZERO, 0);\n4979:\t\n4980:\t\t/*\n4981:\t\t * Allocate an SNP-safe page to workaround the SNP erratum where\n4982:\t\t * the CPU will incorrectly signal an RMP violation #PF if a\n4983:\t\t * hugepage (2MB or 1GB) collides with the RMP entry of a\n4984:\t\t * 2MB-aligned VMCB, VMSA, or AVIC backing page.\n4985:\t\t *\n4986:\t\t * Allocate one extra page, choose a page which is not\n4987:\t\t * 2MB-aligned, and free the other.\n4988:\t\t */\n4989:\t\tp = alloc_pages_node(node, gfp | __GFP_ZERO, 1);\n4990:\t\tif (!p)\n4991:\t\t\treturn NULL;\n"
} |
| 41/3 | 2026/08/10 10:41 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 38567 output: 43 thoughts: 997 Thoughts: **Analyzing Potential Uninitialized Memory Leaks in VMCB Save Areas**
Okay, so I'm looking at this code and immediately my attention is drawn to `alloc_pages_node(node, gfp | __GFP_ZERO, 0);`. The page being zeroed is a good start. That zeroes `vmcb01`, which is key. Now, `svm_get_nested_state` copies `svm->vmcb01.ptr->save` to user space, right? Since `vmcb01` starts zeroed, then the newly added `pmc` array, `perf_cntr_global_status`, and `perf_cntr_global_control` are all zero initially. That's good, minimizes initial risk. KVM can modify these, and that's expected.
My concern is always uninitialized memory, right? Let's check this `pmc` array addition. Aha, here it is: the `pmc` array replaces `reserved_0xd8[112]` with its 96 bytes:
```c
- u8 reserved_0xd8[112];
+ u8 reserved_0xd8[8];
+ struct {
+ u64 perf_ctl;
+ u64 perf_ctr;
+ } __packed pmc[6];
+ u8 reserved_0x140[8];
```
8 + 96 + 8 = 112 bytes, so it's a perfect replacement, good. No change in the overall size of `struct vmcb_save_area`, so that's clean.
Then, there's the other change:
```c
- u8 reserved_0x180[88];
+ u8 reserved_0x180[72];
+ u64 perf_cntr_global_status;
+ u64 perf_cntr_global_control;
```
72 + 16 = 88 bytes, another clean replacement! Since the whole `vmcb01` page is zeroed, even the reserved fields will be initialized to zero, which eliminates the possibility of uninitialized memory in those areas.
But wait, I need to make sure `vmcb_save_area` isn't copied elsewhere to user space. `KVM_GET_NESTED_STATE` is one, done.
Now, checking for any other copying, `nested_svm_vmexit_update_vmcb12` copies `vmcb02->save` to `vmcb12->save`. But `vmcb12` is in guest memory, not user space!
And there's this `svm_copy_pmcs` function:
```c
#define svm_copy_pmcs(to, from) \
do { \
int i; \
\
(to)->perf_cntr_global_control = (from)->perf_cntr_global_control; \
(to)->perf_cntr_global_status = (from)->perf_cntr_global_status; \
for (i = 0; i < kvm_pmu_cap.num_counters_gp; i++) { \
(to)->pmc[i].perf_ctl = (from)->pmc[i].perf_ctl; \
(to)->pmc[i].perf_ctr = (from)->pmc[i].perf_ctr; \
} \
} while (0)
```
Hmm, `kvm_pmu_cap.num_counters_gp` is the limit here. If it's less than 6, then elements of `pmc[i]` for `i >= num_counters_gp` aren't copied. The catch here is that this is a copy *to* guest memory. It's not a user space info leak. But if `vmcb02` somehow had uninitialized data in `pmc[i]`, that could get leaked *to the guest*. That's where it's important to check the initialization of `vmcb02`. And `vmcb02` is also allocated via `snp_safe_alloc_page()` (in `svm_allocate_nested`), so it should be safe. I better verify `svm_allocate_nested` to be sure.
|
| 42/3 | 2026/08/10 10:42 | tool | read-file |
0mArgs:{
"File": "arch/x86/kvm/svm/nested.c",
"FirstLine": 1515,
"LineCount": 20
}Results: {
"Contents": "1515:\tint svm_allocate_nested(struct vcpu_svm *svm)\n1516:\t{\n1517:\t\tstruct page *vmcb02_page;\n1518:\t\n1519:\t\tif (svm-\u003enested.initialized)\n1520:\t\t\treturn 0;\n1521:\t\n1522:\t\tvmcb02_page = snp_safe_alloc_page();\n1523:\t\tif (!vmcb02_page)\n1524:\t\t\treturn -ENOMEM;\n1525:\t\tsvm-\u003enested.vmcb02.ptr = page_address(vmcb02_page);\n1526:\t\tsvm-\u003enested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) \u003c\u003c PAGE_SHIFT);\n1527:\t\n1528:\t\tsvm-\u003enested.msrpm = svm_vcpu_alloc_msrpm();\n1529:\t\tif (!svm-\u003enested.msrpm)\n1530:\t\t\tgoto err_free_vmcb02;\n1531:\t\n1532:\t\tsvm-\u003enested.initialized = true;\n1533:\t\treturn 0;\n1534:\t\n"
} |
| 43/3 | 2026/08/10 10:42 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 40911 output: 33 thoughts: 1609 Thoughts: **Analyzing PMC Virtualization and Potential Uninitialized Memory** Okay, so I'm looking at this patch and, first off, yes, `vmcb02` is definitely zeroed. No other structures are directly passed to userspace, which is good. The main focus here is handling Performance Monitoring Counter (PMC) virtualization. However, I need to double-check `amd_get_vendor_state` and `amd_set_vendor_state`. I see these functions are retrieving and setting vendor-specific state, which I assume refers to the AMD-specific VMCB save area, based on the context. Reading from `save` (which is `vmcb->save`) into the `pmu` and `pmc` structures in `amd_get_vendor_state` is fine because the VMCB is zeroed during allocation. `amd_set_vendor_state` is writing to that `save` area. This is where I want to be *sure* about the initialization. Then, I see `kvm_pmu_get_vendor_state` and `kvm_pmu_set_vendor_state` are called from other places, like `kvm_pmu_rdpmc`. I see `kvm_pmu_get_vendor_state` updates `pmc->counter` from the vendor state before being read. So, here's my critical question: Is there any risk of reading uninitialized memory, specifically in `pmc->counter` or `pmc->eventsel_hw`? Those are fields within `struct kvm_pmc`, which itself is part of `struct kvm_pmu`, which is nested inside `struct kvm_vcpu_arch`, which then lives in `struct kvm_vcpu`. Okay, so `kvm_vcpu` allocation is done via `kmem_cache_zalloc` in `kvm_vcpu_init` or similar functions, so that looks like it *should* ensure all these fields are initialized to zero. So, that's what I will need to check directly. |
| 44/3 | 2026/08/10 10:42 | tool | grepper |
0mArgs:{
"Expression": "kmem_cache_zalloc.*kvm_vcpu",
"PathPrefix": ""
}Results: {
"Output": "virt/kvm/kvm_main.c=4156=static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)\n--\nvirt/kvm/kvm_main.c-4194-\nvirt/kvm/kvm_main.c:4195:\tvcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);\nvirt/kvm/kvm_main.c-4196-\tif (!vcpu) {\n"
} |
| 45/3 | 2026/08/10 10:42 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 43145 output: 259 thoughts: 4428 Thoughts: **KMSAN Assessment: KVM AMD PMC Virtualization Patch** Okay, let's break this down. My initial assessment is that `kvm_vcpu` and related structures are all zero-allocated, which is a good starting point. That means, at the very least, we shouldn't have raw uninitialized data leaking straight out. The core of this patch seems to be around adding Performance Monitoring Counter (PMC) virtualization support for AMD SVM, handling nested virtualization scenarios. I'm focusing primarily on data exposed to userspace via KVM ioctls like `KVM_GET_MSRS`, `KVM_SET_MSRS`, `KVM_GET_NESTED_STATE`, and how these interact with the modified `vmcb_save_area` structure within the SVM context. The critical aspect is to ensure that no uninitialized memory from these structures or associated counters ends up being visible to the guest OS, either directly or indirectly. The use of `snp_safe_alloc_page()`, using `__GFP_ZERO`, provides significant assurance as it's intended for secure memory allocation. Furthermore, `struct vmcb_save_area` is `__packed` so there is no padding that could cause uninitialized values. The patch touches the PMU related MSRs, and calls `kvm_pmu_get_vendor_state`, so I need to check these paths. I'm tracing the data flow through functions such as `kvm_pmu_get_msr`, `amd_pmu_get_msr`, `amd_pmu_set_msr`. The key concern is whether calls to `pmc_read_counter` and `pmc_write_counter` are occurring on memory regions which have not been initialized. It appears that `pmc->counter` is initialized during allocation, and is valid even if `amd_get_vendor_state` fails to return 0. The patch is also adding support for copying data between VMCBs, such as in `svm_copy_pmcs`, and the concern is whether the copy is safe and complete. I need to make sure data isn't leaking from L2 to L1 guests in the nested virtualization case. I looked into how values are copied between `vmcb02`, `vmcb12`, and back again. The calls in `nested_svm_vmexit_update_vmcb12` and `nested_vmcb02_prepare_save` appear to copy the correct number of PMC counter values based on the host capabilities, so it looks like the copying is correct. It also looks like the reset handler `amd_pmu_reset` zeroes the values which it is intended to. The main interaction with userspace is through `KVM_GET_NESTED_STATE`. This copies `svm->vmcb01.ptr->save` (which is a `vmcb_save_area`) to userspace. Since `vmcb01` is zero-allocated and all fields are initialized either directly or via the zero-allocation, there's no way for uninitialized memory to be exposed through this path, either. I checked the various other functions, `kvm_pmu_get_vendor_state`, `kvm_pmu_warn_vendor_state`, `amd_pmu_adjust_msr_idx`. These don't seem to be an issue in terms of exposing uninitialized memory. Reviewing the KMSAN rules, this patch doesn't seem to fit the criteria. It doesn't modify kernel structures that are sent to userspace in a way that risks exposing uninitialized fields. There's no conditional logic or branching that relies on potentially uninitialized variables. Complex data structures are allocated with zero-initialization. There's no manipulation of buffers in a way that could lead to reading uninitialized bytes. KASAN should be sufficient. So, NeedsKMSAN should be false. |
| 46/3 | 2026/08/10 10:43 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch introduces PMC (Performance Monitoring Counter) virtualization support for AMD SVM in KVM. It modifies the `vmcb_save_area` structure to include a `pmc` array and global control/status fields, which replaces previously reserved bytes. The `vmcb_save_area` is copied to userspace via the `KVM_GET_NESTED_STATE` ioctl. However, the underlying VMCB pages (e.g., `vmcb01` and `vmcb02`) are allocated using `snp_safe_alloc_page()`, which explicitly uses `__GFP_ZERO` to zero-initialize the entire page. Furthermore, `struct vmcb_save_area` is marked as `__packed`, preventing any implicit padding bytes. The patch also adds logic to read and write these fields, but since the memory is zero-allocated and explicitly managed, there is no risk of uninitialized memory being leaked to userspace or used in conditional branches. Standard KASAN and other bug detectors are sufficient to catch any potential memory corruption or out-of-bounds access bugs introduced by these changes.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch introduces PMC (Performance Monitoring Counter) virtualization support for AMD SVM in KVM. It modifies the `vmcb_save_area` structure to include a `pmc` array and global control/status fields, which replaces previously reserved bytes. The `vmcb_save_area` is copied to userspace via the `KVM_GET_NESTED_STATE` ioctl. However, the underlying VMCB pages (e.g., `vmcb01` and `vmcb02`) are allocated using `snp_safe_alloc_page()`, which explicitly uses `__GFP_ZERO` to zero-initialize the entire page. Furthermore, `struct vmcb_save_area` is marked as `__packed`, preventing any implicit padding bytes. The patch also adds logic to read and write these fields, but since the memory is zero-allocated and explicitly managed, there is no risk of uninitialized memory being leaked to userspace or used in conditional branches. Standard KASAN and other bug detectors are sufficient to catch any potential memory corruption or out-of-bounds access bugs introduced by these changes.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|