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>
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.
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>
35 #include <linux/lockdep.h>
36 #include <linux/idr.h>
39 * Structure fields follow one of the following exclusion rules.
41 * I: Set during initialization and read-only afterwards.
43 * L: cwq->lock protected. Access with cwq->lock held.
45 * F: wq->flush_mutex protected.
47 * W: workqueue_lock protected.
50 struct cpu_workqueue_struct;
53 struct work_struct *current_work; /* L: work being processed */
54 struct task_struct *task; /* I: worker task */
55 struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
56 int id; /* I: worker id */
60 * The per-CPU workqueue (if single thread, we always use the first
61 * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
62 * work_struct->data are used for flags and thus cwqs need to be
63 * aligned at two's power of the number of flag bits.
65 struct cpu_workqueue_struct {
69 struct list_head worklist;
70 wait_queue_head_t more_work;
72 struct worker *worker;
74 struct workqueue_struct *wq; /* I: the owning workqueue */
75 int work_color; /* L: current color */
76 int flush_color; /* L: flushing color */
77 int nr_in_flight[WORK_NR_COLORS];
78 /* L: nr of in_flight works */
82 * Structure used to wait for workqueue flush.
85 struct list_head list; /* F: list of flushers */
86 int flush_color; /* F: flush color waiting for */
87 struct completion done; /* flush completion */
91 * The externally visible workqueue abstraction is an array of
94 struct workqueue_struct {
95 unsigned int flags; /* I: WQ_* flags */
96 struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
97 struct list_head list; /* W: list of all workqueues */
99 struct mutex flush_mutex; /* protects wq flushing */
100 int work_color; /* F: current work color */
101 int flush_color; /* F: current flush color */
102 atomic_t nr_cwqs_to_flush; /* flush in progress */
103 struct wq_flusher *first_flusher; /* F: first flusher */
104 struct list_head flusher_queue; /* F: flush waiters */
105 struct list_head flusher_overflow; /* F: flush overflow list */
107 const char *name; /* I: workqueue name */
108 #ifdef CONFIG_LOCKDEP
109 struct lockdep_map lockdep_map;
113 #ifdef CONFIG_DEBUG_OBJECTS_WORK
115 static struct debug_obj_descr work_debug_descr;
118 * fixup_init is called when:
119 * - an active object is initialized
121 static int work_fixup_init(void *addr, enum debug_obj_state state)
123 struct work_struct *work = addr;
126 case ODEBUG_STATE_ACTIVE:
127 cancel_work_sync(work);
128 debug_object_init(work, &work_debug_descr);
136 * fixup_activate is called when:
137 * - an active object is activated
138 * - an unknown object is activated (might be a statically initialized object)
140 static int work_fixup_activate(void *addr, enum debug_obj_state state)
142 struct work_struct *work = addr;
146 case ODEBUG_STATE_NOTAVAILABLE:
148 * This is not really a fixup. The work struct was
149 * statically initialized. We just make sure that it
150 * is tracked in the object tracker.
152 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
153 debug_object_init(work, &work_debug_descr);
154 debug_object_activate(work, &work_debug_descr);
160 case ODEBUG_STATE_ACTIVE:
169 * fixup_free is called when:
170 * - an active object is freed
172 static int work_fixup_free(void *addr, enum debug_obj_state state)
174 struct work_struct *work = addr;
177 case ODEBUG_STATE_ACTIVE:
178 cancel_work_sync(work);
179 debug_object_free(work, &work_debug_descr);
186 static struct debug_obj_descr work_debug_descr = {
187 .name = "work_struct",
188 .fixup_init = work_fixup_init,
189 .fixup_activate = work_fixup_activate,
190 .fixup_free = work_fixup_free,
193 static inline void debug_work_activate(struct work_struct *work)
195 debug_object_activate(work, &work_debug_descr);
198 static inline void debug_work_deactivate(struct work_struct *work)
200 debug_object_deactivate(work, &work_debug_descr);
203 void __init_work(struct work_struct *work, int onstack)
206 debug_object_init_on_stack(work, &work_debug_descr);
208 debug_object_init(work, &work_debug_descr);
210 EXPORT_SYMBOL_GPL(__init_work);
212 void destroy_work_on_stack(struct work_struct *work)
214 debug_object_free(work, &work_debug_descr);
216 EXPORT_SYMBOL_GPL(destroy_work_on_stack);
219 static inline void debug_work_activate(struct work_struct *work) { }
220 static inline void debug_work_deactivate(struct work_struct *work) { }
223 /* Serializes the accesses to the list of workqueues. */
224 static DEFINE_SPINLOCK(workqueue_lock);
225 static LIST_HEAD(workqueues);
226 static DEFINE_PER_CPU(struct ida, worker_ida);
228 static int worker_thread(void *__worker);
230 static int singlethread_cpu __read_mostly;
232 static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
233 struct workqueue_struct *wq)
235 return per_cpu_ptr(wq->cpu_wq, cpu);
238 static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
239 struct workqueue_struct *wq)
241 if (unlikely(wq->flags & WQ_SINGLE_THREAD))
242 cpu = singlethread_cpu;
243 return get_cwq(cpu, wq);
246 static unsigned int work_color_to_flags(int color)
248 return color << WORK_STRUCT_COLOR_SHIFT;
251 static int get_work_color(struct work_struct *work)
253 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
254 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
257 static int work_next_color(int color)
259 return (color + 1) % WORK_NR_COLORS;
263 * Set the workqueue on which a work item is to be run
264 * - Must *only* be called if the pending flag is set
266 static inline void set_wq_data(struct work_struct *work,
267 struct cpu_workqueue_struct *cwq,
268 unsigned long extra_flags)
270 BUG_ON(!work_pending(work));
272 atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
273 WORK_STRUCT_PENDING | extra_flags);
277 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
279 static inline void clear_wq_data(struct work_struct *work)
281 atomic_long_set(&work->data, work_static(work));
284 static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
286 return (void *)(atomic_long_read(&work->data) &
287 WORK_STRUCT_WQ_DATA_MASK);
291 * insert_work - insert a work into cwq
292 * @cwq: cwq @work belongs to
293 * @work: work to insert
294 * @head: insertion point
295 * @extra_flags: extra WORK_STRUCT_* flags to set
297 * Insert @work into @cwq after @head.
300 * spin_lock_irq(cwq->lock).
302 static void insert_work(struct cpu_workqueue_struct *cwq,
303 struct work_struct *work, struct list_head *head,
304 unsigned int extra_flags)
306 /* we own @work, set data and link */
307 set_wq_data(work, cwq, extra_flags);
310 * Ensure that we get the right work->data if we see the
311 * result of list_add() below, see try_to_grab_pending().
315 list_add_tail(&work->entry, head);
316 wake_up(&cwq->more_work);
319 static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
320 struct work_struct *work)
322 struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
325 debug_work_activate(work);
326 spin_lock_irqsave(&cwq->lock, flags);
327 BUG_ON(!list_empty(&work->entry));
328 cwq->nr_in_flight[cwq->work_color]++;
329 insert_work(cwq, work, &cwq->worklist,
330 work_color_to_flags(cwq->work_color));
331 spin_unlock_irqrestore(&cwq->lock, flags);
335 * queue_work - queue work on a workqueue
336 * @wq: workqueue to use
337 * @work: work to queue
339 * Returns 0 if @work was already on a queue, non-zero otherwise.
341 * We queue the work to the CPU on which it was submitted, but if the CPU dies
342 * it can be processed by another CPU.
344 int queue_work(struct workqueue_struct *wq, struct work_struct *work)
348 ret = queue_work_on(get_cpu(), wq, work);
353 EXPORT_SYMBOL_GPL(queue_work);
356 * queue_work_on - queue work on specific cpu
357 * @cpu: CPU number to execute work on
358 * @wq: workqueue to use
359 * @work: work to queue
361 * Returns 0 if @work was already on a queue, non-zero otherwise.
363 * We queue the work to a specific CPU, the caller must ensure it
367 queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
371 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
372 __queue_work(cpu, wq, work);
377 EXPORT_SYMBOL_GPL(queue_work_on);
379 static void delayed_work_timer_fn(unsigned long __data)
381 struct delayed_work *dwork = (struct delayed_work *)__data;
382 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
384 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
388 * queue_delayed_work - queue work on a workqueue after delay
389 * @wq: workqueue to use
390 * @dwork: delayable work to queue
391 * @delay: number of jiffies to wait before queueing
393 * Returns 0 if @work was already on a queue, non-zero otherwise.
395 int queue_delayed_work(struct workqueue_struct *wq,
396 struct delayed_work *dwork, unsigned long delay)
399 return queue_work(wq, &dwork->work);
401 return queue_delayed_work_on(-1, wq, dwork, delay);
403 EXPORT_SYMBOL_GPL(queue_delayed_work);
406 * queue_delayed_work_on - queue work on specific CPU after delay
407 * @cpu: CPU number to execute work on
408 * @wq: workqueue to use
409 * @dwork: work to queue
410 * @delay: number of jiffies to wait before queueing
412 * Returns 0 if @work was already on a queue, non-zero otherwise.
414 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
415 struct delayed_work *dwork, unsigned long delay)
418 struct timer_list *timer = &dwork->timer;
419 struct work_struct *work = &dwork->work;
421 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
422 BUG_ON(timer_pending(timer));
423 BUG_ON(!list_empty(&work->entry));
425 timer_stats_timer_set_start_info(&dwork->timer);
427 /* This stores cwq for the moment, for the timer_fn */
428 set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
429 timer->expires = jiffies + delay;
430 timer->data = (unsigned long)dwork;
431 timer->function = delayed_work_timer_fn;
433 if (unlikely(cpu >= 0))
434 add_timer_on(timer, cpu);
441 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
443 static struct worker *alloc_worker(void)
445 struct worker *worker;
447 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
452 * create_worker - create a new workqueue worker
453 * @cwq: cwq the new worker will belong to
454 * @bind: whether to set affinity to @cpu or not
456 * Create a new worker which is bound to @cwq. The returned worker
457 * can be started by calling start_worker() or destroyed using
461 * Might sleep. Does GFP_KERNEL allocations.
464 * Pointer to the newly created worker.
466 static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
469 struct worker *worker = NULL;
471 spin_lock(&workqueue_lock);
472 while (ida_get_new(&per_cpu(worker_ida, cwq->cpu), &id)) {
473 spin_unlock(&workqueue_lock);
474 if (!ida_pre_get(&per_cpu(worker_ida, cwq->cpu), GFP_KERNEL))
476 spin_lock(&workqueue_lock);
478 spin_unlock(&workqueue_lock);
480 worker = alloc_worker();
487 worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
489 if (IS_ERR(worker->task))
493 kthread_bind(worker->task, cwq->cpu);
498 spin_lock(&workqueue_lock);
499 ida_remove(&per_cpu(worker_ida, cwq->cpu), id);
500 spin_unlock(&workqueue_lock);
507 * start_worker - start a newly created worker
508 * @worker: worker to start
513 * spin_lock_irq(cwq->lock).
515 static void start_worker(struct worker *worker)
517 wake_up_process(worker->task);
521 * destroy_worker - destroy a workqueue worker
522 * @worker: worker to be destroyed
526 static void destroy_worker(struct worker *worker)
528 int cpu = worker->cwq->cpu;
531 /* sanity check frenzy */
532 BUG_ON(worker->current_work);
534 kthread_stop(worker->task);
537 spin_lock(&workqueue_lock);
538 ida_remove(&per_cpu(worker_ida, cpu), id);
539 spin_unlock(&workqueue_lock);
543 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
544 * @cwq: cwq of interest
545 * @color: color of work which left the queue
547 * A work either has completed or is removed from pending queue,
548 * decrement nr_in_flight of its cwq and handle workqueue flushing.
551 * spin_lock_irq(cwq->lock).
553 static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
555 /* ignore uncolored works */
556 if (color == WORK_NO_COLOR)
559 cwq->nr_in_flight[color]--;
561 /* is flush in progress and are we at the flushing tip? */
562 if (likely(cwq->flush_color != color))
565 /* are there still in-flight works? */
566 if (cwq->nr_in_flight[color])
569 /* this cwq is done, clear flush_color */
570 cwq->flush_color = -1;
573 * If this was the last cwq, wake up the first flusher. It
574 * will handle the rest.
576 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
577 complete(&cwq->wq->first_flusher->done);
581 * process_one_work - process single work
583 * @work: work to process
585 * Process @work. This function contains all the logics necessary to
586 * process a single work including synchronization against and
587 * interaction with other workers on the same cpu, queueing and
588 * flushing. As long as context requirement is met, any worker can
589 * call this function to process a work.
592 * spin_lock_irq(cwq->lock) which is released and regrabbed.
594 static void process_one_work(struct worker *worker, struct work_struct *work)
596 struct cpu_workqueue_struct *cwq = worker->cwq;
597 work_func_t f = work->func;
599 #ifdef CONFIG_LOCKDEP
601 * It is permissible to free the struct work_struct from
602 * inside the function that is called from it, this we need to
603 * take into account for lockdep too. To avoid bogus "held
604 * lock freed" warnings as well as problems when looking into
605 * work->lockdep_map, make a copy and use that here.
607 struct lockdep_map lockdep_map = work->lockdep_map;
609 /* claim and process */
610 debug_work_deactivate(work);
611 worker->current_work = work;
612 work_color = get_work_color(work);
613 list_del_init(&work->entry);
615 spin_unlock_irq(&cwq->lock);
617 BUG_ON(get_wq_data(work) != cwq);
618 work_clear_pending(work);
619 lock_map_acquire(&cwq->wq->lockdep_map);
620 lock_map_acquire(&lockdep_map);
622 lock_map_release(&lockdep_map);
623 lock_map_release(&cwq->wq->lockdep_map);
625 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
626 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
628 current->comm, preempt_count(), task_pid_nr(current));
629 printk(KERN_ERR " last function: ");
630 print_symbol("%s\n", (unsigned long)f);
631 debug_show_held_locks(current);
635 spin_lock_irq(&cwq->lock);
637 /* we're done with it, release */
638 worker->current_work = NULL;
639 cwq_dec_nr_in_flight(cwq, work_color);
642 static void run_workqueue(struct worker *worker)
644 struct cpu_workqueue_struct *cwq = worker->cwq;
646 spin_lock_irq(&cwq->lock);
647 while (!list_empty(&cwq->worklist)) {
648 struct work_struct *work = list_entry(cwq->worklist.next,
649 struct work_struct, entry);
650 process_one_work(worker, work);
652 spin_unlock_irq(&cwq->lock);
656 * worker_thread - the worker thread function
659 * The cwq worker thread function.
661 static int worker_thread(void *__worker)
663 struct worker *worker = __worker;
664 struct cpu_workqueue_struct *cwq = worker->cwq;
667 if (cwq->wq->flags & WQ_FREEZEABLE)
671 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
672 if (!freezing(current) &&
673 !kthread_should_stop() &&
674 list_empty(&cwq->worklist))
676 finish_wait(&cwq->more_work, &wait);
680 if (kthread_should_stop())
683 if (unlikely(!cpumask_equal(&worker->task->cpus_allowed,
684 get_cpu_mask(cwq->cpu))))
685 set_cpus_allowed_ptr(worker->task,
686 get_cpu_mask(cwq->cpu));
687 run_workqueue(worker);
694 struct work_struct work;
695 struct completion done;
698 static void wq_barrier_func(struct work_struct *work)
700 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
701 complete(&barr->done);
705 * insert_wq_barrier - insert a barrier work
706 * @cwq: cwq to insert barrier into
707 * @barr: wq_barrier to insert
708 * @head: insertion point
710 * Insert barrier @barr into @cwq before @head.
713 * spin_lock_irq(cwq->lock).
715 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
716 struct wq_barrier *barr, struct list_head *head)
719 * debugobject calls are safe here even with cwq->lock locked
720 * as we know for sure that this will not trigger any of the
721 * checks and call back into the fixup functions where we
724 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
725 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
726 init_completion(&barr->done);
728 debug_work_activate(&barr->work);
729 insert_work(cwq, &barr->work, head, work_color_to_flags(WORK_NO_COLOR));
733 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
734 * @wq: workqueue being flushed
735 * @flush_color: new flush color, < 0 for no-op
736 * @work_color: new work color, < 0 for no-op
738 * Prepare cwqs for workqueue flushing.
740 * If @flush_color is non-negative, flush_color on all cwqs should be
741 * -1. If no cwq has in-flight commands at the specified color, all
742 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
743 * has in flight commands, its cwq->flush_color is set to
744 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
745 * wakeup logic is armed and %true is returned.
747 * The caller should have initialized @wq->first_flusher prior to
748 * calling this function with non-negative @flush_color. If
749 * @flush_color is negative, no flush color update is done and %false
752 * If @work_color is non-negative, all cwqs should have the same
753 * work_color which is previous to @work_color and all will be
754 * advanced to @work_color.
757 * mutex_lock(wq->flush_mutex).
760 * %true if @flush_color >= 0 and there's something to flush. %false
763 static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
764 int flush_color, int work_color)
769 if (flush_color >= 0) {
770 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
771 atomic_set(&wq->nr_cwqs_to_flush, 1);
774 for_each_possible_cpu(cpu) {
775 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
777 spin_lock_irq(&cwq->lock);
779 if (flush_color >= 0) {
780 BUG_ON(cwq->flush_color != -1);
782 if (cwq->nr_in_flight[flush_color]) {
783 cwq->flush_color = flush_color;
784 atomic_inc(&wq->nr_cwqs_to_flush);
789 if (work_color >= 0) {
790 BUG_ON(work_color != work_next_color(cwq->work_color));
791 cwq->work_color = work_color;
794 spin_unlock_irq(&cwq->lock);
797 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
798 complete(&wq->first_flusher->done);
804 * flush_workqueue - ensure that any scheduled work has run to completion.
805 * @wq: workqueue to flush
807 * Forces execution of the workqueue and blocks until its completion.
808 * This is typically used in driver shutdown handlers.
810 * We sleep until all works which were queued on entry have been handled,
811 * but we are not livelocked by new incoming ones.
813 void flush_workqueue(struct workqueue_struct *wq)
815 struct wq_flusher this_flusher = {
816 .list = LIST_HEAD_INIT(this_flusher.list),
818 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
822 lock_map_acquire(&wq->lockdep_map);
823 lock_map_release(&wq->lockdep_map);
825 mutex_lock(&wq->flush_mutex);
828 * Start-to-wait phase
830 next_color = work_next_color(wq->work_color);
832 if (next_color != wq->flush_color) {
834 * Color space is not full. The current work_color
835 * becomes our flush_color and work_color is advanced
838 BUG_ON(!list_empty(&wq->flusher_overflow));
839 this_flusher.flush_color = wq->work_color;
840 wq->work_color = next_color;
842 if (!wq->first_flusher) {
843 /* no flush in progress, become the first flusher */
844 BUG_ON(wq->flush_color != this_flusher.flush_color);
846 wq->first_flusher = &this_flusher;
848 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
850 /* nothing to flush, done */
851 wq->flush_color = next_color;
852 wq->first_flusher = NULL;
857 BUG_ON(wq->flush_color == this_flusher.flush_color);
858 list_add_tail(&this_flusher.list, &wq->flusher_queue);
859 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
863 * Oops, color space is full, wait on overflow queue.
864 * The next flush completion will assign us
865 * flush_color and transfer to flusher_queue.
867 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
870 mutex_unlock(&wq->flush_mutex);
872 wait_for_completion(&this_flusher.done);
875 * Wake-up-and-cascade phase
877 * First flushers are responsible for cascading flushes and
878 * handling overflow. Non-first flushers can simply return.
880 if (wq->first_flusher != &this_flusher)
883 mutex_lock(&wq->flush_mutex);
885 wq->first_flusher = NULL;
887 BUG_ON(!list_empty(&this_flusher.list));
888 BUG_ON(wq->flush_color != this_flusher.flush_color);
891 struct wq_flusher *next, *tmp;
893 /* complete all the flushers sharing the current flush color */
894 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
895 if (next->flush_color != wq->flush_color)
897 list_del_init(&next->list);
898 complete(&next->done);
901 BUG_ON(!list_empty(&wq->flusher_overflow) &&
902 wq->flush_color != work_next_color(wq->work_color));
904 /* this flush_color is finished, advance by one */
905 wq->flush_color = work_next_color(wq->flush_color);
907 /* one color has been freed, handle overflow queue */
908 if (!list_empty(&wq->flusher_overflow)) {
910 * Assign the same color to all overflowed
911 * flushers, advance work_color and append to
912 * flusher_queue. This is the start-to-wait
913 * phase for these overflowed flushers.
915 list_for_each_entry(tmp, &wq->flusher_overflow, list)
916 tmp->flush_color = wq->work_color;
918 wq->work_color = work_next_color(wq->work_color);
920 list_splice_tail_init(&wq->flusher_overflow,
922 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
925 if (list_empty(&wq->flusher_queue)) {
926 BUG_ON(wq->flush_color != wq->work_color);
931 * Need to flush more colors. Make the next flusher
932 * the new first flusher and arm cwqs.
934 BUG_ON(wq->flush_color == wq->work_color);
935 BUG_ON(wq->flush_color != next->flush_color);
937 list_del_init(&next->list);
938 wq->first_flusher = next;
940 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
944 * Meh... this color is already done, clear first
945 * flusher and repeat cascading.
947 wq->first_flusher = NULL;
951 mutex_unlock(&wq->flush_mutex);
953 EXPORT_SYMBOL_GPL(flush_workqueue);
956 * flush_work - block until a work_struct's callback has terminated
957 * @work: the work which is to be flushed
959 * Returns false if @work has already terminated.
961 * It is expected that, prior to calling flush_work(), the caller has
962 * arranged for the work to not be requeued, otherwise it doesn't make
963 * sense to use this function.
965 int flush_work(struct work_struct *work)
967 struct cpu_workqueue_struct *cwq;
968 struct list_head *prev;
969 struct wq_barrier barr;
972 cwq = get_wq_data(work);
976 lock_map_acquire(&cwq->wq->lockdep_map);
977 lock_map_release(&cwq->wq->lockdep_map);
979 spin_lock_irq(&cwq->lock);
980 if (!list_empty(&work->entry)) {
982 * See the comment near try_to_grab_pending()->smp_rmb().
983 * If it was re-queued under us we are not going to wait.
986 if (unlikely(cwq != get_wq_data(work)))
990 if (!cwq->worker || cwq->worker->current_work != work)
992 prev = &cwq->worklist;
994 insert_wq_barrier(cwq, &barr, prev->next);
996 spin_unlock_irq(&cwq->lock);
997 wait_for_completion(&barr.done);
998 destroy_work_on_stack(&barr.work);
1001 spin_unlock_irq(&cwq->lock);
1004 EXPORT_SYMBOL_GPL(flush_work);
1007 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
1008 * so this work can't be re-armed in any way.
1010 static int try_to_grab_pending(struct work_struct *work)
1012 struct cpu_workqueue_struct *cwq;
1015 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1019 * The queueing is in progress, or it is already queued. Try to
1020 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1023 cwq = get_wq_data(work);
1027 spin_lock_irq(&cwq->lock);
1028 if (!list_empty(&work->entry)) {
1030 * This work is queued, but perhaps we locked the wrong cwq.
1031 * In that case we must see the new value after rmb(), see
1032 * insert_work()->wmb().
1035 if (cwq == get_wq_data(work)) {
1036 debug_work_deactivate(work);
1037 list_del_init(&work->entry);
1038 cwq_dec_nr_in_flight(cwq, get_work_color(work));
1042 spin_unlock_irq(&cwq->lock);
1047 static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
1048 struct work_struct *work)
1050 struct wq_barrier barr;
1053 spin_lock_irq(&cwq->lock);
1054 if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
1055 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
1058 spin_unlock_irq(&cwq->lock);
1060 if (unlikely(running)) {
1061 wait_for_completion(&barr.done);
1062 destroy_work_on_stack(&barr.work);
1066 static void wait_on_work(struct work_struct *work)
1068 struct cpu_workqueue_struct *cwq;
1069 struct workqueue_struct *wq;
1074 lock_map_acquire(&work->lockdep_map);
1075 lock_map_release(&work->lockdep_map);
1077 cwq = get_wq_data(work);
1083 for_each_possible_cpu(cpu)
1084 wait_on_cpu_work(get_cwq(cpu, wq), work);
1087 static int __cancel_work_timer(struct work_struct *work,
1088 struct timer_list* timer)
1093 ret = (timer && likely(del_timer(timer)));
1095 ret = try_to_grab_pending(work);
1097 } while (unlikely(ret < 0));
1099 clear_wq_data(work);
1104 * cancel_work_sync - block until a work_struct's callback has terminated
1105 * @work: the work which is to be flushed
1107 * Returns true if @work was pending.
1109 * cancel_work_sync() will cancel the work if it is queued. If the work's
1110 * callback appears to be running, cancel_work_sync() will block until it
1113 * It is possible to use this function if the work re-queues itself. It can
1114 * cancel the work even if it migrates to another workqueue, however in that
1115 * case it only guarantees that work->func() has completed on the last queued
1118 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
1119 * pending, otherwise it goes into a busy-wait loop until the timer expires.
1121 * The caller must ensure that workqueue_struct on which this work was last
1122 * queued can't be destroyed before this function returns.
1124 int cancel_work_sync(struct work_struct *work)
1126 return __cancel_work_timer(work, NULL);
1128 EXPORT_SYMBOL_GPL(cancel_work_sync);
1131 * cancel_delayed_work_sync - reliably kill off a delayed work.
1132 * @dwork: the delayed work struct
1134 * Returns true if @dwork was pending.
1136 * It is possible to use this function if @dwork rearms itself via queue_work()
1137 * or queue_delayed_work(). See also the comment for cancel_work_sync().
1139 int cancel_delayed_work_sync(struct delayed_work *dwork)
1141 return __cancel_work_timer(&dwork->work, &dwork->timer);
1143 EXPORT_SYMBOL(cancel_delayed_work_sync);
1145 static struct workqueue_struct *keventd_wq __read_mostly;
1148 * schedule_work - put work task in global workqueue
1149 * @work: job to be done
1151 * Returns zero if @work was already on the kernel-global workqueue and
1152 * non-zero otherwise.
1154 * This puts a job in the kernel-global workqueue if it was not already
1155 * queued and leaves it in the same position on the kernel-global
1156 * workqueue otherwise.
1158 int schedule_work(struct work_struct *work)
1160 return queue_work(keventd_wq, work);
1162 EXPORT_SYMBOL(schedule_work);
1165 * schedule_work_on - put work task on a specific cpu
1166 * @cpu: cpu to put the work task on
1167 * @work: job to be done
1169 * This puts a job on a specific cpu
1171 int schedule_work_on(int cpu, struct work_struct *work)
1173 return queue_work_on(cpu, keventd_wq, work);
1175 EXPORT_SYMBOL(schedule_work_on);
1178 * schedule_delayed_work - put work task in global workqueue after delay
1179 * @dwork: job to be done
1180 * @delay: number of jiffies to wait or 0 for immediate execution
1182 * After waiting for a given time this puts a job in the kernel-global
1185 int schedule_delayed_work(struct delayed_work *dwork,
1186 unsigned long delay)
1188 return queue_delayed_work(keventd_wq, dwork, delay);
1190 EXPORT_SYMBOL(schedule_delayed_work);
1193 * flush_delayed_work - block until a dwork_struct's callback has terminated
1194 * @dwork: the delayed work which is to be flushed
1196 * Any timeout is cancelled, and any pending work is run immediately.
1198 void flush_delayed_work(struct delayed_work *dwork)
1200 if (del_timer_sync(&dwork->timer)) {
1201 __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
1205 flush_work(&dwork->work);
1207 EXPORT_SYMBOL(flush_delayed_work);
1210 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
1212 * @dwork: job to be done
1213 * @delay: number of jiffies to wait
1215 * After waiting for a given time this puts a job in the kernel-global
1216 * workqueue on the specified CPU.
1218 int schedule_delayed_work_on(int cpu,
1219 struct delayed_work *dwork, unsigned long delay)
1221 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
1223 EXPORT_SYMBOL(schedule_delayed_work_on);
1226 * schedule_on_each_cpu - call a function on each online CPU from keventd
1227 * @func: the function to call
1229 * Returns zero on success.
1230 * Returns -ve errno on failure.
1232 * schedule_on_each_cpu() is very slow.
1234 int schedule_on_each_cpu(work_func_t func)
1238 struct work_struct *works;
1240 works = alloc_percpu(struct work_struct);
1247 * When running in keventd don't schedule a work item on
1248 * itself. Can just call directly because the work queue is
1249 * already bound. This also is faster.
1251 if (current_is_keventd())
1252 orig = raw_smp_processor_id();
1254 for_each_online_cpu(cpu) {
1255 struct work_struct *work = per_cpu_ptr(works, cpu);
1257 INIT_WORK(work, func);
1259 schedule_work_on(cpu, work);
1262 func(per_cpu_ptr(works, orig));
1264 for_each_online_cpu(cpu)
1265 flush_work(per_cpu_ptr(works, cpu));
1273 * flush_scheduled_work - ensure that any scheduled work has run to completion.
1275 * Forces execution of the kernel-global workqueue and blocks until its
1278 * Think twice before calling this function! It's very easy to get into
1279 * trouble if you don't take great care. Either of the following situations
1280 * will lead to deadlock:
1282 * One of the work items currently on the workqueue needs to acquire
1283 * a lock held by your code or its caller.
1285 * Your code is running in the context of a work routine.
1287 * They will be detected by lockdep when they occur, but the first might not
1288 * occur very often. It depends on what work items are on the workqueue and
1289 * what locks they need, which you have no control over.
1291 * In most situations flushing the entire workqueue is overkill; you merely
1292 * need to know that a particular work item isn't queued and isn't running.
1293 * In such cases you should use cancel_delayed_work_sync() or
1294 * cancel_work_sync() instead.
1296 void flush_scheduled_work(void)
1298 flush_workqueue(keventd_wq);
1300 EXPORT_SYMBOL(flush_scheduled_work);
1303 * execute_in_process_context - reliably execute the routine with user context
1304 * @fn: the function to execute
1305 * @ew: guaranteed storage for the execute work structure (must
1306 * be available when the work executes)
1308 * Executes the function immediately if process context is available,
1309 * otherwise schedules the function for delayed execution.
1311 * Returns: 0 - function was executed
1312 * 1 - function was scheduled for execution
1314 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1316 if (!in_interrupt()) {
1321 INIT_WORK(&ew->work, fn);
1322 schedule_work(&ew->work);
1326 EXPORT_SYMBOL_GPL(execute_in_process_context);
1328 int keventd_up(void)
1330 return keventd_wq != NULL;
1333 int current_is_keventd(void)
1335 struct cpu_workqueue_struct *cwq;
1336 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
1339 BUG_ON(!keventd_wq);
1341 cwq = get_cwq(cpu, keventd_wq);
1342 if (current == cwq->worker->task)
1349 static struct cpu_workqueue_struct *alloc_cwqs(void)
1352 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
1353 * Make sure that the alignment isn't lower than that of
1354 * unsigned long long.
1356 const size_t size = sizeof(struct cpu_workqueue_struct);
1357 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
1358 __alignof__(unsigned long long));
1359 struct cpu_workqueue_struct *cwqs;
1364 * On UP, percpu allocator doesn't honor alignment parameter
1365 * and simply uses arch-dependent default. Allocate enough
1366 * room to align cwq and put an extra pointer at the end
1367 * pointing back to the originally allocated pointer which
1368 * will be used for free.
1370 * FIXME: This really belongs to UP percpu code. Update UP
1371 * percpu code to honor alignment and remove this ugliness.
1373 ptr = __alloc_percpu(size + align + sizeof(void *), 1);
1374 cwqs = PTR_ALIGN(ptr, align);
1375 *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
1377 /* On SMP, percpu allocator can do it itself */
1378 cwqs = __alloc_percpu(size, align);
1380 /* just in case, make sure it's actually aligned */
1381 BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
1385 static void free_cwqs(struct cpu_workqueue_struct *cwqs)
1388 /* on UP, the pointer to free is stored right after the cwq */
1390 free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
1396 struct workqueue_struct *__create_workqueue_key(const char *name,
1398 struct lock_class_key *key,
1399 const char *lock_name)
1401 bool singlethread = flags & WQ_SINGLE_THREAD;
1402 struct workqueue_struct *wq;
1403 bool failed = false;
1406 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1410 wq->cpu_wq = alloc_cwqs();
1415 mutex_init(&wq->flush_mutex);
1416 atomic_set(&wq->nr_cwqs_to_flush, 0);
1417 INIT_LIST_HEAD(&wq->flusher_queue);
1418 INIT_LIST_HEAD(&wq->flusher_overflow);
1420 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
1421 INIT_LIST_HEAD(&wq->list);
1423 cpu_maps_update_begin();
1425 * We must initialize cwqs for each possible cpu even if we
1426 * are going to call destroy_workqueue() finally. Otherwise
1427 * cpu_up() can hit the uninitialized cwq once we drop the
1430 for_each_possible_cpu(cpu) {
1431 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1433 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
1436 cwq->flush_color = -1;
1437 spin_lock_init(&cwq->lock);
1438 INIT_LIST_HEAD(&cwq->worklist);
1439 init_waitqueue_head(&cwq->more_work);
1443 cwq->worker = create_worker(cwq,
1444 cpu_online(cpu) && !singlethread);
1446 start_worker(cwq->worker);
1451 spin_lock(&workqueue_lock);
1452 list_add(&wq->list, &workqueues);
1453 spin_unlock(&workqueue_lock);
1455 cpu_maps_update_done();
1458 destroy_workqueue(wq);
1464 free_cwqs(wq->cpu_wq);
1469 EXPORT_SYMBOL_GPL(__create_workqueue_key);
1472 * destroy_workqueue - safely terminate a workqueue
1473 * @wq: target workqueue
1475 * Safely destroy a workqueue. All work currently pending will be done first.
1477 void destroy_workqueue(struct workqueue_struct *wq)
1481 cpu_maps_update_begin();
1482 spin_lock(&workqueue_lock);
1483 list_del(&wq->list);
1484 spin_unlock(&workqueue_lock);
1485 cpu_maps_update_done();
1487 flush_workqueue(wq);
1489 for_each_possible_cpu(cpu) {
1490 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1494 destroy_worker(cwq->worker);
1498 for (i = 0; i < WORK_NR_COLORS; i++)
1499 BUG_ON(cwq->nr_in_flight[i]);
1502 free_cwqs(wq->cpu_wq);
1505 EXPORT_SYMBOL_GPL(destroy_workqueue);
1507 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1508 unsigned long action,
1511 unsigned int cpu = (unsigned long)hcpu;
1512 struct cpu_workqueue_struct *cwq;
1513 struct workqueue_struct *wq;
1515 action &= ~CPU_TASKS_FROZEN;
1517 list_for_each_entry(wq, &workqueues, list) {
1518 if (wq->flags & WQ_SINGLE_THREAD)
1521 cwq = get_cwq(cpu, wq);
1525 flush_workqueue(wq);
1530 return notifier_from_errno(0);
1535 struct work_for_cpu {
1536 struct completion completion;
1542 static int do_work_for_cpu(void *_wfc)
1544 struct work_for_cpu *wfc = _wfc;
1545 wfc->ret = wfc->fn(wfc->arg);
1546 complete(&wfc->completion);
1551 * work_on_cpu - run a function in user context on a particular cpu
1552 * @cpu: the cpu to run on
1553 * @fn: the function to run
1554 * @arg: the function arg
1556 * This will return the value @fn returns.
1557 * It is up to the caller to ensure that the cpu doesn't go offline.
1558 * The caller must not hold any locks which would prevent @fn from completing.
1560 long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1562 struct task_struct *sub_thread;
1563 struct work_for_cpu wfc = {
1564 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
1569 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
1570 if (IS_ERR(sub_thread))
1571 return PTR_ERR(sub_thread);
1572 kthread_bind(sub_thread, cpu);
1573 wake_up_process(sub_thread);
1574 wait_for_completion(&wfc.completion);
1577 EXPORT_SYMBOL_GPL(work_on_cpu);
1578 #endif /* CONFIG_SMP */
1580 void __init init_workqueues(void)
1584 for_each_possible_cpu(cpu)
1585 ida_init(&per_cpu(worker_ida, cpu));
1587 singlethread_cpu = cpumask_first(cpu_possible_mask);
1588 hotcpu_notifier(workqueue_cpu_callback, 0);
1589 keventd_wq = create_workqueue("events");
1590 BUG_ON(!keventd_wq);