blob: cc3456f96c56ab5816bfc5e4a367db7d3f8a40d9 [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 Lameter469340232006-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>
Tejun Heoe22bee72010-06-29 10:07:14 +020037
38#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Tejun Heoc8e55f32010-06-29 10:07:12 +020040enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020041 /* global_cwq flags */
Tejun Heoe22bee72010-06-29 10:07:14 +020042 GCWQ_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
43 GCWQ_MANAGING_WORKERS = 1 << 1, /* managing workers */
44 GCWQ_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020045 GCWQ_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heo649027d2010-06-29 10:07:14 +020046 GCWQ_HIGHPRI_PENDING = 1 << 4, /* highpri works on queue */
Tejun Heodb7bccf2010-06-29 10:07:12 +020047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048 /* worker flags */
49 WORKER_STARTED = 1 << 0, /* started */
50 WORKER_DIE = 1 << 1, /* die die die */
51 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020052 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020053 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020054 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020055 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020056 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020057
Tejun Heofb0e7be2010-06-29 10:07:15 +020058 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020059 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020060
61 /* gcwq->trustee_state */
62 TRUSTEE_START = 0, /* start */
63 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
64 TRUSTEE_BUTCHER = 2, /* butcher workers */
65 TRUSTEE_RELEASE = 3, /* release workers */
66 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020067
68 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
69 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
70 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020071
Tejun Heoe22bee72010-06-29 10:07:14 +020072 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
73 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
74
75 MAYDAY_INITIAL_TIMEOUT = HZ / 100, /* call for help after 10ms */
76 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
77 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020078 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020079
80 /*
81 * Rescue workers are used only on emergencies and shared by
82 * all cpus. Give -20.
83 */
84 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020085};
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020088 * Structure fields follow one of the following exclusion rules.
89 *
90 * I: Set during initialization and read-only afterwards.
91 *
Tejun Heoe22bee72010-06-29 10:07:14 +020092 * P: Preemption protected. Disabling preemption is enough and should
93 * only be modified and accessed from the local cpu.
94 *
Tejun Heo8b03ae32010-06-29 10:07:12 +020095 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +020096 *
Tejun Heoe22bee72010-06-29 10:07:14 +020097 * X: During normal operation, modification requires gcwq->lock and
98 * should be done only from local cpu. Either disabling preemption
99 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200100 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200101 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200102 * F: wq->flush_mutex protected.
103 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200104 * W: workqueue_lock protected.
105 */
106
Tejun Heo8b03ae32010-06-29 10:07:12 +0200107struct global_cwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200108
Tejun Heoe22bee72010-06-29 10:07:14 +0200109/*
110 * The poor guys doing the actual heavy lifting. All on-duty workers
111 * are either serving the manager role, on idle list or on busy hash.
112 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200113struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200114 /* on idle list while idle, on busy hash table while busy */
115 union {
116 struct list_head entry; /* L: while idle */
117 struct hlist_node hentry; /* L: while busy */
118 };
119
Tejun Heoc34056a2010-06-29 10:07:11 +0200120 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200121 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200122 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200123 struct task_struct *task; /* I: worker task */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200124 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heoe22bee72010-06-29 10:07:14 +0200125 /* 64 bytes boundary on 64bit, 32 on 32bit */
126 unsigned long last_active; /* L: last active timestamp */
127 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200128 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200129 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200130};
131
Tejun Heo4690c4a2010-06-29 10:07:10 +0200132/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200133 * Global per-cpu workqueue. There's one and only one for each cpu
134 * and all works are queued and processed here regardless of their
135 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200136 */
137struct global_cwq {
138 spinlock_t lock; /* the gcwq lock */
Tejun Heo7e116292010-06-29 10:07:13 +0200139 struct list_head worklist; /* L: list of pending works */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200140 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200141 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200142
143 int nr_workers; /* L: total number of workers */
144 int nr_idle; /* L: currently idle ones */
145
146 /* workers are chained either in the idle_list or busy_hash */
Tejun Heoe22bee72010-06-29 10:07:14 +0200147 struct list_head idle_list; /* X: list of idle workers */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200148 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
149 /* L: hash of busy workers */
150
Tejun Heoe22bee72010-06-29 10:07:14 +0200151 struct timer_list idle_timer; /* L: worker idle timeout */
152 struct timer_list mayday_timer; /* L: SOS timer for dworkers */
153
Tejun Heo8b03ae32010-06-29 10:07:12 +0200154 struct ida worker_ida; /* L: for worker IDs */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200155
156 struct task_struct *trustee; /* L: for gcwq shutdown */
157 unsigned int trustee_state; /* L: trustee state */
158 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heoe22bee72010-06-29 10:07:14 +0200159 struct worker *first_idle; /* L: first idle worker */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200160} ____cacheline_aligned_in_smp;
161
162/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200163 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200164 * work_struct->data are used for flags and thus cwqs need to be
165 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167struct cpu_workqueue_struct {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200168 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200169 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200170 int work_color; /* L: current color */
171 int flush_color; /* L: flushing color */
172 int nr_in_flight[WORK_NR_COLORS];
173 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200174 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200175 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200176 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200177};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200180 * Structure used to wait for workqueue flush.
181 */
182struct wq_flusher {
183 struct list_head list; /* F: list of flushers */
184 int flush_color; /* F: flush color waiting for */
185 struct completion done; /* flush completion */
186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tejun Heo73f53c42010-06-29 10:07:11 +0200188/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200189 * All cpumasks are assumed to be always set on UP and thus can't be
190 * used to determine whether there's something to be done.
191 */
192#ifdef CONFIG_SMP
193typedef cpumask_var_t mayday_mask_t;
194#define mayday_test_and_set_cpu(cpu, mask) \
195 cpumask_test_and_set_cpu((cpu), (mask))
196#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
197#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
198#define alloc_mayday_mask(maskp, gfp) alloc_cpumask_var((maskp), (gfp))
199#define free_mayday_mask(mask) free_cpumask_var((mask))
200#else
201typedef unsigned long mayday_mask_t;
202#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
203#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
204#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
205#define alloc_mayday_mask(maskp, gfp) true
206#define free_mayday_mask(mask) do { } while (0)
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * The externally visible workqueue abstraction is an array of
211 * per-CPU workqueues:
212 */
213struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +0200214 unsigned int flags; /* I: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200215 union {
216 struct cpu_workqueue_struct __percpu *pcpu;
217 struct cpu_workqueue_struct *single;
218 unsigned long v;
219 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200220 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200221
222 struct mutex flush_mutex; /* protects wq flushing */
223 int work_color; /* F: current work color */
224 int flush_color; /* F: current flush color */
225 atomic_t nr_cwqs_to_flush; /* flush in progress */
226 struct wq_flusher *first_flusher; /* F: first flusher */
227 struct list_head flusher_queue; /* F: flush waiters */
228 struct list_head flusher_overflow; /* F: flush overflow list */
229
Tejun Heof2e005a2010-07-20 15:59:09 +0200230 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200231 struct worker *rescuer; /* I: rescue worker */
232
Tejun Heodcd989c2010-06-29 10:07:14 +0200233 int saved_max_active; /* W: saved cwq max_active */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200234 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700235#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200236 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Tejun Heod320c032010-06-29 10:07:14 +0200240struct workqueue_struct *system_wq __read_mostly;
241struct workqueue_struct *system_long_wq __read_mostly;
242struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200243struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200244EXPORT_SYMBOL_GPL(system_wq);
245EXPORT_SYMBOL_GPL(system_long_wq);
246EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200247EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200248
Tejun Heodb7bccf2010-06-29 10:07:12 +0200249#define for_each_busy_worker(worker, i, pos, gcwq) \
250 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
251 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
252
Tejun Heof3421792010-07-02 10:03:51 +0200253static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
254 unsigned int sw)
255{
256 if (cpu < nr_cpu_ids) {
257 if (sw & 1) {
258 cpu = cpumask_next(cpu, mask);
259 if (cpu < nr_cpu_ids)
260 return cpu;
261 }
262 if (sw & 2)
263 return WORK_CPU_UNBOUND;
264 }
265 return WORK_CPU_NONE;
266}
267
268static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
269 struct workqueue_struct *wq)
270{
271 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
272}
273
Tejun Heo09884952010-08-01 11:50:12 +0200274/*
275 * CPU iterators
276 *
277 * An extra gcwq is defined for an invalid cpu number
278 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
279 * specific CPU. The following iterators are similar to
280 * for_each_*_cpu() iterators but also considers the unbound gcwq.
281 *
282 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
283 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
284 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
285 * WORK_CPU_UNBOUND for unbound workqueues
286 */
Tejun Heof3421792010-07-02 10:03:51 +0200287#define for_each_gcwq_cpu(cpu) \
288 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
289 (cpu) < WORK_CPU_NONE; \
290 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
291
292#define for_each_online_gcwq_cpu(cpu) \
293 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
294 (cpu) < WORK_CPU_NONE; \
295 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
296
297#define for_each_cwq_cpu(cpu, wq) \
298 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
299 (cpu) < WORK_CPU_NONE; \
300 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
301
Paul E. McKenneya25909a2010-05-13 12:32:28 -0700302#ifdef CONFIG_LOCKDEP
303/**
304 * in_workqueue_context() - in context of specified workqueue?
305 * @wq: the workqueue of interest
306 *
307 * Checks lockdep state to see if the current task is executing from
308 * within a workqueue item. This function exists only if lockdep is
309 * enabled.
310 */
311int in_workqueue_context(struct workqueue_struct *wq)
312{
313 return lock_is_held(&wq->lockdep_map);
314}
315#endif
316
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900317#ifdef CONFIG_DEBUG_OBJECTS_WORK
318
319static struct debug_obj_descr work_debug_descr;
320
321/*
322 * fixup_init is called when:
323 * - an active object is initialized
324 */
325static int work_fixup_init(void *addr, enum debug_obj_state state)
326{
327 struct work_struct *work = addr;
328
329 switch (state) {
330 case ODEBUG_STATE_ACTIVE:
331 cancel_work_sync(work);
332 debug_object_init(work, &work_debug_descr);
333 return 1;
334 default:
335 return 0;
336 }
337}
338
339/*
340 * fixup_activate is called when:
341 * - an active object is activated
342 * - an unknown object is activated (might be a statically initialized object)
343 */
344static int work_fixup_activate(void *addr, enum debug_obj_state state)
345{
346 struct work_struct *work = addr;
347
348 switch (state) {
349
350 case ODEBUG_STATE_NOTAVAILABLE:
351 /*
352 * This is not really a fixup. The work struct was
353 * statically initialized. We just make sure that it
354 * is tracked in the object tracker.
355 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200356 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900357 debug_object_init(work, &work_debug_descr);
358 debug_object_activate(work, &work_debug_descr);
359 return 0;
360 }
361 WARN_ON_ONCE(1);
362 return 0;
363
364 case ODEBUG_STATE_ACTIVE:
365 WARN_ON(1);
366
367 default:
368 return 0;
369 }
370}
371
372/*
373 * fixup_free is called when:
374 * - an active object is freed
375 */
376static int work_fixup_free(void *addr, enum debug_obj_state state)
377{
378 struct work_struct *work = addr;
379
380 switch (state) {
381 case ODEBUG_STATE_ACTIVE:
382 cancel_work_sync(work);
383 debug_object_free(work, &work_debug_descr);
384 return 1;
385 default:
386 return 0;
387 }
388}
389
390static struct debug_obj_descr work_debug_descr = {
391 .name = "work_struct",
392 .fixup_init = work_fixup_init,
393 .fixup_activate = work_fixup_activate,
394 .fixup_free = work_fixup_free,
395};
396
397static inline void debug_work_activate(struct work_struct *work)
398{
399 debug_object_activate(work, &work_debug_descr);
400}
401
402static inline void debug_work_deactivate(struct work_struct *work)
403{
404 debug_object_deactivate(work, &work_debug_descr);
405}
406
407void __init_work(struct work_struct *work, int onstack)
408{
409 if (onstack)
410 debug_object_init_on_stack(work, &work_debug_descr);
411 else
412 debug_object_init(work, &work_debug_descr);
413}
414EXPORT_SYMBOL_GPL(__init_work);
415
416void destroy_work_on_stack(struct work_struct *work)
417{
418 debug_object_free(work, &work_debug_descr);
419}
420EXPORT_SYMBOL_GPL(destroy_work_on_stack);
421
422#else
423static inline void debug_work_activate(struct work_struct *work) { }
424static inline void debug_work_deactivate(struct work_struct *work) { }
425#endif
426
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100427/* Serializes the accesses to the list of workqueues. */
428static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200430static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Oleg Nesterov14441962007-05-23 13:57:57 -0700432/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200433 * The almighty global cpu workqueues. nr_running is the only field
434 * which is expected to be used frequently by other cpus via
435 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700436 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200437static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200438static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800439
Tejun Heof3421792010-07-02 10:03:51 +0200440/*
441 * Global cpu workqueue and nr_running counter for unbound gcwq. The
442 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
443 * workers have WORKER_UNBOUND set.
444 */
445static struct global_cwq unbound_global_cwq;
446static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
447
Tejun Heoc34056a2010-06-29 10:07:11 +0200448static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Tejun Heo8b03ae32010-06-29 10:07:12 +0200450static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Tejun Heof3421792010-07-02 10:03:51 +0200452 if (cpu != WORK_CPU_UNBOUND)
453 return &per_cpu(global_cwq, cpu);
454 else
455 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Tejun Heoe22bee72010-06-29 10:07:14 +0200458static atomic_t *get_gcwq_nr_running(unsigned int cpu)
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700459{
Tejun Heof3421792010-07-02 10:03:51 +0200460 if (cpu != WORK_CPU_UNBOUND)
461 return &per_cpu(gcwq_nr_running, cpu);
462 else
463 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700464}
465
Tejun Heo4690c4a2010-06-29 10:07:10 +0200466static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
467 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700468{
Tejun Heof3421792010-07-02 10:03:51 +0200469 if (!(wq->flags & WQ_UNBOUND)) {
470 if (likely(cpu < nr_cpu_ids)) {
471#ifdef CONFIG_SMP
472 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200473#else
Tejun Heof3421792010-07-02 10:03:51 +0200474 return wq->cpu_wq.single;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200475#endif
Tejun Heof3421792010-07-02 10:03:51 +0200476 }
477 } else if (likely(cpu == WORK_CPU_UNBOUND))
478 return wq->cpu_wq.single;
479 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700480}
481
Tejun Heo73f53c42010-06-29 10:07:11 +0200482static unsigned int work_color_to_flags(int color)
483{
484 return color << WORK_STRUCT_COLOR_SHIFT;
485}
486
487static int get_work_color(struct work_struct *work)
488{
489 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
490 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
491}
492
493static int work_next_color(int color)
494{
495 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
David Howells4594bf12006-12-07 11:33:26 +0000498/*
Tejun Heoe1201532010-07-22 14:14:25 +0200499 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
500 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
501 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200502 *
503 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
504 * cwq, cpu or clear work->data. These functions should only be
505 * called while the work is owned - ie. while the PENDING bit is set.
506 *
507 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
508 * corresponding to a work. gcwq is available once the work has been
509 * queued anywhere after initialization. cwq is available only from
510 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000511 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200512static inline void set_work_data(struct work_struct *work, unsigned long data,
513 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000514{
David Howells4594bf12006-12-07 11:33:26 +0000515 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200516 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000517}
David Howells365970a2006-11-22 14:54:49 +0000518
Tejun Heo7a22ad72010-06-29 10:07:13 +0200519static void set_work_cwq(struct work_struct *work,
520 struct cpu_workqueue_struct *cwq,
521 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200522{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200523 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200524 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200525}
526
Tejun Heo7a22ad72010-06-29 10:07:13 +0200527static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000528{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
530}
531
532static void clear_work_data(struct work_struct *work)
533{
534 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
535}
536
Tejun Heo7a22ad72010-06-29 10:07:13 +0200537static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
538{
Tejun Heoe1201532010-07-22 14:14:25 +0200539 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540
Tejun Heoe1201532010-07-22 14:14:25 +0200541 if (data & WORK_STRUCT_CWQ)
542 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
543 else
544 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545}
546
547static struct global_cwq *get_work_gcwq(struct work_struct *work)
548{
Tejun Heoe1201532010-07-22 14:14:25 +0200549 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550 unsigned int cpu;
551
Tejun Heoe1201532010-07-22 14:14:25 +0200552 if (data & WORK_STRUCT_CWQ)
553 return ((struct cpu_workqueue_struct *)
554 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555
556 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200557 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 return NULL;
559
Tejun Heof3421792010-07-02 10:03:51 +0200560 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000562}
563
564/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200565 * Policy functions. These define the policies on how the global
566 * worker pool is managed. Unless noted otherwise, these functions
567 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000568 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200569
Tejun Heo649027d2010-06-29 10:07:14 +0200570static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000571{
Tejun Heo649027d2010-06-29 10:07:14 +0200572 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
573 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000574}
575
Tejun Heoe22bee72010-06-29 10:07:14 +0200576/*
577 * Need to wake up a worker? Called from anything but currently
578 * running workers.
579 */
580static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000581{
Tejun Heo649027d2010-06-29 10:07:14 +0200582 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000583}
584
Tejun Heoe22bee72010-06-29 10:07:14 +0200585/* Can I start working? Called from busy but !running workers. */
586static bool may_start_working(struct global_cwq *gcwq)
587{
588 return gcwq->nr_idle;
589}
590
591/* Do I need to keep working? Called from currently running workers. */
592static bool keep_working(struct global_cwq *gcwq)
593{
594 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
595
596 return !list_empty(&gcwq->worklist) && atomic_read(nr_running) <= 1;
597}
598
599/* Do we need a new worker? Called from manager. */
600static bool need_to_create_worker(struct global_cwq *gcwq)
601{
602 return need_more_worker(gcwq) && !may_start_working(gcwq);
603}
604
605/* Do I need to be the manager? */
606static bool need_to_manage_workers(struct global_cwq *gcwq)
607{
608 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
609}
610
611/* Do we have too many workers and should some go away? */
612static bool too_many_workers(struct global_cwq *gcwq)
613{
614 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
615 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
616 int nr_busy = gcwq->nr_workers - nr_idle;
617
618 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
619}
620
621/*
622 * Wake up functions.
623 */
624
Tejun Heo7e116292010-06-29 10:07:13 +0200625/* Return the first worker. Safe with preemption disabled */
626static struct worker *first_worker(struct global_cwq *gcwq)
627{
628 if (unlikely(list_empty(&gcwq->idle_list)))
629 return NULL;
630
631 return list_first_entry(&gcwq->idle_list, struct worker, entry);
632}
633
634/**
635 * wake_up_worker - wake up an idle worker
636 * @gcwq: gcwq to wake worker for
637 *
638 * Wake up the first idle worker of @gcwq.
639 *
640 * CONTEXT:
641 * spin_lock_irq(gcwq->lock).
642 */
643static void wake_up_worker(struct global_cwq *gcwq)
644{
645 struct worker *worker = first_worker(gcwq);
646
647 if (likely(worker))
648 wake_up_process(worker->task);
649}
650
Tejun Heo4690c4a2010-06-29 10:07:10 +0200651/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200652 * wq_worker_waking_up - a worker is waking up
653 * @task: task waking up
654 * @cpu: CPU @task is waking up to
655 *
656 * This function is called during try_to_wake_up() when a worker is
657 * being awoken.
658 *
659 * CONTEXT:
660 * spin_lock_irq(rq->lock)
661 */
662void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
663{
664 struct worker *worker = kthread_data(task);
665
666 if (likely(!(worker->flags & WORKER_NOT_RUNNING)))
667 atomic_inc(get_gcwq_nr_running(cpu));
668}
669
670/**
671 * wq_worker_sleeping - a worker is going to sleep
672 * @task: task going to sleep
673 * @cpu: CPU in question, must be the current CPU number
674 *
675 * This function is called during schedule() when a busy worker is
676 * going to sleep. Worker on the same cpu can be woken up by
677 * returning pointer to its task.
678 *
679 * CONTEXT:
680 * spin_lock_irq(rq->lock)
681 *
682 * RETURNS:
683 * Worker task on @cpu to wake up, %NULL if none.
684 */
685struct task_struct *wq_worker_sleeping(struct task_struct *task,
686 unsigned int cpu)
687{
688 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
689 struct global_cwq *gcwq = get_gcwq(cpu);
690 atomic_t *nr_running = get_gcwq_nr_running(cpu);
691
692 if (unlikely(worker->flags & WORKER_NOT_RUNNING))
693 return NULL;
694
695 /* this can only happen on the local cpu */
696 BUG_ON(cpu != raw_smp_processor_id());
697
698 /*
699 * The counterpart of the following dec_and_test, implied mb,
700 * worklist not empty test sequence is in insert_work().
701 * Please read comment there.
702 *
703 * NOT_RUNNING is clear. This means that trustee is not in
704 * charge and we're running on the local cpu w/ rq lock held
705 * and preemption disabled, which in turn means that none else
706 * could be manipulating idle_list, so dereferencing idle_list
707 * without gcwq lock is safe.
708 */
709 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
710 to_wakeup = first_worker(gcwq);
711 return to_wakeup ? to_wakeup->task : NULL;
712}
713
714/**
715 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200716 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200717 * @flags: flags to set
718 * @wakeup: wakeup an idle worker if necessary
719 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200720 * Set @flags in @worker->flags and adjust nr_running accordingly. If
721 * nr_running becomes zero and @wakeup is %true, an idle worker is
722 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200723 *
Tejun Heocb444762010-07-02 10:03:50 +0200724 * CONTEXT:
725 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200726 */
727static inline void worker_set_flags(struct worker *worker, unsigned int flags,
728 bool wakeup)
729{
Tejun Heoe22bee72010-06-29 10:07:14 +0200730 struct global_cwq *gcwq = worker->gcwq;
731
Tejun Heocb444762010-07-02 10:03:50 +0200732 WARN_ON_ONCE(worker->task != current);
733
Tejun Heoe22bee72010-06-29 10:07:14 +0200734 /*
735 * If transitioning into NOT_RUNNING, adjust nr_running and
736 * wake up an idle worker as necessary if requested by
737 * @wakeup.
738 */
739 if ((flags & WORKER_NOT_RUNNING) &&
740 !(worker->flags & WORKER_NOT_RUNNING)) {
741 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
742
743 if (wakeup) {
744 if (atomic_dec_and_test(nr_running) &&
745 !list_empty(&gcwq->worklist))
746 wake_up_worker(gcwq);
747 } else
748 atomic_dec(nr_running);
749 }
750
Tejun Heod302f012010-06-29 10:07:13 +0200751 worker->flags |= flags;
752}
753
754/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200755 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200756 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200757 * @flags: flags to clear
758 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200759 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200760 *
Tejun Heocb444762010-07-02 10:03:50 +0200761 * CONTEXT:
762 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200763 */
764static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
765{
Tejun Heoe22bee72010-06-29 10:07:14 +0200766 struct global_cwq *gcwq = worker->gcwq;
767 unsigned int oflags = worker->flags;
768
Tejun Heocb444762010-07-02 10:03:50 +0200769 WARN_ON_ONCE(worker->task != current);
770
Tejun Heod302f012010-06-29 10:07:13 +0200771 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200772
773 /* if transitioning out of NOT_RUNNING, increment nr_running */
774 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
775 if (!(worker->flags & WORKER_NOT_RUNNING))
776 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200777}
778
779/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200780 * busy_worker_head - return the busy hash head for a work
781 * @gcwq: gcwq of interest
782 * @work: work to be hashed
783 *
784 * Return hash head of @gcwq for @work.
785 *
786 * CONTEXT:
787 * spin_lock_irq(gcwq->lock).
788 *
789 * RETURNS:
790 * Pointer to the hash head.
791 */
792static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
793 struct work_struct *work)
794{
795 const int base_shift = ilog2(sizeof(struct work_struct));
796 unsigned long v = (unsigned long)work;
797
798 /* simple shift and fold hash, do we need something better? */
799 v >>= base_shift;
800 v += v >> BUSY_WORKER_HASH_ORDER;
801 v &= BUSY_WORKER_HASH_MASK;
802
803 return &gcwq->busy_hash[v];
804}
805
806/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200807 * __find_worker_executing_work - find worker which is executing a work
808 * @gcwq: gcwq of interest
809 * @bwh: hash head as returned by busy_worker_head()
810 * @work: work to find worker for
811 *
812 * Find a worker which is executing @work on @gcwq. @bwh should be
813 * the hash head obtained by calling busy_worker_head() with the same
814 * work.
815 *
816 * CONTEXT:
817 * spin_lock_irq(gcwq->lock).
818 *
819 * RETURNS:
820 * Pointer to worker which is executing @work if found, NULL
821 * otherwise.
822 */
823static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
824 struct hlist_head *bwh,
825 struct work_struct *work)
826{
827 struct worker *worker;
828 struct hlist_node *tmp;
829
830 hlist_for_each_entry(worker, tmp, bwh, hentry)
831 if (worker->current_work == work)
832 return worker;
833 return NULL;
834}
835
836/**
837 * find_worker_executing_work - find worker which is executing a work
838 * @gcwq: gcwq of interest
839 * @work: work to find worker for
840 *
841 * Find a worker which is executing @work on @gcwq. This function is
842 * identical to __find_worker_executing_work() except that this
843 * function calculates @bwh itself.
844 *
845 * CONTEXT:
846 * spin_lock_irq(gcwq->lock).
847 *
848 * RETURNS:
849 * Pointer to worker which is executing @work if found, NULL
850 * otherwise.
851 */
852static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
853 struct work_struct *work)
854{
855 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
856 work);
857}
858
859/**
Tejun Heo649027d2010-06-29 10:07:14 +0200860 * gcwq_determine_ins_pos - find insertion position
861 * @gcwq: gcwq of interest
862 * @cwq: cwq a work is being queued for
863 *
864 * A work for @cwq is about to be queued on @gcwq, determine insertion
865 * position for the work. If @cwq is for HIGHPRI wq, the work is
866 * queued at the head of the queue but in FIFO order with respect to
867 * other HIGHPRI works; otherwise, at the end of the queue. This
868 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
869 * there are HIGHPRI works pending.
870 *
871 * CONTEXT:
872 * spin_lock_irq(gcwq->lock).
873 *
874 * RETURNS:
875 * Pointer to inserstion position.
876 */
877static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
878 struct cpu_workqueue_struct *cwq)
879{
880 struct work_struct *twork;
881
882 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
883 return &gcwq->worklist;
884
885 list_for_each_entry(twork, &gcwq->worklist, entry) {
886 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
887
888 if (!(tcwq->wq->flags & WQ_HIGHPRI))
889 break;
890 }
891
892 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
893 return &twork->entry;
894}
895
896/**
Tejun Heo7e116292010-06-29 10:07:13 +0200897 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200898 * @cwq: cwq @work belongs to
899 * @work: work to insert
900 * @head: insertion point
901 * @extra_flags: extra WORK_STRUCT_* flags to set
902 *
Tejun Heo7e116292010-06-29 10:07:13 +0200903 * Insert @work which belongs to @cwq into @gcwq after @head.
904 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200905 *
906 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200907 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200908 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700909static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200910 struct work_struct *work, struct list_head *head,
911 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700912{
Tejun Heoe22bee72010-06-29 10:07:14 +0200913 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100914
Tejun Heo4690c4a2010-06-29 10:07:10 +0200915 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200916 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200917
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700918 /*
919 * Ensure that we get the right work->data if we see the
920 * result of list_add() below, see try_to_grab_pending().
921 */
922 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200923
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700924 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200925
926 /*
927 * Ensure either worker_sched_deactivated() sees the above
928 * list_add_tail() or we see zero nr_running to avoid workers
929 * lying around lazily while there are works to be processed.
930 */
931 smp_mb();
932
Tejun Heo649027d2010-06-29 10:07:14 +0200933 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200934 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700935}
936
Tejun Heo4690c4a2010-06-29 10:07:10 +0200937static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct work_struct *work)
939{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200940 struct global_cwq *gcwq;
941 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200942 struct list_head *worklist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 unsigned long flags;
944
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900945 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200946
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200947 /* determine gcwq to use */
948 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200949 struct global_cwq *last_gcwq;
950
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200951 if (unlikely(cpu == WORK_CPU_UNBOUND))
952 cpu = raw_smp_processor_id();
953
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200954 /*
955 * It's multi cpu. If @wq is non-reentrant and @work
956 * was previously on a different cpu, it might still
957 * be running there, in which case the work needs to
958 * be queued on that cpu to guarantee non-reentrance.
959 */
Tejun Heo502ca9d2010-06-29 10:07:13 +0200960 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200961 if (wq->flags & WQ_NON_REENTRANT &&
962 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
963 struct worker *worker;
964
965 spin_lock_irqsave(&last_gcwq->lock, flags);
966
967 worker = find_worker_executing_work(last_gcwq, work);
968
969 if (worker && worker->current_cwq->wq == wq)
970 gcwq = last_gcwq;
971 else {
972 /* meh... not running there, queue here */
973 spin_unlock_irqrestore(&last_gcwq->lock, flags);
974 spin_lock_irqsave(&gcwq->lock, flags);
975 }
976 } else
977 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +0200978 } else {
979 gcwq = get_gcwq(WORK_CPU_UNBOUND);
980 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +0200981 }
982
983 /* gcwq determined, get cwq and queue */
984 cwq = get_cwq(gcwq->cpu, wq);
985
Tejun Heo4690c4a2010-06-29 10:07:10 +0200986 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200987
Tejun Heo73f53c42010-06-29 10:07:11 +0200988 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200989
990 if (likely(cwq->nr_active < cwq->max_active)) {
991 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +0200992 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200993 } else
994 worklist = &cwq->delayed_works;
995
996 insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
997
Tejun Heo8b03ae32010-06-29 10:07:12 +0200998 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001001/**
1002 * queue_work - queue work on a workqueue
1003 * @wq: workqueue to use
1004 * @work: work to queue
1005 *
Alan Stern057647f2006-10-28 10:38:58 -07001006 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001008 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1009 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001011int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001013 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001015 ret = queue_work_on(get_cpu(), wq, work);
1016 put_cpu();
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return ret;
1019}
Dave Jonesae90dd52006-06-30 01:40:45 -04001020EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Zhang Ruic1a220e2008-07-23 21:28:39 -07001022/**
1023 * queue_work_on - queue work on specific cpu
1024 * @cpu: CPU number to execute work on
1025 * @wq: workqueue to use
1026 * @work: work to queue
1027 *
1028 * Returns 0 if @work was already on a queue, non-zero otherwise.
1029 *
1030 * We queue the work to a specific CPU, the caller must ensure it
1031 * can't go away.
1032 */
1033int
1034queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1035{
1036 int ret = 0;
1037
Tejun Heo22df02b2010-06-29 10:07:10 +02001038 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001039 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001040 ret = 1;
1041 }
1042 return ret;
1043}
1044EXPORT_SYMBOL_GPL(queue_work_on);
1045
Li Zefan6d141c32008-02-08 04:21:09 -08001046static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
David Howells52bad642006-11-22 14:54:01 +00001048 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001049 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Tejun Heo4690c4a2010-06-29 10:07:10 +02001051 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001054/**
1055 * queue_delayed_work - queue work on a workqueue after delay
1056 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001057 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001058 * @delay: number of jiffies to wait before queueing
1059 *
Alan Stern057647f2006-10-28 10:38:58 -07001060 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001061 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001062int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001063 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
David Howells52bad642006-11-22 14:54:01 +00001065 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001066 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001068 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
Dave Jonesae90dd52006-06-30 01:40:45 -04001070EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001072/**
1073 * queue_delayed_work_on - queue work on specific CPU after delay
1074 * @cpu: CPU number to execute work on
1075 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001076 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001077 * @delay: number of jiffies to wait before queueing
1078 *
Alan Stern057647f2006-10-28 10:38:58 -07001079 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001080 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001081int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001082 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001083{
1084 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001085 struct timer_list *timer = &dwork->timer;
1086 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001087
Tejun Heo22df02b2010-06-29 10:07:10 +02001088 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001089 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001090
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001091 BUG_ON(timer_pending(timer));
1092 BUG_ON(!list_empty(&work->entry));
1093
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001094 timer_stats_timer_set_start_info(&dwork->timer);
1095
Tejun Heo7a22ad72010-06-29 10:07:13 +02001096 /*
1097 * This stores cwq for the moment, for the timer_fn.
1098 * Note that the work's gcwq is preserved to allow
1099 * reentrance detection for delayed works.
1100 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001101 if (!(wq->flags & WQ_UNBOUND)) {
1102 struct global_cwq *gcwq = get_work_gcwq(work);
1103
1104 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1105 lcpu = gcwq->cpu;
1106 else
1107 lcpu = raw_smp_processor_id();
1108 } else
1109 lcpu = WORK_CPU_UNBOUND;
1110
Tejun Heo7a22ad72010-06-29 10:07:13 +02001111 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001112
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001113 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001114 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001115 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001116
1117 if (unlikely(cpu >= 0))
1118 add_timer_on(timer, cpu);
1119 else
1120 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001121 ret = 1;
1122 }
1123 return ret;
1124}
Dave Jonesae90dd52006-06-30 01:40:45 -04001125EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Tejun Heoc8e55f32010-06-29 10:07:12 +02001127/**
1128 * worker_enter_idle - enter idle state
1129 * @worker: worker which is entering idle state
1130 *
1131 * @worker is entering idle state. Update stats and idle timer if
1132 * necessary.
1133 *
1134 * LOCKING:
1135 * spin_lock_irq(gcwq->lock).
1136 */
1137static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001139 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Tejun Heoc8e55f32010-06-29 10:07:12 +02001141 BUG_ON(worker->flags & WORKER_IDLE);
1142 BUG_ON(!list_empty(&worker->entry) &&
1143 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Tejun Heocb444762010-07-02 10:03:50 +02001145 /* can't use worker_set_flags(), also called from start_worker() */
1146 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001147 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001148 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001149
Tejun Heoc8e55f32010-06-29 10:07:12 +02001150 /* idle_list is LIFO */
1151 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001152
Tejun Heoe22bee72010-06-29 10:07:14 +02001153 if (likely(!(worker->flags & WORKER_ROGUE))) {
1154 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1155 mod_timer(&gcwq->idle_timer,
1156 jiffies + IDLE_WORKER_TIMEOUT);
1157 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001158 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001159
1160 /* sanity check nr_running */
1161 WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
1162 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Tejun Heoc8e55f32010-06-29 10:07:12 +02001165/**
1166 * worker_leave_idle - leave idle state
1167 * @worker: worker which is leaving idle state
1168 *
1169 * @worker is leaving idle state. Update stats.
1170 *
1171 * LOCKING:
1172 * spin_lock_irq(gcwq->lock).
1173 */
1174static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001176 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Tejun Heoc8e55f32010-06-29 10:07:12 +02001178 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001179 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001180 gcwq->nr_idle--;
1181 list_del_init(&worker->entry);
1182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Tejun Heoe22bee72010-06-29 10:07:14 +02001184/**
1185 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1186 * @worker: self
1187 *
1188 * Works which are scheduled while the cpu is online must at least be
1189 * scheduled to a worker which is bound to the cpu so that if they are
1190 * flushed from cpu callbacks while cpu is going down, they are
1191 * guaranteed to execute on the cpu.
1192 *
1193 * This function is to be used by rogue workers and rescuers to bind
1194 * themselves to the target cpu and may race with cpu going down or
1195 * coming online. kthread_bind() can't be used because it may put the
1196 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1197 * verbatim as it's best effort and blocking and gcwq may be
1198 * [dis]associated in the meantime.
1199 *
1200 * This function tries set_cpus_allowed() and locks gcwq and verifies
1201 * the binding against GCWQ_DISASSOCIATED which is set during
1202 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1203 * idle state or fetches works without dropping lock, it can guarantee
1204 * the scheduling requirement described in the first paragraph.
1205 *
1206 * CONTEXT:
1207 * Might sleep. Called without any lock but returns with gcwq->lock
1208 * held.
1209 *
1210 * RETURNS:
1211 * %true if the associated gcwq is online (@worker is successfully
1212 * bound), %false if offline.
1213 */
1214static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001215__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001216{
1217 struct global_cwq *gcwq = worker->gcwq;
1218 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Tejun Heoe22bee72010-06-29 10:07:14 +02001220 while (true) {
1221 /*
1222 * The following call may fail, succeed or succeed
1223 * without actually migrating the task to the cpu if
1224 * it races with cpu hotunplug operation. Verify
1225 * against GCWQ_DISASSOCIATED.
1226 */
Tejun Heof3421792010-07-02 10:03:51 +02001227 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1228 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001229
Tejun Heoe22bee72010-06-29 10:07:14 +02001230 spin_lock_irq(&gcwq->lock);
1231 if (gcwq->flags & GCWQ_DISASSOCIATED)
1232 return false;
1233 if (task_cpu(task) == gcwq->cpu &&
1234 cpumask_equal(&current->cpus_allowed,
1235 get_cpu_mask(gcwq->cpu)))
1236 return true;
1237 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001238
Tejun Heoe22bee72010-06-29 10:07:14 +02001239 /* CPU has come up inbetween, retry migration */
1240 cpu_relax();
1241 }
1242}
1243
1244/*
1245 * Function for worker->rebind_work used to rebind rogue busy workers
1246 * to the associated cpu which is coming back online. This is
1247 * scheduled by cpu up but can race with other cpu hotplug operations
1248 * and may be executed twice without intervening cpu down.
1249 */
1250static void worker_rebind_fn(struct work_struct *work)
1251{
1252 struct worker *worker = container_of(work, struct worker, rebind_work);
1253 struct global_cwq *gcwq = worker->gcwq;
1254
1255 if (worker_maybe_bind_and_lock(worker))
1256 worker_clr_flags(worker, WORKER_REBIND);
1257
1258 spin_unlock_irq(&gcwq->lock);
1259}
1260
Tejun Heoc34056a2010-06-29 10:07:11 +02001261static struct worker *alloc_worker(void)
1262{
1263 struct worker *worker;
1264
1265 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001266 if (worker) {
1267 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001268 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001269 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1270 /* on creation a worker is in !idle && prep state */
1271 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001272 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001273 return worker;
1274}
1275
1276/**
1277 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001278 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001279 * @bind: whether to set affinity to @cpu or not
1280 *
Tejun Heo7e116292010-06-29 10:07:13 +02001281 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001282 * can be started by calling start_worker() or destroyed using
1283 * destroy_worker().
1284 *
1285 * CONTEXT:
1286 * Might sleep. Does GFP_KERNEL allocations.
1287 *
1288 * RETURNS:
1289 * Pointer to the newly created worker.
1290 */
Tejun Heo7e116292010-06-29 10:07:13 +02001291static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001292{
Tejun Heof3421792010-07-02 10:03:51 +02001293 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001294 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001295 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001296
Tejun Heo8b03ae32010-06-29 10:07:12 +02001297 spin_lock_irq(&gcwq->lock);
1298 while (ida_get_new(&gcwq->worker_ida, &id)) {
1299 spin_unlock_irq(&gcwq->lock);
1300 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001301 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001302 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001303 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001304 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001305
1306 worker = alloc_worker();
1307 if (!worker)
1308 goto fail;
1309
Tejun Heo8b03ae32010-06-29 10:07:12 +02001310 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001311 worker->id = id;
1312
Tejun Heof3421792010-07-02 10:03:51 +02001313 if (!on_unbound_cpu)
1314 worker->task = kthread_create(worker_thread, worker,
1315 "kworker/%u:%d", gcwq->cpu, id);
1316 else
1317 worker->task = kthread_create(worker_thread, worker,
1318 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001319 if (IS_ERR(worker->task))
1320 goto fail;
1321
Tejun Heodb7bccf2010-06-29 10:07:12 +02001322 /*
1323 * A rogue worker will become a regular one if CPU comes
1324 * online later on. Make sure every worker has
1325 * PF_THREAD_BOUND set.
1326 */
Tejun Heof3421792010-07-02 10:03:51 +02001327 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001328 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001329 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001330 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001331 if (on_unbound_cpu)
1332 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001334
Tejun Heoc34056a2010-06-29 10:07:11 +02001335 return worker;
1336fail:
1337 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001338 spin_lock_irq(&gcwq->lock);
1339 ida_remove(&gcwq->worker_ida, id);
1340 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001341 }
1342 kfree(worker);
1343 return NULL;
1344}
1345
1346/**
1347 * start_worker - start a newly created worker
1348 * @worker: worker to start
1349 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001350 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001351 *
1352 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001353 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001354 */
1355static void start_worker(struct worker *worker)
1356{
Tejun Heocb444762010-07-02 10:03:50 +02001357 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001358 worker->gcwq->nr_workers++;
1359 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001360 wake_up_process(worker->task);
1361}
1362
1363/**
1364 * destroy_worker - destroy a workqueue worker
1365 * @worker: worker to be destroyed
1366 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001367 * Destroy @worker and adjust @gcwq stats accordingly.
1368 *
1369 * CONTEXT:
1370 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001371 */
1372static void destroy_worker(struct worker *worker)
1373{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001374 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001375 int id = worker->id;
1376
1377 /* sanity check frenzy */
1378 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001379 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001380
Tejun Heoc8e55f32010-06-29 10:07:12 +02001381 if (worker->flags & WORKER_STARTED)
1382 gcwq->nr_workers--;
1383 if (worker->flags & WORKER_IDLE)
1384 gcwq->nr_idle--;
1385
1386 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001387 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001388
1389 spin_unlock_irq(&gcwq->lock);
1390
Tejun Heoc34056a2010-06-29 10:07:11 +02001391 kthread_stop(worker->task);
1392 kfree(worker);
1393
Tejun Heo8b03ae32010-06-29 10:07:12 +02001394 spin_lock_irq(&gcwq->lock);
1395 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001396}
1397
Tejun Heoe22bee72010-06-29 10:07:14 +02001398static void idle_worker_timeout(unsigned long __gcwq)
1399{
1400 struct global_cwq *gcwq = (void *)__gcwq;
1401
1402 spin_lock_irq(&gcwq->lock);
1403
1404 if (too_many_workers(gcwq)) {
1405 struct worker *worker;
1406 unsigned long expires;
1407
1408 /* idle_list is kept in LIFO order, check the last one */
1409 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1410 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1411
1412 if (time_before(jiffies, expires))
1413 mod_timer(&gcwq->idle_timer, expires);
1414 else {
1415 /* it's been idle for too long, wake up manager */
1416 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1417 wake_up_worker(gcwq);
1418 }
1419 }
1420
1421 spin_unlock_irq(&gcwq->lock);
1422}
1423
1424static bool send_mayday(struct work_struct *work)
1425{
1426 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1427 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001428 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001429
1430 if (!(wq->flags & WQ_RESCUER))
1431 return false;
1432
1433 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001434 cpu = cwq->gcwq->cpu;
1435 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1436 if (cpu == WORK_CPU_UNBOUND)
1437 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001438 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001439 wake_up_process(wq->rescuer->task);
1440 return true;
1441}
1442
1443static void gcwq_mayday_timeout(unsigned long __gcwq)
1444{
1445 struct global_cwq *gcwq = (void *)__gcwq;
1446 struct work_struct *work;
1447
1448 spin_lock_irq(&gcwq->lock);
1449
1450 if (need_to_create_worker(gcwq)) {
1451 /*
1452 * We've been trying to create a new worker but
1453 * haven't been successful. We might be hitting an
1454 * allocation deadlock. Send distress signals to
1455 * rescuers.
1456 */
1457 list_for_each_entry(work, &gcwq->worklist, entry)
1458 send_mayday(work);
1459 }
1460
1461 spin_unlock_irq(&gcwq->lock);
1462
1463 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1464}
1465
1466/**
1467 * maybe_create_worker - create a new worker if necessary
1468 * @gcwq: gcwq to create a new worker for
1469 *
1470 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1471 * have at least one idle worker on return from this function. If
1472 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1473 * sent to all rescuers with works scheduled on @gcwq to resolve
1474 * possible allocation deadlock.
1475 *
1476 * On return, need_to_create_worker() is guaranteed to be false and
1477 * may_start_working() true.
1478 *
1479 * LOCKING:
1480 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1481 * multiple times. Does GFP_KERNEL allocations. Called only from
1482 * manager.
1483 *
1484 * RETURNS:
1485 * false if no action was taken and gcwq->lock stayed locked, true
1486 * otherwise.
1487 */
1488static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001489__releases(&gcwq->lock)
1490__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001491{
1492 if (!need_to_create_worker(gcwq))
1493 return false;
1494restart:
Tejun Heo9f9c236442010-07-14 11:31:20 +02001495 spin_unlock_irq(&gcwq->lock);
1496
Tejun Heoe22bee72010-06-29 10:07:14 +02001497 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1498 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1499
1500 while (true) {
1501 struct worker *worker;
1502
Tejun Heoe22bee72010-06-29 10:07:14 +02001503 worker = create_worker(gcwq, true);
1504 if (worker) {
1505 del_timer_sync(&gcwq->mayday_timer);
1506 spin_lock_irq(&gcwq->lock);
1507 start_worker(worker);
1508 BUG_ON(need_to_create_worker(gcwq));
1509 return true;
1510 }
1511
1512 if (!need_to_create_worker(gcwq))
1513 break;
1514
Tejun Heoe22bee72010-06-29 10:07:14 +02001515 __set_current_state(TASK_INTERRUPTIBLE);
1516 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c236442010-07-14 11:31:20 +02001517
Tejun Heoe22bee72010-06-29 10:07:14 +02001518 if (!need_to_create_worker(gcwq))
1519 break;
1520 }
1521
Tejun Heoe22bee72010-06-29 10:07:14 +02001522 del_timer_sync(&gcwq->mayday_timer);
1523 spin_lock_irq(&gcwq->lock);
1524 if (need_to_create_worker(gcwq))
1525 goto restart;
1526 return true;
1527}
1528
1529/**
1530 * maybe_destroy_worker - destroy workers which have been idle for a while
1531 * @gcwq: gcwq to destroy workers for
1532 *
1533 * Destroy @gcwq workers which have been idle for longer than
1534 * IDLE_WORKER_TIMEOUT.
1535 *
1536 * LOCKING:
1537 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1538 * multiple times. Called only from manager.
1539 *
1540 * RETURNS:
1541 * false if no action was taken and gcwq->lock stayed locked, true
1542 * otherwise.
1543 */
1544static bool maybe_destroy_workers(struct global_cwq *gcwq)
1545{
1546 bool ret = false;
1547
1548 while (too_many_workers(gcwq)) {
1549 struct worker *worker;
1550 unsigned long expires;
1551
1552 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1553 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1554
1555 if (time_before(jiffies, expires)) {
1556 mod_timer(&gcwq->idle_timer, expires);
1557 break;
1558 }
1559
1560 destroy_worker(worker);
1561 ret = true;
1562 }
1563
1564 return ret;
1565}
1566
1567/**
1568 * manage_workers - manage worker pool
1569 * @worker: self
1570 *
1571 * Assume the manager role and manage gcwq worker pool @worker belongs
1572 * to. At any given time, there can be only zero or one manager per
1573 * gcwq. The exclusion is handled automatically by this function.
1574 *
1575 * The caller can safely start processing works on false return. On
1576 * true return, it's guaranteed that need_to_create_worker() is false
1577 * and may_start_working() is true.
1578 *
1579 * CONTEXT:
1580 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1581 * multiple times. Does GFP_KERNEL allocations.
1582 *
1583 * RETURNS:
1584 * false if no action was taken and gcwq->lock stayed locked, true if
1585 * some action was taken.
1586 */
1587static bool manage_workers(struct worker *worker)
1588{
1589 struct global_cwq *gcwq = worker->gcwq;
1590 bool ret = false;
1591
1592 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1593 return ret;
1594
1595 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1596 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1597
1598 /*
1599 * Destroy and then create so that may_start_working() is true
1600 * on return.
1601 */
1602 ret |= maybe_destroy_workers(gcwq);
1603 ret |= maybe_create_worker(gcwq);
1604
1605 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1606
1607 /*
1608 * The trustee might be waiting to take over the manager
1609 * position, tell it we're done.
1610 */
1611 if (unlikely(gcwq->trustee))
1612 wake_up_all(&gcwq->trustee_wait);
1613
1614 return ret;
1615}
1616
Tejun Heoa62428c2010-06-29 10:07:10 +02001617/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001618 * move_linked_works - move linked works to a list
1619 * @work: start of series of works to be scheduled
1620 * @head: target list to append @work to
1621 * @nextp: out paramter for nested worklist walking
1622 *
1623 * Schedule linked works starting from @work to @head. Work series to
1624 * be scheduled starts at @work and includes any consecutive work with
1625 * WORK_STRUCT_LINKED set in its predecessor.
1626 *
1627 * If @nextp is not NULL, it's updated to point to the next work of
1628 * the last scheduled work. This allows move_linked_works() to be
1629 * nested inside outer list_for_each_entry_safe().
1630 *
1631 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001632 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001633 */
1634static void move_linked_works(struct work_struct *work, struct list_head *head,
1635 struct work_struct **nextp)
1636{
1637 struct work_struct *n;
1638
1639 /*
1640 * Linked worklist will always end before the end of the list,
1641 * use NULL for list head.
1642 */
1643 list_for_each_entry_safe_from(work, n, NULL, entry) {
1644 list_move_tail(&work->entry, head);
1645 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1646 break;
1647 }
1648
1649 /*
1650 * If we're already inside safe list traversal and have moved
1651 * multiple works to the scheduled queue, the next position
1652 * needs to be updated.
1653 */
1654 if (nextp)
1655 *nextp = n;
1656}
1657
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001658static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1659{
1660 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1661 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001662 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001663
Tejun Heo649027d2010-06-29 10:07:14 +02001664 move_linked_works(work, pos, NULL);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001665 cwq->nr_active++;
1666}
1667
Tejun Heoaffee4b2010-06-29 10:07:12 +02001668/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001669 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1670 * @cwq: cwq of interest
1671 * @color: color of work which left the queue
1672 *
1673 * A work either has completed or is removed from pending queue,
1674 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1675 *
1676 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001677 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001678 */
1679static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
1680{
1681 /* ignore uncolored works */
1682 if (color == WORK_NO_COLOR)
1683 return;
1684
1685 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001686 cwq->nr_active--;
1687
Tejun Heo502ca9d2010-06-29 10:07:13 +02001688 if (!list_empty(&cwq->delayed_works)) {
1689 /* one down, submit a delayed one */
1690 if (cwq->nr_active < cwq->max_active)
1691 cwq_activate_first_delayed(cwq);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001692 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001693
1694 /* is flush in progress and are we at the flushing tip? */
1695 if (likely(cwq->flush_color != color))
1696 return;
1697
1698 /* are there still in-flight works? */
1699 if (cwq->nr_in_flight[color])
1700 return;
1701
1702 /* this cwq is done, clear flush_color */
1703 cwq->flush_color = -1;
1704
1705 /*
1706 * If this was the last cwq, wake up the first flusher. It
1707 * will handle the rest.
1708 */
1709 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1710 complete(&cwq->wq->first_flusher->done);
1711}
1712
1713/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001714 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001715 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001716 * @work: work to process
1717 *
1718 * Process @work. This function contains all the logics necessary to
1719 * process a single work including synchronization against and
1720 * interaction with other workers on the same cpu, queueing and
1721 * flushing. As long as context requirement is met, any worker can
1722 * call this function to process a work.
1723 *
1724 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001725 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001726 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001727static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001728__releases(&gcwq->lock)
1729__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001730{
Tejun Heo7e116292010-06-29 10:07:13 +02001731 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001732 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001733 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001734 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001735 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001736 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001737 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001738#ifdef CONFIG_LOCKDEP
1739 /*
1740 * It is permissible to free the struct work_struct from
1741 * inside the function that is called from it, this we need to
1742 * take into account for lockdep too. To avoid bogus "held
1743 * lock freed" warnings as well as problems when looking into
1744 * work->lockdep_map, make a copy and use that here.
1745 */
1746 struct lockdep_map lockdep_map = work->lockdep_map;
1747#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001748 /*
1749 * A single work shouldn't be executed concurrently by
1750 * multiple workers on a single cpu. Check whether anyone is
1751 * already processing the work. If so, defer the work to the
1752 * currently executing one.
1753 */
1754 collision = __find_worker_executing_work(gcwq, bwh, work);
1755 if (unlikely(collision)) {
1756 move_linked_works(work, &collision->scheduled, NULL);
1757 return;
1758 }
1759
Tejun Heoa62428c2010-06-29 10:07:10 +02001760 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001761 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001762 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001763 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001764 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001765 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001766
Tejun Heo7a22ad72010-06-29 10:07:13 +02001767 /* record the current cpu number in the work data and dequeue */
1768 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001769 list_del_init(&work->entry);
1770
Tejun Heo649027d2010-06-29 10:07:14 +02001771 /*
1772 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1773 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1774 */
1775 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1776 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1777 struct work_struct, entry);
1778
1779 if (!list_empty(&gcwq->worklist) &&
1780 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1781 wake_up_worker(gcwq);
1782 else
1783 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1784 }
1785
Tejun Heofb0e7be2010-06-29 10:07:15 +02001786 /*
1787 * CPU intensive works don't participate in concurrency
1788 * management. They're the scheduler's responsibility.
1789 */
1790 if (unlikely(cpu_intensive))
1791 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1792
Tejun Heo8b03ae32010-06-29 10:07:12 +02001793 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001794
Tejun Heoa62428c2010-06-29 10:07:10 +02001795 work_clear_pending(work);
1796 lock_map_acquire(&cwq->wq->lockdep_map);
1797 lock_map_acquire(&lockdep_map);
1798 f(work);
1799 lock_map_release(&lockdep_map);
1800 lock_map_release(&cwq->wq->lockdep_map);
1801
1802 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1803 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1804 "%s/0x%08x/%d\n",
1805 current->comm, preempt_count(), task_pid_nr(current));
1806 printk(KERN_ERR " last function: ");
1807 print_symbol("%s\n", (unsigned long)f);
1808 debug_show_held_locks(current);
1809 dump_stack();
1810 }
1811
Tejun Heo8b03ae32010-06-29 10:07:12 +02001812 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001813
Tejun Heofb0e7be2010-06-29 10:07:15 +02001814 /* clear cpu intensive status */
1815 if (unlikely(cpu_intensive))
1816 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1817
Tejun Heoa62428c2010-06-29 10:07:10 +02001818 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001819 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001820 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001821 worker->current_cwq = NULL;
Tejun Heo73f53c42010-06-29 10:07:11 +02001822 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02001823}
1824
Tejun Heoaffee4b2010-06-29 10:07:12 +02001825/**
1826 * process_scheduled_works - process scheduled works
1827 * @worker: self
1828 *
1829 * Process all scheduled works. Please note that the scheduled list
1830 * may change while processing a work, so this function repeatedly
1831 * fetches a work from the top and executes it.
1832 *
1833 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001834 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001835 * multiple times.
1836 */
1837static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001839 while (!list_empty(&worker->scheduled)) {
1840 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001842 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Tejun Heo4690c4a2010-06-29 10:07:10 +02001846/**
1847 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001848 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001849 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001850 * The gcwq worker thread function. There's a single dynamic pool of
1851 * these per each cpu. These workers process all works regardless of
1852 * their specific target workqueue. The only exception is works which
1853 * belong to workqueues with a rescuer which will be explained in
1854 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001855 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001856static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Tejun Heoc34056a2010-06-29 10:07:11 +02001858 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001859 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Tejun Heoe22bee72010-06-29 10:07:14 +02001861 /* tell the scheduler that this is a workqueue worker */
1862 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001863woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001864 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Tejun Heoc8e55f32010-06-29 10:07:12 +02001866 /* DIE can be set only while we're idle, checking here is enough */
1867 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001868 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
Tejun Heoc8e55f32010-06-29 10:07:12 +02001873 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001874recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001875 /* no more worker necessary? */
1876 if (!need_more_worker(gcwq))
1877 goto sleep;
1878
1879 /* do we need to manage? */
1880 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1881 goto recheck;
1882
Tejun Heoc8e55f32010-06-29 10:07:12 +02001883 /*
1884 * ->scheduled list can only be filled while a worker is
1885 * preparing to process a work or actually processing it.
1886 * Make sure nobody diddled with it while I was sleeping.
1887 */
1888 BUG_ON(!list_empty(&worker->scheduled));
1889
Tejun Heoe22bee72010-06-29 10:07:14 +02001890 /*
1891 * When control reaches this point, we're guaranteed to have
1892 * at least one idle worker or that someone else has already
1893 * assumed the manager role.
1894 */
1895 worker_clr_flags(worker, WORKER_PREP);
1896
1897 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001898 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02001899 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001900 struct work_struct, entry);
1901
1902 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1903 /* optimization path, not strictly necessary */
1904 process_one_work(worker, work);
1905 if (unlikely(!list_empty(&worker->scheduled)))
1906 process_scheduled_works(worker);
1907 } else {
1908 move_linked_works(work, &worker->scheduled, NULL);
1909 process_scheduled_works(worker);
1910 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001912
Tejun Heoe22bee72010-06-29 10:07:14 +02001913 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001914sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
1916 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001917
Tejun Heoc8e55f32010-06-29 10:07:12 +02001918 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 * gcwq->lock is held and there's no work to process and no
1920 * need to manage, sleep. Workers are woken up only while
1921 * holding gcwq->lock or from local cpu, so setting the
1922 * current state before releasing gcwq->lock is enough to
1923 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001924 */
1925 worker_enter_idle(worker);
1926 __set_current_state(TASK_INTERRUPTIBLE);
1927 spin_unlock_irq(&gcwq->lock);
1928 schedule();
1929 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Tejun Heoe22bee72010-06-29 10:07:14 +02001932/**
1933 * rescuer_thread - the rescuer thread function
1934 * @__wq: the associated workqueue
1935 *
1936 * Workqueue rescuer thread function. There's one rescuer for each
1937 * workqueue which has WQ_RESCUER set.
1938 *
1939 * Regular work processing on a gcwq may block trying to create a new
1940 * worker which uses GFP_KERNEL allocation which has slight chance of
1941 * developing into deadlock if some works currently on the same queue
1942 * need to be processed to satisfy the GFP_KERNEL allocation. This is
1943 * the problem rescuer solves.
1944 *
1945 * When such condition is possible, the gcwq summons rescuers of all
1946 * workqueues which have works queued on the gcwq and let them process
1947 * those works so that forward progress can be guaranteed.
1948 *
1949 * This should happen rarely.
1950 */
1951static int rescuer_thread(void *__wq)
1952{
1953 struct workqueue_struct *wq = __wq;
1954 struct worker *rescuer = wq->rescuer;
1955 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02001956 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 unsigned int cpu;
1958
1959 set_user_nice(current, RESCUER_NICE_LEVEL);
1960repeat:
1961 set_current_state(TASK_INTERRUPTIBLE);
1962
1963 if (kthread_should_stop())
1964 return 0;
1965
Tejun Heof3421792010-07-02 10:03:51 +02001966 /*
1967 * See whether any cpu is asking for help. Unbounded
1968 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
1969 */
Tejun Heof2e005a2010-07-20 15:59:09 +02001970 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02001971 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
1972 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 struct global_cwq *gcwq = cwq->gcwq;
1974 struct work_struct *work, *n;
1975
1976 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02001977 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02001978
1979 /* migrate to the target cpu if possible */
1980 rescuer->gcwq = gcwq;
1981 worker_maybe_bind_and_lock(rescuer);
1982
1983 /*
1984 * Slurp in all works issued via this workqueue and
1985 * process'em.
1986 */
1987 BUG_ON(!list_empty(&rescuer->scheduled));
1988 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
1989 if (get_work_cwq(work) == cwq)
1990 move_linked_works(work, scheduled, &n);
1991
1992 process_scheduled_works(rescuer);
1993 spin_unlock_irq(&gcwq->lock);
1994 }
1995
1996 schedule();
1997 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002000struct wq_barrier {
2001 struct work_struct work;
2002 struct completion done;
2003};
2004
2005static void wq_barrier_func(struct work_struct *work)
2006{
2007 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2008 complete(&barr->done);
2009}
2010
Tejun Heo4690c4a2010-06-29 10:07:10 +02002011/**
2012 * insert_wq_barrier - insert a barrier work
2013 * @cwq: cwq to insert barrier into
2014 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002015 * @target: target work to attach @barr to
2016 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002017 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002018 * @barr is linked to @target such that @barr is completed only after
2019 * @target finishes execution. Please note that the ordering
2020 * guarantee is observed only with respect to @target and on the local
2021 * cpu.
2022 *
2023 * Currently, a queued barrier can't be canceled. This is because
2024 * try_to_grab_pending() can't determine whether the work to be
2025 * grabbed is at the head of the queue and thus can't clear LINKED
2026 * flag of the previous work while there must be a valid next work
2027 * after a work with LINKED flag set.
2028 *
2029 * Note that when @worker is non-NULL, @target may be modified
2030 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002031 *
2032 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002033 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002034 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002035static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002036 struct wq_barrier *barr,
2037 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002038{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002039 struct list_head *head;
2040 unsigned int linked = 0;
2041
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002042 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002043 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002044 * as we know for sure that this will not trigger any of the
2045 * checks and call back into the fixup functions where we
2046 * might deadlock.
2047 */
2048 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002049 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002050 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002051
Tejun Heoaffee4b2010-06-29 10:07:12 +02002052 /*
2053 * If @target is currently being executed, schedule the
2054 * barrier to the worker; otherwise, put it after @target.
2055 */
2056 if (worker)
2057 head = worker->scheduled.next;
2058 else {
2059 unsigned long *bits = work_data_bits(target);
2060
2061 head = target->entry.next;
2062 /* there can already be other linked works, inherit and set */
2063 linked = *bits & WORK_STRUCT_LINKED;
2064 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2065 }
2066
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002067 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002068 insert_work(cwq, &barr->work, head,
2069 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002070}
2071
Tejun Heo73f53c42010-06-29 10:07:11 +02002072/**
2073 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2074 * @wq: workqueue being flushed
2075 * @flush_color: new flush color, < 0 for no-op
2076 * @work_color: new work color, < 0 for no-op
2077 *
2078 * Prepare cwqs for workqueue flushing.
2079 *
2080 * If @flush_color is non-negative, flush_color on all cwqs should be
2081 * -1. If no cwq has in-flight commands at the specified color, all
2082 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2083 * has in flight commands, its cwq->flush_color is set to
2084 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2085 * wakeup logic is armed and %true is returned.
2086 *
2087 * The caller should have initialized @wq->first_flusher prior to
2088 * calling this function with non-negative @flush_color. If
2089 * @flush_color is negative, no flush color update is done and %false
2090 * is returned.
2091 *
2092 * If @work_color is non-negative, all cwqs should have the same
2093 * work_color which is previous to @work_color and all will be
2094 * advanced to @work_color.
2095 *
2096 * CONTEXT:
2097 * mutex_lock(wq->flush_mutex).
2098 *
2099 * RETURNS:
2100 * %true if @flush_color >= 0 and there's something to flush. %false
2101 * otherwise.
2102 */
2103static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2104 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Tejun Heo73f53c42010-06-29 10:07:11 +02002106 bool wait = false;
2107 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002108
Tejun Heo73f53c42010-06-29 10:07:11 +02002109 if (flush_color >= 0) {
2110 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2111 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002112 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002113
Tejun Heof3421792010-07-02 10:03:51 +02002114 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002115 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002116 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002117
Tejun Heo8b03ae32010-06-29 10:07:12 +02002118 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002119
2120 if (flush_color >= 0) {
2121 BUG_ON(cwq->flush_color != -1);
2122
2123 if (cwq->nr_in_flight[flush_color]) {
2124 cwq->flush_color = flush_color;
2125 atomic_inc(&wq->nr_cwqs_to_flush);
2126 wait = true;
2127 }
2128 }
2129
2130 if (work_color >= 0) {
2131 BUG_ON(work_color != work_next_color(cwq->work_color));
2132 cwq->work_color = work_color;
2133 }
2134
Tejun Heo8b03ae32010-06-29 10:07:12 +02002135 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002136 }
2137
2138 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2139 complete(&wq->first_flusher->done);
2140
2141 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002144/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002146 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 *
2148 * Forces execution of the workqueue and blocks until its completion.
2149 * This is typically used in driver shutdown handlers.
2150 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002151 * We sleep until all works which were queued on entry have been handled,
2152 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002154void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Tejun Heo73f53c42010-06-29 10:07:11 +02002156 struct wq_flusher this_flusher = {
2157 .list = LIST_HEAD_INIT(this_flusher.list),
2158 .flush_color = -1,
2159 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2160 };
2161 int next_color;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002162
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002163 lock_map_acquire(&wq->lockdep_map);
2164 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002165
2166 mutex_lock(&wq->flush_mutex);
2167
2168 /*
2169 * Start-to-wait phase
2170 */
2171 next_color = work_next_color(wq->work_color);
2172
2173 if (next_color != wq->flush_color) {
2174 /*
2175 * Color space is not full. The current work_color
2176 * becomes our flush_color and work_color is advanced
2177 * by one.
2178 */
2179 BUG_ON(!list_empty(&wq->flusher_overflow));
2180 this_flusher.flush_color = wq->work_color;
2181 wq->work_color = next_color;
2182
2183 if (!wq->first_flusher) {
2184 /* no flush in progress, become the first flusher */
2185 BUG_ON(wq->flush_color != this_flusher.flush_color);
2186
2187 wq->first_flusher = &this_flusher;
2188
2189 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2190 wq->work_color)) {
2191 /* nothing to flush, done */
2192 wq->flush_color = next_color;
2193 wq->first_flusher = NULL;
2194 goto out_unlock;
2195 }
2196 } else {
2197 /* wait in queue */
2198 BUG_ON(wq->flush_color == this_flusher.flush_color);
2199 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2200 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2201 }
2202 } else {
2203 /*
2204 * Oops, color space is full, wait on overflow queue.
2205 * The next flush completion will assign us
2206 * flush_color and transfer to flusher_queue.
2207 */
2208 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2209 }
2210
2211 mutex_unlock(&wq->flush_mutex);
2212
2213 wait_for_completion(&this_flusher.done);
2214
2215 /*
2216 * Wake-up-and-cascade phase
2217 *
2218 * First flushers are responsible for cascading flushes and
2219 * handling overflow. Non-first flushers can simply return.
2220 */
2221 if (wq->first_flusher != &this_flusher)
2222 return;
2223
2224 mutex_lock(&wq->flush_mutex);
2225
Tejun Heo4ce48b32010-07-02 10:03:51 +02002226 /* we might have raced, check again with mutex held */
2227 if (wq->first_flusher != &this_flusher)
2228 goto out_unlock;
2229
Tejun Heo73f53c42010-06-29 10:07:11 +02002230 wq->first_flusher = NULL;
2231
2232 BUG_ON(!list_empty(&this_flusher.list));
2233 BUG_ON(wq->flush_color != this_flusher.flush_color);
2234
2235 while (true) {
2236 struct wq_flusher *next, *tmp;
2237
2238 /* complete all the flushers sharing the current flush color */
2239 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2240 if (next->flush_color != wq->flush_color)
2241 break;
2242 list_del_init(&next->list);
2243 complete(&next->done);
2244 }
2245
2246 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2247 wq->flush_color != work_next_color(wq->work_color));
2248
2249 /* this flush_color is finished, advance by one */
2250 wq->flush_color = work_next_color(wq->flush_color);
2251
2252 /* one color has been freed, handle overflow queue */
2253 if (!list_empty(&wq->flusher_overflow)) {
2254 /*
2255 * Assign the same color to all overflowed
2256 * flushers, advance work_color and append to
2257 * flusher_queue. This is the start-to-wait
2258 * phase for these overflowed flushers.
2259 */
2260 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2261 tmp->flush_color = wq->work_color;
2262
2263 wq->work_color = work_next_color(wq->work_color);
2264
2265 list_splice_tail_init(&wq->flusher_overflow,
2266 &wq->flusher_queue);
2267 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2268 }
2269
2270 if (list_empty(&wq->flusher_queue)) {
2271 BUG_ON(wq->flush_color != wq->work_color);
2272 break;
2273 }
2274
2275 /*
2276 * Need to flush more colors. Make the next flusher
2277 * the new first flusher and arm cwqs.
2278 */
2279 BUG_ON(wq->flush_color == wq->work_color);
2280 BUG_ON(wq->flush_color != next->flush_color);
2281
2282 list_del_init(&next->list);
2283 wq->first_flusher = next;
2284
2285 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2286 break;
2287
2288 /*
2289 * Meh... this color is already done, clear first
2290 * flusher and repeat cascading.
2291 */
2292 wq->first_flusher = NULL;
2293 }
2294
2295out_unlock:
2296 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
Dave Jonesae90dd52006-06-30 01:40:45 -04002298EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Oleg Nesterovdb700892008-07-25 01:47:49 -07002300/**
2301 * flush_work - block until a work_struct's callback has terminated
2302 * @work: the work which is to be flushed
2303 *
Oleg Nesterova67da702008-07-25 01:47:52 -07002304 * Returns false if @work has already terminated.
2305 *
Oleg Nesterovdb700892008-07-25 01:47:49 -07002306 * It is expected that, prior to calling flush_work(), the caller has
2307 * arranged for the work to not be requeued, otherwise it doesn't make
2308 * sense to use this function.
2309 */
2310int flush_work(struct work_struct *work)
2311{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002312 struct worker *worker = NULL;
Tejun Heo8b03ae32010-06-29 10:07:12 +02002313 struct global_cwq *gcwq;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002314 struct cpu_workqueue_struct *cwq;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002315 struct wq_barrier barr;
2316
2317 might_sleep();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002318 gcwq = get_work_gcwq(work);
2319 if (!gcwq)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002320 return 0;
2321
Tejun Heo8b03ae32010-06-29 10:07:12 +02002322 spin_lock_irq(&gcwq->lock);
Oleg Nesterovdb700892008-07-25 01:47:49 -07002323 if (!list_empty(&work->entry)) {
2324 /*
2325 * See the comment near try_to_grab_pending()->smp_rmb().
Tejun Heo7a22ad72010-06-29 10:07:13 +02002326 * If it was re-queued to a different gcwq under us, we
2327 * are not going to wait.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002328 */
2329 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002330 cwq = get_work_cwq(work);
2331 if (unlikely(!cwq || gcwq != cwq->gcwq))
Tejun Heo4690c4a2010-06-29 10:07:10 +02002332 goto already_gone;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002333 } else {
Tejun Heo7a22ad72010-06-29 10:07:13 +02002334 worker = find_worker_executing_work(gcwq, work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002335 if (!worker)
Tejun Heo4690c4a2010-06-29 10:07:10 +02002336 goto already_gone;
Tejun Heo7a22ad72010-06-29 10:07:13 +02002337 cwq = worker->current_cwq;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002338 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002339
Tejun Heoaffee4b2010-06-29 10:07:12 +02002340 insert_wq_barrier(cwq, &barr, work, worker);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002341 spin_unlock_irq(&gcwq->lock);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002342
Oleg Nesterovdb700892008-07-25 01:47:49 -07002343 lock_map_acquire(&cwq->wq->lockdep_map);
2344 lock_map_release(&cwq->wq->lockdep_map);
2345
Oleg Nesterovdb700892008-07-25 01:47:49 -07002346 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002347 destroy_work_on_stack(&barr.work);
Oleg Nesterovdb700892008-07-25 01:47:49 -07002348 return 1;
Tejun Heo4690c4a2010-06-29 10:07:10 +02002349already_gone:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002350 spin_unlock_irq(&gcwq->lock);
Tejun Heo4690c4a2010-06-29 10:07:10 +02002351 return 0;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002352}
2353EXPORT_SYMBOL_GPL(flush_work);
2354
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002355/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002356 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002357 * so this work can't be re-armed in any way.
2358 */
2359static int try_to_grab_pending(struct work_struct *work)
2360{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002361 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002362 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002363
Tejun Heo22df02b2010-06-29 10:07:10 +02002364 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002365 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002366
2367 /*
2368 * The queueing is in progress, or it is already queued. Try to
2369 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2370 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002371 gcwq = get_work_gcwq(work);
2372 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002373 return ret;
2374
Tejun Heo8b03ae32010-06-29 10:07:12 +02002375 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002376 if (!list_empty(&work->entry)) {
2377 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002378 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002379 * In that case we must see the new value after rmb(), see
2380 * insert_work()->wmb().
2381 */
2382 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002383 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002384 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002385 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002386 cwq_dec_nr_in_flight(get_work_cwq(work),
2387 get_work_color(work));
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002388 ret = 1;
2389 }
2390 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002391 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002392
2393 return ret;
2394}
2395
Tejun Heo7a22ad72010-06-29 10:07:13 +02002396static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002397{
2398 struct wq_barrier barr;
Tejun Heoaffee4b2010-06-29 10:07:12 +02002399 struct worker *worker;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002400
Tejun Heo8b03ae32010-06-29 10:07:12 +02002401 spin_lock_irq(&gcwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002402
Tejun Heo7a22ad72010-06-29 10:07:13 +02002403 worker = find_worker_executing_work(gcwq, work);
2404 if (unlikely(worker))
2405 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002406
Tejun Heo8b03ae32010-06-29 10:07:12 +02002407 spin_unlock_irq(&gcwq->lock);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002408
Tejun Heoaffee4b2010-06-29 10:07:12 +02002409 if (unlikely(worker)) {
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002410 wait_for_completion(&barr.done);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002411 destroy_work_on_stack(&barr.work);
2412 }
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002413}
2414
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002415static void wait_on_work(struct work_struct *work)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002416{
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002417 int cpu;
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002418
Oleg Nesterovf293ea92007-05-09 02:34:10 -07002419 might_sleep();
2420
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002421 lock_map_acquire(&work->lockdep_map);
2422 lock_map_release(&work->lockdep_map);
Johannes Berg4e6045f2007-10-18 23:39:55 -07002423
Tejun Heof3421792010-07-02 10:03:51 +02002424 for_each_gcwq_cpu(cpu)
Tejun Heo7a22ad72010-06-29 10:07:13 +02002425 wait_on_cpu_work(get_gcwq(cpu), work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002426}
2427
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002428static int __cancel_work_timer(struct work_struct *work,
2429 struct timer_list* timer)
2430{
2431 int ret;
2432
2433 do {
2434 ret = (timer && likely(del_timer(timer)));
2435 if (!ret)
2436 ret = try_to_grab_pending(work);
2437 wait_on_work(work);
2438 } while (unlikely(ret < 0));
2439
Tejun Heo7a22ad72010-06-29 10:07:13 +02002440 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002441 return ret;
2442}
2443
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002444/**
2445 * cancel_work_sync - block until a work_struct's callback has terminated
2446 * @work: the work which is to be flushed
2447 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002448 * Returns true if @work was pending.
2449 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002450 * cancel_work_sync() will cancel the work if it is queued. If the work's
2451 * callback appears to be running, cancel_work_sync() will block until it
2452 * has completed.
2453 *
2454 * It is possible to use this function if the work re-queues itself. It can
2455 * cancel the work even if it migrates to another workqueue, however in that
2456 * case it only guarantees that work->func() has completed on the last queued
2457 * workqueue.
2458 *
2459 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
2460 * pending, otherwise it goes into a busy-wait loop until the timer expires.
2461 *
2462 * The caller must ensure that workqueue_struct on which this work was last
2463 * queued can't be destroyed before this function returns.
2464 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002465int cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002466{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002467 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002468}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002469EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002470
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002471/**
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002472 * cancel_delayed_work_sync - reliably kill off a delayed work.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002473 * @dwork: the delayed work struct
2474 *
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002475 * Returns true if @dwork was pending.
2476 *
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002477 * It is possible to use this function if @dwork rearms itself via queue_work()
2478 * or queue_delayed_work(). See also the comment for cancel_work_sync().
2479 */
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002480int cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002481{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002482 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002483}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002484EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002486/**
2487 * schedule_work - put work task in global workqueue
2488 * @work: job to be done
2489 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002490 * Returns zero if @work was already on the kernel-global workqueue and
2491 * non-zero otherwise.
2492 *
2493 * This puts a job in the kernel-global workqueue if it was not already
2494 * queued and leaves it in the same position on the kernel-global
2495 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002496 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002497int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Tejun Heod320c032010-06-29 10:07:14 +02002499 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
Dave Jonesae90dd52006-06-30 01:40:45 -04002501EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Zhang Ruic1a220e2008-07-23 21:28:39 -07002503/*
2504 * schedule_work_on - put work task on a specific cpu
2505 * @cpu: cpu to put the work task on
2506 * @work: job to be done
2507 *
2508 * This puts a job on a specific cpu
2509 */
2510int schedule_work_on(int cpu, struct work_struct *work)
2511{
Tejun Heod320c032010-06-29 10:07:14 +02002512 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002513}
2514EXPORT_SYMBOL(schedule_work_on);
2515
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002516/**
2517 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002518 * @dwork: job to be done
2519 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002520 *
2521 * After waiting for a given time this puts a job in the kernel-global
2522 * workqueue.
2523 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002524int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002525 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Tejun Heod320c032010-06-29 10:07:14 +02002527 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
Dave Jonesae90dd52006-06-30 01:40:45 -04002529EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002531/**
Linus Torvalds8c53e462009-10-14 09:16:42 -07002532 * flush_delayed_work - block until a dwork_struct's callback has terminated
2533 * @dwork: the delayed work which is to be flushed
2534 *
2535 * Any timeout is cancelled, and any pending work is run immediately.
2536 */
2537void flush_delayed_work(struct delayed_work *dwork)
2538{
2539 if (del_timer_sync(&dwork->timer)) {
Tejun Heo7a22ad72010-06-29 10:07:13 +02002540 __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02002541 &dwork->work);
Linus Torvalds8c53e462009-10-14 09:16:42 -07002542 put_cpu();
2543 }
2544 flush_work(&dwork->work);
2545}
2546EXPORT_SYMBOL(flush_delayed_work);
2547
2548/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002549 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2550 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002551 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002552 * @delay: number of jiffies to wait
2553 *
2554 * After waiting for a given time this puts a job in the kernel-global
2555 * workqueue on the specified CPU.
2556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002558 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Tejun Heod320c032010-06-29 10:07:14 +02002560 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
Dave Jonesae90dd52006-06-30 01:40:45 -04002562EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Andrew Mortonb6136772006-06-25 05:47:49 -07002564/**
2565 * schedule_on_each_cpu - call a function on each online CPU from keventd
2566 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002567 *
2568 * Returns zero on success.
2569 * Returns -ve errno on failure.
2570 *
Andrew Mortonb6136772006-06-25 05:47:49 -07002571 * schedule_on_each_cpu() is very slow.
2572 */
David Howells65f27f32006-11-22 14:55:48 +00002573int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002574{
2575 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002576 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002577
Andrew Mortonb6136772006-06-25 05:47:49 -07002578 works = alloc_percpu(struct work_struct);
2579 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002580 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002581
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002582 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002583
Christoph Lameter15316ba2006-01-08 01:00:43 -08002584 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002585 struct work_struct *work = per_cpu_ptr(works, cpu);
2586
2587 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002588 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002589 }
Tejun Heo93981802009-11-17 14:06:20 -08002590
2591 for_each_online_cpu(cpu)
2592 flush_work(per_cpu_ptr(works, cpu));
2593
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002594 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002595 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002596 return 0;
2597}
2598
Alan Sterneef6a7d2010-02-12 17:39:21 +09002599/**
2600 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2601 *
2602 * Forces execution of the kernel-global workqueue and blocks until its
2603 * completion.
2604 *
2605 * Think twice before calling this function! It's very easy to get into
2606 * trouble if you don't take great care. Either of the following situations
2607 * will lead to deadlock:
2608 *
2609 * One of the work items currently on the workqueue needs to acquire
2610 * a lock held by your code or its caller.
2611 *
2612 * Your code is running in the context of a work routine.
2613 *
2614 * They will be detected by lockdep when they occur, but the first might not
2615 * occur very often. It depends on what work items are on the workqueue and
2616 * what locks they need, which you have no control over.
2617 *
2618 * In most situations flushing the entire workqueue is overkill; you merely
2619 * need to know that a particular work item isn't queued and isn't running.
2620 * In such cases you should use cancel_delayed_work_sync() or
2621 * cancel_work_sync() instead.
2622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623void flush_scheduled_work(void)
2624{
Tejun Heod320c032010-06-29 10:07:14 +02002625 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
Dave Jonesae90dd52006-06-30 01:40:45 -04002627EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002630 * execute_in_process_context - reliably execute the routine with user context
2631 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002632 * @ew: guaranteed storage for the execute work structure (must
2633 * be available when the work executes)
2634 *
2635 * Executes the function immediately if process context is available,
2636 * otherwise schedules the function for delayed execution.
2637 *
2638 * Returns: 0 - function was executed
2639 * 1 - function was scheduled for execution
2640 */
David Howells65f27f32006-11-22 14:55:48 +00002641int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002642{
2643 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002644 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002645 return 0;
2646 }
2647
David Howells65f27f32006-11-22 14:55:48 +00002648 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002649 schedule_work(&ew->work);
2650
2651 return 1;
2652}
2653EXPORT_SYMBOL_GPL(execute_in_process_context);
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655int keventd_up(void)
2656{
Tejun Heod320c032010-06-29 10:07:14 +02002657 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002660static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002662 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002663 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2664 * Make sure that the alignment isn't lower than that of
2665 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002666 */
Tejun Heo0f900042010-06-29 10:07:11 +02002667 const size_t size = sizeof(struct cpu_workqueue_struct);
2668 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2669 __alignof__(unsigned long long));
Tejun Heo931ac772010-07-20 11:07:48 +02002670#ifdef CONFIG_SMP
2671 bool percpu = !(wq->flags & WQ_UNBOUND);
2672#else
2673 bool percpu = false;
2674#endif
Oleg Nesterov3af244332007-05-09 02:34:09 -07002675
Tejun Heo931ac772010-07-20 11:07:48 +02002676 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002677 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002678 else {
Tejun Heof3421792010-07-02 10:03:51 +02002679 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002680
Tejun Heof3421792010-07-02 10:03:51 +02002681 /*
2682 * Allocate enough room to align cwq and put an extra
2683 * pointer at the end pointing back to the originally
2684 * allocated pointer which will be used for free.
2685 */
2686 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2687 if (ptr) {
2688 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2689 *(void **)(wq->cpu_wq.single + 1) = ptr;
2690 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002691 }
Tejun Heof3421792010-07-02 10:03:51 +02002692
Tejun Heo0f900042010-06-29 10:07:11 +02002693 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002694 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2695 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002696}
2697
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002698static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002699{
Tejun Heo931ac772010-07-20 11:07:48 +02002700#ifdef CONFIG_SMP
2701 bool percpu = !(wq->flags & WQ_UNBOUND);
2702#else
2703 bool percpu = false;
2704#endif
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002705
Tejun Heo931ac772010-07-20 11:07:48 +02002706 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002707 free_percpu(wq->cpu_wq.pcpu);
2708 else if (wq->cpu_wq.single) {
2709 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002710 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002711 }
2712}
2713
Tejun Heof3421792010-07-02 10:03:51 +02002714static int wq_clamp_max_active(int max_active, unsigned int flags,
2715 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002716{
Tejun Heof3421792010-07-02 10:03:51 +02002717 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2718
2719 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002720 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2721 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002722 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002723
Tejun Heof3421792010-07-02 10:03:51 +02002724 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002725}
2726
Tejun Heod320c032010-06-29 10:07:14 +02002727struct workqueue_struct *__alloc_workqueue_key(const char *name,
2728 unsigned int flags,
2729 int max_active,
2730 struct lock_class_key *key,
2731 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002732{
2733 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002734 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002735
Tejun Heof3421792010-07-02 10:03:51 +02002736 /*
2737 * Unbound workqueues aren't concurrency managed and should be
2738 * dispatched to workers immediately.
2739 */
2740 if (flags & WQ_UNBOUND)
2741 flags |= WQ_HIGHPRI;
2742
Tejun Heod320c032010-06-29 10:07:14 +02002743 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heof3421792010-07-02 10:03:51 +02002744 max_active = wq_clamp_max_active(max_active, flags, name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002745
2746 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2747 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02002748 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002749
Tejun Heo97e37d72010-06-29 10:07:10 +02002750 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002751 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002752 mutex_init(&wq->flush_mutex);
2753 atomic_set(&wq->nr_cwqs_to_flush, 0);
2754 INIT_LIST_HEAD(&wq->flusher_queue);
2755 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002756
2757 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01002758 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002759 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002760
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002761 if (alloc_cwqs(wq) < 0)
2762 goto err;
2763
Tejun Heof3421792010-07-02 10:03:51 +02002764 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002765 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002766 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02002767
Tejun Heo0f900042010-06-29 10:07:11 +02002768 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002769 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002770 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002771 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002772 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002773 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002774 }
2775
Tejun Heoe22bee72010-06-29 10:07:14 +02002776 if (flags & WQ_RESCUER) {
2777 struct worker *rescuer;
2778
Tejun Heof2e005a2010-07-20 15:59:09 +02002779 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02002780 goto err;
2781
2782 wq->rescuer = rescuer = alloc_worker();
2783 if (!rescuer)
2784 goto err;
2785
2786 rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
2787 if (IS_ERR(rescuer->task))
2788 goto err;
2789
Tejun Heoe22bee72010-06-29 10:07:14 +02002790 rescuer->task->flags |= PF_THREAD_BOUND;
2791 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002792 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002793
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002794 /*
2795 * workqueue_lock protects global freeze state and workqueues
2796 * list. Grab it, set max_active accordingly and add the new
2797 * workqueue to workqueues list.
2798 */
Tejun Heo15376632010-06-29 10:07:11 +02002799 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002800
2801 if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
Tejun Heof3421792010-07-02 10:03:51 +02002802 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002803 get_cwq(cpu, wq)->max_active = 0;
2804
Tejun Heo15376632010-06-29 10:07:11 +02002805 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002806
Tejun Heo15376632010-06-29 10:07:11 +02002807 spin_unlock(&workqueue_lock);
2808
Oleg Nesterov3af244332007-05-09 02:34:09 -07002809 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02002810err:
2811 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002812 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02002813 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002814 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02002815 kfree(wq);
2816 }
2817 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002818}
Tejun Heod320c032010-06-29 10:07:14 +02002819EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002820
2821/**
2822 * destroy_workqueue - safely terminate a workqueue
2823 * @wq: target workqueue
2824 *
2825 * Safely destroy a workqueue. All work currently pending will be done first.
2826 */
2827void destroy_workqueue(struct workqueue_struct *wq)
2828{
Tejun Heoc8e55f32010-06-29 10:07:12 +02002829 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002830
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002831 flush_workqueue(wq);
2832
2833 /*
2834 * wq list is used to freeze wq, remove from list after
2835 * flushing is complete in case freeze races us.
2836 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002837 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002838 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002839 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002840
Tejun Heoe22bee72010-06-29 10:07:14 +02002841 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02002842 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002843 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2844 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002845
Tejun Heo73f53c42010-06-29 10:07:11 +02002846 for (i = 0; i < WORK_NR_COLORS; i++)
2847 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002848 BUG_ON(cwq->nr_active);
2849 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02002850 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002851
Tejun Heoe22bee72010-06-29 10:07:14 +02002852 if (wq->flags & WQ_RESCUER) {
2853 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02002854 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02002855 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002856 }
2857
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002858 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002859 kfree(wq);
2860}
2861EXPORT_SYMBOL_GPL(destroy_workqueue);
2862
Tejun Heodcd989c2010-06-29 10:07:14 +02002863/**
2864 * workqueue_set_max_active - adjust max_active of a workqueue
2865 * @wq: target workqueue
2866 * @max_active: new max_active value.
2867 *
2868 * Set max_active of @wq to @max_active.
2869 *
2870 * CONTEXT:
2871 * Don't call from IRQ context.
2872 */
2873void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
2874{
2875 unsigned int cpu;
2876
Tejun Heof3421792010-07-02 10:03:51 +02002877 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02002878
2879 spin_lock(&workqueue_lock);
2880
2881 wq->saved_max_active = max_active;
2882
Tejun Heof3421792010-07-02 10:03:51 +02002883 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02002884 struct global_cwq *gcwq = get_gcwq(cpu);
2885
2886 spin_lock_irq(&gcwq->lock);
2887
2888 if (!(wq->flags & WQ_FREEZEABLE) ||
2889 !(gcwq->flags & GCWQ_FREEZING))
2890 get_cwq(gcwq->cpu, wq)->max_active = max_active;
2891
2892 spin_unlock_irq(&gcwq->lock);
2893 }
2894
2895 spin_unlock(&workqueue_lock);
2896}
2897EXPORT_SYMBOL_GPL(workqueue_set_max_active);
2898
2899/**
2900 * workqueue_congested - test whether a workqueue is congested
2901 * @cpu: CPU in question
2902 * @wq: target workqueue
2903 *
2904 * Test whether @wq's cpu workqueue for @cpu is congested. There is
2905 * no synchronization around this function and the test result is
2906 * unreliable and only useful as advisory hints or for debugging.
2907 *
2908 * RETURNS:
2909 * %true if congested, %false otherwise.
2910 */
2911bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
2912{
2913 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2914
2915 return !list_empty(&cwq->delayed_works);
2916}
2917EXPORT_SYMBOL_GPL(workqueue_congested);
2918
2919/**
2920 * work_cpu - return the last known associated cpu for @work
2921 * @work: the work of interest
2922 *
2923 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002924 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02002925 */
2926unsigned int work_cpu(struct work_struct *work)
2927{
2928 struct global_cwq *gcwq = get_work_gcwq(work);
2929
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002930 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02002931}
2932EXPORT_SYMBOL_GPL(work_cpu);
2933
2934/**
2935 * work_busy - test whether a work is currently pending or running
2936 * @work: the work to be tested
2937 *
2938 * Test whether @work is currently pending or running. There is no
2939 * synchronization around this function and the test result is
2940 * unreliable and only useful as advisory hints or for debugging.
2941 * Especially for reentrant wqs, the pending state might hide the
2942 * running state.
2943 *
2944 * RETURNS:
2945 * OR'd bitmask of WORK_BUSY_* bits.
2946 */
2947unsigned int work_busy(struct work_struct *work)
2948{
2949 struct global_cwq *gcwq = get_work_gcwq(work);
2950 unsigned long flags;
2951 unsigned int ret = 0;
2952
2953 if (!gcwq)
2954 return false;
2955
2956 spin_lock_irqsave(&gcwq->lock, flags);
2957
2958 if (work_pending(work))
2959 ret |= WORK_BUSY_PENDING;
2960 if (find_worker_executing_work(gcwq, work))
2961 ret |= WORK_BUSY_RUNNING;
2962
2963 spin_unlock_irqrestore(&gcwq->lock, flags);
2964
2965 return ret;
2966}
2967EXPORT_SYMBOL_GPL(work_busy);
2968
Tejun Heodb7bccf2010-06-29 10:07:12 +02002969/*
2970 * CPU hotplug.
2971 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002972 * There are two challenges in supporting CPU hotplug. Firstly, there
2973 * are a lot of assumptions on strong associations among work, cwq and
2974 * gcwq which make migrating pending and scheduled works very
2975 * difficult to implement without impacting hot paths. Secondly,
2976 * gcwqs serve mix of short, long and very long running works making
2977 * blocked draining impractical.
2978 *
2979 * This is solved by allowing a gcwq to be detached from CPU, running
2980 * it with unbound (rogue) workers and allowing it to be reattached
2981 * later if the cpu comes back online. A separate thread is created
2982 * to govern a gcwq in such state and is called the trustee of the
2983 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02002984 *
2985 * Trustee states and their descriptions.
2986 *
2987 * START Command state used on startup. On CPU_DOWN_PREPARE, a
2988 * new trustee is started with this state.
2989 *
2990 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02002991 * assuming the manager role and making all existing
2992 * workers rogue. DOWN_PREPARE waits for trustee to
2993 * enter this state. After reaching IN_CHARGE, trustee
2994 * tries to execute the pending worklist until it's empty
2995 * and the state is set to BUTCHER, or the state is set
2996 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02002997 *
2998 * BUTCHER Command state which is set by the cpu callback after
2999 * the cpu has went down. Once this state is set trustee
3000 * knows that there will be no new works on the worklist
3001 * and once the worklist is empty it can proceed to
3002 * killing idle workers.
3003 *
3004 * RELEASE Command state which is set by the cpu callback if the
3005 * cpu down has been canceled or it has come online
3006 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003007 * trying to drain or butcher and clears ROGUE, rebinds
3008 * all remaining workers back to the cpu and releases
3009 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003010 *
3011 * DONE Trustee will enter this state after BUTCHER or RELEASE
3012 * is complete.
3013 *
3014 * trustee CPU draining
3015 * took over down complete
3016 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3017 * | | ^
3018 * | CPU is back online v return workers |
3019 * ----------------> RELEASE --------------
3020 */
3021
3022/**
3023 * trustee_wait_event_timeout - timed event wait for trustee
3024 * @cond: condition to wait for
3025 * @timeout: timeout in jiffies
3026 *
3027 * wait_event_timeout() for trustee to use. Handles locking and
3028 * checks for RELEASE request.
3029 *
3030 * CONTEXT:
3031 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3032 * multiple times. To be used by trustee.
3033 *
3034 * RETURNS:
3035 * Positive indicating left time if @cond is satisfied, 0 if timed
3036 * out, -1 if canceled.
3037 */
3038#define trustee_wait_event_timeout(cond, timeout) ({ \
3039 long __ret = (timeout); \
3040 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3041 __ret) { \
3042 spin_unlock_irq(&gcwq->lock); \
3043 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3044 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3045 __ret); \
3046 spin_lock_irq(&gcwq->lock); \
3047 } \
3048 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3049})
3050
3051/**
3052 * trustee_wait_event - event wait for trustee
3053 * @cond: condition to wait for
3054 *
3055 * wait_event() for trustee to use. Automatically handles locking and
3056 * checks for CANCEL request.
3057 *
3058 * CONTEXT:
3059 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3060 * multiple times. To be used by trustee.
3061 *
3062 * RETURNS:
3063 * 0 if @cond is satisfied, -1 if canceled.
3064 */
3065#define trustee_wait_event(cond) ({ \
3066 long __ret1; \
3067 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3068 __ret1 < 0 ? -1 : 0; \
3069})
3070
3071static int __cpuinit trustee_thread(void *__gcwq)
3072{
3073 struct global_cwq *gcwq = __gcwq;
3074 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003075 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003076 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003077 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003078 int i;
3079
3080 BUG_ON(gcwq->cpu != smp_processor_id());
3081
3082 spin_lock_irq(&gcwq->lock);
3083 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003084 * Claim the manager position and make all workers rogue.
3085 * Trustee must be bound to the target cpu and can't be
3086 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003087 */
3088 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003089 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3090 BUG_ON(rc < 0);
3091
3092 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003093
3094 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003095 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003096
3097 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003098 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003099
3100 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003101 * Call schedule() so that we cross rq->lock and thus can
3102 * guarantee sched callbacks see the rogue flag. This is
3103 * necessary as scheduler callbacks may be invoked from other
3104 * cpus.
3105 */
3106 spin_unlock_irq(&gcwq->lock);
3107 schedule();
3108 spin_lock_irq(&gcwq->lock);
3109
3110 /*
Tejun Heocb444762010-07-02 10:03:50 +02003111 * Sched callbacks are disabled now. Zap nr_running. After
3112 * this, nr_running stays zero and need_more_worker() and
3113 * keep_working() are always true as long as the worklist is
3114 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003115 */
Tejun Heocb444762010-07-02 10:03:50 +02003116 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003117
3118 spin_unlock_irq(&gcwq->lock);
3119 del_timer_sync(&gcwq->idle_timer);
3120 spin_lock_irq(&gcwq->lock);
3121
3122 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003123 * We're now in charge. Notify and proceed to drain. We need
3124 * to keep the gcwq running during the whole CPU down
3125 * procedure as other cpu hotunplug callbacks may need to
3126 * flush currently running tasks.
3127 */
3128 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3129 wake_up_all(&gcwq->trustee_wait);
3130
3131 /*
3132 * The original cpu is in the process of dying and may go away
3133 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003134 * be migrated to other cpus. Try draining any left work. We
3135 * want to get it over with ASAP - spam rescuers, wake up as
3136 * many idlers as necessary and create new ones till the
3137 * worklist is empty. Note that if the gcwq is frozen, there
3138 * may be frozen works in freezeable cwqs. Don't declare
3139 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003140 */
3141 while (gcwq->nr_workers != gcwq->nr_idle ||
3142 gcwq->flags & GCWQ_FREEZING ||
3143 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003144 int nr_works = 0;
3145
3146 list_for_each_entry(work, &gcwq->worklist, entry) {
3147 send_mayday(work);
3148 nr_works++;
3149 }
3150
3151 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3152 if (!nr_works--)
3153 break;
3154 wake_up_process(worker->task);
3155 }
3156
3157 if (need_to_create_worker(gcwq)) {
3158 spin_unlock_irq(&gcwq->lock);
3159 worker = create_worker(gcwq, false);
3160 spin_lock_irq(&gcwq->lock);
3161 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003162 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003163 start_worker(worker);
3164 }
3165 }
3166
Tejun Heodb7bccf2010-06-29 10:07:12 +02003167 /* give a breather */
3168 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3169 break;
3170 }
3171
Tejun Heoe22bee72010-06-29 10:07:14 +02003172 /*
3173 * Either all works have been scheduled and cpu is down, or
3174 * cpu down has already been canceled. Wait for and butcher
3175 * all workers till we're canceled.
3176 */
3177 do {
3178 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3179 while (!list_empty(&gcwq->idle_list))
3180 destroy_worker(list_first_entry(&gcwq->idle_list,
3181 struct worker, entry));
3182 } while (gcwq->nr_workers && rc >= 0);
3183
3184 /*
3185 * At this point, either draining has completed and no worker
3186 * is left, or cpu down has been canceled or the cpu is being
3187 * brought back up. There shouldn't be any idle one left.
3188 * Tell the remaining busy ones to rebind once it finishes the
3189 * currently scheduled works by scheduling the rebind_work.
3190 */
3191 WARN_ON(!list_empty(&gcwq->idle_list));
3192
3193 for_each_busy_worker(worker, i, pos, gcwq) {
3194 struct work_struct *rebind_work = &worker->rebind_work;
3195
3196 /*
3197 * Rebind_work may race with future cpu hotplug
3198 * operations. Use a separate flag to mark that
3199 * rebinding is scheduled.
3200 */
Tejun Heocb444762010-07-02 10:03:50 +02003201 worker->flags |= WORKER_REBIND;
3202 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003203
3204 /* queue rebind_work, wq doesn't matter, use the default one */
3205 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3206 work_data_bits(rebind_work)))
3207 continue;
3208
3209 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003210 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003211 worker->scheduled.next,
3212 work_color_to_flags(WORK_NO_COLOR));
3213 }
3214
3215 /* relinquish manager role */
3216 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3217
Tejun Heodb7bccf2010-06-29 10:07:12 +02003218 /* notify completion */
3219 gcwq->trustee = NULL;
3220 gcwq->trustee_state = TRUSTEE_DONE;
3221 wake_up_all(&gcwq->trustee_wait);
3222 spin_unlock_irq(&gcwq->lock);
3223 return 0;
3224}
3225
3226/**
3227 * wait_trustee_state - wait for trustee to enter the specified state
3228 * @gcwq: gcwq the trustee of interest belongs to
3229 * @state: target state to wait for
3230 *
3231 * Wait for the trustee to reach @state. DONE is already matched.
3232 *
3233 * CONTEXT:
3234 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3235 * multiple times. To be used by cpu_callback.
3236 */
3237static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003238__releases(&gcwq->lock)
3239__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003240{
3241 if (!(gcwq->trustee_state == state ||
3242 gcwq->trustee_state == TRUSTEE_DONE)) {
3243 spin_unlock_irq(&gcwq->lock);
3244 __wait_event(gcwq->trustee_wait,
3245 gcwq->trustee_state == state ||
3246 gcwq->trustee_state == TRUSTEE_DONE);
3247 spin_lock_irq(&gcwq->lock);
3248 }
3249}
3250
Oleg Nesterov3af244332007-05-09 02:34:09 -07003251static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3252 unsigned long action,
3253 void *hcpu)
3254{
3255 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003256 struct global_cwq *gcwq = get_gcwq(cpu);
3257 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003258 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003259 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003261 action &= ~CPU_TASKS_FROZEN;
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003264 case CPU_DOWN_PREPARE:
3265 new_trustee = kthread_create(trustee_thread, gcwq,
3266 "workqueue_trustee/%d\n", cpu);
3267 if (IS_ERR(new_trustee))
3268 return notifier_from_errno(PTR_ERR(new_trustee));
3269 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003270 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003272 BUG_ON(gcwq->first_idle);
3273 new_worker = create_worker(gcwq, false);
3274 if (!new_worker) {
3275 if (new_trustee)
3276 kthread_stop(new_trustee);
3277 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280
Tejun Heodb7bccf2010-06-29 10:07:12 +02003281 /* some are called w/ irq disabled, don't disturb irq status */
3282 spin_lock_irqsave(&gcwq->lock, flags);
3283
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003284 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003285 case CPU_DOWN_PREPARE:
3286 /* initialize trustee and tell it to acquire the gcwq */
3287 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3288 gcwq->trustee = new_trustee;
3289 gcwq->trustee_state = TRUSTEE_START;
3290 wake_up_process(gcwq->trustee);
3291 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003292 /* fall through */
3293 case CPU_UP_PREPARE:
3294 BUG_ON(gcwq->first_idle);
3295 gcwq->first_idle = new_worker;
3296 break;
3297
3298 case CPU_DYING:
3299 /*
3300 * Before this, the trustee and all workers except for
3301 * the ones which are still executing works from
3302 * before the last CPU down must be on the cpu. After
3303 * this, they'll all be diasporas.
3304 */
3305 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003306 break;
3307
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003308 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003309 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003310 /* fall through */
3311 case CPU_UP_CANCELED:
3312 destroy_worker(gcwq->first_idle);
3313 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003314 break;
3315
3316 case CPU_DOWN_FAILED:
3317 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003318 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003319 if (gcwq->trustee_state != TRUSTEE_DONE) {
3320 gcwq->trustee_state = TRUSTEE_RELEASE;
3321 wake_up_process(gcwq->trustee);
3322 wait_trustee_state(gcwq, TRUSTEE_DONE);
3323 }
3324
Tejun Heoe22bee72010-06-29 10:07:14 +02003325 /*
3326 * Trustee is done and there might be no worker left.
3327 * Put the first_idle in and request a real manager to
3328 * take a look.
3329 */
3330 spin_unlock_irq(&gcwq->lock);
3331 kthread_bind(gcwq->first_idle->task, cpu);
3332 spin_lock_irq(&gcwq->lock);
3333 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3334 start_worker(gcwq->first_idle);
3335 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003336 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003337 }
3338
Tejun Heodb7bccf2010-06-29 10:07:12 +02003339 spin_unlock_irqrestore(&gcwq->lock, flags);
3340
Tejun Heo15376632010-06-29 10:07:11 +02003341 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Rusty Russell2d3854a2008-11-05 13:39:10 +11003344#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003345
Rusty Russell2d3854a2008-11-05 13:39:10 +11003346struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06003347 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003348 long (*fn)(void *);
3349 void *arg;
3350 long ret;
3351};
3352
Andrew Morton6b44003e2009-04-09 09:50:37 -06003353static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003354{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003355 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003356 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06003357 complete(&wfc->completion);
3358 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003359}
3360
3361/**
3362 * work_on_cpu - run a function in user context on a particular cpu
3363 * @cpu: the cpu to run on
3364 * @fn: the function to run
3365 * @arg: the function arg
3366 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003367 * This will return the value @fn returns.
3368 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003369 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003370 */
3371long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3372{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003373 struct task_struct *sub_thread;
3374 struct work_for_cpu wfc = {
3375 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3376 .fn = fn,
3377 .arg = arg,
3378 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003379
Andrew Morton6b44003e2009-04-09 09:50:37 -06003380 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3381 if (IS_ERR(sub_thread))
3382 return PTR_ERR(sub_thread);
3383 kthread_bind(sub_thread, cpu);
3384 wake_up_process(sub_thread);
3385 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003386 return wfc.ret;
3387}
3388EXPORT_SYMBOL_GPL(work_on_cpu);
3389#endif /* CONFIG_SMP */
3390
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003391#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303392
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003393/**
3394 * freeze_workqueues_begin - begin freezing workqueues
3395 *
3396 * Start freezing workqueues. After this function returns, all
3397 * freezeable workqueues will queue new works to their frozen_works
Tejun Heo7e116292010-06-29 10:07:13 +02003398 * list instead of gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003399 *
3400 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003401 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003402 */
3403void freeze_workqueues_begin(void)
3404{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003405 unsigned int cpu;
3406
3407 spin_lock(&workqueue_lock);
3408
3409 BUG_ON(workqueue_freezing);
3410 workqueue_freezing = true;
3411
Tejun Heof3421792010-07-02 10:03:51 +02003412 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003413 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003414 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003415
3416 spin_lock_irq(&gcwq->lock);
3417
Tejun Heodb7bccf2010-06-29 10:07:12 +02003418 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3419 gcwq->flags |= GCWQ_FREEZING;
3420
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003421 list_for_each_entry(wq, &workqueues, list) {
3422 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3423
Tejun Heof3421792010-07-02 10:03:51 +02003424 if (cwq && wq->flags & WQ_FREEZEABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003425 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003426 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003427
3428 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003429 }
3430
3431 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003433
3434/**
3435 * freeze_workqueues_busy - are freezeable workqueues still busy?
3436 *
3437 * Check whether freezing is complete. This function must be called
3438 * between freeze_workqueues_begin() and thaw_workqueues().
3439 *
3440 * CONTEXT:
3441 * Grabs and releases workqueue_lock.
3442 *
3443 * RETURNS:
3444 * %true if some freezeable workqueues are still busy. %false if
3445 * freezing is complete.
3446 */
3447bool freeze_workqueues_busy(void)
3448{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003449 unsigned int cpu;
3450 bool busy = false;
3451
3452 spin_lock(&workqueue_lock);
3453
3454 BUG_ON(!workqueue_freezing);
3455
Tejun Heof3421792010-07-02 10:03:51 +02003456 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003457 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003458 /*
3459 * nr_active is monotonically decreasing. It's safe
3460 * to peek without lock.
3461 */
3462 list_for_each_entry(wq, &workqueues, list) {
3463 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3464
Tejun Heof3421792010-07-02 10:03:51 +02003465 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003466 continue;
3467
3468 BUG_ON(cwq->nr_active < 0);
3469 if (cwq->nr_active) {
3470 busy = true;
3471 goto out_unlock;
3472 }
3473 }
3474 }
3475out_unlock:
3476 spin_unlock(&workqueue_lock);
3477 return busy;
3478}
3479
3480/**
3481 * thaw_workqueues - thaw workqueues
3482 *
3483 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003484 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003485 *
3486 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003487 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003488 */
3489void thaw_workqueues(void)
3490{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003491 unsigned int cpu;
3492
3493 spin_lock(&workqueue_lock);
3494
3495 if (!workqueue_freezing)
3496 goto out_unlock;
3497
Tejun Heof3421792010-07-02 10:03:51 +02003498 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003499 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003500 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003501
3502 spin_lock_irq(&gcwq->lock);
3503
Tejun Heodb7bccf2010-06-29 10:07:12 +02003504 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3505 gcwq->flags &= ~GCWQ_FREEZING;
3506
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003507 list_for_each_entry(wq, &workqueues, list) {
3508 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3509
Tejun Heof3421792010-07-02 10:03:51 +02003510 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003511 continue;
3512
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003513 /* restore max_active and repopulate worklist */
3514 cwq->max_active = wq->saved_max_active;
3515
3516 while (!list_empty(&cwq->delayed_works) &&
3517 cwq->nr_active < cwq->max_active)
3518 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003519 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003520
Tejun Heoe22bee72010-06-29 10:07:14 +02003521 wake_up_worker(gcwq);
3522
Tejun Heo8b03ae32010-06-29 10:07:12 +02003523 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003524 }
3525
3526 workqueue_freezing = false;
3527out_unlock:
3528 spin_unlock(&workqueue_lock);
3529}
3530#endif /* CONFIG_FREEZER */
3531
Suresh Siddha6ee05782010-07-30 14:57:37 -07003532static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Tejun Heoc34056a2010-06-29 10:07:11 +02003534 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003535 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003536
Tejun Heof6500942010-08-09 11:50:34 +02003537 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003538
3539 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003540 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003541 struct global_cwq *gcwq = get_gcwq(cpu);
3542
3543 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003544 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003545 gcwq->cpu = cpu;
Tejun Heof3421792010-07-02 10:03:51 +02003546 if (cpu == WORK_CPU_UNBOUND)
3547 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003548
Tejun Heoc8e55f32010-06-29 10:07:12 +02003549 INIT_LIST_HEAD(&gcwq->idle_list);
3550 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3551 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3552
Tejun Heoe22bee72010-06-29 10:07:14 +02003553 init_timer_deferrable(&gcwq->idle_timer);
3554 gcwq->idle_timer.function = idle_worker_timeout;
3555 gcwq->idle_timer.data = (unsigned long)gcwq;
3556
3557 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3558 (unsigned long)gcwq);
3559
Tejun Heo8b03ae32010-06-29 10:07:12 +02003560 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003561
3562 gcwq->trustee_state = TRUSTEE_DONE;
3563 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003564 }
3565
Tejun Heoe22bee72010-06-29 10:07:14 +02003566 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003567 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003568 struct global_cwq *gcwq = get_gcwq(cpu);
3569 struct worker *worker;
3570
3571 worker = create_worker(gcwq, true);
3572 BUG_ON(!worker);
3573 spin_lock_irq(&gcwq->lock);
3574 start_worker(worker);
3575 spin_unlock_irq(&gcwq->lock);
3576 }
3577
Tejun Heod320c032010-06-29 10:07:14 +02003578 system_wq = alloc_workqueue("events", 0, 0);
3579 system_long_wq = alloc_workqueue("events_long", 0, 0);
3580 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003581 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3582 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heod320c032010-06-29 10:07:14 +02003583 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003586early_initcall(init_workqueues);