linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arch: x86: power: cpu: init %gs before __restore_processor_state (clang)
@ 2020-09-15 17:26 rkir
  2020-09-15 17:46 ` Borislav Petkov
  2020-09-15 22:17 ` Pavel Machek
  0 siblings, 2 replies; 23+ messages in thread
From: rkir @ 2020-09-15 17:26 UTC (permalink / raw)
  To: rjw, pavel, tglx, mingo
  Cc: x86, linux-pm, gregkh, ndesaulniers, adelva, rkir, Haitao Shan

From: Haitao Shan <hshan@google.com>

This is a workaround which fixes triple fault
in __restore_processor_state on clang when
built with LTO.

When load_TR_desc and load_mm_ldt are inlined into
fix_processor_context due to LTO, they cause
fix_processor_context (or in this case __restore_processor_state,
as fix_processor_context was inlined into __restore_processor_state)
to access the stack canary through %gs, but before
__restore_processor_state has restored the previous value
of %gs properly. LLVM appears to be inlining functions with stack
protectors into functions compiled with -fno-stack-protector,
which is likely a bug in LLVM's inliner that needs to be fixed.

The LLVM bug is here: https://bugs.llvm.org/show_bug.cgi?id=47479

Signed-off-by: Haitao Shan <hshan@google.com>
Signed-off-by: Roman Kiryanov <rkir@google.com>
---
 arch/x86/power/cpu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c
index db1378c6ff26..e5677adb2d28 100644
--- a/arch/x86/power/cpu.c
+++ b/arch/x86/power/cpu.c
@@ -274,6 +274,16 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)
 /* Needed by apm.c */
 void notrace restore_processor_state(void)
 {
+#ifdef __clang__
+	// The following code snippet is copied from __restore_processor_state.
+	// Its purpose is to prepare GS segment before the function is called.
+#ifdef CONFIG_X86_64
+	wrmsrl(MSR_GS_BASE, saved_context.kernelmode_gs_base);
+#else
+	loadsegment(fs, __KERNEL_PERCPU);
+	loadsegment(gs, __KERNEL_STACK_CANARY);
+#endif
+#endif
 	__restore_processor_state(&saved_context);
 }
 #ifdef CONFIG_X86_32
-- 
2.28.0.618.gf4bc123cb7-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2020-10-04  9:59 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-15 17:26 [PATCH] arch: x86: power: cpu: init %gs before __restore_processor_state (clang) rkir
2020-09-15 17:46 ` Borislav Petkov
2020-09-15 17:57   ` Roman Kiryanov
2020-09-15 18:27     ` Borislav Petkov
2020-09-15 18:36       ` Roman Kiryanov
2020-09-15 18:52         ` Borislav Petkov
2020-09-15 18:55           ` Roman Kiryanov
2020-09-18 22:25         ` Pavel Machek
2020-09-21 23:28           ` Roman Kiryanov
2020-10-04  9:59             ` Pavel Machek
2020-09-15 18:00   ` Nick Desaulniers
2020-09-15 18:25     ` Borislav Petkov
2020-09-15 19:51       ` Nick Desaulniers
2020-09-15 20:20         ` Borislav Petkov
2020-09-15 21:49           ` Nick Desaulniers
2020-09-16  9:23             ` Borislav Petkov
2020-09-19 16:48             ` Pavel Machek
2020-09-16  8:17         ` peterz
2020-09-15 20:44     ` Arvind Sankar
2020-09-15 22:17 ` Pavel Machek
2020-09-15 22:21   ` Nick Desaulniers
2020-09-18 22:20     ` Pavel Machek
2020-09-15 23:13   ` Roman Kiryanov

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).