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 * Structure fields follow one of the following exclusion rules.
40 * I: Set during initialization and read-only afterwards.
42 * L: cwq->lock protected. Access with cwq->lock held.
44 * W: workqueue_lock protected.
48 * The per-CPU workqueue (if single thread, we always use the first
51 struct cpu_workqueue_struct
{
55 struct list_head worklist
;
56 wait_queue_head_t more_work
;
57 struct work_struct
*current_work
;
60 struct workqueue_struct
*wq
; /* I: the owning workqueue */
61 struct task_struct
*thread
;
62 } ____cacheline_aligned
;
65 * The externally visible workqueue abstraction is an array of
68 struct workqueue_struct
{
69 unsigned int flags
; /* I: WQ_* flags */
70 struct cpu_workqueue_struct
*cpu_wq
; /* I: cwq's */
71 struct list_head list
; /* W: list of all workqueues */
72 const char *name
; /* I: workqueue name */
74 struct lockdep_map lockdep_map
;
78 #ifdef CONFIG_DEBUG_OBJECTS_WORK
80 static struct debug_obj_descr work_debug_descr
;
83 * fixup_init is called when:
84 * - an active object is initialized
86 static int work_fixup_init(void *addr
, enum debug_obj_state state
)
88 struct work_struct
*work
= addr
;
91 case ODEBUG_STATE_ACTIVE
:
92 cancel_work_sync(work
);
93 debug_object_init(work
, &work_debug_descr
);
101 * fixup_activate is called when:
102 * - an active object is activated
103 * - an unknown object is activated (might be a statically initialized object)
105 static int work_fixup_activate(void *addr
, enum debug_obj_state state
)
107 struct work_struct
*work
= addr
;
111 case ODEBUG_STATE_NOTAVAILABLE
:
113 * This is not really a fixup. The work struct was
114 * statically initialized. We just make sure that it
115 * is tracked in the object tracker.
117 if (test_bit(WORK_STRUCT_STATIC_BIT
, work_data_bits(work
))) {
118 debug_object_init(work
, &work_debug_descr
);
119 debug_object_activate(work
, &work_debug_descr
);
125 case ODEBUG_STATE_ACTIVE
:
134 * fixup_free is called when:
135 * - an active object is freed
137 static int work_fixup_free(void *addr
, enum debug_obj_state state
)
139 struct work_struct
*work
= addr
;
142 case ODEBUG_STATE_ACTIVE
:
143 cancel_work_sync(work
);
144 debug_object_free(work
, &work_debug_descr
);
151 static struct debug_obj_descr work_debug_descr
= {
152 .name
= "work_struct",
153 .fixup_init
= work_fixup_init
,
154 .fixup_activate
= work_fixup_activate
,
155 .fixup_free
= work_fixup_free
,
158 static inline void debug_work_activate(struct work_struct
*work
)
160 debug_object_activate(work
, &work_debug_descr
);
163 static inline void debug_work_deactivate(struct work_struct
*work
)
165 debug_object_deactivate(work
, &work_debug_descr
);
168 void __init_work(struct work_struct
*work
, int onstack
)
171 debug_object_init_on_stack(work
, &work_debug_descr
);
173 debug_object_init(work
, &work_debug_descr
);
175 EXPORT_SYMBOL_GPL(__init_work
);
177 void destroy_work_on_stack(struct work_struct
*work
)
179 debug_object_free(work
, &work_debug_descr
);
181 EXPORT_SYMBOL_GPL(destroy_work_on_stack
);
184 static inline void debug_work_activate(struct work_struct
*work
) { }
185 static inline void debug_work_deactivate(struct work_struct
*work
) { }
188 /* Serializes the accesses to the list of workqueues. */
189 static DEFINE_SPINLOCK(workqueue_lock
);
190 static LIST_HEAD(workqueues
);
192 static int singlethread_cpu __read_mostly
;
194 static struct cpu_workqueue_struct
*get_cwq(unsigned int cpu
,
195 struct workqueue_struct
*wq
)
197 return per_cpu_ptr(wq
->cpu_wq
, cpu
);
200 static struct cpu_workqueue_struct
*target_cwq(unsigned int cpu
,
201 struct workqueue_struct
*wq
)
203 if (unlikely(wq
->flags
& WQ_SINGLE_THREAD
))
204 cpu
= singlethread_cpu
;
205 return get_cwq(cpu
, wq
);
209 * Set the workqueue on which a work item is to be run
210 * - Must *only* be called if the pending flag is set
212 static inline void set_wq_data(struct work_struct
*work
,
213 struct cpu_workqueue_struct
*cwq
,
214 unsigned long extra_flags
)
216 BUG_ON(!work_pending(work
));
218 atomic_long_set(&work
->data
, (unsigned long)cwq
| work_static(work
) |
219 WORK_STRUCT_PENDING
| extra_flags
);
223 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
225 static inline void clear_wq_data(struct work_struct
*work
)
227 atomic_long_set(&work
->data
, work_static(work
));
230 static inline struct cpu_workqueue_struct
*get_wq_data(struct work_struct
*work
)
232 return (void *)(atomic_long_read(&work
->data
) &
233 WORK_STRUCT_WQ_DATA_MASK
);
237 * insert_work - insert a work into cwq
238 * @cwq: cwq @work belongs to
239 * @work: work to insert
240 * @head: insertion point
241 * @extra_flags: extra WORK_STRUCT_* flags to set
243 * Insert @work into @cwq after @head.
246 * spin_lock_irq(cwq->lock).
248 static void insert_work(struct cpu_workqueue_struct
*cwq
,
249 struct work_struct
*work
, struct list_head
*head
,
250 unsigned int extra_flags
)
252 /* we own @work, set data and link */
253 set_wq_data(work
, cwq
, extra_flags
);
256 * Ensure that we get the right work->data if we see the
257 * result of list_add() below, see try_to_grab_pending().
261 list_add_tail(&work
->entry
, head
);
262 wake_up(&cwq
->more_work
);
265 static void __queue_work(unsigned int cpu
, struct workqueue_struct
*wq
,
266 struct work_struct
*work
)
268 struct cpu_workqueue_struct
*cwq
= target_cwq(cpu
, wq
);
271 debug_work_activate(work
);
272 spin_lock_irqsave(&cwq
->lock
, flags
);
273 BUG_ON(!list_empty(&work
->entry
));
274 insert_work(cwq
, work
, &cwq
->worklist
, 0);
275 spin_unlock_irqrestore(&cwq
->lock
, flags
);
279 * queue_work - queue work on a workqueue
280 * @wq: workqueue to use
281 * @work: work to queue
283 * Returns 0 if @work was already on a queue, non-zero otherwise.
285 * We queue the work to the CPU on which it was submitted, but if the CPU dies
286 * it can be processed by another CPU.
288 int queue_work(struct workqueue_struct
*wq
, struct work_struct
*work
)
292 ret
= queue_work_on(get_cpu(), wq
, work
);
297 EXPORT_SYMBOL_GPL(queue_work
);
300 * queue_work_on - queue work on specific cpu
301 * @cpu: CPU number to execute work on
302 * @wq: workqueue to use
303 * @work: work to queue
305 * Returns 0 if @work was already on a queue, non-zero otherwise.
307 * We queue the work to a specific CPU, the caller must ensure it
311 queue_work_on(int cpu
, struct workqueue_struct
*wq
, struct work_struct
*work
)
315 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(work
))) {
316 __queue_work(cpu
, wq
, work
);
321 EXPORT_SYMBOL_GPL(queue_work_on
);
323 static void delayed_work_timer_fn(unsigned long __data
)
325 struct delayed_work
*dwork
= (struct delayed_work
*)__data
;
326 struct cpu_workqueue_struct
*cwq
= get_wq_data(&dwork
->work
);
328 __queue_work(smp_processor_id(), cwq
->wq
, &dwork
->work
);
332 * queue_delayed_work - queue work on a workqueue after delay
333 * @wq: workqueue to use
334 * @dwork: delayable work to queue
335 * @delay: number of jiffies to wait before queueing
337 * Returns 0 if @work was already on a queue, non-zero otherwise.
339 int queue_delayed_work(struct workqueue_struct
*wq
,
340 struct delayed_work
*dwork
, unsigned long delay
)
343 return queue_work(wq
, &dwork
->work
);
345 return queue_delayed_work_on(-1, wq
, dwork
, delay
);
347 EXPORT_SYMBOL_GPL(queue_delayed_work
);
350 * queue_delayed_work_on - queue work on specific CPU after delay
351 * @cpu: CPU number to execute work on
352 * @wq: workqueue to use
353 * @dwork: work to queue
354 * @delay: number of jiffies to wait before queueing
356 * Returns 0 if @work was already on a queue, non-zero otherwise.
358 int queue_delayed_work_on(int cpu
, struct workqueue_struct
*wq
,
359 struct delayed_work
*dwork
, unsigned long delay
)
362 struct timer_list
*timer
= &dwork
->timer
;
363 struct work_struct
*work
= &dwork
->work
;
365 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(work
))) {
366 BUG_ON(timer_pending(timer
));
367 BUG_ON(!list_empty(&work
->entry
));
369 timer_stats_timer_set_start_info(&dwork
->timer
);
371 /* This stores cwq for the moment, for the timer_fn */
372 set_wq_data(work
, target_cwq(raw_smp_processor_id(), wq
), 0);
373 timer
->expires
= jiffies
+ delay
;
374 timer
->data
= (unsigned long)dwork
;
375 timer
->function
= delayed_work_timer_fn
;
377 if (unlikely(cpu
>= 0))
378 add_timer_on(timer
, cpu
);
385 EXPORT_SYMBOL_GPL(queue_delayed_work_on
);
388 * process_one_work - process single work
389 * @cwq: cwq to process work for
390 * @work: work to process
392 * Process @work. This function contains all the logics necessary to
393 * process a single work including synchronization against and
394 * interaction with other workers on the same cpu, queueing and
395 * flushing. As long as context requirement is met, any worker can
396 * call this function to process a work.
399 * spin_lock_irq(cwq->lock) which is released and regrabbed.
401 static void process_one_work(struct cpu_workqueue_struct
*cwq
,
402 struct work_struct
*work
)
404 work_func_t f
= work
->func
;
405 #ifdef CONFIG_LOCKDEP
407 * It is permissible to free the struct work_struct from
408 * inside the function that is called from it, this we need to
409 * take into account for lockdep too. To avoid bogus "held
410 * lock freed" warnings as well as problems when looking into
411 * work->lockdep_map, make a copy and use that here.
413 struct lockdep_map lockdep_map
= work
->lockdep_map
;
415 /* claim and process */
416 debug_work_deactivate(work
);
417 cwq
->current_work
= work
;
418 list_del_init(&work
->entry
);
420 spin_unlock_irq(&cwq
->lock
);
422 BUG_ON(get_wq_data(work
) != cwq
);
423 work_clear_pending(work
);
424 lock_map_acquire(&cwq
->wq
->lockdep_map
);
425 lock_map_acquire(&lockdep_map
);
427 lock_map_release(&lockdep_map
);
428 lock_map_release(&cwq
->wq
->lockdep_map
);
430 if (unlikely(in_atomic() || lockdep_depth(current
) > 0)) {
431 printk(KERN_ERR
"BUG: workqueue leaked lock or atomic: "
433 current
->comm
, preempt_count(), task_pid_nr(current
));
434 printk(KERN_ERR
" last function: ");
435 print_symbol("%s\n", (unsigned long)f
);
436 debug_show_held_locks(current
);
440 spin_lock_irq(&cwq
->lock
);
442 /* we're done with it, release */
443 cwq
->current_work
= NULL
;
446 static void run_workqueue(struct cpu_workqueue_struct
*cwq
)
448 spin_lock_irq(&cwq
->lock
);
449 while (!list_empty(&cwq
->worklist
)) {
450 struct work_struct
*work
= list_entry(cwq
->worklist
.next
,
451 struct work_struct
, entry
);
452 process_one_work(cwq
, work
);
454 spin_unlock_irq(&cwq
->lock
);
458 * worker_thread - the worker thread function
459 * @__cwq: cwq to serve
461 * The cwq worker thread function.
463 static int worker_thread(void *__cwq
)
465 struct cpu_workqueue_struct
*cwq
= __cwq
;
468 if (cwq
->wq
->flags
& WQ_FREEZEABLE
)
472 prepare_to_wait(&cwq
->more_work
, &wait
, TASK_INTERRUPTIBLE
);
473 if (!freezing(current
) &&
474 !kthread_should_stop() &&
475 list_empty(&cwq
->worklist
))
477 finish_wait(&cwq
->more_work
, &wait
);
481 if (kthread_should_stop())
484 if (unlikely(!cpumask_equal(&cwq
->thread
->cpus_allowed
,
485 get_cpu_mask(cwq
->cpu
))))
486 set_cpus_allowed_ptr(cwq
->thread
,
487 get_cpu_mask(cwq
->cpu
));
495 struct work_struct work
;
496 struct completion done
;
499 static void wq_barrier_func(struct work_struct
*work
)
501 struct wq_barrier
*barr
= container_of(work
, struct wq_barrier
, work
);
502 complete(&barr
->done
);
506 * insert_wq_barrier - insert a barrier work
507 * @cwq: cwq to insert barrier into
508 * @barr: wq_barrier to insert
509 * @head: insertion point
511 * Insert barrier @barr into @cwq before @head.
514 * spin_lock_irq(cwq->lock).
516 static void insert_wq_barrier(struct cpu_workqueue_struct
*cwq
,
517 struct wq_barrier
*barr
, struct list_head
*head
)
520 * debugobject calls are safe here even with cwq->lock locked
521 * as we know for sure that this will not trigger any of the
522 * checks and call back into the fixup functions where we
525 INIT_WORK_ON_STACK(&barr
->work
, wq_barrier_func
);
526 __set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(&barr
->work
));
527 init_completion(&barr
->done
);
529 debug_work_activate(&barr
->work
);
530 insert_work(cwq
, &barr
->work
, head
, 0);
533 static int flush_cpu_workqueue(struct cpu_workqueue_struct
*cwq
)
536 struct wq_barrier barr
;
538 WARN_ON(cwq
->thread
== current
);
540 spin_lock_irq(&cwq
->lock
);
541 if (!list_empty(&cwq
->worklist
) || cwq
->current_work
!= NULL
) {
542 insert_wq_barrier(cwq
, &barr
, &cwq
->worklist
);
545 spin_unlock_irq(&cwq
->lock
);
548 wait_for_completion(&barr
.done
);
549 destroy_work_on_stack(&barr
.work
);
556 * flush_workqueue - ensure that any scheduled work has run to completion.
557 * @wq: workqueue to flush
559 * Forces execution of the workqueue and blocks until its completion.
560 * This is typically used in driver shutdown handlers.
562 * We sleep until all works which were queued on entry have been handled,
563 * but we are not livelocked by new incoming ones.
565 void flush_workqueue(struct workqueue_struct
*wq
)
570 lock_map_acquire(&wq
->lockdep_map
);
571 lock_map_release(&wq
->lockdep_map
);
572 for_each_possible_cpu(cpu
)
573 flush_cpu_workqueue(get_cwq(cpu
, wq
));
575 EXPORT_SYMBOL_GPL(flush_workqueue
);
578 * flush_work - block until a work_struct's callback has terminated
579 * @work: the work which is to be flushed
581 * Returns false if @work has already terminated.
583 * It is expected that, prior to calling flush_work(), the caller has
584 * arranged for the work to not be requeued, otherwise it doesn't make
585 * sense to use this function.
587 int flush_work(struct work_struct
*work
)
589 struct cpu_workqueue_struct
*cwq
;
590 struct list_head
*prev
;
591 struct wq_barrier barr
;
594 cwq
= get_wq_data(work
);
598 lock_map_acquire(&cwq
->wq
->lockdep_map
);
599 lock_map_release(&cwq
->wq
->lockdep_map
);
601 spin_lock_irq(&cwq
->lock
);
602 if (!list_empty(&work
->entry
)) {
604 * See the comment near try_to_grab_pending()->smp_rmb().
605 * If it was re-queued under us we are not going to wait.
608 if (unlikely(cwq
!= get_wq_data(work
)))
612 if (cwq
->current_work
!= work
)
614 prev
= &cwq
->worklist
;
616 insert_wq_barrier(cwq
, &barr
, prev
->next
);
618 spin_unlock_irq(&cwq
->lock
);
619 wait_for_completion(&barr
.done
);
620 destroy_work_on_stack(&barr
.work
);
623 spin_unlock_irq(&cwq
->lock
);
626 EXPORT_SYMBOL_GPL(flush_work
);
629 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
630 * so this work can't be re-armed in any way.
632 static int try_to_grab_pending(struct work_struct
*work
)
634 struct cpu_workqueue_struct
*cwq
;
637 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT
, work_data_bits(work
)))
641 * The queueing is in progress, or it is already queued. Try to
642 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
645 cwq
= get_wq_data(work
);
649 spin_lock_irq(&cwq
->lock
);
650 if (!list_empty(&work
->entry
)) {
652 * This work is queued, but perhaps we locked the wrong cwq.
653 * In that case we must see the new value after rmb(), see
654 * insert_work()->wmb().
657 if (cwq
== get_wq_data(work
)) {
658 debug_work_deactivate(work
);
659 list_del_init(&work
->entry
);
663 spin_unlock_irq(&cwq
->lock
);
668 static void wait_on_cpu_work(struct cpu_workqueue_struct
*cwq
,
669 struct work_struct
*work
)
671 struct wq_barrier barr
;
674 spin_lock_irq(&cwq
->lock
);
675 if (unlikely(cwq
->current_work
== work
)) {
676 insert_wq_barrier(cwq
, &barr
, cwq
->worklist
.next
);
679 spin_unlock_irq(&cwq
->lock
);
681 if (unlikely(running
)) {
682 wait_for_completion(&barr
.done
);
683 destroy_work_on_stack(&barr
.work
);
687 static void wait_on_work(struct work_struct
*work
)
689 struct cpu_workqueue_struct
*cwq
;
690 struct workqueue_struct
*wq
;
695 lock_map_acquire(&work
->lockdep_map
);
696 lock_map_release(&work
->lockdep_map
);
698 cwq
= get_wq_data(work
);
704 for_each_possible_cpu(cpu
)
705 wait_on_cpu_work(get_cwq(cpu
, wq
), work
);
708 static int __cancel_work_timer(struct work_struct
*work
,
709 struct timer_list
* timer
)
714 ret
= (timer
&& likely(del_timer(timer
)));
716 ret
= try_to_grab_pending(work
);
718 } while (unlikely(ret
< 0));
725 * cancel_work_sync - block until a work_struct's callback has terminated
726 * @work: the work which is to be flushed
728 * Returns true if @work was pending.
730 * cancel_work_sync() will cancel the work if it is queued. If the work's
731 * callback appears to be running, cancel_work_sync() will block until it
734 * It is possible to use this function if the work re-queues itself. It can
735 * cancel the work even if it migrates to another workqueue, however in that
736 * case it only guarantees that work->func() has completed on the last queued
739 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
740 * pending, otherwise it goes into a busy-wait loop until the timer expires.
742 * The caller must ensure that workqueue_struct on which this work was last
743 * queued can't be destroyed before this function returns.
745 int cancel_work_sync(struct work_struct
*work
)
747 return __cancel_work_timer(work
, NULL
);
749 EXPORT_SYMBOL_GPL(cancel_work_sync
);
752 * cancel_delayed_work_sync - reliably kill off a delayed work.
753 * @dwork: the delayed work struct
755 * Returns true if @dwork was pending.
757 * It is possible to use this function if @dwork rearms itself via queue_work()
758 * or queue_delayed_work(). See also the comment for cancel_work_sync().
760 int cancel_delayed_work_sync(struct delayed_work
*dwork
)
762 return __cancel_work_timer(&dwork
->work
, &dwork
->timer
);
764 EXPORT_SYMBOL(cancel_delayed_work_sync
);
766 static struct workqueue_struct
*keventd_wq __read_mostly
;
769 * schedule_work - put work task in global workqueue
770 * @work: job to be done
772 * Returns zero if @work was already on the kernel-global workqueue and
773 * non-zero otherwise.
775 * This puts a job in the kernel-global workqueue if it was not already
776 * queued and leaves it in the same position on the kernel-global
777 * workqueue otherwise.
779 int schedule_work(struct work_struct
*work
)
781 return queue_work(keventd_wq
, work
);
783 EXPORT_SYMBOL(schedule_work
);
786 * schedule_work_on - put work task on a specific cpu
787 * @cpu: cpu to put the work task on
788 * @work: job to be done
790 * This puts a job on a specific cpu
792 int schedule_work_on(int cpu
, struct work_struct
*work
)
794 return queue_work_on(cpu
, keventd_wq
, work
);
796 EXPORT_SYMBOL(schedule_work_on
);
799 * schedule_delayed_work - put work task in global workqueue after delay
800 * @dwork: job to be done
801 * @delay: number of jiffies to wait or 0 for immediate execution
803 * After waiting for a given time this puts a job in the kernel-global
806 int schedule_delayed_work(struct delayed_work
*dwork
,
809 return queue_delayed_work(keventd_wq
, dwork
, delay
);
811 EXPORT_SYMBOL(schedule_delayed_work
);
814 * flush_delayed_work - block until a dwork_struct's callback has terminated
815 * @dwork: the delayed work which is to be flushed
817 * Any timeout is cancelled, and any pending work is run immediately.
819 void flush_delayed_work(struct delayed_work
*dwork
)
821 if (del_timer_sync(&dwork
->timer
)) {
822 __queue_work(get_cpu(), get_wq_data(&dwork
->work
)->wq
,
826 flush_work(&dwork
->work
);
828 EXPORT_SYMBOL(flush_delayed_work
);
831 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
833 * @dwork: job to be done
834 * @delay: number of jiffies to wait
836 * After waiting for a given time this puts a job in the kernel-global
837 * workqueue on the specified CPU.
839 int schedule_delayed_work_on(int cpu
,
840 struct delayed_work
*dwork
, unsigned long delay
)
842 return queue_delayed_work_on(cpu
, keventd_wq
, dwork
, delay
);
844 EXPORT_SYMBOL(schedule_delayed_work_on
);
847 * schedule_on_each_cpu - call a function on each online CPU from keventd
848 * @func: the function to call
850 * Returns zero on success.
851 * Returns -ve errno on failure.
853 * schedule_on_each_cpu() is very slow.
855 int schedule_on_each_cpu(work_func_t func
)
859 struct work_struct
*works
;
861 works
= alloc_percpu(struct work_struct
);
868 * When running in keventd don't schedule a work item on
869 * itself. Can just call directly because the work queue is
870 * already bound. This also is faster.
872 if (current_is_keventd())
873 orig
= raw_smp_processor_id();
875 for_each_online_cpu(cpu
) {
876 struct work_struct
*work
= per_cpu_ptr(works
, cpu
);
878 INIT_WORK(work
, func
);
880 schedule_work_on(cpu
, work
);
883 func(per_cpu_ptr(works
, orig
));
885 for_each_online_cpu(cpu
)
886 flush_work(per_cpu_ptr(works
, cpu
));
894 * flush_scheduled_work - ensure that any scheduled work has run to completion.
896 * Forces execution of the kernel-global workqueue and blocks until its
899 * Think twice before calling this function! It's very easy to get into
900 * trouble if you don't take great care. Either of the following situations
901 * will lead to deadlock:
903 * One of the work items currently on the workqueue needs to acquire
904 * a lock held by your code or its caller.
906 * Your code is running in the context of a work routine.
908 * They will be detected by lockdep when they occur, but the first might not
909 * occur very often. It depends on what work items are on the workqueue and
910 * what locks they need, which you have no control over.
912 * In most situations flushing the entire workqueue is overkill; you merely
913 * need to know that a particular work item isn't queued and isn't running.
914 * In such cases you should use cancel_delayed_work_sync() or
915 * cancel_work_sync() instead.
917 void flush_scheduled_work(void)
919 flush_workqueue(keventd_wq
);
921 EXPORT_SYMBOL(flush_scheduled_work
);
924 * execute_in_process_context - reliably execute the routine with user context
925 * @fn: the function to execute
926 * @ew: guaranteed storage for the execute work structure (must
927 * be available when the work executes)
929 * Executes the function immediately if process context is available,
930 * otherwise schedules the function for delayed execution.
932 * Returns: 0 - function was executed
933 * 1 - function was scheduled for execution
935 int execute_in_process_context(work_func_t fn
, struct execute_work
*ew
)
937 if (!in_interrupt()) {
942 INIT_WORK(&ew
->work
, fn
);
943 schedule_work(&ew
->work
);
947 EXPORT_SYMBOL_GPL(execute_in_process_context
);
951 return keventd_wq
!= NULL
;
954 int current_is_keventd(void)
956 struct cpu_workqueue_struct
*cwq
;
957 int cpu
= raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
962 cwq
= get_cwq(cpu
, keventd_wq
);
963 if (current
== cwq
->thread
)
970 static int create_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
972 struct workqueue_struct
*wq
= cwq
->wq
;
973 struct task_struct
*p
;
975 p
= kthread_create(worker_thread
, cwq
, "%s/%d", wq
->name
, cpu
);
977 * Nobody can add the work_struct to this cwq,
978 * if (caller is __create_workqueue)
979 * nobody should see this wq
980 * else // caller is CPU_UP_PREPARE
981 * cpu is not on cpu_online_map
982 * so we can abort safely.
991 static void start_workqueue_thread(struct cpu_workqueue_struct
*cwq
, int cpu
)
993 struct task_struct
*p
= cwq
->thread
;
997 kthread_bind(p
, cpu
);
1002 struct workqueue_struct
*__create_workqueue_key(const char *name
,
1004 struct lock_class_key
*key
,
1005 const char *lock_name
)
1007 bool singlethread
= flags
& WQ_SINGLE_THREAD
;
1008 struct workqueue_struct
*wq
;
1011 wq
= kzalloc(sizeof(*wq
), GFP_KERNEL
);
1015 wq
->cpu_wq
= alloc_percpu(struct cpu_workqueue_struct
);
1021 lockdep_init_map(&wq
->lockdep_map
, lock_name
, key
, 0);
1022 INIT_LIST_HEAD(&wq
->list
);
1024 cpu_maps_update_begin();
1026 * We must initialize cwqs for each possible cpu even if we
1027 * are going to call destroy_workqueue() finally. Otherwise
1028 * cpu_up() can hit the uninitialized cwq once we drop the
1031 for_each_possible_cpu(cpu
) {
1032 struct cpu_workqueue_struct
*cwq
= get_cwq(cpu
, wq
);
1036 spin_lock_init(&cwq
->lock
);
1037 INIT_LIST_HEAD(&cwq
->worklist
);
1038 init_waitqueue_head(&cwq
->more_work
);
1042 err
= create_workqueue_thread(cwq
, cpu
);
1043 if (cpu_online(cpu
) && !singlethread
)
1044 start_workqueue_thread(cwq
, cpu
);
1046 start_workqueue_thread(cwq
, -1);
1049 spin_lock(&workqueue_lock
);
1050 list_add(&wq
->list
, &workqueues
);
1051 spin_unlock(&workqueue_lock
);
1053 cpu_maps_update_done();
1056 destroy_workqueue(wq
);
1062 free_percpu(wq
->cpu_wq
);
1067 EXPORT_SYMBOL_GPL(__create_workqueue_key
);
1069 static void cleanup_workqueue_thread(struct cpu_workqueue_struct
*cwq
)
1072 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
1073 * cpu_add_remove_lock protects cwq->thread.
1075 if (cwq
->thread
== NULL
)
1078 lock_map_acquire(&cwq
->wq
->lockdep_map
);
1079 lock_map_release(&cwq
->wq
->lockdep_map
);
1081 flush_cpu_workqueue(cwq
);
1083 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
1084 * a concurrent flush_workqueue() can insert a barrier after us.
1085 * However, in that case run_workqueue() won't return and check
1086 * kthread_should_stop() until it flushes all work_struct's.
1087 * When ->worklist becomes empty it is safe to exit because no
1088 * more work_structs can be queued on this cwq: flush_workqueue
1089 * checks list_empty(), and a "normal" queue_work() can't use
1092 kthread_stop(cwq
->thread
);
1097 * destroy_workqueue - safely terminate a workqueue
1098 * @wq: target workqueue
1100 * Safely destroy a workqueue. All work currently pending will be done first.
1102 void destroy_workqueue(struct workqueue_struct
*wq
)
1106 cpu_maps_update_begin();
1107 spin_lock(&workqueue_lock
);
1108 list_del(&wq
->list
);
1109 spin_unlock(&workqueue_lock
);
1110 cpu_maps_update_done();
1112 for_each_possible_cpu(cpu
)
1113 cleanup_workqueue_thread(get_cwq(cpu
, wq
));
1115 free_percpu(wq
->cpu_wq
);
1118 EXPORT_SYMBOL_GPL(destroy_workqueue
);
1120 static int __devinit
workqueue_cpu_callback(struct notifier_block
*nfb
,
1121 unsigned long action
,
1124 unsigned int cpu
= (unsigned long)hcpu
;
1125 struct cpu_workqueue_struct
*cwq
;
1126 struct workqueue_struct
*wq
;
1128 action
&= ~CPU_TASKS_FROZEN
;
1130 list_for_each_entry(wq
, &workqueues
, list
) {
1131 if (wq
->flags
& WQ_SINGLE_THREAD
)
1134 cwq
= get_cwq(cpu
, wq
);
1138 lock_map_acquire(&cwq
->wq
->lockdep_map
);
1139 lock_map_release(&cwq
->wq
->lockdep_map
);
1140 flush_cpu_workqueue(cwq
);
1145 return notifier_from_errno(0);
1150 struct work_for_cpu
{
1151 struct completion completion
;
1157 static int do_work_for_cpu(void *_wfc
)
1159 struct work_for_cpu
*wfc
= _wfc
;
1160 wfc
->ret
= wfc
->fn(wfc
->arg
);
1161 complete(&wfc
->completion
);
1166 * work_on_cpu - run a function in user context on a particular cpu
1167 * @cpu: the cpu to run on
1168 * @fn: the function to run
1169 * @arg: the function arg
1171 * This will return the value @fn returns.
1172 * It is up to the caller to ensure that the cpu doesn't go offline.
1173 * The caller must not hold any locks which would prevent @fn from completing.
1175 long work_on_cpu(unsigned int cpu
, long (*fn
)(void *), void *arg
)
1177 struct task_struct
*sub_thread
;
1178 struct work_for_cpu wfc
= {
1179 .completion
= COMPLETION_INITIALIZER_ONSTACK(wfc
.completion
),
1184 sub_thread
= kthread_create(do_work_for_cpu
, &wfc
, "work_for_cpu");
1185 if (IS_ERR(sub_thread
))
1186 return PTR_ERR(sub_thread
);
1187 kthread_bind(sub_thread
, cpu
);
1188 wake_up_process(sub_thread
);
1189 wait_for_completion(&wfc
.completion
);
1192 EXPORT_SYMBOL_GPL(work_on_cpu
);
1193 #endif /* CONFIG_SMP */
1195 void __init
init_workqueues(void)
1197 singlethread_cpu
= cpumask_first(cpu_possible_mask
);
1198 hotcpu_notifier(workqueue_cpu_callback
, 0);
1199 keventd_wq
= create_workqueue("events");
1200 BUG_ON(!keventd_wq
);