[3/3] x86/mm: Reinitialize TLB state on hotplug and resume

Message ID 1522907497-14743-4-git-send-email-tyhicks@canonical.com
State New
Headers show
  • Fix deadlock on task switches with new microcode
Related show

Commit Message

Tyler Hicks April 5, 2018, 5:51 a.m.
From: Andy Lutomirski <luto@kernel.org>


When Linux brings a CPU down and back up, it switches to init_mm and then
loads swapper_pg_dir into CR3.  With PCID enabled, this has the side effect
of masking off the ASID bits in CR3.

This can result in some confusion in the TLB handling code.  If we
bring a CPU down and back up with any ASID other than 0, we end up
with the wrong ASID active on the CPU after resume.  This could
cause our internal state to become corrupt, although major
corruption is unlikely because init_mm doesn't have any user pages.
More obviously, if CONFIG_DEBUG_VM=y, we'll trip over an assertion
in the next context switch.  The result of *that* is a failure to
resume from suspend with probability 1 - 1/6^(cpus-1).

Fix it by reinitializing cpu_tlbstate on resume and CPU bringup.

Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Reported-by: Jiri Kosina <jikos@kernel.org>
Fixes: 10af6235e0d3 ("x86/mm: Implement PCID based optimization: try to preserve old TLB entries using PCID")
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(backported from commit 72c0098d92cedb11c7e0151e84918840a4e96b31)
[tyhicks: initialize_tlbstate_and_flush() was added in 72be211ba]
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
 arch/x86/include/asm/tlbflush.h | 2 ++
 arch/x86/kernel/cpu/common.c    | 2 ++
 arch/x86/power/cpu.c            | 1 +
 3 files changed, 5 insertions(+)


diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index 2c75b76..c3427e9 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -331,6 +331,8 @@  static inline void invalidate_user_asid(u16 asid)
 		  (unsigned long *)this_cpu_ptr(&cpu_tlbstate.user_pcid_flush_mask));
+extern void initialize_tlbstate_and_flush(void);
  * flush the entire current user mapping
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 01abbf6..0fc65de 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1563,6 +1563,7 @@  void cpu_init(void)
 	me->active_mm = &init_mm;
+	initialize_tlbstate_and_flush();
 	enter_lazy_tlb(&init_mm, me);
@@ -1620,6 +1621,7 @@  void cpu_init(void)
 	curr->active_mm = &init_mm;
+	initialize_tlbstate_and_flush();
 	enter_lazy_tlb(&init_mm, curr);
diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c
index 2a717e0..e90f1c7 100644
--- a/arch/x86/power/cpu.c
+++ b/arch/x86/power/cpu.c
@@ -183,6 +183,7 @@  static void fix_processor_context(void)
 	load_TR_desc();				/* This does ltr */
 	load_mm_ldt(current->active_mm);	/* This does lldt */
+	initialize_tlbstate_and_flush();