一 rest_init
1.0 rest_init
定义在init/main.c中
static noinline void __init_refok rest_init(void)
{
int pid;
const struct sched_param param = { .sched_priority = 1 };
rcu_scheduler_starting(); // 使能rcu
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND); // 创建kernel_init内核
线程,即init,1号进程,但是在kthreadd后运行
numa_default_policy(); // 设定NUMA系统的默认内存访问策略
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES); // 创建kthreadd内核线程,2号进程,用于管理和调度其它内核线程。// kthread_create创建的内核线程
rcu_read_lock();
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); // 获取kthreadd的进程描述符
rcu_read_unlock();
sched_setscheduler_nocheck(kthreadd_task, SCHED_FIFO, ¶m);
complete(&kthreadd_done); // 通知kernel_init进程kthreadd进程已创建完成
/*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current); // 设置当前进程(0号进程)为idle进程类
schedule_preempt_disabled(); // 主动调用进程调度,并禁止内核抢占
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE); // 0号进程完成kernel初始化的工作,进入idle循环,化身idle进程
}
1.1 rcu_scheduler_starting
定义在kernel/rcutree.c中
void rcu_scheduler_starting(void)
{
WARN_ON(num_online_cpus() != 1); // 确保当前只启动了一个cpu核
WARN_ON(nr_context_switches() > 0); // 确保之前没有进行进程上下文切换
rcu_scheduler_active = 1; // 使能rcu机制
}
1.2 kernel_thread
定义在kernel/fork.c
pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
{
return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
(unsigned long)arg, NULL, NULL);
}
1.3 kthreadd
定义在init/main.c中
int kthreadd(void *unused)
{
struct task_struct *tsk = current;
/* Setup a clean context for our children to inherit. */
set_task_comm(tsk, "kthreadd");
ignore_signals(tsk);
set_cpus_allowed_ptr(tsk, cpu_all_mask);
set_mems_allowed(node_states[N_MEMORY]);
current->flags |= PF_NOFREEZE;
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (list_empty(&kthread_create_list))
schedule();
__set_current_state(TASK_RUNNING);
spin_lock(&kthread_create_lock);
while (!list_empty(&kthr