]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/workqueue.c
make cancel_rearming_delayed_work() work on any workqueue, not just keventd_wq
[linux-2.6-omap-h63xx.git] / kernel / workqueue.c
1 /*
2  * linux/kernel/workqueue.c
3  *
4  * Generic mechanism for defining kernel helper threads for running
5  * arbitrary tasks in process context.
6  *
7  * Started by Ingo Molnar, Copyright (C) 2002
8  *
9  * Derived from the taskqueue/keventd code by:
10  *
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>
15  *
16  * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
17  */
18
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
36 /*
37  * The per-CPU workqueue (if single thread, we always use the first
38  * possible cpu).
39  */
40 struct cpu_workqueue_struct {
41
42         spinlock_t lock;
43
44         struct list_head worklist;
45         wait_queue_head_t more_work;
46         struct work_struct *current_work;
47
48         struct workqueue_struct *wq;
49         struct task_struct *thread;
50         int should_stop;
51
52         int run_depth;          /* Detect run_workqueue() recursion depth */
53 } ____cacheline_aligned;
54
55 /*
56  * The externally visible workqueue abstraction is an array of
57  * per-CPU workqueues:
58  */
59 struct workqueue_struct {
60         struct cpu_workqueue_struct *cpu_wq;
61         struct list_head list;
62         const char *name;
63         int singlethread;
64         int freezeable;         /* Freeze threads during suspend */
65 };
66
67 /* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
68    threads to each one as cpus come/go. */
69 static DEFINE_MUTEX(workqueue_mutex);
70 static LIST_HEAD(workqueues);
71
72 static int singlethread_cpu __read_mostly;
73 static cpumask_t cpu_singlethread_map __read_mostly;
74 /* optimization, we could use cpu_possible_map */
75 static cpumask_t cpu_populated_map __read_mostly;
76
77 /* If it's single threaded, it isn't in the list of workqueues. */
78 static inline int is_single_threaded(struct workqueue_struct *wq)
79 {
80         return wq->singlethread;
81 }
82
83 static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
84 {
85         return is_single_threaded(wq)
86                 ? &cpu_singlethread_map : &cpu_populated_map;
87 }
88
89 static
90 struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
91 {
92         if (unlikely(is_single_threaded(wq)))
93                 cpu = singlethread_cpu;
94         return per_cpu_ptr(wq->cpu_wq, cpu);
95 }
96
97 /*
98  * Set the workqueue on which a work item is to be run
99  * - Must *only* be called if the pending flag is set
100  */
101 static inline void set_wq_data(struct work_struct *work,
102                                 struct cpu_workqueue_struct *cwq)
103 {
104         unsigned long new;
105
106         BUG_ON(!work_pending(work));
107
108         new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
109         new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
110         atomic_long_set(&work->data, new);
111 }
112
113 static inline
114 struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
115 {
116         return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
117 }
118
119 static void insert_work(struct cpu_workqueue_struct *cwq,
120                                 struct work_struct *work, int tail)
121 {
122         set_wq_data(work, cwq);
123         if (tail)
124                 list_add_tail(&work->entry, &cwq->worklist);
125         else
126                 list_add(&work->entry, &cwq->worklist);
127         wake_up(&cwq->more_work);
128 }
129
130 /* Preempt must be disabled. */
131 static void __queue_work(struct cpu_workqueue_struct *cwq,
132                          struct work_struct *work)
133 {
134         unsigned long flags;
135
136         spin_lock_irqsave(&cwq->lock, flags);
137         insert_work(cwq, work, 1);
138         spin_unlock_irqrestore(&cwq->lock, flags);
139 }
140
141 /**
142  * queue_work - queue work on a workqueue
143  * @wq: workqueue to use
144  * @work: work to queue
145  *
146  * Returns 0 if @work was already on a queue, non-zero otherwise.
147  *
148  * We queue the work to the CPU it was submitted, but there is no
149  * guarantee that it will be processed by that CPU.
150  */
151 int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
152 {
153         int ret = 0;
154
155         if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
156                 BUG_ON(!list_empty(&work->entry));
157                 __queue_work(wq_per_cpu(wq, get_cpu()), work);
158                 put_cpu();
159                 ret = 1;
160         }
161         return ret;
162 }
163 EXPORT_SYMBOL_GPL(queue_work);
164
165 void delayed_work_timer_fn(unsigned long __data)
166 {
167         struct delayed_work *dwork = (struct delayed_work *)__data;
168         struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
169         struct workqueue_struct *wq = cwq->wq;
170
171         __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
172 }
173
174 /**
175  * queue_delayed_work - queue work on a workqueue after delay
176  * @wq: workqueue to use
177  * @dwork: delayable work to queue
178  * @delay: number of jiffies to wait before queueing
179  *
180  * Returns 0 if @work was already on a queue, non-zero otherwise.
181  */
182 int fastcall queue_delayed_work(struct workqueue_struct *wq,
183                         struct delayed_work *dwork, unsigned long delay)
184 {
185         timer_stats_timer_set_start_info(&dwork->timer);
186         if (delay == 0)
187                 return queue_work(wq, &dwork->work);
188
189         return queue_delayed_work_on(-1, wq, dwork, delay);
190 }
191 EXPORT_SYMBOL_GPL(queue_delayed_work);
192
193 /**
194  * queue_delayed_work_on - queue work on specific CPU after delay
195  * @cpu: CPU number to execute work on
196  * @wq: workqueue to use
197  * @dwork: work to queue
198  * @delay: number of jiffies to wait before queueing
199  *
200  * Returns 0 if @work was already on a queue, non-zero otherwise.
201  */
202 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
203                         struct delayed_work *dwork, unsigned long delay)
204 {
205         int ret = 0;
206         struct timer_list *timer = &dwork->timer;
207         struct work_struct *work = &dwork->work;
208
209         if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
210                 BUG_ON(timer_pending(timer));
211                 BUG_ON(!list_empty(&work->entry));
212
213                 /* This stores cwq for the moment, for the timer_fn */
214                 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
215                 timer->expires = jiffies + delay;
216                 timer->data = (unsigned long)dwork;
217                 timer->function = delayed_work_timer_fn;
218
219                 if (unlikely(cpu >= 0))
220                         add_timer_on(timer, cpu);
221                 else
222                         add_timer(timer);
223                 ret = 1;
224         }
225         return ret;
226 }
227 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
228
229 static void run_workqueue(struct cpu_workqueue_struct *cwq)
230 {
231         spin_lock_irq(&cwq->lock);
232         cwq->run_depth++;
233         if (cwq->run_depth > 3) {
234                 /* morton gets to eat his hat */
235                 printk("%s: recursion depth exceeded: %d\n",
236                         __FUNCTION__, cwq->run_depth);
237                 dump_stack();
238         }
239         while (!list_empty(&cwq->worklist)) {
240                 struct work_struct *work = list_entry(cwq->worklist.next,
241                                                 struct work_struct, entry);
242                 work_func_t f = work->func;
243
244                 cwq->current_work = work;
245                 list_del_init(cwq->worklist.next);
246                 spin_unlock_irq(&cwq->lock);
247
248                 BUG_ON(get_wq_data(work) != cwq);
249                 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
250                         work_release(work);
251                 f(work);
252
253                 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
254                         printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
255                                         "%s/0x%08x/%d\n",
256                                         current->comm, preempt_count(),
257                                         current->pid);
258                         printk(KERN_ERR "    last function: ");
259                         print_symbol("%s\n", (unsigned long)f);
260                         debug_show_held_locks(current);
261                         dump_stack();
262                 }
263
264                 spin_lock_irq(&cwq->lock);
265                 cwq->current_work = NULL;
266         }
267         cwq->run_depth--;
268         spin_unlock_irq(&cwq->lock);
269 }
270
271 /*
272  * NOTE: the caller must not touch *cwq if this func returns true
273  */
274 static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
275 {
276         int should_stop = cwq->should_stop;
277
278         if (unlikely(should_stop)) {
279                 spin_lock_irq(&cwq->lock);
280                 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
281                 if (should_stop)
282                         cwq->thread = NULL;
283                 spin_unlock_irq(&cwq->lock);
284         }
285
286         return should_stop;
287 }
288
289 static int worker_thread(void *__cwq)
290 {
291         struct cpu_workqueue_struct *cwq = __cwq;
292         DEFINE_WAIT(wait);
293         struct k_sigaction sa;
294         sigset_t blocked;
295
296         if (!cwq->wq->freezeable)
297                 current->flags |= PF_NOFREEZE;
298
299         set_user_nice(current, -5);
300
301         /* Block and flush all signals */
302         sigfillset(&blocked);
303         sigprocmask(SIG_BLOCK, &blocked, NULL);
304         flush_signals(current);
305
306         /*
307          * We inherited MPOL_INTERLEAVE from the booting kernel.
308          * Set MPOL_DEFAULT to insure node local allocations.
309          */
310         numa_default_policy();
311
312         /* SIG_IGN makes children autoreap: see do_notify_parent(). */
313         sa.sa.sa_handler = SIG_IGN;
314         sa.sa.sa_flags = 0;
315         siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
316         do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
317
318         for (;;) {
319                 if (cwq->wq->freezeable)
320                         try_to_freeze();
321
322                 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
323                 if (!cwq->should_stop && list_empty(&cwq->worklist))
324                         schedule();
325                 finish_wait(&cwq->more_work, &wait);
326
327                 if (cwq_should_stop(cwq))
328                         break;
329
330                 run_workqueue(cwq);
331         }
332
333         return 0;
334 }
335
336 struct wq_barrier {
337         struct work_struct      work;
338         struct completion       done;
339 };
340
341 static void wq_barrier_func(struct work_struct *work)
342 {
343         struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
344         complete(&barr->done);
345 }
346
347 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
348                                         struct wq_barrier *barr, int tail)
349 {
350         INIT_WORK(&barr->work, wq_barrier_func);
351         __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
352
353         init_completion(&barr->done);
354
355         insert_work(cwq, &barr->work, tail);
356 }
357
358 static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
359 {
360         if (cwq->thread == current) {
361                 /*
362                  * Probably keventd trying to flush its own queue. So simply run
363                  * it by hand rather than deadlocking.
364                  */
365                 run_workqueue(cwq);
366         } else {
367                 struct wq_barrier barr;
368                 int active = 0;
369
370                 spin_lock_irq(&cwq->lock);
371                 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
372                         insert_wq_barrier(cwq, &barr, 1);
373                         active = 1;
374                 }
375                 spin_unlock_irq(&cwq->lock);
376
377                 if (active)
378                         wait_for_completion(&barr.done);
379         }
380 }
381
382 /**
383  * flush_workqueue - ensure that any scheduled work has run to completion.
384  * @wq: workqueue to flush
385  *
386  * Forces execution of the workqueue and blocks until its completion.
387  * This is typically used in driver shutdown handlers.
388  *
389  * We sleep until all works which were queued on entry have been handled,
390  * but we are not livelocked by new incoming ones.
391  *
392  * This function used to run the workqueues itself.  Now we just wait for the
393  * helper threads to do it.
394  */
395 void fastcall flush_workqueue(struct workqueue_struct *wq)
396 {
397         const cpumask_t *cpu_map = wq_cpu_map(wq);
398         int cpu;
399
400         might_sleep();
401         for_each_cpu_mask(cpu, *cpu_map)
402                 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
403 }
404 EXPORT_SYMBOL_GPL(flush_workqueue);
405
406 static void wait_on_work(struct cpu_workqueue_struct *cwq,
407                                 struct work_struct *work)
408 {
409         struct wq_barrier barr;
410         int running = 0;
411
412         spin_lock_irq(&cwq->lock);
413         if (unlikely(cwq->current_work == work)) {
414                 insert_wq_barrier(cwq, &barr, 0);
415                 running = 1;
416         }
417         spin_unlock_irq(&cwq->lock);
418
419         if (unlikely(running))
420                 wait_for_completion(&barr.done);
421 }
422
423 /**
424  * flush_work - block until a work_struct's callback has terminated
425  * @wq: the workqueue on which the work is queued
426  * @work: the work which is to be flushed
427  *
428  * flush_work() will attempt to cancel the work if it is queued.  If the work's
429  * callback appears to be running, flush_work() will block until it has
430  * completed.
431  *
432  * flush_work() is designed to be used when the caller is tearing down data
433  * structures which the callback function operates upon.  It is expected that,
434  * prior to calling flush_work(), the caller has arranged for the work to not
435  * be requeued.
436  */
437 void flush_work(struct workqueue_struct *wq, struct work_struct *work)
438 {
439         const cpumask_t *cpu_map = wq_cpu_map(wq);
440         struct cpu_workqueue_struct *cwq;
441         int cpu;
442
443         might_sleep();
444
445         cwq = get_wq_data(work);
446         /* Was it ever queued ? */
447         if (!cwq)
448                 return;
449
450         /*
451          * This work can't be re-queued, no need to re-check that
452          * get_wq_data() is still the same when we take cwq->lock.
453          */
454         spin_lock_irq(&cwq->lock);
455         list_del_init(&work->entry);
456         work_release(work);
457         spin_unlock_irq(&cwq->lock);
458
459         for_each_cpu_mask(cpu, *cpu_map)
460                 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
461 }
462 EXPORT_SYMBOL_GPL(flush_work);
463
464
465 static struct workqueue_struct *keventd_wq;
466
467 /**
468  * schedule_work - put work task in global workqueue
469  * @work: job to be done
470  *
471  * This puts a job in the kernel-global workqueue.
472  */
473 int fastcall schedule_work(struct work_struct *work)
474 {
475         return queue_work(keventd_wq, work);
476 }
477 EXPORT_SYMBOL(schedule_work);
478
479 /**
480  * schedule_delayed_work - put work task in global workqueue after delay
481  * @dwork: job to be done
482  * @delay: number of jiffies to wait or 0 for immediate execution
483  *
484  * After waiting for a given time this puts a job in the kernel-global
485  * workqueue.
486  */
487 int fastcall schedule_delayed_work(struct delayed_work *dwork,
488                                         unsigned long delay)
489 {
490         timer_stats_timer_set_start_info(&dwork->timer);
491         return queue_delayed_work(keventd_wq, dwork, delay);
492 }
493 EXPORT_SYMBOL(schedule_delayed_work);
494
495 /**
496  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
497  * @cpu: cpu to use
498  * @dwork: job to be done
499  * @delay: number of jiffies to wait
500  *
501  * After waiting for a given time this puts a job in the kernel-global
502  * workqueue on the specified CPU.
503  */
504 int schedule_delayed_work_on(int cpu,
505                         struct delayed_work *dwork, unsigned long delay)
506 {
507         return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
508 }
509 EXPORT_SYMBOL(schedule_delayed_work_on);
510
511 /**
512  * schedule_on_each_cpu - call a function on each online CPU from keventd
513  * @func: the function to call
514  *
515  * Returns zero on success.
516  * Returns -ve errno on failure.
517  *
518  * Appears to be racy against CPU hotplug.
519  *
520  * schedule_on_each_cpu() is very slow.
521  */
522 int schedule_on_each_cpu(work_func_t func)
523 {
524         int cpu;
525         struct work_struct *works;
526
527         works = alloc_percpu(struct work_struct);
528         if (!works)
529                 return -ENOMEM;
530
531         preempt_disable();              /* CPU hotplug */
532         for_each_online_cpu(cpu) {
533                 struct work_struct *work = per_cpu_ptr(works, cpu);
534
535                 INIT_WORK(work, func);
536                 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
537                 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
538         }
539         preempt_enable();
540         flush_workqueue(keventd_wq);
541         free_percpu(works);
542         return 0;
543 }
544
545 void flush_scheduled_work(void)
546 {
547         flush_workqueue(keventd_wq);
548 }
549 EXPORT_SYMBOL(flush_scheduled_work);
550
551 void flush_work_keventd(struct work_struct *work)
552 {
553         flush_work(keventd_wq, work);
554 }
555 EXPORT_SYMBOL(flush_work_keventd);
556
557 /**
558  * cancel_rearming_delayed_work - kill off a delayed work whose handler rearms the delayed work.
559  * @dwork: the delayed work struct
560  *
561  * Note that the work callback function may still be running on return from
562  * cancel_delayed_work(). Run flush_workqueue() or flush_work() to wait on it.
563  */
564 void cancel_rearming_delayed_work(struct delayed_work *dwork)
565 {
566         struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
567
568         /* Was it ever queued ? */
569         if (cwq != NULL) {
570                 struct workqueue_struct *wq = cwq->wq;
571
572                 while (!cancel_delayed_work(dwork))
573                         flush_workqueue(wq);
574         }
575 }
576 EXPORT_SYMBOL(cancel_rearming_delayed_work);
577
578 /**
579  * execute_in_process_context - reliably execute the routine with user context
580  * @fn:         the function to execute
581  * @ew:         guaranteed storage for the execute work structure (must
582  *              be available when the work executes)
583  *
584  * Executes the function immediately if process context is available,
585  * otherwise schedules the function for delayed execution.
586  *
587  * Returns:     0 - function was executed
588  *              1 - function was scheduled for execution
589  */
590 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
591 {
592         if (!in_interrupt()) {
593                 fn(&ew->work);
594                 return 0;
595         }
596
597         INIT_WORK(&ew->work, fn);
598         schedule_work(&ew->work);
599
600         return 1;
601 }
602 EXPORT_SYMBOL_GPL(execute_in_process_context);
603
604 int keventd_up(void)
605 {
606         return keventd_wq != NULL;
607 }
608
609 int current_is_keventd(void)
610 {
611         struct cpu_workqueue_struct *cwq;
612         int cpu = smp_processor_id();   /* preempt-safe: keventd is per-cpu */
613         int ret = 0;
614
615         BUG_ON(!keventd_wq);
616
617         cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
618         if (current == cwq->thread)
619                 ret = 1;
620
621         return ret;
622
623 }
624
625 static struct cpu_workqueue_struct *
626 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
627 {
628         struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
629
630         cwq->wq = wq;
631         spin_lock_init(&cwq->lock);
632         INIT_LIST_HEAD(&cwq->worklist);
633         init_waitqueue_head(&cwq->more_work);
634
635         return cwq;
636 }
637
638 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
639 {
640         struct workqueue_struct *wq = cwq->wq;
641         const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
642         struct task_struct *p;
643
644         p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
645         /*
646          * Nobody can add the work_struct to this cwq,
647          *      if (caller is __create_workqueue)
648          *              nobody should see this wq
649          *      else // caller is CPU_UP_PREPARE
650          *              cpu is not on cpu_online_map
651          * so we can abort safely.
652          */
653         if (IS_ERR(p))
654                 return PTR_ERR(p);
655
656         cwq->thread = p;
657         cwq->should_stop = 0;
658
659         return 0;
660 }
661
662 static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
663 {
664         struct task_struct *p = cwq->thread;
665
666         if (p != NULL) {
667                 if (cpu >= 0)
668                         kthread_bind(p, cpu);
669                 wake_up_process(p);
670         }
671 }
672
673 struct workqueue_struct *__create_workqueue(const char *name,
674                                             int singlethread, int freezeable)
675 {
676         struct workqueue_struct *wq;
677         struct cpu_workqueue_struct *cwq;
678         int err = 0, cpu;
679
680         wq = kzalloc(sizeof(*wq), GFP_KERNEL);
681         if (!wq)
682                 return NULL;
683
684         wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
685         if (!wq->cpu_wq) {
686                 kfree(wq);
687                 return NULL;
688         }
689
690         wq->name = name;
691         wq->singlethread = singlethread;
692         wq->freezeable = freezeable;
693         INIT_LIST_HEAD(&wq->list);
694
695         if (singlethread) {
696                 cwq = init_cpu_workqueue(wq, singlethread_cpu);
697                 err = create_workqueue_thread(cwq, singlethread_cpu);
698                 start_workqueue_thread(cwq, -1);
699         } else {
700                 mutex_lock(&workqueue_mutex);
701                 list_add(&wq->list, &workqueues);
702
703                 for_each_possible_cpu(cpu) {
704                         cwq = init_cpu_workqueue(wq, cpu);
705                         if (err || !cpu_online(cpu))
706                                 continue;
707                         err = create_workqueue_thread(cwq, cpu);
708                         start_workqueue_thread(cwq, cpu);
709                 }
710                 mutex_unlock(&workqueue_mutex);
711         }
712
713         if (err) {
714                 destroy_workqueue(wq);
715                 wq = NULL;
716         }
717         return wq;
718 }
719 EXPORT_SYMBOL_GPL(__create_workqueue);
720
721 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
722 {
723         struct wq_barrier barr;
724         int alive = 0;
725
726         spin_lock_irq(&cwq->lock);
727         if (cwq->thread != NULL) {
728                 insert_wq_barrier(cwq, &barr, 1);
729                 cwq->should_stop = 1;
730                 alive = 1;
731         }
732         spin_unlock_irq(&cwq->lock);
733
734         if (alive) {
735                 wait_for_completion(&barr.done);
736
737                 while (unlikely(cwq->thread != NULL))
738                         cpu_relax();
739                 /*
740                  * Wait until cwq->thread unlocks cwq->lock,
741                  * it won't touch *cwq after that.
742                  */
743                 smp_rmb();
744                 spin_unlock_wait(&cwq->lock);
745         }
746 }
747
748 /**
749  * destroy_workqueue - safely terminate a workqueue
750  * @wq: target workqueue
751  *
752  * Safely destroy a workqueue. All work currently pending will be done first.
753  */
754 void destroy_workqueue(struct workqueue_struct *wq)
755 {
756         const cpumask_t *cpu_map = wq_cpu_map(wq);
757         struct cpu_workqueue_struct *cwq;
758         int cpu;
759
760         mutex_lock(&workqueue_mutex);
761         list_del(&wq->list);
762         mutex_unlock(&workqueue_mutex);
763
764         for_each_cpu_mask(cpu, *cpu_map) {
765                 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
766                 cleanup_workqueue_thread(cwq, cpu);
767         }
768
769         free_percpu(wq->cpu_wq);
770         kfree(wq);
771 }
772 EXPORT_SYMBOL_GPL(destroy_workqueue);
773
774 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
775                                                 unsigned long action,
776                                                 void *hcpu)
777 {
778         unsigned int cpu = (unsigned long)hcpu;
779         struct cpu_workqueue_struct *cwq;
780         struct workqueue_struct *wq;
781
782         switch (action) {
783         case CPU_LOCK_ACQUIRE:
784                 mutex_lock(&workqueue_mutex);
785                 return NOTIFY_OK;
786
787         case CPU_LOCK_RELEASE:
788                 mutex_unlock(&workqueue_mutex);
789                 return NOTIFY_OK;
790
791         case CPU_UP_PREPARE:
792                 cpu_set(cpu, cpu_populated_map);
793         }
794
795         list_for_each_entry(wq, &workqueues, list) {
796                 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
797
798                 switch (action) {
799                 case CPU_UP_PREPARE:
800                         if (!create_workqueue_thread(cwq, cpu))
801                                 break;
802                         printk(KERN_ERR "workqueue for %i failed\n", cpu);
803                         return NOTIFY_BAD;
804
805                 case CPU_ONLINE:
806                         start_workqueue_thread(cwq, cpu);
807                         break;
808
809                 case CPU_UP_CANCELED:
810                         start_workqueue_thread(cwq, -1);
811                 case CPU_DEAD:
812                         cleanup_workqueue_thread(cwq, cpu);
813                         break;
814                 }
815         }
816
817         return NOTIFY_OK;
818 }
819
820 void __init init_workqueues(void)
821 {
822         cpu_populated_map = cpu_online_map;
823         singlethread_cpu = first_cpu(cpu_possible_map);
824         cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
825         hotcpu_notifier(workqueue_cpu_callback, 0);
826         keventd_wq = create_workqueue("events");
827         BUG_ON(!keventd_wq);
828 }