1 From 83c1822ea048f5429d8cc0ba7f662978c500db85 Mon Sep 17 00:00:00 2001
2 From: Thomas Gleixner <tglx@linutronix.de>
3 Date: Sun, 25 Nov 2018 19:33:54 +0100
4 Subject: [PATCH 46/76] x86/speculation: Enable prctl mode for spectre_v2_user
6 commit 7cc765a67d8e04ef7d772425ca5a2a1e2b894c15 upstream.
8 Now that all prerequisites are in place:
10 - Add the prctl command line option
12 - Default the 'auto' mode to 'prctl'
14 - When SMT state changes, update the static key which controls the
15 conditional STIBP evaluation on context switch.
17 - At init update the static key which controls the conditional IBPB
18 evaluation on context switch.
20 Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
21 Reviewed-by: Ingo Molnar <mingo@kernel.org>
22 Cc: Peter Zijlstra <peterz@infradead.org>
23 Cc: Andy Lutomirski <luto@kernel.org>
24 Cc: Linus Torvalds <torvalds@linux-foundation.org>
25 Cc: Jiri Kosina <jkosina@suse.cz>
26 Cc: Tom Lendacky <thomas.lendacky@amd.com>
27 Cc: Josh Poimboeuf <jpoimboe@redhat.com>
28 Cc: Andrea Arcangeli <aarcange@redhat.com>
29 Cc: David Woodhouse <dwmw@amazon.co.uk>
30 Cc: Tim Chen <tim.c.chen@linux.intel.com>
31 Cc: Andi Kleen <ak@linux.intel.com>
32 Cc: Dave Hansen <dave.hansen@intel.com>
33 Cc: Casey Schaufler <casey.schaufler@intel.com>
34 Cc: Asit Mallick <asit.k.mallick@intel.com>
35 Cc: Arjan van de Ven <arjan@linux.intel.com>
36 Cc: Jon Masters <jcm@redhat.com>
37 Cc: Waiman Long <longman9394@gmail.com>
38 Cc: Greg KH <gregkh@linuxfoundation.org>
39 Cc: Dave Stewart <david.c.stewart@intel.com>
40 Cc: Kees Cook <keescook@chromium.org>
41 Link: https://lkml.kernel.org/r/20181125185005.958421388@linutronix.de
42 [bwh: Backported to 4.9: adjust filename]
43 Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
44 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
46 Documentation/kernel-parameters.txt | 7 ++++-
47 arch/x86/kernel/cpu/bugs.c | 41 ++++++++++++++++++++++-------
48 2 files changed, 38 insertions(+), 10 deletions(-)
50 diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
51 index cef2cb9cbf8a..f87e4bf2ab25 100644
52 --- a/Documentation/kernel-parameters.txt
53 +++ b/Documentation/kernel-parameters.txt
54 @@ -4075,9 +4075,14 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
55 off - Unconditionally disable mitigations. Is
56 enforced by spectre_v2=off
58 + prctl - Indirect branch speculation is enabled,
59 + but mitigation can be enabled via prctl
60 + per thread. The mitigation control state
61 + is inherited on fork.
63 auto - Kernel selects the mitigation depending on
64 the available CPU features and vulnerability.
68 Not specifying this option is equivalent to
70 diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
71 index 3308f18a637d..e28598f3ab12 100644
72 --- a/arch/x86/kernel/cpu/bugs.c
73 +++ b/arch/x86/kernel/cpu/bugs.c
74 @@ -253,11 +253,13 @@ enum spectre_v2_user_cmd {
75 SPECTRE_V2_USER_CMD_NONE,
76 SPECTRE_V2_USER_CMD_AUTO,
77 SPECTRE_V2_USER_CMD_FORCE,
78 + SPECTRE_V2_USER_CMD_PRCTL,
81 static const char * const spectre_v2_user_strings[] = {
82 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
83 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
84 + [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
88 @@ -268,6 +270,7 @@ static const struct {
89 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
90 { "off", SPECTRE_V2_USER_CMD_NONE, false },
91 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
92 + { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
95 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
96 @@ -322,12 +325,15 @@ spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
99 switch (spectre_v2_parse_user_cmdline(v2_cmd)) {
100 - case SPECTRE_V2_USER_CMD_AUTO:
101 case SPECTRE_V2_USER_CMD_NONE:
103 case SPECTRE_V2_USER_CMD_FORCE:
104 mode = SPECTRE_V2_USER_STRICT;
106 + case SPECTRE_V2_USER_CMD_AUTO:
107 + case SPECTRE_V2_USER_CMD_PRCTL:
108 + mode = SPECTRE_V2_USER_PRCTL;
112 /* Initialize Indirect Branch Prediction Barrier */
113 @@ -338,6 +344,9 @@ spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
114 case SPECTRE_V2_USER_STRICT:
115 static_branch_enable(&switch_mm_always_ibpb);
117 + case SPECTRE_V2_USER_PRCTL:
118 + static_branch_enable(&switch_mm_cond_ibpb);
123 @@ -350,6 +359,12 @@ spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
124 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
128 + * If SMT is not possible or STIBP is not available clear the STIPB
131 + if (!smt_possible || !boot_cpu_has(X86_FEATURE_STIBP))
132 + mode = SPECTRE_V2_USER_NONE;
134 spectre_v2_user = mode;
135 /* Only print the STIBP mode when SMT possible */
136 @@ -557,6 +572,15 @@ static void update_stibp_strict(void)
137 on_each_cpu(update_stibp_msr, NULL, 1);
140 +/* Update the static key controlling the evaluation of TIF_SPEC_IB */
141 +static void update_indir_branch_cond(void)
143 + if (sched_smt_active())
144 + static_branch_enable(&switch_to_cond_stibp);
146 + static_branch_disable(&switch_to_cond_stibp);
149 void arch_smt_update(void)
151 /* Enhanced IBRS implies STIBP. No update required. */
152 @@ -572,6 +596,7 @@ void arch_smt_update(void)
153 update_stibp_strict();
155 case SPECTRE_V2_USER_PRCTL:
156 + update_indir_branch_cond();
160 @@ -1044,7 +1069,8 @@ static char *stibp_state(void)
161 case SPECTRE_V2_USER_STRICT:
162 return ", STIBP: forced";
163 case SPECTRE_V2_USER_PRCTL:
165 + if (static_key_enabled(&switch_to_cond_stibp))
166 + return ", STIBP: conditional";
170 @@ -1052,14 +1078,11 @@ static char *stibp_state(void)
171 static char *ibpb_state(void)
173 if (boot_cpu_has(X86_FEATURE_IBPB)) {
174 - switch (spectre_v2_user) {
175 - case SPECTRE_V2_USER_NONE:
176 - return ", IBPB: disabled";
177 - case SPECTRE_V2_USER_STRICT:
178 + if (static_key_enabled(&switch_mm_always_ibpb))
179 return ", IBPB: always-on";
180 - case SPECTRE_V2_USER_PRCTL:
183 + if (static_key_enabled(&switch_mm_cond_ibpb))
184 + return ", IBPB: conditional";
185 + return ", IBPB: disabled";