]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dde/linux26/lib/src/kernel/workqueue.c
Inital import
[l4.git] / l4 / pkg / dde / linux26 / lib / src / 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
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.
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 #include <linux/lockdep.h>
36
37 #ifdef DDE_LINUX
38 #include "local.h"
39 #endif
40
41 /*
42  * The per-CPU workqueue (if single thread, we always use the first
43  * possible cpu).
44  */
45 struct cpu_workqueue_struct {
46
47         spinlock_t lock;
48
49         struct list_head worklist;
50         wait_queue_head_t more_work;
51         struct work_struct *current_work;
52
53         struct workqueue_struct *wq;
54         struct task_struct *thread;
55
56         int run_depth;          /* Detect run_workqueue() recursion depth */
57 } ____cacheline_aligned;
58
59 /*
60  * The externally visible workqueue abstraction is an array of
61  * per-CPU workqueues:
62  */
63 struct workqueue_struct {
64         struct cpu_workqueue_struct *cpu_wq;
65         struct list_head list;
66         const char *name;
67         int singlethread;
68         int freezeable;         /* Freeze threads during suspend */
69         int rt;
70 #ifdef CONFIG_LOCKDEP
71         struct lockdep_map lockdep_map;
72 #endif
73 };
74
75 /* Serializes the accesses to the list of workqueues. */
76 static DEFINE_SPINLOCK(workqueue_lock);
77 static LIST_HEAD(workqueues);
78
79 static int singlethread_cpu __read_mostly;
80 static const struct cpumask *cpu_singlethread_map __read_mostly;
81 /*
82  * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
83  * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
84  * which comes in between can't use for_each_online_cpu(). We could
85  * use cpu_possible_map, the cpumask below is more a documentation
86  * than optimization.
87  */
88 static cpumask_var_t cpu_populated_map __read_mostly;
89
90 /* If it's single threaded, it isn't in the list of workqueues. */
91 static inline int is_wq_single_threaded(struct workqueue_struct *wq)
92 {
93         return wq->singlethread;
94 }
95
96 static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
97 {
98         return is_wq_single_threaded(wq)
99                 ? cpu_singlethread_map : cpu_populated_map;
100 }
101
102 static
103 struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
104 {
105         if (unlikely(is_wq_single_threaded(wq)))
106                 cpu = singlethread_cpu;
107         return per_cpu_ptr(wq->cpu_wq, cpu);
108 }
109
110 /*
111  * Set the workqueue on which a work item is to be run
112  * - Must *only* be called if the pending flag is set
113  */
114 static inline void set_wq_data(struct work_struct *work,
115                                 struct cpu_workqueue_struct *cwq)
116 {
117         unsigned long new;
118
119         BUG_ON(!work_pending(work));
120
121         new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
122         new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
123         atomic_long_set(&work->data, new);
124 }
125
126 static inline
127 struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
128 {
129         return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
130 }
131
132 static void insert_work(struct cpu_workqueue_struct *cwq,
133                         struct work_struct *work, struct list_head *head)
134 {
135         set_wq_data(work, cwq);
136         /*
137          * Ensure that we get the right work->data if we see the
138          * result of list_add() below, see try_to_grab_pending().
139          */
140         smp_wmb();
141         list_add_tail(&work->entry, head);
142         wake_up(&cwq->more_work);
143 }
144
145 static void __queue_work(struct cpu_workqueue_struct *cwq,
146                          struct work_struct *work)
147 {
148         unsigned long flags;
149
150         spin_lock_irqsave(&cwq->lock, flags);
151         insert_work(cwq, work, &cwq->worklist);
152         spin_unlock_irqrestore(&cwq->lock, flags);
153 }
154
155 /**
156  * queue_work - queue work on a workqueue
157  * @wq: workqueue to use
158  * @work: work to queue
159  *
160  * Returns 0 if @work was already on a queue, non-zero otherwise.
161  *
162  * We queue the work to the CPU on which it was submitted, but if the CPU dies
163  * it can be processed by another CPU.
164  */
165 int queue_work(struct workqueue_struct *wq, struct work_struct *work)
166 {
167         int ret;
168
169         ret = queue_work_on(get_cpu(), wq, work);
170         put_cpu();
171
172         return ret;
173 }
174 EXPORT_SYMBOL_GPL(queue_work);
175
176 /**
177  * queue_work_on - queue work on specific cpu
178  * @cpu: CPU number to execute work on
179  * @wq: workqueue to use
180  * @work: work to queue
181  *
182  * Returns 0 if @work was already on a queue, non-zero otherwise.
183  *
184  * We queue the work to a specific CPU, the caller must ensure it
185  * can't go away.
186  */
187 int
188 queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
189 {
190         int ret = 0;
191
192         if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
193                 BUG_ON(!list_empty(&work->entry));
194                 __queue_work(wq_per_cpu(wq, cpu), work);
195                 ret = 1;
196         }
197         return ret;
198 }
199 EXPORT_SYMBOL_GPL(queue_work_on);
200
201 static void delayed_work_timer_fn(unsigned long __data)
202 {
203         struct delayed_work *dwork = (struct delayed_work *)__data;
204         struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
205         struct workqueue_struct *wq = cwq->wq;
206
207         __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
208 }
209
210 /**
211  * queue_delayed_work - queue work on a workqueue after delay
212  * @wq: workqueue to use
213  * @dwork: delayable work to queue
214  * @delay: number of jiffies to wait before queueing
215  *
216  * Returns 0 if @work was already on a queue, non-zero otherwise.
217  */
218 int queue_delayed_work(struct workqueue_struct *wq,
219                         struct delayed_work *dwork, unsigned long delay)
220 {
221         if (delay == 0)
222                 return queue_work(wq, &dwork->work);
223
224         return queue_delayed_work_on(-1, wq, dwork, delay);
225 }
226 EXPORT_SYMBOL_GPL(queue_delayed_work);
227
228 /**
229  * queue_delayed_work_on - queue work on specific CPU after delay
230  * @cpu: CPU number to execute work on
231  * @wq: workqueue to use
232  * @dwork: work to queue
233  * @delay: number of jiffies to wait before queueing
234  *
235  * Returns 0 if @work was already on a queue, non-zero otherwise.
236  */
237 int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
238                         struct delayed_work *dwork, unsigned long delay)
239 {
240         int ret = 0;
241         struct timer_list *timer = &dwork->timer;
242         struct work_struct *work = &dwork->work;
243
244         if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
245                 BUG_ON(timer_pending(timer));
246                 BUG_ON(!list_empty(&work->entry));
247
248                 timer_stats_timer_set_start_info(&dwork->timer);
249
250                 /* This stores cwq for the moment, for the timer_fn */
251                 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
252                 timer->expires = jiffies + delay;
253                 timer->data = (unsigned long)dwork;
254                 timer->function = delayed_work_timer_fn;
255
256                 if (unlikely(cpu >= 0))
257                         add_timer_on(timer, cpu);
258                 else
259                         add_timer(timer);
260                 ret = 1;
261         }
262         return ret;
263 }
264 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
265
266 static void run_workqueue(struct cpu_workqueue_struct *cwq)
267 {
268         spin_lock_irq(&cwq->lock);
269         cwq->run_depth++;
270         if (cwq->run_depth > 3) {
271                 /* morton gets to eat his hat */
272                 printk("%s: recursion depth exceeded: %d\n",
273                         __func__, cwq->run_depth);
274                 dump_stack();
275         }
276         while (!list_empty(&cwq->worklist)) {
277                 struct work_struct *work = list_entry(cwq->worklist.next,
278                                                 struct work_struct, entry);
279                 work_func_t f = work->func;
280 #ifdef CONFIG_LOCKDEP
281                 /*
282                  * It is permissible to free the struct work_struct
283                  * from inside the function that is called from it,
284                  * this we need to take into account for lockdep too.
285                  * To avoid bogus "held lock freed" warnings as well
286                  * as problems when looking into work->lockdep_map,
287                  * make a copy and use that here.
288                  */
289                 struct lockdep_map lockdep_map = work->lockdep_map;
290 #endif
291
292                 cwq->current_work = work;
293                 list_del_init(cwq->worklist.next);
294                 spin_unlock_irq(&cwq->lock);
295
296                 BUG_ON(get_wq_data(work) != cwq);
297                 work_clear_pending(work);
298                 lock_map_acquire(&cwq->wq->lockdep_map);
299                 lock_map_acquire(&lockdep_map);
300                 f(work);
301                 lock_map_release(&lockdep_map);
302                 lock_map_release(&cwq->wq->lockdep_map);
303
304                 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
305                         printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
306                                         "%s/0x%08x/%d\n",
307                                         current->comm, preempt_count(),
308                                         task_pid_nr(current));
309 #ifndef DDE_LINUX
310                         printk(KERN_ERR "    last function: ");
311                         print_symbol("%s\n", (unsigned long)f);
312                         debug_show_held_locks(current);
313                         dump_stack();
314 #endif /* DDE_LINUX */
315                 }
316
317                 spin_lock_irq(&cwq->lock);
318                 cwq->current_work = NULL;
319         }
320         cwq->run_depth--;
321         spin_unlock_irq(&cwq->lock);
322 }
323
324 static int worker_thread(void *__cwq)
325 {
326         struct cpu_workqueue_struct *cwq = __cwq;
327         DEFINE_WAIT(wait);
328
329         if (cwq->wq->freezeable)
330                 set_freezable();
331
332         set_user_nice(current, -5);
333
334         for (;;) {
335                 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
336                 if (!freezing(current) &&
337                     !kthread_should_stop() &&
338                     list_empty(&cwq->worklist))
339                         schedule();
340                 finish_wait(&cwq->more_work, &wait);
341
342                 try_to_freeze();
343
344                 if (kthread_should_stop())
345                         break;
346
347                 run_workqueue(cwq);
348         }
349
350         return 0;
351 }
352
353 struct wq_barrier {
354         struct work_struct      work;
355         struct completion       done;
356 };
357
358 static void wq_barrier_func(struct work_struct *work)
359 {
360         struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
361         complete(&barr->done);
362 }
363
364 static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
365                         struct wq_barrier *barr, struct list_head *head)
366 {
367         INIT_WORK(&barr->work, wq_barrier_func);
368         __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
369
370         init_completion(&barr->done);
371
372         insert_work(cwq, &barr->work, head);
373 }
374
375 static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
376 {
377         int active;
378
379         if (cwq->thread == current) {
380                 /*
381                  * Probably keventd trying to flush its own queue. So simply run
382                  * it by hand rather than deadlocking.
383                  */
384                 run_workqueue(cwq);
385                 active = 1;
386         } else {
387                 struct wq_barrier barr;
388
389                 active = 0;
390                 spin_lock_irq(&cwq->lock);
391                 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
392                         insert_wq_barrier(cwq, &barr, &cwq->worklist);
393                         active = 1;
394                 }
395                 spin_unlock_irq(&cwq->lock);
396
397                 if (active)
398                         wait_for_completion(&barr.done);
399         }
400
401         return active;
402 }
403
404 /**
405  * flush_workqueue - ensure that any scheduled work has run to completion.
406  * @wq: workqueue to flush
407  *
408  * Forces execution of the workqueue and blocks until its completion.
409  * This is typically used in driver shutdown handlers.
410  *
411  * We sleep until all works which were queued on entry have been handled,
412  * but we are not livelocked by new incoming ones.
413  *
414  * This function used to run the workqueues itself.  Now we just wait for the
415  * helper threads to do it.
416  */
417 void flush_workqueue(struct workqueue_struct *wq)
418 {
419         const struct cpumask *cpu_map = wq_cpu_map(wq);
420         int cpu;
421
422         might_sleep();
423         lock_map_acquire(&wq->lockdep_map);
424         lock_map_release(&wq->lockdep_map);
425         for_each_cpu_mask_nr(cpu, *cpu_map)
426                 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
427 }
428 EXPORT_SYMBOL_GPL(flush_workqueue);
429
430 /**
431  * flush_work - block until a work_struct's callback has terminated
432  * @work: the work which is to be flushed
433  *
434  * Returns false if @work has already terminated.
435  *
436  * It is expected that, prior to calling flush_work(), the caller has
437  * arranged for the work to not be requeued, otherwise it doesn't make
438  * sense to use this function.
439  */
440 int flush_work(struct work_struct *work)
441 {
442         struct cpu_workqueue_struct *cwq;
443         struct list_head *prev;
444         struct wq_barrier barr;
445
446         might_sleep();
447         cwq = get_wq_data(work);
448         if (!cwq)
449                 return 0;
450
451         lock_map_acquire(&cwq->wq->lockdep_map);
452         lock_map_release(&cwq->wq->lockdep_map);
453
454         prev = NULL;
455         spin_lock_irq(&cwq->lock);
456         if (!list_empty(&work->entry)) {
457                 /*
458                  * See the comment near try_to_grab_pending()->smp_rmb().
459                  * If it was re-queued under us we are not going to wait.
460                  */
461                 smp_rmb();
462                 if (unlikely(cwq != get_wq_data(work)))
463                         goto out;
464                 prev = &work->entry;
465         } else {
466                 if (cwq->current_work != work)
467                         goto out;
468                 prev = &cwq->worklist;
469         }
470         insert_wq_barrier(cwq, &barr, prev->next);
471 out:
472         spin_unlock_irq(&cwq->lock);
473         if (!prev)
474                 return 0;
475
476         wait_for_completion(&barr.done);
477         return 1;
478 }
479 EXPORT_SYMBOL_GPL(flush_work);
480
481 /*
482  * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
483  * so this work can't be re-armed in any way.
484  */
485 static int try_to_grab_pending(struct work_struct *work)
486 {
487         struct cpu_workqueue_struct *cwq;
488         int ret = -1;
489
490         if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
491                 return 0;
492
493         /*
494          * The queueing is in progress, or it is already queued. Try to
495          * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
496          */
497
498         cwq = get_wq_data(work);
499         if (!cwq)
500                 return ret;
501
502         spin_lock_irq(&cwq->lock);
503         if (!list_empty(&work->entry)) {
504                 /*
505                  * This work is queued, but perhaps we locked the wrong cwq.
506                  * In that case we must see the new value after rmb(), see
507                  * insert_work()->wmb().
508                  */
509                 smp_rmb();
510                 if (cwq == get_wq_data(work)) {
511                         list_del_init(&work->entry);
512                         ret = 1;
513                 }
514         }
515         spin_unlock_irq(&cwq->lock);
516
517         return ret;
518 }
519
520 static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
521                                 struct work_struct *work)
522 {
523         struct wq_barrier barr;
524         int running = 0;
525
526         spin_lock_irq(&cwq->lock);
527         if (unlikely(cwq->current_work == work)) {
528                 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
529                 running = 1;
530         }
531         spin_unlock_irq(&cwq->lock);
532
533         if (unlikely(running))
534                 wait_for_completion(&barr.done);
535 }
536
537 static void wait_on_work(struct work_struct *work)
538 {
539         struct cpu_workqueue_struct *cwq;
540         struct workqueue_struct *wq;
541         const struct cpumask *cpu_map;
542         int cpu;
543
544         might_sleep();
545
546         lock_map_acquire(&work->lockdep_map);
547         lock_map_release(&work->lockdep_map);
548
549         cwq = get_wq_data(work);
550         if (!cwq)
551                 return;
552
553         wq = cwq->wq;
554         cpu_map = wq_cpu_map(wq);
555
556         for_each_cpu_mask_nr(cpu, *cpu_map)
557                 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
558 }
559
560 static int __cancel_work_timer(struct work_struct *work,
561                                 struct timer_list* timer)
562 {
563         int ret;
564
565         do {
566                 ret = (timer && likely(del_timer(timer)));
567                 if (!ret)
568                         ret = try_to_grab_pending(work);
569                 wait_on_work(work);
570         } while (unlikely(ret < 0));
571
572         work_clear_pending(work);
573         return ret;
574 }
575
576 /**
577  * cancel_work_sync - block until a work_struct's callback has terminated
578  * @work: the work which is to be flushed
579  *
580  * Returns true if @work was pending.
581  *
582  * cancel_work_sync() will cancel the work if it is queued. If the work's
583  * callback appears to be running, cancel_work_sync() will block until it
584  * has completed.
585  *
586  * It is possible to use this function if the work re-queues itself. It can
587  * cancel the work even if it migrates to another workqueue, however in that
588  * case it only guarantees that work->func() has completed on the last queued
589  * workqueue.
590  *
591  * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
592  * pending, otherwise it goes into a busy-wait loop until the timer expires.
593  *
594  * The caller must ensure that workqueue_struct on which this work was last
595  * queued can't be destroyed before this function returns.
596  */
597 int cancel_work_sync(struct work_struct *work)
598 {
599         return __cancel_work_timer(work, NULL);
600 }
601 EXPORT_SYMBOL_GPL(cancel_work_sync);
602
603 /**
604  * cancel_delayed_work_sync - reliably kill off a delayed work.
605  * @dwork: the delayed work struct
606  *
607  * Returns true if @dwork was pending.
608  *
609  * It is possible to use this function if @dwork rearms itself via queue_work()
610  * or queue_delayed_work(). See also the comment for cancel_work_sync().
611  */
612 int cancel_delayed_work_sync(struct delayed_work *dwork)
613 {
614         return __cancel_work_timer(&dwork->work, &dwork->timer);
615 }
616 EXPORT_SYMBOL(cancel_delayed_work_sync);
617
618 static struct workqueue_struct *keventd_wq __read_mostly;
619
620 /**
621  * schedule_work - put work task in global workqueue
622  * @work: job to be done
623  *
624  * This puts a job in the kernel-global workqueue.
625  */
626 int schedule_work(struct work_struct *work)
627 {
628         return queue_work(keventd_wq, work);
629 }
630 EXPORT_SYMBOL(schedule_work);
631
632 /*
633  * schedule_work_on - put work task on a specific cpu
634  * @cpu: cpu to put the work task on
635  * @work: job to be done
636  *
637  * This puts a job on a specific cpu
638  */
639 int schedule_work_on(int cpu, struct work_struct *work)
640 {
641         return queue_work_on(cpu, keventd_wq, work);
642 }
643 EXPORT_SYMBOL(schedule_work_on);
644
645 /**
646  * schedule_delayed_work - put work task in global workqueue after delay
647  * @dwork: job to be done
648  * @delay: number of jiffies to wait or 0 for immediate execution
649  *
650  * After waiting for a given time this puts a job in the kernel-global
651  * workqueue.
652  */
653 int schedule_delayed_work(struct delayed_work *dwork,
654                                         unsigned long delay)
655 {
656         return queue_delayed_work(keventd_wq, dwork, delay);
657 }
658 EXPORT_SYMBOL(schedule_delayed_work);
659
660 /**
661  * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
662  * @cpu: cpu to use
663  * @dwork: job to be done
664  * @delay: number of jiffies to wait
665  *
666  * After waiting for a given time this puts a job in the kernel-global
667  * workqueue on the specified CPU.
668  */
669 int schedule_delayed_work_on(int cpu,
670                         struct delayed_work *dwork, unsigned long delay)
671 {
672         return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
673 }
674 EXPORT_SYMBOL(schedule_delayed_work_on);
675
676 /**
677  * schedule_on_each_cpu - call a function on each online CPU from keventd
678  * @func: the function to call
679  *
680  * Returns zero on success.
681  * Returns -ve errno on failure.
682  *
683  * schedule_on_each_cpu() is very slow.
684  */
685 int schedule_on_each_cpu(work_func_t func)
686 {
687         int cpu;
688         struct work_struct *works;
689
690         works = alloc_percpu(struct work_struct);
691         if (!works)
692                 return -ENOMEM;
693
694         get_online_cpus();
695         for_each_online_cpu(cpu) {
696                 struct work_struct *work = per_cpu_ptr(works, cpu);
697
698                 INIT_WORK(work, func);
699                 schedule_work_on(cpu, work);
700         }
701         for_each_online_cpu(cpu)
702                 flush_work(per_cpu_ptr(works, cpu));
703         put_online_cpus();
704         free_percpu(works);
705         return 0;
706 }
707
708 void flush_scheduled_work(void)
709 {
710         flush_workqueue(keventd_wq);
711 }
712 EXPORT_SYMBOL(flush_scheduled_work);
713
714 /**
715  * execute_in_process_context - reliably execute the routine with user context
716  * @fn:         the function to execute
717  * @ew:         guaranteed storage for the execute work structure (must
718  *              be available when the work executes)
719  *
720  * Executes the function immediately if process context is available,
721  * otherwise schedules the function for delayed execution.
722  *
723  * Returns:     0 - function was executed
724  *              1 - function was scheduled for execution
725  */
726 int execute_in_process_context(work_func_t fn, struct execute_work *ew)
727 {
728         if (!in_interrupt()) {
729                 fn(&ew->work);
730                 return 0;
731         }
732
733         INIT_WORK(&ew->work, fn);
734         schedule_work(&ew->work);
735
736         return 1;
737 }
738 EXPORT_SYMBOL_GPL(execute_in_process_context);
739
740 int keventd_up(void)
741 {
742         return keventd_wq != NULL;
743 }
744
745 int current_is_keventd(void)
746 {
747         struct cpu_workqueue_struct *cwq;
748         int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
749         int ret = 0;
750
751         BUG_ON(!keventd_wq);
752
753         cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
754         if (current == cwq->thread)
755                 ret = 1;
756
757         return ret;
758
759 }
760
761 static struct cpu_workqueue_struct *
762 init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
763 {
764         struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
765
766         cwq->wq = wq;
767         spin_lock_init(&cwq->lock);
768         INIT_LIST_HEAD(&cwq->worklist);
769         init_waitqueue_head(&cwq->more_work);
770
771         return cwq;
772 }
773
774 static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
775 {
776         struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
777         struct workqueue_struct *wq = cwq->wq;
778         const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
779         struct task_struct *p;
780
781         p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
782         /*
783          * Nobody can add the work_struct to this cwq,
784          *      if (caller is __create_workqueue)
785          *              nobody should see this wq
786          *      else // caller is CPU_UP_PREPARE
787          *              cpu is not on cpu_online_map
788          * so we can abort safely.
789          */
790         if (IS_ERR(p))
791                 return PTR_ERR(p);
792         if (cwq->wq->rt)
793                 sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
794         cwq->thread = p;
795
796         return 0;
797 }
798
799 static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
800 {
801         struct task_struct *p = cwq->thread;
802
803         if (p != NULL) {
804                 if (cpu >= 0)
805                         kthread_bind(p, cpu);
806                 wake_up_process(p);
807         }
808 }
809
810 struct workqueue_struct *__create_workqueue_key(const char *name,
811                                                 int singlethread,
812                                                 int freezeable,
813                                                 int rt,
814                                                 struct lock_class_key *key,
815                                                 const char *lock_name)
816 {
817         struct workqueue_struct *wq;
818         struct cpu_workqueue_struct *cwq;
819         int err = 0, cpu;
820
821         wq = kzalloc(sizeof(*wq), GFP_KERNEL);
822         if (!wq)
823                 return NULL;
824
825         wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
826         if (!wq->cpu_wq) {
827                 kfree(wq);
828                 return NULL;
829         }
830
831         wq->name = name;
832         lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
833         wq->singlethread = singlethread;
834         wq->freezeable = freezeable;
835         wq->rt = rt;
836         INIT_LIST_HEAD(&wq->list);
837
838         if (singlethread) {
839                 cwq = init_cpu_workqueue(wq, singlethread_cpu);
840                 err = create_workqueue_thread(cwq, singlethread_cpu);
841                 start_workqueue_thread(cwq, -1);
842         } else {
843                 cpu_maps_update_begin();
844                 /*
845                  * We must place this wq on list even if the code below fails.
846                  * cpu_down(cpu) can remove cpu from cpu_populated_map before
847                  * destroy_workqueue() takes the lock, in that case we leak
848                  * cwq[cpu]->thread.
849                  */
850                 spin_lock(&workqueue_lock);
851                 list_add(&wq->list, &workqueues);
852                 spin_unlock(&workqueue_lock);
853                 /*
854                  * We must initialize cwqs for each possible cpu even if we
855                  * are going to call destroy_workqueue() finally. Otherwise
856                  * cpu_up() can hit the uninitialized cwq once we drop the
857                  * lock.
858                  */
859                 for_each_possible_cpu(cpu) {
860                         cwq = init_cpu_workqueue(wq, cpu);
861                         if (err || !cpu_online(cpu))
862                                 continue;
863                         err = create_workqueue_thread(cwq, cpu);
864                         start_workqueue_thread(cwq, cpu);
865                 }
866                 cpu_maps_update_done();
867         }
868
869         if (err) {
870                 destroy_workqueue(wq);
871                 wq = NULL;
872         }
873         return wq;
874 }
875 EXPORT_SYMBOL_GPL(__create_workqueue_key);
876
877 static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
878 {
879         /*
880          * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
881          * cpu_add_remove_lock protects cwq->thread.
882          */
883         if (cwq->thread == NULL)
884                 return;
885
886         lock_map_acquire(&cwq->wq->lockdep_map);
887         lock_map_release(&cwq->wq->lockdep_map);
888
889         flush_cpu_workqueue(cwq);
890         /*
891          * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
892          * a concurrent flush_workqueue() can insert a barrier after us.
893          * However, in that case run_workqueue() won't return and check
894          * kthread_should_stop() until it flushes all work_struct's.
895          * When ->worklist becomes empty it is safe to exit because no
896          * more work_structs can be queued on this cwq: flush_workqueue
897          * checks list_empty(), and a "normal" queue_work() can't use
898          * a dead CPU.
899          */
900         kthread_stop(cwq->thread);
901         cwq->thread = NULL;
902 }
903
904 /**
905  * destroy_workqueue - safely terminate a workqueue
906  * @wq: target workqueue
907  *
908  * Safely destroy a workqueue. All work currently pending will be done first.
909  */
910 void destroy_workqueue(struct workqueue_struct *wq)
911 {
912         const struct cpumask *cpu_map = wq_cpu_map(wq);
913         int cpu;
914
915         cpu_maps_update_begin();
916         spin_lock(&workqueue_lock);
917         list_del(&wq->list);
918         spin_unlock(&workqueue_lock);
919
920         for_each_cpu_mask_nr(cpu, *cpu_map)
921                 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
922         cpu_maps_update_done();
923
924         free_percpu(wq->cpu_wq);
925         kfree(wq);
926 }
927 EXPORT_SYMBOL_GPL(destroy_workqueue);
928
929 static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
930                                                 unsigned long action,
931                                                 void *hcpu)
932 {
933         unsigned int cpu = (unsigned long)hcpu;
934         struct cpu_workqueue_struct *cwq;
935         struct workqueue_struct *wq;
936         int ret = NOTIFY_OK;
937
938         action &= ~CPU_TASKS_FROZEN;
939
940         switch (action) {
941         case CPU_UP_PREPARE:
942                 cpumask_set_cpu(cpu, cpu_populated_map);
943         }
944 undo:
945         list_for_each_entry(wq, &workqueues, list) {
946                 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
947
948                 switch (action) {
949                 case CPU_UP_PREPARE:
950                         if (!create_workqueue_thread(cwq, cpu))
951                                 break;
952                         printk(KERN_ERR "workqueue [%s] for %i failed\n",
953                                 wq->name, cpu);
954                         action = CPU_UP_CANCELED;
955                         ret = NOTIFY_BAD;
956                         goto undo;
957
958                 case CPU_ONLINE:
959                         start_workqueue_thread(cwq, cpu);
960                         break;
961
962                 case CPU_UP_CANCELED:
963                         start_workqueue_thread(cwq, -1);
964                 case CPU_POST_DEAD:
965                         cleanup_workqueue_thread(cwq);
966                         break;
967                 }
968         }
969
970         switch (action) {
971         case CPU_UP_CANCELED:
972         case CPU_POST_DEAD:
973                 cpumask_clear_cpu(cpu, cpu_populated_map);
974         }
975
976         return ret;
977 }
978
979 #ifdef CONFIG_SMP
980 static struct workqueue_struct *work_on_cpu_wq __read_mostly;
981
982 struct work_for_cpu {
983         struct work_struct work;
984         long (*fn)(void *);
985         void *arg;
986         long ret;
987 };
988
989 static void do_work_for_cpu(struct work_struct *w)
990 {
991         struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
992
993         wfc->ret = wfc->fn(wfc->arg);
994 }
995
996 /**
997  * work_on_cpu - run a function in user context on a particular cpu
998  * @cpu: the cpu to run on
999  * @fn: the function to run
1000  * @arg: the function arg
1001  *
1002  * This will return the value @fn returns.
1003  * It is up to the caller to ensure that the cpu doesn't go offline.
1004  */
1005 long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1006 {
1007         struct work_for_cpu wfc;
1008
1009         INIT_WORK(&wfc.work, do_work_for_cpu);
1010         wfc.fn = fn;
1011         wfc.arg = arg;
1012         queue_work_on(cpu, work_on_cpu_wq, &wfc.work);
1013         flush_work(&wfc.work);
1014
1015         return wfc.ret;
1016 }
1017 EXPORT_SYMBOL_GPL(work_on_cpu);
1018 #endif /* CONFIG_SMP */
1019
1020 void __init init_workqueues(void)
1021 {
1022         alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1023
1024         cpumask_copy(cpu_populated_map, cpu_online_mask);
1025         singlethread_cpu = cpumask_first(cpu_possible_mask);
1026         cpu_singlethread_map = cpumask_of(singlethread_cpu);
1027         hotcpu_notifier(workqueue_cpu_callback, 0);
1028         keventd_wq = create_workqueue("events");
1029         BUG_ON(!keventd_wq);
1030 #ifdef CONFIG_SMP
1031         work_on_cpu_wq = create_workqueue("work_on_cpu");
1032         BUG_ON(!work_on_cpu_wq);
1033 #endif
1034 }
1035
1036 #ifdef DDE_LINUX
1037 core_initcall(init_workqueues);
1038 #endif