Skip to content

Commit 5635d99

Browse files
committed
x86/speculation: Avoid __switch_to_xtra() calls
The TIF_SPEC_IB bit does not need to be evaluated in the decision to invoke __switch_to_xtra() when: - CONFIG_SMP is disabled - The conditional STIPB mode is disabled The TIF_SPEC_IB bit still controls IBPB in both cases so the TIF work mask checks might invoke __switch_to_xtra() for nothing if TIF_SPEC_IB is the only set bit in the work masks. Optimize it out by masking the bit at compile time for CONFIG_SMP=n and at run time when the static key controlling the conditional STIBP mode is disabled. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Tim Chen <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Casey Schaufler <[email protected]> Cc: Asit Mallick <[email protected]> Cc: Arjan van de Ven <[email protected]> Cc: Jon Masters <[email protected]> Cc: Waiman Long <[email protected]> Cc: Greg KH <[email protected]> Cc: Dave Stewart <[email protected]> Cc: Kees Cook <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent ff16701 commit 5635d99

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

arch/x86/include/asm/thread_info.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,18 @@ struct thread_info {
147147
_TIF_FSCHECK)
148148

149149
/* flags to check in __switch_to() */
150-
#define _TIF_WORK_CTXSW \
150+
#define _TIF_WORK_CTXSW_BASE \
151151
(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP| \
152-
_TIF_SSBD|_TIF_SPEC_IB)
152+
_TIF_SSBD)
153+
154+
/*
155+
* Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
156+
*/
157+
#ifdef CONFIG_SMP
158+
# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE | _TIF_SPEC_IB)
159+
#else
160+
# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE)
161+
#endif
153162

154163
#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
155164
#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)

arch/x86/kernel/process.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// Code shared between 32 and 64 bit
44

5+
#include <asm/spec-ctrl.h>
6+
57
void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p);
68

79
/*
@@ -14,6 +16,19 @@ static inline void switch_to_extra(struct task_struct *prev,
1416
unsigned long next_tif = task_thread_info(next)->flags;
1517
unsigned long prev_tif = task_thread_info(prev)->flags;
1618

19+
if (IS_ENABLED(CONFIG_SMP)) {
20+
/*
21+
* Avoid __switch_to_xtra() invocation when conditional
22+
* STIPB is disabled and the only different bit is
23+
* TIF_SPEC_IB. For CONFIG_SMP=n TIF_SPEC_IB is not
24+
* in the TIF_WORK_CTXSW masks.
25+
*/
26+
if (!static_branch_likely(&switch_to_cond_stibp)) {
27+
prev_tif &= ~_TIF_SPEC_IB;
28+
next_tif &= ~_TIF_SPEC_IB;
29+
}
30+
}
31+
1732
/*
1833
* __switch_to_xtra() handles debug registers, i/o bitmaps,
1934
* speculation mitigations etc.

0 commit comments

Comments
 (0)