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;
62 struct list_head list; /* Empty if single thread */
63 int freezeable; /* Freeze threads during suspend */
66 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
67 threads to each one as cpus come/go. */
68 static DEFINE_MUTEX(workqueue_mutex);
69 static LIST_HEAD(workqueues);
71 static int singlethread_cpu __read_mostly;
72 /* optimization, we could use cpu_possible_map */
73 static cpumask_t cpu_populated_map __read_mostly;
75 /* If it's single threaded, it isn't in the list of workqueues. */
76 static inline int is_single_threaded(struct workqueue_struct *wq)
78 return list_empty(&wq->list);
82 * Set the workqueue on which a work item is to be run
83 * - Must *only* be called if the pending flag is set
85 static inline void set_wq_data(struct work_struct *work, void *wq)
89 BUG_ON(!work_pending(work));
91 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
92 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
93 atomic_long_set(&work->data, new);
96 static inline void *get_wq_data(struct work_struct *work)
98 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
101 static int __run_work(struct cpu_workqueue_struct *cwq, struct work_struct *work)
106 spin_lock_irqsave(&cwq->lock, flags);
108 * We need to re-validate the work info after we've gotten
109 * the cpu_workqueue lock. We can run the work now iff:
111 * - the wq_data still matches the cpu_workqueue_struct
112 * - AND the work is still marked pending
113 * - AND the work is still on a list (which will be this
114 * workqueue_struct list)
116 * All these conditions are important, because we
117 * need to protect against the work being run right
118 * now on another CPU (all but the last one might be
119 * true if it's currently running and has not been
120 * released yet, for example).
122 if (get_wq_data(work) == cwq
123 && work_pending(work)
124 && !list_empty(&work->entry)) {
125 work_func_t f = work->func;
126 cwq->current_work = work;
127 list_del_init(&work->entry);
128 spin_unlock_irqrestore(&cwq->lock, flags);
130 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
134 spin_lock_irqsave(&cwq->lock, flags);
135 cwq->current_work = NULL;
138 spin_unlock_irqrestore(&cwq->lock, flags);
143 * run_scheduled_work - run scheduled work synchronously
146 * This checks if the work was pending, and runs it
147 * synchronously if so. It returns a boolean to indicate
148 * whether it had any scheduled work to run or not.
150 * NOTE! This _only_ works for normal work_structs. You
151 * CANNOT use this for delayed work, because the wq data
152 * for delayed work will not point properly to the per-
153 * CPU workqueue struct, but will change!
155 int fastcall run_scheduled_work(struct work_struct *work)
158 struct cpu_workqueue_struct *cwq;
160 if (!work_pending(work))
162 if (list_empty(&work->entry))
164 /* NOTE! This depends intimately on __queue_work! */
165 cwq = get_wq_data(work);
168 if (__run_work(cwq, work))
172 EXPORT_SYMBOL(run_scheduled_work);
174 static void insert_work(struct cpu_workqueue_struct *cwq,
175 struct work_struct *work, int tail)
177 set_wq_data(work, cwq);
179 list_add_tail(&work->entry, &cwq->worklist);
181 list_add(&work->entry, &cwq->worklist);
182 wake_up(&cwq->more_work);
185 /* Preempt must be disabled. */
186 static void __queue_work(struct cpu_workqueue_struct *cwq,
187 struct work_struct *work)
191 spin_lock_irqsave(&cwq->lock, flags);
192 insert_work(cwq, work, 1);
193 spin_unlock_irqrestore(&cwq->lock, flags);
197 * queue_work - queue work on a workqueue
198 * @wq: workqueue to use
199 * @work: work to queue
201 * Returns 0 if @work was already on a queue, non-zero otherwise.
203 * We queue the work to the CPU it was submitted, but there is no
204 * guarantee that it will be processed by that CPU.
206 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
208 int ret = 0, cpu = get_cpu();
210 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
211 if (unlikely(is_single_threaded(wq)))
212 cpu = singlethread_cpu;
213 BUG_ON(!list_empty(&work->entry));
214 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
220 EXPORT_SYMBOL_GPL(queue_work);
222 void delayed_work_timer_fn(unsigned long __data)
224 struct delayed_work *dwork = (struct delayed_work *)__data;
225 struct workqueue_struct *wq = get_wq_data(&dwork->work);
226 int cpu = smp_processor_id();
228 if (unlikely(is_single_threaded(wq)))
229 cpu = singlethread_cpu;
231 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
235 * queue_delayed_work - queue work on a workqueue after delay
236 * @wq: workqueue to use
237 * @dwork: delayable work to queue
238 * @delay: number of jiffies to wait before queueing
240 * Returns 0 if @work was already on a queue, non-zero otherwise.
242 int fastcall queue_delayed_work(struct workqueue_struct *wq,
243 struct delayed_work *dwork, unsigned long delay)
246 struct timer_list *timer = &dwork->timer;
247 struct work_struct *work = &dwork->work;
249 timer_stats_timer_set_start_info(timer);
251 return queue_work(wq, work);
253 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
254 BUG_ON(timer_pending(timer));
255 BUG_ON(!list_empty(&work->entry));
257 /* This stores wq for the moment, for the timer_fn */
258 set_wq_data(work, wq);
259 timer->expires = jiffies + delay;
260 timer->data = (unsigned long)dwork;
261 timer->function = delayed_work_timer_fn;
267 EXPORT_SYMBOL_GPL(queue_delayed_work);
270 * queue_delayed_work_on - queue work on specific CPU after delay
271 * @cpu: CPU number to execute work on
272 * @wq: workqueue to use
273 * @dwork: work to queue
274 * @delay: number of jiffies to wait before queueing
276 * Returns 0 if @work was already on a queue, non-zero otherwise.
278 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
279 struct delayed_work *dwork, unsigned long delay)
282 struct timer_list *timer = &dwork->timer;
283 struct work_struct *work = &dwork->work;
285 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
286 BUG_ON(timer_pending(timer));
287 BUG_ON(!list_empty(&work->entry));
289 /* This stores wq for the moment, for the timer_fn */
290 set_wq_data(work, wq);
291 timer->expires = jiffies + delay;
292 timer->data = (unsigned long)dwork;
293 timer->function = delayed_work_timer_fn;
294 add_timer_on(timer, cpu);
299 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
301 static void run_workqueue(struct cpu_workqueue_struct *cwq)
306 * Keep taking off work from the queue until
309 spin_lock_irqsave(&cwq->lock, flags);
311 if (cwq->run_depth > 3) {
312 /* morton gets to eat his hat */
313 printk("%s: recursion depth exceeded: %d\n",
314 __FUNCTION__, cwq->run_depth);
317 while (!list_empty(&cwq->worklist)) {
318 struct work_struct *work = list_entry(cwq->worklist.next,
319 struct work_struct, entry);
320 work_func_t f = work->func;
322 cwq->current_work = work;
323 list_del_init(cwq->worklist.next);
324 spin_unlock_irqrestore(&cwq->lock, flags);
326 BUG_ON(get_wq_data(work) != cwq);
327 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
331 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
332 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
334 current->comm, preempt_count(),
336 printk(KERN_ERR " last function: ");
337 print_symbol("%s\n", (unsigned long)f);
338 debug_show_held_locks(current);
342 spin_lock_irqsave(&cwq->lock, flags);
343 cwq->current_work = NULL;
346 spin_unlock_irqrestore(&cwq->lock, flags);
350 * NOTE: the caller must not touch *cwq if this func returns true
352 static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
354 int should_stop = cwq->should_stop;
356 if (unlikely(should_stop)) {
357 spin_lock_irq(&cwq->lock);
358 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
361 spin_unlock_irq(&cwq->lock);
367 static int worker_thread(void *__cwq)
369 struct cpu_workqueue_struct *cwq = __cwq;
371 struct k_sigaction sa;
374 if (!cwq->wq->freezeable)
375 current->flags |= PF_NOFREEZE;
377 set_user_nice(current, -5);
379 /* Block and flush all signals */
380 sigfillset(&blocked);
381 sigprocmask(SIG_BLOCK, &blocked, NULL);
382 flush_signals(current);
385 * We inherited MPOL_INTERLEAVE from the booting kernel.
386 * Set MPOL_DEFAULT to insure node local allocations.
388 numa_default_policy();
390 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
391 sa.sa.sa_handler = SIG_IGN;
393 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
394 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
397 if (cwq->wq->freezeable)
400 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
401 if (!cwq->should_stop && list_empty(&cwq->worklist))
403 finish_wait(&cwq->more_work, &wait);
405 if (cwq_should_stop(cwq))
415 struct work_struct work;
416 struct completion done;
419 static void wq_barrier_func(struct work_struct *work)
421 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
422 complete(&barr->done);
425 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
426 struct wq_barrier *barr, int tail)
428 INIT_WORK(&barr->work, wq_barrier_func);
429 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
431 init_completion(&barr->done);
433 insert_work(cwq, &barr->work, tail);
436 static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
438 if (cwq->thread == current) {
440 * Probably keventd trying to flush its own queue. So simply run
441 * it by hand rather than deadlocking.
445 struct wq_barrier barr;
448 spin_lock_irq(&cwq->lock);
449 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
450 insert_wq_barrier(cwq, &barr, 1);
453 spin_unlock_irq(&cwq->lock);
456 wait_for_completion(&barr.done);
461 * flush_workqueue - ensure that any scheduled work has run to completion.
462 * @wq: workqueue to flush
464 * Forces execution of the workqueue and blocks until its completion.
465 * This is typically used in driver shutdown handlers.
467 * We sleep until all works which were queued on entry have been handled,
468 * but we are not livelocked by new incoming ones.
470 * This function used to run the workqueues itself. Now we just wait for the
471 * helper threads to do it.
473 void fastcall flush_workqueue(struct workqueue_struct *wq)
475 if (is_single_threaded(wq))
476 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
480 for_each_cpu_mask(cpu, cpu_populated_map)
481 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
484 EXPORT_SYMBOL_GPL(flush_workqueue);
486 static void wait_on_work(struct cpu_workqueue_struct *cwq,
487 struct work_struct *work)
489 struct wq_barrier barr;
492 spin_lock_irq(&cwq->lock);
493 if (unlikely(cwq->current_work == work)) {
494 insert_wq_barrier(cwq, &barr, 0);
497 spin_unlock_irq(&cwq->lock);
499 if (unlikely(running))
500 wait_for_completion(&barr.done);
504 * flush_work - block until a work_struct's callback has terminated
505 * @wq: the workqueue on which the work is queued
506 * @work: the work which is to be flushed
508 * flush_work() will attempt to cancel the work if it is queued. If the work's
509 * callback appears to be running, flush_work() will block until it has
512 * flush_work() is designed to be used when the caller is tearing down data
513 * structures which the callback function operates upon. It is expected that,
514 * prior to calling flush_work(), the caller has arranged for the work to not
517 void flush_work(struct workqueue_struct *wq, struct work_struct *work)
519 struct cpu_workqueue_struct *cwq;
521 cwq = get_wq_data(work);
522 /* Was it ever queued ? */
527 * This work can't be re-queued, no need to re-check that
528 * get_wq_data() is still the same when we take cwq->lock.
530 spin_lock_irq(&cwq->lock);
531 list_del_init(&work->entry);
533 spin_unlock_irq(&cwq->lock);
535 if (is_single_threaded(wq))
536 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
540 for_each_cpu_mask(cpu, cpu_populated_map)
541 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
544 EXPORT_SYMBOL_GPL(flush_work);
547 static struct workqueue_struct *keventd_wq;
550 * schedule_work - put work task in global workqueue
551 * @work: job to be done
553 * This puts a job in the kernel-global workqueue.
555 int fastcall schedule_work(struct work_struct *work)
557 return queue_work(keventd_wq, work);
559 EXPORT_SYMBOL(schedule_work);
562 * schedule_delayed_work - put work task in global workqueue after delay
563 * @dwork: job to be done
564 * @delay: number of jiffies to wait or 0 for immediate execution
566 * After waiting for a given time this puts a job in the kernel-global
569 int fastcall schedule_delayed_work(struct delayed_work *dwork,
572 timer_stats_timer_set_start_info(&dwork->timer);
573 return queue_delayed_work(keventd_wq, dwork, delay);
575 EXPORT_SYMBOL(schedule_delayed_work);
578 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
580 * @dwork: job to be done
581 * @delay: number of jiffies to wait
583 * After waiting for a given time this puts a job in the kernel-global
584 * workqueue on the specified CPU.
586 int schedule_delayed_work_on(int cpu,
587 struct delayed_work *dwork, unsigned long delay)
589 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
591 EXPORT_SYMBOL(schedule_delayed_work_on);
594 * schedule_on_each_cpu - call a function on each online CPU from keventd
595 * @func: the function to call
597 * Returns zero on success.
598 * Returns -ve errno on failure.
600 * Appears to be racy against CPU hotplug.
602 * schedule_on_each_cpu() is very slow.
604 int schedule_on_each_cpu(work_func_t func)
607 struct work_struct *works;
609 works = alloc_percpu(struct work_struct);
613 preempt_disable(); /* CPU hotplug */
614 for_each_online_cpu(cpu) {
615 struct work_struct *work = per_cpu_ptr(works, cpu);
617 INIT_WORK(work, func);
618 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
619 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
622 flush_workqueue(keventd_wq);
627 void flush_scheduled_work(void)
629 flush_workqueue(keventd_wq);
631 EXPORT_SYMBOL(flush_scheduled_work);
633 void flush_work_keventd(struct work_struct *work)
635 flush_work(keventd_wq, work);
637 EXPORT_SYMBOL(flush_work_keventd);
640 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
641 * @wq: the controlling workqueue structure
642 * @dwork: the delayed work struct
644 void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
645 struct delayed_work *dwork)
647 while (!cancel_delayed_work(dwork))
650 EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
653 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
654 * @dwork: the delayed work struct
656 void cancel_rearming_delayed_work(struct delayed_work *dwork)
658 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
660 EXPORT_SYMBOL(cancel_rearming_delayed_work);
663 * execute_in_process_context - reliably execute the routine with user context
664 * @fn: the function to execute
665 * @ew: guaranteed storage for the execute work structure (must
666 * be available when the work executes)
668 * Executes the function immediately if process context is available,
669 * otherwise schedules the function for delayed execution.
671 * Returns: 0 - function was executed
672 * 1 - function was scheduled for execution
674 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
676 if (!in_interrupt()) {
681 INIT_WORK(&ew->work, fn);
682 schedule_work(&ew->work);
686 EXPORT_SYMBOL_GPL(execute_in_process_context);
690 return keventd_wq != NULL;
693 int current_is_keventd(void)
695 struct cpu_workqueue_struct *cwq;
696 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
701 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
702 if (current == cwq->thread)
709 static struct cpu_workqueue_struct *
710 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
712 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
715 spin_lock_init(&cwq->lock);
716 INIT_LIST_HEAD(&cwq->worklist);
717 init_waitqueue_head(&cwq->more_work);
722 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
724 struct workqueue_struct *wq = cwq->wq;
725 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
726 struct task_struct *p;
728 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
730 * Nobody can add the work_struct to this cwq,
731 * if (caller is __create_workqueue)
732 * nobody should see this wq
733 * else // caller is CPU_UP_PREPARE
734 * cpu is not on cpu_online_map
735 * so we can abort safely.
741 cwq->should_stop = 0;
742 if (!is_single_threaded(wq))
743 kthread_bind(p, cpu);
745 if (is_single_threaded(wq) || cpu_online(cpu))
751 struct workqueue_struct *__create_workqueue(const char *name,
752 int singlethread, int freezeable)
754 struct workqueue_struct *wq;
755 struct cpu_workqueue_struct *cwq;
758 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
762 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
769 wq->freezeable = freezeable;
772 INIT_LIST_HEAD(&wq->list);
773 cwq = init_cpu_workqueue(wq, singlethread_cpu);
774 err = create_workqueue_thread(cwq, singlethread_cpu);
776 mutex_lock(&workqueue_mutex);
777 list_add(&wq->list, &workqueues);
779 for_each_possible_cpu(cpu) {
780 cwq = init_cpu_workqueue(wq, cpu);
781 if (err || !cpu_online(cpu))
783 err = create_workqueue_thread(cwq, cpu);
785 mutex_unlock(&workqueue_mutex);
789 destroy_workqueue(wq);
794 EXPORT_SYMBOL_GPL(__create_workqueue);
796 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
798 struct wq_barrier barr;
801 spin_lock_irq(&cwq->lock);
802 if (cwq->thread != NULL) {
803 insert_wq_barrier(cwq, &barr, 1);
804 cwq->should_stop = 1;
807 spin_unlock_irq(&cwq->lock);
810 wait_for_completion(&barr.done);
812 while (unlikely(cwq->thread != NULL))
815 * Wait until cwq->thread unlocks cwq->lock,
816 * it won't touch *cwq after that.
819 spin_unlock_wait(&cwq->lock);
824 * destroy_workqueue - safely terminate a workqueue
825 * @wq: target workqueue
827 * Safely destroy a workqueue. All work currently pending will be done first.
829 void destroy_workqueue(struct workqueue_struct *wq)
831 struct cpu_workqueue_struct *cwq;
833 if (is_single_threaded(wq)) {
834 cwq = per_cpu_ptr(wq->cpu_wq, singlethread_cpu);
835 cleanup_workqueue_thread(cwq, singlethread_cpu);
839 mutex_lock(&workqueue_mutex);
841 mutex_unlock(&workqueue_mutex);
843 for_each_cpu_mask(cpu, cpu_populated_map) {
844 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
845 cleanup_workqueue_thread(cwq, cpu);
849 free_percpu(wq->cpu_wq);
852 EXPORT_SYMBOL_GPL(destroy_workqueue);
854 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
855 unsigned long action,
858 unsigned int cpu = (unsigned long)hcpu;
859 struct cpu_workqueue_struct *cwq;
860 struct workqueue_struct *wq;
863 case CPU_LOCK_ACQUIRE:
864 mutex_lock(&workqueue_mutex);
867 case CPU_LOCK_RELEASE:
868 mutex_unlock(&workqueue_mutex);
872 cpu_set(cpu, cpu_populated_map);
875 list_for_each_entry(wq, &workqueues, list) {
876 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
880 if (!create_workqueue_thread(cwq, cpu))
882 printk(KERN_ERR "workqueue for %i failed\n", cpu);
886 wake_up_process(cwq->thread);
889 case CPU_UP_CANCELED:
891 wake_up_process(cwq->thread);
893 cleanup_workqueue_thread(cwq, cpu);
901 void init_workqueues(void)
903 cpu_populated_map = cpu_online_map;
904 singlethread_cpu = first_cpu(cpu_possible_map);
905 hotcpu_notifier(workqueue_cpu_callback, 0);
906 keventd_wq = create_workqueue("events");