blob: 1a47fbf92faeba2a5ae30b5a1e1f4d6d6023834c [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>
Zhaoleifb391252009-04-17 15:15:51 +080036#define CREATE_TRACE_POINTS
37#include <trace/events/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080040 * The per-CPU workqueue (if single thread, we always use the first
41 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
43struct cpu_workqueue_struct {
44
45 spinlock_t lock;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct list_head worklist;
48 wait_queue_head_t more_work;
Oleg Nesterov3af244332007-05-09 02:34:09 -070049 struct work_struct *current_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 struct workqueue_struct *wq;
Ingo Molnar36c8b582006-07-03 00:25:41 -070052 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053} ____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
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +090070#ifdef CONFIG_DEBUG_OBJECTS_WORK
71
72static struct debug_obj_descr work_debug_descr;
73
74/*
75 * fixup_init is called when:
76 * - an active object is initialized
77 */
78static int work_fixup_init(void *addr, enum debug_obj_state state)
79{
80 struct work_struct *work = addr;
81
82 switch (state) {
83 case ODEBUG_STATE_ACTIVE:
84 cancel_work_sync(work);
85 debug_object_init(work, &work_debug_descr);
86 return 1;
87 default:
88 return 0;
89 }
90}
91
92/*
93 * fixup_activate is called when:
94 * - an active object is activated
95 * - an unknown object is activated (might be a statically initialized object)
96 */
97static int work_fixup_activate(void *addr, enum debug_obj_state state)
98{
99 struct work_struct *work = addr;
100
101 switch (state) {
102
103 case ODEBUG_STATE_NOTAVAILABLE:
104 /*
105 * This is not really a fixup. The work struct was
106 * statically initialized. We just make sure that it
107 * is tracked in the object tracker.
108 */
109 if (test_bit(WORK_STRUCT_STATIC, work_data_bits(work))) {
110 debug_object_init(work, &work_debug_descr);
111 debug_object_activate(work, &work_debug_descr);
112 return 0;
113 }
114 WARN_ON_ONCE(1);
115 return 0;
116
117 case ODEBUG_STATE_ACTIVE:
118 WARN_ON(1);
119
120 default:
121 return 0;
122 }
123}
124
125/*
126 * fixup_free is called when:
127 * - an active object is freed
128 */
129static int work_fixup_free(void *addr, enum debug_obj_state state)
130{
131 struct work_struct *work = addr;
132
133 switch (state) {
134 case ODEBUG_STATE_ACTIVE:
135 cancel_work_sync(work);
136 debug_object_free(work, &work_debug_descr);
137 return 1;
138 default:
139 return 0;
140 }
141}
142
143static struct debug_obj_descr work_debug_descr = {
144 .name = "work_struct",
145 .fixup_init = work_fixup_init,
146 .fixup_activate = work_fixup_activate,
147 .fixup_free = work_fixup_free,
148};
149
150static inline void debug_work_activate(struct work_struct *work)
151{
152 debug_object_activate(work, &work_debug_descr);
153}
154
155static inline void debug_work_deactivate(struct work_struct *work)
156{
157 debug_object_deactivate(work, &work_debug_descr);
158}
159
160void __init_work(struct work_struct *work, int onstack)
161{
162 if (onstack)
163 debug_object_init_on_stack(work, &work_debug_descr);
164 else
165 debug_object_init(work, &work_debug_descr);
166}
167EXPORT_SYMBOL_GPL(__init_work);
168
169void destroy_work_on_stack(struct work_struct *work)
170{
171 debug_object_free(work, &work_debug_descr);
172}
173EXPORT_SYMBOL_GPL(destroy_work_on_stack);
174
175#else
176static inline void debug_work_activate(struct work_struct *work) { }
177static inline void debug_work_deactivate(struct work_struct *work) { }
178#endif
179
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100180/* Serializes the accesses to the list of workqueues. */
181static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static LIST_HEAD(workqueues);
183
Oleg Nesterov3af244332007-05-09 02:34:09 -0700184static int singlethread_cpu __read_mostly;
Rusty Russelle7577c52009-01-01 10:12:25 +1030185static const struct cpumask *cpu_singlethread_map __read_mostly;
Oleg Nesterov14441962007-05-23 13:57:57 -0700186/*
187 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
188 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
189 * which comes in between can't use for_each_online_cpu(). We could
190 * use cpu_possible_map, the cpumask below is more a documentation
191 * than optimization.
192 */
Rusty Russelle7577c52009-01-01 10:12:25 +1030193static cpumask_var_t cpu_populated_map __read_mostly;
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* If it's single threaded, it isn't in the list of workqueues. */
David Howells6cc88bc2008-11-14 10:39:21 +1100196static inline int is_wq_single_threaded(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700198 return wq->singlethread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Rusty Russelle7577c52009-01-01 10:12:25 +1030201static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700202{
David Howells6cc88bc2008-11-14 10:39:21 +1100203 return is_wq_single_threaded(wq)
Rusty Russelle7577c52009-01-01 10:12:25 +1030204 ? cpu_singlethread_map : cpu_populated_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700205}
206
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700207static
208struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
209{
David Howells6cc88bc2008-11-14 10:39:21 +1100210 if (unlikely(is_wq_single_threaded(wq)))
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700211 cpu = singlethread_cpu;
212 return per_cpu_ptr(wq->cpu_wq, cpu);
213}
214
David Howells4594bf12006-12-07 11:33:26 +0000215/*
216 * Set the workqueue on which a work item is to be run
217 * - Must *only* be called if the pending flag is set
218 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700219static inline void set_wq_data(struct work_struct *work,
220 struct cpu_workqueue_struct *cwq)
David Howells365970a2006-11-22 14:54:49 +0000221{
David Howells4594bf12006-12-07 11:33:26 +0000222 unsigned long new;
David Howells365970a2006-11-22 14:54:49 +0000223
David Howells4594bf12006-12-07 11:33:26 +0000224 BUG_ON(!work_pending(work));
225
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700226 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800227 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
228 atomic_long_set(&work->data, new);
David Howells365970a2006-11-22 14:54:49 +0000229}
230
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200231/*
232 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
233 */
234static inline void clear_wq_data(struct work_struct *work)
235{
236 unsigned long flags = *work_data_bits(work) &
237 (1UL << WORK_STRUCT_STATIC);
238 atomic_long_set(&work->data, flags);
239}
240
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700241static inline
242struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000243{
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800244 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000245}
246
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700247static void insert_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700248 struct work_struct *work, struct list_head *head)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700249{
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100250 trace_workqueue_insertion(cwq->thread, work);
251
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700252 set_wq_data(work, cwq);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700253 /*
254 * Ensure that we get the right work->data if we see the
255 * result of list_add() below, see try_to_grab_pending().
256 */
257 smp_wmb();
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700258 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700259 wake_up(&cwq->more_work);
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262static void __queue_work(struct cpu_workqueue_struct *cwq,
263 struct work_struct *work)
264{
265 unsigned long flags;
266
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900267 debug_work_activate(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_lock_irqsave(&cwq->lock, flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700269 insert_work(cwq, work, &cwq->worklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_unlock_irqrestore(&cwq->lock, flags);
271}
272
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700273/**
274 * queue_work - queue work on a workqueue
275 * @wq: workqueue to use
276 * @work: work to queue
277 *
Alan Stern057647f2006-10-28 10:38:58 -0700278 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700280 * We queue the work to the CPU on which it was submitted, but if the CPU dies
281 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800283int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700285 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700287 ret = queue_work_on(get_cpu(), wq, work);
288 put_cpu();
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
Dave Jonesae90dd52006-06-30 01:40:45 -0400292EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Zhang Ruic1a220e2008-07-23 21:28:39 -0700294/**
295 * queue_work_on - queue work on specific cpu
296 * @cpu: CPU number to execute work on
297 * @wq: workqueue to use
298 * @work: work to queue
299 *
300 * Returns 0 if @work was already on a queue, non-zero otherwise.
301 *
302 * We queue the work to a specific CPU, the caller must ensure it
303 * can't go away.
304 */
305int
306queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
307{
308 int ret = 0;
309
310 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
311 BUG_ON(!list_empty(&work->entry));
312 __queue_work(wq_per_cpu(wq, cpu), work);
313 ret = 1;
314 }
315 return ret;
316}
317EXPORT_SYMBOL_GPL(queue_work_on);
318
Li Zefan6d141c32008-02-08 04:21:09 -0800319static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
David Howells52bad642006-11-22 14:54:01 +0000321 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700322 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
323 struct workqueue_struct *wq = cwq->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700325 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700328/**
329 * queue_delayed_work - queue work on a workqueue after delay
330 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800331 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700332 * @delay: number of jiffies to wait before queueing
333 *
Alan Stern057647f2006-10-28 10:38:58 -0700334 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700335 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800336int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000337 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David Howells52bad642006-11-22 14:54:01 +0000339 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700340 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700342 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
Dave Jonesae90dd52006-06-30 01:40:45 -0400344EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700346/**
347 * queue_delayed_work_on - queue work on specific CPU after delay
348 * @cpu: CPU number to execute work on
349 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800350 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700351 * @delay: number of jiffies to wait before queueing
352 *
Alan Stern057647f2006-10-28 10:38:58 -0700353 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700354 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700355int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000356 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700357{
358 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000359 struct timer_list *timer = &dwork->timer;
360 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700361
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800362 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700363 BUG_ON(timer_pending(timer));
364 BUG_ON(!list_empty(&work->entry));
365
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700366 timer_stats_timer_set_start_info(&dwork->timer);
367
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700368 /* This stores cwq for the moment, for the timer_fn */
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700369 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700370 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000371 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700372 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700373
374 if (unlikely(cpu >= 0))
375 add_timer_on(timer, cpu);
376 else
377 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700378 ret = 1;
379 }
380 return ret;
381}
Dave Jonesae90dd52006-06-30 01:40:45 -0400382EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Arjan van de Ven858119e2006-01-14 13:20:43 -0800384static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700386 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 while (!list_empty(&cwq->worklist)) {
388 struct work_struct *work = list_entry(cwq->worklist.next,
389 struct work_struct, entry);
David Howells6bb49e52006-11-22 14:54:45 +0000390 work_func_t f = work->func;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700391#ifdef CONFIG_LOCKDEP
392 /*
393 * It is permissible to free the struct work_struct
394 * from inside the function that is called from it,
395 * this we need to take into account for lockdep too.
396 * To avoid bogus "held lock freed" warnings as well
397 * as problems when looking into work->lockdep_map,
398 * make a copy and use that here.
399 */
400 struct lockdep_map lockdep_map = work->lockdep_map;
401#endif
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100402 trace_workqueue_execution(cwq->thread, work);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900403 debug_work_deactivate(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700404 cwq->current_work = work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 list_del_init(cwq->worklist.next);
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700406 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
David Howells365970a2006-11-22 14:54:49 +0000408 BUG_ON(get_wq_data(work) != cwq);
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700409 work_clear_pending(work);
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200410 lock_map_acquire(&cwq->wq->lockdep_map);
411 lock_map_acquire(&lockdep_map);
David Howells65f27f32006-11-22 14:55:48 +0000412 f(work);
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200413 lock_map_release(&lockdep_map);
414 lock_map_release(&cwq->wq->lockdep_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800416 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
417 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
418 "%s/0x%08x/%d\n",
419 current->comm, preempt_count(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700420 task_pid_nr(current));
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800421 printk(KERN_ERR " last function: ");
422 print_symbol("%s\n", (unsigned long)f);
423 debug_show_held_locks(current);
424 dump_stack();
425 }
426
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700427 spin_lock_irq(&cwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700428 cwq->current_work = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700430 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static int worker_thread(void *__cwq)
434{
435 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700436 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700438 if (cwq->wq->freezeable)
439 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Oleg Nesterov3af244332007-05-09 02:34:09 -0700441 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700442 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700443 if (!freezing(current) &&
444 !kthread_should_stop() &&
445 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700447 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Oleg Nesterov85f41862007-05-09 02:34:20 -0700449 try_to_freeze();
450
Oleg Nesterov14441962007-05-23 13:57:57 -0700451 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700452 break;
453
454 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700460struct wq_barrier {
461 struct work_struct work;
462 struct completion done;
463};
464
465static void wq_barrier_func(struct work_struct *work)
466{
467 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
468 complete(&barr->done);
469}
470
Oleg Nesterov83c22522007-05-09 02:33:54 -0700471static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700472 struct wq_barrier *barr, struct list_head *head)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700473{
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900474 /*
475 * debugobject calls are safe here even with cwq->lock locked
476 * as we know for sure that this will not trigger any of the
477 * checks and call back into the fixup functions where we
478 * might deadlock.
479 */
480 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700481 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
482
483 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700484
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900485 debug_work_activate(&barr->work);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700486 insert_work(cwq, &barr->work, head);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700487}
488
Oleg Nesterov14441962007-05-23 13:57:57 -0700489static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Lai Jiangshan2355b702009-04-02 16:58:24 -0700491 int active = 0;
492 struct wq_barrier barr;
Oleg Nesterov14441962007-05-23 13:57:57 -0700493
Lai Jiangshan2355b702009-04-02 16:58:24 -0700494 WARN_ON(cwq->thread == current);
495
496 spin_lock_irq(&cwq->lock);
497 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
498 insert_wq_barrier(cwq, &barr, &cwq->worklist);
Oleg Nesterov14441962007-05-23 13:57:57 -0700499 active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Lai Jiangshan2355b702009-04-02 16:58:24 -0700501 spin_unlock_irq(&cwq->lock);
502
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900503 if (active) {
Lai Jiangshan2355b702009-04-02 16:58:24 -0700504 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900505 destroy_work_on_stack(&barr.work);
506 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700507
508 return active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700511/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700513 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *
515 * Forces execution of the workqueue and blocks until its completion.
516 * This is typically used in driver shutdown handlers.
517 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700518 * We sleep until all works which were queued on entry have been handled,
519 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 *
521 * This function used to run the workqueues itself. Now we just wait for the
522 * helper threads to do it.
523 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800524void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Rusty Russelle7577c52009-01-01 10:12:25 +1030526 const struct cpumask *cpu_map = wq_cpu_map(wq);
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700527 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700528
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700529 might_sleep();
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200530 lock_map_acquire(&wq->lockdep_map);
531 lock_map_release(&wq->lockdep_map);
Rusty Russellaa85ea52009-03-30 22:05:15 -0600532 for_each_cpu(cpu, cpu_map)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700533 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Dave Jonesae90dd52006-06-30 01:40:45 -0400535EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Oleg Nesterovdb700892008-07-25 01:47:49 -0700537/**
538 * flush_work - block until a work_struct's callback has terminated
539 * @work: the work which is to be flushed
540 *
Oleg Nesterova67da702008-07-25 01:47:52 -0700541 * Returns false if @work has already terminated.
542 *
Oleg Nesterovdb700892008-07-25 01:47:49 -0700543 * It is expected that, prior to calling flush_work(), the caller has
544 * arranged for the work to not be requeued, otherwise it doesn't make
545 * sense to use this function.
546 */
547int flush_work(struct work_struct *work)
548{
549 struct cpu_workqueue_struct *cwq;
550 struct list_head *prev;
551 struct wq_barrier barr;
552
553 might_sleep();
554 cwq = get_wq_data(work);
555 if (!cwq)
556 return 0;
557
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200558 lock_map_acquire(&cwq->wq->lockdep_map);
559 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -0700560
Oleg Nesterovdb700892008-07-25 01:47:49 -0700561 prev = NULL;
562 spin_lock_irq(&cwq->lock);
563 if (!list_empty(&work->entry)) {
564 /*
565 * See the comment near try_to_grab_pending()->smp_rmb().
566 * If it was re-queued under us we are not going to wait.
567 */
568 smp_rmb();
569 if (unlikely(cwq != get_wq_data(work)))
570 goto out;
571 prev = &work->entry;
572 } else {
573 if (cwq->current_work != work)
574 goto out;
575 prev = &cwq->worklist;
576 }
577 insert_wq_barrier(cwq, &barr, prev->next);
578out:
579 spin_unlock_irq(&cwq->lock);
580 if (!prev)
581 return 0;
582
583 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900584 destroy_work_on_stack(&barr.work);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700585 return 1;
586}
587EXPORT_SYMBOL_GPL(flush_work);
588
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700589/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700590 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700591 * so this work can't be re-armed in any way.
592 */
593static int try_to_grab_pending(struct work_struct *work)
594{
595 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700596 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700597
598 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700599 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700600
601 /*
602 * The queueing is in progress, or it is already queued. Try to
603 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
604 */
605
606 cwq = get_wq_data(work);
607 if (!cwq)
608 return ret;
609
610 spin_lock_irq(&cwq->lock);
611 if (!list_empty(&work->entry)) {
612 /*
613 * This work is queued, but perhaps we locked the wrong cwq.
614 * In that case we must see the new value after rmb(), see
615 * insert_work()->wmb().
616 */
617 smp_rmb();
618 if (cwq == get_wq_data(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900619 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700620 list_del_init(&work->entry);
621 ret = 1;
622 }
623 }
624 spin_unlock_irq(&cwq->lock);
625
626 return ret;
627}
628
629static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700630 struct work_struct *work)
631{
632 struct wq_barrier barr;
633 int running = 0;
634
635 spin_lock_irq(&cwq->lock);
636 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700637 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700638 running = 1;
639 }
640 spin_unlock_irq(&cwq->lock);
641
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900642 if (unlikely(running)) {
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700643 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900644 destroy_work_on_stack(&barr.work);
645 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700646}
647
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700648static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700649{
650 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700651 struct workqueue_struct *wq;
Rusty Russelle7577c52009-01-01 10:12:25 +1030652 const struct cpumask *cpu_map;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700653 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700654
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700655 might_sleep();
656
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200657 lock_map_acquire(&work->lockdep_map);
658 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700659
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700660 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700661 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700662 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700663
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700664 wq = cwq->wq;
665 cpu_map = wq_cpu_map(wq);
666
Rusty Russellaa85ea52009-03-30 22:05:15 -0600667 for_each_cpu(cpu, cpu_map)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700668 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
669}
670
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700671static int __cancel_work_timer(struct work_struct *work,
672 struct timer_list* timer)
673{
674 int ret;
675
676 do {
677 ret = (timer && likely(del_timer(timer)));
678 if (!ret)
679 ret = try_to_grab_pending(work);
680 wait_on_work(work);
681 } while (unlikely(ret < 0));
682
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200683 clear_wq_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700684 return ret;
685}
686
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700687/**
688 * cancel_work_sync - block until a work_struct's callback has terminated
689 * @work: the work which is to be flushed
690 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700691 * Returns true if @work was pending.
692 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700693 * cancel_work_sync() will cancel the work if it is queued. If the work's
694 * callback appears to be running, cancel_work_sync() will block until it
695 * has completed.
696 *
697 * It is possible to use this function if the work re-queues itself. It can
698 * cancel the work even if it migrates to another workqueue, however in that
699 * case it only guarantees that work->func() has completed on the last queued
700 * workqueue.
701 *
702 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
703 * pending, otherwise it goes into a busy-wait loop until the timer expires.
704 *
705 * The caller must ensure that workqueue_struct on which this work was last
706 * queued can't be destroyed before this function returns.
707 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700708int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700709{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700710 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700711}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700712EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700713
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700714/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700715 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700716 * @dwork: the delayed work struct
717 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700718 * Returns true if @dwork was pending.
719 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700720 * It is possible to use this function if @dwork rearms itself via queue_work()
721 * or queue_delayed_work(). See also the comment for cancel_work_sync().
722 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700723int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700724{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700725 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700726}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700727EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700729static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700731/**
732 * schedule_work - put work task in global workqueue
733 * @work: job to be done
734 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +0200735 * Returns zero if @work was already on the kernel-global workqueue and
736 * non-zero otherwise.
737 *
738 * This puts a job in the kernel-global workqueue if it was not already
739 * queued and leaves it in the same position on the kernel-global
740 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700741 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800742int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 return queue_work(keventd_wq, work);
745}
Dave Jonesae90dd52006-06-30 01:40:45 -0400746EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Zhang Ruic1a220e2008-07-23 21:28:39 -0700748/*
749 * schedule_work_on - put work task on a specific cpu
750 * @cpu: cpu to put the work task on
751 * @work: job to be done
752 *
753 * This puts a job on a specific cpu
754 */
755int schedule_work_on(int cpu, struct work_struct *work)
756{
757 return queue_work_on(cpu, keventd_wq, work);
758}
759EXPORT_SYMBOL(schedule_work_on);
760
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700761/**
762 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000763 * @dwork: job to be done
764 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700765 *
766 * After waiting for a given time this puts a job in the kernel-global
767 * workqueue.
768 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800769int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800770 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
David Howells52bad642006-11-22 14:54:01 +0000772 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
Dave Jonesae90dd52006-06-30 01:40:45 -0400774EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700776/**
Linus Torvalds8c53e462009-10-14 09:16:42 -0700777 * flush_delayed_work - block until a dwork_struct's callback has terminated
778 * @dwork: the delayed work which is to be flushed
779 *
780 * Any timeout is cancelled, and any pending work is run immediately.
781 */
782void flush_delayed_work(struct delayed_work *dwork)
783{
784 if (del_timer_sync(&dwork->timer)) {
785 struct cpu_workqueue_struct *cwq;
Oleg Nesterov47dd5be2010-04-30 07:23:51 +0200786 cwq = wq_per_cpu(get_wq_data(&dwork->work)->wq, get_cpu());
Linus Torvalds8c53e462009-10-14 09:16:42 -0700787 __queue_work(cwq, &dwork->work);
788 put_cpu();
789 }
790 flush_work(&dwork->work);
791}
792EXPORT_SYMBOL(flush_delayed_work);
793
794/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700795 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
796 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000797 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700798 * @delay: number of jiffies to wait
799 *
800 * After waiting for a given time this puts a job in the kernel-global
801 * workqueue on the specified CPU.
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000804 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
David Howells52bad642006-11-22 14:54:01 +0000806 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Dave Jonesae90dd52006-06-30 01:40:45 -0400808EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Andrew Mortonb6136772006-06-25 05:47:49 -0700810/**
811 * schedule_on_each_cpu - call a function on each online CPU from keventd
812 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700813 *
814 * Returns zero on success.
815 * Returns -ve errno on failure.
816 *
Andrew Mortonb6136772006-06-25 05:47:49 -0700817 * schedule_on_each_cpu() is very slow.
818 */
David Howells65f27f32006-11-22 14:55:48 +0000819int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800820{
821 int cpu;
Andi Kleen65a64462009-10-14 06:22:47 +0200822 int orig = -1;
Andrew Mortonb6136772006-06-25 05:47:49 -0700823 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800824
Andrew Mortonb6136772006-06-25 05:47:49 -0700825 works = alloc_percpu(struct work_struct);
826 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800827 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700828
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100829 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -0800830
831 /*
832 * When running in keventd don't schedule a work item on
833 * itself. Can just call directly because the work queue is
834 * already bound. This also is faster.
835 */
836 if (current_is_keventd())
837 orig = raw_smp_processor_id();
838
Christoph Lameter15316ba2006-01-08 01:00:43 -0800839 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100840 struct work_struct *work = per_cpu_ptr(works, cpu);
841
842 INIT_WORK(work, func);
Andi Kleen65a64462009-10-14 06:22:47 +0200843 if (cpu != orig)
Tejun Heo93981802009-11-17 14:06:20 -0800844 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +0200845 }
Tejun Heo93981802009-11-17 14:06:20 -0800846 if (orig >= 0)
847 func(per_cpu_ptr(works, orig));
848
849 for_each_online_cpu(cpu)
850 flush_work(per_cpu_ptr(works, cpu));
851
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100852 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -0700853 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800854 return 0;
855}
856
Alan Sterneef6a7d2010-02-12 17:39:21 +0900857/**
858 * flush_scheduled_work - ensure that any scheduled work has run to completion.
859 *
860 * Forces execution of the kernel-global workqueue and blocks until its
861 * completion.
862 *
863 * Think twice before calling this function! It's very easy to get into
864 * trouble if you don't take great care. Either of the following situations
865 * will lead to deadlock:
866 *
867 * One of the work items currently on the workqueue needs to acquire
868 * a lock held by your code or its caller.
869 *
870 * Your code is running in the context of a work routine.
871 *
872 * They will be detected by lockdep when they occur, but the first might not
873 * occur very often. It depends on what work items are on the workqueue and
874 * what locks they need, which you have no control over.
875 *
876 * In most situations flushing the entire workqueue is overkill; you merely
877 * need to know that a particular work item isn't queued and isn't running.
878 * In such cases you should use cancel_delayed_work_sync() or
879 * cancel_work_sync() instead.
880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881void flush_scheduled_work(void)
882{
883 flush_workqueue(keventd_wq);
884}
Dave Jonesae90dd52006-06-30 01:40:45 -0400885EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887/**
James Bottomley1fa44ec2006-02-23 12:43:43 -0600888 * execute_in_process_context - reliably execute the routine with user context
889 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600890 * @ew: guaranteed storage for the execute work structure (must
891 * be available when the work executes)
892 *
893 * Executes the function immediately if process context is available,
894 * otherwise schedules the function for delayed execution.
895 *
896 * Returns: 0 - function was executed
897 * 1 - function was scheduled for execution
898 */
David Howells65f27f32006-11-22 14:55:48 +0000899int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600900{
901 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000902 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600903 return 0;
904 }
905
David Howells65f27f32006-11-22 14:55:48 +0000906 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600907 schedule_work(&ew->work);
908
909 return 1;
910}
911EXPORT_SYMBOL_GPL(execute_in_process_context);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913int keventd_up(void)
914{
915 return keventd_wq != NULL;
916}
917
918int current_is_keventd(void)
919{
920 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +0100921 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int ret = 0;
923
924 BUG_ON(!keventd_wq);
925
Christoph Lameter89ada672005-10-30 15:01:59 -0800926 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (current == cwq->thread)
928 ret = 1;
929
930 return ret;
931
932}
933
Oleg Nesterov3af244332007-05-09 02:34:09 -0700934static struct cpu_workqueue_struct *
935init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Christoph Lameter89ada672005-10-30 15:01:59 -0800937 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Oleg Nesterov3af244332007-05-09 02:34:09 -0700939 cwq->wq = wq;
940 spin_lock_init(&cwq->lock);
941 INIT_LIST_HEAD(&cwq->worklist);
942 init_waitqueue_head(&cwq->more_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Oleg Nesterov3af244332007-05-09 02:34:09 -0700944 return cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Oleg Nesterov3af244332007-05-09 02:34:09 -0700947static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700949 struct workqueue_struct *wq = cwq->wq;
David Howells6cc88bc2008-11-14 10:39:21 +1100950 const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
Oleg Nesterov3af244332007-05-09 02:34:09 -0700951 struct task_struct *p;
952
953 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
954 /*
955 * Nobody can add the work_struct to this cwq,
956 * if (caller is __create_workqueue)
957 * nobody should see this wq
958 * else // caller is CPU_UP_PREPARE
959 * cpu is not on cpu_online_map
960 * so we can abort safely.
961 */
962 if (IS_ERR(p))
963 return PTR_ERR(p);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700964 cwq->thread = p;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700965
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100966 trace_workqueue_creation(cwq->thread, cpu);
967
Oleg Nesterov3af244332007-05-09 02:34:09 -0700968 return 0;
969}
970
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700971static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
972{
973 struct task_struct *p = cwq->thread;
974
975 if (p != NULL) {
976 if (cpu >= 0)
977 kthread_bind(p, cpu);
978 wake_up_process(p);
979 }
980}
981
Johannes Berg4e6045f2007-10-18 23:39:55 -0700982struct workqueue_struct *__create_workqueue_key(const char *name,
983 int singlethread,
984 int freezeable,
Johannes Bergeb13ba82008-01-16 09:51:58 +0100985 struct lock_class_key *key,
986 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700987{
988 struct workqueue_struct *wq;
989 struct cpu_workqueue_struct *cwq;
990 int err = 0, cpu;
991
992 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
993 if (!wq)
994 return NULL;
995
996 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
997 if (!wq->cpu_wq) {
998 kfree(wq);
999 return NULL;
1000 }
1001
1002 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01001003 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07001004 wq->singlethread = singlethread;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001005 wq->freezeable = freezeable;
Oleg Nesterovcce1a162007-05-09 02:34:13 -07001006 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001007
1008 if (singlethread) {
Oleg Nesterov3af244332007-05-09 02:34:09 -07001009 cwq = init_cpu_workqueue(wq, singlethread_cpu);
1010 err = create_workqueue_thread(cwq, singlethread_cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07001011 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001012 } else {
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001013 cpu_maps_update_begin();
Oleg Nesterov6af8bf32008-07-29 22:33:49 -07001014 /*
1015 * We must place this wq on list even if the code below fails.
1016 * cpu_down(cpu) can remove cpu from cpu_populated_map before
1017 * destroy_workqueue() takes the lock, in that case we leak
1018 * cwq[cpu]->thread.
1019 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001020 spin_lock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001021 list_add(&wq->list, &workqueues);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001022 spin_unlock(&workqueue_lock);
Oleg Nesterov6af8bf32008-07-29 22:33:49 -07001023 /*
1024 * We must initialize cwqs for each possible cpu even if we
1025 * are going to call destroy_workqueue() finally. Otherwise
1026 * cpu_up() can hit the uninitialized cwq once we drop the
1027 * lock.
1028 */
Oleg Nesterov3af244332007-05-09 02:34:09 -07001029 for_each_possible_cpu(cpu) {
1030 cwq = init_cpu_workqueue(wq, cpu);
1031 if (err || !cpu_online(cpu))
1032 continue;
1033 err = create_workqueue_thread(cwq, cpu);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07001034 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001035 }
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001036 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -07001037 }
1038
1039 if (err) {
1040 destroy_workqueue(wq);
1041 wq = NULL;
1042 }
1043 return wq;
1044}
Johannes Berg4e6045f2007-10-18 23:39:55 -07001045EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001046
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -07001047static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001048{
Oleg Nesterov14441962007-05-23 13:57:57 -07001049 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001050 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
1051 * cpu_add_remove_lock protects cwq->thread.
Oleg Nesterov14441962007-05-23 13:57:57 -07001052 */
1053 if (cwq->thread == NULL)
1054 return;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001055
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001056 lock_map_acquire(&cwq->wq->lockdep_map);
1057 lock_map_release(&cwq->wq->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -07001058
Oleg Nesterov13c22162007-07-17 04:03:55 -07001059 flush_cpu_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -07001060 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001061 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
Oleg Nesterov13c22162007-07-17 04:03:55 -07001062 * a concurrent flush_workqueue() can insert a barrier after us.
1063 * However, in that case run_workqueue() won't return and check
1064 * kthread_should_stop() until it flushes all work_struct's.
Oleg Nesterov14441962007-05-23 13:57:57 -07001065 * When ->worklist becomes empty it is safe to exit because no
1066 * more work_structs can be queued on this cwq: flush_workqueue
1067 * checks list_empty(), and a "normal" queue_work() can't use
1068 * a dead CPU.
1069 */
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001070 trace_workqueue_destruction(cwq->thread);
Oleg Nesterov14441962007-05-23 13:57:57 -07001071 kthread_stop(cwq->thread);
1072 cwq->thread = NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001073}
1074
1075/**
1076 * destroy_workqueue - safely terminate a workqueue
1077 * @wq: target workqueue
1078 *
1079 * Safely destroy a workqueue. All work currently pending will be done first.
1080 */
1081void destroy_workqueue(struct workqueue_struct *wq)
1082{
Rusty Russelle7577c52009-01-01 10:12:25 +10301083 const struct cpumask *cpu_map = wq_cpu_map(wq);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001084 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001085
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001086 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001087 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001088 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001089 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001090
Rusty Russellaa85ea52009-03-30 22:05:15 -06001091 for_each_cpu(cpu, cpu_map)
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -07001092 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001093 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -07001094
1095 free_percpu(wq->cpu_wq);
1096 kfree(wq);
1097}
1098EXPORT_SYMBOL_GPL(destroy_workqueue);
1099
1100static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1101 unsigned long action,
1102 void *hcpu)
1103{
1104 unsigned int cpu = (unsigned long)hcpu;
1105 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct workqueue_struct *wq;
Akinobu Mita80b51842010-05-26 14:43:32 -07001107 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001109 action &= ~CPU_TASKS_FROZEN;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 switch (action) {
1112 case CPU_UP_PREPARE:
Rusty Russelle7577c52009-01-01 10:12:25 +10301113 cpumask_set_cpu(cpu, cpu_populated_map);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001114 }
Oleg Nesterov84485022008-07-25 01:47:54 -07001115undo:
Oleg Nesterov3af244332007-05-09 02:34:09 -07001116 list_for_each_entry(wq, &workqueues, list) {
1117 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
Christoph Lameter89ada672005-10-30 15:01:59 -08001118
Oleg Nesterov3af244332007-05-09 02:34:09 -07001119 switch (action) {
1120 case CPU_UP_PREPARE:
Akinobu Mita80b51842010-05-26 14:43:32 -07001121 err = create_workqueue_thread(cwq, cpu);
1122 if (!err)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001123 break;
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001124 printk(KERN_ERR "workqueue [%s] for %i failed\n",
1125 wq->name, cpu);
Oleg Nesterov84485022008-07-25 01:47:54 -07001126 action = CPU_UP_CANCELED;
Akinobu Mita80b51842010-05-26 14:43:32 -07001127 err = -ENOMEM;
Oleg Nesterov84485022008-07-25 01:47:54 -07001128 goto undo;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001129
1130 case CPU_ONLINE:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07001131 start_workqueue_thread(cwq, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001132 break;
1133
1134 case CPU_UP_CANCELED:
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07001135 start_workqueue_thread(cwq, -1);
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001136 case CPU_POST_DEAD:
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -07001137 cleanup_workqueue_thread(cwq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001142 switch (action) {
1143 case CPU_UP_CANCELED:
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001144 case CPU_POST_DEAD:
Rusty Russelle7577c52009-01-01 10:12:25 +10301145 cpumask_clear_cpu(cpu, cpu_populated_map);
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001146 }
1147
Akinobu Mita80b51842010-05-26 14:43:32 -07001148 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Rusty Russell2d3854a2008-11-05 13:39:10 +11001151#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08001152
Rusty Russell2d3854a2008-11-05 13:39:10 +11001153struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06001154 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001155 long (*fn)(void *);
1156 void *arg;
1157 long ret;
1158};
1159
Andrew Morton6b440032009-04-09 09:50:37 -06001160static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11001161{
Andrew Morton6b440032009-04-09 09:50:37 -06001162 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001163 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06001164 complete(&wfc->completion);
1165 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001166}
1167
1168/**
1169 * work_on_cpu - run a function in user context on a particular cpu
1170 * @cpu: the cpu to run on
1171 * @fn: the function to run
1172 * @arg: the function arg
1173 *
Rusty Russell31ad9082009-01-16 15:31:15 -08001174 * This will return the value @fn returns.
1175 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06001176 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11001177 */
1178long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1179{
Andrew Morton6b440032009-04-09 09:50:37 -06001180 struct task_struct *sub_thread;
1181 struct work_for_cpu wfc = {
1182 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
1183 .fn = fn,
1184 .arg = arg,
1185 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11001186
Andrew Morton6b440032009-04-09 09:50:37 -06001187 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
1188 if (IS_ERR(sub_thread))
1189 return PTR_ERR(sub_thread);
1190 kthread_bind(sub_thread, cpu);
1191 wake_up_process(sub_thread);
1192 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001193 return wfc.ret;
1194}
1195EXPORT_SYMBOL_GPL(work_on_cpu);
1196#endif /* CONFIG_SMP */
1197
Oleg Nesterovc12920d2007-05-09 02:34:14 -07001198void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Rusty Russelle7577c52009-01-01 10:12:25 +10301200 alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
1201
1202 cpumask_copy(cpu_populated_map, cpu_online_mask);
1203 singlethread_cpu = cpumask_first(cpu_possible_mask);
1204 cpu_singlethread_map = cpumask_of(singlethread_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 hotcpu_notifier(workqueue_cpu_callback, 0);
1206 keventd_wq = create_workqueue("events");
1207 BUG_ON(!keventd_wq);
1208}