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 * Set the workqueue on which a work item is to be run
91 * - Must *only* be called if the pending flag is set
93 static inline void set_wq_data(struct work_struct *work, void *wq)
97 BUG_ON(!work_pending(work));
99 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
100 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
101 atomic_long_set(&work->data, new);
104 static inline void *get_wq_data(struct work_struct *work)
106 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
109 static void insert_work(struct cpu_workqueue_struct *cwq,
110 struct work_struct *work, int tail)
112 set_wq_data(work, cwq);
114 list_add_tail(&work->entry, &cwq->worklist);
116 list_add(&work->entry, &cwq->worklist);
117 wake_up(&cwq->more_work);
120 /* Preempt must be disabled. */
121 static void __queue_work(struct cpu_workqueue_struct *cwq,
122 struct work_struct *work)
126 spin_lock_irqsave(&cwq->lock, flags);
127 insert_work(cwq, work, 1);
128 spin_unlock_irqrestore(&cwq->lock, flags);
132 * queue_work - queue work on a workqueue
133 * @wq: workqueue to use
134 * @work: work to queue
136 * Returns 0 if @work was already on a queue, non-zero otherwise.
138 * We queue the work to the CPU it was submitted, but there is no
139 * guarantee that it will be processed by that CPU.
141 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
143 int ret = 0, cpu = get_cpu();
145 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
146 if (unlikely(is_single_threaded(wq)))
147 cpu = singlethread_cpu;
148 BUG_ON(!list_empty(&work->entry));
149 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
155 EXPORT_SYMBOL_GPL(queue_work);
157 void delayed_work_timer_fn(unsigned long __data)
159 struct delayed_work *dwork = (struct delayed_work *)__data;
160 struct workqueue_struct *wq = get_wq_data(&dwork->work);
161 int cpu = smp_processor_id();
163 if (unlikely(is_single_threaded(wq)))
164 cpu = singlethread_cpu;
166 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
170 * queue_delayed_work - queue work on a workqueue after delay
171 * @wq: workqueue to use
172 * @dwork: delayable work to queue
173 * @delay: number of jiffies to wait before queueing
175 * Returns 0 if @work was already on a queue, non-zero otherwise.
177 int fastcall queue_delayed_work(struct workqueue_struct *wq,
178 struct delayed_work *dwork, unsigned long delay)
181 struct timer_list *timer = &dwork->timer;
182 struct work_struct *work = &dwork->work;
184 timer_stats_timer_set_start_info(timer);
186 return queue_work(wq, work);
188 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
189 BUG_ON(timer_pending(timer));
190 BUG_ON(!list_empty(&work->entry));
192 /* This stores wq for the moment, for the timer_fn */
193 set_wq_data(work, wq);
194 timer->expires = jiffies + delay;
195 timer->data = (unsigned long)dwork;
196 timer->function = delayed_work_timer_fn;
202 EXPORT_SYMBOL_GPL(queue_delayed_work);
205 * queue_delayed_work_on - queue work on specific CPU after delay
206 * @cpu: CPU number to execute work on
207 * @wq: workqueue to use
208 * @dwork: work to queue
209 * @delay: number of jiffies to wait before queueing
211 * Returns 0 if @work was already on a queue, non-zero otherwise.
213 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
214 struct delayed_work *dwork, unsigned long delay)
217 struct timer_list *timer = &dwork->timer;
218 struct work_struct *work = &dwork->work;
220 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
221 BUG_ON(timer_pending(timer));
222 BUG_ON(!list_empty(&work->entry));
224 /* This stores wq for the moment, for the timer_fn */
225 set_wq_data(work, wq);
226 timer->expires = jiffies + delay;
227 timer->data = (unsigned long)dwork;
228 timer->function = delayed_work_timer_fn;
229 add_timer_on(timer, cpu);
234 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
236 static void run_workqueue(struct cpu_workqueue_struct *cwq)
238 spin_lock_irq(&cwq->lock);
240 if (cwq->run_depth > 3) {
241 /* morton gets to eat his hat */
242 printk("%s: recursion depth exceeded: %d\n",
243 __FUNCTION__, cwq->run_depth);
246 while (!list_empty(&cwq->worklist)) {
247 struct work_struct *work = list_entry(cwq->worklist.next,
248 struct work_struct, entry);
249 work_func_t f = work->func;
251 cwq->current_work = work;
252 list_del_init(cwq->worklist.next);
253 spin_unlock_irq(&cwq->lock);
255 BUG_ON(get_wq_data(work) != cwq);
256 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
260 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
261 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
263 current->comm, preempt_count(),
265 printk(KERN_ERR " last function: ");
266 print_symbol("%s\n", (unsigned long)f);
267 debug_show_held_locks(current);
271 spin_lock_irq(&cwq->lock);
272 cwq->current_work = NULL;
275 spin_unlock_irq(&cwq->lock);
279 * NOTE: the caller must not touch *cwq if this func returns true
281 static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
283 int should_stop = cwq->should_stop;
285 if (unlikely(should_stop)) {
286 spin_lock_irq(&cwq->lock);
287 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
290 spin_unlock_irq(&cwq->lock);
296 static int worker_thread(void *__cwq)
298 struct cpu_workqueue_struct *cwq = __cwq;
300 struct k_sigaction sa;
303 if (!cwq->wq->freezeable)
304 current->flags |= PF_NOFREEZE;
306 set_user_nice(current, -5);
308 /* Block and flush all signals */
309 sigfillset(&blocked);
310 sigprocmask(SIG_BLOCK, &blocked, NULL);
311 flush_signals(current);
314 * We inherited MPOL_INTERLEAVE from the booting kernel.
315 * Set MPOL_DEFAULT to insure node local allocations.
317 numa_default_policy();
319 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
320 sa.sa.sa_handler = SIG_IGN;
322 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
323 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
326 if (cwq->wq->freezeable)
329 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
330 if (!cwq->should_stop && list_empty(&cwq->worklist))
332 finish_wait(&cwq->more_work, &wait);
334 if (cwq_should_stop(cwq))
344 struct work_struct work;
345 struct completion done;
348 static void wq_barrier_func(struct work_struct *work)
350 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
351 complete(&barr->done);
354 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
355 struct wq_barrier *barr, int tail)
357 INIT_WORK(&barr->work, wq_barrier_func);
358 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
360 init_completion(&barr->done);
362 insert_work(cwq, &barr->work, tail);
365 static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
367 if (cwq->thread == current) {
369 * Probably keventd trying to flush its own queue. So simply run
370 * it by hand rather than deadlocking.
374 struct wq_barrier barr;
377 spin_lock_irq(&cwq->lock);
378 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
379 insert_wq_barrier(cwq, &barr, 1);
382 spin_unlock_irq(&cwq->lock);
385 wait_for_completion(&barr.done);
390 * flush_workqueue - ensure that any scheduled work has run to completion.
391 * @wq: workqueue to flush
393 * Forces execution of the workqueue and blocks until its completion.
394 * This is typically used in driver shutdown handlers.
396 * We sleep until all works which were queued on entry have been handled,
397 * but we are not livelocked by new incoming ones.
399 * This function used to run the workqueues itself. Now we just wait for the
400 * helper threads to do it.
402 void fastcall flush_workqueue(struct workqueue_struct *wq)
404 const cpumask_t *cpu_map = wq_cpu_map(wq);
408 for_each_cpu_mask(cpu, *cpu_map)
409 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
411 EXPORT_SYMBOL_GPL(flush_workqueue);
413 static void wait_on_work(struct cpu_workqueue_struct *cwq,
414 struct work_struct *work)
416 struct wq_barrier barr;
419 spin_lock_irq(&cwq->lock);
420 if (unlikely(cwq->current_work == work)) {
421 insert_wq_barrier(cwq, &barr, 0);
424 spin_unlock_irq(&cwq->lock);
426 if (unlikely(running))
427 wait_for_completion(&barr.done);
431 * flush_work - block until a work_struct's callback has terminated
432 * @wq: the workqueue on which the work is queued
433 * @work: the work which is to be flushed
435 * flush_work() will attempt to cancel the work if it is queued. If the work's
436 * callback appears to be running, flush_work() will block until it has
439 * flush_work() is designed to be used when the caller is tearing down data
440 * structures which the callback function operates upon. It is expected that,
441 * prior to calling flush_work(), the caller has arranged for the work to not
444 void flush_work(struct workqueue_struct *wq, struct work_struct *work)
446 const cpumask_t *cpu_map = wq_cpu_map(wq);
447 struct cpu_workqueue_struct *cwq;
452 cwq = get_wq_data(work);
453 /* Was it ever queued ? */
458 * This work can't be re-queued, no need to re-check that
459 * get_wq_data() is still the same when we take cwq->lock.
461 spin_lock_irq(&cwq->lock);
462 list_del_init(&work->entry);
464 spin_unlock_irq(&cwq->lock);
466 for_each_cpu_mask(cpu, *cpu_map)
467 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
469 EXPORT_SYMBOL_GPL(flush_work);
472 static struct workqueue_struct *keventd_wq;
475 * schedule_work - put work task in global workqueue
476 * @work: job to be done
478 * This puts a job in the kernel-global workqueue.
480 int fastcall schedule_work(struct work_struct *work)
482 return queue_work(keventd_wq, work);
484 EXPORT_SYMBOL(schedule_work);
487 * schedule_delayed_work - put work task in global workqueue after delay
488 * @dwork: job to be done
489 * @delay: number of jiffies to wait or 0 for immediate execution
491 * After waiting for a given time this puts a job in the kernel-global
494 int fastcall schedule_delayed_work(struct delayed_work *dwork,
497 timer_stats_timer_set_start_info(&dwork->timer);
498 return queue_delayed_work(keventd_wq, dwork, delay);
500 EXPORT_SYMBOL(schedule_delayed_work);
503 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
505 * @dwork: job to be done
506 * @delay: number of jiffies to wait
508 * After waiting for a given time this puts a job in the kernel-global
509 * workqueue on the specified CPU.
511 int schedule_delayed_work_on(int cpu,
512 struct delayed_work *dwork, unsigned long delay)
514 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
516 EXPORT_SYMBOL(schedule_delayed_work_on);
519 * schedule_on_each_cpu - call a function on each online CPU from keventd
520 * @func: the function to call
522 * Returns zero on success.
523 * Returns -ve errno on failure.
525 * Appears to be racy against CPU hotplug.
527 * schedule_on_each_cpu() is very slow.
529 int schedule_on_each_cpu(work_func_t func)
532 struct work_struct *works;
534 works = alloc_percpu(struct work_struct);
538 preempt_disable(); /* CPU hotplug */
539 for_each_online_cpu(cpu) {
540 struct work_struct *work = per_cpu_ptr(works, cpu);
542 INIT_WORK(work, func);
543 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
544 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
547 flush_workqueue(keventd_wq);
552 void flush_scheduled_work(void)
554 flush_workqueue(keventd_wq);
556 EXPORT_SYMBOL(flush_scheduled_work);
558 void flush_work_keventd(struct work_struct *work)
560 flush_work(keventd_wq, work);
562 EXPORT_SYMBOL(flush_work_keventd);
565 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
566 * @wq: the controlling workqueue structure
567 * @dwork: the delayed work struct
569 void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
570 struct delayed_work *dwork)
572 /* Was it ever queued ? */
573 if (!get_wq_data(&dwork->work))
576 while (!cancel_delayed_work(dwork))
579 EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
582 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
583 * @dwork: the delayed work struct
585 void cancel_rearming_delayed_work(struct delayed_work *dwork)
587 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
589 EXPORT_SYMBOL(cancel_rearming_delayed_work);
592 * execute_in_process_context - reliably execute the routine with user context
593 * @fn: the function to execute
594 * @ew: guaranteed storage for the execute work structure (must
595 * be available when the work executes)
597 * Executes the function immediately if process context is available,
598 * otherwise schedules the function for delayed execution.
600 * Returns: 0 - function was executed
601 * 1 - function was scheduled for execution
603 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
605 if (!in_interrupt()) {
610 INIT_WORK(&ew->work, fn);
611 schedule_work(&ew->work);
615 EXPORT_SYMBOL_GPL(execute_in_process_context);
619 return keventd_wq != NULL;
622 int current_is_keventd(void)
624 struct cpu_workqueue_struct *cwq;
625 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
630 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
631 if (current == cwq->thread)
638 static struct cpu_workqueue_struct *
639 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
641 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
644 spin_lock_init(&cwq->lock);
645 INIT_LIST_HEAD(&cwq->worklist);
646 init_waitqueue_head(&cwq->more_work);
651 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
653 struct workqueue_struct *wq = cwq->wq;
654 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
655 struct task_struct *p;
657 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
659 * Nobody can add the work_struct to this cwq,
660 * if (caller is __create_workqueue)
661 * nobody should see this wq
662 * else // caller is CPU_UP_PREPARE
663 * cpu is not on cpu_online_map
664 * so we can abort safely.
670 cwq->should_stop = 0;
675 static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
677 struct task_struct *p = cwq->thread;
681 kthread_bind(p, cpu);
686 struct workqueue_struct *__create_workqueue(const char *name,
687 int singlethread, int freezeable)
689 struct workqueue_struct *wq;
690 struct cpu_workqueue_struct *cwq;
693 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
697 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
704 wq->singlethread = singlethread;
705 wq->freezeable = freezeable;
706 INIT_LIST_HEAD(&wq->list);
709 cwq = init_cpu_workqueue(wq, singlethread_cpu);
710 err = create_workqueue_thread(cwq, singlethread_cpu);
711 start_workqueue_thread(cwq, -1);
713 mutex_lock(&workqueue_mutex);
714 list_add(&wq->list, &workqueues);
716 for_each_possible_cpu(cpu) {
717 cwq = init_cpu_workqueue(wq, cpu);
718 if (err || !cpu_online(cpu))
720 err = create_workqueue_thread(cwq, cpu);
721 start_workqueue_thread(cwq, cpu);
723 mutex_unlock(&workqueue_mutex);
727 destroy_workqueue(wq);
732 EXPORT_SYMBOL_GPL(__create_workqueue);
734 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
736 struct wq_barrier barr;
739 spin_lock_irq(&cwq->lock);
740 if (cwq->thread != NULL) {
741 insert_wq_barrier(cwq, &barr, 1);
742 cwq->should_stop = 1;
745 spin_unlock_irq(&cwq->lock);
748 wait_for_completion(&barr.done);
750 while (unlikely(cwq->thread != NULL))
753 * Wait until cwq->thread unlocks cwq->lock,
754 * it won't touch *cwq after that.
757 spin_unlock_wait(&cwq->lock);
762 * destroy_workqueue - safely terminate a workqueue
763 * @wq: target workqueue
765 * Safely destroy a workqueue. All work currently pending will be done first.
767 void destroy_workqueue(struct workqueue_struct *wq)
769 const cpumask_t *cpu_map = wq_cpu_map(wq);
770 struct cpu_workqueue_struct *cwq;
773 mutex_lock(&workqueue_mutex);
775 mutex_unlock(&workqueue_mutex);
777 for_each_cpu_mask(cpu, *cpu_map) {
778 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
779 cleanup_workqueue_thread(cwq, cpu);
782 free_percpu(wq->cpu_wq);
785 EXPORT_SYMBOL_GPL(destroy_workqueue);
787 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
788 unsigned long action,
791 unsigned int cpu = (unsigned long)hcpu;
792 struct cpu_workqueue_struct *cwq;
793 struct workqueue_struct *wq;
796 case CPU_LOCK_ACQUIRE:
797 mutex_lock(&workqueue_mutex);
800 case CPU_LOCK_RELEASE:
801 mutex_unlock(&workqueue_mutex);
805 cpu_set(cpu, cpu_populated_map);
808 list_for_each_entry(wq, &workqueues, list) {
809 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
813 if (!create_workqueue_thread(cwq, cpu))
815 printk(KERN_ERR "workqueue for %i failed\n", cpu);
819 start_workqueue_thread(cwq, cpu);
822 case CPU_UP_CANCELED:
823 start_workqueue_thread(cwq, -1);
825 cleanup_workqueue_thread(cwq, cpu);
833 void __init init_workqueues(void)
835 cpu_populated_map = cpu_online_map;
836 singlethread_cpu = first_cpu(cpu_possible_map);
837 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
838 hotcpu_notifier(workqueue_cpu_callback, 0);
839 keventd_wq = create_workqueue("events");