Skip to content

Commit 5bfbe3a

Browse files
pdxChenKAGA-KOKO
authored andcommitted
x86/speculation: Prepare for per task indirect branch speculation control
To avoid the overhead of STIBP always on, it's necessary to allow per task control of STIBP. Add a new task flag TIF_SPEC_IB and evaluate it during context switch if SMT is active and flag evaluation is enabled by the speculation control code. Add the conditional evaluation to x86_virt_spec_ctrl() as well so the guest/host switch works properly. This has no effect because TIF_SPEC_IB cannot be set yet and the static key which controls evaluation is off. Preparatory patch for adding the control code. [ tglx: Simplify the context switch logic and make the TIF evaluation depend on SMP=y and on the static key controlling the conditional update. Rename it to TIF_SPEC_IB because it controls both STIBP and IBPB ] Signed-off-by: Tim Chen <[email protected]> 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: 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 fa1202e commit 5bfbe3a

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

arch/x86/include/asm/msr-index.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141

4242
#define MSR_IA32_SPEC_CTRL 0x00000048 /* Speculation Control */
4343
#define SPEC_CTRL_IBRS (1 << 0) /* Indirect Branch Restricted Speculation */
44-
#define SPEC_CTRL_STIBP (1 << 1) /* Single Thread Indirect Branch Predictors */
44+
#define SPEC_CTRL_STIBP_SHIFT 1 /* Single Thread Indirect Branch Predictor (STIBP) bit */
45+
#define SPEC_CTRL_STIBP (1 << SPEC_CTRL_STIBP_SHIFT) /* STIBP mask */
4546
#define SPEC_CTRL_SSBD_SHIFT 2 /* Speculative Store Bypass Disable bit */
46-
#define SPEC_CTRL_SSBD (1 << SPEC_CTRL_SSBD_SHIFT) /* Speculative Store Bypass Disable */
47+
#define SPEC_CTRL_SSBD (1 << SPEC_CTRL_SSBD_SHIFT) /* Speculative Store Bypass Disable */
4748

4849
#define MSR_IA32_PRED_CMD 0x00000049 /* Prediction Command */
4950
#define PRED_CMD_IBPB (1 << 0) /* Indirect Branch Prediction Barrier */

arch/x86/include/asm/spec-ctrl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,24 @@ static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
5353
return (tifn & _TIF_SSBD) >> (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
5454
}
5555

56+
static inline u64 stibp_tif_to_spec_ctrl(u64 tifn)
57+
{
58+
BUILD_BUG_ON(TIF_SPEC_IB < SPEC_CTRL_STIBP_SHIFT);
59+
return (tifn & _TIF_SPEC_IB) >> (TIF_SPEC_IB - SPEC_CTRL_STIBP_SHIFT);
60+
}
61+
5662
static inline unsigned long ssbd_spec_ctrl_to_tif(u64 spec_ctrl)
5763
{
5864
BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
5965
return (spec_ctrl & SPEC_CTRL_SSBD) << (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
6066
}
6167

68+
static inline unsigned long stibp_spec_ctrl_to_tif(u64 spec_ctrl)
69+
{
70+
BUILD_BUG_ON(TIF_SPEC_IB < SPEC_CTRL_STIBP_SHIFT);
71+
return (spec_ctrl & SPEC_CTRL_STIBP) << (TIF_SPEC_IB - SPEC_CTRL_STIBP_SHIFT);
72+
}
73+
6274
static inline u64 ssbd_tif_to_amd_ls_cfg(u64 tifn)
6375
{
6476
return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;

arch/x86/include/asm/thread_info.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct thread_info {
8383
#define TIF_SYSCALL_EMU 6 /* syscall emulation active */
8484
#define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
8585
#define TIF_SECCOMP 8 /* secure computing */
86+
#define TIF_SPEC_IB 9 /* Indirect branch speculation mitigation */
8687
#define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
8788
#define TIF_UPROBE 12 /* breakpointed or singlestepping */
8889
#define TIF_PATCH_PENDING 13 /* pending live patching update */
@@ -110,6 +111,7 @@ struct thread_info {
110111
#define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
111112
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
112113
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
114+
#define _TIF_SPEC_IB (1 << TIF_SPEC_IB)
113115
#define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
114116
#define _TIF_UPROBE (1 << TIF_UPROBE)
115117
#define _TIF_PATCH_PENDING (1 << TIF_PATCH_PENDING)
@@ -146,7 +148,8 @@ struct thread_info {
146148

147149
/* flags to check in __switch_to() */
148150
#define _TIF_WORK_CTXSW \
149-
(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|_TIF_SSBD)
151+
(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP| \
152+
_TIF_SSBD|_TIF_SPEC_IB)
150153

151154
#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
152155
#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)

arch/x86/kernel/cpu/bugs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
148148
static_cpu_has(X86_FEATURE_AMD_SSBD))
149149
hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
150150

151+
/* Conditional STIBP enabled? */
152+
if (static_branch_unlikely(&switch_to_cond_stibp))
153+
hostval |= stibp_tif_to_spec_ctrl(ti->flags);
154+
151155
if (hostval != guestval) {
152156
msrval = setguest ? guestval : hostval;
153157
wrmsrl(MSR_IA32_SPEC_CTRL, msrval);

arch/x86/kernel/process.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,17 @@ static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
404404
static __always_inline void __speculation_ctrl_update(unsigned long tifp,
405405
unsigned long tifn)
406406
{
407+
unsigned long tif_diff = tifp ^ tifn;
407408
u64 msr = x86_spec_ctrl_base;
408409
bool updmsr = false;
409410

410-
/* If TIF_SSBD is different, select the proper mitigation method */
411-
if ((tifp ^ tifn) & _TIF_SSBD) {
411+
/*
412+
* If TIF_SSBD is different, select the proper mitigation
413+
* method. Note that if SSBD mitigation is disabled or permanentely
414+
* enabled this branch can't be taken because nothing can set
415+
* TIF_SSBD.
416+
*/
417+
if (tif_diff & _TIF_SSBD) {
412418
if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
413419
amd_set_ssb_virt_state(tifn);
414420
} else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
@@ -420,6 +426,16 @@ static __always_inline void __speculation_ctrl_update(unsigned long tifp,
420426
}
421427
}
422428

429+
/*
430+
* Only evaluate TIF_SPEC_IB if conditional STIBP is enabled,
431+
* otherwise avoid the MSR write.
432+
*/
433+
if (IS_ENABLED(CONFIG_SMP) &&
434+
static_branch_unlikely(&switch_to_cond_stibp)) {
435+
updmsr |= !!(tif_diff & _TIF_SPEC_IB);
436+
msr |= stibp_tif_to_spec_ctrl(tifn);
437+
}
438+
423439
if (updmsr)
424440
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
425441
}

0 commit comments

Comments
 (0)