When kernel bootup, two important threads are created. INIT thread(PID1) and kthreadd(PID2).
PID2 is what we analysis here.
kthread_create_list: all the threads to be created are stored in this list. To creae a new thread, we just fill the related information in a struct kthread_create_info, and list it to the list, then wait until the thread is ready by mutex.
###kthreadd: PID2 main function
it is a infinite loop, which removes the nodes from kthread_create_list, and creates a thread per node struct kthread_create_info, then notify the kthread is ready.
After the new thread is created by do_fork, a common funtion thread is call as the new thread entry, not the threadfn we give by API kthread_create
thread will do some common initialization, and notify new thread is ready, and then the real new thread function threadfn is called.
All the kernel threads are the children of PID2(kthreadd).
##Data structure
kthread_create_lock
spin lock kthread_create_lock is used to protect the kthread_create_list.
1 2 3
23staticDEFINE_SPINLOCK(kthread_create_lock); 24staticLIST_HEAD(kthread_create_list); 25structtask_struct *kthreadd_task; <== store PID2.
In order to create a kernel thread, this structure MUST be created and filled.
kthread_create_info
1 2 3 4 5 6 7 8 9 10 11 12 13
27structkthread_create_info 28 { 29/* Information passed to kthread() from kthreadd. */ 30int (*threadfn)(void *data); 31void *data; 32int node; <== memory node; 33 34/* Result passed back to kthread_create() from kthreadd. */ 35structtask_struct *result; 36structcompletiondone; 37 38structlist_headlist; 39 };
231 /** 232 * kthread_create_on_node - create a kthread. 233 * @threadfn: the function to run until signal_pending(current). 234 * @data: data ptr for @threadfn. 235 * @node: memory node number. 236 * @namefmt: printf-style name for the thread. 237 * 238 * Description: This helper function creates and names a kernel 239 * thread. The thread will be stopped: use wake_up_process() to start 240 * it. See also kthread_run(). 241 * 242 * If thread is going to be bound on a particular cpu, give its node 243 * in @node, to get NUMA affinity for kthread stack, or else give -1. 244 * When woken, the thread will run @threadfn() with @data as its 245 * argument. @threadfn() can either call do_exit() directly if it is a 246 * standalone thread for which no one will call kthread_stop(), or 247 * return when 'kthread_should_stop()' is true (which means 248 * kthread_stop() has been called). The return value should be zero 249 * or a negative error number; it will be passed to kthread_stop(). 250 * 251 * Returns a task_struct or ERR_PTR(-ENOMEM). 252 */ 253 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), 254 void *data, int node, 255 const char namefmt[], 256 ...) 257 { 258 struct kthread_create_info create; 259 260 create.threadfn = threadfn; 261 create.data = data; 262 create.node = node; 263 init_completion(&create.done); 264 265 spin_lock(&kthread_create_lock); 266 list_add_tail(&create.list, &kthread_create_list); 267 spin_unlock(&kthread_create_lock); 268 269 wake_up_process(kthreadd_task); 270 wait_for_completion(&create.done); 271 272 if (!IS_ERR(create.result)) { 273 static const struct sched_param param = { .sched_priority = 0 }; 274 va_list args; 275 276 va_start(args, namefmt); 277 vsnprintf(create.result->comm, sizeof(create.result->comm), 278 namefmt, args); 279 va_end(args); 280 /* 281 * root may have changed our (kthreadd's) priority or CPU mask. 282 * The kernel thread should not inherit these properties. 283 */ 284 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, ¶m); 285 set_cpus_allowed_ptr(create.result, cpu_all_mask); 286 } 287 return create.result; 288 } 289 EXPORT_SYMBOL(kthread_create_on_node);
364static noinline void __init_refok rest_init(void) 365 { 366int pid; 367 368 rcu_scheduler_starting(); 369/* 370 * We need to spawn init first so that it obtains pid 1, however 371 * the init task will end up wanting to create kthreads, which, if 372 * we schedule it before we create kthreadd, will OOPS. 373 */ 374 kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND); 375 numa_default_policy(); 376 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES); <==PID1 377 rcu_read_lock(); 378 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); <== PID2 379 rcu_read_unlock(); 380 complete(&kthreadd_done); 381 382/* 383 * The boot idle thread must execute schedule() 384 * at least once to get things moving: 385 */ 386 init_idle_bootup_task(current); 387 schedule_preempt_disabled(); 388/* Call into cpu_idle with preempt disabled */ 389 cpu_startup_entry(CPUHP_ONLINE); 390 }