2016-12-31

来自本人的旧博客: http://blog.163.com/awaken_ing/blog/static/120613197201511307274526

上图为smpbootregisterpercputhread(), kthreadcreateoncpu(), kthreadcreateon_node(), kthreadd()和kthread()之间的关系.

上图为cpu hotplug对内核线程的操作, 以smpbootthreadfn()为例.

内核启动时, smpbootregisterpercputhread()创建,绑定并启动primary cpu的内核线程, 并挂到链表hotplugthreads上.

linux-3.10.86/kernel/smpboot.c

smpboot_register_percpu_thread
{
    __smpboot_create_thread(plug_thread, cpu);
    smpboot_unpark_thread(plug_thread, cpu);
    list_add(&plug_thread->list, &hotplug_threads);
}

启动非primary cpu时, _cpuup()创建和primary cpu相同的内核线程, 且绑定到非primary cpu上, 之后再启动.
\
cpuup -> smpbootcreatethreads(cpu) -> _smpbootcreatethread(cur, cpu)
如果是cpu down之后的cpu up, 由于内核线程已经创建过, 故smpbootcreatethreads()实际效果为空, _cpu_up()让内核线程unpark, 恢复运行.

cpu down时, 遍历链表, 对链表节点的内核线程执行park(而不是stop)操作.
_cpudown -> smpbootpark_threads(cpu)

linux-3.10.86/kernel/smpboot.c

void smpboot_park_threads(unsigned int cpu)
{
    list_for_each_entry_reverse(cur, &hotplug_threads, list)
        smpboot_park_thread(cur, cpu);
}
static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
{
    ...
    kthread_park(tsk);
}

本文地址: https://awakening-fong.github.io/posts/other/kthread_park_unpark

转载请注明出处: https://awakening-fong.github.io


若无法评论, 请打开JavaScript, 并通过proxy.


blog comments powered by Disqus