linux內核線程migration_thread和kthreadd的創建

linux內核中兩大重要的線程,migration_thread負責cpu的負載均衡(將進程從本地隊列移動到目標cpu的隊列),kthreadd負責爲kthread_create_list鏈表中的成員創建內核線程。

內核版本2.6.24中的引導部分,start_kernel()->rest_init():


點擊(此處)摺疊或打開

  1. static void noinline __init_refok rest_init(void)

  2.         __releases(kernel_lock)

  3. {

  4.         int pid;


  5.         kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);

  6.         numa_default_policy();

  7.         pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);

  8.         kthreadd_task = find_task_by_pid(pid);

  9. ……

  10. }


以上依次創建了kernel_init線程和kthreadd線程,rest_init()是在禁用搶佔(preempt_disable)的情況下運行,因此保證了kernel_init()運行時kthreadd_task 已經指向ktheadd線程。

kernel_init()調用do_pre_smp_initcalls()->migration_init();創建了負責將進程在cpu間移動(cpu負載均衡)的內核線程migration_thread(每個cpu一個),創建線程是通過將包含待運行函數及參數的kthread_create_info結構掛入kthread_create_list鏈表,然後喚醒kthreadd_task(即ktheadd線程),而ktheadd線程負責爲鏈表上的每個結構創建相應的線程。


點擊(此處)摺疊或打開

  1. void __init migration_init(void)

  2. {

  3.         void *cpu = (void *)(long)smp_processor_id();

  4.         int err;


  5.         /* Start one for the boot CPU: */

  6.         err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);

  7.         BUG_ON(err == NOTIFY_BAD);

  8.     migration_call(&migration_notifier, CPU_ONLINE, cpu);

  9.     register_cpu_notifier(&migration_notifier);

  10. }


首先直接調用migration_call兩次創建了引導cpu的migration_thread線程並喚醒,然後調用register_cpu_notifier()將migration_notifier掛入cpu_chain鏈表,在之後kernel_init()->smp_init()中將依次對其餘未上線的cpu調用cpu_up()->_cpu_up(),該函數分別以參數CPU_UP_PREPARE和CPU_ONLINE調用兩次__raw_notifier_call_chain(),實則是運行cpu_chain鏈表上的函數,也包括了migration_call(),因此其餘cpu的migration_thread也得以創建,最終是每個cpu上都有一個migration_thread線程。

點擊(此處)摺疊或打開

  1. static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)

  2. {

  3.     int ret, nr_calls = 0;

  4.     void *hcpu = (void *)(long)cpu;

  5.     unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;


  6.     if (cpu_online(cpu) || !cpu_present(cpu))

  7.         return -EINVAL;


  8.     raw_notifier_call_chain(&cpu_chain, CPU_LOCK_ACQUIRE, hcpu);

  9.     ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,

  10.                             -1, &nr_calls);

  11. ……

  12. /* Now call notifier in preparation. */

  13.     raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);

  14. ……

  15. }


/*

1、以CPU_UP_PREPARE爲參數調用時,創建migration_thread線程,並綁定到cpu,設置調用策略爲實時進程SCHED_FIFO,

優先級99,cpu運行隊列的migration_thread指向該內核線程,此時線程是不可中斷睡眠狀態。

2、以CPU_ONLINE爲參數調用時,喚醒cpu_rq(cpu)->migration_thread指向的migration_thread線程。

*/


點擊(此處)摺疊或打開

  1. static int __cpuinit

  2. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)

  3. {

  4.         struct task_struct *p;

  5.         int cpu = (long)hcpu;

  6.         unsigned long flags;

  7.         struct rq *rq;


  8.         switch (action) {

  9.         case CPU_LOCK_ACQUIRE:

  10.                 mutex_lock(&sched_hotcpu_mutex);

  11.                 break;


  12.         case CPU_UP_PREPARE:

  13.             case CPU_UP_PREPARE_FROZEN:

  14.         p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);

  15.         if (IS_ERR(p))

  16.             return NOTIFY_BAD;

  17.         kthread_bind(p, cpu);

  18.         /* Must be high prio: stop_machine expects to yield to it. */

  19.         rq = task_rq_lock(p, &flags);

  20.         __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);

  21.         task_rq_unlock(rq, &flags);

  22.         cpu_rq(cpu)->migration_thread = p;

  23.         break;


  24.     case CPU_ONLINE:

  25.     case CPU_ONLINE_FROZEN:

  26.         /* Strictly unnecessary, as first user will wake it. */

  27.         wake_up_process(cpu_rq(cpu)->migration_thread);

  28.         break;

  29. ……

  30. }


/*創建線程是通過將包含線程運行函數和參數的kthread_create_info結構掛入kthread_create_list鏈表,

並喚醒內核線程kthreadd_task對鏈表上的各個線程創建需求進行處理來實現的,當創建成功後返回進程指針,

這是通過wait_for_completion(&create.done)進行同步的。此內核線程創建函數不負責將線程綁定到cpu,綁定工作

須由調用函數負責。

*/


點擊(此處)摺疊或打開

  1. struct task_struct *kthread_create(int (*threadfn)(void *data),

  2.                                    void *data,

  3.                                    const char namefmt[],

  4.                                    ...)

  5. {

  6.         struct kthread_create_info create;


  7.         create.threadfn = threadfn;

  8.         create.data = data;

  9.         init_completion(&create.started);

  10.         init_completion(&create.done);


  11.         spin_lock(&kthread_create_lock);

  12.         list_add_tail(&create.list, &kthread_create_list);

  13.         wake_up_process(kthreadd_task);

  14.         spin_unlock(&kthread_create_lock);


  15.     wait_for_completion(&create.done);

  16. ……

  17.         return create.result;

  18. }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章