Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 14: Kernel Activities


14.4.3 Work Queues


Work queues are a further means of deferring actions until later. Because they are executed in the user
context by means of daemons, the functions can sleep as long as they like — it does not matter at all to
the kernel. During the development of 2.5, work queues were designed as a replacement for thekeventd
mechanism formerly used.

Each work queue has an array with as many entries as there are processors in the system. Each entry lists
tasks to be performed at a later time.

For each work queue, the kernel generates a new kernel daemon in whose context the deferred tasks are
performed using the wait queue mechanism just described.

A new wait queue is generated by invoking one of the functionscreate_workqueueor
create_workqueue_singlethread. While the first one creates a worker thread on all CPUs, the latter one
just creates a single thread on the first CPU of the system. Both functions use__create_workqueue_key
internally^19 :

kernel/workqueue.c
struct workqueue_struct *__create_workqueue(const char *name,
int singlethread)

Thenameargument indicates the name under which the generated daemon is shown in the process
list. Ifsinglethreadis set to 0, a thread is created on every CPU of the system, otherwise just on the
first one.

All tasks pushed onto wait queues must be packed into instances of thework_structstructure in which
the following elements are important in the view of the work queue user:

<workqueue.h>
struct work_struct;
typedef void (*work_func_t)(struct work_struct *work);

struct work_struct {
atomic_long_t data;
struct list_head entry;
work_func_t func;
}

entryis used as usual to group severalwork_structinstances in a linked list.funcis a pointer to the
function to be deferred. It is supplied with a pointer to the instance ofwork_structthat was used to
submit the work. This allows the worker function to obtain thedataelement that can point to arbitrary
data associated with thework_struct.

(^19) Another variant,create_freezable_workqueue, is available to create work queues that are friendly toward system hiberna-
tion. Since I do not discuss any mechanisms related to power management, I will also not discuss this alternative any further. Also
note that the prototype of__create_workqueueis simplified and does not contain parameters related to lock depth management
and power management.

Free download pdf