linux 内核学习之八 进程调度过程分析

时间:2021-06-26 15:46:53

一  关于进程的补充

进程调度的时机

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

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

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

进程的切换

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

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

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

          用户地址空间: 包括程序代码,数据,用户堆栈等

          控制信息 :进程描述符,内核堆栈等

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

二  代码分析

    (不同于上几次的模式,即先用gdb跟踪分析,再分析代码)linux中通过schedule()函数来实现进程的调度,涉及到知识很多,如,调度的策略,时间片的分配,进程上下文的切换等,这里只是粗浅的分析schedule等相关函数

   先给出代码:schedule()

asmlinkage __visible void __sched schedule(void)
{
struct task_struct *tsk = current;

sched_submit_work(tsk);
__schedule();
}

  其中 sched_submit_work()是为了避免进程睡眠时发生死锁。

static inline void sched_submit_work(struct task_struct *tsk)
{
if (!tsk->state || tsk_is_pi_blocked(tsk))
return;
/*
* If we are going to sleep and we have plugged IO queued,
* make sure to submit it to avoid deadlocks.
*/
if (blk_needs_flush_plug(tsk))
blk_schedule_flush_plug(tsk);
}

  __schedule() 是进程调度的函数(里面还有嵌套~)

 1 static void __sched __schedule(void)
2 {
3 struct task_struct *prev, *next;
4 unsigned long *switch_count;
5 struct rq *rq;
6 int cpu;
7
8 need_resched:
9 preempt_disable();
10 cpu = smp_processor_id();
11 rq = cpu_rq(cpu);
12 rcu_note_context_switch(cpu);
13 prev = rq->curr;
14
15 schedule_debug(prev);
16
17 if (sched_feat(HRTICK))
18 hrtick_clear(rq);
19
20 /*
21 * Make sure that signal_pending_state()->signal_pending() below
22 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
23 * done by the caller to avoid the race with signal_wake_up().
24 */
25 smp_mb__before_spinlock();
26 raw_spin_lock_irq(&rq->lock);
27
28 switch_count = &prev->nivcsw;
29 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
30 if (unlikely(signal_pending_state(prev->state, prev))) {
31 prev->state = TASK_RUNNING;
32 } else {
33 deactivate_task(rq, prev, DEQUEUE_SLEEP);
34 prev->on_rq = 0;
35
36 /*
37 * If a worker went to sleep, notify and ask workqueue
38 * whether it wants to wake up a task to maintain
39 * concurrency.
40 */
41 if (prev->flags & PF_WQ_WORKER) {
42 struct task_struct *to_wakeup;
43
44 to_wakeup = wq_worker_sleeping(prev, cpu);
45 if (to_wakeup)
46 try_to_wake_up_local(to_wakeup);
47 }
48 }
49 switch_count = &prev->nvcsw;
50 }
51
52 if (task_on_rq_queued(prev) || rq->skip_clock_update < 0)
53 update_rq_clock(rq);
54
55 next = pick_next_task(rq, prev);
56 clear_tsk_need_resched(prev);
57 clear_preempt_need_resched();
58 rq->skip_clock_update = 0;
59
60 if (likely(prev != next)) {
61 rq->nr_switches++;
62 rq->curr = next;
63 ++*switch_count;
64
65 context_switch(rq, prev, next); /* unlocks the rq */
66 /*
67 * The context switch have flipped the stack from under us
68 * and restored the local variables which were saved when
69 * this task called schedule() in the past. prev == current
70 * is still correct, but it can be moved to another cpu/rq.
71 */
72 cpu = smp_processor_id();
73 rq = cpu_rq(cpu);
74 } else
75 raw_spin_unlock_irq(&rq->lock);
76
77 post_schedule(rq);
78
79 sched_preempt_enable_no_resched();
80 if (need_resched())
81 goto need_resched;
82 }

上面的红色给出了重点标记,context_switch(),进行了进程上下文切换。

linux 内核学习之八   进程调度过程分析linux 内核学习之八   进程调度过程分析
 1 static inline void
2 context_switch(struct rq *rq, struct task_struct *prev,
3 struct task_struct *next)
4 {
5 struct mm_struct *mm, *oldmm;
6
7 prepare_task_switch(rq, prev, next);
8
9 mm = next->mm;
10 oldmm = prev->active_mm;
11 /*
12 * For paravirt, this is coupled with an exit in switch_to to
13 * combine the page table reload and the switch backend into
14 * one hypercall.
15 */
16 arch_start_context_switch(prev);
17
18 if (!mm) {
19 next->active_mm = oldmm;
20 atomic_inc(&oldmm->mm_count);
21 enter_lazy_tlb(oldmm, next);
22 } else
23 switch_mm(oldmm, mm, next);
24
25 if (!prev->mm) {
26 prev->active_mm = NULL;
27 rq->prev_mm = oldmm;
28 }
29 /*
30 * Since the runqueue lock will be released by the next
31 * task (which is an invalid locking op but in the case
32 * of the scheduler it's an obvious special-case), so we
33 * do an early lockdep release here:
34 */
35 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
36
37 context_tracking_task_switch(prev, next);
38 /* Here we just switch the register state and the stack. */
39 switch_to(prev, next, prev);
40
41 barrier();
42 /*
43 * this_rq must be evaluated again because prev may have moved
44 * CPUs since it called schedule(), thus the 'rq' on its stack
45 * frame will be invalid.
46 */
47 finish_task_switch(this_rq(), prev);
48 }
context_switch

前文中提到的进程上下文关于硬件上下文,将当前进程的寄存器中的内容保存,并装入下一进程的以前保存的堆栈,寄存器的内容,即下面的switch_to()实现:

 1 #define switch_to(prev, next, last)                    \
2 32do { \
3 33 /* \
4 34 * Context-switching clobbers all registers, so we clobber \
5 35 * them explicitly, via unused output variables. \
6 36 * (EAX and EBP is not listed because EBP is saved/restored \
7 37 * explicitly for wchan access and EAX is the return value of \
8 38 * __switch_to()) \
9 39 */ \
10 40 unsigned long ebx, ecx, edx, esi, edi; \
11 41 \
12 42 asm volatile("pushfl\n\t" /* save flags */ \
13 43 "pushl %%ebp\n\t" /* save EBP */ \
14 44 "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
15 45 "movl %[next_sp],%%esp\n\t" /* restore ESP */ \
16 46 "movl $1f,%[prev_ip]\n\t" /* save EIP */ \
17 47 "pushl %[next_ip]\n\t" /* restore EIP */ \
18 48 __switch_canary \
19 49 "jmp __switch_to\n" /* regparm call */ \
20 50 "1:\t" \
21 51 "popl %%ebp\n\t" /* restore EBP */ \
22 52 "popfl\n" /* restore flags */ \
23 53 \
24 54 /* output parameters */ \
25 55 : [prev_sp] "=m" (prev->thread.sp), \
26 56 [prev_ip] "=m" (prev->thread.ip), \
27 57 "=a" (last), \
28 58 \
29 59 /* clobbered output registers: */ \
30 60 "=b" (ebx), "=c" (ecx), "=d" (edx), \
31 61 "=S" (esi), "=D" (edi) \
32 62 \
33 63 __switch_canary_oparam \
34 64 \
35 65 /* input parameters: */ \
36 66 : [next_sp] "m" (next->thread.sp), \
37 67 [next_ip] "m" (next->thread.ip), \
38 68 \
39 69 /* regparm parameters for __switch_to(): */ \
40 70 [prev] "a" (prev), \
41 71 [next] "d" (next) \
42 72 \
43 73 __switch_canary_iparam \
44 74 \
45 75 : /* reloaded segment registers */ \
46 76 "memory"); \
47 77} while (0)
48 78

可以从上面的嵌入汇编看出主要就是保存当前进程的flags,ebp,esp,eip,并装入下一进程的flags,ebp,esp,eip,当执行到 "pushl %[next_ip]\n\t" ,即进入到下一个进程的范畴了。

 

三  利用gdb工具跟踪

  配置qemu环境:

linux 内核学习之八   进程调度过程分析

设置断点:

linux 内核学习之八   进程调度过程分析

发现gdb工具探测不到context_switch(),switch_to() ,只有断点1有效,下面的结果也验证了

linux 内核学习之八   进程调度过程分析

停在第一个断点处。接着运行“c”,但是断点一直在schedule()处

主要是context_switch(),switch_to() 两处设置不了断点,有待解决,希望大家指出是什么问题。。。。

 

三  总结分析

    linux与任何分时系统一样,通过一个进程到另一个进程的快速切换,达到表面上看起来多个任务并行执行的效果。

  schedule()可以由几个内核控制路径调用,可以采取直接调用的方式或延迟调用的方式。

  直接调用:如果current进程不能获得必须的资源而要立刻被阻塞,直接调用调度程序。

  延迟调用:把current进程的TIF_NEED_RESCHED标志设置为1,而以延迟的方式调度调度程序。由于总是在恢复用户态进程的执行之前检查这个标志的值,所以schedule()将在不久后的某个时刻被明确的执行。

 

by:方龙伟

原创作品 转载请注明出处

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