blob: 6072c9de1fd14bfc37701d36aef942c87827cf27 [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 Heode4637a2012-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 Heoe87d1492012-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 Heo32089f12012-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 Heo55e3e1f2012-12-18 10:35:02 -0800136 work_func_t current_func; /* L: current_work's fn */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200137 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200138 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139 struct task_struct *task; /* I: worker task */
Tejun Heo32089f12012-07-12 14:46:37 -0700140 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200141 /* 64 bytes boundary on 64bit, 32 on 32bit */
142 unsigned long last_active; /* L: last active timestamp */
143 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200144 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200145 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200146};
147
Tejun Heo32089f12012-07-12 14:46:37 -0700148struct worker_pool {
149 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heode4637a2012-07-12 14:46:37 -0700150 unsigned int flags; /* X: flags */
Tejun Heo32089f12012-07-12 14:46:37 -0700151
152 struct list_head worklist; /* L: list of pending works */
153 int nr_workers; /* L: total number of workers */
154 int nr_idle; /* L: currently idle ones */
155
156 struct list_head idle_list; /* X: list of idle workers */
157 struct timer_list idle_timer; /* L: worker idle timeout */
158 struct timer_list mayday_timer; /* L: SOS timer for workers */
159
160 struct ida worker_ida; /* L: for worker IDs */
161 struct worker *first_idle; /* L: first idle worker */
162};
163
Tejun Heo4690c4a2010-06-29 10:07:10 +0200164/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200165 * Global per-cpu workqueue. There's one and only one for each cpu
166 * and all works are queued and processed here regardless of their
167 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200168 */
169struct global_cwq {
170 spinlock_t lock; /* the gcwq lock */
171 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200172 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200173
Tejun Heo32089f12012-07-12 14:46:37 -0700174 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200175 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
176 /* L: hash of busy workers */
177
Tejun Heo32089f12012-07-12 14:46:37 -0700178 struct worker_pool pool; /* the worker pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200179
180 struct task_struct *trustee; /* L: for gcwq shutdown */
181 unsigned int trustee_state; /* L: trustee state */
182 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200183} ____cacheline_aligned_in_smp;
184
185/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200186 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200187 * work_struct->data are used for flags and thus cwqs need to be
188 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190struct cpu_workqueue_struct {
Tejun Heo32089f12012-07-12 14:46:37 -0700191 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200192 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200193 int work_color; /* L: current color */
194 int flush_color; /* L: flushing color */
195 int nr_in_flight[WORK_NR_COLORS];
196 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200197 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200198 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200199 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200200};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200203 * Structure used to wait for workqueue flush.
204 */
205struct wq_flusher {
206 struct list_head list; /* F: list of flushers */
207 int flush_color; /* F: flush color waiting for */
208 struct completion done; /* flush completion */
209};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Tejun Heo73f53c42010-06-29 10:07:11 +0200211/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200212 * All cpumasks are assumed to be always set on UP and thus can't be
213 * used to determine whether there's something to be done.
214 */
215#ifdef CONFIG_SMP
216typedef cpumask_var_t mayday_mask_t;
217#define mayday_test_and_set_cpu(cpu, mask) \
218 cpumask_test_and_set_cpu((cpu), (mask))
219#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
220#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200221#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200222#define free_mayday_mask(mask) free_cpumask_var((mask))
223#else
224typedef unsigned long mayday_mask_t;
225#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
226#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
227#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
228#define alloc_mayday_mask(maskp, gfp) true
229#define free_mayday_mask(mask) do { } while (0)
230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*
233 * The externally visible workqueue abstraction is an array of
234 * per-CPU workqueues:
235 */
236struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200237 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200238 union {
239 struct cpu_workqueue_struct __percpu *pcpu;
240 struct cpu_workqueue_struct *single;
241 unsigned long v;
242 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200243 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200244
245 struct mutex flush_mutex; /* protects wq flushing */
246 int work_color; /* F: current work color */
247 int flush_color; /* F: current flush color */
248 atomic_t nr_cwqs_to_flush; /* flush in progress */
249 struct wq_flusher *first_flusher; /* F: first flusher */
250 struct list_head flusher_queue; /* F: flush waiters */
251 struct list_head flusher_overflow; /* F: flush overflow list */
252
Tejun Heof2e005a2010-07-20 15:59:09 +0200253 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200254 struct worker *rescuer; /* I: rescue worker */
255
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200256 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200257 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700258#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200259 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700260#endif
Tejun Heob196be82012-01-10 15:11:35 -0800261 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
Tejun Heod320c032010-06-29 10:07:14 +0200264struct workqueue_struct *system_wq __read_mostly;
265struct workqueue_struct *system_long_wq __read_mostly;
266struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200267struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100268struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100269struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200270EXPORT_SYMBOL_GPL(system_wq);
271EXPORT_SYMBOL_GPL(system_long_wq);
272EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200273EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100274EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100275EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200276
Tejun Heo97bd2342010-10-05 10:41:14 +0200277#define CREATE_TRACE_POINTS
278#include <trace/events/workqueue.h>
279
Tejun Heoe87d1492012-07-13 22:16:44 -0700280#define for_each_worker_pool(pool, gcwq) \
281 for ((pool) = &(gcwq)->pool; (pool); (pool) = NULL)
282
Tejun Heodb7bccf2010-06-29 10:07:12 +0200283#define for_each_busy_worker(worker, i, pos, gcwq) \
284 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
285 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
286
Tejun Heof3421792010-07-02 10:03:51 +0200287static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
288 unsigned int sw)
289{
290 if (cpu < nr_cpu_ids) {
291 if (sw & 1) {
292 cpu = cpumask_next(cpu, mask);
293 if (cpu < nr_cpu_ids)
294 return cpu;
295 }
296 if (sw & 2)
297 return WORK_CPU_UNBOUND;
298 }
299 return WORK_CPU_NONE;
300}
301
302static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
303 struct workqueue_struct *wq)
304{
305 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
306}
307
Tejun Heo09884952010-08-01 11:50:12 +0200308/*
309 * CPU iterators
310 *
311 * An extra gcwq is defined for an invalid cpu number
312 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
313 * specific CPU. The following iterators are similar to
314 * for_each_*_cpu() iterators but also considers the unbound gcwq.
315 *
316 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
317 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
318 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
319 * WORK_CPU_UNBOUND for unbound workqueues
320 */
Tejun Heof3421792010-07-02 10:03:51 +0200321#define for_each_gcwq_cpu(cpu) \
322 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
323 (cpu) < WORK_CPU_NONE; \
324 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
325
326#define for_each_online_gcwq_cpu(cpu) \
327 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
328 (cpu) < WORK_CPU_NONE; \
329 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
330
331#define for_each_cwq_cpu(cpu, wq) \
332 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
333 (cpu) < WORK_CPU_NONE; \
334 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
335
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900336#ifdef CONFIG_DEBUG_OBJECTS_WORK
337
338static struct debug_obj_descr work_debug_descr;
339
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100340static void *work_debug_hint(void *addr)
341{
342 return ((struct work_struct *) addr)->func;
343}
344
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900345/*
346 * fixup_init is called when:
347 * - an active object is initialized
348 */
349static int work_fixup_init(void *addr, enum debug_obj_state state)
350{
351 struct work_struct *work = addr;
352
353 switch (state) {
354 case ODEBUG_STATE_ACTIVE:
355 cancel_work_sync(work);
356 debug_object_init(work, &work_debug_descr);
357 return 1;
358 default:
359 return 0;
360 }
361}
362
363/*
364 * fixup_activate is called when:
365 * - an active object is activated
366 * - an unknown object is activated (might be a statically initialized object)
367 */
368static int work_fixup_activate(void *addr, enum debug_obj_state state)
369{
370 struct work_struct *work = addr;
371
372 switch (state) {
373
374 case ODEBUG_STATE_NOTAVAILABLE:
375 /*
376 * This is not really a fixup. The work struct was
377 * statically initialized. We just make sure that it
378 * is tracked in the object tracker.
379 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200380 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900381 debug_object_init(work, &work_debug_descr);
382 debug_object_activate(work, &work_debug_descr);
383 return 0;
384 }
385 WARN_ON_ONCE(1);
386 return 0;
387
388 case ODEBUG_STATE_ACTIVE:
389 WARN_ON(1);
390
391 default:
392 return 0;
393 }
394}
395
396/*
397 * fixup_free is called when:
398 * - an active object is freed
399 */
400static int work_fixup_free(void *addr, enum debug_obj_state state)
401{
402 struct work_struct *work = addr;
403
404 switch (state) {
405 case ODEBUG_STATE_ACTIVE:
406 cancel_work_sync(work);
407 debug_object_free(work, &work_debug_descr);
408 return 1;
409 default:
410 return 0;
411 }
412}
413
414static struct debug_obj_descr work_debug_descr = {
415 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100416 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900417 .fixup_init = work_fixup_init,
418 .fixup_activate = work_fixup_activate,
419 .fixup_free = work_fixup_free,
420};
421
422static inline void debug_work_activate(struct work_struct *work)
423{
424 debug_object_activate(work, &work_debug_descr);
425}
426
427static inline void debug_work_deactivate(struct work_struct *work)
428{
429 debug_object_deactivate(work, &work_debug_descr);
430}
431
432void __init_work(struct work_struct *work, int onstack)
433{
434 if (onstack)
435 debug_object_init_on_stack(work, &work_debug_descr);
436 else
437 debug_object_init(work, &work_debug_descr);
438}
439EXPORT_SYMBOL_GPL(__init_work);
440
441void destroy_work_on_stack(struct work_struct *work)
442{
443 debug_object_free(work, &work_debug_descr);
444}
445EXPORT_SYMBOL_GPL(destroy_work_on_stack);
446
447#else
448static inline void debug_work_activate(struct work_struct *work) { }
449static inline void debug_work_deactivate(struct work_struct *work) { }
450#endif
451
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100452/* Serializes the accesses to the list of workqueues. */
453static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200455static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Oleg Nesterov14441962007-05-23 13:57:57 -0700457/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200458 * The almighty global cpu workqueues. nr_running is the only field
459 * which is expected to be used frequently by other cpus via
460 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700461 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200462static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe87d1492012-07-13 22:16:44 -0700463static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800464
Tejun Heof3421792010-07-02 10:03:51 +0200465/*
466 * Global cpu workqueue and nr_running counter for unbound gcwq. The
467 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
468 * workers have WORKER_UNBOUND set.
469 */
470static struct global_cwq unbound_global_cwq;
Tejun Heoe87d1492012-07-13 22:16:44 -0700471static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
472 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
473};
Tejun Heof3421792010-07-02 10:03:51 +0200474
Tejun Heoc34056a2010-06-29 10:07:11 +0200475static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Tejun Heo8b03ae32010-06-29 10:07:12 +0200477static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Tejun Heof3421792010-07-02 10:03:51 +0200479 if (cpu != WORK_CPU_UNBOUND)
480 return &per_cpu(global_cwq, cpu);
481 else
482 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Tejun Heo43370c62012-07-12 14:46:37 -0700485static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700486{
Tejun Heo43370c62012-07-12 14:46:37 -0700487 int cpu = pool->gcwq->cpu;
Tejun Heoe87d1492012-07-13 22:16:44 -0700488 int idx = 0;
Tejun Heo43370c62012-07-12 14:46:37 -0700489
Tejun Heof3421792010-07-02 10:03:51 +0200490 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoe87d1492012-07-13 22:16:44 -0700491 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200492 else
Tejun Heoe87d1492012-07-13 22:16:44 -0700493 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700494}
495
Tejun Heo4690c4a2010-06-29 10:07:10 +0200496static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
497 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700498{
Tejun Heof3421792010-07-02 10:03:51 +0200499 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800500 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200501 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200502 } else if (likely(cpu == WORK_CPU_UNBOUND))
503 return wq->cpu_wq.single;
504 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700505}
506
Tejun Heo73f53c42010-06-29 10:07:11 +0200507static unsigned int work_color_to_flags(int color)
508{
509 return color << WORK_STRUCT_COLOR_SHIFT;
510}
511
512static int get_work_color(struct work_struct *work)
513{
514 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
515 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
516}
517
518static int work_next_color(int color)
519{
520 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
David Howells4594bf12006-12-07 11:33:26 +0000523/*
Tejun Heoe1201532010-07-22 14:14:25 +0200524 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
525 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
526 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200527 *
528 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
529 * cwq, cpu or clear work->data. These functions should only be
530 * called while the work is owned - ie. while the PENDING bit is set.
531 *
532 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
533 * corresponding to a work. gcwq is available once the work has been
534 * queued anywhere after initialization. cwq is available only from
535 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000536 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200537static inline void set_work_data(struct work_struct *work, unsigned long data,
538 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000539{
David Howells4594bf12006-12-07 11:33:26 +0000540 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200541 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000542}
David Howells365970a2006-11-22 14:54:49 +0000543
Tejun Heo7a22ad72010-06-29 10:07:13 +0200544static void set_work_cwq(struct work_struct *work,
545 struct cpu_workqueue_struct *cwq,
546 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200547{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200549 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200550}
551
Tejun Heo7a22ad72010-06-29 10:07:13 +0200552static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000553{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
555}
556
557static void clear_work_data(struct work_struct *work)
558{
559 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
560}
561
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
563{
Tejun Heoe1201532010-07-22 14:14:25 +0200564 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565
Tejun Heoe1201532010-07-22 14:14:25 +0200566 if (data & WORK_STRUCT_CWQ)
567 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
568 else
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530569 {
570 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200571 return NULL;
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530572 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200573}
574
575static struct global_cwq *get_work_gcwq(struct work_struct *work)
576{
Tejun Heoe1201532010-07-22 14:14:25 +0200577 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200578 unsigned int cpu;
579
Tejun Heoe1201532010-07-22 14:14:25 +0200580 if (data & WORK_STRUCT_CWQ)
581 return ((struct cpu_workqueue_struct *)
Tejun Heo32089f12012-07-12 14:46:37 -0700582 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200583
584 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200585 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586 return NULL;
587
Tejun Heof3421792010-07-02 10:03:51 +0200588 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000590}
591
592/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200593 * Policy functions. These define the policies on how the global
594 * worker pool is managed. Unless noted otherwise, these functions
595 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000596 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200597
Tejun Heo43370c62012-07-12 14:46:37 -0700598static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000599{
Tejun Heo43370c62012-07-12 14:46:37 -0700600 return !atomic_read(get_pool_nr_running(pool)) ||
Tejun Heode4637a2012-07-12 14:46:37 -0700601 (pool->flags & POOL_HIGHPRI_PENDING);
David Howells365970a2006-11-22 14:54:49 +0000602}
603
Tejun Heoe22bee72010-06-29 10:07:14 +0200604/*
605 * Need to wake up a worker? Called from anything but currently
606 * running workers.
Tejun Heo546b99b2012-07-12 14:46:37 -0700607 *
608 * Note that, because unbound workers never contribute to nr_running, this
609 * function will always return %true for unbound gcwq as long as the
610 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200611 */
Tejun Heo43370c62012-07-12 14:46:37 -0700612static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000613{
Tejun Heo43370c62012-07-12 14:46:37 -0700614 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000615}
616
Tejun Heoe22bee72010-06-29 10:07:14 +0200617/* Can I start working? Called from busy but !running workers. */
Tejun Heo43370c62012-07-12 14:46:37 -0700618static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200619{
Tejun Heo43370c62012-07-12 14:46:37 -0700620 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200621}
622
623/* Do I need to keep working? Called from currently running workers. */
Tejun Heo43370c62012-07-12 14:46:37 -0700624static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200625{
Tejun Heo43370c62012-07-12 14:46:37 -0700626 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200627
Tejun Heo43370c62012-07-12 14:46:37 -0700628 return !list_empty(&pool->worklist) &&
Tejun Heo30310042010-10-11 11:51:57 +0200629 (atomic_read(nr_running) <= 1 ||
Tejun Heode4637a2012-07-12 14:46:37 -0700630 (pool->flags & POOL_HIGHPRI_PENDING));
Tejun Heoe22bee72010-06-29 10:07:14 +0200631}
632
633/* Do we need a new worker? Called from manager. */
Tejun Heo43370c62012-07-12 14:46:37 -0700634static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200635{
Tejun Heo43370c62012-07-12 14:46:37 -0700636 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200637}
638
639/* Do I need to be the manager? */
Tejun Heo43370c62012-07-12 14:46:37 -0700640static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200641{
Tejun Heo43370c62012-07-12 14:46:37 -0700642 return need_to_create_worker(pool) ||
Tejun Heode4637a2012-07-12 14:46:37 -0700643 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200644}
645
646/* Do we have too many workers and should some go away? */
Tejun Heo43370c62012-07-12 14:46:37 -0700647static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200648{
Tejun Heode4637a2012-07-12 14:46:37 -0700649 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo43370c62012-07-12 14:46:37 -0700650 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
651 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200652
653 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
654}
655
656/*
657 * Wake up functions.
658 */
659
Tejun Heo7e116292010-06-29 10:07:13 +0200660/* Return the first worker. Safe with preemption disabled */
Tejun Heo43370c62012-07-12 14:46:37 -0700661static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200662{
Tejun Heo43370c62012-07-12 14:46:37 -0700663 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200664 return NULL;
665
Tejun Heo43370c62012-07-12 14:46:37 -0700666 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200667}
668
669/**
670 * wake_up_worker - wake up an idle worker
Tejun Heo43370c62012-07-12 14:46:37 -0700671 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200672 *
Tejun Heo43370c62012-07-12 14:46:37 -0700673 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200674 *
675 * CONTEXT:
676 * spin_lock_irq(gcwq->lock).
677 */
Tejun Heo43370c62012-07-12 14:46:37 -0700678static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200679{
Tejun Heo43370c62012-07-12 14:46:37 -0700680 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200681
682 if (likely(worker))
683 wake_up_process(worker->task);
684}
685
Tejun Heo4690c4a2010-06-29 10:07:10 +0200686/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200687 * wq_worker_waking_up - a worker is waking up
688 * @task: task waking up
689 * @cpu: CPU @task is waking up to
690 *
691 * This function is called during try_to_wake_up() when a worker is
692 * being awoken.
693 *
694 * CONTEXT:
695 * spin_lock_irq(rq->lock)
696 */
697void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
698{
699 struct worker *worker = kthread_data(task);
700
Steven Rostedt2d646722010-12-03 23:12:33 -0500701 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo43370c62012-07-12 14:46:37 -0700702 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200703}
704
705/**
706 * wq_worker_sleeping - a worker is going to sleep
707 * @task: task going to sleep
708 * @cpu: CPU in question, must be the current CPU number
709 *
710 * This function is called during schedule() when a busy worker is
711 * going to sleep. Worker on the same cpu can be woken up by
712 * returning pointer to its task.
713 *
714 * CONTEXT:
715 * spin_lock_irq(rq->lock)
716 *
717 * RETURNS:
718 * Worker task on @cpu to wake up, %NULL if none.
719 */
720struct task_struct *wq_worker_sleeping(struct task_struct *task,
721 unsigned int cpu)
722{
723 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo32089f12012-07-12 14:46:37 -0700724 struct worker_pool *pool = worker->pool;
Tejun Heo43370c62012-07-12 14:46:37 -0700725 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200726
Steven Rostedt2d646722010-12-03 23:12:33 -0500727 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200728 return NULL;
729
730 /* this can only happen on the local cpu */
731 BUG_ON(cpu != raw_smp_processor_id());
732
733 /*
734 * The counterpart of the following dec_and_test, implied mb,
735 * worklist not empty test sequence is in insert_work().
736 * Please read comment there.
737 *
738 * NOT_RUNNING is clear. This means that trustee is not in
739 * charge and we're running on the local cpu w/ rq lock held
740 * and preemption disabled, which in turn means that none else
741 * could be manipulating idle_list, so dereferencing idle_list
742 * without gcwq lock is safe.
743 */
Tejun Heo32089f12012-07-12 14:46:37 -0700744 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo43370c62012-07-12 14:46:37 -0700745 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200746 return to_wakeup ? to_wakeup->task : NULL;
747}
748
749/**
750 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200751 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200752 * @flags: flags to set
753 * @wakeup: wakeup an idle worker if necessary
754 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200755 * Set @flags in @worker->flags and adjust nr_running accordingly. If
756 * nr_running becomes zero and @wakeup is %true, an idle worker is
757 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200758 *
Tejun Heocb444762010-07-02 10:03:50 +0200759 * CONTEXT:
760 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200761 */
762static inline void worker_set_flags(struct worker *worker, unsigned int flags,
763 bool wakeup)
764{
Tejun Heo32089f12012-07-12 14:46:37 -0700765 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200766
Tejun Heocb444762010-07-02 10:03:50 +0200767 WARN_ON_ONCE(worker->task != current);
768
Tejun Heoe22bee72010-06-29 10:07:14 +0200769 /*
770 * If transitioning into NOT_RUNNING, adjust nr_running and
771 * wake up an idle worker as necessary if requested by
772 * @wakeup.
773 */
774 if ((flags & WORKER_NOT_RUNNING) &&
775 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo43370c62012-07-12 14:46:37 -0700776 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200777
778 if (wakeup) {
779 if (atomic_dec_and_test(nr_running) &&
Tejun Heo32089f12012-07-12 14:46:37 -0700780 !list_empty(&pool->worklist))
Tejun Heo43370c62012-07-12 14:46:37 -0700781 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200782 } else
783 atomic_dec(nr_running);
784 }
785
Tejun Heod302f012010-06-29 10:07:13 +0200786 worker->flags |= flags;
787}
788
789/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200790 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200791 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200792 * @flags: flags to clear
793 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200794 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200795 *
Tejun Heocb444762010-07-02 10:03:50 +0200796 * CONTEXT:
797 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200798 */
799static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
800{
Tejun Heo43370c62012-07-12 14:46:37 -0700801 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200802 unsigned int oflags = worker->flags;
803
Tejun Heocb444762010-07-02 10:03:50 +0200804 WARN_ON_ONCE(worker->task != current);
805
Tejun Heod302f012010-06-29 10:07:13 +0200806 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200807
Tejun Heo42c025f2011-01-11 15:58:49 +0100808 /*
809 * If transitioning out of NOT_RUNNING, increment nr_running. Note
810 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
811 * of multiple flags, not a single flag.
812 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200813 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
814 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo43370c62012-07-12 14:46:37 -0700815 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200816}
817
818/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200819 * busy_worker_head - return the busy hash head for a work
820 * @gcwq: gcwq of interest
821 * @work: work to be hashed
822 *
823 * Return hash head of @gcwq for @work.
824 *
825 * CONTEXT:
826 * spin_lock_irq(gcwq->lock).
827 *
828 * RETURNS:
829 * Pointer to the hash head.
830 */
831static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
832 struct work_struct *work)
833{
834 const int base_shift = ilog2(sizeof(struct work_struct));
835 unsigned long v = (unsigned long)work;
836
837 /* simple shift and fold hash, do we need something better? */
838 v >>= base_shift;
839 v += v >> BUSY_WORKER_HASH_ORDER;
840 v &= BUSY_WORKER_HASH_MASK;
841
842 return &gcwq->busy_hash[v];
843}
844
845/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200846 * __find_worker_executing_work - find worker which is executing a work
847 * @gcwq: gcwq of interest
848 * @bwh: hash head as returned by busy_worker_head()
849 * @work: work to find worker for
850 *
851 * Find a worker which is executing @work on @gcwq. @bwh should be
852 * the hash head obtained by calling busy_worker_head() with the same
853 * work.
854 *
855 * CONTEXT:
856 * spin_lock_irq(gcwq->lock).
857 *
858 * RETURNS:
859 * Pointer to worker which is executing @work if found, NULL
860 * otherwise.
861 */
862static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
863 struct hlist_head *bwh,
864 struct work_struct *work)
865{
866 struct worker *worker;
867 struct hlist_node *tmp;
868
869 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800870 if (worker->current_work == work &&
871 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200872 return worker;
873 return NULL;
874}
875
876/**
877 * find_worker_executing_work - find worker which is executing a work
878 * @gcwq: gcwq of interest
879 * @work: work to find worker for
880 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800881 * Find a worker which is executing @work on @gcwq by searching
882 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
883 * to match, its current execution should match the address of @work and
884 * its work function. This is to avoid unwanted dependency between
885 * unrelated work executions through a work item being recycled while still
886 * being executed.
887 *
888 * This is a bit tricky. A work item may be freed once its execution
889 * starts and nothing prevents the freed area from being recycled for
890 * another work item. If the same work item address ends up being reused
891 * before the original execution finishes, workqueue will identify the
892 * recycled work item as currently executing and make it wait until the
893 * current execution finishes, introducing an unwanted dependency.
894 *
895 * This function checks the work item address, work function and workqueue
896 * to avoid false positives. Note that this isn't complete as one may
897 * construct a work function which can introduce dependency onto itself
898 * through a recycled work item. Well, if somebody wants to shoot oneself
899 * in the foot that badly, there's only so much we can do, and if such
900 * deadlock actually occurs, it should be easy to locate the culprit work
901 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200902 *
903 * CONTEXT:
904 * spin_lock_irq(gcwq->lock).
905 *
906 * RETURNS:
907 * Pointer to worker which is executing @work if found, NULL
908 * otherwise.
909 */
910static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
911 struct work_struct *work)
912{
913 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
914 work);
915}
916
917/**
Tejun Heo43370c62012-07-12 14:46:37 -0700918 * pool_determine_ins_pos - find insertion position
919 * @pool: pool of interest
Tejun Heo649027d2010-06-29 10:07:14 +0200920 * @cwq: cwq a work is being queued for
921 *
Tejun Heo43370c62012-07-12 14:46:37 -0700922 * A work for @cwq is about to be queued on @pool, determine insertion
Tejun Heo649027d2010-06-29 10:07:14 +0200923 * position for the work. If @cwq is for HIGHPRI wq, the work is
924 * queued at the head of the queue but in FIFO order with respect to
925 * other HIGHPRI works; otherwise, at the end of the queue. This
Tejun Heode4637a2012-07-12 14:46:37 -0700926 * function also sets POOL_HIGHPRI_PENDING flag to hint @pool that
Tejun Heo649027d2010-06-29 10:07:14 +0200927 * there are HIGHPRI works pending.
928 *
929 * CONTEXT:
930 * spin_lock_irq(gcwq->lock).
931 *
932 * RETURNS:
933 * Pointer to inserstion position.
934 */
Tejun Heo43370c62012-07-12 14:46:37 -0700935static inline struct list_head *pool_determine_ins_pos(struct worker_pool *pool,
Tejun Heo649027d2010-06-29 10:07:14 +0200936 struct cpu_workqueue_struct *cwq)
937{
938 struct work_struct *twork;
939
940 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
Tejun Heo43370c62012-07-12 14:46:37 -0700941 return &pool->worklist;
Tejun Heo649027d2010-06-29 10:07:14 +0200942
Tejun Heo43370c62012-07-12 14:46:37 -0700943 list_for_each_entry(twork, &pool->worklist, entry) {
Tejun Heo649027d2010-06-29 10:07:14 +0200944 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
945
946 if (!(tcwq->wq->flags & WQ_HIGHPRI))
947 break;
948 }
949
Tejun Heode4637a2012-07-12 14:46:37 -0700950 pool->flags |= POOL_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +0200951 return &twork->entry;
952}
953
954/**
Tejun Heo7e116292010-06-29 10:07:13 +0200955 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200956 * @cwq: cwq @work belongs to
957 * @work: work to insert
958 * @head: insertion point
959 * @extra_flags: extra WORK_STRUCT_* flags to set
960 *
Tejun Heo7e116292010-06-29 10:07:13 +0200961 * Insert @work which belongs to @cwq into @gcwq after @head.
962 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200963 *
964 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200965 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200966 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700967static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200968 struct work_struct *work, struct list_head *head,
969 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700970{
Tejun Heo43370c62012-07-12 14:46:37 -0700971 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100972
Tejun Heo4690c4a2010-06-29 10:07:10 +0200973 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200974 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200975
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700976 /*
977 * Ensure that we get the right work->data if we see the
978 * result of list_add() below, see try_to_grab_pending().
979 */
980 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200981
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700982 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200983
984 /*
985 * Ensure either worker_sched_deactivated() sees the above
986 * list_add_tail() or we see zero nr_running to avoid workers
987 * lying around lazily while there are works to be processed.
988 */
989 smp_mb();
990
Tejun Heo43370c62012-07-12 14:46:37 -0700991 if (__need_more_worker(pool))
992 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700993}
994
Tejun Heoc8efcc22010-12-20 19:32:04 +0100995/*
996 * Test whether @work is being queued from another work executing on the
997 * same workqueue. This is rather expensive and should only be used from
998 * cold paths.
999 */
1000static bool is_chained_work(struct workqueue_struct *wq)
1001{
1002 unsigned long flags;
1003 unsigned int cpu;
1004
1005 for_each_gcwq_cpu(cpu) {
1006 struct global_cwq *gcwq = get_gcwq(cpu);
1007 struct worker *worker;
1008 struct hlist_node *pos;
1009 int i;
1010
1011 spin_lock_irqsave(&gcwq->lock, flags);
1012 for_each_busy_worker(worker, i, pos, gcwq) {
1013 if (worker->task != current)
1014 continue;
1015 spin_unlock_irqrestore(&gcwq->lock, flags);
1016 /*
1017 * I'm @worker, no locking necessary. See if @work
1018 * is headed to the same workqueue.
1019 */
1020 return worker->current_cwq->wq == wq;
1021 }
1022 spin_unlock_irqrestore(&gcwq->lock, flags);
1023 }
1024 return false;
1025}
1026
Tejun Heo4690c4a2010-06-29 10:07:10 +02001027static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct work_struct *work)
1029{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001030 struct global_cwq *gcwq;
1031 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001032 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001033 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned long flags;
1035
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001036 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001037
Tejun Heoc8efcc22010-12-20 19:32:04 +01001038 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001039 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001040 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001041 return;
1042
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001043 /* determine gcwq to use */
1044 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001045 struct global_cwq *last_gcwq;
1046
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001047 if (unlikely(cpu == WORK_CPU_UNBOUND))
1048 cpu = raw_smp_processor_id();
1049
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001050 /*
1051 * It's multi cpu. If @wq is non-reentrant and @work
1052 * was previously on a different cpu, it might still
1053 * be running there, in which case the work needs to
1054 * be queued on that cpu to guarantee non-reentrance.
1055 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001056 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001057 if (wq->flags & WQ_NON_REENTRANT &&
1058 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1059 struct worker *worker;
1060
1061 spin_lock_irqsave(&last_gcwq->lock, flags);
1062
1063 worker = find_worker_executing_work(last_gcwq, work);
1064
1065 if (worker && worker->current_cwq->wq == wq)
1066 gcwq = last_gcwq;
1067 else {
1068 /* meh... not running there, queue here */
1069 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1070 spin_lock_irqsave(&gcwq->lock, flags);
1071 }
1072 } else
1073 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001074 } else {
1075 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1076 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001077 }
1078
1079 /* gcwq determined, get cwq and queue */
1080 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001081 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001082
Tejun Heo4690c4a2010-06-29 10:07:10 +02001083 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001084
Tejun Heo73f53c42010-06-29 10:07:11 +02001085 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001086 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001087
1088 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001089 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001090 cwq->nr_active++;
Tejun Heo43370c62012-07-12 14:46:37 -07001091 worklist = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001092 } else {
1093 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001094 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001095 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001096
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001097 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001098
Tejun Heo8b03ae32010-06-29 10:07:12 +02001099 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001102/**
1103 * queue_work - queue work on a workqueue
1104 * @wq: workqueue to use
1105 * @work: work to queue
1106 *
Alan Stern057647f2006-10-28 10:38:58 -07001107 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001109 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1110 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001112int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001114 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001116 ret = queue_work_on(get_cpu(), wq, work);
1117 put_cpu();
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return ret;
1120}
Dave Jonesae90dd52006-06-30 01:40:45 -04001121EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Zhang Ruic1a220e2008-07-23 21:28:39 -07001123/**
1124 * queue_work_on - queue work on specific cpu
1125 * @cpu: CPU number to execute work on
1126 * @wq: workqueue to use
1127 * @work: work to queue
1128 *
1129 * Returns 0 if @work was already on a queue, non-zero otherwise.
1130 *
1131 * We queue the work to a specific CPU, the caller must ensure it
1132 * can't go away.
1133 */
1134int
1135queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1136{
1137 int ret = 0;
1138
Tejun Heo22df02b2010-06-29 10:07:10 +02001139 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001140 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001141 ret = 1;
1142 }
1143 return ret;
1144}
1145EXPORT_SYMBOL_GPL(queue_work_on);
1146
Li Zefan6d141c32008-02-08 04:21:09 -08001147static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
David Howells52bad642006-11-22 14:54:01 +00001149 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001150 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Srinivasarao Pb6e586c2013-09-18 14:33:45 +05301152 if (cwq != NULL)
1153 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001156/**
1157 * queue_delayed_work - queue work on a workqueue after delay
1158 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001159 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001160 * @delay: number of jiffies to wait before queueing
1161 *
Alan Stern057647f2006-10-28 10:38:58 -07001162 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001163 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001164int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001165 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
David Howells52bad642006-11-22 14:54:01 +00001167 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001168 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001170 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
Dave Jonesae90dd52006-06-30 01:40:45 -04001172EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001174/**
1175 * queue_delayed_work_on - queue work on specific CPU after delay
1176 * @cpu: CPU number to execute work on
1177 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001178 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001179 * @delay: number of jiffies to wait before queueing
1180 *
Alan Stern057647f2006-10-28 10:38:58 -07001181 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001182 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001183int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001184 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001185{
1186 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001187 struct timer_list *timer = &dwork->timer;
1188 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001189
Tejun Heo22df02b2010-06-29 10:07:10 +02001190 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001191 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001192
Tejun Heo4afca922012-12-04 07:40:39 -08001193 WARN_ON_ONCE(timer_pending(timer));
1194 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001195
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001196 timer_stats_timer_set_start_info(&dwork->timer);
1197
Tejun Heo7a22ad72010-06-29 10:07:13 +02001198 /*
1199 * This stores cwq for the moment, for the timer_fn.
1200 * Note that the work's gcwq is preserved to allow
1201 * reentrance detection for delayed works.
1202 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001203 if (!(wq->flags & WQ_UNBOUND)) {
1204 struct global_cwq *gcwq = get_work_gcwq(work);
1205
1206 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1207 lcpu = gcwq->cpu;
1208 else
1209 lcpu = raw_smp_processor_id();
1210 } else
1211 lcpu = WORK_CPU_UNBOUND;
1212
Tejun Heo7a22ad72010-06-29 10:07:13 +02001213 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001214
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001215 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001216 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001217 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001218
1219 if (unlikely(cpu >= 0))
1220 add_timer_on(timer, cpu);
1221 else
1222 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001223 ret = 1;
1224 }
1225 return ret;
1226}
Dave Jonesae90dd52006-06-30 01:40:45 -04001227EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Tejun Heoc8e55f32010-06-29 10:07:12 +02001229/**
1230 * worker_enter_idle - enter idle state
1231 * @worker: worker which is entering idle state
1232 *
1233 * @worker is entering idle state. Update stats and idle timer if
1234 * necessary.
1235 *
1236 * LOCKING:
1237 * spin_lock_irq(gcwq->lock).
1238 */
1239static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Tejun Heo32089f12012-07-12 14:46:37 -07001241 struct worker_pool *pool = worker->pool;
1242 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Tejun Heoc8e55f32010-06-29 10:07:12 +02001244 BUG_ON(worker->flags & WORKER_IDLE);
1245 BUG_ON(!list_empty(&worker->entry) &&
1246 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Tejun Heocb444762010-07-02 10:03:50 +02001248 /* can't use worker_set_flags(), also called from start_worker() */
1249 worker->flags |= WORKER_IDLE;
Tejun Heo32089f12012-07-12 14:46:37 -07001250 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001251 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001252
Tejun Heoc8e55f32010-06-29 10:07:12 +02001253 /* idle_list is LIFO */
Tejun Heo32089f12012-07-12 14:46:37 -07001254 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001255
Tejun Heoe22bee72010-06-29 10:07:14 +02001256 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo43370c62012-07-12 14:46:37 -07001257 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo32089f12012-07-12 14:46:37 -07001258 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001259 jiffies + IDLE_WORKER_TIMEOUT);
1260 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001261 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001262
Tejun Heo24312d32012-05-14 15:04:50 -07001263 /*
1264 * Sanity check nr_running. Because trustee releases gcwq->lock
1265 * between setting %WORKER_ROGUE and zapping nr_running, the
1266 * warning may trigger spuriously. Check iff trustee is idle.
1267 */
1268 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo32089f12012-07-12 14:46:37 -07001269 pool->nr_workers == pool->nr_idle &&
Tejun Heo43370c62012-07-12 14:46:37 -07001270 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Tejun Heoc8e55f32010-06-29 10:07:12 +02001273/**
1274 * worker_leave_idle - leave idle state
1275 * @worker: worker which is leaving idle state
1276 *
1277 * @worker is leaving idle state. Update stats.
1278 *
1279 * LOCKING:
1280 * spin_lock_irq(gcwq->lock).
1281 */
1282static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Tejun Heo32089f12012-07-12 14:46:37 -07001284 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Tejun Heoc8e55f32010-06-29 10:07:12 +02001286 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001287 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo32089f12012-07-12 14:46:37 -07001288 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001289 list_del_init(&worker->entry);
1290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Tejun Heoe22bee72010-06-29 10:07:14 +02001292/**
1293 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1294 * @worker: self
1295 *
1296 * Works which are scheduled while the cpu is online must at least be
1297 * scheduled to a worker which is bound to the cpu so that if they are
1298 * flushed from cpu callbacks while cpu is going down, they are
1299 * guaranteed to execute on the cpu.
1300 *
1301 * This function is to be used by rogue workers and rescuers to bind
1302 * themselves to the target cpu and may race with cpu going down or
1303 * coming online. kthread_bind() can't be used because it may put the
1304 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1305 * verbatim as it's best effort and blocking and gcwq may be
1306 * [dis]associated in the meantime.
1307 *
1308 * This function tries set_cpus_allowed() and locks gcwq and verifies
1309 * the binding against GCWQ_DISASSOCIATED which is set during
1310 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1311 * idle state or fetches works without dropping lock, it can guarantee
1312 * the scheduling requirement described in the first paragraph.
1313 *
1314 * CONTEXT:
1315 * Might sleep. Called without any lock but returns with gcwq->lock
1316 * held.
1317 *
1318 * RETURNS:
1319 * %true if the associated gcwq is online (@worker is successfully
1320 * bound), %false if offline.
1321 */
1322static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001323__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001324{
Tejun Heo32089f12012-07-12 14:46:37 -07001325 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001326 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Tejun Heoe22bee72010-06-29 10:07:14 +02001328 while (true) {
1329 /*
1330 * The following call may fail, succeed or succeed
1331 * without actually migrating the task to the cpu if
1332 * it races with cpu hotunplug operation. Verify
1333 * against GCWQ_DISASSOCIATED.
1334 */
Tejun Heof3421792010-07-02 10:03:51 +02001335 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1336 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001337
Tejun Heoe22bee72010-06-29 10:07:14 +02001338 spin_lock_irq(&gcwq->lock);
1339 if (gcwq->flags & GCWQ_DISASSOCIATED)
1340 return false;
1341 if (task_cpu(task) == gcwq->cpu &&
1342 cpumask_equal(&current->cpus_allowed,
1343 get_cpu_mask(gcwq->cpu)))
1344 return true;
1345 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001346
Tejun Heo5035b202011-04-29 18:08:37 +02001347 /*
1348 * We've raced with CPU hot[un]plug. Give it a breather
1349 * and retry migration. cond_resched() is required here;
1350 * otherwise, we might deadlock against cpu_stop trying to
1351 * bring down the CPU on non-preemptive kernel.
1352 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001353 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001354 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001355 }
1356}
1357
1358/*
1359 * Function for worker->rebind_work used to rebind rogue busy workers
1360 * to the associated cpu which is coming back online. This is
1361 * scheduled by cpu up but can race with other cpu hotplug operations
1362 * and may be executed twice without intervening cpu down.
1363 */
1364static void worker_rebind_fn(struct work_struct *work)
1365{
1366 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo32089f12012-07-12 14:46:37 -07001367 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001368
1369 if (worker_maybe_bind_and_lock(worker))
1370 worker_clr_flags(worker, WORKER_REBIND);
1371
1372 spin_unlock_irq(&gcwq->lock);
1373}
1374
Tejun Heoc34056a2010-06-29 10:07:11 +02001375static struct worker *alloc_worker(void)
1376{
1377 struct worker *worker;
1378
1379 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001380 if (worker) {
1381 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001382 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001383 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1384 /* on creation a worker is in !idle && prep state */
1385 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001386 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001387 return worker;
1388}
1389
1390/**
1391 * create_worker - create a new workqueue worker
Tejun Heo43370c62012-07-12 14:46:37 -07001392 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001393 * @bind: whether to set affinity to @cpu or not
1394 *
Tejun Heo43370c62012-07-12 14:46:37 -07001395 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001396 * can be started by calling start_worker() or destroyed using
1397 * destroy_worker().
1398 *
1399 * CONTEXT:
1400 * Might sleep. Does GFP_KERNEL allocations.
1401 *
1402 * RETURNS:
1403 * Pointer to the newly created worker.
1404 */
Tejun Heo43370c62012-07-12 14:46:37 -07001405static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001406{
Tejun Heo43370c62012-07-12 14:46:37 -07001407 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001408 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001409 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001410 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001411
Tejun Heo8b03ae32010-06-29 10:07:12 +02001412 spin_lock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001413 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001414 spin_unlock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001415 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001416 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001417 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001418 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001419 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001420
1421 worker = alloc_worker();
1422 if (!worker)
1423 goto fail;
1424
Tejun Heo32089f12012-07-12 14:46:37 -07001425 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001426 worker->id = id;
1427
Tejun Heof3421792010-07-02 10:03:51 +02001428 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001429 worker->task = kthread_create_on_node(worker_thread,
1430 worker,
1431 cpu_to_node(gcwq->cpu),
1432 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001433 else
1434 worker->task = kthread_create(worker_thread, worker,
1435 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001436 if (IS_ERR(worker->task))
1437 goto fail;
1438
Tejun Heodb7bccf2010-06-29 10:07:12 +02001439 /*
1440 * A rogue worker will become a regular one if CPU comes
1441 * online later on. Make sure every worker has
1442 * PF_THREAD_BOUND set.
1443 */
Tejun Heof3421792010-07-02 10:03:51 +02001444 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001445 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001446 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001447 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001448 if (on_unbound_cpu)
1449 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001451
Tejun Heoc34056a2010-06-29 10:07:11 +02001452 return worker;
1453fail:
1454 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001455 spin_lock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001456 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001457 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001458 }
1459 kfree(worker);
1460 return NULL;
1461}
1462
1463/**
1464 * start_worker - start a newly created worker
1465 * @worker: worker to start
1466 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001467 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001468 *
1469 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001470 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001471 */
1472static void start_worker(struct worker *worker)
1473{
Tejun Heocb444762010-07-02 10:03:50 +02001474 worker->flags |= WORKER_STARTED;
Tejun Heo32089f12012-07-12 14:46:37 -07001475 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001476 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001477 wake_up_process(worker->task);
1478}
1479
1480/**
1481 * destroy_worker - destroy a workqueue worker
1482 * @worker: worker to be destroyed
1483 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001484 * Destroy @worker and adjust @gcwq stats accordingly.
1485 *
1486 * CONTEXT:
1487 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001488 */
1489static void destroy_worker(struct worker *worker)
1490{
Tejun Heo32089f12012-07-12 14:46:37 -07001491 struct worker_pool *pool = worker->pool;
1492 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001493 int id = worker->id;
1494
1495 /* sanity check frenzy */
1496 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001497 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001498
Tejun Heoc8e55f32010-06-29 10:07:12 +02001499 if (worker->flags & WORKER_STARTED)
Tejun Heo32089f12012-07-12 14:46:37 -07001500 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001501 if (worker->flags & WORKER_IDLE)
Tejun Heo32089f12012-07-12 14:46:37 -07001502 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001503
Lai Jiangshan23f09132014-02-15 22:02:28 +08001504 /*
1505 * Once WORKER_DIE is set, the kworker may destroy itself at any
1506 * point. Pin to ensure the task stays until we're done with it.
1507 */
1508 get_task_struct(worker->task);
1509
Tejun Heoc8e55f32010-06-29 10:07:12 +02001510 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001511 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001512
1513 spin_unlock_irq(&gcwq->lock);
1514
Tejun Heoc34056a2010-06-29 10:07:11 +02001515 kthread_stop(worker->task);
Lai Jiangshan23f09132014-02-15 22:02:28 +08001516 put_task_struct(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001517 kfree(worker);
1518
Tejun Heo8b03ae32010-06-29 10:07:12 +02001519 spin_lock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001520 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001521}
1522
Tejun Heo43370c62012-07-12 14:46:37 -07001523static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001524{
Tejun Heo43370c62012-07-12 14:46:37 -07001525 struct worker_pool *pool = (void *)__pool;
1526 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001527
1528 spin_lock_irq(&gcwq->lock);
1529
Tejun Heo43370c62012-07-12 14:46:37 -07001530 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001531 struct worker *worker;
1532 unsigned long expires;
1533
1534 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo43370c62012-07-12 14:46:37 -07001535 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1537
1538 if (time_before(jiffies, expires))
Tejun Heo43370c62012-07-12 14:46:37 -07001539 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001540 else {
1541 /* it's been idle for too long, wake up manager */
Tejun Heode4637a2012-07-12 14:46:37 -07001542 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo43370c62012-07-12 14:46:37 -07001543 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 }
1545 }
1546
1547 spin_unlock_irq(&gcwq->lock);
1548}
1549
1550static bool send_mayday(struct work_struct *work)
1551{
1552 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1553 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001554 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001555
1556 if (!(wq->flags & WQ_RESCUER))
1557 return false;
1558
1559 /* mayday mayday mayday */
Tejun Heo32089f12012-07-12 14:46:37 -07001560 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001561 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1562 if (cpu == WORK_CPU_UNBOUND)
1563 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001564 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 wake_up_process(wq->rescuer->task);
1566 return true;
1567}
1568
Tejun Heo43370c62012-07-12 14:46:37 -07001569static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001570{
Tejun Heo43370c62012-07-12 14:46:37 -07001571 struct worker_pool *pool = (void *)__pool;
1572 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 struct work_struct *work;
1574
1575 spin_lock_irq(&gcwq->lock);
1576
Tejun Heo43370c62012-07-12 14:46:37 -07001577 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001578 /*
1579 * We've been trying to create a new worker but
1580 * haven't been successful. We might be hitting an
1581 * allocation deadlock. Send distress signals to
1582 * rescuers.
1583 */
Tejun Heo43370c62012-07-12 14:46:37 -07001584 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 send_mayday(work);
1586 }
1587
1588 spin_unlock_irq(&gcwq->lock);
1589
Tejun Heo43370c62012-07-12 14:46:37 -07001590 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001591}
1592
1593/**
1594 * maybe_create_worker - create a new worker if necessary
Tejun Heo43370c62012-07-12 14:46:37 -07001595 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 *
Tejun Heo43370c62012-07-12 14:46:37 -07001597 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 * have at least one idle worker on return from this function. If
1599 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo43370c62012-07-12 14:46:37 -07001600 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 * possible allocation deadlock.
1602 *
1603 * On return, need_to_create_worker() is guaranteed to be false and
1604 * may_start_working() true.
1605 *
1606 * LOCKING:
1607 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1608 * multiple times. Does GFP_KERNEL allocations. Called only from
1609 * manager.
1610 *
1611 * RETURNS:
1612 * false if no action was taken and gcwq->lock stayed locked, true
1613 * otherwise.
1614 */
Tejun Heo43370c62012-07-12 14:46:37 -07001615static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001616__releases(&gcwq->lock)
1617__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001618{
Tejun Heo43370c62012-07-12 14:46:37 -07001619 struct global_cwq *gcwq = pool->gcwq;
1620
1621 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 return false;
1623restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001624 spin_unlock_irq(&gcwq->lock);
1625
Tejun Heoe22bee72010-06-29 10:07:14 +02001626 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo43370c62012-07-12 14:46:37 -07001627 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001628
1629 while (true) {
1630 struct worker *worker;
1631
Tejun Heo43370c62012-07-12 14:46:37 -07001632 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 if (worker) {
Tejun Heo43370c62012-07-12 14:46:37 -07001634 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001635 spin_lock_irq(&gcwq->lock);
1636 start_worker(worker);
Tejun Heo43370c62012-07-12 14:46:37 -07001637 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001638 return true;
1639 }
1640
Tejun Heo43370c62012-07-12 14:46:37 -07001641 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 break;
1643
Tejun Heoe22bee72010-06-29 10:07:14 +02001644 __set_current_state(TASK_INTERRUPTIBLE);
1645 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001646
Tejun Heo43370c62012-07-12 14:46:37 -07001647 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001648 break;
1649 }
1650
Tejun Heo43370c62012-07-12 14:46:37 -07001651 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001652 spin_lock_irq(&gcwq->lock);
Tejun Heo43370c62012-07-12 14:46:37 -07001653 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001654 goto restart;
1655 return true;
1656}
1657
1658/**
1659 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo43370c62012-07-12 14:46:37 -07001660 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001661 *
Tejun Heo43370c62012-07-12 14:46:37 -07001662 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001663 * IDLE_WORKER_TIMEOUT.
1664 *
1665 * LOCKING:
1666 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1667 * multiple times. Called only from manager.
1668 *
1669 * RETURNS:
1670 * false if no action was taken and gcwq->lock stayed locked, true
1671 * otherwise.
1672 */
Tejun Heo43370c62012-07-12 14:46:37 -07001673static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001674{
1675 bool ret = false;
1676
Tejun Heo43370c62012-07-12 14:46:37 -07001677 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001678 struct worker *worker;
1679 unsigned long expires;
1680
Tejun Heo43370c62012-07-12 14:46:37 -07001681 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001682 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1683
1684 if (time_before(jiffies, expires)) {
Tejun Heo43370c62012-07-12 14:46:37 -07001685 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001686 break;
1687 }
1688
1689 destroy_worker(worker);
1690 ret = true;
1691 }
1692
1693 return ret;
1694}
1695
1696/**
1697 * manage_workers - manage worker pool
1698 * @worker: self
1699 *
1700 * Assume the manager role and manage gcwq worker pool @worker belongs
1701 * to. At any given time, there can be only zero or one manager per
1702 * gcwq. The exclusion is handled automatically by this function.
1703 *
1704 * The caller can safely start processing works on false return. On
1705 * true return, it's guaranteed that need_to_create_worker() is false
1706 * and may_start_working() is true.
1707 *
1708 * CONTEXT:
1709 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1710 * multiple times. Does GFP_KERNEL allocations.
1711 *
1712 * RETURNS:
1713 * false if no action was taken and gcwq->lock stayed locked, true if
1714 * some action was taken.
1715 */
1716static bool manage_workers(struct worker *worker)
1717{
Tejun Heo43370c62012-07-12 14:46:37 -07001718 struct worker_pool *pool = worker->pool;
1719 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001720 bool ret = false;
1721
Tejun Heode4637a2012-07-12 14:46:37 -07001722 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001723 return ret;
1724
Tejun Heode4637a2012-07-12 14:46:37 -07001725 pool->flags &= ~POOL_MANAGE_WORKERS;
1726 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001727
1728 /*
1729 * Destroy and then create so that may_start_working() is true
1730 * on return.
1731 */
Tejun Heo43370c62012-07-12 14:46:37 -07001732 ret |= maybe_destroy_workers(pool);
1733 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001734
Tejun Heode4637a2012-07-12 14:46:37 -07001735 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001736
1737 /*
1738 * The trustee might be waiting to take over the manager
1739 * position, tell it we're done.
1740 */
1741 if (unlikely(gcwq->trustee))
1742 wake_up_all(&gcwq->trustee_wait);
1743
1744 return ret;
1745}
1746
Tejun Heoa62428c2010-06-29 10:07:10 +02001747/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001748 * move_linked_works - move linked works to a list
1749 * @work: start of series of works to be scheduled
1750 * @head: target list to append @work to
1751 * @nextp: out paramter for nested worklist walking
1752 *
1753 * Schedule linked works starting from @work to @head. Work series to
1754 * be scheduled starts at @work and includes any consecutive work with
1755 * WORK_STRUCT_LINKED set in its predecessor.
1756 *
1757 * If @nextp is not NULL, it's updated to point to the next work of
1758 * the last scheduled work. This allows move_linked_works() to be
1759 * nested inside outer list_for_each_entry_safe().
1760 *
1761 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001762 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001763 */
1764static void move_linked_works(struct work_struct *work, struct list_head *head,
1765 struct work_struct **nextp)
1766{
1767 struct work_struct *n;
1768
1769 /*
1770 * Linked worklist will always end before the end of the list,
1771 * use NULL for list head.
1772 */
1773 list_for_each_entry_safe_from(work, n, NULL, entry) {
1774 list_move_tail(&work->entry, head);
1775 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1776 break;
1777 }
1778
1779 /*
1780 * If we're already inside safe list traversal and have moved
1781 * multiple works to the scheduled queue, the next position
1782 * needs to be updated.
1783 */
1784 if (nextp)
1785 *nextp = n;
1786}
1787
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001788static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001789{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001790 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo43370c62012-07-12 14:46:37 -07001791 struct list_head *pos = gcwq_determine_ins_pos(cwq->pool, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001792
Tejun Heocdadf002010-10-05 10:49:55 +02001793 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001794 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001795 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001796 cwq->nr_active++;
1797}
1798
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001799static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1800{
1801 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1802 struct work_struct, entry);
1803
1804 cwq_activate_delayed_work(work);
1805}
1806
Tejun Heoaffee4b2010-06-29 10:07:12 +02001807/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001808 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1809 * @cwq: cwq of interest
1810 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001811 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001812 *
1813 * A work either has completed or is removed from pending queue,
1814 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1815 *
1816 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001817 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001818 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001819static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1820 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001821{
1822 /* ignore uncolored works */
1823 if (color == WORK_NO_COLOR)
1824 return;
1825
1826 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001827
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001828 if (!delayed) {
1829 cwq->nr_active--;
1830 if (!list_empty(&cwq->delayed_works)) {
1831 /* one down, submit a delayed one */
1832 if (cwq->nr_active < cwq->max_active)
1833 cwq_activate_first_delayed(cwq);
1834 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001835 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001836
1837 /* is flush in progress and are we at the flushing tip? */
1838 if (likely(cwq->flush_color != color))
1839 return;
1840
1841 /* are there still in-flight works? */
1842 if (cwq->nr_in_flight[color])
1843 return;
1844
1845 /* this cwq is done, clear flush_color */
1846 cwq->flush_color = -1;
1847
1848 /*
1849 * If this was the last cwq, wake up the first flusher. It
1850 * will handle the rest.
1851 */
1852 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1853 complete(&cwq->wq->first_flusher->done);
1854}
1855
1856/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001857 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001858 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001859 * @work: work to process
1860 *
1861 * Process @work. This function contains all the logics necessary to
1862 * process a single work including synchronization against and
1863 * interaction with other workers on the same cpu, queueing and
1864 * flushing. As long as context requirement is met, any worker can
1865 * call this function to process a work.
1866 *
1867 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001868 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001869 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001870static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001871__releases(&gcwq->lock)
1872__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001873{
Tejun Heo7e116292010-06-29 10:07:13 +02001874 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo32089f12012-07-12 14:46:37 -07001875 struct worker_pool *pool = worker->pool;
1876 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001877 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001878 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001879 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001880 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001881#ifdef CONFIG_LOCKDEP
1882 /*
1883 * It is permissible to free the struct work_struct from
1884 * inside the function that is called from it, this we need to
1885 * take into account for lockdep too. To avoid bogus "held
1886 * lock freed" warnings as well as problems when looking into
1887 * work->lockdep_map, make a copy and use that here.
1888 */
1889 struct lockdep_map lockdep_map = work->lockdep_map;
1890#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001891 /*
1892 * A single work shouldn't be executed concurrently by
1893 * multiple workers on a single cpu. Check whether anyone is
1894 * already processing the work. If so, defer the work to the
1895 * currently executing one.
1896 */
1897 collision = __find_worker_executing_work(gcwq, bwh, work);
1898 if (unlikely(collision)) {
1899 move_linked_works(work, &collision->scheduled, NULL);
1900 return;
1901 }
1902
Tejun Heoa62428c2010-06-29 10:07:10 +02001903 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001904 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001905 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001906 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001907 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001908 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001909 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001910
Tejun Heo7a22ad72010-06-29 10:07:13 +02001911 /* record the current cpu number in the work data and dequeue */
1912 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001913 list_del_init(&work->entry);
1914
Tejun Heo649027d2010-06-29 10:07:14 +02001915 /*
1916 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1917 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1918 */
Tejun Heode4637a2012-07-12 14:46:37 -07001919 if (unlikely(pool->flags & POOL_HIGHPRI_PENDING)) {
Tejun Heo32089f12012-07-12 14:46:37 -07001920 struct work_struct *nwork = list_first_entry(&pool->worklist,
1921 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001922
Tejun Heo32089f12012-07-12 14:46:37 -07001923 if (!list_empty(&pool->worklist) &&
Tejun Heo649027d2010-06-29 10:07:14 +02001924 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
Tejun Heo43370c62012-07-12 14:46:37 -07001925 wake_up_worker(pool);
Tejun Heo649027d2010-06-29 10:07:14 +02001926 else
Tejun Heode4637a2012-07-12 14:46:37 -07001927 pool->flags &= ~POOL_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +02001928 }
1929
Tejun Heofb0e7be2010-06-29 10:07:15 +02001930 /*
1931 * CPU intensive works don't participate in concurrency
1932 * management. They're the scheduler's responsibility.
1933 */
1934 if (unlikely(cpu_intensive))
1935 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1936
Tejun Heo546b99b2012-07-12 14:46:37 -07001937 /*
1938 * Unbound gcwq isn't concurrency managed and work items should be
1939 * executed ASAP. Wake up another worker if necessary.
1940 */
Tejun Heo43370c62012-07-12 14:46:37 -07001941 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1942 wake_up_worker(pool);
Tejun Heo546b99b2012-07-12 14:46:37 -07001943
Tejun Heo8b03ae32010-06-29 10:07:12 +02001944 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001945
Tejun Heo66307ae2012-08-03 10:30:45 -07001946 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001947 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001948
Tejun Heoe1594892011-01-09 23:32:15 +01001949 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001950 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001951 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001952 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001953 /*
1954 * While we must be careful to not use "work" after this, the trace
1955 * point will only record its address.
1956 */
1957 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001958 lock_map_release(&lockdep_map);
1959 lock_map_release(&cwq->wq->lockdep_map);
1960
1961 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001962 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1963 " last function: %pf\n",
1964 current->comm, preempt_count(), task_pid_nr(current),
1965 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001966 debug_show_held_locks(current);
1967 dump_stack();
1968 }
1969
Tejun Heo8b03ae32010-06-29 10:07:12 +02001970 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001971
Tejun Heofb0e7be2010-06-29 10:07:15 +02001972 /* clear cpu intensive status */
1973 if (unlikely(cpu_intensive))
1974 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1975
Tejun Heoa62428c2010-06-29 10:07:10 +02001976 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001978 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001979 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001980 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001981 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001982}
1983
Tejun Heoaffee4b2010-06-29 10:07:12 +02001984/**
1985 * process_scheduled_works - process scheduled works
1986 * @worker: self
1987 *
1988 * Process all scheduled works. Please note that the scheduled list
1989 * may change while processing a work, so this function repeatedly
1990 * fetches a work from the top and executes it.
1991 *
1992 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001993 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001994 * multiple times.
1995 */
1996static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001998 while (!list_empty(&worker->scheduled)) {
1999 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002001 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Tejun Heo4690c4a2010-06-29 10:07:10 +02002005/**
2006 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002007 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002008 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 * The gcwq worker thread function. There's a single dynamic pool of
2010 * these per each cpu. These workers process all works regardless of
2011 * their specific target workqueue. The only exception is works which
2012 * belong to workqueues with a rescuer which will be explained in
2013 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002014 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002015static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Tejun Heoc34056a2010-06-29 10:07:11 +02002017 struct worker *worker = __worker;
Tejun Heo32089f12012-07-12 14:46:37 -07002018 struct worker_pool *pool = worker->pool;
2019 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Tejun Heoe22bee72010-06-29 10:07:14 +02002021 /* tell the scheduler that this is a workqueue worker */
2022 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002023woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002024 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Tejun Heoc8e55f32010-06-29 10:07:12 +02002026 /* DIE can be set only while we're idle, checking here is enough */
2027 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002028 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032
Tejun Heoc8e55f32010-06-29 10:07:12 +02002033 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002034recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002035 /* no more worker necessary? */
Tejun Heo43370c62012-07-12 14:46:37 -07002036 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 goto sleep;
2038
2039 /* do we need to manage? */
Tejun Heo43370c62012-07-12 14:46:37 -07002040 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 goto recheck;
2042
Tejun Heoc8e55f32010-06-29 10:07:12 +02002043 /*
2044 * ->scheduled list can only be filled while a worker is
2045 * preparing to process a work or actually processing it.
2046 * Make sure nobody diddled with it while I was sleeping.
2047 */
2048 BUG_ON(!list_empty(&worker->scheduled));
2049
Tejun Heoe22bee72010-06-29 10:07:14 +02002050 /*
2051 * When control reaches this point, we're guaranteed to have
2052 * at least one idle worker or that someone else has already
2053 * assumed the manager role.
2054 */
2055 worker_clr_flags(worker, WORKER_PREP);
2056
2057 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002058 struct work_struct *work =
Tejun Heo32089f12012-07-12 14:46:37 -07002059 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002060 struct work_struct, entry);
2061
2062 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2063 /* optimization path, not strictly necessary */
2064 process_one_work(worker, work);
2065 if (unlikely(!list_empty(&worker->scheduled)))
2066 process_scheduled_works(worker);
2067 } else {
2068 move_linked_works(work, &worker->scheduled, NULL);
2069 process_scheduled_works(worker);
2070 }
Tejun Heo43370c62012-07-12 14:46:37 -07002071 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002072
Tejun Heoe22bee72010-06-29 10:07:14 +02002073 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002074sleep:
Tejun Heo43370c62012-07-12 14:46:37 -07002075 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002076 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002077
Tejun Heoc8e55f32010-06-29 10:07:12 +02002078 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002079 * gcwq->lock is held and there's no work to process and no
2080 * need to manage, sleep. Workers are woken up only while
2081 * holding gcwq->lock or from local cpu, so setting the
2082 * current state before releasing gcwq->lock is enough to
2083 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002084 */
2085 worker_enter_idle(worker);
2086 __set_current_state(TASK_INTERRUPTIBLE);
2087 spin_unlock_irq(&gcwq->lock);
2088 schedule();
2089 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Tejun Heoe22bee72010-06-29 10:07:14 +02002092/**
2093 * rescuer_thread - the rescuer thread function
2094 * @__wq: the associated workqueue
2095 *
2096 * Workqueue rescuer thread function. There's one rescuer for each
2097 * workqueue which has WQ_RESCUER set.
2098 *
2099 * Regular work processing on a gcwq may block trying to create a new
2100 * worker which uses GFP_KERNEL allocation which has slight chance of
2101 * developing into deadlock if some works currently on the same queue
2102 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2103 * the problem rescuer solves.
2104 *
2105 * When such condition is possible, the gcwq summons rescuers of all
2106 * workqueues which have works queued on the gcwq and let them process
2107 * those works so that forward progress can be guaranteed.
2108 *
2109 * This should happen rarely.
2110 */
2111static int rescuer_thread(void *__wq)
2112{
2113 struct workqueue_struct *wq = __wq;
2114 struct worker *rescuer = wq->rescuer;
2115 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002116 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002117 unsigned int cpu;
2118
2119 set_user_nice(current, RESCUER_NICE_LEVEL);
2120repeat:
2121 set_current_state(TASK_INTERRUPTIBLE);
2122
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002123 if (kthread_should_stop()) {
2124 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002125 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002126 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002127
Tejun Heof3421792010-07-02 10:03:51 +02002128 /*
2129 * See whether any cpu is asking for help. Unbounded
2130 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2131 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002132 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002133 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2134 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo32089f12012-07-12 14:46:37 -07002135 struct worker_pool *pool = cwq->pool;
2136 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002137 struct work_struct *work, *n;
2138
2139 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002140 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002141
2142 /* migrate to the target cpu if possible */
Tejun Heo32089f12012-07-12 14:46:37 -07002143 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002144 worker_maybe_bind_and_lock(rescuer);
2145
2146 /*
2147 * Slurp in all works issued via this workqueue and
2148 * process'em.
2149 */
2150 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo32089f12012-07-12 14:46:37 -07002151 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002152 if (get_work_cwq(work) == cwq)
2153 move_linked_works(work, scheduled, &n);
2154
2155 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002156
2157 /*
2158 * Leave this gcwq. If keep_working() is %true, notify a
2159 * regular worker; otherwise, we end up with 0 concurrency
2160 * and stalling the execution.
2161 */
Tejun Heo43370c62012-07-12 14:46:37 -07002162 if (keep_working(pool))
2163 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002164
Tejun Heoe22bee72010-06-29 10:07:14 +02002165 spin_unlock_irq(&gcwq->lock);
2166 }
2167
2168 schedule();
2169 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002172struct wq_barrier {
2173 struct work_struct work;
2174 struct completion done;
2175};
2176
2177static void wq_barrier_func(struct work_struct *work)
2178{
2179 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2180 complete(&barr->done);
2181}
2182
Tejun Heo4690c4a2010-06-29 10:07:10 +02002183/**
2184 * insert_wq_barrier - insert a barrier work
2185 * @cwq: cwq to insert barrier into
2186 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002187 * @target: target work to attach @barr to
2188 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002189 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002190 * @barr is linked to @target such that @barr is completed only after
2191 * @target finishes execution. Please note that the ordering
2192 * guarantee is observed only with respect to @target and on the local
2193 * cpu.
2194 *
2195 * Currently, a queued barrier can't be canceled. This is because
2196 * try_to_grab_pending() can't determine whether the work to be
2197 * grabbed is at the head of the queue and thus can't clear LINKED
2198 * flag of the previous work while there must be a valid next work
2199 * after a work with LINKED flag set.
2200 *
2201 * Note that when @worker is non-NULL, @target may be modified
2202 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002203 *
2204 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002205 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002206 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002207static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002208 struct wq_barrier *barr,
2209 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002210{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002211 struct list_head *head;
2212 unsigned int linked = 0;
2213
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002214 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002215 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002216 * as we know for sure that this will not trigger any of the
2217 * checks and call back into the fixup functions where we
2218 * might deadlock.
2219 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002220 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002221 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002222 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002223
Tejun Heoaffee4b2010-06-29 10:07:12 +02002224 /*
2225 * If @target is currently being executed, schedule the
2226 * barrier to the worker; otherwise, put it after @target.
2227 */
2228 if (worker)
2229 head = worker->scheduled.next;
2230 else {
2231 unsigned long *bits = work_data_bits(target);
2232
2233 head = target->entry.next;
2234 /* there can already be other linked works, inherit and set */
2235 linked = *bits & WORK_STRUCT_LINKED;
2236 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2237 }
2238
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002239 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002240 insert_work(cwq, &barr->work, head,
2241 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002242}
2243
Tejun Heo73f53c42010-06-29 10:07:11 +02002244/**
2245 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2246 * @wq: workqueue being flushed
2247 * @flush_color: new flush color, < 0 for no-op
2248 * @work_color: new work color, < 0 for no-op
2249 *
2250 * Prepare cwqs for workqueue flushing.
2251 *
2252 * If @flush_color is non-negative, flush_color on all cwqs should be
2253 * -1. If no cwq has in-flight commands at the specified color, all
2254 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2255 * has in flight commands, its cwq->flush_color is set to
2256 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2257 * wakeup logic is armed and %true is returned.
2258 *
2259 * The caller should have initialized @wq->first_flusher prior to
2260 * calling this function with non-negative @flush_color. If
2261 * @flush_color is negative, no flush color update is done and %false
2262 * is returned.
2263 *
2264 * If @work_color is non-negative, all cwqs should have the same
2265 * work_color which is previous to @work_color and all will be
2266 * advanced to @work_color.
2267 *
2268 * CONTEXT:
2269 * mutex_lock(wq->flush_mutex).
2270 *
2271 * RETURNS:
2272 * %true if @flush_color >= 0 and there's something to flush. %false
2273 * otherwise.
2274 */
2275static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2276 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Tejun Heo73f53c42010-06-29 10:07:11 +02002278 bool wait = false;
2279 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002280
Tejun Heo73f53c42010-06-29 10:07:11 +02002281 if (flush_color >= 0) {
2282 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2283 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002284 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002285
Tejun Heof3421792010-07-02 10:03:51 +02002286 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002287 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo32089f12012-07-12 14:46:37 -07002288 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002289
Tejun Heo8b03ae32010-06-29 10:07:12 +02002290 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002291
2292 if (flush_color >= 0) {
2293 BUG_ON(cwq->flush_color != -1);
2294
2295 if (cwq->nr_in_flight[flush_color]) {
2296 cwq->flush_color = flush_color;
2297 atomic_inc(&wq->nr_cwqs_to_flush);
2298 wait = true;
2299 }
2300 }
2301
2302 if (work_color >= 0) {
2303 BUG_ON(work_color != work_next_color(cwq->work_color));
2304 cwq->work_color = work_color;
2305 }
2306
Tejun Heo8b03ae32010-06-29 10:07:12 +02002307 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002308 }
2309
2310 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2311 complete(&wq->first_flusher->done);
2312
2313 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002316/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002318 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 *
2320 * Forces execution of the workqueue and blocks until its completion.
2321 * This is typically used in driver shutdown handlers.
2322 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002323 * We sleep until all works which were queued on entry have been handled,
2324 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002326void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Tejun Heo73f53c42010-06-29 10:07:11 +02002328 struct wq_flusher this_flusher = {
2329 .list = LIST_HEAD_INIT(this_flusher.list),
2330 .flush_color = -1,
2331 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2332 };
2333 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002334
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002335 lock_map_acquire(&wq->lockdep_map);
2336 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002337
2338 mutex_lock(&wq->flush_mutex);
2339
2340 /*
2341 * Start-to-wait phase
2342 */
2343 next_color = work_next_color(wq->work_color);
2344
2345 if (next_color != wq->flush_color) {
2346 /*
2347 * Color space is not full. The current work_color
2348 * becomes our flush_color and work_color is advanced
2349 * by one.
2350 */
2351 BUG_ON(!list_empty(&wq->flusher_overflow));
2352 this_flusher.flush_color = wq->work_color;
2353 wq->work_color = next_color;
2354
2355 if (!wq->first_flusher) {
2356 /* no flush in progress, become the first flusher */
2357 BUG_ON(wq->flush_color != this_flusher.flush_color);
2358
2359 wq->first_flusher = &this_flusher;
2360
2361 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2362 wq->work_color)) {
2363 /* nothing to flush, done */
2364 wq->flush_color = next_color;
2365 wq->first_flusher = NULL;
2366 goto out_unlock;
2367 }
2368 } else {
2369 /* wait in queue */
2370 BUG_ON(wq->flush_color == this_flusher.flush_color);
2371 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2372 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2373 }
2374 } else {
2375 /*
2376 * Oops, color space is full, wait on overflow queue.
2377 * The next flush completion will assign us
2378 * flush_color and transfer to flusher_queue.
2379 */
2380 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2381 }
2382
2383 mutex_unlock(&wq->flush_mutex);
2384
2385 wait_for_completion(&this_flusher.done);
2386
2387 /*
2388 * Wake-up-and-cascade phase
2389 *
2390 * First flushers are responsible for cascading flushes and
2391 * handling overflow. Non-first flushers can simply return.
2392 */
2393 if (wq->first_flusher != &this_flusher)
2394 return;
2395
2396 mutex_lock(&wq->flush_mutex);
2397
Tejun Heo4ce48b32010-07-02 10:03:51 +02002398 /* we might have raced, check again with mutex held */
2399 if (wq->first_flusher != &this_flusher)
2400 goto out_unlock;
2401
Tejun Heo73f53c42010-06-29 10:07:11 +02002402 wq->first_flusher = NULL;
2403
2404 BUG_ON(!list_empty(&this_flusher.list));
2405 BUG_ON(wq->flush_color != this_flusher.flush_color);
2406
2407 while (true) {
2408 struct wq_flusher *next, *tmp;
2409
2410 /* complete all the flushers sharing the current flush color */
2411 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2412 if (next->flush_color != wq->flush_color)
2413 break;
2414 list_del_init(&next->list);
2415 complete(&next->done);
2416 }
2417
2418 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2419 wq->flush_color != work_next_color(wq->work_color));
2420
2421 /* this flush_color is finished, advance by one */
2422 wq->flush_color = work_next_color(wq->flush_color);
2423
2424 /* one color has been freed, handle overflow queue */
2425 if (!list_empty(&wq->flusher_overflow)) {
2426 /*
2427 * Assign the same color to all overflowed
2428 * flushers, advance work_color and append to
2429 * flusher_queue. This is the start-to-wait
2430 * phase for these overflowed flushers.
2431 */
2432 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2433 tmp->flush_color = wq->work_color;
2434
2435 wq->work_color = work_next_color(wq->work_color);
2436
2437 list_splice_tail_init(&wq->flusher_overflow,
2438 &wq->flusher_queue);
2439 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2440 }
2441
2442 if (list_empty(&wq->flusher_queue)) {
2443 BUG_ON(wq->flush_color != wq->work_color);
2444 break;
2445 }
2446
2447 /*
2448 * Need to flush more colors. Make the next flusher
2449 * the new first flusher and arm cwqs.
2450 */
2451 BUG_ON(wq->flush_color == wq->work_color);
2452 BUG_ON(wq->flush_color != next->flush_color);
2453
2454 list_del_init(&next->list);
2455 wq->first_flusher = next;
2456
2457 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2458 break;
2459
2460 /*
2461 * Meh... this color is already done, clear first
2462 * flusher and repeat cascading.
2463 */
2464 wq->first_flusher = NULL;
2465 }
2466
2467out_unlock:
2468 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
Dave Jonesae90dd52006-06-30 01:40:45 -04002470EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002472/**
2473 * drain_workqueue - drain a workqueue
2474 * @wq: workqueue to drain
2475 *
2476 * Wait until the workqueue becomes empty. While draining is in progress,
2477 * only chain queueing is allowed. IOW, only currently pending or running
2478 * work items on @wq can queue further work items on it. @wq is flushed
2479 * repeatedly until it becomes empty. The number of flushing is detemined
2480 * by the depth of chaining and should be relatively short. Whine if it
2481 * takes too long.
2482 */
2483void drain_workqueue(struct workqueue_struct *wq)
2484{
2485 unsigned int flush_cnt = 0;
2486 unsigned int cpu;
2487
2488 /*
2489 * __queue_work() needs to test whether there are drainers, is much
2490 * hotter than drain_workqueue() and already looks at @wq->flags.
2491 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2492 */
2493 spin_lock(&workqueue_lock);
2494 if (!wq->nr_drainers++)
2495 wq->flags |= WQ_DRAINING;
2496 spin_unlock(&workqueue_lock);
2497reflush:
2498 flush_workqueue(wq);
2499
2500 for_each_cwq_cpu(cpu, wq) {
2501 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002502 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002503
Tejun Heo32089f12012-07-12 14:46:37 -07002504 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002505 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo32089f12012-07-12 14:46:37 -07002506 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002507
2508 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002509 continue;
2510
2511 if (++flush_cnt == 10 ||
2512 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2513 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2514 wq->name, flush_cnt);
2515 goto reflush;
2516 }
2517
2518 spin_lock(&workqueue_lock);
2519 if (!--wq->nr_drainers)
2520 wq->flags &= ~WQ_DRAINING;
2521 spin_unlock(&workqueue_lock);
2522}
2523EXPORT_SYMBOL_GPL(drain_workqueue);
2524
Tejun Heobaf59022010-09-16 10:42:16 +02002525static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2526 bool wait_executing)
2527{
2528 struct worker *worker = NULL;
2529 struct global_cwq *gcwq;
2530 struct cpu_workqueue_struct *cwq;
2531
2532 might_sleep();
2533 gcwq = get_work_gcwq(work);
2534 if (!gcwq)
2535 return false;
2536
2537 spin_lock_irq(&gcwq->lock);
2538 if (!list_empty(&work->entry)) {
2539 /*
2540 * See the comment near try_to_grab_pending()->smp_rmb().
2541 * If it was re-queued to a different gcwq under us, we
2542 * are not going to wait.
2543 */
2544 smp_rmb();
2545 cwq = get_work_cwq(work);
Tejun Heo32089f12012-07-12 14:46:37 -07002546 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002547 goto already_gone;
2548 } else if (wait_executing) {
2549 worker = find_worker_executing_work(gcwq, work);
2550 if (!worker)
2551 goto already_gone;
2552 cwq = worker->current_cwq;
2553 } else
2554 goto already_gone;
2555
2556 insert_wq_barrier(cwq, barr, work, worker);
2557 spin_unlock_irq(&gcwq->lock);
2558
Tejun Heoe1594892011-01-09 23:32:15 +01002559 /*
2560 * If @max_active is 1 or rescuer is in use, flushing another work
2561 * item on the same workqueue may lead to deadlock. Make sure the
2562 * flusher is not running on the same workqueue by verifying write
2563 * access.
2564 */
2565 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2566 lock_map_acquire(&cwq->wq->lockdep_map);
2567 else
2568 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002569 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002570
Tejun Heobaf59022010-09-16 10:42:16 +02002571 return true;
2572already_gone:
2573 spin_unlock_irq(&gcwq->lock);
2574 return false;
2575}
2576
Oleg Nesterovdb700892008-07-25 01:47:49 -07002577/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002578 * flush_work - wait for a work to finish executing the last queueing instance
2579 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002580 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002581 * Wait until @work has finished execution. This function considers
2582 * only the last queueing instance of @work. If @work has been
2583 * enqueued across different CPUs on a non-reentrant workqueue or on
2584 * multiple workqueues, @work might still be executing on return on
2585 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002586 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002587 * If @work was queued only on a non-reentrant, ordered or unbound
2588 * workqueue, @work is guaranteed to be idle on return if it hasn't
2589 * been requeued since flush started.
2590 *
2591 * RETURNS:
2592 * %true if flush_work() waited for the work to finish execution,
2593 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002594 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002595bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002596{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002597 struct wq_barrier barr;
2598
Tejun Heobaf59022010-09-16 10:42:16 +02002599 if (start_flush_work(work, &barr, true)) {
2600 wait_for_completion(&barr.done);
2601 destroy_work_on_stack(&barr.work);
2602 return true;
2603 } else
2604 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002605}
2606EXPORT_SYMBOL_GPL(flush_work);
2607
Tejun Heo401a8d02010-09-16 10:36:00 +02002608static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2609{
2610 struct wq_barrier barr;
2611 struct worker *worker;
2612
2613 spin_lock_irq(&gcwq->lock);
2614
2615 worker = find_worker_executing_work(gcwq, work);
2616 if (unlikely(worker))
2617 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2618
2619 spin_unlock_irq(&gcwq->lock);
2620
2621 if (unlikely(worker)) {
2622 wait_for_completion(&barr.done);
2623 destroy_work_on_stack(&barr.work);
2624 return true;
2625 } else
2626 return false;
2627}
2628
2629static bool wait_on_work(struct work_struct *work)
2630{
2631 bool ret = false;
2632 int cpu;
2633
2634 might_sleep();
2635
2636 lock_map_acquire(&work->lockdep_map);
2637 lock_map_release(&work->lockdep_map);
2638
2639 for_each_gcwq_cpu(cpu)
2640 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2641 return ret;
2642}
2643
Tejun Heo09383492010-09-16 10:48:29 +02002644/**
2645 * flush_work_sync - wait until a work has finished execution
2646 * @work: the work to flush
2647 *
2648 * Wait until @work has finished execution. On return, it's
2649 * guaranteed that all queueing instances of @work which happened
2650 * before this function is called are finished. In other words, if
2651 * @work hasn't been requeued since this function was called, @work is
2652 * guaranteed to be idle on return.
2653 *
2654 * RETURNS:
2655 * %true if flush_work_sync() waited for the work to finish execution,
2656 * %false if it was already idle.
2657 */
2658bool flush_work_sync(struct work_struct *work)
2659{
2660 struct wq_barrier barr;
2661 bool pending, waited;
2662
2663 /* we'll wait for executions separately, queue barr only if pending */
2664 pending = start_flush_work(work, &barr, false);
2665
2666 /* wait for executions to finish */
2667 waited = wait_on_work(work);
2668
2669 /* wait for the pending one */
2670 if (pending) {
2671 wait_for_completion(&barr.done);
2672 destroy_work_on_stack(&barr.work);
2673 }
2674
2675 return pending || waited;
2676}
2677EXPORT_SYMBOL_GPL(flush_work_sync);
2678
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002679/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002680 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002681 * so this work can't be re-armed in any way.
2682 */
2683static int try_to_grab_pending(struct work_struct *work)
2684{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002685 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002686 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002687
Tejun Heo22df02b2010-06-29 10:07:10 +02002688 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002689 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002690
2691 /*
2692 * The queueing is in progress, or it is already queued. Try to
2693 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2694 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002695 gcwq = get_work_gcwq(work);
2696 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002697 return ret;
2698
Tejun Heo8b03ae32010-06-29 10:07:12 +02002699 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002700 if (!list_empty(&work->entry)) {
2701 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002702 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002703 * In that case we must see the new value after rmb(), see
2704 * insert_work()->wmb().
2705 */
2706 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002707 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002708 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002709
2710 /*
2711 * A delayed work item cannot be grabbed directly
2712 * because it might have linked NO_COLOR work items
2713 * which, if left on the delayed_list, will confuse
2714 * cwq->nr_active management later on and cause
2715 * stall. Make sure the work item is activated
2716 * before grabbing.
2717 */
2718 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2719 cwq_activate_delayed_work(work);
2720
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002721 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002722 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002723 get_work_color(work),
2724 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002725 ret = 1;
2726 }
2727 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002728 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002729
2730 return ret;
2731}
2732
Tejun Heo401a8d02010-09-16 10:36:00 +02002733static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002734 struct timer_list* timer)
2735{
2736 int ret;
2737
2738 do {
2739 ret = (timer && likely(del_timer(timer)));
2740 if (!ret)
2741 ret = try_to_grab_pending(work);
2742 wait_on_work(work);
2743 } while (unlikely(ret < 0));
2744
Tejun Heo7a22ad72010-06-29 10:07:13 +02002745 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002746 return ret;
2747}
2748
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002749/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002750 * cancel_work_sync - cancel a work and wait for it to finish
2751 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002752 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002753 * Cancel @work and wait for its execution to finish. This function
2754 * can be used even if the work re-queues itself or migrates to
2755 * another workqueue. On return from this function, @work is
2756 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002757 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002758 * cancel_work_sync(&delayed_work->work) must not be used for
2759 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002760 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002761 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002762 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002763 *
2764 * RETURNS:
2765 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002766 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002767bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002768{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002769 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002770}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002771EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002772
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002773/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002774 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2775 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002776 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002777 * Delayed timer is cancelled and the pending work is queued for
2778 * immediate execution. Like flush_work(), this function only
2779 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002780 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002781 * RETURNS:
2782 * %true if flush_work() waited for the work to finish execution,
2783 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002784 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002785bool flush_delayed_work(struct delayed_work *dwork)
2786{
2787 if (del_timer_sync(&dwork->timer))
2788 __queue_work(raw_smp_processor_id(),
2789 get_work_cwq(&dwork->work)->wq, &dwork->work);
2790 return flush_work(&dwork->work);
2791}
2792EXPORT_SYMBOL(flush_delayed_work);
2793
2794/**
Tejun Heo09383492010-09-16 10:48:29 +02002795 * flush_delayed_work_sync - wait for a dwork to finish
2796 * @dwork: the delayed work to flush
2797 *
2798 * Delayed timer is cancelled and the pending work is queued for
2799 * execution immediately. Other than timer handling, its behavior
2800 * is identical to flush_work_sync().
2801 *
2802 * RETURNS:
2803 * %true if flush_work_sync() waited for the work to finish execution,
2804 * %false if it was already idle.
2805 */
2806bool flush_delayed_work_sync(struct delayed_work *dwork)
2807{
2808 if (del_timer_sync(&dwork->timer))
2809 __queue_work(raw_smp_processor_id(),
2810 get_work_cwq(&dwork->work)->wq, &dwork->work);
2811 return flush_work_sync(&dwork->work);
2812}
2813EXPORT_SYMBOL(flush_delayed_work_sync);
2814
2815/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002816 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2817 * @dwork: the delayed work cancel
2818 *
2819 * This is cancel_work_sync() for delayed works.
2820 *
2821 * RETURNS:
2822 * %true if @dwork was pending, %false otherwise.
2823 */
2824bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002825{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002826 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002827}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002828EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002830/**
2831 * schedule_work - put work task in global workqueue
2832 * @work: job to be done
2833 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002834 * Returns zero if @work was already on the kernel-global workqueue and
2835 * non-zero otherwise.
2836 *
2837 * This puts a job in the kernel-global workqueue if it was not already
2838 * queued and leaves it in the same position on the kernel-global
2839 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002840 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002841int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Tejun Heod320c032010-06-29 10:07:14 +02002843 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
Dave Jonesae90dd52006-06-30 01:40:45 -04002845EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Zhang Ruic1a220e2008-07-23 21:28:39 -07002847/*
2848 * schedule_work_on - put work task on a specific cpu
2849 * @cpu: cpu to put the work task on
2850 * @work: job to be done
2851 *
2852 * This puts a job on a specific cpu
2853 */
2854int schedule_work_on(int cpu, struct work_struct *work)
2855{
Tejun Heod320c032010-06-29 10:07:14 +02002856 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002857}
2858EXPORT_SYMBOL(schedule_work_on);
2859
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002860/**
2861 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002862 * @dwork: job to be done
2863 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002864 *
2865 * After waiting for a given time this puts a job in the kernel-global
2866 * workqueue.
2867 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002868int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002869 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Tejun Heod320c032010-06-29 10:07:14 +02002871 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
Dave Jonesae90dd52006-06-30 01:40:45 -04002873EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002875/**
2876 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2877 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002878 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002879 * @delay: number of jiffies to wait
2880 *
2881 * After waiting for a given time this puts a job in the kernel-global
2882 * workqueue on the specified CPU.
2883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002885 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Tejun Heod320c032010-06-29 10:07:14 +02002887 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
Dave Jonesae90dd52006-06-30 01:40:45 -04002889EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Andrew Mortonb6136772006-06-25 05:47:49 -07002891/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002892 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002893 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002894 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002895 * schedule_on_each_cpu() executes @func on each online CPU using the
2896 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002897 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002898 *
2899 * RETURNS:
2900 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002901 */
David Howells65f27f32006-11-22 14:55:48 +00002902int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002903{
2904 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002905 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002906
Andrew Mortonb6136772006-06-25 05:47:49 -07002907 works = alloc_percpu(struct work_struct);
2908 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002909 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002910
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002911 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002912
Christoph Lameter15316ba2006-01-08 01:00:43 -08002913 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002914 struct work_struct *work = per_cpu_ptr(works, cpu);
2915
2916 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002917 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002918 }
Tejun Heo93981802009-11-17 14:06:20 -08002919
2920 for_each_online_cpu(cpu)
2921 flush_work(per_cpu_ptr(works, cpu));
2922
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002923 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002924 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002925 return 0;
2926}
2927
Alan Sterneef6a7d2010-02-12 17:39:21 +09002928/**
2929 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2930 *
2931 * Forces execution of the kernel-global workqueue and blocks until its
2932 * completion.
2933 *
2934 * Think twice before calling this function! It's very easy to get into
2935 * trouble if you don't take great care. Either of the following situations
2936 * will lead to deadlock:
2937 *
2938 * One of the work items currently on the workqueue needs to acquire
2939 * a lock held by your code or its caller.
2940 *
2941 * Your code is running in the context of a work routine.
2942 *
2943 * They will be detected by lockdep when they occur, but the first might not
2944 * occur very often. It depends on what work items are on the workqueue and
2945 * what locks they need, which you have no control over.
2946 *
2947 * In most situations flushing the entire workqueue is overkill; you merely
2948 * need to know that a particular work item isn't queued and isn't running.
2949 * In such cases you should use cancel_delayed_work_sync() or
2950 * cancel_work_sync() instead.
2951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952void flush_scheduled_work(void)
2953{
Tejun Heod320c032010-06-29 10:07:14 +02002954 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
Dave Jonesae90dd52006-06-30 01:40:45 -04002956EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002959 * execute_in_process_context - reliably execute the routine with user context
2960 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002961 * @ew: guaranteed storage for the execute work structure (must
2962 * be available when the work executes)
2963 *
2964 * Executes the function immediately if process context is available,
2965 * otherwise schedules the function for delayed execution.
2966 *
2967 * Returns: 0 - function was executed
2968 * 1 - function was scheduled for execution
2969 */
David Howells65f27f32006-11-22 14:55:48 +00002970int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002971{
2972 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002973 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002974 return 0;
2975 }
2976
David Howells65f27f32006-11-22 14:55:48 +00002977 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002978 schedule_work(&ew->work);
2979
2980 return 1;
2981}
2982EXPORT_SYMBOL_GPL(execute_in_process_context);
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984int keventd_up(void)
2985{
Tejun Heod320c032010-06-29 10:07:14 +02002986 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002989static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002991 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002992 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2993 * Make sure that the alignment isn't lower than that of
2994 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002995 */
Tejun Heo0f900042010-06-29 10:07:11 +02002996 const size_t size = sizeof(struct cpu_workqueue_struct);
2997 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2998 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002999
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003000 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003001 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003002 else {
Tejun Heof3421792010-07-02 10:03:51 +02003003 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003004
Tejun Heof3421792010-07-02 10:03:51 +02003005 /*
3006 * Allocate enough room to align cwq and put an extra
3007 * pointer at the end pointing back to the originally
3008 * allocated pointer which will be used for free.
3009 */
3010 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3011 if (ptr) {
3012 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3013 *(void **)(wq->cpu_wq.single + 1) = ptr;
3014 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003015 }
Tejun Heof3421792010-07-02 10:03:51 +02003016
Tejun Heo0415b002011-03-24 18:50:09 +01003017 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003018 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3019 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003020}
3021
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003022static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003023{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003024 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003025 free_percpu(wq->cpu_wq.pcpu);
3026 else if (wq->cpu_wq.single) {
3027 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003028 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003029 }
3030}
3031
Tejun Heof3421792010-07-02 10:03:51 +02003032static int wq_clamp_max_active(int max_active, unsigned int flags,
3033 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003034{
Tejun Heof3421792010-07-02 10:03:51 +02003035 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3036
3037 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003038 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3039 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003040 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003041
Tejun Heof3421792010-07-02 10:03:51 +02003042 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003043}
3044
Tejun Heob196be82012-01-10 15:11:35 -08003045struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003046 unsigned int flags,
3047 int max_active,
3048 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003049 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003050{
Tejun Heob196be82012-01-10 15:11:35 -08003051 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003052 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003053 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003054 size_t namelen;
3055
3056 /* determine namelen, allocate wq and format name */
3057 va_start(args, lock_name);
3058 va_copy(args1, args);
3059 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3060
3061 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3062 if (!wq)
3063 goto err;
3064
3065 vsnprintf(wq->name, namelen, fmt, args1);
3066 va_end(args);
3067 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003068
Tejun Heof3421792010-07-02 10:03:51 +02003069 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003070 * Workqueues which may be used during memory reclaim should
3071 * have a rescuer to guarantee forward progress.
3072 */
3073 if (flags & WQ_MEM_RECLAIM)
3074 flags |= WQ_RESCUER;
3075
Tejun Heod320c032010-06-29 10:07:14 +02003076 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003077 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003078
Tejun Heob196be82012-01-10 15:11:35 -08003079 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003080 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003081 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003082 mutex_init(&wq->flush_mutex);
3083 atomic_set(&wq->nr_cwqs_to_flush, 0);
3084 INIT_LIST_HEAD(&wq->flusher_queue);
3085 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003086
Johannes Bergeb13ba82008-01-16 09:51:58 +01003087 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003088 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003089
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003090 if (alloc_cwqs(wq) < 0)
3091 goto err;
3092
Tejun Heof3421792010-07-02 10:03:51 +02003093 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003094 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003095 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003096
Tejun Heo0f900042010-06-29 10:07:11 +02003097 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32089f12012-07-12 14:46:37 -07003098 cwq->pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02003099 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003100 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003101 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003102 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003103 }
3104
Tejun Heoe22bee72010-06-29 10:07:14 +02003105 if (flags & WQ_RESCUER) {
3106 struct worker *rescuer;
3107
Tejun Heof2e005a2010-07-20 15:59:09 +02003108 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003109 goto err;
3110
3111 wq->rescuer = rescuer = alloc_worker();
3112 if (!rescuer)
3113 goto err;
3114
Tejun Heob196be82012-01-10 15:11:35 -08003115 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3116 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003117 if (IS_ERR(rescuer->task))
3118 goto err;
3119
Tejun Heoe22bee72010-06-29 10:07:14 +02003120 rescuer->task->flags |= PF_THREAD_BOUND;
3121 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003122 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003123
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003124 /*
3125 * workqueue_lock protects global freeze state and workqueues
3126 * list. Grab it, set max_active accordingly and add the new
3127 * workqueue to workqueues list.
3128 */
Tejun Heo15376632010-06-29 10:07:11 +02003129 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003130
Tejun Heo58a69cb2011-02-16 09:25:31 +01003131 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003132 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003133 get_cwq(cpu, wq)->max_active = 0;
3134
Tejun Heo15376632010-06-29 10:07:11 +02003135 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003136
Tejun Heo15376632010-06-29 10:07:11 +02003137 spin_unlock(&workqueue_lock);
3138
Oleg Nesterov3af244332007-05-09 02:34:09 -07003139 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003140err:
3141 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003142 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003143 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003144 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003145 kfree(wq);
3146 }
3147 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003148}
Tejun Heod320c032010-06-29 10:07:14 +02003149EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003150
3151/**
3152 * destroy_workqueue - safely terminate a workqueue
3153 * @wq: target workqueue
3154 *
3155 * Safely destroy a workqueue. All work currently pending will be done first.
3156 */
3157void destroy_workqueue(struct workqueue_struct *wq)
3158{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003159 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003160
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003161 /* drain it before proceeding with destruction */
3162 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003163
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003164 /*
3165 * wq list is used to freeze wq, remove from list after
3166 * flushing is complete in case freeze races us.
3167 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003168 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003169 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003170 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003171
Tejun Heoe22bee72010-06-29 10:07:14 +02003172 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003173 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003174 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3175 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003176
Tejun Heo73f53c42010-06-29 10:07:11 +02003177 for (i = 0; i < WORK_NR_COLORS; i++)
3178 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003179 BUG_ON(cwq->nr_active);
3180 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003181 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003182
Tejun Heoe22bee72010-06-29 10:07:14 +02003183 if (wq->flags & WQ_RESCUER) {
3184 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003185 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003186 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003187 }
3188
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003189 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003190 kfree(wq);
3191}
3192EXPORT_SYMBOL_GPL(destroy_workqueue);
3193
Tejun Heodcd989c2010-06-29 10:07:14 +02003194/**
3195 * workqueue_set_max_active - adjust max_active of a workqueue
3196 * @wq: target workqueue
3197 * @max_active: new max_active value.
3198 *
3199 * Set max_active of @wq to @max_active.
3200 *
3201 * CONTEXT:
3202 * Don't call from IRQ context.
3203 */
3204void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3205{
3206 unsigned int cpu;
3207
Tejun Heof3421792010-07-02 10:03:51 +02003208 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003209
3210 spin_lock(&workqueue_lock);
3211
3212 wq->saved_max_active = max_active;
3213
Tejun Heof3421792010-07-02 10:03:51 +02003214 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003215 struct global_cwq *gcwq = get_gcwq(cpu);
3216
3217 spin_lock_irq(&gcwq->lock);
3218
Tejun Heo58a69cb2011-02-16 09:25:31 +01003219 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003220 !(gcwq->flags & GCWQ_FREEZING))
3221 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3222
3223 spin_unlock_irq(&gcwq->lock);
3224 }
3225
3226 spin_unlock(&workqueue_lock);
3227}
3228EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3229
3230/**
3231 * workqueue_congested - test whether a workqueue is congested
3232 * @cpu: CPU in question
3233 * @wq: target workqueue
3234 *
3235 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3236 * no synchronization around this function and the test result is
3237 * unreliable and only useful as advisory hints or for debugging.
3238 *
3239 * RETURNS:
3240 * %true if congested, %false otherwise.
3241 */
3242bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3243{
3244 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3245
3246 return !list_empty(&cwq->delayed_works);
3247}
3248EXPORT_SYMBOL_GPL(workqueue_congested);
3249
3250/**
3251 * work_cpu - return the last known associated cpu for @work
3252 * @work: the work of interest
3253 *
3254 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003255 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003256 */
3257unsigned int work_cpu(struct work_struct *work)
3258{
3259 struct global_cwq *gcwq = get_work_gcwq(work);
3260
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003261 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003262}
3263EXPORT_SYMBOL_GPL(work_cpu);
3264
3265/**
3266 * work_busy - test whether a work is currently pending or running
3267 * @work: the work to be tested
3268 *
3269 * Test whether @work is currently pending or running. There is no
3270 * synchronization around this function and the test result is
3271 * unreliable and only useful as advisory hints or for debugging.
3272 * Especially for reentrant wqs, the pending state might hide the
3273 * running state.
3274 *
3275 * RETURNS:
3276 * OR'd bitmask of WORK_BUSY_* bits.
3277 */
3278unsigned int work_busy(struct work_struct *work)
3279{
3280 struct global_cwq *gcwq = get_work_gcwq(work);
3281 unsigned long flags;
3282 unsigned int ret = 0;
3283
3284 if (!gcwq)
3285 return false;
3286
3287 spin_lock_irqsave(&gcwq->lock, flags);
3288
3289 if (work_pending(work))
3290 ret |= WORK_BUSY_PENDING;
3291 if (find_worker_executing_work(gcwq, work))
3292 ret |= WORK_BUSY_RUNNING;
3293
3294 spin_unlock_irqrestore(&gcwq->lock, flags);
3295
3296 return ret;
3297}
3298EXPORT_SYMBOL_GPL(work_busy);
3299
Tejun Heodb7bccf2010-06-29 10:07:12 +02003300/*
3301 * CPU hotplug.
3302 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003303 * There are two challenges in supporting CPU hotplug. Firstly, there
3304 * are a lot of assumptions on strong associations among work, cwq and
3305 * gcwq which make migrating pending and scheduled works very
3306 * difficult to implement without impacting hot paths. Secondly,
3307 * gcwqs serve mix of short, long and very long running works making
3308 * blocked draining impractical.
3309 *
3310 * This is solved by allowing a gcwq to be detached from CPU, running
3311 * it with unbound (rogue) workers and allowing it to be reattached
3312 * later if the cpu comes back online. A separate thread is created
3313 * to govern a gcwq in such state and is called the trustee of the
3314 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003315 *
3316 * Trustee states and their descriptions.
3317 *
3318 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3319 * new trustee is started with this state.
3320 *
3321 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003322 * assuming the manager role and making all existing
3323 * workers rogue. DOWN_PREPARE waits for trustee to
3324 * enter this state. After reaching IN_CHARGE, trustee
3325 * tries to execute the pending worklist until it's empty
3326 * and the state is set to BUTCHER, or the state is set
3327 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003328 *
3329 * BUTCHER Command state which is set by the cpu callback after
3330 * the cpu has went down. Once this state is set trustee
3331 * knows that there will be no new works on the worklist
3332 * and once the worklist is empty it can proceed to
3333 * killing idle workers.
3334 *
3335 * RELEASE Command state which is set by the cpu callback if the
3336 * cpu down has been canceled or it has come online
3337 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003338 * trying to drain or butcher and clears ROGUE, rebinds
3339 * all remaining workers back to the cpu and releases
3340 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003341 *
3342 * DONE Trustee will enter this state after BUTCHER or RELEASE
3343 * is complete.
3344 *
3345 * trustee CPU draining
3346 * took over down complete
3347 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3348 * | | ^
3349 * | CPU is back online v return workers |
3350 * ----------------> RELEASE --------------
3351 */
3352
3353/**
3354 * trustee_wait_event_timeout - timed event wait for trustee
3355 * @cond: condition to wait for
3356 * @timeout: timeout in jiffies
3357 *
3358 * wait_event_timeout() for trustee to use. Handles locking and
3359 * checks for RELEASE request.
3360 *
3361 * CONTEXT:
3362 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3363 * multiple times. To be used by trustee.
3364 *
3365 * RETURNS:
3366 * Positive indicating left time if @cond is satisfied, 0 if timed
3367 * out, -1 if canceled.
3368 */
3369#define trustee_wait_event_timeout(cond, timeout) ({ \
3370 long __ret = (timeout); \
3371 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3372 __ret) { \
3373 spin_unlock_irq(&gcwq->lock); \
3374 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3375 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3376 __ret); \
3377 spin_lock_irq(&gcwq->lock); \
3378 } \
3379 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3380})
3381
3382/**
3383 * trustee_wait_event - event wait for trustee
3384 * @cond: condition to wait for
3385 *
3386 * wait_event() for trustee to use. Automatically handles locking and
3387 * checks for CANCEL request.
3388 *
3389 * CONTEXT:
3390 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3391 * multiple times. To be used by trustee.
3392 *
3393 * RETURNS:
3394 * 0 if @cond is satisfied, -1 if canceled.
3395 */
3396#define trustee_wait_event(cond) ({ \
3397 long __ret1; \
3398 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3399 __ret1 < 0 ? -1 : 0; \
3400})
3401
Tejun Heoe87d1492012-07-13 22:16:44 -07003402static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3403{
3404 struct worker_pool *pool;
3405
3406 for_each_worker_pool(pool, gcwq)
3407 if (pool->flags & POOL_MANAGING_WORKERS)
3408 return true;
3409 return false;
3410}
3411
3412static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3413{
3414 struct worker_pool *pool;
3415
3416 for_each_worker_pool(pool, gcwq)
3417 if (!list_empty(&pool->idle_list))
3418 return true;
3419 return false;
3420}
3421
Tejun Heodb7bccf2010-06-29 10:07:12 +02003422static int __cpuinit trustee_thread(void *__gcwq)
3423{
3424 struct global_cwq *gcwq = __gcwq;
Tejun Heoe87d1492012-07-13 22:16:44 -07003425 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003426 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003427 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003428 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003429 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003430 int i;
3431
3432 BUG_ON(gcwq->cpu != smp_processor_id());
3433
3434 spin_lock_irq(&gcwq->lock);
3435 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003436 * Claim the manager position and make all workers rogue.
3437 * Trustee must be bound to the target cpu and can't be
3438 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003439 */
3440 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe87d1492012-07-13 22:16:44 -07003441 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003442 BUG_ON(rc < 0);
3443
Tejun Heoe87d1492012-07-13 22:16:44 -07003444 for_each_worker_pool(pool, gcwq) {
3445 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003446
Tejun Heoe87d1492012-07-13 22:16:44 -07003447 list_for_each_entry(worker, &pool->idle_list, entry)
3448 worker->flags |= WORKER_ROGUE;
3449 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003450
3451 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003452 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003453
3454 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003455 * Call schedule() so that we cross rq->lock and thus can
3456 * guarantee sched callbacks see the rogue flag. This is
3457 * necessary as scheduler callbacks may be invoked from other
3458 * cpus.
3459 */
3460 spin_unlock_irq(&gcwq->lock);
3461 schedule();
3462 spin_lock_irq(&gcwq->lock);
3463
3464 /*
Tejun Heocb444762010-07-02 10:03:50 +02003465 * Sched callbacks are disabled now. Zap nr_running. After
3466 * this, nr_running stays zero and need_more_worker() and
3467 * keep_working() are always true as long as the worklist is
3468 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003469 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003470 for_each_worker_pool(pool, gcwq)
3471 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003472
3473 spin_unlock_irq(&gcwq->lock);
Tejun Heoe87d1492012-07-13 22:16:44 -07003474 for_each_worker_pool(pool, gcwq)
3475 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003476 spin_lock_irq(&gcwq->lock);
3477
3478 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003479 * We're now in charge. Notify and proceed to drain. We need
3480 * to keep the gcwq running during the whole CPU down
3481 * procedure as other cpu hotunplug callbacks may need to
3482 * flush currently running tasks.
3483 */
3484 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3485 wake_up_all(&gcwq->trustee_wait);
3486
3487 /*
3488 * The original cpu is in the process of dying and may go away
3489 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003490 * be migrated to other cpus. Try draining any left work. We
3491 * want to get it over with ASAP - spam rescuers, wake up as
3492 * many idlers as necessary and create new ones till the
3493 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003494 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003495 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003496 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003497 while (true) {
3498 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003499
Tejun Heoe87d1492012-07-13 22:16:44 -07003500 for_each_worker_pool(pool, gcwq)
3501 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003502
Tejun Heoe87d1492012-07-13 22:16:44 -07003503 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3504 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3505 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003506
Tejun Heoe87d1492012-07-13 22:16:44 -07003507 for_each_worker_pool(pool, gcwq) {
3508 int nr_works = 0;
3509
3510 list_for_each_entry(work, &pool->worklist, entry) {
3511 send_mayday(work);
3512 nr_works++;
3513 }
3514
3515 list_for_each_entry(worker, &pool->idle_list, entry) {
3516 if (!nr_works--)
3517 break;
3518 wake_up_process(worker->task);
3519 }
3520
3521 if (need_to_create_worker(pool)) {
3522 spin_unlock_irq(&gcwq->lock);
3523 worker = create_worker(pool, false);
3524 spin_lock_irq(&gcwq->lock);
3525 if (worker) {
3526 worker->flags |= WORKER_ROGUE;
3527 start_worker(worker);
3528 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003529 }
3530 }
3531
Tejun Heodb7bccf2010-06-29 10:07:12 +02003532 /* give a breather */
3533 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3534 break;
3535 }
3536
Tejun Heoe22bee72010-06-29 10:07:14 +02003537 /*
3538 * Either all works have been scheduled and cpu is down, or
3539 * cpu down has already been canceled. Wait for and butcher
3540 * all workers till we're canceled.
3541 */
3542 do {
Tejun Heoe87d1492012-07-13 22:16:44 -07003543 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3544
3545 i = 0;
3546 for_each_worker_pool(pool, gcwq) {
3547 while (!list_empty(&pool->idle_list)) {
3548 worker = list_first_entry(&pool->idle_list,
3549 struct worker, entry);
3550 destroy_worker(worker);
3551 }
3552 i |= pool->nr_workers;
3553 }
3554 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003555
3556 /*
3557 * At this point, either draining has completed and no worker
3558 * is left, or cpu down has been canceled or the cpu is being
3559 * brought back up. There shouldn't be any idle one left.
3560 * Tell the remaining busy ones to rebind once it finishes the
3561 * currently scheduled works by scheduling the rebind_work.
3562 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003563 for_each_worker_pool(pool, gcwq)
3564 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003565
3566 for_each_busy_worker(worker, i, pos, gcwq) {
3567 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003568 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003569
3570 /*
3571 * Rebind_work may race with future cpu hotplug
3572 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003573 * rebinding is scheduled. The morphing should
3574 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003575 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003576 worker_flags |= WORKER_REBIND;
3577 worker_flags &= ~WORKER_ROGUE;
3578 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003579
3580 /* queue rebind_work, wq doesn't matter, use the default one */
3581 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3582 work_data_bits(rebind_work)))
3583 continue;
3584
3585 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003586 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003587 worker->scheduled.next,
3588 work_color_to_flags(WORK_NO_COLOR));
3589 }
3590
3591 /* relinquish manager role */
Tejun Heoe87d1492012-07-13 22:16:44 -07003592 for_each_worker_pool(pool, gcwq)
3593 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003594
Tejun Heodb7bccf2010-06-29 10:07:12 +02003595 /* notify completion */
3596 gcwq->trustee = NULL;
3597 gcwq->trustee_state = TRUSTEE_DONE;
3598 wake_up_all(&gcwq->trustee_wait);
3599 spin_unlock_irq(&gcwq->lock);
3600 return 0;
3601}
3602
3603/**
3604 * wait_trustee_state - wait for trustee to enter the specified state
3605 * @gcwq: gcwq the trustee of interest belongs to
3606 * @state: target state to wait for
3607 *
3608 * Wait for the trustee to reach @state. DONE is already matched.
3609 *
3610 * CONTEXT:
3611 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3612 * multiple times. To be used by cpu_callback.
3613 */
3614static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003615__releases(&gcwq->lock)
3616__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003617{
3618 if (!(gcwq->trustee_state == state ||
3619 gcwq->trustee_state == TRUSTEE_DONE)) {
3620 spin_unlock_irq(&gcwq->lock);
3621 __wait_event(gcwq->trustee_wait,
3622 gcwq->trustee_state == state ||
3623 gcwq->trustee_state == TRUSTEE_DONE);
3624 spin_lock_irq(&gcwq->lock);
3625 }
3626}
3627
Oleg Nesterov3af244332007-05-09 02:34:09 -07003628static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3629 unsigned long action,
3630 void *hcpu)
3631{
3632 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003633 struct global_cwq *gcwq = get_gcwq(cpu);
3634 struct task_struct *new_trustee = NULL;
Tejun Heoe87d1492012-07-13 22:16:44 -07003635 struct worker *new_workers[NR_WORKER_POOLS] = { };
3636 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003637 unsigned long flags;
Tejun Heoe87d1492012-07-13 22:16:44 -07003638 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003640 action &= ~CPU_TASKS_FROZEN;
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003643 case CPU_DOWN_PREPARE:
3644 new_trustee = kthread_create(trustee_thread, gcwq,
3645 "workqueue_trustee/%d\n", cpu);
3646 if (IS_ERR(new_trustee))
3647 return notifier_from_errno(PTR_ERR(new_trustee));
3648 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003649 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 case CPU_UP_PREPARE:
Tejun Heoe87d1492012-07-13 22:16:44 -07003651 i = 0;
3652 for_each_worker_pool(pool, gcwq) {
3653 BUG_ON(pool->first_idle);
3654 new_workers[i] = create_worker(pool, false);
3655 if (!new_workers[i++])
3656 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
Tejun Heodb7bccf2010-06-29 10:07:12 +02003660 /* some are called w/ irq disabled, don't disturb irq status */
3661 spin_lock_irqsave(&gcwq->lock, flags);
3662
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003663 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003664 case CPU_DOWN_PREPARE:
3665 /* initialize trustee and tell it to acquire the gcwq */
3666 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3667 gcwq->trustee = new_trustee;
3668 gcwq->trustee_state = TRUSTEE_START;
3669 wake_up_process(gcwq->trustee);
3670 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003671 /* fall through */
3672 case CPU_UP_PREPARE:
Tejun Heoe87d1492012-07-13 22:16:44 -07003673 i = 0;
3674 for_each_worker_pool(pool, gcwq) {
3675 BUG_ON(pool->first_idle);
3676 pool->first_idle = new_workers[i++];
3677 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003678 break;
3679
3680 case CPU_DYING:
3681 /*
3682 * Before this, the trustee and all workers except for
3683 * the ones which are still executing works from
3684 * before the last CPU down must be on the cpu. After
3685 * this, they'll all be diasporas.
3686 */
3687 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003688 break;
3689
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003690 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003691 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003692 /* fall through */
3693 case CPU_UP_CANCELED:
Tejun Heoe87d1492012-07-13 22:16:44 -07003694 for_each_worker_pool(pool, gcwq) {
3695 destroy_worker(pool->first_idle);
3696 pool->first_idle = NULL;
3697 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003698 break;
3699
3700 case CPU_DOWN_FAILED:
3701 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003702 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003703 if (gcwq->trustee_state != TRUSTEE_DONE) {
3704 gcwq->trustee_state = TRUSTEE_RELEASE;
3705 wake_up_process(gcwq->trustee);
3706 wait_trustee_state(gcwq, TRUSTEE_DONE);
3707 }
3708
Tejun Heoe22bee72010-06-29 10:07:14 +02003709 /*
3710 * Trustee is done and there might be no worker left.
3711 * Put the first_idle in and request a real manager to
3712 * take a look.
3713 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003714 for_each_worker_pool(pool, gcwq) {
3715 spin_unlock_irq(&gcwq->lock);
3716 kthread_bind(pool->first_idle->task, cpu);
3717 spin_lock_irq(&gcwq->lock);
3718 pool->flags |= POOL_MANAGE_WORKERS;
3719 start_worker(pool->first_idle);
3720 pool->first_idle = NULL;
3721 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003722 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003723 }
3724
Tejun Heodb7bccf2010-06-29 10:07:12 +02003725 spin_unlock_irqrestore(&gcwq->lock, flags);
3726
Tejun Heo15376632010-06-29 10:07:11 +02003727 return notifier_from_errno(0);
Tejun Heoe87d1492012-07-13 22:16:44 -07003728
3729err_destroy:
3730 if (new_trustee)
3731 kthread_stop(new_trustee);
3732
3733 spin_lock_irqsave(&gcwq->lock, flags);
3734 for (i = 0; i < NR_WORKER_POOLS; i++)
3735 if (new_workers[i])
3736 destroy_worker(new_workers[i]);
3737 spin_unlock_irqrestore(&gcwq->lock, flags);
3738
3739 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Tejun Heod3b42542012-07-17 12:39:26 -07003742/*
3743 * Workqueues should be brought up before normal priority CPU notifiers.
3744 * This will be registered high priority CPU notifier.
3745 */
3746static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3747 unsigned long action,
3748 void *hcpu)
3749{
3750 switch (action & ~CPU_TASKS_FROZEN) {
3751 case CPU_UP_PREPARE:
3752 case CPU_UP_CANCELED:
3753 case CPU_DOWN_FAILED:
3754 case CPU_ONLINE:
3755 return workqueue_cpu_callback(nfb, action, hcpu);
3756 }
3757 return NOTIFY_OK;
3758}
3759
3760/*
3761 * Workqueues should be brought down after normal priority CPU notifiers.
3762 * This will be registered as low priority CPU notifier.
3763 */
3764static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3765 unsigned long action,
3766 void *hcpu)
3767{
3768 switch (action & ~CPU_TASKS_FROZEN) {
3769 case CPU_DOWN_PREPARE:
3770 case CPU_DYING:
3771 case CPU_POST_DEAD:
3772 return workqueue_cpu_callback(nfb, action, hcpu);
3773 }
3774 return NOTIFY_OK;
3775}
3776
Rusty Russell2d3854a2008-11-05 13:39:10 +11003777#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003778
Rusty Russell2d3854a2008-11-05 13:39:10 +11003779struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003780 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003781 long (*fn)(void *);
3782 void *arg;
3783 long ret;
3784};
3785
Tejun Heofc7da7e2012-09-18 12:48:43 -07003786static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003787{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003788 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3789
Rusty Russell2d3854a2008-11-05 13:39:10 +11003790 wfc->ret = wfc->fn(wfc->arg);
3791}
3792
3793/**
3794 * work_on_cpu - run a function in user context on a particular cpu
3795 * @cpu: the cpu to run on
3796 * @fn: the function to run
3797 * @arg: the function arg
3798 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003799 * This will return the value @fn returns.
3800 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003801 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003802 */
3803long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3804{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003805 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003806
Tejun Heofc7da7e2012-09-18 12:48:43 -07003807 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3808 schedule_work_on(cpu, &wfc.work);
3809 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003810 return wfc.ret;
3811}
3812EXPORT_SYMBOL_GPL(work_on_cpu);
3813#endif /* CONFIG_SMP */
3814
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003815#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303816
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003817/**
3818 * freeze_workqueues_begin - begin freezing workqueues
3819 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003820 * Start freezing workqueues. After this function returns, all freezable
3821 * workqueues will queue new works to their frozen_works list instead of
3822 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003823 *
3824 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003825 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003826 */
3827void freeze_workqueues_begin(void)
3828{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003829 unsigned int cpu;
3830
3831 spin_lock(&workqueue_lock);
3832
3833 BUG_ON(workqueue_freezing);
3834 workqueue_freezing = true;
3835
Tejun Heof3421792010-07-02 10:03:51 +02003836 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003837 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003838 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003839
3840 spin_lock_irq(&gcwq->lock);
3841
Tejun Heodb7bccf2010-06-29 10:07:12 +02003842 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3843 gcwq->flags |= GCWQ_FREEZING;
3844
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003845 list_for_each_entry(wq, &workqueues, list) {
3846 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3847
Tejun Heo58a69cb2011-02-16 09:25:31 +01003848 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003849 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003850 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003851
3852 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003853 }
3854
3855 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003857
3858/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003859 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003860 *
3861 * Check whether freezing is complete. This function must be called
3862 * between freeze_workqueues_begin() and thaw_workqueues().
3863 *
3864 * CONTEXT:
3865 * Grabs and releases workqueue_lock.
3866 *
3867 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003868 * %true if some freezable workqueues are still busy. %false if freezing
3869 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003870 */
3871bool freeze_workqueues_busy(void)
3872{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003873 unsigned int cpu;
3874 bool busy = false;
3875
3876 spin_lock(&workqueue_lock);
3877
3878 BUG_ON(!workqueue_freezing);
3879
Tejun Heof3421792010-07-02 10:03:51 +02003880 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003881 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003882 /*
3883 * nr_active is monotonically decreasing. It's safe
3884 * to peek without lock.
3885 */
3886 list_for_each_entry(wq, &workqueues, list) {
3887 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3888
Tejun Heo58a69cb2011-02-16 09:25:31 +01003889 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003890 continue;
3891
3892 BUG_ON(cwq->nr_active < 0);
3893 if (cwq->nr_active) {
3894 busy = true;
3895 goto out_unlock;
3896 }
3897 }
3898 }
3899out_unlock:
3900 spin_unlock(&workqueue_lock);
3901 return busy;
3902}
3903
3904/**
3905 * thaw_workqueues - thaw workqueues
3906 *
3907 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003908 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003909 *
3910 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003911 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003912 */
3913void thaw_workqueues(void)
3914{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003915 unsigned int cpu;
3916
3917 spin_lock(&workqueue_lock);
3918
3919 if (!workqueue_freezing)
3920 goto out_unlock;
3921
Tejun Heof3421792010-07-02 10:03:51 +02003922 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003923 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoe87d1492012-07-13 22:16:44 -07003924 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003925 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003926
3927 spin_lock_irq(&gcwq->lock);
3928
Tejun Heodb7bccf2010-06-29 10:07:12 +02003929 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3930 gcwq->flags &= ~GCWQ_FREEZING;
3931
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003932 list_for_each_entry(wq, &workqueues, list) {
3933 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3934
Tejun Heo58a69cb2011-02-16 09:25:31 +01003935 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003936 continue;
3937
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003938 /* restore max_active and repopulate worklist */
3939 cwq->max_active = wq->saved_max_active;
3940
3941 while (!list_empty(&cwq->delayed_works) &&
3942 cwq->nr_active < cwq->max_active)
3943 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003944 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003945
Tejun Heoe87d1492012-07-13 22:16:44 -07003946 for_each_worker_pool(pool, gcwq)
3947 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003948
Tejun Heo8b03ae32010-06-29 10:07:12 +02003949 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003950 }
3951
3952 workqueue_freezing = false;
3953out_unlock:
3954 spin_unlock(&workqueue_lock);
3955}
3956#endif /* CONFIG_FREEZER */
3957
Suresh Siddha6ee05782010-07-30 14:57:37 -07003958static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
Tejun Heoc34056a2010-06-29 10:07:11 +02003960 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003961 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003962
Tejun Heod3b42542012-07-17 12:39:26 -07003963 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3964 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003965
3966 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003967 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003968 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoe87d1492012-07-13 22:16:44 -07003969 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003970
3971 spin_lock_init(&gcwq->lock);
3972 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003973 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003974
Tejun Heoc8e55f32010-06-29 10:07:12 +02003975 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3976 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3977
Tejun Heoe87d1492012-07-13 22:16:44 -07003978 for_each_worker_pool(pool, gcwq) {
3979 pool->gcwq = gcwq;
3980 INIT_LIST_HEAD(&pool->worklist);
3981 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003982
Tejun Heoe87d1492012-07-13 22:16:44 -07003983 init_timer_deferrable(&pool->idle_timer);
3984 pool->idle_timer.function = idle_worker_timeout;
3985 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003986
Tejun Heoe87d1492012-07-13 22:16:44 -07003987 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3988 (unsigned long)pool);
3989
3990 ida_init(&pool->worker_ida);
3991 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003992
3993 gcwq->trustee_state = TRUSTEE_DONE;
3994 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003995 }
3996
Tejun Heoe22bee72010-06-29 10:07:14 +02003997 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003998 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003999 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoe87d1492012-07-13 22:16:44 -07004000 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004001
Tejun Heo477a3c32010-08-31 10:54:35 +02004002 if (cpu != WORK_CPU_UNBOUND)
4003 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe87d1492012-07-13 22:16:44 -07004004
4005 for_each_worker_pool(pool, gcwq) {
4006 struct worker *worker;
4007
4008 worker = create_worker(pool, true);
4009 BUG_ON(!worker);
4010 spin_lock_irq(&gcwq->lock);
4011 start_worker(worker);
4012 spin_unlock_irq(&gcwq->lock);
4013 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004014 }
4015
Tejun Heod320c032010-06-29 10:07:14 +02004016 system_wq = alloc_workqueue("events", 0, 0);
4017 system_long_wq = alloc_workqueue("events_long", 0, 0);
4018 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004019 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4020 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004021 system_freezable_wq = alloc_workqueue("events_freezable",
4022 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01004023 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
4024 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01004025 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01004026 !system_unbound_wq || !system_freezable_wq ||
4027 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004030early_initcall(init_workqueues);