workqueue 是除了 softirq 和 tasklet 以外最常用的下半部机制之一。workqueue 的本质是把 work 交给一个内核线程,在进程上下文调度的时候执行。因为这个特点,所以 workqueue 允许重新调度和睡眠,这种异步执行的进程上下文,能解决因为 softirq 和 tasklet 执行时间长而导致的系统实时性下降等问题。
当驱动程序在进程上下文中有异步执行的工作任务时,可以用 work 来描述工作任务。把 work 添加到一个链表 worklist 中,然后由一个内核线程 worker 遍历链表,串行地执行挂入 worklist 中的所有 work。如果 worklist 中没有 work,那么内核线程 worker 就会变成 IDLE 状态;如果有 work,则执行 work 中的回调函数。
workqueue 相关的数据结构
关于 workqueue 中几个概念都是 work 相关的数据结构非常容易混淆,大概可以这样来理解:
work_struct :
工作。初始化一个 work 并添加到工作队列后,将会将其传递到合适的内核线程来进行处理,它是用于调度的最小单位。
- structwork_struct{
- atomic_long_tdata;
- structlist_headentry;
- work_func_tfunc;
- #ifdefCONFIG_LOCKDEP
- structlockdep_maplockdep_map;
- #endif
- };
- data:低比特存放状态位,高比特存放 worker_pool 的ID或者 pool_workqueue 的指针
- entry:用于添加到其他队列上
- func:工作任务的处理函数,在内核线程中回调
workqueue_struct :
工作的集合。workqueue 和 work 是一对多的关系。内核中工作队列分为两种:
- bound:绑定处理器的工作队列,每个 worker 创建的内核线程绑定到特定的 CPU 上运行。
- unbound:不绑定处理器的工作队列,创建的时候需要指定 WQ_UNBOUND 标志,内核线程可以在处理器间迁移。
- structworkqueue_struct{
- structlist_headpwqs;/*WR:allpwqsofthiswq*/
- structlist_headlist;/*PR:listofallworkqueues*/
- structlist_headmaydays;/*MD:pwqsrequestingrescue*/
- structworker*rescuer;/*I:rescueworker*/
- structpool_workqueue*dfl_pwq;/*PW:onlyforunboundwqs*/
- charname[WQ_NAME_LEN];/*I:workqueuename*/
- /*hotfieldsusedduringcommandissue,alignedtocacheline*/
- unsignedintflags____cacheline_aligned;/*WQ:WQ_*flags*/
- structpool_workqueue__percpu*cpu_pwqs;/*I:per-cpupwqs*/
- structpool_workqueue__rcu*numa_pwq_tbl[];/*PWR:unboundpwqsindexedbynode*///Per-Node创建pool_workqueue
- …
- };
- pwqs:所有的 pool_workqueue 都添加到本链表中
- list:用于将工作队列添加到全局链表 workqueues 中
- maydays:rescue状态下的 pool_workqueue 添加到本链表中
- rescuer:rescuer 内核线程,用于处理内存紧张时创建工作线程失败的情况
- cpu_pwqs:Per-CPU 创建 pool_workqueue
- numa_pwq_tbl[]:Per-Node 创建 pool_workqueue
pool_workqueue:
中间人 / 中介,负责建立起 workqueue 和 worker_pool 之间的关系。workqueue 和 pool_workqueue 是一对多的关系。
- structpool_workqueue{
- structworker_pool*pool;/*I:theassociatedpool*/
- structworkqueue_struct*wq;/*I:theowningworkqueue*/
- intnr_active;/*L:nrofactiveworks*/
- intmax_active;/*L:maxactiveworks*/
- structlist_headdelayed_works;/*L:delayedworks*/
- structlist_headpwqs_node;/*WR:nodeonwq->pwqs*/
- structlist_headmayday_node;/*MD:nodeonwq->maydays*///用于添加到workqueue链表中
- …
- }__aligned(1<<WORK_STRUCT_FLAG_BITS);
- pool:指向 worker_pool
- wq:指向所属的 workqueue
- nr_active:活跃的 work 数量
- max_active:活跃的最大 work 数量
- delayed_works:延迟执行的 work 挂入本链表
- pwqs_node:用于添加到 workqueue 链表中
- mayday_node:用于添加到 workqueue 链表中
worker_pool:
工人的集合。pool_workqueue 和 worker_pool 是一对一的关系,worker_pool 和 worker 是一对多的关系。
- bound 类型的工作队列:worker_pool 是 Per-CPU 创建,每个 CPU 都有两个 worker_pool,对应不同的优先级,nice 值分别为 0 和 -20。
- unbound 类型的工作队列:worker_pool 创建后会添加到 unbound_pool_hash 哈希表中。
- structworker_pool{
- spinlock_tlock;/*thepoollock*/
- intcpu;/*I:theassociatedcpu*/
- intnode;/*I:theassociatednodeID*/
- intid;/*I:poolID*/
- unsignedintflags;/*X:flags*/
- unsignedlongwatchdog_ts;/*L:watchdogtimestamp*/
- structlist_headworklist;/*L:listofpendingworks*/
- intnr_workers;/*L:totalnumberofworkers*/
- /*nr_idleincludestheonesoffidle_listforrebinding*/
- intnr_idle;/*L:currentlyidleones*/
- structlist_headidle_list;/*X:listofidleworkers*/
- structtimer_listidle_timer;/*L:workeridletimeout*/
- structtimer_listmayday_timer;/*L:SOStimerforworkers*/
- /*aworkersiseitheronbusy_hashoridle_list,orthemanager*/
- DECLARE_HASHTABLE(busy_hash,BUSY_WORKER_HASH_ORDER);/*L:hashofbusyworkers*/
- /*seemanage_workers()fordetailsonthetwomanagermutexes*/
- structworker*manager;/*L:purelyinformational*/
- structmutexattach_mutex;/*attach/detachexclusion*/
- structlist_headworkers;/*A:attachedworkers*/
- structcompletion*detach_completion;/*allworkersdetached*/
- structidaworker_ida;/*workerIDsfortaskname*/
- structworkqueue_attrs*attrs;/*I:workerattributes*/
- structhlist_nodehash_node;/*PL:unbound_pool_hashnode*/
- …
- }____cacheline_aligned_in_smp;
- cpu:绑定到 CPU 的 workqueue,代表 CPU ID
- node:非绑定类型的 workqueue,代表内存 Node ID
- worklist:pending 状态的 work 添加到本链表
- nr_workers:worker 的数量
- idle_list:处于 IDLE 状态的 worker 添加到本链表
- busy_hash:工作状态的 worker 添加到本哈希表中
- workers:worker_pool 管理的 worker 添加到本链表中
- hash_node:用于添加到 unbound_pool_hash 中
worker :
工人。在代码中 worker 对应一个 work_thread() 内核线程。
- structworker{
- /*onidlelistwhileidle,onbusyhashtablewhilebusy*/
- union{
- structlist_headentry;/*L:whileidle*/
- structhlist_nodehentry;/*L:whilebusy*/
- };
- structwork_struct*current_work;/*L:workbeingprocessed*/
- work_func_tcurrent_func;/*L:current_work'sfn*/
- structpool_workqueue*current_pwq;/*L:current_work'spwq*/
- structlist_headscheduled;/*L:scheduledworks*/
- /*64bytesboundaryon64bit,32on32bit*/
- structtask_struct*task;/*I:workertask*/
- structworker_pool*pool;/*I:theassociatedpool*/
- /*L:forrescuers*/
- structlist_headnode;/*A:anchoredatpool->workers*///添加到worker_pool->workers链表中
- /*A:runsthroughworker->node*/
- …
- };
- entry:用于添加到 worker_pool 的空闲链表中
- hentry:用于添加到 worker_pool 的忙碌列表中
- current_work:当前正在处理的 work
- current_func:当前正在执行的 work 回调函数
- current_pwq:指向当前 work 所属的 pool_workqueue
- scheduled:所有被调度执行的 work 都将添加到该链表中
- task:指向内核线程
- pool:该 worker 所属的 worker_pool
- node:添加到 worker_pool->workers 链表中
可以用下图来总结:
workqueue 的初始化
内核在启动的时候会对 workqueue 做初始化,workqueue 的初始化包含两个阶段,分别是 workqueue_init_early 和 workqueue_init。
workqueue_init_early
- 分配 worker_pool,并且对该结构中的字段进行初始化操作
- 分配 workqueue_struct,并且对该结构中的字段进行初始化操作
- alloc_and_link_pwqs:分配 pool_workqueue,将 workqueue_struct 和 worker_pool 关联起来
workqueue_init
这里主要完成的工作是给之前创建好的 worker_pool,添加一个初始的 worker,然后利用函数 create_worker,创建名字为 kworker/XX:YY 或者 kworker/uXX:YY 的内核线程。其中 XX 表示 worker_pool 的编号,YY 表示 worker 的编号,u 表示unbound。
- 分配 worker,并且对该结构中的字段进行初始化操作
- 为 worker 创建内核线程 worker_thread
- 将 worker 添加到 worker_pool 中
- worker 进入 IDLE 状态
经过上面两个阶段的初始化,workqueue 子系统基本就已经将数据结构的关联建立好了,当有 work 来进行调度的时候,就可以进行处理了。
使用 workqueue
内核推荐驱动开发者使用默认的 workqueue,而不是新建 workqueue。要使用系统默认的 workqueue,首先需要初始化 work,内核提供了相应的宏 INIT_WORK。
初始化 work
- #defineINIT_WORK(_work,_func)\\
- __INIT_WORK((_work),(_func),0)
- #define__INIT_WORK(_work,_func,_onstack)\\
- do{\\
- __init_work((_work),_onstack);\\
- (_work)->data=(atomic_long_t)WORK_DATA_INIT();\\
- INIT_LIST_HEAD(&(_work)->entry);\\
- (_work)->func=(_func);\\
- }while(0)
初始化 work 后,就可以调用 shedule_work 函数把 work 挂入系统默认的 workqueue 中。
work 调度
- 将 work 添加到系统的 system_wq 工作队列中。
- 判断 workqueue 的类型,如果是 bound 类型,根据 CPU 来获取 pool_workqueue。如果是 unbound 类型,通过 node 号来获取 pool_workqueue。
- 判断 pool_workqueue 活跃的 work 数量,少于最大限值则将 work 加入到 pool->worklist 中,否则加入到 pwq->delayed_works 链表中。
- 如果 __need_more_worker 判断没有 worker 在执行,则通过 wake_up_worker 唤醒 worker 内核线程。
worker_thread
worker 内核线程的执行函数是 worker_thread。
- 设置标志位 PF_WQ_WORKER,调度器在进行调度处理时会对 task 进行判断,针对 workerqueue worker 有特殊的处理。
- worker 被唤醒的时候,跳转到 woke_up 执行。
- woke_up 中,如果此时 worker 是需要销毁的,那就进行清理工作并返回。否则,离开 IDLE 状态,并进入 recheck 模块执行。
- recheck 中,判断是否需要更多的 worker 来处理,如果没有任务处理,跳转到 sleep 地方进行睡眠。如果有任务需要处理时,遍历工作链表,对链表中的每个节点调用 process_one_work 来执行 work 的回调函数,即 INIT_WORK 里的回调函数。
- sleep 中,没有任务处理时,worker 进入空闲状态,并将当前的内核线程设置成睡眠状态,让出 CPU。
总结
原文链接:https://mp.weixin.qq.com/s/zU53dl8me9wHIJOnRmxBtw










