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

Linux context_switch函数实现分析

2017-04-01 19:23 183 查看
转载至:http://blog.csdn.net/gatieme/article/details/51872659?locationNum=5,原文格式太乱了,但是写得很好,我重新组织了格式,删了一些话,加了一些自己的理解

1.上下文切换的概念

进程被抢占CPU时候, 操作系统保存其上下文信息, 同时将新的活动进程的上下文信息加载进来, 这个过程其实就是上下文切换, 而当一个被抢占的进程再次成为活动的, 它可以恢复自己的上下文继续从被抢占的位置开始执行. 

上下文切换(有时也称做进程切换或任务切换)是指CPU从一个进程或线程切换到另一个进程或线程

稍微详细描述一下,上下文切换可以认为是内核(操作系统的核心)在 CPU 上对于进程(包括线程)进行以下的活动:

挂起一个进程,将这个进程在 CPU 中的状态(上下文)存储于内存中的某处,

在内存中检索下一个进程的上下文并将其在 CPU 的寄存器中恢复

跳转到程序计数器所指向的位置(即跳转到进程被中断时的代码行),以恢复该进程

因此上下文是指某一时间点CPU寄存器和程序计数器的内容, 广义上还包括内存中进程的虚拟地址映射信息.

上下文切换只能发生在内核态中, 上下文切换通常是计算密集型的。也就是说,它需要相当可观的处理器时间,在每秒几十上百次的切换中,每次切换都需要纳秒量级的时间。所以,上下文切换对系统来说意味着消耗大量的 CPU 时间,事实上,可能是操作系统中时间消耗最大的操作。 

Linux相比与其他操作系统(包括其他类 Unix 系统)有很多的优点,其中有一项就是,其上下文切换和模式切换的时间消耗非常少.

2.context_switch概览
Linux中进程调度时, 内核在选择新进程之后进行抢占时, 通过context_switch完成进程上下文切换.

context_switch其实是一个分配器, 他会调用所需的特定体系结构的方法

调用switch_mm(), 把虚拟内存从一个进程映射切换到新进程中

switch_mm更换通过task_struct->mm描述的内存管理上下文, 该工作的细节取决于处理器, 主要包括加载页表, 刷出地址转换后备缓冲器(部分或者全部), 向内存管理单元(MMU)提供新的信息

调用switch_to(),从上一个进程的处理器状态切换到新进程的处理器状态。这包括保存、恢复栈信息和寄存器信息

switch_to切换处理器寄存器的内容和内核栈(虚拟地址空间的用户部分已经通过switch_mm变更, 其中也包括了用户状态下的栈, 因此switch_to不需要变更用户栈, 只需变更内核栈), 此段代码严重依赖于体系结构, 且代码通常都是用汇编语言编写.

context_switch函数建立next进程的地址空间。进程描述符的active_mm字段指向进程所使用的内存描述符,而mm字段指向进程所拥有的内存描述符。对于一般的进程,这两个字段有相同的地址,但是,内核线程没有它自己的地址空间而且它的 mm字段总是被设置为 NULL

context_switch( )函数保证:如果next是一个内核线程, 它使用prev所使用的地址空间 

由于不同架构下地址映射的机制有所区别, 而寄存器等信息弊病也是依赖于架构的, 因此switch_mm和switch_to两个函数均是体系结构相关的

3.context_switch源码详解

/*
* context_switch - switch to the new MM and the new thread's register state.
*/
static __always_inline struct rq *
context_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next)
{
struct mm_struct *mm, *oldmm;

/*  完成进程切换的准备工作  */
prepare_task_switch(rq, prev, next);

mm = next->mm;
oldmm = prev->active_mm;
/*
* For paravirt, this is coupled with an exit in switch_to to
* combine the page table reload and the switch backend into
* one hypercall.
*/
arch_start_context_switch(prev);

/*  如果next是内核线程,则线程使用prev所使用的地址空间
*  schedule( )函数把该线程设置为懒惰TLB模式
*  内核线程并不拥有自己的页表集(task_struct->mm = NULL)
*  它使用一个普通进程的页表集
*  不过,没有必要使一个用户态线性地址对应的TLB表项无效
*  因为内核线程不访问用户态地址空间。
*/
if (!mm)        /*  内核线程无虚拟地址空间, mm = NULL*/
{
/*  内核线程的active_mm为上一个进程的mm
*  注意此时如果prev也是内核线程,
*  则oldmm为NULL, 即next->active_mm也为NULL  */
next->active_mm = oldmm;
/*  增加mm的引用计数  */
atomic_inc(&oldmm->mm_count);
/*  通知底层体系结构不需要切换虚拟地址空间的用户部分
*  这种加速上下文切换的技术称为惰性TBL  */
enter_lazy_tlb(oldmm, next);
}
else            /*  不是内核线程, 则需要切切换虚拟地址空间  */
switch_mm(oldmm, mm, next);

/*  如果prev是内核线程或正在退出的进程
*  就重新设置prev->active_mm
*  然后把指向prev内存描述符的指针保存到运行队列的prev_mm字段中
*/
if (!prev->mm)
{
/*  将prev的active_mm赋值和为空  */
prev->active_mm = NULL;
/*  更新运行队列的prev_mm成员  */
rq->prev_mm = oldmm;
}
/*
* Since the runqueue lock will be released by the next
* task (which is an invalid locking op but in the case
* of the scheduler it's an obvious special-case), so we
* do an early lockdep release here:
*/
lockdep_unpin_lock(&rq->lock);
spin_release(&rq->lock.dep_map, 1, _THIS_IP_);

/* Here we just switch the register state and the stack.
* 切换进程的执行环境, 包括堆栈和寄存器
* 同时返回上一个执行的程序
* 相当于prev = witch_to(prev, next)  */
switch_to(prev, next, prev);

/*  switch_to之后的代码只有在
*  当前进程再次被选择运行(恢复执行)时才会运行
*  而此时当前进程恢复执行时的上一个进程可能跟参数传入时的prev不同
*  甚至可能是系统中任意一个随机的进程
*  因此switch_to通过第三个参数将此进程返回
*/

/*  屏障同步, 一般用编译器指令实现
*  确保了switch_to和finish_task_switch的执行顺序
*  不会因为任何可能的优化而改变  */
barrier();

/*  进程切换之后的处理工作  */
return finish_task_switch(prev);
}


在切换进程之前,还需调用prepare_task_switch函数完成准备工作

/**
* prepare_task_switch - prepare to switch tasks
* @rq: the runqueue preparing to switch
* @prev: the current task that is being switched out
* @next: the task we are going to switch to.
*
* This is called with the rq lock held and interrupts off. It must
* be paired with a subsequent finish_task_switch after the context
* switch.
*
* prepare_task_switch sets up locking and calls architecture specific
* hooks.
*/
static inline void
prepare_task_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next)
{
fire_sched_out_preempt_notifiers(prev, next);
prepare_lock_switch(rq, next);
prepare_arch_switch(next);
}


我们再来看switch_mm函数,这个函数负责把地址空间从上一个进程切换到新进程

static inline void switch_mm(struct mm_struct *prev,
struct mm_struct *next,
struct task_struct *tsk)
{
int cpu = smp_processor_id();
//如果没有共享同一地址空间
if (likely(prev != next)) {
/* stop flush ipis for the previous mm */
//清除前一个进程的cpu_vm_mask标志,表示prev放弃cpu
cpu_clear(cpu, prev->cpu_vm_mask);
#ifdef CONFIG_SMP
/* 刷新每个cpu中的tlb
* 在linux/Include/Asm-x86/Tlbflush.h中定义了
* cpu_tlbstate
* struct tlb_state {
*     struct mm_struct *active_mm;
*     int state;
*     char __cacheline_padding[L1_CACHE_BYTES-8];
* };
* DECLARE_PER_CPU(struct tlb_state, cpu_tlbstate);
*/
per_cpu(cpu_tlbstate, cpu).state = TLBSTATE_OK;
per_cpu(cpu_tlbstate, cpu).active_mm = next;
#endif
//设置当前进程的mm->cpu_vm_mask表示其占用cpu
cpu_set(cpu, next->cpu_vm_mask);

/* Re-load page tables */
//将新进程的pgd页全局目录地址加载到cr3寄存器
load_cr3(next->pgd);

/*
* load the LDT, if the LDT is different:
*/
if (unlikely(prev->context.ldt != next->context.ldt))
load_LDT_nolock(&next->context);
}
#ifdef CONFIG_SMP
else {
per_cpu(cpu_tlbstate, cpu).state = TLBSTATE_OK;
BUG_ON(per_cpu(cpu_tlbstate, cpu).active_mm != next);

if (!cpu_test_and_set(cpu, next->cpu_vm_mask)) {
/* We were in lazy tlb mode and leave_mm disabled
* tlb flush IPI delivery. We must reload %cr3.
*/
load_cr3(next->pgd);
load_LDT_nolock(&next->context);
}
}
#endif
}


switch_mm()进行用户空间的切换, 更确切地说, 是切换地址转换表(pgd), 由于pgd包括内核虚拟地址空间和用户虚拟地址空间地址映射, linux内核把进程的整个虚拟地址空间分成两个部分, 一部分是内核虚拟地址空间, 另外一部分是内核虚拟地址空间, 各个进程的虚拟地址空间各不相同, 但是却共用了同样的内核地址空间, 这样在进程切换的时候, 就只需要切换虚拟地址空间的用户空间部分.

每个进程都有其自身的页目录表pgd

进程本身尚未切换, 而存储管理机制的页目录指针cr3却已经切换了,这样不会造成问题吗?不会的,因为这个时候CPU在系统空间运行,而所有进程的页目录表中与系统空间对应的目录项都指向相同的页表,所以,不管切换到哪一个进程的页目录表都一样,受影响的只是用户空间,系统空间的映射则永远不变

再看switch_to函数,该函数负责从上一个进程的处理器状态切换到新进程的处理器状态,这包括保存,恢复栈信息和寄存器信息,还有其他任何与体系结构相关的状态信息,都必须以每个进程为对象进行管理和保存。

/*
* Saving eflags is important. It switches not only IOPL between tasks,
* it also protects other tasks from NT leaking through sysenter etc.
*/
#define switch_to(prev, next, last)                                     \
do {                                                                    \
/*                                                              \
* Context-switching clobbers all registers, so we clobber      \
* them explicitly, via unused output variables.                \
* (EAX and EBP is not listed because EBP is saved/restored     \
* explicitly for wchan access and EAX is the return value of   \
* __switch_to())                                               \
*/                                                             \
unsigned long ebx, ecx, edx, esi, edi;                          \
\
asm volatile("pushfl\n\t" /* save flags 保存就的ebp、和flags寄存器到旧进程的内核栈中*/   \
"pushl %%ebp\n\t"          /* save    EBP   */     \
"movl %%esp,%[prev_sp]\n\t"        /* save ESP  将旧进程esp保存到thread_info结构中 */ \
"movl %[next_sp],%%esp\n\t"        /* restore ESP 用新进程esp填写esp寄存器,此时内核栈已切换  */ \
"movl $1f,%[prev_ip]\n\t"  /* save EIP 将该进程恢复执行时的下条地址保存到旧进程的thread中*/     \
"pushl %[next_ip]\n\t"     /* restore EIP 将新进程的ip值压入到新进程的内核栈中 */     \
__switch_canary                                    \
"jmp __switch_to\n"        /* regparm call  */     \
"1:\t"                                             \
"popl %%ebp\n\t"           /* restore EBP 该进程执行,恢复ebp寄存器*/     \
"popfl\n"                  /* restore flags  恢复flags寄存器*/     \
\
/* output parameters */                            \
: [prev_sp] "=m" (prev->thread.sp),                \
[prev_ip] "=m" (prev->thread.ip),                \
"=a" (last),                                     \
\
/* clobbered output registers: */                \
"=b" (ebx), "=c" (ecx), "=d" (edx),              \
"=S" (esi), "=D" (edi)                           \
\
__switch_canary_oparam                           \
\
/* input parameters: */                          \
: [next_sp]  "m" (next->thread.sp),                \
[next_ip]  "m" (next->thread.ip),                \
\
/* regparm parameters for __switch_to(): */      \
[prev]     "a" (prev),                           \
[next]     "d" (next)                            \
\
__switch_canary_iparam                           \
\
: /* reloaded segment registers */                 \
"memory");                                      \
} while (0)


最后调用finish_task_switch完成一些清理工作

/**
* finish_task_switch - clean up after a task-switch
* @rq: runqueue associated with task-switch
* @prev: the thread we just switched away from.
*
* finish_task_switch must be called after the context switch, paired
* with a prepare_task_switch call before the context switch.
* finish_task_switch will reconcile locking set up by prepare_task_switch,
* and do any other architecture-specific cleanup actions.
*
* Note that we may have delayed dropping an mm in context_switch(). If
* so, we finish that here outside of the runqueue lock. (Doing it
* with the lock held can cause deadlocks; see schedule() for
* details.)
*/
static void finish_task_switch(struct rq *rq, struct task_struct *prev)
__releases(rq->lock)
{
struct mm_struct *mm = rq->prev_mm;
long prev_state;

rq->prev_mm = NULL;

/*
* A task struct has one reference for the use as "current".
* If a task dies, then it sets TASK_DEAD in tsk->state and calls
* schedule one last time. The schedule call will never return, and
* the scheduled task must drop that reference.
* The test for TASK_DEAD must occur while the runqueue locks are
* still held, otherwise prev could be scheduled on another cpu, die
* there before we look at prev->state, and then the reference would
* be dropped twice.
*		Manfred Spraul <manfred@colorfullife.com>
*/
prev_state = prev->state;
finish_arch_switch(prev);
finish_lock_switch(rq, prev);
#ifdef CONFIG_SMP
if (current->sched_class->post_schedule)
current->sched_class->post_schedule(rq);
#endif

fire_sched_in_preempt_notifiers(current);
if (mm)
mmdrop(mm);
if (unlikely(prev_state == TASK_DEAD)) {
/*
* Remove function-return probe instances associated with this
* task and put them back on the free list.
*/
kprobe_flush_task(prev);
put_task_struct(prev);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息