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>
36 #include <trace/workqueue.h>
39 * The per-CPU workqueue (if single thread, we always use the first
42 struct cpu_workqueue_struct {
46 struct list_head worklist;
47 wait_queue_head_t more_work;
48 struct work_struct *current_work;
50 struct workqueue_struct *wq;
51 struct task_struct *thread;
53 int run_depth; /* Detect run_workqueue() recursion depth */
54 } ____cacheline_aligned;
57 * The externally visible workqueue abstraction is an array of
60 struct workqueue_struct {
61 struct cpu_workqueue_struct *cpu_wq;
62 struct list_head list;
65 int freezeable; /* Freeze threads during suspend */
68 struct lockdep_map lockdep_map;
72 /* Serializes the accesses to the list of workqueues. */
73 static DEFINE_SPINLOCK(workqueue_lock);
74 static LIST_HEAD(workqueues);
76 static int singlethread_cpu __read_mostly;
77 static const struct cpumask *cpu_singlethread_map __read_mostly;
79 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
80 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
81 * which comes in between can't use for_each_online_cpu(). We could
82 * use cpu_possible_map, the cpumask below is more a documentation
85 static cpumask_var_t cpu_populated_map __read_mostly;
87 /* If it's single threaded, it isn't in the list of workqueues. */
88 static inline int is_wq_single_threaded(struct workqueue_struct *wq)
90 return wq->singlethread;
93 static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
95 return is_wq_single_threaded(wq)
96 ? cpu_singlethread_map : cpu_populated_map;
100 struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
102 if (unlikely(is_wq_single_threaded(wq)))
103 cpu = singlethread_cpu;
104 return per_cpu_ptr(wq->cpu_wq, cpu);
108 * Set the workqueue on which a work item is to be run
109 * - Must *only* be called if the pending flag is set
111 static inline void set_wq_data(struct work_struct *work,
112 struct cpu_workqueue_struct *cwq)
116 BUG_ON(!work_pending(work));
118 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
119 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
120 atomic_long_set(&work->data, new);
124 struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
126 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
129 DEFINE_TRACE(workqueue_insertion);
131 static void insert_work(struct cpu_workqueue_struct *cwq,
132 struct work_struct *work, struct list_head *head)
134 trace_workqueue_insertion(cwq->thread, work);
136 set_wq_data(work, cwq);
138 * Ensure that we get the right work->data if we see the
139 * result of list_add() below, see try_to_grab_pending().
142 list_add_tail(&work->entry, head);
143 wake_up(&cwq->more_work);
146 static void __queue_work(struct cpu_workqueue_struct *cwq,
147 struct work_struct *work)
151 spin_lock_irqsave(&cwq->lock, flags);
152 insert_work(cwq, work, &cwq->worklist);
153 spin_unlock_irqrestore(&cwq->lock, flags);
157 * queue_work - queue work on a workqueue
158 * @wq: workqueue to use
159 * @work: work to queue
161 * Returns 0 if @work was already on a queue, non-zero otherwise.
163 * We queue the work to the CPU on which it was submitted, but if the CPU dies
164 * it can be processed by another CPU.
166 int queue_work(struct workqueue_struct *wq, struct work_struct *work)
170 ret = queue_work_on(get_cpu(), wq, work);
175 EXPORT_SYMBOL_GPL(queue_work);
178 * queue_work_on - queue work on specific cpu
179 * @cpu: CPU number to execute work on
180 * @wq: workqueue to use
181 * @work: work to queue
183 * Returns 0 if @work was already on a queue, non-zero otherwise.
185 * We queue the work to a specific CPU, the caller must ensure it
189 queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
193 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
194 BUG_ON(!list_empty(&work->entry));
195 __queue_work(wq_per_cpu(wq, cpu), work);
200 EXPORT_SYMBOL_GPL(queue_work_on);
202 static void delayed_work_timer_fn(unsigned long __data)
204 struct delayed_work *dwork = (struct delayed_work *)__data;
205 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
206 struct workqueue_struct *wq = cwq->wq;
208 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
212 * queue_delayed_work - queue work on a workqueue after delay
213 * @wq: workqueue to use
214 * @dwork: delayable work to queue
215 * @delay: number of jiffies to wait before queueing
217 * Returns 0 if @work was already on a queue, non-zero otherwise.
219 int queue_delayed_work(struct workqueue_struct *wq,
220 struct delayed_work *dwork, unsigned long delay)
223 return queue_work(wq, &dwork->work);
225 return queue_delayed_work_on(-1, wq, dwork, delay);
227 EXPORT_SYMBOL_GPL(queue_delayed_work);
230 * queue_delayed_work_on - queue work on specific CPU after delay
231 * @cpu: CPU number to execute work on
232 * @wq: workqueue to use
233 * @dwork: work to queue
234 * @delay: number of jiffies to wait before queueing
236 * Returns 0 if @work was already on a queue, non-zero otherwise.
238 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
239 struct delayed_work *dwork, unsigned long delay)
242 struct timer_list *timer = &dwork->timer;
243 struct work_struct *work = &dwork->work;
245 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
246 BUG_ON(timer_pending(timer));
247 BUG_ON(!list_empty(&work->entry));
249 timer_stats_timer_set_start_info(&dwork->timer);
251 /* This stores cwq for the moment, for the timer_fn */
252 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
253 timer->expires = jiffies + delay;
254 timer->data = (unsigned long)dwork;
255 timer->function = delayed_work_timer_fn;
257 if (unlikely(cpu >= 0))
258 add_timer_on(timer, cpu);
265 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
267 DEFINE_TRACE(workqueue_execution);
269 static void run_workqueue(struct cpu_workqueue_struct *cwq)
271 spin_lock_irq(&cwq->lock);
273 if (cwq->run_depth > 3) {
274 /* morton gets to eat his hat */
275 printk("%s: recursion depth exceeded: %d\n",
276 __func__, cwq->run_depth);
279 while (!list_empty(&cwq->worklist)) {
280 struct work_struct *work = list_entry(cwq->worklist.next,
281 struct work_struct, entry);
282 work_func_t f = work->func;
283 #ifdef CONFIG_LOCKDEP
285 * It is permissible to free the struct work_struct
286 * from inside the function that is called from it,
287 * this we need to take into account for lockdep too.
288 * To avoid bogus "held lock freed" warnings as well
289 * as problems when looking into work->lockdep_map,
290 * make a copy and use that here.
292 struct lockdep_map lockdep_map = work->lockdep_map;
294 trace_workqueue_execution(cwq->thread, work);
295 cwq->current_work = work;
296 list_del_init(cwq->worklist.next);
297 spin_unlock_irq(&cwq->lock);
299 BUG_ON(get_wq_data(work) != cwq);
300 work_clear_pending(work);
301 lock_map_acquire(&cwq->wq->lockdep_map);
302 lock_map_acquire(&lockdep_map);
304 lock_map_release(&lockdep_map);
305 lock_map_release(&cwq->wq->lockdep_map);
307 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
308 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
310 current->comm, preempt_count(),
311 task_pid_nr(current));
312 printk(KERN_ERR " last function: ");
313 print_symbol("%s\n", (unsigned long)f);
314 debug_show_held_locks(current);
318 spin_lock_irq(&cwq->lock);
319 cwq->current_work = NULL;
322 spin_unlock_irq(&cwq->lock);
325 static int worker_thread(void *__cwq)
327 struct cpu_workqueue_struct *cwq = __cwq;
330 if (cwq->wq->freezeable)
333 set_user_nice(current, -5);
336 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
337 if (!freezing(current) &&
338 !kthread_should_stop() &&
339 list_empty(&cwq->worklist))
341 finish_wait(&cwq->more_work, &wait);
345 if (kthread_should_stop())
355 struct work_struct work;
356 struct completion done;
359 static void wq_barrier_func(struct work_struct *work)
361 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
362 complete(&barr->done);
365 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
366 struct wq_barrier *barr, struct list_head *head)
368 INIT_WORK(&barr->work, wq_barrier_func);
369 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
371 init_completion(&barr->done);
373 insert_work(cwq, &barr->work, head);
376 static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
380 if (cwq->thread == current) {
382 * Probably keventd trying to flush its own queue. So simply run
383 * it by hand rather than deadlocking.
388 struct wq_barrier barr;
391 spin_lock_irq(&cwq->lock);
392 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
393 insert_wq_barrier(cwq, &barr, &cwq->worklist);
396 spin_unlock_irq(&cwq->lock);
399 wait_for_completion(&barr.done);
406 * flush_workqueue - ensure that any scheduled work has run to completion.
407 * @wq: workqueue to flush
409 * Forces execution of the workqueue and blocks until its completion.
410 * This is typically used in driver shutdown handlers.
412 * We sleep until all works which were queued on entry have been handled,
413 * but we are not livelocked by new incoming ones.
415 * This function used to run the workqueues itself. Now we just wait for the
416 * helper threads to do it.
418 void flush_workqueue(struct workqueue_struct *wq)
420 const struct cpumask *cpu_map = wq_cpu_map(wq);
424 lock_map_acquire(&wq->lockdep_map);
425 lock_map_release(&wq->lockdep_map);
426 for_each_cpu_mask_nr(cpu, *cpu_map)
427 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
429 EXPORT_SYMBOL_GPL(flush_workqueue);
432 * flush_work - block until a work_struct's callback has terminated
433 * @work: the work which is to be flushed
435 * Returns false if @work has already terminated.
437 * It is expected that, prior to calling flush_work(), the caller has
438 * arranged for the work to not be requeued, otherwise it doesn't make
439 * sense to use this function.
441 int flush_work(struct work_struct *work)
443 struct cpu_workqueue_struct *cwq;
444 struct list_head *prev;
445 struct wq_barrier barr;
448 cwq = get_wq_data(work);
452 lock_map_acquire(&cwq->wq->lockdep_map);
453 lock_map_release(&cwq->wq->lockdep_map);
456 spin_lock_irq(&cwq->lock);
457 if (!list_empty(&work->entry)) {
459 * See the comment near try_to_grab_pending()->smp_rmb().
460 * If it was re-queued under us we are not going to wait.
463 if (unlikely(cwq != get_wq_data(work)))
467 if (cwq->current_work != work)
469 prev = &cwq->worklist;
471 insert_wq_barrier(cwq, &barr, prev->next);
473 spin_unlock_irq(&cwq->lock);
477 wait_for_completion(&barr.done);
480 EXPORT_SYMBOL_GPL(flush_work);
483 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
484 * so this work can't be re-armed in any way.
486 static int try_to_grab_pending(struct work_struct *work)
488 struct cpu_workqueue_struct *cwq;
491 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
495 * The queueing is in progress, or it is already queued. Try to
496 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
499 cwq = get_wq_data(work);
503 spin_lock_irq(&cwq->lock);
504 if (!list_empty(&work->entry)) {
506 * This work is queued, but perhaps we locked the wrong cwq.
507 * In that case we must see the new value after rmb(), see
508 * insert_work()->wmb().
511 if (cwq == get_wq_data(work)) {
512 list_del_init(&work->entry);
516 spin_unlock_irq(&cwq->lock);
521 static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
522 struct work_struct *work)
524 struct wq_barrier barr;
527 spin_lock_irq(&cwq->lock);
528 if (unlikely(cwq->current_work == work)) {
529 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
532 spin_unlock_irq(&cwq->lock);
534 if (unlikely(running))
535 wait_for_completion(&barr.done);
538 static void wait_on_work(struct work_struct *work)
540 struct cpu_workqueue_struct *cwq;
541 struct workqueue_struct *wq;
542 const struct cpumask *cpu_map;
547 lock_map_acquire(&work->lockdep_map);
548 lock_map_release(&work->lockdep_map);
550 cwq = get_wq_data(work);
555 cpu_map = wq_cpu_map(wq);
557 for_each_cpu_mask_nr(cpu, *cpu_map)
558 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
561 static int __cancel_work_timer(struct work_struct *work,
562 struct timer_list* timer)
567 ret = (timer && likely(del_timer(timer)));
569 ret = try_to_grab_pending(work);
571 } while (unlikely(ret < 0));
573 work_clear_pending(work);
578 * cancel_work_sync - block until a work_struct's callback has terminated
579 * @work: the work which is to be flushed
581 * Returns true if @work was pending.
583 * cancel_work_sync() will cancel the work if it is queued. If the work's
584 * callback appears to be running, cancel_work_sync() will block until it
587 * It is possible to use this function if the work re-queues itself. It can
588 * cancel the work even if it migrates to another workqueue, however in that
589 * case it only guarantees that work->func() has completed on the last queued
592 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
593 * pending, otherwise it goes into a busy-wait loop until the timer expires.
595 * The caller must ensure that workqueue_struct on which this work was last
596 * queued can't be destroyed before this function returns.
598 int cancel_work_sync(struct work_struct *work)
600 return __cancel_work_timer(work, NULL);
602 EXPORT_SYMBOL_GPL(cancel_work_sync);
605 * cancel_delayed_work_sync - reliably kill off a delayed work.
606 * @dwork: the delayed work struct
608 * Returns true if @dwork was pending.
610 * It is possible to use this function if @dwork rearms itself via queue_work()
611 * or queue_delayed_work(). See also the comment for cancel_work_sync().
613 int cancel_delayed_work_sync(struct delayed_work *dwork)
615 return __cancel_work_timer(&dwork->work, &dwork->timer);
617 EXPORT_SYMBOL(cancel_delayed_work_sync);
619 static struct workqueue_struct *keventd_wq __read_mostly;
622 * schedule_work - put work task in global workqueue
623 * @work: job to be done
625 * This puts a job in the kernel-global workqueue.
627 int schedule_work(struct work_struct *work)
629 return queue_work(keventd_wq, work);
631 EXPORT_SYMBOL(schedule_work);
634 * schedule_work_on - put work task on a specific cpu
635 * @cpu: cpu to put the work task on
636 * @work: job to be done
638 * This puts a job on a specific cpu
640 int schedule_work_on(int cpu, struct work_struct *work)
642 return queue_work_on(cpu, keventd_wq, work);
644 EXPORT_SYMBOL(schedule_work_on);
647 * schedule_delayed_work - put work task in global workqueue after delay
648 * @dwork: job to be done
649 * @delay: number of jiffies to wait or 0 for immediate execution
651 * After waiting for a given time this puts a job in the kernel-global
654 int schedule_delayed_work(struct delayed_work *dwork,
657 return queue_delayed_work(keventd_wq, dwork, delay);
659 EXPORT_SYMBOL(schedule_delayed_work);
662 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
664 * @dwork: job to be done
665 * @delay: number of jiffies to wait
667 * After waiting for a given time this puts a job in the kernel-global
668 * workqueue on the specified CPU.
670 int schedule_delayed_work_on(int cpu,
671 struct delayed_work *dwork, unsigned long delay)
673 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
675 EXPORT_SYMBOL(schedule_delayed_work_on);
678 * schedule_on_each_cpu - call a function on each online CPU from keventd
679 * @func: the function to call
681 * Returns zero on success.
682 * Returns -ve errno on failure.
684 * schedule_on_each_cpu() is very slow.
686 int schedule_on_each_cpu(work_func_t func)
689 struct work_struct *works;
691 works = alloc_percpu(struct work_struct);
696 for_each_online_cpu(cpu) {
697 struct work_struct *work = per_cpu_ptr(works, cpu);
699 INIT_WORK(work, func);
700 schedule_work_on(cpu, work);
702 for_each_online_cpu(cpu)
703 flush_work(per_cpu_ptr(works, cpu));
709 void flush_scheduled_work(void)
711 flush_workqueue(keventd_wq);
713 EXPORT_SYMBOL(flush_scheduled_work);
716 * execute_in_process_context - reliably execute the routine with user context
717 * @fn: the function to execute
718 * @ew: guaranteed storage for the execute work structure (must
719 * be available when the work executes)
721 * Executes the function immediately if process context is available,
722 * otherwise schedules the function for delayed execution.
724 * Returns: 0 - function was executed
725 * 1 - function was scheduled for execution
727 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
729 if (!in_interrupt()) {
734 INIT_WORK(&ew->work, fn);
735 schedule_work(&ew->work);
739 EXPORT_SYMBOL_GPL(execute_in_process_context);
743 return keventd_wq != NULL;
746 int current_is_keventd(void)
748 struct cpu_workqueue_struct *cwq;
749 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
754 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
755 if (current == cwq->thread)
762 static struct cpu_workqueue_struct *
763 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
765 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
768 spin_lock_init(&cwq->lock);
769 INIT_LIST_HEAD(&cwq->worklist);
770 init_waitqueue_head(&cwq->more_work);
775 DEFINE_TRACE(workqueue_creation);
777 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
779 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
780 struct workqueue_struct *wq = cwq->wq;
781 const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
782 struct task_struct *p;
784 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
786 * Nobody can add the work_struct to this cwq,
787 * if (caller is __create_workqueue)
788 * nobody should see this wq
789 * else // caller is CPU_UP_PREPARE
790 * cpu is not on cpu_online_map
791 * so we can abort safely.
796 sched_setscheduler_nocheck(p, SCHED_FIFO, ¶m);
799 trace_workqueue_creation(cwq->thread, cpu);
804 static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
806 struct task_struct *p = cwq->thread;
810 kthread_bind(p, cpu);
815 struct workqueue_struct *__create_workqueue_key(const char *name,
819 struct lock_class_key *key,
820 const char *lock_name)
822 struct workqueue_struct *wq;
823 struct cpu_workqueue_struct *cwq;
826 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
830 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
837 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
838 wq->singlethread = singlethread;
839 wq->freezeable = freezeable;
841 INIT_LIST_HEAD(&wq->list);
844 cwq = init_cpu_workqueue(wq, singlethread_cpu);
845 err = create_workqueue_thread(cwq, singlethread_cpu);
846 start_workqueue_thread(cwq, -1);
848 cpu_maps_update_begin();
850 * We must place this wq on list even if the code below fails.
851 * cpu_down(cpu) can remove cpu from cpu_populated_map before
852 * destroy_workqueue() takes the lock, in that case we leak
855 spin_lock(&workqueue_lock);
856 list_add(&wq->list, &workqueues);
857 spin_unlock(&workqueue_lock);
859 * We must initialize cwqs for each possible cpu even if we
860 * are going to call destroy_workqueue() finally. Otherwise
861 * cpu_up() can hit the uninitialized cwq once we drop the
864 for_each_possible_cpu(cpu) {
865 cwq = init_cpu_workqueue(wq, cpu);
866 if (err || !cpu_online(cpu))
868 err = create_workqueue_thread(cwq, cpu);
869 start_workqueue_thread(cwq, cpu);
871 cpu_maps_update_done();
875 destroy_workqueue(wq);
880 EXPORT_SYMBOL_GPL(__create_workqueue_key);
882 DEFINE_TRACE(workqueue_destruction);
884 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
887 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
888 * cpu_add_remove_lock protects cwq->thread.
890 if (cwq->thread == NULL)
893 lock_map_acquire(&cwq->wq->lockdep_map);
894 lock_map_release(&cwq->wq->lockdep_map);
896 flush_cpu_workqueue(cwq);
898 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
899 * a concurrent flush_workqueue() can insert a barrier after us.
900 * However, in that case run_workqueue() won't return and check
901 * kthread_should_stop() until it flushes all work_struct's.
902 * When ->worklist becomes empty it is safe to exit because no
903 * more work_structs can be queued on this cwq: flush_workqueue
904 * checks list_empty(), and a "normal" queue_work() can't use
907 trace_workqueue_destruction(cwq->thread);
908 kthread_stop(cwq->thread);
913 * destroy_workqueue - safely terminate a workqueue
914 * @wq: target workqueue
916 * Safely destroy a workqueue. All work currently pending will be done first.
918 void destroy_workqueue(struct workqueue_struct *wq)
920 const struct cpumask *cpu_map = wq_cpu_map(wq);
923 cpu_maps_update_begin();
924 spin_lock(&workqueue_lock);
926 spin_unlock(&workqueue_lock);
928 for_each_cpu_mask_nr(cpu, *cpu_map)
929 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
930 cpu_maps_update_done();
932 free_percpu(wq->cpu_wq);
935 EXPORT_SYMBOL_GPL(destroy_workqueue);
937 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
938 unsigned long action,
941 unsigned int cpu = (unsigned long)hcpu;
942 struct cpu_workqueue_struct *cwq;
943 struct workqueue_struct *wq;
946 action &= ~CPU_TASKS_FROZEN;
950 cpumask_set_cpu(cpu, cpu_populated_map);
953 list_for_each_entry(wq, &workqueues, list) {
954 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
958 if (!create_workqueue_thread(cwq, cpu))
960 printk(KERN_ERR "workqueue [%s] for %i failed\n",
962 action = CPU_UP_CANCELED;
967 start_workqueue_thread(cwq, cpu);
970 case CPU_UP_CANCELED:
971 start_workqueue_thread(cwq, -1);
973 cleanup_workqueue_thread(cwq);
979 case CPU_UP_CANCELED:
981 cpumask_clear_cpu(cpu, cpu_populated_map);
988 struct work_for_cpu {
989 struct work_struct work;
995 static void do_work_for_cpu(struct work_struct *w)
997 struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
999 wfc->ret = wfc->fn(wfc->arg);
1003 * work_on_cpu - run a function in user context on a particular cpu
1004 * @cpu: the cpu to run on
1005 * @fn: the function to run
1006 * @arg: the function arg
1008 * This will return -EINVAL in the cpu is not online, or the return value
1011 long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1013 struct work_for_cpu wfc;
1015 INIT_WORK(&wfc.work, do_work_for_cpu);
1019 if (unlikely(!cpu_online(cpu)))
1022 schedule_work_on(cpu, &wfc.work);
1023 flush_work(&wfc.work);
1029 EXPORT_SYMBOL_GPL(work_on_cpu);
1030 #endif /* CONFIG_SMP */
1032 void __init init_workqueues(void)
1034 alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1036 cpumask_copy(cpu_populated_map, cpu_online_mask);
1037 singlethread_cpu = cpumask_first(cpu_possible_mask);
1038 cpu_singlethread_map = cpumask_of(singlethread_cpu);
1039 hotcpu_notifier(workqueue_cpu_callback, 0);
1040 keventd_wq = create_workqueue("events");
1041 BUG_ON(!keventd_wq);