[PATCH] x86/entry/64: Fix native_load_gs_index() SWAPGS handling with IRQ state tracing enabled

From: Ingo Molnar
Date: Wed Nov 29 2017 - 01:43:27 EST


Jarkko Nikula reported a S2R resume hang regression and bisected it back to:

ca37e57bbe0c ("x86/entry/64: Add missing irqflags tracing to native_load_gs_index()")

Turns out the GS handling of that patch is wrong: when IRQ state tracing is
enabled it calls a kernel function (as part of the TRACE_IRQS_*() functionality),
but we have not switched to the kernel GS yet ...

Fix the SWAPGS handling and also annotate every affected SWAPGS
instance to document the intended state of GS.

Reported-by: Jarkko Nikula <jarkko.nikula@xxxxxxxxxxxxxxx>
Bisected-by: Jarkko Nikula <jarkko.nikula@xxxxxxxxxxxxxxx>
Cc: Andy Lutomirski <luto@xxxxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Link: http://lkml.kernel.org/r/0fede9f9-88b0-a6e7-1027-dfb2019b8ef2@xxxxxxxxxxxxxxx
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
---
arch/x86/entry/entry_64.S | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index f81d50d7ceac..c0b52df8ee4f 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -945,16 +945,16 @@ idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
*/
ENTRY(native_load_gs_index)
FRAME_BEGIN
+ SWAPGS /* switch from user GS to kernel GS */
pushfq
DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
TRACE_IRQS_OFF
- SWAPGS
.Lgs_change:
movl %edi, %gs
2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
- SWAPGS
TRACE_IRQS_FLAGS (%rsp)
popfq
+ SWAPGS /* switch from kernel GS to user GS */
FRAME_END
ret
ENDPROC(native_load_gs_index)
@@ -964,7 +964,7 @@ EXPORT_SYMBOL(native_load_gs_index)
.section .fixup, "ax"
/* running with kernelgs */
bad_gs:
- SWAPGS /* switch back to user gs */
+ SWAPGS /* switch back to user GS, to modify GS */
.macro ZAP_GS
/* This can't be a string because the preprocessor needs to see it. */
movl $__USER_DS, %eax
@@ -973,6 +973,7 @@ EXPORT_SYMBOL(native_load_gs_index)
ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
xorl %eax, %eax
movl %eax, %gs
+ SWAPGS /* switch to kernel GS again before continuing */
jmp 2b
.previous