2 * linux/kernel/workqueue.c
4 * Generic mechanism for defining kernel helper threads for running
5 * arbitrary tasks in process context.
7 * Started by Ingo Molnar, Copyright (C) 2002
9 * Derived from the taskqueue/keventd code by:
11 * David Woodhouse <dwmw2@infradead.org>
12 * Andrew Morton <andrewm@uow.edu.au>
13 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
14 * Theodore Ts'o <tytso@mit.edu>
16 * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/init.h>
23 #include <linux/signal.h>
24 #include <linux/completion.h>
25 #include <linux/workqueue.h>
26 #include <linux/slab.h>
27 #include <linux/cpu.h>
28 #include <linux/notifier.h>
29 #include <linux/kthread.h>
32 * The per-CPU workqueue (if single thread, we always use the first
35 * The sequence counters are for flush_scheduled_work(). It wants to wait
36 * until until all currently-scheduled works are completed, but it doesn't
37 * want to be livelocked by new, incoming ones. So it waits until
38 * remove_sequence is >= the insert_sequence which pertained when
39 * flush_scheduled_work() was called.
41 struct cpu_workqueue_struct
{
45 long remove_sequence
; /* Least-recently added (next to run) */
46 long insert_sequence
; /* Next to add */
48 struct list_head worklist
;
49 wait_queue_head_t more_work
;
50 wait_queue_head_t work_done
;
52 struct workqueue_struct
*wq
;
55 int run_depth
; /* Detect run_workqueue() recursion depth */
56 } ____cacheline_aligned
;
59 * The externally visible workqueue abstraction is an array of
62 struct workqueue_struct
{
63 struct cpu_workqueue_struct
*cpu_wq
;
65 struct list_head list
; /* Empty if single thread */
68 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
69 threads to each one as cpus come/go. */
70 static DEFINE_SPINLOCK(workqueue_lock
);
71 static LIST_HEAD(workqueues
);
73 static int singlethread_cpu
;
75 /* If it's single threaded, it isn't in the list of workqueues. */
76 static inline int is_single_threaded(struct workqueue_struct
*wq
)
78 return list_empty(&wq
->list
);
81 /* Preempt must be disabled. */
82 static void __queue_work(struct cpu_workqueue_struct
*cwq
,
83 struct work_struct
*work
)
87 spin_lock_irqsave(&cwq
->lock
, flags
);
89 list_add_tail(&work
->entry
, &cwq
->worklist
);
90 cwq
->insert_sequence
++;
91 wake_up(&cwq
->more_work
);
92 spin_unlock_irqrestore(&cwq
->lock
, flags
);
96 * Queue work on a workqueue. Return non-zero if it was successfully
99 * We queue the work to the CPU it was submitted, but there is no
100 * guarantee that it will be processed by that CPU.
102 int fastcall
queue_work(struct workqueue_struct
*wq
, struct work_struct
*work
)
104 int ret
= 0, cpu
= get_cpu();
106 if (!test_and_set_bit(0, &work
->pending
)) {
107 if (unlikely(is_single_threaded(wq
)))
108 cpu
= singlethread_cpu
;
109 BUG_ON(!list_empty(&work
->entry
));
110 __queue_work(per_cpu_ptr(wq
->cpu_wq
, cpu
), work
);
117 static void delayed_work_timer_fn(unsigned long __data
)
119 struct work_struct
*work
= (struct work_struct
*)__data
;
120 struct workqueue_struct
*wq
= work
->wq_data
;
121 int cpu
= smp_processor_id();
123 if (unlikely(is_single_threaded(wq
)))
124 cpu
= singlethread_cpu
;
126 __queue_work(per_cpu_ptr(wq
->cpu_wq
, cpu
), work
);
129 int fastcall
queue_delayed_work(struct workqueue_struct
*wq
,
130 struct work_struct
*work
, unsigned long delay
)
133 struct timer_list
*timer
= &work
->timer
;
135 if (!test_and_set_bit(0, &work
->pending
)) {
136 BUG_ON(timer_pending(timer
));
137 BUG_ON(!list_empty(&work
->entry
));
139 /* This stores wq for the moment, for the timer_fn */
141 timer
->expires
= jiffies
+ delay
;
142 timer
->data
= (unsigned long)work
;
143 timer
->function
= delayed_work_timer_fn
;
150 static void run_workqueue(struct cpu_workqueue_struct
*cwq
)
155 * Keep taking off work from the queue until
158 spin_lock_irqsave(&cwq
->lock
, flags
);
160 if (cwq
->run_depth
> 3) {
161 /* morton gets to eat his hat */
162 printk("%s: recursion depth exceeded: %d\n",
163 __FUNCTION__
, cwq
->run_depth
);
166 while (!list_empty(&cwq
->worklist
)) {
167 struct work_struct
*work
= list_entry(cwq
->worklist
.next
,
168 struct work_struct
, entry
);
169 void (*f
) (void *) = work
->func
;
170 void *data
= work
->data
;
172 list_del_init(cwq
->worklist
.next
);
173 spin_unlock_irqrestore(&cwq
->lock
, flags
);
175 BUG_ON(work
->wq_data
!= cwq
);
176 clear_bit(0, &work
->pending
);
179 spin_lock_irqsave(&cwq
->lock
, flags
);
180 cwq
->remove_sequence
++;
181 wake_up(&cwq
->work_done
);
184 spin_unlock_irqrestore(&cwq
->lock
, flags
);
187 static int worker_thread(void *__cwq
)
189 struct cpu_workqueue_struct
*cwq
= __cwq
;
190 DECLARE_WAITQUEUE(wait
, current
);
191 struct k_sigaction sa
;
194 current
->flags
|= PF_NOFREEZE
;
196 set_user_nice(current
, -5);
198 /* Block and flush all signals */
199 sigfillset(&blocked
);
200 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
201 flush_signals(current
);
203 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
204 sa
.sa
.sa_handler
= SIG_IGN
;
206 siginitset(&sa
.sa
.sa_mask
, sigmask(SIGCHLD
));
207 do_sigaction(SIGCHLD
, &sa
, (struct k_sigaction
*)0);
209 set_current_state(TASK_INTERRUPTIBLE
);
210 while (!kthread_should_stop()) {
211 add_wait_queue(&cwq
->more_work
, &wait
);
212 if (list_empty(&cwq
->worklist
))
215 __set_current_state(TASK_RUNNING
);
216 remove_wait_queue(&cwq
->more_work
, &wait
);
218 if (!list_empty(&cwq
->worklist
))
220 set_current_state(TASK_INTERRUPTIBLE
);
222 __set_current_state(TASK_RUNNING
);
226 static void flush_cpu_workqueue(struct cpu_workqueue_struct
*cwq
)
228 if (cwq
->thread
== current
) {
230 * Probably keventd trying to flush its own queue. So simply run
231 * it by hand rather than deadlocking.
236 long sequence_needed
;
238 spin_lock_irq(&cwq
->lock
);
239 sequence_needed
= cwq
->insert_sequence
;
241 while (sequence_needed
- cwq
->remove_sequence
> 0) {
242 prepare_to_wait(&cwq
->work_done
, &wait
,
243 TASK_UNINTERRUPTIBLE
);
244 spin_unlock_irq(&cwq
->lock
);
246 spin_lock_irq(&cwq
->lock
);
248 finish_wait(&cwq
->work_done
, &wait
);
249 spin_unlock_irq(&cwq
->lock
);
254 * flush_workqueue - ensure that any scheduled work has run to completion.
256 * Forces execution of the workqueue and blocks until its completion.
257 * This is typically used in driver shutdown handlers.
259 * This function will sample each workqueue's current insert_sequence number and
260 * will sleep until the head sequence is greater than or equal to that. This
261 * means that we sleep until all works which were queued on entry have been
262 * handled, but we are not livelocked by new incoming ones.
264 * This function used to run the workqueues itself. Now we just wait for the
265 * helper threads to do it.
267 void fastcall
flush_workqueue(struct workqueue_struct
*wq
)
271 if (is_single_threaded(wq
)) {
272 /* Always use first cpu's area. */
273 flush_cpu_workqueue(per_cpu_ptr(wq
->cpu_wq
, singlethread_cpu
));
278 for_each_online_cpu(cpu
)
279 flush_cpu_workqueue(per_cpu_ptr(wq
->cpu_wq
, cpu
));
280 unlock_cpu_hotplug();
284 static struct task_struct
*create_workqueue_thread(struct workqueue_struct
*wq
,
287 struct cpu_workqueue_struct
*cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
288 struct task_struct
*p
;
290 spin_lock_init(&cwq
->lock
);
293 cwq
->insert_sequence
= 0;
294 cwq
->remove_sequence
= 0;
295 INIT_LIST_HEAD(&cwq
->worklist
);
296 init_waitqueue_head(&cwq
->more_work
);
297 init_waitqueue_head(&cwq
->work_done
);
299 if (is_single_threaded(wq
))
300 p
= kthread_create(worker_thread
, cwq
, "%s", wq
->name
);
302 p
= kthread_create(worker_thread
, cwq
, "%s/%d", wq
->name
, cpu
);
309 struct workqueue_struct
*__create_workqueue(const char *name
,
312 int cpu
, destroy
= 0;
313 struct workqueue_struct
*wq
;
314 struct task_struct
*p
;
316 wq
= kzalloc(sizeof(*wq
), GFP_KERNEL
);
320 wq
->cpu_wq
= alloc_percpu(struct cpu_workqueue_struct
);
327 /* We don't need the distraction of CPUs appearing and vanishing. */
330 INIT_LIST_HEAD(&wq
->list
);
331 p
= create_workqueue_thread(wq
, singlethread_cpu
);
337 spin_lock(&workqueue_lock
);
338 list_add(&wq
->list
, &workqueues
);
339 spin_unlock(&workqueue_lock
);
340 for_each_online_cpu(cpu
) {
341 p
= create_workqueue_thread(wq
, cpu
);
343 kthread_bind(p
, cpu
);
349 unlock_cpu_hotplug();
352 * Was there any error during startup? If yes then clean up:
355 destroy_workqueue(wq
);
361 static void cleanup_workqueue_thread(struct workqueue_struct
*wq
, int cpu
)
363 struct cpu_workqueue_struct
*cwq
;
365 struct task_struct
*p
;
367 cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
368 spin_lock_irqsave(&cwq
->lock
, flags
);
371 spin_unlock_irqrestore(&cwq
->lock
, flags
);
376 void destroy_workqueue(struct workqueue_struct
*wq
)
382 /* We don't need the distraction of CPUs appearing and vanishing. */
384 if (is_single_threaded(wq
))
385 cleanup_workqueue_thread(wq
, singlethread_cpu
);
387 for_each_online_cpu(cpu
)
388 cleanup_workqueue_thread(wq
, cpu
);
389 spin_lock(&workqueue_lock
);
391 spin_unlock(&workqueue_lock
);
393 unlock_cpu_hotplug();
394 free_percpu(wq
->cpu_wq
);
398 static struct workqueue_struct
*keventd_wq
;
400 int fastcall
schedule_work(struct work_struct
*work
)
402 return queue_work(keventd_wq
, work
);
405 int fastcall
schedule_delayed_work(struct work_struct
*work
, unsigned long delay
)
407 return queue_delayed_work(keventd_wq
, work
, delay
);
410 int schedule_delayed_work_on(int cpu
,
411 struct work_struct
*work
, unsigned long delay
)
414 struct timer_list
*timer
= &work
->timer
;
416 if (!test_and_set_bit(0, &work
->pending
)) {
417 BUG_ON(timer_pending(timer
));
418 BUG_ON(!list_empty(&work
->entry
));
419 /* This stores keventd_wq for the moment, for the timer_fn */
420 work
->wq_data
= keventd_wq
;
421 timer
->expires
= jiffies
+ delay
;
422 timer
->data
= (unsigned long)work
;
423 timer
->function
= delayed_work_timer_fn
;
424 add_timer_on(timer
, cpu
);
430 int schedule_on_each_cpu(void (*func
) (void *info
), void *info
)
433 struct work_struct
*work
;
435 work
= kmalloc(NR_CPUS
* sizeof(struct work_struct
), GFP_KERNEL
);
439 for_each_online_cpu(cpu
) {
440 INIT_WORK(work
+ cpu
, func
, info
);
441 __queue_work(per_cpu_ptr(keventd_wq
->cpu_wq
, cpu
),
444 flush_workqueue(keventd_wq
);
449 void flush_scheduled_work(void)
451 flush_workqueue(keventd_wq
);
455 * cancel_rearming_delayed_workqueue - reliably kill off a delayed
456 * work whose handler rearms the delayed work.
457 * @wq: the controlling workqueue structure
458 * @work: the delayed work struct
460 void cancel_rearming_delayed_workqueue(struct workqueue_struct
*wq
,
461 struct work_struct
*work
)
463 while (!cancel_delayed_work(work
))
466 EXPORT_SYMBOL(cancel_rearming_delayed_workqueue
);
469 * cancel_rearming_delayed_work - reliably kill off a delayed keventd
470 * work whose handler rearms the delayed work.
471 * @work: the delayed work struct
473 void cancel_rearming_delayed_work(struct work_struct
*work
)
475 cancel_rearming_delayed_workqueue(keventd_wq
, work
);
477 EXPORT_SYMBOL(cancel_rearming_delayed_work
);
481 return keventd_wq
!= NULL
;
484 int current_is_keventd(void)
486 struct cpu_workqueue_struct
*cwq
;
487 int cpu
= smp_processor_id(); /* preempt-safe: keventd is per-cpu */
492 cwq
= per_cpu_ptr(keventd_wq
->cpu_wq
, cpu
);
493 if (current
== cwq
->thread
)
500 #ifdef CONFIG_HOTPLUG_CPU
501 /* Take the work from this (downed) CPU. */
502 static void take_over_work(struct workqueue_struct
*wq
, unsigned int cpu
)
504 struct cpu_workqueue_struct
*cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
506 struct work_struct
*work
;
508 spin_lock_irq(&cwq
->lock
);
509 list_splice_init(&cwq
->worklist
, &list
);
511 while (!list_empty(&list
)) {
512 printk("Taking work for %s\n", wq
->name
);
513 work
= list_entry(list
.next
,struct work_struct
,entry
);
514 list_del(&work
->entry
);
515 __queue_work(per_cpu_ptr(wq
->cpu_wq
, smp_processor_id()), work
);
517 spin_unlock_irq(&cwq
->lock
);
520 /* We're holding the cpucontrol mutex here */
521 static int __devinit
workqueue_cpu_callback(struct notifier_block
*nfb
,
522 unsigned long action
,
525 unsigned int hotcpu
= (unsigned long)hcpu
;
526 struct workqueue_struct
*wq
;
530 /* Create a new workqueue thread for it. */
531 list_for_each_entry(wq
, &workqueues
, list
) {
532 if (!create_workqueue_thread(wq
, hotcpu
)) {
533 printk("workqueue for %i failed\n", hotcpu
);
540 /* Kick off worker threads. */
541 list_for_each_entry(wq
, &workqueues
, list
) {
542 struct cpu_workqueue_struct
*cwq
;
544 cwq
= per_cpu_ptr(wq
->cpu_wq
, hotcpu
);
545 kthread_bind(cwq
->thread
, hotcpu
);
546 wake_up_process(cwq
->thread
);
550 case CPU_UP_CANCELED
:
551 list_for_each_entry(wq
, &workqueues
, list
) {
552 /* Unbind so it can run. */
553 kthread_bind(per_cpu_ptr(wq
->cpu_wq
, hotcpu
)->thread
,
554 any_online_cpu(cpu_online_map
));
555 cleanup_workqueue_thread(wq
, hotcpu
);
560 list_for_each_entry(wq
, &workqueues
, list
)
561 cleanup_workqueue_thread(wq
, hotcpu
);
562 list_for_each_entry(wq
, &workqueues
, list
)
563 take_over_work(wq
, hotcpu
);
571 void init_workqueues(void)
573 singlethread_cpu
= first_cpu(cpu_possible_map
);
574 hotcpu_notifier(workqueue_cpu_callback
, 0);
575 keventd_wq
= create_workqueue("events");
579 EXPORT_SYMBOL_GPL(__create_workqueue
);
580 EXPORT_SYMBOL_GPL(queue_work
);
581 EXPORT_SYMBOL_GPL(queue_delayed_work
);
582 EXPORT_SYMBOL_GPL(flush_workqueue
);
583 EXPORT_SYMBOL_GPL(destroy_workqueue
);
585 EXPORT_SYMBOL(schedule_work
);
586 EXPORT_SYMBOL(schedule_delayed_work
);
587 EXPORT_SYMBOL(schedule_delayed_work_on
);
588 EXPORT_SYMBOL(flush_scheduled_work
);