]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.9/0040-x86-speculation-Avoid-__switch_to_xtra-calls.patch
Linux 4.14.119
[thirdparty/kernel/stable-queue.git] / queue-4.9 / 0040-x86-speculation-Avoid-__switch_to_xtra-calls.patch
1 From ccfa6e5a4d38ff8c1c1bc7b07415ed1afd303f4e Mon Sep 17 00:00:00 2001
2 From: Thomas Gleixner <tglx@linutronix.de>
3 Date: Sun, 25 Nov 2018 19:33:48 +0100
4 Subject: [PATCH 40/76] x86/speculation: Avoid __switch_to_xtra() calls
5
6 commit 5635d99953f04b550738f6f4c1c532667c3fd872 upstream.
7
8 The TIF_SPEC_IB bit does not need to be evaluated in the decision to invoke
9 __switch_to_xtra() when:
10
11 - CONFIG_SMP is disabled
12
13 - The conditional STIPB mode is disabled
14
15 The TIF_SPEC_IB bit still controls IBPB in both cases so the TIF work mask
16 checks might invoke __switch_to_xtra() for nothing if TIF_SPEC_IB is the
17 only set bit in the work masks.
18
19 Optimize it out by masking the bit at compile time for CONFIG_SMP=n and at
20 run time when the static key controlling the conditional STIBP mode is
21 disabled.
22
23 Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
24 Reviewed-by: Ingo Molnar <mingo@kernel.org>
25 Cc: Peter Zijlstra <peterz@infradead.org>
26 Cc: Andy Lutomirski <luto@kernel.org>
27 Cc: Linus Torvalds <torvalds@linux-foundation.org>
28 Cc: Jiri Kosina <jkosina@suse.cz>
29 Cc: Tom Lendacky <thomas.lendacky@amd.com>
30 Cc: Josh Poimboeuf <jpoimboe@redhat.com>
31 Cc: Andrea Arcangeli <aarcange@redhat.com>
32 Cc: David Woodhouse <dwmw@amazon.co.uk>
33 Cc: Tim Chen <tim.c.chen@linux.intel.com>
34 Cc: Andi Kleen <ak@linux.intel.com>
35 Cc: Dave Hansen <dave.hansen@intel.com>
36 Cc: Casey Schaufler <casey.schaufler@intel.com>
37 Cc: Asit Mallick <asit.k.mallick@intel.com>
38 Cc: Arjan van de Ven <arjan@linux.intel.com>
39 Cc: Jon Masters <jcm@redhat.com>
40 Cc: Waiman Long <longman9394@gmail.com>
41 Cc: Greg KH <gregkh@linuxfoundation.org>
42 Cc: Dave Stewart <david.c.stewart@intel.com>
43 Cc: Kees Cook <keescook@chromium.org>
44 Link: https://lkml.kernel.org/r/20181125185005.374062201@linutronix.de
45 [bwh: Backported to 4.9: adjust context]
46 Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
47 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
48 ---
49 arch/x86/include/asm/thread_info.h | 13 +++++++++++--
50 arch/x86/kernel/process.h | 15 +++++++++++++++
51 2 files changed, 26 insertions(+), 2 deletions(-)
52
53 diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
54 index 389b95bb1456..cd3f6be025be 100644
55 --- a/arch/x86/include/asm/thread_info.h
56 +++ b/arch/x86/include/asm/thread_info.h
57 @@ -142,9 +142,18 @@ struct thread_info {
58 _TIF_NOHZ)
59
60 /* flags to check in __switch_to() */
61 -#define _TIF_WORK_CTXSW \
62 +#define _TIF_WORK_CTXSW_BASE \
63 (_TIF_IO_BITMAP|_TIF_NOTSC|_TIF_BLOCKSTEP| \
64 - _TIF_SSBD|_TIF_SPEC_IB)
65 + _TIF_SSBD)
66 +
67 +/*
68 + * Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
69 + */
70 +#ifdef CONFIG_SMP
71 +# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE | _TIF_SPEC_IB)
72 +#else
73 +# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE)
74 +#endif
75
76 #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
77 #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
78 diff --git a/arch/x86/kernel/process.h b/arch/x86/kernel/process.h
79 index 020fbfac3a27..898e97cf6629 100644
80 --- a/arch/x86/kernel/process.h
81 +++ b/arch/x86/kernel/process.h
82 @@ -2,6 +2,8 @@
83 //
84 // Code shared between 32 and 64 bit
85
86 +#include <asm/spec-ctrl.h>
87 +
88 void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p);
89
90 /*
91 @@ -14,6 +16,19 @@ static inline void switch_to_extra(struct task_struct *prev,
92 unsigned long next_tif = task_thread_info(next)->flags;
93 unsigned long prev_tif = task_thread_info(prev)->flags;
94
95 + if (IS_ENABLED(CONFIG_SMP)) {
96 + /*
97 + * Avoid __switch_to_xtra() invocation when conditional
98 + * STIPB is disabled and the only different bit is
99 + * TIF_SPEC_IB. For CONFIG_SMP=n TIF_SPEC_IB is not
100 + * in the TIF_WORK_CTXSW masks.
101 + */
102 + if (!static_branch_likely(&switch_to_cond_stibp)) {
103 + prev_tif &= ~_TIF_SPEC_IB;
104 + next_tif &= ~_TIF_SPEC_IB;
105 + }
106 + }
107 +
108 /*
109 * __switch_to_xtra() handles debug registers, i/o bitmaps,
110 * speculation mitigations etc.
111 --
112 2.21.0
113