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>
30 #include <linux/hardirq.h>
31 #include <linux/mempolicy.h>
32 #include <linux/freezer.h>
33 #include <linux/kallsyms.h>
34 #include <linux/debug_locks.h>
37 * The per-CPU workqueue (if single thread, we always use the first
40 struct cpu_workqueue_struct
{
44 struct list_head worklist
;
45 wait_queue_head_t more_work
;
46 struct work_struct
*current_work
;
48 struct workqueue_struct
*wq
;
49 struct task_struct
*thread
;
51 int run_depth
; /* Detect run_workqueue() recursion depth */
52 } ____cacheline_aligned
;
55 * The externally visible workqueue abstraction is an array of
58 struct workqueue_struct
{
59 struct cpu_workqueue_struct
*cpu_wq
;
60 struct list_head list
;
63 int freezeable
; /* Freeze threads during suspend */
66 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
67 threads to each one as cpus come/go. */
68 static DEFINE_MUTEX(workqueue_mutex
);
69 static LIST_HEAD(workqueues
);
71 static int singlethread_cpu __read_mostly
;
72 static cpumask_t cpu_singlethread_map __read_mostly
;
74 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
75 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
76 * which comes in between can't use for_each_online_cpu(). We could
77 * use cpu_possible_map, the cpumask below is more a documentation
80 static cpumask_t cpu_populated_map __read_mostly
;
82 /* If it's single threaded, it isn't in the list of workqueues. */
83 static inline int is_single_threaded(struct workqueue_struct
*wq
)
85 return wq
->singlethread
;
88 static const cpumask_t
*wq_cpu_map(struct workqueue_struct
*wq
)
90 return is_single_threaded(wq
)
91 ? &cpu_singlethread_map
: &cpu_populated_map
;
95 struct cpu_workqueue_struct
*wq_per_cpu(struct workqueue_struct
*wq
, int cpu
)
97 if (unlikely(is_single_threaded(wq
)))
98 cpu
= singlethread_cpu
;
99 return per_cpu_ptr(wq
->cpu_wq
, cpu
);
103 * Set the workqueue on which a work item is to be run
104 * - Must *only* be called if the pending flag is set
106 static inline void set_wq_data(struct work_struct
*work
,
107 struct cpu_workqueue_struct
*cwq
)
111 BUG_ON(!work_pending(work
));
113 new = (unsigned long) cwq
| (1UL << WORK_STRUCT_PENDING
);
114 new |= WORK_STRUCT_FLAG_MASK
& *work_data_bits(work
);
115 atomic_long_set(&work
->data
, new);
119 struct cpu_workqueue_struct
*get_wq_data(struct work_struct
*work
)
121 return (void *) (atomic_long_read(&work
->data
) & WORK_STRUCT_WQ_DATA_MASK
);
124 static void insert_work(struct cpu_workqueue_struct
*cwq
,
125 struct work_struct
*work
, int tail
)
127 set_wq_data(work
, cwq
);
129 * Ensure that we get the right work->data if we see the
130 * result of list_add() below, see try_to_grab_pending().
134 list_add_tail(&work
->entry
, &cwq
->worklist
);
136 list_add(&work
->entry
, &cwq
->worklist
);
137 wake_up(&cwq
->more_work
);
140 /* Preempt must be disabled. */
141 static void __queue_work(struct cpu_workqueue_struct
*cwq
,
142 struct work_struct
*work
)
146 spin_lock_irqsave(&cwq
->lock
, flags
);
147 insert_work(cwq
, work
, 1);
148 spin_unlock_irqrestore(&cwq
->lock
, flags
);
152 * queue_work - queue work on a workqueue
153 * @wq: workqueue to use
154 * @work: work to queue
156 * Returns 0 if @work was already on a queue, non-zero otherwise.
158 * We queue the work to the CPU it was submitted, but there is no
159 * guarantee that it will be processed by that CPU.
161 int fastcall
queue_work(struct workqueue_struct
*wq
, struct work_struct
*work
)
165 if (!test_and_set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
))) {
166 BUG_ON(!list_empty(&work
->entry
));
167 __queue_work(wq_per_cpu(wq
, get_cpu()), work
);
173 EXPORT_SYMBOL_GPL(queue_work
);
175 void delayed_work_timer_fn(unsigned long __data
)
177 struct delayed_work
*dwork
= (struct delayed_work
*)__data
;
178 struct cpu_workqueue_struct
*cwq
= get_wq_data(&dwork
->work
);
179 struct workqueue_struct
*wq
= cwq
->wq
;
181 __queue_work(wq_per_cpu(wq
, smp_processor_id()), &dwork
->work
);
185 * queue_delayed_work - queue work on a workqueue after delay
186 * @wq: workqueue to use
187 * @dwork: delayable work to queue
188 * @delay: number of jiffies to wait before queueing
190 * Returns 0 if @work was already on a queue, non-zero otherwise.
192 int fastcall
queue_delayed_work(struct workqueue_struct
*wq
,
193 struct delayed_work
*dwork
, unsigned long delay
)
195 timer_stats_timer_set_start_info(&dwork
->timer
);
197 return queue_work(wq
, &dwork
->work
);
199 return queue_delayed_work_on(-1, wq
, dwork
, delay
);
201 EXPORT_SYMBOL_GPL(queue_delayed_work
);
204 * queue_delayed_work_on - queue work on specific CPU after delay
205 * @cpu: CPU number to execute work on
206 * @wq: workqueue to use
207 * @dwork: work to queue
208 * @delay: number of jiffies to wait before queueing
210 * Returns 0 if @work was already on a queue, non-zero otherwise.
212 int queue_delayed_work_on(int cpu
, struct workqueue_struct
*wq
,
213 struct delayed_work
*dwork
, unsigned long delay
)
216 struct timer_list
*timer
= &dwork
->timer
;
217 struct work_struct
*work
= &dwork
->work
;
219 if (!test_and_set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
))) {
220 BUG_ON(timer_pending(timer
));
221 BUG_ON(!list_empty(&work
->entry
));
223 /* This stores cwq for the moment, for the timer_fn */
224 set_wq_data(work
, wq_per_cpu(wq
, raw_smp_processor_id()));
225 timer
->expires
= jiffies
+ delay
;
226 timer
->data
= (unsigned long)dwork
;
227 timer
->function
= delayed_work_timer_fn
;
229 if (unlikely(cpu
>= 0))
230 add_timer_on(timer
, cpu
);
237 EXPORT_SYMBOL_GPL(queue_delayed_work_on
);
239 static void run_workqueue(struct cpu_workqueue_struct
*cwq
)
241 spin_lock_irq(&cwq
->lock
);
243 if (cwq
->run_depth
> 3) {
244 /* morton gets to eat his hat */
245 printk("%s: recursion depth exceeded: %d\n",
246 __FUNCTION__
, cwq
->run_depth
);
249 while (!list_empty(&cwq
->worklist
)) {
250 struct work_struct
*work
= list_entry(cwq
->worklist
.next
,
251 struct work_struct
, entry
);
252 work_func_t f
= work
->func
;
254 cwq
->current_work
= work
;
255 list_del_init(cwq
->worklist
.next
);
256 spin_unlock_irq(&cwq
->lock
);
258 BUG_ON(get_wq_data(work
) != cwq
);
259 work_clear_pending(work
);
262 if (unlikely(in_atomic() || lockdep_depth(current
) > 0)) {
263 printk(KERN_ERR
"BUG: workqueue leaked lock or atomic: "
265 current
->comm
, preempt_count(),
267 printk(KERN_ERR
" last function: ");
268 print_symbol("%s\n", (unsigned long)f
);
269 debug_show_held_locks(current
);
273 spin_lock_irq(&cwq
->lock
);
274 cwq
->current_work
= NULL
;
277 spin_unlock_irq(&cwq
->lock
);
280 static int worker_thread(void *__cwq
)
282 struct cpu_workqueue_struct
*cwq
= __cwq
;
285 if (cwq
->wq
->freezeable
)
288 set_user_nice(current
, -5);
291 prepare_to_wait(&cwq
->more_work
, &wait
, TASK_INTERRUPTIBLE
);
292 if (!freezing(current
) &&
293 !kthread_should_stop() &&
294 list_empty(&cwq
->worklist
))
296 finish_wait(&cwq
->more_work
, &wait
);
300 if (kthread_should_stop())
310 struct work_struct work
;
311 struct completion done
;
314 static void wq_barrier_func(struct work_struct
*work
)
316 struct wq_barrier
*barr
= container_of(work
, struct wq_barrier
, work
);
317 complete(&barr
->done
);
320 static void insert_wq_barrier(struct cpu_workqueue_struct
*cwq
,
321 struct wq_barrier
*barr
, int tail
)
323 INIT_WORK(&barr
->work
, wq_barrier_func
);
324 __set_bit(WORK_STRUCT_PENDING
, work_data_bits(&barr
->work
));
326 init_completion(&barr
->done
);
328 insert_work(cwq
, &barr
->work
, tail
);
331 static int flush_cpu_workqueue(struct cpu_workqueue_struct
*cwq
)
335 if (cwq
->thread
== current
) {
337 * Probably keventd trying to flush its own queue. So simply run
338 * it by hand rather than deadlocking.
343 struct wq_barrier barr
;
346 spin_lock_irq(&cwq
->lock
);
347 if (!list_empty(&cwq
->worklist
) || cwq
->current_work
!= NULL
) {
348 insert_wq_barrier(cwq
, &barr
, 1);
351 spin_unlock_irq(&cwq
->lock
);
354 wait_for_completion(&barr
.done
);
361 * flush_workqueue - ensure that any scheduled work has run to completion.
362 * @wq: workqueue to flush
364 * Forces execution of the workqueue and blocks until its completion.
365 * This is typically used in driver shutdown handlers.
367 * We sleep until all works which were queued on entry have been handled,
368 * but we are not livelocked by new incoming ones.
370 * This function used to run the workqueues itself. Now we just wait for the
371 * helper threads to do it.
373 void fastcall
flush_workqueue(struct workqueue_struct
*wq
)
375 const cpumask_t
*cpu_map
= wq_cpu_map(wq
);
379 for_each_cpu_mask(cpu
, *cpu_map
)
380 flush_cpu_workqueue(per_cpu_ptr(wq
->cpu_wq
, cpu
));
382 EXPORT_SYMBOL_GPL(flush_workqueue
);
385 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
386 * so this work can't be re-armed in any way.
388 static int try_to_grab_pending(struct work_struct
*work
)
390 struct cpu_workqueue_struct
*cwq
;
393 if (!test_and_set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
)))
397 * The queueing is in progress, or it is already queued. Try to
398 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
401 cwq
= get_wq_data(work
);
405 spin_lock_irq(&cwq
->lock
);
406 if (!list_empty(&work
->entry
)) {
408 * This work is queued, but perhaps we locked the wrong cwq.
409 * In that case we must see the new value after rmb(), see
410 * insert_work()->wmb().
413 if (cwq
== get_wq_data(work
)) {
414 list_del_init(&work
->entry
);
418 spin_unlock_irq(&cwq
->lock
);
423 static void wait_on_cpu_work(struct cpu_workqueue_struct
*cwq
,
424 struct work_struct
*work
)
426 struct wq_barrier barr
;
429 spin_lock_irq(&cwq
->lock
);
430 if (unlikely(cwq
->current_work
== work
)) {
431 insert_wq_barrier(cwq
, &barr
, 0);
434 spin_unlock_irq(&cwq
->lock
);
436 if (unlikely(running
))
437 wait_for_completion(&barr
.done
);
440 static void wait_on_work(struct work_struct
*work
)
442 struct cpu_workqueue_struct
*cwq
;
443 struct workqueue_struct
*wq
;
444 const cpumask_t
*cpu_map
;
449 cwq
= get_wq_data(work
);
454 cpu_map
= wq_cpu_map(wq
);
456 for_each_cpu_mask(cpu
, *cpu_map
)
457 wait_on_cpu_work(per_cpu_ptr(wq
->cpu_wq
, cpu
), work
);
460 static int __cancel_work_timer(struct work_struct
*work
,
461 struct timer_list
* timer
)
466 ret
= (timer
&& likely(del_timer(timer
)));
468 ret
= try_to_grab_pending(work
);
470 } while (unlikely(ret
< 0));
472 work_clear_pending(work
);
477 * cancel_work_sync - block until a work_struct's callback has terminated
478 * @work: the work which is to be flushed
480 * Returns true if @work was pending.
482 * cancel_work_sync() will cancel the work if it is queued. If the work's
483 * callback appears to be running, cancel_work_sync() will block until it
486 * It is possible to use this function if the work re-queues itself. It can
487 * cancel the work even if it migrates to another workqueue, however in that
488 * case it only guarantees that work->func() has completed on the last queued
491 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
492 * pending, otherwise it goes into a busy-wait loop until the timer expires.
494 * The caller must ensure that workqueue_struct on which this work was last
495 * queued can't be destroyed before this function returns.
497 int cancel_work_sync(struct work_struct
*work
)
499 return __cancel_work_timer(work
, NULL
);
501 EXPORT_SYMBOL_GPL(cancel_work_sync
);
504 * cancel_delayed_work_sync - reliably kill off a delayed work.
505 * @dwork: the delayed work struct
507 * Returns true if @dwork was pending.
509 * It is possible to use this function if @dwork rearms itself via queue_work()
510 * or queue_delayed_work(). See also the comment for cancel_work_sync().
512 int cancel_delayed_work_sync(struct delayed_work
*dwork
)
514 return __cancel_work_timer(&dwork
->work
, &dwork
->timer
);
516 EXPORT_SYMBOL(cancel_delayed_work_sync
);
518 static struct workqueue_struct
*keventd_wq __read_mostly
;
521 * schedule_work - put work task in global workqueue
522 * @work: job to be done
524 * This puts a job in the kernel-global workqueue.
526 int fastcall
schedule_work(struct work_struct
*work
)
528 return queue_work(keventd_wq
, work
);
530 EXPORT_SYMBOL(schedule_work
);
533 * schedule_delayed_work - put work task in global workqueue after delay
534 * @dwork: job to be done
535 * @delay: number of jiffies to wait or 0 for immediate execution
537 * After waiting for a given time this puts a job in the kernel-global
540 int fastcall
schedule_delayed_work(struct delayed_work
*dwork
,
543 timer_stats_timer_set_start_info(&dwork
->timer
);
544 return queue_delayed_work(keventd_wq
, dwork
, delay
);
546 EXPORT_SYMBOL(schedule_delayed_work
);
549 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
551 * @dwork: job to be done
552 * @delay: number of jiffies to wait
554 * After waiting for a given time this puts a job in the kernel-global
555 * workqueue on the specified CPU.
557 int schedule_delayed_work_on(int cpu
,
558 struct delayed_work
*dwork
, unsigned long delay
)
560 return queue_delayed_work_on(cpu
, keventd_wq
, dwork
, delay
);
562 EXPORT_SYMBOL(schedule_delayed_work_on
);
565 * schedule_on_each_cpu - call a function on each online CPU from keventd
566 * @func: the function to call
568 * Returns zero on success.
569 * Returns -ve errno on failure.
571 * Appears to be racy against CPU hotplug.
573 * schedule_on_each_cpu() is very slow.
575 int schedule_on_each_cpu(work_func_t func
)
578 struct work_struct
*works
;
580 works
= alloc_percpu(struct work_struct
);
584 preempt_disable(); /* CPU hotplug */
585 for_each_online_cpu(cpu
) {
586 struct work_struct
*work
= per_cpu_ptr(works
, cpu
);
588 INIT_WORK(work
, func
);
589 set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
));
590 __queue_work(per_cpu_ptr(keventd_wq
->cpu_wq
, cpu
), work
);
593 flush_workqueue(keventd_wq
);
598 void flush_scheduled_work(void)
600 flush_workqueue(keventd_wq
);
602 EXPORT_SYMBOL(flush_scheduled_work
);
605 * execute_in_process_context - reliably execute the routine with user context
606 * @fn: the function to execute
607 * @ew: guaranteed storage for the execute work structure (must
608 * be available when the work executes)
610 * Executes the function immediately if process context is available,
611 * otherwise schedules the function for delayed execution.
613 * Returns: 0 - function was executed
614 * 1 - function was scheduled for execution
616 int execute_in_process_context(work_func_t fn
, struct execute_work
*ew
)
618 if (!in_interrupt()) {
623 INIT_WORK(&ew
->work
, fn
);
624 schedule_work(&ew
->work
);
628 EXPORT_SYMBOL_GPL(execute_in_process_context
);
632 return keventd_wq
!= NULL
;
635 int current_is_keventd(void)
637 struct cpu_workqueue_struct
*cwq
;
638 int cpu
= raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
643 cwq
= per_cpu_ptr(keventd_wq
->cpu_wq
, cpu
);
644 if (current
== cwq
->thread
)
651 static struct cpu_workqueue_struct
*
652 init_cpu_workqueue(struct workqueue_struct
*wq
, int cpu
)
654 struct cpu_workqueue_struct
*cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
657 spin_lock_init(&cwq
->lock
);
658 INIT_LIST_HEAD(&cwq
->worklist
);
659 init_waitqueue_head(&cwq
->more_work
);
664 static int create_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
666 struct workqueue_struct
*wq
= cwq
->wq
;
667 const char *fmt
= is_single_threaded(wq
) ? "%s" : "%s/%d";
668 struct task_struct
*p
;
670 p
= kthread_create(worker_thread
, cwq
, fmt
, wq
->name
, cpu
);
672 * Nobody can add the work_struct to this cwq,
673 * if (caller is __create_workqueue)
674 * nobody should see this wq
675 * else // caller is CPU_UP_PREPARE
676 * cpu is not on cpu_online_map
677 * so we can abort safely.
687 static void start_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
689 struct task_struct
*p
= cwq
->thread
;
693 kthread_bind(p
, cpu
);
698 struct workqueue_struct
*__create_workqueue(const char *name
,
699 int singlethread
, int freezeable
)
701 struct workqueue_struct
*wq
;
702 struct cpu_workqueue_struct
*cwq
;
705 wq
= kzalloc(sizeof(*wq
), GFP_KERNEL
);
709 wq
->cpu_wq
= alloc_percpu(struct cpu_workqueue_struct
);
716 wq
->singlethread
= singlethread
;
717 wq
->freezeable
= freezeable
;
718 INIT_LIST_HEAD(&wq
->list
);
721 cwq
= init_cpu_workqueue(wq
, singlethread_cpu
);
722 err
= create_workqueue_thread(cwq
, singlethread_cpu
);
723 start_workqueue_thread(cwq
, -1);
725 mutex_lock(&workqueue_mutex
);
726 list_add(&wq
->list
, &workqueues
);
728 for_each_possible_cpu(cpu
) {
729 cwq
= init_cpu_workqueue(wq
, cpu
);
730 if (err
|| !cpu_online(cpu
))
732 err
= create_workqueue_thread(cwq
, cpu
);
733 start_workqueue_thread(cwq
, cpu
);
735 mutex_unlock(&workqueue_mutex
);
739 destroy_workqueue(wq
);
744 EXPORT_SYMBOL_GPL(__create_workqueue
);
746 static void cleanup_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
749 * Our caller is either destroy_workqueue() or CPU_DEAD,
750 * workqueue_mutex protects cwq->thread
752 if (cwq
->thread
== NULL
)
755 flush_cpu_workqueue(cwq
);
757 * If the caller is CPU_DEAD and cwq->worklist was not empty,
758 * a concurrent flush_workqueue() can insert a barrier after us.
759 * However, in that case run_workqueue() won't return and check
760 * kthread_should_stop() until it flushes all work_struct's.
761 * When ->worklist becomes empty it is safe to exit because no
762 * more work_structs can be queued on this cwq: flush_workqueue
763 * checks list_empty(), and a "normal" queue_work() can't use
766 kthread_stop(cwq
->thread
);
771 * destroy_workqueue - safely terminate a workqueue
772 * @wq: target workqueue
774 * Safely destroy a workqueue. All work currently pending will be done first.
776 void destroy_workqueue(struct workqueue_struct
*wq
)
778 const cpumask_t
*cpu_map
= wq_cpu_map(wq
);
779 struct cpu_workqueue_struct
*cwq
;
782 mutex_lock(&workqueue_mutex
);
784 mutex_unlock(&workqueue_mutex
);
786 for_each_cpu_mask(cpu
, *cpu_map
) {
787 cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
788 cleanup_workqueue_thread(cwq
, cpu
);
791 free_percpu(wq
->cpu_wq
);
794 EXPORT_SYMBOL_GPL(destroy_workqueue
);
796 static int __devinit
workqueue_cpu_callback(struct notifier_block
*nfb
,
797 unsigned long action
,
800 unsigned int cpu
= (unsigned long)hcpu
;
801 struct cpu_workqueue_struct
*cwq
;
802 struct workqueue_struct
*wq
;
804 action
&= ~CPU_TASKS_FROZEN
;
807 case CPU_LOCK_ACQUIRE
:
808 mutex_lock(&workqueue_mutex
);
811 case CPU_LOCK_RELEASE
:
812 mutex_unlock(&workqueue_mutex
);
816 cpu_set(cpu
, cpu_populated_map
);
819 list_for_each_entry(wq
, &workqueues
, list
) {
820 cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
824 if (!create_workqueue_thread(cwq
, cpu
))
826 printk(KERN_ERR
"workqueue for %i failed\n", cpu
);
830 start_workqueue_thread(cwq
, cpu
);
833 case CPU_UP_CANCELED
:
834 start_workqueue_thread(cwq
, -1);
836 cleanup_workqueue_thread(cwq
, cpu
);
844 void __init
init_workqueues(void)
846 cpu_populated_map
= cpu_online_map
;
847 singlethread_cpu
= first_cpu(cpu_possible_map
);
848 cpu_singlethread_map
= cpumask_of_cpu(singlethread_cpu
);
849 hotcpu_notifier(workqueue_cpu_callback
, 0);
850 keventd_wq
= create_workqueue("events");