blob: f1a6e9e8e06e9e0157dd6ca4e9879037a5c7c5aa [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 Heo1f19ade2012-07-12 14:46:37 -070049 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
50 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
51
52 /* pool flags */
53 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
54 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020055
Tejun Heoc8e55f32010-06-29 10:07:12 +020056 /* worker flags */
57 WORKER_STARTED = 1 << 0, /* started */
58 WORKER_DIE = 1 << 1, /* die die die */
59 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020060 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020061 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020062 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020063 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020064 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020065
Tejun Heofb0e7be2010-06-29 10:07:15 +020066 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020067 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020068
69 /* gcwq->trustee_state */
70 TRUSTEE_START = 0, /* start */
71 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
72 TRUSTEE_BUTCHER = 2, /* butcher workers */
73 TRUSTEE_RELEASE = 3, /* release workers */
74 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020075
Tejun Heo2bfedd32012-07-13 22:16:45 -070076 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo0290d202012-07-13 22:16:44 -070077
Tejun Heoc8e55f32010-06-29 10:07:12 +020078 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
79 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
80 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heoe22bee72010-06-29 10:07:14 +020082 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
83 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
84
Tejun Heo3233cdb2011-02-16 18:10:19 +010085 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
86 /* call for help after 10ms
87 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020088 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
89 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020090 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020091
92 /*
93 * Rescue workers are used only on emergencies and shared by
94 * all cpus. Give -20.
95 */
96 RESCUER_NICE_LEVEL = -20,
Tejun Heo2bfedd32012-07-13 22:16:45 -070097 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020098};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200101 * Structure fields follow one of the following exclusion rules.
102 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200103 * I: Modifiable by initialization/destruction paths and read-only for
104 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200105 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200106 * P: Preemption protected. Disabling preemption is enough and should
107 * only be modified and accessed from the local cpu.
108 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200109 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * X: During normal operation, modification requires gcwq->lock and
112 * should be done only from local cpu. Either disabling preemption
113 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200114 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200115 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200116 * F: wq->flush_mutex protected.
117 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 * W: workqueue_lock protected.
119 */
120
Tejun Heo8b03ae32010-06-29 10:07:12 +0200121struct global_cwq;
Tejun Heo47303942012-07-12 14:46:37 -0700122struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200123
Tejun Heoe22bee72010-06-29 10:07:14 +0200124/*
125 * The poor guys doing the actual heavy lifting. All on-duty workers
126 * are either serving the manager role, on idle list or on busy hash.
127 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200128struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200129 /* on idle list while idle, on busy hash table while busy */
130 union {
131 struct list_head entry; /* L: while idle */
132 struct hlist_node hentry; /* L: while busy */
133 };
134
Tejun Heoc34056a2010-06-29 10:07:11 +0200135 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200136 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200137 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138 struct task_struct *task; /* I: worker task */
Tejun Heo47303942012-07-12 14:46:37 -0700139 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200140 /* 64 bytes boundary on 64bit, 32 on 32bit */
141 unsigned long last_active; /* L: last active timestamp */
142 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200143 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200144 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200145};
146
Tejun Heo47303942012-07-12 14:46:37 -0700147struct worker_pool {
148 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo1f19ade2012-07-12 14:46:37 -0700149 unsigned int flags; /* X: flags */
Tejun Heo47303942012-07-12 14:46:37 -0700150
151 struct list_head worklist; /* L: list of pending works */
152 int nr_workers; /* L: total number of workers */
153 int nr_idle; /* L: currently idle ones */
154
155 struct list_head idle_list; /* X: list of idle workers */
156 struct timer_list idle_timer; /* L: worker idle timeout */
157 struct timer_list mayday_timer; /* L: SOS timer for workers */
158
159 struct ida worker_ida; /* L: for worker IDs */
160 struct worker *first_idle; /* L: first idle worker */
161};
162
Tejun Heo4690c4a2010-06-29 10:07:10 +0200163/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200164 * Global per-cpu workqueue. There's one and only one for each cpu
165 * and all works are queued and processed here regardless of their
166 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200167 */
168struct global_cwq {
169 spinlock_t lock; /* the gcwq lock */
170 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200171 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200172
Tejun Heo47303942012-07-12 14:46:37 -0700173 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200174 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
175 /* L: hash of busy workers */
176
Tejun Heo2bfedd32012-07-13 22:16:45 -0700177 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200178
179 struct task_struct *trustee; /* L: for gcwq shutdown */
180 unsigned int trustee_state; /* L: trustee state */
181 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200182} ____cacheline_aligned_in_smp;
183
184/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200185 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200186 * work_struct->data are used for flags and thus cwqs need to be
187 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189struct cpu_workqueue_struct {
Tejun Heo47303942012-07-12 14:46:37 -0700190 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200191 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200192 int work_color; /* L: current color */
193 int flush_color; /* L: flushing color */
194 int nr_in_flight[WORK_NR_COLORS];
195 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200196 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200197 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200199};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200202 * Structure used to wait for workqueue flush.
203 */
204struct wq_flusher {
205 struct list_head list; /* F: list of flushers */
206 int flush_color; /* F: flush color waiting for */
207 struct completion done; /* flush completion */
208};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Tejun Heo73f53c42010-06-29 10:07:11 +0200210/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200211 * All cpumasks are assumed to be always set on UP and thus can't be
212 * used to determine whether there's something to be done.
213 */
214#ifdef CONFIG_SMP
215typedef cpumask_var_t mayday_mask_t;
216#define mayday_test_and_set_cpu(cpu, mask) \
217 cpumask_test_and_set_cpu((cpu), (mask))
218#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
219#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200220#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200221#define free_mayday_mask(mask) free_cpumask_var((mask))
222#else
223typedef unsigned long mayday_mask_t;
224#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
225#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
226#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
227#define alloc_mayday_mask(maskp, gfp) true
228#define free_mayday_mask(mask) do { } while (0)
229#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/*
232 * The externally visible workqueue abstraction is an array of
233 * per-CPU workqueues:
234 */
235struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200236 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200237 union {
238 struct cpu_workqueue_struct __percpu *pcpu;
239 struct cpu_workqueue_struct *single;
240 unsigned long v;
241 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200242 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200243
244 struct mutex flush_mutex; /* protects wq flushing */
245 int work_color; /* F: current work color */
246 int flush_color; /* F: current flush color */
247 atomic_t nr_cwqs_to_flush; /* flush in progress */
248 struct wq_flusher *first_flusher; /* F: first flusher */
249 struct list_head flusher_queue; /* F: flush waiters */
250 struct list_head flusher_overflow; /* F: flush overflow list */
251
Tejun Heof2e005a2010-07-20 15:59:09 +0200252 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200253 struct worker *rescuer; /* I: rescue worker */
254
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200255 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200256 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700257#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200258 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700259#endif
Tejun Heob196be82012-01-10 15:11:35 -0800260 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Tejun Heod320c032010-06-29 10:07:14 +0200263struct workqueue_struct *system_wq __read_mostly;
264struct workqueue_struct *system_long_wq __read_mostly;
265struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200266struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100267struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100268struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200269EXPORT_SYMBOL_GPL(system_wq);
270EXPORT_SYMBOL_GPL(system_long_wq);
271EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200272EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100273EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100274EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200275
Tejun Heo97bd2342010-10-05 10:41:14 +0200276#define CREATE_TRACE_POINTS
277#include <trace/events/workqueue.h>
278
Tejun Heo0290d202012-07-13 22:16:44 -0700279#define for_each_worker_pool(pool, gcwq) \
Tejun Heo2bfedd32012-07-13 22:16:45 -0700280 for ((pool) = &(gcwq)->pools[0]; \
281 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo0290d202012-07-13 22:16:44 -0700282
Tejun Heodb7bccf2010-06-29 10:07:12 +0200283#define for_each_busy_worker(worker, i, pos, gcwq) \
284 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
285 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
286
Tejun Heof3421792010-07-02 10:03:51 +0200287static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
288 unsigned int sw)
289{
290 if (cpu < nr_cpu_ids) {
291 if (sw & 1) {
292 cpu = cpumask_next(cpu, mask);
293 if (cpu < nr_cpu_ids)
294 return cpu;
295 }
296 if (sw & 2)
297 return WORK_CPU_UNBOUND;
298 }
299 return WORK_CPU_NONE;
300}
301
302static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
303 struct workqueue_struct *wq)
304{
305 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
306}
307
Tejun Heo09884952010-08-01 11:50:12 +0200308/*
309 * CPU iterators
310 *
311 * An extra gcwq is defined for an invalid cpu number
312 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
313 * specific CPU. The following iterators are similar to
314 * for_each_*_cpu() iterators but also considers the unbound gcwq.
315 *
316 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
317 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
318 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
319 * WORK_CPU_UNBOUND for unbound workqueues
320 */
Tejun Heof3421792010-07-02 10:03:51 +0200321#define for_each_gcwq_cpu(cpu) \
322 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
323 (cpu) < WORK_CPU_NONE; \
324 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
325
326#define for_each_online_gcwq_cpu(cpu) \
327 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
328 (cpu) < WORK_CPU_NONE; \
329 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
330
331#define for_each_cwq_cpu(cpu, wq) \
332 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
333 (cpu) < WORK_CPU_NONE; \
334 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
335
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900336#ifdef CONFIG_DEBUG_OBJECTS_WORK
337
338static struct debug_obj_descr work_debug_descr;
339
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100340static void *work_debug_hint(void *addr)
341{
342 return ((struct work_struct *) addr)->func;
343}
344
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900345/*
346 * fixup_init is called when:
347 * - an active object is initialized
348 */
349static int work_fixup_init(void *addr, enum debug_obj_state state)
350{
351 struct work_struct *work = addr;
352
353 switch (state) {
354 case ODEBUG_STATE_ACTIVE:
355 cancel_work_sync(work);
356 debug_object_init(work, &work_debug_descr);
357 return 1;
358 default:
359 return 0;
360 }
361}
362
363/*
364 * fixup_activate is called when:
365 * - an active object is activated
366 * - an unknown object is activated (might be a statically initialized object)
367 */
368static int work_fixup_activate(void *addr, enum debug_obj_state state)
369{
370 struct work_struct *work = addr;
371
372 switch (state) {
373
374 case ODEBUG_STATE_NOTAVAILABLE:
375 /*
376 * This is not really a fixup. The work struct was
377 * statically initialized. We just make sure that it
378 * is tracked in the object tracker.
379 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200380 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900381 debug_object_init(work, &work_debug_descr);
382 debug_object_activate(work, &work_debug_descr);
383 return 0;
384 }
385 WARN_ON_ONCE(1);
386 return 0;
387
388 case ODEBUG_STATE_ACTIVE:
389 WARN_ON(1);
390
391 default:
392 return 0;
393 }
394}
395
396/*
397 * fixup_free is called when:
398 * - an active object is freed
399 */
400static int work_fixup_free(void *addr, enum debug_obj_state state)
401{
402 struct work_struct *work = addr;
403
404 switch (state) {
405 case ODEBUG_STATE_ACTIVE:
406 cancel_work_sync(work);
407 debug_object_free(work, &work_debug_descr);
408 return 1;
409 default:
410 return 0;
411 }
412}
413
414static struct debug_obj_descr work_debug_descr = {
415 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100416 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900417 .fixup_init = work_fixup_init,
418 .fixup_activate = work_fixup_activate,
419 .fixup_free = work_fixup_free,
420};
421
422static inline void debug_work_activate(struct work_struct *work)
423{
424 debug_object_activate(work, &work_debug_descr);
425}
426
427static inline void debug_work_deactivate(struct work_struct *work)
428{
429 debug_object_deactivate(work, &work_debug_descr);
430}
431
432void __init_work(struct work_struct *work, int onstack)
433{
434 if (onstack)
435 debug_object_init_on_stack(work, &work_debug_descr);
436 else
437 debug_object_init(work, &work_debug_descr);
438}
439EXPORT_SYMBOL_GPL(__init_work);
440
441void destroy_work_on_stack(struct work_struct *work)
442{
443 debug_object_free(work, &work_debug_descr);
444}
445EXPORT_SYMBOL_GPL(destroy_work_on_stack);
446
447#else
448static inline void debug_work_activate(struct work_struct *work) { }
449static inline void debug_work_deactivate(struct work_struct *work) { }
450#endif
451
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100452/* Serializes the accesses to the list of workqueues. */
453static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200455static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Oleg Nesterov14441962007-05-23 13:57:57 -0700457/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200458 * The almighty global cpu workqueues. nr_running is the only field
459 * which is expected to be used frequently by other cpus via
460 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700461 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200462static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo0290d202012-07-13 22:16:44 -0700463static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800464
Tejun Heof3421792010-07-02 10:03:51 +0200465/*
466 * Global cpu workqueue and nr_running counter for unbound gcwq. The
467 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
468 * workers have WORKER_UNBOUND set.
469 */
470static struct global_cwq unbound_global_cwq;
Tejun Heo0290d202012-07-13 22:16:44 -0700471static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
472 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
473};
Tejun Heof3421792010-07-02 10:03:51 +0200474
Tejun Heoc34056a2010-06-29 10:07:11 +0200475static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Tejun Heo2bfedd32012-07-13 22:16:45 -0700477static int worker_pool_pri(struct worker_pool *pool)
478{
479 return pool - pool->gcwq->pools;
480}
481
Tejun Heo8b03ae32010-06-29 10:07:12 +0200482static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Tejun Heof3421792010-07-02 10:03:51 +0200484 if (cpu != WORK_CPU_UNBOUND)
485 return &per_cpu(global_cwq, cpu);
486 else
487 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Tejun Heob0f31fe2012-07-12 14:46:37 -0700490static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700491{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700492 int cpu = pool->gcwq->cpu;
Tejun Heo2bfedd32012-07-13 22:16:45 -0700493 int idx = worker_pool_pri(pool);
Tejun Heob0f31fe2012-07-12 14:46:37 -0700494
Tejun Heof3421792010-07-02 10:03:51 +0200495 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo0290d202012-07-13 22:16:44 -0700496 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200497 else
Tejun Heo0290d202012-07-13 22:16:44 -0700498 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700499}
500
Tejun Heo4690c4a2010-06-29 10:07:10 +0200501static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
502 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700503{
Tejun Heof3421792010-07-02 10:03:51 +0200504 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800505 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200506 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200507 } else if (likely(cpu == WORK_CPU_UNBOUND))
508 return wq->cpu_wq.single;
509 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700510}
511
Tejun Heo73f53c42010-06-29 10:07:11 +0200512static unsigned int work_color_to_flags(int color)
513{
514 return color << WORK_STRUCT_COLOR_SHIFT;
515}
516
517static int get_work_color(struct work_struct *work)
518{
519 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
520 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
521}
522
523static int work_next_color(int color)
524{
525 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
David Howells4594bf12006-12-07 11:33:26 +0000528/*
Tejun Heoe1201532010-07-22 14:14:25 +0200529 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
530 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
531 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532 *
533 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
534 * cwq, cpu or clear work->data. These functions should only be
535 * called while the work is owned - ie. while the PENDING bit is set.
536 *
537 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
538 * corresponding to a work. gcwq is available once the work has been
539 * queued anywhere after initialization. cwq is available only from
540 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000541 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542static inline void set_work_data(struct work_struct *work, unsigned long data,
543 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000544{
David Howells4594bf12006-12-07 11:33:26 +0000545 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200546 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000547}
David Howells365970a2006-11-22 14:54:49 +0000548
Tejun Heo7a22ad72010-06-29 10:07:13 +0200549static void set_work_cwq(struct work_struct *work,
550 struct cpu_workqueue_struct *cwq,
551 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200552{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200554 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200555}
556
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000558{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
560}
561
562static void clear_work_data(struct work_struct *work)
563{
564 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
565}
566
Tejun Heo7a22ad72010-06-29 10:07:13 +0200567static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
568{
Tejun Heoe1201532010-07-22 14:14:25 +0200569 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200570
Tejun Heoe1201532010-07-22 14:14:25 +0200571 if (data & WORK_STRUCT_CWQ)
572 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
573 else
574 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200575}
576
577static struct global_cwq *get_work_gcwq(struct work_struct *work)
578{
Tejun Heoe1201532010-07-22 14:14:25 +0200579 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580 unsigned int cpu;
581
Tejun Heoe1201532010-07-22 14:14:25 +0200582 if (data & WORK_STRUCT_CWQ)
583 return ((struct cpu_workqueue_struct *)
Tejun Heo47303942012-07-12 14:46:37 -0700584 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585
586 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200587 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200588 return NULL;
589
Tejun Heof3421792010-07-02 10:03:51 +0200590 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000592}
593
594/*
Tejun Heo2bfedd32012-07-13 22:16:45 -0700595 * Policy functions. These define the policies on how the global worker
596 * pools are managed. Unless noted otherwise, these functions assume that
597 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000598 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200599
Tejun Heob0f31fe2012-07-12 14:46:37 -0700600static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000601{
Tejun Heo2bfedd32012-07-13 22:16:45 -0700602 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000603}
604
Tejun Heoe22bee72010-06-29 10:07:14 +0200605/*
606 * Need to wake up a worker? Called from anything but currently
607 * running workers.
Tejun Heo0d8a2ff2012-07-12 14:46:37 -0700608 *
609 * Note that, because unbound workers never contribute to nr_running, this
610 * function will always return %true for unbound gcwq as long as the
611 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200612 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700613static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000614{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700615 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000616}
617
Tejun Heoe22bee72010-06-29 10:07:14 +0200618/* Can I start working? Called from busy but !running workers. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700619static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200620{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700621 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200622}
623
624/* Do I need to keep working? Called from currently running workers. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700625static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200626{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700627 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200628
Tejun Heo2bfedd32012-07-13 22:16:45 -0700629 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200630}
631
632/* Do we need a new worker? Called from manager. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700633static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200634{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700635 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200636}
637
638/* Do I need to be the manager? */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700639static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200640{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700641 return need_to_create_worker(pool) ||
Tejun Heo1f19ade2012-07-12 14:46:37 -0700642 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200643}
644
645/* Do we have too many workers and should some go away? */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700646static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200647{
Tejun Heo1f19ade2012-07-12 14:46:37 -0700648 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heob0f31fe2012-07-12 14:46:37 -0700649 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
650 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200651
652 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
653}
654
655/*
656 * Wake up functions.
657 */
658
Tejun Heo7e116292010-06-29 10:07:13 +0200659/* Return the first worker. Safe with preemption disabled */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700660static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200661{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700662 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200663 return NULL;
664
Tejun Heob0f31fe2012-07-12 14:46:37 -0700665 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200666}
667
668/**
669 * wake_up_worker - wake up an idle worker
Tejun Heob0f31fe2012-07-12 14:46:37 -0700670 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200671 *
Tejun Heob0f31fe2012-07-12 14:46:37 -0700672 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200673 *
674 * CONTEXT:
675 * spin_lock_irq(gcwq->lock).
676 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700677static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200678{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700679 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200680
681 if (likely(worker))
682 wake_up_process(worker->task);
683}
684
Tejun Heo4690c4a2010-06-29 10:07:10 +0200685/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200686 * wq_worker_waking_up - a worker is waking up
687 * @task: task waking up
688 * @cpu: CPU @task is waking up to
689 *
690 * This function is called during try_to_wake_up() when a worker is
691 * being awoken.
692 *
693 * CONTEXT:
694 * spin_lock_irq(rq->lock)
695 */
696void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
697{
698 struct worker *worker = kthread_data(task);
699
Steven Rostedt2d646722010-12-03 23:12:33 -0500700 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700701 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200702}
703
704/**
705 * wq_worker_sleeping - a worker is going to sleep
706 * @task: task going to sleep
707 * @cpu: CPU in question, must be the current CPU number
708 *
709 * This function is called during schedule() when a busy worker is
710 * going to sleep. Worker on the same cpu can be woken up by
711 * returning pointer to its task.
712 *
713 * CONTEXT:
714 * spin_lock_irq(rq->lock)
715 *
716 * RETURNS:
717 * Worker task on @cpu to wake up, %NULL if none.
718 */
719struct task_struct *wq_worker_sleeping(struct task_struct *task,
720 unsigned int cpu)
721{
722 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo47303942012-07-12 14:46:37 -0700723 struct worker_pool *pool = worker->pool;
Tejun Heob0f31fe2012-07-12 14:46:37 -0700724 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200725
Steven Rostedt2d646722010-12-03 23:12:33 -0500726 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200727 return NULL;
728
729 /* this can only happen on the local cpu */
730 BUG_ON(cpu != raw_smp_processor_id());
731
732 /*
733 * The counterpart of the following dec_and_test, implied mb,
734 * worklist not empty test sequence is in insert_work().
735 * Please read comment there.
736 *
737 * NOT_RUNNING is clear. This means that trustee is not in
738 * charge and we're running on the local cpu w/ rq lock held
739 * and preemption disabled, which in turn means that none else
740 * could be manipulating idle_list, so dereferencing idle_list
741 * without gcwq lock is safe.
742 */
Tejun Heo47303942012-07-12 14:46:37 -0700743 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700744 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200745 return to_wakeup ? to_wakeup->task : NULL;
746}
747
748/**
749 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200750 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200751 * @flags: flags to set
752 * @wakeup: wakeup an idle worker if necessary
753 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200754 * Set @flags in @worker->flags and adjust nr_running accordingly. If
755 * nr_running becomes zero and @wakeup is %true, an idle worker is
756 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200757 *
Tejun Heocb444762010-07-02 10:03:50 +0200758 * CONTEXT:
759 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200760 */
761static inline void worker_set_flags(struct worker *worker, unsigned int flags,
762 bool wakeup)
763{
Tejun Heo47303942012-07-12 14:46:37 -0700764 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200765
Tejun Heocb444762010-07-02 10:03:50 +0200766 WARN_ON_ONCE(worker->task != current);
767
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 /*
769 * If transitioning into NOT_RUNNING, adjust nr_running and
770 * wake up an idle worker as necessary if requested by
771 * @wakeup.
772 */
773 if ((flags & WORKER_NOT_RUNNING) &&
774 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heob0f31fe2012-07-12 14:46:37 -0700775 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200776
777 if (wakeup) {
778 if (atomic_dec_and_test(nr_running) &&
Tejun Heo47303942012-07-12 14:46:37 -0700779 !list_empty(&pool->worklist))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700780 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200781 } else
782 atomic_dec(nr_running);
783 }
784
Tejun Heod302f012010-06-29 10:07:13 +0200785 worker->flags |= flags;
786}
787
788/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200789 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200790 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200791 * @flags: flags to clear
792 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200793 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200794 *
Tejun Heocb444762010-07-02 10:03:50 +0200795 * CONTEXT:
796 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200797 */
798static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
799{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700800 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200801 unsigned int oflags = worker->flags;
802
Tejun Heocb444762010-07-02 10:03:50 +0200803 WARN_ON_ONCE(worker->task != current);
804
Tejun Heod302f012010-06-29 10:07:13 +0200805 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200806
Tejun Heo42c025f2011-01-11 15:58:49 +0100807 /*
808 * If transitioning out of NOT_RUNNING, increment nr_running. Note
809 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
810 * of multiple flags, not a single flag.
811 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200812 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
813 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700814 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200815}
816
817/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200818 * busy_worker_head - return the busy hash head for a work
819 * @gcwq: gcwq of interest
820 * @work: work to be hashed
821 *
822 * Return hash head of @gcwq for @work.
823 *
824 * CONTEXT:
825 * spin_lock_irq(gcwq->lock).
826 *
827 * RETURNS:
828 * Pointer to the hash head.
829 */
830static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
831 struct work_struct *work)
832{
833 const int base_shift = ilog2(sizeof(struct work_struct));
834 unsigned long v = (unsigned long)work;
835
836 /* simple shift and fold hash, do we need something better? */
837 v >>= base_shift;
838 v += v >> BUSY_WORKER_HASH_ORDER;
839 v &= BUSY_WORKER_HASH_MASK;
840
841 return &gcwq->busy_hash[v];
842}
843
844/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200845 * __find_worker_executing_work - find worker which is executing a work
846 * @gcwq: gcwq of interest
847 * @bwh: hash head as returned by busy_worker_head()
848 * @work: work to find worker for
849 *
850 * Find a worker which is executing @work on @gcwq. @bwh should be
851 * the hash head obtained by calling busy_worker_head() with the same
852 * work.
853 *
854 * CONTEXT:
855 * spin_lock_irq(gcwq->lock).
856 *
857 * RETURNS:
858 * Pointer to worker which is executing @work if found, NULL
859 * otherwise.
860 */
861static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
862 struct hlist_head *bwh,
863 struct work_struct *work)
864{
865 struct worker *worker;
866 struct hlist_node *tmp;
867
868 hlist_for_each_entry(worker, tmp, bwh, hentry)
869 if (worker->current_work == work)
870 return worker;
871 return NULL;
872}
873
874/**
875 * find_worker_executing_work - find worker which is executing a work
876 * @gcwq: gcwq of interest
877 * @work: work to find worker for
878 *
879 * Find a worker which is executing @work on @gcwq. This function is
880 * identical to __find_worker_executing_work() except that this
881 * function calculates @bwh itself.
882 *
883 * CONTEXT:
884 * spin_lock_irq(gcwq->lock).
885 *
886 * RETURNS:
887 * Pointer to worker which is executing @work if found, NULL
888 * otherwise.
889 */
890static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
891 struct work_struct *work)
892{
893 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
894 work);
895}
896
897/**
Tejun Heo7e116292010-06-29 10:07:13 +0200898 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200899 * @cwq: cwq @work belongs to
900 * @work: work to insert
901 * @head: insertion point
902 * @extra_flags: extra WORK_STRUCT_* flags to set
903 *
Tejun Heo7e116292010-06-29 10:07:13 +0200904 * Insert @work which belongs to @cwq into @gcwq after @head.
905 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200906 *
907 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200908 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200909 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700910static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200911 struct work_struct *work, struct list_head *head,
912 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700913{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700914 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100915
Tejun Heo4690c4a2010-06-29 10:07:10 +0200916 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200917 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200918
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700919 /*
920 * Ensure that we get the right work->data if we see the
921 * result of list_add() below, see try_to_grab_pending().
922 */
923 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200924
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700925 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200926
927 /*
928 * Ensure either worker_sched_deactivated() sees the above
929 * list_add_tail() or we see zero nr_running to avoid workers
930 * lying around lazily while there are works to be processed.
931 */
932 smp_mb();
933
Tejun Heob0f31fe2012-07-12 14:46:37 -0700934 if (__need_more_worker(pool))
935 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700936}
937
Tejun Heoc8efcc22010-12-20 19:32:04 +0100938/*
939 * Test whether @work is being queued from another work executing on the
940 * same workqueue. This is rather expensive and should only be used from
941 * cold paths.
942 */
943static bool is_chained_work(struct workqueue_struct *wq)
944{
945 unsigned long flags;
946 unsigned int cpu;
947
948 for_each_gcwq_cpu(cpu) {
949 struct global_cwq *gcwq = get_gcwq(cpu);
950 struct worker *worker;
951 struct hlist_node *pos;
952 int i;
953
954 spin_lock_irqsave(&gcwq->lock, flags);
955 for_each_busy_worker(worker, i, pos, gcwq) {
956 if (worker->task != current)
957 continue;
958 spin_unlock_irqrestore(&gcwq->lock, flags);
959 /*
960 * I'm @worker, no locking necessary. See if @work
961 * is headed to the same workqueue.
962 */
963 return worker->current_cwq->wq == wq;
964 }
965 spin_unlock_irqrestore(&gcwq->lock, flags);
966 }
967 return false;
968}
969
Tejun Heo4690c4a2010-06-29 10:07:10 +0200970static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct work_struct *work)
972{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200973 struct global_cwq *gcwq;
974 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200975 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200976 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned long flags;
978
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900979 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200980
Tejun Heoc8efcc22010-12-20 19:32:04 +0100981 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200982 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +0100983 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200984 return;
985
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200986 /* determine gcwq to use */
987 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200988 struct global_cwq *last_gcwq;
989
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200990 if (unlikely(cpu == WORK_CPU_UNBOUND))
991 cpu = raw_smp_processor_id();
992
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200993 /*
994 * It's multi cpu. If @wq is non-reentrant and @work
995 * was previously on a different cpu, it might still
996 * be running there, in which case the work needs to
997 * be queued on that cpu to guarantee non-reentrance.
998 */
Tejun Heo502ca9d2010-06-29 10:07:13 +0200999 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001000 if (wq->flags & WQ_NON_REENTRANT &&
1001 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1002 struct worker *worker;
1003
1004 spin_lock_irqsave(&last_gcwq->lock, flags);
1005
1006 worker = find_worker_executing_work(last_gcwq, work);
1007
1008 if (worker && worker->current_cwq->wq == wq)
1009 gcwq = last_gcwq;
1010 else {
1011 /* meh... not running there, queue here */
1012 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1013 spin_lock_irqsave(&gcwq->lock, flags);
1014 }
1015 } else
1016 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001017 } else {
1018 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1019 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001020 }
1021
1022 /* gcwq determined, get cwq and queue */
1023 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001024 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001025
Tejun Heo4690c4a2010-06-29 10:07:10 +02001026 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001027
Tejun Heo73f53c42010-06-29 10:07:11 +02001028 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001029 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001030
1031 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001032 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001033 cwq->nr_active++;
Tejun Heo2bfedd32012-07-13 22:16:45 -07001034 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001035 } else {
1036 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001037 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001038 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001039
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001040 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001041
Tejun Heo8b03ae32010-06-29 10:07:12 +02001042 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001045/**
1046 * queue_work - queue work on a workqueue
1047 * @wq: workqueue to use
1048 * @work: work to queue
1049 *
Alan Stern057647f2006-10-28 10:38:58 -07001050 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001052 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1053 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001055int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001057 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001059 ret = queue_work_on(get_cpu(), wq, work);
1060 put_cpu();
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return ret;
1063}
Dave Jonesae90dd52006-06-30 01:40:45 -04001064EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Zhang Ruic1a220e2008-07-23 21:28:39 -07001066/**
1067 * queue_work_on - queue work on specific cpu
1068 * @cpu: CPU number to execute work on
1069 * @wq: workqueue to use
1070 * @work: work to queue
1071 *
1072 * Returns 0 if @work was already on a queue, non-zero otherwise.
1073 *
1074 * We queue the work to a specific CPU, the caller must ensure it
1075 * can't go away.
1076 */
1077int
1078queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1079{
1080 int ret = 0;
1081
Tejun Heo22df02b2010-06-29 10:07:10 +02001082 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001083 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001084 ret = 1;
1085 }
1086 return ret;
1087}
1088EXPORT_SYMBOL_GPL(queue_work_on);
1089
Li Zefan6d141c32008-02-08 04:21:09 -08001090static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
David Howells52bad642006-11-22 14:54:01 +00001092 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001093 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Tejun Heo4690c4a2010-06-29 10:07:10 +02001095 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001098/**
1099 * queue_delayed_work - queue work on a workqueue after delay
1100 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001101 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001102 * @delay: number of jiffies to wait before queueing
1103 *
Alan Stern057647f2006-10-28 10:38:58 -07001104 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001105 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001106int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001107 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
David Howells52bad642006-11-22 14:54:01 +00001109 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001110 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001112 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
Dave Jonesae90dd52006-06-30 01:40:45 -04001114EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001116/**
1117 * queue_delayed_work_on - queue work on specific CPU after delay
1118 * @cpu: CPU number to execute work on
1119 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001120 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001121 * @delay: number of jiffies to wait before queueing
1122 *
Alan Stern057647f2006-10-28 10:38:58 -07001123 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001124 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001125int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001126 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001127{
1128 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001129 struct timer_list *timer = &dwork->timer;
1130 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001131
Tejun Heo22df02b2010-06-29 10:07:10 +02001132 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001133 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001134
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001135 BUG_ON(timer_pending(timer));
1136 BUG_ON(!list_empty(&work->entry));
1137
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001138 timer_stats_timer_set_start_info(&dwork->timer);
1139
Tejun Heo7a22ad72010-06-29 10:07:13 +02001140 /*
1141 * This stores cwq for the moment, for the timer_fn.
1142 * Note that the work's gcwq is preserved to allow
1143 * reentrance detection for delayed works.
1144 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001145 if (!(wq->flags & WQ_UNBOUND)) {
1146 struct global_cwq *gcwq = get_work_gcwq(work);
1147
1148 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1149 lcpu = gcwq->cpu;
1150 else
1151 lcpu = raw_smp_processor_id();
1152 } else
1153 lcpu = WORK_CPU_UNBOUND;
1154
Tejun Heo7a22ad72010-06-29 10:07:13 +02001155 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001156
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001157 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001158 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001159 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001160
1161 if (unlikely(cpu >= 0))
1162 add_timer_on(timer, cpu);
1163 else
1164 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001165 ret = 1;
1166 }
1167 return ret;
1168}
Dave Jonesae90dd52006-06-30 01:40:45 -04001169EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Tejun Heoc8e55f32010-06-29 10:07:12 +02001171/**
1172 * worker_enter_idle - enter idle state
1173 * @worker: worker which is entering idle state
1174 *
1175 * @worker is entering idle state. Update stats and idle timer if
1176 * necessary.
1177 *
1178 * LOCKING:
1179 * spin_lock_irq(gcwq->lock).
1180 */
1181static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Tejun Heo47303942012-07-12 14:46:37 -07001183 struct worker_pool *pool = worker->pool;
1184 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Tejun Heoc8e55f32010-06-29 10:07:12 +02001186 BUG_ON(worker->flags & WORKER_IDLE);
1187 BUG_ON(!list_empty(&worker->entry) &&
1188 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Tejun Heocb444762010-07-02 10:03:50 +02001190 /* can't use worker_set_flags(), also called from start_worker() */
1191 worker->flags |= WORKER_IDLE;
Tejun Heo47303942012-07-12 14:46:37 -07001192 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001193 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001194
Tejun Heoc8e55f32010-06-29 10:07:12 +02001195 /* idle_list is LIFO */
Tejun Heo47303942012-07-12 14:46:37 -07001196 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001197
Tejun Heoe22bee72010-06-29 10:07:14 +02001198 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001199 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo47303942012-07-12 14:46:37 -07001200 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001201 jiffies + IDLE_WORKER_TIMEOUT);
1202 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001203 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001204
Tejun Heo776a17372012-05-14 15:04:50 -07001205 /*
1206 * Sanity check nr_running. Because trustee releases gcwq->lock
1207 * between setting %WORKER_ROGUE and zapping nr_running, the
1208 * warning may trigger spuriously. Check iff trustee is idle.
1209 */
1210 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo47303942012-07-12 14:46:37 -07001211 pool->nr_workers == pool->nr_idle &&
Tejun Heob0f31fe2012-07-12 14:46:37 -07001212 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Tejun Heoc8e55f32010-06-29 10:07:12 +02001215/**
1216 * worker_leave_idle - leave idle state
1217 * @worker: worker which is leaving idle state
1218 *
1219 * @worker is leaving idle state. Update stats.
1220 *
1221 * LOCKING:
1222 * spin_lock_irq(gcwq->lock).
1223 */
1224static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Tejun Heo47303942012-07-12 14:46:37 -07001226 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Tejun Heoc8e55f32010-06-29 10:07:12 +02001228 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001229 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo47303942012-07-12 14:46:37 -07001230 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001231 list_del_init(&worker->entry);
1232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Tejun Heoe22bee72010-06-29 10:07:14 +02001234/**
1235 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1236 * @worker: self
1237 *
1238 * Works which are scheduled while the cpu is online must at least be
1239 * scheduled to a worker which is bound to the cpu so that if they are
1240 * flushed from cpu callbacks while cpu is going down, they are
1241 * guaranteed to execute on the cpu.
1242 *
1243 * This function is to be used by rogue workers and rescuers to bind
1244 * themselves to the target cpu and may race with cpu going down or
1245 * coming online. kthread_bind() can't be used because it may put the
1246 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1247 * verbatim as it's best effort and blocking and gcwq may be
1248 * [dis]associated in the meantime.
1249 *
1250 * This function tries set_cpus_allowed() and locks gcwq and verifies
1251 * the binding against GCWQ_DISASSOCIATED which is set during
1252 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1253 * idle state or fetches works without dropping lock, it can guarantee
1254 * the scheduling requirement described in the first paragraph.
1255 *
1256 * CONTEXT:
1257 * Might sleep. Called without any lock but returns with gcwq->lock
1258 * held.
1259 *
1260 * RETURNS:
1261 * %true if the associated gcwq is online (@worker is successfully
1262 * bound), %false if offline.
1263 */
1264static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001265__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001266{
Tejun Heo47303942012-07-12 14:46:37 -07001267 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001268 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Tejun Heoe22bee72010-06-29 10:07:14 +02001270 while (true) {
1271 /*
1272 * The following call may fail, succeed or succeed
1273 * without actually migrating the task to the cpu if
1274 * it races with cpu hotunplug operation. Verify
1275 * against GCWQ_DISASSOCIATED.
1276 */
Tejun Heof3421792010-07-02 10:03:51 +02001277 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1278 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001279
Tejun Heoe22bee72010-06-29 10:07:14 +02001280 spin_lock_irq(&gcwq->lock);
1281 if (gcwq->flags & GCWQ_DISASSOCIATED)
1282 return false;
1283 if (task_cpu(task) == gcwq->cpu &&
1284 cpumask_equal(&current->cpus_allowed,
1285 get_cpu_mask(gcwq->cpu)))
1286 return true;
1287 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001288
Tejun Heo5035b202011-04-29 18:08:37 +02001289 /*
1290 * We've raced with CPU hot[un]plug. Give it a breather
1291 * and retry migration. cond_resched() is required here;
1292 * otherwise, we might deadlock against cpu_stop trying to
1293 * bring down the CPU on non-preemptive kernel.
1294 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001295 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001296 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001297 }
1298}
1299
1300/*
1301 * Function for worker->rebind_work used to rebind rogue busy workers
1302 * to the associated cpu which is coming back online. This is
1303 * scheduled by cpu up but can race with other cpu hotplug operations
1304 * and may be executed twice without intervening cpu down.
1305 */
1306static void worker_rebind_fn(struct work_struct *work)
1307{
1308 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo47303942012-07-12 14:46:37 -07001309 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001310
1311 if (worker_maybe_bind_and_lock(worker))
1312 worker_clr_flags(worker, WORKER_REBIND);
1313
1314 spin_unlock_irq(&gcwq->lock);
1315}
1316
Tejun Heoc34056a2010-06-29 10:07:11 +02001317static struct worker *alloc_worker(void)
1318{
1319 struct worker *worker;
1320
1321 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001322 if (worker) {
1323 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001324 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001325 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1326 /* on creation a worker is in !idle && prep state */
1327 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001328 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001329 return worker;
1330}
1331
1332/**
1333 * create_worker - create a new workqueue worker
Tejun Heob0f31fe2012-07-12 14:46:37 -07001334 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001335 * @bind: whether to set affinity to @cpu or not
1336 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001337 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001338 * can be started by calling start_worker() or destroyed using
1339 * destroy_worker().
1340 *
1341 * CONTEXT:
1342 * Might sleep. Does GFP_KERNEL allocations.
1343 *
1344 * RETURNS:
1345 * Pointer to the newly created worker.
1346 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001347static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001348{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001349 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001350 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heo2bfedd32012-07-13 22:16:45 -07001351 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001352 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001353 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001354
Tejun Heo8b03ae32010-06-29 10:07:12 +02001355 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001356 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001357 spin_unlock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001358 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001359 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001360 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001361 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001362 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001363
1364 worker = alloc_worker();
1365 if (!worker)
1366 goto fail;
1367
Tejun Heo47303942012-07-12 14:46:37 -07001368 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001369 worker->id = id;
1370
Tejun Heof3421792010-07-02 10:03:51 +02001371 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001372 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo2bfedd32012-07-13 22:16:45 -07001373 worker, cpu_to_node(gcwq->cpu),
1374 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001375 else
1376 worker->task = kthread_create(worker_thread, worker,
Tejun Heo2bfedd32012-07-13 22:16:45 -07001377 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001378 if (IS_ERR(worker->task))
1379 goto fail;
1380
Tejun Heo2bfedd32012-07-13 22:16:45 -07001381 if (worker_pool_pri(pool))
1382 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1383
Tejun Heodb7bccf2010-06-29 10:07:12 +02001384 /*
1385 * A rogue worker will become a regular one if CPU comes
1386 * online later on. Make sure every worker has
1387 * PF_THREAD_BOUND set.
1388 */
Tejun Heof3421792010-07-02 10:03:51 +02001389 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001390 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001391 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001392 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001393 if (on_unbound_cpu)
1394 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001396
Tejun Heoc34056a2010-06-29 10:07:11 +02001397 return worker;
1398fail:
1399 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001400 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001401 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001402 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001403 }
1404 kfree(worker);
1405 return NULL;
1406}
1407
1408/**
1409 * start_worker - start a newly created worker
1410 * @worker: worker to start
1411 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001412 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001413 *
1414 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001415 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001416 */
1417static void start_worker(struct worker *worker)
1418{
Tejun Heocb444762010-07-02 10:03:50 +02001419 worker->flags |= WORKER_STARTED;
Tejun Heo47303942012-07-12 14:46:37 -07001420 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001421 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001422 wake_up_process(worker->task);
1423}
1424
1425/**
1426 * destroy_worker - destroy a workqueue worker
1427 * @worker: worker to be destroyed
1428 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001429 * Destroy @worker and adjust @gcwq stats accordingly.
1430 *
1431 * CONTEXT:
1432 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001433 */
1434static void destroy_worker(struct worker *worker)
1435{
Tejun Heo47303942012-07-12 14:46:37 -07001436 struct worker_pool *pool = worker->pool;
1437 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001438 int id = worker->id;
1439
1440 /* sanity check frenzy */
1441 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001442 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001443
Tejun Heoc8e55f32010-06-29 10:07:12 +02001444 if (worker->flags & WORKER_STARTED)
Tejun Heo47303942012-07-12 14:46:37 -07001445 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001446 if (worker->flags & WORKER_IDLE)
Tejun Heo47303942012-07-12 14:46:37 -07001447 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001448
1449 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001450 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001451
1452 spin_unlock_irq(&gcwq->lock);
1453
Tejun Heoc34056a2010-06-29 10:07:11 +02001454 kthread_stop(worker->task);
1455 kfree(worker);
1456
Tejun Heo8b03ae32010-06-29 10:07:12 +02001457 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001458 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001459}
1460
Tejun Heob0f31fe2012-07-12 14:46:37 -07001461static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001462{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001463 struct worker_pool *pool = (void *)__pool;
1464 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001465
1466 spin_lock_irq(&gcwq->lock);
1467
Tejun Heob0f31fe2012-07-12 14:46:37 -07001468 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001469 struct worker *worker;
1470 unsigned long expires;
1471
1472 /* idle_list is kept in LIFO order, check the last one */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001473 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001474 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1475
1476 if (time_before(jiffies, expires))
Tejun Heob0f31fe2012-07-12 14:46:37 -07001477 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001478 else {
1479 /* it's been idle for too long, wake up manager */
Tejun Heo1f19ade2012-07-12 14:46:37 -07001480 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heob0f31fe2012-07-12 14:46:37 -07001481 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001482 }
1483 }
1484
1485 spin_unlock_irq(&gcwq->lock);
1486}
1487
1488static bool send_mayday(struct work_struct *work)
1489{
1490 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1491 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001492 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001493
1494 if (!(wq->flags & WQ_RESCUER))
1495 return false;
1496
1497 /* mayday mayday mayday */
Tejun Heo47303942012-07-12 14:46:37 -07001498 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001499 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1500 if (cpu == WORK_CPU_UNBOUND)
1501 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001502 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001503 wake_up_process(wq->rescuer->task);
1504 return true;
1505}
1506
Tejun Heob0f31fe2012-07-12 14:46:37 -07001507static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001508{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001509 struct worker_pool *pool = (void *)__pool;
1510 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001511 struct work_struct *work;
1512
1513 spin_lock_irq(&gcwq->lock);
1514
Tejun Heob0f31fe2012-07-12 14:46:37 -07001515 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001516 /*
1517 * We've been trying to create a new worker but
1518 * haven't been successful. We might be hitting an
1519 * allocation deadlock. Send distress signals to
1520 * rescuers.
1521 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001522 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001523 send_mayday(work);
1524 }
1525
1526 spin_unlock_irq(&gcwq->lock);
1527
Tejun Heob0f31fe2012-07-12 14:46:37 -07001528 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001529}
1530
1531/**
1532 * maybe_create_worker - create a new worker if necessary
Tejun Heob0f31fe2012-07-12 14:46:37 -07001533 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001534 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001535 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 * have at least one idle worker on return from this function. If
1537 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heob0f31fe2012-07-12 14:46:37 -07001538 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001539 * possible allocation deadlock.
1540 *
1541 * On return, need_to_create_worker() is guaranteed to be false and
1542 * may_start_working() true.
1543 *
1544 * LOCKING:
1545 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1546 * multiple times. Does GFP_KERNEL allocations. Called only from
1547 * manager.
1548 *
1549 * RETURNS:
1550 * false if no action was taken and gcwq->lock stayed locked, true
1551 * otherwise.
1552 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001553static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001554__releases(&gcwq->lock)
1555__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001556{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001557 struct global_cwq *gcwq = pool->gcwq;
1558
1559 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001560 return false;
1561restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001562 spin_unlock_irq(&gcwq->lock);
1563
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001565 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001566
1567 while (true) {
1568 struct worker *worker;
1569
Tejun Heob0f31fe2012-07-12 14:46:37 -07001570 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001571 if (worker) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001572 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 spin_lock_irq(&gcwq->lock);
1574 start_worker(worker);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001575 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 return true;
1577 }
1578
Tejun Heob0f31fe2012-07-12 14:46:37 -07001579 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001580 break;
1581
Tejun Heoe22bee72010-06-29 10:07:14 +02001582 __set_current_state(TASK_INTERRUPTIBLE);
1583 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001584
Tejun Heob0f31fe2012-07-12 14:46:37 -07001585 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001586 break;
1587 }
1588
Tejun Heob0f31fe2012-07-12 14:46:37 -07001589 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001590 spin_lock_irq(&gcwq->lock);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001591 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001592 goto restart;
1593 return true;
1594}
1595
1596/**
1597 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heob0f31fe2012-07-12 14:46:37 -07001598 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001600 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 * IDLE_WORKER_TIMEOUT.
1602 *
1603 * LOCKING:
1604 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1605 * multiple times. Called only from manager.
1606 *
1607 * RETURNS:
1608 * false if no action was taken and gcwq->lock stayed locked, true
1609 * otherwise.
1610 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001611static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001612{
1613 bool ret = false;
1614
Tejun Heob0f31fe2012-07-12 14:46:37 -07001615 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 struct worker *worker;
1617 unsigned long expires;
1618
Tejun Heob0f31fe2012-07-12 14:46:37 -07001619 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001620 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1621
1622 if (time_before(jiffies, expires)) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001623 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001624 break;
1625 }
1626
1627 destroy_worker(worker);
1628 ret = true;
1629 }
1630
1631 return ret;
1632}
1633
1634/**
1635 * manage_workers - manage worker pool
1636 * @worker: self
1637 *
1638 * Assume the manager role and manage gcwq worker pool @worker belongs
1639 * to. At any given time, there can be only zero or one manager per
1640 * gcwq. The exclusion is handled automatically by this function.
1641 *
1642 * The caller can safely start processing works on false return. On
1643 * true return, it's guaranteed that need_to_create_worker() is false
1644 * and may_start_working() is true.
1645 *
1646 * CONTEXT:
1647 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1648 * multiple times. Does GFP_KERNEL allocations.
1649 *
1650 * RETURNS:
1651 * false if no action was taken and gcwq->lock stayed locked, true if
1652 * some action was taken.
1653 */
1654static bool manage_workers(struct worker *worker)
1655{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001656 struct worker_pool *pool = worker->pool;
1657 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001658 bool ret = false;
1659
Tejun Heo1f19ade2012-07-12 14:46:37 -07001660 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001661 return ret;
1662
Tejun Heo1f19ade2012-07-12 14:46:37 -07001663 pool->flags &= ~POOL_MANAGE_WORKERS;
1664 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001665
1666 /*
1667 * Destroy and then create so that may_start_working() is true
1668 * on return.
1669 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001670 ret |= maybe_destroy_workers(pool);
1671 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001672
Tejun Heo1f19ade2012-07-12 14:46:37 -07001673 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001674
1675 /*
1676 * The trustee might be waiting to take over the manager
1677 * position, tell it we're done.
1678 */
1679 if (unlikely(gcwq->trustee))
1680 wake_up_all(&gcwq->trustee_wait);
1681
1682 return ret;
1683}
1684
Tejun Heoa62428c2010-06-29 10:07:10 +02001685/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001686 * move_linked_works - move linked works to a list
1687 * @work: start of series of works to be scheduled
1688 * @head: target list to append @work to
1689 * @nextp: out paramter for nested worklist walking
1690 *
1691 * Schedule linked works starting from @work to @head. Work series to
1692 * be scheduled starts at @work and includes any consecutive work with
1693 * WORK_STRUCT_LINKED set in its predecessor.
1694 *
1695 * If @nextp is not NULL, it's updated to point to the next work of
1696 * the last scheduled work. This allows move_linked_works() to be
1697 * nested inside outer list_for_each_entry_safe().
1698 *
1699 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001700 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001701 */
1702static void move_linked_works(struct work_struct *work, struct list_head *head,
1703 struct work_struct **nextp)
1704{
1705 struct work_struct *n;
1706
1707 /*
1708 * Linked worklist will always end before the end of the list,
1709 * use NULL for list head.
1710 */
1711 list_for_each_entry_safe_from(work, n, NULL, entry) {
1712 list_move_tail(&work->entry, head);
1713 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1714 break;
1715 }
1716
1717 /*
1718 * If we're already inside safe list traversal and have moved
1719 * multiple works to the scheduled queue, the next position
1720 * needs to be updated.
1721 */
1722 if (nextp)
1723 *nextp = n;
1724}
1725
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001726static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1727{
1728 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1729 struct work_struct, entry);
1730
Tejun Heocdadf002010-10-05 10:49:55 +02001731 trace_workqueue_activate_work(work);
Tejun Heo2bfedd32012-07-13 22:16:45 -07001732 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001733 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001734 cwq->nr_active++;
1735}
1736
Tejun Heoaffee4b2010-06-29 10:07:12 +02001737/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001738 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1739 * @cwq: cwq of interest
1740 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001741 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001742 *
1743 * A work either has completed or is removed from pending queue,
1744 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1745 *
1746 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001747 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001748 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001749static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1750 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001751{
1752 /* ignore uncolored works */
1753 if (color == WORK_NO_COLOR)
1754 return;
1755
1756 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001757
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001758 if (!delayed) {
1759 cwq->nr_active--;
1760 if (!list_empty(&cwq->delayed_works)) {
1761 /* one down, submit a delayed one */
1762 if (cwq->nr_active < cwq->max_active)
1763 cwq_activate_first_delayed(cwq);
1764 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001765 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001766
1767 /* is flush in progress and are we at the flushing tip? */
1768 if (likely(cwq->flush_color != color))
1769 return;
1770
1771 /* are there still in-flight works? */
1772 if (cwq->nr_in_flight[color])
1773 return;
1774
1775 /* this cwq is done, clear flush_color */
1776 cwq->flush_color = -1;
1777
1778 /*
1779 * If this was the last cwq, wake up the first flusher. It
1780 * will handle the rest.
1781 */
1782 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1783 complete(&cwq->wq->first_flusher->done);
1784}
1785
1786/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001787 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001788 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001789 * @work: work to process
1790 *
1791 * Process @work. This function contains all the logics necessary to
1792 * process a single work including synchronization against and
1793 * interaction with other workers on the same cpu, queueing and
1794 * flushing. As long as context requirement is met, any worker can
1795 * call this function to process a work.
1796 *
1797 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001798 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001799 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001800static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001801__releases(&gcwq->lock)
1802__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001803{
Tejun Heo7e116292010-06-29 10:07:13 +02001804 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo47303942012-07-12 14:46:37 -07001805 struct worker_pool *pool = worker->pool;
1806 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001807 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001808 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001809 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001810 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001811 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001812#ifdef CONFIG_LOCKDEP
1813 /*
1814 * It is permissible to free the struct work_struct from
1815 * inside the function that is called from it, this we need to
1816 * take into account for lockdep too. To avoid bogus "held
1817 * lock freed" warnings as well as problems when looking into
1818 * work->lockdep_map, make a copy and use that here.
1819 */
1820 struct lockdep_map lockdep_map = work->lockdep_map;
1821#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001822 /*
1823 * A single work shouldn't be executed concurrently by
1824 * multiple workers on a single cpu. Check whether anyone is
1825 * already processing the work. If so, defer the work to the
1826 * currently executing one.
1827 */
1828 collision = __find_worker_executing_work(gcwq, bwh, work);
1829 if (unlikely(collision)) {
1830 move_linked_works(work, &collision->scheduled, NULL);
1831 return;
1832 }
1833
Tejun Heoa62428c2010-06-29 10:07:10 +02001834 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001835 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001836 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001837 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001838 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001839 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001840
Tejun Heo7a22ad72010-06-29 10:07:13 +02001841 /* record the current cpu number in the work data and dequeue */
1842 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001843 list_del_init(&work->entry);
1844
Tejun Heo649027d2010-06-29 10:07:14 +02001845 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001846 * CPU intensive works don't participate in concurrency
1847 * management. They're the scheduler's responsibility.
1848 */
1849 if (unlikely(cpu_intensive))
1850 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1851
Tejun Heo0d8a2ff2012-07-12 14:46:37 -07001852 /*
1853 * Unbound gcwq isn't concurrency managed and work items should be
1854 * executed ASAP. Wake up another worker if necessary.
1855 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001856 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1857 wake_up_worker(pool);
Tejun Heo0d8a2ff2012-07-12 14:46:37 -07001858
Tejun Heo8b03ae32010-06-29 10:07:12 +02001859 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001860
Tejun Heoa62428c2010-06-29 10:07:10 +02001861 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001862 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001863 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001864 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001865 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001866 /*
1867 * While we must be careful to not use "work" after this, the trace
1868 * point will only record its address.
1869 */
1870 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001871 lock_map_release(&lockdep_map);
1872 lock_map_release(&cwq->wq->lockdep_map);
1873
1874 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1875 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1876 "%s/0x%08x/%d\n",
1877 current->comm, preempt_count(), task_pid_nr(current));
1878 printk(KERN_ERR " last function: ");
1879 print_symbol("%s\n", (unsigned long)f);
1880 debug_show_held_locks(current);
1881 dump_stack();
1882 }
1883
Tejun Heo8b03ae32010-06-29 10:07:12 +02001884 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001885
Tejun Heofb0e7be2010-06-29 10:07:15 +02001886 /* clear cpu intensive status */
1887 if (unlikely(cpu_intensive))
1888 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1889
Tejun Heoa62428c2010-06-29 10:07:10 +02001890 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001891 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001892 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001893 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001894 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001895}
1896
Tejun Heoaffee4b2010-06-29 10:07:12 +02001897/**
1898 * process_scheduled_works - process scheduled works
1899 * @worker: self
1900 *
1901 * Process all scheduled works. Please note that the scheduled list
1902 * may change while processing a work, so this function repeatedly
1903 * fetches a work from the top and executes it.
1904 *
1905 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001906 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001907 * multiple times.
1908 */
1909static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001911 while (!list_empty(&worker->scheduled)) {
1912 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001914 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Tejun Heo4690c4a2010-06-29 10:07:10 +02001918/**
1919 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001920 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001921 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 * The gcwq worker thread function. There's a single dynamic pool of
1923 * these per each cpu. These workers process all works regardless of
1924 * their specific target workqueue. The only exception is works which
1925 * belong to workqueues with a rescuer which will be explained in
1926 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001927 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001928static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Tejun Heoc34056a2010-06-29 10:07:11 +02001930 struct worker *worker = __worker;
Tejun Heo47303942012-07-12 14:46:37 -07001931 struct worker_pool *pool = worker->pool;
1932 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 /* tell the scheduler that this is a workqueue worker */
1935 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001936woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001937 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Tejun Heoc8e55f32010-06-29 10:07:12 +02001939 /* DIE can be set only while we're idle, checking here is enough */
1940 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001941 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Tejun Heoc8e55f32010-06-29 10:07:12 +02001946 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001947recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 /* no more worker necessary? */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001949 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001950 goto sleep;
1951
1952 /* do we need to manage? */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001953 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001954 goto recheck;
1955
Tejun Heoc8e55f32010-06-29 10:07:12 +02001956 /*
1957 * ->scheduled list can only be filled while a worker is
1958 * preparing to process a work or actually processing it.
1959 * Make sure nobody diddled with it while I was sleeping.
1960 */
1961 BUG_ON(!list_empty(&worker->scheduled));
1962
Tejun Heoe22bee72010-06-29 10:07:14 +02001963 /*
1964 * When control reaches this point, we're guaranteed to have
1965 * at least one idle worker or that someone else has already
1966 * assumed the manager role.
1967 */
1968 worker_clr_flags(worker, WORKER_PREP);
1969
1970 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001971 struct work_struct *work =
Tejun Heo47303942012-07-12 14:46:37 -07001972 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001973 struct work_struct, entry);
1974
1975 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1976 /* optimization path, not strictly necessary */
1977 process_one_work(worker, work);
1978 if (unlikely(!list_empty(&worker->scheduled)))
1979 process_scheduled_works(worker);
1980 } else {
1981 move_linked_works(work, &worker->scheduled, NULL);
1982 process_scheduled_works(worker);
1983 }
Tejun Heob0f31fe2012-07-12 14:46:37 -07001984 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001985
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001987sleep:
Tejun Heob0f31fe2012-07-12 14:46:37 -07001988 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001990
Tejun Heoc8e55f32010-06-29 10:07:12 +02001991 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 * gcwq->lock is held and there's no work to process and no
1993 * need to manage, sleep. Workers are woken up only while
1994 * holding gcwq->lock or from local cpu, so setting the
1995 * current state before releasing gcwq->lock is enough to
1996 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001997 */
1998 worker_enter_idle(worker);
1999 __set_current_state(TASK_INTERRUPTIBLE);
2000 spin_unlock_irq(&gcwq->lock);
2001 schedule();
2002 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Tejun Heoe22bee72010-06-29 10:07:14 +02002005/**
2006 * rescuer_thread - the rescuer thread function
2007 * @__wq: the associated workqueue
2008 *
2009 * Workqueue rescuer thread function. There's one rescuer for each
2010 * workqueue which has WQ_RESCUER set.
2011 *
2012 * Regular work processing on a gcwq may block trying to create a new
2013 * worker which uses GFP_KERNEL allocation which has slight chance of
2014 * developing into deadlock if some works currently on the same queue
2015 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2016 * the problem rescuer solves.
2017 *
2018 * When such condition is possible, the gcwq summons rescuers of all
2019 * workqueues which have works queued on the gcwq and let them process
2020 * those works so that forward progress can be guaranteed.
2021 *
2022 * This should happen rarely.
2023 */
2024static int rescuer_thread(void *__wq)
2025{
2026 struct workqueue_struct *wq = __wq;
2027 struct worker *rescuer = wq->rescuer;
2028 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002029 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 unsigned int cpu;
2031
2032 set_user_nice(current, RESCUER_NICE_LEVEL);
2033repeat:
2034 set_current_state(TASK_INTERRUPTIBLE);
2035
2036 if (kthread_should_stop())
2037 return 0;
2038
Tejun Heof3421792010-07-02 10:03:51 +02002039 /*
2040 * See whether any cpu is asking for help. Unbounded
2041 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2042 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002043 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002044 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2045 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo47303942012-07-12 14:46:37 -07002046 struct worker_pool *pool = cwq->pool;
2047 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002048 struct work_struct *work, *n;
2049
2050 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002051 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002052
2053 /* migrate to the target cpu if possible */
Tejun Heo47303942012-07-12 14:46:37 -07002054 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002055 worker_maybe_bind_and_lock(rescuer);
2056
2057 /*
2058 * Slurp in all works issued via this workqueue and
2059 * process'em.
2060 */
2061 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo47303942012-07-12 14:46:37 -07002062 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002063 if (get_work_cwq(work) == cwq)
2064 move_linked_works(work, scheduled, &n);
2065
2066 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002067
2068 /*
2069 * Leave this gcwq. If keep_working() is %true, notify a
2070 * regular worker; otherwise, we end up with 0 concurrency
2071 * and stalling the execution.
2072 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07002073 if (keep_working(pool))
2074 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002075
Tejun Heoe22bee72010-06-29 10:07:14 +02002076 spin_unlock_irq(&gcwq->lock);
2077 }
2078
2079 schedule();
2080 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002083struct wq_barrier {
2084 struct work_struct work;
2085 struct completion done;
2086};
2087
2088static void wq_barrier_func(struct work_struct *work)
2089{
2090 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2091 complete(&barr->done);
2092}
2093
Tejun Heo4690c4a2010-06-29 10:07:10 +02002094/**
2095 * insert_wq_barrier - insert a barrier work
2096 * @cwq: cwq to insert barrier into
2097 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002098 * @target: target work to attach @barr to
2099 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002100 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002101 * @barr is linked to @target such that @barr is completed only after
2102 * @target finishes execution. Please note that the ordering
2103 * guarantee is observed only with respect to @target and on the local
2104 * cpu.
2105 *
2106 * Currently, a queued barrier can't be canceled. This is because
2107 * try_to_grab_pending() can't determine whether the work to be
2108 * grabbed is at the head of the queue and thus can't clear LINKED
2109 * flag of the previous work while there must be a valid next work
2110 * after a work with LINKED flag set.
2111 *
2112 * Note that when @worker is non-NULL, @target may be modified
2113 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002114 *
2115 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002116 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002117 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002118static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002119 struct wq_barrier *barr,
2120 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002121{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002122 struct list_head *head;
2123 unsigned int linked = 0;
2124
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002125 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002126 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002127 * as we know for sure that this will not trigger any of the
2128 * checks and call back into the fixup functions where we
2129 * might deadlock.
2130 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002131 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002132 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002133 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002134
Tejun Heoaffee4b2010-06-29 10:07:12 +02002135 /*
2136 * If @target is currently being executed, schedule the
2137 * barrier to the worker; otherwise, put it after @target.
2138 */
2139 if (worker)
2140 head = worker->scheduled.next;
2141 else {
2142 unsigned long *bits = work_data_bits(target);
2143
2144 head = target->entry.next;
2145 /* there can already be other linked works, inherit and set */
2146 linked = *bits & WORK_STRUCT_LINKED;
2147 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2148 }
2149
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002150 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002151 insert_work(cwq, &barr->work, head,
2152 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002153}
2154
Tejun Heo73f53c42010-06-29 10:07:11 +02002155/**
2156 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2157 * @wq: workqueue being flushed
2158 * @flush_color: new flush color, < 0 for no-op
2159 * @work_color: new work color, < 0 for no-op
2160 *
2161 * Prepare cwqs for workqueue flushing.
2162 *
2163 * If @flush_color is non-negative, flush_color on all cwqs should be
2164 * -1. If no cwq has in-flight commands at the specified color, all
2165 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2166 * has in flight commands, its cwq->flush_color is set to
2167 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2168 * wakeup logic is armed and %true is returned.
2169 *
2170 * The caller should have initialized @wq->first_flusher prior to
2171 * calling this function with non-negative @flush_color. If
2172 * @flush_color is negative, no flush color update is done and %false
2173 * is returned.
2174 *
2175 * If @work_color is non-negative, all cwqs should have the same
2176 * work_color which is previous to @work_color and all will be
2177 * advanced to @work_color.
2178 *
2179 * CONTEXT:
2180 * mutex_lock(wq->flush_mutex).
2181 *
2182 * RETURNS:
2183 * %true if @flush_color >= 0 and there's something to flush. %false
2184 * otherwise.
2185 */
2186static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2187 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Tejun Heo73f53c42010-06-29 10:07:11 +02002189 bool wait = false;
2190 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002191
Tejun Heo73f53c42010-06-29 10:07:11 +02002192 if (flush_color >= 0) {
2193 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2194 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002195 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002196
Tejun Heof3421792010-07-02 10:03:51 +02002197 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002198 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo47303942012-07-12 14:46:37 -07002199 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002200
Tejun Heo8b03ae32010-06-29 10:07:12 +02002201 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002202
2203 if (flush_color >= 0) {
2204 BUG_ON(cwq->flush_color != -1);
2205
2206 if (cwq->nr_in_flight[flush_color]) {
2207 cwq->flush_color = flush_color;
2208 atomic_inc(&wq->nr_cwqs_to_flush);
2209 wait = true;
2210 }
2211 }
2212
2213 if (work_color >= 0) {
2214 BUG_ON(work_color != work_next_color(cwq->work_color));
2215 cwq->work_color = work_color;
2216 }
2217
Tejun Heo8b03ae32010-06-29 10:07:12 +02002218 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002219 }
2220
2221 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2222 complete(&wq->first_flusher->done);
2223
2224 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002227/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002229 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 *
2231 * Forces execution of the workqueue and blocks until its completion.
2232 * This is typically used in driver shutdown handlers.
2233 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002234 * We sleep until all works which were queued on entry have been handled,
2235 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002237void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Tejun Heo73f53c42010-06-29 10:07:11 +02002239 struct wq_flusher this_flusher = {
2240 .list = LIST_HEAD_INIT(this_flusher.list),
2241 .flush_color = -1,
2242 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2243 };
2244 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002245
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002246 lock_map_acquire(&wq->lockdep_map);
2247 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002248
2249 mutex_lock(&wq->flush_mutex);
2250
2251 /*
2252 * Start-to-wait phase
2253 */
2254 next_color = work_next_color(wq->work_color);
2255
2256 if (next_color != wq->flush_color) {
2257 /*
2258 * Color space is not full. The current work_color
2259 * becomes our flush_color and work_color is advanced
2260 * by one.
2261 */
2262 BUG_ON(!list_empty(&wq->flusher_overflow));
2263 this_flusher.flush_color = wq->work_color;
2264 wq->work_color = next_color;
2265
2266 if (!wq->first_flusher) {
2267 /* no flush in progress, become the first flusher */
2268 BUG_ON(wq->flush_color != this_flusher.flush_color);
2269
2270 wq->first_flusher = &this_flusher;
2271
2272 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2273 wq->work_color)) {
2274 /* nothing to flush, done */
2275 wq->flush_color = next_color;
2276 wq->first_flusher = NULL;
2277 goto out_unlock;
2278 }
2279 } else {
2280 /* wait in queue */
2281 BUG_ON(wq->flush_color == this_flusher.flush_color);
2282 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2283 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2284 }
2285 } else {
2286 /*
2287 * Oops, color space is full, wait on overflow queue.
2288 * The next flush completion will assign us
2289 * flush_color and transfer to flusher_queue.
2290 */
2291 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2292 }
2293
2294 mutex_unlock(&wq->flush_mutex);
2295
2296 wait_for_completion(&this_flusher.done);
2297
2298 /*
2299 * Wake-up-and-cascade phase
2300 *
2301 * First flushers are responsible for cascading flushes and
2302 * handling overflow. Non-first flushers can simply return.
2303 */
2304 if (wq->first_flusher != &this_flusher)
2305 return;
2306
2307 mutex_lock(&wq->flush_mutex);
2308
Tejun Heo4ce48b32010-07-02 10:03:51 +02002309 /* we might have raced, check again with mutex held */
2310 if (wq->first_flusher != &this_flusher)
2311 goto out_unlock;
2312
Tejun Heo73f53c42010-06-29 10:07:11 +02002313 wq->first_flusher = NULL;
2314
2315 BUG_ON(!list_empty(&this_flusher.list));
2316 BUG_ON(wq->flush_color != this_flusher.flush_color);
2317
2318 while (true) {
2319 struct wq_flusher *next, *tmp;
2320
2321 /* complete all the flushers sharing the current flush color */
2322 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2323 if (next->flush_color != wq->flush_color)
2324 break;
2325 list_del_init(&next->list);
2326 complete(&next->done);
2327 }
2328
2329 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2330 wq->flush_color != work_next_color(wq->work_color));
2331
2332 /* this flush_color is finished, advance by one */
2333 wq->flush_color = work_next_color(wq->flush_color);
2334
2335 /* one color has been freed, handle overflow queue */
2336 if (!list_empty(&wq->flusher_overflow)) {
2337 /*
2338 * Assign the same color to all overflowed
2339 * flushers, advance work_color and append to
2340 * flusher_queue. This is the start-to-wait
2341 * phase for these overflowed flushers.
2342 */
2343 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2344 tmp->flush_color = wq->work_color;
2345
2346 wq->work_color = work_next_color(wq->work_color);
2347
2348 list_splice_tail_init(&wq->flusher_overflow,
2349 &wq->flusher_queue);
2350 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2351 }
2352
2353 if (list_empty(&wq->flusher_queue)) {
2354 BUG_ON(wq->flush_color != wq->work_color);
2355 break;
2356 }
2357
2358 /*
2359 * Need to flush more colors. Make the next flusher
2360 * the new first flusher and arm cwqs.
2361 */
2362 BUG_ON(wq->flush_color == wq->work_color);
2363 BUG_ON(wq->flush_color != next->flush_color);
2364
2365 list_del_init(&next->list);
2366 wq->first_flusher = next;
2367
2368 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2369 break;
2370
2371 /*
2372 * Meh... this color is already done, clear first
2373 * flusher and repeat cascading.
2374 */
2375 wq->first_flusher = NULL;
2376 }
2377
2378out_unlock:
2379 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
Dave Jonesae90dd52006-06-30 01:40:45 -04002381EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002383/**
2384 * drain_workqueue - drain a workqueue
2385 * @wq: workqueue to drain
2386 *
2387 * Wait until the workqueue becomes empty. While draining is in progress,
2388 * only chain queueing is allowed. IOW, only currently pending or running
2389 * work items on @wq can queue further work items on it. @wq is flushed
2390 * repeatedly until it becomes empty. The number of flushing is detemined
2391 * by the depth of chaining and should be relatively short. Whine if it
2392 * takes too long.
2393 */
2394void drain_workqueue(struct workqueue_struct *wq)
2395{
2396 unsigned int flush_cnt = 0;
2397 unsigned int cpu;
2398
2399 /*
2400 * __queue_work() needs to test whether there are drainers, is much
2401 * hotter than drain_workqueue() and already looks at @wq->flags.
2402 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2403 */
2404 spin_lock(&workqueue_lock);
2405 if (!wq->nr_drainers++)
2406 wq->flags |= WQ_DRAINING;
2407 spin_unlock(&workqueue_lock);
2408reflush:
2409 flush_workqueue(wq);
2410
2411 for_each_cwq_cpu(cpu, wq) {
2412 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002413 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002414
Tejun Heo47303942012-07-12 14:46:37 -07002415 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002416 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo47303942012-07-12 14:46:37 -07002417 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002418
2419 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002420 continue;
2421
2422 if (++flush_cnt == 10 ||
2423 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2424 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2425 wq->name, flush_cnt);
2426 goto reflush;
2427 }
2428
2429 spin_lock(&workqueue_lock);
2430 if (!--wq->nr_drainers)
2431 wq->flags &= ~WQ_DRAINING;
2432 spin_unlock(&workqueue_lock);
2433}
2434EXPORT_SYMBOL_GPL(drain_workqueue);
2435
Tejun Heobaf59022010-09-16 10:42:16 +02002436static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2437 bool wait_executing)
2438{
2439 struct worker *worker = NULL;
2440 struct global_cwq *gcwq;
2441 struct cpu_workqueue_struct *cwq;
2442
2443 might_sleep();
2444 gcwq = get_work_gcwq(work);
2445 if (!gcwq)
2446 return false;
2447
2448 spin_lock_irq(&gcwq->lock);
2449 if (!list_empty(&work->entry)) {
2450 /*
2451 * See the comment near try_to_grab_pending()->smp_rmb().
2452 * If it was re-queued to a different gcwq under us, we
2453 * are not going to wait.
2454 */
2455 smp_rmb();
2456 cwq = get_work_cwq(work);
Tejun Heo47303942012-07-12 14:46:37 -07002457 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002458 goto already_gone;
2459 } else if (wait_executing) {
2460 worker = find_worker_executing_work(gcwq, work);
2461 if (!worker)
2462 goto already_gone;
2463 cwq = worker->current_cwq;
2464 } else
2465 goto already_gone;
2466
2467 insert_wq_barrier(cwq, barr, work, worker);
2468 spin_unlock_irq(&gcwq->lock);
2469
Tejun Heoe1594892011-01-09 23:32:15 +01002470 /*
2471 * If @max_active is 1 or rescuer is in use, flushing another work
2472 * item on the same workqueue may lead to deadlock. Make sure the
2473 * flusher is not running on the same workqueue by verifying write
2474 * access.
2475 */
2476 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2477 lock_map_acquire(&cwq->wq->lockdep_map);
2478 else
2479 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002480 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002481
Tejun Heobaf59022010-09-16 10:42:16 +02002482 return true;
2483already_gone:
2484 spin_unlock_irq(&gcwq->lock);
2485 return false;
2486}
2487
Oleg Nesterovdb700892008-07-25 01:47:49 -07002488/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002489 * flush_work - wait for a work to finish executing the last queueing instance
2490 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002491 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002492 * Wait until @work has finished execution. This function considers
2493 * only the last queueing instance of @work. If @work has been
2494 * enqueued across different CPUs on a non-reentrant workqueue or on
2495 * multiple workqueues, @work might still be executing on return on
2496 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002497 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002498 * If @work was queued only on a non-reentrant, ordered or unbound
2499 * workqueue, @work is guaranteed to be idle on return if it hasn't
2500 * been requeued since flush started.
2501 *
2502 * RETURNS:
2503 * %true if flush_work() waited for the work to finish execution,
2504 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002505 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002506bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002507{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002508 struct wq_barrier barr;
2509
Tejun Heobaf59022010-09-16 10:42:16 +02002510 if (start_flush_work(work, &barr, true)) {
2511 wait_for_completion(&barr.done);
2512 destroy_work_on_stack(&barr.work);
2513 return true;
2514 } else
2515 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002516}
2517EXPORT_SYMBOL_GPL(flush_work);
2518
Tejun Heo401a8d02010-09-16 10:36:00 +02002519static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2520{
2521 struct wq_barrier barr;
2522 struct worker *worker;
2523
2524 spin_lock_irq(&gcwq->lock);
2525
2526 worker = find_worker_executing_work(gcwq, work);
2527 if (unlikely(worker))
2528 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2529
2530 spin_unlock_irq(&gcwq->lock);
2531
2532 if (unlikely(worker)) {
2533 wait_for_completion(&barr.done);
2534 destroy_work_on_stack(&barr.work);
2535 return true;
2536 } else
2537 return false;
2538}
2539
2540static bool wait_on_work(struct work_struct *work)
2541{
2542 bool ret = false;
2543 int cpu;
2544
2545 might_sleep();
2546
2547 lock_map_acquire(&work->lockdep_map);
2548 lock_map_release(&work->lockdep_map);
2549
2550 for_each_gcwq_cpu(cpu)
2551 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2552 return ret;
2553}
2554
Tejun Heo09383492010-09-16 10:48:29 +02002555/**
2556 * flush_work_sync - wait until a work has finished execution
2557 * @work: the work to flush
2558 *
2559 * Wait until @work has finished execution. On return, it's
2560 * guaranteed that all queueing instances of @work which happened
2561 * before this function is called are finished. In other words, if
2562 * @work hasn't been requeued since this function was called, @work is
2563 * guaranteed to be idle on return.
2564 *
2565 * RETURNS:
2566 * %true if flush_work_sync() waited for the work to finish execution,
2567 * %false if it was already idle.
2568 */
2569bool flush_work_sync(struct work_struct *work)
2570{
2571 struct wq_barrier barr;
2572 bool pending, waited;
2573
2574 /* we'll wait for executions separately, queue barr only if pending */
2575 pending = start_flush_work(work, &barr, false);
2576
2577 /* wait for executions to finish */
2578 waited = wait_on_work(work);
2579
2580 /* wait for the pending one */
2581 if (pending) {
2582 wait_for_completion(&barr.done);
2583 destroy_work_on_stack(&barr.work);
2584 }
2585
2586 return pending || waited;
2587}
2588EXPORT_SYMBOL_GPL(flush_work_sync);
2589
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002590/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002591 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002592 * so this work can't be re-armed in any way.
2593 */
2594static int try_to_grab_pending(struct work_struct *work)
2595{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002596 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002597 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002598
Tejun Heo22df02b2010-06-29 10:07:10 +02002599 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002600 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002601
2602 /*
2603 * The queueing is in progress, or it is already queued. Try to
2604 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2605 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002606 gcwq = get_work_gcwq(work);
2607 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002608 return ret;
2609
Tejun Heo8b03ae32010-06-29 10:07:12 +02002610 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002611 if (!list_empty(&work->entry)) {
2612 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002613 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002614 * In that case we must see the new value after rmb(), see
2615 * insert_work()->wmb().
2616 */
2617 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002618 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002619 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002620 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002621 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002622 get_work_color(work),
2623 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002624 ret = 1;
2625 }
2626 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002627 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002628
2629 return ret;
2630}
2631
Tejun Heo401a8d02010-09-16 10:36:00 +02002632static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002633 struct timer_list* timer)
2634{
2635 int ret;
2636
2637 do {
2638 ret = (timer && likely(del_timer(timer)));
2639 if (!ret)
2640 ret = try_to_grab_pending(work);
2641 wait_on_work(work);
2642 } while (unlikely(ret < 0));
2643
Tejun Heo7a22ad72010-06-29 10:07:13 +02002644 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002645 return ret;
2646}
2647
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002648/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002649 * cancel_work_sync - cancel a work and wait for it to finish
2650 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002651 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002652 * Cancel @work and wait for its execution to finish. This function
2653 * can be used even if the work re-queues itself or migrates to
2654 * another workqueue. On return from this function, @work is
2655 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002656 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002657 * cancel_work_sync(&delayed_work->work) must not be used for
2658 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002659 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002660 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002661 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002662 *
2663 * RETURNS:
2664 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002665 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002666bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002668 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002669}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002670EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002671
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002672/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002673 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2674 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002676 * Delayed timer is cancelled and the pending work is queued for
2677 * immediate execution. Like flush_work(), this function only
2678 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002679 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002680 * RETURNS:
2681 * %true if flush_work() waited for the work to finish execution,
2682 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002683 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002684bool flush_delayed_work(struct delayed_work *dwork)
2685{
2686 if (del_timer_sync(&dwork->timer))
2687 __queue_work(raw_smp_processor_id(),
2688 get_work_cwq(&dwork->work)->wq, &dwork->work);
2689 return flush_work(&dwork->work);
2690}
2691EXPORT_SYMBOL(flush_delayed_work);
2692
2693/**
Tejun Heo09383492010-09-16 10:48:29 +02002694 * flush_delayed_work_sync - wait for a dwork to finish
2695 * @dwork: the delayed work to flush
2696 *
2697 * Delayed timer is cancelled and the pending work is queued for
2698 * execution immediately. Other than timer handling, its behavior
2699 * is identical to flush_work_sync().
2700 *
2701 * RETURNS:
2702 * %true if flush_work_sync() waited for the work to finish execution,
2703 * %false if it was already idle.
2704 */
2705bool flush_delayed_work_sync(struct delayed_work *dwork)
2706{
2707 if (del_timer_sync(&dwork->timer))
2708 __queue_work(raw_smp_processor_id(),
2709 get_work_cwq(&dwork->work)->wq, &dwork->work);
2710 return flush_work_sync(&dwork->work);
2711}
2712EXPORT_SYMBOL(flush_delayed_work_sync);
2713
2714/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002715 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2716 * @dwork: the delayed work cancel
2717 *
2718 * This is cancel_work_sync() for delayed works.
2719 *
2720 * RETURNS:
2721 * %true if @dwork was pending, %false otherwise.
2722 */
2723bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002724{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002725 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002726}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002727EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002729/**
2730 * schedule_work - put work task in global workqueue
2731 * @work: job to be done
2732 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002733 * Returns zero if @work was already on the kernel-global workqueue and
2734 * non-zero otherwise.
2735 *
2736 * This puts a job in the kernel-global workqueue if it was not already
2737 * queued and leaves it in the same position on the kernel-global
2738 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002739 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002740int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Tejun Heod320c032010-06-29 10:07:14 +02002742 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
Dave Jonesae90dd52006-06-30 01:40:45 -04002744EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Zhang Ruic1a220e2008-07-23 21:28:39 -07002746/*
2747 * schedule_work_on - put work task on a specific cpu
2748 * @cpu: cpu to put the work task on
2749 * @work: job to be done
2750 *
2751 * This puts a job on a specific cpu
2752 */
2753int schedule_work_on(int cpu, struct work_struct *work)
2754{
Tejun Heod320c032010-06-29 10:07:14 +02002755 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002756}
2757EXPORT_SYMBOL(schedule_work_on);
2758
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002759/**
2760 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002761 * @dwork: job to be done
2762 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002763 *
2764 * After waiting for a given time this puts a job in the kernel-global
2765 * workqueue.
2766 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002767int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002768 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Tejun Heod320c032010-06-29 10:07:14 +02002770 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
Dave Jonesae90dd52006-06-30 01:40:45 -04002772EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002774/**
2775 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2776 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002777 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002778 * @delay: number of jiffies to wait
2779 *
2780 * After waiting for a given time this puts a job in the kernel-global
2781 * workqueue on the specified CPU.
2782 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002784 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Tejun Heod320c032010-06-29 10:07:14 +02002786 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
Dave Jonesae90dd52006-06-30 01:40:45 -04002788EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Andrew Mortonb6136772006-06-25 05:47:49 -07002790/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002791 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002792 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002793 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002794 * schedule_on_each_cpu() executes @func on each online CPU using the
2795 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002796 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002797 *
2798 * RETURNS:
2799 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002800 */
David Howells65f27f32006-11-22 14:55:48 +00002801int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002802{
2803 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002804 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002805
Andrew Mortonb6136772006-06-25 05:47:49 -07002806 works = alloc_percpu(struct work_struct);
2807 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002808 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002809
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002810 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002811
Christoph Lameter15316ba2006-01-08 01:00:43 -08002812 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002813 struct work_struct *work = per_cpu_ptr(works, cpu);
2814
2815 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002816 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002817 }
Tejun Heo93981802009-11-17 14:06:20 -08002818
2819 for_each_online_cpu(cpu)
2820 flush_work(per_cpu_ptr(works, cpu));
2821
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002822 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002823 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002824 return 0;
2825}
2826
Alan Sterneef6a7d2010-02-12 17:39:21 +09002827/**
2828 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2829 *
2830 * Forces execution of the kernel-global workqueue and blocks until its
2831 * completion.
2832 *
2833 * Think twice before calling this function! It's very easy to get into
2834 * trouble if you don't take great care. Either of the following situations
2835 * will lead to deadlock:
2836 *
2837 * One of the work items currently on the workqueue needs to acquire
2838 * a lock held by your code or its caller.
2839 *
2840 * Your code is running in the context of a work routine.
2841 *
2842 * They will be detected by lockdep when they occur, but the first might not
2843 * occur very often. It depends on what work items are on the workqueue and
2844 * what locks they need, which you have no control over.
2845 *
2846 * In most situations flushing the entire workqueue is overkill; you merely
2847 * need to know that a particular work item isn't queued and isn't running.
2848 * In such cases you should use cancel_delayed_work_sync() or
2849 * cancel_work_sync() instead.
2850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851void flush_scheduled_work(void)
2852{
Tejun Heod320c032010-06-29 10:07:14 +02002853 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
Dave Jonesae90dd52006-06-30 01:40:45 -04002855EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002858 * execute_in_process_context - reliably execute the routine with user context
2859 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002860 * @ew: guaranteed storage for the execute work structure (must
2861 * be available when the work executes)
2862 *
2863 * Executes the function immediately if process context is available,
2864 * otherwise schedules the function for delayed execution.
2865 *
2866 * Returns: 0 - function was executed
2867 * 1 - function was scheduled for execution
2868 */
David Howells65f27f32006-11-22 14:55:48 +00002869int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002870{
2871 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002872 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002873 return 0;
2874 }
2875
David Howells65f27f32006-11-22 14:55:48 +00002876 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002877 schedule_work(&ew->work);
2878
2879 return 1;
2880}
2881EXPORT_SYMBOL_GPL(execute_in_process_context);
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883int keventd_up(void)
2884{
Tejun Heod320c032010-06-29 10:07:14 +02002885 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002888static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002890 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002891 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2892 * Make sure that the alignment isn't lower than that of
2893 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002894 */
Tejun Heo0f900042010-06-29 10:07:11 +02002895 const size_t size = sizeof(struct cpu_workqueue_struct);
2896 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2897 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002898
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002899 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002900 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002901 else {
Tejun Heof3421792010-07-02 10:03:51 +02002902 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002903
Tejun Heof3421792010-07-02 10:03:51 +02002904 /*
2905 * Allocate enough room to align cwq and put an extra
2906 * pointer at the end pointing back to the originally
2907 * allocated pointer which will be used for free.
2908 */
2909 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2910 if (ptr) {
2911 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2912 *(void **)(wq->cpu_wq.single + 1) = ptr;
2913 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002914 }
Tejun Heof3421792010-07-02 10:03:51 +02002915
Tejun Heo0415b002011-03-24 18:50:09 +01002916 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002917 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2918 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002919}
2920
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002921static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002922{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002923 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002924 free_percpu(wq->cpu_wq.pcpu);
2925 else if (wq->cpu_wq.single) {
2926 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002927 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002928 }
2929}
2930
Tejun Heof3421792010-07-02 10:03:51 +02002931static int wq_clamp_max_active(int max_active, unsigned int flags,
2932 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002933{
Tejun Heof3421792010-07-02 10:03:51 +02002934 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2935
2936 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002937 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2938 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002939 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002940
Tejun Heof3421792010-07-02 10:03:51 +02002941 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002942}
2943
Tejun Heob196be82012-01-10 15:11:35 -08002944struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002945 unsigned int flags,
2946 int max_active,
2947 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002948 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002949{
Tejun Heob196be82012-01-10 15:11:35 -08002950 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002951 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002952 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002953 size_t namelen;
2954
2955 /* determine namelen, allocate wq and format name */
2956 va_start(args, lock_name);
2957 va_copy(args1, args);
2958 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2959
2960 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2961 if (!wq)
2962 goto err;
2963
2964 vsnprintf(wq->name, namelen, fmt, args1);
2965 va_end(args);
2966 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002967
Tejun Heof3421792010-07-02 10:03:51 +02002968 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002969 * Workqueues which may be used during memory reclaim should
2970 * have a rescuer to guarantee forward progress.
2971 */
2972 if (flags & WQ_MEM_RECLAIM)
2973 flags |= WQ_RESCUER;
2974
Tejun Heod320c032010-06-29 10:07:14 +02002975 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08002976 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002977
Tejun Heob196be82012-01-10 15:11:35 -08002978 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02002979 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002980 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002981 mutex_init(&wq->flush_mutex);
2982 atomic_set(&wq->nr_cwqs_to_flush, 0);
2983 INIT_LIST_HEAD(&wq->flusher_queue);
2984 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002985
Johannes Bergeb13ba82008-01-16 09:51:58 +01002986 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002987 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002988
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002989 if (alloc_cwqs(wq) < 0)
2990 goto err;
2991
Tejun Heof3421792010-07-02 10:03:51 +02002992 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002993 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002994 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo2bfedd32012-07-13 22:16:45 -07002995 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02002996
Tejun Heo0f900042010-06-29 10:07:11 +02002997 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo2bfedd32012-07-13 22:16:45 -07002998 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02002999 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003000 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003001 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003002 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003003 }
3004
Tejun Heoe22bee72010-06-29 10:07:14 +02003005 if (flags & WQ_RESCUER) {
3006 struct worker *rescuer;
3007
Tejun Heof2e005a2010-07-20 15:59:09 +02003008 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003009 goto err;
3010
3011 wq->rescuer = rescuer = alloc_worker();
3012 if (!rescuer)
3013 goto err;
3014
Tejun Heob196be82012-01-10 15:11:35 -08003015 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3016 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003017 if (IS_ERR(rescuer->task))
3018 goto err;
3019
Tejun Heoe22bee72010-06-29 10:07:14 +02003020 rescuer->task->flags |= PF_THREAD_BOUND;
3021 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003022 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003023
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003024 /*
3025 * workqueue_lock protects global freeze state and workqueues
3026 * list. Grab it, set max_active accordingly and add the new
3027 * workqueue to workqueues list.
3028 */
Tejun Heo15376632010-06-29 10:07:11 +02003029 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003030
Tejun Heo58a69cb2011-02-16 09:25:31 +01003031 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003032 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003033 get_cwq(cpu, wq)->max_active = 0;
3034
Tejun Heo15376632010-06-29 10:07:11 +02003035 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003036
Tejun Heo15376632010-06-29 10:07:11 +02003037 spin_unlock(&workqueue_lock);
3038
Oleg Nesterov3af244332007-05-09 02:34:09 -07003039 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003040err:
3041 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003042 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003043 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003044 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003045 kfree(wq);
3046 }
3047 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003048}
Tejun Heod320c032010-06-29 10:07:14 +02003049EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003050
3051/**
3052 * destroy_workqueue - safely terminate a workqueue
3053 * @wq: target workqueue
3054 *
3055 * Safely destroy a workqueue. All work currently pending will be done first.
3056 */
3057void destroy_workqueue(struct workqueue_struct *wq)
3058{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003059 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003060
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003061 /* drain it before proceeding with destruction */
3062 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003063
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003064 /*
3065 * wq list is used to freeze wq, remove from list after
3066 * flushing is complete in case freeze races us.
3067 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003068 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003069 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003070 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003071
Tejun Heoe22bee72010-06-29 10:07:14 +02003072 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003073 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003074 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3075 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003076
Tejun Heo73f53c42010-06-29 10:07:11 +02003077 for (i = 0; i < WORK_NR_COLORS; i++)
3078 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003079 BUG_ON(cwq->nr_active);
3080 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003081 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003082
Tejun Heoe22bee72010-06-29 10:07:14 +02003083 if (wq->flags & WQ_RESCUER) {
3084 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003085 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003086 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003087 }
3088
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003089 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003090 kfree(wq);
3091}
3092EXPORT_SYMBOL_GPL(destroy_workqueue);
3093
Tejun Heodcd989c2010-06-29 10:07:14 +02003094/**
3095 * workqueue_set_max_active - adjust max_active of a workqueue
3096 * @wq: target workqueue
3097 * @max_active: new max_active value.
3098 *
3099 * Set max_active of @wq to @max_active.
3100 *
3101 * CONTEXT:
3102 * Don't call from IRQ context.
3103 */
3104void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3105{
3106 unsigned int cpu;
3107
Tejun Heof3421792010-07-02 10:03:51 +02003108 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003109
3110 spin_lock(&workqueue_lock);
3111
3112 wq->saved_max_active = max_active;
3113
Tejun Heof3421792010-07-02 10:03:51 +02003114 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003115 struct global_cwq *gcwq = get_gcwq(cpu);
3116
3117 spin_lock_irq(&gcwq->lock);
3118
Tejun Heo58a69cb2011-02-16 09:25:31 +01003119 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003120 !(gcwq->flags & GCWQ_FREEZING))
3121 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3122
3123 spin_unlock_irq(&gcwq->lock);
3124 }
3125
3126 spin_unlock(&workqueue_lock);
3127}
3128EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3129
3130/**
3131 * workqueue_congested - test whether a workqueue is congested
3132 * @cpu: CPU in question
3133 * @wq: target workqueue
3134 *
3135 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3136 * no synchronization around this function and the test result is
3137 * unreliable and only useful as advisory hints or for debugging.
3138 *
3139 * RETURNS:
3140 * %true if congested, %false otherwise.
3141 */
3142bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3143{
3144 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3145
3146 return !list_empty(&cwq->delayed_works);
3147}
3148EXPORT_SYMBOL_GPL(workqueue_congested);
3149
3150/**
3151 * work_cpu - return the last known associated cpu for @work
3152 * @work: the work of interest
3153 *
3154 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003155 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003156 */
3157unsigned int work_cpu(struct work_struct *work)
3158{
3159 struct global_cwq *gcwq = get_work_gcwq(work);
3160
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003161 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003162}
3163EXPORT_SYMBOL_GPL(work_cpu);
3164
3165/**
3166 * work_busy - test whether a work is currently pending or running
3167 * @work: the work to be tested
3168 *
3169 * Test whether @work is currently pending or running. There is no
3170 * synchronization around this function and the test result is
3171 * unreliable and only useful as advisory hints or for debugging.
3172 * Especially for reentrant wqs, the pending state might hide the
3173 * running state.
3174 *
3175 * RETURNS:
3176 * OR'd bitmask of WORK_BUSY_* bits.
3177 */
3178unsigned int work_busy(struct work_struct *work)
3179{
3180 struct global_cwq *gcwq = get_work_gcwq(work);
3181 unsigned long flags;
3182 unsigned int ret = 0;
3183
3184 if (!gcwq)
3185 return false;
3186
3187 spin_lock_irqsave(&gcwq->lock, flags);
3188
3189 if (work_pending(work))
3190 ret |= WORK_BUSY_PENDING;
3191 if (find_worker_executing_work(gcwq, work))
3192 ret |= WORK_BUSY_RUNNING;
3193
3194 spin_unlock_irqrestore(&gcwq->lock, flags);
3195
3196 return ret;
3197}
3198EXPORT_SYMBOL_GPL(work_busy);
3199
Tejun Heodb7bccf2010-06-29 10:07:12 +02003200/*
3201 * CPU hotplug.
3202 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003203 * There are two challenges in supporting CPU hotplug. Firstly, there
3204 * are a lot of assumptions on strong associations among work, cwq and
3205 * gcwq which make migrating pending and scheduled works very
3206 * difficult to implement without impacting hot paths. Secondly,
3207 * gcwqs serve mix of short, long and very long running works making
3208 * blocked draining impractical.
3209 *
3210 * This is solved by allowing a gcwq to be detached from CPU, running
3211 * it with unbound (rogue) workers and allowing it to be reattached
3212 * later if the cpu comes back online. A separate thread is created
3213 * to govern a gcwq in such state and is called the trustee of the
3214 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003215 *
3216 * Trustee states and their descriptions.
3217 *
3218 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3219 * new trustee is started with this state.
3220 *
3221 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003222 * assuming the manager role and making all existing
3223 * workers rogue. DOWN_PREPARE waits for trustee to
3224 * enter this state. After reaching IN_CHARGE, trustee
3225 * tries to execute the pending worklist until it's empty
3226 * and the state is set to BUTCHER, or the state is set
3227 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003228 *
3229 * BUTCHER Command state which is set by the cpu callback after
3230 * the cpu has went down. Once this state is set trustee
3231 * knows that there will be no new works on the worklist
3232 * and once the worklist is empty it can proceed to
3233 * killing idle workers.
3234 *
3235 * RELEASE Command state which is set by the cpu callback if the
3236 * cpu down has been canceled or it has come online
3237 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003238 * trying to drain or butcher and clears ROGUE, rebinds
3239 * all remaining workers back to the cpu and releases
3240 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003241 *
3242 * DONE Trustee will enter this state after BUTCHER or RELEASE
3243 * is complete.
3244 *
3245 * trustee CPU draining
3246 * took over down complete
3247 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3248 * | | ^
3249 * | CPU is back online v return workers |
3250 * ----------------> RELEASE --------------
3251 */
3252
3253/**
3254 * trustee_wait_event_timeout - timed event wait for trustee
3255 * @cond: condition to wait for
3256 * @timeout: timeout in jiffies
3257 *
3258 * wait_event_timeout() for trustee to use. Handles locking and
3259 * checks for RELEASE request.
3260 *
3261 * CONTEXT:
3262 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3263 * multiple times. To be used by trustee.
3264 *
3265 * RETURNS:
3266 * Positive indicating left time if @cond is satisfied, 0 if timed
3267 * out, -1 if canceled.
3268 */
3269#define trustee_wait_event_timeout(cond, timeout) ({ \
3270 long __ret = (timeout); \
3271 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3272 __ret) { \
3273 spin_unlock_irq(&gcwq->lock); \
3274 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3275 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3276 __ret); \
3277 spin_lock_irq(&gcwq->lock); \
3278 } \
3279 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3280})
3281
3282/**
3283 * trustee_wait_event - event wait for trustee
3284 * @cond: condition to wait for
3285 *
3286 * wait_event() for trustee to use. Automatically handles locking and
3287 * checks for CANCEL request.
3288 *
3289 * CONTEXT:
3290 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3291 * multiple times. To be used by trustee.
3292 *
3293 * RETURNS:
3294 * 0 if @cond is satisfied, -1 if canceled.
3295 */
3296#define trustee_wait_event(cond) ({ \
3297 long __ret1; \
3298 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3299 __ret1 < 0 ? -1 : 0; \
3300})
3301
Tejun Heo0290d202012-07-13 22:16:44 -07003302static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3303{
3304 struct worker_pool *pool;
3305
3306 for_each_worker_pool(pool, gcwq)
3307 if (pool->flags & POOL_MANAGING_WORKERS)
3308 return true;
3309 return false;
3310}
3311
3312static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3313{
3314 struct worker_pool *pool;
3315
3316 for_each_worker_pool(pool, gcwq)
3317 if (!list_empty(&pool->idle_list))
3318 return true;
3319 return false;
3320}
3321
Tejun Heodb7bccf2010-06-29 10:07:12 +02003322static int __cpuinit trustee_thread(void *__gcwq)
3323{
3324 struct global_cwq *gcwq = __gcwq;
Tejun Heo0290d202012-07-13 22:16:44 -07003325 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003326 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003327 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003328 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003329 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003330 int i;
3331
3332 BUG_ON(gcwq->cpu != smp_processor_id());
3333
3334 spin_lock_irq(&gcwq->lock);
3335 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003336 * Claim the manager position and make all workers rogue.
3337 * Trustee must be bound to the target cpu and can't be
3338 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003339 */
3340 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo0290d202012-07-13 22:16:44 -07003341 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003342 BUG_ON(rc < 0);
3343
Tejun Heo0290d202012-07-13 22:16:44 -07003344 for_each_worker_pool(pool, gcwq) {
3345 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003346
Tejun Heo0290d202012-07-13 22:16:44 -07003347 list_for_each_entry(worker, &pool->idle_list, entry)
3348 worker->flags |= WORKER_ROGUE;
3349 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003350
3351 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003352 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003353
3354 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003355 * Call schedule() so that we cross rq->lock and thus can
3356 * guarantee sched callbacks see the rogue flag. This is
3357 * necessary as scheduler callbacks may be invoked from other
3358 * cpus.
3359 */
3360 spin_unlock_irq(&gcwq->lock);
3361 schedule();
3362 spin_lock_irq(&gcwq->lock);
3363
3364 /*
Tejun Heocb444762010-07-02 10:03:50 +02003365 * Sched callbacks are disabled now. Zap nr_running. After
3366 * this, nr_running stays zero and need_more_worker() and
3367 * keep_working() are always true as long as the worklist is
3368 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003369 */
Tejun Heo0290d202012-07-13 22:16:44 -07003370 for_each_worker_pool(pool, gcwq)
3371 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003372
3373 spin_unlock_irq(&gcwq->lock);
Tejun Heo0290d202012-07-13 22:16:44 -07003374 for_each_worker_pool(pool, gcwq)
3375 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003376 spin_lock_irq(&gcwq->lock);
3377
3378 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003379 * We're now in charge. Notify and proceed to drain. We need
3380 * to keep the gcwq running during the whole CPU down
3381 * procedure as other cpu hotunplug callbacks may need to
3382 * flush currently running tasks.
3383 */
3384 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3385 wake_up_all(&gcwq->trustee_wait);
3386
3387 /*
3388 * The original cpu is in the process of dying and may go away
3389 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003390 * be migrated to other cpus. Try draining any left work. We
3391 * want to get it over with ASAP - spam rescuers, wake up as
3392 * many idlers as necessary and create new ones till the
3393 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003394 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003395 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003396 */
Tejun Heo0290d202012-07-13 22:16:44 -07003397 while (true) {
3398 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003399
Tejun Heo0290d202012-07-13 22:16:44 -07003400 for_each_worker_pool(pool, gcwq)
3401 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003402
Tejun Heo0290d202012-07-13 22:16:44 -07003403 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3404 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3405 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003406
Tejun Heo0290d202012-07-13 22:16:44 -07003407 for_each_worker_pool(pool, gcwq) {
3408 int nr_works = 0;
3409
3410 list_for_each_entry(work, &pool->worklist, entry) {
3411 send_mayday(work);
3412 nr_works++;
3413 }
3414
3415 list_for_each_entry(worker, &pool->idle_list, entry) {
3416 if (!nr_works--)
3417 break;
3418 wake_up_process(worker->task);
3419 }
3420
3421 if (need_to_create_worker(pool)) {
3422 spin_unlock_irq(&gcwq->lock);
3423 worker = create_worker(pool, false);
3424 spin_lock_irq(&gcwq->lock);
3425 if (worker) {
3426 worker->flags |= WORKER_ROGUE;
3427 start_worker(worker);
3428 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003429 }
3430 }
3431
Tejun Heodb7bccf2010-06-29 10:07:12 +02003432 /* give a breather */
3433 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3434 break;
3435 }
3436
Tejun Heoe22bee72010-06-29 10:07:14 +02003437 /*
3438 * Either all works have been scheduled and cpu is down, or
3439 * cpu down has already been canceled. Wait for and butcher
3440 * all workers till we're canceled.
3441 */
3442 do {
Tejun Heo0290d202012-07-13 22:16:44 -07003443 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3444
3445 i = 0;
3446 for_each_worker_pool(pool, gcwq) {
3447 while (!list_empty(&pool->idle_list)) {
3448 worker = list_first_entry(&pool->idle_list,
3449 struct worker, entry);
3450 destroy_worker(worker);
3451 }
3452 i |= pool->nr_workers;
3453 }
3454 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003455
3456 /*
3457 * At this point, either draining has completed and no worker
3458 * is left, or cpu down has been canceled or the cpu is being
3459 * brought back up. There shouldn't be any idle one left.
3460 * Tell the remaining busy ones to rebind once it finishes the
3461 * currently scheduled works by scheduling the rebind_work.
3462 */
Tejun Heo0290d202012-07-13 22:16:44 -07003463 for_each_worker_pool(pool, gcwq)
3464 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003465
3466 for_each_busy_worker(worker, i, pos, gcwq) {
3467 struct work_struct *rebind_work = &worker->rebind_work;
3468
3469 /*
3470 * Rebind_work may race with future cpu hotplug
3471 * operations. Use a separate flag to mark that
3472 * rebinding is scheduled.
3473 */
Tejun Heocb444762010-07-02 10:03:50 +02003474 worker->flags |= WORKER_REBIND;
3475 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003476
3477 /* queue rebind_work, wq doesn't matter, use the default one */
3478 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3479 work_data_bits(rebind_work)))
3480 continue;
3481
3482 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003483 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003484 worker->scheduled.next,
3485 work_color_to_flags(WORK_NO_COLOR));
3486 }
3487
3488 /* relinquish manager role */
Tejun Heo0290d202012-07-13 22:16:44 -07003489 for_each_worker_pool(pool, gcwq)
3490 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003491
Tejun Heodb7bccf2010-06-29 10:07:12 +02003492 /* notify completion */
3493 gcwq->trustee = NULL;
3494 gcwq->trustee_state = TRUSTEE_DONE;
3495 wake_up_all(&gcwq->trustee_wait);
3496 spin_unlock_irq(&gcwq->lock);
3497 return 0;
3498}
3499
3500/**
3501 * wait_trustee_state - wait for trustee to enter the specified state
3502 * @gcwq: gcwq the trustee of interest belongs to
3503 * @state: target state to wait for
3504 *
3505 * Wait for the trustee to reach @state. DONE is already matched.
3506 *
3507 * CONTEXT:
3508 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3509 * multiple times. To be used by cpu_callback.
3510 */
3511static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003512__releases(&gcwq->lock)
3513__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003514{
3515 if (!(gcwq->trustee_state == state ||
3516 gcwq->trustee_state == TRUSTEE_DONE)) {
3517 spin_unlock_irq(&gcwq->lock);
3518 __wait_event(gcwq->trustee_wait,
3519 gcwq->trustee_state == state ||
3520 gcwq->trustee_state == TRUSTEE_DONE);
3521 spin_lock_irq(&gcwq->lock);
3522 }
3523}
3524
Oleg Nesterov3af244332007-05-09 02:34:09 -07003525static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3526 unsigned long action,
3527 void *hcpu)
3528{
3529 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003530 struct global_cwq *gcwq = get_gcwq(cpu);
3531 struct task_struct *new_trustee = NULL;
Tejun Heo0290d202012-07-13 22:16:44 -07003532 struct worker *new_workers[NR_WORKER_POOLS] = { };
3533 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003534 unsigned long flags;
Tejun Heo0290d202012-07-13 22:16:44 -07003535 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003537 action &= ~CPU_TASKS_FROZEN;
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003540 case CPU_DOWN_PREPARE:
3541 new_trustee = kthread_create(trustee_thread, gcwq,
3542 "workqueue_trustee/%d\n", cpu);
3543 if (IS_ERR(new_trustee))
3544 return notifier_from_errno(PTR_ERR(new_trustee));
3545 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003546 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 case CPU_UP_PREPARE:
Tejun Heo0290d202012-07-13 22:16:44 -07003548 i = 0;
3549 for_each_worker_pool(pool, gcwq) {
3550 BUG_ON(pool->first_idle);
3551 new_workers[i] = create_worker(pool, false);
3552 if (!new_workers[i++])
3553 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556
Tejun Heodb7bccf2010-06-29 10:07:12 +02003557 /* some are called w/ irq disabled, don't disturb irq status */
3558 spin_lock_irqsave(&gcwq->lock, flags);
3559
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003560 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003561 case CPU_DOWN_PREPARE:
3562 /* initialize trustee and tell it to acquire the gcwq */
3563 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3564 gcwq->trustee = new_trustee;
3565 gcwq->trustee_state = TRUSTEE_START;
3566 wake_up_process(gcwq->trustee);
3567 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003568 /* fall through */
3569 case CPU_UP_PREPARE:
Tejun Heo0290d202012-07-13 22:16:44 -07003570 i = 0;
3571 for_each_worker_pool(pool, gcwq) {
3572 BUG_ON(pool->first_idle);
3573 pool->first_idle = new_workers[i++];
3574 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003575 break;
3576
3577 case CPU_DYING:
3578 /*
3579 * Before this, the trustee and all workers except for
3580 * the ones which are still executing works from
3581 * before the last CPU down must be on the cpu. After
3582 * this, they'll all be diasporas.
3583 */
3584 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003585 break;
3586
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003587 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003588 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003589 /* fall through */
3590 case CPU_UP_CANCELED:
Tejun Heo0290d202012-07-13 22:16:44 -07003591 for_each_worker_pool(pool, gcwq) {
3592 destroy_worker(pool->first_idle);
3593 pool->first_idle = NULL;
3594 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003595 break;
3596
3597 case CPU_DOWN_FAILED:
3598 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003599 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003600 if (gcwq->trustee_state != TRUSTEE_DONE) {
3601 gcwq->trustee_state = TRUSTEE_RELEASE;
3602 wake_up_process(gcwq->trustee);
3603 wait_trustee_state(gcwq, TRUSTEE_DONE);
3604 }
3605
Tejun Heoe22bee72010-06-29 10:07:14 +02003606 /*
3607 * Trustee is done and there might be no worker left.
3608 * Put the first_idle in and request a real manager to
3609 * take a look.
3610 */
Tejun Heo0290d202012-07-13 22:16:44 -07003611 for_each_worker_pool(pool, gcwq) {
3612 spin_unlock_irq(&gcwq->lock);
3613 kthread_bind(pool->first_idle->task, cpu);
3614 spin_lock_irq(&gcwq->lock);
3615 pool->flags |= POOL_MANAGE_WORKERS;
3616 start_worker(pool->first_idle);
3617 pool->first_idle = NULL;
3618 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003619 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003620 }
3621
Tejun Heodb7bccf2010-06-29 10:07:12 +02003622 spin_unlock_irqrestore(&gcwq->lock, flags);
3623
Tejun Heo15376632010-06-29 10:07:11 +02003624 return notifier_from_errno(0);
Tejun Heo0290d202012-07-13 22:16:44 -07003625
3626err_destroy:
3627 if (new_trustee)
3628 kthread_stop(new_trustee);
3629
3630 spin_lock_irqsave(&gcwq->lock, flags);
3631 for (i = 0; i < NR_WORKER_POOLS; i++)
3632 if (new_workers[i])
3633 destroy_worker(new_workers[i]);
3634 spin_unlock_irqrestore(&gcwq->lock, flags);
3635
3636 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Rusty Russell2d3854a2008-11-05 13:39:10 +11003639#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003640
Rusty Russell2d3854a2008-11-05 13:39:10 +11003641struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003642 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003643 long (*fn)(void *);
3644 void *arg;
3645 long ret;
3646};
3647
Andrew Morton6b440032009-04-09 09:50:37 -06003648static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003649{
Andrew Morton6b440032009-04-09 09:50:37 -06003650 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003651 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003652 complete(&wfc->completion);
3653 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003654}
3655
3656/**
3657 * work_on_cpu - run a function in user context on a particular cpu
3658 * @cpu: the cpu to run on
3659 * @fn: the function to run
3660 * @arg: the function arg
3661 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003662 * This will return the value @fn returns.
3663 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003664 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003665 */
3666long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3667{
Andrew Morton6b440032009-04-09 09:50:37 -06003668 struct task_struct *sub_thread;
3669 struct work_for_cpu wfc = {
3670 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3671 .fn = fn,
3672 .arg = arg,
3673 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003674
Andrew Morton6b440032009-04-09 09:50:37 -06003675 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3676 if (IS_ERR(sub_thread))
3677 return PTR_ERR(sub_thread);
3678 kthread_bind(sub_thread, cpu);
3679 wake_up_process(sub_thread);
3680 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003681 return wfc.ret;
3682}
3683EXPORT_SYMBOL_GPL(work_on_cpu);
3684#endif /* CONFIG_SMP */
3685
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003686#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303687
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003688/**
3689 * freeze_workqueues_begin - begin freezing workqueues
3690 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003691 * Start freezing workqueues. After this function returns, all freezable
3692 * workqueues will queue new works to their frozen_works list instead of
3693 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003694 *
3695 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003696 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003697 */
3698void freeze_workqueues_begin(void)
3699{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003700 unsigned int cpu;
3701
3702 spin_lock(&workqueue_lock);
3703
3704 BUG_ON(workqueue_freezing);
3705 workqueue_freezing = true;
3706
Tejun Heof3421792010-07-02 10:03:51 +02003707 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003708 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003709 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003710
3711 spin_lock_irq(&gcwq->lock);
3712
Tejun Heodb7bccf2010-06-29 10:07:12 +02003713 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3714 gcwq->flags |= GCWQ_FREEZING;
3715
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716 list_for_each_entry(wq, &workqueues, list) {
3717 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3718
Tejun Heo58a69cb2011-02-16 09:25:31 +01003719 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003720 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003721 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003722
3723 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003724 }
3725
3726 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003728
3729/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003730 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003731 *
3732 * Check whether freezing is complete. This function must be called
3733 * between freeze_workqueues_begin() and thaw_workqueues().
3734 *
3735 * CONTEXT:
3736 * Grabs and releases workqueue_lock.
3737 *
3738 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003739 * %true if some freezable workqueues are still busy. %false if freezing
3740 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003741 */
3742bool freeze_workqueues_busy(void)
3743{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003744 unsigned int cpu;
3745 bool busy = false;
3746
3747 spin_lock(&workqueue_lock);
3748
3749 BUG_ON(!workqueue_freezing);
3750
Tejun Heof3421792010-07-02 10:03:51 +02003751 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003752 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003753 /*
3754 * nr_active is monotonically decreasing. It's safe
3755 * to peek without lock.
3756 */
3757 list_for_each_entry(wq, &workqueues, list) {
3758 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3759
Tejun Heo58a69cb2011-02-16 09:25:31 +01003760 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003761 continue;
3762
3763 BUG_ON(cwq->nr_active < 0);
3764 if (cwq->nr_active) {
3765 busy = true;
3766 goto out_unlock;
3767 }
3768 }
3769 }
3770out_unlock:
3771 spin_unlock(&workqueue_lock);
3772 return busy;
3773}
3774
3775/**
3776 * thaw_workqueues - thaw workqueues
3777 *
3778 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003779 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003780 *
3781 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003782 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003783 */
3784void thaw_workqueues(void)
3785{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003786 unsigned int cpu;
3787
3788 spin_lock(&workqueue_lock);
3789
3790 if (!workqueue_freezing)
3791 goto out_unlock;
3792
Tejun Heof3421792010-07-02 10:03:51 +02003793 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003794 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo0290d202012-07-13 22:16:44 -07003795 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003796 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003797
3798 spin_lock_irq(&gcwq->lock);
3799
Tejun Heodb7bccf2010-06-29 10:07:12 +02003800 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3801 gcwq->flags &= ~GCWQ_FREEZING;
3802
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003803 list_for_each_entry(wq, &workqueues, list) {
3804 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3805
Tejun Heo58a69cb2011-02-16 09:25:31 +01003806 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003807 continue;
3808
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003809 /* restore max_active and repopulate worklist */
3810 cwq->max_active = wq->saved_max_active;
3811
3812 while (!list_empty(&cwq->delayed_works) &&
3813 cwq->nr_active < cwq->max_active)
3814 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003815 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003816
Tejun Heo0290d202012-07-13 22:16:44 -07003817 for_each_worker_pool(pool, gcwq)
3818 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003819
Tejun Heo8b03ae32010-06-29 10:07:12 +02003820 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003821 }
3822
3823 workqueue_freezing = false;
3824out_unlock:
3825 spin_unlock(&workqueue_lock);
3826}
3827#endif /* CONFIG_FREEZER */
3828
Suresh Siddha6ee05782010-07-30 14:57:37 -07003829static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
Tejun Heoc34056a2010-06-29 10:07:11 +02003831 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003832 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003833
Tejun Heof6500942010-08-09 11:50:34 +02003834 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003835
3836 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003837 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003838 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo0290d202012-07-13 22:16:44 -07003839 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003840
3841 spin_lock_init(&gcwq->lock);
3842 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003843 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003844
Tejun Heoc8e55f32010-06-29 10:07:12 +02003845 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3846 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3847
Tejun Heo0290d202012-07-13 22:16:44 -07003848 for_each_worker_pool(pool, gcwq) {
3849 pool->gcwq = gcwq;
3850 INIT_LIST_HEAD(&pool->worklist);
3851 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003852
Tejun Heo0290d202012-07-13 22:16:44 -07003853 init_timer_deferrable(&pool->idle_timer);
3854 pool->idle_timer.function = idle_worker_timeout;
3855 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003856
Tejun Heo0290d202012-07-13 22:16:44 -07003857 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3858 (unsigned long)pool);
3859
3860 ida_init(&pool->worker_ida);
3861 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003862
3863 gcwq->trustee_state = TRUSTEE_DONE;
3864 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003865 }
3866
Tejun Heoe22bee72010-06-29 10:07:14 +02003867 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003868 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003869 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo0290d202012-07-13 22:16:44 -07003870 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003871
Tejun Heo477a3c32010-08-31 10:54:35 +02003872 if (cpu != WORK_CPU_UNBOUND)
3873 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo0290d202012-07-13 22:16:44 -07003874
3875 for_each_worker_pool(pool, gcwq) {
3876 struct worker *worker;
3877
3878 worker = create_worker(pool, true);
3879 BUG_ON(!worker);
3880 spin_lock_irq(&gcwq->lock);
3881 start_worker(worker);
3882 spin_unlock_irq(&gcwq->lock);
3883 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003884 }
3885
Tejun Heod320c032010-06-29 10:07:14 +02003886 system_wq = alloc_workqueue("events", 0, 0);
3887 system_long_wq = alloc_workqueue("events_long", 0, 0);
3888 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003889 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3890 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003891 system_freezable_wq = alloc_workqueue("events_freezable",
3892 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003893 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3894 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003895 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003896 !system_unbound_wq || !system_freezable_wq ||
3897 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003900early_initcall(init_workqueues);