blob: 88a3e34f51f6e881e15d004aeedd9144bfff7e0b [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
Tejun Heo42c025f2011-01-11 15:58:49 +0100771 /*
772 * If transitioning out of NOT_RUNNING, increment nr_running. Note
773 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
774 * of multiple flags, not a single flag.
775 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200776 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
777 if (!(worker->flags & WORKER_NOT_RUNNING))
778 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200779}
780
781/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200782 * busy_worker_head - return the busy hash head for a work
783 * @gcwq: gcwq of interest
784 * @work: work to be hashed
785 *
786 * Return hash head of @gcwq for @work.
787 *
788 * CONTEXT:
789 * spin_lock_irq(gcwq->lock).
790 *
791 * RETURNS:
792 * Pointer to the hash head.
793 */
794static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
795 struct work_struct *work)
796{
797 const int base_shift = ilog2(sizeof(struct work_struct));
798 unsigned long v = (unsigned long)work;
799
800 /* simple shift and fold hash, do we need something better? */
801 v >>= base_shift;
802 v += v >> BUSY_WORKER_HASH_ORDER;
803 v &= BUSY_WORKER_HASH_MASK;
804
805 return &gcwq->busy_hash[v];
806}
807
808/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200809 * __find_worker_executing_work - find worker which is executing a work
810 * @gcwq: gcwq of interest
811 * @bwh: hash head as returned by busy_worker_head()
812 * @work: work to find worker for
813 *
814 * Find a worker which is executing @work on @gcwq. @bwh should be
815 * the hash head obtained by calling busy_worker_head() with the same
816 * work.
817 *
818 * CONTEXT:
819 * spin_lock_irq(gcwq->lock).
820 *
821 * RETURNS:
822 * Pointer to worker which is executing @work if found, NULL
823 * otherwise.
824 */
825static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
826 struct hlist_head *bwh,
827 struct work_struct *work)
828{
829 struct worker *worker;
830 struct hlist_node *tmp;
831
832 hlist_for_each_entry(worker, tmp, bwh, hentry)
833 if (worker->current_work == work)
834 return worker;
835 return NULL;
836}
837
838/**
839 * find_worker_executing_work - find worker which is executing a work
840 * @gcwq: gcwq of interest
841 * @work: work to find worker for
842 *
843 * Find a worker which is executing @work on @gcwq. This function is
844 * identical to __find_worker_executing_work() except that this
845 * function calculates @bwh itself.
846 *
847 * CONTEXT:
848 * spin_lock_irq(gcwq->lock).
849 *
850 * RETURNS:
851 * Pointer to worker which is executing @work if found, NULL
852 * otherwise.
853 */
854static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
855 struct work_struct *work)
856{
857 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
858 work);
859}
860
861/**
Tejun Heo649027d2010-06-29 10:07:14 +0200862 * gcwq_determine_ins_pos - find insertion position
863 * @gcwq: gcwq of interest
864 * @cwq: cwq a work is being queued for
865 *
866 * A work for @cwq is about to be queued on @gcwq, determine insertion
867 * position for the work. If @cwq is for HIGHPRI wq, the work is
868 * queued at the head of the queue but in FIFO order with respect to
869 * other HIGHPRI works; otherwise, at the end of the queue. This
870 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
871 * there are HIGHPRI works pending.
872 *
873 * CONTEXT:
874 * spin_lock_irq(gcwq->lock).
875 *
876 * RETURNS:
877 * Pointer to inserstion position.
878 */
879static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
880 struct cpu_workqueue_struct *cwq)
881{
882 struct work_struct *twork;
883
884 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
885 return &gcwq->worklist;
886
887 list_for_each_entry(twork, &gcwq->worklist, entry) {
888 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
889
890 if (!(tcwq->wq->flags & WQ_HIGHPRI))
891 break;
892 }
893
894 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
895 return &twork->entry;
896}
897
898/**
Tejun Heo7e116292010-06-29 10:07:13 +0200899 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200900 * @cwq: cwq @work belongs to
901 * @work: work to insert
902 * @head: insertion point
903 * @extra_flags: extra WORK_STRUCT_* flags to set
904 *
Tejun Heo7e116292010-06-29 10:07:13 +0200905 * Insert @work which belongs to @cwq into @gcwq after @head.
906 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200907 *
908 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200909 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200910 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700911static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200912 struct work_struct *work, struct list_head *head,
913 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700914{
Tejun Heoe22bee72010-06-29 10:07:14 +0200915 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100916
Tejun Heo4690c4a2010-06-29 10:07:10 +0200917 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200918 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200919
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700920 /*
921 * Ensure that we get the right work->data if we see the
922 * result of list_add() below, see try_to_grab_pending().
923 */
924 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200925
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700926 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200927
928 /*
929 * Ensure either worker_sched_deactivated() sees the above
930 * list_add_tail() or we see zero nr_running to avoid workers
931 * lying around lazily while there are works to be processed.
932 */
933 smp_mb();
934
Tejun Heo649027d2010-06-29 10:07:14 +0200935 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200936 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700937}
938
Tejun Heoc8efcc22010-12-20 19:32:04 +0100939/*
940 * Test whether @work is being queued from another work executing on the
941 * same workqueue. This is rather expensive and should only be used from
942 * cold paths.
943 */
944static bool is_chained_work(struct workqueue_struct *wq)
945{
946 unsigned long flags;
947 unsigned int cpu;
948
949 for_each_gcwq_cpu(cpu) {
950 struct global_cwq *gcwq = get_gcwq(cpu);
951 struct worker *worker;
952 struct hlist_node *pos;
953 int i;
954
955 spin_lock_irqsave(&gcwq->lock, flags);
956 for_each_busy_worker(worker, i, pos, gcwq) {
957 if (worker->task != current)
958 continue;
959 spin_unlock_irqrestore(&gcwq->lock, flags);
960 /*
961 * I'm @worker, no locking necessary. See if @work
962 * is headed to the same workqueue.
963 */
964 return worker->current_cwq->wq == wq;
965 }
966 spin_unlock_irqrestore(&gcwq->lock, flags);
967 }
968 return false;
969}
970
Tejun Heo4690c4a2010-06-29 10:07:10 +0200971static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct work_struct *work)
973{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200974 struct global_cwq *gcwq;
975 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200976 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200977 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned long flags;
979
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900980 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200981
Tejun Heoc8efcc22010-12-20 19:32:04 +0100982 /* if dying, only works from the same workqueue are allowed */
983 if (unlikely(wq->flags & WQ_DYING) &&
984 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200985 return;
986
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200987 /* determine gcwq to use */
988 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200989 struct global_cwq *last_gcwq;
990
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200991 if (unlikely(cpu == WORK_CPU_UNBOUND))
992 cpu = raw_smp_processor_id();
993
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200994 /*
995 * It's multi cpu. If @wq is non-reentrant and @work
996 * was previously on a different cpu, it might still
997 * be running there, in which case the work needs to
998 * be queued on that cpu to guarantee non-reentrance.
999 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001000 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001001 if (wq->flags & WQ_NON_REENTRANT &&
1002 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1003 struct worker *worker;
1004
1005 spin_lock_irqsave(&last_gcwq->lock, flags);
1006
1007 worker = find_worker_executing_work(last_gcwq, work);
1008
1009 if (worker && worker->current_cwq->wq == wq)
1010 gcwq = last_gcwq;
1011 else {
1012 /* meh... not running there, queue here */
1013 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1014 spin_lock_irqsave(&gcwq->lock, flags);
1015 }
1016 } else
1017 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001018 } else {
1019 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1020 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001021 }
1022
1023 /* gcwq determined, get cwq and queue */
1024 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001025 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001026
Tejun Heo4690c4a2010-06-29 10:07:10 +02001027 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001028
Tejun Heo73f53c42010-06-29 10:07:11 +02001029 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001030 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001031
1032 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001033 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001034 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001035 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001036 } else {
1037 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001038 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001039 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001040
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001041 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001042
Tejun Heo8b03ae32010-06-29 10:07:12 +02001043 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001046/**
1047 * queue_work - queue work on a workqueue
1048 * @wq: workqueue to use
1049 * @work: work to queue
1050 *
Alan Stern057647f2006-10-28 10:38:58 -07001051 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001053 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1054 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001056int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001058 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001060 ret = queue_work_on(get_cpu(), wq, work);
1061 put_cpu();
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return ret;
1064}
Dave Jonesae90dd52006-06-30 01:40:45 -04001065EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Zhang Ruic1a220e2008-07-23 21:28:39 -07001067/**
1068 * queue_work_on - queue work on specific cpu
1069 * @cpu: CPU number to execute work on
1070 * @wq: workqueue to use
1071 * @work: work to queue
1072 *
1073 * Returns 0 if @work was already on a queue, non-zero otherwise.
1074 *
1075 * We queue the work to a specific CPU, the caller must ensure it
1076 * can't go away.
1077 */
1078int
1079queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1080{
1081 int ret = 0;
1082
Tejun Heo22df02b2010-06-29 10:07:10 +02001083 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001084 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001085 ret = 1;
1086 }
1087 return ret;
1088}
1089EXPORT_SYMBOL_GPL(queue_work_on);
1090
Li Zefan6d141c32008-02-08 04:21:09 -08001091static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
David Howells52bad642006-11-22 14:54:01 +00001093 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001094 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Tejun Heo4690c4a2010-06-29 10:07:10 +02001096 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001099/**
1100 * queue_delayed_work - queue work on a workqueue after delay
1101 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001102 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001103 * @delay: number of jiffies to wait before queueing
1104 *
Alan Stern057647f2006-10-28 10:38:58 -07001105 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001106 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001107int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001108 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
David Howells52bad642006-11-22 14:54:01 +00001110 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001111 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001113 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
Dave Jonesae90dd52006-06-30 01:40:45 -04001115EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001117/**
1118 * queue_delayed_work_on - queue work on specific CPU after delay
1119 * @cpu: CPU number to execute work on
1120 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001121 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001122 * @delay: number of jiffies to wait before queueing
1123 *
Alan Stern057647f2006-10-28 10:38:58 -07001124 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001125 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001126int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001127 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001128{
1129 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001130 struct timer_list *timer = &dwork->timer;
1131 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001132
Tejun Heo22df02b2010-06-29 10:07:10 +02001133 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001134 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001135
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001136 BUG_ON(timer_pending(timer));
1137 BUG_ON(!list_empty(&work->entry));
1138
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001139 timer_stats_timer_set_start_info(&dwork->timer);
1140
Tejun Heo7a22ad72010-06-29 10:07:13 +02001141 /*
1142 * This stores cwq for the moment, for the timer_fn.
1143 * Note that the work's gcwq is preserved to allow
1144 * reentrance detection for delayed works.
1145 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001146 if (!(wq->flags & WQ_UNBOUND)) {
1147 struct global_cwq *gcwq = get_work_gcwq(work);
1148
1149 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1150 lcpu = gcwq->cpu;
1151 else
1152 lcpu = raw_smp_processor_id();
1153 } else
1154 lcpu = WORK_CPU_UNBOUND;
1155
Tejun Heo7a22ad72010-06-29 10:07:13 +02001156 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001157
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001158 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001159 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001160 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001161
1162 if (unlikely(cpu >= 0))
1163 add_timer_on(timer, cpu);
1164 else
1165 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001166 ret = 1;
1167 }
1168 return ret;
1169}
Dave Jonesae90dd52006-06-30 01:40:45 -04001170EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Tejun Heoc8e55f32010-06-29 10:07:12 +02001172/**
1173 * worker_enter_idle - enter idle state
1174 * @worker: worker which is entering idle state
1175 *
1176 * @worker is entering idle state. Update stats and idle timer if
1177 * necessary.
1178 *
1179 * LOCKING:
1180 * spin_lock_irq(gcwq->lock).
1181 */
1182static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001184 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Tejun Heoc8e55f32010-06-29 10:07:12 +02001186 BUG_ON(worker->flags & WORKER_IDLE);
1187 BUG_ON(!list_empty(&worker->entry) &&
1188 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Tejun Heocb444762010-07-02 10:03:50 +02001190 /* can't use worker_set_flags(), also called from start_worker() */
1191 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001192 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001193 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001194
Tejun Heoc8e55f32010-06-29 10:07:12 +02001195 /* idle_list is LIFO */
1196 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001197
Tejun Heoe22bee72010-06-29 10:07:14 +02001198 if (likely(!(worker->flags & WORKER_ROGUE))) {
1199 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1200 mod_timer(&gcwq->idle_timer,
1201 jiffies + IDLE_WORKER_TIMEOUT);
1202 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001203 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001204
1205 /* sanity check nr_running */
1206 WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
1207 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Tejun Heoc8e55f32010-06-29 10:07:12 +02001210/**
1211 * worker_leave_idle - leave idle state
1212 * @worker: worker which is leaving idle state
1213 *
1214 * @worker is leaving idle state. Update stats.
1215 *
1216 * LOCKING:
1217 * spin_lock_irq(gcwq->lock).
1218 */
1219static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001221 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Tejun Heoc8e55f32010-06-29 10:07:12 +02001223 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001224 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001225 gcwq->nr_idle--;
1226 list_del_init(&worker->entry);
1227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Tejun Heoe22bee72010-06-29 10:07:14 +02001229/**
1230 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1231 * @worker: self
1232 *
1233 * Works which are scheduled while the cpu is online must at least be
1234 * scheduled to a worker which is bound to the cpu so that if they are
1235 * flushed from cpu callbacks while cpu is going down, they are
1236 * guaranteed to execute on the cpu.
1237 *
1238 * This function is to be used by rogue workers and rescuers to bind
1239 * themselves to the target cpu and may race with cpu going down or
1240 * coming online. kthread_bind() can't be used because it may put the
1241 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1242 * verbatim as it's best effort and blocking and gcwq may be
1243 * [dis]associated in the meantime.
1244 *
1245 * This function tries set_cpus_allowed() and locks gcwq and verifies
1246 * the binding against GCWQ_DISASSOCIATED which is set during
1247 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1248 * idle state or fetches works without dropping lock, it can guarantee
1249 * the scheduling requirement described in the first paragraph.
1250 *
1251 * CONTEXT:
1252 * Might sleep. Called without any lock but returns with gcwq->lock
1253 * held.
1254 *
1255 * RETURNS:
1256 * %true if the associated gcwq is online (@worker is successfully
1257 * bound), %false if offline.
1258 */
1259static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001260__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001261{
1262 struct global_cwq *gcwq = worker->gcwq;
1263 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Tejun Heoe22bee72010-06-29 10:07:14 +02001265 while (true) {
1266 /*
1267 * The following call may fail, succeed or succeed
1268 * without actually migrating the task to the cpu if
1269 * it races with cpu hotunplug operation. Verify
1270 * against GCWQ_DISASSOCIATED.
1271 */
Tejun Heof3421792010-07-02 10:03:51 +02001272 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1273 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001274
Tejun Heoe22bee72010-06-29 10:07:14 +02001275 spin_lock_irq(&gcwq->lock);
1276 if (gcwq->flags & GCWQ_DISASSOCIATED)
1277 return false;
1278 if (task_cpu(task) == gcwq->cpu &&
1279 cpumask_equal(&current->cpus_allowed,
1280 get_cpu_mask(gcwq->cpu)))
1281 return true;
1282 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001283
Tejun Heoe22bee72010-06-29 10:07:14 +02001284 /* CPU has come up inbetween, retry migration */
1285 cpu_relax();
1286 }
1287}
1288
1289/*
1290 * Function for worker->rebind_work used to rebind rogue busy workers
1291 * to the associated cpu which is coming back online. This is
1292 * scheduled by cpu up but can race with other cpu hotplug operations
1293 * and may be executed twice without intervening cpu down.
1294 */
1295static void worker_rebind_fn(struct work_struct *work)
1296{
1297 struct worker *worker = container_of(work, struct worker, rebind_work);
1298 struct global_cwq *gcwq = worker->gcwq;
1299
1300 if (worker_maybe_bind_and_lock(worker))
1301 worker_clr_flags(worker, WORKER_REBIND);
1302
1303 spin_unlock_irq(&gcwq->lock);
1304}
1305
Tejun Heoc34056a2010-06-29 10:07:11 +02001306static struct worker *alloc_worker(void)
1307{
1308 struct worker *worker;
1309
1310 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001311 if (worker) {
1312 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001313 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001314 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1315 /* on creation a worker is in !idle && prep state */
1316 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001317 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001318 return worker;
1319}
1320
1321/**
1322 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001323 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001324 * @bind: whether to set affinity to @cpu or not
1325 *
Tejun Heo7e116292010-06-29 10:07:13 +02001326 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001327 * can be started by calling start_worker() or destroyed using
1328 * destroy_worker().
1329 *
1330 * CONTEXT:
1331 * Might sleep. Does GFP_KERNEL allocations.
1332 *
1333 * RETURNS:
1334 * Pointer to the newly created worker.
1335 */
Tejun Heo7e116292010-06-29 10:07:13 +02001336static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001337{
Tejun Heof3421792010-07-02 10:03:51 +02001338 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001339 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001340 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001341
Tejun Heo8b03ae32010-06-29 10:07:12 +02001342 spin_lock_irq(&gcwq->lock);
1343 while (ida_get_new(&gcwq->worker_ida, &id)) {
1344 spin_unlock_irq(&gcwq->lock);
1345 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001346 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001347 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001348 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001349 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001350
1351 worker = alloc_worker();
1352 if (!worker)
1353 goto fail;
1354
Tejun Heo8b03ae32010-06-29 10:07:12 +02001355 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001356 worker->id = id;
1357
Tejun Heof3421792010-07-02 10:03:51 +02001358 if (!on_unbound_cpu)
1359 worker->task = kthread_create(worker_thread, worker,
1360 "kworker/%u:%d", gcwq->cpu, id);
1361 else
1362 worker->task = kthread_create(worker_thread, worker,
1363 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001364 if (IS_ERR(worker->task))
1365 goto fail;
1366
Tejun Heodb7bccf2010-06-29 10:07:12 +02001367 /*
1368 * A rogue worker will become a regular one if CPU comes
1369 * online later on. Make sure every worker has
1370 * PF_THREAD_BOUND set.
1371 */
Tejun Heof3421792010-07-02 10:03:51 +02001372 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001373 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001374 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001375 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001376 if (on_unbound_cpu)
1377 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001379
Tejun Heoc34056a2010-06-29 10:07:11 +02001380 return worker;
1381fail:
1382 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001383 spin_lock_irq(&gcwq->lock);
1384 ida_remove(&gcwq->worker_ida, id);
1385 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001386 }
1387 kfree(worker);
1388 return NULL;
1389}
1390
1391/**
1392 * start_worker - start a newly created worker
1393 * @worker: worker to start
1394 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001395 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001396 *
1397 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001398 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001399 */
1400static void start_worker(struct worker *worker)
1401{
Tejun Heocb444762010-07-02 10:03:50 +02001402 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001403 worker->gcwq->nr_workers++;
1404 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001405 wake_up_process(worker->task);
1406}
1407
1408/**
1409 * destroy_worker - destroy a workqueue worker
1410 * @worker: worker to be destroyed
1411 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001412 * Destroy @worker and adjust @gcwq stats accordingly.
1413 *
1414 * CONTEXT:
1415 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001416 */
1417static void destroy_worker(struct worker *worker)
1418{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001419 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001420 int id = worker->id;
1421
1422 /* sanity check frenzy */
1423 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001424 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001425
Tejun Heoc8e55f32010-06-29 10:07:12 +02001426 if (worker->flags & WORKER_STARTED)
1427 gcwq->nr_workers--;
1428 if (worker->flags & WORKER_IDLE)
1429 gcwq->nr_idle--;
1430
1431 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001432 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001433
1434 spin_unlock_irq(&gcwq->lock);
1435
Tejun Heoc34056a2010-06-29 10:07:11 +02001436 kthread_stop(worker->task);
1437 kfree(worker);
1438
Tejun Heo8b03ae32010-06-29 10:07:12 +02001439 spin_lock_irq(&gcwq->lock);
1440 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001441}
1442
Tejun Heoe22bee72010-06-29 10:07:14 +02001443static void idle_worker_timeout(unsigned long __gcwq)
1444{
1445 struct global_cwq *gcwq = (void *)__gcwq;
1446
1447 spin_lock_irq(&gcwq->lock);
1448
1449 if (too_many_workers(gcwq)) {
1450 struct worker *worker;
1451 unsigned long expires;
1452
1453 /* idle_list is kept in LIFO order, check the last one */
1454 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1455 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1456
1457 if (time_before(jiffies, expires))
1458 mod_timer(&gcwq->idle_timer, expires);
1459 else {
1460 /* it's been idle for too long, wake up manager */
1461 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1462 wake_up_worker(gcwq);
1463 }
1464 }
1465
1466 spin_unlock_irq(&gcwq->lock);
1467}
1468
1469static bool send_mayday(struct work_struct *work)
1470{
1471 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1472 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001473 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001474
1475 if (!(wq->flags & WQ_RESCUER))
1476 return false;
1477
1478 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001479 cpu = cwq->gcwq->cpu;
1480 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1481 if (cpu == WORK_CPU_UNBOUND)
1482 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001483 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001484 wake_up_process(wq->rescuer->task);
1485 return true;
1486}
1487
1488static void gcwq_mayday_timeout(unsigned long __gcwq)
1489{
1490 struct global_cwq *gcwq = (void *)__gcwq;
1491 struct work_struct *work;
1492
1493 spin_lock_irq(&gcwq->lock);
1494
1495 if (need_to_create_worker(gcwq)) {
1496 /*
1497 * We've been trying to create a new worker but
1498 * haven't been successful. We might be hitting an
1499 * allocation deadlock. Send distress signals to
1500 * rescuers.
1501 */
1502 list_for_each_entry(work, &gcwq->worklist, entry)
1503 send_mayday(work);
1504 }
1505
1506 spin_unlock_irq(&gcwq->lock);
1507
1508 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1509}
1510
1511/**
1512 * maybe_create_worker - create a new worker if necessary
1513 * @gcwq: gcwq to create a new worker for
1514 *
1515 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1516 * have at least one idle worker on return from this function. If
1517 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1518 * sent to all rescuers with works scheduled on @gcwq to resolve
1519 * possible allocation deadlock.
1520 *
1521 * On return, need_to_create_worker() is guaranteed to be false and
1522 * may_start_working() true.
1523 *
1524 * LOCKING:
1525 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1526 * multiple times. Does GFP_KERNEL allocations. Called only from
1527 * manager.
1528 *
1529 * RETURNS:
1530 * false if no action was taken and gcwq->lock stayed locked, true
1531 * otherwise.
1532 */
1533static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001534__releases(&gcwq->lock)
1535__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001536{
1537 if (!need_to_create_worker(gcwq))
1538 return false;
1539restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001540 spin_unlock_irq(&gcwq->lock);
1541
Tejun Heoe22bee72010-06-29 10:07:14 +02001542 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1543 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1544
1545 while (true) {
1546 struct worker *worker;
1547
Tejun Heoe22bee72010-06-29 10:07:14 +02001548 worker = create_worker(gcwq, true);
1549 if (worker) {
1550 del_timer_sync(&gcwq->mayday_timer);
1551 spin_lock_irq(&gcwq->lock);
1552 start_worker(worker);
1553 BUG_ON(need_to_create_worker(gcwq));
1554 return true;
1555 }
1556
1557 if (!need_to_create_worker(gcwq))
1558 break;
1559
Tejun Heoe22bee72010-06-29 10:07:14 +02001560 __set_current_state(TASK_INTERRUPTIBLE);
1561 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001562
Tejun Heoe22bee72010-06-29 10:07:14 +02001563 if (!need_to_create_worker(gcwq))
1564 break;
1565 }
1566
Tejun Heoe22bee72010-06-29 10:07:14 +02001567 del_timer_sync(&gcwq->mayday_timer);
1568 spin_lock_irq(&gcwq->lock);
1569 if (need_to_create_worker(gcwq))
1570 goto restart;
1571 return true;
1572}
1573
1574/**
1575 * maybe_destroy_worker - destroy workers which have been idle for a while
1576 * @gcwq: gcwq to destroy workers for
1577 *
1578 * Destroy @gcwq workers which have been idle for longer than
1579 * IDLE_WORKER_TIMEOUT.
1580 *
1581 * LOCKING:
1582 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1583 * multiple times. Called only from manager.
1584 *
1585 * RETURNS:
1586 * false if no action was taken and gcwq->lock stayed locked, true
1587 * otherwise.
1588 */
1589static bool maybe_destroy_workers(struct global_cwq *gcwq)
1590{
1591 bool ret = false;
1592
1593 while (too_many_workers(gcwq)) {
1594 struct worker *worker;
1595 unsigned long expires;
1596
1597 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1598 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1599
1600 if (time_before(jiffies, expires)) {
1601 mod_timer(&gcwq->idle_timer, expires);
1602 break;
1603 }
1604
1605 destroy_worker(worker);
1606 ret = true;
1607 }
1608
1609 return ret;
1610}
1611
1612/**
1613 * manage_workers - manage worker pool
1614 * @worker: self
1615 *
1616 * Assume the manager role and manage gcwq worker pool @worker belongs
1617 * to. At any given time, there can be only zero or one manager per
1618 * gcwq. The exclusion is handled automatically by this function.
1619 *
1620 * The caller can safely start processing works on false return. On
1621 * true return, it's guaranteed that need_to_create_worker() is false
1622 * and may_start_working() is true.
1623 *
1624 * CONTEXT:
1625 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1626 * multiple times. Does GFP_KERNEL allocations.
1627 *
1628 * RETURNS:
1629 * false if no action was taken and gcwq->lock stayed locked, true if
1630 * some action was taken.
1631 */
1632static bool manage_workers(struct worker *worker)
1633{
1634 struct global_cwq *gcwq = worker->gcwq;
1635 bool ret = false;
1636
1637 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1638 return ret;
1639
1640 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1641 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1642
1643 /*
1644 * Destroy and then create so that may_start_working() is true
1645 * on return.
1646 */
1647 ret |= maybe_destroy_workers(gcwq);
1648 ret |= maybe_create_worker(gcwq);
1649
1650 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1651
1652 /*
1653 * The trustee might be waiting to take over the manager
1654 * position, tell it we're done.
1655 */
1656 if (unlikely(gcwq->trustee))
1657 wake_up_all(&gcwq->trustee_wait);
1658
1659 return ret;
1660}
1661
Tejun Heoa62428c2010-06-29 10:07:10 +02001662/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001663 * move_linked_works - move linked works to a list
1664 * @work: start of series of works to be scheduled
1665 * @head: target list to append @work to
1666 * @nextp: out paramter for nested worklist walking
1667 *
1668 * Schedule linked works starting from @work to @head. Work series to
1669 * be scheduled starts at @work and includes any consecutive work with
1670 * WORK_STRUCT_LINKED set in its predecessor.
1671 *
1672 * If @nextp is not NULL, it's updated to point to the next work of
1673 * the last scheduled work. This allows move_linked_works() to be
1674 * nested inside outer list_for_each_entry_safe().
1675 *
1676 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001677 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001678 */
1679static void move_linked_works(struct work_struct *work, struct list_head *head,
1680 struct work_struct **nextp)
1681{
1682 struct work_struct *n;
1683
1684 /*
1685 * Linked worklist will always end before the end of the list,
1686 * use NULL for list head.
1687 */
1688 list_for_each_entry_safe_from(work, n, NULL, entry) {
1689 list_move_tail(&work->entry, head);
1690 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1691 break;
1692 }
1693
1694 /*
1695 * If we're already inside safe list traversal and have moved
1696 * multiple works to the scheduled queue, the next position
1697 * needs to be updated.
1698 */
1699 if (nextp)
1700 *nextp = n;
1701}
1702
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001703static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1704{
1705 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1706 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001707 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001708
Tejun Heocdadf002010-10-05 10:49:55 +02001709 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001710 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001711 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001712 cwq->nr_active++;
1713}
1714
Tejun Heoaffee4b2010-06-29 10:07:12 +02001715/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001716 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1717 * @cwq: cwq of interest
1718 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001719 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001720 *
1721 * A work either has completed or is removed from pending queue,
1722 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1723 *
1724 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001725 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001726 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001727static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1728 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001729{
1730 /* ignore uncolored works */
1731 if (color == WORK_NO_COLOR)
1732 return;
1733
1734 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001735
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001736 if (!delayed) {
1737 cwq->nr_active--;
1738 if (!list_empty(&cwq->delayed_works)) {
1739 /* one down, submit a delayed one */
1740 if (cwq->nr_active < cwq->max_active)
1741 cwq_activate_first_delayed(cwq);
1742 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001743 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001744
1745 /* is flush in progress and are we at the flushing tip? */
1746 if (likely(cwq->flush_color != color))
1747 return;
1748
1749 /* are there still in-flight works? */
1750 if (cwq->nr_in_flight[color])
1751 return;
1752
1753 /* this cwq is done, clear flush_color */
1754 cwq->flush_color = -1;
1755
1756 /*
1757 * If this was the last cwq, wake up the first flusher. It
1758 * will handle the rest.
1759 */
1760 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1761 complete(&cwq->wq->first_flusher->done);
1762}
1763
1764/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001765 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001766 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001767 * @work: work to process
1768 *
1769 * Process @work. This function contains all the logics necessary to
1770 * process a single work including synchronization against and
1771 * interaction with other workers on the same cpu, queueing and
1772 * flushing. As long as context requirement is met, any worker can
1773 * call this function to process a work.
1774 *
1775 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001776 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001777 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001778static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001779__releases(&gcwq->lock)
1780__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001781{
Tejun Heo7e116292010-06-29 10:07:13 +02001782 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001783 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001784 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001785 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001786 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001787 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001788 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001789#ifdef CONFIG_LOCKDEP
1790 /*
1791 * It is permissible to free the struct work_struct from
1792 * inside the function that is called from it, this we need to
1793 * take into account for lockdep too. To avoid bogus "held
1794 * lock freed" warnings as well as problems when looking into
1795 * work->lockdep_map, make a copy and use that here.
1796 */
1797 struct lockdep_map lockdep_map = work->lockdep_map;
1798#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001799 /*
1800 * A single work shouldn't be executed concurrently by
1801 * multiple workers on a single cpu. Check whether anyone is
1802 * already processing the work. If so, defer the work to the
1803 * currently executing one.
1804 */
1805 collision = __find_worker_executing_work(gcwq, bwh, work);
1806 if (unlikely(collision)) {
1807 move_linked_works(work, &collision->scheduled, NULL);
1808 return;
1809 }
1810
Tejun Heoa62428c2010-06-29 10:07:10 +02001811 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001812 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001813 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001814 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001815 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001816 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001817
Tejun Heo7a22ad72010-06-29 10:07:13 +02001818 /* record the current cpu number in the work data and dequeue */
1819 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001820 list_del_init(&work->entry);
1821
Tejun Heo649027d2010-06-29 10:07:14 +02001822 /*
1823 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1824 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1825 */
1826 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1827 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1828 struct work_struct, entry);
1829
1830 if (!list_empty(&gcwq->worklist) &&
1831 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1832 wake_up_worker(gcwq);
1833 else
1834 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1835 }
1836
Tejun Heofb0e7be2010-06-29 10:07:15 +02001837 /*
1838 * CPU intensive works don't participate in concurrency
1839 * management. They're the scheduler's responsibility.
1840 */
1841 if (unlikely(cpu_intensive))
1842 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1843
Tejun Heo8b03ae32010-06-29 10:07:12 +02001844 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001845
Tejun Heoa62428c2010-06-29 10:07:10 +02001846 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001847 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001848 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001849 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001850 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001851 /*
1852 * While we must be careful to not use "work" after this, the trace
1853 * point will only record its address.
1854 */
1855 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001856 lock_map_release(&lockdep_map);
1857 lock_map_release(&cwq->wq->lockdep_map);
1858
1859 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1860 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1861 "%s/0x%08x/%d\n",
1862 current->comm, preempt_count(), task_pid_nr(current));
1863 printk(KERN_ERR " last function: ");
1864 print_symbol("%s\n", (unsigned long)f);
1865 debug_show_held_locks(current);
1866 dump_stack();
1867 }
1868
Tejun Heo8b03ae32010-06-29 10:07:12 +02001869 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001870
Tejun Heofb0e7be2010-06-29 10:07:15 +02001871 /* clear cpu intensive status */
1872 if (unlikely(cpu_intensive))
1873 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1874
Tejun Heoa62428c2010-06-29 10:07:10 +02001875 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001876 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001877 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001878 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001879 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001880}
1881
Tejun Heoaffee4b2010-06-29 10:07:12 +02001882/**
1883 * process_scheduled_works - process scheduled works
1884 * @worker: self
1885 *
1886 * Process all scheduled works. Please note that the scheduled list
1887 * may change while processing a work, so this function repeatedly
1888 * fetches a work from the top and executes it.
1889 *
1890 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001891 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001892 * multiple times.
1893 */
1894static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001896 while (!list_empty(&worker->scheduled)) {
1897 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001899 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Tejun Heo4690c4a2010-06-29 10:07:10 +02001903/**
1904 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001905 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001906 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001907 * The gcwq worker thread function. There's a single dynamic pool of
1908 * these per each cpu. These workers process all works regardless of
1909 * their specific target workqueue. The only exception is works which
1910 * belong to workqueues with a rescuer which will be explained in
1911 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001912 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001913static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Tejun Heoc34056a2010-06-29 10:07:11 +02001915 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001916 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Tejun Heoe22bee72010-06-29 10:07:14 +02001918 /* tell the scheduler that this is a workqueue worker */
1919 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001920woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001921 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Tejun Heoc8e55f32010-06-29 10:07:12 +02001923 /* DIE can be set only while we're idle, checking here is enough */
1924 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001925 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001926 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929
Tejun Heoc8e55f32010-06-29 10:07:12 +02001930 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001931recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 /* no more worker necessary? */
1933 if (!need_more_worker(gcwq))
1934 goto sleep;
1935
1936 /* do we need to manage? */
1937 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1938 goto recheck;
1939
Tejun Heoc8e55f32010-06-29 10:07:12 +02001940 /*
1941 * ->scheduled list can only be filled while a worker is
1942 * preparing to process a work or actually processing it.
1943 * Make sure nobody diddled with it while I was sleeping.
1944 */
1945 BUG_ON(!list_empty(&worker->scheduled));
1946
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 /*
1948 * When control reaches this point, we're guaranteed to have
1949 * at least one idle worker or that someone else has already
1950 * assumed the manager role.
1951 */
1952 worker_clr_flags(worker, WORKER_PREP);
1953
1954 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001955 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02001956 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001957 struct work_struct, entry);
1958
1959 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1960 /* optimization path, not strictly necessary */
1961 process_one_work(worker, work);
1962 if (unlikely(!list_empty(&worker->scheduled)))
1963 process_scheduled_works(worker);
1964 } else {
1965 move_linked_works(work, &worker->scheduled, NULL);
1966 process_scheduled_works(worker);
1967 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001969
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001971sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
1973 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001974
Tejun Heoc8e55f32010-06-29 10:07:12 +02001975 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 * gcwq->lock is held and there's no work to process and no
1977 * need to manage, sleep. Workers are woken up only while
1978 * holding gcwq->lock or from local cpu, so setting the
1979 * current state before releasing gcwq->lock is enough to
1980 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001981 */
1982 worker_enter_idle(worker);
1983 __set_current_state(TASK_INTERRUPTIBLE);
1984 spin_unlock_irq(&gcwq->lock);
1985 schedule();
1986 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Tejun Heoe22bee72010-06-29 10:07:14 +02001989/**
1990 * rescuer_thread - the rescuer thread function
1991 * @__wq: the associated workqueue
1992 *
1993 * Workqueue rescuer thread function. There's one rescuer for each
1994 * workqueue which has WQ_RESCUER set.
1995 *
1996 * Regular work processing on a gcwq may block trying to create a new
1997 * worker which uses GFP_KERNEL allocation which has slight chance of
1998 * developing into deadlock if some works currently on the same queue
1999 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2000 * the problem rescuer solves.
2001 *
2002 * When such condition is possible, the gcwq summons rescuers of all
2003 * workqueues which have works queued on the gcwq and let them process
2004 * those works so that forward progress can be guaranteed.
2005 *
2006 * This should happen rarely.
2007 */
2008static int rescuer_thread(void *__wq)
2009{
2010 struct workqueue_struct *wq = __wq;
2011 struct worker *rescuer = wq->rescuer;
2012 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002013 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 unsigned int cpu;
2015
2016 set_user_nice(current, RESCUER_NICE_LEVEL);
2017repeat:
2018 set_current_state(TASK_INTERRUPTIBLE);
2019
2020 if (kthread_should_stop())
2021 return 0;
2022
Tejun Heof3421792010-07-02 10:03:51 +02002023 /*
2024 * See whether any cpu is asking for help. Unbounded
2025 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2026 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002027 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002028 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2029 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 struct global_cwq *gcwq = cwq->gcwq;
2031 struct work_struct *work, *n;
2032
2033 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002034 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002035
2036 /* migrate to the target cpu if possible */
2037 rescuer->gcwq = gcwq;
2038 worker_maybe_bind_and_lock(rescuer);
2039
2040 /*
2041 * Slurp in all works issued via this workqueue and
2042 * process'em.
2043 */
2044 BUG_ON(!list_empty(&rescuer->scheduled));
2045 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2046 if (get_work_cwq(work) == cwq)
2047 move_linked_works(work, scheduled, &n);
2048
2049 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002050
2051 /*
2052 * Leave this gcwq. If keep_working() is %true, notify a
2053 * regular worker; otherwise, we end up with 0 concurrency
2054 * and stalling the execution.
2055 */
2056 if (keep_working(gcwq))
2057 wake_up_worker(gcwq);
2058
Tejun Heoe22bee72010-06-29 10:07:14 +02002059 spin_unlock_irq(&gcwq->lock);
2060 }
2061
2062 schedule();
2063 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002066struct wq_barrier {
2067 struct work_struct work;
2068 struct completion done;
2069};
2070
2071static void wq_barrier_func(struct work_struct *work)
2072{
2073 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2074 complete(&barr->done);
2075}
2076
Tejun Heo4690c4a2010-06-29 10:07:10 +02002077/**
2078 * insert_wq_barrier - insert a barrier work
2079 * @cwq: cwq to insert barrier into
2080 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002081 * @target: target work to attach @barr to
2082 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002083 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002084 * @barr is linked to @target such that @barr is completed only after
2085 * @target finishes execution. Please note that the ordering
2086 * guarantee is observed only with respect to @target and on the local
2087 * cpu.
2088 *
2089 * Currently, a queued barrier can't be canceled. This is because
2090 * try_to_grab_pending() can't determine whether the work to be
2091 * grabbed is at the head of the queue and thus can't clear LINKED
2092 * flag of the previous work while there must be a valid next work
2093 * after a work with LINKED flag set.
2094 *
2095 * Note that when @worker is non-NULL, @target may be modified
2096 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002097 *
2098 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002099 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002100 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002101static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002102 struct wq_barrier *barr,
2103 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002104{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002105 struct list_head *head;
2106 unsigned int linked = 0;
2107
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002108 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002109 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002110 * as we know for sure that this will not trigger any of the
2111 * checks and call back into the fixup functions where we
2112 * might deadlock.
2113 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002114 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002115 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002116 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002117
Tejun Heoaffee4b2010-06-29 10:07:12 +02002118 /*
2119 * If @target is currently being executed, schedule the
2120 * barrier to the worker; otherwise, put it after @target.
2121 */
2122 if (worker)
2123 head = worker->scheduled.next;
2124 else {
2125 unsigned long *bits = work_data_bits(target);
2126
2127 head = target->entry.next;
2128 /* there can already be other linked works, inherit and set */
2129 linked = *bits & WORK_STRUCT_LINKED;
2130 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2131 }
2132
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002133 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002134 insert_work(cwq, &barr->work, head,
2135 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002136}
2137
Tejun Heo73f53c42010-06-29 10:07:11 +02002138/**
2139 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2140 * @wq: workqueue being flushed
2141 * @flush_color: new flush color, < 0 for no-op
2142 * @work_color: new work color, < 0 for no-op
2143 *
2144 * Prepare cwqs for workqueue flushing.
2145 *
2146 * If @flush_color is non-negative, flush_color on all cwqs should be
2147 * -1. If no cwq has in-flight commands at the specified color, all
2148 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2149 * has in flight commands, its cwq->flush_color is set to
2150 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2151 * wakeup logic is armed and %true is returned.
2152 *
2153 * The caller should have initialized @wq->first_flusher prior to
2154 * calling this function with non-negative @flush_color. If
2155 * @flush_color is negative, no flush color update is done and %false
2156 * is returned.
2157 *
2158 * If @work_color is non-negative, all cwqs should have the same
2159 * work_color which is previous to @work_color and all will be
2160 * advanced to @work_color.
2161 *
2162 * CONTEXT:
2163 * mutex_lock(wq->flush_mutex).
2164 *
2165 * RETURNS:
2166 * %true if @flush_color >= 0 and there's something to flush. %false
2167 * otherwise.
2168 */
2169static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2170 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Tejun Heo73f53c42010-06-29 10:07:11 +02002172 bool wait = false;
2173 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002174
Tejun Heo73f53c42010-06-29 10:07:11 +02002175 if (flush_color >= 0) {
2176 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2177 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002178 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002179
Tejun Heof3421792010-07-02 10:03:51 +02002180 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002181 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002182 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002183
Tejun Heo8b03ae32010-06-29 10:07:12 +02002184 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002185
2186 if (flush_color >= 0) {
2187 BUG_ON(cwq->flush_color != -1);
2188
2189 if (cwq->nr_in_flight[flush_color]) {
2190 cwq->flush_color = flush_color;
2191 atomic_inc(&wq->nr_cwqs_to_flush);
2192 wait = true;
2193 }
2194 }
2195
2196 if (work_color >= 0) {
2197 BUG_ON(work_color != work_next_color(cwq->work_color));
2198 cwq->work_color = work_color;
2199 }
2200
Tejun Heo8b03ae32010-06-29 10:07:12 +02002201 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002202 }
2203
2204 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2205 complete(&wq->first_flusher->done);
2206
2207 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002210/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002212 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 *
2214 * Forces execution of the workqueue and blocks until its completion.
2215 * This is typically used in driver shutdown handlers.
2216 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002217 * We sleep until all works which were queued on entry have been handled,
2218 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002220void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Tejun Heo73f53c42010-06-29 10:07:11 +02002222 struct wq_flusher this_flusher = {
2223 .list = LIST_HEAD_INIT(this_flusher.list),
2224 .flush_color = -1,
2225 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2226 };
2227 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002228
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002229 lock_map_acquire(&wq->lockdep_map);
2230 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002231
2232 mutex_lock(&wq->flush_mutex);
2233
2234 /*
2235 * Start-to-wait phase
2236 */
2237 next_color = work_next_color(wq->work_color);
2238
2239 if (next_color != wq->flush_color) {
2240 /*
2241 * Color space is not full. The current work_color
2242 * becomes our flush_color and work_color is advanced
2243 * by one.
2244 */
2245 BUG_ON(!list_empty(&wq->flusher_overflow));
2246 this_flusher.flush_color = wq->work_color;
2247 wq->work_color = next_color;
2248
2249 if (!wq->first_flusher) {
2250 /* no flush in progress, become the first flusher */
2251 BUG_ON(wq->flush_color != this_flusher.flush_color);
2252
2253 wq->first_flusher = &this_flusher;
2254
2255 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2256 wq->work_color)) {
2257 /* nothing to flush, done */
2258 wq->flush_color = next_color;
2259 wq->first_flusher = NULL;
2260 goto out_unlock;
2261 }
2262 } else {
2263 /* wait in queue */
2264 BUG_ON(wq->flush_color == this_flusher.flush_color);
2265 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2266 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2267 }
2268 } else {
2269 /*
2270 * Oops, color space is full, wait on overflow queue.
2271 * The next flush completion will assign us
2272 * flush_color and transfer to flusher_queue.
2273 */
2274 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2275 }
2276
2277 mutex_unlock(&wq->flush_mutex);
2278
2279 wait_for_completion(&this_flusher.done);
2280
2281 /*
2282 * Wake-up-and-cascade phase
2283 *
2284 * First flushers are responsible for cascading flushes and
2285 * handling overflow. Non-first flushers can simply return.
2286 */
2287 if (wq->first_flusher != &this_flusher)
2288 return;
2289
2290 mutex_lock(&wq->flush_mutex);
2291
Tejun Heo4ce48b32010-07-02 10:03:51 +02002292 /* we might have raced, check again with mutex held */
2293 if (wq->first_flusher != &this_flusher)
2294 goto out_unlock;
2295
Tejun Heo73f53c42010-06-29 10:07:11 +02002296 wq->first_flusher = NULL;
2297
2298 BUG_ON(!list_empty(&this_flusher.list));
2299 BUG_ON(wq->flush_color != this_flusher.flush_color);
2300
2301 while (true) {
2302 struct wq_flusher *next, *tmp;
2303
2304 /* complete all the flushers sharing the current flush color */
2305 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2306 if (next->flush_color != wq->flush_color)
2307 break;
2308 list_del_init(&next->list);
2309 complete(&next->done);
2310 }
2311
2312 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2313 wq->flush_color != work_next_color(wq->work_color));
2314
2315 /* this flush_color is finished, advance by one */
2316 wq->flush_color = work_next_color(wq->flush_color);
2317
2318 /* one color has been freed, handle overflow queue */
2319 if (!list_empty(&wq->flusher_overflow)) {
2320 /*
2321 * Assign the same color to all overflowed
2322 * flushers, advance work_color and append to
2323 * flusher_queue. This is the start-to-wait
2324 * phase for these overflowed flushers.
2325 */
2326 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2327 tmp->flush_color = wq->work_color;
2328
2329 wq->work_color = work_next_color(wq->work_color);
2330
2331 list_splice_tail_init(&wq->flusher_overflow,
2332 &wq->flusher_queue);
2333 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2334 }
2335
2336 if (list_empty(&wq->flusher_queue)) {
2337 BUG_ON(wq->flush_color != wq->work_color);
2338 break;
2339 }
2340
2341 /*
2342 * Need to flush more colors. Make the next flusher
2343 * the new first flusher and arm cwqs.
2344 */
2345 BUG_ON(wq->flush_color == wq->work_color);
2346 BUG_ON(wq->flush_color != next->flush_color);
2347
2348 list_del_init(&next->list);
2349 wq->first_flusher = next;
2350
2351 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2352 break;
2353
2354 /*
2355 * Meh... this color is already done, clear first
2356 * flusher and repeat cascading.
2357 */
2358 wq->first_flusher = NULL;
2359 }
2360
2361out_unlock:
2362 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
Dave Jonesae90dd52006-06-30 01:40:45 -04002364EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Tejun Heobaf59022010-09-16 10:42:16 +02002366static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2367 bool wait_executing)
2368{
2369 struct worker *worker = NULL;
2370 struct global_cwq *gcwq;
2371 struct cpu_workqueue_struct *cwq;
2372
2373 might_sleep();
2374 gcwq = get_work_gcwq(work);
2375 if (!gcwq)
2376 return false;
2377
2378 spin_lock_irq(&gcwq->lock);
2379 if (!list_empty(&work->entry)) {
2380 /*
2381 * See the comment near try_to_grab_pending()->smp_rmb().
2382 * If it was re-queued to a different gcwq under us, we
2383 * are not going to wait.
2384 */
2385 smp_rmb();
2386 cwq = get_work_cwq(work);
2387 if (unlikely(!cwq || gcwq != cwq->gcwq))
2388 goto already_gone;
2389 } else if (wait_executing) {
2390 worker = find_worker_executing_work(gcwq, work);
2391 if (!worker)
2392 goto already_gone;
2393 cwq = worker->current_cwq;
2394 } else
2395 goto already_gone;
2396
2397 insert_wq_barrier(cwq, barr, work, worker);
2398 spin_unlock_irq(&gcwq->lock);
2399
Tejun Heoe1594892011-01-09 23:32:15 +01002400 /*
2401 * If @max_active is 1 or rescuer is in use, flushing another work
2402 * item on the same workqueue may lead to deadlock. Make sure the
2403 * flusher is not running on the same workqueue by verifying write
2404 * access.
2405 */
2406 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2407 lock_map_acquire(&cwq->wq->lockdep_map);
2408 else
2409 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002410 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002411
Tejun Heobaf59022010-09-16 10:42:16 +02002412 return true;
2413already_gone:
2414 spin_unlock_irq(&gcwq->lock);
2415 return false;
2416}
2417
Oleg Nesterovdb700892008-07-25 01:47:49 -07002418/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002419 * flush_work - wait for a work to finish executing the last queueing instance
2420 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002421 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002422 * Wait until @work has finished execution. This function considers
2423 * only the last queueing instance of @work. If @work has been
2424 * enqueued across different CPUs on a non-reentrant workqueue or on
2425 * multiple workqueues, @work might still be executing on return on
2426 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002427 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002428 * If @work was queued only on a non-reentrant, ordered or unbound
2429 * workqueue, @work is guaranteed to be idle on return if it hasn't
2430 * been requeued since flush started.
2431 *
2432 * RETURNS:
2433 * %true if flush_work() waited for the work to finish execution,
2434 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002435 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002436bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002437{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002438 struct wq_barrier barr;
2439
Tejun Heobaf59022010-09-16 10:42:16 +02002440 if (start_flush_work(work, &barr, true)) {
2441 wait_for_completion(&barr.done);
2442 destroy_work_on_stack(&barr.work);
2443 return true;
2444 } else
2445 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002446}
2447EXPORT_SYMBOL_GPL(flush_work);
2448
Tejun Heo401a8d02010-09-16 10:36:00 +02002449static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2450{
2451 struct wq_barrier barr;
2452 struct worker *worker;
2453
2454 spin_lock_irq(&gcwq->lock);
2455
2456 worker = find_worker_executing_work(gcwq, work);
2457 if (unlikely(worker))
2458 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2459
2460 spin_unlock_irq(&gcwq->lock);
2461
2462 if (unlikely(worker)) {
2463 wait_for_completion(&barr.done);
2464 destroy_work_on_stack(&barr.work);
2465 return true;
2466 } else
2467 return false;
2468}
2469
2470static bool wait_on_work(struct work_struct *work)
2471{
2472 bool ret = false;
2473 int cpu;
2474
2475 might_sleep();
2476
2477 lock_map_acquire(&work->lockdep_map);
2478 lock_map_release(&work->lockdep_map);
2479
2480 for_each_gcwq_cpu(cpu)
2481 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2482 return ret;
2483}
2484
Tejun Heo09383492010-09-16 10:48:29 +02002485/**
2486 * flush_work_sync - wait until a work has finished execution
2487 * @work: the work to flush
2488 *
2489 * Wait until @work has finished execution. On return, it's
2490 * guaranteed that all queueing instances of @work which happened
2491 * before this function is called are finished. In other words, if
2492 * @work hasn't been requeued since this function was called, @work is
2493 * guaranteed to be idle on return.
2494 *
2495 * RETURNS:
2496 * %true if flush_work_sync() waited for the work to finish execution,
2497 * %false if it was already idle.
2498 */
2499bool flush_work_sync(struct work_struct *work)
2500{
2501 struct wq_barrier barr;
2502 bool pending, waited;
2503
2504 /* we'll wait for executions separately, queue barr only if pending */
2505 pending = start_flush_work(work, &barr, false);
2506
2507 /* wait for executions to finish */
2508 waited = wait_on_work(work);
2509
2510 /* wait for the pending one */
2511 if (pending) {
2512 wait_for_completion(&barr.done);
2513 destroy_work_on_stack(&barr.work);
2514 }
2515
2516 return pending || waited;
2517}
2518EXPORT_SYMBOL_GPL(flush_work_sync);
2519
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002520/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002521 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002522 * so this work can't be re-armed in any way.
2523 */
2524static int try_to_grab_pending(struct work_struct *work)
2525{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002526 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002527 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002528
Tejun Heo22df02b2010-06-29 10:07:10 +02002529 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002530 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002531
2532 /*
2533 * The queueing is in progress, or it is already queued. Try to
2534 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2535 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002536 gcwq = get_work_gcwq(work);
2537 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002538 return ret;
2539
Tejun Heo8b03ae32010-06-29 10:07:12 +02002540 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002541 if (!list_empty(&work->entry)) {
2542 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002543 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002544 * In that case we must see the new value after rmb(), see
2545 * insert_work()->wmb().
2546 */
2547 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002548 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002549 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002550 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002551 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002552 get_work_color(work),
2553 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002554 ret = 1;
2555 }
2556 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002557 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002558
2559 return ret;
2560}
2561
Tejun Heo401a8d02010-09-16 10:36:00 +02002562static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002563 struct timer_list* timer)
2564{
2565 int ret;
2566
2567 do {
2568 ret = (timer && likely(del_timer(timer)));
2569 if (!ret)
2570 ret = try_to_grab_pending(work);
2571 wait_on_work(work);
2572 } while (unlikely(ret < 0));
2573
Tejun Heo7a22ad72010-06-29 10:07:13 +02002574 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002575 return ret;
2576}
2577
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002578/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002579 * cancel_work_sync - cancel a work and wait for it to finish
2580 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002581 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002582 * Cancel @work and wait for its execution to finish. This function
2583 * can be used even if the work re-queues itself or migrates to
2584 * another workqueue. On return from this function, @work is
2585 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002586 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002587 * cancel_work_sync(&delayed_work->work) must not be used for
2588 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002589 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002590 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002591 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002592 *
2593 * RETURNS:
2594 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002595 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002596bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002597{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002598 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002599}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002600EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002601
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002602/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002603 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2604 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002605 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002606 * Delayed timer is cancelled and the pending work is queued for
2607 * immediate execution. Like flush_work(), this function only
2608 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002609 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002610 * RETURNS:
2611 * %true if flush_work() waited for the work to finish execution,
2612 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002613 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002614bool flush_delayed_work(struct delayed_work *dwork)
2615{
2616 if (del_timer_sync(&dwork->timer))
2617 __queue_work(raw_smp_processor_id(),
2618 get_work_cwq(&dwork->work)->wq, &dwork->work);
2619 return flush_work(&dwork->work);
2620}
2621EXPORT_SYMBOL(flush_delayed_work);
2622
2623/**
Tejun Heo09383492010-09-16 10:48:29 +02002624 * flush_delayed_work_sync - wait for a dwork to finish
2625 * @dwork: the delayed work to flush
2626 *
2627 * Delayed timer is cancelled and the pending work is queued for
2628 * execution immediately. Other than timer handling, its behavior
2629 * is identical to flush_work_sync().
2630 *
2631 * RETURNS:
2632 * %true if flush_work_sync() waited for the work to finish execution,
2633 * %false if it was already idle.
2634 */
2635bool flush_delayed_work_sync(struct delayed_work *dwork)
2636{
2637 if (del_timer_sync(&dwork->timer))
2638 __queue_work(raw_smp_processor_id(),
2639 get_work_cwq(&dwork->work)->wq, &dwork->work);
2640 return flush_work_sync(&dwork->work);
2641}
2642EXPORT_SYMBOL(flush_delayed_work_sync);
2643
2644/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002645 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2646 * @dwork: the delayed work cancel
2647 *
2648 * This is cancel_work_sync() for delayed works.
2649 *
2650 * RETURNS:
2651 * %true if @dwork was pending, %false otherwise.
2652 */
2653bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002655 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002656}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002657EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002659/**
2660 * schedule_work - put work task in global workqueue
2661 * @work: job to be done
2662 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002663 * Returns zero if @work was already on the kernel-global workqueue and
2664 * non-zero otherwise.
2665 *
2666 * This puts a job in the kernel-global workqueue if it was not already
2667 * queued and leaves it in the same position on the kernel-global
2668 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002669 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002670int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Tejun Heod320c032010-06-29 10:07:14 +02002672 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
Dave Jonesae90dd52006-06-30 01:40:45 -04002674EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Zhang Ruic1a220e2008-07-23 21:28:39 -07002676/*
2677 * schedule_work_on - put work task on a specific cpu
2678 * @cpu: cpu to put the work task on
2679 * @work: job to be done
2680 *
2681 * This puts a job on a specific cpu
2682 */
2683int schedule_work_on(int cpu, struct work_struct *work)
2684{
Tejun Heod320c032010-06-29 10:07:14 +02002685 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002686}
2687EXPORT_SYMBOL(schedule_work_on);
2688
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002689/**
2690 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002691 * @dwork: job to be done
2692 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002693 *
2694 * After waiting for a given time this puts a job in the kernel-global
2695 * workqueue.
2696 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002697int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002698 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Tejun Heod320c032010-06-29 10:07:14 +02002700 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
Dave Jonesae90dd52006-06-30 01:40:45 -04002702EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002704/**
2705 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2706 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002707 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002708 * @delay: number of jiffies to wait
2709 *
2710 * After waiting for a given time this puts a job in the kernel-global
2711 * workqueue on the specified CPU.
2712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002714 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Tejun Heod320c032010-06-29 10:07:14 +02002716 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
Dave Jonesae90dd52006-06-30 01:40:45 -04002718EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Andrew Mortonb6136772006-06-25 05:47:49 -07002720/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002721 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002722 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002723 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002724 * schedule_on_each_cpu() executes @func on each online CPU using the
2725 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002726 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002727 *
2728 * RETURNS:
2729 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002730 */
David Howells65f27f32006-11-22 14:55:48 +00002731int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002732{
2733 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002734 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002735
Andrew Mortonb6136772006-06-25 05:47:49 -07002736 works = alloc_percpu(struct work_struct);
2737 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002738 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002739
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002740 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002741
Christoph Lameter15316ba2006-01-08 01:00:43 -08002742 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002743 struct work_struct *work = per_cpu_ptr(works, cpu);
2744
2745 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002746 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002747 }
Tejun Heo93981802009-11-17 14:06:20 -08002748
2749 for_each_online_cpu(cpu)
2750 flush_work(per_cpu_ptr(works, cpu));
2751
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002752 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002753 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002754 return 0;
2755}
2756
Alan Sterneef6a7d2010-02-12 17:39:21 +09002757/**
2758 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2759 *
2760 * Forces execution of the kernel-global workqueue and blocks until its
2761 * completion.
2762 *
2763 * Think twice before calling this function! It's very easy to get into
2764 * trouble if you don't take great care. Either of the following situations
2765 * will lead to deadlock:
2766 *
2767 * One of the work items currently on the workqueue needs to acquire
2768 * a lock held by your code or its caller.
2769 *
2770 * Your code is running in the context of a work routine.
2771 *
2772 * They will be detected by lockdep when they occur, but the first might not
2773 * occur very often. It depends on what work items are on the workqueue and
2774 * what locks they need, which you have no control over.
2775 *
2776 * In most situations flushing the entire workqueue is overkill; you merely
2777 * need to know that a particular work item isn't queued and isn't running.
2778 * In such cases you should use cancel_delayed_work_sync() or
2779 * cancel_work_sync() instead.
2780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781void flush_scheduled_work(void)
2782{
Tejun Heod320c032010-06-29 10:07:14 +02002783 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
Dave Jonesae90dd52006-06-30 01:40:45 -04002785EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002788 * execute_in_process_context - reliably execute the routine with user context
2789 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002790 * @ew: guaranteed storage for the execute work structure (must
2791 * be available when the work executes)
2792 *
2793 * Executes the function immediately if process context is available,
2794 * otherwise schedules the function for delayed execution.
2795 *
2796 * Returns: 0 - function was executed
2797 * 1 - function was scheduled for execution
2798 */
David Howells65f27f32006-11-22 14:55:48 +00002799int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002800{
2801 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002802 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002803 return 0;
2804 }
2805
David Howells65f27f32006-11-22 14:55:48 +00002806 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002807 schedule_work(&ew->work);
2808
2809 return 1;
2810}
2811EXPORT_SYMBOL_GPL(execute_in_process_context);
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813int keventd_up(void)
2814{
Tejun Heod320c032010-06-29 10:07:14 +02002815 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002818static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002820 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002821 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2822 * Make sure that the alignment isn't lower than that of
2823 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002824 */
Tejun Heo0f900042010-06-29 10:07:11 +02002825 const size_t size = sizeof(struct cpu_workqueue_struct);
2826 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2827 __alignof__(unsigned long long));
Tejun Heo931ac772010-07-20 11:07:48 +02002828#ifdef CONFIG_SMP
2829 bool percpu = !(wq->flags & WQ_UNBOUND);
2830#else
2831 bool percpu = false;
2832#endif
Oleg Nesterov3af244332007-05-09 02:34:09 -07002833
Tejun Heo931ac772010-07-20 11:07:48 +02002834 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002835 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002836 else {
Tejun Heof3421792010-07-02 10:03:51 +02002837 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002838
Tejun Heof3421792010-07-02 10:03:51 +02002839 /*
2840 * Allocate enough room to align cwq and put an extra
2841 * pointer at the end pointing back to the originally
2842 * allocated pointer which will be used for free.
2843 */
2844 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2845 if (ptr) {
2846 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2847 *(void **)(wq->cpu_wq.single + 1) = ptr;
2848 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002849 }
Tejun Heof3421792010-07-02 10:03:51 +02002850
David Howells52605622010-10-25 23:41:11 +01002851 /* just in case, make sure it's actually aligned
2852 * - this is affected by PERCPU() alignment in vmlinux.lds.S
2853 */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002854 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2855 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002856}
2857
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002858static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002859{
Tejun Heo931ac772010-07-20 11:07:48 +02002860#ifdef CONFIG_SMP
2861 bool percpu = !(wq->flags & WQ_UNBOUND);
2862#else
2863 bool percpu = false;
2864#endif
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002865
Tejun Heo931ac772010-07-20 11:07:48 +02002866 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002867 free_percpu(wq->cpu_wq.pcpu);
2868 else if (wq->cpu_wq.single) {
2869 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002870 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002871 }
2872}
2873
Tejun Heof3421792010-07-02 10:03:51 +02002874static int wq_clamp_max_active(int max_active, unsigned int flags,
2875 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002876{
Tejun Heof3421792010-07-02 10:03:51 +02002877 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2878
2879 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002880 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2881 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002882 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002883
Tejun Heof3421792010-07-02 10:03:51 +02002884 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002885}
2886
Tejun Heod320c032010-06-29 10:07:14 +02002887struct workqueue_struct *__alloc_workqueue_key(const char *name,
2888 unsigned int flags,
2889 int max_active,
2890 struct lock_class_key *key,
2891 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002892{
2893 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002894 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002895
Tejun Heof3421792010-07-02 10:03:51 +02002896 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002897 * Workqueues which may be used during memory reclaim should
2898 * have a rescuer to guarantee forward progress.
2899 */
2900 if (flags & WQ_MEM_RECLAIM)
2901 flags |= WQ_RESCUER;
2902
2903 /*
Tejun Heof3421792010-07-02 10:03:51 +02002904 * Unbound workqueues aren't concurrency managed and should be
2905 * dispatched to workers immediately.
2906 */
2907 if (flags & WQ_UNBOUND)
2908 flags |= WQ_HIGHPRI;
2909
Tejun Heod320c032010-06-29 10:07:14 +02002910 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heof3421792010-07-02 10:03:51 +02002911 max_active = wq_clamp_max_active(max_active, flags, name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002912
2913 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2914 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02002915 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002916
Tejun Heo97e37d72010-06-29 10:07:10 +02002917 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002918 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002919 mutex_init(&wq->flush_mutex);
2920 atomic_set(&wq->nr_cwqs_to_flush, 0);
2921 INIT_LIST_HEAD(&wq->flusher_queue);
2922 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002923
2924 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01002925 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002926 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002927
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002928 if (alloc_cwqs(wq) < 0)
2929 goto err;
2930
Tejun Heof3421792010-07-02 10:03:51 +02002931 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002932 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002933 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02002934
Tejun Heo0f900042010-06-29 10:07:11 +02002935 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002936 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002937 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002938 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002939 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002940 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002941 }
2942
Tejun Heoe22bee72010-06-29 10:07:14 +02002943 if (flags & WQ_RESCUER) {
2944 struct worker *rescuer;
2945
Tejun Heof2e005a2010-07-20 15:59:09 +02002946 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02002947 goto err;
2948
2949 wq->rescuer = rescuer = alloc_worker();
2950 if (!rescuer)
2951 goto err;
2952
2953 rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
2954 if (IS_ERR(rescuer->task))
2955 goto err;
2956
Tejun Heoe22bee72010-06-29 10:07:14 +02002957 rescuer->task->flags |= PF_THREAD_BOUND;
2958 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002959 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002960
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002961 /*
2962 * workqueue_lock protects global freeze state and workqueues
2963 * list. Grab it, set max_active accordingly and add the new
2964 * workqueue to workqueues list.
2965 */
Tejun Heo15376632010-06-29 10:07:11 +02002966 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002967
Tejun Heo58a69cb2011-02-16 09:25:31 +01002968 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02002969 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002970 get_cwq(cpu, wq)->max_active = 0;
2971
Tejun Heo15376632010-06-29 10:07:11 +02002972 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002973
Tejun Heo15376632010-06-29 10:07:11 +02002974 spin_unlock(&workqueue_lock);
2975
Oleg Nesterov3af244332007-05-09 02:34:09 -07002976 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02002977err:
2978 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002979 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02002980 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002981 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02002982 kfree(wq);
2983 }
2984 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002985}
Tejun Heod320c032010-06-29 10:07:14 +02002986EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002987
2988/**
2989 * destroy_workqueue - safely terminate a workqueue
2990 * @wq: target workqueue
2991 *
2992 * Safely destroy a workqueue. All work currently pending will be done first.
2993 */
2994void destroy_workqueue(struct workqueue_struct *wq)
2995{
Tejun Heoc8efcc22010-12-20 19:32:04 +01002996 unsigned int flush_cnt = 0;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002997 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002998
Tejun Heoc8efcc22010-12-20 19:32:04 +01002999 /*
3000 * Mark @wq dying and drain all pending works. Once WQ_DYING is
3001 * set, only chain queueing is allowed. IOW, only currently
3002 * pending or running work items on @wq can queue further work
3003 * items on it. @wq is flushed repeatedly until it becomes empty.
3004 * The number of flushing is detemined by the depth of chaining and
3005 * should be relatively short. Whine if it takes too long.
3006 */
Tejun Heoe41e7042010-08-24 14:22:47 +02003007 wq->flags |= WQ_DYING;
Tejun Heoc8efcc22010-12-20 19:32:04 +01003008reflush:
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003009 flush_workqueue(wq);
3010
Tejun Heoc8efcc22010-12-20 19:32:04 +01003011 for_each_cwq_cpu(cpu, wq) {
3012 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3013
3014 if (!cwq->nr_active && list_empty(&cwq->delayed_works))
3015 continue;
3016
3017 if (++flush_cnt == 10 ||
3018 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
3019 printk(KERN_WARNING "workqueue %s: flush on "
3020 "destruction isn't complete after %u tries\n",
3021 wq->name, flush_cnt);
3022 goto reflush;
3023 }
3024
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003025 /*
3026 * wq list is used to freeze wq, remove from list after
3027 * flushing is complete in case freeze races us.
3028 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003029 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003030 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003031 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003032
Tejun Heoe22bee72010-06-29 10:07:14 +02003033 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003034 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003035 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3036 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003037
Tejun Heo73f53c42010-06-29 10:07:11 +02003038 for (i = 0; i < WORK_NR_COLORS; i++)
3039 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003040 BUG_ON(cwq->nr_active);
3041 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003042 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003043
Tejun Heoe22bee72010-06-29 10:07:14 +02003044 if (wq->flags & WQ_RESCUER) {
3045 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003046 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003047 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003048 }
3049
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003050 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003051 kfree(wq);
3052}
3053EXPORT_SYMBOL_GPL(destroy_workqueue);
3054
Tejun Heodcd989c2010-06-29 10:07:14 +02003055/**
3056 * workqueue_set_max_active - adjust max_active of a workqueue
3057 * @wq: target workqueue
3058 * @max_active: new max_active value.
3059 *
3060 * Set max_active of @wq to @max_active.
3061 *
3062 * CONTEXT:
3063 * Don't call from IRQ context.
3064 */
3065void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3066{
3067 unsigned int cpu;
3068
Tejun Heof3421792010-07-02 10:03:51 +02003069 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003070
3071 spin_lock(&workqueue_lock);
3072
3073 wq->saved_max_active = max_active;
3074
Tejun Heof3421792010-07-02 10:03:51 +02003075 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003076 struct global_cwq *gcwq = get_gcwq(cpu);
3077
3078 spin_lock_irq(&gcwq->lock);
3079
Tejun Heo58a69cb2011-02-16 09:25:31 +01003080 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003081 !(gcwq->flags & GCWQ_FREEZING))
3082 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3083
3084 spin_unlock_irq(&gcwq->lock);
3085 }
3086
3087 spin_unlock(&workqueue_lock);
3088}
3089EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3090
3091/**
3092 * workqueue_congested - test whether a workqueue is congested
3093 * @cpu: CPU in question
3094 * @wq: target workqueue
3095 *
3096 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3097 * no synchronization around this function and the test result is
3098 * unreliable and only useful as advisory hints or for debugging.
3099 *
3100 * RETURNS:
3101 * %true if congested, %false otherwise.
3102 */
3103bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3104{
3105 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3106
3107 return !list_empty(&cwq->delayed_works);
3108}
3109EXPORT_SYMBOL_GPL(workqueue_congested);
3110
3111/**
3112 * work_cpu - return the last known associated cpu for @work
3113 * @work: the work of interest
3114 *
3115 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003116 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003117 */
3118unsigned int work_cpu(struct work_struct *work)
3119{
3120 struct global_cwq *gcwq = get_work_gcwq(work);
3121
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003122 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003123}
3124EXPORT_SYMBOL_GPL(work_cpu);
3125
3126/**
3127 * work_busy - test whether a work is currently pending or running
3128 * @work: the work to be tested
3129 *
3130 * Test whether @work is currently pending or running. There is no
3131 * synchronization around this function and the test result is
3132 * unreliable and only useful as advisory hints or for debugging.
3133 * Especially for reentrant wqs, the pending state might hide the
3134 * running state.
3135 *
3136 * RETURNS:
3137 * OR'd bitmask of WORK_BUSY_* bits.
3138 */
3139unsigned int work_busy(struct work_struct *work)
3140{
3141 struct global_cwq *gcwq = get_work_gcwq(work);
3142 unsigned long flags;
3143 unsigned int ret = 0;
3144
3145 if (!gcwq)
3146 return false;
3147
3148 spin_lock_irqsave(&gcwq->lock, flags);
3149
3150 if (work_pending(work))
3151 ret |= WORK_BUSY_PENDING;
3152 if (find_worker_executing_work(gcwq, work))
3153 ret |= WORK_BUSY_RUNNING;
3154
3155 spin_unlock_irqrestore(&gcwq->lock, flags);
3156
3157 return ret;
3158}
3159EXPORT_SYMBOL_GPL(work_busy);
3160
Tejun Heodb7bccf2010-06-29 10:07:12 +02003161/*
3162 * CPU hotplug.
3163 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003164 * There are two challenges in supporting CPU hotplug. Firstly, there
3165 * are a lot of assumptions on strong associations among work, cwq and
3166 * gcwq which make migrating pending and scheduled works very
3167 * difficult to implement without impacting hot paths. Secondly,
3168 * gcwqs serve mix of short, long and very long running works making
3169 * blocked draining impractical.
3170 *
3171 * This is solved by allowing a gcwq to be detached from CPU, running
3172 * it with unbound (rogue) workers and allowing it to be reattached
3173 * later if the cpu comes back online. A separate thread is created
3174 * to govern a gcwq in such state and is called the trustee of the
3175 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003176 *
3177 * Trustee states and their descriptions.
3178 *
3179 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3180 * new trustee is started with this state.
3181 *
3182 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003183 * assuming the manager role and making all existing
3184 * workers rogue. DOWN_PREPARE waits for trustee to
3185 * enter this state. After reaching IN_CHARGE, trustee
3186 * tries to execute the pending worklist until it's empty
3187 * and the state is set to BUTCHER, or the state is set
3188 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003189 *
3190 * BUTCHER Command state which is set by the cpu callback after
3191 * the cpu has went down. Once this state is set trustee
3192 * knows that there will be no new works on the worklist
3193 * and once the worklist is empty it can proceed to
3194 * killing idle workers.
3195 *
3196 * RELEASE Command state which is set by the cpu callback if the
3197 * cpu down has been canceled or it has come online
3198 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003199 * trying to drain or butcher and clears ROGUE, rebinds
3200 * all remaining workers back to the cpu and releases
3201 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003202 *
3203 * DONE Trustee will enter this state after BUTCHER or RELEASE
3204 * is complete.
3205 *
3206 * trustee CPU draining
3207 * took over down complete
3208 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3209 * | | ^
3210 * | CPU is back online v return workers |
3211 * ----------------> RELEASE --------------
3212 */
3213
3214/**
3215 * trustee_wait_event_timeout - timed event wait for trustee
3216 * @cond: condition to wait for
3217 * @timeout: timeout in jiffies
3218 *
3219 * wait_event_timeout() for trustee to use. Handles locking and
3220 * checks for RELEASE request.
3221 *
3222 * CONTEXT:
3223 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3224 * multiple times. To be used by trustee.
3225 *
3226 * RETURNS:
3227 * Positive indicating left time if @cond is satisfied, 0 if timed
3228 * out, -1 if canceled.
3229 */
3230#define trustee_wait_event_timeout(cond, timeout) ({ \
3231 long __ret = (timeout); \
3232 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3233 __ret) { \
3234 spin_unlock_irq(&gcwq->lock); \
3235 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3236 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3237 __ret); \
3238 spin_lock_irq(&gcwq->lock); \
3239 } \
3240 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3241})
3242
3243/**
3244 * trustee_wait_event - event wait for trustee
3245 * @cond: condition to wait for
3246 *
3247 * wait_event() for trustee to use. Automatically handles locking and
3248 * checks for CANCEL request.
3249 *
3250 * CONTEXT:
3251 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3252 * multiple times. To be used by trustee.
3253 *
3254 * RETURNS:
3255 * 0 if @cond is satisfied, -1 if canceled.
3256 */
3257#define trustee_wait_event(cond) ({ \
3258 long __ret1; \
3259 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3260 __ret1 < 0 ? -1 : 0; \
3261})
3262
3263static int __cpuinit trustee_thread(void *__gcwq)
3264{
3265 struct global_cwq *gcwq = __gcwq;
3266 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003267 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003268 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003269 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003270 int i;
3271
3272 BUG_ON(gcwq->cpu != smp_processor_id());
3273
3274 spin_lock_irq(&gcwq->lock);
3275 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003276 * Claim the manager position and make all workers rogue.
3277 * Trustee must be bound to the target cpu and can't be
3278 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003279 */
3280 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003281 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3282 BUG_ON(rc < 0);
3283
3284 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003285
3286 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003287 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003288
3289 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003290 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003291
3292 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003293 * Call schedule() so that we cross rq->lock and thus can
3294 * guarantee sched callbacks see the rogue flag. This is
3295 * necessary as scheduler callbacks may be invoked from other
3296 * cpus.
3297 */
3298 spin_unlock_irq(&gcwq->lock);
3299 schedule();
3300 spin_lock_irq(&gcwq->lock);
3301
3302 /*
Tejun Heocb444762010-07-02 10:03:50 +02003303 * Sched callbacks are disabled now. Zap nr_running. After
3304 * this, nr_running stays zero and need_more_worker() and
3305 * keep_working() are always true as long as the worklist is
3306 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003307 */
Tejun Heocb444762010-07-02 10:03:50 +02003308 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003309
3310 spin_unlock_irq(&gcwq->lock);
3311 del_timer_sync(&gcwq->idle_timer);
3312 spin_lock_irq(&gcwq->lock);
3313
3314 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003315 * We're now in charge. Notify and proceed to drain. We need
3316 * to keep the gcwq running during the whole CPU down
3317 * procedure as other cpu hotunplug callbacks may need to
3318 * flush currently running tasks.
3319 */
3320 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3321 wake_up_all(&gcwq->trustee_wait);
3322
3323 /*
3324 * The original cpu is in the process of dying and may go away
3325 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003326 * be migrated to other cpus. Try draining any left work. We
3327 * want to get it over with ASAP - spam rescuers, wake up as
3328 * many idlers as necessary and create new ones till the
3329 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003330 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003331 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003332 */
3333 while (gcwq->nr_workers != gcwq->nr_idle ||
3334 gcwq->flags & GCWQ_FREEZING ||
3335 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003336 int nr_works = 0;
3337
3338 list_for_each_entry(work, &gcwq->worklist, entry) {
3339 send_mayday(work);
3340 nr_works++;
3341 }
3342
3343 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3344 if (!nr_works--)
3345 break;
3346 wake_up_process(worker->task);
3347 }
3348
3349 if (need_to_create_worker(gcwq)) {
3350 spin_unlock_irq(&gcwq->lock);
3351 worker = create_worker(gcwq, false);
3352 spin_lock_irq(&gcwq->lock);
3353 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003354 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003355 start_worker(worker);
3356 }
3357 }
3358
Tejun Heodb7bccf2010-06-29 10:07:12 +02003359 /* give a breather */
3360 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3361 break;
3362 }
3363
Tejun Heoe22bee72010-06-29 10:07:14 +02003364 /*
3365 * Either all works have been scheduled and cpu is down, or
3366 * cpu down has already been canceled. Wait for and butcher
3367 * all workers till we're canceled.
3368 */
3369 do {
3370 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3371 while (!list_empty(&gcwq->idle_list))
3372 destroy_worker(list_first_entry(&gcwq->idle_list,
3373 struct worker, entry));
3374 } while (gcwq->nr_workers && rc >= 0);
3375
3376 /*
3377 * At this point, either draining has completed and no worker
3378 * is left, or cpu down has been canceled or the cpu is being
3379 * brought back up. There shouldn't be any idle one left.
3380 * Tell the remaining busy ones to rebind once it finishes the
3381 * currently scheduled works by scheduling the rebind_work.
3382 */
3383 WARN_ON(!list_empty(&gcwq->idle_list));
3384
3385 for_each_busy_worker(worker, i, pos, gcwq) {
3386 struct work_struct *rebind_work = &worker->rebind_work;
3387
3388 /*
3389 * Rebind_work may race with future cpu hotplug
3390 * operations. Use a separate flag to mark that
3391 * rebinding is scheduled.
3392 */
Tejun Heocb444762010-07-02 10:03:50 +02003393 worker->flags |= WORKER_REBIND;
3394 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003395
3396 /* queue rebind_work, wq doesn't matter, use the default one */
3397 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3398 work_data_bits(rebind_work)))
3399 continue;
3400
3401 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003402 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003403 worker->scheduled.next,
3404 work_color_to_flags(WORK_NO_COLOR));
3405 }
3406
3407 /* relinquish manager role */
3408 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3409
Tejun Heodb7bccf2010-06-29 10:07:12 +02003410 /* notify completion */
3411 gcwq->trustee = NULL;
3412 gcwq->trustee_state = TRUSTEE_DONE;
3413 wake_up_all(&gcwq->trustee_wait);
3414 spin_unlock_irq(&gcwq->lock);
3415 return 0;
3416}
3417
3418/**
3419 * wait_trustee_state - wait for trustee to enter the specified state
3420 * @gcwq: gcwq the trustee of interest belongs to
3421 * @state: target state to wait for
3422 *
3423 * Wait for the trustee to reach @state. DONE is already matched.
3424 *
3425 * CONTEXT:
3426 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3427 * multiple times. To be used by cpu_callback.
3428 */
3429static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003430__releases(&gcwq->lock)
3431__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003432{
3433 if (!(gcwq->trustee_state == state ||
3434 gcwq->trustee_state == TRUSTEE_DONE)) {
3435 spin_unlock_irq(&gcwq->lock);
3436 __wait_event(gcwq->trustee_wait,
3437 gcwq->trustee_state == state ||
3438 gcwq->trustee_state == TRUSTEE_DONE);
3439 spin_lock_irq(&gcwq->lock);
3440 }
3441}
3442
Oleg Nesterov3af244332007-05-09 02:34:09 -07003443static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3444 unsigned long action,
3445 void *hcpu)
3446{
3447 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003448 struct global_cwq *gcwq = get_gcwq(cpu);
3449 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003450 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003451 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003453 action &= ~CPU_TASKS_FROZEN;
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003456 case CPU_DOWN_PREPARE:
3457 new_trustee = kthread_create(trustee_thread, gcwq,
3458 "workqueue_trustee/%d\n", cpu);
3459 if (IS_ERR(new_trustee))
3460 return notifier_from_errno(PTR_ERR(new_trustee));
3461 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003462 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003464 BUG_ON(gcwq->first_idle);
3465 new_worker = create_worker(gcwq, false);
3466 if (!new_worker) {
3467 if (new_trustee)
3468 kthread_stop(new_trustee);
3469 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
3472
Tejun Heodb7bccf2010-06-29 10:07:12 +02003473 /* some are called w/ irq disabled, don't disturb irq status */
3474 spin_lock_irqsave(&gcwq->lock, flags);
3475
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003476 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003477 case CPU_DOWN_PREPARE:
3478 /* initialize trustee and tell it to acquire the gcwq */
3479 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3480 gcwq->trustee = new_trustee;
3481 gcwq->trustee_state = TRUSTEE_START;
3482 wake_up_process(gcwq->trustee);
3483 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003484 /* fall through */
3485 case CPU_UP_PREPARE:
3486 BUG_ON(gcwq->first_idle);
3487 gcwq->first_idle = new_worker;
3488 break;
3489
3490 case CPU_DYING:
3491 /*
3492 * Before this, the trustee and all workers except for
3493 * the ones which are still executing works from
3494 * before the last CPU down must be on the cpu. After
3495 * this, they'll all be diasporas.
3496 */
3497 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003498 break;
3499
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003500 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003501 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003502 /* fall through */
3503 case CPU_UP_CANCELED:
3504 destroy_worker(gcwq->first_idle);
3505 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003506 break;
3507
3508 case CPU_DOWN_FAILED:
3509 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003510 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003511 if (gcwq->trustee_state != TRUSTEE_DONE) {
3512 gcwq->trustee_state = TRUSTEE_RELEASE;
3513 wake_up_process(gcwq->trustee);
3514 wait_trustee_state(gcwq, TRUSTEE_DONE);
3515 }
3516
Tejun Heoe22bee72010-06-29 10:07:14 +02003517 /*
3518 * Trustee is done and there might be no worker left.
3519 * Put the first_idle in and request a real manager to
3520 * take a look.
3521 */
3522 spin_unlock_irq(&gcwq->lock);
3523 kthread_bind(gcwq->first_idle->task, cpu);
3524 spin_lock_irq(&gcwq->lock);
3525 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3526 start_worker(gcwq->first_idle);
3527 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003528 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003529 }
3530
Tejun Heodb7bccf2010-06-29 10:07:12 +02003531 spin_unlock_irqrestore(&gcwq->lock, flags);
3532
Tejun Heo15376632010-06-29 10:07:11 +02003533 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Rusty Russell2d3854a2008-11-05 13:39:10 +11003536#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003537
Rusty Russell2d3854a2008-11-05 13:39:10 +11003538struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003539 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003540 long (*fn)(void *);
3541 void *arg;
3542 long ret;
3543};
3544
Andrew Morton6b440032009-04-09 09:50:37 -06003545static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003546{
Andrew Morton6b440032009-04-09 09:50:37 -06003547 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003548 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003549 complete(&wfc->completion);
3550 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003551}
3552
3553/**
3554 * work_on_cpu - run a function in user context on a particular cpu
3555 * @cpu: the cpu to run on
3556 * @fn: the function to run
3557 * @arg: the function arg
3558 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003559 * This will return the value @fn returns.
3560 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003561 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003562 */
3563long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3564{
Andrew Morton6b440032009-04-09 09:50:37 -06003565 struct task_struct *sub_thread;
3566 struct work_for_cpu wfc = {
3567 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3568 .fn = fn,
3569 .arg = arg,
3570 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003571
Andrew Morton6b440032009-04-09 09:50:37 -06003572 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3573 if (IS_ERR(sub_thread))
3574 return PTR_ERR(sub_thread);
3575 kthread_bind(sub_thread, cpu);
3576 wake_up_process(sub_thread);
3577 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003578 return wfc.ret;
3579}
3580EXPORT_SYMBOL_GPL(work_on_cpu);
3581#endif /* CONFIG_SMP */
3582
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003583#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303584
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003585/**
3586 * freeze_workqueues_begin - begin freezing workqueues
3587 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003588 * Start freezing workqueues. After this function returns, all freezable
3589 * workqueues will queue new works to their frozen_works list instead of
3590 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003591 *
3592 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003593 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003594 */
3595void freeze_workqueues_begin(void)
3596{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003597 unsigned int cpu;
3598
3599 spin_lock(&workqueue_lock);
3600
3601 BUG_ON(workqueue_freezing);
3602 workqueue_freezing = true;
3603
Tejun Heof3421792010-07-02 10:03:51 +02003604 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003605 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003606 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003607
3608 spin_lock_irq(&gcwq->lock);
3609
Tejun Heodb7bccf2010-06-29 10:07:12 +02003610 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3611 gcwq->flags |= GCWQ_FREEZING;
3612
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003613 list_for_each_entry(wq, &workqueues, list) {
3614 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3615
Tejun Heo58a69cb2011-02-16 09:25:31 +01003616 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003617 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003618 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003619
3620 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003621 }
3622
3623 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003625
3626/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003627 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003628 *
3629 * Check whether freezing is complete. This function must be called
3630 * between freeze_workqueues_begin() and thaw_workqueues().
3631 *
3632 * CONTEXT:
3633 * Grabs and releases workqueue_lock.
3634 *
3635 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003636 * %true if some freezable workqueues are still busy. %false if freezing
3637 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003638 */
3639bool freeze_workqueues_busy(void)
3640{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003641 unsigned int cpu;
3642 bool busy = false;
3643
3644 spin_lock(&workqueue_lock);
3645
3646 BUG_ON(!workqueue_freezing);
3647
Tejun Heof3421792010-07-02 10:03:51 +02003648 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003649 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003650 /*
3651 * nr_active is monotonically decreasing. It's safe
3652 * to peek without lock.
3653 */
3654 list_for_each_entry(wq, &workqueues, list) {
3655 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3656
Tejun Heo58a69cb2011-02-16 09:25:31 +01003657 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003658 continue;
3659
3660 BUG_ON(cwq->nr_active < 0);
3661 if (cwq->nr_active) {
3662 busy = true;
3663 goto out_unlock;
3664 }
3665 }
3666 }
3667out_unlock:
3668 spin_unlock(&workqueue_lock);
3669 return busy;
3670}
3671
3672/**
3673 * thaw_workqueues - thaw workqueues
3674 *
3675 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003676 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003677 *
3678 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003679 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003680 */
3681void thaw_workqueues(void)
3682{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003683 unsigned int cpu;
3684
3685 spin_lock(&workqueue_lock);
3686
3687 if (!workqueue_freezing)
3688 goto out_unlock;
3689
Tejun Heof3421792010-07-02 10:03:51 +02003690 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003691 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003692 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003693
3694 spin_lock_irq(&gcwq->lock);
3695
Tejun Heodb7bccf2010-06-29 10:07:12 +02003696 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3697 gcwq->flags &= ~GCWQ_FREEZING;
3698
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003699 list_for_each_entry(wq, &workqueues, list) {
3700 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3701
Tejun Heo58a69cb2011-02-16 09:25:31 +01003702 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 continue;
3704
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003705 /* restore max_active and repopulate worklist */
3706 cwq->max_active = wq->saved_max_active;
3707
3708 while (!list_empty(&cwq->delayed_works) &&
3709 cwq->nr_active < cwq->max_active)
3710 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003711 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003712
Tejun Heoe22bee72010-06-29 10:07:14 +02003713 wake_up_worker(gcwq);
3714
Tejun Heo8b03ae32010-06-29 10:07:12 +02003715 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716 }
3717
3718 workqueue_freezing = false;
3719out_unlock:
3720 spin_unlock(&workqueue_lock);
3721}
3722#endif /* CONFIG_FREEZER */
3723
Suresh Siddha6ee05782010-07-30 14:57:37 -07003724static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Tejun Heoc34056a2010-06-29 10:07:11 +02003726 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003727 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003728
Tejun Heof6500942010-08-09 11:50:34 +02003729 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003730
3731 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003732 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003733 struct global_cwq *gcwq = get_gcwq(cpu);
3734
3735 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003736 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003737 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003738 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003739
Tejun Heoc8e55f32010-06-29 10:07:12 +02003740 INIT_LIST_HEAD(&gcwq->idle_list);
3741 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3742 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3743
Tejun Heoe22bee72010-06-29 10:07:14 +02003744 init_timer_deferrable(&gcwq->idle_timer);
3745 gcwq->idle_timer.function = idle_worker_timeout;
3746 gcwq->idle_timer.data = (unsigned long)gcwq;
3747
3748 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3749 (unsigned long)gcwq);
3750
Tejun Heo8b03ae32010-06-29 10:07:12 +02003751 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003752
3753 gcwq->trustee_state = TRUSTEE_DONE;
3754 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003755 }
3756
Tejun Heoe22bee72010-06-29 10:07:14 +02003757 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003758 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003759 struct global_cwq *gcwq = get_gcwq(cpu);
3760 struct worker *worker;
3761
Tejun Heo477a3c32010-08-31 10:54:35 +02003762 if (cpu != WORK_CPU_UNBOUND)
3763 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003764 worker = create_worker(gcwq, true);
3765 BUG_ON(!worker);
3766 spin_lock_irq(&gcwq->lock);
3767 start_worker(worker);
3768 spin_unlock_irq(&gcwq->lock);
3769 }
3770
Tejun Heod320c032010-06-29 10:07:14 +02003771 system_wq = alloc_workqueue("events", 0, 0);
3772 system_long_wq = alloc_workqueue("events_long", 0, 0);
3773 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003774 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3775 WQ_UNBOUND_MAX_ACTIVE);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003776 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
3777 !system_unbound_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003780early_initcall(init_workqueues);