Skip to content

Commit 88ebc2a

Browse files
mzhang3579sean-jc
authored andcommitted
KVM: nVMX: Disable PMU MSR interception as appropriate while running L2
Merge KVM's PMU MSR interception bitmaps with those of L1, i.e. merge the bitmaps of vmcs01 and vmcs12, e.g. so that KVM doesn't interpose on MSR accesses unnecessarily if L1 exposes a mediated PMU (or equivalent) to L2. Signed-off-by: Mingwei Zhang <mizhang@google.com> Co-developed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com> [sean: rewrite changelog and comment, omit MSRs that are always intercepted] Tested-by: Xudong Hao <xudong.hao@intel.com> Tested-by: Manali Shukla <manali.shukla@amd.com> Link: https://patch.msgid.link/20251206001720.468579-32-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent cb58327 commit 88ebc2a

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

arch/x86/kvm/vmx/nested.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,34 @@ static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx,
630630
#define nested_vmx_merge_msr_bitmaps_rw(msr) \
631631
nested_vmx_merge_msr_bitmaps(msr, MSR_TYPE_RW)
632632

633+
static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
634+
unsigned long *msr_bitmap_l1,
635+
unsigned long *msr_bitmap_l0)
636+
{
637+
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
638+
struct vcpu_vmx *vmx = to_vmx(vcpu);
639+
int i;
640+
641+
/*
642+
* Skip the merges if the vCPU doesn't have a mediated PMU MSR, i.e. if
643+
* none of the MSRs can possibly be passed through to L1.
644+
*/
645+
if (!kvm_vcpu_has_mediated_pmu(vcpu))
646+
return;
647+
648+
for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
649+
nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i);
650+
nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i);
651+
}
652+
653+
for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
654+
nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i);
655+
656+
nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
657+
nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);
658+
nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);
659+
}
660+
633661
/*
634662
* Merge L0's and L1's MSR bitmap, return false to indicate that
635663
* we do not use the hardware.
@@ -745,6 +773,8 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
745773
nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
746774
MSR_IA32_PL3_SSP, MSR_TYPE_RW);
747775

776+
nested_vmx_merge_pmu_msr_bitmaps(vcpu, msr_bitmap_l1, msr_bitmap_l0);
777+
748778
kvm_vcpu_unmap(vcpu, &map);
749779

750780
vmx->nested.force_msr_bitmap_recalc = false;

0 commit comments

Comments
 (0)