blob: ea422254f8bf3a9eb463250570605e27fe362220 [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 Lameter469340232006-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;
Oleg Nesterov3af244332007-05-09 02:34:09 -070046 struct work_struct *current_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 struct workqueue_struct *wq;
Ingo Molnar36c8b582006-07-03 00:25:41 -070049 struct task_struct *thread;
Oleg Nesterov3af244332007-05-09 02:34:09 -070050 int should_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
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 {
Christoph Lameter89ada672005-10-30 15:01:59 -080060 struct cpu_workqueue_struct *cpu_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 const char *name;
62 struct list_head list; /* Empty if single thread */
Oleg Nesterov319c2a92007-05-09 02:34:06 -070063 int freezeable; /* Freeze threads during suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
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
Oleg Nesterov3af244332007-05-09 02:34:09 -070071static int singlethread_cpu __read_mostly;
72/* optimization, we could use cpu_possible_map */
73static cpumask_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* 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
David Howells4594bf12006-12-07 11:33:26 +000081/*
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 */
David Howells365970a2006-11-22 14:54:49 +000085static inline void set_wq_data(struct work_struct *work, void *wq)
86{
David Howells4594bf12006-12-07 11:33:26 +000087 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +000088
David Howells4594bf12006-12-07 11:33:26 +000089 BUG_ON(!work_pending(work));
90
David Howells365970a2006-11-22 14:54:49 +000091 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -080092 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
93 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +000094}
95
96static inline void *get_wq_data(struct work_struct *work)
97{
Linus Torvaldsa08727b2006-12-16 09:53:50 -080098 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +000099}
100
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700101static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* 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);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700119 insert_work(cwq, work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_unlock_irqrestore(&cwq->lock, flags);
121}
122
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700123/**
124 * queue_work - queue work on a workqueue
125 * @wq: workqueue to use
126 * @work: work to queue
127 *
Alan Stern057647f2006-10-28 10:38:58 -0700128 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
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
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800137 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800139 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 BUG_ON(!list_empty(&work->entry));
Christoph Lameter89ada672005-10-30 15:01:59 -0800141 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ret = 1;
143 }
144 put_cpu();
145 return ret;
146}
Dave Jonesae90dd52006-06-30 01:40:45 -0400147EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800149void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
David Howells52bad642006-11-22 14:54:01 +0000151 struct delayed_work *dwork = (struct delayed_work *)__data;
David Howells365970a2006-11-22 14:54:49 +0000152 struct workqueue_struct *wq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int cpu = smp_processor_id();
154
155 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800156 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David Howells52bad642006-11-22 14:54:01 +0000158 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700161/**
162 * queue_delayed_work - queue work on a workqueue after delay
163 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800164 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700165 * @delay: number of jiffies to wait before queueing
166 *
Alan Stern057647f2006-10-28 10:38:58 -0700167 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169int fastcall queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000170 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000173 struct timer_list *timer = &dwork->timer;
174 struct work_struct *work = &dwork->work;
175
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800176 timer_stats_timer_set_start_info(timer);
David Howells52bad642006-11-22 14:54:01 +0000177 if (delay == 0)
178 return queue_work(wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800180 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 BUG_ON(timer_pending(timer));
182 BUG_ON(!list_empty(&work->entry));
183
184 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000185 set_wq_data(work, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000187 timer->data = (unsigned long)dwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 timer->function = delayed_work_timer_fn;
189 add_timer(timer);
190 ret = 1;
191 }
192 return ret;
193}
Dave Jonesae90dd52006-06-30 01:40:45 -0400194EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700196/**
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
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800200 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700201 * @delay: number of jiffies to wait before queueing
202 *
Alan Stern057647f2006-10-28 10:38:58 -0700203 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700204 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700205int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000206 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700207{
208 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000209 struct timer_list *timer = &dwork->timer;
210 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700211
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800212 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700213 BUG_ON(timer_pending(timer));
214 BUG_ON(!list_empty(&work->entry));
215
216 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000217 set_wq_data(work, wq);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700218 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000219 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700220 timer->function = delayed_work_timer_fn;
221 add_timer_on(timer, cpu);
222 ret = 1;
223 }
224 return ret;
225}
Dave Jonesae90dd52006-06-30 01:40:45 -0400226EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Arjan van de Ven858119e2006-01-14 13:20:43 -0800228static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
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);
David Howells6bb49e52006-11-22 14:54:45 +0000247 work_func_t f = work->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700249 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 list_del_init(cwq->worklist.next);
251 spin_unlock_irqrestore(&cwq->lock, flags);
252
David Howells365970a2006-11-22 14:54:49 +0000253 BUG_ON(get_wq_data(work) != cwq);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800254 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
David Howells65f27f32006-11-22 14:55:48 +0000255 work_release(work);
256 f(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800258 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700270 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 cwq->run_depth--;
273 spin_unlock_irqrestore(&cwq->lock, flags);
274}
275
Oleg Nesterov3af244332007-05-09 02:34:09 -0700276/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int worker_thread(void *__cwq)
295{
296 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700297 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct k_sigaction sa;
299 sigset_t blocked;
300
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700301 if (!cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800302 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
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
Christoph Lameter469340232006-10-11 01:21:26 -0700311 /*
312 * We inherited MPOL_INTERLEAVE from the booting kernel.
313 * Set MPOL_DEFAULT to insure node local allocations.
314 */
315 numa_default_policy();
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* 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
Oleg Nesterov3af244332007-05-09 02:34:09 -0700323 for (;;) {
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700324 if (cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800325 try_to_freeze();
326
Oleg Nesterov3af244332007-05-09 02:34:09 -0700327 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
328 if (!cwq->should_stop && list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700330 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Oleg Nesterov3af244332007-05-09 02:34:09 -0700332 if (cwq_should_stop(cwq))
333 break;
334
335 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339}
340
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700341struct 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
Oleg Nesterov83c22522007-05-09 02:33:54 -0700352static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
353 struct wq_barrier *barr, int tail)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700354{
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);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700359
360 insert_work(cwq, &barr->work, tail);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static 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 {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700372 struct wq_barrier barr;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700373 int active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Oleg Nesterov83c22522007-05-09 02:33:54 -0700375 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Oleg Nesterovd7213042007-05-09 02:34:07 -0700382 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700383 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385}
386
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700387/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700389 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *
391 * Forces execution of the workqueue and blocks until its completion.
392 * This is typically used in driver shutdown handlers.
393 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700394 * We sleep until all works which were queued on entry have been handled,
395 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
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{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700402 if (is_single_threaded(wq))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800403 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
Oleg Nesterov3af244332007-05-09 02:34:09 -0700404 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int cpu;
406
Oleg Nesterov3af244332007-05-09 02:34:09 -0700407 for_each_cpu_mask(cpu, cpu_populated_map)
Christoph Lameter89ada672005-10-30 15:01:59 -0800408 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410}
Dave Jonesae90dd52006-06-30 01:40:45 -0400411EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700413static 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)) {
Oleg Nesterov83c22522007-05-09 02:33:54 -0700421 insert_wq_barrier(cwq, &barr, 0);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700422 running = 1;
423 }
424 spin_unlock_irq(&cwq->lock);
425
Oleg Nesterov3af244332007-05-09 02:34:09 -0700426 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700427 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700428}
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
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700448 cwq = get_wq_data(work);
449 /* Was it ever queued ? */
450 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700451 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700452
453 /*
Oleg Nesterov3af244332007-05-09 02:34:09 -0700454 * 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.
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700456 */
457 spin_lock_irq(&cwq->lock);
458 list_del_init(&work->entry);
459 work_release(work);
460 spin_unlock_irq(&cwq->lock);
461
Oleg Nesterov3af244332007-05-09 02:34:09 -0700462 if (is_single_threaded(wq))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700463 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700464 else {
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700465 int cpu;
466
Oleg Nesterov3af244332007-05-09 02:34:09 -0700467 for_each_cpu_mask(cpu, cpu_populated_map)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700468 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
469 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700470}
471EXPORT_SYMBOL_GPL(flush_work);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474static struct workqueue_struct *keventd_wq;
475
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700476/**
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482int fastcall schedule_work(struct work_struct *work)
483{
484 return queue_work(keventd_wq, work);
485}
Dave Jonesae90dd52006-06-30 01:40:45 -0400486EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700488/**
489 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000490 * @dwork: job to be done
491 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700492 *
493 * After waiting for a given time this puts a job in the kernel-global
494 * workqueue.
495 */
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800496int fastcall schedule_delayed_work(struct delayed_work *dwork,
497 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800499 timer_stats_timer_set_start_info(&dwork->timer);
David Howells52bad642006-11-22 14:54:01 +0000500 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Dave Jonesae90dd52006-06-30 01:40:45 -0400502EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700504/**
505 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
506 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000507 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700508 * @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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000514 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
David Howells52bad642006-11-22 14:54:01 +0000516 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
Dave Jonesae90dd52006-06-30 01:40:45 -0400518EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Andrew Mortonb6136772006-06-25 05:47:49 -0700520/**
521 * schedule_on_each_cpu - call a function on each online CPU from keventd
522 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700523 *
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 */
David Howells65f27f32006-11-22 14:55:48 +0000531int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba82006-01-08 01:00:43 -0800532{
533 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700534 struct work_struct *works;
Christoph Lameter15316ba82006-01-08 01:00:43 -0800535
Andrew Mortonb6136772006-06-25 05:47:49 -0700536 works = alloc_percpu(struct work_struct);
537 if (!works)
Christoph Lameter15316ba82006-01-08 01:00:43 -0800538 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700539
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700540 preempt_disable(); /* CPU hotplug */
Christoph Lameter15316ba82006-01-08 01:00:43 -0800541 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100542 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);
Christoph Lameter15316ba82006-01-08 01:00:43 -0800547 }
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700548 preempt_enable();
Christoph Lameter15316ba82006-01-08 01:00:43 -0800549 flush_workqueue(keventd_wq);
Andrew Mortonb6136772006-06-25 05:47:49 -0700550 free_percpu(works);
Christoph Lameter15316ba82006-01-08 01:00:43 -0800551 return 0;
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554void flush_scheduled_work(void)
555{
556 flush_workqueue(keventd_wq);
557}
Dave Jonesae90dd52006-06-30 01:40:45 -0400558EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700560void flush_work_keventd(struct work_struct *work)
561{
562 flush_work(keventd_wq, work);
563}
564EXPORT_SYMBOL(flush_work_keventd);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800567 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 * @wq: the controlling workqueue structure
David Howells52bad642006-11-22 14:54:01 +0000569 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
James Bottomley81ddef72005-04-16 15:23:59 -0700571void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000572 struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
David Howells52bad642006-11-22 14:54:01 +0000574 while (!cancel_delayed_work(dwork))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 flush_workqueue(wq);
576}
James Bottomley81ddef72005-04-16 15:23:59 -0700577EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800580 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
David Howells52bad642006-11-22 14:54:01 +0000581 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
David Howells52bad642006-11-22 14:54:01 +0000583void cancel_rearming_delayed_work(struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
David Howells52bad642006-11-22 14:54:01 +0000585 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587EXPORT_SYMBOL(cancel_rearming_delayed_work);
588
James Bottomley1fa44ec2006-02-23 12:43:43 -0600589/**
590 * execute_in_process_context - reliably execute the routine with user context
591 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600592 * @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 */
David Howells65f27f32006-11-22 14:55:48 +0000601int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600602{
603 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000604 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600605 return 0;
606 }
607
David Howells65f27f32006-11-22 14:55:48 +0000608 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600609 schedule_work(&ew->work);
610
611 return 1;
612}
613EXPORT_SYMBOL_GPL(execute_in_process_context);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615int 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
Christoph Lameter89ada672005-10-30 15:01:59 -0800628 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (current == cwq->thread)
630 ret = 1;
631
632 return ret;
633
634}
635
Oleg Nesterov3af244332007-05-09 02:34:09 -0700636static struct cpu_workqueue_struct *
637init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Christoph Lameter89ada672005-10-30 15:01:59 -0800639 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Oleg Nesterov3af244332007-05-09 02:34:09 -0700641 cwq->wq = wq;
642 spin_lock_init(&cwq->lock);
643 INIT_LIST_HEAD(&cwq->worklist);
644 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Oleg Nesterov3af244332007-05-09 02:34:09 -0700646 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Oleg Nesterov3af244332007-05-09 02:34:09 -0700649static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700651 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)
680{
681 struct workqueue_struct *wq;
682 struct cpu_workqueue_struct *cwq;
683 int err = 0, cpu;
684
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 {
703 mutex_lock(&workqueue_mutex);
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);
711 }
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);
722
723static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
724{
725 struct wq_barrier barr;
726 int alive = 0;
727
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);
768 mutex_unlock(&workqueue_mutex);
769
770 for_each_cpu_mask(cpu, cpu_populated_map) {
771 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
772 cleanup_workqueue_thread(cwq, cpu);
773 }
774 }
775
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct workqueue_struct *wq;
788
789 switch (action) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700790 case CPU_LOCK_ACQUIRE:
791 mutex_lock(&workqueue_mutex);
792 return NOTIFY_OK;
793
794 case CPU_LOCK_RELEASE:
795 mutex_unlock(&workqueue_mutex);
796 return NOTIFY_OK;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 case CPU_UP_PREPARE:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700799 cpu_set(cpu, cpu_populated_map);
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Oleg Nesterov3af244332007-05-09 02:34:09 -0700802 list_for_each_entry(wq, &workqueues, list) {
803 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800804
Oleg Nesterov3af244332007-05-09 02:34:09 -0700805 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:
Christoph Lameter89ada672005-10-30 15:01:59 -0800813 wake_up_process(cwq->thread);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700814 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 return NOTIFY_OK;
826}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828void init_workqueues(void)
829{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700830 cpu_populated_map = cpu_online_map;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800831 singlethread_cpu = first_cpu(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 hotcpu_notifier(workqueue_cpu_callback, 0);
833 keventd_wq = create_workqueue("events");
834 BUG_ON(!keventd_wq);
835}