linux内核中从核初始化过程

linux 内核的入口函数是每个体系结构自己定义的,假设当前架构的内核入口函数是:kernel_entry(),则下面梳理了从核的初始化过程(内核版本:4.19):

kernel_entry()
  |_ start_kernel()
     |_ rest_init()
        |_ kernel_init() // 新线程
        |  |_ smp_init()
        |     |_ cpuhp_threads_init()
        |     |  |_ smpboot_register_percpu_thread()
        |     |     |_ kthread_create_on_cpu()
        |     |        |_ kthread_create_on_node()
        |     |           |_ __kthread_create_on_node()
        |     |              |_ 将 create 信息放在 kthread_create_list 链表上
        |     |_ cpu_up()
        |        |_ do_cpu_up()
        |           |_ _cpu_up()
        |              |_ cpuhp_set_state()
        |                 |_ 将从核的 target (在 cpuhp_cpu_state 结构体中) 设置为 CPUHP_ONLINE(最大值)
        |_ kthreadd() // 新线程
           |_ 遍历 kthread_create_list 链表,依次取下每个 create 信息
              |_ create_thread()
                 |_ kthread() // 新线程
                    |_ 执行 create 信息中的 threadfn 函数指针
                       |_ smpboot_thread_fn()
                          |_ cpuhp_thread_fun()
                             |_ cpuhp_invoke_callback()
                                |_ pv_cpu_online()

在从核线程中
smpboot_thread_fn() 会循环(while(1))调用 cpuhp_thread_fun(),后者在每次被调用时都会更新当前 cpu 的 state (在 cpuhp_cpu_state 结构体中),当 state 大于 target 后,结束循环。其中 target 的值由主核设置为:CPUHP_ONLINE

在主核线程中
调用 _cpu_up() 时也做了从核的部分初始化工作,代码如下所示:

static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
{
    ...
    // 设置从核的 target
    cpuhp_set_state(st, target);
   
    /*
     * Try to reach the target state. We max out on the BP at
     * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
     * responsible for bringing it up to the target state.
     */
    target = min((int)target, CPUHP_BRINGUP_CPU);
    ret = cpuhp_up_callbacks(cpu, st, target);
    ...
}

从上面的注释可以看到,主核将从核的状态初始化到了 CPUHP_BRINGUP_CPU,而 CPUHP_BRINGUP_CPU+1CPUHP_ONLINE 之间的状态由从核自己初始化。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值