blob: 6fd158b2102688c254b4dd23614eb8783c078289 [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 *
Christoph Lametercde53532008-07-04 09:59:22 -070016 * Made to use alloc_percpu by Christoph Lameter.
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>
Johannes Berg4e6045f2007-10-18 23:39:55 -070035#include <linux/lockdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080038 * The per-CPU workqueue (if single thread, we always use the first
39 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41struct cpu_workqueue_struct {
42
43 spinlock_t lock;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct list_head worklist;
46 wait_queue_head_t more_work;
Oleg Nesterov3af244332007-05-09 02:34:09 -070047 struct work_struct *current_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 struct workqueue_struct *wq;
Ingo Molnar36c8b582006-07-03 00:25:41 -070050 struct task_struct *thread;
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 */
Johannes Berg4e6045f2007-10-18 23:39:55 -070065#ifdef CONFIG_LOCKDEP
66 struct lockdep_map lockdep_map;
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Gautham R Shenoy95402b32008-01-25 21:08:02 +010070/* Serializes the accesses to the list of workqueues. */
71static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static LIST_HEAD(workqueues);
73
Oleg Nesterov3af244332007-05-09 02:34:09 -070074static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070075static cpumask_t cpu_singlethread_map __read_mostly;
Oleg Nesterov14441962007-05-23 13:57:57 -070076/*
77 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
78 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
79 * which comes in between can't use for_each_online_cpu(). We could
80 * use cpu_possible_map, the cpumask below is more a documentation
81 * than optimization.
82 */
Oleg Nesterov3af244332007-05-09 02:34:09 -070083static cpumask_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -080084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* If it's single threaded, it isn't in the list of workqueues. */
86static inline int is_single_threaded(struct workqueue_struct *wq)
87{
Oleg Nesterovcce1a162007-05-09 02:34:13 -070088 return wq->singlethread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070091static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
92{
93 return is_single_threaded(wq)
94 ? &cpu_singlethread_map : &cpu_populated_map;
95}
96
Oleg Nesterova848e3b2007-05-09 02:34:17 -070097static
98struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
99{
100 if (unlikely(is_single_threaded(wq)))
101 cpu = singlethread_cpu;
102 return per_cpu_ptr(wq->cpu_wq, cpu);
103}
104
David Howells4594bf12006-12-07 11:33:26 +0000105/*
106 * Set the workqueue on which a work item is to be run
107 * - Must *only* be called if the pending flag is set
108 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700109static inline void set_wq_data(struct work_struct *work,
110 struct cpu_workqueue_struct *cwq)
David Howells365970a2006-11-22 14:54:49 +0000111{
David Howells4594bf12006-12-07 11:33:26 +0000112 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +0000113
David Howells4594bf12006-12-07 11:33:26 +0000114 BUG_ON(!work_pending(work));
115
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700116 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800117 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
118 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000119}
120
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700121static inline
122struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000123{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800124 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000125}
126
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700127static void insert_work(struct cpu_workqueue_struct *cwq,
128 struct work_struct *work, int tail)
129{
130 set_wq_data(work, cwq);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700131 /*
132 * Ensure that we get the right work->data if we see the
133 * result of list_add() below, see try_to_grab_pending().
134 */
135 smp_wmb();
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700136 if (tail)
137 list_add_tail(&work->entry, &cwq->worklist);
138 else
139 list_add(&work->entry, &cwq->worklist);
140 wake_up(&cwq->more_work);
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void __queue_work(struct cpu_workqueue_struct *cwq,
144 struct work_struct *work)
145{
146 unsigned long flags;
147
148 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700149 insert_work(cwq, work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_unlock_irqrestore(&cwq->lock, flags);
151}
152
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700153/**
154 * queue_work - queue work on a workqueue
155 * @wq: workqueue to use
156 * @work: work to queue
157 *
Alan Stern057647f2006-10-28 10:38:58 -0700158 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700160 * We queue the work to the CPU on which it was submitted, but if the CPU dies
161 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800163int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700165 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800167 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 BUG_ON(!list_empty(&work->entry));
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700169 __queue_work(wq_per_cpu(wq, get_cpu()), work);
170 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ret = 1;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return ret;
174}
Dave Jonesae90dd52006-06-30 01:40:45 -0400175EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Zhang Ruic1a220e2008-07-23 21:28:39 -0700177/**
178 * queue_work_on - queue work on specific cpu
179 * @cpu: CPU number to execute work on
180 * @wq: workqueue to use
181 * @work: work to queue
182 *
183 * Returns 0 if @work was already on a queue, non-zero otherwise.
184 *
185 * We queue the work to a specific CPU, the caller must ensure it
186 * can't go away.
187 */
188int
189queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
190{
191 int ret = 0;
192
193 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
194 BUG_ON(!list_empty(&work->entry));
195 __queue_work(wq_per_cpu(wq, cpu), work);
196 ret = 1;
197 }
198 return ret;
199}
200EXPORT_SYMBOL_GPL(queue_work_on);
201
Li Zefan6d141c32008-02-08 04:21:09 -0800202static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
David Howells52bad642006-11-22 14:54:01 +0000204 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700205 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
206 struct workqueue_struct *wq = cwq->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700208 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700211/**
212 * queue_delayed_work - queue work on a workqueue after delay
213 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800214 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700215 * @delay: number of jiffies to wait before queueing
216 *
Alan Stern057647f2006-10-28 10:38:58 -0700217 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700218 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800219int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000220 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
David Howells52bad642006-11-22 14:54:01 +0000222 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700223 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700225 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Dave Jonesae90dd52006-06-30 01:40:45 -0400227EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700229/**
230 * queue_delayed_work_on - queue work on specific CPU after delay
231 * @cpu: CPU number to execute work on
232 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800233 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700234 * @delay: number of jiffies to wait before queueing
235 *
Alan Stern057647f2006-10-28 10:38:58 -0700236 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700237 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700238int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000239 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700240{
241 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000242 struct timer_list *timer = &dwork->timer;
243 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700244
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800245 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700246 BUG_ON(timer_pending(timer));
247 BUG_ON(!list_empty(&work->entry));
248
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700249 timer_stats_timer_set_start_info(&dwork->timer);
250
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700251 /* This stores cwq for the moment, for the timer_fn */
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700252 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700253 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000254 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700255 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700256
257 if (unlikely(cpu >= 0))
258 add_timer_on(timer, cpu);
259 else
260 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700261 ret = 1;
262 }
263 return ret;
264}
Dave Jonesae90dd52006-06-30 01:40:45 -0400265EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Arjan van de Ven858119e2006-01-14 13:20:43 -0800267static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700269 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cwq->run_depth++;
271 if (cwq->run_depth > 3) {
272 /* morton gets to eat his hat */
273 printk("%s: recursion depth exceeded: %d\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700274 __func__, cwq->run_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dump_stack();
276 }
277 while (!list_empty(&cwq->worklist)) {
278 struct work_struct *work = list_entry(cwq->worklist.next,
279 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000280 work_func_t f = work->func;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700281#ifdef CONFIG_LOCKDEP
282 /*
283 * It is permissible to free the struct work_struct
284 * from inside the function that is called from it,
285 * this we need to take into account for lockdep too.
286 * To avoid bogus "held lock freed" warnings as well
287 * as problems when looking into work->lockdep_map,
288 * make a copy and use that here.
289 */
290 struct lockdep_map lockdep_map = work->lockdep_map;
291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700293 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700295 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
David Howells365970a2006-11-22 14:54:49 +0000297 BUG_ON(get_wq_data(work) != cwq);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700298 work_clear_pending(work);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700299 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
300 lock_acquire(&lockdep_map, 0, 0, 0, 2, _THIS_IP_);
David Howells65f27f32006-11-22 14:55:48 +0000301 f(work);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700302 lock_release(&lockdep_map, 1, _THIS_IP_);
303 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800305 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
306 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
307 "%s/0x%08x/%d\n",
308 current->comm, preempt_count(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700309 task_pid_nr(current));
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800310 printk(KERN_ERR " last function: ");
311 print_symbol("%s\n", (unsigned long)f);
312 debug_show_held_locks(current);
313 dump_stack();
314 }
315
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700316 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700317 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 cwq->run_depth--;
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700320 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static int worker_thread(void *__cwq)
324{
325 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700326 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700328 if (cwq->wq->freezeable)
329 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 set_user_nice(current, -5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Oleg Nesterov3af244332007-05-09 02:34:09 -0700333 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700334 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700335 if (!freezing(current) &&
336 !kthread_should_stop() &&
337 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700339 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Oleg Nesterov85f41862007-05-09 02:34:20 -0700341 try_to_freeze();
342
Oleg Nesterov14441962007-05-23 13:57:57 -0700343 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700344 break;
345
346 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700352struct wq_barrier {
353 struct work_struct work;
354 struct completion done;
355};
356
357static void wq_barrier_func(struct work_struct *work)
358{
359 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
360 complete(&barr->done);
361}
362
Oleg Nesterov83c22522007-05-09 02:33:54 -0700363static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
364 struct wq_barrier *barr, int tail)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700365{
366 INIT_WORK(&barr->work, wq_barrier_func);
367 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
368
369 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700370
371 insert_work(cwq, &barr->work, tail);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700372}
373
Oleg Nesterov14441962007-05-23 13:57:57 -0700374static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Oleg Nesterov14441962007-05-23 13:57:57 -0700376 int active;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (cwq->thread == current) {
379 /*
380 * Probably keventd trying to flush its own queue. So simply run
381 * it by hand rather than deadlocking.
382 */
383 run_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700384 active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700386 struct wq_barrier barr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Oleg Nesterov14441962007-05-23 13:57:57 -0700388 active = 0;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700389 spin_lock_irq(&cwq->lock);
390 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
391 insert_wq_barrier(cwq, &barr, 1);
392 active = 1;
393 }
394 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Oleg Nesterovd7213042007-05-09 02:34:07 -0700396 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700397 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700399
400 return active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700403/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700405 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *
407 * Forces execution of the workqueue and blocks until its completion.
408 * This is typically used in driver shutdown handlers.
409 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700410 * We sleep until all works which were queued on entry have been handled,
411 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 *
413 * This function used to run the workqueues itself. Now we just wait for the
414 * helper threads to do it.
415 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800416void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700418 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700419 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700420
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700421 might_sleep();
Johannes Berg4e6045f2007-10-18 23:39:55 -0700422 lock_acquire(&wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
423 lock_release(&wq->lockdep_map, 1, _THIS_IP_);
Mike Travis363ab6f2008-05-12 21:21:13 +0200424 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700425 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
Dave Jonesae90dd52006-06-30 01:40:45 -0400427EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700429/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700430 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700431 * so this work can't be re-armed in any way.
432 */
433static int try_to_grab_pending(struct work_struct *work)
434{
435 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700436 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700437
438 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700439 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700440
441 /*
442 * The queueing is in progress, or it is already queued. Try to
443 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
444 */
445
446 cwq = get_wq_data(work);
447 if (!cwq)
448 return ret;
449
450 spin_lock_irq(&cwq->lock);
451 if (!list_empty(&work->entry)) {
452 /*
453 * This work is queued, but perhaps we locked the wrong cwq.
454 * In that case we must see the new value after rmb(), see
455 * insert_work()->wmb().
456 */
457 smp_rmb();
458 if (cwq == get_wq_data(work)) {
459 list_del_init(&work->entry);
460 ret = 1;
461 }
462 }
463 spin_unlock_irq(&cwq->lock);
464
465 return ret;
466}
467
468static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700469 struct work_struct *work)
470{
471 struct wq_barrier barr;
472 int running = 0;
473
474 spin_lock_irq(&cwq->lock);
475 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov83c22522007-05-09 02:33:54 -0700476 insert_wq_barrier(cwq, &barr, 0);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700477 running = 1;
478 }
479 spin_unlock_irq(&cwq->lock);
480
Oleg Nesterov3af244332007-05-09 02:34:09 -0700481 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700482 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700483}
484
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700485static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700486{
487 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700488 struct workqueue_struct *wq;
489 const cpumask_t *cpu_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700490 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700491
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700492 might_sleep();
493
Johannes Berg4e6045f2007-10-18 23:39:55 -0700494 lock_acquire(&work->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
495 lock_release(&work->lockdep_map, 1, _THIS_IP_);
496
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700497 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700498 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700499 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700500
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700501 wq = cwq->wq;
502 cpu_map = wq_cpu_map(wq);
503
Mike Travis363ab6f2008-05-12 21:21:13 +0200504 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700505 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
506}
507
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700508static int __cancel_work_timer(struct work_struct *work,
509 struct timer_list* timer)
510{
511 int ret;
512
513 do {
514 ret = (timer && likely(del_timer(timer)));
515 if (!ret)
516 ret = try_to_grab_pending(work);
517 wait_on_work(work);
518 } while (unlikely(ret < 0));
519
520 work_clear_pending(work);
521 return ret;
522}
523
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700524/**
525 * cancel_work_sync - block until a work_struct's callback has terminated
526 * @work: the work which is to be flushed
527 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700528 * Returns true if @work was pending.
529 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700530 * cancel_work_sync() will cancel the work if it is queued. If the work's
531 * callback appears to be running, cancel_work_sync() will block until it
532 * has completed.
533 *
534 * It is possible to use this function if the work re-queues itself. It can
535 * cancel the work even if it migrates to another workqueue, however in that
536 * case it only guarantees that work->func() has completed on the last queued
537 * workqueue.
538 *
539 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
540 * pending, otherwise it goes into a busy-wait loop until the timer expires.
541 *
542 * The caller must ensure that workqueue_struct on which this work was last
543 * queued can't be destroyed before this function returns.
544 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700545int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700546{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700547 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700548}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700549EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700550
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700551/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700552 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700553 * @dwork: the delayed work struct
554 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700555 * Returns true if @dwork was pending.
556 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700557 * It is possible to use this function if @dwork rearms itself via queue_work()
558 * or queue_delayed_work(). See also the comment for cancel_work_sync().
559 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700560int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700561{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700562 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700563}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700564EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700566static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700568/**
569 * schedule_work - put work task in global workqueue
570 * @work: job to be done
571 *
572 * This puts a job in the kernel-global workqueue.
573 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800574int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 return queue_work(keventd_wq, work);
577}
Dave Jonesae90dd52006-06-30 01:40:45 -0400578EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Zhang Ruic1a220e2008-07-23 21:28:39 -0700580/*
581 * schedule_work_on - put work task on a specific cpu
582 * @cpu: cpu to put the work task on
583 * @work: job to be done
584 *
585 * This puts a job on a specific cpu
586 */
587int schedule_work_on(int cpu, struct work_struct *work)
588{
589 return queue_work_on(cpu, keventd_wq, work);
590}
591EXPORT_SYMBOL(schedule_work_on);
592
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700593/**
594 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000595 * @dwork: job to be done
596 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700597 *
598 * After waiting for a given time this puts a job in the kernel-global
599 * workqueue.
600 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800601int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800602 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
David Howells52bad642006-11-22 14:54:01 +0000604 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
Dave Jonesae90dd52006-06-30 01:40:45 -0400606EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700608/**
609 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
610 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000611 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700612 * @delay: number of jiffies to wait
613 *
614 * After waiting for a given time this puts a job in the kernel-global
615 * workqueue on the specified CPU.
616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000618 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
David Howells52bad642006-11-22 14:54:01 +0000620 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Dave Jonesae90dd52006-06-30 01:40:45 -0400622EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Andrew Mortonb6136772006-06-25 05:47:49 -0700624/**
625 * schedule_on_each_cpu - call a function on each online CPU from keventd
626 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700627 *
628 * Returns zero on success.
629 * Returns -ve errno on failure.
630 *
Andrew Mortonb6136772006-06-25 05:47:49 -0700631 * schedule_on_each_cpu() is very slow.
632 */
David Howells65f27f32006-11-22 14:55:48 +0000633int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800634{
635 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700636 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800637
Andrew Mortonb6136772006-06-25 05:47:49 -0700638 works = alloc_percpu(struct work_struct);
639 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800640 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700641
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100642 get_online_cpus();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800643 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100644 struct work_struct *work = per_cpu_ptr(works, cpu);
645
646 INIT_WORK(work, func);
647 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
648 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800649 }
650 flush_workqueue(keventd_wq);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100651 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -0700652 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800653 return 0;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656void flush_scheduled_work(void)
657{
658 flush_workqueue(keventd_wq);
659}
Dave Jonesae90dd52006-06-30 01:40:45 -0400660EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662/**
James Bottomley1fa44ec2006-02-23 12:43:43 -0600663 * 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;
Hugh Dickinsd2437692007-08-27 16:06:19 +0100696 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 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;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700741
742 return 0;
743}
744
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700745static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
746{
747 struct task_struct *p = cwq->thread;
748
749 if (p != NULL) {
750 if (cpu >= 0)
751 kthread_bind(p, cpu);
752 wake_up_process(p);
753 }
754}
755
Johannes Berg4e6045f2007-10-18 23:39:55 -0700756struct workqueue_struct *__create_workqueue_key(const char *name,
757 int singlethread,
758 int freezeable,
Johannes Bergeb13ba82008-01-16 09:51:58 +0100759 struct lock_class_key *key,
760 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700761{
762 struct workqueue_struct *wq;
763 struct cpu_workqueue_struct *cwq;
764 int err = 0, cpu;
765
766 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
767 if (!wq)
768 return NULL;
769
770 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
771 if (!wq->cpu_wq) {
772 kfree(wq);
773 return NULL;
774 }
775
776 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +0100777 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700778 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700779 wq->freezeable = freezeable;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700780 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700781
782 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700783 cwq = init_cpu_workqueue(wq, singlethread_cpu);
784 err = create_workqueue_thread(cwq, singlethread_cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700785 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700786 } else {
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100787 get_online_cpus();
788 spin_lock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700789 list_add(&wq->list, &workqueues);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100790 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700791
792 for_each_possible_cpu(cpu) {
793 cwq = init_cpu_workqueue(wq, cpu);
794 if (err || !cpu_online(cpu))
795 continue;
796 err = create_workqueue_thread(cwq, cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700797 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700798 }
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100799 put_online_cpus();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700800 }
801
802 if (err) {
803 destroy_workqueue(wq);
804 wq = NULL;
805 }
806 return wq;
807}
Johannes Berg4e6045f2007-10-18 23:39:55 -0700808EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700809
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700810static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700811{
Oleg Nesterov14441962007-05-23 13:57:57 -0700812 /*
813 * Our caller is either destroy_workqueue() or CPU_DEAD,
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100814 * get_online_cpus() protects cwq->thread.
Oleg Nesterov14441962007-05-23 13:57:57 -0700815 */
816 if (cwq->thread == NULL)
817 return;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700818
Johannes Berg4e6045f2007-10-18 23:39:55 -0700819 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
820 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
821
Oleg Nesterov13c22162007-07-17 04:03:55 -0700822 flush_cpu_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700823 /*
Oleg Nesterov13c22162007-07-17 04:03:55 -0700824 * If the caller is CPU_DEAD and cwq->worklist was not empty,
825 * a concurrent flush_workqueue() can insert a barrier after us.
826 * However, in that case run_workqueue() won't return and check
827 * kthread_should_stop() until it flushes all work_struct's.
Oleg Nesterov14441962007-05-23 13:57:57 -0700828 * When ->worklist becomes empty it is safe to exit because no
829 * more work_structs can be queued on this cwq: flush_workqueue
830 * checks list_empty(), and a "normal" queue_work() can't use
831 * a dead CPU.
832 */
Oleg Nesterov14441962007-05-23 13:57:57 -0700833 kthread_stop(cwq->thread);
834 cwq->thread = NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700835}
836
837/**
838 * destroy_workqueue - safely terminate a workqueue
839 * @wq: target workqueue
840 *
841 * Safely destroy a workqueue. All work currently pending will be done first.
842 */
843void destroy_workqueue(struct workqueue_struct *wq)
844{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700845 const cpumask_t *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700846 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700847
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100848 get_online_cpus();
849 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700850 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100851 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700852
Mike Travis363ab6f2008-05-12 21:21:13 +0200853 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700854 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700855 put_online_cpus();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700856
857 free_percpu(wq->cpu_wq);
858 kfree(wq);
859}
860EXPORT_SYMBOL_GPL(destroy_workqueue);
861
862static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
863 unsigned long action,
864 void *hcpu)
865{
866 unsigned int cpu = (unsigned long)hcpu;
867 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct workqueue_struct *wq;
869
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700870 action &= ~CPU_TASKS_FROZEN;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 switch (action) {
873 case CPU_UP_PREPARE:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700874 cpu_set(cpu, cpu_populated_map);
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Oleg Nesterov3af244332007-05-09 02:34:09 -0700877 list_for_each_entry(wq, &workqueues, list) {
878 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800879
Oleg Nesterov3af244332007-05-09 02:34:09 -0700880 switch (action) {
881 case CPU_UP_PREPARE:
882 if (!create_workqueue_thread(cwq, cpu))
883 break;
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100884 printk(KERN_ERR "workqueue [%s] for %i failed\n",
885 wq->name, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700886 return NOTIFY_BAD;
887
888 case CPU_ONLINE:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700889 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700890 break;
891
892 case CPU_UP_CANCELED:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700893 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700894 case CPU_DEAD:
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700895 cleanup_workqueue_thread(cwq);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700900 switch (action) {
901 case CPU_UP_CANCELED:
902 case CPU_DEAD:
903 cpu_clear(cpu, cpu_populated_map);
904 }
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return NOTIFY_OK;
907}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Oleg Nesterovc12920d2007-05-09 02:34:14 -0700909void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700911 cpu_populated_map = cpu_online_map;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800912 singlethread_cpu = first_cpu(cpu_possible_map);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700913 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hotcpu_notifier(workqueue_cpu_callback, 0);
915 keventd_wq = create_workqueue("events");
916 BUG_ON(!keventd_wq);
917}