blob: 2c6871cbcbeedbbd4afa06cba941e7aeb066e905 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter469340232006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020048 /* global_cwq flags */
Tejun Heoe22bee72010-06-29 10:07:14 +020049 GCWQ_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
50 GCWQ_MANAGING_WORKERS = 1 << 1, /* managing workers */
51 GCWQ_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020052 GCWQ_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heo649027d2010-06-29 10:07:14 +020053 GCWQ_HIGHPRI_PENDING = 1 << 4, /* highpri works on queue */
Tejun Heodb7bccf2010-06-29 10:07:12 +020054
Tejun Heoc8e55f32010-06-29 10:07:12 +020055 /* worker flags */
56 WORKER_STARTED = 1 << 0, /* started */
57 WORKER_DIE = 1 << 1, /* die die die */
58 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020059 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020060 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020062 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020063 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020064
Tejun Heofb0e7be2010-06-29 10:07:15 +020065 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020066 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020067
68 /* gcwq->trustee_state */
69 TRUSTEE_START = 0, /* start */
70 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
71 TRUSTEE_BUTCHER = 2, /* butcher workers */
72 TRUSTEE_RELEASE = 3, /* release workers */
73 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020074
75 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
76 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
77 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020078
Tejun Heoe22bee72010-06-29 10:07:14 +020079 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
80 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
81
82 MAYDAY_INITIAL_TIMEOUT = HZ / 100, /* call for help after 10ms */
83 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
84 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020085 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020086
87 /*
88 * Rescue workers are used only on emergencies and shared by
89 * all cpus. Give -20.
90 */
91 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020092};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020095 * Structure fields follow one of the following exclusion rules.
96 *
Tejun Heoe41e7042010-08-24 14:22:47 +020097 * I: Modifiable by initialization/destruction paths and read-only for
98 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +020099 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200100 * P: Preemption protected. Disabling preemption is enough and should
101 * only be modified and accessed from the local cpu.
102 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200103 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200104 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200105 * X: During normal operation, modification requires gcwq->lock and
106 * should be done only from local cpu. Either disabling preemption
107 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200108 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200109 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200110 * F: wq->flush_mutex protected.
111 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 * W: workqueue_lock protected.
113 */
114
Tejun Heo8b03ae32010-06-29 10:07:12 +0200115struct global_cwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200116
Tejun Heoe22bee72010-06-29 10:07:14 +0200117/*
118 * The poor guys doing the actual heavy lifting. All on-duty workers
119 * are either serving the manager role, on idle list or on busy hash.
120 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200121struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200122 /* on idle list while idle, on busy hash table while busy */
123 union {
124 struct list_head entry; /* L: while idle */
125 struct hlist_node hentry; /* L: while busy */
126 };
127
Tejun Heoc34056a2010-06-29 10:07:11 +0200128 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200129 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200130 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200131 struct task_struct *task; /* I: worker task */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200132 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heoe22bee72010-06-29 10:07:14 +0200133 /* 64 bytes boundary on 64bit, 32 on 32bit */
134 unsigned long last_active; /* L: last active timestamp */
135 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200136 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200137 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138};
139
Tejun Heo4690c4a2010-06-29 10:07:10 +0200140/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200141 * Global per-cpu workqueue. There's one and only one for each cpu
142 * and all works are queued and processed here regardless of their
143 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200144 */
145struct global_cwq {
146 spinlock_t lock; /* the gcwq lock */
Tejun Heo7e116292010-06-29 10:07:13 +0200147 struct list_head worklist; /* L: list of pending works */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200148 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200149 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200150
151 int nr_workers; /* L: total number of workers */
152 int nr_idle; /* L: currently idle ones */
153
154 /* workers are chained either in the idle_list or busy_hash */
Tejun Heoe22bee72010-06-29 10:07:14 +0200155 struct list_head idle_list; /* X: list of idle workers */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200156 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
157 /* L: hash of busy workers */
158
Tejun Heoe22bee72010-06-29 10:07:14 +0200159 struct timer_list idle_timer; /* L: worker idle timeout */
160 struct timer_list mayday_timer; /* L: SOS timer for dworkers */
161
Tejun Heo8b03ae32010-06-29 10:07:12 +0200162 struct ida worker_ida; /* L: for worker IDs */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200163
164 struct task_struct *trustee; /* L: for gcwq shutdown */
165 unsigned int trustee_state; /* L: trustee state */
166 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heoe22bee72010-06-29 10:07:14 +0200167 struct worker *first_idle; /* L: first idle worker */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200168} ____cacheline_aligned_in_smp;
169
170/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200171 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200172 * work_struct->data are used for flags and thus cwqs need to be
173 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175struct cpu_workqueue_struct {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200176 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200177 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200178 int work_color; /* L: current color */
179 int flush_color; /* L: flushing color */
180 int nr_in_flight[WORK_NR_COLORS];
181 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200182 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200183 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200184 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200185};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200188 * Structure used to wait for workqueue flush.
189 */
190struct wq_flusher {
191 struct list_head list; /* F: list of flushers */
192 int flush_color; /* F: flush color waiting for */
193 struct completion done; /* flush completion */
194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Tejun Heo73f53c42010-06-29 10:07:11 +0200196/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200197 * All cpumasks are assumed to be always set on UP and thus can't be
198 * used to determine whether there's something to be done.
199 */
200#ifdef CONFIG_SMP
201typedef cpumask_var_t mayday_mask_t;
202#define mayday_test_and_set_cpu(cpu, mask) \
203 cpumask_test_and_set_cpu((cpu), (mask))
204#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
205#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200206#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200207#define free_mayday_mask(mask) free_cpumask_var((mask))
208#else
209typedef unsigned long mayday_mask_t;
210#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
211#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
212#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
213#define alloc_mayday_mask(maskp, gfp) true
214#define free_mayday_mask(mask) do { } while (0)
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/*
218 * The externally visible workqueue abstraction is an array of
219 * per-CPU workqueues:
220 */
221struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +0200222 unsigned int flags; /* I: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200223 union {
224 struct cpu_workqueue_struct __percpu *pcpu;
225 struct cpu_workqueue_struct *single;
226 unsigned long v;
227 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200228 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200229
230 struct mutex flush_mutex; /* protects wq flushing */
231 int work_color; /* F: current work color */
232 int flush_color; /* F: current flush color */
233 atomic_t nr_cwqs_to_flush; /* flush in progress */
234 struct wq_flusher *first_flusher; /* F: first flusher */
235 struct list_head flusher_queue; /* F: flush waiters */
236 struct list_head flusher_overflow; /* F: flush overflow list */
237
Tejun Heof2e005a2010-07-20 15:59:09 +0200238 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200239 struct worker *rescuer; /* I: rescue worker */
240
Tejun Heodcd989c2010-06-29 10:07:14 +0200241 int saved_max_active; /* W: saved cwq max_active */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200242 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700243#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700245#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Tejun Heod320c032010-06-29 10:07:14 +0200248struct workqueue_struct *system_wq __read_mostly;
249struct workqueue_struct *system_long_wq __read_mostly;
250struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200251struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200252EXPORT_SYMBOL_GPL(system_wq);
253EXPORT_SYMBOL_GPL(system_long_wq);
254EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200255EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200256
Tejun Heo97bd2342010-10-05 10:41:14 +0200257#define CREATE_TRACE_POINTS
258#include <trace/events/workqueue.h>
259
Tejun Heodb7bccf2010-06-29 10:07:12 +0200260#define for_each_busy_worker(worker, i, pos, gcwq) \
261 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
262 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
263
Tejun Heof3421792010-07-02 10:03:51 +0200264static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
265 unsigned int sw)
266{
267 if (cpu < nr_cpu_ids) {
268 if (sw & 1) {
269 cpu = cpumask_next(cpu, mask);
270 if (cpu < nr_cpu_ids)
271 return cpu;
272 }
273 if (sw & 2)
274 return WORK_CPU_UNBOUND;
275 }
276 return WORK_CPU_NONE;
277}
278
279static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
280 struct workqueue_struct *wq)
281{
282 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
283}
284
Tejun Heo09884952010-08-01 11:50:12 +0200285/*
286 * CPU iterators
287 *
288 * An extra gcwq is defined for an invalid cpu number
289 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
290 * specific CPU. The following iterators are similar to
291 * for_each_*_cpu() iterators but also considers the unbound gcwq.
292 *
293 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
294 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
295 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
296 * WORK_CPU_UNBOUND for unbound workqueues
297 */
Tejun Heof3421792010-07-02 10:03:51 +0200298#define for_each_gcwq_cpu(cpu) \
299 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
300 (cpu) < WORK_CPU_NONE; \
301 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
302
303#define for_each_online_gcwq_cpu(cpu) \
304 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
305 (cpu) < WORK_CPU_NONE; \
306 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
307
308#define for_each_cwq_cpu(cpu, wq) \
309 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
310 (cpu) < WORK_CPU_NONE; \
311 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
312
Paul E. McKenneya25909a2010-05-13 12:32:28 -0700313#ifdef CONFIG_LOCKDEP
314/**
315 * in_workqueue_context() - in context of specified workqueue?
316 * @wq: the workqueue of interest
317 *
318 * Checks lockdep state to see if the current task is executing from
319 * within a workqueue item. This function exists only if lockdep is
320 * enabled.
321 */
322int in_workqueue_context(struct workqueue_struct *wq)
323{
324 return lock_is_held(&wq->lockdep_map);
325}
326#endif
327
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900328#ifdef CONFIG_DEBUG_OBJECTS_WORK
329
330static struct debug_obj_descr work_debug_descr;
331
332/*
333 * fixup_init is called when:
334 * - an active object is initialized
335 */
336static int work_fixup_init(void *addr, enum debug_obj_state state)
337{
338 struct work_struct *work = addr;
339
340 switch (state) {
341 case ODEBUG_STATE_ACTIVE:
342 cancel_work_sync(work);
343 debug_object_init(work, &work_debug_descr);
344 return 1;
345 default:
346 return 0;
347 }
348}
349
350/*
351 * fixup_activate is called when:
352 * - an active object is activated
353 * - an unknown object is activated (might be a statically initialized object)
354 */
355static int work_fixup_activate(void *addr, enum debug_obj_state state)
356{
357 struct work_struct *work = addr;
358
359 switch (state) {
360
361 case ODEBUG_STATE_NOTAVAILABLE:
362 /*
363 * This is not really a fixup. The work struct was
364 * statically initialized. We just make sure that it
365 * is tracked in the object tracker.
366 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200367 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900368 debug_object_init(work, &work_debug_descr);
369 debug_object_activate(work, &work_debug_descr);
370 return 0;
371 }
372 WARN_ON_ONCE(1);
373 return 0;
374
375 case ODEBUG_STATE_ACTIVE:
376 WARN_ON(1);
377
378 default:
379 return 0;
380 }
381}
382
383/*
384 * fixup_free is called when:
385 * - an active object is freed
386 */
387static int work_fixup_free(void *addr, enum debug_obj_state state)
388{
389 struct work_struct *work = addr;
390
391 switch (state) {
392 case ODEBUG_STATE_ACTIVE:
393 cancel_work_sync(work);
394 debug_object_free(work, &work_debug_descr);
395 return 1;
396 default:
397 return 0;
398 }
399}
400
401static struct debug_obj_descr work_debug_descr = {
402 .name = "work_struct",
403 .fixup_init = work_fixup_init,
404 .fixup_activate = work_fixup_activate,
405 .fixup_free = work_fixup_free,
406};
407
408static inline void debug_work_activate(struct work_struct *work)
409{
410 debug_object_activate(work, &work_debug_descr);
411}
412
413static inline void debug_work_deactivate(struct work_struct *work)
414{
415 debug_object_deactivate(work, &work_debug_descr);
416}
417
418void __init_work(struct work_struct *work, int onstack)
419{
420 if (onstack)
421 debug_object_init_on_stack(work, &work_debug_descr);
422 else
423 debug_object_init(work, &work_debug_descr);
424}
425EXPORT_SYMBOL_GPL(__init_work);
426
427void destroy_work_on_stack(struct work_struct *work)
428{
429 debug_object_free(work, &work_debug_descr);
430}
431EXPORT_SYMBOL_GPL(destroy_work_on_stack);
432
433#else
434static inline void debug_work_activate(struct work_struct *work) { }
435static inline void debug_work_deactivate(struct work_struct *work) { }
436#endif
437
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100438/* Serializes the accesses to the list of workqueues. */
439static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200441static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Oleg Nesterov14441962007-05-23 13:57:57 -0700443/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200444 * The almighty global cpu workqueues. nr_running is the only field
445 * which is expected to be used frequently by other cpus via
446 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700447 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200448static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200449static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800450
Tejun Heof3421792010-07-02 10:03:51 +0200451/*
452 * Global cpu workqueue and nr_running counter for unbound gcwq. The
453 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
454 * workers have WORKER_UNBOUND set.
455 */
456static struct global_cwq unbound_global_cwq;
457static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
458
Tejun Heoc34056a2010-06-29 10:07:11 +0200459static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Tejun Heo8b03ae32010-06-29 10:07:12 +0200461static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Tejun Heof3421792010-07-02 10:03:51 +0200463 if (cpu != WORK_CPU_UNBOUND)
464 return &per_cpu(global_cwq, cpu);
465 else
466 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Tejun Heoe22bee72010-06-29 10:07:14 +0200469static atomic_t *get_gcwq_nr_running(unsigned int cpu)
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700470{
Tejun Heof3421792010-07-02 10:03:51 +0200471 if (cpu != WORK_CPU_UNBOUND)
472 return &per_cpu(gcwq_nr_running, cpu);
473 else
474 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700475}
476
Tejun Heo4690c4a2010-06-29 10:07:10 +0200477static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
478 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700479{
Tejun Heof3421792010-07-02 10:03:51 +0200480 if (!(wq->flags & WQ_UNBOUND)) {
481 if (likely(cpu < nr_cpu_ids)) {
482#ifdef CONFIG_SMP
483 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200484#else
Tejun Heof3421792010-07-02 10:03:51 +0200485 return wq->cpu_wq.single;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200486#endif
Tejun Heof3421792010-07-02 10:03:51 +0200487 }
488 } else if (likely(cpu == WORK_CPU_UNBOUND))
489 return wq->cpu_wq.single;
490 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700491}
492
Tejun Heo73f53c42010-06-29 10:07:11 +0200493static unsigned int work_color_to_flags(int color)
494{
495 return color << WORK_STRUCT_COLOR_SHIFT;
496}
497
498static int get_work_color(struct work_struct *work)
499{
500 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
501 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
502}
503
504static int work_next_color(int color)
505{
506 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
David Howells4594bf12006-12-07 11:33:26 +0000509/*
Tejun Heoe1201532010-07-22 14:14:25 +0200510 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
511 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
512 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200513 *
514 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
515 * cwq, cpu or clear work->data. These functions should only be
516 * called while the work is owned - ie. while the PENDING bit is set.
517 *
518 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
519 * corresponding to a work. gcwq is available once the work has been
520 * queued anywhere after initialization. cwq is available only from
521 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000522 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200523static inline void set_work_data(struct work_struct *work, unsigned long data,
524 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000525{
David Howells4594bf12006-12-07 11:33:26 +0000526 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200527 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000528}
David Howells365970a2006-11-22 14:54:49 +0000529
Tejun Heo7a22ad72010-06-29 10:07:13 +0200530static void set_work_cwq(struct work_struct *work,
531 struct cpu_workqueue_struct *cwq,
532 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200533{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200534 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200535 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200536}
537
Tejun Heo7a22ad72010-06-29 10:07:13 +0200538static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000539{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
541}
542
543static void clear_work_data(struct work_struct *work)
544{
545 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
546}
547
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
549{
Tejun Heoe1201532010-07-22 14:14:25 +0200550 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551
Tejun Heoe1201532010-07-22 14:14:25 +0200552 if (data & WORK_STRUCT_CWQ)
553 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
554 else
555 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200556}
557
558static struct global_cwq *get_work_gcwq(struct work_struct *work)
559{
Tejun Heoe1201532010-07-22 14:14:25 +0200560 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 unsigned int cpu;
562
Tejun Heoe1201532010-07-22 14:14:25 +0200563 if (data & WORK_STRUCT_CWQ)
564 return ((struct cpu_workqueue_struct *)
565 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566
567 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200568 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569 return NULL;
570
Tejun Heof3421792010-07-02 10:03:51 +0200571 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000573}
574
575/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200576 * Policy functions. These define the policies on how the global
577 * worker pool is managed. Unless noted otherwise, these functions
578 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000579 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200580
Tejun Heo649027d2010-06-29 10:07:14 +0200581static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000582{
Tejun Heo649027d2010-06-29 10:07:14 +0200583 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
584 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000585}
586
Tejun Heoe22bee72010-06-29 10:07:14 +0200587/*
588 * Need to wake up a worker? Called from anything but currently
589 * running workers.
590 */
591static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000592{
Tejun Heo649027d2010-06-29 10:07:14 +0200593 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000594}
595
Tejun Heoe22bee72010-06-29 10:07:14 +0200596/* Can I start working? Called from busy but !running workers. */
597static bool may_start_working(struct global_cwq *gcwq)
598{
599 return gcwq->nr_idle;
600}
601
602/* Do I need to keep working? Called from currently running workers. */
603static bool keep_working(struct global_cwq *gcwq)
604{
605 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
606
Tejun Heo30310042010-10-11 11:51:57 +0200607 return !list_empty(&gcwq->worklist) &&
608 (atomic_read(nr_running) <= 1 ||
609 gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200610}
611
612/* Do we need a new worker? Called from manager. */
613static bool need_to_create_worker(struct global_cwq *gcwq)
614{
615 return need_more_worker(gcwq) && !may_start_working(gcwq);
616}
617
618/* Do I need to be the manager? */
619static bool need_to_manage_workers(struct global_cwq *gcwq)
620{
621 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
622}
623
624/* Do we have too many workers and should some go away? */
625static bool too_many_workers(struct global_cwq *gcwq)
626{
627 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
628 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
629 int nr_busy = gcwq->nr_workers - nr_idle;
630
631 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
632}
633
634/*
635 * Wake up functions.
636 */
637
Tejun Heo7e116292010-06-29 10:07:13 +0200638/* Return the first worker. Safe with preemption disabled */
639static struct worker *first_worker(struct global_cwq *gcwq)
640{
641 if (unlikely(list_empty(&gcwq->idle_list)))
642 return NULL;
643
644 return list_first_entry(&gcwq->idle_list, struct worker, entry);
645}
646
647/**
648 * wake_up_worker - wake up an idle worker
649 * @gcwq: gcwq to wake worker for
650 *
651 * Wake up the first idle worker of @gcwq.
652 *
653 * CONTEXT:
654 * spin_lock_irq(gcwq->lock).
655 */
656static void wake_up_worker(struct global_cwq *gcwq)
657{
658 struct worker *worker = first_worker(gcwq);
659
660 if (likely(worker))
661 wake_up_process(worker->task);
662}
663
Tejun Heo4690c4a2010-06-29 10:07:10 +0200664/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200665 * wq_worker_waking_up - a worker is waking up
666 * @task: task waking up
667 * @cpu: CPU @task is waking up to
668 *
669 * This function is called during try_to_wake_up() when a worker is
670 * being awoken.
671 *
672 * CONTEXT:
673 * spin_lock_irq(rq->lock)
674 */
675void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
676{
677 struct worker *worker = kthread_data(task);
678
679 if (likely(!(worker->flags & WORKER_NOT_RUNNING)))
680 atomic_inc(get_gcwq_nr_running(cpu));
681}
682
683/**
684 * wq_worker_sleeping - a worker is going to sleep
685 * @task: task going to sleep
686 * @cpu: CPU in question, must be the current CPU number
687 *
688 * This function is called during schedule() when a busy worker is
689 * going to sleep. Worker on the same cpu can be woken up by
690 * returning pointer to its task.
691 *
692 * CONTEXT:
693 * spin_lock_irq(rq->lock)
694 *
695 * RETURNS:
696 * Worker task on @cpu to wake up, %NULL if none.
697 */
698struct task_struct *wq_worker_sleeping(struct task_struct *task,
699 unsigned int cpu)
700{
701 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
702 struct global_cwq *gcwq = get_gcwq(cpu);
703 atomic_t *nr_running = get_gcwq_nr_running(cpu);
704
705 if (unlikely(worker->flags & WORKER_NOT_RUNNING))
706 return NULL;
707
708 /* this can only happen on the local cpu */
709 BUG_ON(cpu != raw_smp_processor_id());
710
711 /*
712 * The counterpart of the following dec_and_test, implied mb,
713 * worklist not empty test sequence is in insert_work().
714 * Please read comment there.
715 *
716 * NOT_RUNNING is clear. This means that trustee is not in
717 * charge and we're running on the local cpu w/ rq lock held
718 * and preemption disabled, which in turn means that none else
719 * could be manipulating idle_list, so dereferencing idle_list
720 * without gcwq lock is safe.
721 */
722 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
723 to_wakeup = first_worker(gcwq);
724 return to_wakeup ? to_wakeup->task : NULL;
725}
726
727/**
728 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200729 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200730 * @flags: flags to set
731 * @wakeup: wakeup an idle worker if necessary
732 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200733 * Set @flags in @worker->flags and adjust nr_running accordingly. If
734 * nr_running becomes zero and @wakeup is %true, an idle worker is
735 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200736 *
Tejun Heocb444762010-07-02 10:03:50 +0200737 * CONTEXT:
738 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200739 */
740static inline void worker_set_flags(struct worker *worker, unsigned int flags,
741 bool wakeup)
742{
Tejun Heoe22bee72010-06-29 10:07:14 +0200743 struct global_cwq *gcwq = worker->gcwq;
744
Tejun Heocb444762010-07-02 10:03:50 +0200745 WARN_ON_ONCE(worker->task != current);
746
Tejun Heoe22bee72010-06-29 10:07:14 +0200747 /*
748 * If transitioning into NOT_RUNNING, adjust nr_running and
749 * wake up an idle worker as necessary if requested by
750 * @wakeup.
751 */
752 if ((flags & WORKER_NOT_RUNNING) &&
753 !(worker->flags & WORKER_NOT_RUNNING)) {
754 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
755
756 if (wakeup) {
757 if (atomic_dec_and_test(nr_running) &&
758 !list_empty(&gcwq->worklist))
759 wake_up_worker(gcwq);
760 } else
761 atomic_dec(nr_running);
762 }
763
Tejun Heod302f012010-06-29 10:07:13 +0200764 worker->flags |= flags;
765}
766
767/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200769 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200770 * @flags: flags to clear
771 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200772 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200773 *
Tejun Heocb444762010-07-02 10:03:50 +0200774 * CONTEXT:
775 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200776 */
777static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
778{
Tejun Heoe22bee72010-06-29 10:07:14 +0200779 struct global_cwq *gcwq = worker->gcwq;
780 unsigned int oflags = worker->flags;
781
Tejun Heocb444762010-07-02 10:03:50 +0200782 WARN_ON_ONCE(worker->task != current);
783
Tejun Heod302f012010-06-29 10:07:13 +0200784 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200785
786 /* if transitioning out of NOT_RUNNING, increment nr_running */
787 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
788 if (!(worker->flags & WORKER_NOT_RUNNING))
789 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200790}
791
792/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200793 * busy_worker_head - return the busy hash head for a work
794 * @gcwq: gcwq of interest
795 * @work: work to be hashed
796 *
797 * Return hash head of @gcwq for @work.
798 *
799 * CONTEXT:
800 * spin_lock_irq(gcwq->lock).
801 *
802 * RETURNS:
803 * Pointer to the hash head.
804 */
805static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
806 struct work_struct *work)
807{
808 const int base_shift = ilog2(sizeof(struct work_struct));
809 unsigned long v = (unsigned long)work;
810
811 /* simple shift and fold hash, do we need something better? */
812 v >>= base_shift;
813 v += v >> BUSY_WORKER_HASH_ORDER;
814 v &= BUSY_WORKER_HASH_MASK;
815
816 return &gcwq->busy_hash[v];
817}
818
819/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200820 * __find_worker_executing_work - find worker which is executing a work
821 * @gcwq: gcwq of interest
822 * @bwh: hash head as returned by busy_worker_head()
823 * @work: work to find worker for
824 *
825 * Find a worker which is executing @work on @gcwq. @bwh should be
826 * the hash head obtained by calling busy_worker_head() with the same
827 * work.
828 *
829 * CONTEXT:
830 * spin_lock_irq(gcwq->lock).
831 *
832 * RETURNS:
833 * Pointer to worker which is executing @work if found, NULL
834 * otherwise.
835 */
836static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
837 struct hlist_head *bwh,
838 struct work_struct *work)
839{
840 struct worker *worker;
841 struct hlist_node *tmp;
842
843 hlist_for_each_entry(worker, tmp, bwh, hentry)
844 if (worker->current_work == work)
845 return worker;
846 return NULL;
847}
848
849/**
850 * find_worker_executing_work - find worker which is executing a work
851 * @gcwq: gcwq of interest
852 * @work: work to find worker for
853 *
854 * Find a worker which is executing @work on @gcwq. This function is
855 * identical to __find_worker_executing_work() except that this
856 * function calculates @bwh itself.
857 *
858 * CONTEXT:
859 * spin_lock_irq(gcwq->lock).
860 *
861 * RETURNS:
862 * Pointer to worker which is executing @work if found, NULL
863 * otherwise.
864 */
865static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
866 struct work_struct *work)
867{
868 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
869 work);
870}
871
872/**
Tejun Heo649027d2010-06-29 10:07:14 +0200873 * gcwq_determine_ins_pos - find insertion position
874 * @gcwq: gcwq of interest
875 * @cwq: cwq a work is being queued for
876 *
877 * A work for @cwq is about to be queued on @gcwq, determine insertion
878 * position for the work. If @cwq is for HIGHPRI wq, the work is
879 * queued at the head of the queue but in FIFO order with respect to
880 * other HIGHPRI works; otherwise, at the end of the queue. This
881 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
882 * there are HIGHPRI works pending.
883 *
884 * CONTEXT:
885 * spin_lock_irq(gcwq->lock).
886 *
887 * RETURNS:
888 * Pointer to inserstion position.
889 */
890static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
891 struct cpu_workqueue_struct *cwq)
892{
893 struct work_struct *twork;
894
895 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
896 return &gcwq->worklist;
897
898 list_for_each_entry(twork, &gcwq->worklist, entry) {
899 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
900
901 if (!(tcwq->wq->flags & WQ_HIGHPRI))
902 break;
903 }
904
905 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
906 return &twork->entry;
907}
908
909/**
Tejun Heo7e116292010-06-29 10:07:13 +0200910 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200911 * @cwq: cwq @work belongs to
912 * @work: work to insert
913 * @head: insertion point
914 * @extra_flags: extra WORK_STRUCT_* flags to set
915 *
Tejun Heo7e116292010-06-29 10:07:13 +0200916 * Insert @work which belongs to @cwq into @gcwq after @head.
917 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200918 *
919 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200920 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200921 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700922static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200923 struct work_struct *work, struct list_head *head,
924 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700925{
Tejun Heoe22bee72010-06-29 10:07:14 +0200926 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100927
Tejun Heo4690c4a2010-06-29 10:07:10 +0200928 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200929 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200930
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700931 /*
932 * Ensure that we get the right work->data if we see the
933 * result of list_add() below, see try_to_grab_pending().
934 */
935 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200936
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700937 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200938
939 /*
940 * Ensure either worker_sched_deactivated() sees the above
941 * list_add_tail() or we see zero nr_running to avoid workers
942 * lying around lazily while there are works to be processed.
943 */
944 smp_mb();
945
Tejun Heo649027d2010-06-29 10:07:14 +0200946 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200947 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700948}
949
Tejun Heo4690c4a2010-06-29 10:07:10 +0200950static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct work_struct *work)
952{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200953 struct global_cwq *gcwq;
954 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200955 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200956 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 unsigned long flags;
958
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900959 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200960
Tejun Heoe41e7042010-08-24 14:22:47 +0200961 if (WARN_ON_ONCE(wq->flags & WQ_DYING))
962 return;
963
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200964 /* determine gcwq to use */
965 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200966 struct global_cwq *last_gcwq;
967
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200968 if (unlikely(cpu == WORK_CPU_UNBOUND))
969 cpu = raw_smp_processor_id();
970
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200971 /*
972 * It's multi cpu. If @wq is non-reentrant and @work
973 * was previously on a different cpu, it might still
974 * be running there, in which case the work needs to
975 * be queued on that cpu to guarantee non-reentrance.
976 */
Tejun Heo502ca9d2010-06-29 10:07:13 +0200977 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200978 if (wq->flags & WQ_NON_REENTRANT &&
979 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
980 struct worker *worker;
981
982 spin_lock_irqsave(&last_gcwq->lock, flags);
983
984 worker = find_worker_executing_work(last_gcwq, work);
985
986 if (worker && worker->current_cwq->wq == wq)
987 gcwq = last_gcwq;
988 else {
989 /* meh... not running there, queue here */
990 spin_unlock_irqrestore(&last_gcwq->lock, flags);
991 spin_lock_irqsave(&gcwq->lock, flags);
992 }
993 } else
994 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +0200995 } else {
996 gcwq = get_gcwq(WORK_CPU_UNBOUND);
997 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +0200998 }
999
1000 /* gcwq determined, get cwq and queue */
1001 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001002 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001003
Tejun Heo4690c4a2010-06-29 10:07:10 +02001004 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001005
Tejun Heo73f53c42010-06-29 10:07:11 +02001006 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001007 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001008
1009 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001010 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001011 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001012 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001013 } else {
1014 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001015 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001016 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001017
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001018 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001019
Tejun Heo8b03ae32010-06-29 10:07:12 +02001020 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001023/**
1024 * queue_work - queue work on a workqueue
1025 * @wq: workqueue to use
1026 * @work: work to queue
1027 *
Alan Stern057647f2006-10-28 10:38:58 -07001028 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001030 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1031 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001033int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001035 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001037 ret = queue_work_on(get_cpu(), wq, work);
1038 put_cpu();
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return ret;
1041}
Dave Jonesae90dd52006-06-30 01:40:45 -04001042EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Zhang Ruic1a220e2008-07-23 21:28:39 -07001044/**
1045 * queue_work_on - queue work on specific cpu
1046 * @cpu: CPU number to execute work on
1047 * @wq: workqueue to use
1048 * @work: work to queue
1049 *
1050 * Returns 0 if @work was already on a queue, non-zero otherwise.
1051 *
1052 * We queue the work to a specific CPU, the caller must ensure it
1053 * can't go away.
1054 */
1055int
1056queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1057{
1058 int ret = 0;
1059
Tejun Heo22df02b2010-06-29 10:07:10 +02001060 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001061 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001062 ret = 1;
1063 }
1064 return ret;
1065}
1066EXPORT_SYMBOL_GPL(queue_work_on);
1067
Li Zefan6d141c32008-02-08 04:21:09 -08001068static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
David Howells52bad642006-11-22 14:54:01 +00001070 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001071 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Tejun Heo4690c4a2010-06-29 10:07:10 +02001073 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001076/**
1077 * queue_delayed_work - queue work on a workqueue after delay
1078 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001079 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001080 * @delay: number of jiffies to wait before queueing
1081 *
Alan Stern057647f2006-10-28 10:38:58 -07001082 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001083 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001084int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001085 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
David Howells52bad642006-11-22 14:54:01 +00001087 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001088 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001090 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
Dave Jonesae90dd52006-06-30 01:40:45 -04001092EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001094/**
1095 * queue_delayed_work_on - queue work on specific CPU after delay
1096 * @cpu: CPU number to execute work on
1097 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001098 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001099 * @delay: number of jiffies to wait before queueing
1100 *
Alan Stern057647f2006-10-28 10:38:58 -07001101 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001102 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001103int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001104 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001105{
1106 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001107 struct timer_list *timer = &dwork->timer;
1108 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001109
Tejun Heo22df02b2010-06-29 10:07:10 +02001110 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001111 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001112
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001113 BUG_ON(timer_pending(timer));
1114 BUG_ON(!list_empty(&work->entry));
1115
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001116 timer_stats_timer_set_start_info(&dwork->timer);
1117
Tejun Heo7a22ad72010-06-29 10:07:13 +02001118 /*
1119 * This stores cwq for the moment, for the timer_fn.
1120 * Note that the work's gcwq is preserved to allow
1121 * reentrance detection for delayed works.
1122 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001123 if (!(wq->flags & WQ_UNBOUND)) {
1124 struct global_cwq *gcwq = get_work_gcwq(work);
1125
1126 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1127 lcpu = gcwq->cpu;
1128 else
1129 lcpu = raw_smp_processor_id();
1130 } else
1131 lcpu = WORK_CPU_UNBOUND;
1132
Tejun Heo7a22ad72010-06-29 10:07:13 +02001133 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001134
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001135 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001136 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001137 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001138
1139 if (unlikely(cpu >= 0))
1140 add_timer_on(timer, cpu);
1141 else
1142 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001143 ret = 1;
1144 }
1145 return ret;
1146}
Dave Jonesae90dd52006-06-30 01:40:45 -04001147EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Tejun Heoc8e55f32010-06-29 10:07:12 +02001149/**
1150 * worker_enter_idle - enter idle state
1151 * @worker: worker which is entering idle state
1152 *
1153 * @worker is entering idle state. Update stats and idle timer if
1154 * necessary.
1155 *
1156 * LOCKING:
1157 * spin_lock_irq(gcwq->lock).
1158 */
1159static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001161 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Tejun Heoc8e55f32010-06-29 10:07:12 +02001163 BUG_ON(worker->flags & WORKER_IDLE);
1164 BUG_ON(!list_empty(&worker->entry) &&
1165 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Tejun Heocb444762010-07-02 10:03:50 +02001167 /* can't use worker_set_flags(), also called from start_worker() */
1168 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001169 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001170 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001171
Tejun Heoc8e55f32010-06-29 10:07:12 +02001172 /* idle_list is LIFO */
1173 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001174
Tejun Heoe22bee72010-06-29 10:07:14 +02001175 if (likely(!(worker->flags & WORKER_ROGUE))) {
1176 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1177 mod_timer(&gcwq->idle_timer,
1178 jiffies + IDLE_WORKER_TIMEOUT);
1179 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001180 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001181
1182 /* sanity check nr_running */
1183 WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
1184 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Tejun Heoc8e55f32010-06-29 10:07:12 +02001187/**
1188 * worker_leave_idle - leave idle state
1189 * @worker: worker which is leaving idle state
1190 *
1191 * @worker is leaving idle state. Update stats.
1192 *
1193 * LOCKING:
1194 * spin_lock_irq(gcwq->lock).
1195 */
1196static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001198 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tejun Heoc8e55f32010-06-29 10:07:12 +02001200 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001201 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001202 gcwq->nr_idle--;
1203 list_del_init(&worker->entry);
1204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Tejun Heoe22bee72010-06-29 10:07:14 +02001206/**
1207 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1208 * @worker: self
1209 *
1210 * Works which are scheduled while the cpu is online must at least be
1211 * scheduled to a worker which is bound to the cpu so that if they are
1212 * flushed from cpu callbacks while cpu is going down, they are
1213 * guaranteed to execute on the cpu.
1214 *
1215 * This function is to be used by rogue workers and rescuers to bind
1216 * themselves to the target cpu and may race with cpu going down or
1217 * coming online. kthread_bind() can't be used because it may put the
1218 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1219 * verbatim as it's best effort and blocking and gcwq may be
1220 * [dis]associated in the meantime.
1221 *
1222 * This function tries set_cpus_allowed() and locks gcwq and verifies
1223 * the binding against GCWQ_DISASSOCIATED which is set during
1224 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1225 * idle state or fetches works without dropping lock, it can guarantee
1226 * the scheduling requirement described in the first paragraph.
1227 *
1228 * CONTEXT:
1229 * Might sleep. Called without any lock but returns with gcwq->lock
1230 * held.
1231 *
1232 * RETURNS:
1233 * %true if the associated gcwq is online (@worker is successfully
1234 * bound), %false if offline.
1235 */
1236static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001237__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001238{
1239 struct global_cwq *gcwq = worker->gcwq;
1240 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Tejun Heoe22bee72010-06-29 10:07:14 +02001242 while (true) {
1243 /*
1244 * The following call may fail, succeed or succeed
1245 * without actually migrating the task to the cpu if
1246 * it races with cpu hotunplug operation. Verify
1247 * against GCWQ_DISASSOCIATED.
1248 */
Tejun Heof3421792010-07-02 10:03:51 +02001249 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1250 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001251
Tejun Heoe22bee72010-06-29 10:07:14 +02001252 spin_lock_irq(&gcwq->lock);
1253 if (gcwq->flags & GCWQ_DISASSOCIATED)
1254 return false;
1255 if (task_cpu(task) == gcwq->cpu &&
1256 cpumask_equal(&current->cpus_allowed,
1257 get_cpu_mask(gcwq->cpu)))
1258 return true;
1259 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001260
Tejun Heoe22bee72010-06-29 10:07:14 +02001261 /* CPU has come up inbetween, retry migration */
1262 cpu_relax();
1263 }
1264}
1265
1266/*
1267 * Function for worker->rebind_work used to rebind rogue busy workers
1268 * to the associated cpu which is coming back online. This is
1269 * scheduled by cpu up but can race with other cpu hotplug operations
1270 * and may be executed twice without intervening cpu down.
1271 */
1272static void worker_rebind_fn(struct work_struct *work)
1273{
1274 struct worker *worker = container_of(work, struct worker, rebind_work);
1275 struct global_cwq *gcwq = worker->gcwq;
1276
1277 if (worker_maybe_bind_and_lock(worker))
1278 worker_clr_flags(worker, WORKER_REBIND);
1279
1280 spin_unlock_irq(&gcwq->lock);
1281}
1282
Tejun Heoc34056a2010-06-29 10:07:11 +02001283static struct worker *alloc_worker(void)
1284{
1285 struct worker *worker;
1286
1287 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001288 if (worker) {
1289 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001290 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001291 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1292 /* on creation a worker is in !idle && prep state */
1293 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001294 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001295 return worker;
1296}
1297
1298/**
1299 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001300 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001301 * @bind: whether to set affinity to @cpu or not
1302 *
Tejun Heo7e116292010-06-29 10:07:13 +02001303 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001304 * can be started by calling start_worker() or destroyed using
1305 * destroy_worker().
1306 *
1307 * CONTEXT:
1308 * Might sleep. Does GFP_KERNEL allocations.
1309 *
1310 * RETURNS:
1311 * Pointer to the newly created worker.
1312 */
Tejun Heo7e116292010-06-29 10:07:13 +02001313static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001314{
Tejun Heof3421792010-07-02 10:03:51 +02001315 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001316 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001317 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001318
Tejun Heo8b03ae32010-06-29 10:07:12 +02001319 spin_lock_irq(&gcwq->lock);
1320 while (ida_get_new(&gcwq->worker_ida, &id)) {
1321 spin_unlock_irq(&gcwq->lock);
1322 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001323 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001324 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001325 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001326 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001327
1328 worker = alloc_worker();
1329 if (!worker)
1330 goto fail;
1331
Tejun Heo8b03ae32010-06-29 10:07:12 +02001332 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001333 worker->id = id;
1334
Tejun Heof3421792010-07-02 10:03:51 +02001335 if (!on_unbound_cpu)
1336 worker->task = kthread_create(worker_thread, worker,
1337 "kworker/%u:%d", gcwq->cpu, id);
1338 else
1339 worker->task = kthread_create(worker_thread, worker,
1340 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001341 if (IS_ERR(worker->task))
1342 goto fail;
1343
Tejun Heodb7bccf2010-06-29 10:07:12 +02001344 /*
1345 * A rogue worker will become a regular one if CPU comes
1346 * online later on. Make sure every worker has
1347 * PF_THREAD_BOUND set.
1348 */
Tejun Heof3421792010-07-02 10:03:51 +02001349 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001350 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001351 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001352 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001353 if (on_unbound_cpu)
1354 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001356
Tejun Heoc34056a2010-06-29 10:07:11 +02001357 return worker;
1358fail:
1359 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001360 spin_lock_irq(&gcwq->lock);
1361 ida_remove(&gcwq->worker_ida, id);
1362 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001363 }
1364 kfree(worker);
1365 return NULL;
1366}
1367
1368/**
1369 * start_worker - start a newly created worker
1370 * @worker: worker to start
1371 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001372 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001373 *
1374 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001375 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001376 */
1377static void start_worker(struct worker *worker)
1378{
Tejun Heocb444762010-07-02 10:03:50 +02001379 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001380 worker->gcwq->nr_workers++;
1381 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001382 wake_up_process(worker->task);
1383}
1384
1385/**
1386 * destroy_worker - destroy a workqueue worker
1387 * @worker: worker to be destroyed
1388 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001389 * Destroy @worker and adjust @gcwq stats accordingly.
1390 *
1391 * CONTEXT:
1392 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001393 */
1394static void destroy_worker(struct worker *worker)
1395{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001396 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001397 int id = worker->id;
1398
1399 /* sanity check frenzy */
1400 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001401 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001402
Tejun Heoc8e55f32010-06-29 10:07:12 +02001403 if (worker->flags & WORKER_STARTED)
1404 gcwq->nr_workers--;
1405 if (worker->flags & WORKER_IDLE)
1406 gcwq->nr_idle--;
1407
1408 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001409 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001410
1411 spin_unlock_irq(&gcwq->lock);
1412
Tejun Heoc34056a2010-06-29 10:07:11 +02001413 kthread_stop(worker->task);
1414 kfree(worker);
1415
Tejun Heo8b03ae32010-06-29 10:07:12 +02001416 spin_lock_irq(&gcwq->lock);
1417 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001418}
1419
Tejun Heoe22bee72010-06-29 10:07:14 +02001420static void idle_worker_timeout(unsigned long __gcwq)
1421{
1422 struct global_cwq *gcwq = (void *)__gcwq;
1423
1424 spin_lock_irq(&gcwq->lock);
1425
1426 if (too_many_workers(gcwq)) {
1427 struct worker *worker;
1428 unsigned long expires;
1429
1430 /* idle_list is kept in LIFO order, check the last one */
1431 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1432 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1433
1434 if (time_before(jiffies, expires))
1435 mod_timer(&gcwq->idle_timer, expires);
1436 else {
1437 /* it's been idle for too long, wake up manager */
1438 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1439 wake_up_worker(gcwq);
1440 }
1441 }
1442
1443 spin_unlock_irq(&gcwq->lock);
1444}
1445
1446static bool send_mayday(struct work_struct *work)
1447{
1448 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1449 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001450 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001451
1452 if (!(wq->flags & WQ_RESCUER))
1453 return false;
1454
1455 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001456 cpu = cwq->gcwq->cpu;
1457 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1458 if (cpu == WORK_CPU_UNBOUND)
1459 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001460 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001461 wake_up_process(wq->rescuer->task);
1462 return true;
1463}
1464
1465static void gcwq_mayday_timeout(unsigned long __gcwq)
1466{
1467 struct global_cwq *gcwq = (void *)__gcwq;
1468 struct work_struct *work;
1469
1470 spin_lock_irq(&gcwq->lock);
1471
1472 if (need_to_create_worker(gcwq)) {
1473 /*
1474 * We've been trying to create a new worker but
1475 * haven't been successful. We might be hitting an
1476 * allocation deadlock. Send distress signals to
1477 * rescuers.
1478 */
1479 list_for_each_entry(work, &gcwq->worklist, entry)
1480 send_mayday(work);
1481 }
1482
1483 spin_unlock_irq(&gcwq->lock);
1484
1485 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1486}
1487
1488/**
1489 * maybe_create_worker - create a new worker if necessary
1490 * @gcwq: gcwq to create a new worker for
1491 *
1492 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1493 * have at least one idle worker on return from this function. If
1494 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1495 * sent to all rescuers with works scheduled on @gcwq to resolve
1496 * possible allocation deadlock.
1497 *
1498 * On return, need_to_create_worker() is guaranteed to be false and
1499 * may_start_working() true.
1500 *
1501 * LOCKING:
1502 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1503 * multiple times. Does GFP_KERNEL allocations. Called only from
1504 * manager.
1505 *
1506 * RETURNS:
1507 * false if no action was taken and gcwq->lock stayed locked, true
1508 * otherwise.
1509 */
1510static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001511__releases(&gcwq->lock)
1512__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001513{
1514 if (!need_to_create_worker(gcwq))
1515 return false;
1516restart:
Tejun Heo9f9c236442010-07-14 11:31:20 +02001517 spin_unlock_irq(&gcwq->lock);
1518
Tejun Heoe22bee72010-06-29 10:07:14 +02001519 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1520 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1521
1522 while (true) {
1523 struct worker *worker;
1524
Tejun Heoe22bee72010-06-29 10:07:14 +02001525 worker = create_worker(gcwq, true);
1526 if (worker) {
1527 del_timer_sync(&gcwq->mayday_timer);
1528 spin_lock_irq(&gcwq->lock);
1529 start_worker(worker);
1530 BUG_ON(need_to_create_worker(gcwq));
1531 return true;
1532 }
1533
1534 if (!need_to_create_worker(gcwq))
1535 break;
1536
Tejun Heoe22bee72010-06-29 10:07:14 +02001537 __set_current_state(TASK_INTERRUPTIBLE);
1538 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c236442010-07-14 11:31:20 +02001539
Tejun Heoe22bee72010-06-29 10:07:14 +02001540 if (!need_to_create_worker(gcwq))
1541 break;
1542 }
1543
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 del_timer_sync(&gcwq->mayday_timer);
1545 spin_lock_irq(&gcwq->lock);
1546 if (need_to_create_worker(gcwq))
1547 goto restart;
1548 return true;
1549}
1550
1551/**
1552 * maybe_destroy_worker - destroy workers which have been idle for a while
1553 * @gcwq: gcwq to destroy workers for
1554 *
1555 * Destroy @gcwq workers which have been idle for longer than
1556 * IDLE_WORKER_TIMEOUT.
1557 *
1558 * LOCKING:
1559 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1560 * multiple times. Called only from manager.
1561 *
1562 * RETURNS:
1563 * false if no action was taken and gcwq->lock stayed locked, true
1564 * otherwise.
1565 */
1566static bool maybe_destroy_workers(struct global_cwq *gcwq)
1567{
1568 bool ret = false;
1569
1570 while (too_many_workers(gcwq)) {
1571 struct worker *worker;
1572 unsigned long expires;
1573
1574 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1575 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1576
1577 if (time_before(jiffies, expires)) {
1578 mod_timer(&gcwq->idle_timer, expires);
1579 break;
1580 }
1581
1582 destroy_worker(worker);
1583 ret = true;
1584 }
1585
1586 return ret;
1587}
1588
1589/**
1590 * manage_workers - manage worker pool
1591 * @worker: self
1592 *
1593 * Assume the manager role and manage gcwq worker pool @worker belongs
1594 * to. At any given time, there can be only zero or one manager per
1595 * gcwq. The exclusion is handled automatically by this function.
1596 *
1597 * The caller can safely start processing works on false return. On
1598 * true return, it's guaranteed that need_to_create_worker() is false
1599 * and may_start_working() is true.
1600 *
1601 * CONTEXT:
1602 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1603 * multiple times. Does GFP_KERNEL allocations.
1604 *
1605 * RETURNS:
1606 * false if no action was taken and gcwq->lock stayed locked, true if
1607 * some action was taken.
1608 */
1609static bool manage_workers(struct worker *worker)
1610{
1611 struct global_cwq *gcwq = worker->gcwq;
1612 bool ret = false;
1613
1614 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1615 return ret;
1616
1617 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1618 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1619
1620 /*
1621 * Destroy and then create so that may_start_working() is true
1622 * on return.
1623 */
1624 ret |= maybe_destroy_workers(gcwq);
1625 ret |= maybe_create_worker(gcwq);
1626
1627 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1628
1629 /*
1630 * The trustee might be waiting to take over the manager
1631 * position, tell it we're done.
1632 */
1633 if (unlikely(gcwq->trustee))
1634 wake_up_all(&gcwq->trustee_wait);
1635
1636 return ret;
1637}
1638
Tejun Heoa62428c2010-06-29 10:07:10 +02001639/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001640 * move_linked_works - move linked works to a list
1641 * @work: start of series of works to be scheduled
1642 * @head: target list to append @work to
1643 * @nextp: out paramter for nested worklist walking
1644 *
1645 * Schedule linked works starting from @work to @head. Work series to
1646 * be scheduled starts at @work and includes any consecutive work with
1647 * WORK_STRUCT_LINKED set in its predecessor.
1648 *
1649 * If @nextp is not NULL, it's updated to point to the next work of
1650 * the last scheduled work. This allows move_linked_works() to be
1651 * nested inside outer list_for_each_entry_safe().
1652 *
1653 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001654 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001655 */
1656static void move_linked_works(struct work_struct *work, struct list_head *head,
1657 struct work_struct **nextp)
1658{
1659 struct work_struct *n;
1660
1661 /*
1662 * Linked worklist will always end before the end of the list,
1663 * use NULL for list head.
1664 */
1665 list_for_each_entry_safe_from(work, n, NULL, entry) {
1666 list_move_tail(&work->entry, head);
1667 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1668 break;
1669 }
1670
1671 /*
1672 * If we're already inside safe list traversal and have moved
1673 * multiple works to the scheduled queue, the next position
1674 * needs to be updated.
1675 */
1676 if (nextp)
1677 *nextp = n;
1678}
1679
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001680static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1681{
1682 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1683 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001684 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001685
Tejun Heocdadf002010-10-05 10:49:55 +02001686 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001687 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001688 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001689 cwq->nr_active++;
1690}
1691
Tejun Heoaffee4b2010-06-29 10:07:12 +02001692/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001693 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1694 * @cwq: cwq of interest
1695 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001696 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001697 *
1698 * A work either has completed or is removed from pending queue,
1699 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1700 *
1701 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001702 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001703 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001704static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1705 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001706{
1707 /* ignore uncolored works */
1708 if (color == WORK_NO_COLOR)
1709 return;
1710
1711 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001712
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001713 if (!delayed) {
1714 cwq->nr_active--;
1715 if (!list_empty(&cwq->delayed_works)) {
1716 /* one down, submit a delayed one */
1717 if (cwq->nr_active < cwq->max_active)
1718 cwq_activate_first_delayed(cwq);
1719 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001720 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001721
1722 /* is flush in progress and are we at the flushing tip? */
1723 if (likely(cwq->flush_color != color))
1724 return;
1725
1726 /* are there still in-flight works? */
1727 if (cwq->nr_in_flight[color])
1728 return;
1729
1730 /* this cwq is done, clear flush_color */
1731 cwq->flush_color = -1;
1732
1733 /*
1734 * If this was the last cwq, wake up the first flusher. It
1735 * will handle the rest.
1736 */
1737 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1738 complete(&cwq->wq->first_flusher->done);
1739}
1740
1741/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001742 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001743 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001744 * @work: work to process
1745 *
1746 * Process @work. This function contains all the logics necessary to
1747 * process a single work including synchronization against and
1748 * interaction with other workers on the same cpu, queueing and
1749 * flushing. As long as context requirement is met, any worker can
1750 * call this function to process a work.
1751 *
1752 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001753 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001754 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001755static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001756__releases(&gcwq->lock)
1757__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001758{
Tejun Heo7e116292010-06-29 10:07:13 +02001759 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001760 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001761 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001762 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001763 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001764 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001765 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001766#ifdef CONFIG_LOCKDEP
1767 /*
1768 * It is permissible to free the struct work_struct from
1769 * inside the function that is called from it, this we need to
1770 * take into account for lockdep too. To avoid bogus "held
1771 * lock freed" warnings as well as problems when looking into
1772 * work->lockdep_map, make a copy and use that here.
1773 */
1774 struct lockdep_map lockdep_map = work->lockdep_map;
1775#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001776 /*
1777 * A single work shouldn't be executed concurrently by
1778 * multiple workers on a single cpu. Check whether anyone is
1779 * already processing the work. If so, defer the work to the
1780 * currently executing one.
1781 */
1782 collision = __find_worker_executing_work(gcwq, bwh, work);
1783 if (unlikely(collision)) {
1784 move_linked_works(work, &collision->scheduled, NULL);
1785 return;
1786 }
1787
Tejun Heoa62428c2010-06-29 10:07:10 +02001788 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001789 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001790 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001791 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001792 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001793 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001794
Tejun Heo7a22ad72010-06-29 10:07:13 +02001795 /* record the current cpu number in the work data and dequeue */
1796 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001797 list_del_init(&work->entry);
1798
Tejun Heo649027d2010-06-29 10:07:14 +02001799 /*
1800 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1801 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1802 */
1803 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1804 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1805 struct work_struct, entry);
1806
1807 if (!list_empty(&gcwq->worklist) &&
1808 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1809 wake_up_worker(gcwq);
1810 else
1811 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1812 }
1813
Tejun Heofb0e7be2010-06-29 10:07:15 +02001814 /*
1815 * CPU intensive works don't participate in concurrency
1816 * management. They're the scheduler's responsibility.
1817 */
1818 if (unlikely(cpu_intensive))
1819 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1820
Tejun Heo8b03ae32010-06-29 10:07:12 +02001821 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001822
Tejun Heoa62428c2010-06-29 10:07:10 +02001823 work_clear_pending(work);
1824 lock_map_acquire(&cwq->wq->lockdep_map);
1825 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001826 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001827 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001828 /*
1829 * While we must be careful to not use "work" after this, the trace
1830 * point will only record its address.
1831 */
1832 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001833 lock_map_release(&lockdep_map);
1834 lock_map_release(&cwq->wq->lockdep_map);
1835
1836 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1837 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1838 "%s/0x%08x/%d\n",
1839 current->comm, preempt_count(), task_pid_nr(current));
1840 printk(KERN_ERR " last function: ");
1841 print_symbol("%s\n", (unsigned long)f);
1842 debug_show_held_locks(current);
1843 dump_stack();
1844 }
1845
Tejun Heo8b03ae32010-06-29 10:07:12 +02001846 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001847
Tejun Heofb0e7be2010-06-29 10:07:15 +02001848 /* clear cpu intensive status */
1849 if (unlikely(cpu_intensive))
1850 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1851
Tejun Heoa62428c2010-06-29 10:07:10 +02001852 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001853 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001854 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001855 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001856 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001857}
1858
Tejun Heoaffee4b2010-06-29 10:07:12 +02001859/**
1860 * process_scheduled_works - process scheduled works
1861 * @worker: self
1862 *
1863 * Process all scheduled works. Please note that the scheduled list
1864 * may change while processing a work, so this function repeatedly
1865 * fetches a work from the top and executes it.
1866 *
1867 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001868 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001869 * multiple times.
1870 */
1871static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001873 while (!list_empty(&worker->scheduled)) {
1874 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001876 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Tejun Heo4690c4a2010-06-29 10:07:10 +02001880/**
1881 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001882 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001883 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001884 * The gcwq worker thread function. There's a single dynamic pool of
1885 * these per each cpu. These workers process all works regardless of
1886 * their specific target workqueue. The only exception is works which
1887 * belong to workqueues with a rescuer which will be explained in
1888 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001889 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001890static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Tejun Heoc34056a2010-06-29 10:07:11 +02001892 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001893 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 /* tell the scheduler that this is a workqueue worker */
1896 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001897woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001898 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Tejun Heoc8e55f32010-06-29 10:07:12 +02001900 /* DIE can be set only while we're idle, checking here is enough */
1901 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001902 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001903 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Tejun Heoc8e55f32010-06-29 10:07:12 +02001907 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001908recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001909 /* no more worker necessary? */
1910 if (!need_more_worker(gcwq))
1911 goto sleep;
1912
1913 /* do we need to manage? */
1914 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1915 goto recheck;
1916
Tejun Heoc8e55f32010-06-29 10:07:12 +02001917 /*
1918 * ->scheduled list can only be filled while a worker is
1919 * preparing to process a work or actually processing it.
1920 * Make sure nobody diddled with it while I was sleeping.
1921 */
1922 BUG_ON(!list_empty(&worker->scheduled));
1923
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 /*
1925 * When control reaches this point, we're guaranteed to have
1926 * at least one idle worker or that someone else has already
1927 * assumed the manager role.
1928 */
1929 worker_clr_flags(worker, WORKER_PREP);
1930
1931 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001932 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02001933 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001934 struct work_struct, entry);
1935
1936 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1937 /* optimization path, not strictly necessary */
1938 process_one_work(worker, work);
1939 if (unlikely(!list_empty(&worker->scheduled)))
1940 process_scheduled_works(worker);
1941 } else {
1942 move_linked_works(work, &worker->scheduled, NULL);
1943 process_scheduled_works(worker);
1944 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001945 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001946
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001948sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
1950 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001951
Tejun Heoc8e55f32010-06-29 10:07:12 +02001952 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 * gcwq->lock is held and there's no work to process and no
1954 * need to manage, sleep. Workers are woken up only while
1955 * holding gcwq->lock or from local cpu, so setting the
1956 * current state before releasing gcwq->lock is enough to
1957 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001958 */
1959 worker_enter_idle(worker);
1960 __set_current_state(TASK_INTERRUPTIBLE);
1961 spin_unlock_irq(&gcwq->lock);
1962 schedule();
1963 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Tejun Heoe22bee72010-06-29 10:07:14 +02001966/**
1967 * rescuer_thread - the rescuer thread function
1968 * @__wq: the associated workqueue
1969 *
1970 * Workqueue rescuer thread function. There's one rescuer for each
1971 * workqueue which has WQ_RESCUER set.
1972 *
1973 * Regular work processing on a gcwq may block trying to create a new
1974 * worker which uses GFP_KERNEL allocation which has slight chance of
1975 * developing into deadlock if some works currently on the same queue
1976 * need to be processed to satisfy the GFP_KERNEL allocation. This is
1977 * the problem rescuer solves.
1978 *
1979 * When such condition is possible, the gcwq summons rescuers of all
1980 * workqueues which have works queued on the gcwq and let them process
1981 * those works so that forward progress can be guaranteed.
1982 *
1983 * This should happen rarely.
1984 */
1985static int rescuer_thread(void *__wq)
1986{
1987 struct workqueue_struct *wq = __wq;
1988 struct worker *rescuer = wq->rescuer;
1989 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02001990 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 unsigned int cpu;
1992
1993 set_user_nice(current, RESCUER_NICE_LEVEL);
1994repeat:
1995 set_current_state(TASK_INTERRUPTIBLE);
1996
1997 if (kthread_should_stop())
1998 return 0;
1999
Tejun Heof3421792010-07-02 10:03:51 +02002000 /*
2001 * See whether any cpu is asking for help. Unbounded
2002 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2003 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002004 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002005 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2006 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002007 struct global_cwq *gcwq = cwq->gcwq;
2008 struct work_struct *work, *n;
2009
2010 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002011 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002012
2013 /* migrate to the target cpu if possible */
2014 rescuer->gcwq = gcwq;
2015 worker_maybe_bind_and_lock(rescuer);
2016
2017 /*
2018 * Slurp in all works issued via this workqueue and
2019 * process'em.
2020 */
2021 BUG_ON(!list_empty(&rescuer->scheduled));
2022 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2023 if (get_work_cwq(work) == cwq)
2024 move_linked_works(work, scheduled, &n);
2025
2026 process_scheduled_works(rescuer);
2027 spin_unlock_irq(&gcwq->lock);
2028 }
2029
2030 schedule();
2031 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002034struct wq_barrier {
2035 struct work_struct work;
2036 struct completion done;
2037};
2038
2039static void wq_barrier_func(struct work_struct *work)
2040{
2041 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2042 complete(&barr->done);
2043}
2044
Tejun Heo4690c4a2010-06-29 10:07:10 +02002045/**
2046 * insert_wq_barrier - insert a barrier work
2047 * @cwq: cwq to insert barrier into
2048 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002049 * @target: target work to attach @barr to
2050 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002051 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002052 * @barr is linked to @target such that @barr is completed only after
2053 * @target finishes execution. Please note that the ordering
2054 * guarantee is observed only with respect to @target and on the local
2055 * cpu.
2056 *
2057 * Currently, a queued barrier can't be canceled. This is because
2058 * try_to_grab_pending() can't determine whether the work to be
2059 * grabbed is at the head of the queue and thus can't clear LINKED
2060 * flag of the previous work while there must be a valid next work
2061 * after a work with LINKED flag set.
2062 *
2063 * Note that when @worker is non-NULL, @target may be modified
2064 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002065 *
2066 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002067 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002068 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002069static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002070 struct wq_barrier *barr,
2071 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002072{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002073 struct list_head *head;
2074 unsigned int linked = 0;
2075
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002076 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002077 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002078 * as we know for sure that this will not trigger any of the
2079 * checks and call back into the fixup functions where we
2080 * might deadlock.
2081 */
2082 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002083 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002084 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002085
Tejun Heoaffee4b2010-06-29 10:07:12 +02002086 /*
2087 * If @target is currently being executed, schedule the
2088 * barrier to the worker; otherwise, put it after @target.
2089 */
2090 if (worker)
2091 head = worker->scheduled.next;
2092 else {
2093 unsigned long *bits = work_data_bits(target);
2094
2095 head = target->entry.next;
2096 /* there can already be other linked works, inherit and set */
2097 linked = *bits & WORK_STRUCT_LINKED;
2098 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2099 }
2100
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002101 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002102 insert_work(cwq, &barr->work, head,
2103 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002104}
2105
Tejun Heo73f53c42010-06-29 10:07:11 +02002106/**
2107 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2108 * @wq: workqueue being flushed
2109 * @flush_color: new flush color, < 0 for no-op
2110 * @work_color: new work color, < 0 for no-op
2111 *
2112 * Prepare cwqs for workqueue flushing.
2113 *
2114 * If @flush_color is non-negative, flush_color on all cwqs should be
2115 * -1. If no cwq has in-flight commands at the specified color, all
2116 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2117 * has in flight commands, its cwq->flush_color is set to
2118 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2119 * wakeup logic is armed and %true is returned.
2120 *
2121 * The caller should have initialized @wq->first_flusher prior to
2122 * calling this function with non-negative @flush_color. If
2123 * @flush_color is negative, no flush color update is done and %false
2124 * is returned.
2125 *
2126 * If @work_color is non-negative, all cwqs should have the same
2127 * work_color which is previous to @work_color and all will be
2128 * advanced to @work_color.
2129 *
2130 * CONTEXT:
2131 * mutex_lock(wq->flush_mutex).
2132 *
2133 * RETURNS:
2134 * %true if @flush_color >= 0 and there's something to flush. %false
2135 * otherwise.
2136 */
2137static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2138 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Tejun Heo73f53c42010-06-29 10:07:11 +02002140 bool wait = false;
2141 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002142
Tejun Heo73f53c42010-06-29 10:07:11 +02002143 if (flush_color >= 0) {
2144 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2145 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002146 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002147
Tejun Heof3421792010-07-02 10:03:51 +02002148 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002149 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002150 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002151
Tejun Heo8b03ae32010-06-29 10:07:12 +02002152 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002153
2154 if (flush_color >= 0) {
2155 BUG_ON(cwq->flush_color != -1);
2156
2157 if (cwq->nr_in_flight[flush_color]) {
2158 cwq->flush_color = flush_color;
2159 atomic_inc(&wq->nr_cwqs_to_flush);
2160 wait = true;
2161 }
2162 }
2163
2164 if (work_color >= 0) {
2165 BUG_ON(work_color != work_next_color(cwq->work_color));
2166 cwq->work_color = work_color;
2167 }
2168
Tejun Heo8b03ae32010-06-29 10:07:12 +02002169 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002170 }
2171
2172 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2173 complete(&wq->first_flusher->done);
2174
2175 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002178/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002180 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 *
2182 * Forces execution of the workqueue and blocks until its completion.
2183 * This is typically used in driver shutdown handlers.
2184 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002185 * We sleep until all works which were queued on entry have been handled,
2186 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002188void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Tejun Heo73f53c42010-06-29 10:07:11 +02002190 struct wq_flusher this_flusher = {
2191 .list = LIST_HEAD_INIT(this_flusher.list),
2192 .flush_color = -1,
2193 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2194 };
2195 int next_color;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002196
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002197 lock_map_acquire(&wq->lockdep_map);
2198 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002199
2200 mutex_lock(&wq->flush_mutex);
2201
2202 /*
2203 * Start-to-wait phase
2204 */
2205 next_color = work_next_color(wq->work_color);
2206
2207 if (next_color != wq->flush_color) {
2208 /*
2209 * Color space is not full. The current work_color
2210 * becomes our flush_color and work_color is advanced
2211 * by one.
2212 */
2213 BUG_ON(!list_empty(&wq->flusher_overflow));
2214 this_flusher.flush_color = wq->work_color;
2215 wq->work_color = next_color;
2216
2217 if (!wq->first_flusher) {
2218 /* no flush in progress, become the first flusher */
2219 BUG_ON(wq->flush_color != this_flusher.flush_color);
2220
2221 wq->first_flusher = &this_flusher;
2222
2223 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2224 wq->work_color)) {
2225 /* nothing to flush, done */
2226 wq->flush_color = next_color;
2227 wq->first_flusher = NULL;
2228 goto out_unlock;
2229 }
2230 } else {
2231 /* wait in queue */
2232 BUG_ON(wq->flush_color == this_flusher.flush_color);
2233 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2234 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2235 }
2236 } else {
2237 /*
2238 * Oops, color space is full, wait on overflow queue.
2239 * The next flush completion will assign us
2240 * flush_color and transfer to flusher_queue.
2241 */
2242 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2243 }
2244
2245 mutex_unlock(&wq->flush_mutex);
2246
2247 wait_for_completion(&this_flusher.done);
2248
2249 /*
2250 * Wake-up-and-cascade phase
2251 *
2252 * First flushers are responsible for cascading flushes and
2253 * handling overflow. Non-first flushers can simply return.
2254 */
2255 if (wq->first_flusher != &this_flusher)
2256 return;
2257
2258 mutex_lock(&wq->flush_mutex);
2259
Tejun Heo4ce48b32010-07-02 10:03:51 +02002260 /* we might have raced, check again with mutex held */
2261 if (wq->first_flusher != &this_flusher)
2262 goto out_unlock;
2263
Tejun Heo73f53c42010-06-29 10:07:11 +02002264 wq->first_flusher = NULL;
2265
2266 BUG_ON(!list_empty(&this_flusher.list));
2267 BUG_ON(wq->flush_color != this_flusher.flush_color);
2268
2269 while (true) {
2270 struct wq_flusher *next, *tmp;
2271
2272 /* complete all the flushers sharing the current flush color */
2273 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2274 if (next->flush_color != wq->flush_color)
2275 break;
2276 list_del_init(&next->list);
2277 complete(&next->done);
2278 }
2279
2280 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2281 wq->flush_color != work_next_color(wq->work_color));
2282
2283 /* this flush_color is finished, advance by one */
2284 wq->flush_color = work_next_color(wq->flush_color);
2285
2286 /* one color has been freed, handle overflow queue */
2287 if (!list_empty(&wq->flusher_overflow)) {
2288 /*
2289 * Assign the same color to all overflowed
2290 * flushers, advance work_color and append to
2291 * flusher_queue. This is the start-to-wait
2292 * phase for these overflowed flushers.
2293 */
2294 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2295 tmp->flush_color = wq->work_color;
2296
2297 wq->work_color = work_next_color(wq->work_color);
2298
2299 list_splice_tail_init(&wq->flusher_overflow,
2300 &wq->flusher_queue);
2301 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2302 }
2303
2304 if (list_empty(&wq->flusher_queue)) {
2305 BUG_ON(wq->flush_color != wq->work_color);
2306 break;
2307 }
2308
2309 /*
2310 * Need to flush more colors. Make the next flusher
2311 * the new first flusher and arm cwqs.
2312 */
2313 BUG_ON(wq->flush_color == wq->work_color);
2314 BUG_ON(wq->flush_color != next->flush_color);
2315
2316 list_del_init(&next->list);
2317 wq->first_flusher = next;
2318
2319 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2320 break;
2321
2322 /*
2323 * Meh... this color is already done, clear first
2324 * flusher and repeat cascading.
2325 */
2326 wq->first_flusher = NULL;
2327 }
2328
2329out_unlock:
2330 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
Dave Jonesae90dd52006-06-30 01:40:45 -04002332EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Tejun Heobaf59022010-09-16 10:42:16 +02002334static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2335 bool wait_executing)
2336{
2337 struct worker *worker = NULL;
2338 struct global_cwq *gcwq;
2339 struct cpu_workqueue_struct *cwq;
2340
2341 might_sleep();
2342 gcwq = get_work_gcwq(work);
2343 if (!gcwq)
2344 return false;
2345
2346 spin_lock_irq(&gcwq->lock);
2347 if (!list_empty(&work->entry)) {
2348 /*
2349 * See the comment near try_to_grab_pending()->smp_rmb().
2350 * If it was re-queued to a different gcwq under us, we
2351 * are not going to wait.
2352 */
2353 smp_rmb();
2354 cwq = get_work_cwq(work);
2355 if (unlikely(!cwq || gcwq != cwq->gcwq))
2356 goto already_gone;
2357 } else if (wait_executing) {
2358 worker = find_worker_executing_work(gcwq, work);
2359 if (!worker)
2360 goto already_gone;
2361 cwq = worker->current_cwq;
2362 } else
2363 goto already_gone;
2364
2365 insert_wq_barrier(cwq, barr, work, worker);
2366 spin_unlock_irq(&gcwq->lock);
2367
2368 lock_map_acquire(&cwq->wq->lockdep_map);
2369 lock_map_release(&cwq->wq->lockdep_map);
2370 return true;
2371already_gone:
2372 spin_unlock_irq(&gcwq->lock);
2373 return false;
2374}
2375
Oleg Nesterovdb700892008-07-25 01:47:49 -07002376/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002377 * flush_work - wait for a work to finish executing the last queueing instance
2378 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002379 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002380 * Wait until @work has finished execution. This function considers
2381 * only the last queueing instance of @work. If @work has been
2382 * enqueued across different CPUs on a non-reentrant workqueue or on
2383 * multiple workqueues, @work might still be executing on return on
2384 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002385 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002386 * If @work was queued only on a non-reentrant, ordered or unbound
2387 * workqueue, @work is guaranteed to be idle on return if it hasn't
2388 * been requeued since flush started.
2389 *
2390 * RETURNS:
2391 * %true if flush_work() waited for the work to finish execution,
2392 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002393 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002394bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002395{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002396 struct wq_barrier barr;
2397
Tejun Heobaf59022010-09-16 10:42:16 +02002398 if (start_flush_work(work, &barr, true)) {
2399 wait_for_completion(&barr.done);
2400 destroy_work_on_stack(&barr.work);
2401 return true;
2402 } else
2403 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002404}
2405EXPORT_SYMBOL_GPL(flush_work);
2406
Tejun Heo401a8d02010-09-16 10:36:00 +02002407static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2408{
2409 struct wq_barrier barr;
2410 struct worker *worker;
2411
2412 spin_lock_irq(&gcwq->lock);
2413
2414 worker = find_worker_executing_work(gcwq, work);
2415 if (unlikely(worker))
2416 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2417
2418 spin_unlock_irq(&gcwq->lock);
2419
2420 if (unlikely(worker)) {
2421 wait_for_completion(&barr.done);
2422 destroy_work_on_stack(&barr.work);
2423 return true;
2424 } else
2425 return false;
2426}
2427
2428static bool wait_on_work(struct work_struct *work)
2429{
2430 bool ret = false;
2431 int cpu;
2432
2433 might_sleep();
2434
2435 lock_map_acquire(&work->lockdep_map);
2436 lock_map_release(&work->lockdep_map);
2437
2438 for_each_gcwq_cpu(cpu)
2439 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2440 return ret;
2441}
2442
Tejun Heo09383492010-09-16 10:48:29 +02002443/**
2444 * flush_work_sync - wait until a work has finished execution
2445 * @work: the work to flush
2446 *
2447 * Wait until @work has finished execution. On return, it's
2448 * guaranteed that all queueing instances of @work which happened
2449 * before this function is called are finished. In other words, if
2450 * @work hasn't been requeued since this function was called, @work is
2451 * guaranteed to be idle on return.
2452 *
2453 * RETURNS:
2454 * %true if flush_work_sync() waited for the work to finish execution,
2455 * %false if it was already idle.
2456 */
2457bool flush_work_sync(struct work_struct *work)
2458{
2459 struct wq_barrier barr;
2460 bool pending, waited;
2461
2462 /* we'll wait for executions separately, queue barr only if pending */
2463 pending = start_flush_work(work, &barr, false);
2464
2465 /* wait for executions to finish */
2466 waited = wait_on_work(work);
2467
2468 /* wait for the pending one */
2469 if (pending) {
2470 wait_for_completion(&barr.done);
2471 destroy_work_on_stack(&barr.work);
2472 }
2473
2474 return pending || waited;
2475}
2476EXPORT_SYMBOL_GPL(flush_work_sync);
2477
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002478/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002479 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002480 * so this work can't be re-armed in any way.
2481 */
2482static int try_to_grab_pending(struct work_struct *work)
2483{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002484 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002485 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002486
Tejun Heo22df02b2010-06-29 10:07:10 +02002487 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002488 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002489
2490 /*
2491 * The queueing is in progress, or it is already queued. Try to
2492 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2493 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002494 gcwq = get_work_gcwq(work);
2495 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002496 return ret;
2497
Tejun Heo8b03ae32010-06-29 10:07:12 +02002498 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002499 if (!list_empty(&work->entry)) {
2500 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002501 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002502 * In that case we must see the new value after rmb(), see
2503 * insert_work()->wmb().
2504 */
2505 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002506 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002507 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002508 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002509 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002510 get_work_color(work),
2511 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002512 ret = 1;
2513 }
2514 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002515 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002516
2517 return ret;
2518}
2519
Tejun Heo401a8d02010-09-16 10:36:00 +02002520static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002521 struct timer_list* timer)
2522{
2523 int ret;
2524
2525 do {
2526 ret = (timer && likely(del_timer(timer)));
2527 if (!ret)
2528 ret = try_to_grab_pending(work);
2529 wait_on_work(work);
2530 } while (unlikely(ret < 0));
2531
Tejun Heo7a22ad72010-06-29 10:07:13 +02002532 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002533 return ret;
2534}
2535
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002536/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002537 * cancel_work_sync - cancel a work and wait for it to finish
2538 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002539 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002540 * Cancel @work and wait for its execution to finish. This function
2541 * can be used even if the work re-queues itself or migrates to
2542 * another workqueue. On return from this function, @work is
2543 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002544 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002545 * cancel_work_sync(&delayed_work->work) must not be used for
2546 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002547 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002548 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002549 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002550 *
2551 * RETURNS:
2552 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002553 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002554bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002555{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002556 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002557}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002558EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002559
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002560/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002561 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2562 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002563 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002564 * Delayed timer is cancelled and the pending work is queued for
2565 * immediate execution. Like flush_work(), this function only
2566 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002567 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002568 * RETURNS:
2569 * %true if flush_work() waited for the work to finish execution,
2570 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002571 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002572bool flush_delayed_work(struct delayed_work *dwork)
2573{
2574 if (del_timer_sync(&dwork->timer))
2575 __queue_work(raw_smp_processor_id(),
2576 get_work_cwq(&dwork->work)->wq, &dwork->work);
2577 return flush_work(&dwork->work);
2578}
2579EXPORT_SYMBOL(flush_delayed_work);
2580
2581/**
Tejun Heo09383492010-09-16 10:48:29 +02002582 * flush_delayed_work_sync - wait for a dwork to finish
2583 * @dwork: the delayed work to flush
2584 *
2585 * Delayed timer is cancelled and the pending work is queued for
2586 * execution immediately. Other than timer handling, its behavior
2587 * is identical to flush_work_sync().
2588 *
2589 * RETURNS:
2590 * %true if flush_work_sync() waited for the work to finish execution,
2591 * %false if it was already idle.
2592 */
2593bool flush_delayed_work_sync(struct delayed_work *dwork)
2594{
2595 if (del_timer_sync(&dwork->timer))
2596 __queue_work(raw_smp_processor_id(),
2597 get_work_cwq(&dwork->work)->wq, &dwork->work);
2598 return flush_work_sync(&dwork->work);
2599}
2600EXPORT_SYMBOL(flush_delayed_work_sync);
2601
2602/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002603 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2604 * @dwork: the delayed work cancel
2605 *
2606 * This is cancel_work_sync() for delayed works.
2607 *
2608 * RETURNS:
2609 * %true if @dwork was pending, %false otherwise.
2610 */
2611bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002612{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002613 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002614}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002615EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002617/**
2618 * schedule_work - put work task in global workqueue
2619 * @work: job to be done
2620 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002621 * Returns zero if @work was already on the kernel-global workqueue and
2622 * non-zero otherwise.
2623 *
2624 * This puts a job in the kernel-global workqueue if it was not already
2625 * queued and leaves it in the same position on the kernel-global
2626 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002627 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002628int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Tejun Heod320c032010-06-29 10:07:14 +02002630 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
Dave Jonesae90dd52006-06-30 01:40:45 -04002632EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Zhang Ruic1a220e2008-07-23 21:28:39 -07002634/*
2635 * schedule_work_on - put work task on a specific cpu
2636 * @cpu: cpu to put the work task on
2637 * @work: job to be done
2638 *
2639 * This puts a job on a specific cpu
2640 */
2641int schedule_work_on(int cpu, struct work_struct *work)
2642{
Tejun Heod320c032010-06-29 10:07:14 +02002643 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002644}
2645EXPORT_SYMBOL(schedule_work_on);
2646
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002647/**
2648 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002649 * @dwork: job to be done
2650 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002651 *
2652 * After waiting for a given time this puts a job in the kernel-global
2653 * workqueue.
2654 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002655int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002656 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Tejun Heod320c032010-06-29 10:07:14 +02002658 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
Dave Jonesae90dd52006-06-30 01:40:45 -04002660EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002662/**
2663 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2664 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002665 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002666 * @delay: number of jiffies to wait
2667 *
2668 * After waiting for a given time this puts a job in the kernel-global
2669 * workqueue on the specified CPU.
2670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002672 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Tejun Heod320c032010-06-29 10:07:14 +02002674 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
Dave Jonesae90dd52006-06-30 01:40:45 -04002676EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Andrew Mortonb6136772006-06-25 05:47:49 -07002678/**
2679 * schedule_on_each_cpu - call a function on each online CPU from keventd
2680 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002681 *
2682 * Returns zero on success.
2683 * Returns -ve errno on failure.
2684 *
Andrew Mortonb6136772006-06-25 05:47:49 -07002685 * schedule_on_each_cpu() is very slow.
2686 */
David Howells65f27f32006-11-22 14:55:48 +00002687int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002688{
2689 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002690 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002691
Andrew Mortonb6136772006-06-25 05:47:49 -07002692 works = alloc_percpu(struct work_struct);
2693 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002694 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002695
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002696 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002697
Christoph Lameter15316ba2006-01-08 01:00:43 -08002698 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002699 struct work_struct *work = per_cpu_ptr(works, cpu);
2700
2701 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002702 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002703 }
Tejun Heo93981802009-11-17 14:06:20 -08002704
2705 for_each_online_cpu(cpu)
2706 flush_work(per_cpu_ptr(works, cpu));
2707
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002708 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002709 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002710 return 0;
2711}
2712
Alan Sterneef6a7d2010-02-12 17:39:21 +09002713/**
2714 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2715 *
2716 * Forces execution of the kernel-global workqueue and blocks until its
2717 * completion.
2718 *
2719 * Think twice before calling this function! It's very easy to get into
2720 * trouble if you don't take great care. Either of the following situations
2721 * will lead to deadlock:
2722 *
2723 * One of the work items currently on the workqueue needs to acquire
2724 * a lock held by your code or its caller.
2725 *
2726 * Your code is running in the context of a work routine.
2727 *
2728 * They will be detected by lockdep when they occur, but the first might not
2729 * occur very often. It depends on what work items are on the workqueue and
2730 * what locks they need, which you have no control over.
2731 *
2732 * In most situations flushing the entire workqueue is overkill; you merely
2733 * need to know that a particular work item isn't queued and isn't running.
2734 * In such cases you should use cancel_delayed_work_sync() or
2735 * cancel_work_sync() instead.
2736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737void flush_scheduled_work(void)
2738{
Tejun Heod320c032010-06-29 10:07:14 +02002739 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
Dave Jonesae90dd52006-06-30 01:40:45 -04002741EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002744 * execute_in_process_context - reliably execute the routine with user context
2745 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002746 * @ew: guaranteed storage for the execute work structure (must
2747 * be available when the work executes)
2748 *
2749 * Executes the function immediately if process context is available,
2750 * otherwise schedules the function for delayed execution.
2751 *
2752 * Returns: 0 - function was executed
2753 * 1 - function was scheduled for execution
2754 */
David Howells65f27f32006-11-22 14:55:48 +00002755int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002756{
2757 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002758 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002759 return 0;
2760 }
2761
David Howells65f27f32006-11-22 14:55:48 +00002762 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002763 schedule_work(&ew->work);
2764
2765 return 1;
2766}
2767EXPORT_SYMBOL_GPL(execute_in_process_context);
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769int keventd_up(void)
2770{
Tejun Heod320c032010-06-29 10:07:14 +02002771 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002774static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002776 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002777 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2778 * Make sure that the alignment isn't lower than that of
2779 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002780 */
Tejun Heo0f900042010-06-29 10:07:11 +02002781 const size_t size = sizeof(struct cpu_workqueue_struct);
2782 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2783 __alignof__(unsigned long long));
Tejun Heo931ac772010-07-20 11:07:48 +02002784#ifdef CONFIG_SMP
2785 bool percpu = !(wq->flags & WQ_UNBOUND);
2786#else
2787 bool percpu = false;
2788#endif
Oleg Nesterov3af244332007-05-09 02:34:09 -07002789
Tejun Heo931ac772010-07-20 11:07:48 +02002790 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002791 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002792 else {
Tejun Heof3421792010-07-02 10:03:51 +02002793 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002794
Tejun Heof3421792010-07-02 10:03:51 +02002795 /*
2796 * Allocate enough room to align cwq and put an extra
2797 * pointer at the end pointing back to the originally
2798 * allocated pointer which will be used for free.
2799 */
2800 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2801 if (ptr) {
2802 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2803 *(void **)(wq->cpu_wq.single + 1) = ptr;
2804 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002805 }
Tejun Heof3421792010-07-02 10:03:51 +02002806
Tejun Heo0f900042010-06-29 10:07:11 +02002807 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002808 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2809 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002810}
2811
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002812static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002813{
Tejun Heo931ac772010-07-20 11:07:48 +02002814#ifdef CONFIG_SMP
2815 bool percpu = !(wq->flags & WQ_UNBOUND);
2816#else
2817 bool percpu = false;
2818#endif
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002819
Tejun Heo931ac772010-07-20 11:07:48 +02002820 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002821 free_percpu(wq->cpu_wq.pcpu);
2822 else if (wq->cpu_wq.single) {
2823 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002824 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002825 }
2826}
2827
Tejun Heof3421792010-07-02 10:03:51 +02002828static int wq_clamp_max_active(int max_active, unsigned int flags,
2829 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002830{
Tejun Heof3421792010-07-02 10:03:51 +02002831 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2832
2833 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002834 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2835 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002836 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002837
Tejun Heof3421792010-07-02 10:03:51 +02002838 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002839}
2840
Tejun Heod320c032010-06-29 10:07:14 +02002841struct workqueue_struct *__alloc_workqueue_key(const char *name,
2842 unsigned int flags,
2843 int max_active,
2844 struct lock_class_key *key,
2845 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002846{
2847 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002848 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002849
Tejun Heof3421792010-07-02 10:03:51 +02002850 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002851 * Workqueues which may be used during memory reclaim should
2852 * have a rescuer to guarantee forward progress.
2853 */
2854 if (flags & WQ_MEM_RECLAIM)
2855 flags |= WQ_RESCUER;
2856
2857 /*
Tejun Heof3421792010-07-02 10:03:51 +02002858 * Unbound workqueues aren't concurrency managed and should be
2859 * dispatched to workers immediately.
2860 */
2861 if (flags & WQ_UNBOUND)
2862 flags |= WQ_HIGHPRI;
2863
Tejun Heod320c032010-06-29 10:07:14 +02002864 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heof3421792010-07-02 10:03:51 +02002865 max_active = wq_clamp_max_active(max_active, flags, name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002866
2867 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2868 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02002869 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002870
Tejun Heo97e37d72010-06-29 10:07:10 +02002871 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002872 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002873 mutex_init(&wq->flush_mutex);
2874 atomic_set(&wq->nr_cwqs_to_flush, 0);
2875 INIT_LIST_HEAD(&wq->flusher_queue);
2876 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002877
2878 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01002879 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002880 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002881
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002882 if (alloc_cwqs(wq) < 0)
2883 goto err;
2884
Tejun Heof3421792010-07-02 10:03:51 +02002885 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002886 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002887 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02002888
Tejun Heo0f900042010-06-29 10:07:11 +02002889 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002890 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002891 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002892 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002893 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002894 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002895 }
2896
Tejun Heoe22bee72010-06-29 10:07:14 +02002897 if (flags & WQ_RESCUER) {
2898 struct worker *rescuer;
2899
Tejun Heof2e005a2010-07-20 15:59:09 +02002900 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02002901 goto err;
2902
2903 wq->rescuer = rescuer = alloc_worker();
2904 if (!rescuer)
2905 goto err;
2906
2907 rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
2908 if (IS_ERR(rescuer->task))
2909 goto err;
2910
Tejun Heoe22bee72010-06-29 10:07:14 +02002911 rescuer->task->flags |= PF_THREAD_BOUND;
2912 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002913 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002914
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002915 /*
2916 * workqueue_lock protects global freeze state and workqueues
2917 * list. Grab it, set max_active accordingly and add the new
2918 * workqueue to workqueues list.
2919 */
Tejun Heo15376632010-06-29 10:07:11 +02002920 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002921
2922 if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
Tejun Heof3421792010-07-02 10:03:51 +02002923 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002924 get_cwq(cpu, wq)->max_active = 0;
2925
Tejun Heo15376632010-06-29 10:07:11 +02002926 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002927
Tejun Heo15376632010-06-29 10:07:11 +02002928 spin_unlock(&workqueue_lock);
2929
Oleg Nesterov3af244332007-05-09 02:34:09 -07002930 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02002931err:
2932 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002933 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02002934 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002935 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02002936 kfree(wq);
2937 }
2938 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002939}
Tejun Heod320c032010-06-29 10:07:14 +02002940EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002941
2942/**
2943 * destroy_workqueue - safely terminate a workqueue
2944 * @wq: target workqueue
2945 *
2946 * Safely destroy a workqueue. All work currently pending will be done first.
2947 */
2948void destroy_workqueue(struct workqueue_struct *wq)
2949{
Tejun Heoc8e55f32010-06-29 10:07:12 +02002950 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002951
Tejun Heoe41e7042010-08-24 14:22:47 +02002952 wq->flags |= WQ_DYING;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002953 flush_workqueue(wq);
2954
2955 /*
2956 * wq list is used to freeze wq, remove from list after
2957 * flushing is complete in case freeze races us.
2958 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002959 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002960 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002961 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002962
Tejun Heoe22bee72010-06-29 10:07:14 +02002963 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02002964 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002965 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2966 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002967
Tejun Heo73f53c42010-06-29 10:07:11 +02002968 for (i = 0; i < WORK_NR_COLORS; i++)
2969 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002970 BUG_ON(cwq->nr_active);
2971 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02002972 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002973
Tejun Heoe22bee72010-06-29 10:07:14 +02002974 if (wq->flags & WQ_RESCUER) {
2975 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02002976 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02002977 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002978 }
2979
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002980 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002981 kfree(wq);
2982}
2983EXPORT_SYMBOL_GPL(destroy_workqueue);
2984
Tejun Heodcd989c2010-06-29 10:07:14 +02002985/**
2986 * workqueue_set_max_active - adjust max_active of a workqueue
2987 * @wq: target workqueue
2988 * @max_active: new max_active value.
2989 *
2990 * Set max_active of @wq to @max_active.
2991 *
2992 * CONTEXT:
2993 * Don't call from IRQ context.
2994 */
2995void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
2996{
2997 unsigned int cpu;
2998
Tejun Heof3421792010-07-02 10:03:51 +02002999 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003000
3001 spin_lock(&workqueue_lock);
3002
3003 wq->saved_max_active = max_active;
3004
Tejun Heof3421792010-07-02 10:03:51 +02003005 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003006 struct global_cwq *gcwq = get_gcwq(cpu);
3007
3008 spin_lock_irq(&gcwq->lock);
3009
3010 if (!(wq->flags & WQ_FREEZEABLE) ||
3011 !(gcwq->flags & GCWQ_FREEZING))
3012 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3013
3014 spin_unlock_irq(&gcwq->lock);
3015 }
3016
3017 spin_unlock(&workqueue_lock);
3018}
3019EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3020
3021/**
3022 * workqueue_congested - test whether a workqueue is congested
3023 * @cpu: CPU in question
3024 * @wq: target workqueue
3025 *
3026 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3027 * no synchronization around this function and the test result is
3028 * unreliable and only useful as advisory hints or for debugging.
3029 *
3030 * RETURNS:
3031 * %true if congested, %false otherwise.
3032 */
3033bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3034{
3035 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3036
3037 return !list_empty(&cwq->delayed_works);
3038}
3039EXPORT_SYMBOL_GPL(workqueue_congested);
3040
3041/**
3042 * work_cpu - return the last known associated cpu for @work
3043 * @work: the work of interest
3044 *
3045 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003046 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003047 */
3048unsigned int work_cpu(struct work_struct *work)
3049{
3050 struct global_cwq *gcwq = get_work_gcwq(work);
3051
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003052 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003053}
3054EXPORT_SYMBOL_GPL(work_cpu);
3055
3056/**
3057 * work_busy - test whether a work is currently pending or running
3058 * @work: the work to be tested
3059 *
3060 * Test whether @work is currently pending or running. There is no
3061 * synchronization around this function and the test result is
3062 * unreliable and only useful as advisory hints or for debugging.
3063 * Especially for reentrant wqs, the pending state might hide the
3064 * running state.
3065 *
3066 * RETURNS:
3067 * OR'd bitmask of WORK_BUSY_* bits.
3068 */
3069unsigned int work_busy(struct work_struct *work)
3070{
3071 struct global_cwq *gcwq = get_work_gcwq(work);
3072 unsigned long flags;
3073 unsigned int ret = 0;
3074
3075 if (!gcwq)
3076 return false;
3077
3078 spin_lock_irqsave(&gcwq->lock, flags);
3079
3080 if (work_pending(work))
3081 ret |= WORK_BUSY_PENDING;
3082 if (find_worker_executing_work(gcwq, work))
3083 ret |= WORK_BUSY_RUNNING;
3084
3085 spin_unlock_irqrestore(&gcwq->lock, flags);
3086
3087 return ret;
3088}
3089EXPORT_SYMBOL_GPL(work_busy);
3090
Tejun Heodb7bccf2010-06-29 10:07:12 +02003091/*
3092 * CPU hotplug.
3093 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003094 * There are two challenges in supporting CPU hotplug. Firstly, there
3095 * are a lot of assumptions on strong associations among work, cwq and
3096 * gcwq which make migrating pending and scheduled works very
3097 * difficult to implement without impacting hot paths. Secondly,
3098 * gcwqs serve mix of short, long and very long running works making
3099 * blocked draining impractical.
3100 *
3101 * This is solved by allowing a gcwq to be detached from CPU, running
3102 * it with unbound (rogue) workers and allowing it to be reattached
3103 * later if the cpu comes back online. A separate thread is created
3104 * to govern a gcwq in such state and is called the trustee of the
3105 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003106 *
3107 * Trustee states and their descriptions.
3108 *
3109 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3110 * new trustee is started with this state.
3111 *
3112 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003113 * assuming the manager role and making all existing
3114 * workers rogue. DOWN_PREPARE waits for trustee to
3115 * enter this state. After reaching IN_CHARGE, trustee
3116 * tries to execute the pending worklist until it's empty
3117 * and the state is set to BUTCHER, or the state is set
3118 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003119 *
3120 * BUTCHER Command state which is set by the cpu callback after
3121 * the cpu has went down. Once this state is set trustee
3122 * knows that there will be no new works on the worklist
3123 * and once the worklist is empty it can proceed to
3124 * killing idle workers.
3125 *
3126 * RELEASE Command state which is set by the cpu callback if the
3127 * cpu down has been canceled or it has come online
3128 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003129 * trying to drain or butcher and clears ROGUE, rebinds
3130 * all remaining workers back to the cpu and releases
3131 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003132 *
3133 * DONE Trustee will enter this state after BUTCHER or RELEASE
3134 * is complete.
3135 *
3136 * trustee CPU draining
3137 * took over down complete
3138 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3139 * | | ^
3140 * | CPU is back online v return workers |
3141 * ----------------> RELEASE --------------
3142 */
3143
3144/**
3145 * trustee_wait_event_timeout - timed event wait for trustee
3146 * @cond: condition to wait for
3147 * @timeout: timeout in jiffies
3148 *
3149 * wait_event_timeout() for trustee to use. Handles locking and
3150 * checks for RELEASE request.
3151 *
3152 * CONTEXT:
3153 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3154 * multiple times. To be used by trustee.
3155 *
3156 * RETURNS:
3157 * Positive indicating left time if @cond is satisfied, 0 if timed
3158 * out, -1 if canceled.
3159 */
3160#define trustee_wait_event_timeout(cond, timeout) ({ \
3161 long __ret = (timeout); \
3162 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3163 __ret) { \
3164 spin_unlock_irq(&gcwq->lock); \
3165 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3166 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3167 __ret); \
3168 spin_lock_irq(&gcwq->lock); \
3169 } \
3170 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3171})
3172
3173/**
3174 * trustee_wait_event - event wait for trustee
3175 * @cond: condition to wait for
3176 *
3177 * wait_event() for trustee to use. Automatically handles locking and
3178 * checks for CANCEL request.
3179 *
3180 * CONTEXT:
3181 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3182 * multiple times. To be used by trustee.
3183 *
3184 * RETURNS:
3185 * 0 if @cond is satisfied, -1 if canceled.
3186 */
3187#define trustee_wait_event(cond) ({ \
3188 long __ret1; \
3189 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3190 __ret1 < 0 ? -1 : 0; \
3191})
3192
3193static int __cpuinit trustee_thread(void *__gcwq)
3194{
3195 struct global_cwq *gcwq = __gcwq;
3196 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003197 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003198 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003199 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003200 int i;
3201
3202 BUG_ON(gcwq->cpu != smp_processor_id());
3203
3204 spin_lock_irq(&gcwq->lock);
3205 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003206 * Claim the manager position and make all workers rogue.
3207 * Trustee must be bound to the target cpu and can't be
3208 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003209 */
3210 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003211 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3212 BUG_ON(rc < 0);
3213
3214 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003215
3216 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003217 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003218
3219 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003220 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003221
3222 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003223 * Call schedule() so that we cross rq->lock and thus can
3224 * guarantee sched callbacks see the rogue flag. This is
3225 * necessary as scheduler callbacks may be invoked from other
3226 * cpus.
3227 */
3228 spin_unlock_irq(&gcwq->lock);
3229 schedule();
3230 spin_lock_irq(&gcwq->lock);
3231
3232 /*
Tejun Heocb444762010-07-02 10:03:50 +02003233 * Sched callbacks are disabled now. Zap nr_running. After
3234 * this, nr_running stays zero and need_more_worker() and
3235 * keep_working() are always true as long as the worklist is
3236 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003237 */
Tejun Heocb444762010-07-02 10:03:50 +02003238 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003239
3240 spin_unlock_irq(&gcwq->lock);
3241 del_timer_sync(&gcwq->idle_timer);
3242 spin_lock_irq(&gcwq->lock);
3243
3244 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003245 * We're now in charge. Notify and proceed to drain. We need
3246 * to keep the gcwq running during the whole CPU down
3247 * procedure as other cpu hotunplug callbacks may need to
3248 * flush currently running tasks.
3249 */
3250 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3251 wake_up_all(&gcwq->trustee_wait);
3252
3253 /*
3254 * The original cpu is in the process of dying and may go away
3255 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003256 * be migrated to other cpus. Try draining any left work. We
3257 * want to get it over with ASAP - spam rescuers, wake up as
3258 * many idlers as necessary and create new ones till the
3259 * worklist is empty. Note that if the gcwq is frozen, there
3260 * may be frozen works in freezeable cwqs. Don't declare
3261 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003262 */
3263 while (gcwq->nr_workers != gcwq->nr_idle ||
3264 gcwq->flags & GCWQ_FREEZING ||
3265 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003266 int nr_works = 0;
3267
3268 list_for_each_entry(work, &gcwq->worklist, entry) {
3269 send_mayday(work);
3270 nr_works++;
3271 }
3272
3273 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3274 if (!nr_works--)
3275 break;
3276 wake_up_process(worker->task);
3277 }
3278
3279 if (need_to_create_worker(gcwq)) {
3280 spin_unlock_irq(&gcwq->lock);
3281 worker = create_worker(gcwq, false);
3282 spin_lock_irq(&gcwq->lock);
3283 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003284 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003285 start_worker(worker);
3286 }
3287 }
3288
Tejun Heodb7bccf2010-06-29 10:07:12 +02003289 /* give a breather */
3290 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3291 break;
3292 }
3293
Tejun Heoe22bee72010-06-29 10:07:14 +02003294 /*
3295 * Either all works have been scheduled and cpu is down, or
3296 * cpu down has already been canceled. Wait for and butcher
3297 * all workers till we're canceled.
3298 */
3299 do {
3300 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3301 while (!list_empty(&gcwq->idle_list))
3302 destroy_worker(list_first_entry(&gcwq->idle_list,
3303 struct worker, entry));
3304 } while (gcwq->nr_workers && rc >= 0);
3305
3306 /*
3307 * At this point, either draining has completed and no worker
3308 * is left, or cpu down has been canceled or the cpu is being
3309 * brought back up. There shouldn't be any idle one left.
3310 * Tell the remaining busy ones to rebind once it finishes the
3311 * currently scheduled works by scheduling the rebind_work.
3312 */
3313 WARN_ON(!list_empty(&gcwq->idle_list));
3314
3315 for_each_busy_worker(worker, i, pos, gcwq) {
3316 struct work_struct *rebind_work = &worker->rebind_work;
3317
3318 /*
3319 * Rebind_work may race with future cpu hotplug
3320 * operations. Use a separate flag to mark that
3321 * rebinding is scheduled.
3322 */
Tejun Heocb444762010-07-02 10:03:50 +02003323 worker->flags |= WORKER_REBIND;
3324 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003325
3326 /* queue rebind_work, wq doesn't matter, use the default one */
3327 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3328 work_data_bits(rebind_work)))
3329 continue;
3330
3331 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003332 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003333 worker->scheduled.next,
3334 work_color_to_flags(WORK_NO_COLOR));
3335 }
3336
3337 /* relinquish manager role */
3338 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3339
Tejun Heodb7bccf2010-06-29 10:07:12 +02003340 /* notify completion */
3341 gcwq->trustee = NULL;
3342 gcwq->trustee_state = TRUSTEE_DONE;
3343 wake_up_all(&gcwq->trustee_wait);
3344 spin_unlock_irq(&gcwq->lock);
3345 return 0;
3346}
3347
3348/**
3349 * wait_trustee_state - wait for trustee to enter the specified state
3350 * @gcwq: gcwq the trustee of interest belongs to
3351 * @state: target state to wait for
3352 *
3353 * Wait for the trustee to reach @state. DONE is already matched.
3354 *
3355 * CONTEXT:
3356 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3357 * multiple times. To be used by cpu_callback.
3358 */
3359static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003360__releases(&gcwq->lock)
3361__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003362{
3363 if (!(gcwq->trustee_state == state ||
3364 gcwq->trustee_state == TRUSTEE_DONE)) {
3365 spin_unlock_irq(&gcwq->lock);
3366 __wait_event(gcwq->trustee_wait,
3367 gcwq->trustee_state == state ||
3368 gcwq->trustee_state == TRUSTEE_DONE);
3369 spin_lock_irq(&gcwq->lock);
3370 }
3371}
3372
Oleg Nesterov3af244332007-05-09 02:34:09 -07003373static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3374 unsigned long action,
3375 void *hcpu)
3376{
3377 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003378 struct global_cwq *gcwq = get_gcwq(cpu);
3379 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003380 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003381 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003383 action &= ~CPU_TASKS_FROZEN;
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003386 case CPU_DOWN_PREPARE:
3387 new_trustee = kthread_create(trustee_thread, gcwq,
3388 "workqueue_trustee/%d\n", cpu);
3389 if (IS_ERR(new_trustee))
3390 return notifier_from_errno(PTR_ERR(new_trustee));
3391 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003392 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003394 BUG_ON(gcwq->first_idle);
3395 new_worker = create_worker(gcwq, false);
3396 if (!new_worker) {
3397 if (new_trustee)
3398 kthread_stop(new_trustee);
3399 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
3402
Tejun Heodb7bccf2010-06-29 10:07:12 +02003403 /* some are called w/ irq disabled, don't disturb irq status */
3404 spin_lock_irqsave(&gcwq->lock, flags);
3405
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003406 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003407 case CPU_DOWN_PREPARE:
3408 /* initialize trustee and tell it to acquire the gcwq */
3409 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3410 gcwq->trustee = new_trustee;
3411 gcwq->trustee_state = TRUSTEE_START;
3412 wake_up_process(gcwq->trustee);
3413 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003414 /* fall through */
3415 case CPU_UP_PREPARE:
3416 BUG_ON(gcwq->first_idle);
3417 gcwq->first_idle = new_worker;
3418 break;
3419
3420 case CPU_DYING:
3421 /*
3422 * Before this, the trustee and all workers except for
3423 * the ones which are still executing works from
3424 * before the last CPU down must be on the cpu. After
3425 * this, they'll all be diasporas.
3426 */
3427 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003428 break;
3429
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003430 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003431 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003432 /* fall through */
3433 case CPU_UP_CANCELED:
3434 destroy_worker(gcwq->first_idle);
3435 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003436 break;
3437
3438 case CPU_DOWN_FAILED:
3439 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003440 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003441 if (gcwq->trustee_state != TRUSTEE_DONE) {
3442 gcwq->trustee_state = TRUSTEE_RELEASE;
3443 wake_up_process(gcwq->trustee);
3444 wait_trustee_state(gcwq, TRUSTEE_DONE);
3445 }
3446
Tejun Heoe22bee72010-06-29 10:07:14 +02003447 /*
3448 * Trustee is done and there might be no worker left.
3449 * Put the first_idle in and request a real manager to
3450 * take a look.
3451 */
3452 spin_unlock_irq(&gcwq->lock);
3453 kthread_bind(gcwq->first_idle->task, cpu);
3454 spin_lock_irq(&gcwq->lock);
3455 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3456 start_worker(gcwq->first_idle);
3457 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003458 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003459 }
3460
Tejun Heodb7bccf2010-06-29 10:07:12 +02003461 spin_unlock_irqrestore(&gcwq->lock, flags);
3462
Tejun Heo15376632010-06-29 10:07:11 +02003463 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Rusty Russell2d3854a2008-11-05 13:39:10 +11003466#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003467
Rusty Russell2d3854a2008-11-05 13:39:10 +11003468struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06003469 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003470 long (*fn)(void *);
3471 void *arg;
3472 long ret;
3473};
3474
Andrew Morton6b44003e2009-04-09 09:50:37 -06003475static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003476{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003477 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003478 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06003479 complete(&wfc->completion);
3480 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003481}
3482
3483/**
3484 * work_on_cpu - run a function in user context on a particular cpu
3485 * @cpu: the cpu to run on
3486 * @fn: the function to run
3487 * @arg: the function arg
3488 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003489 * This will return the value @fn returns.
3490 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003491 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003492 */
3493long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3494{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003495 struct task_struct *sub_thread;
3496 struct work_for_cpu wfc = {
3497 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3498 .fn = fn,
3499 .arg = arg,
3500 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003501
Andrew Morton6b44003e2009-04-09 09:50:37 -06003502 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3503 if (IS_ERR(sub_thread))
3504 return PTR_ERR(sub_thread);
3505 kthread_bind(sub_thread, cpu);
3506 wake_up_process(sub_thread);
3507 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003508 return wfc.ret;
3509}
3510EXPORT_SYMBOL_GPL(work_on_cpu);
3511#endif /* CONFIG_SMP */
3512
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003513#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303514
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003515/**
3516 * freeze_workqueues_begin - begin freezing workqueues
3517 *
3518 * Start freezing workqueues. After this function returns, all
3519 * freezeable workqueues will queue new works to their frozen_works
Tejun Heo7e116292010-06-29 10:07:13 +02003520 * list instead of gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003521 *
3522 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003523 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003524 */
3525void freeze_workqueues_begin(void)
3526{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003527 unsigned int cpu;
3528
3529 spin_lock(&workqueue_lock);
3530
3531 BUG_ON(workqueue_freezing);
3532 workqueue_freezing = true;
3533
Tejun Heof3421792010-07-02 10:03:51 +02003534 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003535 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003536 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003537
3538 spin_lock_irq(&gcwq->lock);
3539
Tejun Heodb7bccf2010-06-29 10:07:12 +02003540 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3541 gcwq->flags |= GCWQ_FREEZING;
3542
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003543 list_for_each_entry(wq, &workqueues, list) {
3544 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3545
Tejun Heof3421792010-07-02 10:03:51 +02003546 if (cwq && wq->flags & WQ_FREEZEABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003547 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003548 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003549
3550 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003551 }
3552
3553 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003555
3556/**
3557 * freeze_workqueues_busy - are freezeable workqueues still busy?
3558 *
3559 * Check whether freezing is complete. This function must be called
3560 * between freeze_workqueues_begin() and thaw_workqueues().
3561 *
3562 * CONTEXT:
3563 * Grabs and releases workqueue_lock.
3564 *
3565 * RETURNS:
3566 * %true if some freezeable workqueues are still busy. %false if
3567 * freezing is complete.
3568 */
3569bool freeze_workqueues_busy(void)
3570{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003571 unsigned int cpu;
3572 bool busy = false;
3573
3574 spin_lock(&workqueue_lock);
3575
3576 BUG_ON(!workqueue_freezing);
3577
Tejun Heof3421792010-07-02 10:03:51 +02003578 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003579 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003580 /*
3581 * nr_active is monotonically decreasing. It's safe
3582 * to peek without lock.
3583 */
3584 list_for_each_entry(wq, &workqueues, list) {
3585 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3586
Tejun Heof3421792010-07-02 10:03:51 +02003587 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003588 continue;
3589
3590 BUG_ON(cwq->nr_active < 0);
3591 if (cwq->nr_active) {
3592 busy = true;
3593 goto out_unlock;
3594 }
3595 }
3596 }
3597out_unlock:
3598 spin_unlock(&workqueue_lock);
3599 return busy;
3600}
3601
3602/**
3603 * thaw_workqueues - thaw workqueues
3604 *
3605 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003606 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003607 *
3608 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003609 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003610 */
3611void thaw_workqueues(void)
3612{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003613 unsigned int cpu;
3614
3615 spin_lock(&workqueue_lock);
3616
3617 if (!workqueue_freezing)
3618 goto out_unlock;
3619
Tejun Heof3421792010-07-02 10:03:51 +02003620 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003621 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003622 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003623
3624 spin_lock_irq(&gcwq->lock);
3625
Tejun Heodb7bccf2010-06-29 10:07:12 +02003626 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3627 gcwq->flags &= ~GCWQ_FREEZING;
3628
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003629 list_for_each_entry(wq, &workqueues, list) {
3630 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3631
Tejun Heof3421792010-07-02 10:03:51 +02003632 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003633 continue;
3634
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003635 /* restore max_active and repopulate worklist */
3636 cwq->max_active = wq->saved_max_active;
3637
3638 while (!list_empty(&cwq->delayed_works) &&
3639 cwq->nr_active < cwq->max_active)
3640 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003641 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003642
Tejun Heoe22bee72010-06-29 10:07:14 +02003643 wake_up_worker(gcwq);
3644
Tejun Heo8b03ae32010-06-29 10:07:12 +02003645 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003646 }
3647
3648 workqueue_freezing = false;
3649out_unlock:
3650 spin_unlock(&workqueue_lock);
3651}
3652#endif /* CONFIG_FREEZER */
3653
Suresh Siddha6ee05782010-07-30 14:57:37 -07003654static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
Tejun Heoc34056a2010-06-29 10:07:11 +02003656 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003657 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003658
Tejun Heof6500942010-08-09 11:50:34 +02003659 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003660
3661 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003662 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003663 struct global_cwq *gcwq = get_gcwq(cpu);
3664
3665 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003666 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003667 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003668 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003669
Tejun Heoc8e55f32010-06-29 10:07:12 +02003670 INIT_LIST_HEAD(&gcwq->idle_list);
3671 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3672 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3673
Tejun Heoe22bee72010-06-29 10:07:14 +02003674 init_timer_deferrable(&gcwq->idle_timer);
3675 gcwq->idle_timer.function = idle_worker_timeout;
3676 gcwq->idle_timer.data = (unsigned long)gcwq;
3677
3678 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3679 (unsigned long)gcwq);
3680
Tejun Heo8b03ae32010-06-29 10:07:12 +02003681 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003682
3683 gcwq->trustee_state = TRUSTEE_DONE;
3684 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003685 }
3686
Tejun Heoe22bee72010-06-29 10:07:14 +02003687 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003688 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003689 struct global_cwq *gcwq = get_gcwq(cpu);
3690 struct worker *worker;
3691
Tejun Heo477a3c32010-08-31 10:54:35 +02003692 if (cpu != WORK_CPU_UNBOUND)
3693 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003694 worker = create_worker(gcwq, true);
3695 BUG_ON(!worker);
3696 spin_lock_irq(&gcwq->lock);
3697 start_worker(worker);
3698 spin_unlock_irq(&gcwq->lock);
3699 }
3700
Tejun Heod320c032010-06-29 10:07:14 +02003701 system_wq = alloc_workqueue("events", 0, 0);
3702 system_long_wq = alloc_workqueue("events_long", 0, 0);
3703 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003704 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3705 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heod320c032010-06-29 10:07:14 +02003706 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003709early_initcall(init_workqueues);