blob: dc78956ccf034b82e334ad0fe4223702b6ab3065 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020038 * Structure fields follow one of the following exclusion rules.
39 *
40 * I: Set during initialization and read-only afterwards.
41 *
42 * L: cwq->lock protected. Access with cwq->lock held.
43 *
44 * W: workqueue_lock protected.
45 */
46
47/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -080048 * The per-CPU workqueue (if single thread, we always use the first
49 * possible cpu).
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51struct cpu_workqueue_struct {
52
53 spinlock_t lock;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct list_head worklist;
56 wait_queue_head_t more_work;
Oleg Nesterov3af244332007-05-09 02:34:09 -070057 struct work_struct *current_work;
Tejun Heo15376632010-06-29 10:07:11 +020058 unsigned int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Tejun Heo4690c4a2010-06-29 10:07:10 +020060 struct workqueue_struct *wq; /* I: the owning workqueue */
61 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062} ____cacheline_aligned;
63
64/*
65 * The externally visible workqueue abstraction is an array of
66 * per-CPU workqueues:
67 */
68struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +020069 unsigned int flags; /* I: WQ_* flags */
Tejun Heo4690c4a2010-06-29 10:07:10 +020070 struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
71 struct list_head list; /* W: list of all workqueues */
72 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -070073#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +020074 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -070075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +090078#ifdef CONFIG_DEBUG_OBJECTS_WORK
79
80static struct debug_obj_descr work_debug_descr;
81
82/*
83 * fixup_init is called when:
84 * - an active object is initialized
85 */
86static int work_fixup_init(void *addr, enum debug_obj_state state)
87{
88 struct work_struct *work = addr;
89
90 switch (state) {
91 case ODEBUG_STATE_ACTIVE:
92 cancel_work_sync(work);
93 debug_object_init(work, &work_debug_descr);
94 return 1;
95 default:
96 return 0;
97 }
98}
99
100/*
101 * fixup_activate is called when:
102 * - an active object is activated
103 * - an unknown object is activated (might be a statically initialized object)
104 */
105static int work_fixup_activate(void *addr, enum debug_obj_state state)
106{
107 struct work_struct *work = addr;
108
109 switch (state) {
110
111 case ODEBUG_STATE_NOTAVAILABLE:
112 /*
113 * This is not really a fixup. The work struct was
114 * statically initialized. We just make sure that it
115 * is tracked in the object tracker.
116 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200117 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900118 debug_object_init(work, &work_debug_descr);
119 debug_object_activate(work, &work_debug_descr);
120 return 0;
121 }
122 WARN_ON_ONCE(1);
123 return 0;
124
125 case ODEBUG_STATE_ACTIVE:
126 WARN_ON(1);
127
128 default:
129 return 0;
130 }
131}
132
133/*
134 * fixup_free is called when:
135 * - an active object is freed
136 */
137static int work_fixup_free(void *addr, enum debug_obj_state state)
138{
139 struct work_struct *work = addr;
140
141 switch (state) {
142 case ODEBUG_STATE_ACTIVE:
143 cancel_work_sync(work);
144 debug_object_free(work, &work_debug_descr);
145 return 1;
146 default:
147 return 0;
148 }
149}
150
151static struct debug_obj_descr work_debug_descr = {
152 .name = "work_struct",
153 .fixup_init = work_fixup_init,
154 .fixup_activate = work_fixup_activate,
155 .fixup_free = work_fixup_free,
156};
157
158static inline void debug_work_activate(struct work_struct *work)
159{
160 debug_object_activate(work, &work_debug_descr);
161}
162
163static inline void debug_work_deactivate(struct work_struct *work)
164{
165 debug_object_deactivate(work, &work_debug_descr);
166}
167
168void __init_work(struct work_struct *work, int onstack)
169{
170 if (onstack)
171 debug_object_init_on_stack(work, &work_debug_descr);
172 else
173 debug_object_init(work, &work_debug_descr);
174}
175EXPORT_SYMBOL_GPL(__init_work);
176
177void destroy_work_on_stack(struct work_struct *work)
178{
179 debug_object_free(work, &work_debug_descr);
180}
181EXPORT_SYMBOL_GPL(destroy_work_on_stack);
182
183#else
184static inline void debug_work_activate(struct work_struct *work) { }
185static inline void debug_work_deactivate(struct work_struct *work) { }
186#endif
187
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100188/* Serializes the accesses to the list of workqueues. */
189static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static LIST_HEAD(workqueues);
191
Oleg Nesterov3af244332007-05-09 02:34:09 -0700192static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700193
Tejun Heo4690c4a2010-06-29 10:07:10 +0200194static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
195 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700196{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700197 return per_cpu_ptr(wq->cpu_wq, cpu);
198}
199
Tejun Heo15376632010-06-29 10:07:11 +0200200static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
201 struct workqueue_struct *wq)
202{
203 if (unlikely(wq->flags & WQ_SINGLE_THREAD))
204 cpu = singlethread_cpu;
205 return get_cwq(cpu, wq);
206}
207
David Howells4594bf12006-12-07 11:33:26 +0000208/*
209 * Set the workqueue on which a work item is to be run
210 * - Must *only* be called if the pending flag is set
211 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700212static inline void set_wq_data(struct work_struct *work,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200213 struct cpu_workqueue_struct *cwq,
214 unsigned long extra_flags)
David Howells365970a2006-11-22 14:54:49 +0000215{
David Howells4594bf12006-12-07 11:33:26 +0000216 BUG_ON(!work_pending(work));
217
Tejun Heo4690c4a2010-06-29 10:07:10 +0200218 atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
Tejun Heo22df02b2010-06-29 10:07:10 +0200219 WORK_STRUCT_PENDING | extra_flags);
David Howells365970a2006-11-22 14:54:49 +0000220}
221
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200222/*
223 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
224 */
225static inline void clear_wq_data(struct work_struct *work)
226{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200227 atomic_long_set(&work->data, work_static(work));
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200228}
229
Tejun Heo64166692010-06-29 10:07:11 +0200230static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000231{
Tejun Heo64166692010-06-29 10:07:11 +0200232 return (void *)(atomic_long_read(&work->data) &
233 WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000234}
235
Tejun Heo4690c4a2010-06-29 10:07:10 +0200236/**
237 * insert_work - insert a work into cwq
238 * @cwq: cwq @work belongs to
239 * @work: work to insert
240 * @head: insertion point
241 * @extra_flags: extra WORK_STRUCT_* flags to set
242 *
243 * Insert @work into @cwq after @head.
244 *
245 * CONTEXT:
246 * spin_lock_irq(cwq->lock).
247 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700248static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200249 struct work_struct *work, struct list_head *head,
250 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700251{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200252 /* we own @work, set data and link */
253 set_wq_data(work, cwq, extra_flags);
254
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700255 /*
256 * Ensure that we get the right work->data if we see the
257 * result of list_add() below, see try_to_grab_pending().
258 */
259 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200260
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700261 list_add_tail(&work->entry, head);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700262 wake_up(&cwq->more_work);
263}
264
Tejun Heo4690c4a2010-06-29 10:07:10 +0200265static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct work_struct *work)
267{
Tejun Heo15376632010-06-29 10:07:11 +0200268 struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 unsigned long flags;
270
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900271 debug_work_activate(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 spin_lock_irqsave(&cwq->lock, flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200273 BUG_ON(!list_empty(&work->entry));
274 insert_work(cwq, work, &cwq->worklist, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 spin_unlock_irqrestore(&cwq->lock, flags);
276}
277
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700278/**
279 * queue_work - queue work on a workqueue
280 * @wq: workqueue to use
281 * @work: work to queue
282 *
Alan Stern057647f2006-10-28 10:38:58 -0700283 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700285 * We queue the work to the CPU on which it was submitted, but if the CPU dies
286 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800288int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700290 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700292 ret = queue_work_on(get_cpu(), wq, work);
293 put_cpu();
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return ret;
296}
Dave Jonesae90dd52006-06-30 01:40:45 -0400297EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Zhang Ruic1a220e2008-07-23 21:28:39 -0700299/**
300 * queue_work_on - queue work on specific cpu
301 * @cpu: CPU number to execute work on
302 * @wq: workqueue to use
303 * @work: work to queue
304 *
305 * Returns 0 if @work was already on a queue, non-zero otherwise.
306 *
307 * We queue the work to a specific CPU, the caller must ensure it
308 * can't go away.
309 */
310int
311queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
312{
313 int ret = 0;
314
Tejun Heo22df02b2010-06-29 10:07:10 +0200315 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +0200316 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -0700317 ret = 1;
318 }
319 return ret;
320}
321EXPORT_SYMBOL_GPL(queue_work_on);
322
Li Zefan6d141c32008-02-08 04:21:09 -0800323static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
David Howells52bad642006-11-22 14:54:01 +0000325 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700326 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Tejun Heo4690c4a2010-06-29 10:07:10 +0200328 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700331/**
332 * queue_delayed_work - queue work on a workqueue after delay
333 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800334 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700335 * @delay: number of jiffies to wait before queueing
336 *
Alan Stern057647f2006-10-28 10:38:58 -0700337 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700338 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800339int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000340 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
David Howells52bad642006-11-22 14:54:01 +0000342 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700343 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700345 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Dave Jonesae90dd52006-06-30 01:40:45 -0400347EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700349/**
350 * queue_delayed_work_on - queue work on specific CPU after delay
351 * @cpu: CPU number to execute work on
352 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800353 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700354 * @delay: number of jiffies to wait before queueing
355 *
Alan Stern057647f2006-10-28 10:38:58 -0700356 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700357 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700358int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000359 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700360{
361 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000362 struct timer_list *timer = &dwork->timer;
363 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700364
Tejun Heo22df02b2010-06-29 10:07:10 +0200365 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700366 BUG_ON(timer_pending(timer));
367 BUG_ON(!list_empty(&work->entry));
368
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700369 timer_stats_timer_set_start_info(&dwork->timer);
370
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700371 /* This stores cwq for the moment, for the timer_fn */
Tejun Heo15376632010-06-29 10:07:11 +0200372 set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700373 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000374 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700375 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700376
377 if (unlikely(cpu >= 0))
378 add_timer_on(timer, cpu);
379 else
380 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700381 ret = 1;
382 }
383 return ret;
384}
Dave Jonesae90dd52006-06-30 01:40:45 -0400385EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Tejun Heoa62428c2010-06-29 10:07:10 +0200387/**
388 * process_one_work - process single work
389 * @cwq: cwq to process work for
390 * @work: work to process
391 *
392 * Process @work. This function contains all the logics necessary to
393 * process a single work including synchronization against and
394 * interaction with other workers on the same cpu, queueing and
395 * flushing. As long as context requirement is met, any worker can
396 * call this function to process a work.
397 *
398 * CONTEXT:
399 * spin_lock_irq(cwq->lock) which is released and regrabbed.
400 */
401static void process_one_work(struct cpu_workqueue_struct *cwq,
402 struct work_struct *work)
403{
404 work_func_t f = work->func;
405#ifdef CONFIG_LOCKDEP
406 /*
407 * It is permissible to free the struct work_struct from
408 * inside the function that is called from it, this we need to
409 * take into account for lockdep too. To avoid bogus "held
410 * lock freed" warnings as well as problems when looking into
411 * work->lockdep_map, make a copy and use that here.
412 */
413 struct lockdep_map lockdep_map = work->lockdep_map;
414#endif
415 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +0200416 debug_work_deactivate(work);
417 cwq->current_work = work;
418 list_del_init(&work->entry);
419
420 spin_unlock_irq(&cwq->lock);
421
422 BUG_ON(get_wq_data(work) != cwq);
423 work_clear_pending(work);
424 lock_map_acquire(&cwq->wq->lockdep_map);
425 lock_map_acquire(&lockdep_map);
426 f(work);
427 lock_map_release(&lockdep_map);
428 lock_map_release(&cwq->wq->lockdep_map);
429
430 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
431 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
432 "%s/0x%08x/%d\n",
433 current->comm, preempt_count(), task_pid_nr(current));
434 printk(KERN_ERR " last function: ");
435 print_symbol("%s\n", (unsigned long)f);
436 debug_show_held_locks(current);
437 dump_stack();
438 }
439
440 spin_lock_irq(&cwq->lock);
441
442 /* we're done with it, release */
443 cwq->current_work = NULL;
444}
445
Arjan van de Ven858119e2006-01-14 13:20:43 -0800446static void run_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700448 spin_lock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 while (!list_empty(&cwq->worklist)) {
450 struct work_struct *work = list_entry(cwq->worklist.next,
451 struct work_struct, entry);
Tejun Heoa62428c2010-06-29 10:07:10 +0200452 process_one_work(cwq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700454 spin_unlock_irq(&cwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Tejun Heo4690c4a2010-06-29 10:07:10 +0200457/**
458 * worker_thread - the worker thread function
459 * @__cwq: cwq to serve
460 *
461 * The cwq worker thread function.
462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static int worker_thread(void *__cwq)
464{
465 struct cpu_workqueue_struct *cwq = __cwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700466 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Tejun Heo97e37d72010-06-29 10:07:10 +0200468 if (cwq->wq->flags & WQ_FREEZEABLE)
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700469 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Oleg Nesterov3af244332007-05-09 02:34:09 -0700471 for (;;) {
Oleg Nesterov3af244332007-05-09 02:34:09 -0700472 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
Oleg Nesterov14441962007-05-23 13:57:57 -0700473 if (!freezing(current) &&
474 !kthread_should_stop() &&
475 list_empty(&cwq->worklist))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 schedule();
Oleg Nesterov3af244332007-05-09 02:34:09 -0700477 finish_wait(&cwq->more_work, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Oleg Nesterov85f41862007-05-09 02:34:20 -0700479 try_to_freeze();
480
Oleg Nesterov14441962007-05-23 13:57:57 -0700481 if (kthread_should_stop())
Oleg Nesterov3af244332007-05-09 02:34:09 -0700482 break;
483
Tejun Heo15376632010-06-29 10:07:11 +0200484 if (unlikely(!cpumask_equal(&cwq->thread->cpus_allowed,
485 get_cpu_mask(cwq->cpu))))
486 set_cpus_allowed_ptr(cwq->thread,
487 get_cpu_mask(cwq->cpu));
Oleg Nesterov3af244332007-05-09 02:34:09 -0700488 run_workqueue(cwq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
492}
493
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700494struct wq_barrier {
495 struct work_struct work;
496 struct completion done;
497};
498
499static void wq_barrier_func(struct work_struct *work)
500{
501 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
502 complete(&barr->done);
503}
504
Tejun Heo4690c4a2010-06-29 10:07:10 +0200505/**
506 * insert_wq_barrier - insert a barrier work
507 * @cwq: cwq to insert barrier into
508 * @barr: wq_barrier to insert
509 * @head: insertion point
510 *
511 * Insert barrier @barr into @cwq before @head.
512 *
513 * CONTEXT:
514 * spin_lock_irq(cwq->lock).
515 */
Oleg Nesterov83c22522007-05-09 02:33:54 -0700516static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700517 struct wq_barrier *barr, struct list_head *head)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700518{
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900519 /*
520 * debugobject calls are safe here even with cwq->lock locked
521 * as we know for sure that this will not trigger any of the
522 * checks and call back into the fixup functions where we
523 * might deadlock.
524 */
525 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +0200526 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700527 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -0700528
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900529 debug_work_activate(&barr->work);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200530 insert_work(cwq, &barr->work, head, 0);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700531}
532
Oleg Nesterov14441962007-05-23 13:57:57 -0700533static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Lai Jiangshan2355b702009-04-02 16:58:24 -0700535 int active = 0;
536 struct wq_barrier barr;
Oleg Nesterov14441962007-05-23 13:57:57 -0700537
Lai Jiangshan2355b702009-04-02 16:58:24 -0700538 WARN_ON(cwq->thread == current);
539
540 spin_lock_irq(&cwq->lock);
541 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
542 insert_wq_barrier(cwq, &barr, &cwq->worklist);
Oleg Nesterov14441962007-05-23 13:57:57 -0700543 active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Lai Jiangshan2355b702009-04-02 16:58:24 -0700545 spin_unlock_irq(&cwq->lock);
546
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900547 if (active) {
Lai Jiangshan2355b702009-04-02 16:58:24 -0700548 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900549 destroy_work_on_stack(&barr.work);
550 }
Oleg Nesterov14441962007-05-23 13:57:57 -0700551
552 return active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700555/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700557 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
559 * Forces execution of the workqueue and blocks until its completion.
560 * This is typically used in driver shutdown handlers.
561 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700562 * We sleep until all works which were queued on entry have been handled,
563 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800565void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Oleg Nesterovcce1a162007-05-09 02:34:13 -0700567 int cpu;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700568
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700569 might_sleep();
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200570 lock_map_acquire(&wq->lockdep_map);
571 lock_map_release(&wq->lockdep_map);
Tejun Heo15376632010-06-29 10:07:11 +0200572 for_each_possible_cpu(cpu)
573 flush_cpu_workqueue(get_cwq(cpu, wq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Dave Jonesae90dd52006-06-30 01:40:45 -0400575EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Oleg Nesterovdb700892008-07-25 01:47:49 -0700577/**
578 * flush_work - block until a work_struct's callback has terminated
579 * @work: the work which is to be flushed
580 *
Oleg Nesterova67da702008-07-25 01:47:52 -0700581 * Returns false if @work has already terminated.
582 *
Oleg Nesterovdb700892008-07-25 01:47:49 -0700583 * It is expected that, prior to calling flush_work(), the caller has
584 * arranged for the work to not be requeued, otherwise it doesn't make
585 * sense to use this function.
586 */
587int flush_work(struct work_struct *work)
588{
589 struct cpu_workqueue_struct *cwq;
590 struct list_head *prev;
591 struct wq_barrier barr;
592
593 might_sleep();
594 cwq = get_wq_data(work);
595 if (!cwq)
596 return 0;
597
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200598 lock_map_acquire(&cwq->wq->lockdep_map);
599 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -0700600
Oleg Nesterovdb700892008-07-25 01:47:49 -0700601 spin_lock_irq(&cwq->lock);
602 if (!list_empty(&work->entry)) {
603 /*
604 * See the comment near try_to_grab_pending()->smp_rmb().
605 * If it was re-queued under us we are not going to wait.
606 */
607 smp_rmb();
608 if (unlikely(cwq != get_wq_data(work)))
Tejun Heo4690c4a2010-06-29 10:07:10 +0200609 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -0700610 prev = &work->entry;
611 } else {
612 if (cwq->current_work != work)
Tejun Heo4690c4a2010-06-29 10:07:10 +0200613 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -0700614 prev = &cwq->worklist;
615 }
616 insert_wq_barrier(cwq, &barr, prev->next);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700617
Tejun Heo4690c4a2010-06-29 10:07:10 +0200618 spin_unlock_irq(&cwq->lock);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700619 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900620 destroy_work_on_stack(&barr.work);
Oleg Nesterovdb700892008-07-25 01:47:49 -0700621 return 1;
Tejun Heo4690c4a2010-06-29 10:07:10 +0200622already_gone:
623 spin_unlock_irq(&cwq->lock);
624 return 0;
Oleg Nesterovdb700892008-07-25 01:47:49 -0700625}
626EXPORT_SYMBOL_GPL(flush_work);
627
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700628/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700629 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700630 * so this work can't be re-armed in any way.
631 */
632static int try_to_grab_pending(struct work_struct *work)
633{
634 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700635 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700636
Tejun Heo22df02b2010-06-29 10:07:10 +0200637 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700638 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700639
640 /*
641 * The queueing is in progress, or it is already queued. Try to
642 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
643 */
644
645 cwq = get_wq_data(work);
646 if (!cwq)
647 return ret;
648
649 spin_lock_irq(&cwq->lock);
650 if (!list_empty(&work->entry)) {
651 /*
652 * This work is queued, but perhaps we locked the wrong cwq.
653 * In that case we must see the new value after rmb(), see
654 * insert_work()->wmb().
655 */
656 smp_rmb();
657 if (cwq == get_wq_data(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900658 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700659 list_del_init(&work->entry);
660 ret = 1;
661 }
662 }
663 spin_unlock_irq(&cwq->lock);
664
665 return ret;
666}
667
668static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700669 struct work_struct *work)
670{
671 struct wq_barrier barr;
672 int running = 0;
673
674 spin_lock_irq(&cwq->lock);
675 if (unlikely(cwq->current_work == work)) {
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700676 insert_wq_barrier(cwq, &barr, cwq->worklist.next);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700677 running = 1;
678 }
679 spin_unlock_irq(&cwq->lock);
680
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900681 if (unlikely(running)) {
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700682 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900683 destroy_work_on_stack(&barr.work);
684 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700685}
686
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700687static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700688{
689 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700690 struct workqueue_struct *wq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700691 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700692
Oleg Nesterovf293ea92007-05-09 02:34:10 -0700693 might_sleep();
694
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200695 lock_map_acquire(&work->lockdep_map);
696 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -0700697
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700698 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700699 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -0700700 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700701
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700702 wq = cwq->wq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700703
Tejun Heo15376632010-06-29 10:07:11 +0200704 for_each_possible_cpu(cpu)
Tejun Heo4690c4a2010-06-29 10:07:10 +0200705 wait_on_cpu_work(get_cwq(cpu, wq), work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700706}
707
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700708static int __cancel_work_timer(struct work_struct *work,
709 struct timer_list* timer)
710{
711 int ret;
712
713 do {
714 ret = (timer && likely(del_timer(timer)));
715 if (!ret)
716 ret = try_to_grab_pending(work);
717 wait_on_work(work);
718 } while (unlikely(ret < 0));
719
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200720 clear_wq_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700721 return ret;
722}
723
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700724/**
725 * cancel_work_sync - block until a work_struct's callback has terminated
726 * @work: the work which is to be flushed
727 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700728 * Returns true if @work was pending.
729 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700730 * cancel_work_sync() will cancel the work if it is queued. If the work's
731 * callback appears to be running, cancel_work_sync() will block until it
732 * has completed.
733 *
734 * It is possible to use this function if the work re-queues itself. It can
735 * cancel the work even if it migrates to another workqueue, however in that
736 * case it only guarantees that work->func() has completed on the last queued
737 * workqueue.
738 *
739 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
740 * pending, otherwise it goes into a busy-wait loop until the timer expires.
741 *
742 * The caller must ensure that workqueue_struct on which this work was last
743 * queued can't be destroyed before this function returns.
744 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700745int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700746{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700747 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700748}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700749EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700750
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700751/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700752 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700753 * @dwork: the delayed work struct
754 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700755 * Returns true if @dwork was pending.
756 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700757 * It is possible to use this function if @dwork rearms itself via queue_work()
758 * or queue_delayed_work(). See also the comment for cancel_work_sync().
759 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700760int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700761{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700762 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700763}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700764EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700766static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700768/**
769 * schedule_work - put work task in global workqueue
770 * @work: job to be done
771 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +0200772 * Returns zero if @work was already on the kernel-global workqueue and
773 * non-zero otherwise.
774 *
775 * This puts a job in the kernel-global workqueue if it was not already
776 * queued and leaves it in the same position on the kernel-global
777 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700778 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800779int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 return queue_work(keventd_wq, work);
782}
Dave Jonesae90dd52006-06-30 01:40:45 -0400783EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Zhang Ruic1a220e2008-07-23 21:28:39 -0700785/*
786 * schedule_work_on - put work task on a specific cpu
787 * @cpu: cpu to put the work task on
788 * @work: job to be done
789 *
790 * This puts a job on a specific cpu
791 */
792int schedule_work_on(int cpu, struct work_struct *work)
793{
794 return queue_work_on(cpu, keventd_wq, work);
795}
796EXPORT_SYMBOL(schedule_work_on);
797
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700798/**
799 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +0000800 * @dwork: job to be done
801 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700802 *
803 * After waiting for a given time this puts a job in the kernel-global
804 * workqueue.
805 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800806int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800807 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
David Howells52bad642006-11-22 14:54:01 +0000809 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
Dave Jonesae90dd52006-06-30 01:40:45 -0400811EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700813/**
Linus Torvalds8c53e462009-10-14 09:16:42 -0700814 * flush_delayed_work - block until a dwork_struct's callback has terminated
815 * @dwork: the delayed work which is to be flushed
816 *
817 * Any timeout is cancelled, and any pending work is run immediately.
818 */
819void flush_delayed_work(struct delayed_work *dwork)
820{
821 if (del_timer_sync(&dwork->timer)) {
Tejun Heo4690c4a2010-06-29 10:07:10 +0200822 __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
823 &dwork->work);
Linus Torvalds8c53e462009-10-14 09:16:42 -0700824 put_cpu();
825 }
826 flush_work(&dwork->work);
827}
828EXPORT_SYMBOL(flush_delayed_work);
829
830/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700831 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
832 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +0000833 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700834 * @delay: number of jiffies to wait
835 *
836 * After waiting for a given time this puts a job in the kernel-global
837 * workqueue on the specified CPU.
838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +0000840 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
David Howells52bad642006-11-22 14:54:01 +0000842 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
Dave Jonesae90dd52006-06-30 01:40:45 -0400844EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Andrew Mortonb6136772006-06-25 05:47:49 -0700846/**
847 * schedule_on_each_cpu - call a function on each online CPU from keventd
848 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -0700849 *
850 * Returns zero on success.
851 * Returns -ve errno on failure.
852 *
Andrew Mortonb6136772006-06-25 05:47:49 -0700853 * schedule_on_each_cpu() is very slow.
854 */
David Howells65f27f32006-11-22 14:55:48 +0000855int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800856{
857 int cpu;
Andi Kleen65a64462009-10-14 06:22:47 +0200858 int orig = -1;
Andrew Mortonb6136772006-06-25 05:47:49 -0700859 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -0800860
Andrew Mortonb6136772006-06-25 05:47:49 -0700861 works = alloc_percpu(struct work_struct);
862 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -0800863 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -0700864
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100865 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -0800866
867 /*
868 * When running in keventd don't schedule a work item on
869 * itself. Can just call directly because the work queue is
870 * already bound. This also is faster.
871 */
872 if (current_is_keventd())
873 orig = raw_smp_processor_id();
874
Christoph Lameter15316ba2006-01-08 01:00:43 -0800875 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +0100876 struct work_struct *work = per_cpu_ptr(works, cpu);
877
878 INIT_WORK(work, func);
Andi Kleen65a64462009-10-14 06:22:47 +0200879 if (cpu != orig)
Tejun Heo93981802009-11-17 14:06:20 -0800880 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +0200881 }
Tejun Heo93981802009-11-17 14:06:20 -0800882 if (orig >= 0)
883 func(per_cpu_ptr(works, orig));
884
885 for_each_online_cpu(cpu)
886 flush_work(per_cpu_ptr(works, cpu));
887
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100888 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -0700889 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -0800890 return 0;
891}
892
Alan Sterneef6a7d2010-02-12 17:39:21 +0900893/**
894 * flush_scheduled_work - ensure that any scheduled work has run to completion.
895 *
896 * Forces execution of the kernel-global workqueue and blocks until its
897 * completion.
898 *
899 * Think twice before calling this function! It's very easy to get into
900 * trouble if you don't take great care. Either of the following situations
901 * will lead to deadlock:
902 *
903 * One of the work items currently on the workqueue needs to acquire
904 * a lock held by your code or its caller.
905 *
906 * Your code is running in the context of a work routine.
907 *
908 * They will be detected by lockdep when they occur, but the first might not
909 * occur very often. It depends on what work items are on the workqueue and
910 * what locks they need, which you have no control over.
911 *
912 * In most situations flushing the entire workqueue is overkill; you merely
913 * need to know that a particular work item isn't queued and isn't running.
914 * In such cases you should use cancel_delayed_work_sync() or
915 * cancel_work_sync() instead.
916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917void flush_scheduled_work(void)
918{
919 flush_workqueue(keventd_wq);
920}
Dave Jonesae90dd52006-06-30 01:40:45 -0400921EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923/**
James Bottomley1fa44ec2006-02-23 12:43:43 -0600924 * execute_in_process_context - reliably execute the routine with user context
925 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -0600926 * @ew: guaranteed storage for the execute work structure (must
927 * be available when the work executes)
928 *
929 * Executes the function immediately if process context is available,
930 * otherwise schedules the function for delayed execution.
931 *
932 * Returns: 0 - function was executed
933 * 1 - function was scheduled for execution
934 */
David Howells65f27f32006-11-22 14:55:48 +0000935int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -0600936{
937 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +0000938 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600939 return 0;
940 }
941
David Howells65f27f32006-11-22 14:55:48 +0000942 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -0600943 schedule_work(&ew->work);
944
945 return 1;
946}
947EXPORT_SYMBOL_GPL(execute_in_process_context);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949int keventd_up(void)
950{
951 return keventd_wq != NULL;
952}
953
954int current_is_keventd(void)
955{
956 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +0100957 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int ret = 0;
959
960 BUG_ON(!keventd_wq);
961
Tejun Heo15376632010-06-29 10:07:11 +0200962 cwq = get_cwq(cpu, keventd_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (current == cwq->thread)
964 ret = 1;
965
966 return ret;
967
968}
969
Oleg Nesterov3af244332007-05-09 02:34:09 -0700970static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Oleg Nesterov3af244332007-05-09 02:34:09 -0700972 struct workqueue_struct *wq = cwq->wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700973 struct task_struct *p;
974
Tejun Heo15376632010-06-29 10:07:11 +0200975 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700976 /*
977 * Nobody can add the work_struct to this cwq,
978 * if (caller is __create_workqueue)
979 * nobody should see this wq
980 * else // caller is CPU_UP_PREPARE
981 * cpu is not on cpu_online_map
982 * so we can abort safely.
983 */
984 if (IS_ERR(p))
985 return PTR_ERR(p);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700986 cwq->thread = p;
Oleg Nesterov3af244332007-05-09 02:34:09 -0700987
988 return 0;
989}
990
Oleg Nesterov06ba38a2007-05-09 02:34:15 -0700991static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
992{
993 struct task_struct *p = cwq->thread;
994
995 if (p != NULL) {
996 if (cpu >= 0)
997 kthread_bind(p, cpu);
998 wake_up_process(p);
999 }
1000}
1001
Johannes Berg4e6045f2007-10-18 23:39:55 -07001002struct workqueue_struct *__create_workqueue_key(const char *name,
Tejun Heo97e37d72010-06-29 10:07:10 +02001003 unsigned int flags,
Johannes Bergeb13ba82008-01-16 09:51:58 +01001004 struct lock_class_key *key,
1005 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001006{
Tejun Heo15376632010-06-29 10:07:11 +02001007 bool singlethread = flags & WQ_SINGLE_THREAD;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001008 struct workqueue_struct *wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001009 int err = 0, cpu;
1010
1011 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1012 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001013 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001014
1015 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001016 if (!wq->cpu_wq)
1017 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001018
Tejun Heo97e37d72010-06-29 10:07:10 +02001019 wq->flags = flags;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001020 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01001021 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07001022 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001023
Tejun Heo15376632010-06-29 10:07:11 +02001024 cpu_maps_update_begin();
1025 /*
1026 * We must initialize cwqs for each possible cpu even if we
1027 * are going to call destroy_workqueue() finally. Otherwise
1028 * cpu_up() can hit the uninitialized cwq once we drop the
1029 * lock.
1030 */
1031 for_each_possible_cpu(cpu) {
1032 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1033
1034 cwq->wq = wq;
1035 cwq->cpu = cpu;
1036 spin_lock_init(&cwq->lock);
1037 INIT_LIST_HEAD(&cwq->worklist);
1038 init_waitqueue_head(&cwq->more_work);
1039
1040 if (err)
1041 continue;
1042 err = create_workqueue_thread(cwq, cpu);
1043 if (cpu_online(cpu) && !singlethread)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07001044 start_workqueue_thread(cwq, cpu);
Tejun Heo15376632010-06-29 10:07:11 +02001045 else
1046 start_workqueue_thread(cwq, -1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001047 }
1048
Tejun Heo15376632010-06-29 10:07:11 +02001049 spin_lock(&workqueue_lock);
1050 list_add(&wq->list, &workqueues);
1051 spin_unlock(&workqueue_lock);
1052
1053 cpu_maps_update_done();
1054
Oleg Nesterov3af244332007-05-09 02:34:09 -07001055 if (err) {
1056 destroy_workqueue(wq);
1057 wq = NULL;
1058 }
1059 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001060err:
1061 if (wq) {
1062 free_percpu(wq->cpu_wq);
1063 kfree(wq);
1064 }
1065 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001066}
Johannes Berg4e6045f2007-10-18 23:39:55 -07001067EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001068
Oleg Nesterov1e35eaa2008-04-29 01:00:28 -07001069static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001070{
Oleg Nesterov14441962007-05-23 13:57:57 -07001071 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001072 * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
1073 * cpu_add_remove_lock protects cwq->thread.
Oleg Nesterov14441962007-05-23 13:57:57 -07001074 */
1075 if (cwq->thread == NULL)
1076 return;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001077
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001078 lock_map_acquire(&cwq->wq->lockdep_map);
1079 lock_map_release(&cwq->wq->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -07001080
Oleg Nesterov13c22162007-07-17 04:03:55 -07001081 flush_cpu_workqueue(cwq);
Oleg Nesterov14441962007-05-23 13:57:57 -07001082 /*
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001083 * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
Oleg Nesterov13c22162007-07-17 04:03:55 -07001084 * a concurrent flush_workqueue() can insert a barrier after us.
1085 * However, in that case run_workqueue() won't return and check
1086 * kthread_should_stop() until it flushes all work_struct's.
Oleg Nesterov14441962007-05-23 13:57:57 -07001087 * When ->worklist becomes empty it is safe to exit because no
1088 * more work_structs can be queued on this cwq: flush_workqueue
1089 * checks list_empty(), and a "normal" queue_work() can't use
1090 * a dead CPU.
1091 */
Oleg Nesterov14441962007-05-23 13:57:57 -07001092 kthread_stop(cwq->thread);
1093 cwq->thread = NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001094}
1095
1096/**
1097 * destroy_workqueue - safely terminate a workqueue
1098 * @wq: target workqueue
1099 *
1100 * Safely destroy a workqueue. All work currently pending will be done first.
1101 */
1102void destroy_workqueue(struct workqueue_struct *wq)
1103{
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001104 int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001105
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001106 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001107 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001108 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001109 spin_unlock(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02001110 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -07001111
Tejun Heo15376632010-06-29 10:07:11 +02001112 for_each_possible_cpu(cpu)
1113 cleanup_workqueue_thread(get_cwq(cpu, wq));
Oleg Nesterov3af244332007-05-09 02:34:09 -07001114
1115 free_percpu(wq->cpu_wq);
1116 kfree(wq);
1117}
1118EXPORT_SYMBOL_GPL(destroy_workqueue);
1119
1120static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1121 unsigned long action,
1122 void *hcpu)
1123{
1124 unsigned int cpu = (unsigned long)hcpu;
1125 struct cpu_workqueue_struct *cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct workqueue_struct *wq;
1127
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001128 action &= ~CPU_TASKS_FROZEN;
1129
Oleg Nesterov3af244332007-05-09 02:34:09 -07001130 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo15376632010-06-29 10:07:11 +02001131 if (wq->flags & WQ_SINGLE_THREAD)
1132 continue;
1133
1134 cwq = get_cwq(cpu, wq);
Christoph Lameter89ada672005-10-30 15:01:59 -08001135
Oleg Nesterov3af244332007-05-09 02:34:09 -07001136 switch (action) {
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001137 case CPU_POST_DEAD:
Tejun Heo15376632010-06-29 10:07:11 +02001138 lock_map_acquire(&cwq->wq->lockdep_map);
1139 lock_map_release(&cwq->wq->lockdep_map);
1140 flush_cpu_workqueue(cwq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Tejun Heo15376632010-06-29 10:07:11 +02001145 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Rusty Russell2d3854a2008-11-05 13:39:10 +11001148#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08001149
Rusty Russell2d3854a2008-11-05 13:39:10 +11001150struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06001151 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001152 long (*fn)(void *);
1153 void *arg;
1154 long ret;
1155};
1156
Andrew Morton6b44003e2009-04-09 09:50:37 -06001157static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11001158{
Andrew Morton6b44003e2009-04-09 09:50:37 -06001159 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001160 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06001161 complete(&wfc->completion);
1162 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11001163}
1164
1165/**
1166 * work_on_cpu - run a function in user context on a particular cpu
1167 * @cpu: the cpu to run on
1168 * @fn: the function to run
1169 * @arg: the function arg
1170 *
Rusty Russell31ad9082009-01-16 15:31:15 -08001171 * This will return the value @fn returns.
1172 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06001173 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11001174 */
1175long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1176{
Andrew Morton6b44003e2009-04-09 09:50:37 -06001177 struct task_struct *sub_thread;
1178 struct work_for_cpu wfc = {
1179 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
1180 .fn = fn,
1181 .arg = arg,
1182 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11001183
Andrew Morton6b44003e2009-04-09 09:50:37 -06001184 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
1185 if (IS_ERR(sub_thread))
1186 return PTR_ERR(sub_thread);
1187 kthread_bind(sub_thread, cpu);
1188 wake_up_process(sub_thread);
1189 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001190 return wfc.ret;
1191}
1192EXPORT_SYMBOL_GPL(work_on_cpu);
1193#endif /* CONFIG_SMP */
1194
Oleg Nesterovc12920d2007-05-09 02:34:14 -07001195void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Rusty Russelle7577c52009-01-01 10:12:25 +10301197 singlethread_cpu = cpumask_first(cpu_possible_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 hotcpu_notifier(workqueue_cpu_callback, 0);
1199 keventd_wq = create_workqueue("events");
1200 BUG_ON(!keventd_wq);
1201}