blob: 61f154467026f18f0d988586e2f9e784b36f44ad [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 Heoe22bee72010-06-29 10:07:14 +0200474static atomic_t *get_gcwq_nr_running(unsigned int cpu)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700475{
Tejun Heof3421792010-07-02 10:03:51 +0200476 if (cpu != WORK_CPU_UNBOUND)
477 return &per_cpu(gcwq_nr_running, cpu);
478 else
479 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700480}
481
Tejun Heo4690c4a2010-06-29 10:07:10 +0200482static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
483 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700484{
Tejun Heof3421792010-07-02 10:03:51 +0200485 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800486 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200487 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200488 } else if (likely(cpu == WORK_CPU_UNBOUND))
489 return wq->cpu_wq.single;
490 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700491}
492
Tejun Heo73f53c42010-06-29 10:07:11 +0200493static unsigned int work_color_to_flags(int color)
494{
495 return color << WORK_STRUCT_COLOR_SHIFT;
496}
497
498static int get_work_color(struct work_struct *work)
499{
500 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
501 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
502}
503
504static int work_next_color(int color)
505{
506 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
David Howells4594bf12006-12-07 11:33:26 +0000509/*
Tejun Heoe1201532010-07-22 14:14:25 +0200510 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
511 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
512 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200513 *
514 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
515 * cwq, cpu or clear work->data. These functions should only be
516 * called while the work is owned - ie. while the PENDING bit is set.
517 *
518 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
519 * corresponding to a work. gcwq is available once the work has been
520 * queued anywhere after initialization. cwq is available only from
521 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000522 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200523static inline void set_work_data(struct work_struct *work, unsigned long data,
524 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000525{
David Howells4594bf12006-12-07 11:33:26 +0000526 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200527 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000528}
David Howells365970a2006-11-22 14:54:49 +0000529
Tejun Heo7a22ad72010-06-29 10:07:13 +0200530static void set_work_cwq(struct work_struct *work,
531 struct cpu_workqueue_struct *cwq,
532 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200533{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200534 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200535 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200536}
537
Tejun Heo7a22ad72010-06-29 10:07:13 +0200538static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000539{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
541}
542
543static void clear_work_data(struct work_struct *work)
544{
545 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
546}
547
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
549{
Tejun Heoe1201532010-07-22 14:14:25 +0200550 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551
Tejun Heoe1201532010-07-22 14:14:25 +0200552 if (data & WORK_STRUCT_CWQ)
553 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
554 else
555 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200556}
557
558static struct global_cwq *get_work_gcwq(struct work_struct *work)
559{
Tejun Heoe1201532010-07-22 14:14:25 +0200560 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 unsigned int cpu;
562
Tejun Heoe1201532010-07-22 14:14:25 +0200563 if (data & WORK_STRUCT_CWQ)
564 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700565 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566
567 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200568 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569 return NULL;
570
Tejun Heof3421792010-07-02 10:03:51 +0200571 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000573}
574
575/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200576 * Policy functions. These define the policies on how the global
577 * worker pool is managed. Unless noted otherwise, these functions
578 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000579 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200580
Tejun Heo649027d2010-06-29 10:07:14 +0200581static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000582{
Tejun Heo649027d2010-06-29 10:07:14 +0200583 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
584 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000585}
586
Tejun Heoe22bee72010-06-29 10:07:14 +0200587/*
588 * Need to wake up a worker? Called from anything but currently
589 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700590 *
591 * Note that, because unbound workers never contribute to nr_running, this
592 * function will always return %true for unbound gcwq as long as the
593 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200594 */
595static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000596{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700597 return !list_empty(&gcwq->pool.worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000598}
599
Tejun Heoe22bee72010-06-29 10:07:14 +0200600/* Can I start working? Called from busy but !running workers. */
601static bool may_start_working(struct global_cwq *gcwq)
602{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700603 return gcwq->pool.nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200604}
605
606/* Do I need to keep working? Called from currently running workers. */
607static bool keep_working(struct global_cwq *gcwq)
608{
609 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
610
Tejun Heobd7bdd42012-07-12 14:46:37 -0700611 return !list_empty(&gcwq->pool.worklist) &&
Tejun Heo30310042010-10-11 11:51:57 +0200612 (atomic_read(nr_running) <= 1 ||
613 gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200614}
615
616/* Do we need a new worker? Called from manager. */
617static bool need_to_create_worker(struct global_cwq *gcwq)
618{
619 return need_more_worker(gcwq) && !may_start_working(gcwq);
620}
621
622/* Do I need to be the manager? */
623static bool need_to_manage_workers(struct global_cwq *gcwq)
624{
625 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
626}
627
628/* Do we have too many workers and should some go away? */
629static bool too_many_workers(struct global_cwq *gcwq)
630{
631 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700632 int nr_idle = gcwq->pool.nr_idle + managing; /* manager is considered idle */
633 int nr_busy = gcwq->pool.nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200634
635 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
636}
637
638/*
639 * Wake up functions.
640 */
641
Tejun Heo7e116292010-06-29 10:07:13 +0200642/* Return the first worker. Safe with preemption disabled */
643static struct worker *first_worker(struct global_cwq *gcwq)
644{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700645 if (unlikely(list_empty(&gcwq->pool.idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200646 return NULL;
647
Tejun Heobd7bdd42012-07-12 14:46:37 -0700648 return list_first_entry(&gcwq->pool.idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200649}
650
651/**
652 * wake_up_worker - wake up an idle worker
653 * @gcwq: gcwq to wake worker for
654 *
655 * Wake up the first idle worker of @gcwq.
656 *
657 * CONTEXT:
658 * spin_lock_irq(gcwq->lock).
659 */
660static void wake_up_worker(struct global_cwq *gcwq)
661{
662 struct worker *worker = first_worker(gcwq);
663
664 if (likely(worker))
665 wake_up_process(worker->task);
666}
667
Tejun Heo4690c4a2010-06-29 10:07:10 +0200668/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200669 * wq_worker_waking_up - a worker is waking up
670 * @task: task waking up
671 * @cpu: CPU @task is waking up to
672 *
673 * This function is called during try_to_wake_up() when a worker is
674 * being awoken.
675 *
676 * CONTEXT:
677 * spin_lock_irq(rq->lock)
678 */
679void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
680{
681 struct worker *worker = kthread_data(task);
682
Steven Rostedt2d646722010-12-03 23:12:33 -0500683 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe22bee72010-06-29 10:07:14 +0200684 atomic_inc(get_gcwq_nr_running(cpu));
685}
686
687/**
688 * wq_worker_sleeping - a worker is going to sleep
689 * @task: task going to sleep
690 * @cpu: CPU in question, must be the current CPU number
691 *
692 * This function is called during schedule() when a busy worker is
693 * going to sleep. Worker on the same cpu can be woken up by
694 * returning pointer to its task.
695 *
696 * CONTEXT:
697 * spin_lock_irq(rq->lock)
698 *
699 * RETURNS:
700 * Worker task on @cpu to wake up, %NULL if none.
701 */
702struct task_struct *wq_worker_sleeping(struct task_struct *task,
703 unsigned int cpu)
704{
705 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700706 struct worker_pool *pool = worker->pool;
707 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +0200708 atomic_t *nr_running = get_gcwq_nr_running(cpu);
709
Steven Rostedt2d646722010-12-03 23:12:33 -0500710 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200711 return NULL;
712
713 /* this can only happen on the local cpu */
714 BUG_ON(cpu != raw_smp_processor_id());
715
716 /*
717 * The counterpart of the following dec_and_test, implied mb,
718 * worklist not empty test sequence is in insert_work().
719 * Please read comment there.
720 *
721 * NOT_RUNNING is clear. This means that trustee is not in
722 * charge and we're running on the local cpu w/ rq lock held
723 * and preemption disabled, which in turn means that none else
724 * could be manipulating idle_list, so dereferencing idle_list
725 * without gcwq lock is safe.
726 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700727 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heoe22bee72010-06-29 10:07:14 +0200728 to_wakeup = first_worker(gcwq);
729 return to_wakeup ? to_wakeup->task : NULL;
730}
731
732/**
733 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200734 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200735 * @flags: flags to set
736 * @wakeup: wakeup an idle worker if necessary
737 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200738 * Set @flags in @worker->flags and adjust nr_running accordingly. If
739 * nr_running becomes zero and @wakeup is %true, an idle worker is
740 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200741 *
Tejun Heocb444762010-07-02 10:03:50 +0200742 * CONTEXT:
743 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200744 */
745static inline void worker_set_flags(struct worker *worker, unsigned int flags,
746 bool wakeup)
747{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700748 struct worker_pool *pool = worker->pool;
749 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +0200750
Tejun Heocb444762010-07-02 10:03:50 +0200751 WARN_ON_ONCE(worker->task != current);
752
Tejun Heoe22bee72010-06-29 10:07:14 +0200753 /*
754 * If transitioning into NOT_RUNNING, adjust nr_running and
755 * wake up an idle worker as necessary if requested by
756 * @wakeup.
757 */
758 if ((flags & WORKER_NOT_RUNNING) &&
759 !(worker->flags & WORKER_NOT_RUNNING)) {
760 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
761
762 if (wakeup) {
763 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700764 !list_empty(&pool->worklist))
Tejun Heoe22bee72010-06-29 10:07:14 +0200765 wake_up_worker(gcwq);
766 } else
767 atomic_dec(nr_running);
768 }
769
Tejun Heod302f012010-06-29 10:07:13 +0200770 worker->flags |= flags;
771}
772
773/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200774 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200775 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200776 * @flags: flags to clear
777 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200778 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200779 *
Tejun Heocb444762010-07-02 10:03:50 +0200780 * CONTEXT:
781 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200782 */
783static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
784{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700785 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +0200786 unsigned int oflags = worker->flags;
787
Tejun Heocb444762010-07-02 10:03:50 +0200788 WARN_ON_ONCE(worker->task != current);
789
Tejun Heod302f012010-06-29 10:07:13 +0200790 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200791
Tejun Heo42c025f2011-01-11 15:58:49 +0100792 /*
793 * If transitioning out of NOT_RUNNING, increment nr_running. Note
794 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
795 * of multiple flags, not a single flag.
796 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200797 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
798 if (!(worker->flags & WORKER_NOT_RUNNING))
799 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200800}
801
802/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200803 * busy_worker_head - return the busy hash head for a work
804 * @gcwq: gcwq of interest
805 * @work: work to be hashed
806 *
807 * Return hash head of @gcwq for @work.
808 *
809 * CONTEXT:
810 * spin_lock_irq(gcwq->lock).
811 *
812 * RETURNS:
813 * Pointer to the hash head.
814 */
815static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
816 struct work_struct *work)
817{
818 const int base_shift = ilog2(sizeof(struct work_struct));
819 unsigned long v = (unsigned long)work;
820
821 /* simple shift and fold hash, do we need something better? */
822 v >>= base_shift;
823 v += v >> BUSY_WORKER_HASH_ORDER;
824 v &= BUSY_WORKER_HASH_MASK;
825
826 return &gcwq->busy_hash[v];
827}
828
829/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200830 * __find_worker_executing_work - find worker which is executing a work
831 * @gcwq: gcwq of interest
832 * @bwh: hash head as returned by busy_worker_head()
833 * @work: work to find worker for
834 *
835 * Find a worker which is executing @work on @gcwq. @bwh should be
836 * the hash head obtained by calling busy_worker_head() with the same
837 * work.
838 *
839 * CONTEXT:
840 * spin_lock_irq(gcwq->lock).
841 *
842 * RETURNS:
843 * Pointer to worker which is executing @work if found, NULL
844 * otherwise.
845 */
846static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
847 struct hlist_head *bwh,
848 struct work_struct *work)
849{
850 struct worker *worker;
851 struct hlist_node *tmp;
852
853 hlist_for_each_entry(worker, tmp, bwh, hentry)
854 if (worker->current_work == work)
855 return worker;
856 return NULL;
857}
858
859/**
860 * find_worker_executing_work - find worker which is executing a work
861 * @gcwq: gcwq of interest
862 * @work: work to find worker for
863 *
864 * Find a worker which is executing @work on @gcwq. This function is
865 * identical to __find_worker_executing_work() except that this
866 * function calculates @bwh itself.
867 *
868 * CONTEXT:
869 * spin_lock_irq(gcwq->lock).
870 *
871 * RETURNS:
872 * Pointer to worker which is executing @work if found, NULL
873 * otherwise.
874 */
875static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
876 struct work_struct *work)
877{
878 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
879 work);
880}
881
882/**
Tejun Heo649027d2010-06-29 10:07:14 +0200883 * gcwq_determine_ins_pos - find insertion position
884 * @gcwq: gcwq of interest
885 * @cwq: cwq a work is being queued for
886 *
887 * A work for @cwq is about to be queued on @gcwq, determine insertion
888 * position for the work. If @cwq is for HIGHPRI wq, the work is
889 * queued at the head of the queue but in FIFO order with respect to
890 * other HIGHPRI works; otherwise, at the end of the queue. This
891 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
892 * there are HIGHPRI works pending.
893 *
894 * CONTEXT:
895 * spin_lock_irq(gcwq->lock).
896 *
897 * RETURNS:
898 * Pointer to inserstion position.
899 */
900static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
901 struct cpu_workqueue_struct *cwq)
902{
903 struct work_struct *twork;
904
905 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
Tejun Heobd7bdd42012-07-12 14:46:37 -0700906 return &gcwq->pool.worklist;
Tejun Heo649027d2010-06-29 10:07:14 +0200907
Tejun Heobd7bdd42012-07-12 14:46:37 -0700908 list_for_each_entry(twork, &gcwq->pool.worklist, entry) {
Tejun Heo649027d2010-06-29 10:07:14 +0200909 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
910
911 if (!(tcwq->wq->flags & WQ_HIGHPRI))
912 break;
913 }
914
915 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
916 return &twork->entry;
917}
918
919/**
Tejun Heo7e116292010-06-29 10:07:13 +0200920 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200921 * @cwq: cwq @work belongs to
922 * @work: work to insert
923 * @head: insertion point
924 * @extra_flags: extra WORK_STRUCT_* flags to set
925 *
Tejun Heo7e116292010-06-29 10:07:13 +0200926 * Insert @work which belongs to @cwq into @gcwq after @head.
927 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200928 *
929 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200930 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200931 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700932static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200933 struct work_struct *work, struct list_head *head,
934 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700935{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700936 struct global_cwq *gcwq = cwq->pool->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100937
Tejun Heo4690c4a2010-06-29 10:07:10 +0200938 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200939 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200940
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700941 /*
942 * Ensure that we get the right work->data if we see the
943 * result of list_add() below, see try_to_grab_pending().
944 */
945 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200946
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700947 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200948
949 /*
950 * Ensure either worker_sched_deactivated() sees the above
951 * list_add_tail() or we see zero nr_running to avoid workers
952 * lying around lazily while there are works to be processed.
953 */
954 smp_mb();
955
Tejun Heo649027d2010-06-29 10:07:14 +0200956 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200957 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700958}
959
Tejun Heoc8efcc22010-12-20 19:32:04 +0100960/*
961 * Test whether @work is being queued from another work executing on the
962 * same workqueue. This is rather expensive and should only be used from
963 * cold paths.
964 */
965static bool is_chained_work(struct workqueue_struct *wq)
966{
967 unsigned long flags;
968 unsigned int cpu;
969
970 for_each_gcwq_cpu(cpu) {
971 struct global_cwq *gcwq = get_gcwq(cpu);
972 struct worker *worker;
973 struct hlist_node *pos;
974 int i;
975
976 spin_lock_irqsave(&gcwq->lock, flags);
977 for_each_busy_worker(worker, i, pos, gcwq) {
978 if (worker->task != current)
979 continue;
980 spin_unlock_irqrestore(&gcwq->lock, flags);
981 /*
982 * I'm @worker, no locking necessary. See if @work
983 * is headed to the same workqueue.
984 */
985 return worker->current_cwq->wq == wq;
986 }
987 spin_unlock_irqrestore(&gcwq->lock, flags);
988 }
989 return false;
990}
991
Tejun Heo4690c4a2010-06-29 10:07:10 +0200992static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct work_struct *work)
994{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200995 struct global_cwq *gcwq;
996 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200997 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200998 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 unsigned long flags;
1000
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001001 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001002
Tejun Heoc8efcc22010-12-20 19:32:04 +01001003 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001004 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001005 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001006 return;
1007
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001008 /* determine gcwq to use */
1009 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001010 struct global_cwq *last_gcwq;
1011
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001012 if (unlikely(cpu == WORK_CPU_UNBOUND))
1013 cpu = raw_smp_processor_id();
1014
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001015 /*
1016 * It's multi cpu. If @wq is non-reentrant and @work
1017 * was previously on a different cpu, it might still
1018 * be running there, in which case the work needs to
1019 * be queued on that cpu to guarantee non-reentrance.
1020 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001021 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001022 if (wq->flags & WQ_NON_REENTRANT &&
1023 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1024 struct worker *worker;
1025
1026 spin_lock_irqsave(&last_gcwq->lock, flags);
1027
1028 worker = find_worker_executing_work(last_gcwq, work);
1029
1030 if (worker && worker->current_cwq->wq == wq)
1031 gcwq = last_gcwq;
1032 else {
1033 /* meh... not running there, queue here */
1034 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1035 spin_lock_irqsave(&gcwq->lock, flags);
1036 }
1037 } else
1038 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001039 } else {
1040 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1041 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001042 }
1043
1044 /* gcwq determined, get cwq and queue */
1045 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001046 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001047
Dan Carpenterf5b25522012-04-13 22:06:58 +03001048 if (WARN_ON(!list_empty(&work->entry))) {
1049 spin_unlock_irqrestore(&gcwq->lock, flags);
1050 return;
1051 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001052
Tejun Heo73f53c42010-06-29 10:07:11 +02001053 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001054 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001055
1056 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001057 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001058 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001059 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001060 } else {
1061 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001063 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001064
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001065 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001066
Tejun Heo8b03ae32010-06-29 10:07:12 +02001067 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001070/**
1071 * queue_work - queue work on a workqueue
1072 * @wq: workqueue to use
1073 * @work: work to queue
1074 *
Alan Stern057647f2006-10-28 10:38:58 -07001075 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001077 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1078 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001080int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001082 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001084 ret = queue_work_on(get_cpu(), wq, work);
1085 put_cpu();
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return ret;
1088}
Dave Jonesae90dd52006-06-30 01:40:45 -04001089EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Zhang Ruic1a220e2008-07-23 21:28:39 -07001091/**
1092 * queue_work_on - queue work on specific cpu
1093 * @cpu: CPU number to execute work on
1094 * @wq: workqueue to use
1095 * @work: work to queue
1096 *
1097 * Returns 0 if @work was already on a queue, non-zero otherwise.
1098 *
1099 * We queue the work to a specific CPU, the caller must ensure it
1100 * can't go away.
1101 */
1102int
1103queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1104{
1105 int ret = 0;
1106
Tejun Heo22df02b2010-06-29 10:07:10 +02001107 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001108 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001109 ret = 1;
1110 }
1111 return ret;
1112}
1113EXPORT_SYMBOL_GPL(queue_work_on);
1114
Li Zefan6d141c32008-02-08 04:21:09 -08001115static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
David Howells52bad642006-11-22 14:54:01 +00001117 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001118 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Tejun Heo4690c4a2010-06-29 10:07:10 +02001120 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001123/**
1124 * queue_delayed_work - queue work on a workqueue after delay
1125 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001126 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001127 * @delay: number of jiffies to wait before queueing
1128 *
Alan Stern057647f2006-10-28 10:38:58 -07001129 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001130 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001131int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001132 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
David Howells52bad642006-11-22 14:54:01 +00001134 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001135 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001137 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Dave Jonesae90dd52006-06-30 01:40:45 -04001139EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001141/**
1142 * queue_delayed_work_on - queue work on specific CPU after delay
1143 * @cpu: CPU number to execute work on
1144 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001145 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001146 * @delay: number of jiffies to wait before queueing
1147 *
Alan Stern057647f2006-10-28 10:38:58 -07001148 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001149 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001150int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001151 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001152{
1153 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001154 struct timer_list *timer = &dwork->timer;
1155 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001156
Tejun Heo22df02b2010-06-29 10:07:10 +02001157 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001158 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001159
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001160 BUG_ON(timer_pending(timer));
1161 BUG_ON(!list_empty(&work->entry));
1162
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001163 timer_stats_timer_set_start_info(&dwork->timer);
1164
Tejun Heo7a22ad72010-06-29 10:07:13 +02001165 /*
1166 * This stores cwq for the moment, for the timer_fn.
1167 * Note that the work's gcwq is preserved to allow
1168 * reentrance detection for delayed works.
1169 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001170 if (!(wq->flags & WQ_UNBOUND)) {
1171 struct global_cwq *gcwq = get_work_gcwq(work);
1172
1173 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1174 lcpu = gcwq->cpu;
1175 else
1176 lcpu = raw_smp_processor_id();
1177 } else
1178 lcpu = WORK_CPU_UNBOUND;
1179
Tejun Heo7a22ad72010-06-29 10:07:13 +02001180 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001181
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001182 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001183 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001184 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001185
1186 if (unlikely(cpu >= 0))
1187 add_timer_on(timer, cpu);
1188 else
1189 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001190 ret = 1;
1191 }
1192 return ret;
1193}
Dave Jonesae90dd52006-06-30 01:40:45 -04001194EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Tejun Heoc8e55f32010-06-29 10:07:12 +02001196/**
1197 * worker_enter_idle - enter idle state
1198 * @worker: worker which is entering idle state
1199 *
1200 * @worker is entering idle state. Update stats and idle timer if
1201 * necessary.
1202 *
1203 * LOCKING:
1204 * spin_lock_irq(gcwq->lock).
1205 */
1206static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001208 struct worker_pool *pool = worker->pool;
1209 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Tejun Heoc8e55f32010-06-29 10:07:12 +02001211 BUG_ON(worker->flags & WORKER_IDLE);
1212 BUG_ON(!list_empty(&worker->entry) &&
1213 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Tejun Heocb444762010-07-02 10:03:50 +02001215 /* can't use worker_set_flags(), also called from start_worker() */
1216 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001217 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001218 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001219
Tejun Heoc8e55f32010-06-29 10:07:12 +02001220 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001221 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001222
Tejun Heoe22bee72010-06-29 10:07:14 +02001223 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001224 if (too_many_workers(gcwq) && !timer_pending(&pool->idle_timer))
1225 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001226 jiffies + IDLE_WORKER_TIMEOUT);
1227 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001228 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001229
Tejun Heo544ecf32012-05-14 15:04:50 -07001230 /*
1231 * Sanity check nr_running. Because trustee releases gcwq->lock
1232 * between setting %WORKER_ROGUE and zapping nr_running, the
1233 * warning may trigger spuriously. Check iff trustee is idle.
1234 */
1235 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001236 pool->nr_workers == pool->nr_idle &&
Tejun Heocb444762010-07-02 10:03:50 +02001237 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Tejun Heoc8e55f32010-06-29 10:07:12 +02001240/**
1241 * worker_leave_idle - leave idle state
1242 * @worker: worker which is leaving idle state
1243 *
1244 * @worker is leaving idle state. Update stats.
1245 *
1246 * LOCKING:
1247 * spin_lock_irq(gcwq->lock).
1248 */
1249static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001251 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Tejun Heoc8e55f32010-06-29 10:07:12 +02001253 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001254 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001255 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001256 list_del_init(&worker->entry);
1257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Tejun Heoe22bee72010-06-29 10:07:14 +02001259/**
1260 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1261 * @worker: self
1262 *
1263 * Works which are scheduled while the cpu is online must at least be
1264 * scheduled to a worker which is bound to the cpu so that if they are
1265 * flushed from cpu callbacks while cpu is going down, they are
1266 * guaranteed to execute on the cpu.
1267 *
1268 * This function is to be used by rogue workers and rescuers to bind
1269 * themselves to the target cpu and may race with cpu going down or
1270 * coming online. kthread_bind() can't be used because it may put the
1271 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1272 * verbatim as it's best effort and blocking and gcwq may be
1273 * [dis]associated in the meantime.
1274 *
1275 * This function tries set_cpus_allowed() and locks gcwq and verifies
1276 * the binding against GCWQ_DISASSOCIATED which is set during
1277 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1278 * idle state or fetches works without dropping lock, it can guarantee
1279 * the scheduling requirement described in the first paragraph.
1280 *
1281 * CONTEXT:
1282 * Might sleep. Called without any lock but returns with gcwq->lock
1283 * held.
1284 *
1285 * RETURNS:
1286 * %true if the associated gcwq is online (@worker is successfully
1287 * bound), %false if offline.
1288 */
1289static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001290__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001291{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001292 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001293 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Tejun Heoe22bee72010-06-29 10:07:14 +02001295 while (true) {
1296 /*
1297 * The following call may fail, succeed or succeed
1298 * without actually migrating the task to the cpu if
1299 * it races with cpu hotunplug operation. Verify
1300 * against GCWQ_DISASSOCIATED.
1301 */
Tejun Heof3421792010-07-02 10:03:51 +02001302 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1303 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001304
Tejun Heoe22bee72010-06-29 10:07:14 +02001305 spin_lock_irq(&gcwq->lock);
1306 if (gcwq->flags & GCWQ_DISASSOCIATED)
1307 return false;
1308 if (task_cpu(task) == gcwq->cpu &&
1309 cpumask_equal(&current->cpus_allowed,
1310 get_cpu_mask(gcwq->cpu)))
1311 return true;
1312 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001313
Tejun Heo5035b202011-04-29 18:08:37 +02001314 /*
1315 * We've raced with CPU hot[un]plug. Give it a breather
1316 * and retry migration. cond_resched() is required here;
1317 * otherwise, we might deadlock against cpu_stop trying to
1318 * bring down the CPU on non-preemptive kernel.
1319 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001320 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001321 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001322 }
1323}
1324
1325/*
1326 * Function for worker->rebind_work used to rebind rogue busy workers
1327 * to the associated cpu which is coming back online. This is
1328 * scheduled by cpu up but can race with other cpu hotplug operations
1329 * and may be executed twice without intervening cpu down.
1330 */
1331static void worker_rebind_fn(struct work_struct *work)
1332{
1333 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001334 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001335
1336 if (worker_maybe_bind_and_lock(worker))
1337 worker_clr_flags(worker, WORKER_REBIND);
1338
1339 spin_unlock_irq(&gcwq->lock);
1340}
1341
Tejun Heoc34056a2010-06-29 10:07:11 +02001342static struct worker *alloc_worker(void)
1343{
1344 struct worker *worker;
1345
1346 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001347 if (worker) {
1348 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001349 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001350 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1351 /* on creation a worker is in !idle && prep state */
1352 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001353 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001354 return worker;
1355}
1356
1357/**
1358 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001359 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001360 * @bind: whether to set affinity to @cpu or not
1361 *
Tejun Heo7e116292010-06-29 10:07:13 +02001362 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001363 * can be started by calling start_worker() or destroyed using
1364 * destroy_worker().
1365 *
1366 * CONTEXT:
1367 * Might sleep. Does GFP_KERNEL allocations.
1368 *
1369 * RETURNS:
1370 * Pointer to the newly created worker.
1371 */
Tejun Heo7e116292010-06-29 10:07:13 +02001372static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001373{
Tejun Heof3421792010-07-02 10:03:51 +02001374 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001375 struct worker_pool *pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001376 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001377 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001378
Tejun Heo8b03ae32010-06-29 10:07:12 +02001379 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001380 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001381 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001382 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001383 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001384 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001385 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001386 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001387
1388 worker = alloc_worker();
1389 if (!worker)
1390 goto fail;
1391
Tejun Heobd7bdd42012-07-12 14:46:37 -07001392 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001393 worker->id = id;
1394
Tejun Heof3421792010-07-02 10:03:51 +02001395 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001396 worker->task = kthread_create_on_node(worker_thread,
1397 worker,
1398 cpu_to_node(gcwq->cpu),
1399 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001400 else
1401 worker->task = kthread_create(worker_thread, worker,
1402 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001403 if (IS_ERR(worker->task))
1404 goto fail;
1405
Tejun Heodb7bccf2010-06-29 10:07:12 +02001406 /*
1407 * A rogue worker will become a regular one if CPU comes
1408 * online later on. Make sure every worker has
1409 * PF_THREAD_BOUND set.
1410 */
Tejun Heof3421792010-07-02 10:03:51 +02001411 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001412 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001413 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001414 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001415 if (on_unbound_cpu)
1416 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001418
Tejun Heoc34056a2010-06-29 10:07:11 +02001419 return worker;
1420fail:
1421 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001422 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001423 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001424 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001425 }
1426 kfree(worker);
1427 return NULL;
1428}
1429
1430/**
1431 * start_worker - start a newly created worker
1432 * @worker: worker to start
1433 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001434 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001435 *
1436 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001437 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001438 */
1439static void start_worker(struct worker *worker)
1440{
Tejun Heocb444762010-07-02 10:03:50 +02001441 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001442 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001443 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001444 wake_up_process(worker->task);
1445}
1446
1447/**
1448 * destroy_worker - destroy a workqueue worker
1449 * @worker: worker to be destroyed
1450 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001451 * Destroy @worker and adjust @gcwq stats accordingly.
1452 *
1453 * CONTEXT:
1454 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001455 */
1456static void destroy_worker(struct worker *worker)
1457{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001458 struct worker_pool *pool = worker->pool;
1459 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001460 int id = worker->id;
1461
1462 /* sanity check frenzy */
1463 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001464 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001465
Tejun Heoc8e55f32010-06-29 10:07:12 +02001466 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001467 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001468 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001469 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001470
1471 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001472 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001473
1474 spin_unlock_irq(&gcwq->lock);
1475
Tejun Heoc34056a2010-06-29 10:07:11 +02001476 kthread_stop(worker->task);
1477 kfree(worker);
1478
Tejun Heo8b03ae32010-06-29 10:07:12 +02001479 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001480 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001481}
1482
Tejun Heoe22bee72010-06-29 10:07:14 +02001483static void idle_worker_timeout(unsigned long __gcwq)
1484{
1485 struct global_cwq *gcwq = (void *)__gcwq;
1486
1487 spin_lock_irq(&gcwq->lock);
1488
1489 if (too_many_workers(gcwq)) {
1490 struct worker *worker;
1491 unsigned long expires;
1492
1493 /* idle_list is kept in LIFO order, check the last one */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001494 worker = list_entry(gcwq->pool.idle_list.prev, struct worker,
1495 entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001496 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1497
1498 if (time_before(jiffies, expires))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001499 mod_timer(&gcwq->pool.idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001500 else {
1501 /* it's been idle for too long, wake up manager */
1502 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1503 wake_up_worker(gcwq);
1504 }
1505 }
1506
1507 spin_unlock_irq(&gcwq->lock);
1508}
1509
1510static bool send_mayday(struct work_struct *work)
1511{
1512 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1513 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001514 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001515
1516 if (!(wq->flags & WQ_RESCUER))
1517 return false;
1518
1519 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001520 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001521 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1522 if (cpu == WORK_CPU_UNBOUND)
1523 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001524 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001525 wake_up_process(wq->rescuer->task);
1526 return true;
1527}
1528
1529static void gcwq_mayday_timeout(unsigned long __gcwq)
1530{
1531 struct global_cwq *gcwq = (void *)__gcwq;
1532 struct work_struct *work;
1533
1534 spin_lock_irq(&gcwq->lock);
1535
1536 if (need_to_create_worker(gcwq)) {
1537 /*
1538 * We've been trying to create a new worker but
1539 * haven't been successful. We might be hitting an
1540 * allocation deadlock. Send distress signals to
1541 * rescuers.
1542 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001543 list_for_each_entry(work, &gcwq->pool.worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 send_mayday(work);
1545 }
1546
1547 spin_unlock_irq(&gcwq->lock);
1548
Tejun Heobd7bdd42012-07-12 14:46:37 -07001549 mod_timer(&gcwq->pool.mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001550}
1551
1552/**
1553 * maybe_create_worker - create a new worker if necessary
1554 * @gcwq: gcwq to create a new worker for
1555 *
1556 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1557 * have at least one idle worker on return from this function. If
1558 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1559 * sent to all rescuers with works scheduled on @gcwq to resolve
1560 * possible allocation deadlock.
1561 *
1562 * On return, need_to_create_worker() is guaranteed to be false and
1563 * may_start_working() true.
1564 *
1565 * LOCKING:
1566 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1567 * multiple times. Does GFP_KERNEL allocations. Called only from
1568 * manager.
1569 *
1570 * RETURNS:
1571 * false if no action was taken and gcwq->lock stayed locked, true
1572 * otherwise.
1573 */
1574static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001575__releases(&gcwq->lock)
1576__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001577{
1578 if (!need_to_create_worker(gcwq))
1579 return false;
1580restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001581 spin_unlock_irq(&gcwq->lock);
1582
Tejun Heoe22bee72010-06-29 10:07:14 +02001583 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001584 mod_timer(&gcwq->pool.mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001585
1586 while (true) {
1587 struct worker *worker;
1588
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 worker = create_worker(gcwq, true);
1590 if (worker) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001591 del_timer_sync(&gcwq->pool.mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001592 spin_lock_irq(&gcwq->lock);
1593 start_worker(worker);
1594 BUG_ON(need_to_create_worker(gcwq));
1595 return true;
1596 }
1597
1598 if (!need_to_create_worker(gcwq))
1599 break;
1600
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 __set_current_state(TASK_INTERRUPTIBLE);
1602 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001603
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 if (!need_to_create_worker(gcwq))
1605 break;
1606 }
1607
Tejun Heobd7bdd42012-07-12 14:46:37 -07001608 del_timer_sync(&gcwq->pool.mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 spin_lock_irq(&gcwq->lock);
1610 if (need_to_create_worker(gcwq))
1611 goto restart;
1612 return true;
1613}
1614
1615/**
1616 * maybe_destroy_worker - destroy workers which have been idle for a while
1617 * @gcwq: gcwq to destroy workers for
1618 *
1619 * Destroy @gcwq workers which have been idle for longer than
1620 * IDLE_WORKER_TIMEOUT.
1621 *
1622 * LOCKING:
1623 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1624 * multiple times. Called only from manager.
1625 *
1626 * RETURNS:
1627 * false if no action was taken and gcwq->lock stayed locked, true
1628 * otherwise.
1629 */
1630static bool maybe_destroy_workers(struct global_cwq *gcwq)
1631{
1632 bool ret = false;
1633
1634 while (too_many_workers(gcwq)) {
1635 struct worker *worker;
1636 unsigned long expires;
1637
Tejun Heobd7bdd42012-07-12 14:46:37 -07001638 worker = list_entry(gcwq->pool.idle_list.prev, struct worker,
1639 entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001640 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1641
1642 if (time_before(jiffies, expires)) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001643 mod_timer(&gcwq->pool.idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001644 break;
1645 }
1646
1647 destroy_worker(worker);
1648 ret = true;
1649 }
1650
1651 return ret;
1652}
1653
1654/**
1655 * manage_workers - manage worker pool
1656 * @worker: self
1657 *
1658 * Assume the manager role and manage gcwq worker pool @worker belongs
1659 * to. At any given time, there can be only zero or one manager per
1660 * gcwq. The exclusion is handled automatically by this function.
1661 *
1662 * The caller can safely start processing works on false return. On
1663 * true return, it's guaranteed that need_to_create_worker() is false
1664 * and may_start_working() is true.
1665 *
1666 * CONTEXT:
1667 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1668 * multiple times. Does GFP_KERNEL allocations.
1669 *
1670 * RETURNS:
1671 * false if no action was taken and gcwq->lock stayed locked, true if
1672 * some action was taken.
1673 */
1674static bool manage_workers(struct worker *worker)
1675{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001676 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001677 bool ret = false;
1678
1679 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1680 return ret;
1681
1682 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1683 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1684
1685 /*
1686 * Destroy and then create so that may_start_working() is true
1687 * on return.
1688 */
1689 ret |= maybe_destroy_workers(gcwq);
1690 ret |= maybe_create_worker(gcwq);
1691
1692 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1693
1694 /*
1695 * The trustee might be waiting to take over the manager
1696 * position, tell it we're done.
1697 */
1698 if (unlikely(gcwq->trustee))
1699 wake_up_all(&gcwq->trustee_wait);
1700
1701 return ret;
1702}
1703
Tejun Heoa62428c2010-06-29 10:07:10 +02001704/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001705 * move_linked_works - move linked works to a list
1706 * @work: start of series of works to be scheduled
1707 * @head: target list to append @work to
1708 * @nextp: out paramter for nested worklist walking
1709 *
1710 * Schedule linked works starting from @work to @head. Work series to
1711 * be scheduled starts at @work and includes any consecutive work with
1712 * WORK_STRUCT_LINKED set in its predecessor.
1713 *
1714 * If @nextp is not NULL, it's updated to point to the next work of
1715 * the last scheduled work. This allows move_linked_works() to be
1716 * nested inside outer list_for_each_entry_safe().
1717 *
1718 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001719 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001720 */
1721static void move_linked_works(struct work_struct *work, struct list_head *head,
1722 struct work_struct **nextp)
1723{
1724 struct work_struct *n;
1725
1726 /*
1727 * Linked worklist will always end before the end of the list,
1728 * use NULL for list head.
1729 */
1730 list_for_each_entry_safe_from(work, n, NULL, entry) {
1731 list_move_tail(&work->entry, head);
1732 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1733 break;
1734 }
1735
1736 /*
1737 * If we're already inside safe list traversal and have moved
1738 * multiple works to the scheduled queue, the next position
1739 * needs to be updated.
1740 */
1741 if (nextp)
1742 *nextp = n;
1743}
1744
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001745static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1746{
1747 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1748 struct work_struct, entry);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001749 struct list_head *pos = gcwq_determine_ins_pos(cwq->pool->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001750
Tejun Heocdadf002010-10-05 10:49:55 +02001751 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001752 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001753 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001754 cwq->nr_active++;
1755}
1756
Tejun Heoaffee4b2010-06-29 10:07:12 +02001757/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001758 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1759 * @cwq: cwq of interest
1760 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001761 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001762 *
1763 * A work either has completed or is removed from pending queue,
1764 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1765 *
1766 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001767 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001768 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001769static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1770 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001771{
1772 /* ignore uncolored works */
1773 if (color == WORK_NO_COLOR)
1774 return;
1775
1776 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001777
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001778 if (!delayed) {
1779 cwq->nr_active--;
1780 if (!list_empty(&cwq->delayed_works)) {
1781 /* one down, submit a delayed one */
1782 if (cwq->nr_active < cwq->max_active)
1783 cwq_activate_first_delayed(cwq);
1784 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001785 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001786
1787 /* is flush in progress and are we at the flushing tip? */
1788 if (likely(cwq->flush_color != color))
1789 return;
1790
1791 /* are there still in-flight works? */
1792 if (cwq->nr_in_flight[color])
1793 return;
1794
1795 /* this cwq is done, clear flush_color */
1796 cwq->flush_color = -1;
1797
1798 /*
1799 * If this was the last cwq, wake up the first flusher. It
1800 * will handle the rest.
1801 */
1802 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1803 complete(&cwq->wq->first_flusher->done);
1804}
1805
1806/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001807 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001808 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001809 * @work: work to process
1810 *
1811 * Process @work. This function contains all the logics necessary to
1812 * process a single work including synchronization against and
1813 * interaction with other workers on the same cpu, queueing and
1814 * flushing. As long as context requirement is met, any worker can
1815 * call this function to process a work.
1816 *
1817 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001818 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001819 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001820static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001821__releases(&gcwq->lock)
1822__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001823{
Tejun Heo7e116292010-06-29 10:07:13 +02001824 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001825 struct worker_pool *pool = worker->pool;
1826 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001827 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001828 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001829 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001830 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001831 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001832#ifdef CONFIG_LOCKDEP
1833 /*
1834 * It is permissible to free the struct work_struct from
1835 * inside the function that is called from it, this we need to
1836 * take into account for lockdep too. To avoid bogus "held
1837 * lock freed" warnings as well as problems when looking into
1838 * work->lockdep_map, make a copy and use that here.
1839 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001840 struct lockdep_map lockdep_map;
1841
1842 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001843#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001844 /*
1845 * A single work shouldn't be executed concurrently by
1846 * multiple workers on a single cpu. Check whether anyone is
1847 * already processing the work. If so, defer the work to the
1848 * currently executing one.
1849 */
1850 collision = __find_worker_executing_work(gcwq, bwh, work);
1851 if (unlikely(collision)) {
1852 move_linked_works(work, &collision->scheduled, NULL);
1853 return;
1854 }
1855
Tejun Heoa62428c2010-06-29 10:07:10 +02001856 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001857 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001858 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001859 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001860 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001861 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001862
Tejun Heo7a22ad72010-06-29 10:07:13 +02001863 /* record the current cpu number in the work data and dequeue */
1864 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001865 list_del_init(&work->entry);
1866
Tejun Heo649027d2010-06-29 10:07:14 +02001867 /*
1868 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1869 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1870 */
1871 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001872 struct work_struct *nwork = list_first_entry(&pool->worklist,
1873 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001874
Tejun Heobd7bdd42012-07-12 14:46:37 -07001875 if (!list_empty(&pool->worklist) &&
Tejun Heo649027d2010-06-29 10:07:14 +02001876 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1877 wake_up_worker(gcwq);
1878 else
1879 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1880 }
1881
Tejun Heofb0e7be2010-06-29 10:07:15 +02001882 /*
1883 * CPU intensive works don't participate in concurrency
1884 * management. They're the scheduler's responsibility.
1885 */
1886 if (unlikely(cpu_intensive))
1887 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1888
Tejun Heo974271c2012-07-12 14:46:37 -07001889 /*
1890 * Unbound gcwq isn't concurrency managed and work items should be
1891 * executed ASAP. Wake up another worker if necessary.
1892 */
1893 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(gcwq))
1894 wake_up_worker(gcwq);
1895
Tejun Heo8b03ae32010-06-29 10:07:12 +02001896 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001897
Tejun Heoa62428c2010-06-29 10:07:10 +02001898 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001899 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001900 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001901 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001902 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001903 /*
1904 * While we must be careful to not use "work" after this, the trace
1905 * point will only record its address.
1906 */
1907 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001908 lock_map_release(&lockdep_map);
1909 lock_map_release(&cwq->wq->lockdep_map);
1910
1911 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1912 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1913 "%s/0x%08x/%d\n",
1914 current->comm, preempt_count(), task_pid_nr(current));
1915 printk(KERN_ERR " last function: ");
1916 print_symbol("%s\n", (unsigned long)f);
1917 debug_show_held_locks(current);
1918 dump_stack();
1919 }
1920
Tejun Heo8b03ae32010-06-29 10:07:12 +02001921 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001922
Tejun Heofb0e7be2010-06-29 10:07:15 +02001923 /* clear cpu intensive status */
1924 if (unlikely(cpu_intensive))
1925 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1926
Tejun Heoa62428c2010-06-29 10:07:10 +02001927 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001928 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001929 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001930 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001931 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001932}
1933
Tejun Heoaffee4b2010-06-29 10:07:12 +02001934/**
1935 * process_scheduled_works - process scheduled works
1936 * @worker: self
1937 *
1938 * Process all scheduled works. Please note that the scheduled list
1939 * may change while processing a work, so this function repeatedly
1940 * fetches a work from the top and executes it.
1941 *
1942 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001943 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001944 * multiple times.
1945 */
1946static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001948 while (!list_empty(&worker->scheduled)) {
1949 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001951 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
Tejun Heo4690c4a2010-06-29 10:07:10 +02001955/**
1956 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001957 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001958 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001959 * The gcwq worker thread function. There's a single dynamic pool of
1960 * these per each cpu. These workers process all works regardless of
1961 * their specific target workqueue. The only exception is works which
1962 * belong to workqueues with a rescuer which will be explained in
1963 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001964 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001965static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Tejun Heoc34056a2010-06-29 10:07:11 +02001967 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001968 struct worker_pool *pool = worker->pool;
1969 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 /* tell the scheduler that this is a workqueue worker */
1972 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001973woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001974 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Tejun Heoc8e55f32010-06-29 10:07:12 +02001976 /* DIE can be set only while we're idle, checking here is enough */
1977 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001978 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001979 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982
Tejun Heoc8e55f32010-06-29 10:07:12 +02001983 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001984recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 /* no more worker necessary? */
1986 if (!need_more_worker(gcwq))
1987 goto sleep;
1988
1989 /* do we need to manage? */
1990 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1991 goto recheck;
1992
Tejun Heoc8e55f32010-06-29 10:07:12 +02001993 /*
1994 * ->scheduled list can only be filled while a worker is
1995 * preparing to process a work or actually processing it.
1996 * Make sure nobody diddled with it while I was sleeping.
1997 */
1998 BUG_ON(!list_empty(&worker->scheduled));
1999
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 /*
2001 * When control reaches this point, we're guaranteed to have
2002 * at least one idle worker or that someone else has already
2003 * assumed the manager role.
2004 */
2005 worker_clr_flags(worker, WORKER_PREP);
2006
2007 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002008 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002009 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002010 struct work_struct, entry);
2011
2012 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2013 /* optimization path, not strictly necessary */
2014 process_one_work(worker, work);
2015 if (unlikely(!list_empty(&worker->scheduled)))
2016 process_scheduled_works(worker);
2017 } else {
2018 move_linked_works(work, &worker->scheduled, NULL);
2019 process_scheduled_works(worker);
2020 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002021 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002022
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002024sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
2026 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002027
Tejun Heoc8e55f32010-06-29 10:07:12 +02002028 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 * gcwq->lock is held and there's no work to process and no
2030 * need to manage, sleep. Workers are woken up only while
2031 * holding gcwq->lock or from local cpu, so setting the
2032 * current state before releasing gcwq->lock is enough to
2033 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002034 */
2035 worker_enter_idle(worker);
2036 __set_current_state(TASK_INTERRUPTIBLE);
2037 spin_unlock_irq(&gcwq->lock);
2038 schedule();
2039 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Tejun Heoe22bee72010-06-29 10:07:14 +02002042/**
2043 * rescuer_thread - the rescuer thread function
2044 * @__wq: the associated workqueue
2045 *
2046 * Workqueue rescuer thread function. There's one rescuer for each
2047 * workqueue which has WQ_RESCUER set.
2048 *
2049 * Regular work processing on a gcwq may block trying to create a new
2050 * worker which uses GFP_KERNEL allocation which has slight chance of
2051 * developing into deadlock if some works currently on the same queue
2052 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2053 * the problem rescuer solves.
2054 *
2055 * When such condition is possible, the gcwq summons rescuers of all
2056 * workqueues which have works queued on the gcwq and let them process
2057 * those works so that forward progress can be guaranteed.
2058 *
2059 * This should happen rarely.
2060 */
2061static int rescuer_thread(void *__wq)
2062{
2063 struct workqueue_struct *wq = __wq;
2064 struct worker *rescuer = wq->rescuer;
2065 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002066 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002067 unsigned int cpu;
2068
2069 set_user_nice(current, RESCUER_NICE_LEVEL);
2070repeat:
2071 set_current_state(TASK_INTERRUPTIBLE);
2072
2073 if (kthread_should_stop())
2074 return 0;
2075
Tejun Heof3421792010-07-02 10:03:51 +02002076 /*
2077 * See whether any cpu is asking for help. Unbounded
2078 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2079 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002080 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002081 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2082 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002083 struct worker_pool *pool = cwq->pool;
2084 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002085 struct work_struct *work, *n;
2086
2087 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002088 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002089
2090 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002091 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002092 worker_maybe_bind_and_lock(rescuer);
2093
2094 /*
2095 * Slurp in all works issued via this workqueue and
2096 * process'em.
2097 */
2098 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002099 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002100 if (get_work_cwq(work) == cwq)
2101 move_linked_works(work, scheduled, &n);
2102
2103 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002104
2105 /*
2106 * Leave this gcwq. If keep_working() is %true, notify a
2107 * regular worker; otherwise, we end up with 0 concurrency
2108 * and stalling the execution.
2109 */
2110 if (keep_working(gcwq))
2111 wake_up_worker(gcwq);
2112
Tejun Heoe22bee72010-06-29 10:07:14 +02002113 spin_unlock_irq(&gcwq->lock);
2114 }
2115
2116 schedule();
2117 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002120struct wq_barrier {
2121 struct work_struct work;
2122 struct completion done;
2123};
2124
2125static void wq_barrier_func(struct work_struct *work)
2126{
2127 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2128 complete(&barr->done);
2129}
2130
Tejun Heo4690c4a2010-06-29 10:07:10 +02002131/**
2132 * insert_wq_barrier - insert a barrier work
2133 * @cwq: cwq to insert barrier into
2134 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002135 * @target: target work to attach @barr to
2136 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002137 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002138 * @barr is linked to @target such that @barr is completed only after
2139 * @target finishes execution. Please note that the ordering
2140 * guarantee is observed only with respect to @target and on the local
2141 * cpu.
2142 *
2143 * Currently, a queued barrier can't be canceled. This is because
2144 * try_to_grab_pending() can't determine whether the work to be
2145 * grabbed is at the head of the queue and thus can't clear LINKED
2146 * flag of the previous work while there must be a valid next work
2147 * after a work with LINKED flag set.
2148 *
2149 * Note that when @worker is non-NULL, @target may be modified
2150 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002151 *
2152 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002153 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002154 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002155static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002156 struct wq_barrier *barr,
2157 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002158{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002159 struct list_head *head;
2160 unsigned int linked = 0;
2161
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002162 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002163 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002164 * as we know for sure that this will not trigger any of the
2165 * checks and call back into the fixup functions where we
2166 * might deadlock.
2167 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002168 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002169 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002170 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002171
Tejun Heoaffee4b2010-06-29 10:07:12 +02002172 /*
2173 * If @target is currently being executed, schedule the
2174 * barrier to the worker; otherwise, put it after @target.
2175 */
2176 if (worker)
2177 head = worker->scheduled.next;
2178 else {
2179 unsigned long *bits = work_data_bits(target);
2180
2181 head = target->entry.next;
2182 /* there can already be other linked works, inherit and set */
2183 linked = *bits & WORK_STRUCT_LINKED;
2184 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2185 }
2186
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002187 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002188 insert_work(cwq, &barr->work, head,
2189 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002190}
2191
Tejun Heo73f53c42010-06-29 10:07:11 +02002192/**
2193 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2194 * @wq: workqueue being flushed
2195 * @flush_color: new flush color, < 0 for no-op
2196 * @work_color: new work color, < 0 for no-op
2197 *
2198 * Prepare cwqs for workqueue flushing.
2199 *
2200 * If @flush_color is non-negative, flush_color on all cwqs should be
2201 * -1. If no cwq has in-flight commands at the specified color, all
2202 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2203 * has in flight commands, its cwq->flush_color is set to
2204 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2205 * wakeup logic is armed and %true is returned.
2206 *
2207 * The caller should have initialized @wq->first_flusher prior to
2208 * calling this function with non-negative @flush_color. If
2209 * @flush_color is negative, no flush color update is done and %false
2210 * is returned.
2211 *
2212 * If @work_color is non-negative, all cwqs should have the same
2213 * work_color which is previous to @work_color and all will be
2214 * advanced to @work_color.
2215 *
2216 * CONTEXT:
2217 * mutex_lock(wq->flush_mutex).
2218 *
2219 * RETURNS:
2220 * %true if @flush_color >= 0 and there's something to flush. %false
2221 * otherwise.
2222 */
2223static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2224 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Tejun Heo73f53c42010-06-29 10:07:11 +02002226 bool wait = false;
2227 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002228
Tejun Heo73f53c42010-06-29 10:07:11 +02002229 if (flush_color >= 0) {
2230 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2231 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002232 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002233
Tejun Heof3421792010-07-02 10:03:51 +02002234 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002235 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002236 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002237
Tejun Heo8b03ae32010-06-29 10:07:12 +02002238 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002239
2240 if (flush_color >= 0) {
2241 BUG_ON(cwq->flush_color != -1);
2242
2243 if (cwq->nr_in_flight[flush_color]) {
2244 cwq->flush_color = flush_color;
2245 atomic_inc(&wq->nr_cwqs_to_flush);
2246 wait = true;
2247 }
2248 }
2249
2250 if (work_color >= 0) {
2251 BUG_ON(work_color != work_next_color(cwq->work_color));
2252 cwq->work_color = work_color;
2253 }
2254
Tejun Heo8b03ae32010-06-29 10:07:12 +02002255 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002256 }
2257
2258 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2259 complete(&wq->first_flusher->done);
2260
2261 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002264/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002266 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 *
2268 * Forces execution of the workqueue and blocks until its completion.
2269 * This is typically used in driver shutdown handlers.
2270 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002271 * We sleep until all works which were queued on entry have been handled,
2272 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002274void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Tejun Heo73f53c42010-06-29 10:07:11 +02002276 struct wq_flusher this_flusher = {
2277 .list = LIST_HEAD_INIT(this_flusher.list),
2278 .flush_color = -1,
2279 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2280 };
2281 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002282
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002283 lock_map_acquire(&wq->lockdep_map);
2284 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002285
2286 mutex_lock(&wq->flush_mutex);
2287
2288 /*
2289 * Start-to-wait phase
2290 */
2291 next_color = work_next_color(wq->work_color);
2292
2293 if (next_color != wq->flush_color) {
2294 /*
2295 * Color space is not full. The current work_color
2296 * becomes our flush_color and work_color is advanced
2297 * by one.
2298 */
2299 BUG_ON(!list_empty(&wq->flusher_overflow));
2300 this_flusher.flush_color = wq->work_color;
2301 wq->work_color = next_color;
2302
2303 if (!wq->first_flusher) {
2304 /* no flush in progress, become the first flusher */
2305 BUG_ON(wq->flush_color != this_flusher.flush_color);
2306
2307 wq->first_flusher = &this_flusher;
2308
2309 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2310 wq->work_color)) {
2311 /* nothing to flush, done */
2312 wq->flush_color = next_color;
2313 wq->first_flusher = NULL;
2314 goto out_unlock;
2315 }
2316 } else {
2317 /* wait in queue */
2318 BUG_ON(wq->flush_color == this_flusher.flush_color);
2319 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2320 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2321 }
2322 } else {
2323 /*
2324 * Oops, color space is full, wait on overflow queue.
2325 * The next flush completion will assign us
2326 * flush_color and transfer to flusher_queue.
2327 */
2328 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2329 }
2330
2331 mutex_unlock(&wq->flush_mutex);
2332
2333 wait_for_completion(&this_flusher.done);
2334
2335 /*
2336 * Wake-up-and-cascade phase
2337 *
2338 * First flushers are responsible for cascading flushes and
2339 * handling overflow. Non-first flushers can simply return.
2340 */
2341 if (wq->first_flusher != &this_flusher)
2342 return;
2343
2344 mutex_lock(&wq->flush_mutex);
2345
Tejun Heo4ce48b32010-07-02 10:03:51 +02002346 /* we might have raced, check again with mutex held */
2347 if (wq->first_flusher != &this_flusher)
2348 goto out_unlock;
2349
Tejun Heo73f53c42010-06-29 10:07:11 +02002350 wq->first_flusher = NULL;
2351
2352 BUG_ON(!list_empty(&this_flusher.list));
2353 BUG_ON(wq->flush_color != this_flusher.flush_color);
2354
2355 while (true) {
2356 struct wq_flusher *next, *tmp;
2357
2358 /* complete all the flushers sharing the current flush color */
2359 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2360 if (next->flush_color != wq->flush_color)
2361 break;
2362 list_del_init(&next->list);
2363 complete(&next->done);
2364 }
2365
2366 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2367 wq->flush_color != work_next_color(wq->work_color));
2368
2369 /* this flush_color is finished, advance by one */
2370 wq->flush_color = work_next_color(wq->flush_color);
2371
2372 /* one color has been freed, handle overflow queue */
2373 if (!list_empty(&wq->flusher_overflow)) {
2374 /*
2375 * Assign the same color to all overflowed
2376 * flushers, advance work_color and append to
2377 * flusher_queue. This is the start-to-wait
2378 * phase for these overflowed flushers.
2379 */
2380 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2381 tmp->flush_color = wq->work_color;
2382
2383 wq->work_color = work_next_color(wq->work_color);
2384
2385 list_splice_tail_init(&wq->flusher_overflow,
2386 &wq->flusher_queue);
2387 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2388 }
2389
2390 if (list_empty(&wq->flusher_queue)) {
2391 BUG_ON(wq->flush_color != wq->work_color);
2392 break;
2393 }
2394
2395 /*
2396 * Need to flush more colors. Make the next flusher
2397 * the new first flusher and arm cwqs.
2398 */
2399 BUG_ON(wq->flush_color == wq->work_color);
2400 BUG_ON(wq->flush_color != next->flush_color);
2401
2402 list_del_init(&next->list);
2403 wq->first_flusher = next;
2404
2405 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2406 break;
2407
2408 /*
2409 * Meh... this color is already done, clear first
2410 * flusher and repeat cascading.
2411 */
2412 wq->first_flusher = NULL;
2413 }
2414
2415out_unlock:
2416 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
Dave Jonesae90dd52006-06-30 01:40:45 -04002418EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002420/**
2421 * drain_workqueue - drain a workqueue
2422 * @wq: workqueue to drain
2423 *
2424 * Wait until the workqueue becomes empty. While draining is in progress,
2425 * only chain queueing is allowed. IOW, only currently pending or running
2426 * work items on @wq can queue further work items on it. @wq is flushed
2427 * repeatedly until it becomes empty. The number of flushing is detemined
2428 * by the depth of chaining and should be relatively short. Whine if it
2429 * takes too long.
2430 */
2431void drain_workqueue(struct workqueue_struct *wq)
2432{
2433 unsigned int flush_cnt = 0;
2434 unsigned int cpu;
2435
2436 /*
2437 * __queue_work() needs to test whether there are drainers, is much
2438 * hotter than drain_workqueue() and already looks at @wq->flags.
2439 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2440 */
2441 spin_lock(&workqueue_lock);
2442 if (!wq->nr_drainers++)
2443 wq->flags |= WQ_DRAINING;
2444 spin_unlock(&workqueue_lock);
2445reflush:
2446 flush_workqueue(wq);
2447
2448 for_each_cwq_cpu(cpu, wq) {
2449 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002450 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002451
Tejun Heobd7bdd42012-07-12 14:46:37 -07002452 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002453 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002454 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002455
2456 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002457 continue;
2458
2459 if (++flush_cnt == 10 ||
2460 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2461 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2462 wq->name, flush_cnt);
2463 goto reflush;
2464 }
2465
2466 spin_lock(&workqueue_lock);
2467 if (!--wq->nr_drainers)
2468 wq->flags &= ~WQ_DRAINING;
2469 spin_unlock(&workqueue_lock);
2470}
2471EXPORT_SYMBOL_GPL(drain_workqueue);
2472
Tejun Heobaf59022010-09-16 10:42:16 +02002473static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2474 bool wait_executing)
2475{
2476 struct worker *worker = NULL;
2477 struct global_cwq *gcwq;
2478 struct cpu_workqueue_struct *cwq;
2479
2480 might_sleep();
2481 gcwq = get_work_gcwq(work);
2482 if (!gcwq)
2483 return false;
2484
2485 spin_lock_irq(&gcwq->lock);
2486 if (!list_empty(&work->entry)) {
2487 /*
2488 * See the comment near try_to_grab_pending()->smp_rmb().
2489 * If it was re-queued to a different gcwq under us, we
2490 * are not going to wait.
2491 */
2492 smp_rmb();
2493 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002494 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002495 goto already_gone;
2496 } else if (wait_executing) {
2497 worker = find_worker_executing_work(gcwq, work);
2498 if (!worker)
2499 goto already_gone;
2500 cwq = worker->current_cwq;
2501 } else
2502 goto already_gone;
2503
2504 insert_wq_barrier(cwq, barr, work, worker);
2505 spin_unlock_irq(&gcwq->lock);
2506
Tejun Heoe1594892011-01-09 23:32:15 +01002507 /*
2508 * If @max_active is 1 or rescuer is in use, flushing another work
2509 * item on the same workqueue may lead to deadlock. Make sure the
2510 * flusher is not running on the same workqueue by verifying write
2511 * access.
2512 */
2513 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2514 lock_map_acquire(&cwq->wq->lockdep_map);
2515 else
2516 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002517 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002518
Tejun Heobaf59022010-09-16 10:42:16 +02002519 return true;
2520already_gone:
2521 spin_unlock_irq(&gcwq->lock);
2522 return false;
2523}
2524
Oleg Nesterovdb700892008-07-25 01:47:49 -07002525/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002526 * flush_work - wait for a work to finish executing the last queueing instance
2527 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002528 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002529 * Wait until @work has finished execution. This function considers
2530 * only the last queueing instance of @work. If @work has been
2531 * enqueued across different CPUs on a non-reentrant workqueue or on
2532 * multiple workqueues, @work might still be executing on return on
2533 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002534 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002535 * If @work was queued only on a non-reentrant, ordered or unbound
2536 * workqueue, @work is guaranteed to be idle on return if it hasn't
2537 * been requeued since flush started.
2538 *
2539 * RETURNS:
2540 * %true if flush_work() waited for the work to finish execution,
2541 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002542 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002543bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002544{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002545 struct wq_barrier barr;
2546
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002547 lock_map_acquire(&work->lockdep_map);
2548 lock_map_release(&work->lockdep_map);
2549
Tejun Heobaf59022010-09-16 10:42:16 +02002550 if (start_flush_work(work, &barr, true)) {
2551 wait_for_completion(&barr.done);
2552 destroy_work_on_stack(&barr.work);
2553 return true;
2554 } else
2555 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002556}
2557EXPORT_SYMBOL_GPL(flush_work);
2558
Tejun Heo401a8d02010-09-16 10:36:00 +02002559static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2560{
2561 struct wq_barrier barr;
2562 struct worker *worker;
2563
2564 spin_lock_irq(&gcwq->lock);
2565
2566 worker = find_worker_executing_work(gcwq, work);
2567 if (unlikely(worker))
2568 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2569
2570 spin_unlock_irq(&gcwq->lock);
2571
2572 if (unlikely(worker)) {
2573 wait_for_completion(&barr.done);
2574 destroy_work_on_stack(&barr.work);
2575 return true;
2576 } else
2577 return false;
2578}
2579
2580static bool wait_on_work(struct work_struct *work)
2581{
2582 bool ret = false;
2583 int cpu;
2584
2585 might_sleep();
2586
2587 lock_map_acquire(&work->lockdep_map);
2588 lock_map_release(&work->lockdep_map);
2589
2590 for_each_gcwq_cpu(cpu)
2591 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2592 return ret;
2593}
2594
Tejun Heo09383492010-09-16 10:48:29 +02002595/**
2596 * flush_work_sync - wait until a work has finished execution
2597 * @work: the work to flush
2598 *
2599 * Wait until @work has finished execution. On return, it's
2600 * guaranteed that all queueing instances of @work which happened
2601 * before this function is called are finished. In other words, if
2602 * @work hasn't been requeued since this function was called, @work is
2603 * guaranteed to be idle on return.
2604 *
2605 * RETURNS:
2606 * %true if flush_work_sync() waited for the work to finish execution,
2607 * %false if it was already idle.
2608 */
2609bool flush_work_sync(struct work_struct *work)
2610{
2611 struct wq_barrier barr;
2612 bool pending, waited;
2613
2614 /* we'll wait for executions separately, queue barr only if pending */
2615 pending = start_flush_work(work, &barr, false);
2616
2617 /* wait for executions to finish */
2618 waited = wait_on_work(work);
2619
2620 /* wait for the pending one */
2621 if (pending) {
2622 wait_for_completion(&barr.done);
2623 destroy_work_on_stack(&barr.work);
2624 }
2625
2626 return pending || waited;
2627}
2628EXPORT_SYMBOL_GPL(flush_work_sync);
2629
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002630/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002631 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002632 * so this work can't be re-armed in any way.
2633 */
2634static int try_to_grab_pending(struct work_struct *work)
2635{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002636 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002637 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002638
Tejun Heo22df02b2010-06-29 10:07:10 +02002639 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002640 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002641
2642 /*
2643 * The queueing is in progress, or it is already queued. Try to
2644 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2645 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002646 gcwq = get_work_gcwq(work);
2647 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002648 return ret;
2649
Tejun Heo8b03ae32010-06-29 10:07:12 +02002650 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002651 if (!list_empty(&work->entry)) {
2652 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002653 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654 * In that case we must see the new value after rmb(), see
2655 * insert_work()->wmb().
2656 */
2657 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002658 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002659 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002660 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002661 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002662 get_work_color(work),
2663 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002664 ret = 1;
2665 }
2666 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002667 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002668
2669 return ret;
2670}
2671
Tejun Heo401a8d02010-09-16 10:36:00 +02002672static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002673 struct timer_list* timer)
2674{
2675 int ret;
2676
2677 do {
2678 ret = (timer && likely(del_timer(timer)));
2679 if (!ret)
2680 ret = try_to_grab_pending(work);
2681 wait_on_work(work);
2682 } while (unlikely(ret < 0));
2683
Tejun Heo7a22ad72010-06-29 10:07:13 +02002684 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002685 return ret;
2686}
2687
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002688/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002689 * cancel_work_sync - cancel a work and wait for it to finish
2690 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002691 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002692 * Cancel @work and wait for its execution to finish. This function
2693 * can be used even if the work re-queues itself or migrates to
2694 * another workqueue. On return from this function, @work is
2695 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002696 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002697 * cancel_work_sync(&delayed_work->work) must not be used for
2698 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002699 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002700 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002701 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002702 *
2703 * RETURNS:
2704 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002705 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002706bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002707{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002708 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002709}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002710EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002711
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002712/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002713 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2714 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002715 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002716 * Delayed timer is cancelled and the pending work is queued for
2717 * immediate execution. Like flush_work(), this function only
2718 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002719 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002720 * RETURNS:
2721 * %true if flush_work() waited for the work to finish execution,
2722 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002723 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002724bool flush_delayed_work(struct delayed_work *dwork)
2725{
2726 if (del_timer_sync(&dwork->timer))
2727 __queue_work(raw_smp_processor_id(),
2728 get_work_cwq(&dwork->work)->wq, &dwork->work);
2729 return flush_work(&dwork->work);
2730}
2731EXPORT_SYMBOL(flush_delayed_work);
2732
2733/**
Tejun Heo09383492010-09-16 10:48:29 +02002734 * flush_delayed_work_sync - wait for a dwork to finish
2735 * @dwork: the delayed work to flush
2736 *
2737 * Delayed timer is cancelled and the pending work is queued for
2738 * execution immediately. Other than timer handling, its behavior
2739 * is identical to flush_work_sync().
2740 *
2741 * RETURNS:
2742 * %true if flush_work_sync() waited for the work to finish execution,
2743 * %false if it was already idle.
2744 */
2745bool flush_delayed_work_sync(struct delayed_work *dwork)
2746{
2747 if (del_timer_sync(&dwork->timer))
2748 __queue_work(raw_smp_processor_id(),
2749 get_work_cwq(&dwork->work)->wq, &dwork->work);
2750 return flush_work_sync(&dwork->work);
2751}
2752EXPORT_SYMBOL(flush_delayed_work_sync);
2753
2754/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002755 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2756 * @dwork: the delayed work cancel
2757 *
2758 * This is cancel_work_sync() for delayed works.
2759 *
2760 * RETURNS:
2761 * %true if @dwork was pending, %false otherwise.
2762 */
2763bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002764{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002765 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002766}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002767EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002769/**
2770 * schedule_work - put work task in global workqueue
2771 * @work: job to be done
2772 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002773 * Returns zero if @work was already on the kernel-global workqueue and
2774 * non-zero otherwise.
2775 *
2776 * This puts a job in the kernel-global workqueue if it was not already
2777 * queued and leaves it in the same position on the kernel-global
2778 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002779 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002780int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Tejun Heod320c032010-06-29 10:07:14 +02002782 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
Dave Jonesae90dd52006-06-30 01:40:45 -04002784EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Zhang Ruic1a220e2008-07-23 21:28:39 -07002786/*
2787 * schedule_work_on - put work task on a specific cpu
2788 * @cpu: cpu to put the work task on
2789 * @work: job to be done
2790 *
2791 * This puts a job on a specific cpu
2792 */
2793int schedule_work_on(int cpu, struct work_struct *work)
2794{
Tejun Heod320c032010-06-29 10:07:14 +02002795 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002796}
2797EXPORT_SYMBOL(schedule_work_on);
2798
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002799/**
2800 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002801 * @dwork: job to be done
2802 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002803 *
2804 * After waiting for a given time this puts a job in the kernel-global
2805 * workqueue.
2806 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002807int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002808 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Tejun Heod320c032010-06-29 10:07:14 +02002810 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
Dave Jonesae90dd52006-06-30 01:40:45 -04002812EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002814/**
2815 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2816 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002817 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002818 * @delay: number of jiffies to wait
2819 *
2820 * After waiting for a given time this puts a job in the kernel-global
2821 * workqueue on the specified CPU.
2822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002824 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Tejun Heod320c032010-06-29 10:07:14 +02002826 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
Dave Jonesae90dd52006-06-30 01:40:45 -04002828EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Andrew Mortonb6136772006-06-25 05:47:49 -07002830/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002831 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002832 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002833 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002834 * schedule_on_each_cpu() executes @func on each online CPU using the
2835 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002836 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002837 *
2838 * RETURNS:
2839 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002840 */
David Howells65f27f32006-11-22 14:55:48 +00002841int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002842{
2843 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002844 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002845
Andrew Mortonb6136772006-06-25 05:47:49 -07002846 works = alloc_percpu(struct work_struct);
2847 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002848 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002849
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002850 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002851
Christoph Lameter15316ba2006-01-08 01:00:43 -08002852 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002853 struct work_struct *work = per_cpu_ptr(works, cpu);
2854
2855 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002856 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002857 }
Tejun Heo93981802009-11-17 14:06:20 -08002858
2859 for_each_online_cpu(cpu)
2860 flush_work(per_cpu_ptr(works, cpu));
2861
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002862 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002863 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002864 return 0;
2865}
2866
Alan Sterneef6a7d2010-02-12 17:39:21 +09002867/**
2868 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2869 *
2870 * Forces execution of the kernel-global workqueue and blocks until its
2871 * completion.
2872 *
2873 * Think twice before calling this function! It's very easy to get into
2874 * trouble if you don't take great care. Either of the following situations
2875 * will lead to deadlock:
2876 *
2877 * One of the work items currently on the workqueue needs to acquire
2878 * a lock held by your code or its caller.
2879 *
2880 * Your code is running in the context of a work routine.
2881 *
2882 * They will be detected by lockdep when they occur, but the first might not
2883 * occur very often. It depends on what work items are on the workqueue and
2884 * what locks they need, which you have no control over.
2885 *
2886 * In most situations flushing the entire workqueue is overkill; you merely
2887 * need to know that a particular work item isn't queued and isn't running.
2888 * In such cases you should use cancel_delayed_work_sync() or
2889 * cancel_work_sync() instead.
2890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891void flush_scheduled_work(void)
2892{
Tejun Heod320c032010-06-29 10:07:14 +02002893 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
Dave Jonesae90dd52006-06-30 01:40:45 -04002895EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002898 * execute_in_process_context - reliably execute the routine with user context
2899 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002900 * @ew: guaranteed storage for the execute work structure (must
2901 * be available when the work executes)
2902 *
2903 * Executes the function immediately if process context is available,
2904 * otherwise schedules the function for delayed execution.
2905 *
2906 * Returns: 0 - function was executed
2907 * 1 - function was scheduled for execution
2908 */
David Howells65f27f32006-11-22 14:55:48 +00002909int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002910{
2911 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002912 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002913 return 0;
2914 }
2915
David Howells65f27f32006-11-22 14:55:48 +00002916 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002917 schedule_work(&ew->work);
2918
2919 return 1;
2920}
2921EXPORT_SYMBOL_GPL(execute_in_process_context);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923int keventd_up(void)
2924{
Tejun Heod320c032010-06-29 10:07:14 +02002925 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002928static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002930 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002931 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2932 * Make sure that the alignment isn't lower than that of
2933 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002934 */
Tejun Heo0f900042010-06-29 10:07:11 +02002935 const size_t size = sizeof(struct cpu_workqueue_struct);
2936 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2937 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002938
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002939 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002940 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002941 else {
Tejun Heof3421792010-07-02 10:03:51 +02002942 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002943
Tejun Heof3421792010-07-02 10:03:51 +02002944 /*
2945 * Allocate enough room to align cwq and put an extra
2946 * pointer at the end pointing back to the originally
2947 * allocated pointer which will be used for free.
2948 */
2949 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2950 if (ptr) {
2951 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2952 *(void **)(wq->cpu_wq.single + 1) = ptr;
2953 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002954 }
Tejun Heof3421792010-07-02 10:03:51 +02002955
Tejun Heo0415b00d12011-03-24 18:50:09 +01002956 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002957 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2958 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002959}
2960
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002961static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002962{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002963 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002964 free_percpu(wq->cpu_wq.pcpu);
2965 else if (wq->cpu_wq.single) {
2966 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002967 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002968 }
2969}
2970
Tejun Heof3421792010-07-02 10:03:51 +02002971static int wq_clamp_max_active(int max_active, unsigned int flags,
2972 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002973{
Tejun Heof3421792010-07-02 10:03:51 +02002974 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2975
2976 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002977 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2978 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002979 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002980
Tejun Heof3421792010-07-02 10:03:51 +02002981 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002982}
2983
Tejun Heob196be82012-01-10 15:11:35 -08002984struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002985 unsigned int flags,
2986 int max_active,
2987 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002988 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002989{
Tejun Heob196be82012-01-10 15:11:35 -08002990 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002991 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002992 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002993 size_t namelen;
2994
2995 /* determine namelen, allocate wq and format name */
2996 va_start(args, lock_name);
2997 va_copy(args1, args);
2998 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2999
3000 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3001 if (!wq)
3002 goto err;
3003
3004 vsnprintf(wq->name, namelen, fmt, args1);
3005 va_end(args);
3006 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003007
Tejun Heof3421792010-07-02 10:03:51 +02003008 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003009 * Workqueues which may be used during memory reclaim should
3010 * have a rescuer to guarantee forward progress.
3011 */
3012 if (flags & WQ_MEM_RECLAIM)
3013 flags |= WQ_RESCUER;
3014
Tejun Heod320c032010-06-29 10:07:14 +02003015 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003016 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003017
Tejun Heob196be82012-01-10 15:11:35 -08003018 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003019 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003020 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003021 mutex_init(&wq->flush_mutex);
3022 atomic_set(&wq->nr_cwqs_to_flush, 0);
3023 INIT_LIST_HEAD(&wq->flusher_queue);
3024 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003025
Johannes Bergeb13ba82008-01-16 09:51:58 +01003026 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003027 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003028
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003029 if (alloc_cwqs(wq) < 0)
3030 goto err;
3031
Tejun Heof3421792010-07-02 10:03:51 +02003032 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003033 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003034 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003035
Tejun Heo0f900042010-06-29 10:07:11 +02003036 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003037 cwq->pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02003038 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003039 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003040 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003041 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003042 }
3043
Tejun Heoe22bee72010-06-29 10:07:14 +02003044 if (flags & WQ_RESCUER) {
3045 struct worker *rescuer;
3046
Tejun Heof2e005a2010-07-20 15:59:09 +02003047 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003048 goto err;
3049
3050 wq->rescuer = rescuer = alloc_worker();
3051 if (!rescuer)
3052 goto err;
3053
Tejun Heob196be82012-01-10 15:11:35 -08003054 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3055 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003056 if (IS_ERR(rescuer->task))
3057 goto err;
3058
Tejun Heoe22bee72010-06-29 10:07:14 +02003059 rescuer->task->flags |= PF_THREAD_BOUND;
3060 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003061 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003062
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003063 /*
3064 * workqueue_lock protects global freeze state and workqueues
3065 * list. Grab it, set max_active accordingly and add the new
3066 * workqueue to workqueues list.
3067 */
Tejun Heo15376632010-06-29 10:07:11 +02003068 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003069
Tejun Heo58a69cb2011-02-16 09:25:31 +01003070 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003071 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003072 get_cwq(cpu, wq)->max_active = 0;
3073
Tejun Heo15376632010-06-29 10:07:11 +02003074 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003075
Tejun Heo15376632010-06-29 10:07:11 +02003076 spin_unlock(&workqueue_lock);
3077
Oleg Nesterov3af244332007-05-09 02:34:09 -07003078 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003079err:
3080 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003081 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003082 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003083 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003084 kfree(wq);
3085 }
3086 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003087}
Tejun Heod320c032010-06-29 10:07:14 +02003088EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003089
3090/**
3091 * destroy_workqueue - safely terminate a workqueue
3092 * @wq: target workqueue
3093 *
3094 * Safely destroy a workqueue. All work currently pending will be done first.
3095 */
3096void destroy_workqueue(struct workqueue_struct *wq)
3097{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003098 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003099
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003100 /* drain it before proceeding with destruction */
3101 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003102
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003103 /*
3104 * wq list is used to freeze wq, remove from list after
3105 * flushing is complete in case freeze races us.
3106 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003107 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003108 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003109 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003110
Tejun Heoe22bee72010-06-29 10:07:14 +02003111 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003112 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003113 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3114 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003115
Tejun Heo73f53c42010-06-29 10:07:11 +02003116 for (i = 0; i < WORK_NR_COLORS; i++)
3117 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003118 BUG_ON(cwq->nr_active);
3119 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003120 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003121
Tejun Heoe22bee72010-06-29 10:07:14 +02003122 if (wq->flags & WQ_RESCUER) {
3123 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003124 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003125 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003126 }
3127
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003128 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003129 kfree(wq);
3130}
3131EXPORT_SYMBOL_GPL(destroy_workqueue);
3132
Tejun Heodcd989c2010-06-29 10:07:14 +02003133/**
3134 * workqueue_set_max_active - adjust max_active of a workqueue
3135 * @wq: target workqueue
3136 * @max_active: new max_active value.
3137 *
3138 * Set max_active of @wq to @max_active.
3139 *
3140 * CONTEXT:
3141 * Don't call from IRQ context.
3142 */
3143void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3144{
3145 unsigned int cpu;
3146
Tejun Heof3421792010-07-02 10:03:51 +02003147 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003148
3149 spin_lock(&workqueue_lock);
3150
3151 wq->saved_max_active = max_active;
3152
Tejun Heof3421792010-07-02 10:03:51 +02003153 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003154 struct global_cwq *gcwq = get_gcwq(cpu);
3155
3156 spin_lock_irq(&gcwq->lock);
3157
Tejun Heo58a69cb2011-02-16 09:25:31 +01003158 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003159 !(gcwq->flags & GCWQ_FREEZING))
3160 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3161
3162 spin_unlock_irq(&gcwq->lock);
3163 }
3164
3165 spin_unlock(&workqueue_lock);
3166}
3167EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3168
3169/**
3170 * workqueue_congested - test whether a workqueue is congested
3171 * @cpu: CPU in question
3172 * @wq: target workqueue
3173 *
3174 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3175 * no synchronization around this function and the test result is
3176 * unreliable and only useful as advisory hints or for debugging.
3177 *
3178 * RETURNS:
3179 * %true if congested, %false otherwise.
3180 */
3181bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3182{
3183 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3184
3185 return !list_empty(&cwq->delayed_works);
3186}
3187EXPORT_SYMBOL_GPL(workqueue_congested);
3188
3189/**
3190 * work_cpu - return the last known associated cpu for @work
3191 * @work: the work of interest
3192 *
3193 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003194 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003195 */
3196unsigned int work_cpu(struct work_struct *work)
3197{
3198 struct global_cwq *gcwq = get_work_gcwq(work);
3199
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003200 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003201}
3202EXPORT_SYMBOL_GPL(work_cpu);
3203
3204/**
3205 * work_busy - test whether a work is currently pending or running
3206 * @work: the work to be tested
3207 *
3208 * Test whether @work is currently pending or running. There is no
3209 * synchronization around this function and the test result is
3210 * unreliable and only useful as advisory hints or for debugging.
3211 * Especially for reentrant wqs, the pending state might hide the
3212 * running state.
3213 *
3214 * RETURNS:
3215 * OR'd bitmask of WORK_BUSY_* bits.
3216 */
3217unsigned int work_busy(struct work_struct *work)
3218{
3219 struct global_cwq *gcwq = get_work_gcwq(work);
3220 unsigned long flags;
3221 unsigned int ret = 0;
3222
3223 if (!gcwq)
3224 return false;
3225
3226 spin_lock_irqsave(&gcwq->lock, flags);
3227
3228 if (work_pending(work))
3229 ret |= WORK_BUSY_PENDING;
3230 if (find_worker_executing_work(gcwq, work))
3231 ret |= WORK_BUSY_RUNNING;
3232
3233 spin_unlock_irqrestore(&gcwq->lock, flags);
3234
3235 return ret;
3236}
3237EXPORT_SYMBOL_GPL(work_busy);
3238
Tejun Heodb7bccf2010-06-29 10:07:12 +02003239/*
3240 * CPU hotplug.
3241 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003242 * There are two challenges in supporting CPU hotplug. Firstly, there
3243 * are a lot of assumptions on strong associations among work, cwq and
3244 * gcwq which make migrating pending and scheduled works very
3245 * difficult to implement without impacting hot paths. Secondly,
3246 * gcwqs serve mix of short, long and very long running works making
3247 * blocked draining impractical.
3248 *
3249 * This is solved by allowing a gcwq to be detached from CPU, running
3250 * it with unbound (rogue) workers and allowing it to be reattached
3251 * later if the cpu comes back online. A separate thread is created
3252 * to govern a gcwq in such state and is called the trustee of the
3253 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003254 *
3255 * Trustee states and their descriptions.
3256 *
3257 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3258 * new trustee is started with this state.
3259 *
3260 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003261 * assuming the manager role and making all existing
3262 * workers rogue. DOWN_PREPARE waits for trustee to
3263 * enter this state. After reaching IN_CHARGE, trustee
3264 * tries to execute the pending worklist until it's empty
3265 * and the state is set to BUTCHER, or the state is set
3266 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003267 *
3268 * BUTCHER Command state which is set by the cpu callback after
3269 * the cpu has went down. Once this state is set trustee
3270 * knows that there will be no new works on the worklist
3271 * and once the worklist is empty it can proceed to
3272 * killing idle workers.
3273 *
3274 * RELEASE Command state which is set by the cpu callback if the
3275 * cpu down has been canceled or it has come online
3276 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003277 * trying to drain or butcher and clears ROGUE, rebinds
3278 * all remaining workers back to the cpu and releases
3279 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003280 *
3281 * DONE Trustee will enter this state after BUTCHER or RELEASE
3282 * is complete.
3283 *
3284 * trustee CPU draining
3285 * took over down complete
3286 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3287 * | | ^
3288 * | CPU is back online v return workers |
3289 * ----------------> RELEASE --------------
3290 */
3291
3292/**
3293 * trustee_wait_event_timeout - timed event wait for trustee
3294 * @cond: condition to wait for
3295 * @timeout: timeout in jiffies
3296 *
3297 * wait_event_timeout() for trustee to use. Handles locking and
3298 * checks for RELEASE request.
3299 *
3300 * CONTEXT:
3301 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3302 * multiple times. To be used by trustee.
3303 *
3304 * RETURNS:
3305 * Positive indicating left time if @cond is satisfied, 0 if timed
3306 * out, -1 if canceled.
3307 */
3308#define trustee_wait_event_timeout(cond, timeout) ({ \
3309 long __ret = (timeout); \
3310 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3311 __ret) { \
3312 spin_unlock_irq(&gcwq->lock); \
3313 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3314 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3315 __ret); \
3316 spin_lock_irq(&gcwq->lock); \
3317 } \
3318 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3319})
3320
3321/**
3322 * trustee_wait_event - event wait for trustee
3323 * @cond: condition to wait for
3324 *
3325 * wait_event() for trustee to use. Automatically handles locking and
3326 * checks for CANCEL request.
3327 *
3328 * CONTEXT:
3329 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3330 * multiple times. To be used by trustee.
3331 *
3332 * RETURNS:
3333 * 0 if @cond is satisfied, -1 if canceled.
3334 */
3335#define trustee_wait_event(cond) ({ \
3336 long __ret1; \
3337 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3338 __ret1 < 0 ? -1 : 0; \
3339})
3340
3341static int __cpuinit trustee_thread(void *__gcwq)
3342{
3343 struct global_cwq *gcwq = __gcwq;
3344 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003345 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003346 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003347 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003348 int i;
3349
3350 BUG_ON(gcwq->cpu != smp_processor_id());
3351
3352 spin_lock_irq(&gcwq->lock);
3353 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003354 * Claim the manager position and make all workers rogue.
3355 * Trustee must be bound to the target cpu and can't be
3356 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003357 */
3358 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003359 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3360 BUG_ON(rc < 0);
3361
3362 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003363
Tejun Heobd7bdd42012-07-12 14:46:37 -07003364 list_for_each_entry(worker, &gcwq->pool.idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003365 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003366
3367 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003368 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003369
3370 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003371 * Call schedule() so that we cross rq->lock and thus can
3372 * guarantee sched callbacks see the rogue flag. This is
3373 * necessary as scheduler callbacks may be invoked from other
3374 * cpus.
3375 */
3376 spin_unlock_irq(&gcwq->lock);
3377 schedule();
3378 spin_lock_irq(&gcwq->lock);
3379
3380 /*
Tejun Heocb444762010-07-02 10:03:50 +02003381 * Sched callbacks are disabled now. Zap nr_running. After
3382 * this, nr_running stays zero and need_more_worker() and
3383 * keep_working() are always true as long as the worklist is
3384 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003385 */
Tejun Heocb444762010-07-02 10:03:50 +02003386 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003387
3388 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003389 del_timer_sync(&gcwq->pool.idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003390 spin_lock_irq(&gcwq->lock);
3391
3392 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003393 * We're now in charge. Notify and proceed to drain. We need
3394 * to keep the gcwq running during the whole CPU down
3395 * procedure as other cpu hotunplug callbacks may need to
3396 * flush currently running tasks.
3397 */
3398 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3399 wake_up_all(&gcwq->trustee_wait);
3400
3401 /*
3402 * The original cpu is in the process of dying and may go away
3403 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003404 * be migrated to other cpus. Try draining any left work. We
3405 * want to get it over with ASAP - spam rescuers, wake up as
3406 * many idlers as necessary and create new ones till the
3407 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003408 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003409 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003410 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07003411 while (gcwq->pool.nr_workers != gcwq->pool.nr_idle ||
Tejun Heodb7bccf2010-06-29 10:07:12 +02003412 gcwq->flags & GCWQ_FREEZING ||
3413 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003414 int nr_works = 0;
3415
Tejun Heobd7bdd42012-07-12 14:46:37 -07003416 list_for_each_entry(work, &gcwq->pool.worklist, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003417 send_mayday(work);
3418 nr_works++;
3419 }
3420
Tejun Heobd7bdd42012-07-12 14:46:37 -07003421 list_for_each_entry(worker, &gcwq->pool.idle_list, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003422 if (!nr_works--)
3423 break;
3424 wake_up_process(worker->task);
3425 }
3426
3427 if (need_to_create_worker(gcwq)) {
3428 spin_unlock_irq(&gcwq->lock);
3429 worker = create_worker(gcwq, false);
3430 spin_lock_irq(&gcwq->lock);
3431 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003432 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003433 start_worker(worker);
3434 }
3435 }
3436
Tejun Heodb7bccf2010-06-29 10:07:12 +02003437 /* give a breather */
3438 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3439 break;
3440 }
3441
Tejun Heoe22bee72010-06-29 10:07:14 +02003442 /*
3443 * Either all works have been scheduled and cpu is down, or
3444 * cpu down has already been canceled. Wait for and butcher
3445 * all workers till we're canceled.
3446 */
3447 do {
Tejun Heobd7bdd42012-07-12 14:46:37 -07003448 rc = trustee_wait_event(!list_empty(&gcwq->pool.idle_list));
3449 while (!list_empty(&gcwq->pool.idle_list))
3450 destroy_worker(list_first_entry(&gcwq->pool.idle_list,
Tejun Heoe22bee72010-06-29 10:07:14 +02003451 struct worker, entry));
Tejun Heobd7bdd42012-07-12 14:46:37 -07003452 } while (gcwq->pool.nr_workers && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003453
3454 /*
3455 * At this point, either draining has completed and no worker
3456 * is left, or cpu down has been canceled or the cpu is being
3457 * brought back up. There shouldn't be any idle one left.
3458 * Tell the remaining busy ones to rebind once it finishes the
3459 * currently scheduled works by scheduling the rebind_work.
3460 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07003461 WARN_ON(!list_empty(&gcwq->pool.idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003462
3463 for_each_busy_worker(worker, i, pos, gcwq) {
3464 struct work_struct *rebind_work = &worker->rebind_work;
3465
3466 /*
3467 * Rebind_work may race with future cpu hotplug
3468 * operations. Use a separate flag to mark that
3469 * rebinding is scheduled.
3470 */
Tejun Heocb444762010-07-02 10:03:50 +02003471 worker->flags |= WORKER_REBIND;
3472 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003473
3474 /* queue rebind_work, wq doesn't matter, use the default one */
3475 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3476 work_data_bits(rebind_work)))
3477 continue;
3478
3479 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003480 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003481 worker->scheduled.next,
3482 work_color_to_flags(WORK_NO_COLOR));
3483 }
3484
3485 /* relinquish manager role */
3486 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3487
Tejun Heodb7bccf2010-06-29 10:07:12 +02003488 /* notify completion */
3489 gcwq->trustee = NULL;
3490 gcwq->trustee_state = TRUSTEE_DONE;
3491 wake_up_all(&gcwq->trustee_wait);
3492 spin_unlock_irq(&gcwq->lock);
3493 return 0;
3494}
3495
3496/**
3497 * wait_trustee_state - wait for trustee to enter the specified state
3498 * @gcwq: gcwq the trustee of interest belongs to
3499 * @state: target state to wait for
3500 *
3501 * Wait for the trustee to reach @state. DONE is already matched.
3502 *
3503 * CONTEXT:
3504 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3505 * multiple times. To be used by cpu_callback.
3506 */
3507static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003508__releases(&gcwq->lock)
3509__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003510{
3511 if (!(gcwq->trustee_state == state ||
3512 gcwq->trustee_state == TRUSTEE_DONE)) {
3513 spin_unlock_irq(&gcwq->lock);
3514 __wait_event(gcwq->trustee_wait,
3515 gcwq->trustee_state == state ||
3516 gcwq->trustee_state == TRUSTEE_DONE);
3517 spin_lock_irq(&gcwq->lock);
3518 }
3519}
3520
Oleg Nesterov3af244332007-05-09 02:34:09 -07003521static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3522 unsigned long action,
3523 void *hcpu)
3524{
3525 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003526 struct global_cwq *gcwq = get_gcwq(cpu);
3527 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003528 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003529 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003531 action &= ~CPU_TASKS_FROZEN;
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003534 case CPU_DOWN_PREPARE:
3535 new_trustee = kthread_create(trustee_thread, gcwq,
3536 "workqueue_trustee/%d\n", cpu);
3537 if (IS_ERR(new_trustee))
3538 return notifier_from_errno(PTR_ERR(new_trustee));
3539 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003540 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 case CPU_UP_PREPARE:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003542 BUG_ON(gcwq->pool.first_idle);
Tejun Heoe22bee72010-06-29 10:07:14 +02003543 new_worker = create_worker(gcwq, false);
3544 if (!new_worker) {
3545 if (new_trustee)
3546 kthread_stop(new_trustee);
3547 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
3550
Tejun Heodb7bccf2010-06-29 10:07:12 +02003551 /* some are called w/ irq disabled, don't disturb irq status */
3552 spin_lock_irqsave(&gcwq->lock, flags);
3553
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003554 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003555 case CPU_DOWN_PREPARE:
3556 /* initialize trustee and tell it to acquire the gcwq */
3557 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3558 gcwq->trustee = new_trustee;
3559 gcwq->trustee_state = TRUSTEE_START;
3560 wake_up_process(gcwq->trustee);
3561 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003562 /* fall through */
3563 case CPU_UP_PREPARE:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003564 BUG_ON(gcwq->pool.first_idle);
3565 gcwq->pool.first_idle = new_worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003566 break;
3567
3568 case CPU_DYING:
3569 /*
3570 * Before this, the trustee and all workers except for
3571 * the ones which are still executing works from
3572 * before the last CPU down must be on the cpu. After
3573 * this, they'll all be diasporas.
3574 */
3575 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003576 break;
3577
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003578 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003579 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003580 /* fall through */
3581 case CPU_UP_CANCELED:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003582 destroy_worker(gcwq->pool.first_idle);
3583 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003584 break;
3585
3586 case CPU_DOWN_FAILED:
3587 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003588 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003589 if (gcwq->trustee_state != TRUSTEE_DONE) {
3590 gcwq->trustee_state = TRUSTEE_RELEASE;
3591 wake_up_process(gcwq->trustee);
3592 wait_trustee_state(gcwq, TRUSTEE_DONE);
3593 }
3594
Tejun Heoe22bee72010-06-29 10:07:14 +02003595 /*
3596 * Trustee is done and there might be no worker left.
3597 * Put the first_idle in and request a real manager to
3598 * take a look.
3599 */
3600 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003601 kthread_bind(gcwq->pool.first_idle->task, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003602 spin_lock_irq(&gcwq->lock);
3603 gcwq->flags |= GCWQ_MANAGE_WORKERS;
Tejun Heobd7bdd42012-07-12 14:46:37 -07003604 start_worker(gcwq->pool.first_idle);
3605 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003606 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003607 }
3608
Tejun Heodb7bccf2010-06-29 10:07:12 +02003609 spin_unlock_irqrestore(&gcwq->lock, flags);
3610
Tejun Heo15376632010-06-29 10:07:11 +02003611 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Rusty Russell2d3854a2008-11-05 13:39:10 +11003614#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003615
Rusty Russell2d3854a2008-11-05 13:39:10 +11003616struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003617 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003618 long (*fn)(void *);
3619 void *arg;
3620 long ret;
3621};
3622
Andrew Morton6b440032009-04-09 09:50:37 -06003623static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003624{
Andrew Morton6b440032009-04-09 09:50:37 -06003625 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003626 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003627 complete(&wfc->completion);
3628 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003629}
3630
3631/**
3632 * work_on_cpu - run a function in user context on a particular cpu
3633 * @cpu: the cpu to run on
3634 * @fn: the function to run
3635 * @arg: the function arg
3636 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003637 * This will return the value @fn returns.
3638 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003639 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003640 */
3641long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3642{
Andrew Morton6b440032009-04-09 09:50:37 -06003643 struct task_struct *sub_thread;
3644 struct work_for_cpu wfc = {
3645 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3646 .fn = fn,
3647 .arg = arg,
3648 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003649
Andrew Morton6b440032009-04-09 09:50:37 -06003650 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3651 if (IS_ERR(sub_thread))
3652 return PTR_ERR(sub_thread);
3653 kthread_bind(sub_thread, cpu);
3654 wake_up_process(sub_thread);
3655 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003656 return wfc.ret;
3657}
3658EXPORT_SYMBOL_GPL(work_on_cpu);
3659#endif /* CONFIG_SMP */
3660
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003661#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303662
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003663/**
3664 * freeze_workqueues_begin - begin freezing workqueues
3665 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003666 * Start freezing workqueues. After this function returns, all freezable
3667 * workqueues will queue new works to their frozen_works list instead of
3668 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003669 *
3670 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003671 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003672 */
3673void freeze_workqueues_begin(void)
3674{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003675 unsigned int cpu;
3676
3677 spin_lock(&workqueue_lock);
3678
3679 BUG_ON(workqueue_freezing);
3680 workqueue_freezing = true;
3681
Tejun Heof3421792010-07-02 10:03:51 +02003682 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003683 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003684 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003685
3686 spin_lock_irq(&gcwq->lock);
3687
Tejun Heodb7bccf2010-06-29 10:07:12 +02003688 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3689 gcwq->flags |= GCWQ_FREEZING;
3690
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003691 list_for_each_entry(wq, &workqueues, list) {
3692 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3693
Tejun Heo58a69cb2011-02-16 09:25:31 +01003694 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003695 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003696 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003697
3698 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003699 }
3700
3701 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703
3704/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003705 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003706 *
3707 * Check whether freezing is complete. This function must be called
3708 * between freeze_workqueues_begin() and thaw_workqueues().
3709 *
3710 * CONTEXT:
3711 * Grabs and releases workqueue_lock.
3712 *
3713 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003714 * %true if some freezable workqueues are still busy. %false if freezing
3715 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716 */
3717bool freeze_workqueues_busy(void)
3718{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003719 unsigned int cpu;
3720 bool busy = false;
3721
3722 spin_lock(&workqueue_lock);
3723
3724 BUG_ON(!workqueue_freezing);
3725
Tejun Heof3421792010-07-02 10:03:51 +02003726 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003727 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003728 /*
3729 * nr_active is monotonically decreasing. It's safe
3730 * to peek without lock.
3731 */
3732 list_for_each_entry(wq, &workqueues, list) {
3733 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3734
Tejun Heo58a69cb2011-02-16 09:25:31 +01003735 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003736 continue;
3737
3738 BUG_ON(cwq->nr_active < 0);
3739 if (cwq->nr_active) {
3740 busy = true;
3741 goto out_unlock;
3742 }
3743 }
3744 }
3745out_unlock:
3746 spin_unlock(&workqueue_lock);
3747 return busy;
3748}
3749
3750/**
3751 * thaw_workqueues - thaw workqueues
3752 *
3753 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003754 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003755 *
3756 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003757 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003758 */
3759void thaw_workqueues(void)
3760{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003761 unsigned int cpu;
3762
3763 spin_lock(&workqueue_lock);
3764
3765 if (!workqueue_freezing)
3766 goto out_unlock;
3767
Tejun Heof3421792010-07-02 10:03:51 +02003768 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003769 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003770 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003771
3772 spin_lock_irq(&gcwq->lock);
3773
Tejun Heodb7bccf2010-06-29 10:07:12 +02003774 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3775 gcwq->flags &= ~GCWQ_FREEZING;
3776
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003777 list_for_each_entry(wq, &workqueues, list) {
3778 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3779
Tejun Heo58a69cb2011-02-16 09:25:31 +01003780 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003781 continue;
3782
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003783 /* restore max_active and repopulate worklist */
3784 cwq->max_active = wq->saved_max_active;
3785
3786 while (!list_empty(&cwq->delayed_works) &&
3787 cwq->nr_active < cwq->max_active)
3788 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003789 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003790
Tejun Heoe22bee72010-06-29 10:07:14 +02003791 wake_up_worker(gcwq);
3792
Tejun Heo8b03ae32010-06-29 10:07:12 +02003793 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 }
3795
3796 workqueue_freezing = false;
3797out_unlock:
3798 spin_unlock(&workqueue_lock);
3799}
3800#endif /* CONFIG_FREEZER */
3801
Suresh Siddha6ee05782010-07-30 14:57:37 -07003802static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
Tejun Heoc34056a2010-06-29 10:07:11 +02003804 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003805 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003806
Tejun Heof6500942010-08-09 11:50:34 +02003807 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003808
3809 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003810 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003811 struct global_cwq *gcwq = get_gcwq(cpu);
3812
3813 spin_lock_init(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003814 gcwq->pool.gcwq = gcwq;
3815 INIT_LIST_HEAD(&gcwq->pool.worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003816 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003817 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003818
Tejun Heobd7bdd42012-07-12 14:46:37 -07003819 INIT_LIST_HEAD(&gcwq->pool.idle_list);
Tejun Heoc8e55f32010-06-29 10:07:12 +02003820 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3821 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3822
Tejun Heobd7bdd42012-07-12 14:46:37 -07003823 init_timer_deferrable(&gcwq->pool.idle_timer);
3824 gcwq->pool.idle_timer.function = idle_worker_timeout;
3825 gcwq->pool.idle_timer.data = (unsigned long)gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02003826
Tejun Heobd7bdd42012-07-12 14:46:37 -07003827 setup_timer(&gcwq->pool.mayday_timer, gcwq_mayday_timeout,
Tejun Heoe22bee72010-06-29 10:07:14 +02003828 (unsigned long)gcwq);
3829
Tejun Heobd7bdd42012-07-12 14:46:37 -07003830 ida_init(&gcwq->pool.worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003831
3832 gcwq->trustee_state = TRUSTEE_DONE;
3833 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003834 }
3835
Tejun Heoe22bee72010-06-29 10:07:14 +02003836 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003837 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003838 struct global_cwq *gcwq = get_gcwq(cpu);
3839 struct worker *worker;
3840
Tejun Heo477a3c32010-08-31 10:54:35 +02003841 if (cpu != WORK_CPU_UNBOUND)
3842 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003843 worker = create_worker(gcwq, true);
3844 BUG_ON(!worker);
3845 spin_lock_irq(&gcwq->lock);
3846 start_worker(worker);
3847 spin_unlock_irq(&gcwq->lock);
3848 }
3849
Tejun Heod320c032010-06-29 10:07:14 +02003850 system_wq = alloc_workqueue("events", 0, 0);
3851 system_long_wq = alloc_workqueue("events_long", 0, 0);
3852 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003853 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3854 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003855 system_freezable_wq = alloc_workqueue("events_freezable",
3856 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003857 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3858 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003859 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003860 !system_unbound_wq || !system_freezable_wq ||
3861 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003864early_initcall(init_workqueues);