Skip to content

Commit e64dcfa

Browse files
committed
Merge tag 'kvm-x86-misc-6.19' of https://github.com/kvm-x86/linux into HEAD
KVM x86 misc changes for 6.19: - Fix an async #PF bug where KVM would clear the completion queue when the guest transitioned in and out of paging mode, e.g. when handling an SMI and then returning to paged mode via RSM. - Fix a bug where TDX would effectively corrupt user-return MSR values if the TDX Module rejects VP.ENTER and thus doesn't clobber host MSRs as expected. - Leave the user-return notifier used to restore MSRs registered when disabling virtualization, and instead pin kvm.ko. Restoring host MSRs via IPI callback is either pointless (clean reboot) or dangerous (forced reboot) since KVM has no idea what code it's interrupting. - Use the checked version of {get,put}_user(), as Linus wants to kill them off, and they're measurably faster on modern CPUs due to the unchecked versions containing an LFENCE. - Fix a long-lurking bug where KVM's lack of catch-up logic for periodic APIC timers can result in a hard lockup in the host. - Revert the periodic kvmclock sync logic now that KVM doesn't use a clocksource that's subject to NPT corrections. - Clean up KVM's handling of MMIO Stale Data and L1TF, and bury the latter behind CONFIG_CPU_MITIGATIONS. - Context switch XCR0, XSS, and PKRU outside of the entry/exit fastpath as the only reason they were handled in the faspath was to paper of a bug in the core #MC code that has long since been fixed. - Add emulator support for AVX MOV instructions to play nice with emulated devices whose PCI BARs guest drivers like to access with large multi-byte instructions.
2 parents 2368317 + c09816f commit e64dcfa

File tree

24 files changed

+703
-497
lines changed

24 files changed

+703
-497
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@
499499
#define X86_FEATURE_IBPB_EXIT_TO_USER (21*32+14) /* Use IBPB on exit-to-userspace, see VMSCAPE bug */
500500
#define X86_FEATURE_ABMC (21*32+15) /* Assignable Bandwidth Monitoring Counters */
501501
#define X86_FEATURE_MSR_IMM (21*32+16) /* MSR immediate form instructions */
502+
#define X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO (21*32+17) /*
503+
* Clear CPU buffers before VM-Enter if the vCPU
504+
* can access host MMIO (ignored for all intents
505+
* and purposes if CLEAR_CPU_BUF_VM is set).
506+
*/
502507

503508
/*
504509
* BUG word(s)

arch/x86/include/asm/hardirq.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <linux/threads.h>
66

77
typedef struct {
8-
#if IS_ENABLED(CONFIG_KVM_INTEL)
8+
#if IS_ENABLED(CONFIG_CPU_MITIGATIONS) && IS_ENABLED(CONFIG_KVM_INTEL)
99
u8 kvm_cpu_l1tf_flush_l1d;
1010
#endif
1111
unsigned int __nmi_count; /* arch dependent */
@@ -68,7 +68,7 @@ extern u64 arch_irq_stat(void);
6868
DECLARE_PER_CPU_CACHE_HOT(u16, __softirq_pending);
6969
#define local_softirq_pending_ref __softirq_pending
7070

71-
#if IS_ENABLED(CONFIG_KVM_INTEL)
71+
#if IS_ENABLED(CONFIG_CPU_MITIGATIONS) && IS_ENABLED(CONFIG_KVM_INTEL)
7272
/*
7373
* This function is called from noinstr interrupt contexts
7474
* and must be inlined to not get instrumentation.

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,9 +1055,6 @@ struct kvm_vcpu_arch {
10551055
/* be preempted when it's in kernel-mode(cpl=0) */
10561056
bool preempted_in_kernel;
10571057

1058-
/* Flush the L1 Data cache for L1TF mitigation on VMENTER */
1059-
bool l1tf_flush_l1d;
1060-
10611058
/* Host CPU on which VM-entry was most recently attempted */
10621059
int last_vmentry_cpu;
10631060

@@ -1456,8 +1453,6 @@ struct kvm_arch {
14561453
bool use_master_clock;
14571454
u64 master_kernel_ns;
14581455
u64 master_cycle_now;
1459-
struct delayed_work kvmclock_update_work;
1460-
struct delayed_work kvmclock_sync_work;
14611456

14621457
#ifdef CONFIG_KVM_HYPERV
14631458
struct kvm_hv hyperv;
@@ -2167,6 +2162,7 @@ void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu,
21672162
void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu);
21682163

21692164
void kvm_prepare_event_vectoring_exit(struct kvm_vcpu *vcpu, gpa_t gpa);
2165+
void kvm_prepare_unexpected_reason_exit(struct kvm_vcpu *vcpu, u64 exit_reason);
21702166

21712167
void kvm_enable_efer_bits(u64);
21722168
bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
@@ -2378,7 +2374,6 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
23782374
int kvm_add_user_return_msr(u32 msr);
23792375
int kvm_find_user_return_msr(u32 msr);
23802376
int kvm_set_user_return_msr(unsigned index, u64 val, u64 mask);
2381-
void kvm_user_return_msr_update_cache(unsigned int index, u64 val);
23822377
u64 kvm_get_user_return_msr(unsigned int slot);
23832378

23842379
static inline bool kvm_is_supported_user_return_msr(u32 msr)

arch/x86/include/asm/nospec-branch.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -308,24 +308,26 @@
308308
* CFLAGS.ZF.
309309
* Note: Only the memory operand variant of VERW clears the CPU buffers.
310310
*/
311-
.macro __CLEAR_CPU_BUFFERS feature
312311
#ifdef CONFIG_X86_64
313-
ALTERNATIVE "", "verw x86_verw_sel(%rip)", \feature
312+
#define VERW verw x86_verw_sel(%rip)
314313
#else
315-
/*
316-
* In 32bit mode, the memory operand must be a %cs reference. The data
317-
* segments may not be usable (vm86 mode), and the stack segment may not
318-
* be flat (ESPFIX32).
319-
*/
320-
ALTERNATIVE "", "verw %cs:x86_verw_sel", \feature
314+
/*
315+
* In 32bit mode, the memory operand must be a %cs reference. The data segments
316+
* may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32).
317+
*/
318+
#define VERW verw %cs:x86_verw_sel
321319
#endif
322-
.endm
323320

324-
#define CLEAR_CPU_BUFFERS \
325-
__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF
321+
/*
322+
* Provide a stringified VERW macro for simple usage, and a non-stringified
323+
* VERW macro for use in more elaborate sequences, e.g. to encode a conditional
324+
* VERW within an ALTERNATIVE.
325+
*/
326+
#define __CLEAR_CPU_BUFFERS __stringify(VERW)
326327

327-
#define VM_CLEAR_CPU_BUFFERS \
328-
__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF_VM
328+
/* If necessary, emit VERW on exit-to-userspace to clear CPU buffers. */
329+
#define CLEAR_CPU_BUFFERS \
330+
ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF
329331

330332
#ifdef CONFIG_X86_64
331333
.macro CLEAR_BRANCH_HISTORY
@@ -580,8 +582,6 @@ DECLARE_STATIC_KEY_FALSE(cpu_buf_idle_clear);
580582

581583
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
582584

583-
DECLARE_STATIC_KEY_FALSE(cpu_buf_vm_clear);
584-
585585
extern u16 x86_verw_sel;
586586

587587
#include <asm/segment.h>

arch/x86/kernel/cpu/bugs.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,6 @@ EXPORT_SYMBOL_GPL(cpu_buf_idle_clear);
192192
*/
193193
DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
194194

195-
/*
196-
* Controls CPU Fill buffer clear before VMenter. This is a subset of
197-
* X86_FEATURE_CLEAR_CPU_BUF, and should only be enabled when KVM-only
198-
* mitigation is required.
199-
*/
200-
DEFINE_STATIC_KEY_FALSE(cpu_buf_vm_clear);
201-
EXPORT_SYMBOL_GPL(cpu_buf_vm_clear);
202-
203195
#undef pr_fmt
204196
#define pr_fmt(fmt) "mitigations: " fmt
205197

@@ -489,8 +481,8 @@ static enum rfds_mitigations rfds_mitigation __ro_after_init =
489481
IS_ENABLED(CONFIG_MITIGATION_RFDS) ? RFDS_MITIGATION_AUTO : RFDS_MITIGATION_OFF;
490482

491483
/*
492-
* Set if any of MDS/TAA/MMIO/RFDS are going to enable VERW clearing
493-
* through X86_FEATURE_CLEAR_CPU_BUF on kernel and guest entry.
484+
* Set if any of MDS/TAA/MMIO/RFDS are going to enable VERW clearing on exit to
485+
* userspace *and* on entry to KVM guests.
494486
*/
495487
static bool verw_clear_cpu_buf_mitigation_selected __ro_after_init;
496488

@@ -536,6 +528,7 @@ static void __init mds_apply_mitigation(void)
536528
if (mds_mitigation == MDS_MITIGATION_FULL ||
537529
mds_mitigation == MDS_MITIGATION_VMWERV) {
538530
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
531+
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM);
539532
if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
540533
(mds_nosmt || smt_mitigations == SMT_MITIGATIONS_ON))
541534
cpu_smt_disable(false);
@@ -647,6 +640,7 @@ static void __init taa_apply_mitigation(void)
647640
* present on host, enable the mitigation for UCODE_NEEDED as well.
648641
*/
649642
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
643+
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM);
650644

651645
if (taa_nosmt || smt_mitigations == SMT_MITIGATIONS_ON)
652646
cpu_smt_disable(false);
@@ -748,9 +742,9 @@ static void __init mmio_apply_mitigation(void)
748742
*/
749743
if (verw_clear_cpu_buf_mitigation_selected) {
750744
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
751-
static_branch_disable(&cpu_buf_vm_clear);
745+
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM);
752746
} else {
753-
static_branch_enable(&cpu_buf_vm_clear);
747+
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO);
754748
}
755749

756750
/*
@@ -839,8 +833,10 @@ static void __init rfds_update_mitigation(void)
839833

840834
static void __init rfds_apply_mitigation(void)
841835
{
842-
if (rfds_mitigation == RFDS_MITIGATION_VERW)
836+
if (rfds_mitigation == RFDS_MITIGATION_VERW) {
843837
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
838+
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF_VM);
839+
}
844840
}
845841

846842
static __init int rfds_parse_cmdline(char *str)

0 commit comments

Comments
 (0)