x86/syscall: Dispatch via sys_call_table when Spectre v2 and BHI are off
Subsystems (3)
RFC PATCH 1/1 x86/syscall: Dispatch via sys_call_table when Spectre v2 and BHI are off
Linus' commit 1e3ad78334a6 ("x86/syscall: Don't force use of indirect calls for system calls") dispatches system calls with a switch statement, so that syscall entry has no indirect branch for BHI to target.
As noted by Linus in the commit above, the compiler can either decide to generate an indirect jump, or just a series of conditional branches (which is more expensive).
With CONFIG_MITIGATION_RETPOLINE, clang emits no indirect jump, and the switch becomes a tree of compares and conditional branches. That code is built in, so a kernel booted with mitigations=off still walks the tree on every system call.
When neither the Spectre v2 nor the BHI mitigation is enabled, nothing in the kernel protects indirect branches, and the switch buys nothing. Use a static key to call through sys_call_table[] in that case.
The compiler emits that call through a retpoline thunk, which boot-time patching turns into a plain indirect call when retpolines are off.
On next-20261028 built with clang and booted with mitigations=off on a Xeon Gold 6138 at a fixed 2.0 GHz, perf bench syscall basic (getppid) and getpgid. Mean and standard deviation of 30 to 45 runs over 2 to 3 boots, and kernel instructions and cycles per call from perf stat:
CPU_MITIGATIONS=n =y =y + patch
getppid() ns 131.2 +- 0.2 134.1 +- 0.1 131.7 +- 0.4
insns/cyc 310 / 212.1 331 / 218.1 314 / 213.1
getpgid() ns 129.8 +- 0.3 132.5 +- 0.2 130.0 +- 0.5
insns/cyc 302 / 209.4 323 / 214.1 306 / 209.8Kernels that enable either mitigation keep the switch, behind one extra jump.
---
The reason behind this change is to make the mitigations a boot command line option rather than a compilation option.
It is common to find different kernel configurations where the major difference is CONFIG_CPU_MITIGATIONS.
With this patch, CONFIG_CPU_MITIGATIONS can be enabled at build time, and when the mitigations are disabled at boot, system call dispatch has no performance impact.
--- arch/x86/entry/syscall_64.c | 15 +++++++++++---- arch/x86/include/asm/syscall.h | 3 ++- arch/x86/kernel/cpu/bugs.c | 12 ++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-)
arch/x86/entry/syscall_64.c +11 -4
@@ -21,9 +21,8 @@#define __SYSCALL_NORETURN __SYSCALL/* -* The sys_call_table[] is no longer used for system calls, but -* kernel/trace/trace_syscalls.c still wants to know the system -* call address. +* Used for dispatch only when x64_syscall_use_table is enabled; +* kernel/trace/trace_syscalls.c also wants the system call addresses. */ #define __SYSCALL (nr, sym) __x64_##sym,const sys_call_ptr_t sys_call_table[] = {
@@ -31,6 +30,11 @@ const sys_call_ptr_t sys_call_table[] = { };#undef __SYSCALL +/* +* Decide about syscall dispatch method: x64_sys_call() or sys_call_table +*/ +DEFINE_STATIC_KEY_FALSE (x64_syscall_use_table); +#define __SYSCALL (nr, sym) case nr: return __x64_##sym(regs);/* The unsigned int @nr argument is intentional as it creates denser code */
@@ -58,7 +62,10 @@ static __always_inline bool do_syscall_x64(struct pt_regs *regs, unsigned long n {if (likely (nr < NR_syscalls)) { nr =array_index_nospec (nr, NR_syscalls); - regs->ax =x64_sys_call (regs, (unsigned int )nr); +if (static_branch_likely (&x64_syscall_use_table)) + regs->ax = sys_call_table[nr](regs); +else + regs->ax =x64_sys_call (regs, (unsigned int )nr);return true; }return false;
arch/x86/include/asm/syscall.h +2 -1
@@ -13,12 +13,13 @@#include <uapi/linux/audit.h> #include <linux/sched.h> #include <linux/err.h> +#include <linux/jump_label.h> #include <asm/thread_info.h> /* for TS_COMPAT */ #include <asm/unistd.h> -/* This is used purely for kernel/trace/trace_syscalls.c */ typedef long (*sys_call_ptr_t )(const struct pt_regs *);extern const sys_call_ptr_t sys_call_table[]; +DECLARE_STATIC_KEY_FALSE (x64_syscall_use_table);/* * Only the low 32 bits of orig_ax are meaningful, so we return int.
arch/x86/kernel/cpu/bugs.c +12 -0
@@ -32,6 +32,7 @@#include <asm/hypervisor.h> #include <asm/tlbflush.h> #include <asm/cpu.h> +#include <asm/syscall.h> #include "cpu.h"
@@ -2134,6 +2135,16 @@ static void __init bhi_apply_mitigation(void)setup_force_cpu_cap (X86_FEATURE_CLEAR_BHB_VMEXIT ); } +static void __init syscall_dispatch_apply_mitigation (void ) +{ +if (!IS_ENABLED (CONFIG_X86_64 ) || spectre_v2_enabled !=SPECTRE_V2_NONE || + bhi_mitigation !=BHI_MITIGATION_OFF ) +return ; + +pr_info ("BHI mitigation off, dispatching syscalls through sys_call_table\n" ); +static_branch_enable (&x64_syscall_use_table); +} +static void __init spectre_v2_select_mitigation (void ) { if ((spectre_v2_cmd == SPECTRE_V2_CMD_RETPOLINE ||
@@ -3371,6 +3382,7 @@ void __init cpu_select_mitigations(void)bhi_apply_mitigation ();tsa_apply_mitigation ();vmscape_apply_mitigation (); +syscall_dispatch_apply_mitigation (); }#ifdef CONFIG_SYSFS
---
base-commit: 6d0933ae9d46674a6ea63a87e9e9ab4e662670c9
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
change-id: 20261028-syscall-table-dispatch-03d30189dc5b
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Peter Zijlstra
maintainerQuoted 26 lines · Breno Leitao
Linus' commit
1e3ad78334a6("x86/syscall: Don't force use of indirect calls for system calls") dispatches system calls with a switch statement, so that syscall entry has no indirect branch for BHI to target.As noted by Linus in the commit above, the compiler can either decide to generate an indirect jump, or just a series of conditional branches (which is more expensive).
With
CONFIG_MITIGATION_RETPOLINE, clang emits no indirect jump, and the switch becomes a tree of compares and conditional branches. That code is built in, so a kernel booted with mitigations=off still walks the tree on every system call.When neither the Spectre v2 nor the BHI mitigation is enabled, nothing in the kernel protects indirect branches, and the switch buys nothing. Use a static key to call through sys_call_table[] in that case.
The compiler emits that call through a retpoline thunk, which boot-time patching turns into a plain indirect call when retpolines are off.
On next-20261028 built with clang and booted with mitigations=off on a Xeon Gold 6138 at a fixed 2.0 GHz, perf bench syscall basic (getppid) and getpgid. Mean and standard deviation of 30 to 45 runs over 2 to 3 boots, and kernel instructions and cycles per call from perf stat:
Quoted 9 lines
CPU_MITIGATIONS=n =y =y + patch getppid() ns 131.2 +- 0.2 134.1 +- 0.1 131.7 +- 0.4 insns/cyc 310 / 212.1 331 / 218.1 314 / 213.1 getpgid() ns 129.8 +- 0.3 132.5 +- 0.2 130.0 +- 0.5 insns/cyc 302 / 209.4 323 / 214.1 306 / 209.8Kernels that enable either mitigation keep the switch, behind one extra jump.
Bah, this reminds me I still have these patches:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=x86/syscall
which make your patch impossible. The reason for not dispatching through the syscall table is that syscall hijacking becomes a _LOT_ more difficult.
Hello Peter,
Quoted 8 lines · Peter Zijlstra
On Thu, Oct 29, 2026 at 03:27:23AM -0700, Breno Leitao wrote:
Bah, this reminds me I still have these patches:https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=x86/syscall
which make your patch impossible. The reason for not dispatching through the syscall table is that syscall hijacking becomes a _LOT_ more difficult.
Cool, this would in fact solve my problem as well.
My goal is to make booting with mitigations=off equivalent to building with CONFIG_CPU_MITIGATIONS=n, so we can ship a single kernel and pick the mitigations at boot time. Your patches get there in a different way, but that doesn't matter for my use case.
With "x86_64/entry: Unconditionally use -fno-jump-table", x64_sys_call() becomes a compare tree on every x86_64 build, so system call dispatch is the same whether the mitigations are disabled at boot or at build time, which is basically my end goal.
That said, can I do anything to help those patches land? I see they are from June, and I am wondering if anything is blocking them.
Also, KBUILD_RISTFLAGS in that patch looks like a typo for KBUILD_RUSTFLAGS.
Thanks,
--breno
Peter Zijlstra
maintainerQuoted 19 lines · Breno Leitao
Hello Peter,
Quoted 8 lines · Peter Zijlstra
On Thu, Oct 29, 2026 at 03:27:23AM -0700, Breno Leitao wrote:
Bah, this reminds me I still have these patches:https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=x86/syscall
which make your patch impossible. The reason for not dispatching through the syscall table is that syscall hijacking becomes a _LOT_ more difficult.
Cool, this would in fact solve my problem as well.
My goal is to make booting with mitigations=off equivalent to building with
CONFIG_CPU_MITIGATIONS=n, so we can ship a single kernel and pick the mitigations at boot time. Your patches get there in a different way, but that doesn't matter for my use case.
Quoted 7 lines
With "x86_64/entry: Unconditionally use -fno-jump-table",
x64_sys_call()becomes a compare tree on everyx86_64build, so system call dispatch is the same whether the mitigations are disabled at boot or at build time, which is basically my end goal.Also,
KBUILD_RISTFLAGSin that patch looks like a typo forKBUILD_RUSTFLAGS.
Haha, good one, let me fix.
Quoted 2 lines
That said, can I do anything to help those patches land? I see they are from June, and I am wondering if anything is blocking them.
Mostly I just completely forgot about them :/
I've since rebased them and went back to look for robot reports on the tree, and found one weird case where GCC-14 with an UNWIND_FP=y build insists on not doing tail-calls which then trips up objtool noinst validation.
I need to figure a way out of the noinstr validation puzzle. Also, lunch and a school run. I'll try and post them soon.
Thanks!
Borislav Petkov
maintainerQuoted 3 lines · Breno Leitao
My goal is to make booting with mitigations=off equivalent to building with
CONFIG_CPU_MITIGATIONS=n, so we can ship a single kernel and pick the mitigations at boot time.
This might interest you then:
https://lore.kernel.org/all/[email protected]
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 8 lines · Borislav Petkov
Quoted 3 lines · Breno Leitao
My goal is to make booting with mitigations=off equivalent to building with
CONFIG_CPU_MITIGATIONS=n, so we can ship a single kernel and pick the mitigations at boot time.This might interest you then:
Thanks for the pointer. I am very interested in this.
I read most of the thread. As I understand it, there is a desire for something more dynamic, but that is non-trivial work that still needs to be done, and it is unclear if there will be follow-up (the patchset is 1year old)
Borislav Petkov
maintainerQuoted 4 lines · Breno Leitao
I read most of the thread. As I understand it, there is a desire for something more dynamic, but that is non-trivial work that still needs to be done, and it is unclear if there will be follow-up (the patchset is 1year old)
It needs reviewers and testers, yes, exactly because it is complex but a good thing to have, as you guys show interest.
The day has only 24h, however. :-\
But yes, we are going to refresh and update the patchset soon(tm).
The prerequisite is the stomp_machine NMI stuff:
https://lore.kernel.org/r/[email protected]
which needs careful poking at first.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 5 lines · Breno Leitao
With "x86_64/entry: Unconditionally use -fno-jump-table",
x64_sys_call()becomes a compare tree on everyx86_64build, so system call dispatch is the same whether the mitigations are disabled at boot or at build time, which is basically my end goal.
You're basically saying "with that patch, both cases do equally badly whether disabled at boot or build time".
That's not a good end goal. You want both cases to do equally *well*.
I do wonder if we could do computed jumps. It would be lovely if we could basically do something like
# syscall number in %rax
leaq table(%rax,%rax,4),%rax
jmp *%rax
table:
jmp syscall0
jmp syscall1
jmp syscall2
....and avoid any indirect memory lookup or indirect branches (outside of the one computed one). And no extra call/ret frame.
It's possible that the branch prediction hardware hates those kinds of very dense jmp patterns, though. I don't know.
We could have a single static jump that picks between this one and the "conditional branch tree" version, as in your patch at the beginning of the thread.
Linus
H. Peter Anvin
reviewerQuoted 27 lines · Linus Torvalds
Quoted 5 lines · Breno Leitao
With "x86_64/entry: Unconditionally use -fno-jump-table",
x64_sys_call()becomes a compare tree on everyx86_64build, so system call dispatch is the same whether the mitigations are disabled at boot or at build time, which is basically my end goal.You're basically saying "with that patch, both cases do equally badly whether disabled at boot or build time".
That's not a good end goal. You want both cases to do equally *well*.
I do wonder if we could do computed jumps. It would be lovely if we could basically do something like
# syscall number in %rax leaq table(%rax,%rax,4),%rax jmp *%rax table: jmp syscall0 jmp syscall1 jmp syscall2 ....and avoid any indirect memory lookup or indirect branches (outside of the one computed one). And no extra call/ret frame.
Quoted 8 lines
It's possible that the branch prediction hardware hates those kinds of very dense jmp patterns, though. I don't know.
We could have a single static jump that picks between this one and the "conditional branch tree" version, as in your patch at the beginning of the thread.
Linus
As far as I understand, with modern hardware there is very little difference between the two in practice.
The advantage of using a branch tree is that you don't end up having any *possibility* of out-of-range speculative accesses going off into some speculative hyperspace.
Hello Linus,
Quoted 5 lines · Linus Torvalds
On Thu, 29 Oct 2026 at 02:30, Breno Leitao <[email protected]> wrote:
We could have a single static jump that picks between this one and the "conditional branch tree" version, as in your patch at the beginning of the thread.
I am looking at this on top of Peter's "x86: Harden syscall dispatch" series [1], and before spending more time on it, I would like to clarify how you see this suggestion fitting with Peter's series.
Peter's patch 1 makes -fno-jump-tables unconditional on x86_64 and drops array_index_nospec() from the dispatch. Patch 4 then marks every syscall __noendbr, since dispatch is only direct calls.
The computed jump you suggested is somehow a jump table, just without the load: the syscall number picks the target of an indirect JMP. So I suspect we don't need array_index_nospec() back on that path, given the user explictly set mitigations=off (?).
Is that what you had in mind:
a) the switch tree by default
b) table as a fast path only for kernels booted with mitigations disabled
Thanks,
--breno
Quoted 8 lines · Breno Leitao
I am looking at this on top of Peter's "x86: Harden syscall dispatch" series [1], and before spending more time on it, I would like to clarify how you see this suggestion fitting with Peter's series.
Peter's patch 1 makes -fno-jump-tables unconditional on
x86_64and dropsarray_index_nospec()from the dispatch. Patch 4 then marks every syscall__noendbr, since dispatch is only direct calls.
So I'd just make this all a generated asm thing.
It would have to be done as asm, because I don't think gcc (of clang) understand to use computed branches. They always generate table lookups.
And doing the system calls as __noendbr is fine, because the resulting code never does an indirect branch to the system call. The only indirect branch would be that
leaq table(%rax,%rax,4),%rax
jmp *%raxwhich does the computed jump (the "table" isn't a table of pointers, it's just a table of 5-byte "jmp" instructions).
But *those* would have to be marked with endbr, I guess, so they don't actually end up being 5-byte at all. I guess they'd be 9-byte sequences ('endbr' is 4 bytes, no?).
So the actual sequence would have to be
leaq table(%rax,%rax,8),%rax
jmp *%raxwith that nasty generated sequence of 'endbr ; jmp syscallX' for the table.
Linus
Quoted 12 lines
I do wonder if we could do computed jumps. It would be lovely if we could basically do something like
# syscall number in %rax leaq table(%rax,%rax,4),%rax jmp *%rax table: jmp syscall0 jmp syscall1 jmp syscall2 ....
Quoted 2 lines
and avoid any indirect memory lookup or indirect branches (outside of the one computed one). And no extra call/ret frame.
You would need ENDBR64's too, when building for CET-IBT.
Quoted 2 lines
It's possible that the branch prediction hardware hates those kinds of very dense jmp patterns, though. I don't know.
This doesn't buy you anything, except a larger code footprint and more branch predictor pressure.
The moment you've got any indirect branch reasonable from the syscall entrypoint, you're open to all the Spectre v2 attacks.
That's fine if the user wants to go fast, but then just use a real jump table pointing to the real system calls. This is a Not-Inveted-Here halfway house which is just extra bytes to decode and an extra jump to perform.
~Andrew
Quoted 3 lines · Andrew Cooper
This doesn't buy you anything, except a larger code footprint and more branch predictor pressure.
Very possible. At some point in history, computed branches were cheaper than table lookups, but that may simply not be true any more.
Linus
Quoted 24 lines · On Fri, 30 Oct 2026 08:56:12 -1000 Linus Torvalds…
Quoted 5 lines · Breno Leitao
With "x86_64/entry: Unconditionally use -fno-jump-table",
x64_sys_call()becomes a compare tree on everyx86_64build, so system call dispatch is the same whether the mitigations are disabled at boot or at build time, which is basically my end goal.You're basically saying "with that patch, both cases do equally badly whether disabled at boot or build time".
That's not a good end goal. You want both cases to do equally *well*.
I do wonder if we could do computed jumps. It would be lovely if we could basically do something like
# syscall number in %rax leaq table(%rax,%rax,4),%rax jmp *%rax table: jmp syscall0 jmp syscall1 jmp syscall2 ....
Quoted 2 lines
and avoid any indirect memory lookup or indirect branches (outside of the one computed one). And no extra call/ret frame.
Could it be a table of 32bit offsets (from some point) so that it is a data cache access (or miss) rather than an instruction pipeline stall. You might need to use a retpoline to avoid needing endbra.
(or is there another way of avoiding the need for endbra?)
You probably need to patch at least some of the code to nops when it isn't being used.
Quoted 3 lines
It's possible that the branch prediction hardware hates those kinds of very dense jmp patterns, though. I don't know.
Some cpu have definitely not liked having a lot of branches in one cache line.
And performance will be absolutely horrid unless you are doing a benchmark that keeps doing the same system call.
The cost of each branch miss is likely to be at least 20 clocks. For a random system call half of the branches will be misses. Plus all the cache line reads and other delays. Definitely non-trivial.
David
Quoted 7 lines
We could have a single static jump that picks between this one and the "conditional branch tree" version, as in your patch at the beginning of the thread.
Linus
Peter Zijlstra
maintainerQuoted 27 lines · Linus Torvalds
Quoted 5 lines · Breno Leitao
With "x86_64/entry: Unconditionally use -fno-jump-table",
x64_sys_call()becomes a compare tree on everyx86_64build, so system call dispatch is the same whether the mitigations are disabled at boot or at build time, which is basically my end goal.You're basically saying "with that patch, both cases do equally badly whether disabled at boot or build time".
That's not a good end goal. You want both cases to do equally *well*.
I do wonder if we could do computed jumps. It would be lovely if we could basically do something like
# syscall number in %rax leaq table(%rax,%rax,4),%rax jmp *%rax table: jmp syscall0 jmp syscall1 jmp syscall2 ....and avoid any indirect memory lookup or indirect branches (outside of the one computed one). And no extra call/ret frame.
Quoted 6 lines
It's possible that the branch prediction hardware hates those kinds of very dense jmp patterns, though. I don't know.
We could have a single static jump that picks between this one and the "conditional branch tree" version, as in your patch at the beginning of the thread.
The thinking here being that you would still not actually load any value from the sys_call_table and keep it all in code?
As Andrew already pointed out, this required ENDBR (yes 4 bytes) in addition to the JMP.d32 (5 bytes). But when building with kCFI that would require even more.
Notably, you do not want the syscall dispatch to be a kCFI evasion gadget.
The kCFI landing pad would basically add another 16 bytes on top of those earlier 9 bytes, at which point you might as well round up and use 32 bytes per entry.
At which point one has to ask the obvious question on whether this is actually better than just keeping the sys_call_table and syscall functions as is.
That is; do we want to go back to unconditionally doing:
nr = array_index_nospec(nr, NR_syscalls); regs->ax = sys_call_table[nr](regs);
Yes, this will turn into retpoline for old hardware, get patched back into regular indirect call on recent hardware, and will get kCFI/FineIBT crud on for 'free' when your build swings that way.
It does mean syscall hijack through sys_call_table[] is a thing again, but it will be the fastest option on modern chips and automagically 'scale' with the mitigation options selected.
OTOH there are still million upon millions of that old stuff out there in data centers :/