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;
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 */
67 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
68 threads to each one as cpus come/go. */
69 static DEFINE_MUTEX(workqueue_mutex);
70 static LIST_HEAD(workqueues);
72 static int singlethread_cpu __read_mostly;
73 static cpumask_t cpu_singlethread_map __read_mostly;
74 /* optimization, we could use cpu_possible_map */
75 static cpumask_t cpu_populated_map __read_mostly;
77 /* If it's single threaded, it isn't in the list of workqueues. */
78 static inline int is_single_threaded(struct workqueue_struct *wq)
80 return wq->singlethread;
83 static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
85 return is_single_threaded(wq)
86 ? &cpu_singlethread_map : &cpu_populated_map;
90 struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
92 if (unlikely(is_single_threaded(wq)))
93 cpu = singlethread_cpu;
94 return per_cpu_ptr(wq->cpu_wq, cpu);
98 * Set the workqueue on which a work item is to be run
99 * - Must *only* be called if the pending flag is set
101 static inline void set_wq_data(struct work_struct *work,
102 struct cpu_workqueue_struct *cwq)
106 BUG_ON(!work_pending(work));
108 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
109 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
110 atomic_long_set(&work->data, new);
114 struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
116 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
119 static void insert_work(struct cpu_workqueue_struct *cwq,
120 struct work_struct *work, int tail)
122 set_wq_data(work, cwq);
124 list_add_tail(&work->entry, &cwq->worklist);
126 list_add(&work->entry, &cwq->worklist);
127 wake_up(&cwq->more_work);
130 /* Preempt must be disabled. */
131 static void __queue_work(struct cpu_workqueue_struct *cwq,
132 struct work_struct *work)
136 spin_lock_irqsave(&cwq->lock, flags);
137 insert_work(cwq, work, 1);
138 spin_unlock_irqrestore(&cwq->lock, flags);
142 * queue_work - queue work on a workqueue
143 * @wq: workqueue to use
144 * @work: work to queue
146 * Returns 0 if @work was already on a queue, non-zero otherwise.
148 * We queue the work to the CPU it was submitted, but there is no
149 * guarantee that it will be processed by that CPU.
151 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
155 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
156 BUG_ON(!list_empty(&work->entry));
157 __queue_work(wq_per_cpu(wq, get_cpu()), work);
163 EXPORT_SYMBOL_GPL(queue_work);
165 void delayed_work_timer_fn(unsigned long __data)
167 struct delayed_work *dwork = (struct delayed_work *)__data;
168 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
169 struct workqueue_struct *wq = cwq->wq;
171 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
175 * queue_delayed_work - queue work on a workqueue after delay
176 * @wq: workqueue to use
177 * @dwork: delayable work to queue
178 * @delay: number of jiffies to wait before queueing
180 * Returns 0 if @work was already on a queue, non-zero otherwise.
182 int fastcall queue_delayed_work(struct workqueue_struct *wq,
183 struct delayed_work *dwork, unsigned long delay)
185 timer_stats_timer_set_start_info(&dwork->timer);
187 return queue_work(wq, &dwork->work);
189 return queue_delayed_work_on(-1, wq, dwork, delay);
191 EXPORT_SYMBOL_GPL(queue_delayed_work);
194 * queue_delayed_work_on - queue work on specific CPU after delay
195 * @cpu: CPU number to execute work on
196 * @wq: workqueue to use
197 * @dwork: work to queue
198 * @delay: number of jiffies to wait before queueing
200 * Returns 0 if @work was already on a queue, non-zero otherwise.
202 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
203 struct delayed_work *dwork, unsigned long delay)
206 struct timer_list *timer = &dwork->timer;
207 struct work_struct *work = &dwork->work;
209 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
210 BUG_ON(timer_pending(timer));
211 BUG_ON(!list_empty(&work->entry));
213 /* This stores cwq for the moment, for the timer_fn */
214 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
215 timer->expires = jiffies + delay;
216 timer->data = (unsigned long)dwork;
217 timer->function = delayed_work_timer_fn;
219 if (unlikely(cpu >= 0))
220 add_timer_on(timer, cpu);
227 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
229 static void run_workqueue(struct cpu_workqueue_struct *cwq)
231 spin_lock_irq(&cwq->lock);
233 if (cwq->run_depth > 3) {
234 /* morton gets to eat his hat */
235 printk("%s: recursion depth exceeded: %d\n",
236 __FUNCTION__, cwq->run_depth);
239 while (!list_empty(&cwq->worklist)) {
240 struct work_struct *work = list_entry(cwq->worklist.next,
241 struct work_struct, entry);
242 work_func_t f = work->func;
244 cwq->current_work = work;
245 list_del_init(cwq->worklist.next);
246 spin_unlock_irq(&cwq->lock);
248 BUG_ON(get_wq_data(work) != cwq);
249 work_clear_pending(work);
252 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
253 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
255 current->comm, preempt_count(),
257 printk(KERN_ERR " last function: ");
258 print_symbol("%s\n", (unsigned long)f);
259 debug_show_held_locks(current);
263 spin_lock_irq(&cwq->lock);
264 cwq->current_work = NULL;
267 spin_unlock_irq(&cwq->lock);
271 * NOTE: the caller must not touch *cwq if this func returns true
273 static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
275 int should_stop = cwq->should_stop;
277 if (unlikely(should_stop)) {
278 spin_lock_irq(&cwq->lock);
279 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
282 spin_unlock_irq(&cwq->lock);
288 static int worker_thread(void *__cwq)
290 struct cpu_workqueue_struct *cwq = __cwq;
292 struct k_sigaction sa;
294 if (!cwq->wq->freezeable)
295 current->flags |= PF_NOFREEZE;
297 set_user_nice(current, -5);
299 * We inherited MPOL_INTERLEAVE from the booting kernel.
300 * Set MPOL_DEFAULT to insure node local allocations.
302 numa_default_policy();
304 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
305 sa.sa.sa_handler = SIG_IGN;
307 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
308 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
311 if (cwq->wq->freezeable)
314 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
315 if (!cwq->should_stop && list_empty(&cwq->worklist))
317 finish_wait(&cwq->more_work, &wait);
319 if (cwq_should_stop(cwq))
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 void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
352 if (cwq->thread == current) {
354 * Probably keventd trying to flush its own queue. So simply run
355 * it by hand rather than deadlocking.
359 struct wq_barrier barr;
362 spin_lock_irq(&cwq->lock);
363 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
364 insert_wq_barrier(cwq, &barr, 1);
367 spin_unlock_irq(&cwq->lock);
370 wait_for_completion(&barr.done);
375 * flush_workqueue - ensure that any scheduled work has run to completion.
376 * @wq: workqueue to flush
378 * Forces execution of the workqueue and blocks until its completion.
379 * This is typically used in driver shutdown handlers.
381 * We sleep until all works which were queued on entry have been handled,
382 * but we are not livelocked by new incoming ones.
384 * This function used to run the workqueues itself. Now we just wait for the
385 * helper threads to do it.
387 void fastcall flush_workqueue(struct workqueue_struct *wq)
389 const cpumask_t *cpu_map = wq_cpu_map(wq);
393 for_each_cpu_mask(cpu, *cpu_map)
394 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
396 EXPORT_SYMBOL_GPL(flush_workqueue);
398 static void wait_on_work(struct cpu_workqueue_struct *cwq,
399 struct work_struct *work)
401 struct wq_barrier barr;
404 spin_lock_irq(&cwq->lock);
405 if (unlikely(cwq->current_work == work)) {
406 insert_wq_barrier(cwq, &barr, 0);
409 spin_unlock_irq(&cwq->lock);
411 if (unlikely(running))
412 wait_for_completion(&barr.done);
416 * flush_work - block until a work_struct's callback has terminated
417 * @wq: the workqueue on which the work is queued
418 * @work: the work which is to be flushed
420 * flush_work() will attempt to cancel the work if it is queued. If the work's
421 * callback appears to be running, flush_work() will block until it has
424 * flush_work() is designed to be used when the caller is tearing down data
425 * structures which the callback function operates upon. It is expected that,
426 * prior to calling flush_work(), the caller has arranged for the work to not
429 void flush_work(struct workqueue_struct *wq, struct work_struct *work)
431 const cpumask_t *cpu_map = wq_cpu_map(wq);
432 struct cpu_workqueue_struct *cwq;
437 cwq = get_wq_data(work);
438 /* Was it ever queued ? */
443 * This work can't be re-queued, no need to re-check that
444 * get_wq_data() is still the same when we take cwq->lock.
446 spin_lock_irq(&cwq->lock);
447 list_del_init(&work->entry);
448 work_clear_pending(work);
449 spin_unlock_irq(&cwq->lock);
451 for_each_cpu_mask(cpu, *cpu_map)
452 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
454 EXPORT_SYMBOL_GPL(flush_work);
457 static struct workqueue_struct *keventd_wq;
460 * schedule_work - put work task in global workqueue
461 * @work: job to be done
463 * This puts a job in the kernel-global workqueue.
465 int fastcall schedule_work(struct work_struct *work)
467 return queue_work(keventd_wq, work);
469 EXPORT_SYMBOL(schedule_work);
472 * schedule_delayed_work - put work task in global workqueue after delay
473 * @dwork: job to be done
474 * @delay: number of jiffies to wait or 0 for immediate execution
476 * After waiting for a given time this puts a job in the kernel-global
479 int fastcall schedule_delayed_work(struct delayed_work *dwork,
482 timer_stats_timer_set_start_info(&dwork->timer);
483 return queue_delayed_work(keventd_wq, dwork, delay);
485 EXPORT_SYMBOL(schedule_delayed_work);
488 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
490 * @dwork: job to be done
491 * @delay: number of jiffies to wait
493 * After waiting for a given time this puts a job in the kernel-global
494 * workqueue on the specified CPU.
496 int schedule_delayed_work_on(int cpu,
497 struct delayed_work *dwork, unsigned long delay)
499 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
501 EXPORT_SYMBOL(schedule_delayed_work_on);
504 * schedule_on_each_cpu - call a function on each online CPU from keventd
505 * @func: the function to call
507 * Returns zero on success.
508 * Returns -ve errno on failure.
510 * Appears to be racy against CPU hotplug.
512 * schedule_on_each_cpu() is very slow.
514 int schedule_on_each_cpu(work_func_t func)
517 struct work_struct *works;
519 works = alloc_percpu(struct work_struct);
523 preempt_disable(); /* CPU hotplug */
524 for_each_online_cpu(cpu) {
525 struct work_struct *work = per_cpu_ptr(works, cpu);
527 INIT_WORK(work, func);
528 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
529 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
532 flush_workqueue(keventd_wq);
537 void flush_scheduled_work(void)
539 flush_workqueue(keventd_wq);
541 EXPORT_SYMBOL(flush_scheduled_work);
543 void flush_work_keventd(struct work_struct *work)
545 flush_work(keventd_wq, work);
547 EXPORT_SYMBOL(flush_work_keventd);
550 * cancel_rearming_delayed_work - kill off a delayed work whose handler rearms the delayed work.
551 * @dwork: the delayed work struct
553 * Note that the work callback function may still be running on return from
554 * cancel_delayed_work(). Run flush_workqueue() or flush_work() to wait on it.
556 void cancel_rearming_delayed_work(struct delayed_work *dwork)
558 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
560 /* Was it ever queued ? */
562 struct workqueue_struct *wq = cwq->wq;
564 while (!cancel_delayed_work(dwork))
568 EXPORT_SYMBOL(cancel_rearming_delayed_work);
571 * execute_in_process_context - reliably execute the routine with user context
572 * @fn: the function to execute
573 * @ew: guaranteed storage for the execute work structure (must
574 * be available when the work executes)
576 * Executes the function immediately if process context is available,
577 * otherwise schedules the function for delayed execution.
579 * Returns: 0 - function was executed
580 * 1 - function was scheduled for execution
582 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
584 if (!in_interrupt()) {
589 INIT_WORK(&ew->work, fn);
590 schedule_work(&ew->work);
594 EXPORT_SYMBOL_GPL(execute_in_process_context);
598 return keventd_wq != NULL;
601 int current_is_keventd(void)
603 struct cpu_workqueue_struct *cwq;
604 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
609 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
610 if (current == cwq->thread)
617 static struct cpu_workqueue_struct *
618 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
620 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
623 spin_lock_init(&cwq->lock);
624 INIT_LIST_HEAD(&cwq->worklist);
625 init_waitqueue_head(&cwq->more_work);
630 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
632 struct workqueue_struct *wq = cwq->wq;
633 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
634 struct task_struct *p;
636 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
638 * Nobody can add the work_struct to this cwq,
639 * if (caller is __create_workqueue)
640 * nobody should see this wq
641 * else // caller is CPU_UP_PREPARE
642 * cpu is not on cpu_online_map
643 * so we can abort safely.
649 cwq->should_stop = 0;
654 static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
656 struct task_struct *p = cwq->thread;
660 kthread_bind(p, cpu);
665 struct workqueue_struct *__create_workqueue(const char *name,
666 int singlethread, int freezeable)
668 struct workqueue_struct *wq;
669 struct cpu_workqueue_struct *cwq;
672 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
676 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
683 wq->singlethread = singlethread;
684 wq->freezeable = freezeable;
685 INIT_LIST_HEAD(&wq->list);
688 cwq = init_cpu_workqueue(wq, singlethread_cpu);
689 err = create_workqueue_thread(cwq, singlethread_cpu);
690 start_workqueue_thread(cwq, -1);
692 mutex_lock(&workqueue_mutex);
693 list_add(&wq->list, &workqueues);
695 for_each_possible_cpu(cpu) {
696 cwq = init_cpu_workqueue(wq, cpu);
697 if (err || !cpu_online(cpu))
699 err = create_workqueue_thread(cwq, cpu);
700 start_workqueue_thread(cwq, cpu);
702 mutex_unlock(&workqueue_mutex);
706 destroy_workqueue(wq);
711 EXPORT_SYMBOL_GPL(__create_workqueue);
713 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
715 struct wq_barrier barr;
718 spin_lock_irq(&cwq->lock);
719 if (cwq->thread != NULL) {
720 insert_wq_barrier(cwq, &barr, 1);
721 cwq->should_stop = 1;
724 spin_unlock_irq(&cwq->lock);
727 wait_for_completion(&barr.done);
729 while (unlikely(cwq->thread != NULL))
732 * Wait until cwq->thread unlocks cwq->lock,
733 * it won't touch *cwq after that.
736 spin_unlock_wait(&cwq->lock);
741 * destroy_workqueue - safely terminate a workqueue
742 * @wq: target workqueue
744 * Safely destroy a workqueue. All work currently pending will be done first.
746 void destroy_workqueue(struct workqueue_struct *wq)
748 const cpumask_t *cpu_map = wq_cpu_map(wq);
749 struct cpu_workqueue_struct *cwq;
752 mutex_lock(&workqueue_mutex);
754 mutex_unlock(&workqueue_mutex);
756 for_each_cpu_mask(cpu, *cpu_map) {
757 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
758 cleanup_workqueue_thread(cwq, cpu);
761 free_percpu(wq->cpu_wq);
764 EXPORT_SYMBOL_GPL(destroy_workqueue);
766 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
767 unsigned long action,
770 unsigned int cpu = (unsigned long)hcpu;
771 struct cpu_workqueue_struct *cwq;
772 struct workqueue_struct *wq;
775 case CPU_LOCK_ACQUIRE:
776 mutex_lock(&workqueue_mutex);
779 case CPU_LOCK_RELEASE:
780 mutex_unlock(&workqueue_mutex);
784 cpu_set(cpu, cpu_populated_map);
787 list_for_each_entry(wq, &workqueues, list) {
788 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
792 if (!create_workqueue_thread(cwq, cpu))
794 printk(KERN_ERR "workqueue for %i failed\n", cpu);
798 start_workqueue_thread(cwq, cpu);
801 case CPU_UP_CANCELED:
802 start_workqueue_thread(cwq, -1);
804 cleanup_workqueue_thread(cwq, cpu);
812 void __init init_workqueues(void)
814 cpu_populated_map = cpu_online_map;
815 singlethread_cpu = first_cpu(cpu_possible_map);
816 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
817 hotcpu_notifier(workqueue_cpu_callback, 0);
818 keventd_wq = create_workqueue("events");