内核线程创建
内核线程的创建最终是由kthreadd完成,内核创建内核线程的其他api是kthread_create_on_node对它的封装。
常见的kthread_create_on_cpu和create_worker最终调用kthread_create_on_node
内核初始化时,会创建两个进程init和kthreadd,init进程最终会从内核态转到用户态,执行/etc/init.d的各种service,完成系统加载;kthreadd用于创建内核线程,其是所有内核线程的parent
static noinline void __init_refok rest_init(void)
{
int pid;
rcu_scheduler_starting();
/*
* 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); //init进程,kernel_thread是对do_fork的简单封装
numa_default_policy();
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);// kthreadd进程创建
rcu_read_lock();
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
rcu_read_unlock();
complete(&kthreadd_done);
/*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current);
schedule_preempt_disabled();
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE);
}
kthreadd是创建的内核线程kthreadd的执行函数,在一个循环中遍历kthread_create_list,调用create_thread为每个节点创建内核线程。
int kthreadd