Skip to contentSkip to conversation

x86/speculation/mds: Flush store buffer after wake up from sleep

PATCH In review Zhenzhong Duan 2 participants 2 messages Jun 21, 2019 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/speculation/mds: Flush store buffer after wake up from sleep

Intel document says: "When a thread wakes from a sleep state, the store buffer is repartitioned again. This causes the store buffer to transfer store buffer entries from the thread that was already active to the one which just woke up."

To avoid data leak from sibling thread to the woken thread, flush store buffer right after wake up.

Move mds_idle_clear_cpu_buffers() after trace_hardirqs_on() to ensure all store buffer entries are flushed before sleep.

---
 arch/x86/include/asm/irqflags.h | 2 ++
 arch/x86/include/asm/mwait.h    | 6 ++++--
 2 files changed, 6 insertions(+), 2 deletions(-)
arch/x86/include/asm/irqflags.h +2 -0
index 8a0e56e..641c4d8 100644
@@ -58,12 +58,14 @@ static inline __cpuidle void native_safe_halt(void)
 {
 	mds_idle_clear_cpu_buffers();
 	asm volatile("sti; hlt": : :"memory");
+	mds_idle_clear_cpu_buffers();
 }
 
 static inline __cpuidle void native_halt(void)
 {
 	mds_idle_clear_cpu_buffers();
 	asm volatile("hlt": : :"memory");
+	mds_idle_clear_cpu_buffers();
 }
 
 #endif
arch/x86/include/asm/mwait.h +4 -2
index eb0f80c..1d145d9 100644
@@ -46,6 +46,7 @@ static inline void __mwait(unsigned long eax, unsigned long ecx)
 	/* "mwait %eax, %ecx;" */
 	asm volatile(".byte 0x0f, 0x01, 0xc9;"
 		     :: "a" (eax), "c" (ecx));
+	mds_idle_clear_cpu_buffers();
 }
 
 /*
@@ -86,12 +87,13 @@ static inline void __mwaitx(unsigned long eax, unsigned long ebx,
 
 static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
 {
-	mds_idle_clear_cpu_buffers();
-
 	trace_hardirqs_on();
+
+	mds_idle_clear_cpu_buffers();
 	/* "mwait %eax, %ecx;" */
 	asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
 		     :: "a" (eax), "c" (ecx));
+	mds_idle_clear_cpu_buffers();
 }
 
 /*
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 7 lines · Zhenzhong Duan

Intel document says: "When a thread wakes from a sleep state, the store buffer is repartitioned again. This causes the store buffer to transfer store buffer entries from the thread that was already active to the one which just woke up."

To avoid data leak from sibling thread to the woken thread, flush store buffer right after wake up.

That's a pointless exercise. The buffers are flushed again when returning to user space. Inside the kernel the potential leak is completely uninteresting unless you consider the kernel as a malicious entity.

Quoted 2 lines

Move mds_idle_clear_cpu_buffers() after trace_hardirqs_on() to ensure all store buffer entries are flushed before sleep.

I'm fine with that change, albeit trace_hardirqs_on() is hardly leaking somethimg interesting.

Thanks,

tglx