[PATCH] swsusp: kill write-only variable
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / workqueue.c
CommitLineData
1da177e4
LT
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>
89ada679
CL
15 *
16 * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
1da177e4
LT
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>
1fa44eca 30#include <linux/hardirq.h>
46934023 31#include <linux/mempolicy.h>
1da177e4
LT
32
33/*
f756d5e2
NL
34 * The per-CPU workqueue (if single thread, we always use the first
35 * possible cpu).
1da177e4
LT
36 *
37 * The sequence counters are for flush_scheduled_work(). It wants to wait
9f5d785e 38 * until all currently-scheduled works are completed, but it doesn't
1da177e4
LT
39 * want to be livelocked by new, incoming ones. So it waits until
40 * remove_sequence is >= the insert_sequence which pertained when
41 * flush_scheduled_work() was called.
42 */
43struct cpu_workqueue_struct {
44
45 spinlock_t lock;
46
47 long remove_sequence; /* Least-recently added (next to run) */
48 long insert_sequence; /* Next to add */
49
50 struct list_head worklist;
51 wait_queue_head_t more_work;
52 wait_queue_head_t work_done;
53
54 struct workqueue_struct *wq;
36c8b586 55 struct task_struct *thread;
1da177e4
LT
56
57 int run_depth; /* Detect run_workqueue() recursion depth */
58} ____cacheline_aligned;
59
60/*
61 * The externally visible workqueue abstraction is an array of
62 * per-CPU workqueues:
63 */
64struct workqueue_struct {
89ada679 65 struct cpu_workqueue_struct *cpu_wq;
1da177e4
LT
66 const char *name;
67 struct list_head list; /* Empty if single thread */
68};
69
70/* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
71 threads to each one as cpus come/go. */
9b41ea72 72static DEFINE_MUTEX(workqueue_mutex);
1da177e4
LT
73static LIST_HEAD(workqueues);
74
f756d5e2
NL
75static int singlethread_cpu;
76
1da177e4
LT
77/* If it's single threaded, it isn't in the list of workqueues. */
78static inline int is_single_threaded(struct workqueue_struct *wq)
79{
80 return list_empty(&wq->list);
81}
82
365970a1
DH
83static inline void set_wq_data(struct work_struct *work, void *wq)
84{
85 unsigned long new, old, res;
86
87 /* assume the pending flag is already set and that the task has already
88 * been queued on this workqueue */
89 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
90 res = work->management;
91 if (res != new) {
92 do {
93 old = res;
94 new = (unsigned long) wq;
95 new |= (old & WORK_STRUCT_FLAG_MASK);
96 res = cmpxchg(&work->management, old, new);
97 } while (res != old);
98 }
99}
100
101static inline void *get_wq_data(struct work_struct *work)
102{
103 return (void *) (work->management & WORK_STRUCT_WQ_DATA_MASK);
104}
105
1da177e4
LT
106/* Preempt must be disabled. */
107static void __queue_work(struct cpu_workqueue_struct *cwq,
108 struct work_struct *work)
109{
110 unsigned long flags;
111
112 spin_lock_irqsave(&cwq->lock, flags);
365970a1 113 set_wq_data(work, cwq);
1da177e4
LT
114 list_add_tail(&work->entry, &cwq->worklist);
115 cwq->insert_sequence++;
116 wake_up(&cwq->more_work);
117 spin_unlock_irqrestore(&cwq->lock, flags);
118}
119
0fcb78c2
REB
120/**
121 * queue_work - queue work on a workqueue
122 * @wq: workqueue to use
123 * @work: work to queue
124 *
057647fc 125 * Returns 0 if @work was already on a queue, non-zero otherwise.
1da177e4
LT
126 *
127 * We queue the work to the CPU it was submitted, but there is no
128 * guarantee that it will be processed by that CPU.
129 */
130int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
131{
132 int ret = 0, cpu = get_cpu();
133
365970a1 134 if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
1da177e4 135 if (unlikely(is_single_threaded(wq)))
f756d5e2 136 cpu = singlethread_cpu;
1da177e4 137 BUG_ON(!list_empty(&work->entry));
89ada679 138 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
139 ret = 1;
140 }
141 put_cpu();
142 return ret;
143}
ae90dd5d 144EXPORT_SYMBOL_GPL(queue_work);
1da177e4
LT
145
146static void delayed_work_timer_fn(unsigned long __data)
147{
52bad64d 148 struct delayed_work *dwork = (struct delayed_work *)__data;
365970a1 149 struct workqueue_struct *wq = get_wq_data(&dwork->work);
1da177e4
LT
150 int cpu = smp_processor_id();
151
152 if (unlikely(is_single_threaded(wq)))
f756d5e2 153 cpu = singlethread_cpu;
1da177e4 154
52bad64d 155 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
1da177e4
LT
156}
157
0fcb78c2
REB
158/**
159 * queue_delayed_work - queue work on a workqueue after delay
160 * @wq: workqueue to use
52bad64d 161 * @work: delayable work to queue
0fcb78c2
REB
162 * @delay: number of jiffies to wait before queueing
163 *
057647fc 164 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 165 */
1da177e4 166int fastcall queue_delayed_work(struct workqueue_struct *wq,
52bad64d 167 struct delayed_work *dwork, unsigned long delay)
1da177e4
LT
168{
169 int ret = 0;
52bad64d
DH
170 struct timer_list *timer = &dwork->timer;
171 struct work_struct *work = &dwork->work;
172
173 if (delay == 0)
174 return queue_work(wq, work);
1da177e4 175
365970a1 176 if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
1da177e4
LT
177 BUG_ON(timer_pending(timer));
178 BUG_ON(!list_empty(&work->entry));
179
180 /* This stores wq for the moment, for the timer_fn */
365970a1 181 set_wq_data(work, wq);
1da177e4 182 timer->expires = jiffies + delay;
52bad64d 183 timer->data = (unsigned long)dwork;
1da177e4
LT
184 timer->function = delayed_work_timer_fn;
185 add_timer(timer);
186 ret = 1;
187 }
188 return ret;
189}
ae90dd5d 190EXPORT_SYMBOL_GPL(queue_delayed_work);
1da177e4 191
0fcb78c2
REB
192/**
193 * queue_delayed_work_on - queue work on specific CPU after delay
194 * @cpu: CPU number to execute work on
195 * @wq: workqueue to use
196 * @work: work to queue
197 * @delay: number of jiffies to wait before queueing
198 *
057647fc 199 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 200 */
7a6bc1cd 201int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
52bad64d 202 struct delayed_work *dwork, unsigned long delay)
7a6bc1cd
VP
203{
204 int ret = 0;
52bad64d
DH
205 struct timer_list *timer = &dwork->timer;
206 struct work_struct *work = &dwork->work;
7a6bc1cd 207
365970a1 208 if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
7a6bc1cd
VP
209 BUG_ON(timer_pending(timer));
210 BUG_ON(!list_empty(&work->entry));
211
212 /* This stores wq for the moment, for the timer_fn */
365970a1 213 set_wq_data(work, wq);
7a6bc1cd 214 timer->expires = jiffies + delay;
52bad64d 215 timer->data = (unsigned long)dwork;
7a6bc1cd
VP
216 timer->function = delayed_work_timer_fn;
217 add_timer_on(timer, cpu);
218 ret = 1;
219 }
220 return ret;
221}
ae90dd5d 222EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4 223
858119e1 224static void run_workqueue(struct cpu_workqueue_struct *cwq)
1da177e4
LT
225{
226 unsigned long flags;
227
228 /*
229 * Keep taking off work from the queue until
230 * done.
231 */
232 spin_lock_irqsave(&cwq->lock, flags);
233 cwq->run_depth++;
234 if (cwq->run_depth > 3) {
235 /* morton gets to eat his hat */
236 printk("%s: recursion depth exceeded: %d\n",
237 __FUNCTION__, cwq->run_depth);
238 dump_stack();
239 }
240 while (!list_empty(&cwq->worklist)) {
241 struct work_struct *work = list_entry(cwq->worklist.next,
242 struct work_struct, entry);
6bb49e59 243 work_func_t f = work->func;
1da177e4
LT
244
245 list_del_init(cwq->worklist.next);
246 spin_unlock_irqrestore(&cwq->lock, flags);
247
365970a1 248 BUG_ON(get_wq_data(work) != cwq);
65f27f38
DH
249 if (!test_bit(WORK_STRUCT_NOAUTOREL, &work->management))
250 work_release(work);
251 f(work);
1da177e4
LT
252
253 spin_lock_irqsave(&cwq->lock, flags);
254 cwq->remove_sequence++;
255 wake_up(&cwq->work_done);
256 }
257 cwq->run_depth--;
258 spin_unlock_irqrestore(&cwq->lock, flags);
259}
260
261static int worker_thread(void *__cwq)
262{
263 struct cpu_workqueue_struct *cwq = __cwq;
264 DECLARE_WAITQUEUE(wait, current);
265 struct k_sigaction sa;
266 sigset_t blocked;
267
268 current->flags |= PF_NOFREEZE;
269
270 set_user_nice(current, -5);
271
272 /* Block and flush all signals */
273 sigfillset(&blocked);
274 sigprocmask(SIG_BLOCK, &blocked, NULL);
275 flush_signals(current);
276
46934023
CL
277 /*
278 * We inherited MPOL_INTERLEAVE from the booting kernel.
279 * Set MPOL_DEFAULT to insure node local allocations.
280 */
281 numa_default_policy();
282
1da177e4
LT
283 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
284 sa.sa.sa_handler = SIG_IGN;
285 sa.sa.sa_flags = 0;
286 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
287 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
288
289 set_current_state(TASK_INTERRUPTIBLE);
290 while (!kthread_should_stop()) {
291 add_wait_queue(&cwq->more_work, &wait);
292 if (list_empty(&cwq->worklist))
293 schedule();
294 else
295 __set_current_state(TASK_RUNNING);
296 remove_wait_queue(&cwq->more_work, &wait);
297
298 if (!list_empty(&cwq->worklist))
299 run_workqueue(cwq);
300 set_current_state(TASK_INTERRUPTIBLE);
301 }
302 __set_current_state(TASK_RUNNING);
303 return 0;
304}
305
306static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
307{
308 if (cwq->thread == current) {
309 /*
310 * Probably keventd trying to flush its own queue. So simply run
311 * it by hand rather than deadlocking.
312 */
313 run_workqueue(cwq);
314 } else {
315 DEFINE_WAIT(wait);
316 long sequence_needed;
317
318 spin_lock_irq(&cwq->lock);
319 sequence_needed = cwq->insert_sequence;
320
321 while (sequence_needed - cwq->remove_sequence > 0) {
322 prepare_to_wait(&cwq->work_done, &wait,
323 TASK_UNINTERRUPTIBLE);
324 spin_unlock_irq(&cwq->lock);
325 schedule();
326 spin_lock_irq(&cwq->lock);
327 }
328 finish_wait(&cwq->work_done, &wait);
329 spin_unlock_irq(&cwq->lock);
330 }
331}
332
0fcb78c2 333/**
1da177e4 334 * flush_workqueue - ensure that any scheduled work has run to completion.
0fcb78c2 335 * @wq: workqueue to flush
1da177e4
LT
336 *
337 * Forces execution of the workqueue and blocks until its completion.
338 * This is typically used in driver shutdown handlers.
339 *
340 * This function will sample each workqueue's current insert_sequence number and
341 * will sleep until the head sequence is greater than or equal to that. This
342 * means that we sleep until all works which were queued on entry have been
343 * handled, but we are not livelocked by new incoming ones.
344 *
345 * This function used to run the workqueues itself. Now we just wait for the
346 * helper threads to do it.
347 */
348void fastcall flush_workqueue(struct workqueue_struct *wq)
349{
350 might_sleep();
351
352 if (is_single_threaded(wq)) {
bce61dd4 353 /* Always use first cpu's area. */
f756d5e2 354 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
1da177e4
LT
355 } else {
356 int cpu;
357
9b41ea72 358 mutex_lock(&workqueue_mutex);
1da177e4 359 for_each_online_cpu(cpu)
89ada679 360 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
9b41ea72 361 mutex_unlock(&workqueue_mutex);
1da177e4
LT
362 }
363}
ae90dd5d 364EXPORT_SYMBOL_GPL(flush_workqueue);
1da177e4
LT
365
366static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
367 int cpu)
368{
89ada679 369 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
370 struct task_struct *p;
371
372 spin_lock_init(&cwq->lock);
373 cwq->wq = wq;
374 cwq->thread = NULL;
375 cwq->insert_sequence = 0;
376 cwq->remove_sequence = 0;
377 INIT_LIST_HEAD(&cwq->worklist);
378 init_waitqueue_head(&cwq->more_work);
379 init_waitqueue_head(&cwq->work_done);
380
381 if (is_single_threaded(wq))
382 p = kthread_create(worker_thread, cwq, "%s", wq->name);
383 else
384 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
385 if (IS_ERR(p))
386 return NULL;
387 cwq->thread = p;
388 return p;
389}
390
391struct workqueue_struct *__create_workqueue(const char *name,
392 int singlethread)
393{
394 int cpu, destroy = 0;
395 struct workqueue_struct *wq;
396 struct task_struct *p;
397
dd392710 398 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1da177e4
LT
399 if (!wq)
400 return NULL;
1da177e4 401
89ada679 402 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
676121fc
BC
403 if (!wq->cpu_wq) {
404 kfree(wq);
405 return NULL;
406 }
407
1da177e4 408 wq->name = name;
9b41ea72 409 mutex_lock(&workqueue_mutex);
1da177e4
LT
410 if (singlethread) {
411 INIT_LIST_HEAD(&wq->list);
f756d5e2 412 p = create_workqueue_thread(wq, singlethread_cpu);
1da177e4
LT
413 if (!p)
414 destroy = 1;
415 else
416 wake_up_process(p);
417 } else {
1da177e4 418 list_add(&wq->list, &workqueues);
1da177e4
LT
419 for_each_online_cpu(cpu) {
420 p = create_workqueue_thread(wq, cpu);
421 if (p) {
422 kthread_bind(p, cpu);
423 wake_up_process(p);
424 } else
425 destroy = 1;
426 }
427 }
9b41ea72 428 mutex_unlock(&workqueue_mutex);
1da177e4
LT
429
430 /*
431 * Was there any error during startup? If yes then clean up:
432 */
433 if (destroy) {
434 destroy_workqueue(wq);
435 wq = NULL;
436 }
437 return wq;
438}
ae90dd5d 439EXPORT_SYMBOL_GPL(__create_workqueue);
1da177e4
LT
440
441static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
442{
443 struct cpu_workqueue_struct *cwq;
444 unsigned long flags;
445 struct task_struct *p;
446
89ada679 447 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4
LT
448 spin_lock_irqsave(&cwq->lock, flags);
449 p = cwq->thread;
450 cwq->thread = NULL;
451 spin_unlock_irqrestore(&cwq->lock, flags);
452 if (p)
453 kthread_stop(p);
454}
455
0fcb78c2
REB
456/**
457 * destroy_workqueue - safely terminate a workqueue
458 * @wq: target workqueue
459 *
460 * Safely destroy a workqueue. All work currently pending will be done first.
461 */
1da177e4
LT
462void destroy_workqueue(struct workqueue_struct *wq)
463{
464 int cpu;
465
466 flush_workqueue(wq);
467
468 /* We don't need the distraction of CPUs appearing and vanishing. */
9b41ea72 469 mutex_lock(&workqueue_mutex);
1da177e4 470 if (is_single_threaded(wq))
f756d5e2 471 cleanup_workqueue_thread(wq, singlethread_cpu);
1da177e4
LT
472 else {
473 for_each_online_cpu(cpu)
474 cleanup_workqueue_thread(wq, cpu);
1da177e4 475 list_del(&wq->list);
1da177e4 476 }
9b41ea72 477 mutex_unlock(&workqueue_mutex);
89ada679 478 free_percpu(wq->cpu_wq);
1da177e4
LT
479 kfree(wq);
480}
ae90dd5d 481EXPORT_SYMBOL_GPL(destroy_workqueue);
1da177e4
LT
482
483static struct workqueue_struct *keventd_wq;
484
0fcb78c2
REB
485/**
486 * schedule_work - put work task in global workqueue
487 * @work: job to be done
488 *
489 * This puts a job in the kernel-global workqueue.
490 */
1da177e4
LT
491int fastcall schedule_work(struct work_struct *work)
492{
493 return queue_work(keventd_wq, work);
494}
ae90dd5d 495EXPORT_SYMBOL(schedule_work);
1da177e4 496
0fcb78c2
REB
497/**
498 * schedule_delayed_work - put work task in global workqueue after delay
52bad64d
DH
499 * @dwork: job to be done
500 * @delay: number of jiffies to wait or 0 for immediate execution
0fcb78c2
REB
501 *
502 * After waiting for a given time this puts a job in the kernel-global
503 * workqueue.
504 */
52bad64d 505int fastcall schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
1da177e4 506{
52bad64d 507 return queue_delayed_work(keventd_wq, dwork, delay);
1da177e4 508}
ae90dd5d 509EXPORT_SYMBOL(schedule_delayed_work);
1da177e4 510
0fcb78c2
REB
511/**
512 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
513 * @cpu: cpu to use
52bad64d 514 * @dwork: job to be done
0fcb78c2
REB
515 * @delay: number of jiffies to wait
516 *
517 * After waiting for a given time this puts a job in the kernel-global
518 * workqueue on the specified CPU.
519 */
1da177e4 520int schedule_delayed_work_on(int cpu,
52bad64d 521 struct delayed_work *dwork, unsigned long delay)
1da177e4 522{
52bad64d 523 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
1da177e4 524}
ae90dd5d 525EXPORT_SYMBOL(schedule_delayed_work_on);
1da177e4 526
b6136773
AM
527/**
528 * schedule_on_each_cpu - call a function on each online CPU from keventd
529 * @func: the function to call
b6136773
AM
530 *
531 * Returns zero on success.
532 * Returns -ve errno on failure.
533 *
534 * Appears to be racy against CPU hotplug.
535 *
536 * schedule_on_each_cpu() is very slow.
537 */
65f27f38 538int schedule_on_each_cpu(work_func_t func)
15316ba8
CL
539{
540 int cpu;
b6136773 541 struct work_struct *works;
15316ba8 542
b6136773
AM
543 works = alloc_percpu(struct work_struct);
544 if (!works)
15316ba8 545 return -ENOMEM;
b6136773 546
9b41ea72 547 mutex_lock(&workqueue_mutex);
15316ba8 548 for_each_online_cpu(cpu) {
65f27f38 549 INIT_WORK(per_cpu_ptr(works, cpu), func);
15316ba8 550 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
b6136773 551 per_cpu_ptr(works, cpu));
15316ba8 552 }
9b41ea72 553 mutex_unlock(&workqueue_mutex);
15316ba8 554 flush_workqueue(keventd_wq);
b6136773 555 free_percpu(works);
15316ba8
CL
556 return 0;
557}
558
1da177e4
LT
559void flush_scheduled_work(void)
560{
561 flush_workqueue(keventd_wq);
562}
ae90dd5d 563EXPORT_SYMBOL(flush_scheduled_work);
1da177e4
LT
564
565/**
566 * cancel_rearming_delayed_workqueue - reliably kill off a delayed
567 * work whose handler rearms the delayed work.
568 * @wq: the controlling workqueue structure
52bad64d 569 * @dwork: the delayed work struct
1da177e4 570 */
81ddef77 571void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
52bad64d 572 struct delayed_work *dwork)
1da177e4 573{
52bad64d 574 while (!cancel_delayed_work(dwork))
1da177e4
LT
575 flush_workqueue(wq);
576}
81ddef77 577EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
1da177e4
LT
578
579/**
580 * cancel_rearming_delayed_work - reliably kill off a delayed keventd
581 * work whose handler rearms the delayed work.
52bad64d 582 * @dwork: the delayed work struct
1da177e4 583 */
52bad64d 584void cancel_rearming_delayed_work(struct delayed_work *dwork)
1da177e4 585{
52bad64d 586 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
1da177e4
LT
587}
588EXPORT_SYMBOL(cancel_rearming_delayed_work);
589
1fa44eca
JB
590/**
591 * execute_in_process_context - reliably execute the routine with user context
592 * @fn: the function to execute
1fa44eca
JB
593 * @ew: guaranteed storage for the execute work structure (must
594 * be available when the work executes)
595 *
596 * Executes the function immediately if process context is available,
597 * otherwise schedules the function for delayed execution.
598 *
599 * Returns: 0 - function was executed
600 * 1 - function was scheduled for execution
601 */
65f27f38 602int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1fa44eca
JB
603{
604 if (!in_interrupt()) {
65f27f38 605 fn(&ew->work);
1fa44eca
JB
606 return 0;
607 }
608
65f27f38 609 INIT_WORK(&ew->work, fn);
1fa44eca
JB
610 schedule_work(&ew->work);
611
612 return 1;
613}
614EXPORT_SYMBOL_GPL(execute_in_process_context);
615
1da177e4
LT
616int keventd_up(void)
617{
618 return keventd_wq != NULL;
619}
620
621int current_is_keventd(void)
622{
623 struct cpu_workqueue_struct *cwq;
624 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
625 int ret = 0;
626
627 BUG_ON(!keventd_wq);
628
89ada679 629 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
1da177e4
LT
630 if (current == cwq->thread)
631 ret = 1;
632
633 return ret;
634
635}
636
637#ifdef CONFIG_HOTPLUG_CPU
638/* Take the work from this (downed) CPU. */
639static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
640{
89ada679 641 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
626ab0e6 642 struct list_head list;
1da177e4
LT
643 struct work_struct *work;
644
645 spin_lock_irq(&cwq->lock);
626ab0e6 646 list_replace_init(&cwq->worklist, &list);
1da177e4
LT
647
648 while (!list_empty(&list)) {
649 printk("Taking work for %s\n", wq->name);
650 work = list_entry(list.next,struct work_struct,entry);
651 list_del(&work->entry);
89ada679 652 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
1da177e4
LT
653 }
654 spin_unlock_irq(&cwq->lock);
655}
656
657/* We're holding the cpucontrol mutex here */
9c7b216d 658static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1da177e4
LT
659 unsigned long action,
660 void *hcpu)
661{
662 unsigned int hotcpu = (unsigned long)hcpu;
663 struct workqueue_struct *wq;
664
665 switch (action) {
666 case CPU_UP_PREPARE:
9b41ea72 667 mutex_lock(&workqueue_mutex);
1da177e4
LT
668 /* Create a new workqueue thread for it. */
669 list_for_each_entry(wq, &workqueues, list) {
230649da 670 if (!create_workqueue_thread(wq, hotcpu)) {
1da177e4
LT
671 printk("workqueue for %i failed\n", hotcpu);
672 return NOTIFY_BAD;
673 }
674 }
675 break;
676
677 case CPU_ONLINE:
678 /* Kick off worker threads. */
679 list_for_each_entry(wq, &workqueues, list) {
89ada679
CL
680 struct cpu_workqueue_struct *cwq;
681
682 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
683 kthread_bind(cwq->thread, hotcpu);
684 wake_up_process(cwq->thread);
1da177e4 685 }
9b41ea72 686 mutex_unlock(&workqueue_mutex);
1da177e4
LT
687 break;
688
689 case CPU_UP_CANCELED:
690 list_for_each_entry(wq, &workqueues, list) {
fc75cdfa
HC
691 if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
692 continue;
1da177e4 693 /* Unbind so it can run. */
89ada679 694 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
a4c4af7c 695 any_online_cpu(cpu_online_map));
1da177e4
LT
696 cleanup_workqueue_thread(wq, hotcpu);
697 }
9b41ea72
AM
698 mutex_unlock(&workqueue_mutex);
699 break;
700
701 case CPU_DOWN_PREPARE:
702 mutex_lock(&workqueue_mutex);
703 break;
704
705 case CPU_DOWN_FAILED:
706 mutex_unlock(&workqueue_mutex);
1da177e4
LT
707 break;
708
709 case CPU_DEAD:
710 list_for_each_entry(wq, &workqueues, list)
711 cleanup_workqueue_thread(wq, hotcpu);
712 list_for_each_entry(wq, &workqueues, list)
713 take_over_work(wq, hotcpu);
9b41ea72 714 mutex_unlock(&workqueue_mutex);
1da177e4
LT
715 break;
716 }
717
718 return NOTIFY_OK;
719}
720#endif
721
722void init_workqueues(void)
723{
f756d5e2 724 singlethread_cpu = first_cpu(cpu_possible_map);
1da177e4
LT
725 hotcpu_notifier(workqueue_cpu_callback, 0);
726 keventd_wq = create_workqueue("events");
727 BUG_ON(!keventd_wq);
728}
729