Skip to content

Commit 0100301

Browse files
Brian Gerstingomolnar
authored andcommitted
sched/x86: Rewrite the switch_to() code
Move the low-level context switch code to an out-of-line asm stub instead of using complex inline asm. This allows constructing a new stack frame for the child process to make it seamlessly flow to ret_from_fork without an extra test and branch in __switch_to(). It also improves code generation for __schedule() by using the C calling convention instead of clobbering all registers. Signed-off-by: Brian Gerst <brgerst@gmail.com> Reviewed-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1471106302-10159-5-git-send-email-brgerst@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 7b32aea commit 0100301

11 files changed

Lines changed: 125 additions & 132 deletions

File tree

‎arch/x86/entry/entry_32.S‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,43 @@
204204
POP_GS_EX
205205
.endm
206206

207+
/*
208+
* %eax: prev task
209+
* %edx: next task
210+
*/
211+
ENTRY(__switch_to_asm)
212+
/*
213+
* Save callee-saved registers
214+
* This must match the order in struct inactive_task_frame
215+
*/
216+
pushl %ebp
217+
pushl %ebx
218+
pushl %edi
219+
pushl %esi
220+
221+
/* switch stack */
222+
movl %esp, TASK_threadsp(%eax)
223+
movl TASK_threadsp(%edx), %esp
224+
225+
#ifdef CONFIG_CC_STACKPROTECTOR
226+
movl TASK_stack_canary(%edx), %ebx
227+
movl %ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset
228+
#endif
229+
230+
/* restore callee-saved registers */
231+
popl %esi
232+
popl %edi
233+
popl %ebx
234+
popl %ebp
235+
236+
jmp __switch_to
237+
END(__switch_to_asm)
238+
239+
/*
240+
* A newly forked process directly context switches into this address.
241+
*
242+
* eax: prev task we switched from
243+
*/
207244
ENTRY(ret_from_fork)
208245
pushl %eax
209246
call schedule_tail

‎arch/x86/entry/entry_64.S‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,49 @@ END(ptregs_\func)
367367
#define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
368368
#include <asm/syscalls_64.h>
369369

370+
/*
371+
* %rdi: prev task
372+
* %rsi: next task
373+
*/
374+
ENTRY(__switch_to_asm)
375+
/*
376+
* Save callee-saved registers
377+
* This must match the order in inactive_task_frame
378+
*/
379+
pushq %rbp
380+
pushq %rbx
381+
pushq %r12
382+
pushq %r13
383+
pushq %r14
384+
pushq %r15
385+
386+
/* switch stack */
387+
movq %rsp, TASK_threadsp(%rdi)
388+
movq TASK_threadsp(%rsi), %rsp
389+
390+
#ifdef CONFIG_CC_STACKPROTECTOR
391+
movq TASK_stack_canary(%rsi), %rbx
392+
movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
393+
#endif
394+
395+
/* restore callee-saved registers */
396+
popq %r15
397+
popq %r14
398+
popq %r13
399+
popq %r12
400+
popq %rbx
401+
popq %rbp
402+
403+
jmp __switch_to
404+
END(__switch_to_asm)
405+
370406
/*
371407
* A newly forked process directly context switches into this address.
372408
*
373-
* rdi: prev task we switched from
409+
* rax: prev task we switched from
374410
*/
375411
ENTRY(ret_from_fork)
376-
LOCK ; btr $TIF_FORK, TI_flags(%r8)
377-
412+
movq %rax, %rdi
378413
call schedule_tail /* rdi: 'prev' task parameter */
379414

380415
testb $3, CS(%rsp) /* from kernel_thread? */

‎arch/x86/include/asm/processor.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,6 @@ struct thread_struct {
389389
unsigned short fsindex;
390390
unsigned short gsindex;
391391
#endif
392-
#ifdef CONFIG_X86_32
393-
unsigned long ip;
394-
#endif
395392
#ifdef CONFIG_X86_64
396393
unsigned long fsbase;
397394
unsigned long gsbase;

‎arch/x86/include/asm/switch_to.h‎

Lines changed: 21 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
#define _ASM_X86_SWITCH_TO_H
33

44
struct task_struct; /* one of the stranger aspects of C forward declarations */
5+
6+
struct task_struct *__switch_to_asm(struct task_struct *prev,
7+
struct task_struct *next);
8+
59
__visible struct task_struct *__switch_to(struct task_struct *prev,
6-
struct task_struct *next);
10+
struct task_struct *next);
711
struct tss_struct;
812
void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
913
struct tss_struct *tss);
@@ -32,131 +36,30 @@ static inline void prepare_switch_to(struct task_struct *prev,
3236

3337
/* data that is pointed to by thread.sp */
3438
struct inactive_task_frame {
39+
#ifdef CONFIG_X86_64
40+
unsigned long r15;
41+
unsigned long r14;
42+
unsigned long r13;
43+
unsigned long r12;
44+
#else
45+
unsigned long si;
46+
unsigned long di;
47+
#endif
48+
unsigned long bx;
3549
unsigned long bp;
50+
unsigned long ret_addr;
3651
};
3752

38-
#ifdef CONFIG_X86_32
39-
40-
#ifdef CONFIG_CC_STACKPROTECTOR
41-
#define __switch_canary \
42-
"movl %P[task_canary](%[next]), %%ebx\n\t" \
43-
"movl %%ebx, "__percpu_arg([stack_canary])"\n\t"
44-
#define __switch_canary_oparam \
45-
, [stack_canary] "=m" (stack_canary.canary)
46-
#define __switch_canary_iparam \
47-
, [task_canary] "i" (offsetof(struct task_struct, stack_canary))
48-
#else /* CC_STACKPROTECTOR */
49-
#define __switch_canary
50-
#define __switch_canary_oparam
51-
#define __switch_canary_iparam
52-
#endif /* CC_STACKPROTECTOR */
53+
struct fork_frame {
54+
struct inactive_task_frame frame;
55+
struct pt_regs regs;
56+
};
5357

54-
/*
55-
* Saving eflags is important. It switches not only IOPL between tasks,
56-
* it also protects other tasks from NT leaking through sysenter etc.
57-
*/
5858
#define switch_to(prev, next, last) \
5959
do { \
60-
/* \
61-
* Context-switching clobbers all registers, so we clobber \
62-
* them explicitly, via unused output variables. \
63-
* (EAX and EBP is not listed because EBP is saved/restored \
64-
* explicitly for wchan access and EAX is the return value of \
65-
* __switch_to()) \
66-
*/ \
67-
unsigned long ebx, ecx, edx, esi, edi; \
68-
\
6960
prepare_switch_to(prev, next); \
7061
\
71-
asm volatile("pushl %%ebp\n\t" /* save EBP */ \
72-
"movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
73-
"movl %[next_sp],%%esp\n\t" /* restore ESP */ \
74-
"movl $1f,%[prev_ip]\n\t" /* save EIP */ \
75-
"pushl %[next_ip]\n\t" /* restore EIP */ \
76-
__switch_canary \
77-
"jmp __switch_to\n" /* regparm call */ \
78-
"1:\t" \
79-
"popl %%ebp\n\t" /* restore EBP */ \
80-
\
81-
/* output parameters */ \
82-
: [prev_sp] "=m" (prev->thread.sp), \
83-
[prev_ip] "=m" (prev->thread.ip), \
84-
"=a" (last), \
85-
\
86-
/* clobbered output registers: */ \
87-
"=b" (ebx), "=c" (ecx), "=d" (edx), \
88-
"=S" (esi), "=D" (edi) \
89-
\
90-
__switch_canary_oparam \
91-
\
92-
/* input parameters: */ \
93-
: [next_sp] "m" (next->thread.sp), \
94-
[next_ip] "m" (next->thread.ip), \
95-
\
96-
/* regparm parameters for __switch_to(): */ \
97-
[prev] "a" (prev), \
98-
[next] "d" (next) \
99-
\
100-
__switch_canary_iparam \
101-
\
102-
: /* reloaded segment registers */ \
103-
"memory"); \
62+
((last) = __switch_to_asm((prev), (next))); \
10463
} while (0)
10564

106-
#else /* CONFIG_X86_32 */
107-
108-
/* frame pointer must be last for get_wchan */
109-
#define SAVE_CONTEXT "pushq %%rbp ; movq %%rsi,%%rbp\n\t"
110-
#define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp\t"
111-
112-
#define __EXTRA_CLOBBER \
113-
, "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
114-
"r12", "r13", "r14", "r15", "flags"
115-
116-
#ifdef CONFIG_CC_STACKPROTECTOR
117-
#define __switch_canary \
118-
"movq %P[task_canary](%%rsi),%%r8\n\t" \
119-
"movq %%r8,"__percpu_arg([gs_canary])"\n\t"
120-
#define __switch_canary_oparam \
121-
, [gs_canary] "=m" (irq_stack_union.stack_canary)
122-
#define __switch_canary_iparam \
123-
, [task_canary] "i" (offsetof(struct task_struct, stack_canary))
124-
#else /* CC_STACKPROTECTOR */
125-
#define __switch_canary
126-
#define __switch_canary_oparam
127-
#define __switch_canary_iparam
128-
#endif /* CC_STACKPROTECTOR */
129-
130-
/*
131-
* There is no need to save or restore flags, because flags are always
132-
* clean in kernel mode, with the possible exception of IOPL. Kernel IOPL
133-
* has no effect.
134-
*/
135-
#define switch_to(prev, next, last) \
136-
prepare_switch_to(prev, next); \
137-
\
138-
asm volatile(SAVE_CONTEXT \
139-
"movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
140-
"movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
141-
"call __switch_to\n\t" \
142-
"movq "__percpu_arg([current_task])",%%rsi\n\t" \
143-
__switch_canary \
144-
"movq %P[thread_info](%%rsi),%%r8\n\t" \
145-
"movq %%rax,%%rdi\n\t" \
146-
"testl %[_tif_fork],%P[ti_flags](%%r8)\n\t" \
147-
"jnz ret_from_fork\n\t" \
148-
RESTORE_CONTEXT \
149-
: "=a" (last) \
150-
__switch_canary_oparam \
151-
: [next] "S" (next), [prev] "D" (prev), \
152-
[threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
153-
[ti_flags] "i" (offsetof(struct thread_info, flags)), \
154-
[_tif_fork] "i" (_TIF_FORK), \
155-
[thread_info] "i" (offsetof(struct task_struct, stack)), \
156-
[current_task] "m" (current_task) \
157-
__switch_canary_iparam \
158-
: "memory", "cc" __EXTRA_CLOBBER)
159-
160-
#endif /* CONFIG_X86_32 */
161-
16265
#endif /* _ASM_X86_SWITCH_TO_H */

‎arch/x86/include/asm/thread_info.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ struct thread_info {
9595
#define TIF_UPROBE 12 /* breakpointed or singlestepping */
9696
#define TIF_NOTSC 16 /* TSC is not accessible in userland */
9797
#define TIF_IA32 17 /* IA32 compatibility process */
98-
#define TIF_FORK 18 /* ret_from_fork */
9998
#define TIF_NOHZ 19 /* in adaptive nohz mode */
10099
#define TIF_MEMDIE 20 /* is terminating due to OOM killer */
101100
#define TIF_POLLING_NRFLAG 21 /* idle is polling for TIF_NEED_RESCHED */
@@ -119,7 +118,6 @@ struct thread_info {
119118
#define _TIF_UPROBE (1 << TIF_UPROBE)
120119
#define _TIF_NOTSC (1 << TIF_NOTSC)
121120
#define _TIF_IA32 (1 << TIF_IA32)
122-
#define _TIF_FORK (1 << TIF_FORK)
123121
#define _TIF_NOHZ (1 << TIF_NOHZ)
124122
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
125123
#define _TIF_IO_BITMAP (1 << TIF_IO_BITMAP)

‎arch/x86/kernel/asm-offsets.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#endif
2929

3030
void common(void) {
31+
BLANK();
32+
OFFSET(TASK_threadsp, task_struct, thread.sp);
33+
#ifdef CONFIG_CC_STACKPROTECTOR
34+
OFFSET(TASK_stack_canary, task_struct, stack_canary);
35+
#endif
36+
3137
BLANK();
3238
OFFSET(TI_flags, thread_info, flags);
3339
OFFSET(TI_status, thread_info, status);

‎arch/x86/kernel/asm-offsets_32.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ void foo(void)
5757
/* Size of SYSENTER_stack */
5858
DEFINE(SIZEOF_SYSENTER_stack, sizeof(((struct tss_struct *)0)->SYSENTER_stack));
5959

60+
#ifdef CONFIG_CC_STACKPROTECTOR
61+
BLANK();
62+
OFFSET(stack_canary_offset, stack_canary, canary);
63+
#endif
64+
6065
#if defined(CONFIG_LGUEST) || defined(CONFIG_LGUEST_GUEST) || defined(CONFIG_LGUEST_MODULE)
6166
BLANK();
6267
OFFSET(LGUEST_DATA_irq_enabled, lguest_data, irq_enabled);

‎arch/x86/kernel/asm-offsets_64.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ int main(void)
5656
OFFSET(TSS_sp0, tss_struct, x86_tss.sp0);
5757
BLANK();
5858

59+
#ifdef CONFIG_CC_STACKPROTECTOR
60+
DEFINE(stack_canary_offset, offsetof(union irq_stack_union, stack_canary));
61+
BLANK();
62+
#endif
63+
5964
DEFINE(__NR_syscall_max, sizeof(syscalls_64) - 1);
6065
DEFINE(NR_syscalls, sizeof(syscalls_64));
6166

‎arch/x86/kernel/process_32.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,20 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
133133
unsigned long arg, struct task_struct *p, unsigned long tls)
134134
{
135135
struct pt_regs *childregs = task_pt_regs(p);
136+
struct fork_frame *fork_frame = container_of(childregs, struct fork_frame, regs);
137+
struct inactive_task_frame *frame = &fork_frame->frame;
136138
struct task_struct *tsk;
137139
int err;
138140

139-
p->thread.sp = (unsigned long) childregs;
141+
frame->bp = 0;
142+
p->thread.sp = (unsigned long) fork_frame;
140143
p->thread.sp0 = (unsigned long) (childregs+1);
141144
memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
142145

143146
if (unlikely(p->flags & PF_KTHREAD)) {
144147
/* kernel thread */
145148
memset(childregs, 0, sizeof(struct pt_regs));
146-
p->thread.ip = (unsigned long) ret_from_kernel_thread;
149+
frame->ret_addr = (unsigned long) ret_from_kernel_thread;
147150
task_user_gs(p) = __KERNEL_STACK_CANARY;
148151
childregs->ds = __USER_DS;
149152
childregs->es = __USER_DS;
@@ -161,7 +164,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
161164
if (sp)
162165
childregs->sp = sp;
163166

164-
p->thread.ip = (unsigned long) ret_from_fork;
167+
frame->ret_addr = (unsigned long) ret_from_fork;
165168
task_user_gs(p) = get_user_gs(current_pt_regs());
166169

167170
p->thread.io_bitmap_ptr = NULL;

‎arch/x86/kernel/process_64.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,17 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
141141
{
142142
int err;
143143
struct pt_regs *childregs;
144+
struct fork_frame *fork_frame;
145+
struct inactive_task_frame *frame;
144146
struct task_struct *me = current;
145147

146148
p->thread.sp0 = (unsigned long)task_stack_page(p) + THREAD_SIZE;
147149
childregs = task_pt_regs(p);
148-
p->thread.sp = (unsigned long) childregs;
149-
set_tsk_thread_flag(p, TIF_FORK);
150+
fork_frame = container_of(childregs, struct fork_frame, regs);
151+
frame = &fork_frame->frame;
152+
frame->bp = 0;
153+
frame->ret_addr = (unsigned long) ret_from_fork;
154+
p->thread.sp = (unsigned long) fork_frame;
150155
p->thread.io_bitmap_ptr = NULL;
151156

152157
savesegment(gs, p->thread.gsindex);

0 commit comments

Comments
 (0)