x86/bugs: KVM: L1TF and MMIO Stale Data cleanups
Subsystems (4)
Sean Christopherson
maintainerv5 cover x86/bugs: KVM: L1TF and MMIO Stale Data cleanups
Clean up KVM's handling of L1TF and MMIO Stale data, as the code has bit rotted a bit and is harder than it should be to understand, and has a few warts.
TL;DR:
- Unify L1TF flushing under per-CPU variable
- Bury L1TF L1D flushing under CONFIG_CPU_MITIGATIONS=y
- Move MMIO Stale Data into asm, and do VERW at most once per VM-Enter
To allow VMX to use ALTERNATIVE_2 to select slightly different flows for doing VERW, tweak the low lever macros in nospec-branch.h to define the instruction sequence, and then wrap it with __stringify() as needed.
As before, the non-VMX code is lightly tested (but there's far less chance for breakage there). For the VMX code, I verified the KVM side of things by hacking the code to force/clear various mitigations, and using ud2 to confirm the right path got selected.
v5:
- Collect reviews and acks.
- Add/improve comments for various macros and flows. [Everyone]
- s/CLEAR_CPU_BUFFERS_SEQ/VERW [Pawan, Boris]
- Use the on-stack copy of @flags instead of stashing information in RFLAGS' arithmetic flags. [Boris]
- Fix typos (hopefully). [Boris]
v4:
- https://lore.kernel.org/all/[email protected]
- Drop the patch to fallback to handling the MMIO mitigation if vmx_l1d_flush() doesn't flush, and instead use Pawan's approach of decoupling the two entirely.
- Replace the static branch with X86_FEATURE_CLEAR_CPU_BUF_MMIO so that it can be referenced in ALTERNATIVE macros.
- Decouple X86_FEATURE_CLEAR_CPU_BUF_VM from X86_FEATURE_CLEAR_CPU_BUF_MMIO (though they still interact and can both be set)
v3:
- https://lore.kernel.org/all/[email protected]
- [Pawan's series] https://lore.kernel.org/all/[email protected]
- Put the "raw" variant in KVM, dress it up with KVM's "request" terminology,
and add a comment explaining why _KVM_ knows its usage doesn't need to
disable virtualization.
- Add the prep patches.
v2:
- https://lore.kernel.org/all/[email protected]
- Moved the bit back to irq_stat
- Fixed DEBUG_PREEMPT issues by adding a _raw variant
v1: https://lore.kernel.org/r/[email protected]
Brendan Jackman (1):
KVM: x86: Unify L1TF flushing under per-CPU variable
Pawan Gupta (1):
x86/bugs: Use VM_CLEAR_CPU_BUFFERS in VMX as well
Sean Christopherson (7):
KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run() x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition x86/bugs: Use an x86 feature to track the MMIO Stale Data mitigation
KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via
ALTERNATIVES_2
x86/bugs: KVM: Move VM_CLEAR_CPU_BUFFERS into SVM as
SVM_CLEAR_CPU_BUFFERS
KVM: VMX: Bundle all L1 data cache flush mitigation code together
KVM: VMX: Disable L1TF L1 data cache flush if CONFIG_CPU_MITIGATIONS=n
arch/x86/include/asm/cpufeatures.h | 5 + arch/x86/include/asm/hardirq.h | 4 +- arch/x86/include/asm/kvm_host.h | 3 - arch/x86/include/asm/nospec-branch.h | 25 ++- arch/x86/kernel/cpu/bugs.c | 22 +-- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/spte.c | 2 +- arch/x86/kvm/svm/vmenter.S | 6 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/run_flags.h | 10 +- arch/x86/kvm/vmx/vmenter.S | 29 ++-- arch/x86/kvm/vmx/vmx.c | 235 ++++++++++++++------------- arch/x86/kvm/x86.c | 6 +- arch/x86/kvm/x86.h | 14 ++ 14 files changed, 193 insertions(+), 172 deletions(-)
base-commit: 16ec4fb4ac95d878b879192d280db2baeec43272
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Sean Christopherson
maintainerPATCH v5 1/9 KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run()
When testing for VMLAUNCH vs. VMRESUME, use the copy of @flags from the stack instead of first moving it to EBX, and then propagating VMX_RUN_VMRESUME to RFLAGS.CF (because RBX is clobbered with the guest value prior to the conditional branch to VMLAUNCH). Stashing information in RFLAGS is gross, especially with the writer and reader being bifurcated by yet more gnarly assembly code.
Opportunistically drop the SHIFT macros as they existed purely to allow the VM-Enter flow to use Bit Test.
--- arch/x86/kvm/vmx/run_flags.h | 10 +++------- arch/x86/kvm/vmx/vmenter.S | 13 ++++--------- 2 files changed, 7 insertions(+), 16 deletions(-)
arch/x86/kvm/vmx/run_flags.h +3 -7
@@ -2,12 +2,8 @@#ifndef __KVM_X86_VMX_RUN_FLAGS_H#define __KVM_X86_VMX_RUN_FLAGS_H -#define VMX_RUN_VMRESUME_SHIFT 0 -#define VMX_RUN_SAVE_SPEC_CTRL_SHIFT 1 -#define VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO_SHIFT 2 - -#define VMX_RUN_VMRESUME BIT(VMX_RUN_VMRESUME_SHIFT) -#define VMX_RUN_SAVE_SPEC_CTRL BIT(VMX_RUN_SAVE_SPEC_CTRL_SHIFT) -#define VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO BIT(VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO_SHIFT) +#define VMX_RUN_VMRESUME BIT(0) +#define VMX_RUN_SAVE_SPEC_CTRL BIT(1) +#define VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO BIT(2)#endif /* __KVM_X86_VMX_RUN_FLAGS_H */
arch/x86/kvm/vmx/vmenter.S +4 -9
@@ -92,7 +92,7 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Save @vmx for SPEC_CTRL handling */ push %_ASM_ARG1 - /* Save @flags for SPEC_CTRL handling */ + /* Save @flags (used for VMLAUNCH vs. VMRESUME and mitigations). */ push %_ASM_ARG3 /*
@@ -101,9 +101,6 @@ SYM_FUNC_START(__vmx_vcpu_run) */ push %_ASM_ARG2 - /* Copy @flags to EBX, _ASM_ARG3 is volatile. */ - mov %_ASM_ARG3L, %ebx - lea (%_ASM_SP), %_ASM_ARG2 call vmx_update_host_rsp
@@ -147,9 +144,6 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Load @regs to RAX. */ mov (%_ASM_SP), %_ASM_AX - /* Check if vmlaunch or vmresume is needed */ - bt $VMX_RUN_VMRESUME_SHIFT, %ebx - /* Load guest registers. Don't clobber flags. */ mov VCPU_RCX(%_ASM_AX), %_ASM_CX mov VCPU_RDX(%_ASM_AX), %_ASM_DX
@@ -173,8 +167,9 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Clobbers EFLAGS.ZF */ CLEAR_CPU_BUFFERS - /* Check EFLAGS.CF from the VMX_RUN_VMRESUME bit test above. */ - jnc .Lvmlaunch + /* Check @flags to see if vmlaunch or vmresume is needed. */ + testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP) + jz .Lvmlaunch /* * After a successful VMRESUME/VMLAUNCH, control flow "magically"
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 1/9] KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run()
Quoted 7 lines · Sean Christopherson
When testing for VMLAUNCH vs. VMRESUME, use the copy of @flags from the stack instead of first moving it to EBX, and then propagating
VMX_RUN_VMRESUMEto RFLAGS.CF (because RBX is clobbered with the guest value prior to the conditional branch to VMLAUNCH). Stashing information in RFLAGS is gross, especially with the writer and reader being bifurcated by yet more gnarly assembly code.
Quoted 5 lines
Opportunistically drop the SHIFT macros as they existed purely to allow the VM-Enter flow to use Bit Test.
Suggested-by Borislav Petkov <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v5 1/9] KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run()
In reply to Sean ChristophersonQuoted 71 lines · Sean Christopherson
When testing for VMLAUNCH vs. VMRESUME, use the copy of @flags from the stack instead of first moving it to EBX, and then propagating
VMX_RUN_VMRESUMEto RFLAGS.CF (because RBX is clobbered with the guest value prior to the conditional branch to VMLAUNCH). Stashing information in RFLAGS is gross, especially with the writer and reader being bifurcated by yet more gnarly assembly code.Opportunistically drop the SHIFT macros as they existed purely to allow the VM-Enter flow to use Bit Test.
Suggested-by Borislav Petkov <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/run_flags.h | 10 +++------- arch/x86/kvm/vmx/vmenter.S | 13 ++++--------- 2 files changed, 7 insertions(+), 16 deletions(-)arch/x86/kvm/vmx/run_flags.h +3
-7@@ -2,12 +2,8 @@#ifndef __KVM_X86_VMX_RUN_FLAGS_H#define __KVM_X86_VMX_RUN_FLAGS_H -#define VMX_RUN_VMRESUME_SHIFT 0 -#define VMX_RUN_SAVE_SPEC_CTRL_SHIFT 1 -#define VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO_SHIFT 2 - -#define VMX_RUN_VMRESUME BIT(VMX_RUN_VMRESUME_SHIFT) -#define VMX_RUN_SAVE_SPEC_CTRL BIT(VMX_RUN_SAVE_SPEC_CTRL_SHIFT) -#define VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO BIT(VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO_SHIFT) +#define VMX_RUN_VMRESUME BIT(0) +#define VMX_RUN_SAVE_SPEC_CTRL BIT(1) +#define VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO BIT(2)#endif /* __KVM_X86_VMX_RUN_FLAGS_H */ arch/x86/kvm/vmx/vmenter.S +1
-7@@ -92,7 +92,7 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Save @vmx for SPEC_CTRL handling */ push %_ASM_ARG1 - /* Save @flags for SPEC_CTRL handling */ + /* Save @flags (used for VMLAUNCH vs. VMRESUME and mitigations). */ push %_ASM_ARG3 /*@@ -101,9 +101,6 @@ SYM_FUNC_START(__vmx_vcpu_run) */ push %_ASM_ARG2 - /* Copy @flags to EBX, _ASM_ARG3 is volatile. */ - mov %_ASM_ARG3L, %ebx - lea (%_ASM_SP), %_ASM_ARG2 call vmx_update_host_rsp@@ -147,9 +144,6 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Load @regs to RAX. */ mov (%_ASM_SP), %_ASM_AX - /* Check if vmlaunch or vmresume is needed */ - bt $VMX_RUN_VMRESUME_SHIFT, %ebx - /* Load guest registers. Don't clobber flags. */ mov VCPU_RCX(%_ASM_AX), %_ASM_CX mov VCPU_RDX(%_ASM_AX), %_ASM_DX
arch/x86/kvm/vmx/vmenter.S SYM_FUNC_START(__vmx_vcpu_run) · line 172
Quoted 8 lines
arch/x86/kvm/vmx/vmenter.S +3
-2@@ -173,8 +167,9 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Clobbers EFLAGS.ZF */ CLEAR_CPU_BUFFERS - /* Check EFLAGS.CF from the VMX_RUN_VMRESUME bit test above. */ - jnc .Lvmlaunch + /* Check @flags to see if vmlaunch or vmresume is needed. */ + testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP) + jz .Lvmlaunch
You could use TESTB instead of TESTL in the above code to save 3 bytes of code and some memory bandwidth.
Assembler will report unwanted truncation if VMX_RUN_VRESUME ever becomes larger than 255.
BR,
Uros.
Sean Christopherson
maintainerRe: [PATCH v5 1/9] KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run()
Quoted 42 lines · Uros Bizjak
Quoted 29 lines · Sean Christopherson
arch/x86/kvm/vmx/vmenter.S +1
-7@@ -92,7 +92,7 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Save @vmx for SPEC_CTRL handling */ push %_ASM_ARG1 - /* Save @flags for SPEC_CTRL handling */ + /* Save @flags (used for VMLAUNCH vs. VMRESUME and mitigations). */ push %_ASM_ARG3 /*@@ -101,9 +101,6 @@ SYM_FUNC_START(__vmx_vcpu_run) */ push %_ASM_ARG2 - /* Copy @flags to EBX, _ASM_ARG3 is volatile. */ - mov %_ASM_ARG3L, %ebx - lea (%_ASM_SP), %_ASM_ARG2 call vmx_update_host_rsp@@ -147,9 +144,6 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Load @regs to RAX. */ mov (%_ASM_SP), %_ASM_AX - /* Check if vmlaunch or vmresume is needed */ - bt $VMX_RUN_VMRESUME_SHIFT, %ebx - /* Load guest registers. Don't clobber flags. */ mov VCPU_RCX(%_ASM_AX), %_ASM_CX mov VCPU_RDX(%_ASM_AX), %_ASM_DXarch/x86/kvm/vmx/vmenter.S SYM_FUNC_START(__vmx_vcpu_run) · line 171
Quoted 7 lines
arch/x86/kvm/vmx/vmenter.S +3
-2@@ -173,8 +167,9 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Clobbers EFLAGS.ZF */ CLEAR_CPU_BUFFERS - /* Check EFLAGS.CF from the VMX_RUN_VMRESUME bit test above. */ - jnc .Lvmlaunch + /* Check @flags to see if vmlaunch or vmresume is needed. */ + testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP) + jz .LvmlaunchYou could use TESTB instead of TESTL in the above code to save 3 bytes of code and some memory bandwidth.
Quoted 2 lines
Assembler will report unwanted truncation if
VMX_RUN_VRESUMEever becomes larger than 255.
Unfortunately, the warning with gcc isn't escalated to an error with -Werror, e.g. with KVM_WERROR=y. And AFAICT clang's assembler doesn't warn at all and happily generates garbage. E.g. with VMX_RUN_VMRESUME relocated to bit 10, clang generates this without a warning:
33c: f6 44 24 08 00 testb $0x0,0x8(%rsp) 341: 74 08 je 34b <__vmx_vcpu_run+0x9b> 343: 0f 01 c3 vmresume
versus the expected:
33c: f7 44 24 08 00 04 00 testl $0x400,0x8(%rsp) 343: 00 344: 74 08 je 34e <__vmx_vcpu_run+0x9e> 346: 0f 01 c3 vmresume
So for now at least, I'll stick with testl.
Re: [PATCH v5 1/9] KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run()
In reply to Sean ChristophersonQuoted 1 line · Sean Christopherson
Suggested-by Borislav Petkov <[email protected]>
Bah, I didn't suggest that - that's all your effort :)
Quoted 5 lines
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/run_flags.h | 10 +++------- arch/x86/kvm/vmx/vmenter.S | 13 ++++--------- 2 files changed, 7 insertions(+), 16 deletions(-)
...
arch/x86/kvm/vmx/vmenter.S SYM_FUNC_START(__vmx_vcpu_run) · line 170
Quoted 7 lines
arch/x86/kvm/vmx/vmenter.S +1
-2@@ -173,8 +167,9 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Clobbers EFLAGS.ZF */ CLEAR_CPU_BUFFERS - /* Check EFLAGS.CF from the VMX_RUN_VMRESUME bit test above. */ - jnc .Lvmlaunch + /* Check @flags to see if vmlaunch or vmresume is needed. */
VMRESUME/VMLAUNCH in caps, like the rest of the file. We like our x86 insns in all caps. :)
arch/x86/kvm/vmx/vmenter.S SYM_FUNC_START(__vmx_vcpu_run) · line 175
Quoted 6 lines
arch/x86/kvm/vmx/vmenter.S +2
-0+ testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP) + jz .Lvmlaunch /* * After a successful VMRESUME/VMLAUNCH, control flow "magically"
But yeah, AFAIU this code, that's a nice cleanup.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteSean Christopherson
maintainerPATCH v5 2/9 x86/bugs: Use VM_CLEAR_CPU_BUFFERS in VMX as well
In reply to Sean ChristophersonFrom: Pawan Gupta <[email protected]>
TSA mitigation:
d8010d4ba43e ("x86/bugs: Add a Transient Scheduler Attacks mitigation")
introduced VM_CLEAR_CPU_BUFFERS for guests on AMD CPUs. Currently on Intel CLEAR_CPU_BUFFERS is being used for guests which has a much broader scope (kernel->user also).
Make mitigations on Intel consistent with TSA. This would help handling the guest-only mitigations better in future.
--- arch/x86/kernel/cpu/bugs.c | 13 +++++++++---- arch/x86/kvm/vmx/vmenter.S | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/bugs.c +9 -4
@@ -194,7 +194,7 @@ DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);/* * Controls CPU Fill buffer clear before VMenter. This is a subset of -* X86_FEATURE_CLEAR_CPU_BUF, and should only be enabled when KVM-only +* X86_FEATURE_CLEAR_CPU_BUF_VM, and should only be enabled when KVM-only * mitigation is required. */ DEFINE_STATIC_KEY_FALSE (cpu_buf_vm_clear);
@@ -489,8 +489,8 @@ static enum rfds_mitigations rfds_mitigation __ro_after_init =IS_ENABLED (CONFIG_MITIGATION_RFDS ) ?RFDS_MITIGATION_AUTO :RFDS_MITIGATION_OFF ;/* -* Set if any of MDS/TAA/MMIO/RFDS are going to enable VERW clearing -* through X86_FEATURE_CLEAR_CPU_BUF on kernel and guest entry. +* Set if any of MDS/TAA/MMIO/RFDS are going to enable VERW clearing on exit to +* userspace *and* on entry to KVM guests. */ static bool verw_clear_cpu_buf_mitigation_selected__ro_after_init ;
@@ -536,6 +536,7 @@ static void __init mds_apply_mitigation(void)if (mds_mitigation ==MDS_MITIGATION_FULL || mds_mitigation ==MDS_MITIGATION_VMWERV ) {setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF ); +setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF_VM );if (!boot_cpu_has (X86_BUG_MSBDS_ONLY ) && (mds_nosmt || smt_mitigations ==SMT_MITIGATIONS_ON ))cpu_smt_disable (false);
@@ -647,6 +648,7 @@ static void __init taa_apply_mitigation(void)* present on host, enable the mitigation for UCODE_NEEDED as well. */ setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF ); +setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF_VM );if (taa_nosmt || smt_mitigations ==SMT_MITIGATIONS_ON )cpu_smt_disable (false);
@@ -748,6 +750,7 @@ static void __init mmio_apply_mitigation(void)*/ if (verw_clear_cpu_buf_mitigation_selected) {setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF ); +setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF_VM );static_branch_disable (&cpu_buf_vm_clear); }else {static_branch_enable (&cpu_buf_vm_clear);
@@ -839,8 +842,10 @@ static void __init rfds_update_mitigation(void)static void __init rfds_apply_mitigation (void ) { -if (rfds_mitigation ==RFDS_MITIGATION_VERW ) +if (rfds_mitigation ==RFDS_MITIGATION_VERW ) {setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF ); +setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF_VM ); + } }static __init int rfds_parse_cmdline (char *str)
arch/x86/kvm/vmx/vmenter.S +1 -1
@@ -165,7 +165,7 @@ SYM_FUNC_START(__vmx_vcpu_run) mov VCPU_RAX(%_ASM_AX), %_ASM_AX /* Clobbers EFLAGS.ZF */ - CLEAR_CPU_BUFFERS + VM_CLEAR_CPU_BUFFERS /* Check @flags to see if vmlaunch or vmresume is needed. */ testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP)
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 2/9] x86/bugs: Use VM_CLEAR_CPU_BUFFERS in VMX as well
Quoted 10 lines · Sean Christopherson
From: Pawan Gupta <[email protected]>
TSA mitigation:
d8010d4ba43e("x86/bugs: Add a Transient Scheduler Attacks mitigation")introduced
VM_CLEAR_CPU_BUFFERSfor guests on AMD CPUs. Currently on IntelCLEAR_CPU_BUFFERSis being used for guests which has a much broader scope (kernel->useralso).
Quoted 7 lines
Make mitigations on Intel consistent with TSA. This would help handling the guest-only mitigations better in future.
Signed-off-by Pawan Gupta <[email protected]>[sean: make CLEAR_CPU_BUF_VM mutually exclusive with the MMIO mitigation]Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v5 3/9 x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
In reply to Sean ChristophersonDecouple the use of ALTERNATIVE from the encoding of VERW to clear CPU buffers so that KVM can use ALTERNATIVE_2 to handle "always clear buffers" and "clear if guest can access host MMIO" in a single statement.
No functional change intended.
--- arch/x86/include/asm/nospec-branch.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/include/asm/nospec-branch.h +11 -11
@@ -308,24 +308,24 @@* CFLAGS.ZF. * Note: Only the memory operand variant of VERW clears the CPU buffers. */ -.macro __CLEAR_CPU_BUFFERS feature#ifdef CONFIG_X86_64 - ALTERNATIVE "", "verw x86_verw_sel(%rip)", \feature +#define VERW verw x86_verw_sel(%rip)#else -/* -* In 32bit mode, the memory operand must be a %cs reference. The data -* segments may not be usable (vm86 mode), and the stack segment may not -* be flat (ESPFIX32). -*/ - ALTERNATIVE "", "verw %cs:x86_verw_sel", \feature +/* +* In 32bit mode, the memory operand must be a %cs reference. The data segments +* may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32). +*/ +#define VERW verw %cs:x86_verw_sel#endif -.endm +#define __CLEAR_CPU_BUFFERS __stringify(VERW) + +/* If necessary, emit VERW on exit-to-userspace to clear CPU buffers. */ #define CLEAR_CPU_BUFFERS \ - __CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF + ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF#define VM_CLEAR_CPU_BUFFERS \ - __CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF_VM + ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM#ifdef CONFIG_X86_64 .macro CLEAR_BRANCH_HISTORY
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Borislav Petkov
maintainerRe: [PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Quoted 1 line · Sean Christopherson
+#define
__CLEAR_CPU_BUFFERS__stringify(VERW)
Let's get rid of one indirection level pls:
arch/x86/include/asm/nospec-branch.h +4 -6
@@ -309,23 +309,21 @@* Note: Only the memory operand variant of VERW clears the CPU buffers. */ #ifdef CONFIG_X86_64 -#define VERW verw x86_verw_sel(%rip) +#define VERW __stringify(verw x86_verw_sel(%rip))#else /* * In 32bit mode, the memory operand must be a %cs reference. The data segments * may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32). */ -#define VERW verw %cs:x86_verw_sel +#define VERW __stringify(verw %cs:x86_verw_sel)#endif -#define __CLEAR_CPU_BUFFERS __stringify(VERW) -/* If necessary, emit VERW on exit-to-userspace to clear CPU buffers. */ #define CLEAR_CPU_BUFFERS \ - ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF + ALTERNATIVE "", VERW, X86_FEATURE_CLEAR_CPU_BUF#define VM_CLEAR_CPU_BUFFERS \ - ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM + ALTERNATIVE "", VERW, X86_FEATURE_CLEAR_CPU_BUF_VM#ifdef CONFIG_X86_64 .macro CLEAR_BRANCH_HISTORY
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteSean Christopherson
maintainerRe: [PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Quoted 13 lines · Borislav Petkov
Quoted 1 line · Sean Christopherson
+#define
__CLEAR_CPU_BUFFERS__stringify(VERW)Let's get rid of one indirection level pls:
arch/x86/include/asm/nospec-branch.h +0
-0@@ -309,23 +309,21 @@* Note: Only the memory operand variant of VERW clears the CPU buffers. */ #ifdef CONFIG_X86_64
arch/x86/include/asm/nospec-branch.h line 319
Quoted 10 lines
arch/x86/include/asm/nospec-branch.h +2
-2-#define VERW verw x86_verw_sel(%rip) +#define VERW __stringify(verw x86_verw_sel(%rip))#else /* * In 32bit mode, the memory operand must be a %cs reference. The data segments * may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32). */ -#define VERW verw %cs:x86_verw_sel +#define VERW __stringify(verw %cs:x86_verw_sel)#endif
Brendan also brought this up in v4[*]. Unless there's a way to coerce ALTERNATIVE_2 into working with multiple strings, the layer of indirection is needed so that KVM can emit __stringify() for the entire sequence.
: Heh, I tried that, and AFAICT it simply can't work with the way ALTERNATIVE and : friends are implemented, as each paramater needs to be a single unbroken string. : : E.g. this : : diff --git a/arch/x86/kvm/vmx/vmenter.S b/arch/x86/kvm/vmx/vmenter.S : index 61a809790a58..ffa6bc2345e3 100644 : --- a/arch/x86/kvm/vmx/vmenter.S : +++ b/arch/x86/kvm/vmx/vmenter.S : @@ -63,6 +63,8 @@ : RET : .endm : : +#define CLEAR_CPU_BUFFERS_SEQ_STRING "verw x86_verw_sel(%rip)" : + : .section .noinstr.text, "ax" : : /** : @@ -169,9 +171,9 @@ SYM_FUNC_START(__vmx_vcpu_run) : : /* Clobbers EFLAGS.ZF */ : ALTERNATIVE_2 "", \ : - __stringify(jz .Lskip_clear_cpu_buffers; \ : - CLEAR_CPU_BUFFERS_SEQ; \ : - .Lskip_clear_cpu_buffers:), \ : + "jz .Lskip_clear_cpu_buffers; " \ : + CLEAR_CPU_BUFFERS_SEQ_STRING; \ : + ".Lskip_clear_cpu_buffers:", \ : X86_FEATURE_CLEAR_CPU_BUF_MMIO, \ : __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM : : yields wonderfully helpful error messages like so: : : arch/x86/kvm/vmx/vmenter.S: Assembler messages: : arch/x86/kvm/vmx/vmenter.S:173: Error: too many positional arguments : : If there's a magic incanation to get things to work, it's unknown to me.
Borislav Petkov
maintainerRe: [PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Quoted 3 lines · Sean Christopherson
Brendan also brought this up in v4[*]. Unless there's a way to coerce
ALTERNATIVE_2into working with multiple strings, the layer of indirection is needed so that KVM can emit__stringify()for the entire sequence.
Ah, yah, there was that.
Pls put a small comment above it so that we don't forget.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteSean Christopherson
maintainerPATCH v5 4/9 x86/bugs: Use an x86 feature to track the MMIO Stale Data mitigation
In reply to Sean ChristophersonConvert the MMIO Stale Data mitigation tracking from a static branch into an x86 feature flag so that it can be used via ALTERNATIVE_2 in KVM.
No functional change intended.
--- arch/x86/include/asm/cpufeatures.h | 5 +++++ arch/x86/include/asm/nospec-branch.h | 2 -- arch/x86/kernel/cpu/bugs.c | 11 +---------- arch/x86/kvm/mmu/spte.c | 2 +- arch/x86/kvm/vmx/vmx.c | 4 ++-- 5 files changed, 9 insertions(+), 15 deletions(-)
arch/x86/include/asm/cpufeatures.h +5 -0
@@ -501,6 +501,11 @@#define X86_FEATURE_ABMC (21*32+15)/* Assignable Bandwidth Monitoring Counters */ #define X86_FEATURE_MSR_IMM (21*32+16)/* MSR immediate form instructions */ #define X86_FEATURE_X2AVIC_EXT (21*32+17)/* AMD SVM x2AVIC support for 4k vCPUs */ +#define X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO (21*32+18)/* +* Clear CPU buffers before VM-Enter if the vCPU +* can access host MMIO (ignored for all intents +* and purposes if CLEAR_CPU_BUF_VM is set). +*/ /* * BUG word(s)
arch/x86/include/asm/nospec-branch.h +0 -2
@@ -580,8 +580,6 @@ DECLARE_STATIC_KEY_FALSE(cpu_buf_idle_clear);DECLARE_STATIC_KEY_FALSE (switch_mm_cond_l1d_flush); -DECLARE_STATIC_KEY_FALSE (cpu_buf_vm_clear); -extern u16 x86_verw_sel;#include <asm/segment.h>
arch/x86/kernel/cpu/bugs.c +1 -10
@@ -192,14 +192,6 @@ EXPORT_SYMBOL_GPL(cpu_buf_idle_clear);*/ DEFINE_STATIC_KEY_FALSE (switch_mm_cond_l1d_flush); -/* -* Controls CPU Fill buffer clear before VMenter. This is a subset of -* X86_FEATURE_CLEAR_CPU_BUF_VM, and should only be enabled when KVM-only -* mitigation is required. -*/ -DEFINE_STATIC_KEY_FALSE (cpu_buf_vm_clear); -EXPORT_SYMBOL_GPL (cpu_buf_vm_clear); -#undef pr_fmt#define pr_fmt (fmt) "mitigations: " fmt
@@ -751,9 +743,8 @@ static void __init mmio_apply_mitigation(void)if (verw_clear_cpu_buf_mitigation_selected) {setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF );setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF_VM ); -static_branch_disable (&cpu_buf_vm_clear); }else { -static_branch_enable (&cpu_buf_vm_clear); +setup_force_cpu_cap (X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO ); }/*
arch/x86/kvm/mmu/spte.c +1 -1
@@ -292,7 +292,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,mark_page_dirty_in_slot (vcpu->kvm, slot, gfn); } -if (static_branch_unlikely (&cpu_buf_vm_clear) && +if (cpu_feature_enabled (X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO ) && !kvm_vcpu_can_access_host_mmio (vcpu) &&kvm_is_mmio_pfn (pfn, &is_host_mmio))kvm_track_host_mmio_mapping (vcpu);
arch/x86/kvm/vmx/vmx.c +2 -2
@@ -903,7 +903,7 @@ unsigned int __vmx_vcpu_run_flags(struct vcpu_vmx *vmx)if (!msr_write_intercepted (vmx,MSR_IA32_SPEC_CTRL )) flags |=VMX_RUN_SAVE_SPEC_CTRL ; -if (static_branch_unlikely (&cpu_buf_vm_clear) && +if (cpu_feature_enabled (X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO ) &&kvm_vcpu_can_access_host_mmio (&vmx->vcpu)) flags |=VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO ;
@@ -7352,7 +7352,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,*/ if (static_branch_unlikely (&vmx_l1d_should_flush))vmx_l1d_flush (vcpu); -else if (static_branch_unlikely (&cpu_buf_vm_clear) && +else if (cpu_feature_enabled (X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO ) && (flags &VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO ))x86_clear_cpu_buffers ();
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Sean Christopherson
maintainerPATCH v5 5/9 KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via ALTERNATIVES_2
In reply to Sean ChristophersonRework the handling of the MMIO Stale Data mitigation to clear CPU buffers immediately prior to VM-Enter, i.e. in the same location that KVM emits a VERW for unconditional (at runtime) clearing. Co-locating the code and using a single ALTERNATIVES_2 makes it more obvious how VMX mitigates the various vulnerabilities.
Deliberately order the alternatives as:
0. Do nothing
1. Clear if vCPU can access MMIO
2. Clear always
since the last alternative wins in ALTERNATIVES_2(), i.e. so that KVM will honor the strictest mitigation (always clear CPU buffers) if multiple mitigations are selected. E.g. even if the kernel chooses to mitigate MMIO Stale Data via X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO, another mitigation may enable X86_FEATURE_CLEAR_CPU_BUF_VM, and that other thing needs to win.
Note, decoupling the MMIO mitigation from the L1TF mitigation also fixes a mostly-benign flaw where KVM wouldn't do any clearing/flushing if the L1TF mitigation is configured to conditionally flush the L1D, and the MMIO mitigation but not any other "clear CPU buffers" mitigation is enabled. For that specific scenario, KVM would skip clearing CPU buffers for the MMIO mitigation even though the kernel requested a clear on every VM-Enter.
Note #2, the flaw goes back to the introduction of the MDS mitigation. The MDS mitigation was inadvertently fixed by commit 43fb862de8f6 ("KVM/VMX:
Move VERW closer to VMentry for MDS mitigation"), but previous kernels that flush CPU buffers in vmx_vcpu_enter_exit() are affected (though it's unlikely the flaw is meaningfully exploitable even older kernels).
--- arch/x86/kvm/vmx/vmenter.S | 16 ++++++++++++++-- arch/x86/kvm/vmx/vmx.c | 13 ------------- 2 files changed, 14 insertions(+), 15 deletions(-)
arch/x86/kvm/vmx/vmenter.S +14 -2
@@ -71,6 +71,7 @@ * @regs: unsigned long * (to guest registers) * @flags: VMX_RUN_VMRESUME: use VMRESUME instead of VMLAUNCH * VMX_RUN_SAVE_SPEC_CTRL: save guest SPEC_CTRL into vmx->spec_ctrl + * VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO: vCPU can access host MMIO * * Returns: * 0 on VM-Exit, 1 on VM-Fail
@@ -164,8 +165,19 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Load guest RAX. This kills the @regs pointer! */ mov VCPU_RAX(%_ASM_AX), %_ASM_AX - /* Clobbers EFLAGS.ZF */ - VM_CLEAR_CPU_BUFFERS + /* + * Note, ALTERNATIVE_2 works in reverse order. If CLEAR_CPU_BUF_VM is + * enabled, do VERW unconditionally. If CPU_BUF_VM_MMIO is enabled, + * check @flags to see if the vCPU has access to host MMIO, and if so, + * do VERW. Else, do nothing (no mitigations needed/enabled). + */ + ALTERNATIVE_2 "", \ + __stringify(testl $VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO, WORD_SIZE(%_ASM_SP); \ + jz .Lskip_mmio_verw; \ + VERW; \ + .Lskip_mmio_verw:), \ + X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO, \ + __stringify(VERW), X86_FEATURE_CLEAR_CPU_BUF_VM /* Check @flags to see if vmlaunch or vmresume is needed. */ testl $VMX_RUN_VMRESUME, WORD_SIZE(%_ASM_SP)
arch/x86/kvm/vmx/vmx.c +0 -13
@@ -7340,21 +7340,8 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,guest_state_enter_irqoff (); -/* -* L1D Flush includes CPU buffer clear to mitigate MDS, but VERW -* mitigation for MDS is done late in VMentry and is still -* executed in spite of L1D Flush. This is because an extra VERW -* should not matter much after the big hammer L1D Flush. -* -* cpu_buf_vm_clear is used when system is not vulnerable to MDS/TAA, -* and is affected by MMIO Stale Data. In such cases mitigation in only -* needed against an MMIO capable guest. -*/ if (static_branch_unlikely (&vmx_l1d_should_flush))vmx_l1d_flush (vcpu); -else if (cpu_feature_enabled (X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO ) && - (flags &VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO )) -x86_clear_cpu_buffers ();vmx_disable_fb_clear (vmx);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 5/9] KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via ALTERNATIVES_2
Quoted 31 lines · Sean Christopherson
Rework the handling of the MMIO Stale Data mitigation to clear CPU buffers immediately prior to VM-Enter, i.e. in the same location that KVM emits a VERW for unconditional (at runtime) clearing. Co-locating the code and using a single
ALTERNATIVES_2makes it more obvious how VMX mitigates the various vulnerabilities.Deliberately order the alternatives as:
0. Do nothing
1. Clear if vCPU can access MMIO
2. Clear alwayssince the last alternative wins in
ALTERNATIVES_2(), i.e. so that KVM will honor the strictest mitigation (always clear CPU buffers) if multiple mitigations are selected. E.g. even if the kernel chooses to mitigate MMIO Stale Data viaX86_FEATURE_CLEAR_CPU_BUF_VM_MMIO, another mitigation may enableX86_FEATURE_CLEAR_CPU_BUF_VM, and that other thing needs to win.Note, decoupling the MMIO mitigation from the L1TF mitigation also fixes a mostly-benign flaw where KVM wouldn't do any clearing/flushing if the L1TF mitigation is configured to conditionally flush the L1D, and the MMIO mitigation but not any other "clear CPU buffers" mitigation is enabled. For that specific scenario, KVM would skip clearing CPU buffers for the MMIO mitigation even though the kernel requested a clear on every VM-Enter.
Note #2, the flaw goes back to the introduction of the MDS mitigation. The MDS mitigation was inadvertently fixed by commit
43fb862de8f6("KVM/VMX:
Move VERW closer to VMentry for MDS mitigation"), but previous kernels that flush CPU buffers invmx_vcpu_enter_exit()are affected (though it's unlikely the flaw is meaningfully exploitable even older kernels).
Quoted 4 lines
Fixes 650b68a0622f ("x86/kvm/vmx: Add MDS protection when L1D Flush is not active")Suggested-by Pawan Gupta <[email protected]>Reviewed-by Pawan Gupta <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v5 6/9 x86/bugs: KVM: Move VM_CLEAR_CPU_BUFFERS into SVM as SVM_CLEAR_CPU_BUFFERS
In reply to Sean ChristophersonNow that VMX encodes its own sequence for clearing CPU buffers, move VM_CLEAR_CPU_BUFFERS into SVM to minimize the chances of KVM botching a mitigation in the future, e.g. using VM_CLEAR_CPU_BUFFERS instead of checking multiple mitigation flags.
No functional change intended.
--- arch/x86/include/asm/nospec-branch.h | 3 --- arch/x86/kvm/svm/vmenter.S | 6 ++++-- 2 files changed, 4 insertions(+), 5 deletions(-)
arch/x86/include/asm/nospec-branch.h +0 -3
@@ -324,9 +324,6 @@#define CLEAR_CPU_BUFFERS \ ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF -#define VM_CLEAR_CPU_BUFFERS \ - ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM -#ifdef CONFIG_X86_64 .macro CLEAR_BRANCH_HISTORY ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP
arch/x86/kvm/svm/vmenter.S +4 -2
@@ -116,6 +116,8 @@ jmp 901b .endm +#define SVM_CLEAR_CPU_BUFFERS \ + ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM /** * __svm_vcpu_run - Run a vCPU via a transition to SVM guest mode
@@ -194,7 +196,7 @@ SYM_FUNC_START(__svm_vcpu_run) mov VCPU_RDI(%_ASM_DI), %_ASM_DI /* Clobbers EFLAGS.ZF */ - VM_CLEAR_CPU_BUFFERS + SVM_CLEAR_CPU_BUFFERS /* Enter guest mode */ 3: vmrun %_ASM_AX
@@ -366,7 +368,7 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run) mov KVM_VMCB_pa(%rax), %rax /* Clobbers EFLAGS.ZF */ - VM_CLEAR_CPU_BUFFERS + SVM_CLEAR_CPU_BUFFERS /* Enter guest mode */ 1: vmrun %rax
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Sean Christopherson
maintainerPATCH v5 7/9 KVM: VMX: Bundle all L1 data cache flush mitigation code together
In reply to Sean ChristophersonMove vmx_l1d_flush(), vmx_cleanup_l1d_flush(), and the vmentry_l1d_flush param code up in vmx.c so that all of the L1 data cache flushing code is bundled together. This will allow conditioning the mitigation code on CONFIG_CPU_MITIGATIONS=y with minimal #ifdefs.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 174 ++++++++++++++++++++--------------------- 1 file changed, 87 insertions(+), 87 deletions(-)
arch/x86/kvm/vmx/vmx.c +87 -87
@@ -302,6 +302,16 @@ static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)return 0 ; } +static void vmx_cleanup_l1d_flush (void ) +{ +if (vmx_l1d_flush_pages) { +free_pages ((unsigned long )vmx_l1d_flush_pages,L1D_CACHE_ORDER ); + vmx_l1d_flush_pages =NULL ; + } +/* Restore state so sysfs ignores VMX */ + l1tf_vmx_mitigation =VMENTER_L1D_FLUSH_AUTO ; +} +static int vmentry_l1d_flush_parse (const char *s) {unsigned int i;
@@ -352,6 +362,83 @@ static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)return sysfs_emit (s,"%s\n" , vmentry_l1d_param[l1tf_vmx_mitigation].option); } +/* +* Software based L1D cache flush which is used when microcode providing +* the cache control MSR is not loaded. +* +* The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to +* flush it is required to read in 64 KiB because the replacement algorithm +* is not exactly LRU. This could be sized at runtime via topology +* information but as all relevant affected CPUs have 32KiB L1D cache size +* there is no point in doing so. +*/ +static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu) +{ +int size =PAGE_SIZE <<L1D_CACHE_ORDER ; + +/* +* This code is only executed when the flush mode is 'cond' or +* 'always' +*/ +if (static_branch_likely (&vmx_l1d_flush_cond)) { +bool flush_l1d; + +/* +* Clear the per-vcpu flush bit, it gets set again if the vCPU +* is reloaded, i.e. if the vCPU is scheduled out or if KVM +* exits to userspace, or if KVM reaches one of the unsafe +* VMEXIT handlers, e.g. if KVM calls into the emulator. +*/ + flush_l1d = vcpu->arch.l1tf_flush_l1d; + vcpu->arch.l1tf_flush_l1d = false; + +/* +* Clear the per-cpu flush bit, it gets set again from +* the interrupt handlers. +*/ + flush_l1d |=kvm_get_cpu_l1tf_flush_l1d (); +kvm_clear_cpu_l1tf_flush_l1d (); + +if (!flush_l1d) +return ; + } + + vcpu->stat.l1d_flush++; + +if (static_cpu_has (X86_FEATURE_FLUSH_L1D )) { +native_wrmsrq (MSR_IA32_FLUSH_CMD ,L1D_FLUSH ); +return ; + } + + asmvolatile ( +/* First ensure the pages are in the TLB */ +"xorl %%eax, %%eax\n" +".Lpopulate_tlb:\n\t" +"movzbl (%[flush_pages], %%" _ASM_AX"), %%ecx\n\t" +"addl $4096, %%eax\n\t" +"cmpl %%eax, %[size]\n\t" +"jne .Lpopulate_tlb\n\t" +"xorl %%eax, %%eax\n\t" +"cpuid\n\t" +/* Now fill the cache */ +"xorl %%eax, %%eax\n" +".Lfill_cache:\n" +"movzbl (%[flush_pages], %%" _ASM_AX"), %%ecx\n\t" +"addl $64, %%eax\n\t" +"cmpl %%eax, %[size]\n\t" +"jne .Lfill_cache\n\t" +"lfence\n" + :: [flush_pages]"r" (vmx_l1d_flush_pages), + [size]"r" (size) + :"eax" ,"ebx" ,"ecx" ,"edx" ); +} + +static const struct kernel_param_ops vmentry_l1d_flush_ops = { + .set = vmentry_l1d_flush_set, + .get = vmentry_l1d_flush_get, +}; +module_param_cb (vmentry_l1d_flush, &vmentry_l1d_flush_ops,NULL ,0644 ); + static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx) {u64 msr;
@@ -404,12 +491,6 @@ static void vmx_update_fb_clear_dis(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx) vmx->disable_fb_clear = false; } -static const struct kernel_param_ops vmentry_l1d_flush_ops = { - .set = vmentry_l1d_flush_set, - .get = vmentry_l1d_flush_get, -}; -module_param_cb (vmentry_l1d_flush, &vmentry_l1d_flush_ops,NULL ,0644 ); -static u32 vmx_segment_access_rights (struct kvm_segment *var);void vmx_vmexit (void );
@@ -6673,77 +6754,6 @@ int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)return ret; } -/* -* Software based L1D cache flush which is used when microcode providing -* the cache control MSR is not loaded. -* -* The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to -* flush it is required to read in 64 KiB because the replacement algorithm -* is not exactly LRU. This could be sized at runtime via topology -* information but as all relevant affected CPUs have 32KiB L1D cache size -* there is no point in doing so. -*/ -static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu) -{ -int size =PAGE_SIZE <<L1D_CACHE_ORDER ; - -/* -* This code is only executed when the flush mode is 'cond' or -* 'always' -*/ -if (static_branch_likely (&vmx_l1d_flush_cond)) { -bool flush_l1d; - -/* -* Clear the per-vcpu flush bit, it gets set again if the vCPU -* is reloaded, i.e. if the vCPU is scheduled out or if KVM -* exits to userspace, or if KVM reaches one of the unsafe -* VMEXIT handlers, e.g. if KVM calls into the emulator. -*/ - flush_l1d = vcpu->arch.l1tf_flush_l1d; - vcpu->arch.l1tf_flush_l1d = false; - -/* -* Clear the per-cpu flush bit, it gets set again from -* the interrupt handlers. -*/ - flush_l1d |=kvm_get_cpu_l1tf_flush_l1d (); -kvm_clear_cpu_l1tf_flush_l1d (); - -if (!flush_l1d) -return ; - } - - vcpu->stat.l1d_flush++; - -if (static_cpu_has (X86_FEATURE_FLUSH_L1D )) { -native_wrmsrq (MSR_IA32_FLUSH_CMD ,L1D_FLUSH ); -return ; - } - - asmvolatile ( -/* First ensure the pages are in the TLB */ -"xorl %%eax, %%eax\n" -".Lpopulate_tlb:\n\t" -"movzbl (%[flush_pages], %%" _ASM_AX"), %%ecx\n\t" -"addl $4096, %%eax\n\t" -"cmpl %%eax, %[size]\n\t" -"jne .Lpopulate_tlb\n\t" -"xorl %%eax, %%eax\n\t" -"cpuid\n\t" -/* Now fill the cache */ -"xorl %%eax, %%eax\n" -".Lfill_cache:\n" -"movzbl (%[flush_pages], %%" _ASM_AX"), %%ecx\n\t" -"addl $64, %%eax\n\t" -"cmpl %%eax, %[size]\n\t" -"jne .Lfill_cache\n\t" -"lfence\n" - :: [flush_pages]"r" (vmx_l1d_flush_pages), - [size]"r" (size) - :"eax" ,"ebx" ,"ecx" ,"edx" ); -} -void vmx_update_cr8_intercept (struct kvm_vcpu *vcpu,int tpr,int irr) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu);
@@ -8671,16 +8681,6 @@ __init int vmx_hardware_setup(void)return r; } -static void vmx_cleanup_l1d_flush (void ) -{ -if (vmx_l1d_flush_pages) { -free_pages ((unsigned long )vmx_l1d_flush_pages,L1D_CACHE_ORDER ); - vmx_l1d_flush_pages =NULL ; - } -/* Restore state so sysfs ignores VMX */ - l1tf_vmx_mitigation =VMENTER_L1D_FLUSH_AUTO ; -} -void vmx_exit (void ) { allow_smaller_maxphyaddr = false;
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Sean Christopherson
maintainerPATCH v5 8/9 KVM: VMX: Disable L1TF L1 data cache flush if CONFIG_CPU_MITIGATIONS=n
In reply to Sean ChristophersonDisable support for flushing the L1 data cache to mitigate L1TF if CPU mitigations are disabled for the entire kernel. KVM's mitigation of L1TF is in no way special enough to justify ignoring CONFIG_CPU_MITIGATIONS=n.
Deliberately use CPU_MITIGATIONS instead of the more precise MITIGATION_L1TF, as MITIGATION_L1TF only controls the default behavior, i.e. CONFIG_MITIGATION_L1TF=n doesn't completely disable L1TF mitigations in the kernel.
Keep the vmentry_l1d_flush module param to avoid breaking existing setups, and leverage the .set path to alert the user to the fact that vmentry_l1d_flush will be ignored. Don't bother validating the incoming value; if an admin misconfigures vmentry_l1d_flush, the fact that the bad configuration won't be detected when running with CONFIG_CPU_MITIGATIONS=n is likely the least of their worries.
--- arch/x86/include/asm/hardirq.h | 4 +-- arch/x86/kvm/vmx/vmx.c | 56 ++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 14 deletions(-)
arch/x86/include/asm/hardirq.h +2 -2
@@ -5,7 +5,7 @@#include <linux/threads.h> typedef struct { -#if IS_ENABLED(CONFIG_KVM_INTEL) +#if IS_ENABLED(CONFIG_CPU_MITIGATIONS) && IS_ENABLED(CONFIG_KVM_INTEL) u8 kvm_cpu_l1tf_flush_l1d; #endif unsigned int __nmi_count; /* arch dependent */
@@ -68,7 +68,7 @@ extern u64 arch_irq_stat(void);DECLARE_PER_CPU_CACHE_HOT (u16, __softirq_pending);#define local_softirq_pending_ref __softirq_pending -#if IS_ENABLED (CONFIG_KVM_INTEL ) +#if IS_ENABLED (CONFIG_CPU_MITIGATIONS ) &&IS_ENABLED (CONFIG_KVM_INTEL )/* * This function is called from noinstr interrupt contexts * and must be inlined to not get instrumentation.
arch/x86/kvm/vmx/vmx.c +44 -12
104 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Sean Christopherson
maintainerPATCH v5 9/9 KVM: x86: Unify L1TF flushing under per-CPU variable
In reply to Sean ChristophersonFrom: Brendan Jackman <[email protected]>
Currently the tracking of the need to flush L1D for L1TF is tracked by two bits: one per-CPU and one per-vCPU.
The per-vCPU bit is always set when the vCPU shows up on a core, so there is no interesting state that's truly per-vCPU. Indeed, this is a requirement, since L1D is a part of the physical CPU.
So simplify this by combining the two bits.
The vCPU bit was being written from preemption-enabled regions. To play nice with those cases, wrap all calls from KVM and use a raw write so that request a flush with preemption enabled doesn't trigger what would effectively be DEBUG_PREEMPT false positives. Preemption doesn't need to be disabled, as kvm_arch_vcpu_load() will mark the new CPU as needing a flush if the vCPU task is migrated, or if userspace runs the vCPU on a different task.
--- arch/x86/include/asm/kvm_host.h | 3 --- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 20 +++++--------------- arch/x86/kvm/x86.c | 6 +++--- arch/x86/kvm/x86.h | 14 ++++++++++++++ 6 files changed, 24 insertions(+), 23 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -3
@@ -1055,9 +1055,6 @@ struct kvm_vcpu_arch {/* be preempted when it's in kernel-mode(cpl=0) */ bool preempted_in_kernel; -/* Flush the L1 Data cache for L1TF mitigation on VMENTER */ -bool l1tf_flush_l1d; -/* Host CPU on which VM-entry was most recently attempted */ int last_vmentry_cpu;
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4859,7 +4859,7 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,*/ BUILD_BUG_ON (lower_32_bits (PFERR_SYNTHETIC_MASK )); - vcpu->arch.l1tf_flush_l1d = true; +kvm_request_l1tf_flush_l1d ();if (!flags) {trace_kvm_page_fault (vcpu, fault_address, error_code);
arch/x86/kvm/vmx/nested.c +1 -1
@@ -3828,7 +3828,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) goto vmentry_failed;/* Hide L1D cache contents from the nested guest. */ - vcpu->arch.l1tf_flush_l1d = true; +kvm_request_l1tf_flush_l1d ();/* * Must happen outside of nested_vmx_enter_non_root_mode() as it will
arch/x86/kvm/vmx/vmx.c +5 -15
@@ -395,26 +395,16 @@ static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)* 'always' */ if (static_branch_likely (&vmx_l1d_flush_cond)) { -bool flush_l1d; -/* -* Clear the per-vcpu flush bit, it gets set again if the vCPU +* Clear the per-cpu flush bit, it gets set again if the vCPU * is reloaded, i.e. if the vCPU is scheduled out or if KVM * exits to userspace, or if KVM reaches one of the unsafe -* VMEXIT handlers, e.g. if KVM calls into the emulator. +* VMEXIT handlers, e.g. if KVM calls into the emulator, +* or from the interrupt handlers. */ - flush_l1d = vcpu->arch.l1tf_flush_l1d; - vcpu->arch.l1tf_flush_l1d = false; - -/* -* Clear the per-cpu flush bit, it gets set again from -* the interrupt handlers. -*/ - flush_l1d |=kvm_get_cpu_l1tf_flush_l1d (); +if (!kvm_get_cpu_l1tf_flush_l1d ()) +return ;kvm_clear_cpu_l1tf_flush_l1d (); - -if (!flush_l1d) -return ; } vcpu->stat.l1d_flush++;
arch/x86/kvm/x86.c +3 -3
@@ -5209,7 +5209,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); - vcpu->arch.l1tf_flush_l1d = true; +kvm_request_l1tf_flush_l1d ();if (vcpu->scheduled_out && pmu->version && pmu->event_count) { pmu->need_cleanup = true;
@@ -8032,7 +8032,7 @@ int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, unsigned int bytes, struct x86_exception *exception) {/* kvm_write_guest_virt_system can pull in tons of pages. */ - vcpu->arch.l1tf_flush_l1d = true; +kvm_request_l1tf_flush_l1d ();return kvm_write_guest_virt_helper (addr, val, bytes, vcpu,PFERR_WRITE_MASK , exception);
@@ -9440,7 +9440,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,return handle_emulation_failure (vcpu, emulation_type); } - vcpu->arch.l1tf_flush_l1d = true; +kvm_request_l1tf_flush_l1d ();if (!(emulation_type &EMULTYPE_NO_DECODE )) {kvm_clear_exception_queue (vcpu);
arch/x86/kvm/x86.h +14 -0
@@ -420,6 +420,20 @@ static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)return !(kvm->arch.disabled_quirks & quirk); } +static __always_inline void kvm_request_l1tf_flush_l1d(void) +{ +#if IS_ENABLED (CONFIG_CPU_MITIGATIONS ) &&IS_ENABLED (CONFIG_KVM_INTEL ) +/* +* Use a raw write to set the per-CPU flag, as KVM will ensure a flush +* even if preemption is currently enabled.. If the current vCPU task +* is migrated to a different CPU (or userspace runs the vCPU on a +* different task) before the next VM-Entry, then kvm_arch_vcpu_load() +* will request a flush on the new CPU. +*/ +raw_cpu_write (irq_stat.kvm_cpu_l1tf_flush_l1d,1 ); +#endif +} +void kvm_inject_realmode_interrupt (struct kvm_vcpu *vcpu,int irq,int inc_eip);u64 get_kvmclock_ns (struct kvm *kvm);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Quoted 11 lines · Sean Christopherson
Clean up KVM's handling of L1TF and MMIO Stale data, as the code has bit rotted a bit and is harder than it should be to understand, and has a few warts.
TL;DR:
- Unify L1TF flushing under per-CPU variable
- Bury L1TF L1D flushing underCONFIG_CPU_MITIGATIONS=y
- Move MMIO Stale Data into asm, and do VERW at most once per VM-Enter[...]
Applied to kvm-x86 misc, with fixups for Boris' feedback.
[1/9] KVM: VMX: Use on-stack copy of @flags in __vmx_vcpu_run()
https://github.com/kvm-x86/linux/commit/844afc1af3a9 [2/9] x86/bugs: Use VM_CLEAR_CPU_BUFFERS in VMX as well https://github.com/kvm-x86/linux/commit/aba7de6088be [3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition https://github.com/kvm-x86/linux/commit/afb99ffbd582 [4/9] x86/bugs: Use an x86 feature to track the MMIO Stale Data mitigation https://github.com/kvm-x86/linux/commit/f6106d41ec84 [5/9] KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via ALTERNATIVES_2 https://github.com/kvm-x86/linux/commit/e6ff1d61de51 [6/9] x86/bugs: KVM: Move VM_CLEAR_CPU_BUFFERS into SVM as SVM_CLEAR_CPU_BUFFERS https://github.com/kvm-x86/linux/commit/fc704b578976 [7/9] KVM: VMX: Bundle all L1 data cache flush mitigation code together https://github.com/kvm-x86/linux/commit/0abd9610d6c6 [8/9] KVM: VMX: Disable L1TF L1 data cache flush if CONFIG_CPU_MITIGATIONS=n https://github.com/kvm-x86/linux/commit/05bd63959a9d [9/9] KVM: x86: Unify L1TF flushing under per-CPU variable https://github.com/kvm-x86/linux/commit/38ee66cb1845