blob: e858e93886e37e01bbf99f5750484157993d225d [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
David Howells4594bf12006-12-07 11:33:26 +000089/*
90 * Set the workqueue on which a work item is to be run
91 * - Must *only* be called if the pending flag is set
92 */
David Howells365970a2006-11-22 14:54:49 +000093static inline void set_wq_data(struct work_struct *work, void *wq)
94{
David Howells4594bf12006-12-07 11:33:26 +000095 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +000096
David Howells4594bf12006-12-07 11:33:26 +000097 BUG_ON(!work_pending(work));
98
David Howells365970a2006-11-22 14:54:49 +000099 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800100 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
101 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000102}
103
104static inline void *get_wq_data(struct work_struct *work)
105{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800106 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000107}
108
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700109static void insert_work(struct cpu_workqueue_struct *cwq,
110 struct work_struct *work, int tail)
111{
112 set_wq_data(work, cwq);
113 if (tail)
114 list_add_tail(&work->entry, &cwq->worklist);
115 else
116 list_add(&work->entry, &cwq->worklist);
117 wake_up(&cwq->more_work);
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* Preempt must be disabled. */
121static void __queue_work(struct cpu_workqueue_struct *cwq,
122 struct work_struct *work)
123{
124 unsigned long flags;
125
126 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700127 insert_work(cwq, work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_unlock_irqrestore(&cwq->lock, flags);
129}
130
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700131/**
132 * queue_work - queue work on a workqueue
133 * @wq: workqueue to use
134 * @work: work to queue
135 *
Alan Stern057647f2006-10-28 10:38:58 -0700136 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * We queue the work to the CPU it was submitted, but there is no
139 * guarantee that it will be processed by that CPU.
140 */
141int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
142{
143 int ret = 0, cpu = get_cpu();
144
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800145 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800147 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 BUG_ON(!list_empty(&work->entry));
Christoph Lameter89ada672005-10-30 15:01:59 -0800149 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 ret = 1;
151 }
152 put_cpu();
153 return ret;
154}
Dave Jonesae90dd52006-06-30 01:40:45 -0400155EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800157void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
David Howells52bad642006-11-22 14:54:01 +0000159 struct delayed_work *dwork = (struct delayed_work *)__data;
David Howells365970a2006-11-22 14:54:49 +0000160 struct workqueue_struct *wq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int cpu = smp_processor_id();
162
163 if (unlikely(is_single_threaded(wq)))
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800164 cpu = singlethread_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
David Howells52bad642006-11-22 14:54:01 +0000166 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700169/**
170 * queue_delayed_work - queue work on a workqueue after delay
171 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800172 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700173 * @delay: number of jiffies to wait before queueing
174 *
Alan Stern057647f2006-10-28 10:38:58 -0700175 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177int fastcall queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000178 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000181 struct timer_list *timer = &dwork->timer;
182 struct work_struct *work = &dwork->work;
183
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800184 timer_stats_timer_set_start_info(timer);
David Howells52bad642006-11-22 14:54:01 +0000185 if (delay == 0)
186 return queue_work(wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800188 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 BUG_ON(timer_pending(timer));
190 BUG_ON(!list_empty(&work->entry));
191
192 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000193 set_wq_data(work, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000195 timer->data = (unsigned long)dwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 timer->function = delayed_work_timer_fn;
197 add_timer(timer);
198 ret = 1;
199 }
200 return ret;
201}
Dave Jonesae90dd52006-06-30 01:40:45 -0400202EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700204/**
205 * queue_delayed_work_on - queue work on specific CPU after delay
206 * @cpu: CPU number to execute work on
207 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800208 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700209 * @delay: number of jiffies to wait before queueing
210 *
Alan Stern057647f2006-10-28 10:38:58 -0700211 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700212 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700213int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000214 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700215{
216 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000217 struct timer_list *timer = &dwork->timer;
218 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700219
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800220 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700221 BUG_ON(timer_pending(timer));
222 BUG_ON(!list_empty(&work->entry));
223
224 /* This stores wq for the moment, for the timer_fn */
David Howells365970a2006-11-22 14:54:49 +0000225 set_wq_data(work, wq);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700226 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000227 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700228 timer->function = delayed_work_timer_fn;
229 add_timer_on(timer, cpu);
230 ret = 1;
231 }
232 return ret;
233}
Dave Jonesae90dd52006-06-30 01:40:45 -0400234EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Arjan van de Ven858119e2006-01-14 13:20:43 -0800236static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700238 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 cwq->run_depth++;
240 if (cwq->run_depth > 3) {
241 /* morton gets to eat his hat */
242 printk("%s: recursion depth exceeded: %d\n",
243 __FUNCTION__, cwq->run_depth);
244 dump_stack();
245 }
246 while (!list_empty(&cwq->worklist)) {
247 struct work_struct *work = list_entry(cwq->worklist.next,
248 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000249 work_func_t f = work->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700251 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700253 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Howells365970a2006-11-22 14:54:49 +0000255 BUG_ON(get_wq_data(work) != cwq);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800256 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
David Howells65f27f32006-11-22 14:55:48 +0000257 work_release(work);
258 f(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800260 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
261 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
262 "%s/0x%08x/%d\n",
263 current->comm, preempt_count(),
264 current->pid);
265 printk(KERN_ERR " last function: ");
266 print_symbol("%s\n", (unsigned long)f);
267 debug_show_held_locks(current);
268 dump_stack();
269 }
270
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700271 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700272 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 cwq->run_depth--;
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700275 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Oleg Nesterov3af244332007-05-09 02:34:09 -0700278/*
279 * NOTE: the caller must not touch *cwq if this func returns true
280 */
281static int cwq_should_stop(struct cpu_workqueue_struct *cwq)
282{
283 int should_stop = cwq->should_stop;
284
285 if (unlikely(should_stop)) {
286 spin_lock_irq(&cwq->lock);
287 should_stop = cwq->should_stop && list_empty(&cwq->worklist);
288 if (should_stop)
289 cwq->thread = NULL;
290 spin_unlock_irq(&cwq->lock);
291 }
292
293 return should_stop;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int worker_thread(void *__cwq)
297{
298 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700299 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct k_sigaction sa;
301 sigset_t blocked;
302
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700303 if (!cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800304 current->flags |= PF_NOFREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 set_user_nice(current, -5);
307
308 /* Block and flush all signals */
309 sigfillset(&blocked);
310 sigprocmask(SIG_BLOCK, &blocked, NULL);
311 flush_signals(current);
312
Christoph Lameter46934022006-10-11 01:21:26 -0700313 /*
314 * We inherited MPOL_INTERLEAVE from the booting kernel.
315 * Set MPOL_DEFAULT to insure node local allocations.
316 */
317 numa_default_policy();
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
320 sa.sa.sa_handler = SIG_IGN;
321 sa.sa.sa_flags = 0;
322 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
323 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
324
Oleg Nesterov3af244332007-05-09 02:34:09 -0700325 for (;;) {
Oleg Nesterov319c2a92007-05-09 02:34:06 -0700326 if (cwq->wq->freezeable)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800327 try_to_freeze();
328
Oleg Nesterov3af244332007-05-09 02:34:09 -0700329 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
330 if (!cwq->should_stop && list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700332 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Oleg Nesterov3af244332007-05-09 02:34:09 -0700334 if (cwq_should_stop(cwq))
335 break;
336
337 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
342
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700343struct wq_barrier {
344 struct work_struct work;
345 struct completion done;
346};
347
348static void wq_barrier_func(struct work_struct *work)
349{
350 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
351 complete(&barr->done);
352}
353
Oleg Nesterov83c22522007-05-09 02:33:54 -0700354static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
355 struct wq_barrier *barr, int tail)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700356{
357 INIT_WORK(&barr->work, wq_barrier_func);
358 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
359
360 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700361
362 insert_work(cwq, &barr->work, tail);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
366{
367 if (cwq->thread == current) {
368 /*
369 * Probably keventd trying to flush its own queue. So simply run
370 * it by hand rather than deadlocking.
371 */
372 run_workqueue(cwq);
373 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700374 struct wq_barrier barr;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700375 int active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Oleg Nesterov83c22522007-05-09 02:33:54 -0700377 spin_lock_irq(&cwq->lock);
378 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
379 insert_wq_barrier(cwq, &barr, 1);
380 active = 1;
381 }
382 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Oleg Nesterovd7213042007-05-09 02:34:07 -0700384 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700385 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387}
388
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700389/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700391 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *
393 * Forces execution of the workqueue and blocks until its completion.
394 * This is typically used in driver shutdown handlers.
395 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700396 * We sleep until all works which were queued on entry have been handled,
397 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *
399 * This function used to run the workqueues itself. Now we just wait for the
400 * helper threads to do it.
401 */
402void fastcall flush_workqueue(struct workqueue_struct *wq)
403{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700404 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700405 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700406
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700407 might_sleep();
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700408 for_each_cpu_mask(cpu, *cpu_map)
409 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
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{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700446 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700447 struct cpu_workqueue_struct *cwq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700448 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700449
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700450 might_sleep();
451
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700452 cwq = get_wq_data(work);
453 /* Was it ever queued ? */
454 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700455 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700456
457 /*
Oleg Nesterov3af244332007-05-09 02:34:09 -0700458 * This work can't be re-queued, no need to re-check that
459 * get_wq_data() is still the same when we take cwq->lock.
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700460 */
461 spin_lock_irq(&cwq->lock);
462 list_del_init(&work->entry);
463 work_release(work);
464 spin_unlock_irq(&cwq->lock);
465
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700466 for_each_cpu_mask(cpu, *cpu_map)
467 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700468}
469EXPORT_SYMBOL_GPL(flush_work);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472static struct workqueue_struct *keventd_wq;
473
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700474/**
475 * schedule_work - put work task in global workqueue
476 * @work: job to be done
477 *
478 * This puts a job in the kernel-global workqueue.
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480int fastcall schedule_work(struct work_struct *work)
481{
482 return queue_work(keventd_wq, work);
483}
Dave Jonesae90dd52006-06-30 01:40:45 -0400484EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700486/**
487 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000488 * @dwork: job to be done
489 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700490 *
491 * After waiting for a given time this puts a job in the kernel-global
492 * workqueue.
493 */
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800494int fastcall schedule_delayed_work(struct delayed_work *dwork,
495 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800497 timer_stats_timer_set_start_info(&dwork->timer);
David Howells52bad642006-11-22 14:54:01 +0000498 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
Dave Jonesae90dd52006-06-30 01:40:45 -0400500EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700502/**
503 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
504 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000505 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700506 * @delay: number of jiffies to wait
507 *
508 * After waiting for a given time this puts a job in the kernel-global
509 * workqueue on the specified CPU.
510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000512 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howells52bad642006-11-22 14:54:01 +0000514 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
Dave Jonesae90dd52006-06-30 01:40:45 -0400516EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Andrew Mortonb6136772006-06-25 05:47:49 -0700518/**
519 * schedule_on_each_cpu - call a function on each online CPU from keventd
520 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700521 *
522 * Returns zero on success.
523 * Returns -ve errno on failure.
524 *
525 * Appears to be racy against CPU hotplug.
526 *
527 * schedule_on_each_cpu() is very slow.
528 */
David Howells65f27f32006-11-22 14:55:48 +0000529int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800530{
531 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700532 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800533
Andrew Mortonb6136772006-06-25 05:47:49 -0700534 works = alloc_percpu(struct work_struct);
535 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800536 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700537
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700538 preempt_disable(); /* CPU hotplug */
Christoph Lameter15316ba2006-01-08 01:00:43 -0800539 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100540 struct work_struct *work = per_cpu_ptr(works, cpu);
541
542 INIT_WORK(work, func);
543 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
544 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800545 }
Andrew Mortone18f3ff2007-05-09 02:33:50 -0700546 preempt_enable();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800547 flush_workqueue(keventd_wq);
Andrew Mortonb6136772006-06-25 05:47:49 -0700548 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800549 return 0;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552void flush_scheduled_work(void)
553{
554 flush_workqueue(keventd_wq);
555}
Dave Jonesae90dd52006-06-30 01:40:45 -0400556EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700558void flush_work_keventd(struct work_struct *work)
559{
560 flush_work(keventd_wq, work);
561}
562EXPORT_SYMBOL(flush_work_keventd);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800565 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * @wq: the controlling workqueue structure
David Howells52bad642006-11-22 14:54:01 +0000567 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
James Bottomley81ddef72005-04-16 15:23:59 -0700569void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000570 struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Oleg Nesterovdfb4b822007-05-09 02:34:11 -0700572 /* Was it ever queued ? */
573 if (!get_wq_data(&dwork->work))
574 return;
575
David Howells52bad642006-11-22 14:54:01 +0000576 while (!cancel_delayed_work(dwork))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 flush_workqueue(wq);
578}
James Bottomley81ddef72005-04-16 15:23:59 -0700579EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800582 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
David Howells52bad642006-11-22 14:54:01 +0000583 * @dwork: the delayed work struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
David Howells52bad642006-11-22 14:54:01 +0000585void cancel_rearming_delayed_work(struct delayed_work *dwork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
David Howells52bad642006-11-22 14:54:01 +0000587 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589EXPORT_SYMBOL(cancel_rearming_delayed_work);
590
James Bottomley1fa44ec2006-02-23 12:43:43 -0600591/**
592 * execute_in_process_context - reliably execute the routine with user context
593 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600594 * @ew: guaranteed storage for the execute work structure (must
595 * be available when the work executes)
596 *
597 * Executes the function immediately if process context is available,
598 * otherwise schedules the function for delayed execution.
599 *
600 * Returns: 0 - function was executed
601 * 1 - function was scheduled for execution
602 */
David Howells65f27f32006-11-22 14:55:48 +0000603int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600604{
605 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000606 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600607 return 0;
608 }
609
David Howells65f27f32006-11-22 14:55:48 +0000610 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600611 schedule_work(&ew->work);
612
613 return 1;
614}
615EXPORT_SYMBOL_GPL(execute_in_process_context);
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617int keventd_up(void)
618{
619 return keventd_wq != NULL;
620}
621
622int current_is_keventd(void)
623{
624 struct cpu_workqueue_struct *cwq;
625 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
626 int ret = 0;
627
628 BUG_ON(!keventd_wq);
629
Christoph Lameter89ada672005-10-30 15:01:59 -0800630 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (current == cwq->thread)
632 ret = 1;
633
634 return ret;
635
636}
637
Oleg Nesterov3af244332007-05-09 02:34:09 -0700638static struct cpu_workqueue_struct *
639init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Christoph Lameter89ada672005-10-30 15:01:59 -0800641 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Oleg Nesterov3af244332007-05-09 02:34:09 -0700643 cwq->wq = wq;
644 spin_lock_init(&cwq->lock);
645 INIT_LIST_HEAD(&cwq->worklist);
646 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Oleg Nesterov3af244332007-05-09 02:34:09 -0700648 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Oleg Nesterov3af244332007-05-09 02:34:09 -0700651static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700653 struct workqueue_struct *wq = cwq->wq;
654 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
655 struct task_struct *p;
656
657 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
658 /*
659 * Nobody can add the work_struct to this cwq,
660 * if (caller is __create_workqueue)
661 * nobody should see this wq
662 * else // caller is CPU_UP_PREPARE
663 * cpu is not on cpu_online_map
664 * so we can abort safely.
665 */
666 if (IS_ERR(p))
667 return PTR_ERR(p);
668
669 cwq->thread = p;
670 cwq->should_stop = 0;
671 if (!is_single_threaded(wq))
672 kthread_bind(p, cpu);
673
674 if (is_single_threaded(wq) || cpu_online(cpu))
675 wake_up_process(p);
676
677 return 0;
678}
679
680struct workqueue_struct *__create_workqueue(const char *name,
681 int singlethread, int freezeable)
682{
683 struct workqueue_struct *wq;
684 struct cpu_workqueue_struct *cwq;
685 int err = 0, cpu;
686
687 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
688 if (!wq)
689 return NULL;
690
691 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
692 if (!wq->cpu_wq) {
693 kfree(wq);
694 return NULL;
695 }
696
697 wq->name = name;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700698 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700699 wq->freezeable = freezeable;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700700 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700701
702 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700703 cwq = init_cpu_workqueue(wq, singlethread_cpu);
704 err = create_workqueue_thread(cwq, singlethread_cpu);
705 } else {
706 mutex_lock(&workqueue_mutex);
707 list_add(&wq->list, &workqueues);
708
709 for_each_possible_cpu(cpu) {
710 cwq = init_cpu_workqueue(wq, cpu);
711 if (err || !cpu_online(cpu))
712 continue;
713 err = create_workqueue_thread(cwq, cpu);
714 }
715 mutex_unlock(&workqueue_mutex);
716 }
717
718 if (err) {
719 destroy_workqueue(wq);
720 wq = NULL;
721 }
722 return wq;
723}
724EXPORT_SYMBOL_GPL(__create_workqueue);
725
726static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
727{
728 struct wq_barrier barr;
729 int alive = 0;
730
731 spin_lock_irq(&cwq->lock);
732 if (cwq->thread != NULL) {
733 insert_wq_barrier(cwq, &barr, 1);
734 cwq->should_stop = 1;
735 alive = 1;
736 }
737 spin_unlock_irq(&cwq->lock);
738
739 if (alive) {
740 wait_for_completion(&barr.done);
741
742 while (unlikely(cwq->thread != NULL))
743 cpu_relax();
744 /*
745 * Wait until cwq->thread unlocks cwq->lock,
746 * it won't touch *cwq after that.
747 */
748 smp_rmb();
749 spin_unlock_wait(&cwq->lock);
750 }
751}
752
753/**
754 * destroy_workqueue - safely terminate a workqueue
755 * @wq: target workqueue
756 *
757 * Safely destroy a workqueue. All work currently pending will be done first.
758 */
759void destroy_workqueue(struct workqueue_struct *wq)
760{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700761 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700762 struct cpu_workqueue_struct *cwq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700763 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700764
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700765 mutex_lock(&workqueue_mutex);
766 list_del(&wq->list);
767 mutex_unlock(&workqueue_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700768
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700769 for_each_cpu_mask(cpu, *cpu_map) {
770 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
771 cleanup_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700772 }
773
774 free_percpu(wq->cpu_wq);
775 kfree(wq);
776}
777EXPORT_SYMBOL_GPL(destroy_workqueue);
778
779static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
780 unsigned long action,
781 void *hcpu)
782{
783 unsigned int cpu = (unsigned long)hcpu;
784 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct workqueue_struct *wq;
786
787 switch (action) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700788 case CPU_LOCK_ACQUIRE:
789 mutex_lock(&workqueue_mutex);
790 return NOTIFY_OK;
791
792 case CPU_LOCK_RELEASE:
793 mutex_unlock(&workqueue_mutex);
794 return NOTIFY_OK;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case CPU_UP_PREPARE:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700797 cpu_set(cpu, cpu_populated_map);
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Oleg Nesterov3af244332007-05-09 02:34:09 -0700800 list_for_each_entry(wq, &workqueues, list) {
801 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800802
Oleg Nesterov3af244332007-05-09 02:34:09 -0700803 switch (action) {
804 case CPU_UP_PREPARE:
805 if (!create_workqueue_thread(cwq, cpu))
806 break;
807 printk(KERN_ERR "workqueue for %i failed\n", cpu);
808 return NOTIFY_BAD;
809
810 case CPU_ONLINE:
Christoph Lameter89ada672005-10-30 15:01:59 -0800811 wake_up_process(cwq->thread);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700812 break;
813
814 case CPU_UP_CANCELED:
815 if (cwq->thread)
816 wake_up_process(cwq->thread);
817 case CPU_DEAD:
818 cleanup_workqueue_thread(cwq, cpu);
819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 return NOTIFY_OK;
824}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Oleg Nesterovc12920d2007-05-09 02:34:14 -0700826void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700828 cpu_populated_map = cpu_online_map;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800829 singlethread_cpu = first_cpu(cpu_possible_map);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700830 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 hotcpu_notifier(workqueue_cpu_callback, 0);
832 keventd_wq = create_workqueue("events");
833 BUG_ON(!keventd_wq);
834}