Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 02 Jul 2018 23:45:31 -0000 Received: from smtp.eu.citrix.com ([185.25.65.24]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fa8Vd-0006j7-Ry for speck@linutronix.de; Tue, 03 Jul 2018 01:45:30 +0200 Subject: [MODERATED] Re: ***UNCHECKED*** [patch V5 05/10] KVM magic # 5 References: <20180702154426.910579106@linutronix.de> <20180702160528.906801335@linutronix.de> From: Andrew Cooper Message-ID: <3740a705-86f3-8644-4a4b-bc88880b2202@citrix.com> Date: Tue, 3 Jul 2018 00:45:05 +0100 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="husAo0rNyZgakSjuZV4Ze2xubOgGE53Ie"; protected-headers="v1" To: speck@linutronix.de List-ID: --husAo0rNyZgakSjuZV4Ze2xubOgGE53Ie Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-GB On 02/07/2018 22:19, speck for Alexander Graf wrote: > > On 02.07.18 17:44, speck for Thomas Gleixner wrote: >> Subject: [patch V5 05/10] x86/KVM/VMX: Add L1D flush logic >> From: Paolo Bonzini pbonzini@redhat.com >> >> Add the logic for flushing L1D on VMENTER. The flush depends on the st= atic >> key being enabled and the new l1tf_flush_l1d flag being set. >> >> The flags is set: >> - Always, if the flush module parameter is 'always' >> >> - Conditionally at: >> - Entry to vcpu_run(), i.e. after executing user space >> >> - From the sched_in notifier, i.e. when switching to a vCPU thread.= >> >> - From vmexit handlers which are considered unsafe, i.e. where >> sensitive data can be brought into L1D: >> >> - The emulator, which could be a good target for other speculativ= e >> execution-based threats, >> >> - The MMU, which can bring host page tables in the L1 cache. >> =20 >> - External interrupts >> >> - Nested operations that require the MMU (see above). That is >> vmptrld, vmptrst, vmclear,vmwrite,vmread. >> >> - When handling invept,invvpid >> >> [ tglx: Split out from combo patch and reduced to a single flag ] >> >> Signed-off-by: Paolo Bonzini >> Signed-off-by: Konrad Rzeszutek Wilk >> Signed-off-by: Thomas Gleixner >> --- >> v4: Fix from Thomas using static key >> v5: Split it out from the big combo patch >> Simplify the flags logic >> >> arch/x86/include/asm/kvm_host.h | 4 ++++ >> arch/x86/kvm/mmu.c | 1 + >> arch/x86/kvm/vmx.c | 22 +++++++++++++++++++++- >> arch/x86/kvm/x86.c | 7 +++++++ >> 4 files changed, 33 insertions(+), 1 deletion(-) >> >> --- a/arch/x86/include/asm/kvm_host.h >> +++ b/arch/x86/include/asm/kvm_host.h >> @@ -713,6 +713,9 @@ struct kvm_vcpu_arch { >> =20 >> /* be preempted when it's in kernel-mode(cpl=3D0) */ >> bool preempted_in_kernel; >> + >> + /* Flush the L1 Data cache for L1TF mitigation on VMENTER */ >> + bool l1tf_flush_l1d; >> }; >> =20 >> struct kvm_lpage_info { >> @@ -881,6 +884,7 @@ struct kvm_vcpu_stat { >> u64 signal_exits; >> u64 irq_window_exits; >> u64 nmi_window_exits; >> + u64 l1d_flush; >> u64 halt_exits; >> u64 halt_successful_poll; >> u64 halt_attempted_poll; >> --- a/arch/x86/kvm/mmu.c >> +++ b/arch/x86/kvm/mmu.c >> @@ -3840,6 +3840,7 @@ int kvm_handle_page_fault(struct kvm_vcp >> { >> int r =3D 1; >> =20 >> + vcpu->arch.l1tf_flush_l1d =3D true; >> switch (vcpu->arch.apf.host_apf_reason) { >> default: >> trace_kvm_page_fault(fault_address, error_code); >> --- a/arch/x86/kvm/vmx.c >> +++ b/arch/x86/kvm/vmx.c >> @@ -9576,9 +9576,20 @@ static int vmx_handle_exit(struct kvm_vc >> #define L1D_CACHE_ORDER 4 >> static void *vmx_l1d_flush_pages; >> =20 >> -static void __maybe_unused vmx_l1d_flush(void) >> +static void vmx_l1d_flush(struct kvm_vcpu *vcpu) >> { >> int size =3D PAGE_SIZE << L1D_CACHE_ORDER; >> + bool always; >> + >> + /* >> + * If the mitigation mode is 'flush always', keep the flush bit >> + * set, otherwise clear it. It gets set again either from >> + * vcpu_run() or from one of the unsafe VMEXIT handlers. >> + */ >> + always =3D vmentry_l1d_flush =3D=3D VMENTER_L1D_FLUSH_ALWAYS; >> + vcpu->arch.l1tf_flush_l1d =3D always; >> + >> + vcpu->stat.l1d_flush++; >> =20 >> if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { >> wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); >> @@ -9847,6 +9858,7 @@ static void vmx_handle_external_intr(str >> [ss]"i"(__KERNEL_DS), >> [cs]"i"(__KERNEL_CS) >> ); >> + vcpu->arch.l1tf_flush_l1d =3D true; > This means we flush when an external interrupt is the trap reason, but > we may as well get the interrupt only after interrupts are enabled agai= n > from vcpu_enter_guest(), no? How is an interrupt executing "normally" > any different from an interrupt that causes a #vmexit? > > Or in other words: Is it worth it to have anything but always/never as > an option? Has anyone done benchmarks? First pass Xen benchmarks on no-HT and L1D_FLUSH on every vmenter hits up to 50% depending on workload.=C2=A0 Specifically aggregate small block= random access got hit the most, whereas aggregate small packet bandwidth only took a 20% hit IIRC. We are still investigating, but the block side of things is a very unattractive.=C2=A0 OTOH, I remain to be convinced by the "we're only tak= ing interrupts and the prefetch isn't pulling in anything interesting" argument, because its a bit too woolly for my liking, especially in the presence of next-page-prefetch on Haswell and later. ~Andrew --husAo0rNyZgakSjuZV4Ze2xubOgGE53Ie--