workqueue: kill run_scheduled_work()
[linux-2.6-block.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>
341a5958 32#include <linux/freezer.h>
d5abe669
PZ
33#include <linux/kallsyms.h>
34#include <linux/debug_locks.h>
1da177e4
LT
35
36/*
f756d5e2
NL
37 * The per-CPU workqueue (if single thread, we always use the first
38 * possible cpu).
1da177e4
LT
39 */
40struct cpu_workqueue_struct {
41
42 spinlock_t lock;
43
1da177e4
LT
44 struct list_head worklist;
45 wait_queue_head_t more_work;
3af24433 46 struct work_struct *current_work;
1da177e4
LT
47
48 struct workqueue_struct *wq;
36c8b586 49 struct task_struct *thread;
3af24433 50 int should_stop;
1da177e4
LT
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 */
59struct workqueue_struct {
89ada679 60 struct cpu_workqueue_struct *cpu_wq;
1da177e4
LT
61 const char *name;
62 struct list_head list; /* Empty if single thread */
319c2a98 63 int freezeable; /* Freeze threads during suspend */
1da177e4
LT
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. */
9b41ea72 68static DEFINE_MUTEX(workqueue_mutex);
1da177e4
LT
69static LIST_HEAD(workqueues);
70
3af24433
ON
71static int singlethread_cpu __read_mostly;
72/* optimization, we could use cpu_possible_map */
73static cpumask_t cpu_populated_map __read_mostly;
f756d5e2 74
1da177e4
LT
75/* If it's single threaded, it isn't in the list of workqueues. */
76static inline int is_single_threaded(struct workqueue_struct *wq)
77{
78 return list_empty(&wq->list);
79}
80
4594bf15
DH
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 */
365970a1
DH
85static inline void set_wq_data(struct work_struct *work, void *wq)
86{
4594bf15
DH
87 unsigned long new;
88
89 BUG_ON(!work_pending(work));
365970a1 90
365970a1 91 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
a08727ba
LT
92 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
93 atomic_long_set(&work->data, new);
365970a1
DH
94}
95
96static inline void *get_wq_data(struct work_struct *work)
97{
a08727ba 98 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
365970a1
DH
99}
100
b89deed3
ON
101static 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
1da177e4
LT
112/* Preempt must be disabled. */
113static 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);
b89deed3 119 insert_work(cwq, work, 1);
1da177e4
LT
120 spin_unlock_irqrestore(&cwq->lock, flags);
121}
122
0fcb78c2
REB
123/**
124 * queue_work - queue work on a workqueue
125 * @wq: workqueue to use
126 * @work: work to queue
127 *
057647fc 128 * Returns 0 if @work was already on a queue, non-zero otherwise.
1da177e4
LT
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 */
133int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
134{
135 int ret = 0, cpu = get_cpu();
136
a08727ba 137 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
1da177e4 138 if (unlikely(is_single_threaded(wq)))
f756d5e2 139 cpu = singlethread_cpu;
1da177e4 140 BUG_ON(!list_empty(&work->entry));
89ada679 141 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
142 ret = 1;
143 }
144 put_cpu();
145 return ret;
146}
ae90dd5d 147EXPORT_SYMBOL_GPL(queue_work);
1da177e4 148
82f67cd9 149void delayed_work_timer_fn(unsigned long __data)
1da177e4 150{
52bad64d 151 struct delayed_work *dwork = (struct delayed_work *)__data;
365970a1 152 struct workqueue_struct *wq = get_wq_data(&dwork->work);
1da177e4
LT
153 int cpu = smp_processor_id();
154
155 if (unlikely(is_single_threaded(wq)))
f756d5e2 156 cpu = singlethread_cpu;
1da177e4 157
52bad64d 158 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
1da177e4
LT
159}
160
0fcb78c2
REB
161/**
162 * queue_delayed_work - queue work on a workqueue after delay
163 * @wq: workqueue to use
af9997e4 164 * @dwork: delayable work to queue
0fcb78c2
REB
165 * @delay: number of jiffies to wait before queueing
166 *
057647fc 167 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 168 */
1da177e4 169int fastcall queue_delayed_work(struct workqueue_struct *wq,
52bad64d 170 struct delayed_work *dwork, unsigned long delay)
1da177e4
LT
171{
172 int ret = 0;
52bad64d
DH
173 struct timer_list *timer = &dwork->timer;
174 struct work_struct *work = &dwork->work;
175
82f67cd9 176 timer_stats_timer_set_start_info(timer);
52bad64d
DH
177 if (delay == 0)
178 return queue_work(wq, work);
1da177e4 179
a08727ba 180 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
1da177e4
LT
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 */
365970a1 185 set_wq_data(work, wq);
1da177e4 186 timer->expires = jiffies + delay;
52bad64d 187 timer->data = (unsigned long)dwork;
1da177e4
LT
188 timer->function = delayed_work_timer_fn;
189 add_timer(timer);
190 ret = 1;
191 }
192 return ret;
193}
ae90dd5d 194EXPORT_SYMBOL_GPL(queue_delayed_work);
1da177e4 195
0fcb78c2
REB
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
af9997e4 200 * @dwork: work to queue
0fcb78c2
REB
201 * @delay: number of jiffies to wait before queueing
202 *
057647fc 203 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 204 */
7a6bc1cd 205int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
52bad64d 206 struct delayed_work *dwork, unsigned long delay)
7a6bc1cd
VP
207{
208 int ret = 0;
52bad64d
DH
209 struct timer_list *timer = &dwork->timer;
210 struct work_struct *work = &dwork->work;
7a6bc1cd 211
a08727ba 212 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
7a6bc1cd
VP
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 */
365970a1 217 set_wq_data(work, wq);
7a6bc1cd 218 timer->expires = jiffies + delay;
52bad64d 219 timer->data = (unsigned long)dwork;
7a6bc1cd
VP
220 timer->function = delayed_work_timer_fn;
221 add_timer_on(timer, cpu);
222 ret = 1;
223 }
224 return ret;
225}
ae90dd5d 226EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4 227
858119e1 228static void run_workqueue(struct cpu_workqueue_struct *cwq)
1da177e4
LT
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);
6bb49e59 247 work_func_t f = work->func;
1da177e4 248
b89deed3 249 cwq->current_work = work;
1da177e4
LT
250 list_del_init(cwq->worklist.next);
251 spin_unlock_irqrestore(&cwq->lock, flags);
252
365970a1 253 BUG_ON(get_wq_data(work) != cwq);
a08727ba 254 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
65f27f38
DH
255 work_release(work);
256 f(work);
1da177e4 257
d5abe669
PZ
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
1da177e4 269 spin_lock_irqsave(&cwq->lock, flags);
b89deed3 270 cwq->current_work = NULL;
1da177e4
LT
271 }
272 cwq->run_depth--;
273 spin_unlock_irqrestore(&cwq->lock, flags);
274}
275
3af24433
ON
276/*
277 * NOTE: the caller must not touch *cwq if this func returns true
278 */
279static 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
1da177e4
LT
294static int worker_thread(void *__cwq)
295{
296 struct cpu_workqueue_struct *cwq = __cwq;
3af24433 297 DEFINE_WAIT(wait);
1da177e4
LT
298 struct k_sigaction sa;
299 sigset_t blocked;
300
319c2a98 301 if (!cwq->wq->freezeable)
341a5958 302 current->flags |= PF_NOFREEZE;
1da177e4
LT
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
46934023
CL
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
1da177e4
LT
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
3af24433 323 for (;;) {
319c2a98 324 if (cwq->wq->freezeable)
341a5958
RW
325 try_to_freeze();
326
3af24433
ON
327 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
328 if (!cwq->should_stop && list_empty(&cwq->worklist))
1da177e4 329 schedule();
3af24433
ON
330 finish_wait(&cwq->more_work, &wait);
331
332 if (cwq_should_stop(cwq))
333 break;
1da177e4 334
3af24433 335 run_workqueue(cwq);
1da177e4 336 }
3af24433 337
1da177e4
LT
338 return 0;
339}
340
fc2e4d70
ON
341struct wq_barrier {
342 struct work_struct work;
343 struct completion done;
344};
345
346static 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
83c22520
ON
352static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
353 struct wq_barrier *barr, int tail)
fc2e4d70
ON
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);
83c22520
ON
359
360 insert_work(cwq, &barr->work, tail);
fc2e4d70
ON
361}
362
1da177e4
LT
363static 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 {
fc2e4d70 372 struct wq_barrier barr;
83c22520 373 int active = 0;
1da177e4 374
83c22520
ON
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);
1da177e4 381
d721304d 382 if (active)
83c22520 383 wait_for_completion(&barr.done);
1da177e4
LT
384 }
385}
386
0fcb78c2 387/**
1da177e4 388 * flush_workqueue - ensure that any scheduled work has run to completion.
0fcb78c2 389 * @wq: workqueue to flush
1da177e4
LT
390 *
391 * Forces execution of the workqueue and blocks until its completion.
392 * This is typically used in driver shutdown handlers.
393 *
fc2e4d70
ON
394 * We sleep until all works which were queued on entry have been handled,
395 * but we are not livelocked by new incoming ones.
1da177e4
LT
396 *
397 * This function used to run the workqueues itself. Now we just wait for the
398 * helper threads to do it.
399 */
400void fastcall flush_workqueue(struct workqueue_struct *wq)
401{
3af24433 402 if (is_single_threaded(wq))
f756d5e2 403 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
3af24433 404 else {
1da177e4
LT
405 int cpu;
406
3af24433 407 for_each_cpu_mask(cpu, cpu_populated_map)
89ada679 408 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
1da177e4
LT
409 }
410}
ae90dd5d 411EXPORT_SYMBOL_GPL(flush_workqueue);
1da177e4 412
b89deed3
ON
413static 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)) {
83c22520 421 insert_wq_barrier(cwq, &barr, 0);
b89deed3
ON
422 running = 1;
423 }
424 spin_unlock_irq(&cwq->lock);
425
3af24433 426 if (unlikely(running))
b89deed3 427 wait_for_completion(&barr.done);
b89deed3
ON
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 */
444void flush_work(struct workqueue_struct *wq, struct work_struct *work)
445{
446 struct cpu_workqueue_struct *cwq;
447
b89deed3
ON
448 cwq = get_wq_data(work);
449 /* Was it ever queued ? */
450 if (!cwq)
3af24433 451 return;
b89deed3
ON
452
453 /*
3af24433
ON
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.
b89deed3
ON
456 */
457 spin_lock_irq(&cwq->lock);
458 list_del_init(&work->entry);
459 work_release(work);
460 spin_unlock_irq(&cwq->lock);
461
3af24433 462 if (is_single_threaded(wq))
b89deed3 463 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
3af24433 464 else {
b89deed3
ON
465 int cpu;
466
3af24433 467 for_each_cpu_mask(cpu, cpu_populated_map)
b89deed3
ON
468 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
469 }
b89deed3
ON
470}
471EXPORT_SYMBOL_GPL(flush_work);
472
1da177e4
LT
473
474static struct workqueue_struct *keventd_wq;
475
0fcb78c2
REB
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 */
1da177e4
LT
482int fastcall schedule_work(struct work_struct *work)
483{
484 return queue_work(keventd_wq, work);
485}
ae90dd5d 486EXPORT_SYMBOL(schedule_work);
1da177e4 487
0fcb78c2
REB
488/**
489 * schedule_delayed_work - put work task in global workqueue after delay
52bad64d
DH
490 * @dwork: job to be done
491 * @delay: number of jiffies to wait or 0 for immediate execution
0fcb78c2
REB
492 *
493 * After waiting for a given time this puts a job in the kernel-global
494 * workqueue.
495 */
82f67cd9
IM
496int fastcall schedule_delayed_work(struct delayed_work *dwork,
497 unsigned long delay)
1da177e4 498{
82f67cd9 499 timer_stats_timer_set_start_info(&dwork->timer);
52bad64d 500 return queue_delayed_work(keventd_wq, dwork, delay);
1da177e4 501}
ae90dd5d 502EXPORT_SYMBOL(schedule_delayed_work);
1da177e4 503
0fcb78c2
REB
504/**
505 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
506 * @cpu: cpu to use
52bad64d 507 * @dwork: job to be done
0fcb78c2
REB
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 */
1da177e4 513int schedule_delayed_work_on(int cpu,
52bad64d 514 struct delayed_work *dwork, unsigned long delay)
1da177e4 515{
52bad64d 516 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
1da177e4 517}
ae90dd5d 518EXPORT_SYMBOL(schedule_delayed_work_on);
1da177e4 519
b6136773
AM
520/**
521 * schedule_on_each_cpu - call a function on each online CPU from keventd
522 * @func: the function to call
b6136773
AM
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 */
65f27f38 531int schedule_on_each_cpu(work_func_t func)
15316ba8
CL
532{
533 int cpu;
b6136773 534 struct work_struct *works;
15316ba8 535
b6136773
AM
536 works = alloc_percpu(struct work_struct);
537 if (!works)
15316ba8 538 return -ENOMEM;
b6136773 539
e18f3ffb 540 preempt_disable(); /* CPU hotplug */
15316ba8 541 for_each_online_cpu(cpu) {
9bfb1839
IM
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);
15316ba8 547 }
e18f3ffb 548 preempt_enable();
15316ba8 549 flush_workqueue(keventd_wq);
b6136773 550 free_percpu(works);
15316ba8
CL
551 return 0;
552}
553
1da177e4
LT
554void flush_scheduled_work(void)
555{
556 flush_workqueue(keventd_wq);
557}
ae90dd5d 558EXPORT_SYMBOL(flush_scheduled_work);
1da177e4 559
b89deed3
ON
560void flush_work_keventd(struct work_struct *work)
561{
562 flush_work(keventd_wq, work);
563}
564EXPORT_SYMBOL(flush_work_keventd);
565
1da177e4 566/**
72fd4a35 567 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
1da177e4 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/**
72fd4a35 580 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
52bad64d 581 * @dwork: the delayed work struct
1da177e4 582 */
52bad64d 583void cancel_rearming_delayed_work(struct delayed_work *dwork)
1da177e4 584{
52bad64d 585 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
1da177e4
LT
586}
587EXPORT_SYMBOL(cancel_rearming_delayed_work);
588
1fa44eca
JB
589/**
590 * execute_in_process_context - reliably execute the routine with user context
591 * @fn: the function to execute
1fa44eca
JB
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 */
65f27f38 601int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1fa44eca
JB
602{
603 if (!in_interrupt()) {
65f27f38 604 fn(&ew->work);
1fa44eca
JB
605 return 0;
606 }
607
65f27f38 608 INIT_WORK(&ew->work, fn);
1fa44eca
JB
609 schedule_work(&ew->work);
610
611 return 1;
612}
613EXPORT_SYMBOL_GPL(execute_in_process_context);
614
1da177e4
LT
615int keventd_up(void)
616{
617 return keventd_wq != NULL;
618}
619
620int 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
89ada679 628 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
1da177e4
LT
629 if (current == cwq->thread)
630 ret = 1;
631
632 return ret;
633
634}
635
3af24433
ON
636static struct cpu_workqueue_struct *
637init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
1da177e4 638{
89ada679 639 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4 640
3af24433
ON
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;
1da177e4
LT
647}
648
3af24433
ON
649static 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
678struct workqueue_struct *__create_workqueue(const char *name,
679 int singlethread, int freezeable)
1da177e4 680{
1da177e4 681 struct workqueue_struct *wq;
3af24433
ON
682 struct cpu_workqueue_struct *cwq;
683 int err = 0, cpu;
1da177e4 684
3af24433
ON
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 {
9b41ea72 703 mutex_lock(&workqueue_mutex);
3af24433
ON
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);
1da177e4 711 }
3af24433
ON
712 mutex_unlock(&workqueue_mutex);
713 }
714
715 if (err) {
716 destroy_workqueue(wq);
717 wq = NULL;
718 }
719 return wq;
720}
721EXPORT_SYMBOL_GPL(__create_workqueue);
1da177e4 722
3af24433
ON
723static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
724{
725 struct wq_barrier barr;
726 int alive = 0;
89ada679 727
3af24433
ON
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 */
756void 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);
9b41ea72 768 mutex_unlock(&workqueue_mutex);
1da177e4 769
3af24433
ON
770 for_each_cpu_mask(cpu, cpu_populated_map) {
771 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
772 cleanup_workqueue_thread(cwq, cpu);
1da177e4 773 }
3af24433 774 }
9b41ea72 775
3af24433
ON
776 free_percpu(wq->cpu_wq);
777 kfree(wq);
778}
779EXPORT_SYMBOL_GPL(destroy_workqueue);
780
781static 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:
9b41ea72 791 mutex_lock(&workqueue_mutex);
3af24433 792 return NOTIFY_OK;
9b41ea72 793
3af24433 794 case CPU_LOCK_RELEASE:
9b41ea72 795 mutex_unlock(&workqueue_mutex);
3af24433 796 return NOTIFY_OK;
1da177e4 797
3af24433
ON
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 }
1da177e4
LT
823 }
824
825 return NOTIFY_OK;
826}
1da177e4
LT
827
828void init_workqueues(void)
829{
3af24433 830 cpu_populated_map = cpu_online_map;
f756d5e2 831 singlethread_cpu = first_cpu(cpu_possible_map);
1da177e4
LT
832 hotcpu_notifier(workqueue_cpu_callback, 0);
833 keventd_wq = create_workqueue("events");
834 BUG_ON(!keventd_wq);
835}