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>
35 #include <linux/lockdep.h>
38 * The per-CPU workqueue (if single thread, we always use the first
41 struct cpu_workqueue_struct {
45 struct list_head worklist;
46 wait_queue_head_t more_work;
47 struct work_struct *current_work;
49 struct workqueue_struct *wq;
50 struct task_struct *thread;
52 int run_depth; /* Detect run_workqueue() recursion depth */
53 } ____cacheline_aligned;
56 * The externally visible workqueue abstraction is an array of
59 struct workqueue_struct {
60 struct cpu_workqueue_struct *cpu_wq;
61 struct list_head list;
64 int freezeable; /* Freeze threads during suspend */
66 struct lockdep_map lockdep_map;
70 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
71 threads to each one as cpus come/go. */
72 static DEFINE_MUTEX(workqueue_mutex);
73 static LIST_HEAD(workqueues);
75 static int singlethread_cpu __read_mostly;
76 static cpumask_t cpu_singlethread_map __read_mostly;
78 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
79 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
80 * which comes in between can't use for_each_online_cpu(). We could
81 * use cpu_possible_map, the cpumask below is more a documentation
84 static cpumask_t cpu_populated_map __read_mostly;
86 /* If it's single threaded, it isn't in the list of workqueues. */
87 static inline int is_single_threaded(struct workqueue_struct *wq)
89 return wq->singlethread;
92 static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
94 return is_single_threaded(wq)
95 ? &cpu_singlethread_map : &cpu_populated_map;
99 struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
101 if (unlikely(is_single_threaded(wq)))
102 cpu = singlethread_cpu;
103 return per_cpu_ptr(wq->cpu_wq, cpu);
107 * Set the workqueue on which a work item is to be run
108 * - Must *only* be called if the pending flag is set
110 static inline void set_wq_data(struct work_struct *work,
111 struct cpu_workqueue_struct *cwq)
115 BUG_ON(!work_pending(work));
117 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
118 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
119 atomic_long_set(&work->data, new);
123 struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
125 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
128 static void insert_work(struct cpu_workqueue_struct *cwq,
129 struct work_struct *work, int tail)
131 set_wq_data(work, cwq);
133 * Ensure that we get the right work->data if we see the
134 * result of list_add() below, see try_to_grab_pending().
138 list_add_tail(&work->entry, &cwq->worklist);
140 list_add(&work->entry, &cwq->worklist);
141 wake_up(&cwq->more_work);
144 /* Preempt must be disabled. */
145 static void __queue_work(struct cpu_workqueue_struct *cwq,
146 struct work_struct *work)
150 spin_lock_irqsave(&cwq->lock, flags);
151 insert_work(cwq, work, 1);
152 spin_unlock_irqrestore(&cwq->lock, flags);
156 * queue_work - queue work on a workqueue
157 * @wq: workqueue to use
158 * @work: work to queue
160 * Returns 0 if @work was already on a queue, non-zero otherwise.
162 * We queue the work to the CPU it was submitted, but there is no
163 * guarantee that it will be processed by that CPU.
165 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
169 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
170 BUG_ON(!list_empty(&work->entry));
171 __queue_work(wq_per_cpu(wq, get_cpu()), work);
177 EXPORT_SYMBOL_GPL(queue_work);
179 void delayed_work_timer_fn(unsigned long __data)
181 struct delayed_work *dwork = (struct delayed_work *)__data;
182 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
183 struct workqueue_struct *wq = cwq->wq;
185 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
189 * queue_delayed_work - queue work on a workqueue after delay
190 * @wq: workqueue to use
191 * @dwork: delayable work to queue
192 * @delay: number of jiffies to wait before queueing
194 * Returns 0 if @work was already on a queue, non-zero otherwise.
196 int fastcall queue_delayed_work(struct workqueue_struct *wq,
197 struct delayed_work *dwork, unsigned long delay)
199 timer_stats_timer_set_start_info(&dwork->timer);
201 return queue_work(wq, &dwork->work);
203 return queue_delayed_work_on(-1, wq, dwork, delay);
205 EXPORT_SYMBOL_GPL(queue_delayed_work);
208 * queue_delayed_work_on - queue work on specific CPU after delay
209 * @cpu: CPU number to execute work on
210 * @wq: workqueue to use
211 * @dwork: work to queue
212 * @delay: number of jiffies to wait before queueing
214 * Returns 0 if @work was already on a queue, non-zero otherwise.
216 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
217 struct delayed_work *dwork, unsigned long delay)
220 struct timer_list *timer = &dwork->timer;
221 struct work_struct *work = &dwork->work;
223 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
224 BUG_ON(timer_pending(timer));
225 BUG_ON(!list_empty(&work->entry));
227 /* This stores cwq for the moment, for the timer_fn */
228 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
229 timer->expires = jiffies + delay;
230 timer->data = (unsigned long)dwork;
231 timer->function = delayed_work_timer_fn;
233 if (unlikely(cpu >= 0))
234 add_timer_on(timer, cpu);
241 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
243 static void run_workqueue(struct cpu_workqueue_struct *cwq)
245 spin_lock_irq(&cwq->lock);
247 if (cwq->run_depth > 3) {
248 /* morton gets to eat his hat */
249 printk("%s: recursion depth exceeded: %d\n",
250 __FUNCTION__, cwq->run_depth);
253 while (!list_empty(&cwq->worklist)) {
254 struct work_struct *work = list_entry(cwq->worklist.next,
255 struct work_struct, entry);
256 work_func_t f = work->func;
257 #ifdef CONFIG_LOCKDEP
259 * It is permissible to free the struct work_struct
260 * from inside the function that is called from it,
261 * this we need to take into account for lockdep too.
262 * To avoid bogus "held lock freed" warnings as well
263 * as problems when looking into work->lockdep_map,
264 * make a copy and use that here.
266 struct lockdep_map lockdep_map = work->lockdep_map;
269 cwq->current_work = work;
270 list_del_init(cwq->worklist.next);
271 spin_unlock_irq(&cwq->lock);
273 BUG_ON(get_wq_data(work) != cwq);
274 work_clear_pending(work);
275 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
276 lock_acquire(&lockdep_map, 0, 0, 0, 2, _THIS_IP_);
278 lock_release(&lockdep_map, 1, _THIS_IP_);
279 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
281 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
282 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
284 current->comm, preempt_count(),
285 task_pid_nr(current));
286 printk(KERN_ERR " last function: ");
287 print_symbol("%s\n", (unsigned long)f);
288 debug_show_held_locks(current);
292 spin_lock_irq(&cwq->lock);
293 cwq->current_work = NULL;
296 spin_unlock_irq(&cwq->lock);
299 static int worker_thread(void *__cwq)
301 struct cpu_workqueue_struct *cwq = __cwq;
304 if (cwq->wq->freezeable)
307 set_user_nice(current, -5);
310 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
311 if (!freezing(current) &&
312 !kthread_should_stop() &&
313 list_empty(&cwq->worklist))
315 finish_wait(&cwq->more_work, &wait);
319 if (kthread_should_stop())
329 struct work_struct work;
330 struct completion done;
333 static void wq_barrier_func(struct work_struct *work)
335 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
336 complete(&barr->done);
339 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
340 struct wq_barrier *barr, int tail)
342 INIT_WORK(&barr->work, wq_barrier_func);
343 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
345 init_completion(&barr->done);
347 insert_work(cwq, &barr->work, tail);
350 static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
354 if (cwq->thread == current) {
356 * Probably keventd trying to flush its own queue. So simply run
357 * it by hand rather than deadlocking.
362 struct wq_barrier barr;
365 spin_lock_irq(&cwq->lock);
366 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
367 insert_wq_barrier(cwq, &barr, 1);
370 spin_unlock_irq(&cwq->lock);
373 wait_for_completion(&barr.done);
380 * flush_workqueue - ensure that any scheduled work has run to completion.
381 * @wq: workqueue to flush
383 * Forces execution of the workqueue and blocks until its completion.
384 * This is typically used in driver shutdown handlers.
386 * We sleep until all works which were queued on entry have been handled,
387 * but we are not livelocked by new incoming ones.
389 * This function used to run the workqueues itself. Now we just wait for the
390 * helper threads to do it.
392 void fastcall flush_workqueue(struct workqueue_struct *wq)
394 const cpumask_t *cpu_map = wq_cpu_map(wq);
398 lock_acquire(&wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
399 lock_release(&wq->lockdep_map, 1, _THIS_IP_);
400 for_each_cpu_mask(cpu, *cpu_map)
401 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
403 EXPORT_SYMBOL_GPL(flush_workqueue);
406 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
407 * so this work can't be re-armed in any way.
409 static int try_to_grab_pending(struct work_struct *work)
411 struct cpu_workqueue_struct *cwq;
414 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
418 * The queueing is in progress, or it is already queued. Try to
419 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
422 cwq = get_wq_data(work);
426 spin_lock_irq(&cwq->lock);
427 if (!list_empty(&work->entry)) {
429 * This work is queued, but perhaps we locked the wrong cwq.
430 * In that case we must see the new value after rmb(), see
431 * insert_work()->wmb().
434 if (cwq == get_wq_data(work)) {
435 list_del_init(&work->entry);
439 spin_unlock_irq(&cwq->lock);
444 static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
445 struct work_struct *work)
447 struct wq_barrier barr;
450 spin_lock_irq(&cwq->lock);
451 if (unlikely(cwq->current_work == work)) {
452 insert_wq_barrier(cwq, &barr, 0);
455 spin_unlock_irq(&cwq->lock);
457 if (unlikely(running))
458 wait_for_completion(&barr.done);
461 static void wait_on_work(struct work_struct *work)
463 struct cpu_workqueue_struct *cwq;
464 struct workqueue_struct *wq;
465 const cpumask_t *cpu_map;
470 lock_acquire(&work->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
471 lock_release(&work->lockdep_map, 1, _THIS_IP_);
473 cwq = get_wq_data(work);
478 cpu_map = wq_cpu_map(wq);
480 for_each_cpu_mask(cpu, *cpu_map)
481 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
484 static int __cancel_work_timer(struct work_struct *work,
485 struct timer_list* timer)
490 ret = (timer && likely(del_timer(timer)));
492 ret = try_to_grab_pending(work);
494 } while (unlikely(ret < 0));
496 work_clear_pending(work);
501 * cancel_work_sync - block until a work_struct's callback has terminated
502 * @work: the work which is to be flushed
504 * Returns true if @work was pending.
506 * cancel_work_sync() will cancel the work if it is queued. If the work's
507 * callback appears to be running, cancel_work_sync() will block until it
510 * It is possible to use this function if the work re-queues itself. It can
511 * cancel the work even if it migrates to another workqueue, however in that
512 * case it only guarantees that work->func() has completed on the last queued
515 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
516 * pending, otherwise it goes into a busy-wait loop until the timer expires.
518 * The caller must ensure that workqueue_struct on which this work was last
519 * queued can't be destroyed before this function returns.
521 int cancel_work_sync(struct work_struct *work)
523 return __cancel_work_timer(work, NULL);
525 EXPORT_SYMBOL_GPL(cancel_work_sync);
528 * cancel_delayed_work_sync - reliably kill off a delayed work.
529 * @dwork: the delayed work struct
531 * Returns true if @dwork was pending.
533 * It is possible to use this function if @dwork rearms itself via queue_work()
534 * or queue_delayed_work(). See also the comment for cancel_work_sync().
536 int cancel_delayed_work_sync(struct delayed_work *dwork)
538 return __cancel_work_timer(&dwork->work, &dwork->timer);
540 EXPORT_SYMBOL(cancel_delayed_work_sync);
542 static struct workqueue_struct *keventd_wq __read_mostly;
545 * schedule_work - put work task in global workqueue
546 * @work: job to be done
548 * This puts a job in the kernel-global workqueue.
550 int fastcall schedule_work(struct work_struct *work)
552 return queue_work(keventd_wq, work);
554 EXPORT_SYMBOL(schedule_work);
557 * schedule_delayed_work - put work task in global workqueue after delay
558 * @dwork: job to be done
559 * @delay: number of jiffies to wait or 0 for immediate execution
561 * After waiting for a given time this puts a job in the kernel-global
564 int fastcall schedule_delayed_work(struct delayed_work *dwork,
567 timer_stats_timer_set_start_info(&dwork->timer);
568 return queue_delayed_work(keventd_wq, dwork, delay);
570 EXPORT_SYMBOL(schedule_delayed_work);
573 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
575 * @dwork: job to be done
576 * @delay: number of jiffies to wait
578 * After waiting for a given time this puts a job in the kernel-global
579 * workqueue on the specified CPU.
581 int schedule_delayed_work_on(int cpu,
582 struct delayed_work *dwork, unsigned long delay)
584 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
586 EXPORT_SYMBOL(schedule_delayed_work_on);
589 * schedule_on_each_cpu - call a function on each online CPU from keventd
590 * @func: the function to call
592 * Returns zero on success.
593 * Returns -ve errno on failure.
595 * Appears to be racy against CPU hotplug.
597 * schedule_on_each_cpu() is very slow.
599 int schedule_on_each_cpu(work_func_t func)
602 struct work_struct *works;
604 works = alloc_percpu(struct work_struct);
608 preempt_disable(); /* CPU hotplug */
609 for_each_online_cpu(cpu) {
610 struct work_struct *work = per_cpu_ptr(works, cpu);
612 INIT_WORK(work, func);
613 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
614 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
617 flush_workqueue(keventd_wq);
622 void flush_scheduled_work(void)
624 flush_workqueue(keventd_wq);
626 EXPORT_SYMBOL(flush_scheduled_work);
629 * execute_in_process_context - reliably execute the routine with user context
630 * @fn: the function to execute
631 * @ew: guaranteed storage for the execute work structure (must
632 * be available when the work executes)
634 * Executes the function immediately if process context is available,
635 * otherwise schedules the function for delayed execution.
637 * Returns: 0 - function was executed
638 * 1 - function was scheduled for execution
640 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
642 if (!in_interrupt()) {
647 INIT_WORK(&ew->work, fn);
648 schedule_work(&ew->work);
652 EXPORT_SYMBOL_GPL(execute_in_process_context);
656 return keventd_wq != NULL;
659 int current_is_keventd(void)
661 struct cpu_workqueue_struct *cwq;
662 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
667 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
668 if (current == cwq->thread)
675 static struct cpu_workqueue_struct *
676 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
678 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
681 spin_lock_init(&cwq->lock);
682 INIT_LIST_HEAD(&cwq->worklist);
683 init_waitqueue_head(&cwq->more_work);
688 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
690 struct workqueue_struct *wq = cwq->wq;
691 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
692 struct task_struct *p;
694 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
696 * Nobody can add the work_struct to this cwq,
697 * if (caller is __create_workqueue)
698 * nobody should see this wq
699 * else // caller is CPU_UP_PREPARE
700 * cpu is not on cpu_online_map
701 * so we can abort safely.
711 static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
713 struct task_struct *p = cwq->thread;
717 kthread_bind(p, cpu);
722 struct workqueue_struct *__create_workqueue_key(const char *name,
725 struct lock_class_key *key,
726 const char *lock_name)
728 struct workqueue_struct *wq;
729 struct cpu_workqueue_struct *cwq;
732 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
736 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
743 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
744 wq->singlethread = singlethread;
745 wq->freezeable = freezeable;
746 INIT_LIST_HEAD(&wq->list);
749 cwq = init_cpu_workqueue(wq, singlethread_cpu);
750 err = create_workqueue_thread(cwq, singlethread_cpu);
751 start_workqueue_thread(cwq, -1);
753 mutex_lock(&workqueue_mutex);
754 list_add(&wq->list, &workqueues);
756 for_each_possible_cpu(cpu) {
757 cwq = init_cpu_workqueue(wq, cpu);
758 if (err || !cpu_online(cpu))
760 err = create_workqueue_thread(cwq, cpu);
761 start_workqueue_thread(cwq, cpu);
763 mutex_unlock(&workqueue_mutex);
767 destroy_workqueue(wq);
772 EXPORT_SYMBOL_GPL(__create_workqueue_key);
774 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
777 * Our caller is either destroy_workqueue() or CPU_DEAD,
778 * workqueue_mutex protects cwq->thread
780 if (cwq->thread == NULL)
783 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
784 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
786 flush_cpu_workqueue(cwq);
788 * If the caller is CPU_DEAD and cwq->worklist was not empty,
789 * a concurrent flush_workqueue() can insert a barrier after us.
790 * However, in that case run_workqueue() won't return and check
791 * kthread_should_stop() until it flushes all work_struct's.
792 * When ->worklist becomes empty it is safe to exit because no
793 * more work_structs can be queued on this cwq: flush_workqueue
794 * checks list_empty(), and a "normal" queue_work() can't use
797 kthread_stop(cwq->thread);
802 * destroy_workqueue - safely terminate a workqueue
803 * @wq: target workqueue
805 * Safely destroy a workqueue. All work currently pending will be done first.
807 void destroy_workqueue(struct workqueue_struct *wq)
809 const cpumask_t *cpu_map = wq_cpu_map(wq);
810 struct cpu_workqueue_struct *cwq;
813 mutex_lock(&workqueue_mutex);
815 mutex_unlock(&workqueue_mutex);
817 for_each_cpu_mask(cpu, *cpu_map) {
818 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
819 cleanup_workqueue_thread(cwq, cpu);
822 free_percpu(wq->cpu_wq);
825 EXPORT_SYMBOL_GPL(destroy_workqueue);
827 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
828 unsigned long action,
831 unsigned int cpu = (unsigned long)hcpu;
832 struct cpu_workqueue_struct *cwq;
833 struct workqueue_struct *wq;
835 action &= ~CPU_TASKS_FROZEN;
838 case CPU_LOCK_ACQUIRE:
839 mutex_lock(&workqueue_mutex);
842 case CPU_LOCK_RELEASE:
843 mutex_unlock(&workqueue_mutex);
847 cpu_set(cpu, cpu_populated_map);
850 list_for_each_entry(wq, &workqueues, list) {
851 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
855 if (!create_workqueue_thread(cwq, cpu))
857 printk(KERN_ERR "workqueue for %i failed\n", cpu);
861 start_workqueue_thread(cwq, cpu);
864 case CPU_UP_CANCELED:
865 start_workqueue_thread(cwq, -1);
867 cleanup_workqueue_thread(cwq, cpu);
875 void __init init_workqueues(void)
877 cpu_populated_map = cpu_online_map;
878 singlethread_cpu = first_cpu(cpu_possible_map);
879 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
880 hotcpu_notifier(workqueue_cpu_callback, 0);
881 keventd_wq = create_workqueue("events");