blob: 55bbc076245618779241699b988998d71adae227 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020048 /* global_cwq flags */
Tejun Heoe22bee72010-06-29 10:07:14 +020049 GCWQ_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
50 GCWQ_MANAGING_WORKERS = 1 << 1, /* managing workers */
51 GCWQ_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020052 GCWQ_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heo649027d2010-06-29 10:07:14 +020053 GCWQ_HIGHPRI_PENDING = 1 << 4, /* highpri works on queue */
Tejun Heodb7bccf2010-06-29 10:07:12 +020054
Tejun Heoc8e55f32010-06-29 10:07:12 +020055 /* worker flags */
56 WORKER_STARTED = 1 << 0, /* started */
57 WORKER_DIE = 1 << 1, /* die die die */
58 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020059 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020060 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020062 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020063 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020064
Tejun Heofb0e7be2010-06-29 10:07:15 +020065 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020066 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020067
68 /* gcwq->trustee_state */
69 TRUSTEE_START = 0, /* start */
70 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
71 TRUSTEE_BUTCHER = 2, /* butcher workers */
72 TRUSTEE_RELEASE = 3, /* release workers */
73 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020074
75 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
76 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
77 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020078
Tejun Heoe22bee72010-06-29 10:07:14 +020079 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
80 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
81
Tejun Heo3233cdb2011-02-16 18:10:19 +010082 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
83 /* call for help after 10ms
84 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
86 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020087 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020088
89 /*
90 * Rescue workers are used only on emergencies and shared by
91 * all cpus. Give -20.
92 */
93 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020094};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020097 * Structure fields follow one of the following exclusion rules.
98 *
Tejun Heoe41e7042010-08-24 14:22:47 +020099 * I: Modifiable by initialization/destruction paths and read-only for
100 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200101 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200102 * P: Preemption protected. Disabling preemption is enough and should
103 * only be modified and accessed from the local cpu.
104 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200105 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200107 * X: During normal operation, modification requires gcwq->lock and
108 * should be done only from local cpu. Either disabling preemption
109 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200110 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200112 * F: wq->flush_mutex protected.
113 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 * W: workqueue_lock protected.
115 */
116
Tejun Heo8b03ae32010-06-29 10:07:12 +0200117struct global_cwq;
Tejun Heo47303942012-07-12 14:46:37 -0700118struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200119
Tejun Heoe22bee72010-06-29 10:07:14 +0200120/*
121 * The poor guys doing the actual heavy lifting. All on-duty workers
122 * are either serving the manager role, on idle list or on busy hash.
123 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200125 /* on idle list while idle, on busy hash table while busy */
126 union {
127 struct list_head entry; /* L: while idle */
128 struct hlist_node hentry; /* L: while busy */
129 };
130
Tejun Heoc34056a2010-06-29 10:07:11 +0200131 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200132 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200133 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200134 struct task_struct *task; /* I: worker task */
Tejun Heo47303942012-07-12 14:46:37 -0700135 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200136 /* 64 bytes boundary on 64bit, 32 on 32bit */
137 unsigned long last_active; /* L: last active timestamp */
138 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200140 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200141};
142
Tejun Heo47303942012-07-12 14:46:37 -0700143struct worker_pool {
144 struct global_cwq *gcwq; /* I: the owning gcwq */
145
146 struct list_head worklist; /* L: list of pending works */
147 int nr_workers; /* L: total number of workers */
148 int nr_idle; /* L: currently idle ones */
149
150 struct list_head idle_list; /* X: list of idle workers */
151 struct timer_list idle_timer; /* L: worker idle timeout */
152 struct timer_list mayday_timer; /* L: SOS timer for workers */
153
154 struct ida worker_ida; /* L: for worker IDs */
155 struct worker *first_idle; /* L: first idle worker */
156};
157
Tejun Heo4690c4a2010-06-29 10:07:10 +0200158/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200159 * Global per-cpu workqueue. There's one and only one for each cpu
160 * and all works are queued and processed here regardless of their
161 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200162 */
163struct global_cwq {
164 spinlock_t lock; /* the gcwq lock */
165 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200166 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200167
Tejun Heo47303942012-07-12 14:46:37 -0700168 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200169 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
170 /* L: hash of busy workers */
171
Tejun Heo47303942012-07-12 14:46:37 -0700172 struct worker_pool pool; /* the worker pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200173
174 struct task_struct *trustee; /* L: for gcwq shutdown */
175 unsigned int trustee_state; /* L: trustee state */
176 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200177} ____cacheline_aligned_in_smp;
178
179/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200180 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200181 * work_struct->data are used for flags and thus cwqs need to be
182 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184struct cpu_workqueue_struct {
Tejun Heo47303942012-07-12 14:46:37 -0700185 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200186 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200187 int work_color; /* L: current color */
188 int flush_color; /* L: flushing color */
189 int nr_in_flight[WORK_NR_COLORS];
190 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200191 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200192 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200193 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200197 * Structure used to wait for workqueue flush.
198 */
199struct wq_flusher {
200 struct list_head list; /* F: list of flushers */
201 int flush_color; /* F: flush color waiting for */
202 struct completion done; /* flush completion */
203};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tejun Heo73f53c42010-06-29 10:07:11 +0200205/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200206 * All cpumasks are assumed to be always set on UP and thus can't be
207 * used to determine whether there's something to be done.
208 */
209#ifdef CONFIG_SMP
210typedef cpumask_var_t mayday_mask_t;
211#define mayday_test_and_set_cpu(cpu, mask) \
212 cpumask_test_and_set_cpu((cpu), (mask))
213#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
214#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200215#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200216#define free_mayday_mask(mask) free_cpumask_var((mask))
217#else
218typedef unsigned long mayday_mask_t;
219#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
220#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
221#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
222#define alloc_mayday_mask(maskp, gfp) true
223#define free_mayday_mask(mask) do { } while (0)
224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/*
227 * The externally visible workqueue abstraction is an array of
228 * per-CPU workqueues:
229 */
230struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200231 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200232 union {
233 struct cpu_workqueue_struct __percpu *pcpu;
234 struct cpu_workqueue_struct *single;
235 unsigned long v;
236 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200237 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200238
239 struct mutex flush_mutex; /* protects wq flushing */
240 int work_color; /* F: current work color */
241 int flush_color; /* F: current flush color */
242 atomic_t nr_cwqs_to_flush; /* flush in progress */
243 struct wq_flusher *first_flusher; /* F: first flusher */
244 struct list_head flusher_queue; /* F: flush waiters */
245 struct list_head flusher_overflow; /* F: flush overflow list */
246
Tejun Heof2e005a2010-07-20 15:59:09 +0200247 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200248 struct worker *rescuer; /* I: rescue worker */
249
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200250 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200251 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700252#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200253 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700254#endif
Tejun Heob196be82012-01-10 15:11:35 -0800255 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Tejun Heod320c032010-06-29 10:07:14 +0200258struct workqueue_struct *system_wq __read_mostly;
259struct workqueue_struct *system_long_wq __read_mostly;
260struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200261struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100262struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100263struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200264EXPORT_SYMBOL_GPL(system_wq);
265EXPORT_SYMBOL_GPL(system_long_wq);
266EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200267EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100268EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100269EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200270
Tejun Heo97bd2342010-10-05 10:41:14 +0200271#define CREATE_TRACE_POINTS
272#include <trace/events/workqueue.h>
273
Tejun Heodb7bccf2010-06-29 10:07:12 +0200274#define for_each_busy_worker(worker, i, pos, gcwq) \
275 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
276 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
277
Tejun Heof3421792010-07-02 10:03:51 +0200278static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
279 unsigned int sw)
280{
281 if (cpu < nr_cpu_ids) {
282 if (sw & 1) {
283 cpu = cpumask_next(cpu, mask);
284 if (cpu < nr_cpu_ids)
285 return cpu;
286 }
287 if (sw & 2)
288 return WORK_CPU_UNBOUND;
289 }
290 return WORK_CPU_NONE;
291}
292
293static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
294 struct workqueue_struct *wq)
295{
296 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
297}
298
Tejun Heo09884952010-08-01 11:50:12 +0200299/*
300 * CPU iterators
301 *
302 * An extra gcwq is defined for an invalid cpu number
303 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
304 * specific CPU. The following iterators are similar to
305 * for_each_*_cpu() iterators but also considers the unbound gcwq.
306 *
307 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
308 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
309 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
310 * WORK_CPU_UNBOUND for unbound workqueues
311 */
Tejun Heof3421792010-07-02 10:03:51 +0200312#define for_each_gcwq_cpu(cpu) \
313 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
314 (cpu) < WORK_CPU_NONE; \
315 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
316
317#define for_each_online_gcwq_cpu(cpu) \
318 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
319 (cpu) < WORK_CPU_NONE; \
320 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
321
322#define for_each_cwq_cpu(cpu, wq) \
323 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
324 (cpu) < WORK_CPU_NONE; \
325 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
326
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900327#ifdef CONFIG_DEBUG_OBJECTS_WORK
328
329static struct debug_obj_descr work_debug_descr;
330
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100331static void *work_debug_hint(void *addr)
332{
333 return ((struct work_struct *) addr)->func;
334}
335
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900336/*
337 * fixup_init is called when:
338 * - an active object is initialized
339 */
340static int work_fixup_init(void *addr, enum debug_obj_state state)
341{
342 struct work_struct *work = addr;
343
344 switch (state) {
345 case ODEBUG_STATE_ACTIVE:
346 cancel_work_sync(work);
347 debug_object_init(work, &work_debug_descr);
348 return 1;
349 default:
350 return 0;
351 }
352}
353
354/*
355 * fixup_activate is called when:
356 * - an active object is activated
357 * - an unknown object is activated (might be a statically initialized object)
358 */
359static int work_fixup_activate(void *addr, enum debug_obj_state state)
360{
361 struct work_struct *work = addr;
362
363 switch (state) {
364
365 case ODEBUG_STATE_NOTAVAILABLE:
366 /*
367 * This is not really a fixup. The work struct was
368 * statically initialized. We just make sure that it
369 * is tracked in the object tracker.
370 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200371 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900372 debug_object_init(work, &work_debug_descr);
373 debug_object_activate(work, &work_debug_descr);
374 return 0;
375 }
376 WARN_ON_ONCE(1);
377 return 0;
378
379 case ODEBUG_STATE_ACTIVE:
380 WARN_ON(1);
381
382 default:
383 return 0;
384 }
385}
386
387/*
388 * fixup_free is called when:
389 * - an active object is freed
390 */
391static int work_fixup_free(void *addr, enum debug_obj_state state)
392{
393 struct work_struct *work = addr;
394
395 switch (state) {
396 case ODEBUG_STATE_ACTIVE:
397 cancel_work_sync(work);
398 debug_object_free(work, &work_debug_descr);
399 return 1;
400 default:
401 return 0;
402 }
403}
404
405static struct debug_obj_descr work_debug_descr = {
406 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100407 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900408 .fixup_init = work_fixup_init,
409 .fixup_activate = work_fixup_activate,
410 .fixup_free = work_fixup_free,
411};
412
413static inline void debug_work_activate(struct work_struct *work)
414{
415 debug_object_activate(work, &work_debug_descr);
416}
417
418static inline void debug_work_deactivate(struct work_struct *work)
419{
420 debug_object_deactivate(work, &work_debug_descr);
421}
422
423void __init_work(struct work_struct *work, int onstack)
424{
425 if (onstack)
426 debug_object_init_on_stack(work, &work_debug_descr);
427 else
428 debug_object_init(work, &work_debug_descr);
429}
430EXPORT_SYMBOL_GPL(__init_work);
431
432void destroy_work_on_stack(struct work_struct *work)
433{
434 debug_object_free(work, &work_debug_descr);
435}
436EXPORT_SYMBOL_GPL(destroy_work_on_stack);
437
438#else
439static inline void debug_work_activate(struct work_struct *work) { }
440static inline void debug_work_deactivate(struct work_struct *work) { }
441#endif
442
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100443/* Serializes the accesses to the list of workqueues. */
444static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200446static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Oleg Nesterov14441962007-05-23 13:57:57 -0700448/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200449 * The almighty global cpu workqueues. nr_running is the only field
450 * which is expected to be used frequently by other cpus via
451 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700452 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200453static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200454static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800455
Tejun Heof3421792010-07-02 10:03:51 +0200456/*
457 * Global cpu workqueue and nr_running counter for unbound gcwq. The
458 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
459 * workers have WORKER_UNBOUND set.
460 */
461static struct global_cwq unbound_global_cwq;
462static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
463
Tejun Heoc34056a2010-06-29 10:07:11 +0200464static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Tejun Heo8b03ae32010-06-29 10:07:12 +0200466static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Tejun Heof3421792010-07-02 10:03:51 +0200468 if (cpu != WORK_CPU_UNBOUND)
469 return &per_cpu(global_cwq, cpu);
470 else
471 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Tejun Heob0f31fe2012-07-12 14:46:37 -0700474static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700475{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700476 int cpu = pool->gcwq->cpu;
477
Tejun Heof3421792010-07-02 10:03:51 +0200478 if (cpu != WORK_CPU_UNBOUND)
479 return &per_cpu(gcwq_nr_running, cpu);
480 else
481 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700482}
483
Tejun Heo4690c4a2010-06-29 10:07:10 +0200484static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
485 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700486{
Tejun Heof3421792010-07-02 10:03:51 +0200487 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800488 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200489 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200490 } else if (likely(cpu == WORK_CPU_UNBOUND))
491 return wq->cpu_wq.single;
492 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700493}
494
Tejun Heo73f53c42010-06-29 10:07:11 +0200495static unsigned int work_color_to_flags(int color)
496{
497 return color << WORK_STRUCT_COLOR_SHIFT;
498}
499
500static int get_work_color(struct work_struct *work)
501{
502 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
503 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
504}
505
506static int work_next_color(int color)
507{
508 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
David Howells4594bf12006-12-07 11:33:26 +0000511/*
Tejun Heoe1201532010-07-22 14:14:25 +0200512 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
513 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
514 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200515 *
516 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
517 * cwq, cpu or clear work->data. These functions should only be
518 * called while the work is owned - ie. while the PENDING bit is set.
519 *
520 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
521 * corresponding to a work. gcwq is available once the work has been
522 * queued anywhere after initialization. cwq is available only from
523 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000524 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200525static inline void set_work_data(struct work_struct *work, unsigned long data,
526 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000527{
David Howells4594bf12006-12-07 11:33:26 +0000528 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000530}
David Howells365970a2006-11-22 14:54:49 +0000531
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532static void set_work_cwq(struct work_struct *work,
533 struct cpu_workqueue_struct *cwq,
534 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200535{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200536 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200537 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200538}
539
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000541{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
543}
544
545static void clear_work_data(struct work_struct *work)
546{
547 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
548}
549
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
551{
Tejun Heoe1201532010-07-22 14:14:25 +0200552 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553
Tejun Heoe1201532010-07-22 14:14:25 +0200554 if (data & WORK_STRUCT_CWQ)
555 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
556 else
557 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558}
559
560static struct global_cwq *get_work_gcwq(struct work_struct *work)
561{
Tejun Heoe1201532010-07-22 14:14:25 +0200562 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200563 unsigned int cpu;
564
Tejun Heoe1201532010-07-22 14:14:25 +0200565 if (data & WORK_STRUCT_CWQ)
566 return ((struct cpu_workqueue_struct *)
Tejun Heo47303942012-07-12 14:46:37 -0700567 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568
569 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200570 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571 return NULL;
572
Tejun Heof3421792010-07-02 10:03:51 +0200573 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000575}
576
577/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200578 * Policy functions. These define the policies on how the global
579 * worker pool is managed. Unless noted otherwise, these functions
580 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000581 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200582
Tejun Heob0f31fe2012-07-12 14:46:37 -0700583static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000584{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700585 return !atomic_read(get_pool_nr_running(pool)) ||
586 pool->gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000587}
588
Tejun Heoe22bee72010-06-29 10:07:14 +0200589/*
590 * Need to wake up a worker? Called from anything but currently
591 * running workers.
Tejun Heo0d8a2ff2012-07-12 14:46:37 -0700592 *
593 * Note that, because unbound workers never contribute to nr_running, this
594 * function will always return %true for unbound gcwq as long as the
595 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200596 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700597static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000598{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700599 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000600}
601
Tejun Heoe22bee72010-06-29 10:07:14 +0200602/* Can I start working? Called from busy but !running workers. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700603static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200604{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700605 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200606}
607
608/* Do I need to keep working? Called from currently running workers. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700609static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200610{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700611 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200612
Tejun Heob0f31fe2012-07-12 14:46:37 -0700613 return !list_empty(&pool->worklist) &&
Tejun Heo30310042010-10-11 11:51:57 +0200614 (atomic_read(nr_running) <= 1 ||
Tejun Heob0f31fe2012-07-12 14:46:37 -0700615 pool->gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200616}
617
618/* Do we need a new worker? Called from manager. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700619static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200620{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700621 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200622}
623
624/* Do I need to be the manager? */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700625static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200626{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700627 return need_to_create_worker(pool) ||
628 pool->gcwq->flags & GCWQ_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +0200629}
630
631/* Do we have too many workers and should some go away? */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700632static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200633{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700634 bool managing = pool->gcwq->flags & GCWQ_MANAGING_WORKERS;
635 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
636 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200637
638 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
639}
640
641/*
642 * Wake up functions.
643 */
644
Tejun Heo7e116292010-06-29 10:07:13 +0200645/* Return the first worker. Safe with preemption disabled */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700646static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200647{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700648 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200649 return NULL;
650
Tejun Heob0f31fe2012-07-12 14:46:37 -0700651 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200652}
653
654/**
655 * wake_up_worker - wake up an idle worker
Tejun Heob0f31fe2012-07-12 14:46:37 -0700656 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200657 *
Tejun Heob0f31fe2012-07-12 14:46:37 -0700658 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200659 *
660 * CONTEXT:
661 * spin_lock_irq(gcwq->lock).
662 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700663static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200664{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700665 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200666
667 if (likely(worker))
668 wake_up_process(worker->task);
669}
670
Tejun Heo4690c4a2010-06-29 10:07:10 +0200671/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200672 * wq_worker_waking_up - a worker is waking up
673 * @task: task waking up
674 * @cpu: CPU @task is waking up to
675 *
676 * This function is called during try_to_wake_up() when a worker is
677 * being awoken.
678 *
679 * CONTEXT:
680 * spin_lock_irq(rq->lock)
681 */
682void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
683{
684 struct worker *worker = kthread_data(task);
685
Steven Rostedt2d646722010-12-03 23:12:33 -0500686 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700687 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200688}
689
690/**
691 * wq_worker_sleeping - a worker is going to sleep
692 * @task: task going to sleep
693 * @cpu: CPU in question, must be the current CPU number
694 *
695 * This function is called during schedule() when a busy worker is
696 * going to sleep. Worker on the same cpu can be woken up by
697 * returning pointer to its task.
698 *
699 * CONTEXT:
700 * spin_lock_irq(rq->lock)
701 *
702 * RETURNS:
703 * Worker task on @cpu to wake up, %NULL if none.
704 */
705struct task_struct *wq_worker_sleeping(struct task_struct *task,
706 unsigned int cpu)
707{
708 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo47303942012-07-12 14:46:37 -0700709 struct worker_pool *pool = worker->pool;
Tejun Heob0f31fe2012-07-12 14:46:37 -0700710 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200711
Steven Rostedt2d646722010-12-03 23:12:33 -0500712 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200713 return NULL;
714
715 /* this can only happen on the local cpu */
716 BUG_ON(cpu != raw_smp_processor_id());
717
718 /*
719 * The counterpart of the following dec_and_test, implied mb,
720 * worklist not empty test sequence is in insert_work().
721 * Please read comment there.
722 *
723 * NOT_RUNNING is clear. This means that trustee is not in
724 * charge and we're running on the local cpu w/ rq lock held
725 * and preemption disabled, which in turn means that none else
726 * could be manipulating idle_list, so dereferencing idle_list
727 * without gcwq lock is safe.
728 */
Tejun Heo47303942012-07-12 14:46:37 -0700729 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700730 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200731 return to_wakeup ? to_wakeup->task : NULL;
732}
733
734/**
735 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200736 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200737 * @flags: flags to set
738 * @wakeup: wakeup an idle worker if necessary
739 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200740 * Set @flags in @worker->flags and adjust nr_running accordingly. If
741 * nr_running becomes zero and @wakeup is %true, an idle worker is
742 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200743 *
Tejun Heocb444762010-07-02 10:03:50 +0200744 * CONTEXT:
745 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200746 */
747static inline void worker_set_flags(struct worker *worker, unsigned int flags,
748 bool wakeup)
749{
Tejun Heo47303942012-07-12 14:46:37 -0700750 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200751
Tejun Heocb444762010-07-02 10:03:50 +0200752 WARN_ON_ONCE(worker->task != current);
753
Tejun Heoe22bee72010-06-29 10:07:14 +0200754 /*
755 * If transitioning into NOT_RUNNING, adjust nr_running and
756 * wake up an idle worker as necessary if requested by
757 * @wakeup.
758 */
759 if ((flags & WORKER_NOT_RUNNING) &&
760 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heob0f31fe2012-07-12 14:46:37 -0700761 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200762
763 if (wakeup) {
764 if (atomic_dec_and_test(nr_running) &&
Tejun Heo47303942012-07-12 14:46:37 -0700765 !list_empty(&pool->worklist))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700766 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200767 } else
768 atomic_dec(nr_running);
769 }
770
Tejun Heod302f012010-06-29 10:07:13 +0200771 worker->flags |= flags;
772}
773
774/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200775 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200776 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200777 * @flags: flags to clear
778 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200779 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200780 *
Tejun Heocb444762010-07-02 10:03:50 +0200781 * CONTEXT:
782 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200783 */
784static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
785{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700786 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200787 unsigned int oflags = worker->flags;
788
Tejun Heocb444762010-07-02 10:03:50 +0200789 WARN_ON_ONCE(worker->task != current);
790
Tejun Heod302f012010-06-29 10:07:13 +0200791 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200792
Tejun Heo42c025f2011-01-11 15:58:49 +0100793 /*
794 * If transitioning out of NOT_RUNNING, increment nr_running. Note
795 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
796 * of multiple flags, not a single flag.
797 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200798 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
799 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700800 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200801}
802
803/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200804 * busy_worker_head - return the busy hash head for a work
805 * @gcwq: gcwq of interest
806 * @work: work to be hashed
807 *
808 * Return hash head of @gcwq for @work.
809 *
810 * CONTEXT:
811 * spin_lock_irq(gcwq->lock).
812 *
813 * RETURNS:
814 * Pointer to the hash head.
815 */
816static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
817 struct work_struct *work)
818{
819 const int base_shift = ilog2(sizeof(struct work_struct));
820 unsigned long v = (unsigned long)work;
821
822 /* simple shift and fold hash, do we need something better? */
823 v >>= base_shift;
824 v += v >> BUSY_WORKER_HASH_ORDER;
825 v &= BUSY_WORKER_HASH_MASK;
826
827 return &gcwq->busy_hash[v];
828}
829
830/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200831 * __find_worker_executing_work - find worker which is executing a work
832 * @gcwq: gcwq of interest
833 * @bwh: hash head as returned by busy_worker_head()
834 * @work: work to find worker for
835 *
836 * Find a worker which is executing @work on @gcwq. @bwh should be
837 * the hash head obtained by calling busy_worker_head() with the same
838 * work.
839 *
840 * CONTEXT:
841 * spin_lock_irq(gcwq->lock).
842 *
843 * RETURNS:
844 * Pointer to worker which is executing @work if found, NULL
845 * otherwise.
846 */
847static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
848 struct hlist_head *bwh,
849 struct work_struct *work)
850{
851 struct worker *worker;
852 struct hlist_node *tmp;
853
854 hlist_for_each_entry(worker, tmp, bwh, hentry)
855 if (worker->current_work == work)
856 return worker;
857 return NULL;
858}
859
860/**
861 * find_worker_executing_work - find worker which is executing a work
862 * @gcwq: gcwq of interest
863 * @work: work to find worker for
864 *
865 * Find a worker which is executing @work on @gcwq. This function is
866 * identical to __find_worker_executing_work() except that this
867 * function calculates @bwh itself.
868 *
869 * CONTEXT:
870 * spin_lock_irq(gcwq->lock).
871 *
872 * RETURNS:
873 * Pointer to worker which is executing @work if found, NULL
874 * otherwise.
875 */
876static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
877 struct work_struct *work)
878{
879 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
880 work);
881}
882
883/**
Tejun Heob0f31fe2012-07-12 14:46:37 -0700884 * pool_determine_ins_pos - find insertion position
885 * @pool: pool of interest
Tejun Heo649027d2010-06-29 10:07:14 +0200886 * @cwq: cwq a work is being queued for
887 *
Tejun Heob0f31fe2012-07-12 14:46:37 -0700888 * A work for @cwq is about to be queued on @pool, determine insertion
Tejun Heo649027d2010-06-29 10:07:14 +0200889 * position for the work. If @cwq is for HIGHPRI wq, the work is
890 * queued at the head of the queue but in FIFO order with respect to
891 * other HIGHPRI works; otherwise, at the end of the queue. This
Tejun Heob0f31fe2012-07-12 14:46:37 -0700892 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @pool that
Tejun Heo649027d2010-06-29 10:07:14 +0200893 * there are HIGHPRI works pending.
894 *
895 * CONTEXT:
896 * spin_lock_irq(gcwq->lock).
897 *
898 * RETURNS:
899 * Pointer to inserstion position.
900 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700901static inline struct list_head *pool_determine_ins_pos(struct worker_pool *pool,
Tejun Heo649027d2010-06-29 10:07:14 +0200902 struct cpu_workqueue_struct *cwq)
903{
904 struct work_struct *twork;
905
906 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700907 return &pool->worklist;
Tejun Heo649027d2010-06-29 10:07:14 +0200908
Tejun Heob0f31fe2012-07-12 14:46:37 -0700909 list_for_each_entry(twork, &pool->worklist, entry) {
Tejun Heo649027d2010-06-29 10:07:14 +0200910 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
911
912 if (!(tcwq->wq->flags & WQ_HIGHPRI))
913 break;
914 }
915
Tejun Heob0f31fe2012-07-12 14:46:37 -0700916 pool->gcwq->flags |= GCWQ_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +0200917 return &twork->entry;
918}
919
920/**
Tejun Heo7e116292010-06-29 10:07:13 +0200921 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200922 * @cwq: cwq @work belongs to
923 * @work: work to insert
924 * @head: insertion point
925 * @extra_flags: extra WORK_STRUCT_* flags to set
926 *
Tejun Heo7e116292010-06-29 10:07:13 +0200927 * Insert @work which belongs to @cwq into @gcwq after @head.
928 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200929 *
930 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200931 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200932 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700933static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200934 struct work_struct *work, struct list_head *head,
935 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700936{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700937 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100938
Tejun Heo4690c4a2010-06-29 10:07:10 +0200939 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200940 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200941
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700942 /*
943 * Ensure that we get the right work->data if we see the
944 * result of list_add() below, see try_to_grab_pending().
945 */
946 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200947
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700948 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200949
950 /*
951 * Ensure either worker_sched_deactivated() sees the above
952 * list_add_tail() or we see zero nr_running to avoid workers
953 * lying around lazily while there are works to be processed.
954 */
955 smp_mb();
956
Tejun Heob0f31fe2012-07-12 14:46:37 -0700957 if (__need_more_worker(pool))
958 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700959}
960
Tejun Heoc8efcc22010-12-20 19:32:04 +0100961/*
962 * Test whether @work is being queued from another work executing on the
963 * same workqueue. This is rather expensive and should only be used from
964 * cold paths.
965 */
966static bool is_chained_work(struct workqueue_struct *wq)
967{
968 unsigned long flags;
969 unsigned int cpu;
970
971 for_each_gcwq_cpu(cpu) {
972 struct global_cwq *gcwq = get_gcwq(cpu);
973 struct worker *worker;
974 struct hlist_node *pos;
975 int i;
976
977 spin_lock_irqsave(&gcwq->lock, flags);
978 for_each_busy_worker(worker, i, pos, gcwq) {
979 if (worker->task != current)
980 continue;
981 spin_unlock_irqrestore(&gcwq->lock, flags);
982 /*
983 * I'm @worker, no locking necessary. See if @work
984 * is headed to the same workqueue.
985 */
986 return worker->current_cwq->wq == wq;
987 }
988 spin_unlock_irqrestore(&gcwq->lock, flags);
989 }
990 return false;
991}
992
Tejun Heo4690c4a2010-06-29 10:07:10 +0200993static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct work_struct *work)
995{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200996 struct global_cwq *gcwq;
997 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200998 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200999 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned long flags;
1001
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001002 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001003
Tejun Heoc8efcc22010-12-20 19:32:04 +01001004 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001005 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001006 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001007 return;
1008
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001009 /* determine gcwq to use */
1010 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001011 struct global_cwq *last_gcwq;
1012
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001013 if (unlikely(cpu == WORK_CPU_UNBOUND))
1014 cpu = raw_smp_processor_id();
1015
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001016 /*
1017 * It's multi cpu. If @wq is non-reentrant and @work
1018 * was previously on a different cpu, it might still
1019 * be running there, in which case the work needs to
1020 * be queued on that cpu to guarantee non-reentrance.
1021 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001022 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001023 if (wq->flags & WQ_NON_REENTRANT &&
1024 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1025 struct worker *worker;
1026
1027 spin_lock_irqsave(&last_gcwq->lock, flags);
1028
1029 worker = find_worker_executing_work(last_gcwq, work);
1030
1031 if (worker && worker->current_cwq->wq == wq)
1032 gcwq = last_gcwq;
1033 else {
1034 /* meh... not running there, queue here */
1035 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1036 spin_lock_irqsave(&gcwq->lock, flags);
1037 }
1038 } else
1039 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001040 } else {
1041 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1042 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001043 }
1044
1045 /* gcwq determined, get cwq and queue */
1046 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001047 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001048
Tejun Heo4690c4a2010-06-29 10:07:10 +02001049 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001050
Tejun Heo73f53c42010-06-29 10:07:11 +02001051 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001052 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001053
1054 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001055 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056 cwq->nr_active++;
Tejun Heob0f31fe2012-07-12 14:46:37 -07001057 worklist = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001058 } else {
1059 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001060 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001061 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001063 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001064
Tejun Heo8b03ae32010-06-29 10:07:12 +02001065 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001068/**
1069 * queue_work - queue work on a workqueue
1070 * @wq: workqueue to use
1071 * @work: work to queue
1072 *
Alan Stern057647f2006-10-28 10:38:58 -07001073 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001075 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1076 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001078int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001080 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001082 ret = queue_work_on(get_cpu(), wq, work);
1083 put_cpu();
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return ret;
1086}
Dave Jonesae90dd52006-06-30 01:40:45 -04001087EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Zhang Ruic1a220e2008-07-23 21:28:39 -07001089/**
1090 * queue_work_on - queue work on specific cpu
1091 * @cpu: CPU number to execute work on
1092 * @wq: workqueue to use
1093 * @work: work to queue
1094 *
1095 * Returns 0 if @work was already on a queue, non-zero otherwise.
1096 *
1097 * We queue the work to a specific CPU, the caller must ensure it
1098 * can't go away.
1099 */
1100int
1101queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1102{
1103 int ret = 0;
1104
Tejun Heo22df02b2010-06-29 10:07:10 +02001105 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001106 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001107 ret = 1;
1108 }
1109 return ret;
1110}
1111EXPORT_SYMBOL_GPL(queue_work_on);
1112
Li Zefan6d141c32008-02-08 04:21:09 -08001113static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
David Howells52bad642006-11-22 14:54:01 +00001115 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001116 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Tejun Heo4690c4a2010-06-29 10:07:10 +02001118 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001121/**
1122 * queue_delayed_work - queue work on a workqueue after delay
1123 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001124 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001125 * @delay: number of jiffies to wait before queueing
1126 *
Alan Stern057647f2006-10-28 10:38:58 -07001127 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001128 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001129int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001130 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
David Howells52bad642006-11-22 14:54:01 +00001132 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001133 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001135 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
Dave Jonesae90dd52006-06-30 01:40:45 -04001137EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001139/**
1140 * queue_delayed_work_on - queue work on specific CPU after delay
1141 * @cpu: CPU number to execute work on
1142 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001143 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001144 * @delay: number of jiffies to wait before queueing
1145 *
Alan Stern057647f2006-10-28 10:38:58 -07001146 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001147 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001148int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001149 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001150{
1151 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001152 struct timer_list *timer = &dwork->timer;
1153 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001154
Tejun Heo22df02b2010-06-29 10:07:10 +02001155 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001156 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001157
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001158 BUG_ON(timer_pending(timer));
1159 BUG_ON(!list_empty(&work->entry));
1160
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001161 timer_stats_timer_set_start_info(&dwork->timer);
1162
Tejun Heo7a22ad72010-06-29 10:07:13 +02001163 /*
1164 * This stores cwq for the moment, for the timer_fn.
1165 * Note that the work's gcwq is preserved to allow
1166 * reentrance detection for delayed works.
1167 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001168 if (!(wq->flags & WQ_UNBOUND)) {
1169 struct global_cwq *gcwq = get_work_gcwq(work);
1170
1171 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1172 lcpu = gcwq->cpu;
1173 else
1174 lcpu = raw_smp_processor_id();
1175 } else
1176 lcpu = WORK_CPU_UNBOUND;
1177
Tejun Heo7a22ad72010-06-29 10:07:13 +02001178 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001179
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001180 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001181 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001182 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001183
1184 if (unlikely(cpu >= 0))
1185 add_timer_on(timer, cpu);
1186 else
1187 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001188 ret = 1;
1189 }
1190 return ret;
1191}
Dave Jonesae90dd52006-06-30 01:40:45 -04001192EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Tejun Heoc8e55f32010-06-29 10:07:12 +02001194/**
1195 * worker_enter_idle - enter idle state
1196 * @worker: worker which is entering idle state
1197 *
1198 * @worker is entering idle state. Update stats and idle timer if
1199 * necessary.
1200 *
1201 * LOCKING:
1202 * spin_lock_irq(gcwq->lock).
1203 */
1204static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Tejun Heo47303942012-07-12 14:46:37 -07001206 struct worker_pool *pool = worker->pool;
1207 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Tejun Heoc8e55f32010-06-29 10:07:12 +02001209 BUG_ON(worker->flags & WORKER_IDLE);
1210 BUG_ON(!list_empty(&worker->entry) &&
1211 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Tejun Heocb444762010-07-02 10:03:50 +02001213 /* can't use worker_set_flags(), also called from start_worker() */
1214 worker->flags |= WORKER_IDLE;
Tejun Heo47303942012-07-12 14:46:37 -07001215 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001216 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001217
Tejun Heoc8e55f32010-06-29 10:07:12 +02001218 /* idle_list is LIFO */
Tejun Heo47303942012-07-12 14:46:37 -07001219 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001220
Tejun Heoe22bee72010-06-29 10:07:14 +02001221 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001222 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo47303942012-07-12 14:46:37 -07001223 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001224 jiffies + IDLE_WORKER_TIMEOUT);
1225 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001226 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001227
Tejun Heo776a17372012-05-14 15:04:50 -07001228 /*
1229 * Sanity check nr_running. Because trustee releases gcwq->lock
1230 * between setting %WORKER_ROGUE and zapping nr_running, the
1231 * warning may trigger spuriously. Check iff trustee is idle.
1232 */
1233 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo47303942012-07-12 14:46:37 -07001234 pool->nr_workers == pool->nr_idle &&
Tejun Heob0f31fe2012-07-12 14:46:37 -07001235 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Tejun Heoc8e55f32010-06-29 10:07:12 +02001238/**
1239 * worker_leave_idle - leave idle state
1240 * @worker: worker which is leaving idle state
1241 *
1242 * @worker is leaving idle state. Update stats.
1243 *
1244 * LOCKING:
1245 * spin_lock_irq(gcwq->lock).
1246 */
1247static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Tejun Heo47303942012-07-12 14:46:37 -07001249 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Tejun Heoc8e55f32010-06-29 10:07:12 +02001251 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001252 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo47303942012-07-12 14:46:37 -07001253 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001254 list_del_init(&worker->entry);
1255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Tejun Heoe22bee72010-06-29 10:07:14 +02001257/**
1258 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1259 * @worker: self
1260 *
1261 * Works which are scheduled while the cpu is online must at least be
1262 * scheduled to a worker which is bound to the cpu so that if they are
1263 * flushed from cpu callbacks while cpu is going down, they are
1264 * guaranteed to execute on the cpu.
1265 *
1266 * This function is to be used by rogue workers and rescuers to bind
1267 * themselves to the target cpu and may race with cpu going down or
1268 * coming online. kthread_bind() can't be used because it may put the
1269 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1270 * verbatim as it's best effort and blocking and gcwq may be
1271 * [dis]associated in the meantime.
1272 *
1273 * This function tries set_cpus_allowed() and locks gcwq and verifies
1274 * the binding against GCWQ_DISASSOCIATED which is set during
1275 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1276 * idle state or fetches works without dropping lock, it can guarantee
1277 * the scheduling requirement described in the first paragraph.
1278 *
1279 * CONTEXT:
1280 * Might sleep. Called without any lock but returns with gcwq->lock
1281 * held.
1282 *
1283 * RETURNS:
1284 * %true if the associated gcwq is online (@worker is successfully
1285 * bound), %false if offline.
1286 */
1287static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001288__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001289{
Tejun Heo47303942012-07-12 14:46:37 -07001290 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001291 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Tejun Heoe22bee72010-06-29 10:07:14 +02001293 while (true) {
1294 /*
1295 * The following call may fail, succeed or succeed
1296 * without actually migrating the task to the cpu if
1297 * it races with cpu hotunplug operation. Verify
1298 * against GCWQ_DISASSOCIATED.
1299 */
Tejun Heof3421792010-07-02 10:03:51 +02001300 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1301 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001302
Tejun Heoe22bee72010-06-29 10:07:14 +02001303 spin_lock_irq(&gcwq->lock);
1304 if (gcwq->flags & GCWQ_DISASSOCIATED)
1305 return false;
1306 if (task_cpu(task) == gcwq->cpu &&
1307 cpumask_equal(&current->cpus_allowed,
1308 get_cpu_mask(gcwq->cpu)))
1309 return true;
1310 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001311
Tejun Heo5035b202011-04-29 18:08:37 +02001312 /*
1313 * We've raced with CPU hot[un]plug. Give it a breather
1314 * and retry migration. cond_resched() is required here;
1315 * otherwise, we might deadlock against cpu_stop trying to
1316 * bring down the CPU on non-preemptive kernel.
1317 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001318 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001319 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001320 }
1321}
1322
1323/*
1324 * Function for worker->rebind_work used to rebind rogue busy workers
1325 * to the associated cpu which is coming back online. This is
1326 * scheduled by cpu up but can race with other cpu hotplug operations
1327 * and may be executed twice without intervening cpu down.
1328 */
1329static void worker_rebind_fn(struct work_struct *work)
1330{
1331 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo47303942012-07-12 14:46:37 -07001332 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001333
1334 if (worker_maybe_bind_and_lock(worker))
1335 worker_clr_flags(worker, WORKER_REBIND);
1336
1337 spin_unlock_irq(&gcwq->lock);
1338}
1339
Tejun Heoc34056a2010-06-29 10:07:11 +02001340static struct worker *alloc_worker(void)
1341{
1342 struct worker *worker;
1343
1344 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001345 if (worker) {
1346 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001347 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001348 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1349 /* on creation a worker is in !idle && prep state */
1350 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001351 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001352 return worker;
1353}
1354
1355/**
1356 * create_worker - create a new workqueue worker
Tejun Heob0f31fe2012-07-12 14:46:37 -07001357 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001358 * @bind: whether to set affinity to @cpu or not
1359 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001360 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001361 * can be started by calling start_worker() or destroyed using
1362 * destroy_worker().
1363 *
1364 * CONTEXT:
1365 * Might sleep. Does GFP_KERNEL allocations.
1366 *
1367 * RETURNS:
1368 * Pointer to the newly created worker.
1369 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001370static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001371{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001372 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001373 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001374 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001375 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001376
Tejun Heo8b03ae32010-06-29 10:07:12 +02001377 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001378 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001379 spin_unlock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001380 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001381 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001382 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001383 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001384 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001385
1386 worker = alloc_worker();
1387 if (!worker)
1388 goto fail;
1389
Tejun Heo47303942012-07-12 14:46:37 -07001390 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001391 worker->id = id;
1392
Tejun Heof3421792010-07-02 10:03:51 +02001393 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001394 worker->task = kthread_create_on_node(worker_thread,
1395 worker,
1396 cpu_to_node(gcwq->cpu),
1397 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001398 else
1399 worker->task = kthread_create(worker_thread, worker,
1400 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001401 if (IS_ERR(worker->task))
1402 goto fail;
1403
Tejun Heodb7bccf2010-06-29 10:07:12 +02001404 /*
1405 * A rogue worker will become a regular one if CPU comes
1406 * online later on. Make sure every worker has
1407 * PF_THREAD_BOUND set.
1408 */
Tejun Heof3421792010-07-02 10:03:51 +02001409 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001410 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001411 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001412 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001413 if (on_unbound_cpu)
1414 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001416
Tejun Heoc34056a2010-06-29 10:07:11 +02001417 return worker;
1418fail:
1419 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001420 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001421 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001422 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001423 }
1424 kfree(worker);
1425 return NULL;
1426}
1427
1428/**
1429 * start_worker - start a newly created worker
1430 * @worker: worker to start
1431 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001432 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001433 *
1434 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001435 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001436 */
1437static void start_worker(struct worker *worker)
1438{
Tejun Heocb444762010-07-02 10:03:50 +02001439 worker->flags |= WORKER_STARTED;
Tejun Heo47303942012-07-12 14:46:37 -07001440 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001441 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001442 wake_up_process(worker->task);
1443}
1444
1445/**
1446 * destroy_worker - destroy a workqueue worker
1447 * @worker: worker to be destroyed
1448 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001449 * Destroy @worker and adjust @gcwq stats accordingly.
1450 *
1451 * CONTEXT:
1452 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001453 */
1454static void destroy_worker(struct worker *worker)
1455{
Tejun Heo47303942012-07-12 14:46:37 -07001456 struct worker_pool *pool = worker->pool;
1457 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001458 int id = worker->id;
1459
1460 /* sanity check frenzy */
1461 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001462 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001463
Tejun Heoc8e55f32010-06-29 10:07:12 +02001464 if (worker->flags & WORKER_STARTED)
Tejun Heo47303942012-07-12 14:46:37 -07001465 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001466 if (worker->flags & WORKER_IDLE)
Tejun Heo47303942012-07-12 14:46:37 -07001467 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001468
1469 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001470 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001471
1472 spin_unlock_irq(&gcwq->lock);
1473
Tejun Heoc34056a2010-06-29 10:07:11 +02001474 kthread_stop(worker->task);
1475 kfree(worker);
1476
Tejun Heo8b03ae32010-06-29 10:07:12 +02001477 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001478 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001479}
1480
Tejun Heob0f31fe2012-07-12 14:46:37 -07001481static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001482{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001483 struct worker_pool *pool = (void *)__pool;
1484 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001485
1486 spin_lock_irq(&gcwq->lock);
1487
Tejun Heob0f31fe2012-07-12 14:46:37 -07001488 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001489 struct worker *worker;
1490 unsigned long expires;
1491
1492 /* idle_list is kept in LIFO order, check the last one */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001493 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001494 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1495
1496 if (time_before(jiffies, expires))
Tejun Heob0f31fe2012-07-12 14:46:37 -07001497 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001498 else {
1499 /* it's been idle for too long, wake up manager */
1500 gcwq->flags |= GCWQ_MANAGE_WORKERS;
Tejun Heob0f31fe2012-07-12 14:46:37 -07001501 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001502 }
1503 }
1504
1505 spin_unlock_irq(&gcwq->lock);
1506}
1507
1508static bool send_mayday(struct work_struct *work)
1509{
1510 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1511 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001512 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001513
1514 if (!(wq->flags & WQ_RESCUER))
1515 return false;
1516
1517 /* mayday mayday mayday */
Tejun Heo47303942012-07-12 14:46:37 -07001518 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001519 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1520 if (cpu == WORK_CPU_UNBOUND)
1521 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001522 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001523 wake_up_process(wq->rescuer->task);
1524 return true;
1525}
1526
Tejun Heob0f31fe2012-07-12 14:46:37 -07001527static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001528{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001529 struct worker_pool *pool = (void *)__pool;
1530 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001531 struct work_struct *work;
1532
1533 spin_lock_irq(&gcwq->lock);
1534
Tejun Heob0f31fe2012-07-12 14:46:37 -07001535 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 /*
1537 * We've been trying to create a new worker but
1538 * haven't been successful. We might be hitting an
1539 * allocation deadlock. Send distress signals to
1540 * rescuers.
1541 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001542 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001543 send_mayday(work);
1544 }
1545
1546 spin_unlock_irq(&gcwq->lock);
1547
Tejun Heob0f31fe2012-07-12 14:46:37 -07001548 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001549}
1550
1551/**
1552 * maybe_create_worker - create a new worker if necessary
Tejun Heob0f31fe2012-07-12 14:46:37 -07001553 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001555 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001556 * have at least one idle worker on return from this function. If
1557 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heob0f31fe2012-07-12 14:46:37 -07001558 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 * possible allocation deadlock.
1560 *
1561 * On return, need_to_create_worker() is guaranteed to be false and
1562 * may_start_working() true.
1563 *
1564 * LOCKING:
1565 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1566 * multiple times. Does GFP_KERNEL allocations. Called only from
1567 * manager.
1568 *
1569 * RETURNS:
1570 * false if no action was taken and gcwq->lock stayed locked, true
1571 * otherwise.
1572 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001573static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001574__releases(&gcwq->lock)
1575__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001576{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001577 struct global_cwq *gcwq = pool->gcwq;
1578
1579 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001580 return false;
1581restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001582 spin_unlock_irq(&gcwq->lock);
1583
Tejun Heoe22bee72010-06-29 10:07:14 +02001584 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001585 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001586
1587 while (true) {
1588 struct worker *worker;
1589
Tejun Heob0f31fe2012-07-12 14:46:37 -07001590 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 if (worker) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001592 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 spin_lock_irq(&gcwq->lock);
1594 start_worker(worker);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001595 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 return true;
1597 }
1598
Tejun Heob0f31fe2012-07-12 14:46:37 -07001599 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 break;
1601
Tejun Heoe22bee72010-06-29 10:07:14 +02001602 __set_current_state(TASK_INTERRUPTIBLE);
1603 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001604
Tejun Heob0f31fe2012-07-12 14:46:37 -07001605 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 break;
1607 }
1608
Tejun Heob0f31fe2012-07-12 14:46:37 -07001609 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 spin_lock_irq(&gcwq->lock);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001611 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 goto restart;
1613 return true;
1614}
1615
1616/**
1617 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heob0f31fe2012-07-12 14:46:37 -07001618 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001619 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001620 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 * IDLE_WORKER_TIMEOUT.
1622 *
1623 * LOCKING:
1624 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1625 * multiple times. Called only from manager.
1626 *
1627 * RETURNS:
1628 * false if no action was taken and gcwq->lock stayed locked, true
1629 * otherwise.
1630 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001631static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001632{
1633 bool ret = false;
1634
Tejun Heob0f31fe2012-07-12 14:46:37 -07001635 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001636 struct worker *worker;
1637 unsigned long expires;
1638
Tejun Heob0f31fe2012-07-12 14:46:37 -07001639 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001640 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1641
1642 if (time_before(jiffies, expires)) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001643 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001644 break;
1645 }
1646
1647 destroy_worker(worker);
1648 ret = true;
1649 }
1650
1651 return ret;
1652}
1653
1654/**
1655 * manage_workers - manage worker pool
1656 * @worker: self
1657 *
1658 * Assume the manager role and manage gcwq worker pool @worker belongs
1659 * to. At any given time, there can be only zero or one manager per
1660 * gcwq. The exclusion is handled automatically by this function.
1661 *
1662 * The caller can safely start processing works on false return. On
1663 * true return, it's guaranteed that need_to_create_worker() is false
1664 * and may_start_working() is true.
1665 *
1666 * CONTEXT:
1667 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1668 * multiple times. Does GFP_KERNEL allocations.
1669 *
1670 * RETURNS:
1671 * false if no action was taken and gcwq->lock stayed locked, true if
1672 * some action was taken.
1673 */
1674static bool manage_workers(struct worker *worker)
1675{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001676 struct worker_pool *pool = worker->pool;
1677 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001678 bool ret = false;
1679
1680 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1681 return ret;
1682
1683 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1684 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1685
1686 /*
1687 * Destroy and then create so that may_start_working() is true
1688 * on return.
1689 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001690 ret |= maybe_destroy_workers(pool);
1691 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001692
1693 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1694
1695 /*
1696 * The trustee might be waiting to take over the manager
1697 * position, tell it we're done.
1698 */
1699 if (unlikely(gcwq->trustee))
1700 wake_up_all(&gcwq->trustee_wait);
1701
1702 return ret;
1703}
1704
Tejun Heoa62428c2010-06-29 10:07:10 +02001705/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001706 * move_linked_works - move linked works to a list
1707 * @work: start of series of works to be scheduled
1708 * @head: target list to append @work to
1709 * @nextp: out paramter for nested worklist walking
1710 *
1711 * Schedule linked works starting from @work to @head. Work series to
1712 * be scheduled starts at @work and includes any consecutive work with
1713 * WORK_STRUCT_LINKED set in its predecessor.
1714 *
1715 * If @nextp is not NULL, it's updated to point to the next work of
1716 * the last scheduled work. This allows move_linked_works() to be
1717 * nested inside outer list_for_each_entry_safe().
1718 *
1719 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001720 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001721 */
1722static void move_linked_works(struct work_struct *work, struct list_head *head,
1723 struct work_struct **nextp)
1724{
1725 struct work_struct *n;
1726
1727 /*
1728 * Linked worklist will always end before the end of the list,
1729 * use NULL for list head.
1730 */
1731 list_for_each_entry_safe_from(work, n, NULL, entry) {
1732 list_move_tail(&work->entry, head);
1733 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1734 break;
1735 }
1736
1737 /*
1738 * If we're already inside safe list traversal and have moved
1739 * multiple works to the scheduled queue, the next position
1740 * needs to be updated.
1741 */
1742 if (nextp)
1743 *nextp = n;
1744}
1745
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001746static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1747{
1748 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1749 struct work_struct, entry);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001750 struct list_head *pos = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001751
Tejun Heocdadf002010-10-05 10:49:55 +02001752 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001753 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001754 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001755 cwq->nr_active++;
1756}
1757
Tejun Heoaffee4b2010-06-29 10:07:12 +02001758/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001759 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1760 * @cwq: cwq of interest
1761 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001762 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001763 *
1764 * A work either has completed or is removed from pending queue,
1765 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1766 *
1767 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001768 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001769 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001770static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1771 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001772{
1773 /* ignore uncolored works */
1774 if (color == WORK_NO_COLOR)
1775 return;
1776
1777 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001778
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001779 if (!delayed) {
1780 cwq->nr_active--;
1781 if (!list_empty(&cwq->delayed_works)) {
1782 /* one down, submit a delayed one */
1783 if (cwq->nr_active < cwq->max_active)
1784 cwq_activate_first_delayed(cwq);
1785 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001786 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001787
1788 /* is flush in progress and are we at the flushing tip? */
1789 if (likely(cwq->flush_color != color))
1790 return;
1791
1792 /* are there still in-flight works? */
1793 if (cwq->nr_in_flight[color])
1794 return;
1795
1796 /* this cwq is done, clear flush_color */
1797 cwq->flush_color = -1;
1798
1799 /*
1800 * If this was the last cwq, wake up the first flusher. It
1801 * will handle the rest.
1802 */
1803 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1804 complete(&cwq->wq->first_flusher->done);
1805}
1806
1807/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001808 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001809 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001810 * @work: work to process
1811 *
1812 * Process @work. This function contains all the logics necessary to
1813 * process a single work including synchronization against and
1814 * interaction with other workers on the same cpu, queueing and
1815 * flushing. As long as context requirement is met, any worker can
1816 * call this function to process a work.
1817 *
1818 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001819 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001820 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001821static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001822__releases(&gcwq->lock)
1823__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001824{
Tejun Heo7e116292010-06-29 10:07:13 +02001825 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo47303942012-07-12 14:46:37 -07001826 struct worker_pool *pool = worker->pool;
1827 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001828 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001829 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001830 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001831 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001832 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001833#ifdef CONFIG_LOCKDEP
1834 /*
1835 * It is permissible to free the struct work_struct from
1836 * inside the function that is called from it, this we need to
1837 * take into account for lockdep too. To avoid bogus "held
1838 * lock freed" warnings as well as problems when looking into
1839 * work->lockdep_map, make a copy and use that here.
1840 */
1841 struct lockdep_map lockdep_map = work->lockdep_map;
1842#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001843 /*
1844 * A single work shouldn't be executed concurrently by
1845 * multiple workers on a single cpu. Check whether anyone is
1846 * already processing the work. If so, defer the work to the
1847 * currently executing one.
1848 */
1849 collision = __find_worker_executing_work(gcwq, bwh, work);
1850 if (unlikely(collision)) {
1851 move_linked_works(work, &collision->scheduled, NULL);
1852 return;
1853 }
1854
Tejun Heoa62428c2010-06-29 10:07:10 +02001855 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001856 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001857 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001858 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001859 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001860 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001861
Tejun Heo7a22ad72010-06-29 10:07:13 +02001862 /* record the current cpu number in the work data and dequeue */
1863 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001864 list_del_init(&work->entry);
1865
Tejun Heo649027d2010-06-29 10:07:14 +02001866 /*
1867 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1868 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1869 */
1870 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
Tejun Heo47303942012-07-12 14:46:37 -07001871 struct work_struct *nwork = list_first_entry(&pool->worklist,
1872 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001873
Tejun Heo47303942012-07-12 14:46:37 -07001874 if (!list_empty(&pool->worklist) &&
Tejun Heo649027d2010-06-29 10:07:14 +02001875 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
Tejun Heob0f31fe2012-07-12 14:46:37 -07001876 wake_up_worker(pool);
Tejun Heo649027d2010-06-29 10:07:14 +02001877 else
1878 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1879 }
1880
Tejun Heofb0e7be2010-06-29 10:07:15 +02001881 /*
1882 * CPU intensive works don't participate in concurrency
1883 * management. They're the scheduler's responsibility.
1884 */
1885 if (unlikely(cpu_intensive))
1886 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1887
Tejun Heo0d8a2ff2012-07-12 14:46:37 -07001888 /*
1889 * Unbound gcwq isn't concurrency managed and work items should be
1890 * executed ASAP. Wake up another worker if necessary.
1891 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001892 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1893 wake_up_worker(pool);
Tejun Heo0d8a2ff2012-07-12 14:46:37 -07001894
Tejun Heo8b03ae32010-06-29 10:07:12 +02001895 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001896
Tejun Heoa62428c2010-06-29 10:07:10 +02001897 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001898 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001899 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001900 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001901 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001902 /*
1903 * While we must be careful to not use "work" after this, the trace
1904 * point will only record its address.
1905 */
1906 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001907 lock_map_release(&lockdep_map);
1908 lock_map_release(&cwq->wq->lockdep_map);
1909
1910 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1911 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1912 "%s/0x%08x/%d\n",
1913 current->comm, preempt_count(), task_pid_nr(current));
1914 printk(KERN_ERR " last function: ");
1915 print_symbol("%s\n", (unsigned long)f);
1916 debug_show_held_locks(current);
1917 dump_stack();
1918 }
1919
Tejun Heo8b03ae32010-06-29 10:07:12 +02001920 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001921
Tejun Heofb0e7be2010-06-29 10:07:15 +02001922 /* clear cpu intensive status */
1923 if (unlikely(cpu_intensive))
1924 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1925
Tejun Heoa62428c2010-06-29 10:07:10 +02001926 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001927 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001928 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001929 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001930 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001931}
1932
Tejun Heoaffee4b2010-06-29 10:07:12 +02001933/**
1934 * process_scheduled_works - process scheduled works
1935 * @worker: self
1936 *
1937 * Process all scheduled works. Please note that the scheduled list
1938 * may change while processing a work, so this function repeatedly
1939 * fetches a work from the top and executes it.
1940 *
1941 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001942 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001943 * multiple times.
1944 */
1945static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001947 while (!list_empty(&worker->scheduled)) {
1948 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001950 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Tejun Heo4690c4a2010-06-29 10:07:10 +02001954/**
1955 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001956 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001957 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 * The gcwq worker thread function. There's a single dynamic pool of
1959 * these per each cpu. These workers process all works regardless of
1960 * their specific target workqueue. The only exception is works which
1961 * belong to workqueues with a rescuer which will be explained in
1962 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001963 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001964static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Tejun Heoc34056a2010-06-29 10:07:11 +02001966 struct worker *worker = __worker;
Tejun Heo47303942012-07-12 14:46:37 -07001967 struct worker_pool *pool = worker->pool;
1968 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 /* tell the scheduler that this is a workqueue worker */
1971 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001972woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001973 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Tejun Heoc8e55f32010-06-29 10:07:12 +02001975 /* DIE can be set only while we're idle, checking here is enough */
1976 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001977 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981
Tejun Heoc8e55f32010-06-29 10:07:12 +02001982 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001983recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 /* no more worker necessary? */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001985 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 goto sleep;
1987
1988 /* do we need to manage? */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001989 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 goto recheck;
1991
Tejun Heoc8e55f32010-06-29 10:07:12 +02001992 /*
1993 * ->scheduled list can only be filled while a worker is
1994 * preparing to process a work or actually processing it.
1995 * Make sure nobody diddled with it while I was sleeping.
1996 */
1997 BUG_ON(!list_empty(&worker->scheduled));
1998
Tejun Heoe22bee72010-06-29 10:07:14 +02001999 /*
2000 * When control reaches this point, we're guaranteed to have
2001 * at least one idle worker or that someone else has already
2002 * assumed the manager role.
2003 */
2004 worker_clr_flags(worker, WORKER_PREP);
2005
2006 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002007 struct work_struct *work =
Tejun Heo47303942012-07-12 14:46:37 -07002008 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002009 struct work_struct, entry);
2010
2011 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2012 /* optimization path, not strictly necessary */
2013 process_one_work(worker, work);
2014 if (unlikely(!list_empty(&worker->scheduled)))
2015 process_scheduled_works(worker);
2016 } else {
2017 move_linked_works(work, &worker->scheduled, NULL);
2018 process_scheduled_works(worker);
2019 }
Tejun Heob0f31fe2012-07-12 14:46:37 -07002020 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002021
Tejun Heoe22bee72010-06-29 10:07:14 +02002022 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002023sleep:
Tejun Heob0f31fe2012-07-12 14:46:37 -07002024 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002026
Tejun Heoc8e55f32010-06-29 10:07:12 +02002027 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 * gcwq->lock is held and there's no work to process and no
2029 * need to manage, sleep. Workers are woken up only while
2030 * holding gcwq->lock or from local cpu, so setting the
2031 * current state before releasing gcwq->lock is enough to
2032 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002033 */
2034 worker_enter_idle(worker);
2035 __set_current_state(TASK_INTERRUPTIBLE);
2036 spin_unlock_irq(&gcwq->lock);
2037 schedule();
2038 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Tejun Heoe22bee72010-06-29 10:07:14 +02002041/**
2042 * rescuer_thread - the rescuer thread function
2043 * @__wq: the associated workqueue
2044 *
2045 * Workqueue rescuer thread function. There's one rescuer for each
2046 * workqueue which has WQ_RESCUER set.
2047 *
2048 * Regular work processing on a gcwq may block trying to create a new
2049 * worker which uses GFP_KERNEL allocation which has slight chance of
2050 * developing into deadlock if some works currently on the same queue
2051 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2052 * the problem rescuer solves.
2053 *
2054 * When such condition is possible, the gcwq summons rescuers of all
2055 * workqueues which have works queued on the gcwq and let them process
2056 * those works so that forward progress can be guaranteed.
2057 *
2058 * This should happen rarely.
2059 */
2060static int rescuer_thread(void *__wq)
2061{
2062 struct workqueue_struct *wq = __wq;
2063 struct worker *rescuer = wq->rescuer;
2064 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002065 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002066 unsigned int cpu;
2067
2068 set_user_nice(current, RESCUER_NICE_LEVEL);
2069repeat:
2070 set_current_state(TASK_INTERRUPTIBLE);
2071
2072 if (kthread_should_stop())
2073 return 0;
2074
Tejun Heof3421792010-07-02 10:03:51 +02002075 /*
2076 * See whether any cpu is asking for help. Unbounded
2077 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2078 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002079 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002080 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2081 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo47303942012-07-12 14:46:37 -07002082 struct worker_pool *pool = cwq->pool;
2083 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002084 struct work_struct *work, *n;
2085
2086 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002087 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002088
2089 /* migrate to the target cpu if possible */
Tejun Heo47303942012-07-12 14:46:37 -07002090 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002091 worker_maybe_bind_and_lock(rescuer);
2092
2093 /*
2094 * Slurp in all works issued via this workqueue and
2095 * process'em.
2096 */
2097 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo47303942012-07-12 14:46:37 -07002098 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002099 if (get_work_cwq(work) == cwq)
2100 move_linked_works(work, scheduled, &n);
2101
2102 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002103
2104 /*
2105 * Leave this gcwq. If keep_working() is %true, notify a
2106 * regular worker; otherwise, we end up with 0 concurrency
2107 * and stalling the execution.
2108 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07002109 if (keep_working(pool))
2110 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002111
Tejun Heoe22bee72010-06-29 10:07:14 +02002112 spin_unlock_irq(&gcwq->lock);
2113 }
2114
2115 schedule();
2116 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002119struct wq_barrier {
2120 struct work_struct work;
2121 struct completion done;
2122};
2123
2124static void wq_barrier_func(struct work_struct *work)
2125{
2126 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2127 complete(&barr->done);
2128}
2129
Tejun Heo4690c4a2010-06-29 10:07:10 +02002130/**
2131 * insert_wq_barrier - insert a barrier work
2132 * @cwq: cwq to insert barrier into
2133 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002134 * @target: target work to attach @barr to
2135 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002136 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002137 * @barr is linked to @target such that @barr is completed only after
2138 * @target finishes execution. Please note that the ordering
2139 * guarantee is observed only with respect to @target and on the local
2140 * cpu.
2141 *
2142 * Currently, a queued barrier can't be canceled. This is because
2143 * try_to_grab_pending() can't determine whether the work to be
2144 * grabbed is at the head of the queue and thus can't clear LINKED
2145 * flag of the previous work while there must be a valid next work
2146 * after a work with LINKED flag set.
2147 *
2148 * Note that when @worker is non-NULL, @target may be modified
2149 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002150 *
2151 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002152 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002153 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002154static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002155 struct wq_barrier *barr,
2156 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002157{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002158 struct list_head *head;
2159 unsigned int linked = 0;
2160
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002161 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002162 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002163 * as we know for sure that this will not trigger any of the
2164 * checks and call back into the fixup functions where we
2165 * might deadlock.
2166 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002167 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002168 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002169 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002170
Tejun Heoaffee4b2010-06-29 10:07:12 +02002171 /*
2172 * If @target is currently being executed, schedule the
2173 * barrier to the worker; otherwise, put it after @target.
2174 */
2175 if (worker)
2176 head = worker->scheduled.next;
2177 else {
2178 unsigned long *bits = work_data_bits(target);
2179
2180 head = target->entry.next;
2181 /* there can already be other linked works, inherit and set */
2182 linked = *bits & WORK_STRUCT_LINKED;
2183 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2184 }
2185
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002186 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002187 insert_work(cwq, &barr->work, head,
2188 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002189}
2190
Tejun Heo73f53c42010-06-29 10:07:11 +02002191/**
2192 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2193 * @wq: workqueue being flushed
2194 * @flush_color: new flush color, < 0 for no-op
2195 * @work_color: new work color, < 0 for no-op
2196 *
2197 * Prepare cwqs for workqueue flushing.
2198 *
2199 * If @flush_color is non-negative, flush_color on all cwqs should be
2200 * -1. If no cwq has in-flight commands at the specified color, all
2201 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2202 * has in flight commands, its cwq->flush_color is set to
2203 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2204 * wakeup logic is armed and %true is returned.
2205 *
2206 * The caller should have initialized @wq->first_flusher prior to
2207 * calling this function with non-negative @flush_color. If
2208 * @flush_color is negative, no flush color update is done and %false
2209 * is returned.
2210 *
2211 * If @work_color is non-negative, all cwqs should have the same
2212 * work_color which is previous to @work_color and all will be
2213 * advanced to @work_color.
2214 *
2215 * CONTEXT:
2216 * mutex_lock(wq->flush_mutex).
2217 *
2218 * RETURNS:
2219 * %true if @flush_color >= 0 and there's something to flush. %false
2220 * otherwise.
2221 */
2222static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2223 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Tejun Heo73f53c42010-06-29 10:07:11 +02002225 bool wait = false;
2226 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002227
Tejun Heo73f53c42010-06-29 10:07:11 +02002228 if (flush_color >= 0) {
2229 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2230 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002231 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002232
Tejun Heof3421792010-07-02 10:03:51 +02002233 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002234 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo47303942012-07-12 14:46:37 -07002235 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002236
Tejun Heo8b03ae32010-06-29 10:07:12 +02002237 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002238
2239 if (flush_color >= 0) {
2240 BUG_ON(cwq->flush_color != -1);
2241
2242 if (cwq->nr_in_flight[flush_color]) {
2243 cwq->flush_color = flush_color;
2244 atomic_inc(&wq->nr_cwqs_to_flush);
2245 wait = true;
2246 }
2247 }
2248
2249 if (work_color >= 0) {
2250 BUG_ON(work_color != work_next_color(cwq->work_color));
2251 cwq->work_color = work_color;
2252 }
2253
Tejun Heo8b03ae32010-06-29 10:07:12 +02002254 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002255 }
2256
2257 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2258 complete(&wq->first_flusher->done);
2259
2260 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002263/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002265 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 *
2267 * Forces execution of the workqueue and blocks until its completion.
2268 * This is typically used in driver shutdown handlers.
2269 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002270 * We sleep until all works which were queued on entry have been handled,
2271 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002273void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Tejun Heo73f53c42010-06-29 10:07:11 +02002275 struct wq_flusher this_flusher = {
2276 .list = LIST_HEAD_INIT(this_flusher.list),
2277 .flush_color = -1,
2278 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2279 };
2280 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002281
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002282 lock_map_acquire(&wq->lockdep_map);
2283 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002284
2285 mutex_lock(&wq->flush_mutex);
2286
2287 /*
2288 * Start-to-wait phase
2289 */
2290 next_color = work_next_color(wq->work_color);
2291
2292 if (next_color != wq->flush_color) {
2293 /*
2294 * Color space is not full. The current work_color
2295 * becomes our flush_color and work_color is advanced
2296 * by one.
2297 */
2298 BUG_ON(!list_empty(&wq->flusher_overflow));
2299 this_flusher.flush_color = wq->work_color;
2300 wq->work_color = next_color;
2301
2302 if (!wq->first_flusher) {
2303 /* no flush in progress, become the first flusher */
2304 BUG_ON(wq->flush_color != this_flusher.flush_color);
2305
2306 wq->first_flusher = &this_flusher;
2307
2308 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2309 wq->work_color)) {
2310 /* nothing to flush, done */
2311 wq->flush_color = next_color;
2312 wq->first_flusher = NULL;
2313 goto out_unlock;
2314 }
2315 } else {
2316 /* wait in queue */
2317 BUG_ON(wq->flush_color == this_flusher.flush_color);
2318 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2319 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2320 }
2321 } else {
2322 /*
2323 * Oops, color space is full, wait on overflow queue.
2324 * The next flush completion will assign us
2325 * flush_color and transfer to flusher_queue.
2326 */
2327 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2328 }
2329
2330 mutex_unlock(&wq->flush_mutex);
2331
2332 wait_for_completion(&this_flusher.done);
2333
2334 /*
2335 * Wake-up-and-cascade phase
2336 *
2337 * First flushers are responsible for cascading flushes and
2338 * handling overflow. Non-first flushers can simply return.
2339 */
2340 if (wq->first_flusher != &this_flusher)
2341 return;
2342
2343 mutex_lock(&wq->flush_mutex);
2344
Tejun Heo4ce48b32010-07-02 10:03:51 +02002345 /* we might have raced, check again with mutex held */
2346 if (wq->first_flusher != &this_flusher)
2347 goto out_unlock;
2348
Tejun Heo73f53c42010-06-29 10:07:11 +02002349 wq->first_flusher = NULL;
2350
2351 BUG_ON(!list_empty(&this_flusher.list));
2352 BUG_ON(wq->flush_color != this_flusher.flush_color);
2353
2354 while (true) {
2355 struct wq_flusher *next, *tmp;
2356
2357 /* complete all the flushers sharing the current flush color */
2358 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2359 if (next->flush_color != wq->flush_color)
2360 break;
2361 list_del_init(&next->list);
2362 complete(&next->done);
2363 }
2364
2365 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2366 wq->flush_color != work_next_color(wq->work_color));
2367
2368 /* this flush_color is finished, advance by one */
2369 wq->flush_color = work_next_color(wq->flush_color);
2370
2371 /* one color has been freed, handle overflow queue */
2372 if (!list_empty(&wq->flusher_overflow)) {
2373 /*
2374 * Assign the same color to all overflowed
2375 * flushers, advance work_color and append to
2376 * flusher_queue. This is the start-to-wait
2377 * phase for these overflowed flushers.
2378 */
2379 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2380 tmp->flush_color = wq->work_color;
2381
2382 wq->work_color = work_next_color(wq->work_color);
2383
2384 list_splice_tail_init(&wq->flusher_overflow,
2385 &wq->flusher_queue);
2386 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2387 }
2388
2389 if (list_empty(&wq->flusher_queue)) {
2390 BUG_ON(wq->flush_color != wq->work_color);
2391 break;
2392 }
2393
2394 /*
2395 * Need to flush more colors. Make the next flusher
2396 * the new first flusher and arm cwqs.
2397 */
2398 BUG_ON(wq->flush_color == wq->work_color);
2399 BUG_ON(wq->flush_color != next->flush_color);
2400
2401 list_del_init(&next->list);
2402 wq->first_flusher = next;
2403
2404 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2405 break;
2406
2407 /*
2408 * Meh... this color is already done, clear first
2409 * flusher and repeat cascading.
2410 */
2411 wq->first_flusher = NULL;
2412 }
2413
2414out_unlock:
2415 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
Dave Jonesae90dd52006-06-30 01:40:45 -04002417EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002419/**
2420 * drain_workqueue - drain a workqueue
2421 * @wq: workqueue to drain
2422 *
2423 * Wait until the workqueue becomes empty. While draining is in progress,
2424 * only chain queueing is allowed. IOW, only currently pending or running
2425 * work items on @wq can queue further work items on it. @wq is flushed
2426 * repeatedly until it becomes empty. The number of flushing is detemined
2427 * by the depth of chaining and should be relatively short. Whine if it
2428 * takes too long.
2429 */
2430void drain_workqueue(struct workqueue_struct *wq)
2431{
2432 unsigned int flush_cnt = 0;
2433 unsigned int cpu;
2434
2435 /*
2436 * __queue_work() needs to test whether there are drainers, is much
2437 * hotter than drain_workqueue() and already looks at @wq->flags.
2438 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2439 */
2440 spin_lock(&workqueue_lock);
2441 if (!wq->nr_drainers++)
2442 wq->flags |= WQ_DRAINING;
2443 spin_unlock(&workqueue_lock);
2444reflush:
2445 flush_workqueue(wq);
2446
2447 for_each_cwq_cpu(cpu, wq) {
2448 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002449 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002450
Tejun Heo47303942012-07-12 14:46:37 -07002451 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002452 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo47303942012-07-12 14:46:37 -07002453 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002454
2455 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002456 continue;
2457
2458 if (++flush_cnt == 10 ||
2459 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2460 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2461 wq->name, flush_cnt);
2462 goto reflush;
2463 }
2464
2465 spin_lock(&workqueue_lock);
2466 if (!--wq->nr_drainers)
2467 wq->flags &= ~WQ_DRAINING;
2468 spin_unlock(&workqueue_lock);
2469}
2470EXPORT_SYMBOL_GPL(drain_workqueue);
2471
Tejun Heobaf59022010-09-16 10:42:16 +02002472static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2473 bool wait_executing)
2474{
2475 struct worker *worker = NULL;
2476 struct global_cwq *gcwq;
2477 struct cpu_workqueue_struct *cwq;
2478
2479 might_sleep();
2480 gcwq = get_work_gcwq(work);
2481 if (!gcwq)
2482 return false;
2483
2484 spin_lock_irq(&gcwq->lock);
2485 if (!list_empty(&work->entry)) {
2486 /*
2487 * See the comment near try_to_grab_pending()->smp_rmb().
2488 * If it was re-queued to a different gcwq under us, we
2489 * are not going to wait.
2490 */
2491 smp_rmb();
2492 cwq = get_work_cwq(work);
Tejun Heo47303942012-07-12 14:46:37 -07002493 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002494 goto already_gone;
2495 } else if (wait_executing) {
2496 worker = find_worker_executing_work(gcwq, work);
2497 if (!worker)
2498 goto already_gone;
2499 cwq = worker->current_cwq;
2500 } else
2501 goto already_gone;
2502
2503 insert_wq_barrier(cwq, barr, work, worker);
2504 spin_unlock_irq(&gcwq->lock);
2505
Tejun Heoe1594892011-01-09 23:32:15 +01002506 /*
2507 * If @max_active is 1 or rescuer is in use, flushing another work
2508 * item on the same workqueue may lead to deadlock. Make sure the
2509 * flusher is not running on the same workqueue by verifying write
2510 * access.
2511 */
2512 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2513 lock_map_acquire(&cwq->wq->lockdep_map);
2514 else
2515 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002516 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002517
Tejun Heobaf59022010-09-16 10:42:16 +02002518 return true;
2519already_gone:
2520 spin_unlock_irq(&gcwq->lock);
2521 return false;
2522}
2523
Oleg Nesterovdb700892008-07-25 01:47:49 -07002524/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002525 * flush_work - wait for a work to finish executing the last queueing instance
2526 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002527 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002528 * Wait until @work has finished execution. This function considers
2529 * only the last queueing instance of @work. If @work has been
2530 * enqueued across different CPUs on a non-reentrant workqueue or on
2531 * multiple workqueues, @work might still be executing on return on
2532 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002533 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002534 * If @work was queued only on a non-reentrant, ordered or unbound
2535 * workqueue, @work is guaranteed to be idle on return if it hasn't
2536 * been requeued since flush started.
2537 *
2538 * RETURNS:
2539 * %true if flush_work() waited for the work to finish execution,
2540 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002541 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002542bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002543{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002544 struct wq_barrier barr;
2545
Tejun Heobaf59022010-09-16 10:42:16 +02002546 if (start_flush_work(work, &barr, true)) {
2547 wait_for_completion(&barr.done);
2548 destroy_work_on_stack(&barr.work);
2549 return true;
2550 } else
2551 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002552}
2553EXPORT_SYMBOL_GPL(flush_work);
2554
Tejun Heo401a8d02010-09-16 10:36:00 +02002555static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2556{
2557 struct wq_barrier barr;
2558 struct worker *worker;
2559
2560 spin_lock_irq(&gcwq->lock);
2561
2562 worker = find_worker_executing_work(gcwq, work);
2563 if (unlikely(worker))
2564 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2565
2566 spin_unlock_irq(&gcwq->lock);
2567
2568 if (unlikely(worker)) {
2569 wait_for_completion(&barr.done);
2570 destroy_work_on_stack(&barr.work);
2571 return true;
2572 } else
2573 return false;
2574}
2575
2576static bool wait_on_work(struct work_struct *work)
2577{
2578 bool ret = false;
2579 int cpu;
2580
2581 might_sleep();
2582
2583 lock_map_acquire(&work->lockdep_map);
2584 lock_map_release(&work->lockdep_map);
2585
2586 for_each_gcwq_cpu(cpu)
2587 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2588 return ret;
2589}
2590
Tejun Heo09383492010-09-16 10:48:29 +02002591/**
2592 * flush_work_sync - wait until a work has finished execution
2593 * @work: the work to flush
2594 *
2595 * Wait until @work has finished execution. On return, it's
2596 * guaranteed that all queueing instances of @work which happened
2597 * before this function is called are finished. In other words, if
2598 * @work hasn't been requeued since this function was called, @work is
2599 * guaranteed to be idle on return.
2600 *
2601 * RETURNS:
2602 * %true if flush_work_sync() waited for the work to finish execution,
2603 * %false if it was already idle.
2604 */
2605bool flush_work_sync(struct work_struct *work)
2606{
2607 struct wq_barrier barr;
2608 bool pending, waited;
2609
2610 /* we'll wait for executions separately, queue barr only if pending */
2611 pending = start_flush_work(work, &barr, false);
2612
2613 /* wait for executions to finish */
2614 waited = wait_on_work(work);
2615
2616 /* wait for the pending one */
2617 if (pending) {
2618 wait_for_completion(&barr.done);
2619 destroy_work_on_stack(&barr.work);
2620 }
2621
2622 return pending || waited;
2623}
2624EXPORT_SYMBOL_GPL(flush_work_sync);
2625
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002626/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002627 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002628 * so this work can't be re-armed in any way.
2629 */
2630static int try_to_grab_pending(struct work_struct *work)
2631{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002632 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002633 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002634
Tejun Heo22df02b2010-06-29 10:07:10 +02002635 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002636 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002637
2638 /*
2639 * The queueing is in progress, or it is already queued. Try to
2640 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2641 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002642 gcwq = get_work_gcwq(work);
2643 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002644 return ret;
2645
Tejun Heo8b03ae32010-06-29 10:07:12 +02002646 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002647 if (!list_empty(&work->entry)) {
2648 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002649 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002650 * In that case we must see the new value after rmb(), see
2651 * insert_work()->wmb().
2652 */
2653 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002654 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002655 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002656 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002657 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002658 get_work_color(work),
2659 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002660 ret = 1;
2661 }
2662 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002663 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002664
2665 return ret;
2666}
2667
Tejun Heo401a8d02010-09-16 10:36:00 +02002668static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002669 struct timer_list* timer)
2670{
2671 int ret;
2672
2673 do {
2674 ret = (timer && likely(del_timer(timer)));
2675 if (!ret)
2676 ret = try_to_grab_pending(work);
2677 wait_on_work(work);
2678 } while (unlikely(ret < 0));
2679
Tejun Heo7a22ad72010-06-29 10:07:13 +02002680 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002681 return ret;
2682}
2683
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002684/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002685 * cancel_work_sync - cancel a work and wait for it to finish
2686 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002687 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002688 * Cancel @work and wait for its execution to finish. This function
2689 * can be used even if the work re-queues itself or migrates to
2690 * another workqueue. On return from this function, @work is
2691 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002692 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002693 * cancel_work_sync(&delayed_work->work) must not be used for
2694 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002695 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002696 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002697 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002698 *
2699 * RETURNS:
2700 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002701 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002702bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002703{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002704 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002705}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002706EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002707
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002708/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002709 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2710 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002711 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002712 * Delayed timer is cancelled and the pending work is queued for
2713 * immediate execution. Like flush_work(), this function only
2714 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002715 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002716 * RETURNS:
2717 * %true if flush_work() waited for the work to finish execution,
2718 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002720bool flush_delayed_work(struct delayed_work *dwork)
2721{
2722 if (del_timer_sync(&dwork->timer))
2723 __queue_work(raw_smp_processor_id(),
2724 get_work_cwq(&dwork->work)->wq, &dwork->work);
2725 return flush_work(&dwork->work);
2726}
2727EXPORT_SYMBOL(flush_delayed_work);
2728
2729/**
Tejun Heo09383492010-09-16 10:48:29 +02002730 * flush_delayed_work_sync - wait for a dwork to finish
2731 * @dwork: the delayed work to flush
2732 *
2733 * Delayed timer is cancelled and the pending work is queued for
2734 * execution immediately. Other than timer handling, its behavior
2735 * is identical to flush_work_sync().
2736 *
2737 * RETURNS:
2738 * %true if flush_work_sync() waited for the work to finish execution,
2739 * %false if it was already idle.
2740 */
2741bool flush_delayed_work_sync(struct delayed_work *dwork)
2742{
2743 if (del_timer_sync(&dwork->timer))
2744 __queue_work(raw_smp_processor_id(),
2745 get_work_cwq(&dwork->work)->wq, &dwork->work);
2746 return flush_work_sync(&dwork->work);
2747}
2748EXPORT_SYMBOL(flush_delayed_work_sync);
2749
2750/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002751 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2752 * @dwork: the delayed work cancel
2753 *
2754 * This is cancel_work_sync() for delayed works.
2755 *
2756 * RETURNS:
2757 * %true if @dwork was pending, %false otherwise.
2758 */
2759bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002760{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002761 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002762}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002763EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002765/**
2766 * schedule_work - put work task in global workqueue
2767 * @work: job to be done
2768 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002769 * Returns zero if @work was already on the kernel-global workqueue and
2770 * non-zero otherwise.
2771 *
2772 * This puts a job in the kernel-global workqueue if it was not already
2773 * queued and leaves it in the same position on the kernel-global
2774 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002775 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002776int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Tejun Heod320c032010-06-29 10:07:14 +02002778 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
Dave Jonesae90dd52006-06-30 01:40:45 -04002780EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Zhang Ruic1a220e2008-07-23 21:28:39 -07002782/*
2783 * schedule_work_on - put work task on a specific cpu
2784 * @cpu: cpu to put the work task on
2785 * @work: job to be done
2786 *
2787 * This puts a job on a specific cpu
2788 */
2789int schedule_work_on(int cpu, struct work_struct *work)
2790{
Tejun Heod320c032010-06-29 10:07:14 +02002791 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002792}
2793EXPORT_SYMBOL(schedule_work_on);
2794
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002795/**
2796 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002797 * @dwork: job to be done
2798 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002799 *
2800 * After waiting for a given time this puts a job in the kernel-global
2801 * workqueue.
2802 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002803int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002804 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Tejun Heod320c032010-06-29 10:07:14 +02002806 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
Dave Jonesae90dd52006-06-30 01:40:45 -04002808EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002810/**
2811 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2812 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002813 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002814 * @delay: number of jiffies to wait
2815 *
2816 * After waiting for a given time this puts a job in the kernel-global
2817 * workqueue on the specified CPU.
2818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002820 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Tejun Heod320c032010-06-29 10:07:14 +02002822 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
Dave Jonesae90dd52006-06-30 01:40:45 -04002824EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Andrew Mortonb6136772006-06-25 05:47:49 -07002826/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002827 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002828 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002829 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002830 * schedule_on_each_cpu() executes @func on each online CPU using the
2831 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002832 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002833 *
2834 * RETURNS:
2835 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002836 */
David Howells65f27f32006-11-22 14:55:48 +00002837int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002838{
2839 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002840 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002841
Andrew Mortonb6136772006-06-25 05:47:49 -07002842 works = alloc_percpu(struct work_struct);
2843 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002844 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002845
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002846 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002847
Christoph Lameter15316ba2006-01-08 01:00:43 -08002848 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002849 struct work_struct *work = per_cpu_ptr(works, cpu);
2850
2851 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002852 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002853 }
Tejun Heo93981802009-11-17 14:06:20 -08002854
2855 for_each_online_cpu(cpu)
2856 flush_work(per_cpu_ptr(works, cpu));
2857
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002858 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002859 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002860 return 0;
2861}
2862
Alan Sterneef6a7d2010-02-12 17:39:21 +09002863/**
2864 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2865 *
2866 * Forces execution of the kernel-global workqueue and blocks until its
2867 * completion.
2868 *
2869 * Think twice before calling this function! It's very easy to get into
2870 * trouble if you don't take great care. Either of the following situations
2871 * will lead to deadlock:
2872 *
2873 * One of the work items currently on the workqueue needs to acquire
2874 * a lock held by your code or its caller.
2875 *
2876 * Your code is running in the context of a work routine.
2877 *
2878 * They will be detected by lockdep when they occur, but the first might not
2879 * occur very often. It depends on what work items are on the workqueue and
2880 * what locks they need, which you have no control over.
2881 *
2882 * In most situations flushing the entire workqueue is overkill; you merely
2883 * need to know that a particular work item isn't queued and isn't running.
2884 * In such cases you should use cancel_delayed_work_sync() or
2885 * cancel_work_sync() instead.
2886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887void flush_scheduled_work(void)
2888{
Tejun Heod320c032010-06-29 10:07:14 +02002889 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
Dave Jonesae90dd52006-06-30 01:40:45 -04002891EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002894 * execute_in_process_context - reliably execute the routine with user context
2895 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002896 * @ew: guaranteed storage for the execute work structure (must
2897 * be available when the work executes)
2898 *
2899 * Executes the function immediately if process context is available,
2900 * otherwise schedules the function for delayed execution.
2901 *
2902 * Returns: 0 - function was executed
2903 * 1 - function was scheduled for execution
2904 */
David Howells65f27f32006-11-22 14:55:48 +00002905int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002906{
2907 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002908 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002909 return 0;
2910 }
2911
David Howells65f27f32006-11-22 14:55:48 +00002912 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002913 schedule_work(&ew->work);
2914
2915 return 1;
2916}
2917EXPORT_SYMBOL_GPL(execute_in_process_context);
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919int keventd_up(void)
2920{
Tejun Heod320c032010-06-29 10:07:14 +02002921 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002924static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002926 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002927 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2928 * Make sure that the alignment isn't lower than that of
2929 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002930 */
Tejun Heo0f900042010-06-29 10:07:11 +02002931 const size_t size = sizeof(struct cpu_workqueue_struct);
2932 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2933 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002934
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002935 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002936 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002937 else {
Tejun Heof3421792010-07-02 10:03:51 +02002938 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002939
Tejun Heof3421792010-07-02 10:03:51 +02002940 /*
2941 * Allocate enough room to align cwq and put an extra
2942 * pointer at the end pointing back to the originally
2943 * allocated pointer which will be used for free.
2944 */
2945 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2946 if (ptr) {
2947 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2948 *(void **)(wq->cpu_wq.single + 1) = ptr;
2949 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002950 }
Tejun Heof3421792010-07-02 10:03:51 +02002951
Tejun Heo0415b002011-03-24 18:50:09 +01002952 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002953 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2954 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002955}
2956
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002957static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002958{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002959 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002960 free_percpu(wq->cpu_wq.pcpu);
2961 else if (wq->cpu_wq.single) {
2962 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002963 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002964 }
2965}
2966
Tejun Heof3421792010-07-02 10:03:51 +02002967static int wq_clamp_max_active(int max_active, unsigned int flags,
2968 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002969{
Tejun Heof3421792010-07-02 10:03:51 +02002970 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2971
2972 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002973 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2974 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002975 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002976
Tejun Heof3421792010-07-02 10:03:51 +02002977 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002978}
2979
Tejun Heob196be82012-01-10 15:11:35 -08002980struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002981 unsigned int flags,
2982 int max_active,
2983 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002984 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002985{
Tejun Heob196be82012-01-10 15:11:35 -08002986 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002987 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002988 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002989 size_t namelen;
2990
2991 /* determine namelen, allocate wq and format name */
2992 va_start(args, lock_name);
2993 va_copy(args1, args);
2994 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2995
2996 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2997 if (!wq)
2998 goto err;
2999
3000 vsnprintf(wq->name, namelen, fmt, args1);
3001 va_end(args);
3002 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003003
Tejun Heof3421792010-07-02 10:03:51 +02003004 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003005 * Workqueues which may be used during memory reclaim should
3006 * have a rescuer to guarantee forward progress.
3007 */
3008 if (flags & WQ_MEM_RECLAIM)
3009 flags |= WQ_RESCUER;
3010
Tejun Heod320c032010-06-29 10:07:14 +02003011 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003012 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003013
Tejun Heob196be82012-01-10 15:11:35 -08003014 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003015 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003016 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003017 mutex_init(&wq->flush_mutex);
3018 atomic_set(&wq->nr_cwqs_to_flush, 0);
3019 INIT_LIST_HEAD(&wq->flusher_queue);
3020 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003021
Johannes Bergeb13ba82008-01-16 09:51:58 +01003022 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003023 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003024
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003025 if (alloc_cwqs(wq) < 0)
3026 goto err;
3027
Tejun Heof3421792010-07-02 10:03:51 +02003028 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003029 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003030 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003031
Tejun Heo0f900042010-06-29 10:07:11 +02003032 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo47303942012-07-12 14:46:37 -07003033 cwq->pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02003034 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003035 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003036 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003037 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003038 }
3039
Tejun Heoe22bee72010-06-29 10:07:14 +02003040 if (flags & WQ_RESCUER) {
3041 struct worker *rescuer;
3042
Tejun Heof2e005a2010-07-20 15:59:09 +02003043 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003044 goto err;
3045
3046 wq->rescuer = rescuer = alloc_worker();
3047 if (!rescuer)
3048 goto err;
3049
Tejun Heob196be82012-01-10 15:11:35 -08003050 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3051 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003052 if (IS_ERR(rescuer->task))
3053 goto err;
3054
Tejun Heoe22bee72010-06-29 10:07:14 +02003055 rescuer->task->flags |= PF_THREAD_BOUND;
3056 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003057 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003058
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003059 /*
3060 * workqueue_lock protects global freeze state and workqueues
3061 * list. Grab it, set max_active accordingly and add the new
3062 * workqueue to workqueues list.
3063 */
Tejun Heo15376632010-06-29 10:07:11 +02003064 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003065
Tejun Heo58a69cb2011-02-16 09:25:31 +01003066 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003067 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003068 get_cwq(cpu, wq)->max_active = 0;
3069
Tejun Heo15376632010-06-29 10:07:11 +02003070 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003071
Tejun Heo15376632010-06-29 10:07:11 +02003072 spin_unlock(&workqueue_lock);
3073
Oleg Nesterov3af244332007-05-09 02:34:09 -07003074 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003075err:
3076 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003077 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003078 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003079 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003080 kfree(wq);
3081 }
3082 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003083}
Tejun Heod320c032010-06-29 10:07:14 +02003084EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003085
3086/**
3087 * destroy_workqueue - safely terminate a workqueue
3088 * @wq: target workqueue
3089 *
3090 * Safely destroy a workqueue. All work currently pending will be done first.
3091 */
3092void destroy_workqueue(struct workqueue_struct *wq)
3093{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003094 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003095
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003096 /* drain it before proceeding with destruction */
3097 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003098
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003099 /*
3100 * wq list is used to freeze wq, remove from list after
3101 * flushing is complete in case freeze races us.
3102 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003103 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003104 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003105 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003106
Tejun Heoe22bee72010-06-29 10:07:14 +02003107 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003108 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003109 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3110 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003111
Tejun Heo73f53c42010-06-29 10:07:11 +02003112 for (i = 0; i < WORK_NR_COLORS; i++)
3113 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003114 BUG_ON(cwq->nr_active);
3115 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003116 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003117
Tejun Heoe22bee72010-06-29 10:07:14 +02003118 if (wq->flags & WQ_RESCUER) {
3119 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003120 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003121 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003122 }
3123
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003124 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125 kfree(wq);
3126}
3127EXPORT_SYMBOL_GPL(destroy_workqueue);
3128
Tejun Heodcd989c2010-06-29 10:07:14 +02003129/**
3130 * workqueue_set_max_active - adjust max_active of a workqueue
3131 * @wq: target workqueue
3132 * @max_active: new max_active value.
3133 *
3134 * Set max_active of @wq to @max_active.
3135 *
3136 * CONTEXT:
3137 * Don't call from IRQ context.
3138 */
3139void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3140{
3141 unsigned int cpu;
3142
Tejun Heof3421792010-07-02 10:03:51 +02003143 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003144
3145 spin_lock(&workqueue_lock);
3146
3147 wq->saved_max_active = max_active;
3148
Tejun Heof3421792010-07-02 10:03:51 +02003149 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003150 struct global_cwq *gcwq = get_gcwq(cpu);
3151
3152 spin_lock_irq(&gcwq->lock);
3153
Tejun Heo58a69cb2011-02-16 09:25:31 +01003154 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003155 !(gcwq->flags & GCWQ_FREEZING))
3156 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3157
3158 spin_unlock_irq(&gcwq->lock);
3159 }
3160
3161 spin_unlock(&workqueue_lock);
3162}
3163EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3164
3165/**
3166 * workqueue_congested - test whether a workqueue is congested
3167 * @cpu: CPU in question
3168 * @wq: target workqueue
3169 *
3170 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3171 * no synchronization around this function and the test result is
3172 * unreliable and only useful as advisory hints or for debugging.
3173 *
3174 * RETURNS:
3175 * %true if congested, %false otherwise.
3176 */
3177bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3178{
3179 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3180
3181 return !list_empty(&cwq->delayed_works);
3182}
3183EXPORT_SYMBOL_GPL(workqueue_congested);
3184
3185/**
3186 * work_cpu - return the last known associated cpu for @work
3187 * @work: the work of interest
3188 *
3189 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003190 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003191 */
3192unsigned int work_cpu(struct work_struct *work)
3193{
3194 struct global_cwq *gcwq = get_work_gcwq(work);
3195
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003196 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003197}
3198EXPORT_SYMBOL_GPL(work_cpu);
3199
3200/**
3201 * work_busy - test whether a work is currently pending or running
3202 * @work: the work to be tested
3203 *
3204 * Test whether @work is currently pending or running. There is no
3205 * synchronization around this function and the test result is
3206 * unreliable and only useful as advisory hints or for debugging.
3207 * Especially for reentrant wqs, the pending state might hide the
3208 * running state.
3209 *
3210 * RETURNS:
3211 * OR'd bitmask of WORK_BUSY_* bits.
3212 */
3213unsigned int work_busy(struct work_struct *work)
3214{
3215 struct global_cwq *gcwq = get_work_gcwq(work);
3216 unsigned long flags;
3217 unsigned int ret = 0;
3218
3219 if (!gcwq)
3220 return false;
3221
3222 spin_lock_irqsave(&gcwq->lock, flags);
3223
3224 if (work_pending(work))
3225 ret |= WORK_BUSY_PENDING;
3226 if (find_worker_executing_work(gcwq, work))
3227 ret |= WORK_BUSY_RUNNING;
3228
3229 spin_unlock_irqrestore(&gcwq->lock, flags);
3230
3231 return ret;
3232}
3233EXPORT_SYMBOL_GPL(work_busy);
3234
Tejun Heodb7bccf2010-06-29 10:07:12 +02003235/*
3236 * CPU hotplug.
3237 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003238 * There are two challenges in supporting CPU hotplug. Firstly, there
3239 * are a lot of assumptions on strong associations among work, cwq and
3240 * gcwq which make migrating pending and scheduled works very
3241 * difficult to implement without impacting hot paths. Secondly,
3242 * gcwqs serve mix of short, long and very long running works making
3243 * blocked draining impractical.
3244 *
3245 * This is solved by allowing a gcwq to be detached from CPU, running
3246 * it with unbound (rogue) workers and allowing it to be reattached
3247 * later if the cpu comes back online. A separate thread is created
3248 * to govern a gcwq in such state and is called the trustee of the
3249 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003250 *
3251 * Trustee states and their descriptions.
3252 *
3253 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3254 * new trustee is started with this state.
3255 *
3256 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003257 * assuming the manager role and making all existing
3258 * workers rogue. DOWN_PREPARE waits for trustee to
3259 * enter this state. After reaching IN_CHARGE, trustee
3260 * tries to execute the pending worklist until it's empty
3261 * and the state is set to BUTCHER, or the state is set
3262 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003263 *
3264 * BUTCHER Command state which is set by the cpu callback after
3265 * the cpu has went down. Once this state is set trustee
3266 * knows that there will be no new works on the worklist
3267 * and once the worklist is empty it can proceed to
3268 * killing idle workers.
3269 *
3270 * RELEASE Command state which is set by the cpu callback if the
3271 * cpu down has been canceled or it has come online
3272 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003273 * trying to drain or butcher and clears ROGUE, rebinds
3274 * all remaining workers back to the cpu and releases
3275 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003276 *
3277 * DONE Trustee will enter this state after BUTCHER or RELEASE
3278 * is complete.
3279 *
3280 * trustee CPU draining
3281 * took over down complete
3282 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3283 * | | ^
3284 * | CPU is back online v return workers |
3285 * ----------------> RELEASE --------------
3286 */
3287
3288/**
3289 * trustee_wait_event_timeout - timed event wait for trustee
3290 * @cond: condition to wait for
3291 * @timeout: timeout in jiffies
3292 *
3293 * wait_event_timeout() for trustee to use. Handles locking and
3294 * checks for RELEASE request.
3295 *
3296 * CONTEXT:
3297 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3298 * multiple times. To be used by trustee.
3299 *
3300 * RETURNS:
3301 * Positive indicating left time if @cond is satisfied, 0 if timed
3302 * out, -1 if canceled.
3303 */
3304#define trustee_wait_event_timeout(cond, timeout) ({ \
3305 long __ret = (timeout); \
3306 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3307 __ret) { \
3308 spin_unlock_irq(&gcwq->lock); \
3309 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3310 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3311 __ret); \
3312 spin_lock_irq(&gcwq->lock); \
3313 } \
3314 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3315})
3316
3317/**
3318 * trustee_wait_event - event wait for trustee
3319 * @cond: condition to wait for
3320 *
3321 * wait_event() for trustee to use. Automatically handles locking and
3322 * checks for CANCEL request.
3323 *
3324 * CONTEXT:
3325 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3326 * multiple times. To be used by trustee.
3327 *
3328 * RETURNS:
3329 * 0 if @cond is satisfied, -1 if canceled.
3330 */
3331#define trustee_wait_event(cond) ({ \
3332 long __ret1; \
3333 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3334 __ret1 < 0 ? -1 : 0; \
3335})
3336
3337static int __cpuinit trustee_thread(void *__gcwq)
3338{
3339 struct global_cwq *gcwq = __gcwq;
3340 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003341 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003342 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003343 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003344 int i;
3345
3346 BUG_ON(gcwq->cpu != smp_processor_id());
3347
3348 spin_lock_irq(&gcwq->lock);
3349 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003350 * Claim the manager position and make all workers rogue.
3351 * Trustee must be bound to the target cpu and can't be
3352 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003353 */
3354 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003355 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3356 BUG_ON(rc < 0);
3357
3358 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003359
Tejun Heo47303942012-07-12 14:46:37 -07003360 list_for_each_entry(worker, &gcwq->pool.idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003361 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003362
3363 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003364 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003365
3366 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003367 * Call schedule() so that we cross rq->lock and thus can
3368 * guarantee sched callbacks see the rogue flag. This is
3369 * necessary as scheduler callbacks may be invoked from other
3370 * cpus.
3371 */
3372 spin_unlock_irq(&gcwq->lock);
3373 schedule();
3374 spin_lock_irq(&gcwq->lock);
3375
3376 /*
Tejun Heocb444762010-07-02 10:03:50 +02003377 * Sched callbacks are disabled now. Zap nr_running. After
3378 * this, nr_running stays zero and need_more_worker() and
3379 * keep_working() are always true as long as the worklist is
3380 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003381 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07003382 atomic_set(get_pool_nr_running(&gcwq->pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003383
3384 spin_unlock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07003385 del_timer_sync(&gcwq->pool.idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003386 spin_lock_irq(&gcwq->lock);
3387
3388 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003389 * We're now in charge. Notify and proceed to drain. We need
3390 * to keep the gcwq running during the whole CPU down
3391 * procedure as other cpu hotunplug callbacks may need to
3392 * flush currently running tasks.
3393 */
3394 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3395 wake_up_all(&gcwq->trustee_wait);
3396
3397 /*
3398 * The original cpu is in the process of dying and may go away
3399 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003400 * be migrated to other cpus. Try draining any left work. We
3401 * want to get it over with ASAP - spam rescuers, wake up as
3402 * many idlers as necessary and create new ones till the
3403 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003404 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003405 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003406 */
Tejun Heo47303942012-07-12 14:46:37 -07003407 while (gcwq->pool.nr_workers != gcwq->pool.nr_idle ||
Tejun Heodb7bccf2010-06-29 10:07:12 +02003408 gcwq->flags & GCWQ_FREEZING ||
3409 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003410 int nr_works = 0;
3411
Tejun Heo47303942012-07-12 14:46:37 -07003412 list_for_each_entry(work, &gcwq->pool.worklist, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003413 send_mayday(work);
3414 nr_works++;
3415 }
3416
Tejun Heo47303942012-07-12 14:46:37 -07003417 list_for_each_entry(worker, &gcwq->pool.idle_list, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003418 if (!nr_works--)
3419 break;
3420 wake_up_process(worker->task);
3421 }
3422
Tejun Heob0f31fe2012-07-12 14:46:37 -07003423 if (need_to_create_worker(&gcwq->pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003424 spin_unlock_irq(&gcwq->lock);
Tejun Heob0f31fe2012-07-12 14:46:37 -07003425 worker = create_worker(&gcwq->pool, false);
Tejun Heoe22bee72010-06-29 10:07:14 +02003426 spin_lock_irq(&gcwq->lock);
3427 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003428 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003429 start_worker(worker);
3430 }
3431 }
3432
Tejun Heodb7bccf2010-06-29 10:07:12 +02003433 /* give a breather */
3434 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3435 break;
3436 }
3437
Tejun Heoe22bee72010-06-29 10:07:14 +02003438 /*
3439 * Either all works have been scheduled and cpu is down, or
3440 * cpu down has already been canceled. Wait for and butcher
3441 * all workers till we're canceled.
3442 */
3443 do {
Tejun Heo47303942012-07-12 14:46:37 -07003444 rc = trustee_wait_event(!list_empty(&gcwq->pool.idle_list));
3445 while (!list_empty(&gcwq->pool.idle_list))
3446 destroy_worker(list_first_entry(&gcwq->pool.idle_list,
Tejun Heoe22bee72010-06-29 10:07:14 +02003447 struct worker, entry));
Tejun Heo47303942012-07-12 14:46:37 -07003448 } while (gcwq->pool.nr_workers && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003449
3450 /*
3451 * At this point, either draining has completed and no worker
3452 * is left, or cpu down has been canceled or the cpu is being
3453 * brought back up. There shouldn't be any idle one left.
3454 * Tell the remaining busy ones to rebind once it finishes the
3455 * currently scheduled works by scheduling the rebind_work.
3456 */
Tejun Heo47303942012-07-12 14:46:37 -07003457 WARN_ON(!list_empty(&gcwq->pool.idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003458
3459 for_each_busy_worker(worker, i, pos, gcwq) {
3460 struct work_struct *rebind_work = &worker->rebind_work;
3461
3462 /*
3463 * Rebind_work may race with future cpu hotplug
3464 * operations. Use a separate flag to mark that
3465 * rebinding is scheduled.
3466 */
Tejun Heocb444762010-07-02 10:03:50 +02003467 worker->flags |= WORKER_REBIND;
3468 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003469
3470 /* queue rebind_work, wq doesn't matter, use the default one */
3471 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3472 work_data_bits(rebind_work)))
3473 continue;
3474
3475 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003476 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003477 worker->scheduled.next,
3478 work_color_to_flags(WORK_NO_COLOR));
3479 }
3480
3481 /* relinquish manager role */
3482 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3483
Tejun Heodb7bccf2010-06-29 10:07:12 +02003484 /* notify completion */
3485 gcwq->trustee = NULL;
3486 gcwq->trustee_state = TRUSTEE_DONE;
3487 wake_up_all(&gcwq->trustee_wait);
3488 spin_unlock_irq(&gcwq->lock);
3489 return 0;
3490}
3491
3492/**
3493 * wait_trustee_state - wait for trustee to enter the specified state
3494 * @gcwq: gcwq the trustee of interest belongs to
3495 * @state: target state to wait for
3496 *
3497 * Wait for the trustee to reach @state. DONE is already matched.
3498 *
3499 * CONTEXT:
3500 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3501 * multiple times. To be used by cpu_callback.
3502 */
3503static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003504__releases(&gcwq->lock)
3505__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003506{
3507 if (!(gcwq->trustee_state == state ||
3508 gcwq->trustee_state == TRUSTEE_DONE)) {
3509 spin_unlock_irq(&gcwq->lock);
3510 __wait_event(gcwq->trustee_wait,
3511 gcwq->trustee_state == state ||
3512 gcwq->trustee_state == TRUSTEE_DONE);
3513 spin_lock_irq(&gcwq->lock);
3514 }
3515}
3516
Oleg Nesterov3af244332007-05-09 02:34:09 -07003517static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3518 unsigned long action,
3519 void *hcpu)
3520{
3521 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003522 struct global_cwq *gcwq = get_gcwq(cpu);
3523 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003524 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003525 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003527 action &= ~CPU_TASKS_FROZEN;
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003530 case CPU_DOWN_PREPARE:
3531 new_trustee = kthread_create(trustee_thread, gcwq,
3532 "workqueue_trustee/%d\n", cpu);
3533 if (IS_ERR(new_trustee))
3534 return notifier_from_errno(PTR_ERR(new_trustee));
3535 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003536 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 case CPU_UP_PREPARE:
Tejun Heo47303942012-07-12 14:46:37 -07003538 BUG_ON(gcwq->pool.first_idle);
Tejun Heob0f31fe2012-07-12 14:46:37 -07003539 new_worker = create_worker(&gcwq->pool, false);
Tejun Heoe22bee72010-06-29 10:07:14 +02003540 if (!new_worker) {
3541 if (new_trustee)
3542 kthread_stop(new_trustee);
3543 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
3546
Tejun Heodb7bccf2010-06-29 10:07:12 +02003547 /* some are called w/ irq disabled, don't disturb irq status */
3548 spin_lock_irqsave(&gcwq->lock, flags);
3549
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003550 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003551 case CPU_DOWN_PREPARE:
3552 /* initialize trustee and tell it to acquire the gcwq */
3553 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3554 gcwq->trustee = new_trustee;
3555 gcwq->trustee_state = TRUSTEE_START;
3556 wake_up_process(gcwq->trustee);
3557 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003558 /* fall through */
3559 case CPU_UP_PREPARE:
Tejun Heo47303942012-07-12 14:46:37 -07003560 BUG_ON(gcwq->pool.first_idle);
3561 gcwq->pool.first_idle = new_worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003562 break;
3563
3564 case CPU_DYING:
3565 /*
3566 * Before this, the trustee and all workers except for
3567 * the ones which are still executing works from
3568 * before the last CPU down must be on the cpu. After
3569 * this, they'll all be diasporas.
3570 */
3571 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003572 break;
3573
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003574 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003575 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003576 /* fall through */
3577 case CPU_UP_CANCELED:
Tejun Heo47303942012-07-12 14:46:37 -07003578 destroy_worker(gcwq->pool.first_idle);
3579 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003580 break;
3581
3582 case CPU_DOWN_FAILED:
3583 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003584 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003585 if (gcwq->trustee_state != TRUSTEE_DONE) {
3586 gcwq->trustee_state = TRUSTEE_RELEASE;
3587 wake_up_process(gcwq->trustee);
3588 wait_trustee_state(gcwq, TRUSTEE_DONE);
3589 }
3590
Tejun Heoe22bee72010-06-29 10:07:14 +02003591 /*
3592 * Trustee is done and there might be no worker left.
3593 * Put the first_idle in and request a real manager to
3594 * take a look.
3595 */
3596 spin_unlock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07003597 kthread_bind(gcwq->pool.first_idle->task, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003598 spin_lock_irq(&gcwq->lock);
3599 gcwq->flags |= GCWQ_MANAGE_WORKERS;
Tejun Heo47303942012-07-12 14:46:37 -07003600 start_worker(gcwq->pool.first_idle);
3601 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003602 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003603 }
3604
Tejun Heodb7bccf2010-06-29 10:07:12 +02003605 spin_unlock_irqrestore(&gcwq->lock, flags);
3606
Tejun Heo15376632010-06-29 10:07:11 +02003607 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Rusty Russell2d3854a2008-11-05 13:39:10 +11003610#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003611
Rusty Russell2d3854a2008-11-05 13:39:10 +11003612struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003613 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003614 long (*fn)(void *);
3615 void *arg;
3616 long ret;
3617};
3618
Andrew Morton6b440032009-04-09 09:50:37 -06003619static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003620{
Andrew Morton6b440032009-04-09 09:50:37 -06003621 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003622 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003623 complete(&wfc->completion);
3624 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003625}
3626
3627/**
3628 * work_on_cpu - run a function in user context on a particular cpu
3629 * @cpu: the cpu to run on
3630 * @fn: the function to run
3631 * @arg: the function arg
3632 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003633 * This will return the value @fn returns.
3634 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003635 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003636 */
3637long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3638{
Andrew Morton6b440032009-04-09 09:50:37 -06003639 struct task_struct *sub_thread;
3640 struct work_for_cpu wfc = {
3641 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3642 .fn = fn,
3643 .arg = arg,
3644 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003645
Andrew Morton6b440032009-04-09 09:50:37 -06003646 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3647 if (IS_ERR(sub_thread))
3648 return PTR_ERR(sub_thread);
3649 kthread_bind(sub_thread, cpu);
3650 wake_up_process(sub_thread);
3651 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003652 return wfc.ret;
3653}
3654EXPORT_SYMBOL_GPL(work_on_cpu);
3655#endif /* CONFIG_SMP */
3656
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003657#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303658
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003659/**
3660 * freeze_workqueues_begin - begin freezing workqueues
3661 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003662 * Start freezing workqueues. After this function returns, all freezable
3663 * workqueues will queue new works to their frozen_works list instead of
3664 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003665 *
3666 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003667 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003668 */
3669void freeze_workqueues_begin(void)
3670{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003671 unsigned int cpu;
3672
3673 spin_lock(&workqueue_lock);
3674
3675 BUG_ON(workqueue_freezing);
3676 workqueue_freezing = true;
3677
Tejun Heof3421792010-07-02 10:03:51 +02003678 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003679 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003680 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003681
3682 spin_lock_irq(&gcwq->lock);
3683
Tejun Heodb7bccf2010-06-29 10:07:12 +02003684 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3685 gcwq->flags |= GCWQ_FREEZING;
3686
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003687 list_for_each_entry(wq, &workqueues, list) {
3688 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3689
Tejun Heo58a69cb2011-02-16 09:25:31 +01003690 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003691 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003692 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003693
3694 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003695 }
3696
3697 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003699
3700/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003701 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003702 *
3703 * Check whether freezing is complete. This function must be called
3704 * between freeze_workqueues_begin() and thaw_workqueues().
3705 *
3706 * CONTEXT:
3707 * Grabs and releases workqueue_lock.
3708 *
3709 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003710 * %true if some freezable workqueues are still busy. %false if freezing
3711 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003712 */
3713bool freeze_workqueues_busy(void)
3714{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003715 unsigned int cpu;
3716 bool busy = false;
3717
3718 spin_lock(&workqueue_lock);
3719
3720 BUG_ON(!workqueue_freezing);
3721
Tejun Heof3421792010-07-02 10:03:51 +02003722 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003723 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003724 /*
3725 * nr_active is monotonically decreasing. It's safe
3726 * to peek without lock.
3727 */
3728 list_for_each_entry(wq, &workqueues, list) {
3729 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3730
Tejun Heo58a69cb2011-02-16 09:25:31 +01003731 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003732 continue;
3733
3734 BUG_ON(cwq->nr_active < 0);
3735 if (cwq->nr_active) {
3736 busy = true;
3737 goto out_unlock;
3738 }
3739 }
3740 }
3741out_unlock:
3742 spin_unlock(&workqueue_lock);
3743 return busy;
3744}
3745
3746/**
3747 * thaw_workqueues - thaw workqueues
3748 *
3749 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003750 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003751 *
3752 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003753 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003754 */
3755void thaw_workqueues(void)
3756{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003757 unsigned int cpu;
3758
3759 spin_lock(&workqueue_lock);
3760
3761 if (!workqueue_freezing)
3762 goto out_unlock;
3763
Tejun Heof3421792010-07-02 10:03:51 +02003764 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003765 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003766 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003767
3768 spin_lock_irq(&gcwq->lock);
3769
Tejun Heodb7bccf2010-06-29 10:07:12 +02003770 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3771 gcwq->flags &= ~GCWQ_FREEZING;
3772
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003773 list_for_each_entry(wq, &workqueues, list) {
3774 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3775
Tejun Heo58a69cb2011-02-16 09:25:31 +01003776 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003777 continue;
3778
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003779 /* restore max_active and repopulate worklist */
3780 cwq->max_active = wq->saved_max_active;
3781
3782 while (!list_empty(&cwq->delayed_works) &&
3783 cwq->nr_active < cwq->max_active)
3784 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003785 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003786
Tejun Heob0f31fe2012-07-12 14:46:37 -07003787 wake_up_worker(&gcwq->pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003788
Tejun Heo8b03ae32010-06-29 10:07:12 +02003789 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003790 }
3791
3792 workqueue_freezing = false;
3793out_unlock:
3794 spin_unlock(&workqueue_lock);
3795}
3796#endif /* CONFIG_FREEZER */
3797
Suresh Siddha6ee05782010-07-30 14:57:37 -07003798static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
Tejun Heoc34056a2010-06-29 10:07:11 +02003800 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003801 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003802
Tejun Heof6500942010-08-09 11:50:34 +02003803 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003804
3805 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003806 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003807 struct global_cwq *gcwq = get_gcwq(cpu);
3808
3809 spin_lock_init(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07003810 gcwq->pool.gcwq = gcwq;
3811 INIT_LIST_HEAD(&gcwq->pool.worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003812 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003813 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003814
Tejun Heo47303942012-07-12 14:46:37 -07003815 INIT_LIST_HEAD(&gcwq->pool.idle_list);
Tejun Heoc8e55f32010-06-29 10:07:12 +02003816 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3817 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3818
Tejun Heo47303942012-07-12 14:46:37 -07003819 init_timer_deferrable(&gcwq->pool.idle_timer);
3820 gcwq->pool.idle_timer.function = idle_worker_timeout;
Tejun Heob0f31fe2012-07-12 14:46:37 -07003821 gcwq->pool.idle_timer.data = (unsigned long)&gcwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003822
Tejun Heo47303942012-07-12 14:46:37 -07003823 setup_timer(&gcwq->pool.mayday_timer, gcwq_mayday_timeout,
Tejun Heob0f31fe2012-07-12 14:46:37 -07003824 (unsigned long)&gcwq->pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003825
Tejun Heo47303942012-07-12 14:46:37 -07003826 ida_init(&gcwq->pool.worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003827
3828 gcwq->trustee_state = TRUSTEE_DONE;
3829 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003830 }
3831
Tejun Heoe22bee72010-06-29 10:07:14 +02003832 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003833 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003834 struct global_cwq *gcwq = get_gcwq(cpu);
3835 struct worker *worker;
3836
Tejun Heo477a3c32010-08-31 10:54:35 +02003837 if (cpu != WORK_CPU_UNBOUND)
3838 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heob0f31fe2012-07-12 14:46:37 -07003839 worker = create_worker(&gcwq->pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02003840 BUG_ON(!worker);
3841 spin_lock_irq(&gcwq->lock);
3842 start_worker(worker);
3843 spin_unlock_irq(&gcwq->lock);
3844 }
3845
Tejun Heod320c032010-06-29 10:07:14 +02003846 system_wq = alloc_workqueue("events", 0, 0);
3847 system_long_wq = alloc_workqueue("events_long", 0, 0);
3848 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003849 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3850 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003851 system_freezable_wq = alloc_workqueue("events_freezable",
3852 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003853 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3854 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003855 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003856 !system_unbound_wq || !system_freezable_wq ||
3857 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003860early_initcall(init_workqueues);