扒开 Linux 中断的底裤之 workqueue

系统 Linux
workqueue 是除了 softirq 和 tasklet 以外最常用的下半部机制之一。workqueue 的本质是把 work 交给一个内核线程,在进程上下文调度的时候执行。

[[416567]]

 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 并添加到工作队列后,将会将其传递到合适的内核线程来进行处理,它是用于调度的最小单位。 

  1. struct work_struct {  
  2.  atomic_long_t data;     
  3.  struct list_head entry;   
  4.  work_func_t func;       
  5.  #ifdef CONFIG_LOCKDEP  
  6.  struct lockdep_map lockdep_map;  
  7. #endif  
  8. }; 
  •   data:低比特存放状态位,高比特存放 worker_pool 的ID或者 pool_workqueue 的指针
  •  entry:用于添加到其他队列上
  •  func:工作任务的处理函数,在内核线程中回调

workqueue_struct :

工作的集合。workqueue 和 work 是一对多的关系。内核中工作队列分为两种:

  1.  bound:绑定处理器的工作队列,每个 worker 创建的内核线程绑定到特定的 CPU 上运行。
  2.  unbound:不绑定处理器的工作队列,创建的时候需要指定 WQ_UNBOUND 标志,内核线程可以在处理器间迁移。 
  1. struct workqueue_struct {  
  2.  struct list_head pwqs;  /* WR: all pwqs of this wq */    
  3.  struct list_head list;  /* PR: list of all workqueues */    
  4.  struct list_head maydays; /* MD: pwqs requesting rescue */     
  5.  struct worker  *rescuer; /* I: rescue worker */    
  6.  struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */  
  7.  char   name[WQ_NAME_LEN]; /* I: workqueue name */  
  8.  /* hot fields used during command issue, aligned to cacheline */  
  9.  unsigned int  flags ____cacheline_aligned; /* WQ: WQ_* flags */  
  10.  struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */      
  11.  struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */    //Per-Node创建pool_workqueue  
  12.     ...  
  13. }; 
  •  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 是一对多的关系。 

  1. struct pool_workqueue {  
  2.  struct worker_pool *pool;  /* I: the associated pool */      
  3.  struct workqueue_struct *wq;  /* I: the owning workqueue */     
  4.  int   nr_active; /* L: nr of active works */      
  5.  int   max_active; /* L: max active works */     
  6.  struct list_head delayed_works; /* L: delayed works */     
  7.  struct list_head pwqs_node; /* WR: node on wq->pwqs */     
  8.  struct list_head mayday_node; /* MD: node on wq->maydays */   //用于添加到workqueue链表中 
  9.     ...  
  10. } __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 是一对多的关系。

  1.   bound 类型的工作队列:worker_pool 是 Per-CPU 创建,每个 CPU 都有两个 worker_pool,对应不同的优先级,nice 值分别为 0 和 -20。
  2.   unbound 类型的工作队列:worker_pool 创建后会添加到 unbound_pool_hash 哈希表中。 
  1. struct worker_pool {  
  2.  spinlock_t  lock;  /* the pool lock */  
  3.  int   cpu;  /* I: the associated cpu */    
  4.  int   node;  /* I: the associated node ID */   
  5.  int   id;  /* I: pool ID */  
  6.  unsigned int  flags;  /* X: flags */  
  7.  unsigned long  watchdog_ts; /* L: watchdog timestamp */  
  8.  struct list_head worklist; /* L: list of pending works */    
  9.  int   nr_workers; /* L: total number of workers */     
  10.  /* nr_idle includes the ones off idle_list for rebinding */  
  11.  int   nr_idle; /* L: currently idle ones */  
  12.  struct list_head idle_list; /* X: list of idle workers */    
  13.  struct timer_list idle_timer; /* L: worker idle timeout */  
  14.  struct timer_list mayday_timer; /* L: SOS timer for workers */  
  15.  /* a workers is either on busy_hash or idle_list, or the manager */  
  16.  DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);   /* L: hash of busy workers */  
  17.  /* see manage_workers() for details on the two manager mutexes */  
  18.  struct worker  *manager; /* L: purely informational */  
  19.  struct mutex  attach_mutex; /* attach/detach exclusion */  
  20.  struct list_head workers; /* A: attached workers */     
  21.  struct completion *detach_completion; /* all workers detached */  
  22.  struct ida  worker_ida; /* worker IDs for task name */  
  23.  struct workqueue_attrs *attrs;  /* I: worker attributes */  
  24.  struct hlist_node hash_node; /* PL: unbound_pool_hash node */     
  25.     ...  
  26. } ____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() 内核线程。 

  1. struct worker {  
  2.  /* on idle list while idle, on busy hash table while busy */  
  3.  union {  
  4.   struct list_head entry; /* L: while idle */     
  5.    struct hlist_node hentry; /* L: while busy */  
  6.  };  
  7.  struct work_struct *current_work; /* L: work being processed */    
  8.  work_func_t  current_func; /* L: current_work's fn */                
  9.   struct pool_workqueue *current_pwq; /* L: current_work's pwq */    
  10.  struct list_head scheduled; /* L: scheduled works */     
  11.   /* 64 bytes boundary on 64bit, 32 on 32bit */  
  12.  struct task_struct *task;  /* I: worker task */     
  13.  struct worker_pool *pool;  /* I: the associated pool */     
  14.       /* L: for rescuers */  
  15.  struct list_head node;  /* A: anchored at pool->workers */  //添加到worker_pool->workers链表中  
  16.       /* A: runs through worker->node */  
  17.     ...  
  18. }; 
  •  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 

  1. #define INIT_WORK(_work, _func)      \  
  2.  __INIT_WORK((_work), (_func), 0)   
  3.  #define __INIT_WORK(_work, _func, _onstack)    \  
  4.  do {        \  
  5.   __init_work((_work), _onstack);    \  
  6.   (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \  
  7.   INIT_LIST_HEAD(&(_work)->entry);   \  
  8.   (_work)->func = (_func);    \  
  9.  } 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。

总结

 

 

责任编辑:庞桂玉 来源: Linux学习
相关推荐

2021-12-27 07:55:59

Linux 中断处理Linux 系统

2021-03-28 20:44:34

Kafka中间件MQ

2020-12-29 08:02:37

SqlSession 程序Executor

2022-01-12 12:35:36

Linuxworkqueue工作队列

2021-08-03 15:10:26

Linux代码驱动

2021-08-10 11:30:30

Linux代码中断控制器

2021-12-10 08:45:45

Linux GIC Linux 系统

2021-12-14 08:51:23

Linux 中断子系统Linux 系统

2015-11-02 16:42:26

2020-12-29 09:11:33

LinuxLinux内核

2021-12-08 08:41:31

Linux 中断子系统Linux 系统

2021-12-01 07:02:16

虚拟化LinuxCPU

2021-11-30 07:02:10

虚拟化Linux 中断

2017-07-14 14:35:27

Linux中断系统

2021-05-25 07:59:59

Linux运维Linux系统

2021-09-03 09:12:09

Linux中断软件

2013-05-21 11:33:11

Android游戏开发按键中断事件

2021-11-02 10:53:56

Linux机制CPU

2020-07-09 17:37:47

Linux网络包中断

2021-01-11 12:53:28

线程Java管理
点赞
收藏

51CTO技术栈公众号