blob: b0daaea44eaa1c07f350dcd5fc8583ea0ee37c2b [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 Heo11ebea52012-07-12 14:46:37 -070049 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
50 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
51
52 /* pool flags */
53 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
54 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
55 POOL_HIGHPRI_PENDING = 1 << 2, /* highpri works on queue */
Tejun Heodb7bccf2010-06-29 10:07:12 +020056
Tejun Heoc8e55f32010-06-29 10:07:12 +020057 /* worker flags */
58 WORKER_STARTED = 1 << 0, /* started */
59 WORKER_DIE = 1 << 1, /* die die die */
60 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020062 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020063 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020064 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020065 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020066
Tejun Heofb0e7be2010-06-29 10:07:15 +020067 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020068 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
70 /* gcwq->trustee_state */
71 TRUSTEE_START = 0, /* start */
72 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
73 TRUSTEE_BUTCHER = 2, /* butcher workers */
74 TRUSTEE_RELEASE = 3, /* release workers */
75 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020076
Tejun Heo4ce62e92012-07-13 22:16:44 -070077 NR_WORKER_POOLS = 1, /* # worker pools per gcwq */
78
Tejun Heoc8e55f32010-06-29 10:07:12 +020079 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
80 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
81 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heoe22bee72010-06-29 10:07:14 +020083 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
84 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
85
Tejun Heo3233cdb2011-02-16 18:10:19 +010086 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
87 /* call for help after 10ms
88 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
90 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020091 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020092
93 /*
94 * Rescue workers are used only on emergencies and shared by
95 * all cpus. Give -20.
96 */
97 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020098};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200101 * Structure fields follow one of the following exclusion rules.
102 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200103 * I: Modifiable by initialization/destruction paths and read-only for
104 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200105 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200106 * P: Preemption protected. Disabling preemption is enough and should
107 * only be modified and accessed from the local cpu.
108 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200109 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * X: During normal operation, modification requires gcwq->lock and
112 * should be done only from local cpu. Either disabling preemption
113 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200114 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200115 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200116 * F: wq->flush_mutex protected.
117 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 * W: workqueue_lock protected.
119 */
120
Tejun Heo8b03ae32010-06-29 10:07:12 +0200121struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700122struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200123
Tejun Heoe22bee72010-06-29 10:07:14 +0200124/*
125 * The poor guys doing the actual heavy lifting. All on-duty workers
126 * are either serving the manager role, on idle list or on busy hash.
127 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200128struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200129 /* on idle list while idle, on busy hash table while busy */
130 union {
131 struct list_head entry; /* L: while idle */
132 struct hlist_node hentry; /* L: while busy */
133 };
134
Tejun Heoc34056a2010-06-29 10:07:11 +0200135 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200136 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200137 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700139 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200140 /* 64 bytes boundary on 64bit, 32 on 32bit */
141 unsigned long last_active; /* L: last active timestamp */
142 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200143 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200144 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200145};
146
Tejun Heobd7bdd42012-07-12 14:46:37 -0700147struct worker_pool {
148 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700149 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700150
151 struct list_head worklist; /* L: list of pending works */
152 int nr_workers; /* L: total number of workers */
153 int nr_idle; /* L: currently idle ones */
154
155 struct list_head idle_list; /* X: list of idle workers */
156 struct timer_list idle_timer; /* L: worker idle timeout */
157 struct timer_list mayday_timer; /* L: SOS timer for workers */
158
159 struct ida worker_ida; /* L: for worker IDs */
160 struct worker *first_idle; /* L: first idle worker */
161};
162
Tejun Heo4690c4a2010-06-29 10:07:10 +0200163/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200164 * Global per-cpu workqueue. There's one and only one for each cpu
165 * and all works are queued and processed here regardless of their
166 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200167 */
168struct global_cwq {
169 spinlock_t lock; /* the gcwq lock */
170 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200171 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200172
Tejun Heobd7bdd42012-07-12 14:46:37 -0700173 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200174 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
175 /* L: hash of busy workers */
176
Tejun Heobd7bdd42012-07-12 14:46:37 -0700177 struct worker_pool pool; /* the worker pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200178
179 struct task_struct *trustee; /* L: for gcwq shutdown */
180 unsigned int trustee_state; /* L: trustee state */
181 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200182} ____cacheline_aligned_in_smp;
183
184/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200185 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200186 * work_struct->data are used for flags and thus cwqs need to be
187 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700190 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200191 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200192 int work_color; /* L: current color */
193 int flush_color; /* L: flushing color */
194 int nr_in_flight[WORK_NR_COLORS];
195 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200196 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200197 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200199};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200202 * Structure used to wait for workqueue flush.
203 */
204struct wq_flusher {
205 struct list_head list; /* F: list of flushers */
206 int flush_color; /* F: flush color waiting for */
207 struct completion done; /* flush completion */
208};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Tejun Heo73f53c42010-06-29 10:07:11 +0200210/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200211 * All cpumasks are assumed to be always set on UP and thus can't be
212 * used to determine whether there's something to be done.
213 */
214#ifdef CONFIG_SMP
215typedef cpumask_var_t mayday_mask_t;
216#define mayday_test_and_set_cpu(cpu, mask) \
217 cpumask_test_and_set_cpu((cpu), (mask))
218#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
219#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200220#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200221#define free_mayday_mask(mask) free_cpumask_var((mask))
222#else
223typedef unsigned long mayday_mask_t;
224#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
225#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
226#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
227#define alloc_mayday_mask(maskp, gfp) true
228#define free_mayday_mask(mask) do { } while (0)
229#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/*
232 * The externally visible workqueue abstraction is an array of
233 * per-CPU workqueues:
234 */
235struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200236 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200237 union {
238 struct cpu_workqueue_struct __percpu *pcpu;
239 struct cpu_workqueue_struct *single;
240 unsigned long v;
241 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200242 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200243
244 struct mutex flush_mutex; /* protects wq flushing */
245 int work_color; /* F: current work color */
246 int flush_color; /* F: current flush color */
247 atomic_t nr_cwqs_to_flush; /* flush in progress */
248 struct wq_flusher *first_flusher; /* F: first flusher */
249 struct list_head flusher_queue; /* F: flush waiters */
250 struct list_head flusher_overflow; /* F: flush overflow list */
251
Tejun Heof2e005a2010-07-20 15:59:09 +0200252 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200253 struct worker *rescuer; /* I: rescue worker */
254
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200255 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200256 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700257#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200258 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700259#endif
Tejun Heob196be82012-01-10 15:11:35 -0800260 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Tejun Heod320c032010-06-29 10:07:14 +0200263struct workqueue_struct *system_wq __read_mostly;
264struct workqueue_struct *system_long_wq __read_mostly;
265struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200266struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100267struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100268struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200269EXPORT_SYMBOL_GPL(system_wq);
270EXPORT_SYMBOL_GPL(system_long_wq);
271EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200272EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100273EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100274EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200275
Tejun Heo97bd2342010-10-05 10:41:14 +0200276#define CREATE_TRACE_POINTS
277#include <trace/events/workqueue.h>
278
Tejun Heo4ce62e92012-07-13 22:16:44 -0700279#define for_each_worker_pool(pool, gcwq) \
280 for ((pool) = &(gcwq)->pool; (pool); (pool) = NULL)
281
Tejun Heodb7bccf2010-06-29 10:07:12 +0200282#define for_each_busy_worker(worker, i, pos, gcwq) \
283 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
284 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
285
Tejun Heof3421792010-07-02 10:03:51 +0200286static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
287 unsigned int sw)
288{
289 if (cpu < nr_cpu_ids) {
290 if (sw & 1) {
291 cpu = cpumask_next(cpu, mask);
292 if (cpu < nr_cpu_ids)
293 return cpu;
294 }
295 if (sw & 2)
296 return WORK_CPU_UNBOUND;
297 }
298 return WORK_CPU_NONE;
299}
300
301static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
302 struct workqueue_struct *wq)
303{
304 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
305}
306
Tejun Heo09884952010-08-01 11:50:12 +0200307/*
308 * CPU iterators
309 *
310 * An extra gcwq is defined for an invalid cpu number
311 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
312 * specific CPU. The following iterators are similar to
313 * for_each_*_cpu() iterators but also considers the unbound gcwq.
314 *
315 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
316 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
317 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
318 * WORK_CPU_UNBOUND for unbound workqueues
319 */
Tejun Heof3421792010-07-02 10:03:51 +0200320#define for_each_gcwq_cpu(cpu) \
321 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
322 (cpu) < WORK_CPU_NONE; \
323 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
324
325#define for_each_online_gcwq_cpu(cpu) \
326 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
327 (cpu) < WORK_CPU_NONE; \
328 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
329
330#define for_each_cwq_cpu(cpu, wq) \
331 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
332 (cpu) < WORK_CPU_NONE; \
333 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
334
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900335#ifdef CONFIG_DEBUG_OBJECTS_WORK
336
337static struct debug_obj_descr work_debug_descr;
338
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100339static void *work_debug_hint(void *addr)
340{
341 return ((struct work_struct *) addr)->func;
342}
343
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900344/*
345 * fixup_init is called when:
346 * - an active object is initialized
347 */
348static int work_fixup_init(void *addr, enum debug_obj_state state)
349{
350 struct work_struct *work = addr;
351
352 switch (state) {
353 case ODEBUG_STATE_ACTIVE:
354 cancel_work_sync(work);
355 debug_object_init(work, &work_debug_descr);
356 return 1;
357 default:
358 return 0;
359 }
360}
361
362/*
363 * fixup_activate is called when:
364 * - an active object is activated
365 * - an unknown object is activated (might be a statically initialized object)
366 */
367static int work_fixup_activate(void *addr, enum debug_obj_state state)
368{
369 struct work_struct *work = addr;
370
371 switch (state) {
372
373 case ODEBUG_STATE_NOTAVAILABLE:
374 /*
375 * This is not really a fixup. The work struct was
376 * statically initialized. We just make sure that it
377 * is tracked in the object tracker.
378 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200379 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900380 debug_object_init(work, &work_debug_descr);
381 debug_object_activate(work, &work_debug_descr);
382 return 0;
383 }
384 WARN_ON_ONCE(1);
385 return 0;
386
387 case ODEBUG_STATE_ACTIVE:
388 WARN_ON(1);
389
390 default:
391 return 0;
392 }
393}
394
395/*
396 * fixup_free is called when:
397 * - an active object is freed
398 */
399static int work_fixup_free(void *addr, enum debug_obj_state state)
400{
401 struct work_struct *work = addr;
402
403 switch (state) {
404 case ODEBUG_STATE_ACTIVE:
405 cancel_work_sync(work);
406 debug_object_free(work, &work_debug_descr);
407 return 1;
408 default:
409 return 0;
410 }
411}
412
413static struct debug_obj_descr work_debug_descr = {
414 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100415 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900416 .fixup_init = work_fixup_init,
417 .fixup_activate = work_fixup_activate,
418 .fixup_free = work_fixup_free,
419};
420
421static inline void debug_work_activate(struct work_struct *work)
422{
423 debug_object_activate(work, &work_debug_descr);
424}
425
426static inline void debug_work_deactivate(struct work_struct *work)
427{
428 debug_object_deactivate(work, &work_debug_descr);
429}
430
431void __init_work(struct work_struct *work, int onstack)
432{
433 if (onstack)
434 debug_object_init_on_stack(work, &work_debug_descr);
435 else
436 debug_object_init(work, &work_debug_descr);
437}
438EXPORT_SYMBOL_GPL(__init_work);
439
440void destroy_work_on_stack(struct work_struct *work)
441{
442 debug_object_free(work, &work_debug_descr);
443}
444EXPORT_SYMBOL_GPL(destroy_work_on_stack);
445
446#else
447static inline void debug_work_activate(struct work_struct *work) { }
448static inline void debug_work_deactivate(struct work_struct *work) { }
449#endif
450
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100451/* Serializes the accesses to the list of workqueues. */
452static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200454static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Oleg Nesterov14441962007-05-23 13:57:57 -0700456/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200457 * The almighty global cpu workqueues. nr_running is the only field
458 * which is expected to be used frequently by other cpus via
459 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700460 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200461static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700462static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800463
Tejun Heof3421792010-07-02 10:03:51 +0200464/*
465 * Global cpu workqueue and nr_running counter for unbound gcwq. The
466 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
467 * workers have WORKER_UNBOUND set.
468 */
469static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700470static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
471 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
472};
Tejun Heof3421792010-07-02 10:03:51 +0200473
Tejun Heoc34056a2010-06-29 10:07:11 +0200474static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Tejun Heo8b03ae32010-06-29 10:07:12 +0200476static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Tejun Heof3421792010-07-02 10:03:51 +0200478 if (cpu != WORK_CPU_UNBOUND)
479 return &per_cpu(global_cwq, cpu);
480 else
481 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Tejun Heo63d95a92012-07-12 14:46:37 -0700484static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700485{
Tejun Heo63d95a92012-07-12 14:46:37 -0700486 int cpu = pool->gcwq->cpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700487 int idx = 0;
Tejun Heo63d95a92012-07-12 14:46:37 -0700488
Tejun Heof3421792010-07-02 10:03:51 +0200489 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700490 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200491 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700492 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700493}
494
Tejun Heo4690c4a2010-06-29 10:07:10 +0200495static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
496 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700497{
Tejun Heof3421792010-07-02 10:03:51 +0200498 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800499 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200500 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200501 } else if (likely(cpu == WORK_CPU_UNBOUND))
502 return wq->cpu_wq.single;
503 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700504}
505
Tejun Heo73f53c42010-06-29 10:07:11 +0200506static unsigned int work_color_to_flags(int color)
507{
508 return color << WORK_STRUCT_COLOR_SHIFT;
509}
510
511static int get_work_color(struct work_struct *work)
512{
513 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
514 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
515}
516
517static int work_next_color(int color)
518{
519 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
David Howells4594bf12006-12-07 11:33:26 +0000522/*
Tejun Heoe1201532010-07-22 14:14:25 +0200523 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
524 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
525 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200526 *
527 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
528 * cwq, cpu or clear work->data. These functions should only be
529 * called while the work is owned - ie. while the PENDING bit is set.
530 *
531 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
532 * corresponding to a work. gcwq is available once the work has been
533 * queued anywhere after initialization. cwq is available only from
534 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000535 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200536static inline void set_work_data(struct work_struct *work, unsigned long data,
537 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000538{
David Howells4594bf12006-12-07 11:33:26 +0000539 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000541}
David Howells365970a2006-11-22 14:54:49 +0000542
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543static void set_work_cwq(struct work_struct *work,
544 struct cpu_workqueue_struct *cwq,
545 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200546{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200547 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200548 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200549}
550
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000552{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
554}
555
556static void clear_work_data(struct work_struct *work)
557{
558 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
559}
560
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
562{
Tejun Heoe1201532010-07-22 14:14:25 +0200563 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564
Tejun Heoe1201532010-07-22 14:14:25 +0200565 if (data & WORK_STRUCT_CWQ)
566 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
567 else
568 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569}
570
571static struct global_cwq *get_work_gcwq(struct work_struct *work)
572{
Tejun Heoe1201532010-07-22 14:14:25 +0200573 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574 unsigned int cpu;
575
Tejun Heoe1201532010-07-22 14:14:25 +0200576 if (data & WORK_STRUCT_CWQ)
577 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700578 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579
580 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200581 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200582 return NULL;
583
Tejun Heof3421792010-07-02 10:03:51 +0200584 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000586}
587
588/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200589 * Policy functions. These define the policies on how the global
590 * worker pool is managed. Unless noted otherwise, these functions
591 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000592 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200593
Tejun Heo63d95a92012-07-12 14:46:37 -0700594static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000595{
Tejun Heo63d95a92012-07-12 14:46:37 -0700596 return !atomic_read(get_pool_nr_running(pool)) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700597 (pool->flags & POOL_HIGHPRI_PENDING);
David Howells365970a2006-11-22 14:54:49 +0000598}
599
Tejun Heoe22bee72010-06-29 10:07:14 +0200600/*
601 * Need to wake up a worker? Called from anything but currently
602 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700603 *
604 * Note that, because unbound workers never contribute to nr_running, this
605 * function will always return %true for unbound gcwq as long as the
606 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200607 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700608static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000609{
Tejun Heo63d95a92012-07-12 14:46:37 -0700610 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000611}
612
Tejun Heoe22bee72010-06-29 10:07:14 +0200613/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700614static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200615{
Tejun Heo63d95a92012-07-12 14:46:37 -0700616 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200617}
618
619/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700620static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200621{
Tejun Heo63d95a92012-07-12 14:46:37 -0700622 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200623
Tejun Heo63d95a92012-07-12 14:46:37 -0700624 return !list_empty(&pool->worklist) &&
Tejun Heo30310042010-10-11 11:51:57 +0200625 (atomic_read(nr_running) <= 1 ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700626 (pool->flags & POOL_HIGHPRI_PENDING));
Tejun Heoe22bee72010-06-29 10:07:14 +0200627}
628
629/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700630static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200631{
Tejun Heo63d95a92012-07-12 14:46:37 -0700632 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200633}
634
635/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700636static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200637{
Tejun Heo63d95a92012-07-12 14:46:37 -0700638 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700639 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200640}
641
642/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700643static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200644{
Tejun Heo11ebea52012-07-12 14:46:37 -0700645 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700646 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
647 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200648
649 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
650}
651
652/*
653 * Wake up functions.
654 */
655
Tejun Heo7e116292010-06-29 10:07:13 +0200656/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700657static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200658{
Tejun Heo63d95a92012-07-12 14:46:37 -0700659 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200660 return NULL;
661
Tejun Heo63d95a92012-07-12 14:46:37 -0700662 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200663}
664
665/**
666 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700667 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200668 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700669 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200670 *
671 * CONTEXT:
672 * spin_lock_irq(gcwq->lock).
673 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700674static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200675{
Tejun Heo63d95a92012-07-12 14:46:37 -0700676 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200677
678 if (likely(worker))
679 wake_up_process(worker->task);
680}
681
Tejun Heo4690c4a2010-06-29 10:07:10 +0200682/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200683 * wq_worker_waking_up - a worker is waking up
684 * @task: task waking up
685 * @cpu: CPU @task is waking up to
686 *
687 * This function is called during try_to_wake_up() when a worker is
688 * being awoken.
689 *
690 * CONTEXT:
691 * spin_lock_irq(rq->lock)
692 */
693void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
694{
695 struct worker *worker = kthread_data(task);
696
Steven Rostedt2d646722010-12-03 23:12:33 -0500697 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700698 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200699}
700
701/**
702 * wq_worker_sleeping - a worker is going to sleep
703 * @task: task going to sleep
704 * @cpu: CPU in question, must be the current CPU number
705 *
706 * This function is called during schedule() when a busy worker is
707 * going to sleep. Worker on the same cpu can be woken up by
708 * returning pointer to its task.
709 *
710 * CONTEXT:
711 * spin_lock_irq(rq->lock)
712 *
713 * RETURNS:
714 * Worker task on @cpu to wake up, %NULL if none.
715 */
716struct task_struct *wq_worker_sleeping(struct task_struct *task,
717 unsigned int cpu)
718{
719 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700720 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700721 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200722
Steven Rostedt2d646722010-12-03 23:12:33 -0500723 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200724 return NULL;
725
726 /* this can only happen on the local cpu */
727 BUG_ON(cpu != raw_smp_processor_id());
728
729 /*
730 * The counterpart of the following dec_and_test, implied mb,
731 * worklist not empty test sequence is in insert_work().
732 * Please read comment there.
733 *
734 * NOT_RUNNING is clear. This means that trustee is not in
735 * charge and we're running on the local cpu w/ rq lock held
736 * and preemption disabled, which in turn means that none else
737 * could be manipulating idle_list, so dereferencing idle_list
738 * without gcwq lock is safe.
739 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700740 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700741 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200742 return to_wakeup ? to_wakeup->task : NULL;
743}
744
745/**
746 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200747 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200748 * @flags: flags to set
749 * @wakeup: wakeup an idle worker if necessary
750 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200751 * Set @flags in @worker->flags and adjust nr_running accordingly. If
752 * nr_running becomes zero and @wakeup is %true, an idle worker is
753 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200754 *
Tejun Heocb444762010-07-02 10:03:50 +0200755 * CONTEXT:
756 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200757 */
758static inline void worker_set_flags(struct worker *worker, unsigned int flags,
759 bool wakeup)
760{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700761 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200762
Tejun Heocb444762010-07-02 10:03:50 +0200763 WARN_ON_ONCE(worker->task != current);
764
Tejun Heoe22bee72010-06-29 10:07:14 +0200765 /*
766 * If transitioning into NOT_RUNNING, adjust nr_running and
767 * wake up an idle worker as necessary if requested by
768 * @wakeup.
769 */
770 if ((flags & WORKER_NOT_RUNNING) &&
771 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700772 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200773
774 if (wakeup) {
775 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700776 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700777 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200778 } else
779 atomic_dec(nr_running);
780 }
781
Tejun Heod302f012010-06-29 10:07:13 +0200782 worker->flags |= flags;
783}
784
785/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200786 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200787 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200788 * @flags: flags to clear
789 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200790 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200791 *
Tejun Heocb444762010-07-02 10:03:50 +0200792 * CONTEXT:
793 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200794 */
795static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
796{
Tejun Heo63d95a92012-07-12 14:46:37 -0700797 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200798 unsigned int oflags = worker->flags;
799
Tejun Heocb444762010-07-02 10:03:50 +0200800 WARN_ON_ONCE(worker->task != current);
801
Tejun Heod302f012010-06-29 10:07:13 +0200802 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200803
Tejun Heo42c025f2011-01-11 15:58:49 +0100804 /*
805 * If transitioning out of NOT_RUNNING, increment nr_running. Note
806 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
807 * of multiple flags, not a single flag.
808 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200809 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
810 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700811 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200812}
813
814/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200815 * busy_worker_head - return the busy hash head for a work
816 * @gcwq: gcwq of interest
817 * @work: work to be hashed
818 *
819 * Return hash head of @gcwq for @work.
820 *
821 * CONTEXT:
822 * spin_lock_irq(gcwq->lock).
823 *
824 * RETURNS:
825 * Pointer to the hash head.
826 */
827static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
828 struct work_struct *work)
829{
830 const int base_shift = ilog2(sizeof(struct work_struct));
831 unsigned long v = (unsigned long)work;
832
833 /* simple shift and fold hash, do we need something better? */
834 v >>= base_shift;
835 v += v >> BUSY_WORKER_HASH_ORDER;
836 v &= BUSY_WORKER_HASH_MASK;
837
838 return &gcwq->busy_hash[v];
839}
840
841/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200842 * __find_worker_executing_work - find worker which is executing a work
843 * @gcwq: gcwq of interest
844 * @bwh: hash head as returned by busy_worker_head()
845 * @work: work to find worker for
846 *
847 * Find a worker which is executing @work on @gcwq. @bwh should be
848 * the hash head obtained by calling busy_worker_head() with the same
849 * work.
850 *
851 * CONTEXT:
852 * spin_lock_irq(gcwq->lock).
853 *
854 * RETURNS:
855 * Pointer to worker which is executing @work if found, NULL
856 * otherwise.
857 */
858static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
859 struct hlist_head *bwh,
860 struct work_struct *work)
861{
862 struct worker *worker;
863 struct hlist_node *tmp;
864
865 hlist_for_each_entry(worker, tmp, bwh, hentry)
866 if (worker->current_work == work)
867 return worker;
868 return NULL;
869}
870
871/**
872 * find_worker_executing_work - find worker which is executing a work
873 * @gcwq: gcwq of interest
874 * @work: work to find worker for
875 *
876 * Find a worker which is executing @work on @gcwq. This function is
877 * identical to __find_worker_executing_work() except that this
878 * function calculates @bwh itself.
879 *
880 * CONTEXT:
881 * spin_lock_irq(gcwq->lock).
882 *
883 * RETURNS:
884 * Pointer to worker which is executing @work if found, NULL
885 * otherwise.
886 */
887static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
888 struct work_struct *work)
889{
890 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
891 work);
892}
893
894/**
Tejun Heo63d95a92012-07-12 14:46:37 -0700895 * pool_determine_ins_pos - find insertion position
896 * @pool: pool of interest
Tejun Heo649027d2010-06-29 10:07:14 +0200897 * @cwq: cwq a work is being queued for
898 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700899 * A work for @cwq is about to be queued on @pool, determine insertion
Tejun Heo649027d2010-06-29 10:07:14 +0200900 * position for the work. If @cwq is for HIGHPRI wq, the work is
901 * queued at the head of the queue but in FIFO order with respect to
902 * other HIGHPRI works; otherwise, at the end of the queue. This
Tejun Heo11ebea52012-07-12 14:46:37 -0700903 * function also sets POOL_HIGHPRI_PENDING flag to hint @pool that
Tejun Heo649027d2010-06-29 10:07:14 +0200904 * there are HIGHPRI works pending.
905 *
906 * CONTEXT:
907 * spin_lock_irq(gcwq->lock).
908 *
909 * RETURNS:
910 * Pointer to inserstion position.
911 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700912static inline struct list_head *pool_determine_ins_pos(struct worker_pool *pool,
Tejun Heo649027d2010-06-29 10:07:14 +0200913 struct cpu_workqueue_struct *cwq)
914{
915 struct work_struct *twork;
916
917 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
Tejun Heo63d95a92012-07-12 14:46:37 -0700918 return &pool->worklist;
Tejun Heo649027d2010-06-29 10:07:14 +0200919
Tejun Heo63d95a92012-07-12 14:46:37 -0700920 list_for_each_entry(twork, &pool->worklist, entry) {
Tejun Heo649027d2010-06-29 10:07:14 +0200921 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
922
923 if (!(tcwq->wq->flags & WQ_HIGHPRI))
924 break;
925 }
926
Tejun Heo11ebea52012-07-12 14:46:37 -0700927 pool->flags |= POOL_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +0200928 return &twork->entry;
929}
930
931/**
Tejun Heo7e116292010-06-29 10:07:13 +0200932 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200933 * @cwq: cwq @work belongs to
934 * @work: work to insert
935 * @head: insertion point
936 * @extra_flags: extra WORK_STRUCT_* flags to set
937 *
Tejun Heo7e116292010-06-29 10:07:13 +0200938 * Insert @work which belongs to @cwq into @gcwq after @head.
939 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200940 *
941 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200942 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200943 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700944static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200945 struct work_struct *work, struct list_head *head,
946 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700947{
Tejun Heo63d95a92012-07-12 14:46:37 -0700948 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100949
Tejun Heo4690c4a2010-06-29 10:07:10 +0200950 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200951 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200952
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700953 /*
954 * Ensure that we get the right work->data if we see the
955 * result of list_add() below, see try_to_grab_pending().
956 */
957 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200958
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700959 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200960
961 /*
962 * Ensure either worker_sched_deactivated() sees the above
963 * list_add_tail() or we see zero nr_running to avoid workers
964 * lying around lazily while there are works to be processed.
965 */
966 smp_mb();
967
Tejun Heo63d95a92012-07-12 14:46:37 -0700968 if (__need_more_worker(pool))
969 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700970}
971
Tejun Heoc8efcc22010-12-20 19:32:04 +0100972/*
973 * Test whether @work is being queued from another work executing on the
974 * same workqueue. This is rather expensive and should only be used from
975 * cold paths.
976 */
977static bool is_chained_work(struct workqueue_struct *wq)
978{
979 unsigned long flags;
980 unsigned int cpu;
981
982 for_each_gcwq_cpu(cpu) {
983 struct global_cwq *gcwq = get_gcwq(cpu);
984 struct worker *worker;
985 struct hlist_node *pos;
986 int i;
987
988 spin_lock_irqsave(&gcwq->lock, flags);
989 for_each_busy_worker(worker, i, pos, gcwq) {
990 if (worker->task != current)
991 continue;
992 spin_unlock_irqrestore(&gcwq->lock, flags);
993 /*
994 * I'm @worker, no locking necessary. See if @work
995 * is headed to the same workqueue.
996 */
997 return worker->current_cwq->wq == wq;
998 }
999 spin_unlock_irqrestore(&gcwq->lock, flags);
1000 }
1001 return false;
1002}
1003
Tejun Heo4690c4a2010-06-29 10:07:10 +02001004static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct work_struct *work)
1006{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001007 struct global_cwq *gcwq;
1008 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001009 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001010 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 unsigned long flags;
1012
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001013 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001014
Tejun Heoc8efcc22010-12-20 19:32:04 +01001015 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001016 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001017 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001018 return;
1019
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001020 /* determine gcwq to use */
1021 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001022 struct global_cwq *last_gcwq;
1023
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001024 if (unlikely(cpu == WORK_CPU_UNBOUND))
1025 cpu = raw_smp_processor_id();
1026
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001027 /*
1028 * It's multi cpu. If @wq is non-reentrant and @work
1029 * was previously on a different cpu, it might still
1030 * be running there, in which case the work needs to
1031 * be queued on that cpu to guarantee non-reentrance.
1032 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001033 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001034 if (wq->flags & WQ_NON_REENTRANT &&
1035 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1036 struct worker *worker;
1037
1038 spin_lock_irqsave(&last_gcwq->lock, flags);
1039
1040 worker = find_worker_executing_work(last_gcwq, work);
1041
1042 if (worker && worker->current_cwq->wq == wq)
1043 gcwq = last_gcwq;
1044 else {
1045 /* meh... not running there, queue here */
1046 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1047 spin_lock_irqsave(&gcwq->lock, flags);
1048 }
1049 } else
1050 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001051 } else {
1052 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1053 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001054 }
1055
1056 /* gcwq determined, get cwq and queue */
1057 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001058 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001059
Dan Carpenterf5b25522012-04-13 22:06:58 +03001060 if (WARN_ON(!list_empty(&work->entry))) {
1061 spin_unlock_irqrestore(&gcwq->lock, flags);
1062 return;
1063 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001064
Tejun Heo73f53c42010-06-29 10:07:11 +02001065 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001066 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001067
1068 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001069 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001070 cwq->nr_active++;
Tejun Heo63d95a92012-07-12 14:46:37 -07001071 worklist = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001072 } else {
1073 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001074 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001075 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001076
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001077 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001078
Tejun Heo8b03ae32010-06-29 10:07:12 +02001079 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001082/**
1083 * queue_work - queue work on a workqueue
1084 * @wq: workqueue to use
1085 * @work: work to queue
1086 *
Alan Stern057647f2006-10-28 10:38:58 -07001087 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001089 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1090 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001092int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001094 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001096 ret = queue_work_on(get_cpu(), wq, work);
1097 put_cpu();
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return ret;
1100}
Dave Jonesae90dd52006-06-30 01:40:45 -04001101EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Zhang Ruic1a220e2008-07-23 21:28:39 -07001103/**
1104 * queue_work_on - queue work on specific cpu
1105 * @cpu: CPU number to execute work on
1106 * @wq: workqueue to use
1107 * @work: work to queue
1108 *
1109 * Returns 0 if @work was already on a queue, non-zero otherwise.
1110 *
1111 * We queue the work to a specific CPU, the caller must ensure it
1112 * can't go away.
1113 */
1114int
1115queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1116{
1117 int ret = 0;
1118
Tejun Heo22df02b2010-06-29 10:07:10 +02001119 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001120 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001121 ret = 1;
1122 }
1123 return ret;
1124}
1125EXPORT_SYMBOL_GPL(queue_work_on);
1126
Li Zefan6d141c32008-02-08 04:21:09 -08001127static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
David Howells52bad642006-11-22 14:54:01 +00001129 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001130 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Tejun Heo4690c4a2010-06-29 10:07:10 +02001132 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001135/**
1136 * queue_delayed_work - queue work on a workqueue after delay
1137 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001138 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001139 * @delay: number of jiffies to wait before queueing
1140 *
Alan Stern057647f2006-10-28 10:38:58 -07001141 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001142 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001143int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001144 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
David Howells52bad642006-11-22 14:54:01 +00001146 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001147 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001149 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
Dave Jonesae90dd52006-06-30 01:40:45 -04001151EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001153/**
1154 * queue_delayed_work_on - queue work on specific CPU after delay
1155 * @cpu: CPU number to execute work on
1156 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001157 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001158 * @delay: number of jiffies to wait before queueing
1159 *
Alan Stern057647f2006-10-28 10:38:58 -07001160 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001161 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001162int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001163 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001164{
1165 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001166 struct timer_list *timer = &dwork->timer;
1167 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001168
Tejun Heo22df02b2010-06-29 10:07:10 +02001169 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001170 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001171
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001172 BUG_ON(timer_pending(timer));
1173 BUG_ON(!list_empty(&work->entry));
1174
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001175 timer_stats_timer_set_start_info(&dwork->timer);
1176
Tejun Heo7a22ad72010-06-29 10:07:13 +02001177 /*
1178 * This stores cwq for the moment, for the timer_fn.
1179 * Note that the work's gcwq is preserved to allow
1180 * reentrance detection for delayed works.
1181 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001182 if (!(wq->flags & WQ_UNBOUND)) {
1183 struct global_cwq *gcwq = get_work_gcwq(work);
1184
1185 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1186 lcpu = gcwq->cpu;
1187 else
1188 lcpu = raw_smp_processor_id();
1189 } else
1190 lcpu = WORK_CPU_UNBOUND;
1191
Tejun Heo7a22ad72010-06-29 10:07:13 +02001192 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001193
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001194 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001195 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001196 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001197
1198 if (unlikely(cpu >= 0))
1199 add_timer_on(timer, cpu);
1200 else
1201 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001202 ret = 1;
1203 }
1204 return ret;
1205}
Dave Jonesae90dd52006-06-30 01:40:45 -04001206EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Tejun Heoc8e55f32010-06-29 10:07:12 +02001208/**
1209 * worker_enter_idle - enter idle state
1210 * @worker: worker which is entering idle state
1211 *
1212 * @worker is entering idle state. Update stats and idle timer if
1213 * necessary.
1214 *
1215 * LOCKING:
1216 * spin_lock_irq(gcwq->lock).
1217 */
1218static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001220 struct worker_pool *pool = worker->pool;
1221 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Tejun Heoc8e55f32010-06-29 10:07:12 +02001223 BUG_ON(worker->flags & WORKER_IDLE);
1224 BUG_ON(!list_empty(&worker->entry) &&
1225 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Tejun Heocb444762010-07-02 10:03:50 +02001227 /* can't use worker_set_flags(), also called from start_worker() */
1228 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001229 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001230 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001231
Tejun Heoc8e55f32010-06-29 10:07:12 +02001232 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001233 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001234
Tejun Heoe22bee72010-06-29 10:07:14 +02001235 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001236 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001237 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001238 jiffies + IDLE_WORKER_TIMEOUT);
1239 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001240 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001241
Tejun Heo544ecf32012-05-14 15:04:50 -07001242 /*
1243 * Sanity check nr_running. Because trustee releases gcwq->lock
1244 * between setting %WORKER_ROGUE and zapping nr_running, the
1245 * warning may trigger spuriously. Check iff trustee is idle.
1246 */
1247 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001248 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001249 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Tejun Heoc8e55f32010-06-29 10:07:12 +02001252/**
1253 * worker_leave_idle - leave idle state
1254 * @worker: worker which is leaving idle state
1255 *
1256 * @worker is leaving idle state. Update stats.
1257 *
1258 * LOCKING:
1259 * spin_lock_irq(gcwq->lock).
1260 */
1261static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001263 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Tejun Heoc8e55f32010-06-29 10:07:12 +02001265 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001266 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001267 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001268 list_del_init(&worker->entry);
1269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Tejun Heoe22bee72010-06-29 10:07:14 +02001271/**
1272 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1273 * @worker: self
1274 *
1275 * Works which are scheduled while the cpu is online must at least be
1276 * scheduled to a worker which is bound to the cpu so that if they are
1277 * flushed from cpu callbacks while cpu is going down, they are
1278 * guaranteed to execute on the cpu.
1279 *
1280 * This function is to be used by rogue workers and rescuers to bind
1281 * themselves to the target cpu and may race with cpu going down or
1282 * coming online. kthread_bind() can't be used because it may put the
1283 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1284 * verbatim as it's best effort and blocking and gcwq may be
1285 * [dis]associated in the meantime.
1286 *
1287 * This function tries set_cpus_allowed() and locks gcwq and verifies
1288 * the binding against GCWQ_DISASSOCIATED which is set during
1289 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1290 * idle state or fetches works without dropping lock, it can guarantee
1291 * the scheduling requirement described in the first paragraph.
1292 *
1293 * CONTEXT:
1294 * Might sleep. Called without any lock but returns with gcwq->lock
1295 * held.
1296 *
1297 * RETURNS:
1298 * %true if the associated gcwq is online (@worker is successfully
1299 * bound), %false if offline.
1300 */
1301static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001302__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001303{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001304 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001305 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Tejun Heoe22bee72010-06-29 10:07:14 +02001307 while (true) {
1308 /*
1309 * The following call may fail, succeed or succeed
1310 * without actually migrating the task to the cpu if
1311 * it races with cpu hotunplug operation. Verify
1312 * against GCWQ_DISASSOCIATED.
1313 */
Tejun Heof3421792010-07-02 10:03:51 +02001314 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1315 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001316
Tejun Heoe22bee72010-06-29 10:07:14 +02001317 spin_lock_irq(&gcwq->lock);
1318 if (gcwq->flags & GCWQ_DISASSOCIATED)
1319 return false;
1320 if (task_cpu(task) == gcwq->cpu &&
1321 cpumask_equal(&current->cpus_allowed,
1322 get_cpu_mask(gcwq->cpu)))
1323 return true;
1324 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001325
Tejun Heo5035b202011-04-29 18:08:37 +02001326 /*
1327 * We've raced with CPU hot[un]plug. Give it a breather
1328 * and retry migration. cond_resched() is required here;
1329 * otherwise, we might deadlock against cpu_stop trying to
1330 * bring down the CPU on non-preemptive kernel.
1331 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001332 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001333 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001334 }
1335}
1336
1337/*
1338 * Function for worker->rebind_work used to rebind rogue busy workers
1339 * to the associated cpu which is coming back online. This is
1340 * scheduled by cpu up but can race with other cpu hotplug operations
1341 * and may be executed twice without intervening cpu down.
1342 */
1343static void worker_rebind_fn(struct work_struct *work)
1344{
1345 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001346 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001347
1348 if (worker_maybe_bind_and_lock(worker))
1349 worker_clr_flags(worker, WORKER_REBIND);
1350
1351 spin_unlock_irq(&gcwq->lock);
1352}
1353
Tejun Heoc34056a2010-06-29 10:07:11 +02001354static struct worker *alloc_worker(void)
1355{
1356 struct worker *worker;
1357
1358 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001359 if (worker) {
1360 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001361 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001362 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1363 /* on creation a worker is in !idle && prep state */
1364 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001365 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001366 return worker;
1367}
1368
1369/**
1370 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001371 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001372 * @bind: whether to set affinity to @cpu or not
1373 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001374 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001375 * can be started by calling start_worker() or destroyed using
1376 * destroy_worker().
1377 *
1378 * CONTEXT:
1379 * Might sleep. Does GFP_KERNEL allocations.
1380 *
1381 * RETURNS:
1382 * Pointer to the newly created worker.
1383 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001384static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001385{
Tejun Heo63d95a92012-07-12 14:46:37 -07001386 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001387 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001388 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001389 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001390
Tejun Heo8b03ae32010-06-29 10:07:12 +02001391 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001392 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001393 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001394 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001395 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001396 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001397 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001398 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001399
1400 worker = alloc_worker();
1401 if (!worker)
1402 goto fail;
1403
Tejun Heobd7bdd42012-07-12 14:46:37 -07001404 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001405 worker->id = id;
1406
Tejun Heof3421792010-07-02 10:03:51 +02001407 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001408 worker->task = kthread_create_on_node(worker_thread,
1409 worker,
1410 cpu_to_node(gcwq->cpu),
1411 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001412 else
1413 worker->task = kthread_create(worker_thread, worker,
1414 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001415 if (IS_ERR(worker->task))
1416 goto fail;
1417
Tejun Heodb7bccf2010-06-29 10:07:12 +02001418 /*
1419 * A rogue worker will become a regular one if CPU comes
1420 * online later on. Make sure every worker has
1421 * PF_THREAD_BOUND set.
1422 */
Tejun Heof3421792010-07-02 10:03:51 +02001423 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001424 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001425 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001426 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001427 if (on_unbound_cpu)
1428 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001430
Tejun Heoc34056a2010-06-29 10:07:11 +02001431 return worker;
1432fail:
1433 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001434 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001435 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001436 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001437 }
1438 kfree(worker);
1439 return NULL;
1440}
1441
1442/**
1443 * start_worker - start a newly created worker
1444 * @worker: worker to start
1445 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001446 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001447 *
1448 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001449 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001450 */
1451static void start_worker(struct worker *worker)
1452{
Tejun Heocb444762010-07-02 10:03:50 +02001453 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001454 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001455 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001456 wake_up_process(worker->task);
1457}
1458
1459/**
1460 * destroy_worker - destroy a workqueue worker
1461 * @worker: worker to be destroyed
1462 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001463 * Destroy @worker and adjust @gcwq stats accordingly.
1464 *
1465 * CONTEXT:
1466 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001467 */
1468static void destroy_worker(struct worker *worker)
1469{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001470 struct worker_pool *pool = worker->pool;
1471 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001472 int id = worker->id;
1473
1474 /* sanity check frenzy */
1475 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001476 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001477
Tejun Heoc8e55f32010-06-29 10:07:12 +02001478 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001479 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001481 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001482
1483 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001484 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001485
1486 spin_unlock_irq(&gcwq->lock);
1487
Tejun Heoc34056a2010-06-29 10:07:11 +02001488 kthread_stop(worker->task);
1489 kfree(worker);
1490
Tejun Heo8b03ae32010-06-29 10:07:12 +02001491 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001492 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001493}
1494
Tejun Heo63d95a92012-07-12 14:46:37 -07001495static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001496{
Tejun Heo63d95a92012-07-12 14:46:37 -07001497 struct worker_pool *pool = (void *)__pool;
1498 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001499
1500 spin_lock_irq(&gcwq->lock);
1501
Tejun Heo63d95a92012-07-12 14:46:37 -07001502 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001503 struct worker *worker;
1504 unsigned long expires;
1505
1506 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001507 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001508 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1509
1510 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001511 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001512 else {
1513 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001514 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001515 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001516 }
1517 }
1518
1519 spin_unlock_irq(&gcwq->lock);
1520}
1521
1522static bool send_mayday(struct work_struct *work)
1523{
1524 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1525 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001526 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001527
1528 if (!(wq->flags & WQ_RESCUER))
1529 return false;
1530
1531 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001532 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001533 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1534 if (cpu == WORK_CPU_UNBOUND)
1535 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001536 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001537 wake_up_process(wq->rescuer->task);
1538 return true;
1539}
1540
Tejun Heo63d95a92012-07-12 14:46:37 -07001541static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001542{
Tejun Heo63d95a92012-07-12 14:46:37 -07001543 struct worker_pool *pool = (void *)__pool;
1544 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001545 struct work_struct *work;
1546
1547 spin_lock_irq(&gcwq->lock);
1548
Tejun Heo63d95a92012-07-12 14:46:37 -07001549 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001550 /*
1551 * We've been trying to create a new worker but
1552 * haven't been successful. We might be hitting an
1553 * allocation deadlock. Send distress signals to
1554 * rescuers.
1555 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001556 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001557 send_mayday(work);
1558 }
1559
1560 spin_unlock_irq(&gcwq->lock);
1561
Tejun Heo63d95a92012-07-12 14:46:37 -07001562 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001563}
1564
1565/**
1566 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001567 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001568 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001569 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001570 * have at least one idle worker on return from this function. If
1571 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001572 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 * possible allocation deadlock.
1574 *
1575 * On return, need_to_create_worker() is guaranteed to be false and
1576 * may_start_working() true.
1577 *
1578 * LOCKING:
1579 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1580 * multiple times. Does GFP_KERNEL allocations. Called only from
1581 * manager.
1582 *
1583 * RETURNS:
1584 * false if no action was taken and gcwq->lock stayed locked, true
1585 * otherwise.
1586 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001587static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001588__releases(&gcwq->lock)
1589__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001590{
Tejun Heo63d95a92012-07-12 14:46:37 -07001591 struct global_cwq *gcwq = pool->gcwq;
1592
1593 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001594 return false;
1595restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001596 spin_unlock_irq(&gcwq->lock);
1597
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001599 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001600
1601 while (true) {
1602 struct worker *worker;
1603
Tejun Heo63d95a92012-07-12 14:46:37 -07001604 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001606 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001607 spin_lock_irq(&gcwq->lock);
1608 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001609 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 return true;
1611 }
1612
Tejun Heo63d95a92012-07-12 14:46:37 -07001613 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 break;
1615
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 __set_current_state(TASK_INTERRUPTIBLE);
1617 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001618
Tejun Heo63d95a92012-07-12 14:46:37 -07001619 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001620 break;
1621 }
1622
Tejun Heo63d95a92012-07-12 14:46:37 -07001623 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001624 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001625 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001626 goto restart;
1627 return true;
1628}
1629
1630/**
1631 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001632 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001634 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001635 * IDLE_WORKER_TIMEOUT.
1636 *
1637 * LOCKING:
1638 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1639 * multiple times. Called only from manager.
1640 *
1641 * RETURNS:
1642 * false if no action was taken and gcwq->lock stayed locked, true
1643 * otherwise.
1644 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001645static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001646{
1647 bool ret = false;
1648
Tejun Heo63d95a92012-07-12 14:46:37 -07001649 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001650 struct worker *worker;
1651 unsigned long expires;
1652
Tejun Heo63d95a92012-07-12 14:46:37 -07001653 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001654 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1655
1656 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001657 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001658 break;
1659 }
1660
1661 destroy_worker(worker);
1662 ret = true;
1663 }
1664
1665 return ret;
1666}
1667
1668/**
1669 * manage_workers - manage worker pool
1670 * @worker: self
1671 *
1672 * Assume the manager role and manage gcwq worker pool @worker belongs
1673 * to. At any given time, there can be only zero or one manager per
1674 * gcwq. The exclusion is handled automatically by this function.
1675 *
1676 * The caller can safely start processing works on false return. On
1677 * true return, it's guaranteed that need_to_create_worker() is false
1678 * and may_start_working() is true.
1679 *
1680 * CONTEXT:
1681 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1682 * multiple times. Does GFP_KERNEL allocations.
1683 *
1684 * RETURNS:
1685 * false if no action was taken and gcwq->lock stayed locked, true if
1686 * some action was taken.
1687 */
1688static bool manage_workers(struct worker *worker)
1689{
Tejun Heo63d95a92012-07-12 14:46:37 -07001690 struct worker_pool *pool = worker->pool;
1691 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001692 bool ret = false;
1693
Tejun Heo11ebea52012-07-12 14:46:37 -07001694 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001695 return ret;
1696
Tejun Heo11ebea52012-07-12 14:46:37 -07001697 pool->flags &= ~POOL_MANAGE_WORKERS;
1698 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001699
1700 /*
1701 * Destroy and then create so that may_start_working() is true
1702 * on return.
1703 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001704 ret |= maybe_destroy_workers(pool);
1705 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001706
Tejun Heo11ebea52012-07-12 14:46:37 -07001707 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001708
1709 /*
1710 * The trustee might be waiting to take over the manager
1711 * position, tell it we're done.
1712 */
1713 if (unlikely(gcwq->trustee))
1714 wake_up_all(&gcwq->trustee_wait);
1715
1716 return ret;
1717}
1718
Tejun Heoa62428c2010-06-29 10:07:10 +02001719/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001720 * move_linked_works - move linked works to a list
1721 * @work: start of series of works to be scheduled
1722 * @head: target list to append @work to
1723 * @nextp: out paramter for nested worklist walking
1724 *
1725 * Schedule linked works starting from @work to @head. Work series to
1726 * be scheduled starts at @work and includes any consecutive work with
1727 * WORK_STRUCT_LINKED set in its predecessor.
1728 *
1729 * If @nextp is not NULL, it's updated to point to the next work of
1730 * the last scheduled work. This allows move_linked_works() to be
1731 * nested inside outer list_for_each_entry_safe().
1732 *
1733 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001734 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001735 */
1736static void move_linked_works(struct work_struct *work, struct list_head *head,
1737 struct work_struct **nextp)
1738{
1739 struct work_struct *n;
1740
1741 /*
1742 * Linked worklist will always end before the end of the list,
1743 * use NULL for list head.
1744 */
1745 list_for_each_entry_safe_from(work, n, NULL, entry) {
1746 list_move_tail(&work->entry, head);
1747 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1748 break;
1749 }
1750
1751 /*
1752 * If we're already inside safe list traversal and have moved
1753 * multiple works to the scheduled queue, the next position
1754 * needs to be updated.
1755 */
1756 if (nextp)
1757 *nextp = n;
1758}
1759
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001760static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1761{
1762 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1763 struct work_struct, entry);
Tejun Heo63d95a92012-07-12 14:46:37 -07001764 struct list_head *pos = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001765
Tejun Heocdadf002010-10-05 10:49:55 +02001766 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001767 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001768 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001769 cwq->nr_active++;
1770}
1771
Tejun Heoaffee4b2010-06-29 10:07:12 +02001772/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001773 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1774 * @cwq: cwq of interest
1775 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001776 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001777 *
1778 * A work either has completed or is removed from pending queue,
1779 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1780 *
1781 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001782 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001783 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001784static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1785 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001786{
1787 /* ignore uncolored works */
1788 if (color == WORK_NO_COLOR)
1789 return;
1790
1791 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001792
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001793 if (!delayed) {
1794 cwq->nr_active--;
1795 if (!list_empty(&cwq->delayed_works)) {
1796 /* one down, submit a delayed one */
1797 if (cwq->nr_active < cwq->max_active)
1798 cwq_activate_first_delayed(cwq);
1799 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001800 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001801
1802 /* is flush in progress and are we at the flushing tip? */
1803 if (likely(cwq->flush_color != color))
1804 return;
1805
1806 /* are there still in-flight works? */
1807 if (cwq->nr_in_flight[color])
1808 return;
1809
1810 /* this cwq is done, clear flush_color */
1811 cwq->flush_color = -1;
1812
1813 /*
1814 * If this was the last cwq, wake up the first flusher. It
1815 * will handle the rest.
1816 */
1817 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1818 complete(&cwq->wq->first_flusher->done);
1819}
1820
1821/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001822 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001823 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001824 * @work: work to process
1825 *
1826 * Process @work. This function contains all the logics necessary to
1827 * process a single work including synchronization against and
1828 * interaction with other workers on the same cpu, queueing and
1829 * flushing. As long as context requirement is met, any worker can
1830 * call this function to process a work.
1831 *
1832 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001833 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001834 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001835static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001836__releases(&gcwq->lock)
1837__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001838{
Tejun Heo7e116292010-06-29 10:07:13 +02001839 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001840 struct worker_pool *pool = worker->pool;
1841 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001842 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001843 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001844 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001845 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001846 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001847#ifdef CONFIG_LOCKDEP
1848 /*
1849 * It is permissible to free the struct work_struct from
1850 * inside the function that is called from it, this we need to
1851 * take into account for lockdep too. To avoid bogus "held
1852 * lock freed" warnings as well as problems when looking into
1853 * work->lockdep_map, make a copy and use that here.
1854 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001855 struct lockdep_map lockdep_map;
1856
1857 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001858#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001859 /*
1860 * A single work shouldn't be executed concurrently by
1861 * multiple workers on a single cpu. Check whether anyone is
1862 * already processing the work. If so, defer the work to the
1863 * currently executing one.
1864 */
1865 collision = __find_worker_executing_work(gcwq, bwh, work);
1866 if (unlikely(collision)) {
1867 move_linked_works(work, &collision->scheduled, NULL);
1868 return;
1869 }
1870
Tejun Heoa62428c2010-06-29 10:07:10 +02001871 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001872 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001873 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001874 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001875 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001876 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001877
Tejun Heo7a22ad72010-06-29 10:07:13 +02001878 /* record the current cpu number in the work data and dequeue */
1879 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001880 list_del_init(&work->entry);
1881
Tejun Heo649027d2010-06-29 10:07:14 +02001882 /*
1883 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1884 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1885 */
Tejun Heo11ebea52012-07-12 14:46:37 -07001886 if (unlikely(pool->flags & POOL_HIGHPRI_PENDING)) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001887 struct work_struct *nwork = list_first_entry(&pool->worklist,
1888 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001889
Tejun Heobd7bdd42012-07-12 14:46:37 -07001890 if (!list_empty(&pool->worklist) &&
Tejun Heo649027d2010-06-29 10:07:14 +02001891 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
Tejun Heo63d95a92012-07-12 14:46:37 -07001892 wake_up_worker(pool);
Tejun Heo649027d2010-06-29 10:07:14 +02001893 else
Tejun Heo11ebea52012-07-12 14:46:37 -07001894 pool->flags &= ~POOL_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +02001895 }
1896
Tejun Heofb0e7be2010-06-29 10:07:15 +02001897 /*
1898 * CPU intensive works don't participate in concurrency
1899 * management. They're the scheduler's responsibility.
1900 */
1901 if (unlikely(cpu_intensive))
1902 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1903
Tejun Heo974271c2012-07-12 14:46:37 -07001904 /*
1905 * Unbound gcwq isn't concurrency managed and work items should be
1906 * executed ASAP. Wake up another worker if necessary.
1907 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1909 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07001910
Tejun Heo8b03ae32010-06-29 10:07:12 +02001911 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001912
Tejun Heoa62428c2010-06-29 10:07:10 +02001913 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001914 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001915 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001916 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001917 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001918 /*
1919 * While we must be careful to not use "work" after this, the trace
1920 * point will only record its address.
1921 */
1922 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001923 lock_map_release(&lockdep_map);
1924 lock_map_release(&cwq->wq->lockdep_map);
1925
1926 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1927 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1928 "%s/0x%08x/%d\n",
1929 current->comm, preempt_count(), task_pid_nr(current));
1930 printk(KERN_ERR " last function: ");
1931 print_symbol("%s\n", (unsigned long)f);
1932 debug_show_held_locks(current);
1933 dump_stack();
1934 }
1935
Tejun Heo8b03ae32010-06-29 10:07:12 +02001936 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001937
Tejun Heofb0e7be2010-06-29 10:07:15 +02001938 /* clear cpu intensive status */
1939 if (unlikely(cpu_intensive))
1940 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1941
Tejun Heoa62428c2010-06-29 10:07:10 +02001942 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001943 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001944 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001945 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001946 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001947}
1948
Tejun Heoaffee4b2010-06-29 10:07:12 +02001949/**
1950 * process_scheduled_works - process scheduled works
1951 * @worker: self
1952 *
1953 * Process all scheduled works. Please note that the scheduled list
1954 * may change while processing a work, so this function repeatedly
1955 * fetches a work from the top and executes it.
1956 *
1957 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001958 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001959 * multiple times.
1960 */
1961static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001963 while (!list_empty(&worker->scheduled)) {
1964 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001966 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Tejun Heo4690c4a2010-06-29 10:07:10 +02001970/**
1971 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001972 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001973 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 * The gcwq worker thread function. There's a single dynamic pool of
1975 * these per each cpu. These workers process all works regardless of
1976 * their specific target workqueue. The only exception is works which
1977 * belong to workqueues with a rescuer which will be explained in
1978 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001979 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001980static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Tejun Heoc34056a2010-06-29 10:07:11 +02001982 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001983 struct worker_pool *pool = worker->pool;
1984 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 /* tell the scheduler that this is a workqueue worker */
1987 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001988woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001989 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Tejun Heoc8e55f32010-06-29 10:07:12 +02001991 /* DIE can be set only while we're idle, checking here is enough */
1992 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001993 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
Tejun Heoc8e55f32010-06-29 10:07:12 +02001998 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001999recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002001 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002002 goto sleep;
2003
2004 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002005 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002006 goto recheck;
2007
Tejun Heoc8e55f32010-06-29 10:07:12 +02002008 /*
2009 * ->scheduled list can only be filled while a worker is
2010 * preparing to process a work or actually processing it.
2011 * Make sure nobody diddled with it while I was sleeping.
2012 */
2013 BUG_ON(!list_empty(&worker->scheduled));
2014
Tejun Heoe22bee72010-06-29 10:07:14 +02002015 /*
2016 * When control reaches this point, we're guaranteed to have
2017 * at least one idle worker or that someone else has already
2018 * assumed the manager role.
2019 */
2020 worker_clr_flags(worker, WORKER_PREP);
2021
2022 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002023 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002024 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002025 struct work_struct, entry);
2026
2027 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2028 /* optimization path, not strictly necessary */
2029 process_one_work(worker, work);
2030 if (unlikely(!list_empty(&worker->scheduled)))
2031 process_scheduled_works(worker);
2032 } else {
2033 move_linked_works(work, &worker->scheduled, NULL);
2034 process_scheduled_works(worker);
2035 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002036 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002037
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002039sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002040 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002042
Tejun Heoc8e55f32010-06-29 10:07:12 +02002043 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002044 * gcwq->lock is held and there's no work to process and no
2045 * need to manage, sleep. Workers are woken up only while
2046 * holding gcwq->lock or from local cpu, so setting the
2047 * current state before releasing gcwq->lock is enough to
2048 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002049 */
2050 worker_enter_idle(worker);
2051 __set_current_state(TASK_INTERRUPTIBLE);
2052 spin_unlock_irq(&gcwq->lock);
2053 schedule();
2054 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Tejun Heoe22bee72010-06-29 10:07:14 +02002057/**
2058 * rescuer_thread - the rescuer thread function
2059 * @__wq: the associated workqueue
2060 *
2061 * Workqueue rescuer thread function. There's one rescuer for each
2062 * workqueue which has WQ_RESCUER set.
2063 *
2064 * Regular work processing on a gcwq may block trying to create a new
2065 * worker which uses GFP_KERNEL allocation which has slight chance of
2066 * developing into deadlock if some works currently on the same queue
2067 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2068 * the problem rescuer solves.
2069 *
2070 * When such condition is possible, the gcwq summons rescuers of all
2071 * workqueues which have works queued on the gcwq and let them process
2072 * those works so that forward progress can be guaranteed.
2073 *
2074 * This should happen rarely.
2075 */
2076static int rescuer_thread(void *__wq)
2077{
2078 struct workqueue_struct *wq = __wq;
2079 struct worker *rescuer = wq->rescuer;
2080 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002081 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002082 unsigned int cpu;
2083
2084 set_user_nice(current, RESCUER_NICE_LEVEL);
2085repeat:
2086 set_current_state(TASK_INTERRUPTIBLE);
2087
2088 if (kthread_should_stop())
2089 return 0;
2090
Tejun Heof3421792010-07-02 10:03:51 +02002091 /*
2092 * See whether any cpu is asking for help. Unbounded
2093 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2094 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002095 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002096 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2097 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002098 struct worker_pool *pool = cwq->pool;
2099 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002100 struct work_struct *work, *n;
2101
2102 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002103 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002104
2105 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002106 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002107 worker_maybe_bind_and_lock(rescuer);
2108
2109 /*
2110 * Slurp in all works issued via this workqueue and
2111 * process'em.
2112 */
2113 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002114 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002115 if (get_work_cwq(work) == cwq)
2116 move_linked_works(work, scheduled, &n);
2117
2118 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002119
2120 /*
2121 * Leave this gcwq. If keep_working() is %true, notify a
2122 * regular worker; otherwise, we end up with 0 concurrency
2123 * and stalling the execution.
2124 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002125 if (keep_working(pool))
2126 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002127
Tejun Heoe22bee72010-06-29 10:07:14 +02002128 spin_unlock_irq(&gcwq->lock);
2129 }
2130
2131 schedule();
2132 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002135struct wq_barrier {
2136 struct work_struct work;
2137 struct completion done;
2138};
2139
2140static void wq_barrier_func(struct work_struct *work)
2141{
2142 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2143 complete(&barr->done);
2144}
2145
Tejun Heo4690c4a2010-06-29 10:07:10 +02002146/**
2147 * insert_wq_barrier - insert a barrier work
2148 * @cwq: cwq to insert barrier into
2149 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002150 * @target: target work to attach @barr to
2151 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002152 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002153 * @barr is linked to @target such that @barr is completed only after
2154 * @target finishes execution. Please note that the ordering
2155 * guarantee is observed only with respect to @target and on the local
2156 * cpu.
2157 *
2158 * Currently, a queued barrier can't be canceled. This is because
2159 * try_to_grab_pending() can't determine whether the work to be
2160 * grabbed is at the head of the queue and thus can't clear LINKED
2161 * flag of the previous work while there must be a valid next work
2162 * after a work with LINKED flag set.
2163 *
2164 * Note that when @worker is non-NULL, @target may be modified
2165 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002166 *
2167 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002168 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002169 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002170static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002171 struct wq_barrier *barr,
2172 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002173{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002174 struct list_head *head;
2175 unsigned int linked = 0;
2176
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002177 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002178 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002179 * as we know for sure that this will not trigger any of the
2180 * checks and call back into the fixup functions where we
2181 * might deadlock.
2182 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002183 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002184 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002185 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002186
Tejun Heoaffee4b2010-06-29 10:07:12 +02002187 /*
2188 * If @target is currently being executed, schedule the
2189 * barrier to the worker; otherwise, put it after @target.
2190 */
2191 if (worker)
2192 head = worker->scheduled.next;
2193 else {
2194 unsigned long *bits = work_data_bits(target);
2195
2196 head = target->entry.next;
2197 /* there can already be other linked works, inherit and set */
2198 linked = *bits & WORK_STRUCT_LINKED;
2199 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2200 }
2201
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002202 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002203 insert_work(cwq, &barr->work, head,
2204 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002205}
2206
Tejun Heo73f53c42010-06-29 10:07:11 +02002207/**
2208 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2209 * @wq: workqueue being flushed
2210 * @flush_color: new flush color, < 0 for no-op
2211 * @work_color: new work color, < 0 for no-op
2212 *
2213 * Prepare cwqs for workqueue flushing.
2214 *
2215 * If @flush_color is non-negative, flush_color on all cwqs should be
2216 * -1. If no cwq has in-flight commands at the specified color, all
2217 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2218 * has in flight commands, its cwq->flush_color is set to
2219 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2220 * wakeup logic is armed and %true is returned.
2221 *
2222 * The caller should have initialized @wq->first_flusher prior to
2223 * calling this function with non-negative @flush_color. If
2224 * @flush_color is negative, no flush color update is done and %false
2225 * is returned.
2226 *
2227 * If @work_color is non-negative, all cwqs should have the same
2228 * work_color which is previous to @work_color and all will be
2229 * advanced to @work_color.
2230 *
2231 * CONTEXT:
2232 * mutex_lock(wq->flush_mutex).
2233 *
2234 * RETURNS:
2235 * %true if @flush_color >= 0 and there's something to flush. %false
2236 * otherwise.
2237 */
2238static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2239 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Tejun Heo73f53c42010-06-29 10:07:11 +02002241 bool wait = false;
2242 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002243
Tejun Heo73f53c42010-06-29 10:07:11 +02002244 if (flush_color >= 0) {
2245 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2246 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002247 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002248
Tejun Heof3421792010-07-02 10:03:51 +02002249 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002250 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002251 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002252
Tejun Heo8b03ae32010-06-29 10:07:12 +02002253 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002254
2255 if (flush_color >= 0) {
2256 BUG_ON(cwq->flush_color != -1);
2257
2258 if (cwq->nr_in_flight[flush_color]) {
2259 cwq->flush_color = flush_color;
2260 atomic_inc(&wq->nr_cwqs_to_flush);
2261 wait = true;
2262 }
2263 }
2264
2265 if (work_color >= 0) {
2266 BUG_ON(work_color != work_next_color(cwq->work_color));
2267 cwq->work_color = work_color;
2268 }
2269
Tejun Heo8b03ae32010-06-29 10:07:12 +02002270 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002271 }
2272
2273 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2274 complete(&wq->first_flusher->done);
2275
2276 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002279/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002281 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 *
2283 * Forces execution of the workqueue and blocks until its completion.
2284 * This is typically used in driver shutdown handlers.
2285 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002286 * We sleep until all works which were queued on entry have been handled,
2287 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002289void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Tejun Heo73f53c42010-06-29 10:07:11 +02002291 struct wq_flusher this_flusher = {
2292 .list = LIST_HEAD_INIT(this_flusher.list),
2293 .flush_color = -1,
2294 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2295 };
2296 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002297
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002298 lock_map_acquire(&wq->lockdep_map);
2299 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002300
2301 mutex_lock(&wq->flush_mutex);
2302
2303 /*
2304 * Start-to-wait phase
2305 */
2306 next_color = work_next_color(wq->work_color);
2307
2308 if (next_color != wq->flush_color) {
2309 /*
2310 * Color space is not full. The current work_color
2311 * becomes our flush_color and work_color is advanced
2312 * by one.
2313 */
2314 BUG_ON(!list_empty(&wq->flusher_overflow));
2315 this_flusher.flush_color = wq->work_color;
2316 wq->work_color = next_color;
2317
2318 if (!wq->first_flusher) {
2319 /* no flush in progress, become the first flusher */
2320 BUG_ON(wq->flush_color != this_flusher.flush_color);
2321
2322 wq->first_flusher = &this_flusher;
2323
2324 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2325 wq->work_color)) {
2326 /* nothing to flush, done */
2327 wq->flush_color = next_color;
2328 wq->first_flusher = NULL;
2329 goto out_unlock;
2330 }
2331 } else {
2332 /* wait in queue */
2333 BUG_ON(wq->flush_color == this_flusher.flush_color);
2334 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2335 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2336 }
2337 } else {
2338 /*
2339 * Oops, color space is full, wait on overflow queue.
2340 * The next flush completion will assign us
2341 * flush_color and transfer to flusher_queue.
2342 */
2343 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2344 }
2345
2346 mutex_unlock(&wq->flush_mutex);
2347
2348 wait_for_completion(&this_flusher.done);
2349
2350 /*
2351 * Wake-up-and-cascade phase
2352 *
2353 * First flushers are responsible for cascading flushes and
2354 * handling overflow. Non-first flushers can simply return.
2355 */
2356 if (wq->first_flusher != &this_flusher)
2357 return;
2358
2359 mutex_lock(&wq->flush_mutex);
2360
Tejun Heo4ce48b32010-07-02 10:03:51 +02002361 /* we might have raced, check again with mutex held */
2362 if (wq->first_flusher != &this_flusher)
2363 goto out_unlock;
2364
Tejun Heo73f53c42010-06-29 10:07:11 +02002365 wq->first_flusher = NULL;
2366
2367 BUG_ON(!list_empty(&this_flusher.list));
2368 BUG_ON(wq->flush_color != this_flusher.flush_color);
2369
2370 while (true) {
2371 struct wq_flusher *next, *tmp;
2372
2373 /* complete all the flushers sharing the current flush color */
2374 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2375 if (next->flush_color != wq->flush_color)
2376 break;
2377 list_del_init(&next->list);
2378 complete(&next->done);
2379 }
2380
2381 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2382 wq->flush_color != work_next_color(wq->work_color));
2383
2384 /* this flush_color is finished, advance by one */
2385 wq->flush_color = work_next_color(wq->flush_color);
2386
2387 /* one color has been freed, handle overflow queue */
2388 if (!list_empty(&wq->flusher_overflow)) {
2389 /*
2390 * Assign the same color to all overflowed
2391 * flushers, advance work_color and append to
2392 * flusher_queue. This is the start-to-wait
2393 * phase for these overflowed flushers.
2394 */
2395 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2396 tmp->flush_color = wq->work_color;
2397
2398 wq->work_color = work_next_color(wq->work_color);
2399
2400 list_splice_tail_init(&wq->flusher_overflow,
2401 &wq->flusher_queue);
2402 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2403 }
2404
2405 if (list_empty(&wq->flusher_queue)) {
2406 BUG_ON(wq->flush_color != wq->work_color);
2407 break;
2408 }
2409
2410 /*
2411 * Need to flush more colors. Make the next flusher
2412 * the new first flusher and arm cwqs.
2413 */
2414 BUG_ON(wq->flush_color == wq->work_color);
2415 BUG_ON(wq->flush_color != next->flush_color);
2416
2417 list_del_init(&next->list);
2418 wq->first_flusher = next;
2419
2420 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2421 break;
2422
2423 /*
2424 * Meh... this color is already done, clear first
2425 * flusher and repeat cascading.
2426 */
2427 wq->first_flusher = NULL;
2428 }
2429
2430out_unlock:
2431 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
Dave Jonesae90dd52006-06-30 01:40:45 -04002433EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002435/**
2436 * drain_workqueue - drain a workqueue
2437 * @wq: workqueue to drain
2438 *
2439 * Wait until the workqueue becomes empty. While draining is in progress,
2440 * only chain queueing is allowed. IOW, only currently pending or running
2441 * work items on @wq can queue further work items on it. @wq is flushed
2442 * repeatedly until it becomes empty. The number of flushing is detemined
2443 * by the depth of chaining and should be relatively short. Whine if it
2444 * takes too long.
2445 */
2446void drain_workqueue(struct workqueue_struct *wq)
2447{
2448 unsigned int flush_cnt = 0;
2449 unsigned int cpu;
2450
2451 /*
2452 * __queue_work() needs to test whether there are drainers, is much
2453 * hotter than drain_workqueue() and already looks at @wq->flags.
2454 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2455 */
2456 spin_lock(&workqueue_lock);
2457 if (!wq->nr_drainers++)
2458 wq->flags |= WQ_DRAINING;
2459 spin_unlock(&workqueue_lock);
2460reflush:
2461 flush_workqueue(wq);
2462
2463 for_each_cwq_cpu(cpu, wq) {
2464 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002465 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002466
Tejun Heobd7bdd42012-07-12 14:46:37 -07002467 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002468 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002469 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002470
2471 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002472 continue;
2473
2474 if (++flush_cnt == 10 ||
2475 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2476 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2477 wq->name, flush_cnt);
2478 goto reflush;
2479 }
2480
2481 spin_lock(&workqueue_lock);
2482 if (!--wq->nr_drainers)
2483 wq->flags &= ~WQ_DRAINING;
2484 spin_unlock(&workqueue_lock);
2485}
2486EXPORT_SYMBOL_GPL(drain_workqueue);
2487
Tejun Heobaf59022010-09-16 10:42:16 +02002488static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2489 bool wait_executing)
2490{
2491 struct worker *worker = NULL;
2492 struct global_cwq *gcwq;
2493 struct cpu_workqueue_struct *cwq;
2494
2495 might_sleep();
2496 gcwq = get_work_gcwq(work);
2497 if (!gcwq)
2498 return false;
2499
2500 spin_lock_irq(&gcwq->lock);
2501 if (!list_empty(&work->entry)) {
2502 /*
2503 * See the comment near try_to_grab_pending()->smp_rmb().
2504 * If it was re-queued to a different gcwq under us, we
2505 * are not going to wait.
2506 */
2507 smp_rmb();
2508 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002509 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002510 goto already_gone;
2511 } else if (wait_executing) {
2512 worker = find_worker_executing_work(gcwq, work);
2513 if (!worker)
2514 goto already_gone;
2515 cwq = worker->current_cwq;
2516 } else
2517 goto already_gone;
2518
2519 insert_wq_barrier(cwq, barr, work, worker);
2520 spin_unlock_irq(&gcwq->lock);
2521
Tejun Heoe1594892011-01-09 23:32:15 +01002522 /*
2523 * If @max_active is 1 or rescuer is in use, flushing another work
2524 * item on the same workqueue may lead to deadlock. Make sure the
2525 * flusher is not running on the same workqueue by verifying write
2526 * access.
2527 */
2528 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2529 lock_map_acquire(&cwq->wq->lockdep_map);
2530 else
2531 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002532 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002533
Tejun Heobaf59022010-09-16 10:42:16 +02002534 return true;
2535already_gone:
2536 spin_unlock_irq(&gcwq->lock);
2537 return false;
2538}
2539
Oleg Nesterovdb700892008-07-25 01:47:49 -07002540/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002541 * flush_work - wait for a work to finish executing the last queueing instance
2542 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002543 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002544 * Wait until @work has finished execution. This function considers
2545 * only the last queueing instance of @work. If @work has been
2546 * enqueued across different CPUs on a non-reentrant workqueue or on
2547 * multiple workqueues, @work might still be executing on return on
2548 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002549 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002550 * If @work was queued only on a non-reentrant, ordered or unbound
2551 * workqueue, @work is guaranteed to be idle on return if it hasn't
2552 * been requeued since flush started.
2553 *
2554 * RETURNS:
2555 * %true if flush_work() waited for the work to finish execution,
2556 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002557 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002558bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002559{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002560 struct wq_barrier barr;
2561
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002562 lock_map_acquire(&work->lockdep_map);
2563 lock_map_release(&work->lockdep_map);
2564
Tejun Heobaf59022010-09-16 10:42:16 +02002565 if (start_flush_work(work, &barr, true)) {
2566 wait_for_completion(&barr.done);
2567 destroy_work_on_stack(&barr.work);
2568 return true;
2569 } else
2570 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002571}
2572EXPORT_SYMBOL_GPL(flush_work);
2573
Tejun Heo401a8d02010-09-16 10:36:00 +02002574static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2575{
2576 struct wq_barrier barr;
2577 struct worker *worker;
2578
2579 spin_lock_irq(&gcwq->lock);
2580
2581 worker = find_worker_executing_work(gcwq, work);
2582 if (unlikely(worker))
2583 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2584
2585 spin_unlock_irq(&gcwq->lock);
2586
2587 if (unlikely(worker)) {
2588 wait_for_completion(&barr.done);
2589 destroy_work_on_stack(&barr.work);
2590 return true;
2591 } else
2592 return false;
2593}
2594
2595static bool wait_on_work(struct work_struct *work)
2596{
2597 bool ret = false;
2598 int cpu;
2599
2600 might_sleep();
2601
2602 lock_map_acquire(&work->lockdep_map);
2603 lock_map_release(&work->lockdep_map);
2604
2605 for_each_gcwq_cpu(cpu)
2606 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2607 return ret;
2608}
2609
Tejun Heo09383492010-09-16 10:48:29 +02002610/**
2611 * flush_work_sync - wait until a work has finished execution
2612 * @work: the work to flush
2613 *
2614 * Wait until @work has finished execution. On return, it's
2615 * guaranteed that all queueing instances of @work which happened
2616 * before this function is called are finished. In other words, if
2617 * @work hasn't been requeued since this function was called, @work is
2618 * guaranteed to be idle on return.
2619 *
2620 * RETURNS:
2621 * %true if flush_work_sync() waited for the work to finish execution,
2622 * %false if it was already idle.
2623 */
2624bool flush_work_sync(struct work_struct *work)
2625{
2626 struct wq_barrier barr;
2627 bool pending, waited;
2628
2629 /* we'll wait for executions separately, queue barr only if pending */
2630 pending = start_flush_work(work, &barr, false);
2631
2632 /* wait for executions to finish */
2633 waited = wait_on_work(work);
2634
2635 /* wait for the pending one */
2636 if (pending) {
2637 wait_for_completion(&barr.done);
2638 destroy_work_on_stack(&barr.work);
2639 }
2640
2641 return pending || waited;
2642}
2643EXPORT_SYMBOL_GPL(flush_work_sync);
2644
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002645/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002646 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002647 * so this work can't be re-armed in any way.
2648 */
2649static int try_to_grab_pending(struct work_struct *work)
2650{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002651 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002652 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002653
Tejun Heo22df02b2010-06-29 10:07:10 +02002654 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002655 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002656
2657 /*
2658 * The queueing is in progress, or it is already queued. Try to
2659 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2660 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002661 gcwq = get_work_gcwq(work);
2662 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002663 return ret;
2664
Tejun Heo8b03ae32010-06-29 10:07:12 +02002665 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002666 if (!list_empty(&work->entry)) {
2667 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002668 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002669 * In that case we must see the new value after rmb(), see
2670 * insert_work()->wmb().
2671 */
2672 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002673 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002674 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002676 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002677 get_work_color(work),
2678 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002679 ret = 1;
2680 }
2681 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002682 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002683
2684 return ret;
2685}
2686
Tejun Heo401a8d02010-09-16 10:36:00 +02002687static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002688 struct timer_list* timer)
2689{
2690 int ret;
2691
2692 do {
2693 ret = (timer && likely(del_timer(timer)));
2694 if (!ret)
2695 ret = try_to_grab_pending(work);
2696 wait_on_work(work);
2697 } while (unlikely(ret < 0));
2698
Tejun Heo7a22ad72010-06-29 10:07:13 +02002699 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002700 return ret;
2701}
2702
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002703/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002704 * cancel_work_sync - cancel a work and wait for it to finish
2705 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002706 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002707 * Cancel @work and wait for its execution to finish. This function
2708 * can be used even if the work re-queues itself or migrates to
2709 * another workqueue. On return from this function, @work is
2710 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002711 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002712 * cancel_work_sync(&delayed_work->work) must not be used for
2713 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002714 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002715 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002716 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002717 *
2718 * RETURNS:
2719 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002720 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002721bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002722{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002723 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002724}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002725EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002726
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002727/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002728 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2729 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002731 * Delayed timer is cancelled and the pending work is queued for
2732 * immediate execution. Like flush_work(), this function only
2733 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002734 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002735 * RETURNS:
2736 * %true if flush_work() waited for the work to finish execution,
2737 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002738 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002739bool flush_delayed_work(struct delayed_work *dwork)
2740{
2741 if (del_timer_sync(&dwork->timer))
2742 __queue_work(raw_smp_processor_id(),
2743 get_work_cwq(&dwork->work)->wq, &dwork->work);
2744 return flush_work(&dwork->work);
2745}
2746EXPORT_SYMBOL(flush_delayed_work);
2747
2748/**
Tejun Heo09383492010-09-16 10:48:29 +02002749 * flush_delayed_work_sync - wait for a dwork to finish
2750 * @dwork: the delayed work to flush
2751 *
2752 * Delayed timer is cancelled and the pending work is queued for
2753 * execution immediately. Other than timer handling, its behavior
2754 * is identical to flush_work_sync().
2755 *
2756 * RETURNS:
2757 * %true if flush_work_sync() waited for the work to finish execution,
2758 * %false if it was already idle.
2759 */
2760bool flush_delayed_work_sync(struct delayed_work *dwork)
2761{
2762 if (del_timer_sync(&dwork->timer))
2763 __queue_work(raw_smp_processor_id(),
2764 get_work_cwq(&dwork->work)->wq, &dwork->work);
2765 return flush_work_sync(&dwork->work);
2766}
2767EXPORT_SYMBOL(flush_delayed_work_sync);
2768
2769/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002770 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2771 * @dwork: the delayed work cancel
2772 *
2773 * This is cancel_work_sync() for delayed works.
2774 *
2775 * RETURNS:
2776 * %true if @dwork was pending, %false otherwise.
2777 */
2778bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002779{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002780 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002781}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002782EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002784/**
2785 * schedule_work - put work task in global workqueue
2786 * @work: job to be done
2787 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002788 * Returns zero if @work was already on the kernel-global workqueue and
2789 * non-zero otherwise.
2790 *
2791 * This puts a job in the kernel-global workqueue if it was not already
2792 * queued and leaves it in the same position on the kernel-global
2793 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002794 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002795int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Tejun Heod320c032010-06-29 10:07:14 +02002797 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
Dave Jonesae90dd52006-06-30 01:40:45 -04002799EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Zhang Ruic1a220e2008-07-23 21:28:39 -07002801/*
2802 * schedule_work_on - put work task on a specific cpu
2803 * @cpu: cpu to put the work task on
2804 * @work: job to be done
2805 *
2806 * This puts a job on a specific cpu
2807 */
2808int schedule_work_on(int cpu, struct work_struct *work)
2809{
Tejun Heod320c032010-06-29 10:07:14 +02002810 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002811}
2812EXPORT_SYMBOL(schedule_work_on);
2813
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002814/**
2815 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002816 * @dwork: job to be done
2817 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002818 *
2819 * After waiting for a given time this puts a job in the kernel-global
2820 * workqueue.
2821 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002822int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002823 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Tejun Heod320c032010-06-29 10:07:14 +02002825 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
Dave Jonesae90dd52006-06-30 01:40:45 -04002827EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002829/**
2830 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2831 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002832 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002833 * @delay: number of jiffies to wait
2834 *
2835 * After waiting for a given time this puts a job in the kernel-global
2836 * workqueue on the specified CPU.
2837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002839 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Tejun Heod320c032010-06-29 10:07:14 +02002841 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
Dave Jonesae90dd52006-06-30 01:40:45 -04002843EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Andrew Mortonb6136772006-06-25 05:47:49 -07002845/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002846 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002848 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002849 * schedule_on_each_cpu() executes @func on each online CPU using the
2850 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002851 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002852 *
2853 * RETURNS:
2854 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002855 */
David Howells65f27f32006-11-22 14:55:48 +00002856int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002857{
2858 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002859 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002860
Andrew Mortonb6136772006-06-25 05:47:49 -07002861 works = alloc_percpu(struct work_struct);
2862 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002863 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002864
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002865 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002866
Christoph Lameter15316ba2006-01-08 01:00:43 -08002867 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002868 struct work_struct *work = per_cpu_ptr(works, cpu);
2869
2870 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002871 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002872 }
Tejun Heo93981802009-11-17 14:06:20 -08002873
2874 for_each_online_cpu(cpu)
2875 flush_work(per_cpu_ptr(works, cpu));
2876
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002877 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002878 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002879 return 0;
2880}
2881
Alan Sterneef6a7d2010-02-12 17:39:21 +09002882/**
2883 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2884 *
2885 * Forces execution of the kernel-global workqueue and blocks until its
2886 * completion.
2887 *
2888 * Think twice before calling this function! It's very easy to get into
2889 * trouble if you don't take great care. Either of the following situations
2890 * will lead to deadlock:
2891 *
2892 * One of the work items currently on the workqueue needs to acquire
2893 * a lock held by your code or its caller.
2894 *
2895 * Your code is running in the context of a work routine.
2896 *
2897 * They will be detected by lockdep when they occur, but the first might not
2898 * occur very often. It depends on what work items are on the workqueue and
2899 * what locks they need, which you have no control over.
2900 *
2901 * In most situations flushing the entire workqueue is overkill; you merely
2902 * need to know that a particular work item isn't queued and isn't running.
2903 * In such cases you should use cancel_delayed_work_sync() or
2904 * cancel_work_sync() instead.
2905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906void flush_scheduled_work(void)
2907{
Tejun Heod320c032010-06-29 10:07:14 +02002908 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
Dave Jonesae90dd52006-06-30 01:40:45 -04002910EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002913 * execute_in_process_context - reliably execute the routine with user context
2914 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002915 * @ew: guaranteed storage for the execute work structure (must
2916 * be available when the work executes)
2917 *
2918 * Executes the function immediately if process context is available,
2919 * otherwise schedules the function for delayed execution.
2920 *
2921 * Returns: 0 - function was executed
2922 * 1 - function was scheduled for execution
2923 */
David Howells65f27f32006-11-22 14:55:48 +00002924int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002925{
2926 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002927 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002928 return 0;
2929 }
2930
David Howells65f27f32006-11-22 14:55:48 +00002931 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002932 schedule_work(&ew->work);
2933
2934 return 1;
2935}
2936EXPORT_SYMBOL_GPL(execute_in_process_context);
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938int keventd_up(void)
2939{
Tejun Heod320c032010-06-29 10:07:14 +02002940 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
2942
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002943static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002945 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002946 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2947 * Make sure that the alignment isn't lower than that of
2948 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002949 */
Tejun Heo0f900042010-06-29 10:07:11 +02002950 const size_t size = sizeof(struct cpu_workqueue_struct);
2951 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2952 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002953
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002954 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002955 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002956 else {
Tejun Heof3421792010-07-02 10:03:51 +02002957 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002958
Tejun Heof3421792010-07-02 10:03:51 +02002959 /*
2960 * Allocate enough room to align cwq and put an extra
2961 * pointer at the end pointing back to the originally
2962 * allocated pointer which will be used for free.
2963 */
2964 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2965 if (ptr) {
2966 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2967 *(void **)(wq->cpu_wq.single + 1) = ptr;
2968 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002969 }
Tejun Heof3421792010-07-02 10:03:51 +02002970
Tejun Heo0415b00d12011-03-24 18:50:09 +01002971 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002972 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2973 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002974}
2975
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002976static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002977{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002978 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002979 free_percpu(wq->cpu_wq.pcpu);
2980 else if (wq->cpu_wq.single) {
2981 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002982 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002983 }
2984}
2985
Tejun Heof3421792010-07-02 10:03:51 +02002986static int wq_clamp_max_active(int max_active, unsigned int flags,
2987 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002988{
Tejun Heof3421792010-07-02 10:03:51 +02002989 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2990
2991 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002992 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2993 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002994 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002995
Tejun Heof3421792010-07-02 10:03:51 +02002996 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002997}
2998
Tejun Heob196be82012-01-10 15:11:35 -08002999struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003000 unsigned int flags,
3001 int max_active,
3002 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003003 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003004{
Tejun Heob196be82012-01-10 15:11:35 -08003005 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003006 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003007 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003008 size_t namelen;
3009
3010 /* determine namelen, allocate wq and format name */
3011 va_start(args, lock_name);
3012 va_copy(args1, args);
3013 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3014
3015 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3016 if (!wq)
3017 goto err;
3018
3019 vsnprintf(wq->name, namelen, fmt, args1);
3020 va_end(args);
3021 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003022
Tejun Heof3421792010-07-02 10:03:51 +02003023 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003024 * Workqueues which may be used during memory reclaim should
3025 * have a rescuer to guarantee forward progress.
3026 */
3027 if (flags & WQ_MEM_RECLAIM)
3028 flags |= WQ_RESCUER;
3029
Tejun Heod320c032010-06-29 10:07:14 +02003030 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003031 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003032
Tejun Heob196be82012-01-10 15:11:35 -08003033 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003034 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003035 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003036 mutex_init(&wq->flush_mutex);
3037 atomic_set(&wq->nr_cwqs_to_flush, 0);
3038 INIT_LIST_HEAD(&wq->flusher_queue);
3039 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003040
Johannes Bergeb13ba82008-01-16 09:51:58 +01003041 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003042 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003043
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003044 if (alloc_cwqs(wq) < 0)
3045 goto err;
3046
Tejun Heof3421792010-07-02 10:03:51 +02003047 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003048 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003049 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003050
Tejun Heo0f900042010-06-29 10:07:11 +02003051 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003052 cwq->pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02003053 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003054 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003055 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003056 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003057 }
3058
Tejun Heoe22bee72010-06-29 10:07:14 +02003059 if (flags & WQ_RESCUER) {
3060 struct worker *rescuer;
3061
Tejun Heof2e005a2010-07-20 15:59:09 +02003062 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003063 goto err;
3064
3065 wq->rescuer = rescuer = alloc_worker();
3066 if (!rescuer)
3067 goto err;
3068
Tejun Heob196be82012-01-10 15:11:35 -08003069 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3070 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003071 if (IS_ERR(rescuer->task))
3072 goto err;
3073
Tejun Heoe22bee72010-06-29 10:07:14 +02003074 rescuer->task->flags |= PF_THREAD_BOUND;
3075 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003076 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003077
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003078 /*
3079 * workqueue_lock protects global freeze state and workqueues
3080 * list. Grab it, set max_active accordingly and add the new
3081 * workqueue to workqueues list.
3082 */
Tejun Heo15376632010-06-29 10:07:11 +02003083 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003084
Tejun Heo58a69cb2011-02-16 09:25:31 +01003085 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003086 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003087 get_cwq(cpu, wq)->max_active = 0;
3088
Tejun Heo15376632010-06-29 10:07:11 +02003089 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003090
Tejun Heo15376632010-06-29 10:07:11 +02003091 spin_unlock(&workqueue_lock);
3092
Oleg Nesterov3af244332007-05-09 02:34:09 -07003093 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003094err:
3095 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003096 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003097 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003098 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003099 kfree(wq);
3100 }
3101 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003102}
Tejun Heod320c032010-06-29 10:07:14 +02003103EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003104
3105/**
3106 * destroy_workqueue - safely terminate a workqueue
3107 * @wq: target workqueue
3108 *
3109 * Safely destroy a workqueue. All work currently pending will be done first.
3110 */
3111void destroy_workqueue(struct workqueue_struct *wq)
3112{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003113 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003114
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003115 /* drain it before proceeding with destruction */
3116 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003117
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003118 /*
3119 * wq list is used to freeze wq, remove from list after
3120 * flushing is complete in case freeze races us.
3121 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003122 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003123 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003124 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125
Tejun Heoe22bee72010-06-29 10:07:14 +02003126 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003127 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003128 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3129 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003130
Tejun Heo73f53c42010-06-29 10:07:11 +02003131 for (i = 0; i < WORK_NR_COLORS; i++)
3132 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003133 BUG_ON(cwq->nr_active);
3134 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003135 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003136
Tejun Heoe22bee72010-06-29 10:07:14 +02003137 if (wq->flags & WQ_RESCUER) {
3138 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003139 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003140 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003141 }
3142
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003143 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003144 kfree(wq);
3145}
3146EXPORT_SYMBOL_GPL(destroy_workqueue);
3147
Tejun Heodcd989c2010-06-29 10:07:14 +02003148/**
3149 * workqueue_set_max_active - adjust max_active of a workqueue
3150 * @wq: target workqueue
3151 * @max_active: new max_active value.
3152 *
3153 * Set max_active of @wq to @max_active.
3154 *
3155 * CONTEXT:
3156 * Don't call from IRQ context.
3157 */
3158void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3159{
3160 unsigned int cpu;
3161
Tejun Heof3421792010-07-02 10:03:51 +02003162 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003163
3164 spin_lock(&workqueue_lock);
3165
3166 wq->saved_max_active = max_active;
3167
Tejun Heof3421792010-07-02 10:03:51 +02003168 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003169 struct global_cwq *gcwq = get_gcwq(cpu);
3170
3171 spin_lock_irq(&gcwq->lock);
3172
Tejun Heo58a69cb2011-02-16 09:25:31 +01003173 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003174 !(gcwq->flags & GCWQ_FREEZING))
3175 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3176
3177 spin_unlock_irq(&gcwq->lock);
3178 }
3179
3180 spin_unlock(&workqueue_lock);
3181}
3182EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3183
3184/**
3185 * workqueue_congested - test whether a workqueue is congested
3186 * @cpu: CPU in question
3187 * @wq: target workqueue
3188 *
3189 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3190 * no synchronization around this function and the test result is
3191 * unreliable and only useful as advisory hints or for debugging.
3192 *
3193 * RETURNS:
3194 * %true if congested, %false otherwise.
3195 */
3196bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3197{
3198 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3199
3200 return !list_empty(&cwq->delayed_works);
3201}
3202EXPORT_SYMBOL_GPL(workqueue_congested);
3203
3204/**
3205 * work_cpu - return the last known associated cpu for @work
3206 * @work: the work of interest
3207 *
3208 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003209 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003210 */
3211unsigned int work_cpu(struct work_struct *work)
3212{
3213 struct global_cwq *gcwq = get_work_gcwq(work);
3214
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003215 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003216}
3217EXPORT_SYMBOL_GPL(work_cpu);
3218
3219/**
3220 * work_busy - test whether a work is currently pending or running
3221 * @work: the work to be tested
3222 *
3223 * Test whether @work is currently pending or running. There is no
3224 * synchronization around this function and the test result is
3225 * unreliable and only useful as advisory hints or for debugging.
3226 * Especially for reentrant wqs, the pending state might hide the
3227 * running state.
3228 *
3229 * RETURNS:
3230 * OR'd bitmask of WORK_BUSY_* bits.
3231 */
3232unsigned int work_busy(struct work_struct *work)
3233{
3234 struct global_cwq *gcwq = get_work_gcwq(work);
3235 unsigned long flags;
3236 unsigned int ret = 0;
3237
3238 if (!gcwq)
3239 return false;
3240
3241 spin_lock_irqsave(&gcwq->lock, flags);
3242
3243 if (work_pending(work))
3244 ret |= WORK_BUSY_PENDING;
3245 if (find_worker_executing_work(gcwq, work))
3246 ret |= WORK_BUSY_RUNNING;
3247
3248 spin_unlock_irqrestore(&gcwq->lock, flags);
3249
3250 return ret;
3251}
3252EXPORT_SYMBOL_GPL(work_busy);
3253
Tejun Heodb7bccf2010-06-29 10:07:12 +02003254/*
3255 * CPU hotplug.
3256 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003257 * There are two challenges in supporting CPU hotplug. Firstly, there
3258 * are a lot of assumptions on strong associations among work, cwq and
3259 * gcwq which make migrating pending and scheduled works very
3260 * difficult to implement without impacting hot paths. Secondly,
3261 * gcwqs serve mix of short, long and very long running works making
3262 * blocked draining impractical.
3263 *
3264 * This is solved by allowing a gcwq to be detached from CPU, running
3265 * it with unbound (rogue) workers and allowing it to be reattached
3266 * later if the cpu comes back online. A separate thread is created
3267 * to govern a gcwq in such state and is called the trustee of the
3268 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003269 *
3270 * Trustee states and their descriptions.
3271 *
3272 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3273 * new trustee is started with this state.
3274 *
3275 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003276 * assuming the manager role and making all existing
3277 * workers rogue. DOWN_PREPARE waits for trustee to
3278 * enter this state. After reaching IN_CHARGE, trustee
3279 * tries to execute the pending worklist until it's empty
3280 * and the state is set to BUTCHER, or the state is set
3281 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003282 *
3283 * BUTCHER Command state which is set by the cpu callback after
3284 * the cpu has went down. Once this state is set trustee
3285 * knows that there will be no new works on the worklist
3286 * and once the worklist is empty it can proceed to
3287 * killing idle workers.
3288 *
3289 * RELEASE Command state which is set by the cpu callback if the
3290 * cpu down has been canceled or it has come online
3291 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003292 * trying to drain or butcher and clears ROGUE, rebinds
3293 * all remaining workers back to the cpu and releases
3294 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003295 *
3296 * DONE Trustee will enter this state after BUTCHER or RELEASE
3297 * is complete.
3298 *
3299 * trustee CPU draining
3300 * took over down complete
3301 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3302 * | | ^
3303 * | CPU is back online v return workers |
3304 * ----------------> RELEASE --------------
3305 */
3306
3307/**
3308 * trustee_wait_event_timeout - timed event wait for trustee
3309 * @cond: condition to wait for
3310 * @timeout: timeout in jiffies
3311 *
3312 * wait_event_timeout() for trustee to use. Handles locking and
3313 * checks for RELEASE request.
3314 *
3315 * CONTEXT:
3316 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3317 * multiple times. To be used by trustee.
3318 *
3319 * RETURNS:
3320 * Positive indicating left time if @cond is satisfied, 0 if timed
3321 * out, -1 if canceled.
3322 */
3323#define trustee_wait_event_timeout(cond, timeout) ({ \
3324 long __ret = (timeout); \
3325 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3326 __ret) { \
3327 spin_unlock_irq(&gcwq->lock); \
3328 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3329 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3330 __ret); \
3331 spin_lock_irq(&gcwq->lock); \
3332 } \
3333 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3334})
3335
3336/**
3337 * trustee_wait_event - event wait for trustee
3338 * @cond: condition to wait for
3339 *
3340 * wait_event() for trustee to use. Automatically handles locking and
3341 * checks for CANCEL request.
3342 *
3343 * CONTEXT:
3344 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3345 * multiple times. To be used by trustee.
3346 *
3347 * RETURNS:
3348 * 0 if @cond is satisfied, -1 if canceled.
3349 */
3350#define trustee_wait_event(cond) ({ \
3351 long __ret1; \
3352 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3353 __ret1 < 0 ? -1 : 0; \
3354})
3355
Tejun Heo4ce62e92012-07-13 22:16:44 -07003356static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3357{
3358 struct worker_pool *pool;
3359
3360 for_each_worker_pool(pool, gcwq)
3361 if (pool->flags & POOL_MANAGING_WORKERS)
3362 return true;
3363 return false;
3364}
3365
3366static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3367{
3368 struct worker_pool *pool;
3369
3370 for_each_worker_pool(pool, gcwq)
3371 if (!list_empty(&pool->idle_list))
3372 return true;
3373 return false;
3374}
3375
Tejun Heodb7bccf2010-06-29 10:07:12 +02003376static int __cpuinit trustee_thread(void *__gcwq)
3377{
3378 struct global_cwq *gcwq = __gcwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003379 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003380 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003381 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003382 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003383 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003384 int i;
3385
3386 BUG_ON(gcwq->cpu != smp_processor_id());
3387
3388 spin_lock_irq(&gcwq->lock);
3389 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003390 * Claim the manager position and make all workers rogue.
3391 * Trustee must be bound to the target cpu and can't be
3392 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003393 */
3394 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003395 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003396 BUG_ON(rc < 0);
3397
Tejun Heo4ce62e92012-07-13 22:16:44 -07003398 for_each_worker_pool(pool, gcwq) {
3399 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003400
Tejun Heo4ce62e92012-07-13 22:16:44 -07003401 list_for_each_entry(worker, &pool->idle_list, entry)
3402 worker->flags |= WORKER_ROGUE;
3403 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003404
3405 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003406 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003407
3408 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003409 * Call schedule() so that we cross rq->lock and thus can
3410 * guarantee sched callbacks see the rogue flag. This is
3411 * necessary as scheduler callbacks may be invoked from other
3412 * cpus.
3413 */
3414 spin_unlock_irq(&gcwq->lock);
3415 schedule();
3416 spin_lock_irq(&gcwq->lock);
3417
3418 /*
Tejun Heocb444762010-07-02 10:03:50 +02003419 * Sched callbacks are disabled now. Zap nr_running. After
3420 * this, nr_running stays zero and need_more_worker() and
3421 * keep_working() are always true as long as the worklist is
3422 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003423 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003424 for_each_worker_pool(pool, gcwq)
3425 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003426
3427 spin_unlock_irq(&gcwq->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003428 for_each_worker_pool(pool, gcwq)
3429 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003430 spin_lock_irq(&gcwq->lock);
3431
3432 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003433 * We're now in charge. Notify and proceed to drain. We need
3434 * to keep the gcwq running during the whole CPU down
3435 * procedure as other cpu hotunplug callbacks may need to
3436 * flush currently running tasks.
3437 */
3438 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3439 wake_up_all(&gcwq->trustee_wait);
3440
3441 /*
3442 * The original cpu is in the process of dying and may go away
3443 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003444 * be migrated to other cpus. Try draining any left work. We
3445 * want to get it over with ASAP - spam rescuers, wake up as
3446 * many idlers as necessary and create new ones till the
3447 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003448 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003450 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003451 while (true) {
3452 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003453
Tejun Heo4ce62e92012-07-13 22:16:44 -07003454 for_each_worker_pool(pool, gcwq)
3455 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003456
Tejun Heo4ce62e92012-07-13 22:16:44 -07003457 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3458 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3459 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003460
Tejun Heo4ce62e92012-07-13 22:16:44 -07003461 for_each_worker_pool(pool, gcwq) {
3462 int nr_works = 0;
3463
3464 list_for_each_entry(work, &pool->worklist, entry) {
3465 send_mayday(work);
3466 nr_works++;
3467 }
3468
3469 list_for_each_entry(worker, &pool->idle_list, entry) {
3470 if (!nr_works--)
3471 break;
3472 wake_up_process(worker->task);
3473 }
3474
3475 if (need_to_create_worker(pool)) {
3476 spin_unlock_irq(&gcwq->lock);
3477 worker = create_worker(pool, false);
3478 spin_lock_irq(&gcwq->lock);
3479 if (worker) {
3480 worker->flags |= WORKER_ROGUE;
3481 start_worker(worker);
3482 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003483 }
3484 }
3485
Tejun Heodb7bccf2010-06-29 10:07:12 +02003486 /* give a breather */
3487 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3488 break;
3489 }
3490
Tejun Heoe22bee72010-06-29 10:07:14 +02003491 /*
3492 * Either all works have been scheduled and cpu is down, or
3493 * cpu down has already been canceled. Wait for and butcher
3494 * all workers till we're canceled.
3495 */
3496 do {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003497 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3498
3499 i = 0;
3500 for_each_worker_pool(pool, gcwq) {
3501 while (!list_empty(&pool->idle_list)) {
3502 worker = list_first_entry(&pool->idle_list,
3503 struct worker, entry);
3504 destroy_worker(worker);
3505 }
3506 i |= pool->nr_workers;
3507 }
3508 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003509
3510 /*
3511 * At this point, either draining has completed and no worker
3512 * is left, or cpu down has been canceled or the cpu is being
3513 * brought back up. There shouldn't be any idle one left.
3514 * Tell the remaining busy ones to rebind once it finishes the
3515 * currently scheduled works by scheduling the rebind_work.
3516 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003517 for_each_worker_pool(pool, gcwq)
3518 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003519
3520 for_each_busy_worker(worker, i, pos, gcwq) {
3521 struct work_struct *rebind_work = &worker->rebind_work;
3522
3523 /*
3524 * Rebind_work may race with future cpu hotplug
3525 * operations. Use a separate flag to mark that
3526 * rebinding is scheduled.
3527 */
Tejun Heocb444762010-07-02 10:03:50 +02003528 worker->flags |= WORKER_REBIND;
3529 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003530
3531 /* queue rebind_work, wq doesn't matter, use the default one */
3532 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3533 work_data_bits(rebind_work)))
3534 continue;
3535
3536 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003537 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003538 worker->scheduled.next,
3539 work_color_to_flags(WORK_NO_COLOR));
3540 }
3541
3542 /* relinquish manager role */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003543 for_each_worker_pool(pool, gcwq)
3544 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003545
Tejun Heodb7bccf2010-06-29 10:07:12 +02003546 /* notify completion */
3547 gcwq->trustee = NULL;
3548 gcwq->trustee_state = TRUSTEE_DONE;
3549 wake_up_all(&gcwq->trustee_wait);
3550 spin_unlock_irq(&gcwq->lock);
3551 return 0;
3552}
3553
3554/**
3555 * wait_trustee_state - wait for trustee to enter the specified state
3556 * @gcwq: gcwq the trustee of interest belongs to
3557 * @state: target state to wait for
3558 *
3559 * Wait for the trustee to reach @state. DONE is already matched.
3560 *
3561 * CONTEXT:
3562 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3563 * multiple times. To be used by cpu_callback.
3564 */
3565static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003566__releases(&gcwq->lock)
3567__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003568{
3569 if (!(gcwq->trustee_state == state ||
3570 gcwq->trustee_state == TRUSTEE_DONE)) {
3571 spin_unlock_irq(&gcwq->lock);
3572 __wait_event(gcwq->trustee_wait,
3573 gcwq->trustee_state == state ||
3574 gcwq->trustee_state == TRUSTEE_DONE);
3575 spin_lock_irq(&gcwq->lock);
3576 }
3577}
3578
Oleg Nesterov3af244332007-05-09 02:34:09 -07003579static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3580 unsigned long action,
3581 void *hcpu)
3582{
3583 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003584 struct global_cwq *gcwq = get_gcwq(cpu);
3585 struct task_struct *new_trustee = NULL;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003586 struct worker *new_workers[NR_WORKER_POOLS] = { };
3587 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003588 unsigned long flags;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003589 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003591 action &= ~CPU_TASKS_FROZEN;
3592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003594 case CPU_DOWN_PREPARE:
3595 new_trustee = kthread_create(trustee_thread, gcwq,
3596 "workqueue_trustee/%d\n", cpu);
3597 if (IS_ERR(new_trustee))
3598 return notifier_from_errno(PTR_ERR(new_trustee));
3599 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003600 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003602 i = 0;
3603 for_each_worker_pool(pool, gcwq) {
3604 BUG_ON(pool->first_idle);
3605 new_workers[i] = create_worker(pool, false);
3606 if (!new_workers[i++])
3607 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
Tejun Heodb7bccf2010-06-29 10:07:12 +02003611 /* some are called w/ irq disabled, don't disturb irq status */
3612 spin_lock_irqsave(&gcwq->lock, flags);
3613
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003614 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003615 case CPU_DOWN_PREPARE:
3616 /* initialize trustee and tell it to acquire the gcwq */
3617 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3618 gcwq->trustee = new_trustee;
3619 gcwq->trustee_state = TRUSTEE_START;
3620 wake_up_process(gcwq->trustee);
3621 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003622 /* fall through */
3623 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003624 i = 0;
3625 for_each_worker_pool(pool, gcwq) {
3626 BUG_ON(pool->first_idle);
3627 pool->first_idle = new_workers[i++];
3628 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003629 break;
3630
3631 case CPU_DYING:
3632 /*
3633 * Before this, the trustee and all workers except for
3634 * the ones which are still executing works from
3635 * before the last CPU down must be on the cpu. After
3636 * this, they'll all be diasporas.
3637 */
3638 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003639 break;
3640
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003641 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003642 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003643 /* fall through */
3644 case CPU_UP_CANCELED:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003645 for_each_worker_pool(pool, gcwq) {
3646 destroy_worker(pool->first_idle);
3647 pool->first_idle = NULL;
3648 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003649 break;
3650
3651 case CPU_DOWN_FAILED:
3652 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003653 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003654 if (gcwq->trustee_state != TRUSTEE_DONE) {
3655 gcwq->trustee_state = TRUSTEE_RELEASE;
3656 wake_up_process(gcwq->trustee);
3657 wait_trustee_state(gcwq, TRUSTEE_DONE);
3658 }
3659
Tejun Heoe22bee72010-06-29 10:07:14 +02003660 /*
3661 * Trustee is done and there might be no worker left.
3662 * Put the first_idle in and request a real manager to
3663 * take a look.
3664 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003665 for_each_worker_pool(pool, gcwq) {
3666 spin_unlock_irq(&gcwq->lock);
3667 kthread_bind(pool->first_idle->task, cpu);
3668 spin_lock_irq(&gcwq->lock);
3669 pool->flags |= POOL_MANAGE_WORKERS;
3670 start_worker(pool->first_idle);
3671 pool->first_idle = NULL;
3672 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003673 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003674 }
3675
Tejun Heodb7bccf2010-06-29 10:07:12 +02003676 spin_unlock_irqrestore(&gcwq->lock, flags);
3677
Tejun Heo15376632010-06-29 10:07:11 +02003678 return notifier_from_errno(0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003679
3680err_destroy:
3681 if (new_trustee)
3682 kthread_stop(new_trustee);
3683
3684 spin_lock_irqsave(&gcwq->lock, flags);
3685 for (i = 0; i < NR_WORKER_POOLS; i++)
3686 if (new_workers[i])
3687 destroy_worker(new_workers[i]);
3688 spin_unlock_irqrestore(&gcwq->lock, flags);
3689
3690 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Rusty Russell2d3854a2008-11-05 13:39:10 +11003693#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003694
Rusty Russell2d3854a2008-11-05 13:39:10 +11003695struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003696 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003697 long (*fn)(void *);
3698 void *arg;
3699 long ret;
3700};
3701
Andrew Morton6b440032009-04-09 09:50:37 -06003702static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003703{
Andrew Morton6b440032009-04-09 09:50:37 -06003704 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003705 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003706 complete(&wfc->completion);
3707 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003708}
3709
3710/**
3711 * work_on_cpu - run a function in user context on a particular cpu
3712 * @cpu: the cpu to run on
3713 * @fn: the function to run
3714 * @arg: the function arg
3715 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003716 * This will return the value @fn returns.
3717 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003718 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003719 */
3720long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3721{
Andrew Morton6b440032009-04-09 09:50:37 -06003722 struct task_struct *sub_thread;
3723 struct work_for_cpu wfc = {
3724 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3725 .fn = fn,
3726 .arg = arg,
3727 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003728
Andrew Morton6b440032009-04-09 09:50:37 -06003729 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3730 if (IS_ERR(sub_thread))
3731 return PTR_ERR(sub_thread);
3732 kthread_bind(sub_thread, cpu);
3733 wake_up_process(sub_thread);
3734 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003735 return wfc.ret;
3736}
3737EXPORT_SYMBOL_GPL(work_on_cpu);
3738#endif /* CONFIG_SMP */
3739
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003740#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303741
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003742/**
3743 * freeze_workqueues_begin - begin freezing workqueues
3744 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003745 * Start freezing workqueues. After this function returns, all freezable
3746 * workqueues will queue new works to their frozen_works list instead of
3747 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003748 *
3749 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003750 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003751 */
3752void freeze_workqueues_begin(void)
3753{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003754 unsigned int cpu;
3755
3756 spin_lock(&workqueue_lock);
3757
3758 BUG_ON(workqueue_freezing);
3759 workqueue_freezing = true;
3760
Tejun Heof3421792010-07-02 10:03:51 +02003761 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003762 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003763 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003764
3765 spin_lock_irq(&gcwq->lock);
3766
Tejun Heodb7bccf2010-06-29 10:07:12 +02003767 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3768 gcwq->flags |= GCWQ_FREEZING;
3769
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003770 list_for_each_entry(wq, &workqueues, list) {
3771 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3772
Tejun Heo58a69cb2011-02-16 09:25:31 +01003773 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003774 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003775 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003776
3777 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003778 }
3779
3780 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003782
3783/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003784 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003785 *
3786 * Check whether freezing is complete. This function must be called
3787 * between freeze_workqueues_begin() and thaw_workqueues().
3788 *
3789 * CONTEXT:
3790 * Grabs and releases workqueue_lock.
3791 *
3792 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003793 * %true if some freezable workqueues are still busy. %false if freezing
3794 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003795 */
3796bool freeze_workqueues_busy(void)
3797{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003798 unsigned int cpu;
3799 bool busy = false;
3800
3801 spin_lock(&workqueue_lock);
3802
3803 BUG_ON(!workqueue_freezing);
3804
Tejun Heof3421792010-07-02 10:03:51 +02003805 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003806 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003807 /*
3808 * nr_active is monotonically decreasing. It's safe
3809 * to peek without lock.
3810 */
3811 list_for_each_entry(wq, &workqueues, list) {
3812 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3813
Tejun Heo58a69cb2011-02-16 09:25:31 +01003814 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003815 continue;
3816
3817 BUG_ON(cwq->nr_active < 0);
3818 if (cwq->nr_active) {
3819 busy = true;
3820 goto out_unlock;
3821 }
3822 }
3823 }
3824out_unlock:
3825 spin_unlock(&workqueue_lock);
3826 return busy;
3827}
3828
3829/**
3830 * thaw_workqueues - thaw workqueues
3831 *
3832 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003833 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003834 *
3835 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003836 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003837 */
3838void thaw_workqueues(void)
3839{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003840 unsigned int cpu;
3841
3842 spin_lock(&workqueue_lock);
3843
3844 if (!workqueue_freezing)
3845 goto out_unlock;
3846
Tejun Heof3421792010-07-02 10:03:51 +02003847 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003848 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003849 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003850 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003851
3852 spin_lock_irq(&gcwq->lock);
3853
Tejun Heodb7bccf2010-06-29 10:07:12 +02003854 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3855 gcwq->flags &= ~GCWQ_FREEZING;
3856
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003857 list_for_each_entry(wq, &workqueues, list) {
3858 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3859
Tejun Heo58a69cb2011-02-16 09:25:31 +01003860 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003861 continue;
3862
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003863 /* restore max_active and repopulate worklist */
3864 cwq->max_active = wq->saved_max_active;
3865
3866 while (!list_empty(&cwq->delayed_works) &&
3867 cwq->nr_active < cwq->max_active)
3868 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003869 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003870
Tejun Heo4ce62e92012-07-13 22:16:44 -07003871 for_each_worker_pool(pool, gcwq)
3872 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003873
Tejun Heo8b03ae32010-06-29 10:07:12 +02003874 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003875 }
3876
3877 workqueue_freezing = false;
3878out_unlock:
3879 spin_unlock(&workqueue_lock);
3880}
3881#endif /* CONFIG_FREEZER */
3882
Suresh Siddha6ee05782010-07-30 14:57:37 -07003883static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
Tejun Heoc34056a2010-06-29 10:07:11 +02003885 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003886 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003887
Tejun Heof6500942010-08-09 11:50:34 +02003888 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003889
3890 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003891 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003892 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003893 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003894
3895 spin_lock_init(&gcwq->lock);
3896 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003897 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003898
Tejun Heoc8e55f32010-06-29 10:07:12 +02003899 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3900 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3901
Tejun Heo4ce62e92012-07-13 22:16:44 -07003902 for_each_worker_pool(pool, gcwq) {
3903 pool->gcwq = gcwq;
3904 INIT_LIST_HEAD(&pool->worklist);
3905 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003906
Tejun Heo4ce62e92012-07-13 22:16:44 -07003907 init_timer_deferrable(&pool->idle_timer);
3908 pool->idle_timer.function = idle_worker_timeout;
3909 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003910
Tejun Heo4ce62e92012-07-13 22:16:44 -07003911 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3912 (unsigned long)pool);
3913
3914 ida_init(&pool->worker_ida);
3915 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003916
3917 gcwq->trustee_state = TRUSTEE_DONE;
3918 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003919 }
3920
Tejun Heoe22bee72010-06-29 10:07:14 +02003921 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003922 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003923 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003924 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003925
Tejun Heo477a3c32010-08-31 10:54:35 +02003926 if (cpu != WORK_CPU_UNBOUND)
3927 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003928
3929 for_each_worker_pool(pool, gcwq) {
3930 struct worker *worker;
3931
3932 worker = create_worker(pool, true);
3933 BUG_ON(!worker);
3934 spin_lock_irq(&gcwq->lock);
3935 start_worker(worker);
3936 spin_unlock_irq(&gcwq->lock);
3937 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003938 }
3939
Tejun Heod320c032010-06-29 10:07:14 +02003940 system_wq = alloc_workqueue("events", 0, 0);
3941 system_long_wq = alloc_workqueue("events_long", 0, 0);
3942 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003943 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3944 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003945 system_freezable_wq = alloc_workqueue("events_freezable",
3946 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003947 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3948 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003949 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003950 !system_unbound_wq || !system_freezable_wq ||
3951 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003954early_initcall(init_workqueues);