blob: 63885abf1ba093e09af3b0dc981a789bd1bf1d46 [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;
Oleg Nesterovcce1a162007-05-09 02:34:13 -070061 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 const char *name;
Oleg Nesterovcce1a162007-05-09 02:34:13 -070063 int singlethread;
Oleg Nesterov319c2a92007-05-09 02:34:06 -070064 int freezeable; /* Freeze threads during suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67/* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
68 threads to each one as cpus come/go. */
Andrew Morton9b41ea72006-08-13 23:24:26 -070069static DEFINE_MUTEX(workqueue_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static LIST_HEAD(workqueues);
71
Oleg Nesterov3af244332007-05-09 02:34:09 -070072static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070073static cpumask_t cpu_singlethread_map __read_mostly;
Oleg Nesterov3af244332007-05-09 02:34:09 -070074/* optimization, we could use cpu_possible_map */
75static cpumask_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -080076
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{
Oleg Nesterovcce1a162007-05-09 02:34:13 -070080 return wq->singlethread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070083static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
84{
85 return is_single_threaded(wq)
86 ? &cpu_singlethread_map : &cpu_populated_map;
87}
88
Oleg Nesterova848e3b2007-05-09 02:34:17 -070089static
90struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
91{
92 if (unlikely(is_single_threaded(wq)))
93 cpu = singlethread_cpu;
94 return per_cpu_ptr(wq->cpu_wq, cpu);
95}
96
David Howells4594bf12006-12-07 11:33:26 +000097/*
98 * Set the workqueue on which a work item is to be run
99 * - Must *only* be called if the pending flag is set
100 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700101static inline void set_wq_data(struct work_struct *work,
102 struct cpu_workqueue_struct *cwq)
David Howells365970a2006-11-22 14:54:49 +0000103{
David Howells4594bf12006-12-07 11:33:26 +0000104 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +0000105
David Howells4594bf12006-12-07 11:33:26 +0000106 BUG_ON(!work_pending(work));
107
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700108 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800109 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
110 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000111}
112
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700113static inline
114struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000115{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800116 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000117}
118
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700119static void insert_work(struct cpu_workqueue_struct *cwq,
120 struct work_struct *work, int tail)
121{
122 set_wq_data(work, cwq);
123 if (tail)
124 list_add_tail(&work->entry, &cwq->worklist);
125 else
126 list_add(&work->entry, &cwq->worklist);
127 wake_up(&cwq->more_work);
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* Preempt must be disabled. */
131static void __queue_work(struct cpu_workqueue_struct *cwq,
132 struct work_struct *work)
133{
134 unsigned long flags;
135
136 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700137 insert_work(cwq, work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 spin_unlock_irqrestore(&cwq->lock, flags);
139}
140
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700141/**
142 * queue_work - queue work on a workqueue
143 * @wq: workqueue to use
144 * @work: work to queue
145 *
Alan Stern057647f2006-10-28 10:38:58 -0700146 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
148 * We queue the work to the CPU it was submitted, but there is no
149 * guarantee that it will be processed by that CPU.
150 */
151int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
152{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700153 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800155 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 BUG_ON(!list_empty(&work->entry));
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700157 __queue_work(wq_per_cpu(wq, get_cpu()), work);
158 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 ret = 1;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return ret;
162}
Dave Jonesae90dd52006-06-30 01:40:45 -0400163EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800165void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
David Howells52bad642006-11-22 14:54:01 +0000167 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700168 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
169 struct workqueue_struct *wq = cwq->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700171 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700174/**
175 * queue_delayed_work - queue work on a workqueue after delay
176 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800177 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700178 * @delay: number of jiffies to wait before queueing
179 *
Alan Stern057647f2006-10-28 10:38:58 -0700180 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182int fastcall queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000183 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700185 timer_stats_timer_set_start_info(&dwork->timer);
David Howells52bad642006-11-22 14:54:01 +0000186 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700187 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700189 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
Dave Jonesae90dd52006-06-30 01:40:45 -0400191EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700193/**
194 * queue_delayed_work_on - queue work on specific CPU after delay
195 * @cpu: CPU number to execute work on
196 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800197 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700198 * @delay: number of jiffies to wait before queueing
199 *
Alan Stern057647f2006-10-28 10:38:58 -0700200 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700201 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700202int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000203 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700204{
205 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000206 struct timer_list *timer = &dwork->timer;
207 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700208
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800209 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700210 BUG_ON(timer_pending(timer));
211 BUG_ON(!list_empty(&work->entry));
212
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700213 /* This stores cwq for the moment, for the timer_fn */
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700214 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700215 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000216 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700217 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700218
219 if (unlikely(cpu >= 0))
220 add_timer_on(timer, cpu);
221 else
222 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700223 ret = 1;
224 }
225 return ret;
226}
Dave Jonesae90dd52006-06-30 01:40:45 -0400227EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Arjan van de Ven858119e2006-01-14 13:20:43 -0800229static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700231 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cwq->run_depth++;
233 if (cwq->run_depth > 3) {
234 /* morton gets to eat his hat */
235 printk("%s: recursion depth exceeded: %d\n",
236 __FUNCTION__, cwq->run_depth);
237 dump_stack();
238 }
239 while (!list_empty(&cwq->worklist)) {
240 struct work_struct *work = list_entry(cwq->worklist.next,
241 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000242 work_func_t f = work->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700244 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700246 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David Howells365970a2006-11-22 14:54:49 +0000248 BUG_ON(get_wq_data(work) != cwq);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700249 work_clear_pending(work);
David Howells65f27f32006-11-22 14:55:48 +0000250 f(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800252 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
253 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
254 "%s/0x%08x/%d\n",
255 current->comm, preempt_count(),
256 current->pid);
257 printk(KERN_ERR " last function: ");
258 print_symbol("%s\n", (unsigned long)f);
259 debug_show_held_locks(current);
260 dump_stack();
261 }
262
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700263 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700264 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 cwq->run_depth--;
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700267 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Oleg Nesterov3af244332007-05-09 02:34:09 -0700270/*
271 * NOTE: the caller must not touch *cwq if this func returns true
272 */
273static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
274{
275 int should_stop = cwq->should_stop;
276
277 if (unlikely(should_stop)) {
278 spin_lock_irq(&cwq->lock);
279 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
280 if (should_stop)
281 cwq->thread = NULL;
282 spin_unlock_irq(&cwq->lock);
283 }
284
285 return should_stop;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static int worker_thread(void *__cwq)
289{
290 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700291 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct k_sigaction sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700294 if (!cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800295 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 set_user_nice(current, -5);
Christoph Lameter46934022006-10-11 01:21:26 -0700298 /*
299 * We inherited MPOL_INTERLEAVE from the booting kernel.
300 * Set MPOL_DEFAULT to insure node local allocations.
301 */
302 numa_default_policy();
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
305 sa.sa.sa_handler = SIG_IGN;
306 sa.sa.sa_flags = 0;
307 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
308 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
309
Oleg Nesterov3af244332007-05-09 02:34:09 -0700310 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700311 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov85f41862007-05-09 02:34:20 -0700312 if (!freezing(current) && !cwq->should_stop
313 && list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700315 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Oleg Nesterov85f41862007-05-09 02:34:20 -0700317 try_to_freeze();
318
Oleg Nesterov3af244332007-05-09 02:34:09 -0700319 if (cwq_should_stop(cwq))
320 break;
321
322 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700328struct wq_barrier {
329 struct work_struct work;
330 struct completion done;
331};
332
333static void wq_barrier_func(struct work_struct *work)
334{
335 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
336 complete(&barr->done);
337}
338
Oleg Nesterov83c22522007-05-09 02:33:54 -0700339static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
340 struct wq_barrier *barr, int tail)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700341{
342 INIT_WORK(&barr->work, wq_barrier_func);
343 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
344
345 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700346
347 insert_work(cwq, &barr->work, tail);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
351{
352 if (cwq->thread == current) {
353 /*
354 * Probably keventd trying to flush its own queue. So simply run
355 * it by hand rather than deadlocking.
356 */
357 run_workqueue(cwq);
358 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700359 struct wq_barrier barr;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700360 int active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Oleg Nesterov83c22522007-05-09 02:33:54 -0700362 spin_lock_irq(&cwq->lock);
363 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
364 insert_wq_barrier(cwq, &barr, 1);
365 active = 1;
366 }
367 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Oleg Nesterovd7213042007-05-09 02:34:07 -0700369 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700370 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372}
373
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700374/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700376 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
378 * Forces execution of the workqueue and blocks until its completion.
379 * This is typically used in driver shutdown handlers.
380 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700381 * We sleep until all works which were queued on entry have been handled,
382 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
384 * This function used to run the workqueues itself. Now we just wait for the
385 * helper threads to do it.
386 */
387void fastcall flush_workqueue(struct workqueue_struct *wq)
388{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700389 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700390 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700391
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700392 might_sleep();
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700393 for_each_cpu_mask(cpu, *cpu_map)
394 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
Dave Jonesae90dd52006-06-30 01:40:45 -0400396EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700398static void wait_on_work(struct cpu_workqueue_struct *cwq,
399 struct work_struct *work)
400{
401 struct wq_barrier barr;
402 int running = 0;
403
404 spin_lock_irq(&cwq->lock);
405 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov83c22522007-05-09 02:33:54 -0700406 insert_wq_barrier(cwq, &barr, 0);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700407 running = 1;
408 }
409 spin_unlock_irq(&cwq->lock);
410
Oleg Nesterov3af244332007-05-09 02:34:09 -0700411 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700412 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700413}
414
415/**
416 * flush_work - block until a work_struct's callback has terminated
417 * @wq: the workqueue on which the work is queued
418 * @work: the work which is to be flushed
419 *
420 * flush_work() will attempt to cancel the work if it is queued. If the work's
421 * callback appears to be running, flush_work() will block until it has
422 * completed.
423 *
424 * flush_work() is designed to be used when the caller is tearing down data
425 * structures which the callback function operates upon. It is expected that,
426 * prior to calling flush_work(), the caller has arranged for the work to not
427 * be requeued.
428 */
429void flush_work(struct workqueue_struct *wq, struct work_struct *work)
430{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700431 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700432 struct cpu_workqueue_struct *cwq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700433 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700434
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700435 might_sleep();
436
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700437 cwq = get_wq_data(work);
438 /* Was it ever queued ? */
439 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700440 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700441
442 /*
Oleg Nesterov3af244332007-05-09 02:34:09 -0700443 * This work can't be re-queued, no need to re-check that
444 * get_wq_data() is still the same when we take cwq->lock.
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700445 */
446 spin_lock_irq(&cwq->lock);
447 list_del_init(&work->entry);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700448 work_clear_pending(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700449 spin_unlock_irq(&cwq->lock);
450
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700451 for_each_cpu_mask(cpu, *cpu_map)
452 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700453}
454EXPORT_SYMBOL_GPL(flush_work);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457static struct workqueue_struct *keventd_wq;
458
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700459/**
460 * schedule_work - put work task in global workqueue
461 * @work: job to be done
462 *
463 * This puts a job in the kernel-global workqueue.
464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465int fastcall schedule_work(struct work_struct *work)
466{
467 return queue_work(keventd_wq, work);
468}
Dave Jonesae90dd52006-06-30 01:40:45 -0400469EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700471/**
472 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000473 * @dwork: job to be done
474 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700475 *
476 * After waiting for a given time this puts a job in the kernel-global
477 * workqueue.
478 */
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800479int fastcall schedule_delayed_work(struct delayed_work *dwork,
480 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800482 timer_stats_timer_set_start_info(&dwork->timer);
David Howells52bad642006-11-22 14:54:01 +0000483 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Dave Jonesae90dd52006-06-30 01:40:45 -0400485EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700487/**
488 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
489 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000490 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700491 * @delay: number of jiffies to wait
492 *
493 * After waiting for a given time this puts a job in the kernel-global
494 * workqueue on the specified CPU.
495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000497 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
David Howells52bad642006-11-22 14:54:01 +0000499 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Dave Jonesae90dd52006-06-30 01:40:45 -0400501EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Andrew Mortonb6136772006-06-25 05:47:49 -0700503/**
504 * schedule_on_each_cpu - call a function on each online CPU from keventd
505 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700506 *
507 * Returns zero on success.
508 * Returns -ve errno on failure.
509 *
510 * Appears to be racy against CPU hotplug.
511 *
512 * schedule_on_each_cpu() is very slow.
513 */
David Howells65f27f32006-11-22 14:55:48 +0000514int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800515{
516 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700517 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800518
Andrew Mortonb6136772006-06-25 05:47:49 -0700519 works = alloc_percpu(struct work_struct);
520 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800521 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700522
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700523 preempt_disable(); /* CPU hotplug */
Christoph Lameter15316ba2006-01-08 01:00:43 -0800524 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100525 struct work_struct *work = per_cpu_ptr(works, cpu);
526
527 INIT_WORK(work, func);
528 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
529 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800530 }
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700531 preempt_enable();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800532 flush_workqueue(keventd_wq);
Andrew Mortonb6136772006-06-25 05:47:49 -0700533 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800534 return 0;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537void flush_scheduled_work(void)
538{
539 flush_workqueue(keventd_wq);
540}
Dave Jonesae90dd52006-06-30 01:40:45 -0400541EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700543void flush_work_keventd(struct work_struct *work)
544{
545 flush_work(keventd_wq, work);
546}
547EXPORT_SYMBOL(flush_work_keventd);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Oleg Nesterov1634c482007-05-09 02:34:18 -0700550 * cancel_rearming_delayed_work - kill off a delayed work whose handler rearms the delayed work.
David Howells52bad642006-11-22 14:54:01 +0000551 * @dwork: the delayed work struct
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700552 *
553 * Note that the work callback function may still be running on return from
554 * cancel_delayed_work(). Run flush_workqueue() or flush_work() to wait on it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
David Howells52bad642006-11-22 14:54:01 +0000556void cancel_rearming_delayed_work(struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Oleg Nesterov1634c482007-05-09 02:34:18 -0700558 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
559
560 /* Was it ever queued ? */
561 if (cwq != NULL) {
562 struct workqueue_struct *wq = cwq->wq;
563
564 while (!cancel_delayed_work(dwork))
565 flush_workqueue(wq);
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568EXPORT_SYMBOL(cancel_rearming_delayed_work);
569
James Bottomley1fa44ec2006-02-23 12:43:43 -0600570/**
571 * execute_in_process_context - reliably execute the routine with user context
572 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600573 * @ew: guaranteed storage for the execute work structure (must
574 * be available when the work executes)
575 *
576 * Executes the function immediately if process context is available,
577 * otherwise schedules the function for delayed execution.
578 *
579 * Returns: 0 - function was executed
580 * 1 - function was scheduled for execution
581 */
David Howells65f27f32006-11-22 14:55:48 +0000582int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600583{
584 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000585 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600586 return 0;
587 }
588
David Howells65f27f32006-11-22 14:55:48 +0000589 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600590 schedule_work(&ew->work);
591
592 return 1;
593}
594EXPORT_SYMBOL_GPL(execute_in_process_context);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596int keventd_up(void)
597{
598 return keventd_wq != NULL;
599}
600
601int current_is_keventd(void)
602{
603 struct cpu_workqueue_struct *cwq;
604 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
605 int ret = 0;
606
607 BUG_ON(!keventd_wq);
608
Christoph Lameter89ada672005-10-30 15:01:59 -0800609 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (current == cwq->thread)
611 ret = 1;
612
613 return ret;
614
615}
616
Oleg Nesterov3af244332007-05-09 02:34:09 -0700617static struct cpu_workqueue_struct *
618init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Christoph Lameter89ada672005-10-30 15:01:59 -0800620 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Oleg Nesterov3af244332007-05-09 02:34:09 -0700622 cwq->wq = wq;
623 spin_lock_init(&cwq->lock);
624 INIT_LIST_HEAD(&cwq->worklist);
625 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Oleg Nesterov3af244332007-05-09 02:34:09 -0700627 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Oleg Nesterov3af244332007-05-09 02:34:09 -0700630static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700632 struct workqueue_struct *wq = cwq->wq;
633 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
634 struct task_struct *p;
635
636 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
637 /*
638 * Nobody can add the work_struct to this cwq,
639 * if (caller is __create_workqueue)
640 * nobody should see this wq
641 * else // caller is CPU_UP_PREPARE
642 * cpu is not on cpu_online_map
643 * so we can abort safely.
644 */
645 if (IS_ERR(p))
646 return PTR_ERR(p);
647
648 cwq->thread = p;
649 cwq->should_stop = 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700650
651 return 0;
652}
653
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700654static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
655{
656 struct task_struct *p = cwq->thread;
657
658 if (p != NULL) {
659 if (cpu >= 0)
660 kthread_bind(p, cpu);
661 wake_up_process(p);
662 }
663}
664
Oleg Nesterov3af244332007-05-09 02:34:09 -0700665struct workqueue_struct *__create_workqueue(const char *name,
666 int singlethread, int freezeable)
667{
668 struct workqueue_struct *wq;
669 struct cpu_workqueue_struct *cwq;
670 int err = 0, cpu;
671
672 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
673 if (!wq)
674 return NULL;
675
676 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
677 if (!wq->cpu_wq) {
678 kfree(wq);
679 return NULL;
680 }
681
682 wq->name = name;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700683 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700684 wq->freezeable = freezeable;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700685 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700686
687 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700688 cwq = init_cpu_workqueue(wq, singlethread_cpu);
689 err = create_workqueue_thread(cwq, singlethread_cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700690 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700691 } else {
692 mutex_lock(&workqueue_mutex);
693 list_add(&wq->list, &workqueues);
694
695 for_each_possible_cpu(cpu) {
696 cwq = init_cpu_workqueue(wq, cpu);
697 if (err || !cpu_online(cpu))
698 continue;
699 err = create_workqueue_thread(cwq, cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700700 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700701 }
702 mutex_unlock(&workqueue_mutex);
703 }
704
705 if (err) {
706 destroy_workqueue(wq);
707 wq = NULL;
708 }
709 return wq;
710}
711EXPORT_SYMBOL_GPL(__create_workqueue);
712
713static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
714{
715 struct wq_barrier barr;
716 int alive = 0;
717
718 spin_lock_irq(&cwq->lock);
719 if (cwq->thread != NULL) {
720 insert_wq_barrier(cwq, &barr, 1);
721 cwq->should_stop = 1;
722 alive = 1;
723 }
724 spin_unlock_irq(&cwq->lock);
725
726 if (alive) {
727 wait_for_completion(&barr.done);
728
729 while (unlikely(cwq->thread != NULL))
730 cpu_relax();
731 /*
732 * Wait until cwq->thread unlocks cwq->lock,
733 * it won't touch *cwq after that.
734 */
735 smp_rmb();
736 spin_unlock_wait(&cwq->lock);
737 }
738}
739
740/**
741 * destroy_workqueue - safely terminate a workqueue
742 * @wq: target workqueue
743 *
744 * Safely destroy a workqueue. All work currently pending will be done first.
745 */
746void destroy_workqueue(struct workqueue_struct *wq)
747{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700748 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700749 struct cpu_workqueue_struct *cwq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700750 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700751
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700752 mutex_lock(&workqueue_mutex);
753 list_del(&wq->list);
754 mutex_unlock(&workqueue_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700755
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700756 for_each_cpu_mask(cpu, *cpu_map) {
757 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
758 cleanup_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700759 }
760
761 free_percpu(wq->cpu_wq);
762 kfree(wq);
763}
764EXPORT_SYMBOL_GPL(destroy_workqueue);
765
766static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
767 unsigned long action,
768 void *hcpu)
769{
770 unsigned int cpu = (unsigned long)hcpu;
771 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct workqueue_struct *wq;
773
774 switch (action) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700775 case CPU_LOCK_ACQUIRE:
776 mutex_lock(&workqueue_mutex);
777 return NOTIFY_OK;
778
779 case CPU_LOCK_RELEASE:
780 mutex_unlock(&workqueue_mutex);
781 return NOTIFY_OK;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 case CPU_UP_PREPARE:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700784 cpu_set(cpu, cpu_populated_map);
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Oleg Nesterov3af244332007-05-09 02:34:09 -0700787 list_for_each_entry(wq, &workqueues, list) {
788 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800789
Oleg Nesterov3af244332007-05-09 02:34:09 -0700790 switch (action) {
791 case CPU_UP_PREPARE:
792 if (!create_workqueue_thread(cwq, cpu))
793 break;
794 printk(KERN_ERR "workqueue for %i failed\n", cpu);
795 return NOTIFY_BAD;
796
797 case CPU_ONLINE:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700798 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700799 break;
800
801 case CPU_UP_CANCELED:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700802 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700803 case CPU_DEAD:
804 cleanup_workqueue_thread(cwq, cpu);
805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
809 return NOTIFY_OK;
810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Oleg Nesterovc12920d2007-05-09 02:34:14 -0700812void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700814 cpu_populated_map = cpu_online_map;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800815 singlethread_cpu = first_cpu(cpu_possible_map);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700816 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 hotcpu_notifier(workqueue_cpu_callback, 0);
818 keventd_wq = create_workqueue("events");
819 BUG_ON(!keventd_wq);
820}