blob: 918d55267a12a98c43aa1c9ee43e17644712af8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Christoph Lameter89ada672005-10-30 15:01:59 -080015 *
16 * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
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>
James Bottomley1fa44ec2006-02-23 12:43:43 -060030#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070031#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080032#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080033#include <linux/kallsyms.h>
34#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080037 * The per-CPU workqueue (if single thread, we always use the first
38 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40struct cpu_workqueue_struct {
41
42 spinlock_t lock;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct list_head worklist;
45 wait_queue_head_t more_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 struct workqueue_struct *wq;
Ingo Molnar36c8b582006-07-03 00:25:41 -070048 struct task_struct *thread;
Oleg Nesterovb89deed2007-05-09 02:33:52 -070049 struct work_struct *current_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 int run_depth; /* Detect run_workqueue() recursion depth */
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080052
53 int freezeable; /* Freeze the thread during suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054} ____cacheline_aligned;
55
56/*
57 * The externally visible workqueue abstraction is an array of
58 * per-CPU workqueues:
59 */
60struct workqueue_struct {
Christoph Lameter89ada672005-10-30 15:01:59 -080061 struct cpu_workqueue_struct *cpu_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 const char *name;
63 struct list_head list; /* Empty if single thread */
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. */
Andrew Morton9b41ea72006-08-13 23:24:26 -070068static DEFINE_MUTEX(workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static LIST_HEAD(workqueues);
70
Nathan Lynchf756d5e2006-01-08 01:05:12 -080071static int singlethread_cpu;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* If it's single threaded, it isn't in the list of workqueues. */
74static inline int is_single_threaded(struct workqueue_struct *wq)
75{
76 return list_empty(&wq->list);
77}
78
David Howells4594bf12006-12-07 11:33:26 +000079/*
80 * Set the workqueue on which a work item is to be run
81 * - Must *only* be called if the pending flag is set
82 */
David Howells365970a2006-11-22 14:54:49 +000083static inline void set_wq_data(struct work_struct *work, void *wq)
84{
David Howells4594bf12006-12-07 11:33:26 +000085 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +000086
David Howells4594bf12006-12-07 11:33:26 +000087 BUG_ON(!work_pending(work));
88
David Howells365970a2006-11-22 14:54:49 +000089 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -080090 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
91 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +000092}
93
94static inline void *get_wq_data(struct work_struct *work)
95{
Linus Torvaldsa08727b2006-12-16 09:53:50 -080096 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +000097}
98
Linus Torvalds68380b52006-12-07 09:28:19 -080099static int __run_work(struct cpu_workqueue_struct *cwq, struct work_struct *work)
100{
101 int ret = 0;
102 unsigned long flags;
103
104 spin_lock_irqsave(&cwq->lock, flags);
105 /*
106 * We need to re-validate the work info after we've gotten
107 * the cpu_workqueue lock. We can run the work now iff:
108 *
109 * - the wq_data still matches the cpu_workqueue_struct
110 * - AND the work is still marked pending
111 * - AND the work is still on a list (which will be this
112 * workqueue_struct list)
113 *
114 * All these conditions are important, because we
115 * need to protect against the work being run right
116 * now on another CPU (all but the last one might be
117 * true if it's currently running and has not been
118 * released yet, for example).
119 */
120 if (get_wq_data(work) == cwq
121 && work_pending(work)
122 && !list_empty(&work->entry)) {
123 work_func_t f = work->func;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700124 cwq->current_work = work;
Linus Torvalds68380b52006-12-07 09:28:19 -0800125 list_del_init(&work->entry);
126 spin_unlock_irqrestore(&cwq->lock, flags);
127
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800128 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
Linus Torvalds68380b52006-12-07 09:28:19 -0800129 work_release(work);
130 f(work);
131
132 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700133 cwq->current_work = NULL;
Linus Torvalds68380b52006-12-07 09:28:19 -0800134 ret = 1;
135 }
136 spin_unlock_irqrestore(&cwq->lock, flags);
137 return ret;
138}
139
140/**
141 * run_scheduled_work - run scheduled work synchronously
142 * @work: work to run
143 *
144 * This checks if the work was pending, and runs it
145 * synchronously if so. It returns a boolean to indicate
146 * whether it had any scheduled work to run or not.
147 *
148 * NOTE! This _only_ works for normal work_structs. You
149 * CANNOT use this for delayed work, because the wq data
150 * for delayed work will not point properly to the per-
151 * CPU workqueue struct, but will change!
152 */
153int fastcall run_scheduled_work(struct work_struct *work)
154{
155 for (;;) {
156 struct cpu_workqueue_struct *cwq;
157
158 if (!work_pending(work))
159 return 0;
160 if (list_empty(&work->entry))
161 return 0;
162 /* NOTE! This depends intimately on __queue_work! */
163 cwq = get_wq_data(work);
164 if (!cwq)
165 return 0;
166 if (__run_work(cwq, work))
167 return 1;
168 }
169}
170EXPORT_SYMBOL(run_scheduled_work);
171
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700172static void insert_work(struct cpu_workqueue_struct *cwq,
173 struct work_struct *work, int tail)
174{
175 set_wq_data(work, cwq);
176 if (tail)
177 list_add_tail(&work->entry, &cwq->worklist);
178 else
179 list_add(&work->entry, &cwq->worklist);
180 wake_up(&cwq->more_work);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* Preempt must be disabled. */
184static void __queue_work(struct cpu_workqueue_struct *cwq,
185 struct work_struct *work)
186{
187 unsigned long flags;
188
189 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700190 insert_work(cwq, work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 spin_unlock_irqrestore(&cwq->lock, flags);
192}
193
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700194/**
195 * queue_work - queue work on a workqueue
196 * @wq: workqueue to use
197 * @work: work to queue
198 *
Alan Stern057647f2006-10-28 10:38:58 -0700199 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * We queue the work to the CPU it was submitted, but there is no
202 * guarantee that it will be processed by that CPU.
203 */
204int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
205{
206 int ret = 0, cpu = get_cpu();
207
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800208 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800210 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 BUG_ON(!list_empty(&work->entry));
Christoph Lameter89ada672005-10-30 15:01:59 -0800212 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 ret = 1;
214 }
215 put_cpu();
216 return ret;
217}
Dave Jonesae90dd52006-06-30 01:40:45 -0400218EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800220void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
David Howells52bad642006-11-22 14:54:01 +0000222 struct delayed_work *dwork = (struct delayed_work *)__data;
David Howells365970a2006-11-22 14:54:49 +0000223 struct workqueue_struct *wq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int cpu = smp_processor_id();
225
226 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800227 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
David Howells52bad642006-11-22 14:54:01 +0000229 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700232/**
233 * queue_delayed_work - queue work on a workqueue after delay
234 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800235 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700236 * @delay: number of jiffies to wait before queueing
237 *
Alan Stern057647f2006-10-28 10:38:58 -0700238 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240int fastcall queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000241 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000244 struct timer_list *timer = &dwork->timer;
245 struct work_struct *work = &dwork->work;
246
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800247 timer_stats_timer_set_start_info(timer);
David Howells52bad642006-11-22 14:54:01 +0000248 if (delay == 0)
249 return queue_work(wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800251 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 BUG_ON(timer_pending(timer));
253 BUG_ON(!list_empty(&work->entry));
254
255 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000256 set_wq_data(work, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000258 timer->data = (unsigned long)dwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 timer->function = delayed_work_timer_fn;
260 add_timer(timer);
261 ret = 1;
262 }
263 return ret;
264}
Dave Jonesae90dd52006-06-30 01:40:45 -0400265EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700267/**
268 * queue_delayed_work_on - queue work on specific CPU after delay
269 * @cpu: CPU number to execute work on
270 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800271 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700272 * @delay: number of jiffies to wait before queueing
273 *
Alan Stern057647f2006-10-28 10:38:58 -0700274 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700275 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700276int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000277 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700278{
279 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000280 struct timer_list *timer = &dwork->timer;
281 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700282
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800283 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700284 BUG_ON(timer_pending(timer));
285 BUG_ON(!list_empty(&work->entry));
286
287 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000288 set_wq_data(work, wq);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700289 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000290 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700291 timer->function = delayed_work_timer_fn;
292 add_timer_on(timer, cpu);
293 ret = 1;
294 }
295 return ret;
296}
Dave Jonesae90dd52006-06-30 01:40:45 -0400297EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Arjan van de Ven858119e2006-01-14 13:20:43 -0800299static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 unsigned long flags;
302
303 /*
304 * Keep taking off work from the queue until
305 * done.
306 */
307 spin_lock_irqsave(&cwq->lock, flags);
308 cwq->run_depth++;
309 if (cwq->run_depth > 3) {
310 /* morton gets to eat his hat */
311 printk("%s: recursion depth exceeded: %d\n",
312 __FUNCTION__, cwq->run_depth);
313 dump_stack();
314 }
315 while (!list_empty(&cwq->worklist)) {
316 struct work_struct *work = list_entry(cwq->worklist.next,
317 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000318 work_func_t f = work->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700320 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 list_del_init(cwq->worklist.next);
322 spin_unlock_irqrestore(&cwq->lock, flags);
323
David Howells365970a2006-11-22 14:54:49 +0000324 BUG_ON(get_wq_data(work) != cwq);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800325 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
David Howells65f27f32006-11-22 14:55:48 +0000326 work_release(work);
327 f(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800329 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
330 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
331 "%s/0x%08x/%d\n",
332 current->comm, preempt_count(),
333 current->pid);
334 printk(KERN_ERR " last function: ");
335 print_symbol("%s\n", (unsigned long)f);
336 debug_show_held_locks(current);
337 dump_stack();
338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700341 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 cwq->run_depth--;
344 spin_unlock_irqrestore(&cwq->lock, flags);
345}
346
347static int worker_thread(void *__cwq)
348{
349 struct cpu_workqueue_struct *cwq = __cwq;
350 DECLARE_WAITQUEUE(wait, current);
351 struct k_sigaction sa;
352 sigset_t blocked;
353
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800354 if (!cwq->freezeable)
355 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 set_user_nice(current, -5);
358
359 /* Block and flush all signals */
360 sigfillset(&blocked);
361 sigprocmask(SIG_BLOCK, &blocked, NULL);
362 flush_signals(current);
363
Christoph Lameter46934022006-10-11 01:21:26 -0700364 /*
365 * We inherited MPOL_INTERLEAVE from the booting kernel.
366 * Set MPOL_DEFAULT to insure node local allocations.
367 */
368 numa_default_policy();
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
371 sa.sa.sa_handler = SIG_IGN;
372 sa.sa.sa_flags = 0;
373 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
374 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
375
376 set_current_state(TASK_INTERRUPTIBLE);
377 while (!kthread_should_stop()) {
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800378 if (cwq->freezeable)
379 try_to_freeze();
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 add_wait_queue(&cwq->more_work, &wait);
382 if (list_empty(&cwq->worklist))
383 schedule();
384 else
385 __set_current_state(TASK_RUNNING);
386 remove_wait_queue(&cwq->more_work, &wait);
387
388 if (!list_empty(&cwq->worklist))
389 run_workqueue(cwq);
390 set_current_state(TASK_INTERRUPTIBLE);
391 }
392 __set_current_state(TASK_RUNNING);
393 return 0;
394}
395
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700396struct wq_barrier {
397 struct work_struct work;
398 struct completion done;
399};
400
401static void wq_barrier_func(struct work_struct *work)
402{
403 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
404 complete(&barr->done);
405}
406
407static inline void init_wq_barrier(struct wq_barrier *barr)
408{
409 INIT_WORK(&barr->work, wq_barrier_func);
410 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
411
412 init_completion(&barr->done);
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
416{
417 if (cwq->thread == current) {
418 /*
419 * Probably keventd trying to flush its own queue. So simply run
420 * it by hand rather than deadlocking.
421 */
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700422 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 run_workqueue(cwq);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700424 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700426 struct wq_barrier barr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700428 init_wq_barrier(&barr);
429 __queue_work(cwq, &barr.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700431 mutex_unlock(&workqueue_mutex);
432 wait_for_completion(&barr.done);
433 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435}
436
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700437/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700439 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
441 * Forces execution of the workqueue and blocks until its completion.
442 * This is typically used in driver shutdown handlers.
443 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700444 * We sleep until all works which were queued on entry have been handled,
445 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 * This function used to run the workqueues itself. Now we just wait for the
448 * helper threads to do it.
449 */
450void fastcall flush_workqueue(struct workqueue_struct *wq)
451{
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700452 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (is_single_threaded(wq)) {
Ben Collinsbce61dd2005-11-28 13:43:56 -0800454 /* Always use first cpu's area. */
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800455 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else {
457 int cpu;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 for_each_online_cpu(cpu)
Christoph Lameter89ada672005-10-30 15:01:59 -0800460 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700462 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
Dave Jonesae90dd52006-06-30 01:40:45 -0400464EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700466static void wait_on_work(struct cpu_workqueue_struct *cwq,
467 struct work_struct *work)
468{
469 struct wq_barrier barr;
470 int running = 0;
471
472 spin_lock_irq(&cwq->lock);
473 if (unlikely(cwq->current_work == work)) {
474 init_wq_barrier(&barr);
475 insert_work(cwq, &barr.work, 0);
476 running = 1;
477 }
478 spin_unlock_irq(&cwq->lock);
479
480 if (unlikely(running)) {
481 mutex_unlock(&workqueue_mutex);
482 wait_for_completion(&barr.done);
483 mutex_lock(&workqueue_mutex);
484 }
485}
486
487/**
488 * flush_work - block until a work_struct's callback has terminated
489 * @wq: the workqueue on which the work is queued
490 * @work: the work which is to be flushed
491 *
492 * flush_work() will attempt to cancel the work if it is queued. If the work's
493 * callback appears to be running, flush_work() will block until it has
494 * completed.
495 *
496 * flush_work() is designed to be used when the caller is tearing down data
497 * structures which the callback function operates upon. It is expected that,
498 * prior to calling flush_work(), the caller has arranged for the work to not
499 * be requeued.
500 */
501void flush_work(struct workqueue_struct *wq, struct work_struct *work)
502{
503 struct cpu_workqueue_struct *cwq;
504
505 mutex_lock(&workqueue_mutex);
506 cwq = get_wq_data(work);
507 /* Was it ever queued ? */
508 if (!cwq)
509 goto out;
510
511 /*
512 * This work can't be re-queued, and the lock above protects us
513 * from take_over_work(), no need to re-check that get_wq_data()
514 * is still the same when we take cwq->lock.
515 */
516 spin_lock_irq(&cwq->lock);
517 list_del_init(&work->entry);
518 work_release(work);
519 spin_unlock_irq(&cwq->lock);
520
521 if (is_single_threaded(wq)) {
522 /* Always use first cpu's area. */
523 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
524 } else {
525 int cpu;
526
527 for_each_online_cpu(cpu)
528 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
529 }
530out:
531 mutex_unlock(&workqueue_mutex);
532}
533EXPORT_SYMBOL_GPL(flush_work);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800536 int cpu, int freezeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Christoph Lameter89ada672005-10-30 15:01:59 -0800538 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct task_struct *p;
540
541 spin_lock_init(&cwq->lock);
542 cwq->wq = wq;
543 cwq->thread = NULL;
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800544 cwq->freezeable = freezeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 INIT_LIST_HEAD(&cwq->worklist);
546 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (is_single_threaded(wq))
549 p = kthread_create(worker_thread, cwq, "%s", wq->name);
550 else
551 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
552 if (IS_ERR(p))
553 return NULL;
554 cwq->thread = p;
555 return p;
556}
557
558struct workqueue_struct *__create_workqueue(const char *name,
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800559 int singlethread, int freezeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 int cpu, destroy = 0;
562 struct workqueue_struct *wq;
563 struct task_struct *p;
564
Pekka J Enbergdd392712005-09-06 15:18:31 -0700565 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!wq)
567 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Christoph Lameter89ada672005-10-30 15:01:59 -0800569 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
Ben Collins676121f2006-01-08 01:03:04 -0800570 if (!wq->cpu_wq) {
571 kfree(wq);
572 return NULL;
573 }
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 wq->name = name;
Andrew Morton9b41ea72006-08-13 23:24:26 -0700576 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (singlethread) {
578 INIT_LIST_HEAD(&wq->list);
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800579 p = create_workqueue_thread(wq, singlethread_cpu, freezeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!p)
581 destroy = 1;
582 else
583 wake_up_process(p);
584 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 list_add(&wq->list, &workqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 for_each_online_cpu(cpu) {
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800587 p = create_workqueue_thread(wq, cpu, freezeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (p) {
589 kthread_bind(p, cpu);
590 wake_up_process(p);
591 } else
592 destroy = 1;
593 }
594 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700595 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /*
598 * Was there any error during startup? If yes then clean up:
599 */
600 if (destroy) {
601 destroy_workqueue(wq);
602 wq = NULL;
603 }
604 return wq;
605}
Dave Jonesae90dd52006-06-30 01:40:45 -0400606EXPORT_SYMBOL_GPL(__create_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
609{
610 struct cpu_workqueue_struct *cwq;
611 unsigned long flags;
612 struct task_struct *p;
613
Christoph Lameter89ada672005-10-30 15:01:59 -0800614 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 spin_lock_irqsave(&cwq->lock, flags);
616 p = cwq->thread;
617 cwq->thread = NULL;
618 spin_unlock_irqrestore(&cwq->lock, flags);
619 if (p)
620 kthread_stop(p);
621}
622
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700623/**
624 * destroy_workqueue - safely terminate a workqueue
625 * @wq: target workqueue
626 *
627 * Safely destroy a workqueue. All work currently pending will be done first.
628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629void destroy_workqueue(struct workqueue_struct *wq)
630{
631 int cpu;
632
633 flush_workqueue(wq);
634
635 /* We don't need the distraction of CPUs appearing and vanishing. */
Andrew Morton9b41ea72006-08-13 23:24:26 -0700636 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (is_single_threaded(wq))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800638 cleanup_workqueue_thread(wq, singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 else {
640 for_each_online_cpu(cpu)
641 cleanup_workqueue_thread(wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 list_del(&wq->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700644 mutex_unlock(&workqueue_mutex);
Christoph Lameter89ada672005-10-30 15:01:59 -0800645 free_percpu(wq->cpu_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 kfree(wq);
647}
Dave Jonesae90dd52006-06-30 01:40:45 -0400648EXPORT_SYMBOL_GPL(destroy_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650static struct workqueue_struct *keventd_wq;
651
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700652/**
653 * schedule_work - put work task in global workqueue
654 * @work: job to be done
655 *
656 * This puts a job in the kernel-global workqueue.
657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658int fastcall schedule_work(struct work_struct *work)
659{
660 return queue_work(keventd_wq, work);
661}
Dave Jonesae90dd52006-06-30 01:40:45 -0400662EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700664/**
665 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000666 * @dwork: job to be done
667 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700668 *
669 * After waiting for a given time this puts a job in the kernel-global
670 * workqueue.
671 */
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800672int fastcall schedule_delayed_work(struct delayed_work *dwork,
673 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800675 timer_stats_timer_set_start_info(&dwork->timer);
David Howells52bad642006-11-22 14:54:01 +0000676 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
Dave Jonesae90dd52006-06-30 01:40:45 -0400678EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700680/**
681 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
682 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000683 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700684 * @delay: number of jiffies to wait
685 *
686 * After waiting for a given time this puts a job in the kernel-global
687 * workqueue on the specified CPU.
688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000690 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
David Howells52bad642006-11-22 14:54:01 +0000692 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Dave Jonesae90dd52006-06-30 01:40:45 -0400694EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Andrew Mortonb6136772006-06-25 05:47:49 -0700696/**
697 * schedule_on_each_cpu - call a function on each online CPU from keventd
698 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700699 *
700 * Returns zero on success.
701 * Returns -ve errno on failure.
702 *
703 * Appears to be racy against CPU hotplug.
704 *
705 * schedule_on_each_cpu() is very slow.
706 */
David Howells65f27f32006-11-22 14:55:48 +0000707int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800708{
709 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700710 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800711
Andrew Mortonb6136772006-06-25 05:47:49 -0700712 works = alloc_percpu(struct work_struct);
713 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800714 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700715
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700716 preempt_disable(); /* CPU hotplug */
Christoph Lameter15316ba2006-01-08 01:00:43 -0800717 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100718 struct work_struct *work = per_cpu_ptr(works, cpu);
719
720 INIT_WORK(work, func);
721 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
722 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800723 }
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700724 preempt_enable();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800725 flush_workqueue(keventd_wq);
Andrew Mortonb6136772006-06-25 05:47:49 -0700726 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800727 return 0;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730void flush_scheduled_work(void)
731{
732 flush_workqueue(keventd_wq);
733}
Dave Jonesae90dd52006-06-30 01:40:45 -0400734EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700736void flush_work_keventd(struct work_struct *work)
737{
738 flush_work(keventd_wq, work);
739}
740EXPORT_SYMBOL(flush_work_keventd);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800743 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * @wq: the controlling workqueue structure
David Howells52bad642006-11-22 14:54:01 +0000745 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
James Bottomley81ddef72005-04-16 15:23:59 -0700747void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000748 struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
David Howells52bad642006-11-22 14:54:01 +0000750 while (!cancel_delayed_work(dwork))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 flush_workqueue(wq);
752}
James Bottomley81ddef72005-04-16 15:23:59 -0700753EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800756 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
David Howells52bad642006-11-22 14:54:01 +0000757 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
David Howells52bad642006-11-22 14:54:01 +0000759void cancel_rearming_delayed_work(struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
David Howells52bad642006-11-22 14:54:01 +0000761 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763EXPORT_SYMBOL(cancel_rearming_delayed_work);
764
James Bottomley1fa44ec2006-02-23 12:43:43 -0600765/**
766 * execute_in_process_context - reliably execute the routine with user context
767 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600768 * @ew: guaranteed storage for the execute work structure (must
769 * be available when the work executes)
770 *
771 * Executes the function immediately if process context is available,
772 * otherwise schedules the function for delayed execution.
773 *
774 * Returns: 0 - function was executed
775 * 1 - function was scheduled for execution
776 */
David Howells65f27f32006-11-22 14:55:48 +0000777int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600778{
779 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000780 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600781 return 0;
782 }
783
David Howells65f27f32006-11-22 14:55:48 +0000784 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600785 schedule_work(&ew->work);
786
787 return 1;
788}
789EXPORT_SYMBOL_GPL(execute_in_process_context);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791int keventd_up(void)
792{
793 return keventd_wq != NULL;
794}
795
796int current_is_keventd(void)
797{
798 struct cpu_workqueue_struct *cwq;
799 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
800 int ret = 0;
801
802 BUG_ON(!keventd_wq);
803
Christoph Lameter89ada672005-10-30 15:01:59 -0800804 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (current == cwq->thread)
806 ret = 1;
807
808 return ret;
809
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/* Take the work from this (downed) CPU. */
813static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
814{
Christoph Lameter89ada672005-10-30 15:01:59 -0800815 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700816 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct work_struct *work;
818
819 spin_lock_irq(&cwq->lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700820 list_replace_init(&cwq->worklist, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 while (!list_empty(&list)) {
823 printk("Taking work for %s\n", wq->name);
824 work = list_entry(list.next,struct work_struct,entry);
825 list_del(&work->entry);
Christoph Lameter89ada672005-10-30 15:01:59 -0800826 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 spin_unlock_irq(&cwq->lock);
829}
830
831/* We're holding the cpucontrol mutex here */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700832static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 unsigned long action,
834 void *hcpu)
835{
836 unsigned int hotcpu = (unsigned long)hcpu;
837 struct workqueue_struct *wq;
838
839 switch (action) {
840 case CPU_UP_PREPARE:
Andrew Morton9b41ea72006-08-13 23:24:26 -0700841 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Create a new workqueue thread for it. */
843 list_for_each_entry(wq, &workqueues, list) {
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800844 if (!create_workqueue_thread(wq, hotcpu, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 printk("workqueue for %i failed\n", hotcpu);
846 return NOTIFY_BAD;
847 }
848 }
849 break;
850
851 case CPU_ONLINE:
852 /* Kick off worker threads. */
853 list_for_each_entry(wq, &workqueues, list) {
Christoph Lameter89ada672005-10-30 15:01:59 -0800854 struct cpu_workqueue_struct *cwq;
855
856 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
857 kthread_bind(cwq->thread, hotcpu);
858 wake_up_process(cwq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700860 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862
863 case CPU_UP_CANCELED:
864 list_for_each_entry(wq, &workqueues, list) {
Heiko Carstensfc75cdf2006-06-25 05:49:10 -0700865 if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
866 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Unbind so it can run. */
Christoph Lameter89ada672005-10-30 15:01:59 -0800868 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
Heiko Carstensa4c4af72005-11-07 00:58:38 -0800869 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 cleanup_workqueue_thread(wq, hotcpu);
871 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700872 mutex_unlock(&workqueue_mutex);
873 break;
874
875 case CPU_DOWN_PREPARE:
876 mutex_lock(&workqueue_mutex);
877 break;
878
879 case CPU_DOWN_FAILED:
880 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882
883 case CPU_DEAD:
884 list_for_each_entry(wq, &workqueues, list)
885 cleanup_workqueue_thread(wq, hotcpu);
886 list_for_each_entry(wq, &workqueues, list)
887 take_over_work(wq, hotcpu);
Andrew Morton9b41ea72006-08-13 23:24:26 -0700888 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890 }
891
892 return NOTIFY_OK;
893}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895void init_workqueues(void)
896{
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800897 singlethread_cpu = first_cpu(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 hotcpu_notifier(workqueue_cpu_callback, 0);
899 keventd_wq = create_workqueue("events");
900 BUG_ON(!keventd_wq);
901}
902