blob: e53ee18ef43199205ca3bffebc30614ff49fedb8 [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>
Francois Camie1f8e872008-10-15 22:01:59 -070012 * Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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>
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010036#include <trace/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080039 * The per-CPU workqueue (if single thread, we always use the first
40 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42struct cpu_workqueue_struct {
43
44 spinlock_t lock;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct list_head worklist;
47 wait_queue_head_t more_work;
Oleg Nesterov3af244332007-05-09 02:34:09 -070048 struct work_struct *current_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 struct workqueue_struct *wq;
Ingo Molnar36c8b582006-07-03 00:25:41 -070051 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 int run_depth; /* Detect run_workqueue() recursion depth */
54} ____cacheline_aligned;
55
56/*
57 * The externally visible workqueue abstraction is an array of
58 * per-CPU workqueues:
59 */
60struct workqueue_struct {
Christoph Lameter89ada672005-10-30 15:01:59 -080061 struct cpu_workqueue_struct *cpu_wq;
Oleg Nesterovcce1a162007-05-09 02:34:13 -070062 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 const char *name;
Oleg Nesterovcce1a162007-05-09 02:34:13 -070064 int singlethread;
Oleg Nesterov319c2a92007-05-09 02:34:06 -070065 int freezeable; /* Freeze threads during suspend */
Heiko Carstens0d557dc2008-10-13 23:50:09 +020066 int rt;
Johannes Berg4e6045f2007-10-18 23:39:55 -070067#ifdef CONFIG_LOCKDEP
68 struct lockdep_map lockdep_map;
69#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Gautham R Shenoy95402b32008-01-25 21:08:02 +010072/* Serializes the accesses to the list of workqueues. */
73static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static LIST_HEAD(workqueues);
75
Oleg Nesterov3af244332007-05-09 02:34:09 -070076static int singlethread_cpu __read_mostly;
Rusty Russelle7577c52009-01-01 10:12:25 +103077static const struct cpumask *cpu_singlethread_map __read_mostly;
Oleg Nesterov14441962007-05-23 13:57:57 -070078/*
79 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
80 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
81 * which comes in between can't use for_each_online_cpu(). We could
82 * use cpu_possible_map, the cpumask below is more a documentation
83 * than optimization.
84 */
Rusty Russelle7577c52009-01-01 10:12:25 +103085static cpumask_var_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -080086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* If it's single threaded, it isn't in the list of workqueues. */
David Howells6cc88bc2008-11-14 10:39:21 +110088static inline int is_wq_single_threaded(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Oleg Nesterovcce1a162007-05-09 02:34:13 -070090 return wq->singlethread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Rusty Russelle7577c52009-01-01 10:12:25 +103093static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070094{
David Howells6cc88bc2008-11-14 10:39:21 +110095 return is_wq_single_threaded(wq)
Rusty Russelle7577c52009-01-01 10:12:25 +103096 ? cpu_singlethread_map : cpu_populated_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -070097}
98
Oleg Nesterova848e3b2007-05-09 02:34:17 -070099static
100struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
101{
David Howells6cc88bc2008-11-14 10:39:21 +1100102 if (unlikely(is_wq_single_threaded(wq)))
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700103 cpu = singlethread_cpu;
104 return per_cpu_ptr(wq->cpu_wq, cpu);
105}
106
David Howells4594bf12006-12-07 11:33:26 +0000107/*
108 * Set the workqueue on which a work item is to be run
109 * - Must *only* be called if the pending flag is set
110 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700111static inline void set_wq_data(struct work_struct *work,
112 struct cpu_workqueue_struct *cwq)
David Howells365970a2006-11-22 14:54:49 +0000113{
David Howells4594bf12006-12-07 11:33:26 +0000114 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +0000115
David Howells4594bf12006-12-07 11:33:26 +0000116 BUG_ON(!work_pending(work));
117
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700118 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800119 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
120 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000121}
122
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700123static inline
124struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000125{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800126 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000127}
128
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100129DEFINE_TRACE(workqueue_insertion);
130
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700131static void insert_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700132 struct work_struct *work, struct list_head *head)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700133{
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100134 trace_workqueue_insertion(cwq->thread, work);
135
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700136 set_wq_data(work, cwq);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700137 /*
138 * Ensure that we get the right work->data if we see the
139 * result of list_add() below, see try_to_grab_pending().
140 */
141 smp_wmb();
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700142 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700143 wake_up(&cwq->more_work);
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static void __queue_work(struct cpu_workqueue_struct *cwq,
147 struct work_struct *work)
148{
149 unsigned long flags;
150
151 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700152 insert_work(cwq, work, &cwq->worklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 spin_unlock_irqrestore(&cwq->lock, flags);
154}
155
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700156/**
157 * queue_work - queue work on a workqueue
158 * @wq: workqueue to use
159 * @work: work to queue
160 *
Alan Stern057647f2006-10-28 10:38:58 -0700161 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700163 * We queue the work to the CPU on which it was submitted, but if the CPU dies
164 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800166int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700168 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700170 ret = queue_work_on(get_cpu(), wq, work);
171 put_cpu();
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
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100267DEFINE_TRACE(workqueue_execution);
268
Arjan van de Ven858119e2006-01-14 13:20:43 -0800269static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700271 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 cwq->run_depth++;
273 if (cwq->run_depth > 3) {
274 /* morton gets to eat his hat */
275 printk("%s: recursion depth exceeded: %d\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700276 __func__, cwq->run_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dump_stack();
278 }
279 while (!list_empty(&cwq->worklist)) {
280 struct work_struct *work = list_entry(cwq->worklist.next,
281 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000282 work_func_t f = work->func;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700283#ifdef CONFIG_LOCKDEP
284 /*
285 * It is permissible to free the struct work_struct
286 * from inside the function that is called from it,
287 * this we need to take into account for lockdep too.
288 * To avoid bogus "held lock freed" warnings as well
289 * as problems when looking into work->lockdep_map,
290 * make a copy and use that here.
291 */
292 struct lockdep_map lockdep_map = work->lockdep_map;
293#endif
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100294 trace_workqueue_execution(cwq->thread, work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700295 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700297 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
David Howells365970a2006-11-22 14:54:49 +0000299 BUG_ON(get_wq_data(work) != cwq);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700300 work_clear_pending(work);
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200301 lock_map_acquire(&cwq->wq->lockdep_map);
302 lock_map_acquire(&lockdep_map);
David Howells65f27f32006-11-22 14:55:48 +0000303 f(work);
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200304 lock_map_release(&lockdep_map);
305 lock_map_release(&cwq->wq->lockdep_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800307 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
308 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
309 "%s/0x%08x/%d\n",
310 current->comm, preempt_count(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700311 task_pid_nr(current));
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800312 printk(KERN_ERR " last function: ");
313 print_symbol("%s\n", (unsigned long)f);
314 debug_show_held_locks(current);
315 dump_stack();
316 }
317
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700318 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700319 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 cwq->run_depth--;
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700322 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325static int worker_thread(void *__cwq)
326{
327 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700328 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700330 if (cwq->wq->freezeable)
331 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 set_user_nice(current, -5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Oleg Nesterov3af244332007-05-09 02:34:09 -0700335 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700336 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700337 if (!freezing(current) &&
338 !kthread_should_stop() &&
339 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700341 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Oleg Nesterov85f41862007-05-09 02:34:20 -0700343 try_to_freeze();
344
Oleg Nesterov14441962007-05-23 13:57:57 -0700345 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700346 break;
347
348 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352}
353
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700354struct wq_barrier {
355 struct work_struct work;
356 struct completion done;
357};
358
359static void wq_barrier_func(struct work_struct *work)
360{
361 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
362 complete(&barr->done);
363}
364
Oleg Nesterov83c22522007-05-09 02:33:54 -0700365static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700366 struct wq_barrier *barr, struct list_head *head)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700367{
368 INIT_WORK(&barr->work, wq_barrier_func);
369 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
370
371 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700372
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700373 insert_work(cwq, &barr->work, head);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700374}
375
Oleg Nesterov14441962007-05-23 13:57:57 -0700376static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Oleg Nesterov14441962007-05-23 13:57:57 -0700378 int active;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (cwq->thread == current) {
381 /*
382 * Probably keventd trying to flush its own queue. So simply run
383 * it by hand rather than deadlocking.
384 */
385 run_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700386 active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 } else {
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700388 struct wq_barrier barr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Oleg Nesterov14441962007-05-23 13:57:57 -0700390 active = 0;
Oleg Nesterov83c22522007-05-09 02:33:54 -0700391 spin_lock_irq(&cwq->lock);
392 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700393 insert_wq_barrier(cwq, &barr, &cwq->worklist);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700394 active = 1;
395 }
396 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Oleg Nesterovd7213042007-05-09 02:34:07 -0700398 if (active)
Oleg Nesterov83c22522007-05-09 02:33:54 -0700399 wait_for_completion(&barr.done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700401
402 return active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700405/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700407 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
409 * Forces execution of the workqueue and blocks until its completion.
410 * This is typically used in driver shutdown handlers.
411 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700412 * We sleep until all works which were queued on entry have been handled,
413 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
415 * This function used to run the workqueues itself. Now we just wait for the
416 * helper threads to do it.
417 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800418void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Rusty Russelle7577c52009-01-01 10:12:25 +1030420 const struct cpumask *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700421 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700422
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700423 might_sleep();
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200424 lock_map_acquire(&wq->lockdep_map);
425 lock_map_release(&wq->lockdep_map);
Mike Travis363ab6f2008-05-12 21:21:13 +0200426 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700427 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Dave Jonesae90dd52006-06-30 01:40:45 -0400429EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Oleg Nesterovdb700892008-07-25 01:47:49 -0700431/**
432 * flush_work - block until a work_struct's callback has terminated
433 * @work: the work which is to be flushed
434 *
Oleg Nesterova67da702008-07-25 01:47:52 -0700435 * Returns false if @work has already terminated.
436 *
Oleg Nesterovdb700892008-07-25 01:47:49 -0700437 * It is expected that, prior to calling flush_work(), the caller has
438 * arranged for the work to not be requeued, otherwise it doesn't make
439 * sense to use this function.
440 */
441int flush_work(struct work_struct *work)
442{
443 struct cpu_workqueue_struct *cwq;
444 struct list_head *prev;
445 struct wq_barrier barr;
446
447 might_sleep();
448 cwq = get_wq_data(work);
449 if (!cwq)
450 return 0;
451
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200452 lock_map_acquire(&cwq->wq->lockdep_map);
453 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -0700454
Oleg Nesterovdb700892008-07-25 01:47:49 -0700455 prev = NULL;
456 spin_lock_irq(&cwq->lock);
457 if (!list_empty(&work->entry)) {
458 /*
459 * See the comment near try_to_grab_pending()->smp_rmb().
460 * If it was re-queued under us we are not going to wait.
461 */
462 smp_rmb();
463 if (unlikely(cwq != get_wq_data(work)))
464 goto out;
465 prev = &work->entry;
466 } else {
467 if (cwq->current_work != work)
468 goto out;
469 prev = &cwq->worklist;
470 }
471 insert_wq_barrier(cwq, &barr, prev->next);
472out:
473 spin_unlock_irq(&cwq->lock);
474 if (!prev)
475 return 0;
476
477 wait_for_completion(&barr.done);
478 return 1;
479}
480EXPORT_SYMBOL_GPL(flush_work);
481
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700482/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700483 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700484 * so this work can't be re-armed in any way.
485 */
486static int try_to_grab_pending(struct work_struct *work)
487{
488 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700489 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700490
491 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700492 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700493
494 /*
495 * The queueing is in progress, or it is already queued. Try to
496 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
497 */
498
499 cwq = get_wq_data(work);
500 if (!cwq)
501 return ret;
502
503 spin_lock_irq(&cwq->lock);
504 if (!list_empty(&work->entry)) {
505 /*
506 * This work is queued, but perhaps we locked the wrong cwq.
507 * In that case we must see the new value after rmb(), see
508 * insert_work()->wmb().
509 */
510 smp_rmb();
511 if (cwq == get_wq_data(work)) {
512 list_del_init(&work->entry);
513 ret = 1;
514 }
515 }
516 spin_unlock_irq(&cwq->lock);
517
518 return ret;
519}
520
521static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700522 struct work_struct *work)
523{
524 struct wq_barrier barr;
525 int running = 0;
526
527 spin_lock_irq(&cwq->lock);
528 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700529 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700530 running = 1;
531 }
532 spin_unlock_irq(&cwq->lock);
533
Oleg Nesterov3af244332007-05-09 02:34:09 -0700534 if (unlikely(running))
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700535 wait_for_completion(&barr.done);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700536}
537
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700538static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700539{
540 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700541 struct workqueue_struct *wq;
Rusty Russelle7577c52009-01-01 10:12:25 +1030542 const struct cpumask *cpu_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700543 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700544
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700545 might_sleep();
546
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200547 lock_map_acquire(&work->lockdep_map);
548 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700549
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700550 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700551 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700552 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700553
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700554 wq = cwq->wq;
555 cpu_map = wq_cpu_map(wq);
556
Mike Travis363ab6f2008-05-12 21:21:13 +0200557 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700558 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
559}
560
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700561static int __cancel_work_timer(struct work_struct *work,
562 struct timer_list* timer)
563{
564 int ret;
565
566 do {
567 ret = (timer && likely(del_timer(timer)));
568 if (!ret)
569 ret = try_to_grab_pending(work);
570 wait_on_work(work);
571 } while (unlikely(ret < 0));
572
573 work_clear_pending(work);
574 return ret;
575}
576
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700577/**
578 * cancel_work_sync - block until a work_struct's callback has terminated
579 * @work: the work which is to be flushed
580 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700581 * Returns true if @work was pending.
582 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700583 * cancel_work_sync() will cancel the work if it is queued. If the work's
584 * callback appears to be running, cancel_work_sync() will block until it
585 * has completed.
586 *
587 * It is possible to use this function if the work re-queues itself. It can
588 * cancel the work even if it migrates to another workqueue, however in that
589 * case it only guarantees that work->func() has completed on the last queued
590 * workqueue.
591 *
592 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
593 * pending, otherwise it goes into a busy-wait loop until the timer expires.
594 *
595 * The caller must ensure that workqueue_struct on which this work was last
596 * queued can't be destroyed before this function returns.
597 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700598int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700599{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700600 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700601}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700602EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700603
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700604/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700605 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700606 * @dwork: the delayed work struct
607 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700608 * Returns true if @dwork was pending.
609 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700610 * It is possible to use this function if @dwork rearms itself via queue_work()
611 * or queue_delayed_work(). See also the comment for cancel_work_sync().
612 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700613int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700614{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700615 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700616}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700617EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700619static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700621/**
622 * schedule_work - put work task in global workqueue
623 * @work: job to be done
624 *
625 * This puts a job in the kernel-global workqueue.
626 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800627int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 return queue_work(keventd_wq, work);
630}
Dave Jonesae90dd52006-06-30 01:40:45 -0400631EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Zhang Ruic1a220e2008-07-23 21:28:39 -0700633/*
634 * schedule_work_on - put work task on a specific cpu
635 * @cpu: cpu to put the work task on
636 * @work: job to be done
637 *
638 * This puts a job on a specific cpu
639 */
640int schedule_work_on(int cpu, struct work_struct *work)
641{
642 return queue_work_on(cpu, keventd_wq, work);
643}
644EXPORT_SYMBOL(schedule_work_on);
645
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700646/**
647 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000648 * @dwork: job to be done
649 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700650 *
651 * After waiting for a given time this puts a job in the kernel-global
652 * workqueue.
653 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800654int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800655 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
David Howells52bad642006-11-22 14:54:01 +0000657 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
Dave Jonesae90dd52006-06-30 01:40:45 -0400659EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700661/**
662 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
663 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000664 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700665 * @delay: number of jiffies to wait
666 *
667 * After waiting for a given time this puts a job in the kernel-global
668 * workqueue on the specified CPU.
669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000671 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
David Howells52bad642006-11-22 14:54:01 +0000673 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
Dave Jonesae90dd52006-06-30 01:40:45 -0400675EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Andrew Mortonb6136772006-06-25 05:47:49 -0700677/**
678 * schedule_on_each_cpu - call a function on each online CPU from keventd
679 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700680 *
681 * Returns zero on success.
682 * Returns -ve errno on failure.
683 *
Andrew Mortonb6136772006-06-25 05:47:49 -0700684 * schedule_on_each_cpu() is very slow.
685 */
David Howells65f27f32006-11-22 14:55:48 +0000686int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800687{
688 int cpu;
Andrew Mortonb6136772006-06-25 05:47:49 -0700689 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800690
Andrew Mortonb6136772006-06-25 05:47:49 -0700691 works = alloc_percpu(struct work_struct);
692 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800693 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700694
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100695 get_online_cpus();
Christoph Lameter15316ba2006-01-08 01:00:43 -0800696 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100697 struct work_struct *work = per_cpu_ptr(works, cpu);
698
699 INIT_WORK(work, func);
Oleg Nesterov8de6d302008-07-25 01:47:53 -0700700 schedule_work_on(cpu, work);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800701 }
Oleg Nesterov8616a892008-07-25 01:47:49 -0700702 for_each_online_cpu(cpu)
703 flush_work(per_cpu_ptr(works, cpu));
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100704 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -0700705 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800706 return 0;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709void flush_scheduled_work(void)
710{
711 flush_workqueue(keventd_wq);
712}
Dave Jonesae90dd52006-06-30 01:40:45 -0400713EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715/**
James Bottomley1fa44ec2006-02-23 12:43:43 -0600716 * execute_in_process_context - reliably execute the routine with user context
717 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600718 * @ew: guaranteed storage for the execute work structure (must
719 * be available when the work executes)
720 *
721 * Executes the function immediately if process context is available,
722 * otherwise schedules the function for delayed execution.
723 *
724 * Returns: 0 - function was executed
725 * 1 - function was scheduled for execution
726 */
David Howells65f27f32006-11-22 14:55:48 +0000727int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600728{
729 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000730 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600731 return 0;
732 }
733
David Howells65f27f32006-11-22 14:55:48 +0000734 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600735 schedule_work(&ew->work);
736
737 return 1;
738}
739EXPORT_SYMBOL_GPL(execute_in_process_context);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741int keventd_up(void)
742{
743 return keventd_wq != NULL;
744}
745
746int current_is_keventd(void)
747{
748 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +0100749 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int ret = 0;
751
752 BUG_ON(!keventd_wq);
753
Christoph Lameter89ada672005-10-30 15:01:59 -0800754 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (current == cwq->thread)
756 ret = 1;
757
758 return ret;
759
760}
761
Oleg Nesterov3af244332007-05-09 02:34:09 -0700762static struct cpu_workqueue_struct *
763init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Christoph Lameter89ada672005-10-30 15:01:59 -0800765 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Oleg Nesterov3af244332007-05-09 02:34:09 -0700767 cwq->wq = wq;
768 spin_lock_init(&cwq->lock);
769 INIT_LIST_HEAD(&cwq->worklist);
770 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Oleg Nesterov3af244332007-05-09 02:34:09 -0700772 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100775DEFINE_TRACE(workqueue_creation);
776
Oleg Nesterov3af244332007-05-09 02:34:09 -0700777static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200779 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
Oleg Nesterov3af244332007-05-09 02:34:09 -0700780 struct workqueue_struct *wq = cwq->wq;
David Howells6cc88bc2008-11-14 10:39:21 +1100781 const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
Oleg Nesterov3af244332007-05-09 02:34:09 -0700782 struct task_struct *p;
783
784 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
785 /*
786 * Nobody can add the work_struct to this cwq,
787 * if (caller is __create_workqueue)
788 * nobody should see this wq
789 * else // caller is CPU_UP_PREPARE
790 * cpu is not on cpu_online_map
791 * so we can abort safely.
792 */
793 if (IS_ERR(p))
794 return PTR_ERR(p);
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200795 if (cwq->wq->rt)
796 sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700797 cwq->thread = p;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700798
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100799 trace_workqueue_creation(cwq->thread, cpu);
800
Oleg Nesterov3af244332007-05-09 02:34:09 -0700801 return 0;
802}
803
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700804static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
805{
806 struct task_struct *p = cwq->thread;
807
808 if (p != NULL) {
809 if (cpu >= 0)
810 kthread_bind(p, cpu);
811 wake_up_process(p);
812 }
813}
814
Johannes Berg4e6045f2007-10-18 23:39:55 -0700815struct workqueue_struct *__create_workqueue_key(const char *name,
816 int singlethread,
817 int freezeable,
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200818 int rt,
Johannes Bergeb13ba82008-01-16 09:51:58 +0100819 struct lock_class_key *key,
820 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700821{
822 struct workqueue_struct *wq;
823 struct cpu_workqueue_struct *cwq;
824 int err = 0, cpu;
825
826 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
827 if (!wq)
828 return NULL;
829
830 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
831 if (!wq->cpu_wq) {
832 kfree(wq);
833 return NULL;
834 }
835
836 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +0100837 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700838 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700839 wq->freezeable = freezeable;
Heiko Carstens0d557dc2008-10-13 23:50:09 +0200840 wq->rt = rt;
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700841 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700842
843 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700844 cwq = init_cpu_workqueue(wq, singlethread_cpu);
845 err = create_workqueue_thread(cwq, singlethread_cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700846 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700847 } else {
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700848 cpu_maps_update_begin();
Oleg Nesterov6af8bf32008-07-29 22:33:49 -0700849 /*
850 * We must place this wq on list even if the code below fails.
851 * cpu_down(cpu) can remove cpu from cpu_populated_map before
852 * destroy_workqueue() takes the lock, in that case we leak
853 * cwq[cpu]->thread.
854 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100855 spin_lock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700856 list_add(&wq->list, &workqueues);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100857 spin_unlock(&workqueue_lock);
Oleg Nesterov6af8bf32008-07-29 22:33:49 -0700858 /*
859 * We must initialize cwqs for each possible cpu even if we
860 * are going to call destroy_workqueue() finally. Otherwise
861 * cpu_up() can hit the uninitialized cwq once we drop the
862 * lock.
863 */
Oleg Nesterov3af244332007-05-09 02:34:09 -0700864 for_each_possible_cpu(cpu) {
865 cwq = init_cpu_workqueue(wq, cpu);
866 if (err || !cpu_online(cpu))
867 continue;
868 err = create_workqueue_thread(cwq, cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700869 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700870 }
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700871 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700872 }
873
874 if (err) {
875 destroy_workqueue(wq);
876 wq = NULL;
877 }
878 return wq;
879}
Johannes Berg4e6045f2007-10-18 23:39:55 -0700880EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700881
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100882DEFINE_TRACE(workqueue_destruction);
883
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700884static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700885{
Oleg Nesterov14441962007-05-23 13:57:57 -0700886 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700887 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
888 * cpu_add_remove_lock protects cwq->thread.
Oleg Nesterov14441962007-05-23 13:57:57 -0700889 */
890 if (cwq->thread == NULL)
891 return;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700892
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200893 lock_map_acquire(&cwq->wq->lockdep_map);
894 lock_map_release(&cwq->wq->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700895
Oleg Nesterov13c22162007-07-17 04:03:55 -0700896 flush_cpu_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -0700897 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700898 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
Oleg Nesterov13c22162007-07-17 04:03:55 -0700899 * a concurrent flush_workqueue() can insert a barrier after us.
900 * However, in that case run_workqueue() won't return and check
901 * kthread_should_stop() until it flushes all work_struct's.
Oleg Nesterov14441962007-05-23 13:57:57 -0700902 * When ->worklist becomes empty it is safe to exit because no
903 * more work_structs can be queued on this cwq: flush_workqueue
904 * checks list_empty(), and a "normal" queue_work() can't use
905 * a dead CPU.
906 */
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100907 trace_workqueue_destruction(cwq->thread);
Oleg Nesterov14441962007-05-23 13:57:57 -0700908 kthread_stop(cwq->thread);
909 cwq->thread = NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700910}
911
912/**
913 * destroy_workqueue - safely terminate a workqueue
914 * @wq: target workqueue
915 *
916 * Safely destroy a workqueue. All work currently pending will be done first.
917 */
918void destroy_workqueue(struct workqueue_struct *wq)
919{
Rusty Russelle7577c52009-01-01 10:12:25 +1030920 const struct cpumask *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700921 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700922
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700923 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100924 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700925 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100926 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700927
Mike Travis363ab6f2008-05-12 21:21:13 +0200928 for_each_cpu_mask_nr(cpu, *cpu_map)
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700929 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700930 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700931
932 free_percpu(wq->cpu_wq);
933 kfree(wq);
934}
935EXPORT_SYMBOL_GPL(destroy_workqueue);
936
937static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
938 unsigned long action,
939 void *hcpu)
940{
941 unsigned int cpu = (unsigned long)hcpu;
942 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct workqueue_struct *wq;
Oleg Nesterov84485022008-07-25 01:47:54 -0700944 int ret = NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700946 action &= ~CPU_TASKS_FROZEN;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 switch (action) {
949 case CPU_UP_PREPARE:
Rusty Russelle7577c52009-01-01 10:12:25 +1030950 cpumask_set_cpu(cpu, cpu_populated_map);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700951 }
Oleg Nesterov84485022008-07-25 01:47:54 -0700952undo:
Oleg Nesterov3af244332007-05-09 02:34:09 -0700953 list_for_each_entry(wq, &workqueues, list) {
954 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -0800955
Oleg Nesterov3af244332007-05-09 02:34:09 -0700956 switch (action) {
957 case CPU_UP_PREPARE:
958 if (!create_workqueue_thread(cwq, cpu))
959 break;
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100960 printk(KERN_ERR "workqueue [%s] for %i failed\n",
961 wq->name, cpu);
Oleg Nesterov84485022008-07-25 01:47:54 -0700962 action = CPU_UP_CANCELED;
963 ret = NOTIFY_BAD;
964 goto undo;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700965
966 case CPU_ONLINE:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700967 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700968 break;
969
970 case CPU_UP_CANCELED:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700971 start_workqueue_thread(cwq, -1);
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700972 case CPU_POST_DEAD:
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -0700973 cleanup_workqueue_thread(cwq);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700978 switch (action) {
979 case CPU_UP_CANCELED:
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700980 case CPU_POST_DEAD:
Rusty Russelle7577c52009-01-01 10:12:25 +1030981 cpumask_clear_cpu(cpu, cpu_populated_map);
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700982 }
983
Oleg Nesterov84485022008-07-25 01:47:54 -0700984 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Rusty Russell2d3854a2008-11-05 13:39:10 +1100987#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -0800988static struct workqueue_struct *work_on_cpu_wq __read_mostly;
989
Rusty Russell2d3854a2008-11-05 13:39:10 +1100990struct work_for_cpu {
991 struct work_struct work;
992 long (*fn)(void *);
993 void *arg;
994 long ret;
995};
996
997static void do_work_for_cpu(struct work_struct *w)
998{
999 struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
1000
1001 wfc->ret = wfc->fn(wfc->arg);
1002}
1003
1004/**
1005 * work_on_cpu - run a function in user context on a particular cpu
1006 * @cpu: the cpu to run on
1007 * @fn: the function to run
1008 * @arg: the function arg
1009 *
Rusty Russell31ad9082009-01-16 15:31:15 -08001010 * This will return the value @fn returns.
1011 * It is up to the caller to ensure that the cpu doesn't go offline.
Rusty Russell2d3854a2008-11-05 13:39:10 +11001012 */
1013long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1014{
1015 struct work_for_cpu wfc;
1016
1017 INIT_WORK(&wfc.work, do_work_for_cpu);
1018 wfc.fn = fn;
1019 wfc.arg = arg;
Rusty Russell8ccad402009-01-16 15:31:15 -08001020 queue_work_on(cpu, work_on_cpu_wq, &wfc.work);
Rusty Russell31ad9082009-01-16 15:31:15 -08001021 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001022
1023 return wfc.ret;
1024}
1025EXPORT_SYMBOL_GPL(work_on_cpu);
1026#endif /* CONFIG_SMP */
1027
Oleg Nesterovc12920d2007-05-09 02:34:14 -07001028void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Rusty Russelle7577c52009-01-01 10:12:25 +10301030 alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1031
1032 cpumask_copy(cpu_populated_map, cpu_online_mask);
1033 singlethread_cpu = cpumask_first(cpu_possible_mask);
1034 cpu_singlethread_map = cpumask_of(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 hotcpu_notifier(workqueue_cpu_callback, 0);
1036 keventd_wq = create_workqueue("events");
1037 BUG_ON(!keventd_wq);
Rusty Russell8ccad402009-01-16 15:31:15 -08001038#ifdef CONFIG_SMP
1039 work_on_cpu_wq = create_workqueue("work_on_cpu");
1040 BUG_ON(!work_on_cpu_wq);
1041#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}