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 void insert_work(struct cpu_workqueue_struct *cwq,
102 struct work_struct *work, int tail)
104 set_wq_data(work, cwq);
106 list_add_tail(&work->entry, &cwq->worklist);
108 list_add(&work->entry, &cwq->worklist);
109 wake_up(&cwq->more_work);
112 /* Preempt must be disabled. */
113 static void __queue_work(struct cpu_workqueue_struct *cwq,
114 struct work_struct *work)
118 spin_lock_irqsave(&cwq->lock, flags);
119 insert_work(cwq, work, 1);
120 spin_unlock_irqrestore(&cwq->lock, flags);
124 * queue_work - queue work on a workqueue
125 * @wq: workqueue to use
126 * @work: work to queue
128 * Returns 0 if @work was already on a queue, non-zero otherwise.
130 * We queue the work to the CPU it was submitted, but there is no
131 * guarantee that it will be processed by that CPU.
133 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
135 int ret = 0, cpu = get_cpu();
137 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
138 if (unlikely(is_single_threaded(wq)))
139 cpu = singlethread_cpu;
140 BUG_ON(!list_empty(&work->entry));
141 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
147 EXPORT_SYMBOL_GPL(queue_work);
149 void delayed_work_timer_fn(unsigned long __data)
151 struct delayed_work *dwork = (struct delayed_work *)__data;
152 struct workqueue_struct *wq = get_wq_data(&dwork->work);
153 int cpu = smp_processor_id();
155 if (unlikely(is_single_threaded(wq)))
156 cpu = singlethread_cpu;
158 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
162 * queue_delayed_work - queue work on a workqueue after delay
163 * @wq: workqueue to use
164 * @dwork: delayable work to queue
165 * @delay: number of jiffies to wait before queueing
167 * Returns 0 if @work was already on a queue, non-zero otherwise.
169 int fastcall queue_delayed_work(struct workqueue_struct *wq,
170 struct delayed_work *dwork, unsigned long delay)
173 struct timer_list *timer = &dwork->timer;
174 struct work_struct *work = &dwork->work;
176 timer_stats_timer_set_start_info(timer);
178 return queue_work(wq, work);
180 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
181 BUG_ON(timer_pending(timer));
182 BUG_ON(!list_empty(&work->entry));
184 /* This stores wq for the moment, for the timer_fn */
185 set_wq_data(work, wq);
186 timer->expires = jiffies + delay;
187 timer->data = (unsigned long)dwork;
188 timer->function = delayed_work_timer_fn;
194 EXPORT_SYMBOL_GPL(queue_delayed_work);
197 * queue_delayed_work_on - queue work on specific CPU after delay
198 * @cpu: CPU number to execute work on
199 * @wq: workqueue to use
200 * @dwork: work to queue
201 * @delay: number of jiffies to wait before queueing
203 * Returns 0 if @work was already on a queue, non-zero otherwise.
205 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
206 struct delayed_work *dwork, unsigned long delay)
209 struct timer_list *timer = &dwork->timer;
210 struct work_struct *work = &dwork->work;
212 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
213 BUG_ON(timer_pending(timer));
214 BUG_ON(!list_empty(&work->entry));
216 /* This stores wq for the moment, for the timer_fn */
217 set_wq_data(work, wq);
218 timer->expires = jiffies + delay;
219 timer->data = (unsigned long)dwork;
220 timer->function = delayed_work_timer_fn;
221 add_timer_on(timer, cpu);
226 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
228 static void run_workqueue(struct cpu_workqueue_struct *cwq)
233 * Keep taking off work from the queue until
236 spin_lock_irqsave(&cwq->lock, flags);
238 if (cwq->run_depth > 3) {
239 /* morton gets to eat his hat */
240 printk("%s: recursion depth exceeded: %d\n",
241 __FUNCTION__, cwq->run_depth);
244 while (!list_empty(&cwq->worklist)) {
245 struct work_struct *work = list_entry(cwq->worklist.next,
246 struct work_struct, entry);
247 work_func_t f = work->func;
249 cwq->current_work = work;
250 list_del_init(cwq->worklist.next);
251 spin_unlock_irqrestore(&cwq->lock, flags);
253 BUG_ON(get_wq_data(work) != cwq);
254 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
258 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
259 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
261 current->comm, preempt_count(),
263 printk(KERN_ERR " last function: ");
264 print_symbol("%s\n", (unsigned long)f);
265 debug_show_held_locks(current);
269 spin_lock_irqsave(&cwq->lock, flags);
270 cwq->current_work = NULL;
273 spin_unlock_irqrestore(&cwq->lock, flags);
277 * NOTE: the caller must not touch *cwq if this func returns true
279 static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
281 int should_stop = cwq->should_stop;
283 if (unlikely(should_stop)) {
284 spin_lock_irq(&cwq->lock);
285 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
288 spin_unlock_irq(&cwq->lock);
294 static int worker_thread(void *__cwq)
296 struct cpu_workqueue_struct *cwq = __cwq;
298 struct k_sigaction sa;
301 if (!cwq->wq->freezeable)
302 current->flags |= PF_NOFREEZE;
304 set_user_nice(current, -5);
306 /* Block and flush all signals */
307 sigfillset(&blocked);
308 sigprocmask(SIG_BLOCK, &blocked, NULL);
309 flush_signals(current);
312 * We inherited MPOL_INTERLEAVE from the booting kernel.
313 * Set MPOL_DEFAULT to insure node local allocations.
315 numa_default_policy();
317 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
318 sa.sa.sa_handler = SIG_IGN;
320 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
321 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
324 if (cwq->wq->freezeable)
327 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
328 if (!cwq->should_stop && list_empty(&cwq->worklist))
330 finish_wait(&cwq->more_work, &wait);
332 if (cwq_should_stop(cwq))
342 struct work_struct work;
343 struct completion done;
346 static void wq_barrier_func(struct work_struct *work)
348 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
349 complete(&barr->done);
352 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
353 struct wq_barrier *barr, int tail)
355 INIT_WORK(&barr->work, wq_barrier_func);
356 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
358 init_completion(&barr->done);
360 insert_work(cwq, &barr->work, tail);
363 static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
365 if (cwq->thread == current) {
367 * Probably keventd trying to flush its own queue. So simply run
368 * it by hand rather than deadlocking.
372 struct wq_barrier barr;
375 spin_lock_irq(&cwq->lock);
376 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
377 insert_wq_barrier(cwq, &barr, 1);
380 spin_unlock_irq(&cwq->lock);
383 wait_for_completion(&barr.done);
388 * flush_workqueue - ensure that any scheduled work has run to completion.
389 * @wq: workqueue to flush
391 * Forces execution of the workqueue and blocks until its completion.
392 * This is typically used in driver shutdown handlers.
394 * We sleep until all works which were queued on entry have been handled,
395 * but we are not livelocked by new incoming ones.
397 * This function used to run the workqueues itself. Now we just wait for the
398 * helper threads to do it.
400 void fastcall flush_workqueue(struct workqueue_struct *wq)
402 if (is_single_threaded(wq))
403 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
407 for_each_cpu_mask(cpu, cpu_populated_map)
408 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 struct cpu_workqueue_struct *cwq;
448 cwq = get_wq_data(work);
449 /* Was it ever queued ? */
454 * This work can't be re-queued, no need to re-check that
455 * get_wq_data() is still the same when we take cwq->lock.
457 spin_lock_irq(&cwq->lock);
458 list_del_init(&work->entry);
460 spin_unlock_irq(&cwq->lock);
462 if (is_single_threaded(wq))
463 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
467 for_each_cpu_mask(cpu, cpu_populated_map)
468 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
471 EXPORT_SYMBOL_GPL(flush_work);
474 static struct workqueue_struct *keventd_wq;
477 * schedule_work - put work task in global workqueue
478 * @work: job to be done
480 * This puts a job in the kernel-global workqueue.
482 int fastcall schedule_work(struct work_struct *work)
484 return queue_work(keventd_wq, work);
486 EXPORT_SYMBOL(schedule_work);
489 * schedule_delayed_work - put work task in global workqueue after delay
490 * @dwork: job to be done
491 * @delay: number of jiffies to wait or 0 for immediate execution
493 * After waiting for a given time this puts a job in the kernel-global
496 int fastcall schedule_delayed_work(struct delayed_work *dwork,
499 timer_stats_timer_set_start_info(&dwork->timer);
500 return queue_delayed_work(keventd_wq, dwork, delay);
502 EXPORT_SYMBOL(schedule_delayed_work);
505 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
507 * @dwork: job to be done
508 * @delay: number of jiffies to wait
510 * After waiting for a given time this puts a job in the kernel-global
511 * workqueue on the specified CPU.
513 int schedule_delayed_work_on(int cpu,
514 struct delayed_work *dwork, unsigned long delay)
516 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
518 EXPORT_SYMBOL(schedule_delayed_work_on);
521 * schedule_on_each_cpu - call a function on each online CPU from keventd
522 * @func: the function to call
524 * Returns zero on success.
525 * Returns -ve errno on failure.
527 * Appears to be racy against CPU hotplug.
529 * schedule_on_each_cpu() is very slow.
531 int schedule_on_each_cpu(work_func_t func)
534 struct work_struct *works;
536 works = alloc_percpu(struct work_struct);
540 preempt_disable(); /* CPU hotplug */
541 for_each_online_cpu(cpu) {
542 struct work_struct *work = per_cpu_ptr(works, cpu);
544 INIT_WORK(work, func);
545 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
546 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
549 flush_workqueue(keventd_wq);
554 void flush_scheduled_work(void)
556 flush_workqueue(keventd_wq);
558 EXPORT_SYMBOL(flush_scheduled_work);
560 void flush_work_keventd(struct work_struct *work)
562 flush_work(keventd_wq, work);
564 EXPORT_SYMBOL(flush_work_keventd);
567 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
568 * @wq: the controlling workqueue structure
569 * @dwork: the delayed work struct
571 void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
572 struct delayed_work *dwork)
574 while (!cancel_delayed_work(dwork))
577 EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
580 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
581 * @dwork: the delayed work struct
583 void cancel_rearming_delayed_work(struct delayed_work *dwork)
585 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
587 EXPORT_SYMBOL(cancel_rearming_delayed_work);
590 * execute_in_process_context - reliably execute the routine with user context
591 * @fn: the function to execute
592 * @ew: guaranteed storage for the execute work structure (must
593 * be available when the work executes)
595 * Executes the function immediately if process context is available,
596 * otherwise schedules the function for delayed execution.
598 * Returns: 0 - function was executed
599 * 1 - function was scheduled for execution
601 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
603 if (!in_interrupt()) {
608 INIT_WORK(&ew->work, fn);
609 schedule_work(&ew->work);
613 EXPORT_SYMBOL_GPL(execute_in_process_context);
617 return keventd_wq != NULL;
620 int current_is_keventd(void)
622 struct cpu_workqueue_struct *cwq;
623 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
628 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
629 if (current == cwq->thread)
636 static struct cpu_workqueue_struct *
637 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
639 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
642 spin_lock_init(&cwq->lock);
643 INIT_LIST_HEAD(&cwq->worklist);
644 init_waitqueue_head(&cwq->more_work);
649 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
651 struct workqueue_struct *wq = cwq->wq;
652 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
653 struct task_struct *p;
655 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
657 * Nobody can add the work_struct to this cwq,
658 * if (caller is __create_workqueue)
659 * nobody should see this wq
660 * else // caller is CPU_UP_PREPARE
661 * cpu is not on cpu_online_map
662 * so we can abort safely.
668 cwq->should_stop = 0;
669 if (!is_single_threaded(wq))
670 kthread_bind(p, cpu);
672 if (is_single_threaded(wq) || cpu_online(cpu))
678 struct workqueue_struct *__create_workqueue(const char *name,
679 int singlethread, int freezeable)
681 struct workqueue_struct *wq;
682 struct cpu_workqueue_struct *cwq;
685 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
689 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
696 wq->freezeable = freezeable;
699 INIT_LIST_HEAD(&wq->list);
700 cwq = init_cpu_workqueue(wq, singlethread_cpu);
701 err = create_workqueue_thread(cwq, singlethread_cpu);
703 mutex_lock(&workqueue_mutex);
704 list_add(&wq->list, &workqueues);
706 for_each_possible_cpu(cpu) {
707 cwq = init_cpu_workqueue(wq, cpu);
708 if (err || !cpu_online(cpu))
710 err = create_workqueue_thread(cwq, cpu);
712 mutex_unlock(&workqueue_mutex);
716 destroy_workqueue(wq);
721 EXPORT_SYMBOL_GPL(__create_workqueue);
723 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
725 struct wq_barrier barr;
728 spin_lock_irq(&cwq->lock);
729 if (cwq->thread != NULL) {
730 insert_wq_barrier(cwq, &barr, 1);
731 cwq->should_stop = 1;
734 spin_unlock_irq(&cwq->lock);
737 wait_for_completion(&barr.done);
739 while (unlikely(cwq->thread != NULL))
742 * Wait until cwq->thread unlocks cwq->lock,
743 * it won't touch *cwq after that.
746 spin_unlock_wait(&cwq->lock);
751 * destroy_workqueue - safely terminate a workqueue
752 * @wq: target workqueue
754 * Safely destroy a workqueue. All work currently pending will be done first.
756 void destroy_workqueue(struct workqueue_struct *wq)
758 struct cpu_workqueue_struct *cwq;
760 if (is_single_threaded(wq)) {
761 cwq = per_cpu_ptr(wq->cpu_wq, singlethread_cpu);
762 cleanup_workqueue_thread(cwq, singlethread_cpu);
766 mutex_lock(&workqueue_mutex);
768 mutex_unlock(&workqueue_mutex);
770 for_each_cpu_mask(cpu, cpu_populated_map) {
771 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
772 cleanup_workqueue_thread(cwq, cpu);
776 free_percpu(wq->cpu_wq);
779 EXPORT_SYMBOL_GPL(destroy_workqueue);
781 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
782 unsigned long action,
785 unsigned int cpu = (unsigned long)hcpu;
786 struct cpu_workqueue_struct *cwq;
787 struct workqueue_struct *wq;
790 case CPU_LOCK_ACQUIRE:
791 mutex_lock(&workqueue_mutex);
794 case CPU_LOCK_RELEASE:
795 mutex_unlock(&workqueue_mutex);
799 cpu_set(cpu, cpu_populated_map);
802 list_for_each_entry(wq, &workqueues, list) {
803 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
807 if (!create_workqueue_thread(cwq, cpu))
809 printk(KERN_ERR "workqueue for %i failed\n", cpu);
813 wake_up_process(cwq->thread);
816 case CPU_UP_CANCELED:
818 wake_up_process(cwq->thread);
820 cleanup_workqueue_thread(cwq, cpu);
828 void init_workqueues(void)
830 cpu_populated_map = cpu_online_map;
831 singlethread_cpu = first_cpu(cpu_possible_map);
832 hotcpu_notifier(workqueue_cpu_callback, 0);
833 keventd_wq = create_workqueue("events");