blob: a981add58fb9bca7c870d3a94c0034a9bed58789 [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;
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
Linus Torvalds68380b52006-12-07 09:28:19 -0800101static int __run_work(struct cpu_workqueue_struct *cwq, struct work_struct *work)
102{
103 int ret = 0;
104 unsigned long flags;
105
106 spin_lock_irqsave(&cwq->lock, flags);
107 /*
108 * We need to re-validate the work info after we've gotten
109 * the cpu_workqueue lock. We can run the work now iff:
110 *
111 * - the wq_data still matches the cpu_workqueue_struct
112 * - AND the work is still marked pending
113 * - AND the work is still on a list (which will be this
114 * workqueue_struct list)
115 *
116 * All these conditions are important, because we
117 * need to protect against the work being run right
118 * now on another CPU (all but the last one might be
119 * true if it's currently running and has not been
120 * released yet, for example).
121 */
122 if (get_wq_data(work) == cwq
123 && work_pending(work)
124 && !list_empty(&work->entry)) {
125 work_func_t f = work->func;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700126 cwq->current_work = work;
Linus Torvalds68380b52006-12-07 09:28:19 -0800127 list_del_init(&work->entry);
128 spin_unlock_irqrestore(&cwq->lock, flags);
129
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800130 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
Linus Torvalds68380b52006-12-07 09:28:19 -0800131 work_release(work);
132 f(work);
133
134 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700135 cwq->current_work = NULL;
Linus Torvalds68380b52006-12-07 09:28:19 -0800136 ret = 1;
137 }
138 spin_unlock_irqrestore(&cwq->lock, flags);
139 return ret;
140}
141
142/**
143 * run_scheduled_work - run scheduled work synchronously
144 * @work: work to run
145 *
146 * This checks if the work was pending, and runs it
147 * synchronously if so. It returns a boolean to indicate
148 * whether it had any scheduled work to run or not.
149 *
150 * NOTE! This _only_ works for normal work_structs. You
151 * CANNOT use this for delayed work, because the wq data
152 * for delayed work will not point properly to the per-
153 * CPU workqueue struct, but will change!
154 */
155int fastcall run_scheduled_work(struct work_struct *work)
156{
157 for (;;) {
158 struct cpu_workqueue_struct *cwq;
159
160 if (!work_pending(work))
161 return 0;
162 if (list_empty(&work->entry))
163 return 0;
164 /* NOTE! This depends intimately on __queue_work! */
165 cwq = get_wq_data(work);
166 if (!cwq)
167 return 0;
168 if (__run_work(cwq, work))
169 return 1;
170 }
171}
172EXPORT_SYMBOL(run_scheduled_work);
173
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700174static void insert_work(struct cpu_workqueue_struct *cwq,
175 struct work_struct *work, int tail)
176{
177 set_wq_data(work, cwq);
178 if (tail)
179 list_add_tail(&work->entry, &cwq->worklist);
180 else
181 list_add(&work->entry, &cwq->worklist);
182 wake_up(&cwq->more_work);
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Preempt must be disabled. */
186static void __queue_work(struct cpu_workqueue_struct *cwq,
187 struct work_struct *work)
188{
189 unsigned long flags;
190
191 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700192 insert_work(cwq, work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 spin_unlock_irqrestore(&cwq->lock, flags);
194}
195
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700196/**
197 * queue_work - queue work on a workqueue
198 * @wq: workqueue to use
199 * @work: work to queue
200 *
Alan Stern057647f2006-10-28 10:38:58 -0700201 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
203 * We queue the work to the CPU it was submitted, but there is no
204 * guarantee that it will be processed by that CPU.
205 */
206int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
207{
208 int ret = 0, cpu = get_cpu();
209
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800210 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800212 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 BUG_ON(!list_empty(&work->entry));
Christoph Lameter89ada672005-10-30 15:01:59 -0800214 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ret = 1;
216 }
217 put_cpu();
218 return ret;
219}
Dave Jonesae90dd52006-06-30 01:40:45 -0400220EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800222void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
David Howells52bad642006-11-22 14:54:01 +0000224 struct delayed_work *dwork = (struct delayed_work *)__data;
David Howells365970a2006-11-22 14:54:49 +0000225 struct workqueue_struct *wq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int cpu = smp_processor_id();
227
228 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800229 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
David Howells52bad642006-11-22 14:54:01 +0000231 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700234/**
235 * queue_delayed_work - queue work on a workqueue after delay
236 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800237 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700238 * @delay: number of jiffies to wait before queueing
239 *
Alan Stern057647f2006-10-28 10:38:58 -0700240 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242int fastcall queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000243 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000246 struct timer_list *timer = &dwork->timer;
247 struct work_struct *work = &dwork->work;
248
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800249 timer_stats_timer_set_start_info(timer);
David Howells52bad642006-11-22 14:54:01 +0000250 if (delay == 0)
251 return queue_work(wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800253 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 BUG_ON(timer_pending(timer));
255 BUG_ON(!list_empty(&work->entry));
256
257 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000258 set_wq_data(work, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000260 timer->data = (unsigned long)dwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 timer->function = delayed_work_timer_fn;
262 add_timer(timer);
263 ret = 1;
264 }
265 return ret;
266}
Dave Jonesae90dd52006-06-30 01:40:45 -0400267EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700269/**
270 * queue_delayed_work_on - queue work on specific CPU after delay
271 * @cpu: CPU number to execute work on
272 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800273 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700274 * @delay: number of jiffies to wait before queueing
275 *
Alan Stern057647f2006-10-28 10:38:58 -0700276 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700277 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700278int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000279 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700280{
281 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000282 struct timer_list *timer = &dwork->timer;
283 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700284
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800285 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700286 BUG_ON(timer_pending(timer));
287 BUG_ON(!list_empty(&work->entry));
288
289 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000290 set_wq_data(work, wq);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700291 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000292 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700293 timer->function = delayed_work_timer_fn;
294 add_timer_on(timer, cpu);
295 ret = 1;
296 }
297 return ret;
298}
Dave Jonesae90dd52006-06-30 01:40:45 -0400299EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Arjan van de Ven858119e2006-01-14 13:20:43 -0800301static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 unsigned long flags;
304
305 /*
306 * Keep taking off work from the queue until
307 * done.
308 */
309 spin_lock_irqsave(&cwq->lock, flags);
310 cwq->run_depth++;
311 if (cwq->run_depth > 3) {
312 /* morton gets to eat his hat */
313 printk("%s: recursion depth exceeded: %d\n",
314 __FUNCTION__, cwq->run_depth);
315 dump_stack();
316 }
317 while (!list_empty(&cwq->worklist)) {
318 struct work_struct *work = list_entry(cwq->worklist.next,
319 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000320 work_func_t f = work->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700322 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 list_del_init(cwq->worklist.next);
324 spin_unlock_irqrestore(&cwq->lock, flags);
325
David Howells365970a2006-11-22 14:54:49 +0000326 BUG_ON(get_wq_data(work) != cwq);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800327 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
David Howells65f27f32006-11-22 14:55:48 +0000328 work_release(work);
329 f(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800331 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
332 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
333 "%s/0x%08x/%d\n",
334 current->comm, preempt_count(),
335 current->pid);
336 printk(KERN_ERR " last function: ");
337 print_symbol("%s\n", (unsigned long)f);
338 debug_show_held_locks(current);
339 dump_stack();
340 }
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700343 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 cwq->run_depth--;
346 spin_unlock_irqrestore(&cwq->lock, flags);
347}
348
Oleg Nesterov3af244332007-05-09 02:34:09 -0700349/*
350 * NOTE: the caller must not touch *cwq if this func returns true
351 */
352static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
353{
354 int should_stop = cwq->should_stop;
355
356 if (unlikely(should_stop)) {
357 spin_lock_irq(&cwq->lock);
358 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
359 if (should_stop)
360 cwq->thread = NULL;
361 spin_unlock_irq(&cwq->lock);
362 }
363
364 return should_stop;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int worker_thread(void *__cwq)
368{
369 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700370 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct k_sigaction sa;
372 sigset_t blocked;
373
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700374 if (!cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800375 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 set_user_nice(current, -5);
378
379 /* Block and flush all signals */
380 sigfillset(&blocked);
381 sigprocmask(SIG_BLOCK, &blocked, NULL);
382 flush_signals(current);
383
Christoph Lameter46934022006-10-11 01:21:26 -0700384 /*
385 * We inherited MPOL_INTERLEAVE from the booting kernel.
386 * Set MPOL_DEFAULT to insure node local allocations.
387 */
388 numa_default_policy();
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
391 sa.sa.sa_handler = SIG_IGN;
392 sa.sa.sa_flags = 0;
393 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
394 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
395
Oleg Nesterov3af244332007-05-09 02:34:09 -0700396 for (;;) {
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700397 if (cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800398 try_to_freeze();
399
Oleg Nesterov3af244332007-05-09 02:34:09 -0700400 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
401 if (!cwq->should_stop && list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700403 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Oleg Nesterov3af244332007-05-09 02:34:09 -0700405 if (cwq_should_stop(cwq))
406 break;
407
408 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412}
413
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700414struct wq_barrier {
415 struct work_struct work;
416 struct completion done;
417};
418
419static void wq_barrier_func(struct work_struct *work)
420{
421 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
422 complete(&barr->done);
423}
424
Oleg Nesterov83c22522007-05-09 02:33:54 -0700425static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
426 struct wq_barrier *barr, int tail)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700427{
428 INIT_WORK(&barr->work, wq_barrier_func);
429 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
430
431 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700432
433 insert_work(cwq, &barr->work, tail);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
437{
438 if (cwq->thread == current) {
439 /*
440 * Probably keventd trying to flush its own queue. So simply run
441 * it by hand rather than deadlocking.
442 */
443 run_workqueue(cwq);
444 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700445 struct wq_barrier barr;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700446 int active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Oleg Nesterov83c22522007-05-09 02:33:54 -0700448 spin_lock_irq(&cwq->lock);
449 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
450 insert_wq_barrier(cwq, &barr, 1);
451 active = 1;
452 }
453 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Oleg Nesterovd7213042007-05-09 02:34:07 -0700455 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700456 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458}
459
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700462 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
464 * Forces execution of the workqueue and blocks until its completion.
465 * This is typically used in driver shutdown handlers.
466 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700467 * We sleep until all works which were queued on entry have been handled,
468 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 * This function used to run the workqueues itself. Now we just wait for the
471 * helper threads to do it.
472 */
473void fastcall flush_workqueue(struct workqueue_struct *wq)
474{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700475 if (is_single_threaded(wq))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800476 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
Oleg Nesterov3af244332007-05-09 02:34:09 -0700477 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int cpu;
479
Oleg Nesterov3af244332007-05-09 02:34:09 -0700480 for_each_cpu_mask(cpu, cpu_populated_map)
Christoph Lameter89ada672005-10-30 15:01:59 -0800481 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483}
Dave Jonesae90dd52006-06-30 01:40:45 -0400484EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700486static void wait_on_work(struct cpu_workqueue_struct *cwq,
487 struct work_struct *work)
488{
489 struct wq_barrier barr;
490 int running = 0;
491
492 spin_lock_irq(&cwq->lock);
493 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov83c22522007-05-09 02:33:54 -0700494 insert_wq_barrier(cwq, &barr, 0);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700495 running = 1;
496 }
497 spin_unlock_irq(&cwq->lock);
498
Oleg Nesterov3af244332007-05-09 02:34:09 -0700499 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700500 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700501}
502
503/**
504 * flush_work - block until a work_struct's callback has terminated
505 * @wq: the workqueue on which the work is queued
506 * @work: the work which is to be flushed
507 *
508 * flush_work() will attempt to cancel the work if it is queued. If the work's
509 * callback appears to be running, flush_work() will block until it has
510 * completed.
511 *
512 * flush_work() is designed to be used when the caller is tearing down data
513 * structures which the callback function operates upon. It is expected that,
514 * prior to calling flush_work(), the caller has arranged for the work to not
515 * be requeued.
516 */
517void flush_work(struct workqueue_struct *wq, struct work_struct *work)
518{
519 struct cpu_workqueue_struct *cwq;
520
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700521 cwq = get_wq_data(work);
522 /* Was it ever queued ? */
523 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700524 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700525
526 /*
Oleg Nesterov3af244332007-05-09 02:34:09 -0700527 * This work can't be re-queued, no need to re-check that
528 * get_wq_data() is still the same when we take cwq->lock.
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700529 */
530 spin_lock_irq(&cwq->lock);
531 list_del_init(&work->entry);
532 work_release(work);
533 spin_unlock_irq(&cwq->lock);
534
Oleg Nesterov3af244332007-05-09 02:34:09 -0700535 if (is_single_threaded(wq))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700536 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700537 else {
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700538 int cpu;
539
Oleg Nesterov3af244332007-05-09 02:34:09 -0700540 for_each_cpu_mask(cpu, cpu_populated_map)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700541 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
542 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700543}
544EXPORT_SYMBOL_GPL(flush_work);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547static struct workqueue_struct *keventd_wq;
548
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700549/**
550 * schedule_work - put work task in global workqueue
551 * @work: job to be done
552 *
553 * This puts a job in the kernel-global workqueue.
554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555int fastcall schedule_work(struct work_struct *work)
556{
557 return queue_work(keventd_wq, work);
558}
Dave Jonesae90dd52006-06-30 01:40:45 -0400559EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700561/**
562 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000563 * @dwork: job to be done
564 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700565 *
566 * After waiting for a given time this puts a job in the kernel-global
567 * workqueue.
568 */
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800569int fastcall schedule_delayed_work(struct delayed_work *dwork,
570 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800572 timer_stats_timer_set_start_info(&dwork->timer);
David Howells52bad642006-11-22 14:54:01 +0000573 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Dave Jonesae90dd52006-06-30 01:40:45 -0400575EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700577/**
578 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
579 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000580 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700581 * @delay: number of jiffies to wait
582 *
583 * After waiting for a given time this puts a job in the kernel-global
584 * workqueue on the specified CPU.
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000587 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
David Howells52bad642006-11-22 14:54:01 +0000589 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Dave Jonesae90dd52006-06-30 01:40:45 -0400591EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Andrew Mortonb6136772006-06-25 05:47:49 -0700593/**
594 * schedule_on_each_cpu - call a function on each online CPU from keventd
595 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700596 *
597 * Returns zero on success.
598 * Returns -ve errno on failure.
599 *
600 * Appears to be racy against CPU hotplug.
601 *
602 * schedule_on_each_cpu() is very slow.
603 */
David Howells65f27f32006-11-22 14:55:48 +0000604int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800605{
606 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700607 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800608
Andrew Mortonb6136772006-06-25 05:47:49 -0700609 works = alloc_percpu(struct work_struct);
610 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800611 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700612
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700613 preempt_disable(); /* CPU hotplug */
Christoph Lameter15316ba2006-01-08 01:00:43 -0800614 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100615 struct work_struct *work = per_cpu_ptr(works, cpu);
616
617 INIT_WORK(work, func);
618 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
619 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800620 }
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700621 preempt_enable();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800622 flush_workqueue(keventd_wq);
Andrew Mortonb6136772006-06-25 05:47:49 -0700623 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800624 return 0;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627void flush_scheduled_work(void)
628{
629 flush_workqueue(keventd_wq);
630}
Dave Jonesae90dd52006-06-30 01:40:45 -0400631EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700633void flush_work_keventd(struct work_struct *work)
634{
635 flush_work(keventd_wq, work);
636}
637EXPORT_SYMBOL(flush_work_keventd);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800640 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * @wq: the controlling workqueue structure
David Howells52bad642006-11-22 14:54:01 +0000642 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
James Bottomley81ddef72005-04-16 15:23:59 -0700644void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000645 struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
David Howells52bad642006-11-22 14:54:01 +0000647 while (!cancel_delayed_work(dwork))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 flush_workqueue(wq);
649}
James Bottomley81ddef72005-04-16 15:23:59 -0700650EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800653 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
David Howells52bad642006-11-22 14:54:01 +0000654 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
David Howells52bad642006-11-22 14:54:01 +0000656void cancel_rearming_delayed_work(struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
David Howells52bad642006-11-22 14:54:01 +0000658 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660EXPORT_SYMBOL(cancel_rearming_delayed_work);
661
James Bottomley1fa44ec2006-02-23 12:43:43 -0600662/**
663 * execute_in_process_context - reliably execute the routine with user context
664 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600665 * @ew: guaranteed storage for the execute work structure (must
666 * be available when the work executes)
667 *
668 * Executes the function immediately if process context is available,
669 * otherwise schedules the function for delayed execution.
670 *
671 * Returns: 0 - function was executed
672 * 1 - function was scheduled for execution
673 */
David Howells65f27f32006-11-22 14:55:48 +0000674int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600675{
676 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000677 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600678 return 0;
679 }
680
David Howells65f27f32006-11-22 14:55:48 +0000681 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600682 schedule_work(&ew->work);
683
684 return 1;
685}
686EXPORT_SYMBOL_GPL(execute_in_process_context);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688int keventd_up(void)
689{
690 return keventd_wq != NULL;
691}
692
693int current_is_keventd(void)
694{
695 struct cpu_workqueue_struct *cwq;
696 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
697 int ret = 0;
698
699 BUG_ON(!keventd_wq);
700
Christoph Lameter89ada672005-10-30 15:01:59 -0800701 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (current == cwq->thread)
703 ret = 1;
704
705 return ret;
706
707}
708
Oleg Nesterov3af244332007-05-09 02:34:09 -0700709static struct cpu_workqueue_struct *
710init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Christoph Lameter89ada672005-10-30 15:01:59 -0800712 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Oleg Nesterov3af244332007-05-09 02:34:09 -0700714 cwq->wq = wq;
715 spin_lock_init(&cwq->lock);
716 INIT_LIST_HEAD(&cwq->worklist);
717 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Oleg Nesterov3af244332007-05-09 02:34:09 -0700719 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Oleg Nesterov3af244332007-05-09 02:34:09 -0700722static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700724 struct workqueue_struct *wq = cwq->wq;
725 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
726 struct task_struct *p;
727
728 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
729 /*
730 * Nobody can add the work_struct to this cwq,
731 * if (caller is __create_workqueue)
732 * nobody should see this wq
733 * else // caller is CPU_UP_PREPARE
734 * cpu is not on cpu_online_map
735 * so we can abort safely.
736 */
737 if (IS_ERR(p))
738 return PTR_ERR(p);
739
740 cwq->thread = p;
741 cwq->should_stop = 0;
742 if (!is_single_threaded(wq))
743 kthread_bind(p, cpu);
744
745 if (is_single_threaded(wq) || cpu_online(cpu))
746 wake_up_process(p);
747
748 return 0;
749}
750
751struct workqueue_struct *__create_workqueue(const char *name,
752 int singlethread, int freezeable)
753{
754 struct workqueue_struct *wq;
755 struct cpu_workqueue_struct *cwq;
756 int err = 0, cpu;
757
758 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
759 if (!wq)
760 return NULL;
761
762 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
763 if (!wq->cpu_wq) {
764 kfree(wq);
765 return NULL;
766 }
767
768 wq->name = name;
769 wq->freezeable = freezeable;
770
771 if (singlethread) {
772 INIT_LIST_HEAD(&wq->list);
773 cwq = init_cpu_workqueue(wq, singlethread_cpu);
774 err = create_workqueue_thread(cwq, singlethread_cpu);
775 } else {
776 mutex_lock(&workqueue_mutex);
777 list_add(&wq->list, &workqueues);
778
779 for_each_possible_cpu(cpu) {
780 cwq = init_cpu_workqueue(wq, cpu);
781 if (err || !cpu_online(cpu))
782 continue;
783 err = create_workqueue_thread(cwq, cpu);
784 }
785 mutex_unlock(&workqueue_mutex);
786 }
787
788 if (err) {
789 destroy_workqueue(wq);
790 wq = NULL;
791 }
792 return wq;
793}
794EXPORT_SYMBOL_GPL(__create_workqueue);
795
796static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
797{
798 struct wq_barrier barr;
799 int alive = 0;
800
801 spin_lock_irq(&cwq->lock);
802 if (cwq->thread != NULL) {
803 insert_wq_barrier(cwq, &barr, 1);
804 cwq->should_stop = 1;
805 alive = 1;
806 }
807 spin_unlock_irq(&cwq->lock);
808
809 if (alive) {
810 wait_for_completion(&barr.done);
811
812 while (unlikely(cwq->thread != NULL))
813 cpu_relax();
814 /*
815 * Wait until cwq->thread unlocks cwq->lock,
816 * it won't touch *cwq after that.
817 */
818 smp_rmb();
819 spin_unlock_wait(&cwq->lock);
820 }
821}
822
823/**
824 * destroy_workqueue - safely terminate a workqueue
825 * @wq: target workqueue
826 *
827 * Safely destroy a workqueue. All work currently pending will be done first.
828 */
829void destroy_workqueue(struct workqueue_struct *wq)
830{
831 struct cpu_workqueue_struct *cwq;
832
833 if (is_single_threaded(wq)) {
834 cwq = per_cpu_ptr(wq->cpu_wq, singlethread_cpu);
835 cleanup_workqueue_thread(cwq, singlethread_cpu);
836 } else {
837 int cpu;
838
839 mutex_lock(&workqueue_mutex);
840 list_del(&wq->list);
841 mutex_unlock(&workqueue_mutex);
842
843 for_each_cpu_mask(cpu, cpu_populated_map) {
844 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
845 cleanup_workqueue_thread(cwq, cpu);
846 }
847 }
848
849 free_percpu(wq->cpu_wq);
850 kfree(wq);
851}
852EXPORT_SYMBOL_GPL(destroy_workqueue);
853
854static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
855 unsigned long action,
856 void *hcpu)
857{
858 unsigned int cpu = (unsigned long)hcpu;
859 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct workqueue_struct *wq;
861
862 switch (action) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700863 case CPU_LOCK_ACQUIRE:
864 mutex_lock(&workqueue_mutex);
865 return NOTIFY_OK;
866
867 case CPU_LOCK_RELEASE:
868 mutex_unlock(&workqueue_mutex);
869 return NOTIFY_OK;
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 case CPU_UP_PREPARE:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700872 cpu_set(cpu, cpu_populated_map);
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Oleg Nesterov3af244332007-05-09 02:34:09 -0700875 list_for_each_entry(wq, &workqueues, list) {
876 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800877
Oleg Nesterov3af244332007-05-09 02:34:09 -0700878 switch (action) {
879 case CPU_UP_PREPARE:
880 if (!create_workqueue_thread(cwq, cpu))
881 break;
882 printk(KERN_ERR "workqueue for %i failed\n", cpu);
883 return NOTIFY_BAD;
884
885 case CPU_ONLINE:
Christoph Lameter89ada672005-10-30 15:01:59 -0800886 wake_up_process(cwq->thread);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700887 break;
888
889 case CPU_UP_CANCELED:
890 if (cwq->thread)
891 wake_up_process(cwq->thread);
892 case CPU_DEAD:
893 cleanup_workqueue_thread(cwq, cpu);
894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 return NOTIFY_OK;
899}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901void init_workqueues(void)
902{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700903 cpu_populated_map = cpu_online_map;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800904 singlethread_cpu = first_cpu(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 hotcpu_notifier(workqueue_cpu_callback, 0);
906 keventd_wq = create_workqueue("events");
907 BUG_ON(!keventd_wq);
908}