lab5:深入理解进程切换

Linux 内核中的上下文切换函数是content_switch 。该函数位于 Linux 内核源码目录的 kernel/sched/core.c 中,代码如下:

static __always_inline struct rq *
context_switch(struct rq *rq, struct task_struct *prev,
         struct task_struct *next, struct rq_flags *rf)
{
  prepare_task_switch(rq, prev, next);

  /*
   * 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);

  /*
   * kernel -> kernel   lazy + transfer active
   *   user -> kernel   lazy + mmgrab() active
   *
   * kernel ->   user   switch + mmdrop() active
   *   user ->   user   switch
   */
  if (!next->mm) {                                // to kernel
    enter_lazy_tlb(prev->active_mm, next);

    next->active_mm = prev->active_mm;
    if (prev->mm)                           // from user
      mmgrab(prev->active_mm);
    else
      prev->active_mm = NULL;
  } else {                                        // to user
    membarrier_switch_mm(rq, prev->active_mm, next->mm);
    /*
     * sys_membarrier() requires an smp_mb() between setting
     * rq->curr / membarrier_switch_mm() and returning to userspace.
     *
     * The below provides this either through switch_mm(), or in
     * case 'prev->active_mm == next->mm' through
     * finish_task_switch()'s mmdrop().
     */
    switch_mm_irqs_off(prev->active_mm, next->mm, next);

    if (!prev->mm) {                        // from kernel
      /* will mmdrop() in finish_task_switch(). */
      rq->prev_mm = prev->active_mm;
      prev->active_mm = NULL;
    }
  }

  rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);

  prepare_lock_switch(rq, next, rf);

  /* Here we just switch the register state and the stack. */
  switch_to(prev, next, prev);
  barrier();

  return finish_task_switch(prev);
}

其中content_switch 函数有三个参数:rq、prev、next,其中 rq 指向本次进程切换发生的 running queue;prev 和 next 分别指向切换前后进程的进程描述符。

首先需要调用 prepare_task_switch 函数。

static inline void
prepare_task_switch(struct rq *rq, struct task_struct *prev,
            struct task_struct *next)
{
    sched_info_switch(rq, prev, next);
    perf_event_task_sched_out(prev, next);
    fire_sched_out_preempt_notifiers(prev, next);
    prepare_lock_switch(rq, next);
    prepare_arch_switch(next);
}

随后通过调用 arch_start_context_switch 函数,通知相关模块处理进程切换。在这一步中,可能会涉及到虚拟化、页表切换等操作。接下来,根据被调度的下一个进程的类型来选择不同的执行路径。如果被调度的下一个进程是内核态进程,需要调用 enter_lazy_tlb 函数,更新 TLB,并将被调度的下一个进程的 active_mm 字段设为当前进程的 active_mm 字段。而如果当前进程是用户态进程,则需要调用 mmgrab 函数,增加当前进程的 active_mm 引用计数。

如果被调度的下一个进程是用户态进程,则需要调用 membarrier_switch_mm 函数,执行一些内存障碍操作,并将当前进程的 active_mm 字段设置为 NULL,将被调度的下一个进程的 active_mm 字段设置为其自己的 mm 字段。然后,调用 switch_mm_irqs_off 函数,切换当前进程和被调度的下一个进程的地址空间,并禁止中断。接下来,根据当前进程和被调度的下一个进程的类型和状态,执行相应的操作,例如更新运行队列、设置进程状态等。最后,调用 switch_to 函数,进行进程切换,实现将当前正在运行的进程prev切换为被调度的下一个进程next。完成进程切换后,调用 finish_task_switch 函数,进行收尾工作并返回。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值