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