Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/asm/entry/64: Save R11 into pt_regs->flags on SYSCALL64 fastpath

Before this patch, R11 was saved in pt_regs->r11.

Which looks natural, but requires messy shuffling to/from iret
frame whenever ptrace or e.g. sys_iopl() wants to modify flags -
because that's how this register is used by SYSCALL/SYSRET.

This patch saves R11 in pt_regs->flags, and uses that value for
the SYSRET64 instruction. Shuffling is eliminated.

FIXUP/RESTORE_TOP_OF_STACK are simplified.

stub_iopl is no longer needed: pt_regs->flags needs no fixing up.

Testing shows that syscall fast path is ~54.3 ns before
and after the patch (on 2.7 GHz Sandy Bridge CPU).

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Will Drewry <wad@chromium.org>
Link: http://lkml.kernel.org/r/1425926364-9526-2-git-send-email-dvlasenk@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Denys Vlasenko and committed by
Ingo Molnar
29722cd4 3e1aa7cb

+25 -19
+14 -6
arch/x86/include/asm/calling.h
··· 95 95 CFI_ADJUST_CFA_OFFSET 15*8+\addskip 96 96 .endm 97 97 98 - .macro SAVE_C_REGS_HELPER offset=0 rax=1 rcx=1 r8plus=1 99 - .if \r8plus 98 + .macro SAVE_C_REGS_HELPER offset=0 rax=1 rcx=1 r8910=1 r11=1 99 + .if \r11 100 100 movq_cfi r11, 6*8+\offset 101 + .endif 102 + .if \r8910 101 103 movq_cfi r10, 7*8+\offset 102 104 movq_cfi r9, 8*8+\offset 103 105 movq_cfi r8, 9*8+\offset ··· 115 113 movq_cfi rdi, 14*8+\offset 116 114 .endm 117 115 .macro SAVE_C_REGS offset=0 118 - SAVE_C_REGS_HELPER \offset, 1, 1, 1 116 + SAVE_C_REGS_HELPER \offset, 1, 1, 1, 1 119 117 .endm 120 118 .macro SAVE_C_REGS_EXCEPT_RAX_RCX offset=0 121 - SAVE_C_REGS_HELPER \offset, 0, 0, 1 119 + SAVE_C_REGS_HELPER \offset, 0, 0, 1, 1 122 120 .endm 123 121 .macro SAVE_C_REGS_EXCEPT_R891011 124 - SAVE_C_REGS_HELPER 0, 1, 1, 0 122 + SAVE_C_REGS_HELPER 0, 1, 1, 0, 0 125 123 .endm 126 124 .macro SAVE_C_REGS_EXCEPT_RCX_R891011 127 - SAVE_C_REGS_HELPER 0, 1, 0, 0 125 + SAVE_C_REGS_HELPER 0, 1, 0, 0, 0 126 + .endm 127 + .macro SAVE_C_REGS_EXCEPT_RAX_RCX_R11 128 + SAVE_C_REGS_HELPER 0, 0, 0, 1, 0 128 129 .endm 129 130 130 131 .macro SAVE_EXTRA_REGS offset=0 ··· 183 178 .endm 184 179 .macro RESTORE_C_REGS_EXCEPT_R11 185 180 RESTORE_C_REGS_HELPER 1,1,0,1,1 181 + .endm 182 + .macro RESTORE_C_REGS_EXCEPT_RCX_R11 183 + RESTORE_C_REGS_HELPER 1,0,0,1,1 186 184 .endm 187 185 .macro RESTORE_RSI_RDI 188 186 RESTORE_C_REGS_HELPER 0,0,0,0,0
+11 -13
arch/x86/kernel/entry_64.S
··· 121 121 #endif 122 122 123 123 /* 124 - * C code is not supposed to know about undefined top of stack. Every time 125 - * a C function with an pt_regs argument is called from the SYSCALL based 126 - * fast path FIXUP_TOP_OF_STACK is needed. 124 + * C code is not supposed to know that the iret frame is not populated. 125 + * Every time a C function with an pt_regs argument is called from 126 + * the SYSCALL based fast path FIXUP_TOP_OF_STACK is needed. 127 127 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs 128 128 * manipulation. 129 129 */ 130 - 131 - /* %rsp:at FRAMEEND */ 132 130 .macro FIXUP_TOP_OF_STACK tmp offset=0 133 131 movq PER_CPU_VAR(old_rsp),\tmp 134 132 movq \tmp,RSP+\offset(%rsp) ··· 134 136 movq $__USER_CS,CS+\offset(%rsp) 135 137 movq RIP+\offset(%rsp),\tmp /* get rip */ 136 138 movq \tmp,RCX+\offset(%rsp) /* copy it to rcx as sysret would do */ 137 - movq R11+\offset(%rsp),\tmp /* get eflags */ 138 - movq \tmp,EFLAGS+\offset(%rsp) 139 + movq EFLAGS+\offset(%rsp),\tmp /* ditto for rflags->r11 */ 140 + movq \tmp,R11+\offset(%rsp) 139 141 .endm 140 142 141 143 .macro RESTORE_TOP_OF_STACK tmp offset=0 142 144 movq RSP+\offset(%rsp),\tmp 143 145 movq \tmp,PER_CPU_VAR(old_rsp) 144 - movq EFLAGS+\offset(%rsp),\tmp 145 - movq \tmp,R11+\offset(%rsp) 146 146 .endm 147 147 148 148 /* ··· 253 257 */ 254 258 ENABLE_INTERRUPTS(CLBR_NONE) 255 259 ALLOC_PT_GPREGS_ON_STACK 8 /* +8: space for orig_ax */ 256 - SAVE_C_REGS_EXCEPT_RAX_RCX 260 + SAVE_C_REGS_EXCEPT_RAX_RCX_R11 257 261 movq $-ENOSYS,RAX(%rsp) 258 262 movq_cfi rax,ORIG_RAX 263 + movq %r11,EFLAGS(%rsp) 259 264 movq %rcx,RIP(%rsp) 260 265 CFI_REL_OFFSET rip,RIP 261 266 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP) ··· 274 277 movq %rax,RAX(%rsp) 275 278 /* 276 279 * Syscall return path ending with SYSRET (fast path) 277 - * Has incomplete stack frame and undefined top of stack. 280 + * Has incompletely filled pt_regs, iret frame is also incomplete. 278 281 */ 279 282 ret_from_sys_call: 280 283 testl $_TIF_ALLWORK_MASK,TI_flags+THREAD_INFO(%rsp,RIP) ··· 288 291 * sysretq will re-enable interrupts: 289 292 */ 290 293 TRACE_IRQS_ON 291 - RESTORE_C_REGS_EXCEPT_RCX 292 - movq RIP(%rsp),%rcx 294 + RESTORE_C_REGS_EXCEPT_RCX_R11 295 + movq RIP(%rsp),%rcx 293 296 CFI_REGISTER rip,rcx 297 + movq EFLAGS(%rsp),%r11 294 298 /*CFI_REGISTER rflags,r11*/ 295 299 movq PER_CPU_VAR(old_rsp), %rsp 296 300 /*