[patch 18/24] x86/speculation: Avoid __switch_to_xtra() calls

From: Thomas Gleixner
Date: Wed Nov 21 2018 - 15:18:29 EST


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.

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 <tglx@xxxxxxxxxxxxx>
---
arch/x86/include/asm/spec-ctrl.h | 5 +++++
arch/x86/include/asm/thread_info.h | 13 +++++++++++--
arch/x86/kernel/process_32.c | 9 +++++++++
arch/x86/kernel/process_64.c | 9 +++++++++
4 files changed, 34 insertions(+), 2 deletions(-)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -115,8 +115,13 @@ static inline void switch_to_ibpb(struct
}

#ifdef CONFIG_SMP
+static __always_inline bool switch_to_xtra_needs_spec_ib(void)
+{
+ return static_branch_likely(&switch_to_cond_stibp);
+}
extern void speculative_store_bypass_ht_init(void);
#else
+static inline bool switch_to_xtra_needs_spec_ib(void) { return false; }
static inline void speculative_store_bypass_ht_init(void) { }
#endif

--- a/arch/x86/include/asm/thread_info.h
+++ b/arch/x86/include/asm/thread_info.h
@@ -147,9 +147,18 @@ struct thread_info {
_TIF_FSCHECK)

/* flags to check in __switch_to() */
-#define _TIF_WORK_CTXSW \
+#define _TIF_WORK_CTXSW_BASE \
(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP| \
- _TIF_SSBD|_TIF_SPEC_IB)
+ _TIF_SSBD)
+
+/*
+ * Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
+ */
+#ifdef CONFIG_SMP
+# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE | _TIF_SPEC_IB)
+#else
+# define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE)
+#endif

#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
--- a/arch/x86/kernel/process_32.c
+++ b/arch/x86/kernel/process_32.c
@@ -272,6 +272,15 @@ EXPORT_SYMBOL_GPL(start_thread);
switch_to_ibpb(next_p, prev_tif, next_tif);

/*
+ * Avoid __switch_to_xtra() invocation when conditional stpib is
+ * disabled.
+ */
+ if (!switch_to_xtra_needs_spec_ib()) {
+ prev_tif &= ~_TIF_SPEC_IB;
+ next_tif &= ~_TIF_SPEC_IB;
+ }
+
+ /*
* Now maybe handle debug registers and/or IO bitmaps
*/
if (unlikely(next_tif & _TIF_WORK_CTXSW_NEXT ||
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -625,6 +625,15 @@ void compat_start_thread(struct pt_regs
switch_to_ibpb(next_p, prev_tif, next_tif);

/*
+ * Avoid __switch_to_xtra() invocation when conditional stpib is
+ * disabled.
+ */
+ if (!switch_to_xtra_needs_spec_ib()) {
+ prev_tif &= ~_TIF_SPEC_IB;
+ next_tif &= ~_TIF_SPEC_IB;
+ }
+
+ /*
* Now maybe reload the debug registers and handle I/O bitmaps
*/
if (unlikely(next_tif & _TIF_WORK_CTXSW_NEXT ||