blob: 967479756511a9c15e10e8fa3fb27c346982da7f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080034 * The per-CPU workqueue (if single thread, we always use the first
35 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
37 * The sequence counters are for flush_scheduled_work(). It wants to wait
Rolf Eike Beer9f5d7852006-10-03 23:07:31 +020038 * until all currently-scheduled works are completed, but it doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * 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;
Ingo Molnar36c8b582006-07-03 00:25:41 -070055 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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 {
Christoph Lameter89ada672005-10-30 15:01:59 -080065 struct cpu_workqueue_struct *cpu_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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. */
Andrew Morton9b41ea72006-08-13 23:24:26 -070072static DEFINE_MUTEX(workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static LIST_HEAD(workqueues);
74
Nathan Lynchf756d5e2006-01-08 01:05:12 -080075static int singlethread_cpu;
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* 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
David Howells365970a2006-11-22 14:54:49 +000083static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* 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);
David Howells365970a2006-11-22 14:54:49 +0000113 set_wq_data(work, cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 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
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700120/**
121 * queue_work - queue work on a workqueue
122 * @wq: workqueue to use
123 * @work: work to queue
124 *
Alan Stern057647f2006-10-28 10:38:58 -0700125 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
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
David Howells365970a2006-11-22 14:54:49 +0000134 if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800136 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 BUG_ON(!list_empty(&work->entry));
Christoph Lameter89ada672005-10-30 15:01:59 -0800138 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 ret = 1;
140 }
141 put_cpu();
142 return ret;
143}
Dave Jonesae90dd52006-06-30 01:40:45 -0400144EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146static void delayed_work_timer_fn(unsigned long __data)
147{
David Howells52bad642006-11-22 14:54:01 +0000148 struct delayed_work *dwork = (struct delayed_work *)__data;
David Howells365970a2006-11-22 14:54:49 +0000149 struct workqueue_struct *wq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int cpu = smp_processor_id();
151
152 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800153 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
David Howells52bad642006-11-22 14:54:01 +0000155 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700158/**
159 * queue_delayed_work - queue work on a workqueue after delay
160 * @wq: workqueue to use
David Howells52bad642006-11-22 14:54:01 +0000161 * @work: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700162 * @delay: number of jiffies to wait before queueing
163 *
Alan Stern057647f2006-10-28 10:38:58 -0700164 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166int fastcall queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000167 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000170 struct timer_list *timer = &dwork->timer;
171 struct work_struct *work = &dwork->work;
172
173 if (delay == 0)
174 return queue_work(wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Howells365970a2006-11-22 14:54:49 +0000176 if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 BUG_ON(timer_pending(timer));
178 BUG_ON(!list_empty(&work->entry));
179
180 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000181 set_wq_data(work, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000183 timer->data = (unsigned long)dwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 timer->function = delayed_work_timer_fn;
185 add_timer(timer);
186 ret = 1;
187 }
188 return ret;
189}
Dave Jonesae90dd52006-06-30 01:40:45 -0400190EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700192/**
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 *
Alan Stern057647f2006-10-28 10:38:58 -0700199 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700200 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700201int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000202 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700203{
204 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000205 struct timer_list *timer = &dwork->timer;
206 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700207
David Howells365970a2006-11-22 14:54:49 +0000208 if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700209 BUG_ON(timer_pending(timer));
210 BUG_ON(!list_empty(&work->entry));
211
212 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000213 set_wq_data(work, wq);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700214 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000215 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700216 timer->function = delayed_work_timer_fn;
217 add_timer_on(timer, cpu);
218 ret = 1;
219 }
220 return ret;
221}
Dave Jonesae90dd52006-06-30 01:40:45 -0400222EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Arjan van de Ven858119e2006-01-14 13:20:43 -0800224static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
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);
David Howells6bb49e52006-11-22 14:54:45 +0000243 work_func_t f = work->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 void *data = work->data;
245
246 list_del_init(cwq->worklist.next);
247 spin_unlock_irqrestore(&cwq->lock, flags);
248
David Howells365970a2006-11-22 14:54:49 +0000249 BUG_ON(get_wq_data(work) != cwq);
250 clear_bit(WORK_STRUCT_PENDING, &work->management);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 f(data);
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
Christoph Lameter46934022006-10-11 01:21:26 -0700277 /*
278 * We inherited MPOL_INTERLEAVE from the booting kernel.
279 * Set MPOL_DEFAULT to insure node local allocations.
280 */
281 numa_default_policy();
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* 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
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700333/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700335 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
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)) {
Ben Collinsbce61dd2005-11-28 13:43:56 -0800353 /* Always use first cpu's area. */
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800354 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else {
356 int cpu;
357
Andrew Morton9b41ea72006-08-13 23:24:26 -0700358 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 for_each_online_cpu(cpu)
Christoph Lameter89ada672005-10-30 15:01:59 -0800360 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Andrew Morton9b41ea72006-08-13 23:24:26 -0700361 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363}
Dave Jonesae90dd52006-06-30 01:40:45 -0400364EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
367 int cpu)
368{
Christoph Lameter89ada672005-10-30 15:01:59 -0800369 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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
Pekka J Enbergdd392712005-09-06 15:18:31 -0700398 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!wq)
400 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Christoph Lameter89ada672005-10-30 15:01:59 -0800402 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
Ben Collins676121f2006-01-08 01:03:04 -0800403 if (!wq->cpu_wq) {
404 kfree(wq);
405 return NULL;
406 }
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 wq->name = name;
Andrew Morton9b41ea72006-08-13 23:24:26 -0700409 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (singlethread) {
411 INIT_LIST_HEAD(&wq->list);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800412 p = create_workqueue_thread(wq, singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!p)
414 destroy = 1;
415 else
416 wake_up_process(p);
417 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 list_add(&wq->list, &workqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 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 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700428 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
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}
Dave Jonesae90dd52006-06-30 01:40:45 -0400439EXPORT_SYMBOL_GPL(__create_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
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
Christoph Lameter89ada672005-10-30 15:01:59 -0800447 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 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
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700456/**
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462void 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. */
Andrew Morton9b41ea72006-08-13 23:24:26 -0700469 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (is_single_threaded(wq))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800471 cleanup_workqueue_thread(wq, singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 else {
473 for_each_online_cpu(cpu)
474 cleanup_workqueue_thread(wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 list_del(&wq->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700477 mutex_unlock(&workqueue_mutex);
Christoph Lameter89ada672005-10-30 15:01:59 -0800478 free_percpu(wq->cpu_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 kfree(wq);
480}
Dave Jonesae90dd52006-06-30 01:40:45 -0400481EXPORT_SYMBOL_GPL(destroy_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483static struct workqueue_struct *keventd_wq;
484
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700485/**
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491int fastcall schedule_work(struct work_struct *work)
492{
493 return queue_work(keventd_wq, work);
494}
Dave Jonesae90dd52006-06-30 01:40:45 -0400495EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700497/**
498 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000499 * @dwork: job to be done
500 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700501 *
502 * After waiting for a given time this puts a job in the kernel-global
503 * workqueue.
504 */
David Howells52bad642006-11-22 14:54:01 +0000505int fastcall schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
David Howells52bad642006-11-22 14:54:01 +0000507 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
Dave Jonesae90dd52006-06-30 01:40:45 -0400509EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700511/**
512 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
513 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000514 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700515 * @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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000521 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
David Howells52bad642006-11-22 14:54:01 +0000523 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
Dave Jonesae90dd52006-06-30 01:40:45 -0400525EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Andrew Mortonb6136772006-06-25 05:47:49 -0700527/**
528 * schedule_on_each_cpu - call a function on each online CPU from keventd
529 * @func: the function to call
530 * @info: a pointer to pass to func()
531 *
532 * Returns zero on success.
533 * Returns -ve errno on failure.
534 *
535 * Appears to be racy against CPU hotplug.
536 *
537 * schedule_on_each_cpu() is very slow.
538 */
David Howells6bb49e52006-11-22 14:54:45 +0000539int schedule_on_each_cpu(work_func_t func, void *info)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800540{
541 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700542 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800543
Andrew Mortonb6136772006-06-25 05:47:49 -0700544 works = alloc_percpu(struct work_struct);
545 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800546 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700547
Andrew Morton9b41ea72006-08-13 23:24:26 -0700548 mutex_lock(&workqueue_mutex);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800549 for_each_online_cpu(cpu) {
Andrew Mortonb6136772006-06-25 05:47:49 -0700550 INIT_WORK(per_cpu_ptr(works, cpu), func, info);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800551 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu),
Andrew Mortonb6136772006-06-25 05:47:49 -0700552 per_cpu_ptr(works, cpu));
Christoph Lameter15316ba2006-01-08 01:00:43 -0800553 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700554 mutex_unlock(&workqueue_mutex);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800555 flush_workqueue(keventd_wq);
Andrew Mortonb6136772006-06-25 05:47:49 -0700556 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800557 return 0;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560void flush_scheduled_work(void)
561{
562 flush_workqueue(keventd_wq);
563}
Dave Jonesae90dd52006-06-30 01:40:45 -0400564EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/**
567 * cancel_rearming_delayed_workqueue - reliably kill off a delayed
568 * work whose handler rearms the delayed work.
569 * @wq: the controlling workqueue structure
David Howells52bad642006-11-22 14:54:01 +0000570 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
James Bottomley81ddef72005-04-16 15:23:59 -0700572void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000573 struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
David Howells52bad642006-11-22 14:54:01 +0000575 while (!cancel_delayed_work(dwork))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 flush_workqueue(wq);
577}
James Bottomley81ddef72005-04-16 15:23:59 -0700578EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/**
581 * cancel_rearming_delayed_work - reliably kill off a delayed keventd
582 * work whose handler rearms the delayed work.
David Howells52bad642006-11-22 14:54:01 +0000583 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
David Howells52bad642006-11-22 14:54:01 +0000585void cancel_rearming_delayed_work(struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
David Howells52bad642006-11-22 14:54:01 +0000587 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589EXPORT_SYMBOL(cancel_rearming_delayed_work);
590
James Bottomley1fa44ec2006-02-23 12:43:43 -0600591/**
592 * execute_in_process_context - reliably execute the routine with user context
593 * @fn: the function to execute
594 * @data: data to pass to the function
595 * @ew: guaranteed storage for the execute work structure (must
596 * be available when the work executes)
597 *
598 * Executes the function immediately if process context is available,
599 * otherwise schedules the function for delayed execution.
600 *
601 * Returns: 0 - function was executed
602 * 1 - function was scheduled for execution
603 */
David Howells6bb49e52006-11-22 14:54:45 +0000604int execute_in_process_context(work_func_t fn, void *data,
James Bottomley1fa44ec2006-02-23 12:43:43 -0600605 struct execute_work *ew)
606{
607 if (!in_interrupt()) {
608 fn(data);
609 return 0;
610 }
611
612 INIT_WORK(&ew->work, fn, data);
613 schedule_work(&ew->work);
614
615 return 1;
616}
617EXPORT_SYMBOL_GPL(execute_in_process_context);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619int keventd_up(void)
620{
621 return keventd_wq != NULL;
622}
623
624int current_is_keventd(void)
625{
626 struct cpu_workqueue_struct *cwq;
627 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
628 int ret = 0;
629
630 BUG_ON(!keventd_wq);
631
Christoph Lameter89ada672005-10-30 15:01:59 -0800632 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (current == cwq->thread)
634 ret = 1;
635
636 return ret;
637
638}
639
640#ifdef CONFIG_HOTPLUG_CPU
641/* Take the work from this (downed) CPU. */
642static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
643{
Christoph Lameter89ada672005-10-30 15:01:59 -0800644 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700645 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct work_struct *work;
647
648 spin_lock_irq(&cwq->lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700649 list_replace_init(&cwq->worklist, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 while (!list_empty(&list)) {
652 printk("Taking work for %s\n", wq->name);
653 work = list_entry(list.next,struct work_struct,entry);
654 list_del(&work->entry);
Christoph Lameter89ada672005-10-30 15:01:59 -0800655 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 spin_unlock_irq(&cwq->lock);
658}
659
660/* We're holding the cpucontrol mutex here */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700661static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 unsigned long action,
663 void *hcpu)
664{
665 unsigned int hotcpu = (unsigned long)hcpu;
666 struct workqueue_struct *wq;
667
668 switch (action) {
669 case CPU_UP_PREPARE:
Andrew Morton9b41ea72006-08-13 23:24:26 -0700670 mutex_lock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Create a new workqueue thread for it. */
672 list_for_each_entry(wq, &workqueues, list) {
Mika Kukkonen230649d2005-09-06 15:17:17 -0700673 if (!create_workqueue_thread(wq, hotcpu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 printk("workqueue for %i failed\n", hotcpu);
675 return NOTIFY_BAD;
676 }
677 }
678 break;
679
680 case CPU_ONLINE:
681 /* Kick off worker threads. */
682 list_for_each_entry(wq, &workqueues, list) {
Christoph Lameter89ada672005-10-30 15:01:59 -0800683 struct cpu_workqueue_struct *cwq;
684
685 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
686 kthread_bind(cwq->thread, hotcpu);
687 wake_up_process(cwq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700689 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691
692 case CPU_UP_CANCELED:
693 list_for_each_entry(wq, &workqueues, list) {
Heiko Carstensfc75cdf2006-06-25 05:49:10 -0700694 if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
695 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Unbind so it can run. */
Christoph Lameter89ada672005-10-30 15:01:59 -0800697 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
Heiko Carstensa4c4af72005-11-07 00:58:38 -0800698 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 cleanup_workqueue_thread(wq, hotcpu);
700 }
Andrew Morton9b41ea72006-08-13 23:24:26 -0700701 mutex_unlock(&workqueue_mutex);
702 break;
703
704 case CPU_DOWN_PREPARE:
705 mutex_lock(&workqueue_mutex);
706 break;
707
708 case CPU_DOWN_FAILED:
709 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711
712 case CPU_DEAD:
713 list_for_each_entry(wq, &workqueues, list)
714 cleanup_workqueue_thread(wq, hotcpu);
715 list_for_each_entry(wq, &workqueues, list)
716 take_over_work(wq, hotcpu);
Andrew Morton9b41ea72006-08-13 23:24:26 -0700717 mutex_unlock(&workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719 }
720
721 return NOTIFY_OK;
722}
723#endif
724
725void init_workqueues(void)
726{
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800727 singlethread_cpu = first_cpu(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 hotcpu_notifier(workqueue_cpu_callback, 0);
729 keventd_wq = create_workqueue("events");
730 BUG_ON(!keventd_wq);
731}
732