x86: two-phase syscall tracing and seccomp fastpath
Subsystems (4)
v4 cover x86: two-phase syscall tracing and seccomp fastpath
This applies to:git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git seccomp-fastpath
Gitweb:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=seccomp/fastpath
This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return SECCOMP_RET_TRACE.
This series depends on splitting the seccomp hooks into two phases. The first phase evaluates the filter; it can skip syscalls, allow them, kill the calling task, or pass a u32 to the second phase. The second phase requires a full tracing context, and it sends ptrace events if necessary. The seccomp core part is in Kees' seccomp/fastpath tree.
These patches implement a similar split for the x86 syscall entry work. The C callback is invoked in two phases: the first has only a partial frame, and it can request phase 2 processing with a full frame.
Finally, I switch the 64-bit system_call code to use the new split entry work. This is a net deletion of assembly code: it replaces all of the audit entry muck.
In the process, I fixed some bugs.
If this is acceptable, someone can do the same tweak for the ia32entry and entry_32 code.
This passes all seccomp tests that I know of.
Changes from v3:
- Dropped the core seccomp changes from the email -- Kees has applied them.
- Add patch 2 (the TIF_NOHZ change).
- Fix TIF_NOHZ in the two-phase entry code (thanks, Oleg).
Changes from v2:
- Fixed 32-bit x86 build (and the tests pass).
- Put the doc patch where it belongs.
Changes from v1:
- Rebased on top of Kees' shiny new seccomp tree (no effect on the x86 part).
- Improved patch 6 vs patch 7 split (thanks Alexei!)
- Fixed bogus -ENOSYS in patch 5 (thanks Kees!)
- Improved changelog message in patch 6.
Changes from RFC version:
- The first three patches are more or less the same
- The rest is more or less a rewrite
Andy Lutomirski (5):
x86,x32,audit: Fix x32's AUDIT_ARCH wrt audit
x86,entry: Only call user_exit if TIF_NOHZ
x86: Split syscall_trace_enter into two phases x86_64,entry: Treat regs->ax the same in fastpath and slowpath
syscalls
x86_64,entry: Use split-phase syscall_trace_enter for 64-bit syscalls
arch/x86/include/asm/calling.h | 6 +- arch/x86/include/asm/ptrace.h | 5 ++ arch/x86/kernel/entry_64.S | 51 +++++-------- arch/x86/kernel/ptrace.c | 159 ++++++++++++++++++++++++++++++++++------- 4 files changed, 161 insertions(+), 60 deletions(-)
Signature · 1 line
1.9.3
PATCH v4 1/5 x86,x32,audit: Fix x32's AUDIT_ARCH wrt audit
is_compat_task() is the wrong check for audit arch; the check should be is_ia32_task(): x32 syscalls should be AUDIT_ARCH_X86_64, not AUDIT_ARCH_I386.
CONFIG_AUDITSYSCALL is currently incompatible with x32, so this has no visible effect.
--- arch/x86/kernel/ptrace.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
arch/x86/kernel/ptrace.c +1 -10
@@ -1441,15 +1441,6 @@ void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,force_sig_info (SIGTRAP , &info, tsk); } - -#ifdef CONFIG_X86_32 -# define IS_IA32 1 -#elif definedCONFIG_IA32_EMULATION -# define IS_IA32 is_compat_task() -#else -# define IS_IA32 0 -#endif -/* * We must return the syscall number to actually look up in the table. * This can be -1L to skip running any syscall at all.
@@ -1487,7 +1478,7 @@ long syscall_trace_enter(struct pt_regs *regs)if (unlikely (test_thread_flag (TIF_SYSCALL_TRACEPOINT )))trace_sys_enter (regs, regs->orig_ax); -if (IS_IA32 ) +if (is_ia32_task ()) audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax, regs->bx, regs->cx,
Signature · 1 line
1.9.3
PATCH v4 2/5 x86,entry: Only call user_exit if TIF_NOHZ
In reply to Andy LutomirskiThe RCU context tracking code requires that arch code call user_exit() on any entry into kernel code if TIF_NOHZ is set. This patch adds a check for TIF_NOHZ and a comment to the syscall entry tracing code.
The main purpose of this patch is to make the code easier to follow:
one can read the body of user_exit and of every function it calls without finding any explanation of why it's called for traced syscalls but not for untraced syscalls. This makes it clear when user_exit() is necessary.
--- arch/x86/kernel/ptrace.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kernel/ptrace.c +6 -1
@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs) {long ret =0 ; -user_exit (); +/* +* If TIF_NOHZ is set, we are required to call user_exit() before +* doing anything that could touch RCU. +*/ +if (test_thread_flag (TIF_NOHZ )) +user_exit ();/* * If we stepped into a sysenter/syscall insn, it trapped in
Signature · 1 line
1.9.3
Oleg Nesterov
maintainerRe: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
arch/x86/kernel/ptrace.c long syscall_trace_enter(struct pt_regs *regs) · line 1457
Quoted 12 lines · Andy Lutomirski
arch/x86/kernel/ptrace.c +6
-1@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs) {long ret =0 ; -user_exit (); +/* +* If TIF_NOHZ is set, we are required to call user_exit() before +* doing anything that could touch RCU. +*/ +if (test_thread_flag (TIF_NOHZ )) +user_exit ();
Personally I still think this change just adds more confusion, but I leave this to you and Frederic.
It is not that "If TIF_NOHZ is set, we are required to call user_exit()", we need to call user_exit() just because we enter the kernel. TIF_NOHZ is just the implementation detail which triggers this slow path.
At least it should be correct, unless I am confused even more than I think.
Oleg.
Re: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 17 lines · Oleg Nesterov
arch/x86/kernel/ptrace.c long syscall_trace_enter(struct pt_regs *regs) · line 1457
Quoted 12 lines · Andy Lutomirski
arch/x86/kernel/ptrace.c +6
-1@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs) {long ret =0 ; -user_exit (); +/* +* If TIF_NOHZ is set, we are required to call user_exit() before +* doing anything that could touch RCU. +*/ +if (test_thread_flag (TIF_NOHZ )) +user_exit ();Personally I still think this change just adds more confusion, but I leave this to you and Frederic.
Quoted 5 lines
It is not that "If
TIF_NOHZis set, we are required to calluser_exit()", we need to calluser_exit()just because we enter the kernel.TIF_NOHZis just the implementation detail which triggers this slow path.At least it should be correct, unless I am confused even more than I think.
Agreed, Perhaps the confusion is on the syscall_trace_enter() name which suggests this is only about tracing? syscall_slowpath_enter() could be an alternative. But that's still tracing in a general sense so...
Re: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 22 lines · Frederic Weisbecker
Quoted 21 lines · Oleg Nesterov
arch/x86/kernel/ptrace.c long syscall_trace_enter(struct pt_regs *regs) · line 1457
Quoted 12 lines · Andy Lutomirski
arch/x86/kernel/ptrace.c +6
-1@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs) {long ret =0 ; -user_exit (); +/* +* If TIF_NOHZ is set, we are required to call user_exit() before +* doing anything that could touch RCU. +*/ +if (test_thread_flag (TIF_NOHZ )) +user_exit ();Personally I still think this change just adds more confusion, but I leave this to you and Frederic.
It is not that "If
TIF_NOHZis set, we are required to calluser_exit()", we need to calluser_exit()just because we enter the kernel.TIF_NOHZis just the implementation detail which triggers this slow path.
Quoted 5 lines
Quoted 1 line
At least it should be correct, unless I am confused even more than I think.
Agreed, Perhaps the confusion is on the
syscall_trace_enter()name which suggests this is only about tracing?syscall_slowpath_enter()could be an alternative. But that's still tracing in a general sense so...
At the end of the day, the syscall slowpath code calls a bunch of functions depending on what TIF_XYZ flags are set. As long as it's structured like "if (TIF_A) do_a(); if (TIF_B) do_b();" or something like that, it's comprehensible. But once random functions with no explicit flag checks or comments start showing up, it gets confusing.
If it's indeed all-or-nothing, I could remove the check and add a comment. But please keep in mind that, currently, the slow path is *slow*, and my patches only improve the entry case. So enabling context tracking on every task will hurt.
--Andy
Re: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 29 lines · Andy Lutomirski
Quoted 22 lines · Frederic Weisbecker
Quoted 21 lines · Oleg Nesterov
arch/x86/kernel/ptrace.c long syscall_trace_enter(struct pt_regs *regs) · line 1457
Quoted 12 lines · Andy Lutomirski
arch/x86/kernel/ptrace.c +6
-1@@ -1449,7 +1449,12 @@ long syscall_trace_enter(struct pt_regs *regs) {long ret =0 ; -user_exit (); +/* +* If TIF_NOHZ is set, we are required to call user_exit() before +* doing anything that could touch RCU. +*/ +if (test_thread_flag (TIF_NOHZ )) +user_exit ();Personally I still think this change just adds more confusion, but I leave this to you and Frederic.
It is not that "If
TIF_NOHZis set, we are required to calluser_exit()", we need to calluser_exit()just because we enter the kernel.TIF_NOHZis just the implementation detail which triggers this slow path.Quoted 5 lines
Quoted 1 line
At least it should be correct, unless I am confused even more than I think.
Agreed, Perhaps the confusion is on the
syscall_trace_enter()name which suggests this is only about tracing?syscall_slowpath_enter()could be an alternative. But that's still tracing in a general sense so...
Quoted 5 lines
At the end of the day, the syscall slowpath code calls a bunch of functions depending on what
TIF_XYZflags are set. As long as it's structured like "if (TIF_A)do_a(); if (TIF_B)do_b();" or something like that, it's comprehensible. But once random functions with no explicit flag checks or comments start showing up, it gets confusing.
Yeah that's a point. I don't mind much the TIF_NOHZ test if you like.
Quoted 5 lines
If it's indeed all-or-nothing, I could remove the check and add a comment. But please keep in mind that, currently, the slow path is *slow*, and my patches only improve the entry case. So enabling context tracking on every task will hurt.
That's what we do anyway. I haven't found a safe way to enabled context tracking without tracking all CPUs.
Quoted 2 lines
--Andy
Oleg Nesterov
maintainerRe: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 10 lines · Frederic Weisbecker
Quoted 6 lines · Andy Lutomirski
At the end of the day, the syscall slowpath code calls a bunch of functions depending on what
TIF_XYZflags are set. As long as it's structured like "if (TIF_A)do_a(); if (TIF_B)do_b();" or something like that, it's comprehensible. But once random functions with no explicit flag checks or comments start showing up, it gets confusing.Yeah that's a point. I don't mind much the
TIF_NOHZtest if you like.
And in my opinion
if (work & TIF_XYZ) user_exit();
looks even more confusing. Because, once again, TIF_XYZ is not the reason to call user_exit().
Not to mention this adds a minor performance penalty.
Quoted 7 lines
Quoted 4 lines
If it's indeed all-or-nothing, I could remove the check and add a comment. But please keep in mind that, currently, the slow path is *slow*, and my patches only improve the entry case. So enabling context tracking on every task will hurt.
That's what we do anyway. I haven't found a safe way to enabled context tracking without tracking all CPUs.
And if we change this, then the code above becomes racy. The state of TIF_XYZ can be changed right after the check. OK, it is racy anyway ;) but still this adds more confusion.
I feel that TIF_XYZ must die. But yes, yes, I know that it is very simple to say this. And no, so far I do not know how we can improve this all.
But again, again, I won't insist. Just another "can't resist" email, please ignore.
Oleg.
Re: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 14 lines · Oleg Nesterov
Quoted 10 lines · Frederic Weisbecker
Quoted 6 lines · Andy Lutomirski
At the end of the day, the syscall slowpath code calls a bunch of functions depending on what
TIF_XYZflags are set. As long as it's structured like "if (TIF_A)do_a(); if (TIF_B)do_b();" or something like that, it's comprehensible. But once random functions with no explicit flag checks or comments start showing up, it gets confusing.Yeah that's a point. I don't mind much the
TIF_NOHZtest if you like.And in my opinion
Quoted 7 lines
if (work & TIF_XYZ) user_exit();looks even more confusing. Because, once again,
TIF_XYZis not the reason to calluser_exit().Not to mention this adds a minor performance penalty.
That's a point too! You guys both convinced me! ;-)
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
If it's indeed all-or-nothing, I could remove the check and add a comment. But please keep in mind that, currently, the slow path is *slow*, and my patches only improve the entry case. So enabling context tracking on every task will hurt.
Quoted 6 lines
Quoted 2 lines
That's what we do anyway. I haven't found a safe way to enabled context tracking without tracking all CPUs.
And if we change this, then the code above becomes racy. The state of
TIF_XYZcan be changed right after the check. OK, it is racy anyway ;) but still this adds more confusion.
No because all running tasks have this flag set when context tracking is enabled. And context tracking can't be disabled on runtime.
Oleg Nesterov
maintainerRe: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 19 lines · Frederic Weisbecker
Quoted 17 lines · Oleg Nesterov
Quoted 10 lines · Frederic Weisbecker
Quoted 6 lines · Andy Lutomirski
At the end of the day, the syscall slowpath code calls a bunch of functions depending on what
TIF_XYZflags are set. As long as it's structured like "if (TIF_A)do_a(); if (TIF_B)do_b();" or something like that, it's comprehensible. But once random functions with no explicit flag checks or comments start showing up, it gets confusing.Yeah that's a point. I don't mind much the
TIF_NOHZtest if you like.And in my opinion
if (work & TIF_XYZ) user_exit();
Quoted 6 lines
Quoted 4 lines
looks even more confusing. Because, once again,
TIF_XYZis not the reason to calluser_exit().Not to mention this adds a minor performance penalty.
That's a point too! You guys both convinced me! ;-)
Very nice, now I know that you can agree with 2 opposite opinions at the same time ;)
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
If it's indeed all-or-nothing, I could remove the check and add a comment. But please keep in mind that, currently, the slow path is *slow*, and my patches only improve the entry case. So enabling context tracking on every task will hurt.
That's what we do anyway. I haven't found a safe way to enabled context tracking without tracking all CPUs.
Quoted 6 lines
Quoted 3 lines
And if we change this, then the code above becomes racy. The state of
TIF_XYZcan be changed right after the check. OK, it is racy anyway ;) but still this adds more confusion.No because all running tasks have this flag set when context tracking is enabled. And context tracking can't be disabled on runtime.
Yes, yes, please note that I said "if we change this".
Oleg.
Oleg Nesterov
maintainerRe: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
Quoted 5 lines · Oleg Nesterov
Quoted 3 lines · Frederic Weisbecker
Quoted 1 line · Oleg Nesterov
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
And if we change this, then the code above becomes racy. The state of
TIF_XYZcan be changed right after the check. OK, it is racy anyway ;) but still this adds more confusion.No because all running tasks have this flag set when context tracking is enabled. And context tracking can't be disabled on runtime.
Yes, yes, please note that I said "if we change this".
And "it is racy anyway" connects to the problems we discuss in another thread.
Oleg.
Re: [PATCH v4 2/5] x86,entry: Only call user_exit if TIF_NOHZ
In reply to Oleg NesterovQuoted 23 lines · Oleg Nesterov
Quoted 22 lines · Frederic Weisbecker
Quoted 20 lines · Oleg Nesterov
Quoted 10 lines · Frederic Weisbecker
Quoted 6 lines · Andy Lutomirski
At the end of the day, the syscall slowpath code calls a bunch of functions depending on what
TIF_XYZflags are set. As long as it's structured like "if (TIF_A)do_a(); if (TIF_B)do_b();" or something like that, it's comprehensible. But once random functions with no explicit flag checks or comments start showing up, it gets confusing.Yeah that's a point. I don't mind much the
TIF_NOHZtest if you like.And in my opinion
if (work & TIF_XYZ) user_exit();looks even more confusing. Because, once again,
TIF_XYZis not the reason to calluser_exit().
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Not to mention this adds a minor performance penalty.
That's a point too! You guys both convinced me! ;-)
Very nice, now I know that you can agree with 2 opposite opinions at the same time ;)
That's what an Acked-by from Schroedinger would look like!
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
If it's indeed all-or-nothing, I could remove the check and add a comment. But please keep in mind that, currently, the slow path is *slow*, and my patches only improve the entry case. So enabling context tracking on every task will hurt.
That's what we do anyway. I haven't found a safe way to enabled context tracking without tracking all CPUs.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
And if we change this, then the code above becomes racy. The state of
TIF_XYZcan be changed right after the check. OK, it is racy anyway ;) but still this adds more confusion.No because all running tasks have this flag set when context tracking is enabled. And context tracking can't be disabled on runtime.
Yes, yes, please note that I said "if we change this".
Yeah but the NO_HZ test wouldn't change much the situation I think.
PATCH v4 3/5 x86: Split syscall_trace_enter into two phases
In reply to Andy LutomirskiThis splits syscall_trace_enter into syscall_trace_enter_phase1 and syscall_trace_enter_phase2. Only phase 2 has full pt_regs, and only phase 2 is permitted to modify any of pt_regs except for orig_ax.
The intent is that phase 1 can be called from the syscall fast path.
In this implementation, phase1 can handle any combination of TIF_NOHZ (RCU context tracking), TIF_SECCOMP, and TIF_SYSCALL_AUDIT, unless seccomp requests a ptrace event, in which case phase2 is forced.
In principle, this could yield a big speedup for TIF_NOHZ as well as for TIF_SECCOMP if syscall exit work were similarly split up.
--- arch/x86/include/asm/ptrace.h | 5 ++ arch/x86/kernel/ptrace.c | 151 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 135 insertions(+), 21 deletions(-)
arch/x86/include/asm/ptrace.h +5 -0
@@ -75,6 +75,11 @@ convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);extern void send_sigtrap (struct task_struct *tsk,struct pt_regs *regs,int error_code,int si_code); + +extern unsigned long syscall_trace_enter_phase1 (struct pt_regs *,u32 arch); +extern long syscall_trace_enter_phase2 (struct pt_regs *,u32 arch, +unsigned long phase1_result); +extern long syscall_trace_enter (struct pt_regs *);extern void syscall_trace_leave (struct pt_regs *);
arch/x86/kernel/ptrace.c +130 -21
190 lines not on this page · raw message
Signature · 1 line
1.9.3
Oleg Nesterov
maintainerRe: [PATCH v4 3/5] x86: Split syscall_trace_enter into two phases
Quoted 13 lines · Andy Lutomirski
arch/x86/kernel/ptrace.c +8
-0+long syscall_trace_enter_phase2 (struct pt_regs *regs,u32 arch, +unsigned long phase1_result) +{ +long ret =0 ; +u32 work =ACCESS_ONCE (current_thread_info ()->flags) & + _TIF_WORK_SYSCALL_ENTRY; + +BUG_ON (regs !=task_pt_regs (current));/* * If we stepped into a sysenter/syscall insn, it trapped in
arch/x86/kernel/ptrace.c long syscall_trace_enter(struct pt_regs *regs) · line 1573
Quoted 6 lines
arch/x86/kernel/ptrace.c +1
-1@@ -1463,17 +1569,20 @@ long syscall_trace_enter(struct pt_regs *regs)* do_debug() and we need to set it again to restore the user * state. If we entered on the slow path, TF was already set. */ -if (test_thread_flag (TIF_SINGLESTEP )) +if (work & _TIF_SINGLESTEP) regs->flags |=X86_EFLAGS_TF ;
This is minor, I won't argue, but this can be moved into phase1(), afaics.
Oleg.
PATCH v4 4/5 x86_64,entry: Treat regs->ax the same in fastpath and slowpath syscalls
In reply to Andy LutomirskiFor slowpath syscalls, we initialize regs->ax to -ENOSYS and stick the syscall number into regs->orig_ax prior to any possible tracing and syscall execution. This is user-visible ABI used by ptrace syscall emulation and seccomp.
For fastpath syscalls, there's no good reason not to do the same thing. It's even slightly simpler than what we're currently doing. It probably has no measureable performance impact. It should have no user-visible effect.
The purpose of this patch is to prepare for two-phase syscall tracing, in which the first phase might modify the saved RAX without leaving the fast path. This change is just subtle enough that I'm keeping it separate.
--- arch/x86/include/asm/calling.h | 6 +++++- arch/x86/kernel/entry_64.S | 13 ++++--------- 2 files changed, 9 insertions(+), 10 deletions(-)
arch/x86/include/asm/calling.h +5 -1
@@ -85,7 +85,7 @@ For 32-bit we have the following conventions - kernel is built with#define ARGOFFSET R11#define SWFRAME ORIG_RAX - .macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1 + .macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1, rax_enosys=0 subq $9*8+\addskip, %rsp CFI_ADJUST_CFA_OFFSET 9*8+\addskip movq_cfi rdi, 8*8
@@ -96,7 +96,11 @@ For 32-bit we have the following conventions - kernel is built with movq_cfi rcx, 5*8 .endif + .if \rax_enosys + movq $-ENOSYS, 4*8(%rsp) + .else movq_cfi rax, 4*8 + .endif .if \save_r891011 movq_cfi r8, 3*8
arch/x86/kernel/entry_64.S +4 -9
@@ -405,8 +405,8 @@ GLOBAL(system_call_after_swapgs) * and short: */ ENABLE_INTERRUPTS(CLBR_NONE) - SAVE_ARGS 8,0 - movq %rax,ORIG_RAX-ARGOFFSET(%rsp) + SAVE_ARGS 8, 0, rax_enosys=1 + movq_cfi rax,(ORIG_RAX-ARGOFFSET) movq %rcx,RIP-ARGOFFSET(%rsp) CFI_REL_OFFSET rip,RIP-ARGOFFSET testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
@@ -418,7 +418,7 @@ system_call_fastpath: andl $__SYSCALL_MASK,%eax cmpl $__NR_syscall_max,%eax #endif - ja badsys + ja ret_from_sys_call /* and return regs->ax */ movq %r10,%rcx call *sys_call_table(,%rax,8) # XXX: rip relative movq %rax,RAX-ARGOFFSET(%rsp)
@@ -477,10 +477,6 @@ sysret_signal: FIXUP_TOP_OF_STACK %r11, -ARGOFFSET jmp int_check_syscall_exit_work -badsys: - movq $-ENOSYS,RAX-ARGOFFSET(%rsp) - jmp ret_from_sys_call - #ifdef CONFIG_AUDITSYSCALL /* * Fast path for syscall audit without full syscall trace.
@@ -520,7 +516,6 @@ tracesys: jz auditsys #endif SAVE_REST - movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */ FIXUP_TOP_OF_STACK %rdi movq %rsp,%rdi call syscall_trace_enter
@@ -537,7 +532,7 @@ tracesys: andl $__SYSCALL_MASK,%eax cmpl $__NR_syscall_max,%eax #endif - ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */ + ja int_ret_from_sys_call /* RAX(%rsp) is already set */ movq %r10,%rcx /* fixup for C */ call *sys_call_table(,%rax,8) movq %rax,RAX-ARGOFFSET(%rsp)
Signature · 1 line
1.9.3
PATCH v4 5/5 x86_64,entry: Use split-phase syscall_trace_enter for 64-bit syscalls
In reply to Andy LutomirskiOn KVM on my box, this reduces the overhead from an always-accept seccomp filter from ~130ns to ~17ns. Most of that comes from avoiding IRET on every syscall when seccomp is enabled.
In extremely approximate hacked-up benchmarking, just bypassing IRET saves about 80ns, so there's another 43ns of savings here from simplifying the seccomp path.
The diffstat is also rather nice :)
--- arch/x86/kernel/entry_64.S | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-)
arch/x86/kernel/entry_64.S +15 -23
@@ -479,22 +479,6 @@ sysret_signal: #ifdef CONFIG_AUDITSYSCALL /* - * Fast path for syscall audit without full syscall trace. - * We just call __audit_syscall_entry() directly, and then - * jump back to the normal fast path. - */ -auditsys: - movq %r10,%r9 /* 6th arg: 4th syscall arg */ - movq %rdx,%r8 /* 5th arg: 3rd syscall arg */ - movq %rsi,%rcx /* 4th arg: 2nd syscall arg */ - movq %rdi,%rdx /* 3rd arg: 1st syscall arg */ - movq %rax,%rsi /* 2nd arg: syscall number */ - movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */ - call __audit_syscall_entry - LOAD_ARGS 0 /* reload call-clobbered registers */ - jmp system_call_fastpath - - /* * Return fast path for syscall audit. Call __audit_syscall_exit() * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT * masked off.
@@ -511,17 +495,25 @@ sysret_audit: /* Do syscall tracing */ tracesys: -#ifdef CONFIG_AUDITSYSCALL - testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET) - jz auditsys -#endif + leaq -REST_SKIP(%rsp), %rdi + movq $AUDIT_ARCH_X86_64, %rsi + call syscall_trace_enter_phase1 + test %rax, %rax + jnz tracesys_phase2 /* if needed, run the slow path */ + LOAD_ARGS 0 /* else restore clobbered regs */ + jmp system_call_fastpath /* and return to the fast path */ + +tracesys_phase2: SAVE_REST FIXUP_TOP_OF_STACK %rdi - movq %rsp,%rdi - call syscall_trace_enter + movq %rsp, %rdi + movq $AUDIT_ARCH_X86_64, %rsi + movq %rax,%rdx + call syscall_trace_enter_phase2 + /* * Reload arg registers from stack in case ptrace changed them. - * We don't reload %rax because syscall_trace_enter() returned + * We don't reload %rax because syscall_trace_entry_phase2() returned * the value it wants us to use in the table lookup. */ LOAD_ARGS ARGOFFSET, 1
Signature · 1 line
1.9.3
Oleg Nesterov
maintainerAndy, to avoid the confusion: I am not trying to review this changes. As you probably know my understanding of asm code in entry.S is very limited.
Just a couple of questions to ensure I understand this correctly.
Quoted 5 lines · Andy Lutomirski
This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return
SECCOMP_RET_TRACE.
And only after I look at 5/5 I _seem_ to actually understand where this speedup comes from.
So. Currently tracesys: path always lead to "iret" after syscall, with this change we can avoid it if phase_1() returns zero, correct?
And, this also removes the special TIF_SYSCALL_AUDIT-only case in entry.S, cool.
I am wondering if we can do something similar with do_notify_resume() ?
Stupid question. To simplify, lets forget that syscall_trace_enter() already returns the value. Can't we simplify the asm code if we do not export 2 functions, but make syscall_trace_enter() return "bool slow_path_is_needed". So that "tracesys:" could do
// pseudo code
tracesys:
SAVE_REST
FIXUP_TOP_OF_STACK
call syscall_trace_enter
if (!slow_path_is_needed) {
addq REST_SKIP, %rsp
jmp system_call_fastpath
}
...?
Once again, I am just curious, it is not that I actually suggest to consider this option.
Oleg.
Quoted 46 lines · Oleg Nesterov
Andy, to avoid the confusion: I am not trying to review this changes. As you probably know my understanding of asm code in
entry.Sis very limited.Just a couple of questions to ensure I understand this correctly.
Quoted 5 lines · Andy Lutomirski
This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return
SECCOMP_RET_TRACE.And only after I look at 5/5 I _seem_ to actually understand where this speedup comes from.
So. Currently tracesys: path always lead to "iret" after syscall, with this change we can avoid it if
phase_1()returns zero, correct?And, this also removes the special TIF_SYSCALL_AUDIT-only case in
entry.S, cool.I am wondering if we can do something similar with
do_notify_resume()?Stupid question. To simplify, lets forget that
syscall_trace_enter()already returns the value. Can't we simplify the asm code if we do not export 2 functions, but makesyscall_trace_enter()return "boolslow_path_is_needed". So that "tracesys:" could do// pseudo codetracesys:
SAVE_REST FIXUP_TOP_OF_STACK call syscall_trace_enter if (!slow_path_is_needed) { addq REST_SKIP, %rsp jmp system_call_fastpath } ...
Quoted 4 lines
?
Once again, I am just curious, it is not that I actually suggest to consider this option.
We could, but this would lose a decent amount of the speedup. I could try it and benchmark it, but I'm guessing that the save and restore is kind of expensive. This will make audit slower than it currently is, which may also annoy some people. (Not me.)
I'm also not convinced that it would be much simpler. My code is currently:
tracesys:
leaq -REST_SKIP(%rsp), %rdi
movq $AUDIT_ARCH_X86_64, %rsi
call syscall_trace_enter_phase1
test %rax, %rax
jnz tracesys_phase2 /* if needed, run the slow path */
LOAD_ARGS 0 /* else restore clobbered regs */
jmp system_call_fastpath /* and return to the fast path */
tracesys_phase2:
SAVE_REST
FIXUP_TOP_OF_STACK %rdi
movq %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
movq %rax,%rdx
call syscall_trace_enter_phase2
LOAD_ARGS ARGOFFSET, 1
RESTORE_REST
... slow path here ...
It would end up looking more like (totally untested):
tracesys:
SAVE_REST
FIXUP_TOP_OF_STACK %rdi
mov %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
call syscall_trace_enter
LOAD_ARGS
RESTORE_REST
test [whatever condition]
j[cond] system_call_fastpath
... slow path here ...
So it's a bit simpler. Oddly, the ia32entry code doesn't have this multiple syscall path distinction.
SAVE_REST is 6 movq instructions and a subq. FIXUP_TOP_OF_STACK is 7 movqs (and 8 if I ever get my way). RESTORE_TOP_OF_STACK is 4. RESTORE_REST is 6 movqs and an adsq. So we're talking about avoiding 21 movqs, and addq, and a subq. That may be significant. (And I suspect that the difference is much larger on platforms like arm64, but that's a separate issue.)
Quoted 99 lines · Andy Lutomirski
Quoted 46 lines · Oleg Nesterov
Andy, to avoid the confusion: I am not trying to review this changes. As you probably know my understanding of asm code in
entry.Sis very limited.Just a couple of questions to ensure I understand this correctly.
Quoted 5 lines · Andy Lutomirski
This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return
SECCOMP_RET_TRACE.And only after I look at 5/5 I _seem_ to actually understand where this speedup comes from.
So. Currently tracesys: path always lead to "iret" after syscall, with this change we can avoid it if
phase_1()returns zero, correct?And, this also removes the special TIF_SYSCALL_AUDIT-only case in
entry.S, cool.I am wondering if we can do something similar with
do_notify_resume()?Stupid question. To simplify, lets forget that
syscall_trace_enter()already returns the value. Can't we simplify the asm code if we do not export 2 functions, but makesyscall_trace_enter()return "boolslow_path_is_needed". So that "tracesys:" could do// pseudo codetracesys:
SAVE_REST FIXUP_TOP_OF_STACK call syscall_trace_enter if (!slow_path_is_needed) { addq REST_SKIP, %rsp jmp system_call_fastpath } ...Quoted 4 lines
?
Once again, I am just curious, it is not that I actually suggest to consider this option.
We could, but this would lose a decent amount of the speedup. I could try it and benchmark it, but I'm guessing that the save and restore is kind of expensive. This will make audit slower than it currently is, which may also annoy some people. (Not me.)
I'm also not convinced that it would be much simpler. My code is currently:
tracesys:
leaq -REST_SKIP(%rsp), %rdi
movq $AUDIT_ARCH_X86_64, %rsi
callsyscall_trace_enter_phase1
test %rax, %rax
jnztracesys_phase2/* if needed, run the slow path */
LOAD_ARGS0 /* else restore clobbered regs */
jmpsystem_call_fastpath/* and return to the fast path */
tracesys_phase2:
SAVE_REST
FIXUP_TOP_OF_STACK%rdi
movq %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
movq %rax,%rdx
callsyscall_trace_enter_phase2
LOAD_ARGSARGOFFSET, 1
RESTORE_REST... slow path here ...
It would end up looking more like (totally untested):
tracesys:
SAVE_REST
FIXUP_TOP_OF_STACK%rdi
mov %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
callsyscall_trace_enter
LOAD_ARGS
RESTORE_REST
test [whatever condition]
j[cond]system_call_fastpath... slow path here ...
So it's a bit simpler. Oddly, the ia32entry code doesn't have this multiple syscall path distinction.
Quoted 6 lines
SAVE_RESTis 6 movq instructions and a subq.FIXUP_TOP_OF_STACKis 7 movqs (and 8 if I ever get my way).RESTORE_TOP_OF_STACKis 4.RESTORE_RESTis 6 movqs and an adsq. So we're talking about avoiding 21 movqs, and addq, and a subq. That may be significant. (And I suspect that the difference is much larger on platforms like arm64, but that's a separate issue.)
To put some more options on the table: there's an argument to be made that the whole fast-path/slow-path split isn't worth it. We could unconditionally set up a full frame for all syscalls. This means:
No FIXUP_TOP_OF_STACK. Instead, the system_call entry sets up RSP, SS, CS, RCX, and EFLAGS right away. That's five stores for all syscalls instead of two loads and five stores for syscalls that need it. But it also gets rid of RESTORE_TOP_OF_STACK completely.
No more bugs involving C code that assumes a full stack frame when no such frame exists inside syscall code.
We could possibly remove a whole bunch of duplicated code.
The upshot would be simpler code, faster slow-path syscalls, and slower fast-path syscalls (but probably not much slower). On the other hand, there's zero chance that this would be ready for 3.17.
I'd tend to advocate for keeping the approach in my patches for now. It's probably a smaller assembly diff than any of the other options -- the split between fast-path, slower fast-path, and slow-path already exists due to the audit crap. If we end up making more radical changes later, then at worst we end up reverting part of the change to ptrace.c.
--Andy
Oleg Nesterov
maintainerQuoted 7 lines · Andy Lutomirski
Quoted 7 lines
SAVE_RESTis 6 movq instructions and a subq.FIXUP_TOP_OF_STACKis 7 movqs (and 8 if I ever get my way).RESTORE_TOP_OF_STACKis 4.RESTORE_RESTis 6 movqs and an adsq. So we're talking about avoiding 21 movqs, and addq, and a subq. That may be significant. (And I suspect that the difference is much larger on platforms like arm64, but that's a separate issue.)
OK, thanks. We could probably simplify the logic in phase1 + phase2 if it was a single function though.
Quoted 3 lines
To put some more options on the table: there's an argument to be made that the whole fast-path/slow-path split isn't worth it. We could unconditionally set up a full frame for all syscalls. This means:
Or, at least, can't we allocate the full frame and avoid "add/sub %rsp"?
Quoted 1 line
This means:
...
Quoted 4 lines
On the
other hand, there's zero chance that this would be ready for 3.17.I'd tend to advocate for keeping the approach in my patches for now.
Yes, sure, I didn't try to convince you to change this code. Thanks.
Oleg.
Quoted 116 lines · Andy Lutomirski
Quoted 99 lines · Andy Lutomirski
Quoted 46 lines · Oleg Nesterov
Andy, to avoid the confusion: I am not trying to review this changes. As you probably know my understanding of asm code in
entry.Sis very limited.Just a couple of questions to ensure I understand this correctly.
Quoted 5 lines · Andy Lutomirski
This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return
SECCOMP_RET_TRACE.And only after I look at 5/5 I _seem_ to actually understand where this speedup comes from.
So. Currently tracesys: path always lead to "iret" after syscall, with this change we can avoid it if
phase_1()returns zero, correct?And, this also removes the special TIF_SYSCALL_AUDIT-only case in
entry.S, cool.I am wondering if we can do something similar with
do_notify_resume()?Stupid question. To simplify, lets forget that
syscall_trace_enter()already returns the value. Can't we simplify the asm code if we do not export 2 functions, but makesyscall_trace_enter()return "boolslow_path_is_needed". So that "tracesys:" could do// pseudo codetracesys:
SAVE_REST FIXUP_TOP_OF_STACK call syscall_trace_enter if (!slow_path_is_needed) { addq REST_SKIP, %rsp jmp system_call_fastpath } ...Quoted 4 lines
?
Once again, I am just curious, it is not that I actually suggest to consider this option.
We could, but this would lose a decent amount of the speedup. I could try it and benchmark it, but I'm guessing that the save and restore is kind of expensive. This will make audit slower than it currently is, which may also annoy some people. (Not me.)
I'm also not convinced that it would be much simpler. My code is currently:
tracesys:
leaq -REST_SKIP(%rsp), %rdi
movq $AUDIT_ARCH_X86_64, %rsi
callsyscall_trace_enter_phase1
test %rax, %rax
jnztracesys_phase2/* if needed, run the slow path */
LOAD_ARGS0 /* else restore clobbered regs */
jmpsystem_call_fastpath/* and return to the fast path */
tracesys_phase2:
SAVE_REST
FIXUP_TOP_OF_STACK%rdi
movq %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
movq %rax,%rdx
callsyscall_trace_enter_phase2
LOAD_ARGSARGOFFSET, 1
RESTORE_REST... slow path here ...
It would end up looking more like (totally untested):
tracesys:
SAVE_REST
FIXUP_TOP_OF_STACK%rdi
mov %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
callsyscall_trace_enter
LOAD_ARGS
RESTORE_REST
test [whatever condition]
j[cond]system_call_fastpath... slow path here ...
So it's a bit simpler. Oddly, the ia32entry code doesn't have this multiple syscall path distinction.
Quoted 6 lines
SAVE_RESTis 6 movq instructions and a subq.FIXUP_TOP_OF_STACKis 7 movqs (and 8 if I ever get my way).RESTORE_TOP_OF_STACKis 4.RESTORE_RESTis 6 movqs and an adsq. So we're talking about avoiding 21 movqs, and addq, and a subq. That may be significant. (And I suspect that the difference is much larger on platforms like arm64, but that's a separate issue.)To put some more options on the table: there's an argument to be made that the whole fast-path/slow-path split isn't worth it. We could unconditionally set up a full frame for all syscalls. This means:
No
FIXUP_TOP_OF_STACK. Instead, thesystem_callentry sets up RSP, SS, CS, RCX, and EFLAGS right away. That's five stores for all syscalls instead of two loads and five stores for syscalls that need it. But it also gets rid ofRESTORE_TOP_OF_STACKcompletely.
Quoted 8 lines
No more bugs involving C code that assumes a full stack frame when no such frame exists inside syscall code.
We could possibly remove a whole bunch of duplicated code.
The upshot would be simpler code, faster slow-path syscalls, and slower fast-path syscalls (but probably not much slower). On the other hand, there's zero chance that this would be ready for 3.17.
Indeed.
If we ever take that direction (ie: remove that partial frame optimization), there is that common part that we can find in many archs when they return from syscalls, exceptions or interrupts which could be more or less consolidated as:
void sysret_check(struct pt_regs *regs)
{
while(test_thread_flag(TIF_ALLWORK_MASK)) {
int resched = need_resched();
local_irq_enable();
if (resched)
schedule();
else
do_notify_resume(regs);
local_irq_disable()
}
}But well, probably the syscall fastpath is still worth it.
Quoted 9 lines
I'd tend to advocate for keeping the approach in my patches for now. It's probably a smaller assembly diff than any of the other options -- the split between fast-path, slower fast-path, and slow-path already exists due to the audit crap. If we end up making more radical changes later, then at worst we end up reverting part of the change to
ptrace.c.--Andy
Quoted 146 lines · Frederic Weisbecker
Quoted 116 lines · Andy Lutomirski
Quoted 99 lines · Andy Lutomirski
Quoted 46 lines · Oleg Nesterov
Andy, to avoid the confusion: I am not trying to review this changes. As you probably know my understanding of asm code in
entry.Sis very limited.Just a couple of questions to ensure I understand this correctly.
Quoted 5 lines · Andy Lutomirski
This is both a cleanup and a speedup. It reduces overhead due to installing a trivial seccomp filter by 87%. The speedup comes from avoiding the full syscall tracing mechanism for filters that don't return
SECCOMP_RET_TRACE.And only after I look at 5/5 I _seem_ to actually understand where this speedup comes from.
So. Currently tracesys: path always lead to "iret" after syscall, with this change we can avoid it if
phase_1()returns zero, correct?And, this also removes the special TIF_SYSCALL_AUDIT-only case in
entry.S, cool.I am wondering if we can do something similar with
do_notify_resume()?Stupid question. To simplify, lets forget that
syscall_trace_enter()already returns the value. Can't we simplify the asm code if we do not export 2 functions, but makesyscall_trace_enter()return "boolslow_path_is_needed". So that "tracesys:" could do// pseudo codetracesys:
SAVE_REST FIXUP_TOP_OF_STACK call syscall_trace_enter if (!slow_path_is_needed) { addq REST_SKIP, %rsp jmp system_call_fastpath } ...Quoted 4 lines
?
Once again, I am just curious, it is not that I actually suggest to consider this option.
We could, but this would lose a decent amount of the speedup. I could try it and benchmark it, but I'm guessing that the save and restore is kind of expensive. This will make audit slower than it currently is, which may also annoy some people. (Not me.)
I'm also not convinced that it would be much simpler. My code is currently:
tracesys:
leaq -REST_SKIP(%rsp), %rdi
movq $AUDIT_ARCH_X86_64, %rsi
callsyscall_trace_enter_phase1
test %rax, %rax
jnztracesys_phase2/* if needed, run the slow path */
LOAD_ARGS0 /* else restore clobbered regs */
jmpsystem_call_fastpath/* and return to the fast path */
tracesys_phase2:
SAVE_REST
FIXUP_TOP_OF_STACK%rdi
movq %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
movq %rax,%rdx
callsyscall_trace_enter_phase2
LOAD_ARGSARGOFFSET, 1
RESTORE_REST... slow path here ...
It would end up looking more like (totally untested):
tracesys:
SAVE_REST
FIXUP_TOP_OF_STACK%rdi
mov %rsp, %rdi
movq $AUDIT_ARCH_X86_64, %rsi
callsyscall_trace_enter
LOAD_ARGS
RESTORE_REST
test [whatever condition]
j[cond]system_call_fastpath... slow path here ...
So it's a bit simpler. Oddly, the ia32entry code doesn't have this multiple syscall path distinction.
Quoted 6 lines
SAVE_RESTis 6 movq instructions and a subq.FIXUP_TOP_OF_STACKis 7 movqs (and 8 if I ever get my way).RESTORE_TOP_OF_STACKis 4.RESTORE_RESTis 6 movqs and an adsq. So we're talking about avoiding 21 movqs, and addq, and a subq. That may be significant. (And I suspect that the difference is much larger on platforms like arm64, but that's a separate issue.)To put some more options on the table: there's an argument to be made that the whole fast-path/slow-path split isn't worth it. We could unconditionally set up a full frame for all syscalls. This means:
No
FIXUP_TOP_OF_STACK. Instead, thesystem_callentry sets up RSP, SS, CS, RCX, and EFLAGS right away. That's five stores for all syscalls instead of two loads and five stores for syscalls that need it. But it also gets rid ofRESTORE_TOP_OF_STACKcompletely.Quoted 8 lines
No more bugs involving C code that assumes a full stack frame when no such frame exists inside syscall code.
We could possibly remove a whole bunch of duplicated code.
The upshot would be simpler code, faster slow-path syscalls, and slower fast-path syscalls (but probably not much slower). On the other hand, there's zero chance that this would be ready for 3.17.
Indeed.
If we ever take that direction (ie: remove that partial frame optimization), there is that common part that we can find in many archs when they return from syscalls, exceptions or interrupts which could be more or less consolidated as:
void
sysret_check(struct pt_regs *regs){ while(test_thread_flag(TIF_ALLWORK_MASK)) { int resched = need_resched(); local_irq_enable(); if (resched) schedule(); else do_notify_resume(regs); local_irq_disable() } }
Quoted 1 line
But well, probably the syscall fastpath is still worth it.
And yet x86_64 has this code implemented in assembly even in the slowpath. Go figure.
--Andy
H. Peter Anvin
reviewerQuoted 4 lines · Andy Lutomirski
And yet
x86_64has this code implemented in assembly even in the slowpath. Go figure.
There is way too much assembly in entry_64.S probably because things have been grafted on, ahem, "organically". It is darn nigh impossible to even remotely figure out what goes on in that file.
-hpa
Quoted 9 lines · H. Peter Anvin
Quoted 4 lines · Andy Lutomirski
And yet
x86_64has this code implemented in assembly even in the slowpath. Go figure.There is way too much assembly in
entry_64.Sprobably because things have been grafted on, ahem, "organically". It is darn nigh impossible to even remotely figure out what goes on in that file.
Always warn your family and give them an estimate return hour before opening that file.