您的位置:首页 > 运维架构 > Linux

Linux进程调度与切换

2016-04-15 22:33 871 查看
2016-04-15

张超《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

一、分析

进程调度的时机与进程切换

操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已。对于理解操作系统的工作机制,反而是进程的调度时机与进程的切换机制更为关键。

进程调度的时机:

schedule()是个内核函数,不是内核函数。所以用户态的进程不能直接调用,只能间接调用。内核线程是只有内核态没有用户态的特殊进程。

1.中断处理过程(包括时钟中断、I/O中断、系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule();

2.内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;

3.用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。

进程切换:

1.为了控制进程的执行,内核必须有能力挂起正在CPU上执行的进程,并恢复以前挂起的某个进程的执行,这叫做进程切换、任务切换、上下文切换;

2.挂起正在CPU上执行的进程,与中断时保存现场是不同的,中断前后是在同一个进程上下文中,只是由用户态转向内核态执行;

3.进程上下文包含了进程执行需要的所有信息

I 用户地址空间: 包括程序代码,数据,用户堆栈等 II 控制信息 :进程描述符,内核堆栈等

III 硬件上下文(注意中断也要保存硬件上下文只是保存的方法不同)

4.schedule()函数选择一个新的进程来运行,并调用context_switch进行上下文的切换,这个宏调用switch_to来进行关键上下文切换

schedule 在/linux-3.18.6/kernel/sched/core.c

1#ifndef _ASM_X86_SWITCH_TO_H
2#define _ASM_X86_SWITCH_TO_H
3
4struct task_struct; /* one of the stranger aspects of C forward declarations */
5__visible struct task_struct *__switch_to(struct task_struct *prev,
6                       struct task_struct *next);
7struct tss_struct;
8void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
9              struct tss_struct *tss);
10
11#ifdef CONFIG_X86_32
12
13#ifdef CONFIG_CC_STACKPROTECTOR
14#define __switch_canary                            \
15    "movl %P[task_canary](%[next]), %%ebx\n\t"            \
16    "movl %%ebx, "__percpu_arg([stack_canary])"\n\t"
17#define __switch_canary_oparam                        \
18    , [stack_canary] "=m" (stack_canary.canary)
19#define __switch_canary_iparam                        \
20    , [task_canary] "i" (offsetof(struct task_struct, stack_canary))
21#else    /* CC_STACKPROTECTOR */
22#define __switch_canary
23#define __switch_canary_oparam
24#define __switch_canary_iparam
25#endif    /* CC_STACKPROTECTOR */
26
27/*
28 * Saving eflags is important. It switches not only IOPL between tasks,
29 * it also protects other tasks from NT leaking through sysenter etc.
30 */
31#define switch_to(prev, next, last)                    \
32do {                                    \
33    /*                                \
34     * Context-switching clobbers all registers, so we clobber    \
35     * them explicitly, via unused output variables.        \
36     * (EAX and EBP is not listed because EBP is saved/restored    \
37     * explicitly for wchan access and EAX is the return value of    \
38     * __switch_to())                        \
39     */                                \
40    unsigned long ebx, ecx, edx, esi, edi;                \
41                                    \
42    asm volatile("pushfl\n\t"        /* save    flags */    \
43             "pushl %%ebp\n\t"        /* save    EBP   */    \
44             "movl %%esp,%[prev_sp]\n\t"    /* save    ESP   */ \
45             "movl %[next_sp],%%esp\n\t"    /* restore ESP   */ \
46             "movl $1f,%[prev_ip]\n\t"    /* save    EIP   */    \
47             "pushl %[next_ip]\n\t"    /* restore EIP   */    \
48             __switch_canary                    \
49             "jmp __switch_to\n"    /* regparm call  */    \
50             "1:\t"                        \
51             "popl %%ebp\n\t"        /* restore EBP   */    \
52             "popfl\n"            /* restore flags */    \
53                                    \
54             /* output parameters */                \
55             : [prev_sp] "=m" (prev->thread.sp),        \
56               [prev_ip] "=m" (prev->thread.ip),        \
57               "=a" (last),                    \
58                                    \
59               /* clobbered output registers: */        \
60               "=b" (ebx), "=c" (ecx), "=d" (edx),        \
61               "=S" (esi), "=D" (edi)                \
62                                           \
63               __switch_canary_oparam                \
64                                    \
65               /* input parameters: */                \
66             : [next_sp]  "m" (next->thread.sp),        \
67               [next_ip]  "m" (next->thread.ip),        \
68                                           \
69               /* regparm parameters for __switch_to(): */    \
70               [prev]     "a" (prev),                \
71               [next]     "d" (next)                \
72                                    \
73               __switch_canary_iparam                \
74                                    \
75             : /* reloaded segment registers */            \
76            "memory");                    \
77} while (0)
78
79#else /* CONFIG_X86_32 */
80
81/* frame pointer must be last for get_wchan */
82#define SAVE_CONTEXT    "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
83#define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
84
85#define __EXTRA_CLOBBER  \
86    , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
87      "r12", "r13", "r14", "r15"
88
89#ifdef CONFIG_CC_STACKPROTECTOR
90#define __switch_canary                              \
91    "movq %P[task_canary](%%rsi),%%r8\n\t"                  \
92    "movq %%r8,"__percpu_arg([gs_canary])"\n\t"
93#define __switch_canary_oparam                          \
94    , [gs_canary] "=m" (irq_stack_union.stack_canary)
95#define __switch_canary_iparam                          \
96    , [task_canary] "i" (offsetof(struct task_struct, stack_canary))
97#else    /* CC_STACKPROTECTOR */
98#define __switch_canary
99#define __switch_canary_oparam
100#define __switch_canary_iparam
101#endif    /* CC_STACKPROTECTOR */
102
103/* Save restore flags to clear handle leaking NT */
104#define switch_to(prev, next, last) \
105    asm volatile(SAVE_CONTEXT                      \
106         "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */      \
107         "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */      \
108         "call __switch_to\n\t"                      \
109         "movq "__percpu_arg([current_task])",%%rsi\n\t"          \
110         __switch_canary                          \
111         "movq %P[thread_info](%%rsi),%%r8\n\t"              \
112         "movq %%rax,%%rdi\n\t"                       \
113         "testl  %[_tif_fork],%P[ti_flags](%%r8)\n\t"          \
114         "jnz   ret_from_fork\n\t"                      \
115         RESTORE_CONTEXT                          \
116         : "=a" (last)                            \
117           __switch_canary_oparam                      \
118         : [next] "S" (next), [prev] "D" (prev),              \
119           [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
120           [ti_flags] "i" (offsetof(struct thread_info, flags)),      \
121           [_tif_fork] "i" (_TIF_FORK),                    \
122           [thread_info] "i" (offsetof(struct task_struct, stack)),   \
123           [current_task] "m" (current_task)              \
124           __switch_canary_iparam                      \
125         : "memory", "cc" __EXTRA_CLOBBER)
126
127#endif /* CONFIG_X86_32 */
128
129#endif /* _ASM_X86_SWITCH_TO_H */
130


switch_to
完成进程切换

二、分析进程切换:我们用switch_to中的部分代码分析

27/*
28 * Saving eflags is important. It switches not only IOPL between tasks,
29 * it also protects other tasks from NT leaking through sysenter etc.
30 */
31#define switch_to(prev, next, last)                    \
32do {                                    \
33    /*                                \
34     * Context-switching clobbers all registers, so we clobber    \
35     * them explicitly, via unused output variables.        \
36     * (EAX and EBP is not listed because EBP is saved/restored    \
37     * explicitly for wchan access and EAX is the return value of    \
38     * __switch_to())                        \
39     */                                \
40    unsigned long ebx, ecx, edx, esi, edi;                \
41                                    \
42    asm volatile("pushfl\n\t"        /* save    flags */    \
43             "pushl %%ebp\n\t"        /* save    EBP   */    \
44             "movl %%esp,%[prev_sp]\n\t"    /* save    ESP   */ \
45             "movl %[next_sp],%%esp\n\t"    /* restore ESP   */ \
46             "movl $1f,%[prev_ip]\n\t"    /* save    EIP   */    \
47             "pushl %[next_ip]\n\t"    /* restore EIP   */    \
48             __switch_canary                    \
49             "jmp __switch_to\n"    /* regparm call  */    \
50             "1:\t"                        \
51             "popl %%ebp\n\t"        /* restore EBP   */    \
52             "popfl\n"            /* restore flags */    \
53                                    \
54             /* output parameters */                \
55             : [prev_sp] "=m" (prev->thread.sp),        \
56               [prev_ip] "=m" (prev->thread.ip),        \
57               "=a" (last),                    \
58                                    \
59               /* clobbered output registers: */        \
60               "=b" (ebx), "=c" (ecx), "=d" (edx),        \
61               "=S" (esi), "=D" (edi)                \
62                                           \
63               __switch_canary_oparam                \
64                                    \
65               /* input parameters: */                \
66             : [next_sp]  "m" (next->thread.sp),        \
67               [next_ip]  "m" (next->thread.ip),        \
68                                           \
69               /* regparm parameters for __switch_to(): */    \
70               [prev]     "a" (prev),                \
71               [next]     "d" (next)                \
72                                    \
73               __switch_canary_iparam                \
74                                    \
75             : /* reloaded segment registers */            \
76            "memory");                    \
77} while (0)


利用了prev和next两个参数:prev指向当前进程,当前进程用X表示。next指向被调度的进程,即下一个进程,用Y表示。至于如何实现调度,看pick_next_task。

看第42行:把flags压入到当前进程X的栈里面,保存flags。

看第43行:把当前的ebp压入当前进程X的栈里,保存ebp。

看第44行:把当前的esp保存到当前进程X的thread.sp里面。其中[prev_sp]是个标识,他在第55行,代替的是prev->thread.sp。

看第45行:把下一个进行Y的thread.sp赋值给esp,这一步实现把本来指向X的栈指针esp,现在指向了Y。其中[next_sp]如上所述,在第66行。

看第46行:把50行的位置存到X进程的thread_ip里面,保存eip。下一次可以从50行开始执行。其中[prev_ip]如上所述,在第56行。

看第47行:把下一个进程Y的threat.ip压入Y进程的栈里面。其中[next_ip]如上所示,在第67行。

看第49行:跳转到__swap_to

看第51行:Y进程里面出栈操作,放到ebp里面。

看第52行:把Y进程里面的出栈,弹出flags

第51,52行正好和第42,43行操作互逆。

三、实验:用gdb跟踪分析一个schedule()函数



四、Linux系统的一般执行过程

最一般情况:正在运行的用户态进程X切换到运行用户态进程Y的过程

1.正在运行的用户态进程X

2.发生中断——save cs:eip/esp/eflags(current) to kernel stack,then load cs:eip(entry of a specific ISR) and ss:esp(point to kernel stack).

3. SAVE_ALL //保存现场

4. 中断处理过程中或中断返回前调用了schedule(),其中的switch_to做了关键的进程上下文切换

5. 标号1之后开始运行用户态进程Y(这里Y曾经通过以上步骤被切换出去过因此可以从标号1继续执行)

6. restore_all //恢复现场

7. iret - pop cs:eip/ss:esp/eflags from kernel stack

8. 继续运行用户态进程Y

几种特殊的情况:

1. 通过中断处理过程中的调度时机,用户态进程与内核线程之间互相切换和内核线程之间互相切换,与最一般的情况非常类似,只是内核线程运行过程中发生中断没有进程用户态和内核态的转换;

2. 内核线程主动调用schedule(),只有进程上下文的切换,没有发生中断上下文的切换,与最一般的情况略简略;

3. 创建子进程的系统调用在子进程中的执行起点及返回用户态,如fork;

4. 加载一个新的可执行程序后返回到用户态的情况,如execve;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: