Skip to content

Commit e6da8bb

Browse files
committed
x86/speculation: Split out TIF update
The update of the TIF_SSBD flag and the conditional speculation control MSR update is done in the ssb_prctl_set() function directly. The upcoming prctl support for controlling indirect branch speculation via STIBP needs the same mechanism. Split the code out and make it reusable. Reword the comment about updates for other tasks. 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 46f7ecb commit e6da8bb

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

arch/x86/kernel/cpu/bugs.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,29 @@ static void ssb_select_mitigation(void)
702702
#undef pr_fmt
703703
#define pr_fmt(fmt) "Speculation prctl: " fmt
704704

705-
static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
705+
static void task_update_spec_tif(struct task_struct *tsk, int tifbit, bool on)
706706
{
707707
bool update;
708708

709+
if (on)
710+
update = !test_and_set_tsk_thread_flag(tsk, tifbit);
711+
else
712+
update = test_and_clear_tsk_thread_flag(tsk, tifbit);
713+
714+
/*
715+
* Immediately update the speculation control MSRs for the current
716+
* task, but for a non-current task delay setting the CPU
717+
* mitigation until it is scheduled next.
718+
*
719+
* This can only happen for SECCOMP mitigation. For PRCTL it's
720+
* always the current task.
721+
*/
722+
if (tsk == current && update)
723+
speculation_ctrl_update_current();
724+
}
725+
726+
static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
727+
{
709728
if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
710729
ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
711730
return -ENXIO;
@@ -716,28 +735,20 @@ static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
716735
if (task_spec_ssb_force_disable(task))
717736
return -EPERM;
718737
task_clear_spec_ssb_disable(task);
719-
update = test_and_clear_tsk_thread_flag(task, TIF_SSBD);
738+
task_update_spec_tif(task, TIF_SSBD, false);
720739
break;
721740
case PR_SPEC_DISABLE:
722741
task_set_spec_ssb_disable(task);
723-
update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
742+
task_update_spec_tif(task, TIF_SSBD, true);
724743
break;
725744
case PR_SPEC_FORCE_DISABLE:
726745
task_set_spec_ssb_disable(task);
727746
task_set_spec_ssb_force_disable(task);
728-
update = !test_and_set_tsk_thread_flag(task, TIF_SSBD);
747+
task_update_spec_tif(task, TIF_SSBD, true);
729748
break;
730749
default:
731750
return -ERANGE;
732751
}
733-
734-
/*
735-
* If being set on non-current task, delay setting the CPU
736-
* mitigation until it is next scheduled.
737-
*/
738-
if (task == current && update)
739-
speculation_ctrl_update_current();
740-
741752
return 0;
742753
}
743754

0 commit comments

Comments
 (0)