Skip to content

Commit 7ea3763

Browse files
Maxim Levitskygregkh
authored andcommitted
KVM: VMX: Preserve host's DEBUGCTLMSR_FREEZE_IN_SMM while running the guest
[ Upstream commit 6b1dd26 ] Set/clear DEBUGCTLMSR_FREEZE_IN_SMM in GUEST_IA32_DEBUGCTL based on the host's pre-VM-Enter value, i.e. preserve the host's FREEZE_IN_SMM setting while running the guest. When running with the "default treatment of SMIs" in effect (the only mode KVM supports), SMIs do not generate a VM-Exit that is visible to host (non-SMM) software, and instead transitions directly from VMX non-root to SMM. And critically, DEBUGCTL isn't context switched by hardware on SMI or RSM, i.e. SMM will run with whatever value was resident in hardware at the time of the SMI. Failure to preserve FREEZE_IN_SMM results in the PMU unexpectedly counting events while the CPU is executing in SMM, which can pollute profiling and potentially leak information into the guest. Check for changes in FREEZE_IN_SMM prior to every entry into KVM's inner run loop, as the bit can be toggled in IRQ context via IPI callback (SMP function call), by way of /sys/devices/cpu/freeze_on_smi. Add a field in kvm_x86_ops to communicate which DEBUGCTL bits need to be preserved, as FREEZE_IN_SMM is only supported and defined for Intel CPUs, i.e. explicitly checking FREEZE_IN_SMM in common x86 is at best weird, and at worst could lead to undesirable behavior in the future if AMD CPUs ever happened to pick up a collision with the bit. Exempt TDX vCPUs, i.e. protected guests, from the check, as the TDX Module owns and controls GUEST_IA32_DEBUGCTL. WARN in SVM if KVM_RUN_LOAD_DEBUGCTL is set, mostly to document that the lack of handling isn't a KVM bug (TDX already WARNs on any run_flag). Lastly, explicitly reload GUEST_IA32_DEBUGCTL on a VM-Fail that is missed by KVM but detected by hardware, i.e. in nested_vmx_restore_host_state(). Doing so avoids the need to track host_debugctl on a per-VMCS basis, as GUEST_IA32_DEBUGCTL is unconditionally written by prepare_vmcs02() and load_vmcs12_host_state(). For the VM-Fail case, even though KVM won't have actually entered the guest, vcpu_enter_guest() will have run with vmcs02 active and thus could result in vmcs01 being run with a stale value. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Co-developed-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/20250610232010.162191-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> [sean: resolve syntactic conflict in vt_x86_ops definition] Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a8db759 commit 7ea3763

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)
16301630
enum kvm_x86_run_flags {
16311631
KVM_RUN_FORCE_IMMEDIATE_EXIT = BIT(0),
16321632
KVM_RUN_LOAD_GUEST_DR6 = BIT(1),
1633+
KVM_RUN_LOAD_DEBUGCTL = BIT(2),
16331634
};
16341635

16351636
struct kvm_x86_ops {
@@ -1659,6 +1660,12 @@ struct kvm_x86_ops {
16591660
void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
16601661
void (*vcpu_put)(struct kvm_vcpu *vcpu);
16611662

1663+
/*
1664+
* Mask of DEBUGCTL bits that are owned by the host, i.e. that need to
1665+
* match the host's value even while the guest is active.
1666+
*/
1667+
const u64 HOST_OWNED_DEBUGCTL;
1668+
16621669
void (*update_exception_bitmap)(struct kvm_vcpu *vcpu);
16631670
int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
16641671
int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);

arch/x86/kvm/vmx/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
4242
.vcpu_load = vmx_vcpu_load,
4343
.vcpu_put = vmx_vcpu_put,
4444

45+
.HOST_OWNED_DEBUGCTL = DEBUGCTLMSR_FREEZE_IN_SMM,
46+
4547
.update_exception_bitmap = vmx_update_exception_bitmap,
4648
.get_feature_msr = vmx_get_feature_msr,
4749
.get_msr = vmx_get_msr,

arch/x86/kvm/vmx/nested.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4829,6 +4829,9 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
48294829
WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
48304830
}
48314831

4832+
/* Reload DEBUGCTL to ensure vmcs01 has a fresh FREEZE_IN_SMM value. */
4833+
vmx_reload_guest_debugctl(vcpu);
4834+
48324835
/*
48334836
* Note that calling vmx_set_{efer,cr0,cr4} is important as they
48344837
* handle a variety of side effects to KVM's software model.

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7407,6 +7407,9 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
74077407
if (run_flags & KVM_RUN_LOAD_GUEST_DR6)
74087408
set_debugreg(vcpu->arch.dr6, 6);
74097409

7410+
if (run_flags & KVM_RUN_LOAD_DEBUGCTL)
7411+
vmx_reload_guest_debugctl(vcpu);
7412+
74107413
/*
74117414
* Refresh vmcs.HOST_CR3 if necessary. This must be done immediately
74127415
* prior to VM-Enter, as the kernel may load a new ASID (PCID) any time

arch/x86/kvm/vmx/vmx.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,25 @@ bool vmx_is_valid_debugctl(struct kvm_vcpu *vcpu, u64 data, bool host_initiated)
440440

441441
static inline void vmx_guest_debugctl_write(struct kvm_vcpu *vcpu, u64 val)
442442
{
443+
WARN_ON_ONCE(val & DEBUGCTLMSR_FREEZE_IN_SMM);
444+
445+
val |= vcpu->arch.host_debugctl & DEBUGCTLMSR_FREEZE_IN_SMM;
443446
vmcs_write64(GUEST_IA32_DEBUGCTL, val);
444447
}
445448

446449
static inline u64 vmx_guest_debugctl_read(void)
447450
{
448-
return vmcs_read64(GUEST_IA32_DEBUGCTL);
451+
return vmcs_read64(GUEST_IA32_DEBUGCTL) & ~DEBUGCTLMSR_FREEZE_IN_SMM;
452+
}
453+
454+
static inline void vmx_reload_guest_debugctl(struct kvm_vcpu *vcpu)
455+
{
456+
u64 val = vmcs_read64(GUEST_IA32_DEBUGCTL);
457+
458+
if (!((val ^ vcpu->arch.host_debugctl) & DEBUGCTLMSR_FREEZE_IN_SMM))
459+
return;
460+
461+
vmx_guest_debugctl_write(vcpu, val & ~DEBUGCTLMSR_FREEZE_IN_SMM);
449462
}
450463

451464
/*

arch/x86/kvm/x86.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10711,7 +10711,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1071110711
dm_request_for_irq_injection(vcpu) &&
1071210712
kvm_cpu_accept_dm_intr(vcpu);
1071310713
fastpath_t exit_fastpath;
10714-
u64 run_flags;
10714+
u64 run_flags, debug_ctl;
1071510715

1071610716
bool req_immediate_exit = false;
1071710717

@@ -10982,7 +10982,17 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1098210982
set_debugreg(DR7_FIXED_1, 7);
1098310983
}
1098410984

10985-
vcpu->arch.host_debugctl = get_debugctlmsr();
10985+
/*
10986+
* Refresh the host DEBUGCTL snapshot after disabling IRQs, as DEBUGCTL
10987+
* can be modified in IRQ context, e.g. via SMP function calls. Inform
10988+
* vendor code if any host-owned bits were changed, e.g. so that the
10989+
* value loaded into hardware while running the guest can be updated.
10990+
*/
10991+
debug_ctl = get_debugctlmsr();
10992+
if ((debug_ctl ^ vcpu->arch.host_debugctl) & kvm_x86_ops.HOST_OWNED_DEBUGCTL &&
10993+
!vcpu->arch.guest_state_protected)
10994+
run_flags |= KVM_RUN_LOAD_DEBUGCTL;
10995+
vcpu->arch.host_debugctl = debug_ctl;
1098610996

1098710997
guest_timing_enter_irqoff();
1098810998

0 commit comments

Comments
 (0)