x86/speculation: Add finer control for when to issue IBPB
v2 cover x86/speculation: Add finer control for when to issue IBPB
It is documented in Documentation/admin-guide/hw-vuln/spectre.rst, that disabling indirect branch speculation for a user-space process creates more overhead and cause it to run slower. The performance hit varies by CPU, but on the AMD A4-9120C and A6-9220C CPUs, a simple ping-pong using pipes between two processes runs ~10x slower when disabling IB speculation.
Patch 2, included in this RFC but not intended for commit, is a simple program that demonstrates this issue. Running on a A4-9120C without IB speculation disabled, each process ping-pong takes ~7us:
localhost ~ # taskset 1 /usr/local/bin/test
...
iters: 262144, t: 1936300, iter/sec: 135383, us/iter: 7
But when IB speculation is disabled, that number increases significantly:
localhost ~ # taskset 1 /usr/local/bin/test d
...
iters: 16384, t: 1500518, iter/sec: 10918, us/iter: 91
Although this test is a worst-case scenario, we can also consider a real
situation: an audio server (i.e. pulse). If we imagine a low-latency capture, with 10ms packets and a concurrent task on the same CPU (i.e. video encoding, for a video call), the audio server will preempt the CPU at a rate of 100HZ. At 91us overhead per preemption (switching to and from the audio process), that's 0.9% overhead for one process doing preemption. In real-world testing (on a A4-9120C), I've seen 9% of CPU used by IBPB when doing a 2-person video call.
With this patch, the number of IBPBs issued can be reduced to the minimum necessary, only when there's a potential attacker->victim process switch.
Running on the same A4-9120C device, this patch reduces the performance hit of IBPB by ~half, as expected:
localhost ~ # taskset 1 /usr/local/bin/test ds
...
iters: 32768, t: 1824043, iter/sec: 17964, us/iter: 55
It should be noted, CPUs from multiple vendors experience a performance hit due to IBPB. I also tested a Intel i3-8130U which sees a noticable (~2x) increase in process switch time due to IBPB.
IB spec enabled:
localhost ~ # taskset 1 /usr/local/bin/test
...
iters: 262144, t: 1210821us, iter/sec: 216501, us/iter: 4
IB spec disabled:
localhost ~ # taskset 1 /usr/local/bin/test d
...
iters: 131072, t: 1257583us, iter/sec: 104225, us/iter: 9
Open questions:
- There are a significant number of task flags, which also now reaches the limit of the 'long' on 32-bit systems. Should the 'mode' flags be stored somewhere else?
- Having x86-specific flags in linux/sched.h feels wrong. However, this is the mechanism for doing atomic flag updates. Is there an alternate approach?
Open tasks:
- Documentation
- Naming
Changes in v2:
- Make flag per-process using prctl().
Anand K Mistry (2):
x86/speculation: Allow per-process control of when to issue IBPB
selftests: Benchmark for the cost of disabling IB speculation
arch/x86/include/asm/thread_info.h | 4 + arch/x86/kernel/cpu/bugs.c | 56 +++++++++ arch/x86/kernel/process.c | 10 ++ arch/x86/mm/tlb.c | 51 ++++++-- include/linux/sched.h | 10 ++ include/uapi/linux/prctl.h | 5 + .../testing/selftests/ib_spec/ib_spec_bench.c | 109 ++++++++++++++++++ 7 files changed, 236 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/ib_spec/ib_spec_bench.c
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
RFC PATCH v2 1/2 x86/speculation: Allow per-process control of when to issue IBPB
When IB speculation is conditionally disabled for a process (via prctl() or seccomp), an IBPB is issued whenever that process is switched to/from. However, this results more IBPBs than necessary. The goal is to protect a victim process from an attacker poisoning the BTB by issuing IBPB in the attacker->victim switch. However, the current logic will also issue IBPB in the victim->attacker switch, because there's no notion of whether the process with IB speculation disabled is a potential attacker or victim.
By default, there is no change in behaviour. If IBPB is conditionally enabled, and a process has IB speculation disabled (either by seccomp or prctl()), an IBPB will be issued whenever the process is switched to or from.
But when prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_IBPB_MODE) is used, a process can be tagged as either "sandboxed" or "protected". This explicit tagging allows the IBPB to be avoided in one direction, potentially cutting the number of IBPBs issued in half.
---
Changes in v2:
- Make flag per-process using prctl().
arch/x86/include/asm/thread_info.h | 4 +++ arch/x86/kernel/cpu/bugs.c | 56 ++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 10 ++++++ arch/x86/mm/tlb.c | 51 ++++++++++++++++++++++----- include/linux/sched.h | 10 ++++++ include/uapi/linux/prctl.h | 5 +++ 6 files changed, 127 insertions(+), 9 deletions(-)
arch/x86/include/asm/thread_info.h +4 -0
@@ -97,6 +97,8 @@ struct thread_info {#define TIF_BLOCKSTEP 25/* set when we want DEBUGCTLMSR_BTF */ #define TIF_LAZY_MMU_UPDATES 27/* task is updating the mmu lazily */ #define TIF_ADDR32 29/* 32-bit address space on 64 bits */ +#define TIF_NO_IBPB_ENTRY 30/* Prevent IBPB on address space entry */ +#define TIF_NO_IBPB_LEAVE 31/* Prevent IBPB on address space leave */ #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
@@ -119,6 +121,8 @@ struct thread_info {#define _TIF_BLOCKSTEP (1 << TIF_BLOCKSTEP)#define _TIF_LAZY_MMU_UPDATES (1 << TIF_LAZY_MMU_UPDATES)#define _TIF_ADDR32 (1 << TIF_ADDR32) +#define _TIF_NO_IBPB_ENTRY (1 << TIF_NO_IBPB_ENTRY) +#define _TIF_NO_IBPB_LEAVE (1 << TIF_NO_IBPB_LEAVE)/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW_BASE \
arch/x86/kernel/cpu/bugs.c +56 -0
68 lines not on this page · raw message
arch/x86/kernel/process.c +10 -0
@@ -565,6 +565,16 @@ static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)set_tsk_thread_flag (tsk,TIF_SPEC_IB ); elseclear_tsk_thread_flag (tsk ,TIF_SPEC_IB ); + +if (task_spec_ibpb_disable_entry (tsk)) +set_tsk_thread_flag (tsk,TIF_NO_IBPB_ENTRY ); +else +clear_tsk_thread_flag (tsk,TIF_NO_IBPB_ENTRY ); + +if (task_spec_ibpb_disable_leave (tsk)) +set_tsk_thread_flag (tsk,TIF_NO_IBPB_LEAVE ); +else +clear_tsk_thread_flag (tsk,TIF_NO_IBPB_LEAVE ); }/* Return the updated threadinfo flags*/ return task_thread_info (tsk)->flags;
arch/x86/mm/tlb.c +42 -9
92 lines not on this page · raw message
include/linux/sched.h +10 -0
@@ -1625,6 +1625,8 @@ static inline bool is_percpu_thread(void)#define PFA_SPEC_IB_DISABLE 5/* Indirect branch speculation restricted */ #define PFA_SPEC_IB_FORCE_DISABLE 6/* Indirect branch speculation permanently restricted */ #define PFA_SPEC_SSB_NOEXEC 7/* Speculative Store Bypass clear on execve() */ +#define PFA_SPEC_IBPB_DISABLE_ENTRY 8/* Avoid indirect branch prediction barrier on task entry */ +#define PFA_SPEC_IBPB_DISABLE_LEAVE 9/* Avoid indirect branch prediction barrier on task leave */ #define TASK_PFA_TEST (name, func) \ static inline bool task_##func(struct task_struct *p) \
@@ -1667,6 +1669,14 @@ TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable) TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable) TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable) +TASK_PFA_TEST (SPEC_IBPB_DISABLE_ENTRY , spec_ibpb_disable_entry); +TASK_PFA_SET (SPEC_IBPB_DISABLE_ENTRY , spec_ibpb_disable_entry); +TASK_PFA_CLEAR (SPEC_IBPB_DISABLE_ENTRY , spec_ibpb_disable_entry); + +TASK_PFA_TEST (SPEC_IBPB_DISABLE_LEAVE , spec_ibpb_disable_leave); +TASK_PFA_SET (SPEC_IBPB_DISABLE_LEAVE , spec_ibpb_disable_leave); +TASK_PFA_CLEAR (SPEC_IBPB_DISABLE_LEAVE , spec_ibpb_disable_leave); +static inline void current_restore_flags (unsigned long orig_flags,unsigned long flags) {
include/uapi/linux/prctl.h +5 -0
@@ -213,6 +213,7 @@ struct prctl_mm_map {/* Speculation control variants */ # define PR_SPEC_STORE_BYPASS 0# define PR_SPEC_INDIRECT_BRANCH 1 +# define PR_SPEC_IBPB_MODE 2/* Return and control values for PR_SET/GET_SPECULATION_CTRL */ # define PR_SPEC_NOT_AFFECTED 0# define PR_SPEC_PRCTL (1UL << 0)
@@ -220,6 +221,10 @@ struct prctl_mm_map {# define PR_SPEC_DISABLE (1UL << 2)# define PR_SPEC_FORCE_DISABLE (1UL << 3)# define PR_SPEC_DISABLE_NOEXEC (1UL << 4) +/* Indirect branch prediction barrier control */ +# define PR_SPEC_IBPB_MODE_DEFAULT 0 +# define PR_SPEC_IBPB_MODE_SANDBOX 1 +# define PR_SPEC_IBPB_MODE_PROTECT 2/* Reset arm64 pointer authentication keys */ #define PR_PAC_RESET_KEYS 54
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
RFC PATCH v2 2/2 selftests: Benchmark for the cost of disabling IB speculation
In reply to Anand K MistryThis is a simple benchmark for determining the cost of disabling IB speculation. It forks a child process and does a simple ping-pong using pipes between the parent and child. The child process can have IB speculation disabled by running with 'd' as the first argument.
The test increases the number of iterations until the iterations take at least 1 second, to minimise noise.
This file is NOT intended for inclusion in the kernel source. It is presented here as a patch for reference and for others to replicate results.
The binary should be run with 'taskset' and pinned to a single core, since the goal is to benchmark process switching on a single core.
---
(no changes since v1)
.../testing/selftests/ib_spec/ib_spec_bench.c | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tools/testing/selftests/ib_spec/ib_spec_bench.c
tools/testing/selftests/ib_spec/ib_spec_bench.c new file +109 -0
@@ -0,0 +1,109 @@ +#include <stdio.h> +#include <time.h> +#include <stdint.h> +#include <assert.h> +#include <unistd.h> +#include <sys/prctl.h> + +#define PR_SPEC_IBPB_MODE 2 +#define PR_SPEC_IBPB_MODE_DEFAULT 0 +#define PR_SPEC_IBPB_MODE_SANDBOX 1 +#define PR_SPEC_IBPB_MODE_PROTECT 2 + +int64_t get_time_us () { +struct timespec ts = {0 }; +assert (clock_gettime (CLOCK_MONOTONIC , &ts) ==0 ); +return (ts.tv_sec *1000000 ) + (ts.tv_nsec/1000 ); +} + +void pong (int read_fd,int write_fd) { +int ret; +char buf; + +while (1 ) { + ret =read (read_fd, &buf,1 ); +if (ret ==0 ) +return ; +assert (ret ==1 ); + +assert (write (write_fd, &buf,1 ) ==1 ); + } +} + +void ping_once (int write_fd,int read_fd) { +char buf =42 ; +assert (write (write_fd, &buf,1 ) ==1 ); +assert (read (read_fd, &buf,1 ) ==1 ); +} + +int64_t ping_multi (int iters,int write_fd,int read_fd) { +int64_t start_time =get_time_us (); +for (int i =0 ; i < iters; i++) +ping_once (write_fd, read_fd); +return get_time_us () - start_time; +} + +void run_test (int write_fd,int read_fd) { +int64_t iters =1 ; +int64_t t; +for (int i =0 ; i <60 ; i++) { + t =ping_multi (iters, write_fd, read_fd); +printf ("iters: %d, t: %dus, iter/sec: %d, us/iter: %d\n" , + iters, t, (iters *1000000LL ) / t, t/iters); + +if (t >1000000 ) +break ; + iters <<=1 ; + } +} + +int main (int argc,char * argv[]) { +int fds_ping[2 ], fds_pong[2 ]; +assert (pipe (fds_ping) ==0 ); +assert (pipe (fds_pong) ==0 ); + +int disable_ib =0 ; +int spec_ibpb_mode =0 ; + +if (argc >1 ) { +int done =0 ; +for (int i =0 ; !done; i++) { +switch (argv[1 ][i]) { +case 0 : + done =1 ; +break ; +case 'd' : + disable_ib =1 ; +break ; +case 's' : + spec_ibpb_mode =PR_SPEC_IBPB_MODE_SANDBOX ; +break ; +case 'p' : + spec_ibpb_mode =PR_SPEC_IBPB_MODE_PROTECT ; +break ; + } + } + } + +pid_t pid =fork (); +assert (pid >=0 ); +if (!pid) { +if (prctl (PR_SET_SPECULATION_CTRL , +PR_SPEC_IBPB_MODE , spec_ibpb_mode,0 ,0 )) { +perror ("Unable to set IBPB mode" ); + } + +if (disable_ib) +assert (prctl (PR_SET_SPECULATION_CTRL , +PR_SPEC_INDIRECT_BRANCH , +PR_SPEC_DISABLE ,0 ,0 ) ==0 ); + +close (fds_ping[1 ]); +pong (fds_ping[0 ], fds_pong[1 ]); + }else { +run_test (fds_ping[1 ], fds_pong[0 ]); +close (fds_ping[1 ]); + } + +return 0 ; +}
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
Re: [RFC PATCH v2 1/2] x86/speculation: Allow per-process control of when to issue IBPB
In reply to Anand K MistryAnand,
Quoted 11 lines · Anand K. Mistry
arch/x86/mm/tlb.c +5
-2-static inline unsigned long mm_mangle_tif_spec_ib (struct task_struct *next) +static inline unsigned long mm_mangle_tif_spec_ib_on_leave ( +struct task_struct *next) {unsigned long next_tif =task_thread_info (next)->flags; -unsigned long ibpb = (next_tif >>TIF_SPEC_IB ) &LAST_USER_MM_IBPB ; +unsigned long spec_disabled = + (next_tif >>TIF_SPEC_IB ) & ~(next_tif >>TIF_NO_IBPB_LEAVE ); +unsigned long ibpb = spec_disabled &LAST_USER_MM_IBPB ;
arch/x86/mm/tlb.c void switch_mm(struct mm_struct *prev, struct mm_struct *next, · line 336
Quoted 10 lines
arch/x86/mm/tlb.c +7
-0return (unsigned long )next->mm | ibpb; } +static inline bool ibpb_on_entry (struct task_struct *next) +{ +unsigned long next_tif =task_thread_info (next)->flags; +unsigned long spec_disabled = + (next_tif >>TIF_SPEC_IB ) & ~(next_tif >>TIF_NO_IBPB_ENTRY ); +return spec_disabled &1 ; +}
Why exactly do we need _three_ TIF bits and this non-intuitive inverted logic?
The existing mode is: Issue IBPB when scheduling in and when scheduling out.
Ergo the obvious approach for making it more finegrained is to split the existing TIF_SPEC_IB into TIF_SPEC_IB_IN and TIF_SPEC_IB_OUT and have the existing mode both bits set.
Thanks,
tglx
Re: [RFC PATCH v2 1/2] x86/speculation: Allow per-process control of when to issue IBPB
Quoted 18 lines · Thomas Gleixner
Anand,
Quoted 14 lines · Anand K. Mistry
arch/x86/mm/tlb.c +5
-2-static inline unsigned long mm_mangle_tif_spec_ib (struct task_struct *next) +static inline unsigned long mm_mangle_tif_spec_ib_on_leave ( +struct task_struct *next) {unsigned long next_tif =task_thread_info (next)->flags; -unsigned long ibpb = (next_tif >>TIF_SPEC_IB ) &LAST_USER_MM_IBPB ; +unsigned long spec_disabled = + (next_tif >>TIF_SPEC_IB ) & ~(next_tif >>TIF_NO_IBPB_LEAVE ); +unsigned long ibpb = spec_disabled &LAST_USER_MM_IBPB ;return (unsigned long )next->mm | ibpb; }
Quoted 10 lines
arch/x86/mm/tlb.c void switch_mm(struct mm_struct *prev, struct mm_struct *next, · line 336
Quoted 7 lines
arch/x86/mm/tlb.c +7
-0+static inline bool ibpb_on_entry (struct task_struct *next) +{ +unsigned long next_tif =task_thread_info (next)->flags; +unsigned long spec_disabled = + (next_tif >>TIF_SPEC_IB ) & ~(next_tif >>TIF_NO_IBPB_ENTRY ); +return spec_disabled &1 ; +}Why exactly do we need _three_ TIF bits and this non-intuitive inverted logic?
The inverted logic is to avoid any side effects of flag bits defaulting to 0, which should keep the existing behaviour of doing an IBPB on both entry and exit.
Quoted 6 lines
The existing mode is: Issue IBPB when scheduling in and when scheduling out.
Ergo the obvious approach for making it more finegrained is to split the existing
TIF_SPEC_IBintoTIF_SPEC_IB_INandTIF_SPEC_IB_OUTand have the existing mode both bits set.
Agreed. The problem is the existing bit doesn't just control IBPB behaviour. It also controls STIBP, which you can kinda control separately on the command line when booting with the spectre_v2_user flag. The code, however, uses the same flag for both (see arch/x86/include/asm/spec-ctrl.h).
Maybe this is another cleanup I should do prior to this change? Separating IBPB and STIBP flags in the implementation.
Quoted 6 lines
Thanks,
tglx