All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andi Kleen <andi@firstfloor.org>
To: speck@linutronix.de
Cc: Andi Kleen <ak@linux.intel.com>
Subject: [MODERATED] [PATCH v5 03/27] MDSv5 16
Date: Fri, 18 Jan 2019 16:50:18 -0800	[thread overview]
Message-ID: <6bfd31477bf279d9af286a17ad6b87c7f6656ddf.1547858934.git.ak@linux.intel.com> (raw)
In-Reply-To: <cover.1547858934.git.ak@linux.intel.com>
In-Reply-To: <cover.1547858934.git.ak@linux.intel.com>

From: Andi Kleen <ak@linux.intel.com>
Subject:  x86/speculation/mds: Support clearing CPU data on
 kernel exit

Add infrastructure for clearing CPU data on kernel exit

Instead of clearing unconditionally we support clearing
lazily when some kernel subsystem touched sensitive data
and sets the new TIF_CLEAR_CPU flag.

We handle TIF_CLEAR_CPU in kernel exit, similar to
other kernel exit action flags.

The flushing is provided by new microcode as a new side
effect of the otherwise unused VERW instruction.

So far this patch doesn't do anything, it relies on
later patches to set TIF_CLEAR_CPU.

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Tested-by: Neelima Krishnan <neelima.krishnan@intel.com>
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
 arch/x86/entry/common.c            |  8 +++++++-
 arch/x86/include/asm/clearcpu.h    | 23 +++++++++++++++++++++++
 arch/x86/include/asm/thread_info.h |  2 ++
 3 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/include/asm/clearcpu.h

diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
index 7bc105f47d21..924f8dab2068 100644
--- a/arch/x86/entry/common.c
+++ b/arch/x86/entry/common.c
@@ -29,6 +29,7 @@
 #include <asm/desc.h>
 #include <asm/traps.h>
 #include <asm/vdso.h>
+#include <asm/clearcpu.h>
 #include <linux/uaccess.h>
 #include <asm/cpufeature.h>
 
@@ -132,7 +133,7 @@ static long syscall_trace_enter(struct pt_regs *regs)
 }
 
 #define EXIT_TO_USERMODE_LOOP_FLAGS				\
-	(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |	\
+	(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | _TIF_CLEAR_CPU |\
 	 _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY | _TIF_PATCH_PENDING)
 
 static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
@@ -170,6 +171,11 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
 		if (cached_flags & _TIF_USER_RETURN_NOTIFY)
 			fire_user_return_notifiers();
 
+		if (cached_flags & _TIF_CLEAR_CPU) {
+			clear_thread_flag(TIF_CLEAR_CPU);
+			clear_cpu();
+		}
+
 		/* Disable IRQs and retry */
 		local_irq_disable();
 
diff --git a/arch/x86/include/asm/clearcpu.h b/arch/x86/include/asm/clearcpu.h
new file mode 100644
index 000000000000..530ef619ac1b
--- /dev/null
+++ b/arch/x86/include/asm/clearcpu.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_CLEARCPU_H
+#define _ASM_CLEARCPU_H 1
+
+#include <linux/jump_label.h>
+#include <linux/sched/smt.h>
+#include <asm/alternative.h>
+#include <linux/thread_info.h>
+
+/*
+ * Clear CPU buffers to avoid side channels.
+ * We use microcode as a side effect of the obsolete VERW instruction
+ */
+
+static inline void clear_cpu(void)
+{
+	unsigned kernel_ds = __KERNEL_DS;
+	/* Has to be memory form, don't modify to use an register */
+	alternative_input("verw %[kernelds]", "", X86_FEATURE_NO_VERW,
+		[kernelds] "m" (kernel_ds));
+}
+
+#endif
diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
index e0eccbcb8447..0c1e3d71018e 100644
--- a/arch/x86/include/asm/thread_info.h
+++ b/arch/x86/include/asm/thread_info.h
@@ -95,6 +95,7 @@ struct thread_info {
 #define TIF_MEMDIE		20	/* is terminating due to OOM killer */
 #define TIF_POLLING_NRFLAG	21	/* idle is polling for TIF_NEED_RESCHED */
 #define TIF_IO_BITMAP		22	/* uses I/O bitmap */
+#define TIF_CLEAR_CPU		23	/* clear CPU on kernel exit */
 #define TIF_FORCED_TF		24	/* true if TF in eflags artificially */
 #define TIF_BLOCKSTEP		25	/* set when we want DEBUGCTLMSR_BTF */
 #define TIF_LAZY_MMU_UPDATES	27	/* task is updating the mmu lazily */
@@ -123,6 +124,7 @@ struct thread_info {
 #define _TIF_NOHZ		(1 << TIF_NOHZ)
 #define _TIF_POLLING_NRFLAG	(1 << TIF_POLLING_NRFLAG)
 #define _TIF_IO_BITMAP		(1 << TIF_IO_BITMAP)
+#define _TIF_CLEAR_CPU		(1 << TIF_CLEAR_CPU)
 #define _TIF_FORCED_TF		(1 << TIF_FORCED_TF)
 #define _TIF_BLOCKSTEP		(1 << TIF_BLOCKSTEP)
 #define _TIF_LAZY_MMU_UPDATES	(1 << TIF_LAZY_MMU_UPDATES)
-- 
2.17.2

  parent reply	other threads:[~2019-01-21 19:54 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-19  0:50 [MODERATED] [PATCH v5 00/27] MDSv5 19 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 01/27] MDSv5 26 Andi Kleen
2019-01-22  4:17   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22 12:46   ` Thomas Gleixner
2019-01-19  0:50 ` [MODERATED] [PATCH v5 02/27] MDSv5 14 Andi Kleen
2019-01-22  4:20   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22 12:51   ` Thomas Gleixner
2019-01-19  0:50 ` Andi Kleen [this message]
2019-01-22  4:23   ` [MODERATED] Re: [PATCH v5 03/27] MDSv5 16 Konrad Rzeszutek Wilk
2019-01-22 12:55   ` Thomas Gleixner
2019-01-27 21:58   ` Thomas Gleixner
2019-01-28  3:30     ` [MODERATED] " Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 04/27] MDSv5 15 Andi Kleen
2019-01-22  4:33   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22 12:59   ` Thomas Gleixner
2019-01-19  0:50 ` [MODERATED] [PATCH v5 05/27] MDSv5 21 Andi Kleen
2019-01-22  4:35   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22 13:01   ` Thomas Gleixner
2019-02-21 12:06   ` Thomas Gleixner
2019-01-19  0:50 ` [MODERATED] [PATCH v5 06/27] MDSv5 18 Andi Kleen
2019-01-21 22:41   ` [MODERATED] " Josh Poimboeuf
2019-01-22  1:16     ` Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 07/27] MDSv5 0 Andi Kleen
2019-01-22  4:39   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-27 22:09   ` Thomas Gleixner
2019-01-28  3:33     ` [MODERATED] " Andi Kleen
2019-01-28  8:29       ` Thomas Gleixner
2019-02-13 22:26   ` [MODERATED] " Tyler Hicks
2019-01-19  0:50 ` [MODERATED] [PATCH v5 08/27] MDSv5 13 Andi Kleen
2019-01-22  4:40   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-19  0:50 ` [MODERATED] [PATCH v5 09/27] MDSv5 23 Andi Kleen
2019-01-22  4:56   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22  7:26   ` Greg KH
2019-01-22 13:07   ` Thomas Gleixner
2019-01-19  0:50 ` [MODERATED] [PATCH v5 10/27] MDSv5 7 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 11/27] MDSv5 2 Andi Kleen
2019-01-22 13:11   ` Thomas Gleixner
2019-01-19  0:50 ` [MODERATED] [PATCH v5 12/27] MDSv5 6 Andi Kleen
2019-01-22 14:01   ` Thomas Gleixner
2019-01-22 15:42     ` Thomas Gleixner
2019-01-22 18:01     ` [MODERATED] " Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 13/27] MDSv5 17 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 14/27] MDSv5 3 Andi Kleen
2019-01-22  4:48   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22 15:58   ` Thomas Gleixner
2019-01-22 17:57     ` Thomas Gleixner
2019-01-23  1:35       ` [MODERATED] " Andi Kleen
2019-01-23  9:27         ` Thomas Gleixner
2019-01-23 16:02           ` [MODERATED] " Andi Kleen
2019-01-23 22:40             ` Josh Poimboeuf
2019-01-23 22:57               ` Josh Poimboeuf
2019-01-24  0:25                 ` Josh Poimboeuf
2019-01-24  2:26               ` Andi Kleen
2019-01-24 12:04             ` Thomas Gleixner
2019-01-28  3:42               ` [MODERATED] " Andi Kleen
2019-01-28  8:33                 ` Thomas Gleixner
2019-02-16  2:00       ` [MODERATED] " Andi Kleen
2019-02-16 10:32         ` Thomas Gleixner
2019-02-16 16:58           ` [MODERATED] " Andi Kleen
2019-02-16 17:12             ` Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 15/27] MDSv5 1 Andi Kleen
2019-01-22  4:48   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-19  0:50 ` [MODERATED] [PATCH v5 16/27] MDSv5 10 Andi Kleen
2019-01-22  4:54   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-22  7:33   ` Greg KH
2019-01-19  0:50 ` [MODERATED] [PATCH v5 17/27] MDSv5 9 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 18/27] MDSv5 8 Andi Kleen
2019-01-22  5:07   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-19  0:50 ` [MODERATED] [PATCH v5 19/27] MDSv5 12 Andi Kleen
2019-01-22  5:09   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-19  0:50 ` [MODERATED] [PATCH v5 20/27] MDSv5 27 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 21/27] MDSv5 20 Andi Kleen
2019-01-22  5:11   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-19  0:50 ` [MODERATED] [PATCH v5 22/27] MDSv5 24 Andi Kleen
2019-01-21 21:24   ` [MODERATED] " Linus Torvalds
2019-01-22  1:22     ` Andi Kleen
2019-01-22 16:09       ` Thomas Gleixner
2019-01-22 17:56         ` [MODERATED] " Andi Kleen
2019-01-22 18:56           ` Thomas Gleixner
2019-01-23  1:39             ` [MODERATED] " Andi Kleen
2019-01-23  6:39               ` Greg KH
2019-01-24  9:55               ` Thomas Gleixner
2019-01-19  0:50 ` [MODERATED] [PATCH v5 23/27] MDSv5 22 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 24/27] MDSv5 5 Andi Kleen
2019-01-21 21:20   ` [MODERATED] " Linus Torvalds
2019-01-19  0:50 ` [MODERATED] [PATCH v5 25/27] MDSv5 4 Andi Kleen
2019-01-22  5:15   ` [MODERATED] " Konrad Rzeszutek Wilk
2019-01-19  0:50 ` [MODERATED] [PATCH v5 26/27] MDSv5 11 Andi Kleen
2019-01-19  0:50 ` [MODERATED] [PATCH v5 27/27] MDSv5 25 Andi Kleen
2019-01-21 21:18 ` [MODERATED] Re: [PATCH v5 00/27] MDSv5 19 Linus Torvalds
2019-01-22  1:14   ` Andi Kleen
2019-01-22  7:38     ` Greg KH
2019-01-28 11:34 ` Thomas Gleixner
2019-02-13 22:33   ` [MODERATED] " Tyler Hicks
2019-02-14 13:09     ` Jiri Kosina
2019-02-14 13:51       ` Greg KH
2019-02-14 16:53       ` Andi Kleen
2019-02-14 18:00         ` Greg KH
2019-02-14 18:05           ` Andrew Cooper
2019-02-14 18:33           ` Andi Kleen
2019-02-14 18:52             ` Greg KH
2019-02-14 19:50               ` Andi Kleen
2019-02-15  7:06                 ` Greg KH
2019-02-15 13:06                   ` Andi Kleen
2019-02-19 12:12                     ` Greg KH

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=6bfd31477bf279d9af286a17ad6b87c7f6656ddf.1547858934.git.ak@linux.intel.com \
    --to=andi@firstfloor.org \
    --cc=ak@linux.intel.com \
    --cc=speck@linutronix.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.