linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ingo Molnar <mingo@kernel.org>
To: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Andy Lutomirski <luto@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Borislav Petkov <bp@alien8.de>
Subject: [PATCH] x86/entry/64: Fix native_load_gs_index() SWAPGS handling with IRQ state tracing enabled
Date: Wed, 29 Nov 2017 08:09:51 +0100	[thread overview]
Message-ID: <20171129070951.hjjjpbyilzaak4ig@gmail.com> (raw)
In-Reply-To: <0fede9f9-88b0-a6e7-1027-dfb2019b8ef2@linux.intel.com>


* Jarkko Nikula <jarkko.nikula@linux.intel.com> wrote:

> Hi
> 
> Suspend-to-ram and resume stopped working on v4.15-rc1 and I bisected it to
> commit ca37e57bbe0c ("x86/entry/64: Add missing irqflags tracing to
> native_load_gs_index()").
> 
> I noticed it on Intel Kabylake (core) and Apollolake (atom) based prototype
> machines. Symptoms are that machine appears to enter into suspend but
> resumes instantly and hangs. Unfortunately no logs.
> 
> If I revert ca37e57bbe0c on v4.15-rc1 it works as expected.

Hm, that commit looks broken with irq-tracing enabled.
Does the patch below fix it?

In fact the exception handler itself appears to have broken GS handling as well - 
I suspect it never triggers in practice, because it was broken forever.

Andy, do you concur?

On a related note, we should definitely extend the 'intended GS state' annotation 
comments I did in this patch to all SWAPGS instances - this way code review has a 
much higher chance of finding discrepancies between intent and actual code.

Thanks,

	Ingo

=================>
>From 769dbd33a272214c48c0fc5a17bed9c1597e222f Mon Sep 17 00:00:00 2001
From: Ingo Molnar <mingo@kernel.org>
Date: Wed, 29 Nov 2017 07:43:27 +0100
Subject: [PATCH] x86/entry/64: Fix native_load_gs_index() SWAPGS handling with IRQ state tracing enabled

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@linux.intel.com>
Bisected-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/0fede9f9-88b0-a6e7-1027-dfb2019b8ef2@linux.intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 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
 

  reply	other threads:[~2017-11-29  7:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-28 14:35 Suspend-to-ram/resume regression with commit ca37e57bbe0c Jarkko Nikula
2017-11-29  7:09 ` Ingo Molnar [this message]
2017-11-29  9:25   ` [PATCH] x86/entry/64: Fix native_load_gs_index() SWAPGS handling with IRQ state tracing enabled Jarkko Nikula
2017-11-29  9:28     ` Thomas Gleixner
2017-11-29 12:39       ` Jarkko Nikula
2017-11-29 12:47   ` Peter Zijlstra
2017-11-29 14:33     ` Andy Lutomirski
2017-11-29 14:56       ` David Laight
2017-11-29 16:22         ` Andy Lutomirski
2017-11-29 16:51           ` David Laight
2017-11-29 20:48             ` Linus Torvalds
2017-11-29 16:51           ` Andy Lutomirski
2017-11-29 17:57   ` Andy Lutomirski
2017-11-29 18:08     ` Peter Zijlstra
2017-11-29 18:12     ` Andy Lutomirski
2017-11-29 20:58       ` Linus Torvalds
2017-11-29 21:25         ` Andy Lutomirski
2017-11-29 21:41           ` Andy Lutomirski
2017-11-29 21:57             ` Andy Lutomirski
2017-11-30  8:42       ` Jarkko Nikula

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171129070951.hjjjpbyilzaak4ig@gmail.com \
    --to=mingo@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=bp@alien8.de \
    --cc=jarkko.nikula@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).