blob: f57855f718d7c0593fdd7be015f9693ffcf47712 [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>
Tejun Heoc34056a2010-06-29 10:07:11 +020036#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Tejun Heoc8e55f32010-06-29 10:07:12 +020038enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020039 /* global_cwq flags */
40 GCWQ_FREEZING = 1 << 3, /* freeze in progress */
41
Tejun Heoc8e55f32010-06-29 10:07:12 +020042 /* worker flags */
43 WORKER_STARTED = 1 << 0, /* started */
44 WORKER_DIE = 1 << 1, /* die die die */
45 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heodb7bccf2010-06-29 10:07:12 +020046 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
47
48 /* gcwq->trustee_state */
49 TRUSTEE_START = 0, /* start */
50 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
51 TRUSTEE_BUTCHER = 2, /* butcher workers */
52 TRUSTEE_RELEASE = 3, /* release workers */
53 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020054
55 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
56 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
57 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020058
59 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoc8e55f32010-06-29 10:07:12 +020060};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020063 * Structure fields follow one of the following exclusion rules.
64 *
65 * I: Set during initialization and read-only afterwards.
66 *
Tejun Heo8b03ae32010-06-29 10:07:12 +020067 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +020068 *
Tejun Heo73f53c42010-06-29 10:07:11 +020069 * F: wq->flush_mutex protected.
70 *
Tejun Heo4690c4a2010-06-29 10:07:10 +020071 * W: workqueue_lock protected.
72 */
73
Tejun Heo8b03ae32010-06-29 10:07:12 +020074struct global_cwq;
Tejun Heoc34056a2010-06-29 10:07:11 +020075struct cpu_workqueue_struct;
76
77struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +020078 /* on idle list while idle, on busy hash table while busy */
79 union {
80 struct list_head entry; /* L: while idle */
81 struct hlist_node hentry; /* L: while busy */
82 };
83
Tejun Heoc34056a2010-06-29 10:07:11 +020084 struct work_struct *current_work; /* L: work being processed */
Tejun Heoaffee4b2010-06-29 10:07:12 +020085 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +020086 struct task_struct *task; /* I: worker task */
Tejun Heo8b03ae32010-06-29 10:07:12 +020087 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heoc34056a2010-06-29 10:07:11 +020088 struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
Tejun Heoc8e55f32010-06-29 10:07:12 +020089 unsigned int flags; /* L: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +020090 int id; /* I: worker id */
91};
92
Tejun Heo4690c4a2010-06-29 10:07:10 +020093/*
Tejun Heo8b03ae32010-06-29 10:07:12 +020094 * Global per-cpu workqueue.
95 */
96struct global_cwq {
97 spinlock_t lock; /* the gcwq lock */
98 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +020099 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100
101 int nr_workers; /* L: total number of workers */
102 int nr_idle; /* L: currently idle ones */
103
104 /* workers are chained either in the idle_list or busy_hash */
105 struct list_head idle_list; /* L: list of idle workers */
106 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
107 /* L: hash of busy workers */
108
Tejun Heo8b03ae32010-06-29 10:07:12 +0200109 struct ida worker_ida; /* L: for worker IDs */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200110
111 struct task_struct *trustee; /* L: for gcwq shutdown */
112 unsigned int trustee_state; /* L: trustee state */
113 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200114} ____cacheline_aligned_in_smp;
115
116/*
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800117 * The per-CPU workqueue (if single thread, we always use the first
Tejun Heo0f900042010-06-29 10:07:11 +0200118 * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
119 * work_struct->data are used for flags and thus cwqs need to be
120 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122struct cpu_workqueue_struct {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200123 struct global_cwq *gcwq; /* I: the associated gcwq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct list_head worklist;
Tejun Heoc34056a2010-06-29 10:07:11 +0200125 struct worker *worker;
Tejun Heo4690c4a2010-06-29 10:07:10 +0200126 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200127 int work_color; /* L: current color */
128 int flush_color; /* L: flushing color */
129 int nr_in_flight[WORK_NR_COLORS];
130 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200131 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200132 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200133 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200134};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200137 * Structure used to wait for workqueue flush.
138 */
139struct wq_flusher {
140 struct list_head list; /* F: list of flushers */
141 int flush_color; /* F: flush color waiting for */
142 struct completion done; /* flush completion */
143};
144
145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * The externally visible workqueue abstraction is an array of
147 * per-CPU workqueues:
148 */
149struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +0200150 unsigned int flags; /* I: WQ_* flags */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200151 struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
152 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200153
154 struct mutex flush_mutex; /* protects wq flushing */
155 int work_color; /* F: current work color */
156 int flush_color; /* F: current flush color */
157 atomic_t nr_cwqs_to_flush; /* flush in progress */
158 struct wq_flusher *first_flusher; /* F: first flusher */
159 struct list_head flusher_queue; /* F: flush waiters */
160 struct list_head flusher_overflow; /* F: flush overflow list */
161
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200162 int saved_max_active; /* I: saved cwq max_active */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200163 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700164#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200165 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
Tejun Heodb7bccf2010-06-29 10:07:12 +0200169#define for_each_busy_worker(worker, i, pos, gcwq) \
170 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
171 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
172
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900173#ifdef CONFIG_DEBUG_OBJECTS_WORK
174
175static struct debug_obj_descr work_debug_descr;
176
177/*
178 * fixup_init is called when:
179 * - an active object is initialized
180 */
181static int work_fixup_init(void *addr, enum debug_obj_state state)
182{
183 struct work_struct *work = addr;
184
185 switch (state) {
186 case ODEBUG_STATE_ACTIVE:
187 cancel_work_sync(work);
188 debug_object_init(work, &work_debug_descr);
189 return 1;
190 default:
191 return 0;
192 }
193}
194
195/*
196 * fixup_activate is called when:
197 * - an active object is activated
198 * - an unknown object is activated (might be a statically initialized object)
199 */
200static int work_fixup_activate(void *addr, enum debug_obj_state state)
201{
202 struct work_struct *work = addr;
203
204 switch (state) {
205
206 case ODEBUG_STATE_NOTAVAILABLE:
207 /*
208 * This is not really a fixup. The work struct was
209 * statically initialized. We just make sure that it
210 * is tracked in the object tracker.
211 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200212 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900213 debug_object_init(work, &work_debug_descr);
214 debug_object_activate(work, &work_debug_descr);
215 return 0;
216 }
217 WARN_ON_ONCE(1);
218 return 0;
219
220 case ODEBUG_STATE_ACTIVE:
221 WARN_ON(1);
222
223 default:
224 return 0;
225 }
226}
227
228/*
229 * fixup_free is called when:
230 * - an active object is freed
231 */
232static int work_fixup_free(void *addr, enum debug_obj_state state)
233{
234 struct work_struct *work = addr;
235
236 switch (state) {
237 case ODEBUG_STATE_ACTIVE:
238 cancel_work_sync(work);
239 debug_object_free(work, &work_debug_descr);
240 return 1;
241 default:
242 return 0;
243 }
244}
245
246static struct debug_obj_descr work_debug_descr = {
247 .name = "work_struct",
248 .fixup_init = work_fixup_init,
249 .fixup_activate = work_fixup_activate,
250 .fixup_free = work_fixup_free,
251};
252
253static inline void debug_work_activate(struct work_struct *work)
254{
255 debug_object_activate(work, &work_debug_descr);
256}
257
258static inline void debug_work_deactivate(struct work_struct *work)
259{
260 debug_object_deactivate(work, &work_debug_descr);
261}
262
263void __init_work(struct work_struct *work, int onstack)
264{
265 if (onstack)
266 debug_object_init_on_stack(work, &work_debug_descr);
267 else
268 debug_object_init(work, &work_debug_descr);
269}
270EXPORT_SYMBOL_GPL(__init_work);
271
272void destroy_work_on_stack(struct work_struct *work)
273{
274 debug_object_free(work, &work_debug_descr);
275}
276EXPORT_SYMBOL_GPL(destroy_work_on_stack);
277
278#else
279static inline void debug_work_activate(struct work_struct *work) { }
280static inline void debug_work_deactivate(struct work_struct *work) { }
281#endif
282
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100283/* Serializes the accesses to the list of workqueues. */
284static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200286static bool workqueue_freezing; /* W: have wqs started freezing? */
Tejun Heoc34056a2010-06-29 10:07:11 +0200287
Tejun Heo8b03ae32010-06-29 10:07:12 +0200288static DEFINE_PER_CPU(struct global_cwq, global_cwq);
289
Tejun Heoc34056a2010-06-29 10:07:11 +0200290static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Oleg Nesterov3af244332007-05-09 02:34:09 -0700292static int singlethread_cpu __read_mostly;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700293
Tejun Heo8b03ae32010-06-29 10:07:12 +0200294static struct global_cwq *get_gcwq(unsigned int cpu)
295{
296 return &per_cpu(global_cwq, cpu);
297}
298
Tejun Heo4690c4a2010-06-29 10:07:10 +0200299static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
300 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700301{
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700302 return per_cpu_ptr(wq->cpu_wq, cpu);
303}
304
Tejun Heo15376632010-06-29 10:07:11 +0200305static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
306 struct workqueue_struct *wq)
307{
308 if (unlikely(wq->flags & WQ_SINGLE_THREAD))
309 cpu = singlethread_cpu;
310 return get_cwq(cpu, wq);
311}
312
Tejun Heo73f53c42010-06-29 10:07:11 +0200313static unsigned int work_color_to_flags(int color)
314{
315 return color << WORK_STRUCT_COLOR_SHIFT;
316}
317
318static int get_work_color(struct work_struct *work)
319{
320 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
321 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
322}
323
324static int work_next_color(int color)
325{
326 return (color + 1) % WORK_NR_COLORS;
327}
328
David Howells4594bf12006-12-07 11:33:26 +0000329/*
330 * Set the workqueue on which a work item is to be run
331 * - Must *only* be called if the pending flag is set
332 */
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700333static inline void set_wq_data(struct work_struct *work,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200334 struct cpu_workqueue_struct *cwq,
335 unsigned long extra_flags)
David Howells365970a2006-11-22 14:54:49 +0000336{
David Howells4594bf12006-12-07 11:33:26 +0000337 BUG_ON(!work_pending(work));
338
Tejun Heo4690c4a2010-06-29 10:07:10 +0200339 atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
Tejun Heo22df02b2010-06-29 10:07:10 +0200340 WORK_STRUCT_PENDING | extra_flags);
David Howells365970a2006-11-22 14:54:49 +0000341}
342
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200343/*
344 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
345 */
346static inline void clear_wq_data(struct work_struct *work)
347{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200348 atomic_long_set(&work->data, work_static(work));
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200349}
350
Tejun Heo64166692010-06-29 10:07:11 +0200351static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
David Howells365970a2006-11-22 14:54:49 +0000352{
Tejun Heo64166692010-06-29 10:07:11 +0200353 return (void *)(atomic_long_read(&work->data) &
354 WORK_STRUCT_WQ_DATA_MASK);
David Howells365970a2006-11-22 14:54:49 +0000355}
356
Tejun Heo4690c4a2010-06-29 10:07:10 +0200357/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200358 * busy_worker_head - return the busy hash head for a work
359 * @gcwq: gcwq of interest
360 * @work: work to be hashed
361 *
362 * Return hash head of @gcwq for @work.
363 *
364 * CONTEXT:
365 * spin_lock_irq(gcwq->lock).
366 *
367 * RETURNS:
368 * Pointer to the hash head.
369 */
370static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
371 struct work_struct *work)
372{
373 const int base_shift = ilog2(sizeof(struct work_struct));
374 unsigned long v = (unsigned long)work;
375
376 /* simple shift and fold hash, do we need something better? */
377 v >>= base_shift;
378 v += v >> BUSY_WORKER_HASH_ORDER;
379 v &= BUSY_WORKER_HASH_MASK;
380
381 return &gcwq->busy_hash[v];
382}
383
384/**
Tejun Heo4690c4a2010-06-29 10:07:10 +0200385 * insert_work - insert a work into cwq
386 * @cwq: cwq @work belongs to
387 * @work: work to insert
388 * @head: insertion point
389 * @extra_flags: extra WORK_STRUCT_* flags to set
390 *
391 * Insert @work into @cwq after @head.
392 *
393 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200394 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200395 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700396static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200397 struct work_struct *work, struct list_head *head,
398 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700399{
Tejun Heo4690c4a2010-06-29 10:07:10 +0200400 /* we own @work, set data and link */
401 set_wq_data(work, cwq, extra_flags);
402
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700403 /*
404 * Ensure that we get the right work->data if we see the
405 * result of list_add() below, see try_to_grab_pending().
406 */
407 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200408
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700409 list_add_tail(&work->entry, head);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200410 wake_up_process(cwq->worker->task);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700411}
412
Tejun Heo4690c4a2010-06-29 10:07:10 +0200413static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct work_struct *work)
415{
Tejun Heo15376632010-06-29 10:07:11 +0200416 struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +0200417 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200418 struct list_head *worklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned long flags;
420
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900421 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200422
Tejun Heo8b03ae32010-06-29 10:07:12 +0200423 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200424 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200425
Tejun Heo73f53c42010-06-29 10:07:11 +0200426 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200427
428 if (likely(cwq->nr_active < cwq->max_active)) {
429 cwq->nr_active++;
430 worklist = &cwq->worklist;
431 } else
432 worklist = &cwq->delayed_works;
433
434 insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
435
Tejun Heo8b03ae32010-06-29 10:07:12 +0200436 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700439/**
440 * queue_work - queue work on a workqueue
441 * @wq: workqueue to use
442 * @work: work to queue
443 *
Alan Stern057647f2006-10-28 10:38:58 -0700444 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -0700446 * We queue the work to the CPU on which it was submitted, but if the CPU dies
447 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800449int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700451 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Oleg Nesterovef1ca232008-07-25 01:47:53 -0700453 ret = queue_work_on(get_cpu(), wq, work);
454 put_cpu();
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return ret;
457}
Dave Jonesae90dd52006-06-30 01:40:45 -0400458EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Zhang Ruic1a220e2008-07-23 21:28:39 -0700460/**
461 * queue_work_on - queue work on specific cpu
462 * @cpu: CPU number to execute work on
463 * @wq: workqueue to use
464 * @work: work to queue
465 *
466 * Returns 0 if @work was already on a queue, non-zero otherwise.
467 *
468 * We queue the work to a specific CPU, the caller must ensure it
469 * can't go away.
470 */
471int
472queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
473{
474 int ret = 0;
475
Tejun Heo22df02b2010-06-29 10:07:10 +0200476 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +0200477 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -0700478 ret = 1;
479 }
480 return ret;
481}
482EXPORT_SYMBOL_GPL(queue_work_on);
483
Li Zefan6d141c32008-02-08 04:21:09 -0800484static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
David Howells52bad642006-11-22 14:54:01 +0000486 struct delayed_work *dwork = (struct delayed_work *)__data;
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700487 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Tejun Heo4690c4a2010-06-29 10:07:10 +0200489 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700492/**
493 * queue_delayed_work - queue work on a workqueue after delay
494 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800495 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700496 * @delay: number of jiffies to wait before queueing
497 *
Alan Stern057647f2006-10-28 10:38:58 -0700498 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700499 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800500int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000501 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
David Howells52bad642006-11-22 14:54:01 +0000503 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700504 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700506 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
Dave Jonesae90dd52006-06-30 01:40:45 -0400508EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700510/**
511 * queue_delayed_work_on - queue work on specific CPU after delay
512 * @cpu: CPU number to execute work on
513 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800514 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700515 * @delay: number of jiffies to wait before queueing
516 *
Alan Stern057647f2006-10-28 10:38:58 -0700517 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -0700518 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700519int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +0000520 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700521{
522 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +0000523 struct timer_list *timer = &dwork->timer;
524 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700525
Tejun Heo22df02b2010-06-29 10:07:10 +0200526 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700527 BUG_ON(timer_pending(timer));
528 BUG_ON(!list_empty(&work->entry));
529
Andrew Liu8a3e77c2008-05-01 04:35:14 -0700530 timer_stats_timer_set_start_info(&dwork->timer);
531
Oleg Nesteroved7c0fe2007-05-09 02:34:16 -0700532 /* This stores cwq for the moment, for the timer_fn */
Tejun Heo15376632010-06-29 10:07:11 +0200533 set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700534 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +0000535 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700536 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -0700537
538 if (unlikely(cpu >= 0))
539 add_timer_on(timer, cpu);
540 else
541 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700542 ret = 1;
543 }
544 return ret;
545}
Dave Jonesae90dd52006-06-30 01:40:45 -0400546EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Tejun Heoc8e55f32010-06-29 10:07:12 +0200548/**
549 * worker_enter_idle - enter idle state
550 * @worker: worker which is entering idle state
551 *
552 * @worker is entering idle state. Update stats and idle timer if
553 * necessary.
554 *
555 * LOCKING:
556 * spin_lock_irq(gcwq->lock).
557 */
558static void worker_enter_idle(struct worker *worker)
559{
560 struct global_cwq *gcwq = worker->gcwq;
561
562 BUG_ON(worker->flags & WORKER_IDLE);
563 BUG_ON(!list_empty(&worker->entry) &&
564 (worker->hentry.next || worker->hentry.pprev));
565
566 worker->flags |= WORKER_IDLE;
567 gcwq->nr_idle++;
568
569 /* idle_list is LIFO */
570 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +0200571
572 if (unlikely(worker->flags & WORKER_ROGUE))
573 wake_up_all(&gcwq->trustee_wait);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200574}
575
576/**
577 * worker_leave_idle - leave idle state
578 * @worker: worker which is leaving idle state
579 *
580 * @worker is leaving idle state. Update stats.
581 *
582 * LOCKING:
583 * spin_lock_irq(gcwq->lock).
584 */
585static void worker_leave_idle(struct worker *worker)
586{
587 struct global_cwq *gcwq = worker->gcwq;
588
589 BUG_ON(!(worker->flags & WORKER_IDLE));
590 worker->flags &= ~WORKER_IDLE;
591 gcwq->nr_idle--;
592 list_del_init(&worker->entry);
593}
594
Tejun Heoc34056a2010-06-29 10:07:11 +0200595static struct worker *alloc_worker(void)
596{
597 struct worker *worker;
598
599 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200600 if (worker) {
601 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +0200602 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200603 }
Tejun Heoc34056a2010-06-29 10:07:11 +0200604 return worker;
605}
606
607/**
608 * create_worker - create a new workqueue worker
609 * @cwq: cwq the new worker will belong to
610 * @bind: whether to set affinity to @cpu or not
611 *
612 * Create a new worker which is bound to @cwq. The returned worker
613 * can be started by calling start_worker() or destroyed using
614 * destroy_worker().
615 *
616 * CONTEXT:
617 * Might sleep. Does GFP_KERNEL allocations.
618 *
619 * RETURNS:
620 * Pointer to the newly created worker.
621 */
622static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
623{
Tejun Heo8b03ae32010-06-29 10:07:12 +0200624 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200625 int id = -1;
626 struct worker *worker = NULL;
627
Tejun Heo8b03ae32010-06-29 10:07:12 +0200628 spin_lock_irq(&gcwq->lock);
629 while (ida_get_new(&gcwq->worker_ida, &id)) {
630 spin_unlock_irq(&gcwq->lock);
631 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +0200632 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200633 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +0200634 }
Tejun Heo8b03ae32010-06-29 10:07:12 +0200635 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +0200636
637 worker = alloc_worker();
638 if (!worker)
639 goto fail;
640
Tejun Heo8b03ae32010-06-29 10:07:12 +0200641 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200642 worker->cwq = cwq;
643 worker->id = id;
644
645 worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
Tejun Heo8b03ae32010-06-29 10:07:12 +0200646 gcwq->cpu, id);
Tejun Heoc34056a2010-06-29 10:07:11 +0200647 if (IS_ERR(worker->task))
648 goto fail;
649
Tejun Heodb7bccf2010-06-29 10:07:12 +0200650 /*
651 * A rogue worker will become a regular one if CPU comes
652 * online later on. Make sure every worker has
653 * PF_THREAD_BOUND set.
654 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200655 if (bind)
Tejun Heo8b03ae32010-06-29 10:07:12 +0200656 kthread_bind(worker->task, gcwq->cpu);
Tejun Heodb7bccf2010-06-29 10:07:12 +0200657 else
658 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +0200659
660 return worker;
661fail:
662 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200663 spin_lock_irq(&gcwq->lock);
664 ida_remove(&gcwq->worker_ida, id);
665 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +0200666 }
667 kfree(worker);
668 return NULL;
669}
670
671/**
672 * start_worker - start a newly created worker
673 * @worker: worker to start
674 *
Tejun Heoc8e55f32010-06-29 10:07:12 +0200675 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +0200676 *
677 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200678 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +0200679 */
680static void start_worker(struct worker *worker)
681{
Tejun Heoc8e55f32010-06-29 10:07:12 +0200682 worker->flags |= WORKER_STARTED;
683 worker->gcwq->nr_workers++;
684 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +0200685 wake_up_process(worker->task);
686}
687
688/**
689 * destroy_worker - destroy a workqueue worker
690 * @worker: worker to be destroyed
691 *
Tejun Heoc8e55f32010-06-29 10:07:12 +0200692 * Destroy @worker and adjust @gcwq stats accordingly.
693 *
694 * CONTEXT:
695 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +0200696 */
697static void destroy_worker(struct worker *worker)
698{
Tejun Heo8b03ae32010-06-29 10:07:12 +0200699 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200700 int id = worker->id;
701
702 /* sanity check frenzy */
703 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +0200704 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +0200705
Tejun Heoc8e55f32010-06-29 10:07:12 +0200706 if (worker->flags & WORKER_STARTED)
707 gcwq->nr_workers--;
708 if (worker->flags & WORKER_IDLE)
709 gcwq->nr_idle--;
710
711 list_del_init(&worker->entry);
712 worker->flags |= WORKER_DIE;
713
714 spin_unlock_irq(&gcwq->lock);
715
Tejun Heoc34056a2010-06-29 10:07:11 +0200716 kthread_stop(worker->task);
717 kfree(worker);
718
Tejun Heo8b03ae32010-06-29 10:07:12 +0200719 spin_lock_irq(&gcwq->lock);
720 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +0200721}
722
Tejun Heoa62428c2010-06-29 10:07:10 +0200723/**
Tejun Heoaffee4b2010-06-29 10:07:12 +0200724 * move_linked_works - move linked works to a list
725 * @work: start of series of works to be scheduled
726 * @head: target list to append @work to
727 * @nextp: out paramter for nested worklist walking
728 *
729 * Schedule linked works starting from @work to @head. Work series to
730 * be scheduled starts at @work and includes any consecutive work with
731 * WORK_STRUCT_LINKED set in its predecessor.
732 *
733 * If @nextp is not NULL, it's updated to point to the next work of
734 * the last scheduled work. This allows move_linked_works() to be
735 * nested inside outer list_for_each_entry_safe().
736 *
737 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200738 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +0200739 */
740static void move_linked_works(struct work_struct *work, struct list_head *head,
741 struct work_struct **nextp)
742{
743 struct work_struct *n;
744
745 /*
746 * Linked worklist will always end before the end of the list,
747 * use NULL for list head.
748 */
749 list_for_each_entry_safe_from(work, n, NULL, entry) {
750 list_move_tail(&work->entry, head);
751 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
752 break;
753 }
754
755 /*
756 * If we're already inside safe list traversal and have moved
757 * multiple works to the scheduled queue, the next position
758 * needs to be updated.
759 */
760 if (nextp)
761 *nextp = n;
762}
763
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200764static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
765{
766 struct work_struct *work = list_first_entry(&cwq->delayed_works,
767 struct work_struct, entry);
768
769 move_linked_works(work, &cwq->worklist, NULL);
770 cwq->nr_active++;
771}
772
Tejun Heoaffee4b2010-06-29 10:07:12 +0200773/**
Tejun Heo73f53c42010-06-29 10:07:11 +0200774 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
775 * @cwq: cwq of interest
776 * @color: color of work which left the queue
777 *
778 * A work either has completed or is removed from pending queue,
779 * decrement nr_in_flight of its cwq and handle workqueue flushing.
780 *
781 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200782 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +0200783 */
784static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
785{
786 /* ignore uncolored works */
787 if (color == WORK_NO_COLOR)
788 return;
789
790 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200791 cwq->nr_active--;
792
793 /* one down, submit a delayed one */
794 if (!list_empty(&cwq->delayed_works) &&
795 cwq->nr_active < cwq->max_active)
796 cwq_activate_first_delayed(cwq);
Tejun Heo73f53c42010-06-29 10:07:11 +0200797
798 /* is flush in progress and are we at the flushing tip? */
799 if (likely(cwq->flush_color != color))
800 return;
801
802 /* are there still in-flight works? */
803 if (cwq->nr_in_flight[color])
804 return;
805
806 /* this cwq is done, clear flush_color */
807 cwq->flush_color = -1;
808
809 /*
810 * If this was the last cwq, wake up the first flusher. It
811 * will handle the rest.
812 */
813 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
814 complete(&cwq->wq->first_flusher->done);
815}
816
817/**
Tejun Heoa62428c2010-06-29 10:07:10 +0200818 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +0200819 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +0200820 * @work: work to process
821 *
822 * Process @work. This function contains all the logics necessary to
823 * process a single work including synchronization against and
824 * interaction with other workers on the same cpu, queueing and
825 * flushing. As long as context requirement is met, any worker can
826 * call this function to process a work.
827 *
828 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200829 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +0200830 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200831static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heoa62428c2010-06-29 10:07:10 +0200832{
Tejun Heoc34056a2010-06-29 10:07:11 +0200833 struct cpu_workqueue_struct *cwq = worker->cwq;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200834 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +0200835 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heoa62428c2010-06-29 10:07:10 +0200836 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +0200837 int work_color;
Tejun Heoa62428c2010-06-29 10:07:10 +0200838#ifdef CONFIG_LOCKDEP
839 /*
840 * It is permissible to free the struct work_struct from
841 * inside the function that is called from it, this we need to
842 * take into account for lockdep too. To avoid bogus "held
843 * lock freed" warnings as well as problems when looking into
844 * work->lockdep_map, make a copy and use that here.
845 */
846 struct lockdep_map lockdep_map = work->lockdep_map;
847#endif
848 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +0200849 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200850 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +0200851 worker->current_work = work;
Tejun Heo73f53c42010-06-29 10:07:11 +0200852 work_color = get_work_color(work);
Tejun Heoa62428c2010-06-29 10:07:10 +0200853 list_del_init(&work->entry);
854
Tejun Heo8b03ae32010-06-29 10:07:12 +0200855 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +0200856
857 BUG_ON(get_wq_data(work) != cwq);
858 work_clear_pending(work);
859 lock_map_acquire(&cwq->wq->lockdep_map);
860 lock_map_acquire(&lockdep_map);
861 f(work);
862 lock_map_release(&lockdep_map);
863 lock_map_release(&cwq->wq->lockdep_map);
864
865 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
866 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
867 "%s/0x%08x/%d\n",
868 current->comm, preempt_count(), task_pid_nr(current));
869 printk(KERN_ERR " last function: ");
870 print_symbol("%s\n", (unsigned long)f);
871 debug_show_held_locks(current);
872 dump_stack();
873 }
874
Tejun Heo8b03ae32010-06-29 10:07:12 +0200875 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +0200876
877 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200878 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +0200879 worker->current_work = NULL;
Tejun Heo73f53c42010-06-29 10:07:11 +0200880 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +0200881}
882
Tejun Heoaffee4b2010-06-29 10:07:12 +0200883/**
884 * process_scheduled_works - process scheduled works
885 * @worker: self
886 *
887 * Process all scheduled works. Please note that the scheduled list
888 * may change while processing a work, so this function repeatedly
889 * fetches a work from the top and executes it.
890 *
891 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200892 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +0200893 * multiple times.
894 */
895static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Tejun Heoaffee4b2010-06-29 10:07:12 +0200897 while (!list_empty(&worker->scheduled)) {
898 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +0200900 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Tejun Heo4690c4a2010-06-29 10:07:10 +0200904/**
905 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +0200906 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +0200907 *
908 * The cwq worker thread function.
909 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200910static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Tejun Heoc34056a2010-06-29 10:07:11 +0200912 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200913 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200914 struct cpu_workqueue_struct *cwq = worker->cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Tejun Heoc8e55f32010-06-29 10:07:12 +0200916woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +0200917 spin_lock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -0700918
Tejun Heoc8e55f32010-06-29 10:07:12 +0200919 /* DIE can be set only while we're idle, checking here is enough */
920 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200921 spin_unlock_irq(&gcwq->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Oleg Nesterov3af244332007-05-09 02:34:09 -0700924
Tejun Heoc8e55f32010-06-29 10:07:12 +0200925 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +0200926recheck:
Tejun Heoc8e55f32010-06-29 10:07:12 +0200927 /*
928 * ->scheduled list can only be filled while a worker is
929 * preparing to process a work or actually processing it.
930 * Make sure nobody diddled with it while I was sleeping.
931 */
932 BUG_ON(!list_empty(&worker->scheduled));
933
934 while (!list_empty(&cwq->worklist)) {
935 struct work_struct *work =
936 list_first_entry(&cwq->worklist,
937 struct work_struct, entry);
938
Tejun Heodb7bccf2010-06-29 10:07:12 +0200939 /*
940 * The following is a rather inefficient way to close
941 * race window against cpu hotplug operations. Will
942 * be replaced soon.
943 */
944 if (unlikely(!(worker->flags & WORKER_ROGUE) &&
945 !cpumask_equal(&worker->task->cpus_allowed,
946 get_cpu_mask(gcwq->cpu)))) {
947 spin_unlock_irq(&gcwq->lock);
948 set_cpus_allowed_ptr(worker->task,
949 get_cpu_mask(gcwq->cpu));
950 cpu_relax();
951 spin_lock_irq(&gcwq->lock);
952 goto recheck;
953 }
954
Tejun Heoc8e55f32010-06-29 10:07:12 +0200955 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
956 /* optimization path, not strictly necessary */
957 process_one_work(worker, work);
958 if (unlikely(!list_empty(&worker->scheduled)))
959 process_scheduled_works(worker);
960 } else {
961 move_linked_works(work, &worker->scheduled, NULL);
962 process_scheduled_works(worker);
963 }
964 }
965
966 /*
967 * gcwq->lock is held and there's no work to process, sleep.
968 * Workers are woken up only while holding gcwq->lock, so
969 * setting the current state before releasing gcwq->lock is
970 * enough to prevent losing any event.
971 */
972 worker_enter_idle(worker);
973 __set_current_state(TASK_INTERRUPTIBLE);
974 spin_unlock_irq(&gcwq->lock);
975 schedule();
976 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -0700979struct wq_barrier {
980 struct work_struct work;
981 struct completion done;
982};
983
984static void wq_barrier_func(struct work_struct *work)
985{
986 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
987 complete(&barr->done);
988}
989
Tejun Heo4690c4a2010-06-29 10:07:10 +0200990/**
991 * insert_wq_barrier - insert a barrier work
992 * @cwq: cwq to insert barrier into
993 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +0200994 * @target: target work to attach @barr to
995 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +0200996 *
Tejun Heoaffee4b2010-06-29 10:07:12 +0200997 * @barr is linked to @target such that @barr is completed only after
998 * @target finishes execution. Please note that the ordering
999 * guarantee is observed only with respect to @target and on the local
1000 * cpu.
1001 *
1002 * Currently, a queued barrier can't be canceled. This is because
1003 * try_to_grab_pending() can't determine whether the work to be
1004 * grabbed is at the head of the queue and thus can't clear LINKED
1005 * flag of the previous work while there must be a valid next work
1006 * after a work with LINKED flag set.
1007 *
1008 * Note that when @worker is non-NULL, @target may be modified
1009 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001010 *
1011 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001012 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001013 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07001014static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02001015 struct wq_barrier *barr,
1016 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07001017{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001018 struct list_head *head;
1019 unsigned int linked = 0;
1020
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001021 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02001022 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001023 * as we know for sure that this will not trigger any of the
1024 * checks and call back into the fixup functions where we
1025 * might deadlock.
1026 */
1027 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02001028 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07001029 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07001030
Tejun Heoaffee4b2010-06-29 10:07:12 +02001031 /*
1032 * If @target is currently being executed, schedule the
1033 * barrier to the worker; otherwise, put it after @target.
1034 */
1035 if (worker)
1036 head = worker->scheduled.next;
1037 else {
1038 unsigned long *bits = work_data_bits(target);
1039
1040 head = target->entry.next;
1041 /* there can already be other linked works, inherit and set */
1042 linked = *bits & WORK_STRUCT_LINKED;
1043 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
1044 }
1045
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001046 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001047 insert_work(cwq, &barr->work, head,
1048 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07001049}
1050
Tejun Heo73f53c42010-06-29 10:07:11 +02001051/**
1052 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
1053 * @wq: workqueue being flushed
1054 * @flush_color: new flush color, < 0 for no-op
1055 * @work_color: new work color, < 0 for no-op
1056 *
1057 * Prepare cwqs for workqueue flushing.
1058 *
1059 * If @flush_color is non-negative, flush_color on all cwqs should be
1060 * -1. If no cwq has in-flight commands at the specified color, all
1061 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
1062 * has in flight commands, its cwq->flush_color is set to
1063 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
1064 * wakeup logic is armed and %true is returned.
1065 *
1066 * The caller should have initialized @wq->first_flusher prior to
1067 * calling this function with non-negative @flush_color. If
1068 * @flush_color is negative, no flush color update is done and %false
1069 * is returned.
1070 *
1071 * If @work_color is non-negative, all cwqs should have the same
1072 * work_color which is previous to @work_color and all will be
1073 * advanced to @work_color.
1074 *
1075 * CONTEXT:
1076 * mutex_lock(wq->flush_mutex).
1077 *
1078 * RETURNS:
1079 * %true if @flush_color >= 0 and there's something to flush. %false
1080 * otherwise.
1081 */
1082static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
1083 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Tejun Heo73f53c42010-06-29 10:07:11 +02001085 bool wait = false;
1086 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07001087
Tejun Heo73f53c42010-06-29 10:07:11 +02001088 if (flush_color >= 0) {
1089 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
1090 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001091 }
Oleg Nesterov14441962007-05-23 13:57:57 -07001092
Tejun Heo73f53c42010-06-29 10:07:11 +02001093 for_each_possible_cpu(cpu) {
1094 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001095 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001096
Tejun Heo8b03ae32010-06-29 10:07:12 +02001097 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02001098
1099 if (flush_color >= 0) {
1100 BUG_ON(cwq->flush_color != -1);
1101
1102 if (cwq->nr_in_flight[flush_color]) {
1103 cwq->flush_color = flush_color;
1104 atomic_inc(&wq->nr_cwqs_to_flush);
1105 wait = true;
1106 }
1107 }
1108
1109 if (work_color >= 0) {
1110 BUG_ON(work_color != work_next_color(cwq->work_color));
1111 cwq->work_color = work_color;
1112 }
1113
Tejun Heo8b03ae32010-06-29 10:07:12 +02001114 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02001115 }
1116
1117 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
1118 complete(&wq->first_flusher->done);
1119
1120 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001123/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001125 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 *
1127 * Forces execution of the workqueue and blocks until its completion.
1128 * This is typically used in driver shutdown handlers.
1129 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07001130 * We sleep until all works which were queued on entry have been handled,
1131 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001133void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Tejun Heo73f53c42010-06-29 10:07:11 +02001135 struct wq_flusher this_flusher = {
1136 .list = LIST_HEAD_INIT(this_flusher.list),
1137 .flush_color = -1,
1138 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
1139 };
1140 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001141
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001142 lock_map_acquire(&wq->lockdep_map);
1143 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02001144
1145 mutex_lock(&wq->flush_mutex);
1146
1147 /*
1148 * Start-to-wait phase
1149 */
1150 next_color = work_next_color(wq->work_color);
1151
1152 if (next_color != wq->flush_color) {
1153 /*
1154 * Color space is not full. The current work_color
1155 * becomes our flush_color and work_color is advanced
1156 * by one.
1157 */
1158 BUG_ON(!list_empty(&wq->flusher_overflow));
1159 this_flusher.flush_color = wq->work_color;
1160 wq->work_color = next_color;
1161
1162 if (!wq->first_flusher) {
1163 /* no flush in progress, become the first flusher */
1164 BUG_ON(wq->flush_color != this_flusher.flush_color);
1165
1166 wq->first_flusher = &this_flusher;
1167
1168 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
1169 wq->work_color)) {
1170 /* nothing to flush, done */
1171 wq->flush_color = next_color;
1172 wq->first_flusher = NULL;
1173 goto out_unlock;
1174 }
1175 } else {
1176 /* wait in queue */
1177 BUG_ON(wq->flush_color == this_flusher.flush_color);
1178 list_add_tail(&this_flusher.list, &wq->flusher_queue);
1179 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
1180 }
1181 } else {
1182 /*
1183 * Oops, color space is full, wait on overflow queue.
1184 * The next flush completion will assign us
1185 * flush_color and transfer to flusher_queue.
1186 */
1187 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
1188 }
1189
1190 mutex_unlock(&wq->flush_mutex);
1191
1192 wait_for_completion(&this_flusher.done);
1193
1194 /*
1195 * Wake-up-and-cascade phase
1196 *
1197 * First flushers are responsible for cascading flushes and
1198 * handling overflow. Non-first flushers can simply return.
1199 */
1200 if (wq->first_flusher != &this_flusher)
1201 return;
1202
1203 mutex_lock(&wq->flush_mutex);
1204
1205 wq->first_flusher = NULL;
1206
1207 BUG_ON(!list_empty(&this_flusher.list));
1208 BUG_ON(wq->flush_color != this_flusher.flush_color);
1209
1210 while (true) {
1211 struct wq_flusher *next, *tmp;
1212
1213 /* complete all the flushers sharing the current flush color */
1214 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
1215 if (next->flush_color != wq->flush_color)
1216 break;
1217 list_del_init(&next->list);
1218 complete(&next->done);
1219 }
1220
1221 BUG_ON(!list_empty(&wq->flusher_overflow) &&
1222 wq->flush_color != work_next_color(wq->work_color));
1223
1224 /* this flush_color is finished, advance by one */
1225 wq->flush_color = work_next_color(wq->flush_color);
1226
1227 /* one color has been freed, handle overflow queue */
1228 if (!list_empty(&wq->flusher_overflow)) {
1229 /*
1230 * Assign the same color to all overflowed
1231 * flushers, advance work_color and append to
1232 * flusher_queue. This is the start-to-wait
1233 * phase for these overflowed flushers.
1234 */
1235 list_for_each_entry(tmp, &wq->flusher_overflow, list)
1236 tmp->flush_color = wq->work_color;
1237
1238 wq->work_color = work_next_color(wq->work_color);
1239
1240 list_splice_tail_init(&wq->flusher_overflow,
1241 &wq->flusher_queue);
1242 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
1243 }
1244
1245 if (list_empty(&wq->flusher_queue)) {
1246 BUG_ON(wq->flush_color != wq->work_color);
1247 break;
1248 }
1249
1250 /*
1251 * Need to flush more colors. Make the next flusher
1252 * the new first flusher and arm cwqs.
1253 */
1254 BUG_ON(wq->flush_color == wq->work_color);
1255 BUG_ON(wq->flush_color != next->flush_color);
1256
1257 list_del_init(&next->list);
1258 wq->first_flusher = next;
1259
1260 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
1261 break;
1262
1263 /*
1264 * Meh... this color is already done, clear first
1265 * flusher and repeat cascading.
1266 */
1267 wq->first_flusher = NULL;
1268 }
1269
1270out_unlock:
1271 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
Dave Jonesae90dd52006-06-30 01:40:45 -04001273EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Oleg Nesterovdb700892008-07-25 01:47:49 -07001275/**
1276 * flush_work - block until a work_struct's callback has terminated
1277 * @work: the work which is to be flushed
1278 *
Oleg Nesterova67da702008-07-25 01:47:52 -07001279 * Returns false if @work has already terminated.
1280 *
Oleg Nesterovdb700892008-07-25 01:47:49 -07001281 * It is expected that, prior to calling flush_work(), the caller has
1282 * arranged for the work to not be requeued, otherwise it doesn't make
1283 * sense to use this function.
1284 */
1285int flush_work(struct work_struct *work)
1286{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001287 struct worker *worker = NULL;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001288 struct cpu_workqueue_struct *cwq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001289 struct global_cwq *gcwq;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001290 struct wq_barrier barr;
1291
1292 might_sleep();
1293 cwq = get_wq_data(work);
1294 if (!cwq)
1295 return 0;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001296 gcwq = cwq->gcwq;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001297
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001298 lock_map_acquire(&cwq->wq->lockdep_map);
1299 lock_map_release(&cwq->wq->lockdep_map);
Oleg Nesterova67da702008-07-25 01:47:52 -07001300
Tejun Heo8b03ae32010-06-29 10:07:12 +02001301 spin_lock_irq(&gcwq->lock);
Oleg Nesterovdb700892008-07-25 01:47:49 -07001302 if (!list_empty(&work->entry)) {
1303 /*
1304 * See the comment near try_to_grab_pending()->smp_rmb().
1305 * If it was re-queued under us we are not going to wait.
1306 */
1307 smp_rmb();
1308 if (unlikely(cwq != get_wq_data(work)))
Tejun Heo4690c4a2010-06-29 10:07:10 +02001309 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001310 } else {
Tejun Heoaffee4b2010-06-29 10:07:12 +02001311 if (cwq->worker && cwq->worker->current_work == work)
1312 worker = cwq->worker;
1313 if (!worker)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001314 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001315 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07001316
Tejun Heoaffee4b2010-06-29 10:07:12 +02001317 insert_wq_barrier(cwq, &barr, work, worker);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001318 spin_unlock_irq(&gcwq->lock);
Oleg Nesterovdb700892008-07-25 01:47:49 -07001319 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001320 destroy_work_on_stack(&barr.work);
Oleg Nesterovdb700892008-07-25 01:47:49 -07001321 return 1;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001322already_gone:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001323 spin_unlock_irq(&gcwq->lock);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001324 return 0;
Oleg Nesterovdb700892008-07-25 01:47:49 -07001325}
1326EXPORT_SYMBOL_GPL(flush_work);
1327
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001328/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001329 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001330 * so this work can't be re-armed in any way.
1331 */
1332static int try_to_grab_pending(struct work_struct *work)
1333{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001334 struct global_cwq *gcwq;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001335 struct cpu_workqueue_struct *cwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001336 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001337
Tejun Heo22df02b2010-06-29 10:07:10 +02001338 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001339 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001340
1341 /*
1342 * The queueing is in progress, or it is already queued. Try to
1343 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1344 */
1345
1346 cwq = get_wq_data(work);
1347 if (!cwq)
1348 return ret;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001349 gcwq = cwq->gcwq;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001350
Tejun Heo8b03ae32010-06-29 10:07:12 +02001351 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001352 if (!list_empty(&work->entry)) {
1353 /*
1354 * This work is queued, but perhaps we locked the wrong cwq.
1355 * In that case we must see the new value after rmb(), see
1356 * insert_work()->wmb().
1357 */
1358 smp_rmb();
1359 if (cwq == get_wq_data(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001360 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001361 list_del_init(&work->entry);
Tejun Heo73f53c42010-06-29 10:07:11 +02001362 cwq_dec_nr_in_flight(cwq, get_work_color(work));
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001363 ret = 1;
1364 }
1365 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001366 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001367
1368 return ret;
1369}
1370
1371static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001372 struct work_struct *work)
1373{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001374 struct global_cwq *gcwq = cwq->gcwq;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001375 struct wq_barrier barr;
Tejun Heoaffee4b2010-06-29 10:07:12 +02001376 struct worker *worker;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001377
Tejun Heo8b03ae32010-06-29 10:07:12 +02001378 spin_lock_irq(&gcwq->lock);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001379
1380 worker = NULL;
Tejun Heoc34056a2010-06-29 10:07:11 +02001381 if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
Tejun Heoaffee4b2010-06-29 10:07:12 +02001382 worker = cwq->worker;
1383 insert_wq_barrier(cwq, &barr, work, worker);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001384 }
Tejun Heoaffee4b2010-06-29 10:07:12 +02001385
Tejun Heo8b03ae32010-06-29 10:07:12 +02001386 spin_unlock_irq(&gcwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001387
Tejun Heoaffee4b2010-06-29 10:07:12 +02001388 if (unlikely(worker)) {
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001389 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001390 destroy_work_on_stack(&barr.work);
1391 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001392}
1393
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001394static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001395{
1396 struct cpu_workqueue_struct *cwq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001397 struct workqueue_struct *wq;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001398 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001399
Oleg Nesterovf293ea92007-05-09 02:34:10 -07001400 might_sleep();
1401
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001402 lock_map_acquire(&work->lockdep_map);
1403 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -07001404
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001405 cwq = get_wq_data(work);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001406 if (!cwq)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001407 return;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001408
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001409 wq = cwq->wq;
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001410
Tejun Heo15376632010-06-29 10:07:11 +02001411 for_each_possible_cpu(cpu)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001412 wait_on_cpu_work(get_cwq(cpu, wq), work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001413}
1414
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001415static int __cancel_work_timer(struct work_struct *work,
1416 struct timer_list* timer)
1417{
1418 int ret;
1419
1420 do {
1421 ret = (timer && likely(del_timer(timer)));
1422 if (!ret)
1423 ret = try_to_grab_pending(work);
1424 wait_on_work(work);
1425 } while (unlikely(ret < 0));
1426
Oleg Nesterov4d707b92010-04-23 17:40:40 +02001427 clear_wq_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001428 return ret;
1429}
1430
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001431/**
1432 * cancel_work_sync - block until a work_struct's callback has terminated
1433 * @work: the work which is to be flushed
1434 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001435 * Returns true if @work was pending.
1436 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001437 * cancel_work_sync() will cancel the work if it is queued. If the work's
1438 * callback appears to be running, cancel_work_sync() will block until it
1439 * has completed.
1440 *
1441 * It is possible to use this function if the work re-queues itself. It can
1442 * cancel the work even if it migrates to another workqueue, however in that
1443 * case it only guarantees that work->func() has completed on the last queued
1444 * workqueue.
1445 *
1446 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
1447 * pending, otherwise it goes into a busy-wait loop until the timer expires.
1448 *
1449 * The caller must ensure that workqueue_struct on which this work was last
1450 * queued can't be destroyed before this function returns.
1451 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001452int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001453{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001454 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001455}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001456EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001457
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001458/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07001459 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001460 * @dwork: the delayed work struct
1461 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001462 * Returns true if @dwork was pending.
1463 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001464 * It is possible to use this function if @dwork rearms itself via queue_work()
1465 * or queue_delayed_work(). See also the comment for cancel_work_sync().
1466 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001467int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001468{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07001469 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001470}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07001471EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001473static struct workqueue_struct *keventd_wq __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001475/**
1476 * schedule_work - put work task in global workqueue
1477 * @work: job to be done
1478 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02001479 * Returns zero if @work was already on the kernel-global workqueue and
1480 * non-zero otherwise.
1481 *
1482 * This puts a job in the kernel-global workqueue if it was not already
1483 * queued and leaves it in the same position on the kernel-global
1484 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001485 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001486int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 return queue_work(keventd_wq, work);
1489}
Dave Jonesae90dd52006-06-30 01:40:45 -04001490EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Zhang Ruic1a220e2008-07-23 21:28:39 -07001492/*
1493 * schedule_work_on - put work task on a specific cpu
1494 * @cpu: cpu to put the work task on
1495 * @work: job to be done
1496 *
1497 * This puts a job on a specific cpu
1498 */
1499int schedule_work_on(int cpu, struct work_struct *work)
1500{
1501 return queue_work_on(cpu, keventd_wq, work);
1502}
1503EXPORT_SYMBOL(schedule_work_on);
1504
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001505/**
1506 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00001507 * @dwork: job to be done
1508 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001509 *
1510 * After waiting for a given time this puts a job in the kernel-global
1511 * workqueue.
1512 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001513int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001514 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
David Howells52bad642006-11-22 14:54:01 +00001516 return queue_delayed_work(keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
Dave Jonesae90dd52006-06-30 01:40:45 -04001518EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001520/**
Linus Torvalds8c53e462009-10-14 09:16:42 -07001521 * flush_delayed_work - block until a dwork_struct's callback has terminated
1522 * @dwork: the delayed work which is to be flushed
1523 *
1524 * Any timeout is cancelled, and any pending work is run immediately.
1525 */
1526void flush_delayed_work(struct delayed_work *dwork)
1527{
1528 if (del_timer_sync(&dwork->timer)) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001529 __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
1530 &dwork->work);
Linus Torvalds8c53e462009-10-14 09:16:42 -07001531 put_cpu();
1532 }
1533 flush_work(&dwork->work);
1534}
1535EXPORT_SYMBOL(flush_delayed_work);
1536
1537/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001538 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
1539 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00001540 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001541 * @delay: number of jiffies to wait
1542 *
1543 * After waiting for a given time this puts a job in the kernel-global
1544 * workqueue on the specified CPU.
1545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00001547 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
David Howells52bad642006-11-22 14:54:01 +00001549 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
Dave Jonesae90dd52006-06-30 01:40:45 -04001551EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Andrew Mortonb6136772006-06-25 05:47:49 -07001553/**
1554 * schedule_on_each_cpu - call a function on each online CPU from keventd
1555 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07001556 *
1557 * Returns zero on success.
1558 * Returns -ve errno on failure.
1559 *
Andrew Mortonb6136772006-06-25 05:47:49 -07001560 * schedule_on_each_cpu() is very slow.
1561 */
David Howells65f27f32006-11-22 14:55:48 +00001562int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08001563{
1564 int cpu;
Andi Kleen65a64462009-10-14 06:22:47 +02001565 int orig = -1;
Andrew Mortonb6136772006-06-25 05:47:49 -07001566 struct work_struct *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08001567
Andrew Mortonb6136772006-06-25 05:47:49 -07001568 works = alloc_percpu(struct work_struct);
1569 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08001570 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07001571
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001572 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08001573
1574 /*
1575 * When running in keventd don't schedule a work item on
1576 * itself. Can just call directly because the work queue is
1577 * already bound. This also is faster.
1578 */
1579 if (current_is_keventd())
1580 orig = raw_smp_processor_id();
1581
Christoph Lameter15316ba2006-01-08 01:00:43 -08001582 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01001583 struct work_struct *work = per_cpu_ptr(works, cpu);
1584
1585 INIT_WORK(work, func);
Andi Kleen65a64462009-10-14 06:22:47 +02001586 if (cpu != orig)
Tejun Heo93981802009-11-17 14:06:20 -08001587 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02001588 }
Tejun Heo93981802009-11-17 14:06:20 -08001589 if (orig >= 0)
1590 func(per_cpu_ptr(works, orig));
1591
1592 for_each_online_cpu(cpu)
1593 flush_work(per_cpu_ptr(works, cpu));
1594
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001595 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07001596 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08001597 return 0;
1598}
1599
Alan Sterneef6a7d2010-02-12 17:39:21 +09001600/**
1601 * flush_scheduled_work - ensure that any scheduled work has run to completion.
1602 *
1603 * Forces execution of the kernel-global workqueue and blocks until its
1604 * completion.
1605 *
1606 * Think twice before calling this function! It's very easy to get into
1607 * trouble if you don't take great care. Either of the following situations
1608 * will lead to deadlock:
1609 *
1610 * One of the work items currently on the workqueue needs to acquire
1611 * a lock held by your code or its caller.
1612 *
1613 * Your code is running in the context of a work routine.
1614 *
1615 * They will be detected by lockdep when they occur, but the first might not
1616 * occur very often. It depends on what work items are on the workqueue and
1617 * what locks they need, which you have no control over.
1618 *
1619 * In most situations flushing the entire workqueue is overkill; you merely
1620 * need to know that a particular work item isn't queued and isn't running.
1621 * In such cases you should use cancel_delayed_work_sync() or
1622 * cancel_work_sync() instead.
1623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624void flush_scheduled_work(void)
1625{
1626 flush_workqueue(keventd_wq);
1627}
Dave Jonesae90dd52006-06-30 01:40:45 -04001628EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06001631 * execute_in_process_context - reliably execute the routine with user context
1632 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06001633 * @ew: guaranteed storage for the execute work structure (must
1634 * be available when the work executes)
1635 *
1636 * Executes the function immediately if process context is available,
1637 * otherwise schedules the function for delayed execution.
1638 *
1639 * Returns: 0 - function was executed
1640 * 1 - function was scheduled for execution
1641 */
David Howells65f27f32006-11-22 14:55:48 +00001642int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06001643{
1644 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00001645 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06001646 return 0;
1647 }
1648
David Howells65f27f32006-11-22 14:55:48 +00001649 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06001650 schedule_work(&ew->work);
1651
1652 return 1;
1653}
1654EXPORT_SYMBOL_GPL(execute_in_process_context);
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656int keventd_up(void)
1657{
1658 return keventd_wq != NULL;
1659}
1660
1661int current_is_keventd(void)
1662{
1663 struct cpu_workqueue_struct *cwq;
Hugh Dickinsd2437692007-08-27 16:06:19 +01001664 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int ret = 0;
1666
1667 BUG_ON(!keventd_wq);
1668
Tejun Heo15376632010-06-29 10:07:11 +02001669 cwq = get_cwq(cpu, keventd_wq);
Tejun Heoc34056a2010-06-29 10:07:11 +02001670 if (current == cwq->worker->task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 ret = 1;
1672
1673 return ret;
1674
1675}
1676
Tejun Heo0f900042010-06-29 10:07:11 +02001677static struct cpu_workqueue_struct *alloc_cwqs(void)
1678{
1679 /*
1680 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
1681 * Make sure that the alignment isn't lower than that of
1682 * unsigned long long.
1683 */
1684 const size_t size = sizeof(struct cpu_workqueue_struct);
1685 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
1686 __alignof__(unsigned long long));
1687 struct cpu_workqueue_struct *cwqs;
1688#ifndef CONFIG_SMP
1689 void *ptr;
1690
1691 /*
1692 * On UP, percpu allocator doesn't honor alignment parameter
1693 * and simply uses arch-dependent default. Allocate enough
1694 * room to align cwq and put an extra pointer at the end
1695 * pointing back to the originally allocated pointer which
1696 * will be used for free.
1697 *
1698 * FIXME: This really belongs to UP percpu code. Update UP
1699 * percpu code to honor alignment and remove this ugliness.
1700 */
1701 ptr = __alloc_percpu(size + align + sizeof(void *), 1);
1702 cwqs = PTR_ALIGN(ptr, align);
1703 *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
1704#else
1705 /* On SMP, percpu allocator can do it itself */
1706 cwqs = __alloc_percpu(size, align);
1707#endif
1708 /* just in case, make sure it's actually aligned */
1709 BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
1710 return cwqs;
1711}
1712
1713static void free_cwqs(struct cpu_workqueue_struct *cwqs)
1714{
1715#ifndef CONFIG_SMP
1716 /* on UP, the pointer to free is stored right after the cwq */
1717 if (cwqs)
1718 free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
1719#else
1720 free_percpu(cwqs);
1721#endif
1722}
1723
Johannes Berg4e6045f2007-10-18 23:39:55 -07001724struct workqueue_struct *__create_workqueue_key(const char *name,
Tejun Heo97e37d72010-06-29 10:07:10 +02001725 unsigned int flags,
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001726 int max_active,
Johannes Bergeb13ba82008-01-16 09:51:58 +01001727 struct lock_class_key *key,
1728 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07001729{
Tejun Heo15376632010-06-29 10:07:11 +02001730 bool singlethread = flags & WQ_SINGLE_THREAD;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001731 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001732 bool failed = false;
1733 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001734
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001735 max_active = clamp_val(max_active, 1, INT_MAX);
1736
Oleg Nesterov3af244332007-05-09 02:34:09 -07001737 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1738 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02001739 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001740
Tejun Heo0f900042010-06-29 10:07:11 +02001741 wq->cpu_wq = alloc_cwqs();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001742 if (!wq->cpu_wq)
1743 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001744
Tejun Heo97e37d72010-06-29 10:07:10 +02001745 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02001746 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02001747 mutex_init(&wq->flush_mutex);
1748 atomic_set(&wq->nr_cwqs_to_flush, 0);
1749 INIT_LIST_HEAD(&wq->flusher_queue);
1750 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001751 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01001752 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07001753 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001754
Tejun Heo15376632010-06-29 10:07:11 +02001755 cpu_maps_update_begin();
1756 /*
1757 * We must initialize cwqs for each possible cpu even if we
1758 * are going to call destroy_workqueue() finally. Otherwise
1759 * cpu_up() can hit the uninitialized cwq once we drop the
1760 * lock.
1761 */
1762 for_each_possible_cpu(cpu) {
1763 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001764 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02001765
Tejun Heo0f900042010-06-29 10:07:11 +02001766 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001767 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001768 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001769 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001770 cwq->max_active = max_active;
Tejun Heo15376632010-06-29 10:07:11 +02001771 INIT_LIST_HEAD(&cwq->worklist);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001772 INIT_LIST_HEAD(&cwq->delayed_works);
Tejun Heo15376632010-06-29 10:07:11 +02001773
Tejun Heoc34056a2010-06-29 10:07:11 +02001774 if (failed)
Tejun Heo15376632010-06-29 10:07:11 +02001775 continue;
Tejun Heoc34056a2010-06-29 10:07:11 +02001776 cwq->worker = create_worker(cwq,
1777 cpu_online(cpu) && !singlethread);
1778 if (cwq->worker)
1779 start_worker(cwq->worker);
Tejun Heo15376632010-06-29 10:07:11 +02001780 else
Tejun Heoc34056a2010-06-29 10:07:11 +02001781 failed = true;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001782 }
1783
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02001784 /*
1785 * workqueue_lock protects global freeze state and workqueues
1786 * list. Grab it, set max_active accordingly and add the new
1787 * workqueue to workqueues list.
1788 */
Tejun Heo15376632010-06-29 10:07:11 +02001789 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02001790
1791 if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
1792 for_each_possible_cpu(cpu)
1793 get_cwq(cpu, wq)->max_active = 0;
1794
Tejun Heo15376632010-06-29 10:07:11 +02001795 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02001796
Tejun Heo15376632010-06-29 10:07:11 +02001797 spin_unlock(&workqueue_lock);
1798
1799 cpu_maps_update_done();
1800
Tejun Heoc34056a2010-06-29 10:07:11 +02001801 if (failed) {
Oleg Nesterov3af244332007-05-09 02:34:09 -07001802 destroy_workqueue(wq);
1803 wq = NULL;
1804 }
1805 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02001806err:
1807 if (wq) {
Tejun Heo0f900042010-06-29 10:07:11 +02001808 free_cwqs(wq->cpu_wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001809 kfree(wq);
1810 }
1811 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001812}
Johannes Berg4e6045f2007-10-18 23:39:55 -07001813EXPORT_SYMBOL_GPL(__create_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001814
Oleg Nesterov3af244332007-05-09 02:34:09 -07001815/**
1816 * destroy_workqueue - safely terminate a workqueue
1817 * @wq: target workqueue
1818 *
1819 * Safely destroy a workqueue. All work currently pending will be done first.
1820 */
1821void destroy_workqueue(struct workqueue_struct *wq)
1822{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001823 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001824
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02001825 flush_workqueue(wq);
1826
1827 /*
1828 * wq list is used to freeze wq, remove from list after
1829 * flushing is complete in case freeze races us.
1830 */
Oleg Nesterov3da1c842008-07-25 01:47:50 -07001831 cpu_maps_update_begin();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001832 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07001833 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001834 spin_unlock(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02001835 cpu_maps_update_done();
Oleg Nesterov3af244332007-05-09 02:34:09 -07001836
Tejun Heo73f53c42010-06-29 10:07:11 +02001837 for_each_possible_cpu(cpu) {
1838 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1839 int i;
1840
Tejun Heoc34056a2010-06-29 10:07:11 +02001841 if (cwq->worker) {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001842 spin_lock_irq(&cwq->gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001843 destroy_worker(cwq->worker);
1844 cwq->worker = NULL;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001845 spin_unlock_irq(&cwq->gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02001846 }
1847
1848 for (i = 0; i < WORK_NR_COLORS; i++)
1849 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001850 BUG_ON(cwq->nr_active);
1851 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02001852 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001853
Tejun Heo0f900042010-06-29 10:07:11 +02001854 free_cwqs(wq->cpu_wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001855 kfree(wq);
1856}
1857EXPORT_SYMBOL_GPL(destroy_workqueue);
1858
Tejun Heodb7bccf2010-06-29 10:07:12 +02001859/*
1860 * CPU hotplug.
1861 *
1862 * CPU hotplug is implemented by allowing cwqs to be detached from
1863 * CPU, running with unbound workers and allowing them to be
1864 * reattached later if the cpu comes back online. A separate thread
1865 * is created to govern cwqs in such state and is called the trustee.
1866 *
1867 * Trustee states and their descriptions.
1868 *
1869 * START Command state used on startup. On CPU_DOWN_PREPARE, a
1870 * new trustee is started with this state.
1871 *
1872 * IN_CHARGE Once started, trustee will enter this state after
1873 * making all existing workers rogue. DOWN_PREPARE waits
1874 * for trustee to enter this state. After reaching
1875 * IN_CHARGE, trustee tries to execute the pending
1876 * worklist until it's empty and the state is set to
1877 * BUTCHER, or the state is set to RELEASE.
1878 *
1879 * BUTCHER Command state which is set by the cpu callback after
1880 * the cpu has went down. Once this state is set trustee
1881 * knows that there will be no new works on the worklist
1882 * and once the worklist is empty it can proceed to
1883 * killing idle workers.
1884 *
1885 * RELEASE Command state which is set by the cpu callback if the
1886 * cpu down has been canceled or it has come online
1887 * again. After recognizing this state, trustee stops
1888 * trying to drain or butcher and transits to DONE.
1889 *
1890 * DONE Trustee will enter this state after BUTCHER or RELEASE
1891 * is complete.
1892 *
1893 * trustee CPU draining
1894 * took over down complete
1895 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
1896 * | | ^
1897 * | CPU is back online v return workers |
1898 * ----------------> RELEASE --------------
1899 */
1900
1901/**
1902 * trustee_wait_event_timeout - timed event wait for trustee
1903 * @cond: condition to wait for
1904 * @timeout: timeout in jiffies
1905 *
1906 * wait_event_timeout() for trustee to use. Handles locking and
1907 * checks for RELEASE request.
1908 *
1909 * CONTEXT:
1910 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1911 * multiple times. To be used by trustee.
1912 *
1913 * RETURNS:
1914 * Positive indicating left time if @cond is satisfied, 0 if timed
1915 * out, -1 if canceled.
1916 */
1917#define trustee_wait_event_timeout(cond, timeout) ({ \
1918 long __ret = (timeout); \
1919 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
1920 __ret) { \
1921 spin_unlock_irq(&gcwq->lock); \
1922 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
1923 (gcwq->trustee_state == TRUSTEE_RELEASE), \
1924 __ret); \
1925 spin_lock_irq(&gcwq->lock); \
1926 } \
1927 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
1928})
1929
1930/**
1931 * trustee_wait_event - event wait for trustee
1932 * @cond: condition to wait for
1933 *
1934 * wait_event() for trustee to use. Automatically handles locking and
1935 * checks for CANCEL request.
1936 *
1937 * CONTEXT:
1938 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1939 * multiple times. To be used by trustee.
1940 *
1941 * RETURNS:
1942 * 0 if @cond is satisfied, -1 if canceled.
1943 */
1944#define trustee_wait_event(cond) ({ \
1945 long __ret1; \
1946 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
1947 __ret1 < 0 ? -1 : 0; \
1948})
1949
1950static int __cpuinit trustee_thread(void *__gcwq)
1951{
1952 struct global_cwq *gcwq = __gcwq;
1953 struct worker *worker;
1954 struct hlist_node *pos;
1955 int i;
1956
1957 BUG_ON(gcwq->cpu != smp_processor_id());
1958
1959 spin_lock_irq(&gcwq->lock);
1960 /*
1961 * Make all multithread workers rogue. Trustee must be bound
1962 * to the target cpu and can't be cancelled.
1963 */
1964 BUG_ON(gcwq->cpu != smp_processor_id());
1965
1966 list_for_each_entry(worker, &gcwq->idle_list, entry)
1967 if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
1968 worker->flags |= WORKER_ROGUE;
1969
1970 for_each_busy_worker(worker, i, pos, gcwq)
1971 if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
1972 worker->flags |= WORKER_ROGUE;
1973
1974 /*
1975 * We're now in charge. Notify and proceed to drain. We need
1976 * to keep the gcwq running during the whole CPU down
1977 * procedure as other cpu hotunplug callbacks may need to
1978 * flush currently running tasks.
1979 */
1980 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
1981 wake_up_all(&gcwq->trustee_wait);
1982
1983 /*
1984 * The original cpu is in the process of dying and may go away
1985 * anytime now. When that happens, we and all workers would
1986 * be migrated to other cpus. Try draining any left work.
1987 * Note that if the gcwq is frozen, there may be frozen works
1988 * in freezeable cwqs. Don't declare completion while frozen.
1989 */
1990 while (gcwq->nr_workers != gcwq->nr_idle ||
1991 gcwq->flags & GCWQ_FREEZING ||
1992 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
1993 /* give a breather */
1994 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
1995 break;
1996 }
1997
1998 /* notify completion */
1999 gcwq->trustee = NULL;
2000 gcwq->trustee_state = TRUSTEE_DONE;
2001 wake_up_all(&gcwq->trustee_wait);
2002 spin_unlock_irq(&gcwq->lock);
2003 return 0;
2004}
2005
2006/**
2007 * wait_trustee_state - wait for trustee to enter the specified state
2008 * @gcwq: gcwq the trustee of interest belongs to
2009 * @state: target state to wait for
2010 *
2011 * Wait for the trustee to reach @state. DONE is already matched.
2012 *
2013 * CONTEXT:
2014 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2015 * multiple times. To be used by cpu_callback.
2016 */
2017static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
2018{
2019 if (!(gcwq->trustee_state == state ||
2020 gcwq->trustee_state == TRUSTEE_DONE)) {
2021 spin_unlock_irq(&gcwq->lock);
2022 __wait_event(gcwq->trustee_wait,
2023 gcwq->trustee_state == state ||
2024 gcwq->trustee_state == TRUSTEE_DONE);
2025 spin_lock_irq(&gcwq->lock);
2026 }
2027}
2028
Oleg Nesterov3af244332007-05-09 02:34:09 -07002029static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
2030 unsigned long action,
2031 void *hcpu)
2032{
2033 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02002034 struct global_cwq *gcwq = get_gcwq(cpu);
2035 struct task_struct *new_trustee = NULL;
2036 struct worker *worker;
2037 struct hlist_node *pos;
2038 unsigned long flags;
2039 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002041 action &= ~CPU_TASKS_FROZEN;
2042
Tejun Heodb7bccf2010-06-29 10:07:12 +02002043 switch (action) {
2044 case CPU_DOWN_PREPARE:
2045 new_trustee = kthread_create(trustee_thread, gcwq,
2046 "workqueue_trustee/%d\n", cpu);
2047 if (IS_ERR(new_trustee))
2048 return notifier_from_errno(PTR_ERR(new_trustee));
2049 kthread_bind(new_trustee, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
Tejun Heodb7bccf2010-06-29 10:07:12 +02002052 /* some are called w/ irq disabled, don't disturb irq status */
2053 spin_lock_irqsave(&gcwq->lock, flags);
2054
2055 switch (action) {
2056 case CPU_DOWN_PREPARE:
2057 /* initialize trustee and tell it to acquire the gcwq */
2058 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
2059 gcwq->trustee = new_trustee;
2060 gcwq->trustee_state = TRUSTEE_START;
2061 wake_up_process(gcwq->trustee);
2062 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
2063 break;
2064
2065 case CPU_POST_DEAD:
2066 gcwq->trustee_state = TRUSTEE_BUTCHER;
2067 break;
2068
2069 case CPU_DOWN_FAILED:
2070 case CPU_ONLINE:
2071 if (gcwq->trustee_state != TRUSTEE_DONE) {
2072 gcwq->trustee_state = TRUSTEE_RELEASE;
2073 wake_up_process(gcwq->trustee);
2074 wait_trustee_state(gcwq, TRUSTEE_DONE);
2075 }
2076
2077 /* clear ROGUE from all multithread workers */
2078 list_for_each_entry(worker, &gcwq->idle_list, entry)
2079 if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
2080 worker->flags &= ~WORKER_ROGUE;
2081
2082 for_each_busy_worker(worker, i, pos, gcwq)
2083 if (!(worker->cwq->wq->flags & WQ_SINGLE_THREAD))
2084 worker->flags &= ~WORKER_ROGUE;
2085 break;
2086 }
2087
2088 spin_unlock_irqrestore(&gcwq->lock, flags);
2089
Tejun Heo15376632010-06-29 10:07:11 +02002090 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Rusty Russell2d3854a2008-11-05 13:39:10 +11002093#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08002094
Rusty Russell2d3854a2008-11-05 13:39:10 +11002095struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06002096 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11002097 long (*fn)(void *);
2098 void *arg;
2099 long ret;
2100};
2101
Andrew Morton6b440032009-04-09 09:50:37 -06002102static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11002103{
Andrew Morton6b440032009-04-09 09:50:37 -06002104 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11002105 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06002106 complete(&wfc->completion);
2107 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11002108}
2109
2110/**
2111 * work_on_cpu - run a function in user context on a particular cpu
2112 * @cpu: the cpu to run on
2113 * @fn: the function to run
2114 * @arg: the function arg
2115 *
Rusty Russell31ad9082009-01-16 15:31:15 -08002116 * This will return the value @fn returns.
2117 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06002118 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11002119 */
2120long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
2121{
Andrew Morton6b440032009-04-09 09:50:37 -06002122 struct task_struct *sub_thread;
2123 struct work_for_cpu wfc = {
2124 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
2125 .fn = fn,
2126 .arg = arg,
2127 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11002128
Andrew Morton6b440032009-04-09 09:50:37 -06002129 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
2130 if (IS_ERR(sub_thread))
2131 return PTR_ERR(sub_thread);
2132 kthread_bind(sub_thread, cpu);
2133 wake_up_process(sub_thread);
2134 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11002135 return wfc.ret;
2136}
2137EXPORT_SYMBOL_GPL(work_on_cpu);
2138#endif /* CONFIG_SMP */
2139
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002140#ifdef CONFIG_FREEZER
2141
2142/**
2143 * freeze_workqueues_begin - begin freezing workqueues
2144 *
2145 * Start freezing workqueues. After this function returns, all
2146 * freezeable workqueues will queue new works to their frozen_works
2147 * list instead of the cwq ones.
2148 *
2149 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002150 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002151 */
2152void freeze_workqueues_begin(void)
2153{
2154 struct workqueue_struct *wq;
2155 unsigned int cpu;
2156
2157 spin_lock(&workqueue_lock);
2158
2159 BUG_ON(workqueue_freezing);
2160 workqueue_freezing = true;
2161
2162 for_each_possible_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002163 struct global_cwq *gcwq = get_gcwq(cpu);
2164
2165 spin_lock_irq(&gcwq->lock);
2166
Tejun Heodb7bccf2010-06-29 10:07:12 +02002167 BUG_ON(gcwq->flags & GCWQ_FREEZING);
2168 gcwq->flags |= GCWQ_FREEZING;
2169
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002170 list_for_each_entry(wq, &workqueues, list) {
2171 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2172
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002173 if (wq->flags & WQ_FREEZEABLE)
2174 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002175 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002176
2177 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002178 }
2179
2180 spin_unlock(&workqueue_lock);
2181}
2182
2183/**
2184 * freeze_workqueues_busy - are freezeable workqueues still busy?
2185 *
2186 * Check whether freezing is complete. This function must be called
2187 * between freeze_workqueues_begin() and thaw_workqueues().
2188 *
2189 * CONTEXT:
2190 * Grabs and releases workqueue_lock.
2191 *
2192 * RETURNS:
2193 * %true if some freezeable workqueues are still busy. %false if
2194 * freezing is complete.
2195 */
2196bool freeze_workqueues_busy(void)
2197{
2198 struct workqueue_struct *wq;
2199 unsigned int cpu;
2200 bool busy = false;
2201
2202 spin_lock(&workqueue_lock);
2203
2204 BUG_ON(!workqueue_freezing);
2205
2206 for_each_possible_cpu(cpu) {
2207 /*
2208 * nr_active is monotonically decreasing. It's safe
2209 * to peek without lock.
2210 */
2211 list_for_each_entry(wq, &workqueues, list) {
2212 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2213
2214 if (!(wq->flags & WQ_FREEZEABLE))
2215 continue;
2216
2217 BUG_ON(cwq->nr_active < 0);
2218 if (cwq->nr_active) {
2219 busy = true;
2220 goto out_unlock;
2221 }
2222 }
2223 }
2224out_unlock:
2225 spin_unlock(&workqueue_lock);
2226 return busy;
2227}
2228
2229/**
2230 * thaw_workqueues - thaw workqueues
2231 *
2232 * Thaw workqueues. Normal queueing is restored and all collected
2233 * frozen works are transferred to their respective cwq worklists.
2234 *
2235 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002236 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002237 */
2238void thaw_workqueues(void)
2239{
2240 struct workqueue_struct *wq;
2241 unsigned int cpu;
2242
2243 spin_lock(&workqueue_lock);
2244
2245 if (!workqueue_freezing)
2246 goto out_unlock;
2247
2248 for_each_possible_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002249 struct global_cwq *gcwq = get_gcwq(cpu);
2250
2251 spin_lock_irq(&gcwq->lock);
2252
Tejun Heodb7bccf2010-06-29 10:07:12 +02002253 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
2254 gcwq->flags &= ~GCWQ_FREEZING;
2255
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002256 list_for_each_entry(wq, &workqueues, list) {
2257 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2258
2259 if (!(wq->flags & WQ_FREEZEABLE))
2260 continue;
2261
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002262 /* restore max_active and repopulate worklist */
2263 cwq->max_active = wq->saved_max_active;
2264
2265 while (!list_empty(&cwq->delayed_works) &&
2266 cwq->nr_active < cwq->max_active)
2267 cwq_activate_first_delayed(cwq);
2268
Tejun Heoc8e55f32010-06-29 10:07:12 +02002269 wake_up_process(cwq->worker->task);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002270 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002271
2272 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002273 }
2274
2275 workqueue_freezing = false;
2276out_unlock:
2277 spin_unlock(&workqueue_lock);
2278}
2279#endif /* CONFIG_FREEZER */
2280
Oleg Nesterovc12920d2007-05-09 02:34:14 -07002281void __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Tejun Heoc34056a2010-06-29 10:07:11 +02002283 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002284 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02002285
Rusty Russelle7577c52009-01-01 10:12:25 +10302286 singlethread_cpu = cpumask_first(cpu_possible_mask);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002287 hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002288
2289 /* initialize gcwqs */
2290 for_each_possible_cpu(cpu) {
2291 struct global_cwq *gcwq = get_gcwq(cpu);
2292
2293 spin_lock_init(&gcwq->lock);
2294 gcwq->cpu = cpu;
2295
Tejun Heoc8e55f32010-06-29 10:07:12 +02002296 INIT_LIST_HEAD(&gcwq->idle_list);
2297 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
2298 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
2299
Tejun Heo8b03ae32010-06-29 10:07:12 +02002300 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002301
2302 gcwq->trustee_state = TRUSTEE_DONE;
2303 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002304 }
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 keventd_wq = create_workqueue("events");
2307 BUG_ON(!keventd_wq);
2308}