blob: 930c2390b77e7873126e7a242845149f574eb8ef [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
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020048 /* global_cwq flags */
Tejun Heoe22bee72010-06-29 10:07:14 +020049 GCWQ_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
50 GCWQ_MANAGING_WORKERS = 1 << 1, /* managing workers */
51 GCWQ_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020052 GCWQ_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heo649027d2010-06-29 10:07:14 +020053 GCWQ_HIGHPRI_PENDING = 1 << 4, /* highpri works on queue */
Tejun Heodb7bccf2010-06-29 10:07:12 +020054
Tejun Heoc8e55f32010-06-29 10:07:12 +020055 /* worker flags */
56 WORKER_STARTED = 1 << 0, /* started */
57 WORKER_DIE = 1 << 1, /* die die die */
58 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020059 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020060 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020062 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020063 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020064
Tejun Heofb0e7be2010-06-29 10:07:15 +020065 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020066 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020067
68 /* gcwq->trustee_state */
69 TRUSTEE_START = 0, /* start */
70 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
71 TRUSTEE_BUTCHER = 2, /* butcher workers */
72 TRUSTEE_RELEASE = 3, /* release workers */
73 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020074
75 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
76 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
77 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020078
Tejun Heoe22bee72010-06-29 10:07:14 +020079 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
80 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
81
82 MAYDAY_INITIAL_TIMEOUT = HZ / 100, /* call for help after 10ms */
83 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
84 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020085 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020086
87 /*
88 * Rescue workers are used only on emergencies and shared by
89 * all cpus. Give -20.
90 */
91 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020092};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020095 * Structure fields follow one of the following exclusion rules.
96 *
Tejun Heoe41e7042010-08-24 14:22:47 +020097 * I: Modifiable by initialization/destruction paths and read-only for
98 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +020099 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200100 * P: Preemption protected. Disabling preemption is enough and should
101 * only be modified and accessed from the local cpu.
102 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200103 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200104 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200105 * X: During normal operation, modification requires gcwq->lock and
106 * should be done only from local cpu. Either disabling preemption
107 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200108 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200109 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200110 * F: wq->flush_mutex protected.
111 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 * W: workqueue_lock protected.
113 */
114
Tejun Heo8b03ae32010-06-29 10:07:12 +0200115struct global_cwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200116
Tejun Heoe22bee72010-06-29 10:07:14 +0200117/*
118 * The poor guys doing the actual heavy lifting. All on-duty workers
119 * are either serving the manager role, on idle list or on busy hash.
120 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200121struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200122 /* on idle list while idle, on busy hash table while busy */
123 union {
124 struct list_head entry; /* L: while idle */
125 struct hlist_node hentry; /* L: while busy */
126 };
127
Tejun Heoc34056a2010-06-29 10:07:11 +0200128 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200129 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200130 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200131 struct task_struct *task; /* I: worker task */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200132 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heoe22bee72010-06-29 10:07:14 +0200133 /* 64 bytes boundary on 64bit, 32 on 32bit */
134 unsigned long last_active; /* L: last active timestamp */
135 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200136 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200137 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138};
139
Tejun Heo4690c4a2010-06-29 10:07:10 +0200140/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200141 * Global per-cpu workqueue. There's one and only one for each cpu
142 * and all works are queued and processed here regardless of their
143 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200144 */
145struct global_cwq {
146 spinlock_t lock; /* the gcwq lock */
Tejun Heo7e116292010-06-29 10:07:13 +0200147 struct list_head worklist; /* L: list of pending works */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200148 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200149 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200150
151 int nr_workers; /* L: total number of workers */
152 int nr_idle; /* L: currently idle ones */
153
154 /* workers are chained either in the idle_list or busy_hash */
Tejun Heoe22bee72010-06-29 10:07:14 +0200155 struct list_head idle_list; /* X: list of idle workers */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200156 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
157 /* L: hash of busy workers */
158
Tejun Heoe22bee72010-06-29 10:07:14 +0200159 struct timer_list idle_timer; /* L: worker idle timeout */
160 struct timer_list mayday_timer; /* L: SOS timer for dworkers */
161
Tejun Heo8b03ae32010-06-29 10:07:12 +0200162 struct ida worker_ida; /* L: for worker IDs */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200163
164 struct task_struct *trustee; /* L: for gcwq shutdown */
165 unsigned int trustee_state; /* L: trustee state */
166 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heoe22bee72010-06-29 10:07:14 +0200167 struct worker *first_idle; /* L: first idle worker */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200168} ____cacheline_aligned_in_smp;
169
170/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200171 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200172 * work_struct->data are used for flags and thus cwqs need to be
173 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175struct cpu_workqueue_struct {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200176 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200177 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200178 int work_color; /* L: current color */
179 int flush_color; /* L: flushing color */
180 int nr_in_flight[WORK_NR_COLORS];
181 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200182 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200183 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200184 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200185};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200188 * Structure used to wait for workqueue flush.
189 */
190struct wq_flusher {
191 struct list_head list; /* F: list of flushers */
192 int flush_color; /* F: flush color waiting for */
193 struct completion done; /* flush completion */
194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Tejun Heo73f53c42010-06-29 10:07:11 +0200196/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200197 * All cpumasks are assumed to be always set on UP and thus can't be
198 * used to determine whether there's something to be done.
199 */
200#ifdef CONFIG_SMP
201typedef cpumask_var_t mayday_mask_t;
202#define mayday_test_and_set_cpu(cpu, mask) \
203 cpumask_test_and_set_cpu((cpu), (mask))
204#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
205#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200206#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200207#define free_mayday_mask(mask) free_cpumask_var((mask))
208#else
209typedef unsigned long mayday_mask_t;
210#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
211#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
212#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
213#define alloc_mayday_mask(maskp, gfp) true
214#define free_mayday_mask(mask) do { } while (0)
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/*
218 * The externally visible workqueue abstraction is an array of
219 * per-CPU workqueues:
220 */
221struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +0200222 unsigned int flags; /* I: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200223 union {
224 struct cpu_workqueue_struct __percpu *pcpu;
225 struct cpu_workqueue_struct *single;
226 unsigned long v;
227 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200228 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200229
230 struct mutex flush_mutex; /* protects wq flushing */
231 int work_color; /* F: current work color */
232 int flush_color; /* F: current flush color */
233 atomic_t nr_cwqs_to_flush; /* flush in progress */
234 struct wq_flusher *first_flusher; /* F: first flusher */
235 struct list_head flusher_queue; /* F: flush waiters */
236 struct list_head flusher_overflow; /* F: flush overflow list */
237
Tejun Heof2e005a2010-07-20 15:59:09 +0200238 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200239 struct worker *rescuer; /* I: rescue worker */
240
Tejun Heodcd989c2010-06-29 10:07:14 +0200241 int saved_max_active; /* W: saved cwq max_active */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200242 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700243#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700245#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Tejun Heod320c032010-06-29 10:07:14 +0200248struct workqueue_struct *system_wq __read_mostly;
249struct workqueue_struct *system_long_wq __read_mostly;
250struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200251struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200252EXPORT_SYMBOL_GPL(system_wq);
253EXPORT_SYMBOL_GPL(system_long_wq);
254EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200255EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200256
Tejun Heo97bd2342010-10-05 10:41:14 +0200257#define CREATE_TRACE_POINTS
258#include <trace/events/workqueue.h>
259
Tejun Heodb7bccf2010-06-29 10:07:12 +0200260#define for_each_busy_worker(worker, i, pos, gcwq) \
261 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
262 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
263
Tejun Heof3421792010-07-02 10:03:51 +0200264static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
265 unsigned int sw)
266{
267 if (cpu < nr_cpu_ids) {
268 if (sw & 1) {
269 cpu = cpumask_next(cpu, mask);
270 if (cpu < nr_cpu_ids)
271 return cpu;
272 }
273 if (sw & 2)
274 return WORK_CPU_UNBOUND;
275 }
276 return WORK_CPU_NONE;
277}
278
279static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
280 struct workqueue_struct *wq)
281{
282 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
283}
284
Tejun Heo09884952010-08-01 11:50:12 +0200285/*
286 * CPU iterators
287 *
288 * An extra gcwq is defined for an invalid cpu number
289 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
290 * specific CPU. The following iterators are similar to
291 * for_each_*_cpu() iterators but also considers the unbound gcwq.
292 *
293 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
294 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
295 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
296 * WORK_CPU_UNBOUND for unbound workqueues
297 */
Tejun Heof3421792010-07-02 10:03:51 +0200298#define for_each_gcwq_cpu(cpu) \
299 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
300 (cpu) < WORK_CPU_NONE; \
301 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
302
303#define for_each_online_gcwq_cpu(cpu) \
304 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
305 (cpu) < WORK_CPU_NONE; \
306 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
307
308#define for_each_cwq_cpu(cpu, wq) \
309 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
310 (cpu) < WORK_CPU_NONE; \
311 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
312
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900313#ifdef CONFIG_DEBUG_OBJECTS_WORK
314
315static struct debug_obj_descr work_debug_descr;
316
317/*
318 * fixup_init is called when:
319 * - an active object is initialized
320 */
321static int work_fixup_init(void *addr, enum debug_obj_state state)
322{
323 struct work_struct *work = addr;
324
325 switch (state) {
326 case ODEBUG_STATE_ACTIVE:
327 cancel_work_sync(work);
328 debug_object_init(work, &work_debug_descr);
329 return 1;
330 default:
331 return 0;
332 }
333}
334
335/*
336 * fixup_activate is called when:
337 * - an active object is activated
338 * - an unknown object is activated (might be a statically initialized object)
339 */
340static int work_fixup_activate(void *addr, enum debug_obj_state state)
341{
342 struct work_struct *work = addr;
343
344 switch (state) {
345
346 case ODEBUG_STATE_NOTAVAILABLE:
347 /*
348 * This is not really a fixup. The work struct was
349 * statically initialized. We just make sure that it
350 * is tracked in the object tracker.
351 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200352 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900353 debug_object_init(work, &work_debug_descr);
354 debug_object_activate(work, &work_debug_descr);
355 return 0;
356 }
357 WARN_ON_ONCE(1);
358 return 0;
359
360 case ODEBUG_STATE_ACTIVE:
361 WARN_ON(1);
362
363 default:
364 return 0;
365 }
366}
367
368/*
369 * fixup_free is called when:
370 * - an active object is freed
371 */
372static int work_fixup_free(void *addr, enum debug_obj_state state)
373{
374 struct work_struct *work = addr;
375
376 switch (state) {
377 case ODEBUG_STATE_ACTIVE:
378 cancel_work_sync(work);
379 debug_object_free(work, &work_debug_descr);
380 return 1;
381 default:
382 return 0;
383 }
384}
385
386static struct debug_obj_descr work_debug_descr = {
387 .name = "work_struct",
388 .fixup_init = work_fixup_init,
389 .fixup_activate = work_fixup_activate,
390 .fixup_free = work_fixup_free,
391};
392
393static inline void debug_work_activate(struct work_struct *work)
394{
395 debug_object_activate(work, &work_debug_descr);
396}
397
398static inline void debug_work_deactivate(struct work_struct *work)
399{
400 debug_object_deactivate(work, &work_debug_descr);
401}
402
403void __init_work(struct work_struct *work, int onstack)
404{
405 if (onstack)
406 debug_object_init_on_stack(work, &work_debug_descr);
407 else
408 debug_object_init(work, &work_debug_descr);
409}
410EXPORT_SYMBOL_GPL(__init_work);
411
412void destroy_work_on_stack(struct work_struct *work)
413{
414 debug_object_free(work, &work_debug_descr);
415}
416EXPORT_SYMBOL_GPL(destroy_work_on_stack);
417
418#else
419static inline void debug_work_activate(struct work_struct *work) { }
420static inline void debug_work_deactivate(struct work_struct *work) { }
421#endif
422
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100423/* Serializes the accesses to the list of workqueues. */
424static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200426static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Oleg Nesterov14441962007-05-23 13:57:57 -0700428/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200429 * The almighty global cpu workqueues. nr_running is the only field
430 * which is expected to be used frequently by other cpus via
431 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700432 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200433static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200434static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800435
Tejun Heof3421792010-07-02 10:03:51 +0200436/*
437 * Global cpu workqueue and nr_running counter for unbound gcwq. The
438 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
439 * workers have WORKER_UNBOUND set.
440 */
441static struct global_cwq unbound_global_cwq;
442static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
443
Tejun Heoc34056a2010-06-29 10:07:11 +0200444static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Tejun Heo8b03ae32010-06-29 10:07:12 +0200446static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Tejun Heof3421792010-07-02 10:03:51 +0200448 if (cpu != WORK_CPU_UNBOUND)
449 return &per_cpu(global_cwq, cpu);
450 else
451 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Tejun Heoe22bee72010-06-29 10:07:14 +0200454static atomic_t *get_gcwq_nr_running(unsigned int cpu)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700455{
Tejun Heof3421792010-07-02 10:03:51 +0200456 if (cpu != WORK_CPU_UNBOUND)
457 return &per_cpu(gcwq_nr_running, cpu);
458 else
459 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700460}
461
Tejun Heo4690c4a2010-06-29 10:07:10 +0200462static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
463 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700464{
Tejun Heof3421792010-07-02 10:03:51 +0200465 if (!(wq->flags & WQ_UNBOUND)) {
466 if (likely(cpu < nr_cpu_ids)) {
467#ifdef CONFIG_SMP
468 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200469#else
Tejun Heof3421792010-07-02 10:03:51 +0200470 return wq->cpu_wq.single;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200471#endif
Tejun Heof3421792010-07-02 10:03:51 +0200472 }
473 } else if (likely(cpu == WORK_CPU_UNBOUND))
474 return wq->cpu_wq.single;
475 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700476}
477
Tejun Heo73f53c42010-06-29 10:07:11 +0200478static unsigned int work_color_to_flags(int color)
479{
480 return color << WORK_STRUCT_COLOR_SHIFT;
481}
482
483static int get_work_color(struct work_struct *work)
484{
485 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
486 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
487}
488
489static int work_next_color(int color)
490{
491 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
David Howells4594bf12006-12-07 11:33:26 +0000494/*
Tejun Heoe1201532010-07-22 14:14:25 +0200495 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
496 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
497 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200498 *
499 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
500 * cwq, cpu or clear work->data. These functions should only be
501 * called while the work is owned - ie. while the PENDING bit is set.
502 *
503 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
504 * corresponding to a work. gcwq is available once the work has been
505 * queued anywhere after initialization. cwq is available only from
506 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000507 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200508static inline void set_work_data(struct work_struct *work, unsigned long data,
509 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000510{
David Howells4594bf12006-12-07 11:33:26 +0000511 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200512 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000513}
David Howells365970a2006-11-22 14:54:49 +0000514
Tejun Heo7a22ad72010-06-29 10:07:13 +0200515static void set_work_cwq(struct work_struct *work,
516 struct cpu_workqueue_struct *cwq,
517 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200518{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200519 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200520 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200521}
522
Tejun Heo7a22ad72010-06-29 10:07:13 +0200523static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000524{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200525 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
526}
527
528static void clear_work_data(struct work_struct *work)
529{
530 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
531}
532
Tejun Heo7a22ad72010-06-29 10:07:13 +0200533static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
534{
Tejun Heoe1201532010-07-22 14:14:25 +0200535 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200536
Tejun Heoe1201532010-07-22 14:14:25 +0200537 if (data & WORK_STRUCT_CWQ)
538 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
539 else
540 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200541}
542
543static struct global_cwq *get_work_gcwq(struct work_struct *work)
544{
Tejun Heoe1201532010-07-22 14:14:25 +0200545 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200546 unsigned int cpu;
547
Tejun Heoe1201532010-07-22 14:14:25 +0200548 if (data & WORK_STRUCT_CWQ)
549 return ((struct cpu_workqueue_struct *)
550 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551
552 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200553 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554 return NULL;
555
Tejun Heof3421792010-07-02 10:03:51 +0200556 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000558}
559
560/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200561 * Policy functions. These define the policies on how the global
562 * worker pool is managed. Unless noted otherwise, these functions
563 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000564 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200565
Tejun Heo649027d2010-06-29 10:07:14 +0200566static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000567{
Tejun Heo649027d2010-06-29 10:07:14 +0200568 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
569 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000570}
571
Tejun Heoe22bee72010-06-29 10:07:14 +0200572/*
573 * Need to wake up a worker? Called from anything but currently
574 * running workers.
575 */
576static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000577{
Tejun Heo649027d2010-06-29 10:07:14 +0200578 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000579}
580
Tejun Heoe22bee72010-06-29 10:07:14 +0200581/* Can I start working? Called from busy but !running workers. */
582static bool may_start_working(struct global_cwq *gcwq)
583{
584 return gcwq->nr_idle;
585}
586
587/* Do I need to keep working? Called from currently running workers. */
588static bool keep_working(struct global_cwq *gcwq)
589{
590 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
591
Tejun Heo30310042010-10-11 11:51:57 +0200592 return !list_empty(&gcwq->worklist) &&
593 (atomic_read(nr_running) <= 1 ||
594 gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200595}
596
597/* Do we need a new worker? Called from manager. */
598static bool need_to_create_worker(struct global_cwq *gcwq)
599{
600 return need_more_worker(gcwq) && !may_start_working(gcwq);
601}
602
603/* Do I need to be the manager? */
604static bool need_to_manage_workers(struct global_cwq *gcwq)
605{
606 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
607}
608
609/* Do we have too many workers and should some go away? */
610static bool too_many_workers(struct global_cwq *gcwq)
611{
612 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
613 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
614 int nr_busy = gcwq->nr_workers - nr_idle;
615
616 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
617}
618
619/*
620 * Wake up functions.
621 */
622
Tejun Heo7e116292010-06-29 10:07:13 +0200623/* Return the first worker. Safe with preemption disabled */
624static struct worker *first_worker(struct global_cwq *gcwq)
625{
626 if (unlikely(list_empty(&gcwq->idle_list)))
627 return NULL;
628
629 return list_first_entry(&gcwq->idle_list, struct worker, entry);
630}
631
632/**
633 * wake_up_worker - wake up an idle worker
634 * @gcwq: gcwq to wake worker for
635 *
636 * Wake up the first idle worker of @gcwq.
637 *
638 * CONTEXT:
639 * spin_lock_irq(gcwq->lock).
640 */
641static void wake_up_worker(struct global_cwq *gcwq)
642{
643 struct worker *worker = first_worker(gcwq);
644
645 if (likely(worker))
646 wake_up_process(worker->task);
647}
648
Tejun Heo4690c4a2010-06-29 10:07:10 +0200649/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200650 * wq_worker_waking_up - a worker is waking up
651 * @task: task waking up
652 * @cpu: CPU @task is waking up to
653 *
654 * This function is called during try_to_wake_up() when a worker is
655 * being awoken.
656 *
657 * CONTEXT:
658 * spin_lock_irq(rq->lock)
659 */
660void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
661{
662 struct worker *worker = kthread_data(task);
663
Steven Rostedt2d646722010-12-03 23:12:33 -0500664 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe22bee72010-06-29 10:07:14 +0200665 atomic_inc(get_gcwq_nr_running(cpu));
666}
667
668/**
669 * wq_worker_sleeping - a worker is going to sleep
670 * @task: task going to sleep
671 * @cpu: CPU in question, must be the current CPU number
672 *
673 * This function is called during schedule() when a busy worker is
674 * going to sleep. Worker on the same cpu can be woken up by
675 * returning pointer to its task.
676 *
677 * CONTEXT:
678 * spin_lock_irq(rq->lock)
679 *
680 * RETURNS:
681 * Worker task on @cpu to wake up, %NULL if none.
682 */
683struct task_struct *wq_worker_sleeping(struct task_struct *task,
684 unsigned int cpu)
685{
686 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
687 struct global_cwq *gcwq = get_gcwq(cpu);
688 atomic_t *nr_running = get_gcwq_nr_running(cpu);
689
Steven Rostedt2d646722010-12-03 23:12:33 -0500690 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200691 return NULL;
692
693 /* this can only happen on the local cpu */
694 BUG_ON(cpu != raw_smp_processor_id());
695
696 /*
697 * The counterpart of the following dec_and_test, implied mb,
698 * worklist not empty test sequence is in insert_work().
699 * Please read comment there.
700 *
701 * NOT_RUNNING is clear. This means that trustee is not in
702 * charge and we're running on the local cpu w/ rq lock held
703 * and preemption disabled, which in turn means that none else
704 * could be manipulating idle_list, so dereferencing idle_list
705 * without gcwq lock is safe.
706 */
707 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
708 to_wakeup = first_worker(gcwq);
709 return to_wakeup ? to_wakeup->task : NULL;
710}
711
712/**
713 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200714 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200715 * @flags: flags to set
716 * @wakeup: wakeup an idle worker if necessary
717 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200718 * Set @flags in @worker->flags and adjust nr_running accordingly. If
719 * nr_running becomes zero and @wakeup is %true, an idle worker is
720 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200721 *
Tejun Heocb444762010-07-02 10:03:50 +0200722 * CONTEXT:
723 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200724 */
725static inline void worker_set_flags(struct worker *worker, unsigned int flags,
726 bool wakeup)
727{
Tejun Heoe22bee72010-06-29 10:07:14 +0200728 struct global_cwq *gcwq = worker->gcwq;
729
Tejun Heocb444762010-07-02 10:03:50 +0200730 WARN_ON_ONCE(worker->task != current);
731
Tejun Heoe22bee72010-06-29 10:07:14 +0200732 /*
733 * If transitioning into NOT_RUNNING, adjust nr_running and
734 * wake up an idle worker as necessary if requested by
735 * @wakeup.
736 */
737 if ((flags & WORKER_NOT_RUNNING) &&
738 !(worker->flags & WORKER_NOT_RUNNING)) {
739 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
740
741 if (wakeup) {
742 if (atomic_dec_and_test(nr_running) &&
743 !list_empty(&gcwq->worklist))
744 wake_up_worker(gcwq);
745 } else
746 atomic_dec(nr_running);
747 }
748
Tejun Heod302f012010-06-29 10:07:13 +0200749 worker->flags |= flags;
750}
751
752/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200753 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200754 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200755 * @flags: flags to clear
756 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200757 * Clear @flags in @worker->flags and adjust nr_running accordingly.
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_clr_flags(struct worker *worker, unsigned int flags)
763{
Tejun Heoe22bee72010-06-29 10:07:14 +0200764 struct global_cwq *gcwq = worker->gcwq;
765 unsigned int oflags = worker->flags;
766
Tejun Heocb444762010-07-02 10:03:50 +0200767 WARN_ON_ONCE(worker->task != current);
768
Tejun Heod302f012010-06-29 10:07:13 +0200769 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200770
771 /* if transitioning out of NOT_RUNNING, increment nr_running */
772 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
773 if (!(worker->flags & WORKER_NOT_RUNNING))
774 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200775}
776
777/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200778 * busy_worker_head - return the busy hash head for a work
779 * @gcwq: gcwq of interest
780 * @work: work to be hashed
781 *
782 * Return hash head of @gcwq for @work.
783 *
784 * CONTEXT:
785 * spin_lock_irq(gcwq->lock).
786 *
787 * RETURNS:
788 * Pointer to the hash head.
789 */
790static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
791 struct work_struct *work)
792{
793 const int base_shift = ilog2(sizeof(struct work_struct));
794 unsigned long v = (unsigned long)work;
795
796 /* simple shift and fold hash, do we need something better? */
797 v >>= base_shift;
798 v += v >> BUSY_WORKER_HASH_ORDER;
799 v &= BUSY_WORKER_HASH_MASK;
800
801 return &gcwq->busy_hash[v];
802}
803
804/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200805 * __find_worker_executing_work - find worker which is executing a work
806 * @gcwq: gcwq of interest
807 * @bwh: hash head as returned by busy_worker_head()
808 * @work: work to find worker for
809 *
810 * Find a worker which is executing @work on @gcwq. @bwh should be
811 * the hash head obtained by calling busy_worker_head() with the same
812 * work.
813 *
814 * CONTEXT:
815 * spin_lock_irq(gcwq->lock).
816 *
817 * RETURNS:
818 * Pointer to worker which is executing @work if found, NULL
819 * otherwise.
820 */
821static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
822 struct hlist_head *bwh,
823 struct work_struct *work)
824{
825 struct worker *worker;
826 struct hlist_node *tmp;
827
828 hlist_for_each_entry(worker, tmp, bwh, hentry)
829 if (worker->current_work == work)
830 return worker;
831 return NULL;
832}
833
834/**
835 * find_worker_executing_work - find worker which is executing a work
836 * @gcwq: gcwq of interest
837 * @work: work to find worker for
838 *
839 * Find a worker which is executing @work on @gcwq. This function is
840 * identical to __find_worker_executing_work() except that this
841 * function calculates @bwh itself.
842 *
843 * CONTEXT:
844 * spin_lock_irq(gcwq->lock).
845 *
846 * RETURNS:
847 * Pointer to worker which is executing @work if found, NULL
848 * otherwise.
849 */
850static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
851 struct work_struct *work)
852{
853 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
854 work);
855}
856
857/**
Tejun Heo649027d2010-06-29 10:07:14 +0200858 * gcwq_determine_ins_pos - find insertion position
859 * @gcwq: gcwq of interest
860 * @cwq: cwq a work is being queued for
861 *
862 * A work for @cwq is about to be queued on @gcwq, determine insertion
863 * position for the work. If @cwq is for HIGHPRI wq, the work is
864 * queued at the head of the queue but in FIFO order with respect to
865 * other HIGHPRI works; otherwise, at the end of the queue. This
866 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
867 * there are HIGHPRI works pending.
868 *
869 * CONTEXT:
870 * spin_lock_irq(gcwq->lock).
871 *
872 * RETURNS:
873 * Pointer to inserstion position.
874 */
875static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
876 struct cpu_workqueue_struct *cwq)
877{
878 struct work_struct *twork;
879
880 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
881 return &gcwq->worklist;
882
883 list_for_each_entry(twork, &gcwq->worklist, entry) {
884 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
885
886 if (!(tcwq->wq->flags & WQ_HIGHPRI))
887 break;
888 }
889
890 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
891 return &twork->entry;
892}
893
894/**
Tejun Heo7e116292010-06-29 10:07:13 +0200895 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200896 * @cwq: cwq @work belongs to
897 * @work: work to insert
898 * @head: insertion point
899 * @extra_flags: extra WORK_STRUCT_* flags to set
900 *
Tejun Heo7e116292010-06-29 10:07:13 +0200901 * Insert @work which belongs to @cwq into @gcwq after @head.
902 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200903 *
904 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200905 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200906 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700907static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200908 struct work_struct *work, struct list_head *head,
909 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700910{
Tejun Heoe22bee72010-06-29 10:07:14 +0200911 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100912
Tejun Heo4690c4a2010-06-29 10:07:10 +0200913 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200914 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200915
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700916 /*
917 * Ensure that we get the right work->data if we see the
918 * result of list_add() below, see try_to_grab_pending().
919 */
920 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200921
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700922 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200923
924 /*
925 * Ensure either worker_sched_deactivated() sees the above
926 * list_add_tail() or we see zero nr_running to avoid workers
927 * lying around lazily while there are works to be processed.
928 */
929 smp_mb();
930
Tejun Heo649027d2010-06-29 10:07:14 +0200931 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200932 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700933}
934
Tejun Heoc8efcc22010-12-20 19:32:04 +0100935/*
936 * Test whether @work is being queued from another work executing on the
937 * same workqueue. This is rather expensive and should only be used from
938 * cold paths.
939 */
940static bool is_chained_work(struct workqueue_struct *wq)
941{
942 unsigned long flags;
943 unsigned int cpu;
944
945 for_each_gcwq_cpu(cpu) {
946 struct global_cwq *gcwq = get_gcwq(cpu);
947 struct worker *worker;
948 struct hlist_node *pos;
949 int i;
950
951 spin_lock_irqsave(&gcwq->lock, flags);
952 for_each_busy_worker(worker, i, pos, gcwq) {
953 if (worker->task != current)
954 continue;
955 spin_unlock_irqrestore(&gcwq->lock, flags);
956 /*
957 * I'm @worker, no locking necessary. See if @work
958 * is headed to the same workqueue.
959 */
960 return worker->current_cwq->wq == wq;
961 }
962 spin_unlock_irqrestore(&gcwq->lock, flags);
963 }
964 return false;
965}
966
Tejun Heo4690c4a2010-06-29 10:07:10 +0200967static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct work_struct *work)
969{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200970 struct global_cwq *gcwq;
971 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200972 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200973 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long flags;
975
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900976 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200977
Tejun Heoc8efcc22010-12-20 19:32:04 +0100978 /* if dying, only works from the same workqueue are allowed */
979 if (unlikely(wq->flags & WQ_DYING) &&
980 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200981 return;
982
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200983 /* determine gcwq to use */
984 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200985 struct global_cwq *last_gcwq;
986
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200987 if (unlikely(cpu == WORK_CPU_UNBOUND))
988 cpu = raw_smp_processor_id();
989
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200990 /*
991 * It's multi cpu. If @wq is non-reentrant and @work
992 * was previously on a different cpu, it might still
993 * be running there, in which case the work needs to
994 * be queued on that cpu to guarantee non-reentrance.
995 */
Tejun Heo502ca9d2010-06-29 10:07:13 +0200996 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200997 if (wq->flags & WQ_NON_REENTRANT &&
998 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
999 struct worker *worker;
1000
1001 spin_lock_irqsave(&last_gcwq->lock, flags);
1002
1003 worker = find_worker_executing_work(last_gcwq, work);
1004
1005 if (worker && worker->current_cwq->wq == wq)
1006 gcwq = last_gcwq;
1007 else {
1008 /* meh... not running there, queue here */
1009 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1010 spin_lock_irqsave(&gcwq->lock, flags);
1011 }
1012 } else
1013 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001014 } else {
1015 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1016 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001017 }
1018
1019 /* gcwq determined, get cwq and queue */
1020 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001021 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001022
Tejun Heo4690c4a2010-06-29 10:07:10 +02001023 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001024
Tejun Heo73f53c42010-06-29 10:07:11 +02001025 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001026 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001027
1028 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001029 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001030 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001031 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001032 } else {
1033 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001034 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001035 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001036
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001037 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001038
Tejun Heo8b03ae32010-06-29 10:07:12 +02001039 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001042/**
1043 * queue_work - queue work on a workqueue
1044 * @wq: workqueue to use
1045 * @work: work to queue
1046 *
Alan Stern057647f2006-10-28 10:38:58 -07001047 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001049 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1050 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001052int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001054 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001056 ret = queue_work_on(get_cpu(), wq, work);
1057 put_cpu();
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return ret;
1060}
Dave Jonesae90dd52006-06-30 01:40:45 -04001061EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Zhang Ruic1a220e2008-07-23 21:28:39 -07001063/**
1064 * queue_work_on - queue work on specific cpu
1065 * @cpu: CPU number to execute work on
1066 * @wq: workqueue to use
1067 * @work: work to queue
1068 *
1069 * Returns 0 if @work was already on a queue, non-zero otherwise.
1070 *
1071 * We queue the work to a specific CPU, the caller must ensure it
1072 * can't go away.
1073 */
1074int
1075queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1076{
1077 int ret = 0;
1078
Tejun Heo22df02b2010-06-29 10:07:10 +02001079 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001080 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001081 ret = 1;
1082 }
1083 return ret;
1084}
1085EXPORT_SYMBOL_GPL(queue_work_on);
1086
Li Zefan6d141c32008-02-08 04:21:09 -08001087static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
David Howells52bad642006-11-22 14:54:01 +00001089 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001090 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Tejun Heo4690c4a2010-06-29 10:07:10 +02001092 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001095/**
1096 * queue_delayed_work - queue work on a workqueue after delay
1097 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001098 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001099 * @delay: number of jiffies to wait before queueing
1100 *
Alan Stern057647f2006-10-28 10:38:58 -07001101 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001102 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001103int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001104 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
David Howells52bad642006-11-22 14:54:01 +00001106 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001107 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001109 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
Dave Jonesae90dd52006-06-30 01:40:45 -04001111EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001113/**
1114 * queue_delayed_work_on - queue work on specific CPU after delay
1115 * @cpu: CPU number to execute work on
1116 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001117 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001118 * @delay: number of jiffies to wait before queueing
1119 *
Alan Stern057647f2006-10-28 10:38:58 -07001120 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001121 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001122int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001123 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001124{
1125 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001126 struct timer_list *timer = &dwork->timer;
1127 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001128
Tejun Heo22df02b2010-06-29 10:07:10 +02001129 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001130 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001131
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001132 BUG_ON(timer_pending(timer));
1133 BUG_ON(!list_empty(&work->entry));
1134
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001135 timer_stats_timer_set_start_info(&dwork->timer);
1136
Tejun Heo7a22ad72010-06-29 10:07:13 +02001137 /*
1138 * This stores cwq for the moment, for the timer_fn.
1139 * Note that the work's gcwq is preserved to allow
1140 * reentrance detection for delayed works.
1141 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001142 if (!(wq->flags & WQ_UNBOUND)) {
1143 struct global_cwq *gcwq = get_work_gcwq(work);
1144
1145 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1146 lcpu = gcwq->cpu;
1147 else
1148 lcpu = raw_smp_processor_id();
1149 } else
1150 lcpu = WORK_CPU_UNBOUND;
1151
Tejun Heo7a22ad72010-06-29 10:07:13 +02001152 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001153
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001154 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001155 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001156 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001157
1158 if (unlikely(cpu >= 0))
1159 add_timer_on(timer, cpu);
1160 else
1161 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001162 ret = 1;
1163 }
1164 return ret;
1165}
Dave Jonesae90dd52006-06-30 01:40:45 -04001166EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Tejun Heoc8e55f32010-06-29 10:07:12 +02001168/**
1169 * worker_enter_idle - enter idle state
1170 * @worker: worker which is entering idle state
1171 *
1172 * @worker is entering idle state. Update stats and idle timer if
1173 * necessary.
1174 *
1175 * LOCKING:
1176 * spin_lock_irq(gcwq->lock).
1177 */
1178static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001180 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Tejun Heoc8e55f32010-06-29 10:07:12 +02001182 BUG_ON(worker->flags & WORKER_IDLE);
1183 BUG_ON(!list_empty(&worker->entry) &&
1184 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Tejun Heocb444762010-07-02 10:03:50 +02001186 /* can't use worker_set_flags(), also called from start_worker() */
1187 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001188 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001189 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001190
Tejun Heoc8e55f32010-06-29 10:07:12 +02001191 /* idle_list is LIFO */
1192 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001193
Tejun Heoe22bee72010-06-29 10:07:14 +02001194 if (likely(!(worker->flags & WORKER_ROGUE))) {
1195 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1196 mod_timer(&gcwq->idle_timer,
1197 jiffies + IDLE_WORKER_TIMEOUT);
1198 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001199 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001200
1201 /* sanity check nr_running */
1202 WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
1203 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Tejun Heoc8e55f32010-06-29 10:07:12 +02001206/**
1207 * worker_leave_idle - leave idle state
1208 * @worker: worker which is leaving idle state
1209 *
1210 * @worker is leaving idle state. Update stats.
1211 *
1212 * LOCKING:
1213 * spin_lock_irq(gcwq->lock).
1214 */
1215static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001217 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Tejun Heoc8e55f32010-06-29 10:07:12 +02001219 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001220 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001221 gcwq->nr_idle--;
1222 list_del_init(&worker->entry);
1223}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Tejun Heoe22bee72010-06-29 10:07:14 +02001225/**
1226 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1227 * @worker: self
1228 *
1229 * Works which are scheduled while the cpu is online must at least be
1230 * scheduled to a worker which is bound to the cpu so that if they are
1231 * flushed from cpu callbacks while cpu is going down, they are
1232 * guaranteed to execute on the cpu.
1233 *
1234 * This function is to be used by rogue workers and rescuers to bind
1235 * themselves to the target cpu and may race with cpu going down or
1236 * coming online. kthread_bind() can't be used because it may put the
1237 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1238 * verbatim as it's best effort and blocking and gcwq may be
1239 * [dis]associated in the meantime.
1240 *
1241 * This function tries set_cpus_allowed() and locks gcwq and verifies
1242 * the binding against GCWQ_DISASSOCIATED which is set during
1243 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1244 * idle state or fetches works without dropping lock, it can guarantee
1245 * the scheduling requirement described in the first paragraph.
1246 *
1247 * CONTEXT:
1248 * Might sleep. Called without any lock but returns with gcwq->lock
1249 * held.
1250 *
1251 * RETURNS:
1252 * %true if the associated gcwq is online (@worker is successfully
1253 * bound), %false if offline.
1254 */
1255static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001256__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001257{
1258 struct global_cwq *gcwq = worker->gcwq;
1259 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Tejun Heoe22bee72010-06-29 10:07:14 +02001261 while (true) {
1262 /*
1263 * The following call may fail, succeed or succeed
1264 * without actually migrating the task to the cpu if
1265 * it races with cpu hotunplug operation. Verify
1266 * against GCWQ_DISASSOCIATED.
1267 */
Tejun Heof3421792010-07-02 10:03:51 +02001268 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1269 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001270
Tejun Heoe22bee72010-06-29 10:07:14 +02001271 spin_lock_irq(&gcwq->lock);
1272 if (gcwq->flags & GCWQ_DISASSOCIATED)
1273 return false;
1274 if (task_cpu(task) == gcwq->cpu &&
1275 cpumask_equal(&current->cpus_allowed,
1276 get_cpu_mask(gcwq->cpu)))
1277 return true;
1278 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001279
Tejun Heoe22bee72010-06-29 10:07:14 +02001280 /* CPU has come up inbetween, retry migration */
1281 cpu_relax();
1282 }
1283}
1284
1285/*
1286 * Function for worker->rebind_work used to rebind rogue busy workers
1287 * to the associated cpu which is coming back online. This is
1288 * scheduled by cpu up but can race with other cpu hotplug operations
1289 * and may be executed twice without intervening cpu down.
1290 */
1291static void worker_rebind_fn(struct work_struct *work)
1292{
1293 struct worker *worker = container_of(work, struct worker, rebind_work);
1294 struct global_cwq *gcwq = worker->gcwq;
1295
1296 if (worker_maybe_bind_and_lock(worker))
1297 worker_clr_flags(worker, WORKER_REBIND);
1298
1299 spin_unlock_irq(&gcwq->lock);
1300}
1301
Tejun Heoc34056a2010-06-29 10:07:11 +02001302static struct worker *alloc_worker(void)
1303{
1304 struct worker *worker;
1305
1306 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001307 if (worker) {
1308 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001309 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001310 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1311 /* on creation a worker is in !idle && prep state */
1312 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001313 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001314 return worker;
1315}
1316
1317/**
1318 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001319 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001320 * @bind: whether to set affinity to @cpu or not
1321 *
Tejun Heo7e116292010-06-29 10:07:13 +02001322 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001323 * can be started by calling start_worker() or destroyed using
1324 * destroy_worker().
1325 *
1326 * CONTEXT:
1327 * Might sleep. Does GFP_KERNEL allocations.
1328 *
1329 * RETURNS:
1330 * Pointer to the newly created worker.
1331 */
Tejun Heo7e116292010-06-29 10:07:13 +02001332static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001333{
Tejun Heof3421792010-07-02 10:03:51 +02001334 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001335 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001336 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001337
Tejun Heo8b03ae32010-06-29 10:07:12 +02001338 spin_lock_irq(&gcwq->lock);
1339 while (ida_get_new(&gcwq->worker_ida, &id)) {
1340 spin_unlock_irq(&gcwq->lock);
1341 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001342 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001343 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001344 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001345 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001346
1347 worker = alloc_worker();
1348 if (!worker)
1349 goto fail;
1350
Tejun Heo8b03ae32010-06-29 10:07:12 +02001351 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001352 worker->id = id;
1353
Tejun Heof3421792010-07-02 10:03:51 +02001354 if (!on_unbound_cpu)
1355 worker->task = kthread_create(worker_thread, worker,
1356 "kworker/%u:%d", gcwq->cpu, id);
1357 else
1358 worker->task = kthread_create(worker_thread, worker,
1359 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001360 if (IS_ERR(worker->task))
1361 goto fail;
1362
Tejun Heodb7bccf2010-06-29 10:07:12 +02001363 /*
1364 * A rogue worker will become a regular one if CPU comes
1365 * online later on. Make sure every worker has
1366 * PF_THREAD_BOUND set.
1367 */
Tejun Heof3421792010-07-02 10:03:51 +02001368 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001369 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001370 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001371 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001372 if (on_unbound_cpu)
1373 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001375
Tejun Heoc34056a2010-06-29 10:07:11 +02001376 return worker;
1377fail:
1378 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001379 spin_lock_irq(&gcwq->lock);
1380 ida_remove(&gcwq->worker_ida, id);
1381 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001382 }
1383 kfree(worker);
1384 return NULL;
1385}
1386
1387/**
1388 * start_worker - start a newly created worker
1389 * @worker: worker to start
1390 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001391 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001392 *
1393 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001394 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001395 */
1396static void start_worker(struct worker *worker)
1397{
Tejun Heocb444762010-07-02 10:03:50 +02001398 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001399 worker->gcwq->nr_workers++;
1400 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001401 wake_up_process(worker->task);
1402}
1403
1404/**
1405 * destroy_worker - destroy a workqueue worker
1406 * @worker: worker to be destroyed
1407 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001408 * Destroy @worker and adjust @gcwq stats accordingly.
1409 *
1410 * CONTEXT:
1411 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001412 */
1413static void destroy_worker(struct worker *worker)
1414{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001415 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001416 int id = worker->id;
1417
1418 /* sanity check frenzy */
1419 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001420 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001421
Tejun Heoc8e55f32010-06-29 10:07:12 +02001422 if (worker->flags & WORKER_STARTED)
1423 gcwq->nr_workers--;
1424 if (worker->flags & WORKER_IDLE)
1425 gcwq->nr_idle--;
1426
1427 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001428 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001429
1430 spin_unlock_irq(&gcwq->lock);
1431
Tejun Heoc34056a2010-06-29 10:07:11 +02001432 kthread_stop(worker->task);
1433 kfree(worker);
1434
Tejun Heo8b03ae32010-06-29 10:07:12 +02001435 spin_lock_irq(&gcwq->lock);
1436 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001437}
1438
Tejun Heoe22bee72010-06-29 10:07:14 +02001439static void idle_worker_timeout(unsigned long __gcwq)
1440{
1441 struct global_cwq *gcwq = (void *)__gcwq;
1442
1443 spin_lock_irq(&gcwq->lock);
1444
1445 if (too_many_workers(gcwq)) {
1446 struct worker *worker;
1447 unsigned long expires;
1448
1449 /* idle_list is kept in LIFO order, check the last one */
1450 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1451 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1452
1453 if (time_before(jiffies, expires))
1454 mod_timer(&gcwq->idle_timer, expires);
1455 else {
1456 /* it's been idle for too long, wake up manager */
1457 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1458 wake_up_worker(gcwq);
1459 }
1460 }
1461
1462 spin_unlock_irq(&gcwq->lock);
1463}
1464
1465static bool send_mayday(struct work_struct *work)
1466{
1467 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1468 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001469 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001470
1471 if (!(wq->flags & WQ_RESCUER))
1472 return false;
1473
1474 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001475 cpu = cwq->gcwq->cpu;
1476 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1477 if (cpu == WORK_CPU_UNBOUND)
1478 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001479 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001480 wake_up_process(wq->rescuer->task);
1481 return true;
1482}
1483
1484static void gcwq_mayday_timeout(unsigned long __gcwq)
1485{
1486 struct global_cwq *gcwq = (void *)__gcwq;
1487 struct work_struct *work;
1488
1489 spin_lock_irq(&gcwq->lock);
1490
1491 if (need_to_create_worker(gcwq)) {
1492 /*
1493 * We've been trying to create a new worker but
1494 * haven't been successful. We might be hitting an
1495 * allocation deadlock. Send distress signals to
1496 * rescuers.
1497 */
1498 list_for_each_entry(work, &gcwq->worklist, entry)
1499 send_mayday(work);
1500 }
1501
1502 spin_unlock_irq(&gcwq->lock);
1503
1504 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1505}
1506
1507/**
1508 * maybe_create_worker - create a new worker if necessary
1509 * @gcwq: gcwq to create a new worker for
1510 *
1511 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1512 * have at least one idle worker on return from this function. If
1513 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1514 * sent to all rescuers with works scheduled on @gcwq to resolve
1515 * possible allocation deadlock.
1516 *
1517 * On return, need_to_create_worker() is guaranteed to be false and
1518 * may_start_working() true.
1519 *
1520 * LOCKING:
1521 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1522 * multiple times. Does GFP_KERNEL allocations. Called only from
1523 * manager.
1524 *
1525 * RETURNS:
1526 * false if no action was taken and gcwq->lock stayed locked, true
1527 * otherwise.
1528 */
1529static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001530__releases(&gcwq->lock)
1531__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001532{
1533 if (!need_to_create_worker(gcwq))
1534 return false;
1535restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001536 spin_unlock_irq(&gcwq->lock);
1537
Tejun Heoe22bee72010-06-29 10:07:14 +02001538 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1539 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1540
1541 while (true) {
1542 struct worker *worker;
1543
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 worker = create_worker(gcwq, true);
1545 if (worker) {
1546 del_timer_sync(&gcwq->mayday_timer);
1547 spin_lock_irq(&gcwq->lock);
1548 start_worker(worker);
1549 BUG_ON(need_to_create_worker(gcwq));
1550 return true;
1551 }
1552
1553 if (!need_to_create_worker(gcwq))
1554 break;
1555
Tejun Heoe22bee72010-06-29 10:07:14 +02001556 __set_current_state(TASK_INTERRUPTIBLE);
1557 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001558
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 if (!need_to_create_worker(gcwq))
1560 break;
1561 }
1562
Tejun Heoe22bee72010-06-29 10:07:14 +02001563 del_timer_sync(&gcwq->mayday_timer);
1564 spin_lock_irq(&gcwq->lock);
1565 if (need_to_create_worker(gcwq))
1566 goto restart;
1567 return true;
1568}
1569
1570/**
1571 * maybe_destroy_worker - destroy workers which have been idle for a while
1572 * @gcwq: gcwq to destroy workers for
1573 *
1574 * Destroy @gcwq workers which have been idle for longer than
1575 * IDLE_WORKER_TIMEOUT.
1576 *
1577 * LOCKING:
1578 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1579 * multiple times. Called only from manager.
1580 *
1581 * RETURNS:
1582 * false if no action was taken and gcwq->lock stayed locked, true
1583 * otherwise.
1584 */
1585static bool maybe_destroy_workers(struct global_cwq *gcwq)
1586{
1587 bool ret = false;
1588
1589 while (too_many_workers(gcwq)) {
1590 struct worker *worker;
1591 unsigned long expires;
1592
1593 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1594 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1595
1596 if (time_before(jiffies, expires)) {
1597 mod_timer(&gcwq->idle_timer, expires);
1598 break;
1599 }
1600
1601 destroy_worker(worker);
1602 ret = true;
1603 }
1604
1605 return ret;
1606}
1607
1608/**
1609 * manage_workers - manage worker pool
1610 * @worker: self
1611 *
1612 * Assume the manager role and manage gcwq worker pool @worker belongs
1613 * to. At any given time, there can be only zero or one manager per
1614 * gcwq. The exclusion is handled automatically by this function.
1615 *
1616 * The caller can safely start processing works on false return. On
1617 * true return, it's guaranteed that need_to_create_worker() is false
1618 * and may_start_working() is true.
1619 *
1620 * CONTEXT:
1621 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1622 * multiple times. Does GFP_KERNEL allocations.
1623 *
1624 * RETURNS:
1625 * false if no action was taken and gcwq->lock stayed locked, true if
1626 * some action was taken.
1627 */
1628static bool manage_workers(struct worker *worker)
1629{
1630 struct global_cwq *gcwq = worker->gcwq;
1631 bool ret = false;
1632
1633 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1634 return ret;
1635
1636 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1637 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1638
1639 /*
1640 * Destroy and then create so that may_start_working() is true
1641 * on return.
1642 */
1643 ret |= maybe_destroy_workers(gcwq);
1644 ret |= maybe_create_worker(gcwq);
1645
1646 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1647
1648 /*
1649 * The trustee might be waiting to take over the manager
1650 * position, tell it we're done.
1651 */
1652 if (unlikely(gcwq->trustee))
1653 wake_up_all(&gcwq->trustee_wait);
1654
1655 return ret;
1656}
1657
Tejun Heoa62428c2010-06-29 10:07:10 +02001658/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001659 * move_linked_works - move linked works to a list
1660 * @work: start of series of works to be scheduled
1661 * @head: target list to append @work to
1662 * @nextp: out paramter for nested worklist walking
1663 *
1664 * Schedule linked works starting from @work to @head. Work series to
1665 * be scheduled starts at @work and includes any consecutive work with
1666 * WORK_STRUCT_LINKED set in its predecessor.
1667 *
1668 * If @nextp is not NULL, it's updated to point to the next work of
1669 * the last scheduled work. This allows move_linked_works() to be
1670 * nested inside outer list_for_each_entry_safe().
1671 *
1672 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001673 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001674 */
1675static void move_linked_works(struct work_struct *work, struct list_head *head,
1676 struct work_struct **nextp)
1677{
1678 struct work_struct *n;
1679
1680 /*
1681 * Linked worklist will always end before the end of the list,
1682 * use NULL for list head.
1683 */
1684 list_for_each_entry_safe_from(work, n, NULL, entry) {
1685 list_move_tail(&work->entry, head);
1686 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1687 break;
1688 }
1689
1690 /*
1691 * If we're already inside safe list traversal and have moved
1692 * multiple works to the scheduled queue, the next position
1693 * needs to be updated.
1694 */
1695 if (nextp)
1696 *nextp = n;
1697}
1698
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001699static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1700{
1701 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1702 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001703 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001704
Tejun Heocdadf002010-10-05 10:49:55 +02001705 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001706 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001707 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001708 cwq->nr_active++;
1709}
1710
Tejun Heoaffee4b2010-06-29 10:07:12 +02001711/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001712 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1713 * @cwq: cwq of interest
1714 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001715 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001716 *
1717 * A work either has completed or is removed from pending queue,
1718 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1719 *
1720 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001721 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001722 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001723static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1724 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001725{
1726 /* ignore uncolored works */
1727 if (color == WORK_NO_COLOR)
1728 return;
1729
1730 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001731
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001732 if (!delayed) {
1733 cwq->nr_active--;
1734 if (!list_empty(&cwq->delayed_works)) {
1735 /* one down, submit a delayed one */
1736 if (cwq->nr_active < cwq->max_active)
1737 cwq_activate_first_delayed(cwq);
1738 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001739 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001740
1741 /* is flush in progress and are we at the flushing tip? */
1742 if (likely(cwq->flush_color != color))
1743 return;
1744
1745 /* are there still in-flight works? */
1746 if (cwq->nr_in_flight[color])
1747 return;
1748
1749 /* this cwq is done, clear flush_color */
1750 cwq->flush_color = -1;
1751
1752 /*
1753 * If this was the last cwq, wake up the first flusher. It
1754 * will handle the rest.
1755 */
1756 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1757 complete(&cwq->wq->first_flusher->done);
1758}
1759
1760/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001761 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001762 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001763 * @work: work to process
1764 *
1765 * Process @work. This function contains all the logics necessary to
1766 * process a single work including synchronization against and
1767 * interaction with other workers on the same cpu, queueing and
1768 * flushing. As long as context requirement is met, any worker can
1769 * call this function to process a work.
1770 *
1771 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001772 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001773 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001774static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001775__releases(&gcwq->lock)
1776__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001777{
Tejun Heo7e116292010-06-29 10:07:13 +02001778 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001779 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001780 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001781 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001782 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001783 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001784 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001785#ifdef CONFIG_LOCKDEP
1786 /*
1787 * It is permissible to free the struct work_struct from
1788 * inside the function that is called from it, this we need to
1789 * take into account for lockdep too. To avoid bogus "held
1790 * lock freed" warnings as well as problems when looking into
1791 * work->lockdep_map, make a copy and use that here.
1792 */
1793 struct lockdep_map lockdep_map = work->lockdep_map;
1794#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001795 /*
1796 * A single work shouldn't be executed concurrently by
1797 * multiple workers on a single cpu. Check whether anyone is
1798 * already processing the work. If so, defer the work to the
1799 * currently executing one.
1800 */
1801 collision = __find_worker_executing_work(gcwq, bwh, work);
1802 if (unlikely(collision)) {
1803 move_linked_works(work, &collision->scheduled, NULL);
1804 return;
1805 }
1806
Tejun Heoa62428c2010-06-29 10:07:10 +02001807 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001808 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001809 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001810 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001811 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001812 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001813
Tejun Heo7a22ad72010-06-29 10:07:13 +02001814 /* record the current cpu number in the work data and dequeue */
1815 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001816 list_del_init(&work->entry);
1817
Tejun Heo649027d2010-06-29 10:07:14 +02001818 /*
1819 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1820 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1821 */
1822 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1823 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1824 struct work_struct, entry);
1825
1826 if (!list_empty(&gcwq->worklist) &&
1827 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1828 wake_up_worker(gcwq);
1829 else
1830 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1831 }
1832
Tejun Heofb0e7be2010-06-29 10:07:15 +02001833 /*
1834 * CPU intensive works don't participate in concurrency
1835 * management. They're the scheduler's responsibility.
1836 */
1837 if (unlikely(cpu_intensive))
1838 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1839
Tejun Heo8b03ae32010-06-29 10:07:12 +02001840 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001841
Tejun Heoa62428c2010-06-29 10:07:10 +02001842 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001843 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001844 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001845 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001846 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001847 /*
1848 * While we must be careful to not use "work" after this, the trace
1849 * point will only record its address.
1850 */
1851 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001852 lock_map_release(&lockdep_map);
1853 lock_map_release(&cwq->wq->lockdep_map);
1854
1855 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1856 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1857 "%s/0x%08x/%d\n",
1858 current->comm, preempt_count(), task_pid_nr(current));
1859 printk(KERN_ERR " last function: ");
1860 print_symbol("%s\n", (unsigned long)f);
1861 debug_show_held_locks(current);
1862 dump_stack();
1863 }
1864
Tejun Heo8b03ae32010-06-29 10:07:12 +02001865 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001866
Tejun Heofb0e7be2010-06-29 10:07:15 +02001867 /* clear cpu intensive status */
1868 if (unlikely(cpu_intensive))
1869 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1870
Tejun Heoa62428c2010-06-29 10:07:10 +02001871 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001872 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001873 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001874 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001875 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001876}
1877
Tejun Heoaffee4b2010-06-29 10:07:12 +02001878/**
1879 * process_scheduled_works - process scheduled works
1880 * @worker: self
1881 *
1882 * Process all scheduled works. Please note that the scheduled list
1883 * may change while processing a work, so this function repeatedly
1884 * fetches a work from the top and executes it.
1885 *
1886 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001887 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001888 * multiple times.
1889 */
1890static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001892 while (!list_empty(&worker->scheduled)) {
1893 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001895 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Tejun Heo4690c4a2010-06-29 10:07:10 +02001899/**
1900 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001901 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001902 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001903 * The gcwq worker thread function. There's a single dynamic pool of
1904 * these per each cpu. These workers process all works regardless of
1905 * their specific target workqueue. The only exception is works which
1906 * belong to workqueues with a rescuer which will be explained in
1907 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001908 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001909static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Tejun Heoc34056a2010-06-29 10:07:11 +02001911 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001912 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 /* tell the scheduler that this is a workqueue worker */
1915 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001916woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001917 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Tejun Heoc8e55f32010-06-29 10:07:12 +02001919 /* DIE can be set only while we're idle, checking here is enough */
1920 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001921 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
Tejun Heoc8e55f32010-06-29 10:07:12 +02001926 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001927recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 /* no more worker necessary? */
1929 if (!need_more_worker(gcwq))
1930 goto sleep;
1931
1932 /* do we need to manage? */
1933 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1934 goto recheck;
1935
Tejun Heoc8e55f32010-06-29 10:07:12 +02001936 /*
1937 * ->scheduled list can only be filled while a worker is
1938 * preparing to process a work or actually processing it.
1939 * Make sure nobody diddled with it while I was sleeping.
1940 */
1941 BUG_ON(!list_empty(&worker->scheduled));
1942
Tejun Heoe22bee72010-06-29 10:07:14 +02001943 /*
1944 * When control reaches this point, we're guaranteed to have
1945 * at least one idle worker or that someone else has already
1946 * assumed the manager role.
1947 */
1948 worker_clr_flags(worker, WORKER_PREP);
1949
1950 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001951 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02001952 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001953 struct work_struct, entry);
1954
1955 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1956 /* optimization path, not strictly necessary */
1957 process_one_work(worker, work);
1958 if (unlikely(!list_empty(&worker->scheduled)))
1959 process_scheduled_works(worker);
1960 } else {
1961 move_linked_works(work, &worker->scheduled, NULL);
1962 process_scheduled_works(worker);
1963 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001965
Tejun Heoe22bee72010-06-29 10:07:14 +02001966 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001967sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
1969 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001970
Tejun Heoc8e55f32010-06-29 10:07:12 +02001971 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 * gcwq->lock is held and there's no work to process and no
1973 * need to manage, sleep. Workers are woken up only while
1974 * holding gcwq->lock or from local cpu, so setting the
1975 * current state before releasing gcwq->lock is enough to
1976 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977 */
1978 worker_enter_idle(worker);
1979 __set_current_state(TASK_INTERRUPTIBLE);
1980 spin_unlock_irq(&gcwq->lock);
1981 schedule();
1982 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Tejun Heoe22bee72010-06-29 10:07:14 +02001985/**
1986 * rescuer_thread - the rescuer thread function
1987 * @__wq: the associated workqueue
1988 *
1989 * Workqueue rescuer thread function. There's one rescuer for each
1990 * workqueue which has WQ_RESCUER set.
1991 *
1992 * Regular work processing on a gcwq may block trying to create a new
1993 * worker which uses GFP_KERNEL allocation which has slight chance of
1994 * developing into deadlock if some works currently on the same queue
1995 * need to be processed to satisfy the GFP_KERNEL allocation. This is
1996 * the problem rescuer solves.
1997 *
1998 * When such condition is possible, the gcwq summons rescuers of all
1999 * workqueues which have works queued on the gcwq and let them process
2000 * those works so that forward progress can be guaranteed.
2001 *
2002 * This should happen rarely.
2003 */
2004static int rescuer_thread(void *__wq)
2005{
2006 struct workqueue_struct *wq = __wq;
2007 struct worker *rescuer = wq->rescuer;
2008 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002009 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 unsigned int cpu;
2011
2012 set_user_nice(current, RESCUER_NICE_LEVEL);
2013repeat:
2014 set_current_state(TASK_INTERRUPTIBLE);
2015
2016 if (kthread_should_stop())
2017 return 0;
2018
Tejun Heof3421792010-07-02 10:03:51 +02002019 /*
2020 * See whether any cpu is asking for help. Unbounded
2021 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2022 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002023 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002024 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2025 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 struct global_cwq *gcwq = cwq->gcwq;
2027 struct work_struct *work, *n;
2028
2029 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002030 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002031
2032 /* migrate to the target cpu if possible */
2033 rescuer->gcwq = gcwq;
2034 worker_maybe_bind_and_lock(rescuer);
2035
2036 /*
2037 * Slurp in all works issued via this workqueue and
2038 * process'em.
2039 */
2040 BUG_ON(!list_empty(&rescuer->scheduled));
2041 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2042 if (get_work_cwq(work) == cwq)
2043 move_linked_works(work, scheduled, &n);
2044
2045 process_scheduled_works(rescuer);
2046 spin_unlock_irq(&gcwq->lock);
2047 }
2048
2049 schedule();
2050 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002053struct wq_barrier {
2054 struct work_struct work;
2055 struct completion done;
2056};
2057
2058static void wq_barrier_func(struct work_struct *work)
2059{
2060 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2061 complete(&barr->done);
2062}
2063
Tejun Heo4690c4a2010-06-29 10:07:10 +02002064/**
2065 * insert_wq_barrier - insert a barrier work
2066 * @cwq: cwq to insert barrier into
2067 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002068 * @target: target work to attach @barr to
2069 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002070 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002071 * @barr is linked to @target such that @barr is completed only after
2072 * @target finishes execution. Please note that the ordering
2073 * guarantee is observed only with respect to @target and on the local
2074 * cpu.
2075 *
2076 * Currently, a queued barrier can't be canceled. This is because
2077 * try_to_grab_pending() can't determine whether the work to be
2078 * grabbed is at the head of the queue and thus can't clear LINKED
2079 * flag of the previous work while there must be a valid next work
2080 * after a work with LINKED flag set.
2081 *
2082 * Note that when @worker is non-NULL, @target may be modified
2083 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002084 *
2085 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002086 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002087 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002088static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002089 struct wq_barrier *barr,
2090 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002091{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002092 struct list_head *head;
2093 unsigned int linked = 0;
2094
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002095 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002096 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002097 * as we know for sure that this will not trigger any of the
2098 * checks and call back into the fixup functions where we
2099 * might deadlock.
2100 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002101 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002102 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002103 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002104
Tejun Heoaffee4b2010-06-29 10:07:12 +02002105 /*
2106 * If @target is currently being executed, schedule the
2107 * barrier to the worker; otherwise, put it after @target.
2108 */
2109 if (worker)
2110 head = worker->scheduled.next;
2111 else {
2112 unsigned long *bits = work_data_bits(target);
2113
2114 head = target->entry.next;
2115 /* there can already be other linked works, inherit and set */
2116 linked = *bits & WORK_STRUCT_LINKED;
2117 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2118 }
2119
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002120 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002121 insert_work(cwq, &barr->work, head,
2122 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002123}
2124
Tejun Heo73f53c42010-06-29 10:07:11 +02002125/**
2126 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2127 * @wq: workqueue being flushed
2128 * @flush_color: new flush color, < 0 for no-op
2129 * @work_color: new work color, < 0 for no-op
2130 *
2131 * Prepare cwqs for workqueue flushing.
2132 *
2133 * If @flush_color is non-negative, flush_color on all cwqs should be
2134 * -1. If no cwq has in-flight commands at the specified color, all
2135 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2136 * has in flight commands, its cwq->flush_color is set to
2137 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2138 * wakeup logic is armed and %true is returned.
2139 *
2140 * The caller should have initialized @wq->first_flusher prior to
2141 * calling this function with non-negative @flush_color. If
2142 * @flush_color is negative, no flush color update is done and %false
2143 * is returned.
2144 *
2145 * If @work_color is non-negative, all cwqs should have the same
2146 * work_color which is previous to @work_color and all will be
2147 * advanced to @work_color.
2148 *
2149 * CONTEXT:
2150 * mutex_lock(wq->flush_mutex).
2151 *
2152 * RETURNS:
2153 * %true if @flush_color >= 0 and there's something to flush. %false
2154 * otherwise.
2155 */
2156static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2157 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Tejun Heo73f53c42010-06-29 10:07:11 +02002159 bool wait = false;
2160 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002161
Tejun Heo73f53c42010-06-29 10:07:11 +02002162 if (flush_color >= 0) {
2163 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2164 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002165 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002166
Tejun Heof3421792010-07-02 10:03:51 +02002167 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002168 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002169 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002170
Tejun Heo8b03ae32010-06-29 10:07:12 +02002171 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002172
2173 if (flush_color >= 0) {
2174 BUG_ON(cwq->flush_color != -1);
2175
2176 if (cwq->nr_in_flight[flush_color]) {
2177 cwq->flush_color = flush_color;
2178 atomic_inc(&wq->nr_cwqs_to_flush);
2179 wait = true;
2180 }
2181 }
2182
2183 if (work_color >= 0) {
2184 BUG_ON(work_color != work_next_color(cwq->work_color));
2185 cwq->work_color = work_color;
2186 }
2187
Tejun Heo8b03ae32010-06-29 10:07:12 +02002188 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002189 }
2190
2191 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2192 complete(&wq->first_flusher->done);
2193
2194 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002197/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002199 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 *
2201 * Forces execution of the workqueue and blocks until its completion.
2202 * This is typically used in driver shutdown handlers.
2203 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002204 * We sleep until all works which were queued on entry have been handled,
2205 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002207void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Tejun Heo73f53c42010-06-29 10:07:11 +02002209 struct wq_flusher this_flusher = {
2210 .list = LIST_HEAD_INIT(this_flusher.list),
2211 .flush_color = -1,
2212 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2213 };
2214 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002215
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002216 lock_map_acquire(&wq->lockdep_map);
2217 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002218
2219 mutex_lock(&wq->flush_mutex);
2220
2221 /*
2222 * Start-to-wait phase
2223 */
2224 next_color = work_next_color(wq->work_color);
2225
2226 if (next_color != wq->flush_color) {
2227 /*
2228 * Color space is not full. The current work_color
2229 * becomes our flush_color and work_color is advanced
2230 * by one.
2231 */
2232 BUG_ON(!list_empty(&wq->flusher_overflow));
2233 this_flusher.flush_color = wq->work_color;
2234 wq->work_color = next_color;
2235
2236 if (!wq->first_flusher) {
2237 /* no flush in progress, become the first flusher */
2238 BUG_ON(wq->flush_color != this_flusher.flush_color);
2239
2240 wq->first_flusher = &this_flusher;
2241
2242 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2243 wq->work_color)) {
2244 /* nothing to flush, done */
2245 wq->flush_color = next_color;
2246 wq->first_flusher = NULL;
2247 goto out_unlock;
2248 }
2249 } else {
2250 /* wait in queue */
2251 BUG_ON(wq->flush_color == this_flusher.flush_color);
2252 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2253 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2254 }
2255 } else {
2256 /*
2257 * Oops, color space is full, wait on overflow queue.
2258 * The next flush completion will assign us
2259 * flush_color and transfer to flusher_queue.
2260 */
2261 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2262 }
2263
2264 mutex_unlock(&wq->flush_mutex);
2265
2266 wait_for_completion(&this_flusher.done);
2267
2268 /*
2269 * Wake-up-and-cascade phase
2270 *
2271 * First flushers are responsible for cascading flushes and
2272 * handling overflow. Non-first flushers can simply return.
2273 */
2274 if (wq->first_flusher != &this_flusher)
2275 return;
2276
2277 mutex_lock(&wq->flush_mutex);
2278
Tejun Heo4ce48b32010-07-02 10:03:51 +02002279 /* we might have raced, check again with mutex held */
2280 if (wq->first_flusher != &this_flusher)
2281 goto out_unlock;
2282
Tejun Heo73f53c42010-06-29 10:07:11 +02002283 wq->first_flusher = NULL;
2284
2285 BUG_ON(!list_empty(&this_flusher.list));
2286 BUG_ON(wq->flush_color != this_flusher.flush_color);
2287
2288 while (true) {
2289 struct wq_flusher *next, *tmp;
2290
2291 /* complete all the flushers sharing the current flush color */
2292 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2293 if (next->flush_color != wq->flush_color)
2294 break;
2295 list_del_init(&next->list);
2296 complete(&next->done);
2297 }
2298
2299 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2300 wq->flush_color != work_next_color(wq->work_color));
2301
2302 /* this flush_color is finished, advance by one */
2303 wq->flush_color = work_next_color(wq->flush_color);
2304
2305 /* one color has been freed, handle overflow queue */
2306 if (!list_empty(&wq->flusher_overflow)) {
2307 /*
2308 * Assign the same color to all overflowed
2309 * flushers, advance work_color and append to
2310 * flusher_queue. This is the start-to-wait
2311 * phase for these overflowed flushers.
2312 */
2313 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2314 tmp->flush_color = wq->work_color;
2315
2316 wq->work_color = work_next_color(wq->work_color);
2317
2318 list_splice_tail_init(&wq->flusher_overflow,
2319 &wq->flusher_queue);
2320 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2321 }
2322
2323 if (list_empty(&wq->flusher_queue)) {
2324 BUG_ON(wq->flush_color != wq->work_color);
2325 break;
2326 }
2327
2328 /*
2329 * Need to flush more colors. Make the next flusher
2330 * the new first flusher and arm cwqs.
2331 */
2332 BUG_ON(wq->flush_color == wq->work_color);
2333 BUG_ON(wq->flush_color != next->flush_color);
2334
2335 list_del_init(&next->list);
2336 wq->first_flusher = next;
2337
2338 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2339 break;
2340
2341 /*
2342 * Meh... this color is already done, clear first
2343 * flusher and repeat cascading.
2344 */
2345 wq->first_flusher = NULL;
2346 }
2347
2348out_unlock:
2349 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
Dave Jonesae90dd52006-06-30 01:40:45 -04002351EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Tejun Heobaf59022010-09-16 10:42:16 +02002353static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2354 bool wait_executing)
2355{
2356 struct worker *worker = NULL;
2357 struct global_cwq *gcwq;
2358 struct cpu_workqueue_struct *cwq;
2359
2360 might_sleep();
2361 gcwq = get_work_gcwq(work);
2362 if (!gcwq)
2363 return false;
2364
2365 spin_lock_irq(&gcwq->lock);
2366 if (!list_empty(&work->entry)) {
2367 /*
2368 * See the comment near try_to_grab_pending()->smp_rmb().
2369 * If it was re-queued to a different gcwq under us, we
2370 * are not going to wait.
2371 */
2372 smp_rmb();
2373 cwq = get_work_cwq(work);
2374 if (unlikely(!cwq || gcwq != cwq->gcwq))
2375 goto already_gone;
2376 } else if (wait_executing) {
2377 worker = find_worker_executing_work(gcwq, work);
2378 if (!worker)
2379 goto already_gone;
2380 cwq = worker->current_cwq;
2381 } else
2382 goto already_gone;
2383
2384 insert_wq_barrier(cwq, barr, work, worker);
2385 spin_unlock_irq(&gcwq->lock);
2386
Tejun Heoe1594892011-01-09 23:32:15 +01002387 /*
2388 * If @max_active is 1 or rescuer is in use, flushing another work
2389 * item on the same workqueue may lead to deadlock. Make sure the
2390 * flusher is not running on the same workqueue by verifying write
2391 * access.
2392 */
2393 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2394 lock_map_acquire(&cwq->wq->lockdep_map);
2395 else
2396 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002397 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002398
Tejun Heobaf59022010-09-16 10:42:16 +02002399 return true;
2400already_gone:
2401 spin_unlock_irq(&gcwq->lock);
2402 return false;
2403}
2404
Oleg Nesterovdb700892008-07-25 01:47:49 -07002405/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002406 * flush_work - wait for a work to finish executing the last queueing instance
2407 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002408 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002409 * Wait until @work has finished execution. This function considers
2410 * only the last queueing instance of @work. If @work has been
2411 * enqueued across different CPUs on a non-reentrant workqueue or on
2412 * multiple workqueues, @work might still be executing on return on
2413 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002414 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002415 * If @work was queued only on a non-reentrant, ordered or unbound
2416 * workqueue, @work is guaranteed to be idle on return if it hasn't
2417 * been requeued since flush started.
2418 *
2419 * RETURNS:
2420 * %true if flush_work() waited for the work to finish execution,
2421 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002422 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002423bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002424{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002425 struct wq_barrier barr;
2426
Tejun Heobaf59022010-09-16 10:42:16 +02002427 if (start_flush_work(work, &barr, true)) {
2428 wait_for_completion(&barr.done);
2429 destroy_work_on_stack(&barr.work);
2430 return true;
2431 } else
2432 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002433}
2434EXPORT_SYMBOL_GPL(flush_work);
2435
Tejun Heo401a8d02010-09-16 10:36:00 +02002436static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2437{
2438 struct wq_barrier barr;
2439 struct worker *worker;
2440
2441 spin_lock_irq(&gcwq->lock);
2442
2443 worker = find_worker_executing_work(gcwq, work);
2444 if (unlikely(worker))
2445 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2446
2447 spin_unlock_irq(&gcwq->lock);
2448
2449 if (unlikely(worker)) {
2450 wait_for_completion(&barr.done);
2451 destroy_work_on_stack(&barr.work);
2452 return true;
2453 } else
2454 return false;
2455}
2456
2457static bool wait_on_work(struct work_struct *work)
2458{
2459 bool ret = false;
2460 int cpu;
2461
2462 might_sleep();
2463
2464 lock_map_acquire(&work->lockdep_map);
2465 lock_map_release(&work->lockdep_map);
2466
2467 for_each_gcwq_cpu(cpu)
2468 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2469 return ret;
2470}
2471
Tejun Heo09383492010-09-16 10:48:29 +02002472/**
2473 * flush_work_sync - wait until a work has finished execution
2474 * @work: the work to flush
2475 *
2476 * Wait until @work has finished execution. On return, it's
2477 * guaranteed that all queueing instances of @work which happened
2478 * before this function is called are finished. In other words, if
2479 * @work hasn't been requeued since this function was called, @work is
2480 * guaranteed to be idle on return.
2481 *
2482 * RETURNS:
2483 * %true if flush_work_sync() waited for the work to finish execution,
2484 * %false if it was already idle.
2485 */
2486bool flush_work_sync(struct work_struct *work)
2487{
2488 struct wq_barrier barr;
2489 bool pending, waited;
2490
2491 /* we'll wait for executions separately, queue barr only if pending */
2492 pending = start_flush_work(work, &barr, false);
2493
2494 /* wait for executions to finish */
2495 waited = wait_on_work(work);
2496
2497 /* wait for the pending one */
2498 if (pending) {
2499 wait_for_completion(&barr.done);
2500 destroy_work_on_stack(&barr.work);
2501 }
2502
2503 return pending || waited;
2504}
2505EXPORT_SYMBOL_GPL(flush_work_sync);
2506
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002507/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002508 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002509 * so this work can't be re-armed in any way.
2510 */
2511static int try_to_grab_pending(struct work_struct *work)
2512{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002513 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002514 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002515
Tejun Heo22df02b2010-06-29 10:07:10 +02002516 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002517 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002518
2519 /*
2520 * The queueing is in progress, or it is already queued. Try to
2521 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2522 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002523 gcwq = get_work_gcwq(work);
2524 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002525 return ret;
2526
Tejun Heo8b03ae32010-06-29 10:07:12 +02002527 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002528 if (!list_empty(&work->entry)) {
2529 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002530 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002531 * In that case we must see the new value after rmb(), see
2532 * insert_work()->wmb().
2533 */
2534 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002535 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002536 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002537 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002538 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002539 get_work_color(work),
2540 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002541 ret = 1;
2542 }
2543 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002544 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002545
2546 return ret;
2547}
2548
Tejun Heo401a8d02010-09-16 10:36:00 +02002549static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002550 struct timer_list* timer)
2551{
2552 int ret;
2553
2554 do {
2555 ret = (timer && likely(del_timer(timer)));
2556 if (!ret)
2557 ret = try_to_grab_pending(work);
2558 wait_on_work(work);
2559 } while (unlikely(ret < 0));
2560
Tejun Heo7a22ad72010-06-29 10:07:13 +02002561 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002562 return ret;
2563}
2564
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002565/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002566 * cancel_work_sync - cancel a work and wait for it to finish
2567 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002568 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002569 * Cancel @work and wait for its execution to finish. This function
2570 * can be used even if the work re-queues itself or migrates to
2571 * another workqueue. On return from this function, @work is
2572 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002573 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002574 * cancel_work_sync(&delayed_work->work) must not be used for
2575 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002576 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002577 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002578 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002579 *
2580 * RETURNS:
2581 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002582 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002583bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002584{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002585 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002586}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002587EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002588
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002589/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002590 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2591 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002592 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002593 * Delayed timer is cancelled and the pending work is queued for
2594 * immediate execution. Like flush_work(), this function only
2595 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002596 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002597 * RETURNS:
2598 * %true if flush_work() waited for the work to finish execution,
2599 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002600 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002601bool flush_delayed_work(struct delayed_work *dwork)
2602{
2603 if (del_timer_sync(&dwork->timer))
2604 __queue_work(raw_smp_processor_id(),
2605 get_work_cwq(&dwork->work)->wq, &dwork->work);
2606 return flush_work(&dwork->work);
2607}
2608EXPORT_SYMBOL(flush_delayed_work);
2609
2610/**
Tejun Heo09383492010-09-16 10:48:29 +02002611 * flush_delayed_work_sync - wait for a dwork to finish
2612 * @dwork: the delayed work to flush
2613 *
2614 * Delayed timer is cancelled and the pending work is queued for
2615 * execution immediately. Other than timer handling, its behavior
2616 * is identical to flush_work_sync().
2617 *
2618 * RETURNS:
2619 * %true if flush_work_sync() waited for the work to finish execution,
2620 * %false if it was already idle.
2621 */
2622bool flush_delayed_work_sync(struct delayed_work *dwork)
2623{
2624 if (del_timer_sync(&dwork->timer))
2625 __queue_work(raw_smp_processor_id(),
2626 get_work_cwq(&dwork->work)->wq, &dwork->work);
2627 return flush_work_sync(&dwork->work);
2628}
2629EXPORT_SYMBOL(flush_delayed_work_sync);
2630
2631/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002632 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2633 * @dwork: the delayed work cancel
2634 *
2635 * This is cancel_work_sync() for delayed works.
2636 *
2637 * RETURNS:
2638 * %true if @dwork was pending, %false otherwise.
2639 */
2640bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002641{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002642 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002643}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002644EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002646/**
2647 * schedule_work - put work task in global workqueue
2648 * @work: job to be done
2649 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002650 * Returns zero if @work was already on the kernel-global workqueue and
2651 * non-zero otherwise.
2652 *
2653 * This puts a job in the kernel-global workqueue if it was not already
2654 * queued and leaves it in the same position on the kernel-global
2655 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002656 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002657int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Tejun Heod320c032010-06-29 10:07:14 +02002659 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
Dave Jonesae90dd52006-06-30 01:40:45 -04002661EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Zhang Ruic1a220e2008-07-23 21:28:39 -07002663/*
2664 * schedule_work_on - put work task on a specific cpu
2665 * @cpu: cpu to put the work task on
2666 * @work: job to be done
2667 *
2668 * This puts a job on a specific cpu
2669 */
2670int schedule_work_on(int cpu, struct work_struct *work)
2671{
Tejun Heod320c032010-06-29 10:07:14 +02002672 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002673}
2674EXPORT_SYMBOL(schedule_work_on);
2675
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002676/**
2677 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002678 * @dwork: job to be done
2679 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002680 *
2681 * After waiting for a given time this puts a job in the kernel-global
2682 * workqueue.
2683 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002684int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002685 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Tejun Heod320c032010-06-29 10:07:14 +02002687 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
Dave Jonesae90dd52006-06-30 01:40:45 -04002689EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002691/**
2692 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2693 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002694 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002695 * @delay: number of jiffies to wait
2696 *
2697 * After waiting for a given time this puts a job in the kernel-global
2698 * workqueue on the specified CPU.
2699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002701 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Tejun Heod320c032010-06-29 10:07:14 +02002703 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
Dave Jonesae90dd52006-06-30 01:40:45 -04002705EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Andrew Mortonb6136772006-06-25 05:47:49 -07002707/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002708 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002709 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002710 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002711 * schedule_on_each_cpu() executes @func on each online CPU using the
2712 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002713 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002714 *
2715 * RETURNS:
2716 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002717 */
David Howells65f27f32006-11-22 14:55:48 +00002718int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002719{
2720 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002721 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002722
Andrew Mortonb6136772006-06-25 05:47:49 -07002723 works = alloc_percpu(struct work_struct);
2724 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002725 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002726
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002727 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002728
Christoph Lameter15316ba2006-01-08 01:00:43 -08002729 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002730 struct work_struct *work = per_cpu_ptr(works, cpu);
2731
2732 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002733 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002734 }
Tejun Heo93981802009-11-17 14:06:20 -08002735
2736 for_each_online_cpu(cpu)
2737 flush_work(per_cpu_ptr(works, cpu));
2738
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002739 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002740 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002741 return 0;
2742}
2743
Alan Sterneef6a7d2010-02-12 17:39:21 +09002744/**
2745 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2746 *
2747 * Forces execution of the kernel-global workqueue and blocks until its
2748 * completion.
2749 *
2750 * Think twice before calling this function! It's very easy to get into
2751 * trouble if you don't take great care. Either of the following situations
2752 * will lead to deadlock:
2753 *
2754 * One of the work items currently on the workqueue needs to acquire
2755 * a lock held by your code or its caller.
2756 *
2757 * Your code is running in the context of a work routine.
2758 *
2759 * They will be detected by lockdep when they occur, but the first might not
2760 * occur very often. It depends on what work items are on the workqueue and
2761 * what locks they need, which you have no control over.
2762 *
2763 * In most situations flushing the entire workqueue is overkill; you merely
2764 * need to know that a particular work item isn't queued and isn't running.
2765 * In such cases you should use cancel_delayed_work_sync() or
2766 * cancel_work_sync() instead.
2767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768void flush_scheduled_work(void)
2769{
Tejun Heod320c032010-06-29 10:07:14 +02002770 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
Dave Jonesae90dd52006-06-30 01:40:45 -04002772EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002775 * execute_in_process_context - reliably execute the routine with user context
2776 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002777 * @ew: guaranteed storage for the execute work structure (must
2778 * be available when the work executes)
2779 *
2780 * Executes the function immediately if process context is available,
2781 * otherwise schedules the function for delayed execution.
2782 *
2783 * Returns: 0 - function was executed
2784 * 1 - function was scheduled for execution
2785 */
David Howells65f27f32006-11-22 14:55:48 +00002786int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002787{
2788 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002789 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002790 return 0;
2791 }
2792
David Howells65f27f32006-11-22 14:55:48 +00002793 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002794 schedule_work(&ew->work);
2795
2796 return 1;
2797}
2798EXPORT_SYMBOL_GPL(execute_in_process_context);
2799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800int keventd_up(void)
2801{
Tejun Heod320c032010-06-29 10:07:14 +02002802 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002805static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002807 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002808 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2809 * Make sure that the alignment isn't lower than that of
2810 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002811 */
Tejun Heo0f900042010-06-29 10:07:11 +02002812 const size_t size = sizeof(struct cpu_workqueue_struct);
2813 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2814 __alignof__(unsigned long long));
Tejun Heo931ac772010-07-20 11:07:48 +02002815#ifdef CONFIG_SMP
2816 bool percpu = !(wq->flags & WQ_UNBOUND);
2817#else
2818 bool percpu = false;
2819#endif
Oleg Nesterov3af244332007-05-09 02:34:09 -07002820
Tejun Heo931ac772010-07-20 11:07:48 +02002821 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002822 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002823 else {
Tejun Heof3421792010-07-02 10:03:51 +02002824 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002825
Tejun Heof3421792010-07-02 10:03:51 +02002826 /*
2827 * Allocate enough room to align cwq and put an extra
2828 * pointer at the end pointing back to the originally
2829 * allocated pointer which will be used for free.
2830 */
2831 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2832 if (ptr) {
2833 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2834 *(void **)(wq->cpu_wq.single + 1) = ptr;
2835 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002836 }
Tejun Heof3421792010-07-02 10:03:51 +02002837
David Howells52605622010-10-25 23:41:11 +01002838 /* just in case, make sure it's actually aligned
2839 * - this is affected by PERCPU() alignment in vmlinux.lds.S
2840 */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002841 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2842 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002843}
2844
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002845static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002846{
Tejun Heo931ac772010-07-20 11:07:48 +02002847#ifdef CONFIG_SMP
2848 bool percpu = !(wq->flags & WQ_UNBOUND);
2849#else
2850 bool percpu = false;
2851#endif
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002852
Tejun Heo931ac772010-07-20 11:07:48 +02002853 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002854 free_percpu(wq->cpu_wq.pcpu);
2855 else if (wq->cpu_wq.single) {
2856 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002857 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002858 }
2859}
2860
Tejun Heof3421792010-07-02 10:03:51 +02002861static int wq_clamp_max_active(int max_active, unsigned int flags,
2862 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002863{
Tejun Heof3421792010-07-02 10:03:51 +02002864 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2865
2866 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002867 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2868 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002869 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002870
Tejun Heof3421792010-07-02 10:03:51 +02002871 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002872}
2873
Tejun Heod320c032010-06-29 10:07:14 +02002874struct workqueue_struct *__alloc_workqueue_key(const char *name,
2875 unsigned int flags,
2876 int max_active,
2877 struct lock_class_key *key,
2878 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002879{
2880 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002881 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002882
Tejun Heof3421792010-07-02 10:03:51 +02002883 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002884 * Workqueues which may be used during memory reclaim should
2885 * have a rescuer to guarantee forward progress.
2886 */
2887 if (flags & WQ_MEM_RECLAIM)
2888 flags |= WQ_RESCUER;
2889
2890 /*
Tejun Heof3421792010-07-02 10:03:51 +02002891 * Unbound workqueues aren't concurrency managed and should be
2892 * dispatched to workers immediately.
2893 */
2894 if (flags & WQ_UNBOUND)
2895 flags |= WQ_HIGHPRI;
2896
Tejun Heod320c032010-06-29 10:07:14 +02002897 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heof3421792010-07-02 10:03:51 +02002898 max_active = wq_clamp_max_active(max_active, flags, name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002899
2900 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2901 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02002902 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002903
Tejun Heo97e37d72010-06-29 10:07:10 +02002904 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002905 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002906 mutex_init(&wq->flush_mutex);
2907 atomic_set(&wq->nr_cwqs_to_flush, 0);
2908 INIT_LIST_HEAD(&wq->flusher_queue);
2909 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002910
2911 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01002912 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002913 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002914
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002915 if (alloc_cwqs(wq) < 0)
2916 goto err;
2917
Tejun Heof3421792010-07-02 10:03:51 +02002918 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002919 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002920 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02002921
Tejun Heo0f900042010-06-29 10:07:11 +02002922 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002923 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002924 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002925 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002926 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002927 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002928 }
2929
Tejun Heoe22bee72010-06-29 10:07:14 +02002930 if (flags & WQ_RESCUER) {
2931 struct worker *rescuer;
2932
Tejun Heof2e005a2010-07-20 15:59:09 +02002933 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02002934 goto err;
2935
2936 wq->rescuer = rescuer = alloc_worker();
2937 if (!rescuer)
2938 goto err;
2939
2940 rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
2941 if (IS_ERR(rescuer->task))
2942 goto err;
2943
Tejun Heoe22bee72010-06-29 10:07:14 +02002944 rescuer->task->flags |= PF_THREAD_BOUND;
2945 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002946 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002947
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002948 /*
2949 * workqueue_lock protects global freeze state and workqueues
2950 * list. Grab it, set max_active accordingly and add the new
2951 * workqueue to workqueues list.
2952 */
Tejun Heo15376632010-06-29 10:07:11 +02002953 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002954
2955 if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
Tejun Heof3421792010-07-02 10:03:51 +02002956 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002957 get_cwq(cpu, wq)->max_active = 0;
2958
Tejun Heo15376632010-06-29 10:07:11 +02002959 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002960
Tejun Heo15376632010-06-29 10:07:11 +02002961 spin_unlock(&workqueue_lock);
2962
Oleg Nesterov3af244332007-05-09 02:34:09 -07002963 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02002964err:
2965 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002966 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02002967 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002968 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02002969 kfree(wq);
2970 }
2971 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002972}
Tejun Heod320c032010-06-29 10:07:14 +02002973EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002974
2975/**
2976 * destroy_workqueue - safely terminate a workqueue
2977 * @wq: target workqueue
2978 *
2979 * Safely destroy a workqueue. All work currently pending will be done first.
2980 */
2981void destroy_workqueue(struct workqueue_struct *wq)
2982{
Tejun Heoc8efcc22010-12-20 19:32:04 +01002983 unsigned int flush_cnt = 0;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002984 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002985
Tejun Heoc8efcc22010-12-20 19:32:04 +01002986 /*
2987 * Mark @wq dying and drain all pending works. Once WQ_DYING is
2988 * set, only chain queueing is allowed. IOW, only currently
2989 * pending or running work items on @wq can queue further work
2990 * items on it. @wq is flushed repeatedly until it becomes empty.
2991 * The number of flushing is detemined by the depth of chaining and
2992 * should be relatively short. Whine if it takes too long.
2993 */
Tejun Heoe41e7042010-08-24 14:22:47 +02002994 wq->flags |= WQ_DYING;
Tejun Heoc8efcc22010-12-20 19:32:04 +01002995reflush:
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002996 flush_workqueue(wq);
2997
Tejun Heoc8efcc22010-12-20 19:32:04 +01002998 for_each_cwq_cpu(cpu, wq) {
2999 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3000
3001 if (!cwq->nr_active && list_empty(&cwq->delayed_works))
3002 continue;
3003
3004 if (++flush_cnt == 10 ||
3005 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
3006 printk(KERN_WARNING "workqueue %s: flush on "
3007 "destruction isn't complete after %u tries\n",
3008 wq->name, flush_cnt);
3009 goto reflush;
3010 }
3011
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003012 /*
3013 * wq list is used to freeze wq, remove from list after
3014 * flushing is complete in case freeze races us.
3015 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003016 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003017 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003018 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003019
Tejun Heoe22bee72010-06-29 10:07:14 +02003020 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003021 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003022 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3023 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003024
Tejun Heo73f53c42010-06-29 10:07:11 +02003025 for (i = 0; i < WORK_NR_COLORS; i++)
3026 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003027 BUG_ON(cwq->nr_active);
3028 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003029 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003030
Tejun Heoe22bee72010-06-29 10:07:14 +02003031 if (wq->flags & WQ_RESCUER) {
3032 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003033 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003034 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003035 }
3036
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003037 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003038 kfree(wq);
3039}
3040EXPORT_SYMBOL_GPL(destroy_workqueue);
3041
Tejun Heodcd989c2010-06-29 10:07:14 +02003042/**
3043 * workqueue_set_max_active - adjust max_active of a workqueue
3044 * @wq: target workqueue
3045 * @max_active: new max_active value.
3046 *
3047 * Set max_active of @wq to @max_active.
3048 *
3049 * CONTEXT:
3050 * Don't call from IRQ context.
3051 */
3052void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3053{
3054 unsigned int cpu;
3055
Tejun Heof3421792010-07-02 10:03:51 +02003056 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003057
3058 spin_lock(&workqueue_lock);
3059
3060 wq->saved_max_active = max_active;
3061
Tejun Heof3421792010-07-02 10:03:51 +02003062 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003063 struct global_cwq *gcwq = get_gcwq(cpu);
3064
3065 spin_lock_irq(&gcwq->lock);
3066
3067 if (!(wq->flags & WQ_FREEZEABLE) ||
3068 !(gcwq->flags & GCWQ_FREEZING))
3069 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3070
3071 spin_unlock_irq(&gcwq->lock);
3072 }
3073
3074 spin_unlock(&workqueue_lock);
3075}
3076EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3077
3078/**
3079 * workqueue_congested - test whether a workqueue is congested
3080 * @cpu: CPU in question
3081 * @wq: target workqueue
3082 *
3083 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3084 * no synchronization around this function and the test result is
3085 * unreliable and only useful as advisory hints or for debugging.
3086 *
3087 * RETURNS:
3088 * %true if congested, %false otherwise.
3089 */
3090bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3091{
3092 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3093
3094 return !list_empty(&cwq->delayed_works);
3095}
3096EXPORT_SYMBOL_GPL(workqueue_congested);
3097
3098/**
3099 * work_cpu - return the last known associated cpu for @work
3100 * @work: the work of interest
3101 *
3102 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003103 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003104 */
3105unsigned int work_cpu(struct work_struct *work)
3106{
3107 struct global_cwq *gcwq = get_work_gcwq(work);
3108
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003109 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003110}
3111EXPORT_SYMBOL_GPL(work_cpu);
3112
3113/**
3114 * work_busy - test whether a work is currently pending or running
3115 * @work: the work to be tested
3116 *
3117 * Test whether @work is currently pending or running. There is no
3118 * synchronization around this function and the test result is
3119 * unreliable and only useful as advisory hints or for debugging.
3120 * Especially for reentrant wqs, the pending state might hide the
3121 * running state.
3122 *
3123 * RETURNS:
3124 * OR'd bitmask of WORK_BUSY_* bits.
3125 */
3126unsigned int work_busy(struct work_struct *work)
3127{
3128 struct global_cwq *gcwq = get_work_gcwq(work);
3129 unsigned long flags;
3130 unsigned int ret = 0;
3131
3132 if (!gcwq)
3133 return false;
3134
3135 spin_lock_irqsave(&gcwq->lock, flags);
3136
3137 if (work_pending(work))
3138 ret |= WORK_BUSY_PENDING;
3139 if (find_worker_executing_work(gcwq, work))
3140 ret |= WORK_BUSY_RUNNING;
3141
3142 spin_unlock_irqrestore(&gcwq->lock, flags);
3143
3144 return ret;
3145}
3146EXPORT_SYMBOL_GPL(work_busy);
3147
Tejun Heodb7bccf2010-06-29 10:07:12 +02003148/*
3149 * CPU hotplug.
3150 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003151 * There are two challenges in supporting CPU hotplug. Firstly, there
3152 * are a lot of assumptions on strong associations among work, cwq and
3153 * gcwq which make migrating pending and scheduled works very
3154 * difficult to implement without impacting hot paths. Secondly,
3155 * gcwqs serve mix of short, long and very long running works making
3156 * blocked draining impractical.
3157 *
3158 * This is solved by allowing a gcwq to be detached from CPU, running
3159 * it with unbound (rogue) workers and allowing it to be reattached
3160 * later if the cpu comes back online. A separate thread is created
3161 * to govern a gcwq in such state and is called the trustee of the
3162 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003163 *
3164 * Trustee states and their descriptions.
3165 *
3166 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3167 * new trustee is started with this state.
3168 *
3169 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003170 * assuming the manager role and making all existing
3171 * workers rogue. DOWN_PREPARE waits for trustee to
3172 * enter this state. After reaching IN_CHARGE, trustee
3173 * tries to execute the pending worklist until it's empty
3174 * and the state is set to BUTCHER, or the state is set
3175 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003176 *
3177 * BUTCHER Command state which is set by the cpu callback after
3178 * the cpu has went down. Once this state is set trustee
3179 * knows that there will be no new works on the worklist
3180 * and once the worklist is empty it can proceed to
3181 * killing idle workers.
3182 *
3183 * RELEASE Command state which is set by the cpu callback if the
3184 * cpu down has been canceled or it has come online
3185 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003186 * trying to drain or butcher and clears ROGUE, rebinds
3187 * all remaining workers back to the cpu and releases
3188 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003189 *
3190 * DONE Trustee will enter this state after BUTCHER or RELEASE
3191 * is complete.
3192 *
3193 * trustee CPU draining
3194 * took over down complete
3195 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3196 * | | ^
3197 * | CPU is back online v return workers |
3198 * ----------------> RELEASE --------------
3199 */
3200
3201/**
3202 * trustee_wait_event_timeout - timed event wait for trustee
3203 * @cond: condition to wait for
3204 * @timeout: timeout in jiffies
3205 *
3206 * wait_event_timeout() for trustee to use. Handles locking and
3207 * checks for RELEASE request.
3208 *
3209 * CONTEXT:
3210 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3211 * multiple times. To be used by trustee.
3212 *
3213 * RETURNS:
3214 * Positive indicating left time if @cond is satisfied, 0 if timed
3215 * out, -1 if canceled.
3216 */
3217#define trustee_wait_event_timeout(cond, timeout) ({ \
3218 long __ret = (timeout); \
3219 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3220 __ret) { \
3221 spin_unlock_irq(&gcwq->lock); \
3222 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3223 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3224 __ret); \
3225 spin_lock_irq(&gcwq->lock); \
3226 } \
3227 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3228})
3229
3230/**
3231 * trustee_wait_event - event wait for trustee
3232 * @cond: condition to wait for
3233 *
3234 * wait_event() for trustee to use. Automatically handles locking and
3235 * checks for CANCEL request.
3236 *
3237 * CONTEXT:
3238 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3239 * multiple times. To be used by trustee.
3240 *
3241 * RETURNS:
3242 * 0 if @cond is satisfied, -1 if canceled.
3243 */
3244#define trustee_wait_event(cond) ({ \
3245 long __ret1; \
3246 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3247 __ret1 < 0 ? -1 : 0; \
3248})
3249
3250static int __cpuinit trustee_thread(void *__gcwq)
3251{
3252 struct global_cwq *gcwq = __gcwq;
3253 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003254 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003255 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003256 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003257 int i;
3258
3259 BUG_ON(gcwq->cpu != smp_processor_id());
3260
3261 spin_lock_irq(&gcwq->lock);
3262 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003263 * Claim the manager position and make all workers rogue.
3264 * Trustee must be bound to the target cpu and can't be
3265 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003266 */
3267 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003268 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3269 BUG_ON(rc < 0);
3270
3271 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003272
3273 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003274 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003275
3276 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003277 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003278
3279 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003280 * Call schedule() so that we cross rq->lock and thus can
3281 * guarantee sched callbacks see the rogue flag. This is
3282 * necessary as scheduler callbacks may be invoked from other
3283 * cpus.
3284 */
3285 spin_unlock_irq(&gcwq->lock);
3286 schedule();
3287 spin_lock_irq(&gcwq->lock);
3288
3289 /*
Tejun Heocb444762010-07-02 10:03:50 +02003290 * Sched callbacks are disabled now. Zap nr_running. After
3291 * this, nr_running stays zero and need_more_worker() and
3292 * keep_working() are always true as long as the worklist is
3293 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003294 */
Tejun Heocb444762010-07-02 10:03:50 +02003295 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003296
3297 spin_unlock_irq(&gcwq->lock);
3298 del_timer_sync(&gcwq->idle_timer);
3299 spin_lock_irq(&gcwq->lock);
3300
3301 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003302 * We're now in charge. Notify and proceed to drain. We need
3303 * to keep the gcwq running during the whole CPU down
3304 * procedure as other cpu hotunplug callbacks may need to
3305 * flush currently running tasks.
3306 */
3307 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3308 wake_up_all(&gcwq->trustee_wait);
3309
3310 /*
3311 * The original cpu is in the process of dying and may go away
3312 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003313 * be migrated to other cpus. Try draining any left work. We
3314 * want to get it over with ASAP - spam rescuers, wake up as
3315 * many idlers as necessary and create new ones till the
3316 * worklist is empty. Note that if the gcwq is frozen, there
3317 * may be frozen works in freezeable cwqs. Don't declare
3318 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003319 */
3320 while (gcwq->nr_workers != gcwq->nr_idle ||
3321 gcwq->flags & GCWQ_FREEZING ||
3322 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003323 int nr_works = 0;
3324
3325 list_for_each_entry(work, &gcwq->worklist, entry) {
3326 send_mayday(work);
3327 nr_works++;
3328 }
3329
3330 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3331 if (!nr_works--)
3332 break;
3333 wake_up_process(worker->task);
3334 }
3335
3336 if (need_to_create_worker(gcwq)) {
3337 spin_unlock_irq(&gcwq->lock);
3338 worker = create_worker(gcwq, false);
3339 spin_lock_irq(&gcwq->lock);
3340 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003341 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003342 start_worker(worker);
3343 }
3344 }
3345
Tejun Heodb7bccf2010-06-29 10:07:12 +02003346 /* give a breather */
3347 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3348 break;
3349 }
3350
Tejun Heoe22bee72010-06-29 10:07:14 +02003351 /*
3352 * Either all works have been scheduled and cpu is down, or
3353 * cpu down has already been canceled. Wait for and butcher
3354 * all workers till we're canceled.
3355 */
3356 do {
3357 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3358 while (!list_empty(&gcwq->idle_list))
3359 destroy_worker(list_first_entry(&gcwq->idle_list,
3360 struct worker, entry));
3361 } while (gcwq->nr_workers && rc >= 0);
3362
3363 /*
3364 * At this point, either draining has completed and no worker
3365 * is left, or cpu down has been canceled or the cpu is being
3366 * brought back up. There shouldn't be any idle one left.
3367 * Tell the remaining busy ones to rebind once it finishes the
3368 * currently scheduled works by scheduling the rebind_work.
3369 */
3370 WARN_ON(!list_empty(&gcwq->idle_list));
3371
3372 for_each_busy_worker(worker, i, pos, gcwq) {
3373 struct work_struct *rebind_work = &worker->rebind_work;
3374
3375 /*
3376 * Rebind_work may race with future cpu hotplug
3377 * operations. Use a separate flag to mark that
3378 * rebinding is scheduled.
3379 */
Tejun Heocb444762010-07-02 10:03:50 +02003380 worker->flags |= WORKER_REBIND;
3381 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003382
3383 /* queue rebind_work, wq doesn't matter, use the default one */
3384 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3385 work_data_bits(rebind_work)))
3386 continue;
3387
3388 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003389 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003390 worker->scheduled.next,
3391 work_color_to_flags(WORK_NO_COLOR));
3392 }
3393
3394 /* relinquish manager role */
3395 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3396
Tejun Heodb7bccf2010-06-29 10:07:12 +02003397 /* notify completion */
3398 gcwq->trustee = NULL;
3399 gcwq->trustee_state = TRUSTEE_DONE;
3400 wake_up_all(&gcwq->trustee_wait);
3401 spin_unlock_irq(&gcwq->lock);
3402 return 0;
3403}
3404
3405/**
3406 * wait_trustee_state - wait for trustee to enter the specified state
3407 * @gcwq: gcwq the trustee of interest belongs to
3408 * @state: target state to wait for
3409 *
3410 * Wait for the trustee to reach @state. DONE is already matched.
3411 *
3412 * CONTEXT:
3413 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3414 * multiple times. To be used by cpu_callback.
3415 */
3416static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003417__releases(&gcwq->lock)
3418__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003419{
3420 if (!(gcwq->trustee_state == state ||
3421 gcwq->trustee_state == TRUSTEE_DONE)) {
3422 spin_unlock_irq(&gcwq->lock);
3423 __wait_event(gcwq->trustee_wait,
3424 gcwq->trustee_state == state ||
3425 gcwq->trustee_state == TRUSTEE_DONE);
3426 spin_lock_irq(&gcwq->lock);
3427 }
3428}
3429
Oleg Nesterov3af244332007-05-09 02:34:09 -07003430static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3431 unsigned long action,
3432 void *hcpu)
3433{
3434 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003435 struct global_cwq *gcwq = get_gcwq(cpu);
3436 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003437 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003438 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003440 action &= ~CPU_TASKS_FROZEN;
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003443 case CPU_DOWN_PREPARE:
3444 new_trustee = kthread_create(trustee_thread, gcwq,
3445 "workqueue_trustee/%d\n", cpu);
3446 if (IS_ERR(new_trustee))
3447 return notifier_from_errno(PTR_ERR(new_trustee));
3448 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003451 BUG_ON(gcwq->first_idle);
3452 new_worker = create_worker(gcwq, false);
3453 if (!new_worker) {
3454 if (new_trustee)
3455 kthread_stop(new_trustee);
3456 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
3459
Tejun Heodb7bccf2010-06-29 10:07:12 +02003460 /* some are called w/ irq disabled, don't disturb irq status */
3461 spin_lock_irqsave(&gcwq->lock, flags);
3462
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003463 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003464 case CPU_DOWN_PREPARE:
3465 /* initialize trustee and tell it to acquire the gcwq */
3466 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3467 gcwq->trustee = new_trustee;
3468 gcwq->trustee_state = TRUSTEE_START;
3469 wake_up_process(gcwq->trustee);
3470 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003471 /* fall through */
3472 case CPU_UP_PREPARE:
3473 BUG_ON(gcwq->first_idle);
3474 gcwq->first_idle = new_worker;
3475 break;
3476
3477 case CPU_DYING:
3478 /*
3479 * Before this, the trustee and all workers except for
3480 * the ones which are still executing works from
3481 * before the last CPU down must be on the cpu. After
3482 * this, they'll all be diasporas.
3483 */
3484 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003485 break;
3486
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003487 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003488 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003489 /* fall through */
3490 case CPU_UP_CANCELED:
3491 destroy_worker(gcwq->first_idle);
3492 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003493 break;
3494
3495 case CPU_DOWN_FAILED:
3496 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003497 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003498 if (gcwq->trustee_state != TRUSTEE_DONE) {
3499 gcwq->trustee_state = TRUSTEE_RELEASE;
3500 wake_up_process(gcwq->trustee);
3501 wait_trustee_state(gcwq, TRUSTEE_DONE);
3502 }
3503
Tejun Heoe22bee72010-06-29 10:07:14 +02003504 /*
3505 * Trustee is done and there might be no worker left.
3506 * Put the first_idle in and request a real manager to
3507 * take a look.
3508 */
3509 spin_unlock_irq(&gcwq->lock);
3510 kthread_bind(gcwq->first_idle->task, cpu);
3511 spin_lock_irq(&gcwq->lock);
3512 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3513 start_worker(gcwq->first_idle);
3514 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003515 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003516 }
3517
Tejun Heodb7bccf2010-06-29 10:07:12 +02003518 spin_unlock_irqrestore(&gcwq->lock, flags);
3519
Tejun Heo15376632010-06-29 10:07:11 +02003520 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Rusty Russell2d3854a2008-11-05 13:39:10 +11003523#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003524
Rusty Russell2d3854a2008-11-05 13:39:10 +11003525struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003526 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003527 long (*fn)(void *);
3528 void *arg;
3529 long ret;
3530};
3531
Andrew Morton6b440032009-04-09 09:50:37 -06003532static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003533{
Andrew Morton6b440032009-04-09 09:50:37 -06003534 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003535 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003536 complete(&wfc->completion);
3537 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003538}
3539
3540/**
3541 * work_on_cpu - run a function in user context on a particular cpu
3542 * @cpu: the cpu to run on
3543 * @fn: the function to run
3544 * @arg: the function arg
3545 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003546 * This will return the value @fn returns.
3547 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003548 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003549 */
3550long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3551{
Andrew Morton6b440032009-04-09 09:50:37 -06003552 struct task_struct *sub_thread;
3553 struct work_for_cpu wfc = {
3554 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3555 .fn = fn,
3556 .arg = arg,
3557 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003558
Andrew Morton6b440032009-04-09 09:50:37 -06003559 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3560 if (IS_ERR(sub_thread))
3561 return PTR_ERR(sub_thread);
3562 kthread_bind(sub_thread, cpu);
3563 wake_up_process(sub_thread);
3564 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003565 return wfc.ret;
3566}
3567EXPORT_SYMBOL_GPL(work_on_cpu);
3568#endif /* CONFIG_SMP */
3569
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003570#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303571
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003572/**
3573 * freeze_workqueues_begin - begin freezing workqueues
3574 *
3575 * Start freezing workqueues. After this function returns, all
3576 * freezeable workqueues will queue new works to their frozen_works
Tejun Heo7e116292010-06-29 10:07:13 +02003577 * list instead of gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003578 *
3579 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003580 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003581 */
3582void freeze_workqueues_begin(void)
3583{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003584 unsigned int cpu;
3585
3586 spin_lock(&workqueue_lock);
3587
3588 BUG_ON(workqueue_freezing);
3589 workqueue_freezing = true;
3590
Tejun Heof3421792010-07-02 10:03:51 +02003591 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003592 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003593 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003594
3595 spin_lock_irq(&gcwq->lock);
3596
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3598 gcwq->flags |= GCWQ_FREEZING;
3599
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003600 list_for_each_entry(wq, &workqueues, list) {
3601 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3602
Tejun Heof3421792010-07-02 10:03:51 +02003603 if (cwq && wq->flags & WQ_FREEZEABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003604 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003605 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003606
3607 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003608 }
3609
3610 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003612
3613/**
3614 * freeze_workqueues_busy - are freezeable workqueues still busy?
3615 *
3616 * Check whether freezing is complete. This function must be called
3617 * between freeze_workqueues_begin() and thaw_workqueues().
3618 *
3619 * CONTEXT:
3620 * Grabs and releases workqueue_lock.
3621 *
3622 * RETURNS:
3623 * %true if some freezeable workqueues are still busy. %false if
3624 * freezing is complete.
3625 */
3626bool freeze_workqueues_busy(void)
3627{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003628 unsigned int cpu;
3629 bool busy = false;
3630
3631 spin_lock(&workqueue_lock);
3632
3633 BUG_ON(!workqueue_freezing);
3634
Tejun Heof3421792010-07-02 10:03:51 +02003635 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003636 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003637 /*
3638 * nr_active is monotonically decreasing. It's safe
3639 * to peek without lock.
3640 */
3641 list_for_each_entry(wq, &workqueues, list) {
3642 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3643
Tejun Heof3421792010-07-02 10:03:51 +02003644 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003645 continue;
3646
3647 BUG_ON(cwq->nr_active < 0);
3648 if (cwq->nr_active) {
3649 busy = true;
3650 goto out_unlock;
3651 }
3652 }
3653 }
3654out_unlock:
3655 spin_unlock(&workqueue_lock);
3656 return busy;
3657}
3658
3659/**
3660 * thaw_workqueues - thaw workqueues
3661 *
3662 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003663 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003664 *
3665 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003666 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003667 */
3668void thaw_workqueues(void)
3669{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003670 unsigned int cpu;
3671
3672 spin_lock(&workqueue_lock);
3673
3674 if (!workqueue_freezing)
3675 goto out_unlock;
3676
Tejun Heof3421792010-07-02 10:03:51 +02003677 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003678 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003679 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003680
3681 spin_lock_irq(&gcwq->lock);
3682
Tejun Heodb7bccf2010-06-29 10:07:12 +02003683 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3684 gcwq->flags &= ~GCWQ_FREEZING;
3685
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003686 list_for_each_entry(wq, &workqueues, list) {
3687 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3688
Tejun Heof3421792010-07-02 10:03:51 +02003689 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003690 continue;
3691
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003692 /* restore max_active and repopulate worklist */
3693 cwq->max_active = wq->saved_max_active;
3694
3695 while (!list_empty(&cwq->delayed_works) &&
3696 cwq->nr_active < cwq->max_active)
3697 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003698 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003699
Tejun Heoe22bee72010-06-29 10:07:14 +02003700 wake_up_worker(gcwq);
3701
Tejun Heo8b03ae32010-06-29 10:07:12 +02003702 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 }
3704
3705 workqueue_freezing = false;
3706out_unlock:
3707 spin_unlock(&workqueue_lock);
3708}
3709#endif /* CONFIG_FREEZER */
3710
Suresh Siddha6ee05782010-07-30 14:57:37 -07003711static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Tejun Heoc34056a2010-06-29 10:07:11 +02003713 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003714 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003715
Tejun Heof6500942010-08-09 11:50:34 +02003716 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003717
3718 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003719 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003720 struct global_cwq *gcwq = get_gcwq(cpu);
3721
3722 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003723 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003724 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003725 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003726
Tejun Heoc8e55f32010-06-29 10:07:12 +02003727 INIT_LIST_HEAD(&gcwq->idle_list);
3728 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3729 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3730
Tejun Heoe22bee72010-06-29 10:07:14 +02003731 init_timer_deferrable(&gcwq->idle_timer);
3732 gcwq->idle_timer.function = idle_worker_timeout;
3733 gcwq->idle_timer.data = (unsigned long)gcwq;
3734
3735 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3736 (unsigned long)gcwq);
3737
Tejun Heo8b03ae32010-06-29 10:07:12 +02003738 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003739
3740 gcwq->trustee_state = TRUSTEE_DONE;
3741 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003742 }
3743
Tejun Heoe22bee72010-06-29 10:07:14 +02003744 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003745 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003746 struct global_cwq *gcwq = get_gcwq(cpu);
3747 struct worker *worker;
3748
Tejun Heo477a3c32010-08-31 10:54:35 +02003749 if (cpu != WORK_CPU_UNBOUND)
3750 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003751 worker = create_worker(gcwq, true);
3752 BUG_ON(!worker);
3753 spin_lock_irq(&gcwq->lock);
3754 start_worker(worker);
3755 spin_unlock_irq(&gcwq->lock);
3756 }
3757
Tejun Heod320c032010-06-29 10:07:14 +02003758 system_wq = alloc_workqueue("events", 0, 0);
3759 system_long_wq = alloc_workqueue("events_long", 0, 0);
3760 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003761 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3762 WQ_UNBOUND_MAX_ACTIVE);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003763 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
3764 !system_unbound_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003767early_initcall(init_workqueues);