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>
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.
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 */
67 struct lockdep_map lockdep_map
;
71 /* Serializes the accesses to the list of workqueues. */
72 static DEFINE_SPINLOCK(workqueue_lock
);
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
, struct list_head
*head
)
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().
137 list_add_tail(&work
->entry
, head
);
138 wake_up(&cwq
->more_work
);
141 static void __queue_work(struct cpu_workqueue_struct
*cwq
,
142 struct work_struct
*work
)
146 spin_lock_irqsave(&cwq
->lock
, flags
);
147 insert_work(cwq
, work
, &cwq
->worklist
);
148 spin_unlock_irqrestore(&cwq
->lock
, flags
);
152 * queue_work - queue work on a workqueue
153 * @wq: workqueue to use
154 * @work: work to queue
156 * Returns 0 if @work was already on a queue, non-zero otherwise.
158 * We queue the work to the CPU on which it was submitted, but if the CPU dies
159 * it can be processed by another CPU.
161 int queue_work(struct workqueue_struct
*wq
, struct work_struct
*work
)
165 ret
= queue_work_on(get_cpu(), wq
, work
);
170 EXPORT_SYMBOL_GPL(queue_work
);
173 * queue_work_on - queue work on specific cpu
174 * @cpu: CPU number to execute work on
175 * @wq: workqueue to use
176 * @work: work to queue
178 * Returns 0 if @work was already on a queue, non-zero otherwise.
180 * We queue the work to a specific CPU, the caller must ensure it
184 queue_work_on(int cpu
, struct workqueue_struct
*wq
, struct work_struct
*work
)
188 if (!test_and_set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
))) {
189 BUG_ON(!list_empty(&work
->entry
));
190 __queue_work(wq_per_cpu(wq
, cpu
), work
);
195 EXPORT_SYMBOL_GPL(queue_work_on
);
197 static void delayed_work_timer_fn(unsigned long __data
)
199 struct delayed_work
*dwork
= (struct delayed_work
*)__data
;
200 struct cpu_workqueue_struct
*cwq
= get_wq_data(&dwork
->work
);
201 struct workqueue_struct
*wq
= cwq
->wq
;
203 __queue_work(wq_per_cpu(wq
, smp_processor_id()), &dwork
->work
);
207 * queue_delayed_work - queue work on a workqueue after delay
208 * @wq: workqueue to use
209 * @dwork: delayable work to queue
210 * @delay: number of jiffies to wait before queueing
212 * Returns 0 if @work was already on a queue, non-zero otherwise.
214 int queue_delayed_work(struct workqueue_struct
*wq
,
215 struct delayed_work
*dwork
, unsigned long delay
)
218 return queue_work(wq
, &dwork
->work
);
220 return queue_delayed_work_on(-1, wq
, dwork
, delay
);
222 EXPORT_SYMBOL_GPL(queue_delayed_work
);
225 * queue_delayed_work_on - queue work on specific CPU after delay
226 * @cpu: CPU number to execute work on
227 * @wq: workqueue to use
228 * @dwork: work to queue
229 * @delay: number of jiffies to wait before queueing
231 * Returns 0 if @work was already on a queue, non-zero otherwise.
233 int queue_delayed_work_on(int cpu
, struct workqueue_struct
*wq
,
234 struct delayed_work
*dwork
, unsigned long delay
)
237 struct timer_list
*timer
= &dwork
->timer
;
238 struct work_struct
*work
= &dwork
->work
;
240 if (!test_and_set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
))) {
241 BUG_ON(timer_pending(timer
));
242 BUG_ON(!list_empty(&work
->entry
));
244 timer_stats_timer_set_start_info(&dwork
->timer
);
246 /* This stores cwq for the moment, for the timer_fn */
247 set_wq_data(work
, wq_per_cpu(wq
, raw_smp_processor_id()));
248 timer
->expires
= jiffies
+ delay
;
249 timer
->data
= (unsigned long)dwork
;
250 timer
->function
= delayed_work_timer_fn
;
252 if (unlikely(cpu
>= 0))
253 add_timer_on(timer
, cpu
);
260 EXPORT_SYMBOL_GPL(queue_delayed_work_on
);
262 static void run_workqueue(struct cpu_workqueue_struct
*cwq
)
264 spin_lock_irq(&cwq
->lock
);
266 if (cwq
->run_depth
> 3) {
267 /* morton gets to eat his hat */
268 printk("%s: recursion depth exceeded: %d\n",
269 __func__
, cwq
->run_depth
);
272 while (!list_empty(&cwq
->worklist
)) {
273 struct work_struct
*work
= list_entry(cwq
->worklist
.next
,
274 struct work_struct
, entry
);
275 work_func_t f
= work
->func
;
276 #ifdef CONFIG_LOCKDEP
278 * It is permissible to free the struct work_struct
279 * from inside the function that is called from it,
280 * this we need to take into account for lockdep too.
281 * To avoid bogus "held lock freed" warnings as well
282 * as problems when looking into work->lockdep_map,
283 * make a copy and use that here.
285 struct lockdep_map lockdep_map
= work
->lockdep_map
;
288 cwq
->current_work
= work
;
289 list_del_init(cwq
->worklist
.next
);
290 spin_unlock_irq(&cwq
->lock
);
292 BUG_ON(get_wq_data(work
) != cwq
);
293 work_clear_pending(work
);
294 lock_map_acquire(&cwq
->wq
->lockdep_map
);
295 lock_map_acquire(&lockdep_map
);
297 lock_map_release(&lockdep_map
);
298 lock_map_release(&cwq
->wq
->lockdep_map
);
300 if (unlikely(in_atomic() || lockdep_depth(current
) > 0)) {
301 printk(KERN_ERR
"BUG: workqueue leaked lock or atomic: "
303 current
->comm
, preempt_count(),
304 task_pid_nr(current
));
305 printk(KERN_ERR
" last function: ");
306 print_symbol("%s\n", (unsigned long)f
);
307 debug_show_held_locks(current
);
311 spin_lock_irq(&cwq
->lock
);
312 cwq
->current_work
= NULL
;
315 spin_unlock_irq(&cwq
->lock
);
318 static int worker_thread(void *__cwq
)
320 struct cpu_workqueue_struct
*cwq
= __cwq
;
323 if (cwq
->wq
->freezeable
)
326 set_user_nice(current
, -5);
329 prepare_to_wait(&cwq
->more_work
, &wait
, TASK_INTERRUPTIBLE
);
330 if (!freezing(current
) &&
331 !kthread_should_stop() &&
332 list_empty(&cwq
->worklist
))
334 finish_wait(&cwq
->more_work
, &wait
);
338 if (kthread_should_stop())
348 struct work_struct work
;
349 struct completion done
;
352 static void wq_barrier_func(struct work_struct
*work
)
354 struct wq_barrier
*barr
= container_of(work
, struct wq_barrier
, work
);
355 complete(&barr
->done
);
358 static void insert_wq_barrier(struct cpu_workqueue_struct
*cwq
,
359 struct wq_barrier
*barr
, struct list_head
*head
)
361 INIT_WORK(&barr
->work
, wq_barrier_func
);
362 __set_bit(WORK_STRUCT_PENDING
, work_data_bits(&barr
->work
));
364 init_completion(&barr
->done
);
366 insert_work(cwq
, &barr
->work
, head
);
369 static int flush_cpu_workqueue(struct cpu_workqueue_struct
*cwq
)
373 if (cwq
->thread
== current
) {
375 * Probably keventd trying to flush its own queue. So simply run
376 * it by hand rather than deadlocking.
381 struct wq_barrier barr
;
384 spin_lock_irq(&cwq
->lock
);
385 if (!list_empty(&cwq
->worklist
) || cwq
->current_work
!= NULL
) {
386 insert_wq_barrier(cwq
, &barr
, &cwq
->worklist
);
389 spin_unlock_irq(&cwq
->lock
);
392 wait_for_completion(&barr
.done
);
399 * flush_workqueue - ensure that any scheduled work has run to completion.
400 * @wq: workqueue to flush
402 * Forces execution of the workqueue and blocks until its completion.
403 * This is typically used in driver shutdown handlers.
405 * We sleep until all works which were queued on entry have been handled,
406 * but we are not livelocked by new incoming ones.
408 * This function used to run the workqueues itself. Now we just wait for the
409 * helper threads to do it.
411 void flush_workqueue(struct workqueue_struct
*wq
)
413 const cpumask_t
*cpu_map
= wq_cpu_map(wq
);
417 lock_map_acquire(&wq
->lockdep_map
);
418 lock_map_release(&wq
->lockdep_map
);
419 for_each_cpu_mask_nr(cpu
, *cpu_map
)
420 flush_cpu_workqueue(per_cpu_ptr(wq
->cpu_wq
, cpu
));
422 EXPORT_SYMBOL_GPL(flush_workqueue
);
425 * flush_work - block until a work_struct's callback has terminated
426 * @work: the work which is to be flushed
428 * Returns false if @work has already terminated.
430 * It is expected that, prior to calling flush_work(), the caller has
431 * arranged for the work to not be requeued, otherwise it doesn't make
432 * sense to use this function.
434 int flush_work(struct work_struct
*work
)
436 struct cpu_workqueue_struct
*cwq
;
437 struct list_head
*prev
;
438 struct wq_barrier barr
;
441 cwq
= get_wq_data(work
);
445 lock_map_acquire(&cwq
->wq
->lockdep_map
);
446 lock_map_release(&cwq
->wq
->lockdep_map
);
449 spin_lock_irq(&cwq
->lock
);
450 if (!list_empty(&work
->entry
)) {
452 * See the comment near try_to_grab_pending()->smp_rmb().
453 * If it was re-queued under us we are not going to wait.
456 if (unlikely(cwq
!= get_wq_data(work
)))
460 if (cwq
->current_work
!= work
)
462 prev
= &cwq
->worklist
;
464 insert_wq_barrier(cwq
, &barr
, prev
->next
);
466 spin_unlock_irq(&cwq
->lock
);
470 wait_for_completion(&barr
.done
);
473 EXPORT_SYMBOL_GPL(flush_work
);
476 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
477 * so this work can't be re-armed in any way.
479 static int try_to_grab_pending(struct work_struct
*work
)
481 struct cpu_workqueue_struct
*cwq
;
484 if (!test_and_set_bit(WORK_STRUCT_PENDING
, work_data_bits(work
)))
488 * The queueing is in progress, or it is already queued. Try to
489 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
492 cwq
= get_wq_data(work
);
496 spin_lock_irq(&cwq
->lock
);
497 if (!list_empty(&work
->entry
)) {
499 * This work is queued, but perhaps we locked the wrong cwq.
500 * In that case we must see the new value after rmb(), see
501 * insert_work()->wmb().
504 if (cwq
== get_wq_data(work
)) {
505 list_del_init(&work
->entry
);
509 spin_unlock_irq(&cwq
->lock
);
514 static void wait_on_cpu_work(struct cpu_workqueue_struct
*cwq
,
515 struct work_struct
*work
)
517 struct wq_barrier barr
;
520 spin_lock_irq(&cwq
->lock
);
521 if (unlikely(cwq
->current_work
== work
)) {
522 insert_wq_barrier(cwq
, &barr
, cwq
->worklist
.next
);
525 spin_unlock_irq(&cwq
->lock
);
527 if (unlikely(running
))
528 wait_for_completion(&barr
.done
);
531 static void wait_on_work(struct work_struct
*work
)
533 struct cpu_workqueue_struct
*cwq
;
534 struct workqueue_struct
*wq
;
535 const cpumask_t
*cpu_map
;
540 lock_map_acquire(&work
->lockdep_map
);
541 lock_map_release(&work
->lockdep_map
);
543 cwq
= get_wq_data(work
);
548 cpu_map
= wq_cpu_map(wq
);
550 for_each_cpu_mask_nr(cpu
, *cpu_map
)
551 wait_on_cpu_work(per_cpu_ptr(wq
->cpu_wq
, cpu
), work
);
554 static int __cancel_work_timer(struct work_struct
*work
,
555 struct timer_list
* timer
)
560 ret
= (timer
&& likely(del_timer(timer
)));
562 ret
= try_to_grab_pending(work
);
564 } while (unlikely(ret
< 0));
566 work_clear_pending(work
);
571 * cancel_work_sync - block until a work_struct's callback has terminated
572 * @work: the work which is to be flushed
574 * Returns true if @work was pending.
576 * cancel_work_sync() will cancel the work if it is queued. If the work's
577 * callback appears to be running, cancel_work_sync() will block until it
580 * It is possible to use this function if the work re-queues itself. It can
581 * cancel the work even if it migrates to another workqueue, however in that
582 * case it only guarantees that work->func() has completed on the last queued
585 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
586 * pending, otherwise it goes into a busy-wait loop until the timer expires.
588 * The caller must ensure that workqueue_struct on which this work was last
589 * queued can't be destroyed before this function returns.
591 int cancel_work_sync(struct work_struct
*work
)
593 return __cancel_work_timer(work
, NULL
);
595 EXPORT_SYMBOL_GPL(cancel_work_sync
);
598 * cancel_delayed_work_sync - reliably kill off a delayed work.
599 * @dwork: the delayed work struct
601 * Returns true if @dwork was pending.
603 * It is possible to use this function if @dwork rearms itself via queue_work()
604 * or queue_delayed_work(). See also the comment for cancel_work_sync().
606 int cancel_delayed_work_sync(struct delayed_work
*dwork
)
608 return __cancel_work_timer(&dwork
->work
, &dwork
->timer
);
610 EXPORT_SYMBOL(cancel_delayed_work_sync
);
612 static struct workqueue_struct
*keventd_wq __read_mostly
;
615 * schedule_work - put work task in global workqueue
616 * @work: job to be done
618 * This puts a job in the kernel-global workqueue.
620 int schedule_work(struct work_struct
*work
)
622 return queue_work(keventd_wq
, work
);
624 EXPORT_SYMBOL(schedule_work
);
627 * schedule_work_on - put work task on a specific cpu
628 * @cpu: cpu to put the work task on
629 * @work: job to be done
631 * This puts a job on a specific cpu
633 int schedule_work_on(int cpu
, struct work_struct
*work
)
635 return queue_work_on(cpu
, keventd_wq
, work
);
637 EXPORT_SYMBOL(schedule_work_on
);
640 * schedule_delayed_work - put work task in global workqueue after delay
641 * @dwork: job to be done
642 * @delay: number of jiffies to wait or 0 for immediate execution
644 * After waiting for a given time this puts a job in the kernel-global
647 int schedule_delayed_work(struct delayed_work
*dwork
,
650 return queue_delayed_work(keventd_wq
, dwork
, delay
);
652 EXPORT_SYMBOL(schedule_delayed_work
);
655 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
657 * @dwork: job to be done
658 * @delay: number of jiffies to wait
660 * After waiting for a given time this puts a job in the kernel-global
661 * workqueue on the specified CPU.
663 int schedule_delayed_work_on(int cpu
,
664 struct delayed_work
*dwork
, unsigned long delay
)
666 return queue_delayed_work_on(cpu
, keventd_wq
, dwork
, delay
);
668 EXPORT_SYMBOL(schedule_delayed_work_on
);
671 * schedule_on_each_cpu - call a function on each online CPU from keventd
672 * @func: the function to call
674 * Returns zero on success.
675 * Returns -ve errno on failure.
677 * schedule_on_each_cpu() is very slow.
679 int schedule_on_each_cpu(work_func_t func
)
682 struct work_struct
*works
;
684 works
= alloc_percpu(struct work_struct
);
689 for_each_online_cpu(cpu
) {
690 struct work_struct
*work
= per_cpu_ptr(works
, cpu
);
692 INIT_WORK(work
, func
);
693 schedule_work_on(cpu
, work
);
695 for_each_online_cpu(cpu
)
696 flush_work(per_cpu_ptr(works
, cpu
));
702 void flush_scheduled_work(void)
704 flush_workqueue(keventd_wq
);
706 EXPORT_SYMBOL(flush_scheduled_work
);
709 * execute_in_process_context - reliably execute the routine with user context
710 * @fn: the function to execute
711 * @ew: guaranteed storage for the execute work structure (must
712 * be available when the work executes)
714 * Executes the function immediately if process context is available,
715 * otherwise schedules the function for delayed execution.
717 * Returns: 0 - function was executed
718 * 1 - function was scheduled for execution
720 int execute_in_process_context(work_func_t fn
, struct execute_work
*ew
)
722 if (!in_interrupt()) {
727 INIT_WORK(&ew
->work
, fn
);
728 schedule_work(&ew
->work
);
732 EXPORT_SYMBOL_GPL(execute_in_process_context
);
736 return keventd_wq
!= NULL
;
739 int current_is_keventd(void)
741 struct cpu_workqueue_struct
*cwq
;
742 int cpu
= raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
747 cwq
= per_cpu_ptr(keventd_wq
->cpu_wq
, cpu
);
748 if (current
== cwq
->thread
)
755 static struct cpu_workqueue_struct
*
756 init_cpu_workqueue(struct workqueue_struct
*wq
, int cpu
)
758 struct cpu_workqueue_struct
*cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
761 spin_lock_init(&cwq
->lock
);
762 INIT_LIST_HEAD(&cwq
->worklist
);
763 init_waitqueue_head(&cwq
->more_work
);
768 static int create_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
770 struct sched_param param
= { .sched_priority
= MAX_RT_PRIO
-1 };
771 struct workqueue_struct
*wq
= cwq
->wq
;
772 const char *fmt
= is_single_threaded(wq
) ? "%s" : "%s/%d";
773 struct task_struct
*p
;
775 p
= kthread_create(worker_thread
, cwq
, fmt
, wq
->name
, cpu
);
777 * Nobody can add the work_struct to this cwq,
778 * if (caller is __create_workqueue)
779 * nobody should see this wq
780 * else // caller is CPU_UP_PREPARE
781 * cpu is not on cpu_online_map
782 * so we can abort safely.
787 sched_setscheduler_nocheck(p
, SCHED_FIFO
, ¶m
);
793 static void start_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
795 struct task_struct
*p
= cwq
->thread
;
799 kthread_bind(p
, cpu
);
804 struct workqueue_struct
*__create_workqueue_key(const char *name
,
808 struct lock_class_key
*key
,
809 const char *lock_name
)
811 struct workqueue_struct
*wq
;
812 struct cpu_workqueue_struct
*cwq
;
815 wq
= kzalloc(sizeof(*wq
), GFP_KERNEL
);
819 wq
->cpu_wq
= alloc_percpu(struct cpu_workqueue_struct
);
826 lockdep_init_map(&wq
->lockdep_map
, lock_name
, key
, 0);
827 wq
->singlethread
= singlethread
;
828 wq
->freezeable
= freezeable
;
830 INIT_LIST_HEAD(&wq
->list
);
833 cwq
= init_cpu_workqueue(wq
, singlethread_cpu
);
834 err
= create_workqueue_thread(cwq
, singlethread_cpu
);
835 start_workqueue_thread(cwq
, -1);
837 cpu_maps_update_begin();
839 * We must place this wq on list even if the code below fails.
840 * cpu_down(cpu) can remove cpu from cpu_populated_map before
841 * destroy_workqueue() takes the lock, in that case we leak
844 spin_lock(&workqueue_lock
);
845 list_add(&wq
->list
, &workqueues
);
846 spin_unlock(&workqueue_lock
);
848 * We must initialize cwqs for each possible cpu even if we
849 * are going to call destroy_workqueue() finally. Otherwise
850 * cpu_up() can hit the uninitialized cwq once we drop the
853 for_each_possible_cpu(cpu
) {
854 cwq
= init_cpu_workqueue(wq
, cpu
);
855 if (err
|| !cpu_online(cpu
))
857 err
= create_workqueue_thread(cwq
, cpu
);
858 start_workqueue_thread(cwq
, cpu
);
860 cpu_maps_update_done();
864 destroy_workqueue(wq
);
869 EXPORT_SYMBOL_GPL(__create_workqueue_key
);
871 static void cleanup_workqueue_thread(struct cpu_workqueue_struct
*cwq
)
874 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
875 * cpu_add_remove_lock protects cwq->thread.
877 if (cwq
->thread
== NULL
)
880 lock_map_acquire(&cwq
->wq
->lockdep_map
);
881 lock_map_release(&cwq
->wq
->lockdep_map
);
883 flush_cpu_workqueue(cwq
);
885 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
886 * a concurrent flush_workqueue() can insert a barrier after us.
887 * However, in that case run_workqueue() won't return and check
888 * kthread_should_stop() until it flushes all work_struct's.
889 * When ->worklist becomes empty it is safe to exit because no
890 * more work_structs can be queued on this cwq: flush_workqueue
891 * checks list_empty(), and a "normal" queue_work() can't use
894 kthread_stop(cwq
->thread
);
899 * destroy_workqueue - safely terminate a workqueue
900 * @wq: target workqueue
902 * Safely destroy a workqueue. All work currently pending will be done first.
904 void destroy_workqueue(struct workqueue_struct
*wq
)
906 const cpumask_t
*cpu_map
= wq_cpu_map(wq
);
909 cpu_maps_update_begin();
910 spin_lock(&workqueue_lock
);
912 spin_unlock(&workqueue_lock
);
914 for_each_cpu_mask_nr(cpu
, *cpu_map
)
915 cleanup_workqueue_thread(per_cpu_ptr(wq
->cpu_wq
, cpu
));
916 cpu_maps_update_done();
918 free_percpu(wq
->cpu_wq
);
921 EXPORT_SYMBOL_GPL(destroy_workqueue
);
923 static int __devinit
workqueue_cpu_callback(struct notifier_block
*nfb
,
924 unsigned long action
,
927 unsigned int cpu
= (unsigned long)hcpu
;
928 struct cpu_workqueue_struct
*cwq
;
929 struct workqueue_struct
*wq
;
932 action
&= ~CPU_TASKS_FROZEN
;
936 cpu_set(cpu
, cpu_populated_map
);
939 list_for_each_entry(wq
, &workqueues
, list
) {
940 cwq
= per_cpu_ptr(wq
->cpu_wq
, cpu
);
944 if (!create_workqueue_thread(cwq
, cpu
))
946 printk(KERN_ERR
"workqueue [%s] for %i failed\n",
948 action
= CPU_UP_CANCELED
;
953 start_workqueue_thread(cwq
, cpu
);
956 case CPU_UP_CANCELED
:
957 start_workqueue_thread(cwq
, -1);
959 cleanup_workqueue_thread(cwq
);
965 case CPU_UP_CANCELED
:
967 cpu_clear(cpu
, cpu_populated_map
);
974 struct work_for_cpu
{
975 struct work_struct work
;
981 static void do_work_for_cpu(struct work_struct
*w
)
983 struct work_for_cpu
*wfc
= container_of(w
, struct work_for_cpu
, work
);
985 wfc
->ret
= wfc
->fn(wfc
->arg
);
989 * work_on_cpu - run a function in user context on a particular cpu
990 * @cpu: the cpu to run on
991 * @fn: the function to run
992 * @arg: the function arg
994 * This will return -EINVAL in the cpu is not online, or the return value
997 long work_on_cpu(unsigned int cpu
, long (*fn
)(void *), void *arg
)
999 struct work_for_cpu wfc
;
1001 INIT_WORK(&wfc
.work
, do_work_for_cpu
);
1005 if (unlikely(!cpu_online(cpu
)))
1008 schedule_work_on(cpu
, &wfc
.work
);
1009 flush_work(&wfc
.work
);
1015 EXPORT_SYMBOL_GPL(work_on_cpu
);
1016 #endif /* CONFIG_SMP */
1018 void __init
init_workqueues(void)
1020 cpu_populated_map
= cpu_online_map
;
1021 singlethread_cpu
= first_cpu(cpu_possible_map
);
1022 cpu_singlethread_map
= cpumask_of_cpu(singlethread_cpu
);
1023 hotcpu_notifier(workqueue_cpu_callback
, 0);
1024 keventd_wq
= create_workqueue("events");
1025 BUG_ON(!keventd_wq
);