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