blob: 2d82f7b193a0691e0f298a36a6f912f026615e73 [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
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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 Lameter46934022006-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
Tejun Heo3233cdb2011-02-16 18:10:19 +010082 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
83 /* call for help after 10ms
84 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
86 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020087 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020088
89 /*
90 * Rescue workers are used only on emergencies and shared by
91 * all cpus. Give -20.
92 */
93 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020094};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020097 * Structure fields follow one of the following exclusion rules.
98 *
Tejun Heoe41e7042010-08-24 14:22:47 +020099 * I: Modifiable by initialization/destruction paths and read-only for
100 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200101 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200102 * P: Preemption protected. Disabling preemption is enough and should
103 * only be modified and accessed from the local cpu.
104 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200105 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200107 * X: During normal operation, modification requires gcwq->lock and
108 * should be done only from local cpu. Either disabling preemption
109 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200110 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200112 * F: wq->flush_mutex protected.
113 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 * W: workqueue_lock protected.
115 */
116
Tejun Heo8b03ae32010-06-29 10:07:12 +0200117struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700118struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200119
Tejun Heoe22bee72010-06-29 10:07:14 +0200120/*
121 * The poor guys doing the actual heavy lifting. All on-duty workers
122 * are either serving the manager role, on idle list or on busy hash.
123 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200125 /* on idle list while idle, on busy hash table while busy */
126 union {
127 struct list_head entry; /* L: while idle */
128 struct hlist_node hentry; /* L: while busy */
129 };
130
Tejun Heoc34056a2010-06-29 10:07:11 +0200131 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200132 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200133 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200134 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200136 /* 64 bytes boundary on 64bit, 32 on 32bit */
137 unsigned long last_active; /* L: last active timestamp */
138 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200140 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200141};
142
Tejun Heobd7bdd42012-07-12 14:46:37 -0700143struct worker_pool {
144 struct global_cwq *gcwq; /* I: the owning gcwq */
145
146 struct list_head worklist; /* L: list of pending works */
147 int nr_workers; /* L: total number of workers */
148 int nr_idle; /* L: currently idle ones */
149
150 struct list_head idle_list; /* X: list of idle workers */
151 struct timer_list idle_timer; /* L: worker idle timeout */
152 struct timer_list mayday_timer; /* L: SOS timer for workers */
153
154 struct ida worker_ida; /* L: for worker IDs */
155 struct worker *first_idle; /* L: first idle worker */
156};
157
Tejun Heo4690c4a2010-06-29 10:07:10 +0200158/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200159 * Global per-cpu workqueue. There's one and only one for each cpu
160 * and all works are queued and processed here regardless of their
161 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200162 */
163struct global_cwq {
164 spinlock_t lock; /* the gcwq lock */
165 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200166 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200167
Tejun Heobd7bdd42012-07-12 14:46:37 -0700168 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200169 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
170 /* L: hash of busy workers */
171
Tejun Heobd7bdd42012-07-12 14:46:37 -0700172 struct worker_pool pool; /* the worker pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200173
174 struct task_struct *trustee; /* L: for gcwq shutdown */
175 unsigned int trustee_state; /* L: trustee state */
176 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200177} ____cacheline_aligned_in_smp;
178
179/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200180 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200181 * work_struct->data are used for flags and thus cwqs need to be
182 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700185 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200186 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200187 int work_color; /* L: current color */
188 int flush_color; /* L: flushing color */
189 int nr_in_flight[WORK_NR_COLORS];
190 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200191 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200192 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200193 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200197 * Structure used to wait for workqueue flush.
198 */
199struct wq_flusher {
200 struct list_head list; /* F: list of flushers */
201 int flush_color; /* F: flush color waiting for */
202 struct completion done; /* flush completion */
203};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tejun Heo73f53c42010-06-29 10:07:11 +0200205/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200206 * All cpumasks are assumed to be always set on UP and thus can't be
207 * used to determine whether there's something to be done.
208 */
209#ifdef CONFIG_SMP
210typedef cpumask_var_t mayday_mask_t;
211#define mayday_test_and_set_cpu(cpu, mask) \
212 cpumask_test_and_set_cpu((cpu), (mask))
213#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
214#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200215#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200216#define free_mayday_mask(mask) free_cpumask_var((mask))
217#else
218typedef unsigned long mayday_mask_t;
219#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
220#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
221#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
222#define alloc_mayday_mask(maskp, gfp) true
223#define free_mayday_mask(mask) do { } while (0)
224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/*
227 * The externally visible workqueue abstraction is an array of
228 * per-CPU workqueues:
229 */
230struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200231 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200232 union {
233 struct cpu_workqueue_struct __percpu *pcpu;
234 struct cpu_workqueue_struct *single;
235 unsigned long v;
236 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200237 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200238
239 struct mutex flush_mutex; /* protects wq flushing */
240 int work_color; /* F: current work color */
241 int flush_color; /* F: current flush color */
242 atomic_t nr_cwqs_to_flush; /* flush in progress */
243 struct wq_flusher *first_flusher; /* F: first flusher */
244 struct list_head flusher_queue; /* F: flush waiters */
245 struct list_head flusher_overflow; /* F: flush overflow list */
246
Tejun Heof2e005a2010-07-20 15:59:09 +0200247 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200248 struct worker *rescuer; /* I: rescue worker */
249
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200250 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200251 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700252#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200253 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700254#endif
Tejun Heob196be82012-01-10 15:11:35 -0800255 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Tejun Heod320c032010-06-29 10:07:14 +0200258struct workqueue_struct *system_wq __read_mostly;
259struct workqueue_struct *system_long_wq __read_mostly;
260struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200261struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100262struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100263struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200264EXPORT_SYMBOL_GPL(system_wq);
265EXPORT_SYMBOL_GPL(system_long_wq);
266EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200267EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100268EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100269EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200270
Tejun Heo97bd2342010-10-05 10:41:14 +0200271#define CREATE_TRACE_POINTS
272#include <trace/events/workqueue.h>
273
Tejun Heodb7bccf2010-06-29 10:07:12 +0200274#define for_each_busy_worker(worker, i, pos, gcwq) \
275 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
276 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
277
Tejun Heof3421792010-07-02 10:03:51 +0200278static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
279 unsigned int sw)
280{
281 if (cpu < nr_cpu_ids) {
282 if (sw & 1) {
283 cpu = cpumask_next(cpu, mask);
284 if (cpu < nr_cpu_ids)
285 return cpu;
286 }
287 if (sw & 2)
288 return WORK_CPU_UNBOUND;
289 }
290 return WORK_CPU_NONE;
291}
292
293static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
294 struct workqueue_struct *wq)
295{
296 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
297}
298
Tejun Heo09884952010-08-01 11:50:12 +0200299/*
300 * CPU iterators
301 *
302 * An extra gcwq is defined for an invalid cpu number
303 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
304 * specific CPU. The following iterators are similar to
305 * for_each_*_cpu() iterators but also considers the unbound gcwq.
306 *
307 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
308 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
309 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
310 * WORK_CPU_UNBOUND for unbound workqueues
311 */
Tejun Heof3421792010-07-02 10:03:51 +0200312#define for_each_gcwq_cpu(cpu) \
313 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
314 (cpu) < WORK_CPU_NONE; \
315 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
316
317#define for_each_online_gcwq_cpu(cpu) \
318 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
319 (cpu) < WORK_CPU_NONE; \
320 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
321
322#define for_each_cwq_cpu(cpu, wq) \
323 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
324 (cpu) < WORK_CPU_NONE; \
325 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
326
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900327#ifdef CONFIG_DEBUG_OBJECTS_WORK
328
329static struct debug_obj_descr work_debug_descr;
330
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100331static void *work_debug_hint(void *addr)
332{
333 return ((struct work_struct *) addr)->func;
334}
335
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900336/*
337 * fixup_init is called when:
338 * - an active object is initialized
339 */
340static int work_fixup_init(void *addr, enum debug_obj_state state)
341{
342 struct work_struct *work = addr;
343
344 switch (state) {
345 case ODEBUG_STATE_ACTIVE:
346 cancel_work_sync(work);
347 debug_object_init(work, &work_debug_descr);
348 return 1;
349 default:
350 return 0;
351 }
352}
353
354/*
355 * fixup_activate is called when:
356 * - an active object is activated
357 * - an unknown object is activated (might be a statically initialized object)
358 */
359static int work_fixup_activate(void *addr, enum debug_obj_state state)
360{
361 struct work_struct *work = addr;
362
363 switch (state) {
364
365 case ODEBUG_STATE_NOTAVAILABLE:
366 /*
367 * This is not really a fixup. The work struct was
368 * statically initialized. We just make sure that it
369 * is tracked in the object tracker.
370 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200371 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900372 debug_object_init(work, &work_debug_descr);
373 debug_object_activate(work, &work_debug_descr);
374 return 0;
375 }
376 WARN_ON_ONCE(1);
377 return 0;
378
379 case ODEBUG_STATE_ACTIVE:
380 WARN_ON(1);
381
382 default:
383 return 0;
384 }
385}
386
387/*
388 * fixup_free is called when:
389 * - an active object is freed
390 */
391static int work_fixup_free(void *addr, enum debug_obj_state state)
392{
393 struct work_struct *work = addr;
394
395 switch (state) {
396 case ODEBUG_STATE_ACTIVE:
397 cancel_work_sync(work);
398 debug_object_free(work, &work_debug_descr);
399 return 1;
400 default:
401 return 0;
402 }
403}
404
405static struct debug_obj_descr work_debug_descr = {
406 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100407 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900408 .fixup_init = work_fixup_init,
409 .fixup_activate = work_fixup_activate,
410 .fixup_free = work_fixup_free,
411};
412
413static inline void debug_work_activate(struct work_struct *work)
414{
415 debug_object_activate(work, &work_debug_descr);
416}
417
418static inline void debug_work_deactivate(struct work_struct *work)
419{
420 debug_object_deactivate(work, &work_debug_descr);
421}
422
423void __init_work(struct work_struct *work, int onstack)
424{
425 if (onstack)
426 debug_object_init_on_stack(work, &work_debug_descr);
427 else
428 debug_object_init(work, &work_debug_descr);
429}
430EXPORT_SYMBOL_GPL(__init_work);
431
432void destroy_work_on_stack(struct work_struct *work)
433{
434 debug_object_free(work, &work_debug_descr);
435}
436EXPORT_SYMBOL_GPL(destroy_work_on_stack);
437
438#else
439static inline void debug_work_activate(struct work_struct *work) { }
440static inline void debug_work_deactivate(struct work_struct *work) { }
441#endif
442
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100443/* Serializes the accesses to the list of workqueues. */
444static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200446static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Oleg Nesterov14441962007-05-23 13:57:57 -0700448/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200449 * The almighty global cpu workqueues. nr_running is the only field
450 * which is expected to be used frequently by other cpus via
451 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700452 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200453static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200454static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800455
Tejun Heof3421792010-07-02 10:03:51 +0200456/*
457 * Global cpu workqueue and nr_running counter for unbound gcwq. The
458 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
459 * workers have WORKER_UNBOUND set.
460 */
461static struct global_cwq unbound_global_cwq;
462static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
463
Tejun Heoc34056a2010-06-29 10:07:11 +0200464static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Tejun Heo8b03ae32010-06-29 10:07:12 +0200466static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Tejun Heof3421792010-07-02 10:03:51 +0200468 if (cpu != WORK_CPU_UNBOUND)
469 return &per_cpu(global_cwq, cpu);
470 else
471 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Tejun Heo63d95a92012-07-12 14:46:37 -0700474static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700475{
Tejun Heo63d95a92012-07-12 14:46:37 -0700476 int cpu = pool->gcwq->cpu;
477
Tejun Heof3421792010-07-02 10:03:51 +0200478 if (cpu != WORK_CPU_UNBOUND)
479 return &per_cpu(gcwq_nr_running, cpu);
480 else
481 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700482}
483
Tejun Heo4690c4a2010-06-29 10:07:10 +0200484static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
485 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700486{
Tejun Heof3421792010-07-02 10:03:51 +0200487 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800488 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200489 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200490 } else if (likely(cpu == WORK_CPU_UNBOUND))
491 return wq->cpu_wq.single;
492 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700493}
494
Tejun Heo73f53c42010-06-29 10:07:11 +0200495static unsigned int work_color_to_flags(int color)
496{
497 return color << WORK_STRUCT_COLOR_SHIFT;
498}
499
500static int get_work_color(struct work_struct *work)
501{
502 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
503 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
504}
505
506static int work_next_color(int color)
507{
508 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
David Howells4594bf12006-12-07 11:33:26 +0000511/*
Tejun Heoe1201532010-07-22 14:14:25 +0200512 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
513 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
514 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200515 *
516 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
517 * cwq, cpu or clear work->data. These functions should only be
518 * called while the work is owned - ie. while the PENDING bit is set.
519 *
520 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
521 * corresponding to a work. gcwq is available once the work has been
522 * queued anywhere after initialization. cwq is available only from
523 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000524 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200525static inline void set_work_data(struct work_struct *work, unsigned long data,
526 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000527{
David Howells4594bf12006-12-07 11:33:26 +0000528 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000530}
David Howells365970a2006-11-22 14:54:49 +0000531
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532static void set_work_cwq(struct work_struct *work,
533 struct cpu_workqueue_struct *cwq,
534 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200535{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200536 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200537 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200538}
539
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000541{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
543}
544
545static void clear_work_data(struct work_struct *work)
546{
547 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
548}
549
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
551{
Tejun Heoe1201532010-07-22 14:14:25 +0200552 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553
Tejun Heoe1201532010-07-22 14:14:25 +0200554 if (data & WORK_STRUCT_CWQ)
555 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
556 else
557 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558}
559
560static struct global_cwq *get_work_gcwq(struct work_struct *work)
561{
Tejun Heoe1201532010-07-22 14:14:25 +0200562 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200563 unsigned int cpu;
564
Tejun Heoe1201532010-07-22 14:14:25 +0200565 if (data & WORK_STRUCT_CWQ)
566 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700567 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568
569 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200570 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571 return NULL;
572
Tejun Heof3421792010-07-02 10:03:51 +0200573 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000575}
576
577/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200578 * Policy functions. These define the policies on how the global
579 * worker pool is managed. Unless noted otherwise, these functions
580 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000581 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200582
Tejun Heo63d95a92012-07-12 14:46:37 -0700583static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000584{
Tejun Heo63d95a92012-07-12 14:46:37 -0700585 return !atomic_read(get_pool_nr_running(pool)) ||
586 pool->gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000587}
588
Tejun Heoe22bee72010-06-29 10:07:14 +0200589/*
590 * Need to wake up a worker? Called from anything but currently
591 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700592 *
593 * Note that, because unbound workers never contribute to nr_running, this
594 * function will always return %true for unbound gcwq as long as the
595 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200596 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700597static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000598{
Tejun Heo63d95a92012-07-12 14:46:37 -0700599 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000600}
601
Tejun Heoe22bee72010-06-29 10:07:14 +0200602/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700603static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200604{
Tejun Heo63d95a92012-07-12 14:46:37 -0700605 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200606}
607
608/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700609static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200610{
Tejun Heo63d95a92012-07-12 14:46:37 -0700611 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200612
Tejun Heo63d95a92012-07-12 14:46:37 -0700613 return !list_empty(&pool->worklist) &&
Tejun Heo30310042010-10-11 11:51:57 +0200614 (atomic_read(nr_running) <= 1 ||
Tejun Heo63d95a92012-07-12 14:46:37 -0700615 pool->gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200616}
617
618/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700619static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200620{
Tejun Heo63d95a92012-07-12 14:46:37 -0700621 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200622}
623
624/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700625static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200626{
Tejun Heo63d95a92012-07-12 14:46:37 -0700627 return need_to_create_worker(pool) ||
628 pool->gcwq->flags & GCWQ_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +0200629}
630
631/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700632static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200633{
Tejun Heo63d95a92012-07-12 14:46:37 -0700634 bool managing = pool->gcwq->flags & GCWQ_MANAGING_WORKERS;
635 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
636 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200637
638 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
639}
640
641/*
642 * Wake up functions.
643 */
644
Tejun Heo7e116292010-06-29 10:07:13 +0200645/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700646static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200647{
Tejun Heo63d95a92012-07-12 14:46:37 -0700648 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200649 return NULL;
650
Tejun Heo63d95a92012-07-12 14:46:37 -0700651 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200652}
653
654/**
655 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700656 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200657 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700658 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200659 *
660 * CONTEXT:
661 * spin_lock_irq(gcwq->lock).
662 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700663static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200664{
Tejun Heo63d95a92012-07-12 14:46:37 -0700665 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200666
667 if (likely(worker))
668 wake_up_process(worker->task);
669}
670
Tejun Heo4690c4a2010-06-29 10:07:10 +0200671/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200672 * wq_worker_waking_up - a worker is waking up
673 * @task: task waking up
674 * @cpu: CPU @task is waking up to
675 *
676 * This function is called during try_to_wake_up() when a worker is
677 * being awoken.
678 *
679 * CONTEXT:
680 * spin_lock_irq(rq->lock)
681 */
682void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
683{
684 struct worker *worker = kthread_data(task);
685
Steven Rostedt2d646722010-12-03 23:12:33 -0500686 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700687 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200688}
689
690/**
691 * wq_worker_sleeping - a worker is going to sleep
692 * @task: task going to sleep
693 * @cpu: CPU in question, must be the current CPU number
694 *
695 * This function is called during schedule() when a busy worker is
696 * going to sleep. Worker on the same cpu can be woken up by
697 * returning pointer to its task.
698 *
699 * CONTEXT:
700 * spin_lock_irq(rq->lock)
701 *
702 * RETURNS:
703 * Worker task on @cpu to wake up, %NULL if none.
704 */
705struct task_struct *wq_worker_sleeping(struct task_struct *task,
706 unsigned int cpu)
707{
708 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700709 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700710 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200711
Steven Rostedt2d646722010-12-03 23:12:33 -0500712 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200713 return NULL;
714
715 /* this can only happen on the local cpu */
716 BUG_ON(cpu != raw_smp_processor_id());
717
718 /*
719 * The counterpart of the following dec_and_test, implied mb,
720 * worklist not empty test sequence is in insert_work().
721 * Please read comment there.
722 *
723 * NOT_RUNNING is clear. This means that trustee is not in
724 * charge and we're running on the local cpu w/ rq lock held
725 * and preemption disabled, which in turn means that none else
726 * could be manipulating idle_list, so dereferencing idle_list
727 * without gcwq lock is safe.
728 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700729 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700730 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200731 return to_wakeup ? to_wakeup->task : NULL;
732}
733
734/**
735 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200736 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200737 * @flags: flags to set
738 * @wakeup: wakeup an idle worker if necessary
739 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200740 * Set @flags in @worker->flags and adjust nr_running accordingly. If
741 * nr_running becomes zero and @wakeup is %true, an idle worker is
742 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200743 *
Tejun Heocb444762010-07-02 10:03:50 +0200744 * CONTEXT:
745 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200746 */
747static inline void worker_set_flags(struct worker *worker, unsigned int flags,
748 bool wakeup)
749{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700750 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200751
Tejun Heocb444762010-07-02 10:03:50 +0200752 WARN_ON_ONCE(worker->task != current);
753
Tejun Heoe22bee72010-06-29 10:07:14 +0200754 /*
755 * If transitioning into NOT_RUNNING, adjust nr_running and
756 * wake up an idle worker as necessary if requested by
757 * @wakeup.
758 */
759 if ((flags & WORKER_NOT_RUNNING) &&
760 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700761 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200762
763 if (wakeup) {
764 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700765 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700766 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200767 } else
768 atomic_dec(nr_running);
769 }
770
Tejun Heod302f012010-06-29 10:07:13 +0200771 worker->flags |= flags;
772}
773
774/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200775 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200776 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200777 * @flags: flags to clear
778 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200779 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200780 *
Tejun Heocb444762010-07-02 10:03:50 +0200781 * CONTEXT:
782 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200783 */
784static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
785{
Tejun Heo63d95a92012-07-12 14:46:37 -0700786 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200787 unsigned int oflags = worker->flags;
788
Tejun Heocb444762010-07-02 10:03:50 +0200789 WARN_ON_ONCE(worker->task != current);
790
Tejun Heod302f012010-06-29 10:07:13 +0200791 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200792
Tejun Heo42c025f2011-01-11 15:58:49 +0100793 /*
794 * If transitioning out of NOT_RUNNING, increment nr_running. Note
795 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
796 * of multiple flags, not a single flag.
797 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200798 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
799 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700800 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200801}
802
803/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200804 * busy_worker_head - return the busy hash head for a work
805 * @gcwq: gcwq of interest
806 * @work: work to be hashed
807 *
808 * Return hash head of @gcwq for @work.
809 *
810 * CONTEXT:
811 * spin_lock_irq(gcwq->lock).
812 *
813 * RETURNS:
814 * Pointer to the hash head.
815 */
816static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
817 struct work_struct *work)
818{
819 const int base_shift = ilog2(sizeof(struct work_struct));
820 unsigned long v = (unsigned long)work;
821
822 /* simple shift and fold hash, do we need something better? */
823 v >>= base_shift;
824 v += v >> BUSY_WORKER_HASH_ORDER;
825 v &= BUSY_WORKER_HASH_MASK;
826
827 return &gcwq->busy_hash[v];
828}
829
830/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200831 * __find_worker_executing_work - find worker which is executing a work
832 * @gcwq: gcwq of interest
833 * @bwh: hash head as returned by busy_worker_head()
834 * @work: work to find worker for
835 *
836 * Find a worker which is executing @work on @gcwq. @bwh should be
837 * the hash head obtained by calling busy_worker_head() with the same
838 * work.
839 *
840 * CONTEXT:
841 * spin_lock_irq(gcwq->lock).
842 *
843 * RETURNS:
844 * Pointer to worker which is executing @work if found, NULL
845 * otherwise.
846 */
847static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
848 struct hlist_head *bwh,
849 struct work_struct *work)
850{
851 struct worker *worker;
852 struct hlist_node *tmp;
853
854 hlist_for_each_entry(worker, tmp, bwh, hentry)
855 if (worker->current_work == work)
856 return worker;
857 return NULL;
858}
859
860/**
861 * find_worker_executing_work - find worker which is executing a work
862 * @gcwq: gcwq of interest
863 * @work: work to find worker for
864 *
865 * Find a worker which is executing @work on @gcwq. This function is
866 * identical to __find_worker_executing_work() except that this
867 * function calculates @bwh itself.
868 *
869 * CONTEXT:
870 * spin_lock_irq(gcwq->lock).
871 *
872 * RETURNS:
873 * Pointer to worker which is executing @work if found, NULL
874 * otherwise.
875 */
876static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
877 struct work_struct *work)
878{
879 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
880 work);
881}
882
883/**
Tejun Heo63d95a92012-07-12 14:46:37 -0700884 * pool_determine_ins_pos - find insertion position
885 * @pool: pool of interest
Tejun Heo649027d2010-06-29 10:07:14 +0200886 * @cwq: cwq a work is being queued for
887 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700888 * A work for @cwq is about to be queued on @pool, determine insertion
Tejun Heo649027d2010-06-29 10:07:14 +0200889 * position for the work. If @cwq is for HIGHPRI wq, the work is
890 * queued at the head of the queue but in FIFO order with respect to
891 * other HIGHPRI works; otherwise, at the end of the queue. This
Tejun Heo63d95a92012-07-12 14:46:37 -0700892 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @pool that
Tejun Heo649027d2010-06-29 10:07:14 +0200893 * there are HIGHPRI works pending.
894 *
895 * CONTEXT:
896 * spin_lock_irq(gcwq->lock).
897 *
898 * RETURNS:
899 * Pointer to inserstion position.
900 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700901static inline struct list_head *pool_determine_ins_pos(struct worker_pool *pool,
Tejun Heo649027d2010-06-29 10:07:14 +0200902 struct cpu_workqueue_struct *cwq)
903{
904 struct work_struct *twork;
905
906 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
Tejun Heo63d95a92012-07-12 14:46:37 -0700907 return &pool->worklist;
Tejun Heo649027d2010-06-29 10:07:14 +0200908
Tejun Heo63d95a92012-07-12 14:46:37 -0700909 list_for_each_entry(twork, &pool->worklist, entry) {
Tejun Heo649027d2010-06-29 10:07:14 +0200910 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
911
912 if (!(tcwq->wq->flags & WQ_HIGHPRI))
913 break;
914 }
915
Tejun Heo63d95a92012-07-12 14:46:37 -0700916 pool->gcwq->flags |= GCWQ_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +0200917 return &twork->entry;
918}
919
920/**
Tejun Heo7e116292010-06-29 10:07:13 +0200921 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200922 * @cwq: cwq @work belongs to
923 * @work: work to insert
924 * @head: insertion point
925 * @extra_flags: extra WORK_STRUCT_* flags to set
926 *
Tejun Heo7e116292010-06-29 10:07:13 +0200927 * Insert @work which belongs to @cwq into @gcwq after @head.
928 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200929 *
930 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200931 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200932 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700933static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200934 struct work_struct *work, struct list_head *head,
935 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700936{
Tejun Heo63d95a92012-07-12 14:46:37 -0700937 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100938
Tejun Heo4690c4a2010-06-29 10:07:10 +0200939 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200940 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200941
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700942 /*
943 * Ensure that we get the right work->data if we see the
944 * result of list_add() below, see try_to_grab_pending().
945 */
946 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200947
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700948 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200949
950 /*
951 * Ensure either worker_sched_deactivated() sees the above
952 * list_add_tail() or we see zero nr_running to avoid workers
953 * lying around lazily while there are works to be processed.
954 */
955 smp_mb();
956
Tejun Heo63d95a92012-07-12 14:46:37 -0700957 if (__need_more_worker(pool))
958 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700959}
960
Tejun Heoc8efcc22010-12-20 19:32:04 +0100961/*
962 * Test whether @work is being queued from another work executing on the
963 * same workqueue. This is rather expensive and should only be used from
964 * cold paths.
965 */
966static bool is_chained_work(struct workqueue_struct *wq)
967{
968 unsigned long flags;
969 unsigned int cpu;
970
971 for_each_gcwq_cpu(cpu) {
972 struct global_cwq *gcwq = get_gcwq(cpu);
973 struct worker *worker;
974 struct hlist_node *pos;
975 int i;
976
977 spin_lock_irqsave(&gcwq->lock, flags);
978 for_each_busy_worker(worker, i, pos, gcwq) {
979 if (worker->task != current)
980 continue;
981 spin_unlock_irqrestore(&gcwq->lock, flags);
982 /*
983 * I'm @worker, no locking necessary. See if @work
984 * is headed to the same workqueue.
985 */
986 return worker->current_cwq->wq == wq;
987 }
988 spin_unlock_irqrestore(&gcwq->lock, flags);
989 }
990 return false;
991}
992
Tejun Heo4690c4a2010-06-29 10:07:10 +0200993static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct work_struct *work)
995{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200996 struct global_cwq *gcwq;
997 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200998 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200999 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned long flags;
1001
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001002 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001003
Tejun Heoc8efcc22010-12-20 19:32:04 +01001004 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001005 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001006 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001007 return;
1008
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001009 /* determine gcwq to use */
1010 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001011 struct global_cwq *last_gcwq;
1012
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001013 if (unlikely(cpu == WORK_CPU_UNBOUND))
1014 cpu = raw_smp_processor_id();
1015
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001016 /*
1017 * It's multi cpu. If @wq is non-reentrant and @work
1018 * was previously on a different cpu, it might still
1019 * be running there, in which case the work needs to
1020 * be queued on that cpu to guarantee non-reentrance.
1021 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001022 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001023 if (wq->flags & WQ_NON_REENTRANT &&
1024 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1025 struct worker *worker;
1026
1027 spin_lock_irqsave(&last_gcwq->lock, flags);
1028
1029 worker = find_worker_executing_work(last_gcwq, work);
1030
1031 if (worker && worker->current_cwq->wq == wq)
1032 gcwq = last_gcwq;
1033 else {
1034 /* meh... not running there, queue here */
1035 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1036 spin_lock_irqsave(&gcwq->lock, flags);
1037 }
1038 } else
1039 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001040 } else {
1041 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1042 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001043 }
1044
1045 /* gcwq determined, get cwq and queue */
1046 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001047 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001048
Dan Carpenterf5b25522012-04-13 22:06:58 +03001049 if (WARN_ON(!list_empty(&work->entry))) {
1050 spin_unlock_irqrestore(&gcwq->lock, flags);
1051 return;
1052 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001053
Tejun Heo73f53c42010-06-29 10:07:11 +02001054 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001055 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056
1057 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001058 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001059 cwq->nr_active++;
Tejun Heo63d95a92012-07-12 14:46:37 -07001060 worklist = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001061 } else {
1062 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001063 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001064 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001065
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001066 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001067
Tejun Heo8b03ae32010-06-29 10:07:12 +02001068 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001071/**
1072 * queue_work - queue work on a workqueue
1073 * @wq: workqueue to use
1074 * @work: work to queue
1075 *
Alan Stern057647f2006-10-28 10:38:58 -07001076 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001078 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1079 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001081int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001083 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001085 ret = queue_work_on(get_cpu(), wq, work);
1086 put_cpu();
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return ret;
1089}
Dave Jonesae90dd52006-06-30 01:40:45 -04001090EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Zhang Ruic1a220e2008-07-23 21:28:39 -07001092/**
1093 * queue_work_on - queue work on specific cpu
1094 * @cpu: CPU number to execute work on
1095 * @wq: workqueue to use
1096 * @work: work to queue
1097 *
1098 * Returns 0 if @work was already on a queue, non-zero otherwise.
1099 *
1100 * We queue the work to a specific CPU, the caller must ensure it
1101 * can't go away.
1102 */
1103int
1104queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1105{
1106 int ret = 0;
1107
Tejun Heo22df02b2010-06-29 10:07:10 +02001108 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001109 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001110 ret = 1;
1111 }
1112 return ret;
1113}
1114EXPORT_SYMBOL_GPL(queue_work_on);
1115
Li Zefan6d141c32008-02-08 04:21:09 -08001116static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
David Howells52bad642006-11-22 14:54:01 +00001118 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001119 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Tejun Heo4690c4a2010-06-29 10:07:10 +02001121 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001124/**
1125 * queue_delayed_work - queue work on a workqueue after delay
1126 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001127 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001128 * @delay: number of jiffies to wait before queueing
1129 *
Alan Stern057647f2006-10-28 10:38:58 -07001130 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001131 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001132int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001133 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
David Howells52bad642006-11-22 14:54:01 +00001135 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001136 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001138 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
Dave Jonesae90dd52006-06-30 01:40:45 -04001140EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001142/**
1143 * queue_delayed_work_on - queue work on specific CPU after delay
1144 * @cpu: CPU number to execute work on
1145 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001146 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001147 * @delay: number of jiffies to wait before queueing
1148 *
Alan Stern057647f2006-10-28 10:38:58 -07001149 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001150 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001151int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001152 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001153{
1154 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001155 struct timer_list *timer = &dwork->timer;
1156 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001157
Tejun Heo22df02b2010-06-29 10:07:10 +02001158 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001159 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001160
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001161 BUG_ON(timer_pending(timer));
1162 BUG_ON(!list_empty(&work->entry));
1163
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001164 timer_stats_timer_set_start_info(&dwork->timer);
1165
Tejun Heo7a22ad72010-06-29 10:07:13 +02001166 /*
1167 * This stores cwq for the moment, for the timer_fn.
1168 * Note that the work's gcwq is preserved to allow
1169 * reentrance detection for delayed works.
1170 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001171 if (!(wq->flags & WQ_UNBOUND)) {
1172 struct global_cwq *gcwq = get_work_gcwq(work);
1173
1174 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1175 lcpu = gcwq->cpu;
1176 else
1177 lcpu = raw_smp_processor_id();
1178 } else
1179 lcpu = WORK_CPU_UNBOUND;
1180
Tejun Heo7a22ad72010-06-29 10:07:13 +02001181 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001182
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001183 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001184 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001185 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001186
1187 if (unlikely(cpu >= 0))
1188 add_timer_on(timer, cpu);
1189 else
1190 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001191 ret = 1;
1192 }
1193 return ret;
1194}
Dave Jonesae90dd52006-06-30 01:40:45 -04001195EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Tejun Heoc8e55f32010-06-29 10:07:12 +02001197/**
1198 * worker_enter_idle - enter idle state
1199 * @worker: worker which is entering idle state
1200 *
1201 * @worker is entering idle state. Update stats and idle timer if
1202 * necessary.
1203 *
1204 * LOCKING:
1205 * spin_lock_irq(gcwq->lock).
1206 */
1207static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001209 struct worker_pool *pool = worker->pool;
1210 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Tejun Heoc8e55f32010-06-29 10:07:12 +02001212 BUG_ON(worker->flags & WORKER_IDLE);
1213 BUG_ON(!list_empty(&worker->entry) &&
1214 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Tejun Heocb444762010-07-02 10:03:50 +02001216 /* can't use worker_set_flags(), also called from start_worker() */
1217 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001218 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001219 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001220
Tejun Heoc8e55f32010-06-29 10:07:12 +02001221 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001222 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001223
Tejun Heoe22bee72010-06-29 10:07:14 +02001224 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001225 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001226 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001227 jiffies + IDLE_WORKER_TIMEOUT);
1228 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001229 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001230
Tejun Heo544ecf32012-05-14 15:04:50 -07001231 /*
1232 * Sanity check nr_running. Because trustee releases gcwq->lock
1233 * between setting %WORKER_ROGUE and zapping nr_running, the
1234 * warning may trigger spuriously. Check iff trustee is idle.
1235 */
1236 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001237 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001238 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Tejun Heoc8e55f32010-06-29 10:07:12 +02001241/**
1242 * worker_leave_idle - leave idle state
1243 * @worker: worker which is leaving idle state
1244 *
1245 * @worker is leaving idle state. Update stats.
1246 *
1247 * LOCKING:
1248 * spin_lock_irq(gcwq->lock).
1249 */
1250static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001252 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Tejun Heoc8e55f32010-06-29 10:07:12 +02001254 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001255 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001256 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001257 list_del_init(&worker->entry);
1258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Tejun Heoe22bee72010-06-29 10:07:14 +02001260/**
1261 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1262 * @worker: self
1263 *
1264 * Works which are scheduled while the cpu is online must at least be
1265 * scheduled to a worker which is bound to the cpu so that if they are
1266 * flushed from cpu callbacks while cpu is going down, they are
1267 * guaranteed to execute on the cpu.
1268 *
1269 * This function is to be used by rogue workers and rescuers to bind
1270 * themselves to the target cpu and may race with cpu going down or
1271 * coming online. kthread_bind() can't be used because it may put the
1272 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1273 * verbatim as it's best effort and blocking and gcwq may be
1274 * [dis]associated in the meantime.
1275 *
1276 * This function tries set_cpus_allowed() and locks gcwq and verifies
1277 * the binding against GCWQ_DISASSOCIATED which is set during
1278 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1279 * idle state or fetches works without dropping lock, it can guarantee
1280 * the scheduling requirement described in the first paragraph.
1281 *
1282 * CONTEXT:
1283 * Might sleep. Called without any lock but returns with gcwq->lock
1284 * held.
1285 *
1286 * RETURNS:
1287 * %true if the associated gcwq is online (@worker is successfully
1288 * bound), %false if offline.
1289 */
1290static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001291__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001292{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001293 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001294 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Tejun Heoe22bee72010-06-29 10:07:14 +02001296 while (true) {
1297 /*
1298 * The following call may fail, succeed or succeed
1299 * without actually migrating the task to the cpu if
1300 * it races with cpu hotunplug operation. Verify
1301 * against GCWQ_DISASSOCIATED.
1302 */
Tejun Heof3421792010-07-02 10:03:51 +02001303 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1304 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001305
Tejun Heoe22bee72010-06-29 10:07:14 +02001306 spin_lock_irq(&gcwq->lock);
1307 if (gcwq->flags & GCWQ_DISASSOCIATED)
1308 return false;
1309 if (task_cpu(task) == gcwq->cpu &&
1310 cpumask_equal(&current->cpus_allowed,
1311 get_cpu_mask(gcwq->cpu)))
1312 return true;
1313 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001314
Tejun Heo5035b202011-04-29 18:08:37 +02001315 /*
1316 * We've raced with CPU hot[un]plug. Give it a breather
1317 * and retry migration. cond_resched() is required here;
1318 * otherwise, we might deadlock against cpu_stop trying to
1319 * bring down the CPU on non-preemptive kernel.
1320 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001321 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001322 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001323 }
1324}
1325
1326/*
1327 * Function for worker->rebind_work used to rebind rogue busy workers
1328 * to the associated cpu which is coming back online. This is
1329 * scheduled by cpu up but can race with other cpu hotplug operations
1330 * and may be executed twice without intervening cpu down.
1331 */
1332static void worker_rebind_fn(struct work_struct *work)
1333{
1334 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001335 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001336
1337 if (worker_maybe_bind_and_lock(worker))
1338 worker_clr_flags(worker, WORKER_REBIND);
1339
1340 spin_unlock_irq(&gcwq->lock);
1341}
1342
Tejun Heoc34056a2010-06-29 10:07:11 +02001343static struct worker *alloc_worker(void)
1344{
1345 struct worker *worker;
1346
1347 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001348 if (worker) {
1349 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001350 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001351 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1352 /* on creation a worker is in !idle && prep state */
1353 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001354 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001355 return worker;
1356}
1357
1358/**
1359 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001360 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001361 * @bind: whether to set affinity to @cpu or not
1362 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001363 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001364 * can be started by calling start_worker() or destroyed using
1365 * destroy_worker().
1366 *
1367 * CONTEXT:
1368 * Might sleep. Does GFP_KERNEL allocations.
1369 *
1370 * RETURNS:
1371 * Pointer to the newly created worker.
1372 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001373static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001374{
Tejun Heo63d95a92012-07-12 14:46:37 -07001375 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001376 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001377 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001378 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001379
Tejun Heo8b03ae32010-06-29 10:07:12 +02001380 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001381 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001382 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001383 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001384 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001385 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001386 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001387 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001388
1389 worker = alloc_worker();
1390 if (!worker)
1391 goto fail;
1392
Tejun Heobd7bdd42012-07-12 14:46:37 -07001393 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001394 worker->id = id;
1395
Tejun Heof3421792010-07-02 10:03:51 +02001396 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001397 worker->task = kthread_create_on_node(worker_thread,
1398 worker,
1399 cpu_to_node(gcwq->cpu),
1400 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001401 else
1402 worker->task = kthread_create(worker_thread, worker,
1403 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001404 if (IS_ERR(worker->task))
1405 goto fail;
1406
Tejun Heodb7bccf2010-06-29 10:07:12 +02001407 /*
1408 * A rogue worker will become a regular one if CPU comes
1409 * online later on. Make sure every worker has
1410 * PF_THREAD_BOUND set.
1411 */
Tejun Heof3421792010-07-02 10:03:51 +02001412 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001413 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001414 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001415 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001416 if (on_unbound_cpu)
1417 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001419
Tejun Heoc34056a2010-06-29 10:07:11 +02001420 return worker;
1421fail:
1422 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001423 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001424 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001425 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001426 }
1427 kfree(worker);
1428 return NULL;
1429}
1430
1431/**
1432 * start_worker - start a newly created worker
1433 * @worker: worker to start
1434 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001435 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001436 *
1437 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001438 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001439 */
1440static void start_worker(struct worker *worker)
1441{
Tejun Heocb444762010-07-02 10:03:50 +02001442 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001443 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001444 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001445 wake_up_process(worker->task);
1446}
1447
1448/**
1449 * destroy_worker - destroy a workqueue worker
1450 * @worker: worker to be destroyed
1451 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001452 * Destroy @worker and adjust @gcwq stats accordingly.
1453 *
1454 * CONTEXT:
1455 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001456 */
1457static void destroy_worker(struct worker *worker)
1458{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001459 struct worker_pool *pool = worker->pool;
1460 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001461 int id = worker->id;
1462
1463 /* sanity check frenzy */
1464 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001465 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001466
Tejun Heoc8e55f32010-06-29 10:07:12 +02001467 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001468 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001469 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001470 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001471
1472 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001473 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001474
1475 spin_unlock_irq(&gcwq->lock);
1476
Tejun Heoc34056a2010-06-29 10:07:11 +02001477 kthread_stop(worker->task);
1478 kfree(worker);
1479
Tejun Heo8b03ae32010-06-29 10:07:12 +02001480 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001481 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001482}
1483
Tejun Heo63d95a92012-07-12 14:46:37 -07001484static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001485{
Tejun Heo63d95a92012-07-12 14:46:37 -07001486 struct worker_pool *pool = (void *)__pool;
1487 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001488
1489 spin_lock_irq(&gcwq->lock);
1490
Tejun Heo63d95a92012-07-12 14:46:37 -07001491 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001492 struct worker *worker;
1493 unsigned long expires;
1494
1495 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001496 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001497 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1498
1499 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001500 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001501 else {
1502 /* it's been idle for too long, wake up manager */
1503 gcwq->flags |= GCWQ_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001504 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001505 }
1506 }
1507
1508 spin_unlock_irq(&gcwq->lock);
1509}
1510
1511static bool send_mayday(struct work_struct *work)
1512{
1513 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1514 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001515 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001516
1517 if (!(wq->flags & WQ_RESCUER))
1518 return false;
1519
1520 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001521 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001522 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1523 if (cpu == WORK_CPU_UNBOUND)
1524 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001525 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001526 wake_up_process(wq->rescuer->task);
1527 return true;
1528}
1529
Tejun Heo63d95a92012-07-12 14:46:37 -07001530static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001531{
Tejun Heo63d95a92012-07-12 14:46:37 -07001532 struct worker_pool *pool = (void *)__pool;
1533 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001534 struct work_struct *work;
1535
1536 spin_lock_irq(&gcwq->lock);
1537
Tejun Heo63d95a92012-07-12 14:46:37 -07001538 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001539 /*
1540 * We've been trying to create a new worker but
1541 * haven't been successful. We might be hitting an
1542 * allocation deadlock. Send distress signals to
1543 * rescuers.
1544 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001545 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001546 send_mayday(work);
1547 }
1548
1549 spin_unlock_irq(&gcwq->lock);
1550
Tejun Heo63d95a92012-07-12 14:46:37 -07001551 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001552}
1553
1554/**
1555 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001556 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001557 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001558 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 * have at least one idle worker on return from this function. If
1560 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001561 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 * possible allocation deadlock.
1563 *
1564 * On return, need_to_create_worker() is guaranteed to be false and
1565 * may_start_working() true.
1566 *
1567 * LOCKING:
1568 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1569 * multiple times. Does GFP_KERNEL allocations. Called only from
1570 * manager.
1571 *
1572 * RETURNS:
1573 * false if no action was taken and gcwq->lock stayed locked, true
1574 * otherwise.
1575 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001576static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001577__releases(&gcwq->lock)
1578__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001579{
Tejun Heo63d95a92012-07-12 14:46:37 -07001580 struct global_cwq *gcwq = pool->gcwq;
1581
1582 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001583 return false;
1584restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001585 spin_unlock_irq(&gcwq->lock);
1586
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001588 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001589
1590 while (true) {
1591 struct worker *worker;
1592
Tejun Heo63d95a92012-07-12 14:46:37 -07001593 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001594 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001595 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 spin_lock_irq(&gcwq->lock);
1597 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001598 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 return true;
1600 }
1601
Tejun Heo63d95a92012-07-12 14:46:37 -07001602 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001603 break;
1604
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 __set_current_state(TASK_INTERRUPTIBLE);
1606 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001607
Tejun Heo63d95a92012-07-12 14:46:37 -07001608 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 break;
1610 }
1611
Tejun Heo63d95a92012-07-12 14:46:37 -07001612 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001613 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001614 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001615 goto restart;
1616 return true;
1617}
1618
1619/**
1620 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001621 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001623 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001624 * IDLE_WORKER_TIMEOUT.
1625 *
1626 * LOCKING:
1627 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1628 * multiple times. Called only from manager.
1629 *
1630 * RETURNS:
1631 * false if no action was taken and gcwq->lock stayed locked, true
1632 * otherwise.
1633 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001634static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001635{
1636 bool ret = false;
1637
Tejun Heo63d95a92012-07-12 14:46:37 -07001638 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001639 struct worker *worker;
1640 unsigned long expires;
1641
Tejun Heo63d95a92012-07-12 14:46:37 -07001642 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001643 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1644
1645 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001646 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001647 break;
1648 }
1649
1650 destroy_worker(worker);
1651 ret = true;
1652 }
1653
1654 return ret;
1655}
1656
1657/**
1658 * manage_workers - manage worker pool
1659 * @worker: self
1660 *
1661 * Assume the manager role and manage gcwq worker pool @worker belongs
1662 * to. At any given time, there can be only zero or one manager per
1663 * gcwq. The exclusion is handled automatically by this function.
1664 *
1665 * The caller can safely start processing works on false return. On
1666 * true return, it's guaranteed that need_to_create_worker() is false
1667 * and may_start_working() is true.
1668 *
1669 * CONTEXT:
1670 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1671 * multiple times. Does GFP_KERNEL allocations.
1672 *
1673 * RETURNS:
1674 * false if no action was taken and gcwq->lock stayed locked, true if
1675 * some action was taken.
1676 */
1677static bool manage_workers(struct worker *worker)
1678{
Tejun Heo63d95a92012-07-12 14:46:37 -07001679 struct worker_pool *pool = worker->pool;
1680 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001681 bool ret = false;
1682
1683 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1684 return ret;
1685
1686 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1687 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1688
1689 /*
1690 * Destroy and then create so that may_start_working() is true
1691 * on return.
1692 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001693 ret |= maybe_destroy_workers(pool);
1694 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001695
1696 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1697
1698 /*
1699 * The trustee might be waiting to take over the manager
1700 * position, tell it we're done.
1701 */
1702 if (unlikely(gcwq->trustee))
1703 wake_up_all(&gcwq->trustee_wait);
1704
1705 return ret;
1706}
1707
Tejun Heoa62428c2010-06-29 10:07:10 +02001708/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001709 * move_linked_works - move linked works to a list
1710 * @work: start of series of works to be scheduled
1711 * @head: target list to append @work to
1712 * @nextp: out paramter for nested worklist walking
1713 *
1714 * Schedule linked works starting from @work to @head. Work series to
1715 * be scheduled starts at @work and includes any consecutive work with
1716 * WORK_STRUCT_LINKED set in its predecessor.
1717 *
1718 * If @nextp is not NULL, it's updated to point to the next work of
1719 * the last scheduled work. This allows move_linked_works() to be
1720 * nested inside outer list_for_each_entry_safe().
1721 *
1722 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001723 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001724 */
1725static void move_linked_works(struct work_struct *work, struct list_head *head,
1726 struct work_struct **nextp)
1727{
1728 struct work_struct *n;
1729
1730 /*
1731 * Linked worklist will always end before the end of the list,
1732 * use NULL for list head.
1733 */
1734 list_for_each_entry_safe_from(work, n, NULL, entry) {
1735 list_move_tail(&work->entry, head);
1736 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1737 break;
1738 }
1739
1740 /*
1741 * If we're already inside safe list traversal and have moved
1742 * multiple works to the scheduled queue, the next position
1743 * needs to be updated.
1744 */
1745 if (nextp)
1746 *nextp = n;
1747}
1748
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001749static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1750{
1751 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1752 struct work_struct, entry);
Tejun Heo63d95a92012-07-12 14:46:37 -07001753 struct list_head *pos = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001754
Tejun Heocdadf002010-10-05 10:49:55 +02001755 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001756 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001757 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001758 cwq->nr_active++;
1759}
1760
Tejun Heoaffee4b2010-06-29 10:07:12 +02001761/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001762 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1763 * @cwq: cwq of interest
1764 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001765 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001766 *
1767 * A work either has completed or is removed from pending queue,
1768 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1769 *
1770 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001771 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001772 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001773static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1774 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001775{
1776 /* ignore uncolored works */
1777 if (color == WORK_NO_COLOR)
1778 return;
1779
1780 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001781
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001782 if (!delayed) {
1783 cwq->nr_active--;
1784 if (!list_empty(&cwq->delayed_works)) {
1785 /* one down, submit a delayed one */
1786 if (cwq->nr_active < cwq->max_active)
1787 cwq_activate_first_delayed(cwq);
1788 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001789 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001790
1791 /* is flush in progress and are we at the flushing tip? */
1792 if (likely(cwq->flush_color != color))
1793 return;
1794
1795 /* are there still in-flight works? */
1796 if (cwq->nr_in_flight[color])
1797 return;
1798
1799 /* this cwq is done, clear flush_color */
1800 cwq->flush_color = -1;
1801
1802 /*
1803 * If this was the last cwq, wake up the first flusher. It
1804 * will handle the rest.
1805 */
1806 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1807 complete(&cwq->wq->first_flusher->done);
1808}
1809
1810/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001811 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001812 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001813 * @work: work to process
1814 *
1815 * Process @work. This function contains all the logics necessary to
1816 * process a single work including synchronization against and
1817 * interaction with other workers on the same cpu, queueing and
1818 * flushing. As long as context requirement is met, any worker can
1819 * call this function to process a work.
1820 *
1821 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001822 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001823 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001824static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001825__releases(&gcwq->lock)
1826__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001827{
Tejun Heo7e116292010-06-29 10:07:13 +02001828 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001829 struct worker_pool *pool = worker->pool;
1830 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001831 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001832 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001833 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001834 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001835 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001836#ifdef CONFIG_LOCKDEP
1837 /*
1838 * It is permissible to free the struct work_struct from
1839 * inside the function that is called from it, this we need to
1840 * take into account for lockdep too. To avoid bogus "held
1841 * lock freed" warnings as well as problems when looking into
1842 * work->lockdep_map, make a copy and use that here.
1843 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001844 struct lockdep_map lockdep_map;
1845
1846 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001847#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001848 /*
1849 * A single work shouldn't be executed concurrently by
1850 * multiple workers on a single cpu. Check whether anyone is
1851 * already processing the work. If so, defer the work to the
1852 * currently executing one.
1853 */
1854 collision = __find_worker_executing_work(gcwq, bwh, work);
1855 if (unlikely(collision)) {
1856 move_linked_works(work, &collision->scheduled, NULL);
1857 return;
1858 }
1859
Tejun Heoa62428c2010-06-29 10:07:10 +02001860 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001861 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001862 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001863 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001864 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001865 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001866
Tejun Heo7a22ad72010-06-29 10:07:13 +02001867 /* record the current cpu number in the work data and dequeue */
1868 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001869 list_del_init(&work->entry);
1870
Tejun Heo649027d2010-06-29 10:07:14 +02001871 /*
1872 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1873 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1874 */
1875 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001876 struct work_struct *nwork = list_first_entry(&pool->worklist,
1877 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001878
Tejun Heobd7bdd42012-07-12 14:46:37 -07001879 if (!list_empty(&pool->worklist) &&
Tejun Heo649027d2010-06-29 10:07:14 +02001880 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
Tejun Heo63d95a92012-07-12 14:46:37 -07001881 wake_up_worker(pool);
Tejun Heo649027d2010-06-29 10:07:14 +02001882 else
1883 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1884 }
1885
Tejun Heofb0e7be2010-06-29 10:07:15 +02001886 /*
1887 * CPU intensive works don't participate in concurrency
1888 * management. They're the scheduler's responsibility.
1889 */
1890 if (unlikely(cpu_intensive))
1891 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1892
Tejun Heo974271c2012-07-12 14:46:37 -07001893 /*
1894 * Unbound gcwq isn't concurrency managed and work items should be
1895 * executed ASAP. Wake up another worker if necessary.
1896 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001897 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1898 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07001899
Tejun Heo8b03ae32010-06-29 10:07:12 +02001900 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001901
Tejun Heoa62428c2010-06-29 10:07:10 +02001902 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001903 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001904 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001905 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001906 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001907 /*
1908 * While we must be careful to not use "work" after this, the trace
1909 * point will only record its address.
1910 */
1911 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001912 lock_map_release(&lockdep_map);
1913 lock_map_release(&cwq->wq->lockdep_map);
1914
1915 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1916 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1917 "%s/0x%08x/%d\n",
1918 current->comm, preempt_count(), task_pid_nr(current));
1919 printk(KERN_ERR " last function: ");
1920 print_symbol("%s\n", (unsigned long)f);
1921 debug_show_held_locks(current);
1922 dump_stack();
1923 }
1924
Tejun Heo8b03ae32010-06-29 10:07:12 +02001925 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001926
Tejun Heofb0e7be2010-06-29 10:07:15 +02001927 /* clear cpu intensive status */
1928 if (unlikely(cpu_intensive))
1929 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1930
Tejun Heoa62428c2010-06-29 10:07:10 +02001931 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001932 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001933 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001934 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001935 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001936}
1937
Tejun Heoaffee4b2010-06-29 10:07:12 +02001938/**
1939 * process_scheduled_works - process scheduled works
1940 * @worker: self
1941 *
1942 * Process all scheduled works. Please note that the scheduled list
1943 * may change while processing a work, so this function repeatedly
1944 * fetches a work from the top and executes it.
1945 *
1946 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001947 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001948 * multiple times.
1949 */
1950static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001952 while (!list_empty(&worker->scheduled)) {
1953 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001955 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
Tejun Heo4690c4a2010-06-29 10:07:10 +02001959/**
1960 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001961 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001962 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001963 * The gcwq worker thread function. There's a single dynamic pool of
1964 * these per each cpu. These workers process all works regardless of
1965 * their specific target workqueue. The only exception is works which
1966 * belong to workqueues with a rescuer which will be explained in
1967 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001968 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001969static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Tejun Heoc34056a2010-06-29 10:07:11 +02001971 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001972 struct worker_pool *pool = worker->pool;
1973 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 /* tell the scheduler that this is a workqueue worker */
1976 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001978 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Tejun Heoc8e55f32010-06-29 10:07:12 +02001980 /* DIE can be set only while we're idle, checking here is enough */
1981 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001982 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986
Tejun Heoc8e55f32010-06-29 10:07:12 +02001987 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001988recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 goto sleep;
1992
1993 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 goto recheck;
1996
Tejun Heoc8e55f32010-06-29 10:07:12 +02001997 /*
1998 * ->scheduled list can only be filled while a worker is
1999 * preparing to process a work or actually processing it.
2000 * Make sure nobody diddled with it while I was sleeping.
2001 */
2002 BUG_ON(!list_empty(&worker->scheduled));
2003
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 /*
2005 * When control reaches this point, we're guaranteed to have
2006 * at least one idle worker or that someone else has already
2007 * assumed the manager role.
2008 */
2009 worker_clr_flags(worker, WORKER_PREP);
2010
2011 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002012 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002013 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002014 struct work_struct, entry);
2015
2016 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2017 /* optimization path, not strictly necessary */
2018 process_one_work(worker, work);
2019 if (unlikely(!list_empty(&worker->scheduled)))
2020 process_scheduled_works(worker);
2021 } else {
2022 move_linked_works(work, &worker->scheduled, NULL);
2023 process_scheduled_works(worker);
2024 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002025 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002026
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002028sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002029 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002031
Tejun Heoc8e55f32010-06-29 10:07:12 +02002032 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 * gcwq->lock is held and there's no work to process and no
2034 * need to manage, sleep. Workers are woken up only while
2035 * holding gcwq->lock or from local cpu, so setting the
2036 * current state before releasing gcwq->lock is enough to
2037 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002038 */
2039 worker_enter_idle(worker);
2040 __set_current_state(TASK_INTERRUPTIBLE);
2041 spin_unlock_irq(&gcwq->lock);
2042 schedule();
2043 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Tejun Heoe22bee72010-06-29 10:07:14 +02002046/**
2047 * rescuer_thread - the rescuer thread function
2048 * @__wq: the associated workqueue
2049 *
2050 * Workqueue rescuer thread function. There's one rescuer for each
2051 * workqueue which has WQ_RESCUER set.
2052 *
2053 * Regular work processing on a gcwq may block trying to create a new
2054 * worker which uses GFP_KERNEL allocation which has slight chance of
2055 * developing into deadlock if some works currently on the same queue
2056 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2057 * the problem rescuer solves.
2058 *
2059 * When such condition is possible, the gcwq summons rescuers of all
2060 * workqueues which have works queued on the gcwq and let them process
2061 * those works so that forward progress can be guaranteed.
2062 *
2063 * This should happen rarely.
2064 */
2065static int rescuer_thread(void *__wq)
2066{
2067 struct workqueue_struct *wq = __wq;
2068 struct worker *rescuer = wq->rescuer;
2069 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002070 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002071 unsigned int cpu;
2072
2073 set_user_nice(current, RESCUER_NICE_LEVEL);
2074repeat:
2075 set_current_state(TASK_INTERRUPTIBLE);
2076
2077 if (kthread_should_stop())
2078 return 0;
2079
Tejun Heof3421792010-07-02 10:03:51 +02002080 /*
2081 * See whether any cpu is asking for help. Unbounded
2082 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2083 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002084 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002085 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2086 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002087 struct worker_pool *pool = cwq->pool;
2088 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002089 struct work_struct *work, *n;
2090
2091 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002092 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002093
2094 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002095 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002096 worker_maybe_bind_and_lock(rescuer);
2097
2098 /*
2099 * Slurp in all works issued via this workqueue and
2100 * process'em.
2101 */
2102 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002103 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002104 if (get_work_cwq(work) == cwq)
2105 move_linked_works(work, scheduled, &n);
2106
2107 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002108
2109 /*
2110 * Leave this gcwq. If keep_working() is %true, notify a
2111 * regular worker; otherwise, we end up with 0 concurrency
2112 * and stalling the execution.
2113 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002114 if (keep_working(pool))
2115 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002116
Tejun Heoe22bee72010-06-29 10:07:14 +02002117 spin_unlock_irq(&gcwq->lock);
2118 }
2119
2120 schedule();
2121 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002124struct wq_barrier {
2125 struct work_struct work;
2126 struct completion done;
2127};
2128
2129static void wq_barrier_func(struct work_struct *work)
2130{
2131 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2132 complete(&barr->done);
2133}
2134
Tejun Heo4690c4a2010-06-29 10:07:10 +02002135/**
2136 * insert_wq_barrier - insert a barrier work
2137 * @cwq: cwq to insert barrier into
2138 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002139 * @target: target work to attach @barr to
2140 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002141 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002142 * @barr is linked to @target such that @barr is completed only after
2143 * @target finishes execution. Please note that the ordering
2144 * guarantee is observed only with respect to @target and on the local
2145 * cpu.
2146 *
2147 * Currently, a queued barrier can't be canceled. This is because
2148 * try_to_grab_pending() can't determine whether the work to be
2149 * grabbed is at the head of the queue and thus can't clear LINKED
2150 * flag of the previous work while there must be a valid next work
2151 * after a work with LINKED flag set.
2152 *
2153 * Note that when @worker is non-NULL, @target may be modified
2154 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002155 *
2156 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002157 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002158 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002159static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002160 struct wq_barrier *barr,
2161 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002162{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002163 struct list_head *head;
2164 unsigned int linked = 0;
2165
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002166 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002167 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002168 * as we know for sure that this will not trigger any of the
2169 * checks and call back into the fixup functions where we
2170 * might deadlock.
2171 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002172 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002173 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002174 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002175
Tejun Heoaffee4b2010-06-29 10:07:12 +02002176 /*
2177 * If @target is currently being executed, schedule the
2178 * barrier to the worker; otherwise, put it after @target.
2179 */
2180 if (worker)
2181 head = worker->scheduled.next;
2182 else {
2183 unsigned long *bits = work_data_bits(target);
2184
2185 head = target->entry.next;
2186 /* there can already be other linked works, inherit and set */
2187 linked = *bits & WORK_STRUCT_LINKED;
2188 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2189 }
2190
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002191 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002192 insert_work(cwq, &barr->work, head,
2193 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002194}
2195
Tejun Heo73f53c42010-06-29 10:07:11 +02002196/**
2197 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2198 * @wq: workqueue being flushed
2199 * @flush_color: new flush color, < 0 for no-op
2200 * @work_color: new work color, < 0 for no-op
2201 *
2202 * Prepare cwqs for workqueue flushing.
2203 *
2204 * If @flush_color is non-negative, flush_color on all cwqs should be
2205 * -1. If no cwq has in-flight commands at the specified color, all
2206 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2207 * has in flight commands, its cwq->flush_color is set to
2208 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2209 * wakeup logic is armed and %true is returned.
2210 *
2211 * The caller should have initialized @wq->first_flusher prior to
2212 * calling this function with non-negative @flush_color. If
2213 * @flush_color is negative, no flush color update is done and %false
2214 * is returned.
2215 *
2216 * If @work_color is non-negative, all cwqs should have the same
2217 * work_color which is previous to @work_color and all will be
2218 * advanced to @work_color.
2219 *
2220 * CONTEXT:
2221 * mutex_lock(wq->flush_mutex).
2222 *
2223 * RETURNS:
2224 * %true if @flush_color >= 0 and there's something to flush. %false
2225 * otherwise.
2226 */
2227static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2228 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Tejun Heo73f53c42010-06-29 10:07:11 +02002230 bool wait = false;
2231 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002232
Tejun Heo73f53c42010-06-29 10:07:11 +02002233 if (flush_color >= 0) {
2234 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2235 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002236 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002237
Tejun Heof3421792010-07-02 10:03:51 +02002238 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002239 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002240 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002241
Tejun Heo8b03ae32010-06-29 10:07:12 +02002242 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002243
2244 if (flush_color >= 0) {
2245 BUG_ON(cwq->flush_color != -1);
2246
2247 if (cwq->nr_in_flight[flush_color]) {
2248 cwq->flush_color = flush_color;
2249 atomic_inc(&wq->nr_cwqs_to_flush);
2250 wait = true;
2251 }
2252 }
2253
2254 if (work_color >= 0) {
2255 BUG_ON(work_color != work_next_color(cwq->work_color));
2256 cwq->work_color = work_color;
2257 }
2258
Tejun Heo8b03ae32010-06-29 10:07:12 +02002259 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002260 }
2261
2262 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2263 complete(&wq->first_flusher->done);
2264
2265 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002268/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002270 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 *
2272 * Forces execution of the workqueue and blocks until its completion.
2273 * This is typically used in driver shutdown handlers.
2274 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002275 * We sleep until all works which were queued on entry have been handled,
2276 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002278void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Tejun Heo73f53c42010-06-29 10:07:11 +02002280 struct wq_flusher this_flusher = {
2281 .list = LIST_HEAD_INIT(this_flusher.list),
2282 .flush_color = -1,
2283 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2284 };
2285 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002286
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002287 lock_map_acquire(&wq->lockdep_map);
2288 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002289
2290 mutex_lock(&wq->flush_mutex);
2291
2292 /*
2293 * Start-to-wait phase
2294 */
2295 next_color = work_next_color(wq->work_color);
2296
2297 if (next_color != wq->flush_color) {
2298 /*
2299 * Color space is not full. The current work_color
2300 * becomes our flush_color and work_color is advanced
2301 * by one.
2302 */
2303 BUG_ON(!list_empty(&wq->flusher_overflow));
2304 this_flusher.flush_color = wq->work_color;
2305 wq->work_color = next_color;
2306
2307 if (!wq->first_flusher) {
2308 /* no flush in progress, become the first flusher */
2309 BUG_ON(wq->flush_color != this_flusher.flush_color);
2310
2311 wq->first_flusher = &this_flusher;
2312
2313 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2314 wq->work_color)) {
2315 /* nothing to flush, done */
2316 wq->flush_color = next_color;
2317 wq->first_flusher = NULL;
2318 goto out_unlock;
2319 }
2320 } else {
2321 /* wait in queue */
2322 BUG_ON(wq->flush_color == this_flusher.flush_color);
2323 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2324 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2325 }
2326 } else {
2327 /*
2328 * Oops, color space is full, wait on overflow queue.
2329 * The next flush completion will assign us
2330 * flush_color and transfer to flusher_queue.
2331 */
2332 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2333 }
2334
2335 mutex_unlock(&wq->flush_mutex);
2336
2337 wait_for_completion(&this_flusher.done);
2338
2339 /*
2340 * Wake-up-and-cascade phase
2341 *
2342 * First flushers are responsible for cascading flushes and
2343 * handling overflow. Non-first flushers can simply return.
2344 */
2345 if (wq->first_flusher != &this_flusher)
2346 return;
2347
2348 mutex_lock(&wq->flush_mutex);
2349
Tejun Heo4ce48b32010-07-02 10:03:51 +02002350 /* we might have raced, check again with mutex held */
2351 if (wq->first_flusher != &this_flusher)
2352 goto out_unlock;
2353
Tejun Heo73f53c42010-06-29 10:07:11 +02002354 wq->first_flusher = NULL;
2355
2356 BUG_ON(!list_empty(&this_flusher.list));
2357 BUG_ON(wq->flush_color != this_flusher.flush_color);
2358
2359 while (true) {
2360 struct wq_flusher *next, *tmp;
2361
2362 /* complete all the flushers sharing the current flush color */
2363 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2364 if (next->flush_color != wq->flush_color)
2365 break;
2366 list_del_init(&next->list);
2367 complete(&next->done);
2368 }
2369
2370 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2371 wq->flush_color != work_next_color(wq->work_color));
2372
2373 /* this flush_color is finished, advance by one */
2374 wq->flush_color = work_next_color(wq->flush_color);
2375
2376 /* one color has been freed, handle overflow queue */
2377 if (!list_empty(&wq->flusher_overflow)) {
2378 /*
2379 * Assign the same color to all overflowed
2380 * flushers, advance work_color and append to
2381 * flusher_queue. This is the start-to-wait
2382 * phase for these overflowed flushers.
2383 */
2384 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2385 tmp->flush_color = wq->work_color;
2386
2387 wq->work_color = work_next_color(wq->work_color);
2388
2389 list_splice_tail_init(&wq->flusher_overflow,
2390 &wq->flusher_queue);
2391 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2392 }
2393
2394 if (list_empty(&wq->flusher_queue)) {
2395 BUG_ON(wq->flush_color != wq->work_color);
2396 break;
2397 }
2398
2399 /*
2400 * Need to flush more colors. Make the next flusher
2401 * the new first flusher and arm cwqs.
2402 */
2403 BUG_ON(wq->flush_color == wq->work_color);
2404 BUG_ON(wq->flush_color != next->flush_color);
2405
2406 list_del_init(&next->list);
2407 wq->first_flusher = next;
2408
2409 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2410 break;
2411
2412 /*
2413 * Meh... this color is already done, clear first
2414 * flusher and repeat cascading.
2415 */
2416 wq->first_flusher = NULL;
2417 }
2418
2419out_unlock:
2420 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
Dave Jonesae90dd52006-06-30 01:40:45 -04002422EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002424/**
2425 * drain_workqueue - drain a workqueue
2426 * @wq: workqueue to drain
2427 *
2428 * Wait until the workqueue becomes empty. While draining is in progress,
2429 * only chain queueing is allowed. IOW, only currently pending or running
2430 * work items on @wq can queue further work items on it. @wq is flushed
2431 * repeatedly until it becomes empty. The number of flushing is detemined
2432 * by the depth of chaining and should be relatively short. Whine if it
2433 * takes too long.
2434 */
2435void drain_workqueue(struct workqueue_struct *wq)
2436{
2437 unsigned int flush_cnt = 0;
2438 unsigned int cpu;
2439
2440 /*
2441 * __queue_work() needs to test whether there are drainers, is much
2442 * hotter than drain_workqueue() and already looks at @wq->flags.
2443 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2444 */
2445 spin_lock(&workqueue_lock);
2446 if (!wq->nr_drainers++)
2447 wq->flags |= WQ_DRAINING;
2448 spin_unlock(&workqueue_lock);
2449reflush:
2450 flush_workqueue(wq);
2451
2452 for_each_cwq_cpu(cpu, wq) {
2453 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002454 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002455
Tejun Heobd7bdd42012-07-12 14:46:37 -07002456 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002457 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002458 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002459
2460 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002461 continue;
2462
2463 if (++flush_cnt == 10 ||
2464 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2465 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2466 wq->name, flush_cnt);
2467 goto reflush;
2468 }
2469
2470 spin_lock(&workqueue_lock);
2471 if (!--wq->nr_drainers)
2472 wq->flags &= ~WQ_DRAINING;
2473 spin_unlock(&workqueue_lock);
2474}
2475EXPORT_SYMBOL_GPL(drain_workqueue);
2476
Tejun Heobaf59022010-09-16 10:42:16 +02002477static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2478 bool wait_executing)
2479{
2480 struct worker *worker = NULL;
2481 struct global_cwq *gcwq;
2482 struct cpu_workqueue_struct *cwq;
2483
2484 might_sleep();
2485 gcwq = get_work_gcwq(work);
2486 if (!gcwq)
2487 return false;
2488
2489 spin_lock_irq(&gcwq->lock);
2490 if (!list_empty(&work->entry)) {
2491 /*
2492 * See the comment near try_to_grab_pending()->smp_rmb().
2493 * If it was re-queued to a different gcwq under us, we
2494 * are not going to wait.
2495 */
2496 smp_rmb();
2497 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002498 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002499 goto already_gone;
2500 } else if (wait_executing) {
2501 worker = find_worker_executing_work(gcwq, work);
2502 if (!worker)
2503 goto already_gone;
2504 cwq = worker->current_cwq;
2505 } else
2506 goto already_gone;
2507
2508 insert_wq_barrier(cwq, barr, work, worker);
2509 spin_unlock_irq(&gcwq->lock);
2510
Tejun Heoe1594892011-01-09 23:32:15 +01002511 /*
2512 * If @max_active is 1 or rescuer is in use, flushing another work
2513 * item on the same workqueue may lead to deadlock. Make sure the
2514 * flusher is not running on the same workqueue by verifying write
2515 * access.
2516 */
2517 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2518 lock_map_acquire(&cwq->wq->lockdep_map);
2519 else
2520 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002521 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002522
Tejun Heobaf59022010-09-16 10:42:16 +02002523 return true;
2524already_gone:
2525 spin_unlock_irq(&gcwq->lock);
2526 return false;
2527}
2528
Oleg Nesterovdb700892008-07-25 01:47:49 -07002529/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002530 * flush_work - wait for a work to finish executing the last queueing instance
2531 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002532 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002533 * Wait until @work has finished execution. This function considers
2534 * only the last queueing instance of @work. If @work has been
2535 * enqueued across different CPUs on a non-reentrant workqueue or on
2536 * multiple workqueues, @work might still be executing on return on
2537 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002538 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002539 * If @work was queued only on a non-reentrant, ordered or unbound
2540 * workqueue, @work is guaranteed to be idle on return if it hasn't
2541 * been requeued since flush started.
2542 *
2543 * RETURNS:
2544 * %true if flush_work() waited for the work to finish execution,
2545 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002546 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002547bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002548{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002549 struct wq_barrier barr;
2550
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002551 lock_map_acquire(&work->lockdep_map);
2552 lock_map_release(&work->lockdep_map);
2553
Tejun Heobaf59022010-09-16 10:42:16 +02002554 if (start_flush_work(work, &barr, true)) {
2555 wait_for_completion(&barr.done);
2556 destroy_work_on_stack(&barr.work);
2557 return true;
2558 } else
2559 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002560}
2561EXPORT_SYMBOL_GPL(flush_work);
2562
Tejun Heo401a8d02010-09-16 10:36:00 +02002563static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2564{
2565 struct wq_barrier barr;
2566 struct worker *worker;
2567
2568 spin_lock_irq(&gcwq->lock);
2569
2570 worker = find_worker_executing_work(gcwq, work);
2571 if (unlikely(worker))
2572 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2573
2574 spin_unlock_irq(&gcwq->lock);
2575
2576 if (unlikely(worker)) {
2577 wait_for_completion(&barr.done);
2578 destroy_work_on_stack(&barr.work);
2579 return true;
2580 } else
2581 return false;
2582}
2583
2584static bool wait_on_work(struct work_struct *work)
2585{
2586 bool ret = false;
2587 int cpu;
2588
2589 might_sleep();
2590
2591 lock_map_acquire(&work->lockdep_map);
2592 lock_map_release(&work->lockdep_map);
2593
2594 for_each_gcwq_cpu(cpu)
2595 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2596 return ret;
2597}
2598
Tejun Heo09383492010-09-16 10:48:29 +02002599/**
2600 * flush_work_sync - wait until a work has finished execution
2601 * @work: the work to flush
2602 *
2603 * Wait until @work has finished execution. On return, it's
2604 * guaranteed that all queueing instances of @work which happened
2605 * before this function is called are finished. In other words, if
2606 * @work hasn't been requeued since this function was called, @work is
2607 * guaranteed to be idle on return.
2608 *
2609 * RETURNS:
2610 * %true if flush_work_sync() waited for the work to finish execution,
2611 * %false if it was already idle.
2612 */
2613bool flush_work_sync(struct work_struct *work)
2614{
2615 struct wq_barrier barr;
2616 bool pending, waited;
2617
2618 /* we'll wait for executions separately, queue barr only if pending */
2619 pending = start_flush_work(work, &barr, false);
2620
2621 /* wait for executions to finish */
2622 waited = wait_on_work(work);
2623
2624 /* wait for the pending one */
2625 if (pending) {
2626 wait_for_completion(&barr.done);
2627 destroy_work_on_stack(&barr.work);
2628 }
2629
2630 return pending || waited;
2631}
2632EXPORT_SYMBOL_GPL(flush_work_sync);
2633
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002634/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002635 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002636 * so this work can't be re-armed in any way.
2637 */
2638static int try_to_grab_pending(struct work_struct *work)
2639{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002640 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002641 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002642
Tejun Heo22df02b2010-06-29 10:07:10 +02002643 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002644 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002645
2646 /*
2647 * The queueing is in progress, or it is already queued. Try to
2648 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2649 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002650 gcwq = get_work_gcwq(work);
2651 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002652 return ret;
2653
Tejun Heo8b03ae32010-06-29 10:07:12 +02002654 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002655 if (!list_empty(&work->entry)) {
2656 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002657 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002658 * In that case we must see the new value after rmb(), see
2659 * insert_work()->wmb().
2660 */
2661 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002662 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002663 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002664 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002665 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002666 get_work_color(work),
2667 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002668 ret = 1;
2669 }
2670 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002671 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002672
2673 return ret;
2674}
2675
Tejun Heo401a8d02010-09-16 10:36:00 +02002676static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002677 struct timer_list* timer)
2678{
2679 int ret;
2680
2681 do {
2682 ret = (timer && likely(del_timer(timer)));
2683 if (!ret)
2684 ret = try_to_grab_pending(work);
2685 wait_on_work(work);
2686 } while (unlikely(ret < 0));
2687
Tejun Heo7a22ad72010-06-29 10:07:13 +02002688 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002689 return ret;
2690}
2691
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002692/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002693 * cancel_work_sync - cancel a work and wait for it to finish
2694 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002695 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002696 * Cancel @work and wait for its execution to finish. This function
2697 * can be used even if the work re-queues itself or migrates to
2698 * another workqueue. On return from this function, @work is
2699 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002700 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002701 * cancel_work_sync(&delayed_work->work) must not be used for
2702 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002703 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002704 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002705 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002706 *
2707 * RETURNS:
2708 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002709 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002710bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002711{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002712 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002713}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002714EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002715
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002716/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002717 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2718 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002720 * Delayed timer is cancelled and the pending work is queued for
2721 * immediate execution. Like flush_work(), this function only
2722 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002723 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002724 * RETURNS:
2725 * %true if flush_work() waited for the work to finish execution,
2726 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002727 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002728bool flush_delayed_work(struct delayed_work *dwork)
2729{
2730 if (del_timer_sync(&dwork->timer))
2731 __queue_work(raw_smp_processor_id(),
2732 get_work_cwq(&dwork->work)->wq, &dwork->work);
2733 return flush_work(&dwork->work);
2734}
2735EXPORT_SYMBOL(flush_delayed_work);
2736
2737/**
Tejun Heo09383492010-09-16 10:48:29 +02002738 * flush_delayed_work_sync - wait for a dwork to finish
2739 * @dwork: the delayed work to flush
2740 *
2741 * Delayed timer is cancelled and the pending work is queued for
2742 * execution immediately. Other than timer handling, its behavior
2743 * is identical to flush_work_sync().
2744 *
2745 * RETURNS:
2746 * %true if flush_work_sync() waited for the work to finish execution,
2747 * %false if it was already idle.
2748 */
2749bool flush_delayed_work_sync(struct delayed_work *dwork)
2750{
2751 if (del_timer_sync(&dwork->timer))
2752 __queue_work(raw_smp_processor_id(),
2753 get_work_cwq(&dwork->work)->wq, &dwork->work);
2754 return flush_work_sync(&dwork->work);
2755}
2756EXPORT_SYMBOL(flush_delayed_work_sync);
2757
2758/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002759 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2760 * @dwork: the delayed work cancel
2761 *
2762 * This is cancel_work_sync() for delayed works.
2763 *
2764 * RETURNS:
2765 * %true if @dwork was pending, %false otherwise.
2766 */
2767bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002768{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002769 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002770}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002771EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002773/**
2774 * schedule_work - put work task in global workqueue
2775 * @work: job to be done
2776 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002777 * Returns zero if @work was already on the kernel-global workqueue and
2778 * non-zero otherwise.
2779 *
2780 * This puts a job in the kernel-global workqueue if it was not already
2781 * queued and leaves it in the same position on the kernel-global
2782 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002783 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002784int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Tejun Heod320c032010-06-29 10:07:14 +02002786 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
Dave Jonesae90dd52006-06-30 01:40:45 -04002788EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Zhang Ruic1a220e2008-07-23 21:28:39 -07002790/*
2791 * schedule_work_on - put work task on a specific cpu
2792 * @cpu: cpu to put the work task on
2793 * @work: job to be done
2794 *
2795 * This puts a job on a specific cpu
2796 */
2797int schedule_work_on(int cpu, struct work_struct *work)
2798{
Tejun Heod320c032010-06-29 10:07:14 +02002799 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002800}
2801EXPORT_SYMBOL(schedule_work_on);
2802
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002803/**
2804 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002805 * @dwork: job to be done
2806 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002807 *
2808 * After waiting for a given time this puts a job in the kernel-global
2809 * workqueue.
2810 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002811int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002812 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Tejun Heod320c032010-06-29 10:07:14 +02002814 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
Dave Jonesae90dd52006-06-30 01:40:45 -04002816EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002818/**
2819 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2820 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002821 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002822 * @delay: number of jiffies to wait
2823 *
2824 * After waiting for a given time this puts a job in the kernel-global
2825 * workqueue on the specified CPU.
2826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002828 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Tejun Heod320c032010-06-29 10:07:14 +02002830 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
Dave Jonesae90dd52006-06-30 01:40:45 -04002832EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Andrew Mortonb6136772006-06-25 05:47:49 -07002834/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002835 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002836 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002837 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002838 * schedule_on_each_cpu() executes @func on each online CPU using the
2839 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002840 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002841 *
2842 * RETURNS:
2843 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002844 */
David Howells65f27f32006-11-22 14:55:48 +00002845int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002846{
2847 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002848 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002849
Andrew Mortonb6136772006-06-25 05:47:49 -07002850 works = alloc_percpu(struct work_struct);
2851 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002852 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002853
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002854 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002855
Christoph Lameter15316ba2006-01-08 01:00:43 -08002856 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002857 struct work_struct *work = per_cpu_ptr(works, cpu);
2858
2859 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002860 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002861 }
Tejun Heo93981802009-11-17 14:06:20 -08002862
2863 for_each_online_cpu(cpu)
2864 flush_work(per_cpu_ptr(works, cpu));
2865
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002866 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002867 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002868 return 0;
2869}
2870
Alan Sterneef6a7d2010-02-12 17:39:21 +09002871/**
2872 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2873 *
2874 * Forces execution of the kernel-global workqueue and blocks until its
2875 * completion.
2876 *
2877 * Think twice before calling this function! It's very easy to get into
2878 * trouble if you don't take great care. Either of the following situations
2879 * will lead to deadlock:
2880 *
2881 * One of the work items currently on the workqueue needs to acquire
2882 * a lock held by your code or its caller.
2883 *
2884 * Your code is running in the context of a work routine.
2885 *
2886 * They will be detected by lockdep when they occur, but the first might not
2887 * occur very often. It depends on what work items are on the workqueue and
2888 * what locks they need, which you have no control over.
2889 *
2890 * In most situations flushing the entire workqueue is overkill; you merely
2891 * need to know that a particular work item isn't queued and isn't running.
2892 * In such cases you should use cancel_delayed_work_sync() or
2893 * cancel_work_sync() instead.
2894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895void flush_scheduled_work(void)
2896{
Tejun Heod320c032010-06-29 10:07:14 +02002897 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
Dave Jonesae90dd52006-06-30 01:40:45 -04002899EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002902 * execute_in_process_context - reliably execute the routine with user context
2903 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002904 * @ew: guaranteed storage for the execute work structure (must
2905 * be available when the work executes)
2906 *
2907 * Executes the function immediately if process context is available,
2908 * otherwise schedules the function for delayed execution.
2909 *
2910 * Returns: 0 - function was executed
2911 * 1 - function was scheduled for execution
2912 */
David Howells65f27f32006-11-22 14:55:48 +00002913int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002914{
2915 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002916 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002917 return 0;
2918 }
2919
David Howells65f27f32006-11-22 14:55:48 +00002920 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002921 schedule_work(&ew->work);
2922
2923 return 1;
2924}
2925EXPORT_SYMBOL_GPL(execute_in_process_context);
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927int keventd_up(void)
2928{
Tejun Heod320c032010-06-29 10:07:14 +02002929 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002932static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002934 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002935 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2936 * Make sure that the alignment isn't lower than that of
2937 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002938 */
Tejun Heo0f900042010-06-29 10:07:11 +02002939 const size_t size = sizeof(struct cpu_workqueue_struct);
2940 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2941 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002942
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002943 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002944 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002945 else {
Tejun Heof3421792010-07-02 10:03:51 +02002946 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002947
Tejun Heof3421792010-07-02 10:03:51 +02002948 /*
2949 * Allocate enough room to align cwq and put an extra
2950 * pointer at the end pointing back to the originally
2951 * allocated pointer which will be used for free.
2952 */
2953 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2954 if (ptr) {
2955 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2956 *(void **)(wq->cpu_wq.single + 1) = ptr;
2957 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002958 }
Tejun Heof3421792010-07-02 10:03:51 +02002959
Tejun Heo0415b00d12011-03-24 18:50:09 +01002960 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002961 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2962 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002963}
2964
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002965static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002966{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002967 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002968 free_percpu(wq->cpu_wq.pcpu);
2969 else if (wq->cpu_wq.single) {
2970 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002971 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002972 }
2973}
2974
Tejun Heof3421792010-07-02 10:03:51 +02002975static int wq_clamp_max_active(int max_active, unsigned int flags,
2976 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002977{
Tejun Heof3421792010-07-02 10:03:51 +02002978 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2979
2980 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002981 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2982 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002983 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002984
Tejun Heof3421792010-07-02 10:03:51 +02002985 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002986}
2987
Tejun Heob196be82012-01-10 15:11:35 -08002988struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002989 unsigned int flags,
2990 int max_active,
2991 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002992 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002993{
Tejun Heob196be82012-01-10 15:11:35 -08002994 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002995 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002996 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002997 size_t namelen;
2998
2999 /* determine namelen, allocate wq and format name */
3000 va_start(args, lock_name);
3001 va_copy(args1, args);
3002 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3003
3004 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3005 if (!wq)
3006 goto err;
3007
3008 vsnprintf(wq->name, namelen, fmt, args1);
3009 va_end(args);
3010 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003011
Tejun Heof3421792010-07-02 10:03:51 +02003012 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003013 * Workqueues which may be used during memory reclaim should
3014 * have a rescuer to guarantee forward progress.
3015 */
3016 if (flags & WQ_MEM_RECLAIM)
3017 flags |= WQ_RESCUER;
3018
Tejun Heod320c032010-06-29 10:07:14 +02003019 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003020 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003021
Tejun Heob196be82012-01-10 15:11:35 -08003022 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003023 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003024 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003025 mutex_init(&wq->flush_mutex);
3026 atomic_set(&wq->nr_cwqs_to_flush, 0);
3027 INIT_LIST_HEAD(&wq->flusher_queue);
3028 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003029
Johannes Bergeb13ba82008-01-16 09:51:58 +01003030 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003031 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003032
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003033 if (alloc_cwqs(wq) < 0)
3034 goto err;
3035
Tejun Heof3421792010-07-02 10:03:51 +02003036 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003037 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003038 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003039
Tejun Heo0f900042010-06-29 10:07:11 +02003040 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003041 cwq->pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02003042 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003043 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003044 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003045 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003046 }
3047
Tejun Heoe22bee72010-06-29 10:07:14 +02003048 if (flags & WQ_RESCUER) {
3049 struct worker *rescuer;
3050
Tejun Heof2e005a2010-07-20 15:59:09 +02003051 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003052 goto err;
3053
3054 wq->rescuer = rescuer = alloc_worker();
3055 if (!rescuer)
3056 goto err;
3057
Tejun Heob196be82012-01-10 15:11:35 -08003058 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3059 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003060 if (IS_ERR(rescuer->task))
3061 goto err;
3062
Tejun Heoe22bee72010-06-29 10:07:14 +02003063 rescuer->task->flags |= PF_THREAD_BOUND;
3064 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003065 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003066
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003067 /*
3068 * workqueue_lock protects global freeze state and workqueues
3069 * list. Grab it, set max_active accordingly and add the new
3070 * workqueue to workqueues list.
3071 */
Tejun Heo15376632010-06-29 10:07:11 +02003072 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003073
Tejun Heo58a69cb2011-02-16 09:25:31 +01003074 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003075 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003076 get_cwq(cpu, wq)->max_active = 0;
3077
Tejun Heo15376632010-06-29 10:07:11 +02003078 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003079
Tejun Heo15376632010-06-29 10:07:11 +02003080 spin_unlock(&workqueue_lock);
3081
Oleg Nesterov3af244332007-05-09 02:34:09 -07003082 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003083err:
3084 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003085 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003086 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003087 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003088 kfree(wq);
3089 }
3090 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003091}
Tejun Heod320c032010-06-29 10:07:14 +02003092EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003093
3094/**
3095 * destroy_workqueue - safely terminate a workqueue
3096 * @wq: target workqueue
3097 *
3098 * Safely destroy a workqueue. All work currently pending will be done first.
3099 */
3100void destroy_workqueue(struct workqueue_struct *wq)
3101{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003102 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003103
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003104 /* drain it before proceeding with destruction */
3105 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003106
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003107 /*
3108 * wq list is used to freeze wq, remove from list after
3109 * flushing is complete in case freeze races us.
3110 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003111 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003112 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003113 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003114
Tejun Heoe22bee72010-06-29 10:07:14 +02003115 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003116 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003117 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3118 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003119
Tejun Heo73f53c42010-06-29 10:07:11 +02003120 for (i = 0; i < WORK_NR_COLORS; i++)
3121 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003122 BUG_ON(cwq->nr_active);
3123 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003124 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125
Tejun Heoe22bee72010-06-29 10:07:14 +02003126 if (wq->flags & WQ_RESCUER) {
3127 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003128 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003129 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003130 }
3131
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003132 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003133 kfree(wq);
3134}
3135EXPORT_SYMBOL_GPL(destroy_workqueue);
3136
Tejun Heodcd989c2010-06-29 10:07:14 +02003137/**
3138 * workqueue_set_max_active - adjust max_active of a workqueue
3139 * @wq: target workqueue
3140 * @max_active: new max_active value.
3141 *
3142 * Set max_active of @wq to @max_active.
3143 *
3144 * CONTEXT:
3145 * Don't call from IRQ context.
3146 */
3147void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3148{
3149 unsigned int cpu;
3150
Tejun Heof3421792010-07-02 10:03:51 +02003151 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003152
3153 spin_lock(&workqueue_lock);
3154
3155 wq->saved_max_active = max_active;
3156
Tejun Heof3421792010-07-02 10:03:51 +02003157 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003158 struct global_cwq *gcwq = get_gcwq(cpu);
3159
3160 spin_lock_irq(&gcwq->lock);
3161
Tejun Heo58a69cb2011-02-16 09:25:31 +01003162 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003163 !(gcwq->flags & GCWQ_FREEZING))
3164 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3165
3166 spin_unlock_irq(&gcwq->lock);
3167 }
3168
3169 spin_unlock(&workqueue_lock);
3170}
3171EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3172
3173/**
3174 * workqueue_congested - test whether a workqueue is congested
3175 * @cpu: CPU in question
3176 * @wq: target workqueue
3177 *
3178 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3179 * no synchronization around this function and the test result is
3180 * unreliable and only useful as advisory hints or for debugging.
3181 *
3182 * RETURNS:
3183 * %true if congested, %false otherwise.
3184 */
3185bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3186{
3187 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3188
3189 return !list_empty(&cwq->delayed_works);
3190}
3191EXPORT_SYMBOL_GPL(workqueue_congested);
3192
3193/**
3194 * work_cpu - return the last known associated cpu for @work
3195 * @work: the work of interest
3196 *
3197 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003198 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003199 */
3200unsigned int work_cpu(struct work_struct *work)
3201{
3202 struct global_cwq *gcwq = get_work_gcwq(work);
3203
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003204 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003205}
3206EXPORT_SYMBOL_GPL(work_cpu);
3207
3208/**
3209 * work_busy - test whether a work is currently pending or running
3210 * @work: the work to be tested
3211 *
3212 * Test whether @work is currently pending or running. There is no
3213 * synchronization around this function and the test result is
3214 * unreliable and only useful as advisory hints or for debugging.
3215 * Especially for reentrant wqs, the pending state might hide the
3216 * running state.
3217 *
3218 * RETURNS:
3219 * OR'd bitmask of WORK_BUSY_* bits.
3220 */
3221unsigned int work_busy(struct work_struct *work)
3222{
3223 struct global_cwq *gcwq = get_work_gcwq(work);
3224 unsigned long flags;
3225 unsigned int ret = 0;
3226
3227 if (!gcwq)
3228 return false;
3229
3230 spin_lock_irqsave(&gcwq->lock, flags);
3231
3232 if (work_pending(work))
3233 ret |= WORK_BUSY_PENDING;
3234 if (find_worker_executing_work(gcwq, work))
3235 ret |= WORK_BUSY_RUNNING;
3236
3237 spin_unlock_irqrestore(&gcwq->lock, flags);
3238
3239 return ret;
3240}
3241EXPORT_SYMBOL_GPL(work_busy);
3242
Tejun Heodb7bccf2010-06-29 10:07:12 +02003243/*
3244 * CPU hotplug.
3245 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003246 * There are two challenges in supporting CPU hotplug. Firstly, there
3247 * are a lot of assumptions on strong associations among work, cwq and
3248 * gcwq which make migrating pending and scheduled works very
3249 * difficult to implement without impacting hot paths. Secondly,
3250 * gcwqs serve mix of short, long and very long running works making
3251 * blocked draining impractical.
3252 *
3253 * This is solved by allowing a gcwq to be detached from CPU, running
3254 * it with unbound (rogue) workers and allowing it to be reattached
3255 * later if the cpu comes back online. A separate thread is created
3256 * to govern a gcwq in such state and is called the trustee of the
3257 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003258 *
3259 * Trustee states and their descriptions.
3260 *
3261 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3262 * new trustee is started with this state.
3263 *
3264 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003265 * assuming the manager role and making all existing
3266 * workers rogue. DOWN_PREPARE waits for trustee to
3267 * enter this state. After reaching IN_CHARGE, trustee
3268 * tries to execute the pending worklist until it's empty
3269 * and the state is set to BUTCHER, or the state is set
3270 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003271 *
3272 * BUTCHER Command state which is set by the cpu callback after
3273 * the cpu has went down. Once this state is set trustee
3274 * knows that there will be no new works on the worklist
3275 * and once the worklist is empty it can proceed to
3276 * killing idle workers.
3277 *
3278 * RELEASE Command state which is set by the cpu callback if the
3279 * cpu down has been canceled or it has come online
3280 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003281 * trying to drain or butcher and clears ROGUE, rebinds
3282 * all remaining workers back to the cpu and releases
3283 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003284 *
3285 * DONE Trustee will enter this state after BUTCHER or RELEASE
3286 * is complete.
3287 *
3288 * trustee CPU draining
3289 * took over down complete
3290 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3291 * | | ^
3292 * | CPU is back online v return workers |
3293 * ----------------> RELEASE --------------
3294 */
3295
3296/**
3297 * trustee_wait_event_timeout - timed event wait for trustee
3298 * @cond: condition to wait for
3299 * @timeout: timeout in jiffies
3300 *
3301 * wait_event_timeout() for trustee to use. Handles locking and
3302 * checks for RELEASE request.
3303 *
3304 * CONTEXT:
3305 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3306 * multiple times. To be used by trustee.
3307 *
3308 * RETURNS:
3309 * Positive indicating left time if @cond is satisfied, 0 if timed
3310 * out, -1 if canceled.
3311 */
3312#define trustee_wait_event_timeout(cond, timeout) ({ \
3313 long __ret = (timeout); \
3314 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3315 __ret) { \
3316 spin_unlock_irq(&gcwq->lock); \
3317 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3318 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3319 __ret); \
3320 spin_lock_irq(&gcwq->lock); \
3321 } \
3322 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3323})
3324
3325/**
3326 * trustee_wait_event - event wait for trustee
3327 * @cond: condition to wait for
3328 *
3329 * wait_event() for trustee to use. Automatically handles locking and
3330 * checks for CANCEL request.
3331 *
3332 * CONTEXT:
3333 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3334 * multiple times. To be used by trustee.
3335 *
3336 * RETURNS:
3337 * 0 if @cond is satisfied, -1 if canceled.
3338 */
3339#define trustee_wait_event(cond) ({ \
3340 long __ret1; \
3341 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3342 __ret1 < 0 ? -1 : 0; \
3343})
3344
3345static int __cpuinit trustee_thread(void *__gcwq)
3346{
3347 struct global_cwq *gcwq = __gcwq;
3348 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003349 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003350 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003351 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003352 int i;
3353
3354 BUG_ON(gcwq->cpu != smp_processor_id());
3355
3356 spin_lock_irq(&gcwq->lock);
3357 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003358 * Claim the manager position and make all workers rogue.
3359 * Trustee must be bound to the target cpu and can't be
3360 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003361 */
3362 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003363 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3364 BUG_ON(rc < 0);
3365
3366 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003367
Tejun Heobd7bdd42012-07-12 14:46:37 -07003368 list_for_each_entry(worker, &gcwq->pool.idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003369 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003370
3371 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003372 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003373
3374 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003375 * Call schedule() so that we cross rq->lock and thus can
3376 * guarantee sched callbacks see the rogue flag. This is
3377 * necessary as scheduler callbacks may be invoked from other
3378 * cpus.
3379 */
3380 spin_unlock_irq(&gcwq->lock);
3381 schedule();
3382 spin_lock_irq(&gcwq->lock);
3383
3384 /*
Tejun Heocb444762010-07-02 10:03:50 +02003385 * Sched callbacks are disabled now. Zap nr_running. After
3386 * this, nr_running stays zero and need_more_worker() and
3387 * keep_working() are always true as long as the worklist is
3388 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003389 */
Tejun Heo63d95a92012-07-12 14:46:37 -07003390 atomic_set(get_pool_nr_running(&gcwq->pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003391
3392 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003393 del_timer_sync(&gcwq->pool.idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003394 spin_lock_irq(&gcwq->lock);
3395
3396 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003397 * We're now in charge. Notify and proceed to drain. We need
3398 * to keep the gcwq running during the whole CPU down
3399 * procedure as other cpu hotunplug callbacks may need to
3400 * flush currently running tasks.
3401 */
3402 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3403 wake_up_all(&gcwq->trustee_wait);
3404
3405 /*
3406 * The original cpu is in the process of dying and may go away
3407 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003408 * be migrated to other cpus. Try draining any left work. We
3409 * want to get it over with ASAP - spam rescuers, wake up as
3410 * many idlers as necessary and create new ones till the
3411 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003412 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003413 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003414 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07003415 while (gcwq->pool.nr_workers != gcwq->pool.nr_idle ||
Tejun Heodb7bccf2010-06-29 10:07:12 +02003416 gcwq->flags & GCWQ_FREEZING ||
3417 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003418 int nr_works = 0;
3419
Tejun Heobd7bdd42012-07-12 14:46:37 -07003420 list_for_each_entry(work, &gcwq->pool.worklist, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003421 send_mayday(work);
3422 nr_works++;
3423 }
3424
Tejun Heobd7bdd42012-07-12 14:46:37 -07003425 list_for_each_entry(worker, &gcwq->pool.idle_list, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003426 if (!nr_works--)
3427 break;
3428 wake_up_process(worker->task);
3429 }
3430
Tejun Heo63d95a92012-07-12 14:46:37 -07003431 if (need_to_create_worker(&gcwq->pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003432 spin_unlock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07003433 worker = create_worker(&gcwq->pool, false);
Tejun Heoe22bee72010-06-29 10:07:14 +02003434 spin_lock_irq(&gcwq->lock);
3435 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003436 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003437 start_worker(worker);
3438 }
3439 }
3440
Tejun Heodb7bccf2010-06-29 10:07:12 +02003441 /* give a breather */
3442 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3443 break;
3444 }
3445
Tejun Heoe22bee72010-06-29 10:07:14 +02003446 /*
3447 * Either all works have been scheduled and cpu is down, or
3448 * cpu down has already been canceled. Wait for and butcher
3449 * all workers till we're canceled.
3450 */
3451 do {
Tejun Heobd7bdd42012-07-12 14:46:37 -07003452 rc = trustee_wait_event(!list_empty(&gcwq->pool.idle_list));
3453 while (!list_empty(&gcwq->pool.idle_list))
3454 destroy_worker(list_first_entry(&gcwq->pool.idle_list,
Tejun Heoe22bee72010-06-29 10:07:14 +02003455 struct worker, entry));
Tejun Heobd7bdd42012-07-12 14:46:37 -07003456 } while (gcwq->pool.nr_workers && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003457
3458 /*
3459 * At this point, either draining has completed and no worker
3460 * is left, or cpu down has been canceled or the cpu is being
3461 * brought back up. There shouldn't be any idle one left.
3462 * Tell the remaining busy ones to rebind once it finishes the
3463 * currently scheduled works by scheduling the rebind_work.
3464 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07003465 WARN_ON(!list_empty(&gcwq->pool.idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003466
3467 for_each_busy_worker(worker, i, pos, gcwq) {
3468 struct work_struct *rebind_work = &worker->rebind_work;
3469
3470 /*
3471 * Rebind_work may race with future cpu hotplug
3472 * operations. Use a separate flag to mark that
3473 * rebinding is scheduled.
3474 */
Tejun Heocb444762010-07-02 10:03:50 +02003475 worker->flags |= WORKER_REBIND;
3476 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003477
3478 /* queue rebind_work, wq doesn't matter, use the default one */
3479 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3480 work_data_bits(rebind_work)))
3481 continue;
3482
3483 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003484 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003485 worker->scheduled.next,
3486 work_color_to_flags(WORK_NO_COLOR));
3487 }
3488
3489 /* relinquish manager role */
3490 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3491
Tejun Heodb7bccf2010-06-29 10:07:12 +02003492 /* notify completion */
3493 gcwq->trustee = NULL;
3494 gcwq->trustee_state = TRUSTEE_DONE;
3495 wake_up_all(&gcwq->trustee_wait);
3496 spin_unlock_irq(&gcwq->lock);
3497 return 0;
3498}
3499
3500/**
3501 * wait_trustee_state - wait for trustee to enter the specified state
3502 * @gcwq: gcwq the trustee of interest belongs to
3503 * @state: target state to wait for
3504 *
3505 * Wait for the trustee to reach @state. DONE is already matched.
3506 *
3507 * CONTEXT:
3508 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3509 * multiple times. To be used by cpu_callback.
3510 */
3511static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003512__releases(&gcwq->lock)
3513__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003514{
3515 if (!(gcwq->trustee_state == state ||
3516 gcwq->trustee_state == TRUSTEE_DONE)) {
3517 spin_unlock_irq(&gcwq->lock);
3518 __wait_event(gcwq->trustee_wait,
3519 gcwq->trustee_state == state ||
3520 gcwq->trustee_state == TRUSTEE_DONE);
3521 spin_lock_irq(&gcwq->lock);
3522 }
3523}
3524
Oleg Nesterov3af244332007-05-09 02:34:09 -07003525static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3526 unsigned long action,
3527 void *hcpu)
3528{
3529 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003530 struct global_cwq *gcwq = get_gcwq(cpu);
3531 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003532 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003533 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003535 action &= ~CPU_TASKS_FROZEN;
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003538 case CPU_DOWN_PREPARE:
3539 new_trustee = kthread_create(trustee_thread, gcwq,
3540 "workqueue_trustee/%d\n", cpu);
3541 if (IS_ERR(new_trustee))
3542 return notifier_from_errno(PTR_ERR(new_trustee));
3543 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003544 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 case CPU_UP_PREPARE:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003546 BUG_ON(gcwq->pool.first_idle);
Tejun Heo63d95a92012-07-12 14:46:37 -07003547 new_worker = create_worker(&gcwq->pool, false);
Tejun Heoe22bee72010-06-29 10:07:14 +02003548 if (!new_worker) {
3549 if (new_trustee)
3550 kthread_stop(new_trustee);
3551 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
3554
Tejun Heodb7bccf2010-06-29 10:07:12 +02003555 /* some are called w/ irq disabled, don't disturb irq status */
3556 spin_lock_irqsave(&gcwq->lock, flags);
3557
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003558 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003559 case CPU_DOWN_PREPARE:
3560 /* initialize trustee and tell it to acquire the gcwq */
3561 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3562 gcwq->trustee = new_trustee;
3563 gcwq->trustee_state = TRUSTEE_START;
3564 wake_up_process(gcwq->trustee);
3565 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003566 /* fall through */
3567 case CPU_UP_PREPARE:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003568 BUG_ON(gcwq->pool.first_idle);
3569 gcwq->pool.first_idle = new_worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003570 break;
3571
3572 case CPU_DYING:
3573 /*
3574 * Before this, the trustee and all workers except for
3575 * the ones which are still executing works from
3576 * before the last CPU down must be on the cpu. After
3577 * this, they'll all be diasporas.
3578 */
3579 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003580 break;
3581
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003582 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003583 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003584 /* fall through */
3585 case CPU_UP_CANCELED:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003586 destroy_worker(gcwq->pool.first_idle);
3587 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003588 break;
3589
3590 case CPU_DOWN_FAILED:
3591 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003592 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003593 if (gcwq->trustee_state != TRUSTEE_DONE) {
3594 gcwq->trustee_state = TRUSTEE_RELEASE;
3595 wake_up_process(gcwq->trustee);
3596 wait_trustee_state(gcwq, TRUSTEE_DONE);
3597 }
3598
Tejun Heoe22bee72010-06-29 10:07:14 +02003599 /*
3600 * Trustee is done and there might be no worker left.
3601 * Put the first_idle in and request a real manager to
3602 * take a look.
3603 */
3604 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003605 kthread_bind(gcwq->pool.first_idle->task, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003606 spin_lock_irq(&gcwq->lock);
3607 gcwq->flags |= GCWQ_MANAGE_WORKERS;
Tejun Heobd7bdd42012-07-12 14:46:37 -07003608 start_worker(gcwq->pool.first_idle);
3609 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003610 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003611 }
3612
Tejun Heodb7bccf2010-06-29 10:07:12 +02003613 spin_unlock_irqrestore(&gcwq->lock, flags);
3614
Tejun Heo15376632010-06-29 10:07:11 +02003615 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Rusty Russell2d3854a2008-11-05 13:39:10 +11003618#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003619
Rusty Russell2d3854a2008-11-05 13:39:10 +11003620struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06003621 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003622 long (*fn)(void *);
3623 void *arg;
3624 long ret;
3625};
3626
Andrew Morton6b44003e2009-04-09 09:50:37 -06003627static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003628{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003629 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003630 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06003631 complete(&wfc->completion);
3632 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003633}
3634
3635/**
3636 * work_on_cpu - run a function in user context on a particular cpu
3637 * @cpu: the cpu to run on
3638 * @fn: the function to run
3639 * @arg: the function arg
3640 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003641 * This will return the value @fn returns.
3642 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003643 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003644 */
3645long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3646{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003647 struct task_struct *sub_thread;
3648 struct work_for_cpu wfc = {
3649 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3650 .fn = fn,
3651 .arg = arg,
3652 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003653
Andrew Morton6b44003e2009-04-09 09:50:37 -06003654 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3655 if (IS_ERR(sub_thread))
3656 return PTR_ERR(sub_thread);
3657 kthread_bind(sub_thread, cpu);
3658 wake_up_process(sub_thread);
3659 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003660 return wfc.ret;
3661}
3662EXPORT_SYMBOL_GPL(work_on_cpu);
3663#endif /* CONFIG_SMP */
3664
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003665#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303666
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003667/**
3668 * freeze_workqueues_begin - begin freezing workqueues
3669 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003670 * Start freezing workqueues. After this function returns, all freezable
3671 * workqueues will queue new works to their frozen_works list instead of
3672 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003673 *
3674 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003675 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003676 */
3677void freeze_workqueues_begin(void)
3678{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003679 unsigned int cpu;
3680
3681 spin_lock(&workqueue_lock);
3682
3683 BUG_ON(workqueue_freezing);
3684 workqueue_freezing = true;
3685
Tejun Heof3421792010-07-02 10:03:51 +02003686 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003687 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003688 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003689
3690 spin_lock_irq(&gcwq->lock);
3691
Tejun Heodb7bccf2010-06-29 10:07:12 +02003692 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3693 gcwq->flags |= GCWQ_FREEZING;
3694
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003695 list_for_each_entry(wq, &workqueues, list) {
3696 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3697
Tejun Heo58a69cb2011-02-16 09:25:31 +01003698 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003699 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003700 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003701
3702 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 }
3704
3705 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003707
3708/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003709 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003710 *
3711 * Check whether freezing is complete. This function must be called
3712 * between freeze_workqueues_begin() and thaw_workqueues().
3713 *
3714 * CONTEXT:
3715 * Grabs and releases workqueue_lock.
3716 *
3717 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003718 * %true if some freezable workqueues are still busy. %false if freezing
3719 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003720 */
3721bool freeze_workqueues_busy(void)
3722{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003723 unsigned int cpu;
3724 bool busy = false;
3725
3726 spin_lock(&workqueue_lock);
3727
3728 BUG_ON(!workqueue_freezing);
3729
Tejun Heof3421792010-07-02 10:03:51 +02003730 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003731 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003732 /*
3733 * nr_active is monotonically decreasing. It's safe
3734 * to peek without lock.
3735 */
3736 list_for_each_entry(wq, &workqueues, list) {
3737 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3738
Tejun Heo58a69cb2011-02-16 09:25:31 +01003739 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003740 continue;
3741
3742 BUG_ON(cwq->nr_active < 0);
3743 if (cwq->nr_active) {
3744 busy = true;
3745 goto out_unlock;
3746 }
3747 }
3748 }
3749out_unlock:
3750 spin_unlock(&workqueue_lock);
3751 return busy;
3752}
3753
3754/**
3755 * thaw_workqueues - thaw workqueues
3756 *
3757 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003758 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003759 *
3760 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003761 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003762 */
3763void thaw_workqueues(void)
3764{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003765 unsigned int cpu;
3766
3767 spin_lock(&workqueue_lock);
3768
3769 if (!workqueue_freezing)
3770 goto out_unlock;
3771
Tejun Heof3421792010-07-02 10:03:51 +02003772 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003773 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003774 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003775
3776 spin_lock_irq(&gcwq->lock);
3777
Tejun Heodb7bccf2010-06-29 10:07:12 +02003778 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3779 gcwq->flags &= ~GCWQ_FREEZING;
3780
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003781 list_for_each_entry(wq, &workqueues, list) {
3782 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3783
Tejun Heo58a69cb2011-02-16 09:25:31 +01003784 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003785 continue;
3786
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003787 /* restore max_active and repopulate worklist */
3788 cwq->max_active = wq->saved_max_active;
3789
3790 while (!list_empty(&cwq->delayed_works) &&
3791 cwq->nr_active < cwq->max_active)
3792 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003793 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003794
Tejun Heo63d95a92012-07-12 14:46:37 -07003795 wake_up_worker(&gcwq->pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003796
Tejun Heo8b03ae32010-06-29 10:07:12 +02003797 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003798 }
3799
3800 workqueue_freezing = false;
3801out_unlock:
3802 spin_unlock(&workqueue_lock);
3803}
3804#endif /* CONFIG_FREEZER */
3805
Suresh Siddha6ee05782010-07-30 14:57:37 -07003806static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Tejun Heoc34056a2010-06-29 10:07:11 +02003808 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003809 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003810
Tejun Heof6500942010-08-09 11:50:34 +02003811 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003812
3813 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003814 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003815 struct global_cwq *gcwq = get_gcwq(cpu);
3816
3817 spin_lock_init(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003818 gcwq->pool.gcwq = gcwq;
3819 INIT_LIST_HEAD(&gcwq->pool.worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003820 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003821 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003822
Tejun Heobd7bdd42012-07-12 14:46:37 -07003823 INIT_LIST_HEAD(&gcwq->pool.idle_list);
Tejun Heoc8e55f32010-06-29 10:07:12 +02003824 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3825 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3826
Tejun Heobd7bdd42012-07-12 14:46:37 -07003827 init_timer_deferrable(&gcwq->pool.idle_timer);
3828 gcwq->pool.idle_timer.function = idle_worker_timeout;
Tejun Heo63d95a92012-07-12 14:46:37 -07003829 gcwq->pool.idle_timer.data = (unsigned long)&gcwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003830
Tejun Heobd7bdd42012-07-12 14:46:37 -07003831 setup_timer(&gcwq->pool.mayday_timer, gcwq_mayday_timeout,
Tejun Heo63d95a92012-07-12 14:46:37 -07003832 (unsigned long)&gcwq->pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003833
Tejun Heobd7bdd42012-07-12 14:46:37 -07003834 ida_init(&gcwq->pool.worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003835
3836 gcwq->trustee_state = TRUSTEE_DONE;
3837 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003838 }
3839
Tejun Heoe22bee72010-06-29 10:07:14 +02003840 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003841 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003842 struct global_cwq *gcwq = get_gcwq(cpu);
3843 struct worker *worker;
3844
Tejun Heo477a3c32010-08-31 10:54:35 +02003845 if (cpu != WORK_CPU_UNBOUND)
3846 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo63d95a92012-07-12 14:46:37 -07003847 worker = create_worker(&gcwq->pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02003848 BUG_ON(!worker);
3849 spin_lock_irq(&gcwq->lock);
3850 start_worker(worker);
3851 spin_unlock_irq(&gcwq->lock);
3852 }
3853
Tejun Heod320c032010-06-29 10:07:14 +02003854 system_wq = alloc_workqueue("events", 0, 0);
3855 system_long_wq = alloc_workqueue("events_long", 0, 0);
3856 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003857 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3858 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003859 system_freezable_wq = alloc_workqueue("events_freezable",
3860 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003861 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3862 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003863 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003864 !system_unbound_wq || !system_freezable_wq ||
3865 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003868early_initcall(init_workqueues);