public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Mark Langsdorf <mark.langsdorf@amd.com>
To: Arjan van de Ven <arjan@infradead.org>
Cc: "Ingo Molnar" <mingo@elte.hu>,
	"Linus Torvalds" <torvalds@linux-foundation.org>,
	linux-kernel@vger.kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Thomas Gleixner" <tglx@linutronix.de>
Subject: Re: [PATCH](retry 3) invalidate caches before going into suspend
Date: Thu, 14 Aug 2008 08:45:04 -0500	[thread overview]
Message-ID: <200808140845.05160.mark.langsdorf@amd.com> (raw)
In-Reply-To: <20080813143206.07550498@infradead.org>

When a CPU core is shut down, all of its caches need to be flushed
to prevent stale data from causing errors if the core is resumed.
Current Linux suspend code performs an assignment after the flush,
which can add dirty data back to the cache.  On some AMD platforms,
additional speculative reads have caused crashes on resume because
of this dirty data.

Relocate the cache flush to be the very last thing done before 
halting.  Tie into an assembly line so the compile will not
reorder it.  Add some documentation explaining what is going
on and why we're doing this.

Signed-off-by: Mark Langsdorf <mark.langsdorf@amd.com>
Acked-by: Mark Borden <mark.borden@amd.com>
Acked-by: Michael Hohmuth <michael.hohmuth@amd.com>

diff -r 1e74a821dd00 arch/x86/kernel/process_32.c
--- a/arch/x86/kernel/process_32.c	Tue Aug 12 12:04:12 2008 -0500
+++ b/arch/x86/kernel/process_32.c	Thu Aug 14 03:04:52 2008 -0500
@@ -65,6 +65,28 @@ EXPORT_PER_CPU_SYMBOL(cpu_number);
 EXPORT_PER_CPU_SYMBOL(cpu_number);
 
 /*
+ * on systems with caches, caches must be flashed as the absolute
+ * last instruction before going into a suspended halt.  Otherwise,
+ * dirty data can linger in the cache and become stale on resume,
+ * leading to strange errors.
+ *
+ * perform a variety of operations to guarantee that the compiler
+ * will not reorder instructions.  wbinvd itself is serializing
+ * so the processor will not reorder.
+ *
+ * Systems without cache can just go into halt.
+ */
+static void wbinvd_halt(void) {
+        mb();
+        /* check for clflush to determine if wbinvd is legal */
+        if (cpu_has_clflush)
+		asm volatile("cli; wbinvd; 1: hlt; jmp 1b": : :"memory");
+        else
+                while (1)
+                        halt();
+}
+
+/*
  * Return saved PC of a blocked thread.
  */
 unsigned long thread_saved_pc(struct task_struct *tsk)
@@ -95,7 +117,6 @@ static inline void play_dead(void)
 {
 	/* This must be done before dead CPU ack */
 	cpu_exit_clear();
-	wbinvd();
 	mb();
 	/* Ack it */
 	__get_cpu_var(cpu_state) = CPU_DEAD;
@@ -104,8 +125,8 @@ static inline void play_dead(void)
 	 * With physical CPU hotplug, we should halt the cpu
 	 */
 	local_irq_disable();
-	while (1)
-		halt();
+	/* mask all interrupts, flush any and all caches, and halt */
+	wbinvd_halt();
 }
 #else
 static inline void play_dead(void)
diff -r 1e74a821dd00 arch/x86/kernel/process_64.c
--- a/arch/x86/kernel/process_64.c	Tue Aug 12 12:04:12 2008 -0500
+++ b/arch/x86/kernel/process_64.c	Thu Aug 14 03:04:42 2008 -0500
@@ -58,6 +58,28 @@ unsigned long kernel_thread_flags = CLON
 
 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
 
+/*
+ * on systems with caches, caches must be flashed as the absolute
+ * last instruction before going into a suspended halt.  Otherwise,
+ * dirty data can linger in the cache and become stale on resume,
+ * leading to strange errors.
+ *
+ * perform a variety of operations to guarantee that the compiler
+ * will not reorder instructions.  wbinvd itself is serializing
+ * so the processor will not reorder.
+ *
+ * Systems without cache can just go into halt.
+ */
+static void wbinvd_halt(void) {
+	mb(); 
+	/* check for clflush to determine if wbinvd is legal */
+	if (cpu_has_clflush)
+		asm volatile("cli; wbinvd; 1: hlt; jmp 1b": : :"memory");
+	else
+		while (1)
+			halt();
+}
+
 void idle_notifier_register(struct notifier_block *n)
 {
 	atomic_notifier_chain_register(&idle_notifier, n);
@@ -93,14 +115,13 @@ static inline void play_dead(void)
 static inline void play_dead(void)
 {
 	idle_task_exit();
-	wbinvd();
 	mb();
 	/* Ack it */
 	__get_cpu_var(cpu_state) = CPU_DEAD;
 
 	local_irq_disable();
-	while (1)
-		halt();
+	/* mask all interrupts, flush any and all caches, and halt */
+	wbinvd_halt();
 }
 #else
 static inline void play_dead(void)


  reply	other threads:[~2008-08-14 13:39 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-13 16:41 invalidate caches before going into suspend Mark Langsdorf
2008-08-13 16:47 ` Ingo Molnar
2008-08-13 16:53   ` H. Peter Anvin
2008-08-13 17:01     ` Ingo Molnar
2008-08-13 17:28       ` H. Peter Anvin
2008-08-13 17:35         ` Ingo Molnar
2008-08-13 17:37           ` H. Peter Anvin
2008-08-13 17:09   ` Mark Langsdorf
2008-08-13 17:17     ` Linus Torvalds
2008-08-13 17:30       ` Mark Langsdorf
2008-08-13 17:52         ` Ingo Molnar
2008-08-13 18:33           ` [PATCH](retry 2) " Mark Langsdorf
2008-08-13 18:42             ` Arjan van de Ven
2008-08-13 18:47               ` Langsdorf, Mark
2008-08-13 21:32                 ` Arjan van de Ven
2008-08-14 13:45                   ` Mark Langsdorf [this message]
2008-08-14 13:49                     ` [PATCH](retry 3) " Ingo Molnar
2008-08-14 14:00                     ` Arjan van de Ven
2008-08-14 14:11                     ` [PATCH](retry 4) " Mark Langsdorf
2008-08-15 12:04                       ` Ingo Molnar
2008-08-13 17:38     ` Ingo Molnar
2008-08-13 19:39 ` Andi Kleen

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=200808140845.05160.mark.langsdorf@amd.com \
    --to=mark.langsdorf@amd.com \
    --cc=arjan@infradead.org \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --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