blob: 572f559f6cb992695425f138e94b533a88d8e36f [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
Tejun Heo3233cdb2011-02-16 18:10:19 +010082 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
83 /* call for help after 10ms
84 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
86 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020087 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020088
89 /*
90 * Rescue workers are used only on emergencies and shared by
91 * all cpus. Give -20.
92 */
93 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020094};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020097 * Structure fields follow one of the following exclusion rules.
98 *
Tejun Heoe41e7042010-08-24 14:22:47 +020099 * I: Modifiable by initialization/destruction paths and read-only for
100 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200101 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200102 * P: Preemption protected. Disabling preemption is enough and should
103 * only be modified and accessed from the local cpu.
104 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200105 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200107 * X: During normal operation, modification requires gcwq->lock and
108 * should be done only from local cpu. Either disabling preemption
109 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200110 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200112 * F: wq->flush_mutex protected.
113 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 * W: workqueue_lock protected.
115 */
116
Tejun Heo8b03ae32010-06-29 10:07:12 +0200117struct global_cwq;
Tejun Heoc34056a2010-06-29 10:07:11 +0200118
Tejun Heoe22bee72010-06-29 10:07:14 +0200119/*
120 * The poor guys doing the actual heavy lifting. All on-duty workers
121 * are either serving the manager role, on idle list or on busy hash.
122 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200123struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200124 /* on idle list while idle, on busy hash table while busy */
125 union {
126 struct list_head entry; /* L: while idle */
127 struct hlist_node hentry; /* L: while busy */
128 };
129
Tejun Heoc34056a2010-06-29 10:07:11 +0200130 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200131 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200132 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200133 struct task_struct *task; /* I: worker task */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200134 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heoe22bee72010-06-29 10:07:14 +0200135 /* 64 bytes boundary on 64bit, 32 on 32bit */
136 unsigned long last_active; /* L: last active timestamp */
137 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200139 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200140};
141
Tejun Heo4690c4a2010-06-29 10:07:10 +0200142/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200143 * Global per-cpu workqueue. There's one and only one for each cpu
144 * and all works are queued and processed here regardless of their
145 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200146 */
147struct global_cwq {
148 spinlock_t lock; /* the gcwq lock */
Tejun Heo7e116292010-06-29 10:07:13 +0200149 struct list_head worklist; /* L: list of pending works */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200150 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200151 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200152
153 int nr_workers; /* L: total number of workers */
154 int nr_idle; /* L: currently idle ones */
155
156 /* workers are chained either in the idle_list or busy_hash */
Tejun Heoe22bee72010-06-29 10:07:14 +0200157 struct list_head idle_list; /* X: list of idle workers */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200158 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
159 /* L: hash of busy workers */
160
Tejun Heoe22bee72010-06-29 10:07:14 +0200161 struct timer_list idle_timer; /* L: worker idle timeout */
162 struct timer_list mayday_timer; /* L: SOS timer for dworkers */
163
Tejun Heo8b03ae32010-06-29 10:07:12 +0200164 struct ida worker_ida; /* L: for worker IDs */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200165
166 struct task_struct *trustee; /* L: for gcwq shutdown */
167 unsigned int trustee_state; /* L: trustee state */
168 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heoe22bee72010-06-29 10:07:14 +0200169 struct worker *first_idle; /* L: first idle worker */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200170} ____cacheline_aligned_in_smp;
171
172/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200173 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200174 * work_struct->data are used for flags and thus cwqs need to be
175 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177struct cpu_workqueue_struct {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200178 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200179 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200180 int work_color; /* L: current color */
181 int flush_color; /* L: flushing color */
182 int nr_in_flight[WORK_NR_COLORS];
183 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200184 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200185 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200186 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200190 * Structure used to wait for workqueue flush.
191 */
192struct wq_flusher {
193 struct list_head list; /* F: list of flushers */
194 int flush_color; /* F: flush color waiting for */
195 struct completion done; /* flush completion */
196};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Tejun Heo73f53c42010-06-29 10:07:11 +0200198/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200199 * All cpumasks are assumed to be always set on UP and thus can't be
200 * used to determine whether there's something to be done.
201 */
202#ifdef CONFIG_SMP
203typedef cpumask_var_t mayday_mask_t;
204#define mayday_test_and_set_cpu(cpu, mask) \
205 cpumask_test_and_set_cpu((cpu), (mask))
206#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
207#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200208#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200209#define free_mayday_mask(mask) free_cpumask_var((mask))
210#else
211typedef unsigned long mayday_mask_t;
212#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
213#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
214#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
215#define alloc_mayday_mask(maskp, gfp) true
216#define free_mayday_mask(mask) do { } while (0)
217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/*
220 * The externally visible workqueue abstraction is an array of
221 * per-CPU workqueues:
222 */
223struct workqueue_struct {
Tejun Heo97e37d72010-06-29 10:07:10 +0200224 unsigned int flags; /* I: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200225 union {
226 struct cpu_workqueue_struct __percpu *pcpu;
227 struct cpu_workqueue_struct *single;
228 unsigned long v;
229 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200230 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200231
232 struct mutex flush_mutex; /* protects wq flushing */
233 int work_color; /* F: current work color */
234 int flush_color; /* F: current flush color */
235 atomic_t nr_cwqs_to_flush; /* flush in progress */
236 struct wq_flusher *first_flusher; /* F: first flusher */
237 struct list_head flusher_queue; /* F: flush waiters */
238 struct list_head flusher_overflow; /* F: flush overflow list */
239
Tejun Heof2e005a2010-07-20 15:59:09 +0200240 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200241 struct worker *rescuer; /* I: rescue worker */
242
Tejun Heodcd989c2010-06-29 10:07:14 +0200243 int saved_max_active; /* W: saved cwq max_active */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 const char *name; /* I: workqueue name */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700245#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200246 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Tejun Heod320c032010-06-29 10:07:14 +0200250struct workqueue_struct *system_wq __read_mostly;
251struct workqueue_struct *system_long_wq __read_mostly;
252struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200253struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo4149efb2011-02-08 10:39:03 +0100254struct workqueue_struct *system_freezeable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200255EXPORT_SYMBOL_GPL(system_wq);
256EXPORT_SYMBOL_GPL(system_long_wq);
257EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200258EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo4149efb2011-02-08 10:39:03 +0100259EXPORT_SYMBOL_GPL(system_freezeable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200260
Tejun Heo97bd2342010-10-05 10:41:14 +0200261#define CREATE_TRACE_POINTS
262#include <trace/events/workqueue.h>
263
Tejun Heodb7bccf2010-06-29 10:07:12 +0200264#define for_each_busy_worker(worker, i, pos, gcwq) \
265 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
266 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
267
Tejun Heof3421792010-07-02 10:03:51 +0200268static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
269 unsigned int sw)
270{
271 if (cpu < nr_cpu_ids) {
272 if (sw & 1) {
273 cpu = cpumask_next(cpu, mask);
274 if (cpu < nr_cpu_ids)
275 return cpu;
276 }
277 if (sw & 2)
278 return WORK_CPU_UNBOUND;
279 }
280 return WORK_CPU_NONE;
281}
282
283static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
284 struct workqueue_struct *wq)
285{
286 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
287}
288
Tejun Heo09884952010-08-01 11:50:12 +0200289/*
290 * CPU iterators
291 *
292 * An extra gcwq is defined for an invalid cpu number
293 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
294 * specific CPU. The following iterators are similar to
295 * for_each_*_cpu() iterators but also considers the unbound gcwq.
296 *
297 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
298 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
299 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
300 * WORK_CPU_UNBOUND for unbound workqueues
301 */
Tejun Heof3421792010-07-02 10:03:51 +0200302#define for_each_gcwq_cpu(cpu) \
303 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
304 (cpu) < WORK_CPU_NONE; \
305 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
306
307#define for_each_online_gcwq_cpu(cpu) \
308 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
309 (cpu) < WORK_CPU_NONE; \
310 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
311
312#define for_each_cwq_cpu(cpu, wq) \
313 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
314 (cpu) < WORK_CPU_NONE; \
315 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
316
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900317#ifdef CONFIG_DEBUG_OBJECTS_WORK
318
319static struct debug_obj_descr work_debug_descr;
320
321/*
322 * fixup_init is called when:
323 * - an active object is initialized
324 */
325static int work_fixup_init(void *addr, enum debug_obj_state state)
326{
327 struct work_struct *work = addr;
328
329 switch (state) {
330 case ODEBUG_STATE_ACTIVE:
331 cancel_work_sync(work);
332 debug_object_init(work, &work_debug_descr);
333 return 1;
334 default:
335 return 0;
336 }
337}
338
339/*
340 * fixup_activate is called when:
341 * - an active object is activated
342 * - an unknown object is activated (might be a statically initialized object)
343 */
344static int work_fixup_activate(void *addr, enum debug_obj_state state)
345{
346 struct work_struct *work = addr;
347
348 switch (state) {
349
350 case ODEBUG_STATE_NOTAVAILABLE:
351 /*
352 * This is not really a fixup. The work struct was
353 * statically initialized. We just make sure that it
354 * is tracked in the object tracker.
355 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200356 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900357 debug_object_init(work, &work_debug_descr);
358 debug_object_activate(work, &work_debug_descr);
359 return 0;
360 }
361 WARN_ON_ONCE(1);
362 return 0;
363
364 case ODEBUG_STATE_ACTIVE:
365 WARN_ON(1);
366
367 default:
368 return 0;
369 }
370}
371
372/*
373 * fixup_free is called when:
374 * - an active object is freed
375 */
376static int work_fixup_free(void *addr, enum debug_obj_state state)
377{
378 struct work_struct *work = addr;
379
380 switch (state) {
381 case ODEBUG_STATE_ACTIVE:
382 cancel_work_sync(work);
383 debug_object_free(work, &work_debug_descr);
384 return 1;
385 default:
386 return 0;
387 }
388}
389
390static struct debug_obj_descr work_debug_descr = {
391 .name = "work_struct",
392 .fixup_init = work_fixup_init,
393 .fixup_activate = work_fixup_activate,
394 .fixup_free = work_fixup_free,
395};
396
397static inline void debug_work_activate(struct work_struct *work)
398{
399 debug_object_activate(work, &work_debug_descr);
400}
401
402static inline void debug_work_deactivate(struct work_struct *work)
403{
404 debug_object_deactivate(work, &work_debug_descr);
405}
406
407void __init_work(struct work_struct *work, int onstack)
408{
409 if (onstack)
410 debug_object_init_on_stack(work, &work_debug_descr);
411 else
412 debug_object_init(work, &work_debug_descr);
413}
414EXPORT_SYMBOL_GPL(__init_work);
415
416void destroy_work_on_stack(struct work_struct *work)
417{
418 debug_object_free(work, &work_debug_descr);
419}
420EXPORT_SYMBOL_GPL(destroy_work_on_stack);
421
422#else
423static inline void debug_work_activate(struct work_struct *work) { }
424static inline void debug_work_deactivate(struct work_struct *work) { }
425#endif
426
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100427/* Serializes the accesses to the list of workqueues. */
428static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200430static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Oleg Nesterov14441962007-05-23 13:57:57 -0700432/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200433 * The almighty global cpu workqueues. nr_running is the only field
434 * which is expected to be used frequently by other cpus via
435 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700436 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200437static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200438static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800439
Tejun Heof3421792010-07-02 10:03:51 +0200440/*
441 * Global cpu workqueue and nr_running counter for unbound gcwq. The
442 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
443 * workers have WORKER_UNBOUND set.
444 */
445static struct global_cwq unbound_global_cwq;
446static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
447
Tejun Heoc34056a2010-06-29 10:07:11 +0200448static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Tejun Heo8b03ae32010-06-29 10:07:12 +0200450static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Tejun Heof3421792010-07-02 10:03:51 +0200452 if (cpu != WORK_CPU_UNBOUND)
453 return &per_cpu(global_cwq, cpu);
454 else
455 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Tejun Heoe22bee72010-06-29 10:07:14 +0200458static atomic_t *get_gcwq_nr_running(unsigned int cpu)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700459{
Tejun Heof3421792010-07-02 10:03:51 +0200460 if (cpu != WORK_CPU_UNBOUND)
461 return &per_cpu(gcwq_nr_running, cpu);
462 else
463 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700464}
465
Tejun Heo4690c4a2010-06-29 10:07:10 +0200466static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
467 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700468{
Tejun Heof3421792010-07-02 10:03:51 +0200469 if (!(wq->flags & WQ_UNBOUND)) {
470 if (likely(cpu < nr_cpu_ids)) {
471#ifdef CONFIG_SMP
472 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200473#else
Tejun Heof3421792010-07-02 10:03:51 +0200474 return wq->cpu_wq.single;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200475#endif
Tejun Heof3421792010-07-02 10:03:51 +0200476 }
477 } else if (likely(cpu == WORK_CPU_UNBOUND))
478 return wq->cpu_wq.single;
479 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700480}
481
Tejun Heo73f53c42010-06-29 10:07:11 +0200482static unsigned int work_color_to_flags(int color)
483{
484 return color << WORK_STRUCT_COLOR_SHIFT;
485}
486
487static int get_work_color(struct work_struct *work)
488{
489 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
490 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
491}
492
493static int work_next_color(int color)
494{
495 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
David Howells4594bf12006-12-07 11:33:26 +0000498/*
Tejun Heoe1201532010-07-22 14:14:25 +0200499 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
500 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
501 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200502 *
503 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
504 * cwq, cpu or clear work->data. These functions should only be
505 * called while the work is owned - ie. while the PENDING bit is set.
506 *
507 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
508 * corresponding to a work. gcwq is available once the work has been
509 * queued anywhere after initialization. cwq is available only from
510 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000511 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200512static inline void set_work_data(struct work_struct *work, unsigned long data,
513 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000514{
David Howells4594bf12006-12-07 11:33:26 +0000515 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200516 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000517}
David Howells365970a2006-11-22 14:54:49 +0000518
Tejun Heo7a22ad72010-06-29 10:07:13 +0200519static void set_work_cwq(struct work_struct *work,
520 struct cpu_workqueue_struct *cwq,
521 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200522{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200523 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200524 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200525}
526
Tejun Heo7a22ad72010-06-29 10:07:13 +0200527static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000528{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
530}
531
532static void clear_work_data(struct work_struct *work)
533{
534 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
535}
536
Tejun Heo7a22ad72010-06-29 10:07:13 +0200537static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
538{
Tejun Heoe1201532010-07-22 14:14:25 +0200539 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540
Tejun Heoe1201532010-07-22 14:14:25 +0200541 if (data & WORK_STRUCT_CWQ)
542 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
543 else
544 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545}
546
547static struct global_cwq *get_work_gcwq(struct work_struct *work)
548{
Tejun Heoe1201532010-07-22 14:14:25 +0200549 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550 unsigned int cpu;
551
Tejun Heoe1201532010-07-22 14:14:25 +0200552 if (data & WORK_STRUCT_CWQ)
553 return ((struct cpu_workqueue_struct *)
554 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555
556 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200557 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 return NULL;
559
Tejun Heof3421792010-07-02 10:03:51 +0200560 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000562}
563
564/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200565 * Policy functions. These define the policies on how the global
566 * worker pool is managed. Unless noted otherwise, these functions
567 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000568 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200569
Tejun Heo649027d2010-06-29 10:07:14 +0200570static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000571{
Tejun Heo649027d2010-06-29 10:07:14 +0200572 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
573 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000574}
575
Tejun Heoe22bee72010-06-29 10:07:14 +0200576/*
577 * Need to wake up a worker? Called from anything but currently
578 * running workers.
579 */
580static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000581{
Tejun Heo649027d2010-06-29 10:07:14 +0200582 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000583}
584
Tejun Heoe22bee72010-06-29 10:07:14 +0200585/* Can I start working? Called from busy but !running workers. */
586static bool may_start_working(struct global_cwq *gcwq)
587{
588 return gcwq->nr_idle;
589}
590
591/* Do I need to keep working? Called from currently running workers. */
592static bool keep_working(struct global_cwq *gcwq)
593{
594 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
595
Tejun Heo30310042010-10-11 11:51:57 +0200596 return !list_empty(&gcwq->worklist) &&
597 (atomic_read(nr_running) <= 1 ||
598 gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200599}
600
601/* Do we need a new worker? Called from manager. */
602static bool need_to_create_worker(struct global_cwq *gcwq)
603{
604 return need_more_worker(gcwq) && !may_start_working(gcwq);
605}
606
607/* Do I need to be the manager? */
608static bool need_to_manage_workers(struct global_cwq *gcwq)
609{
610 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
611}
612
613/* Do we have too many workers and should some go away? */
614static bool too_many_workers(struct global_cwq *gcwq)
615{
616 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
617 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
618 int nr_busy = gcwq->nr_workers - nr_idle;
619
620 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
621}
622
623/*
624 * Wake up functions.
625 */
626
Tejun Heo7e116292010-06-29 10:07:13 +0200627/* Return the first worker. Safe with preemption disabled */
628static struct worker *first_worker(struct global_cwq *gcwq)
629{
630 if (unlikely(list_empty(&gcwq->idle_list)))
631 return NULL;
632
633 return list_first_entry(&gcwq->idle_list, struct worker, entry);
634}
635
636/**
637 * wake_up_worker - wake up an idle worker
638 * @gcwq: gcwq to wake worker for
639 *
640 * Wake up the first idle worker of @gcwq.
641 *
642 * CONTEXT:
643 * spin_lock_irq(gcwq->lock).
644 */
645static void wake_up_worker(struct global_cwq *gcwq)
646{
647 struct worker *worker = first_worker(gcwq);
648
649 if (likely(worker))
650 wake_up_process(worker->task);
651}
652
Tejun Heo4690c4a2010-06-29 10:07:10 +0200653/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200654 * wq_worker_waking_up - a worker is waking up
655 * @task: task waking up
656 * @cpu: CPU @task is waking up to
657 *
658 * This function is called during try_to_wake_up() when a worker is
659 * being awoken.
660 *
661 * CONTEXT:
662 * spin_lock_irq(rq->lock)
663 */
664void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
665{
666 struct worker *worker = kthread_data(task);
667
Steven Rostedt2d646722010-12-03 23:12:33 -0500668 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe22bee72010-06-29 10:07:14 +0200669 atomic_inc(get_gcwq_nr_running(cpu));
670}
671
672/**
673 * wq_worker_sleeping - a worker is going to sleep
674 * @task: task going to sleep
675 * @cpu: CPU in question, must be the current CPU number
676 *
677 * This function is called during schedule() when a busy worker is
678 * going to sleep. Worker on the same cpu can be woken up by
679 * returning pointer to its task.
680 *
681 * CONTEXT:
682 * spin_lock_irq(rq->lock)
683 *
684 * RETURNS:
685 * Worker task on @cpu to wake up, %NULL if none.
686 */
687struct task_struct *wq_worker_sleeping(struct task_struct *task,
688 unsigned int cpu)
689{
690 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
691 struct global_cwq *gcwq = get_gcwq(cpu);
692 atomic_t *nr_running = get_gcwq_nr_running(cpu);
693
Steven Rostedt2d646722010-12-03 23:12:33 -0500694 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200695 return NULL;
696
697 /* this can only happen on the local cpu */
698 BUG_ON(cpu != raw_smp_processor_id());
699
700 /*
701 * The counterpart of the following dec_and_test, implied mb,
702 * worklist not empty test sequence is in insert_work().
703 * Please read comment there.
704 *
705 * NOT_RUNNING is clear. This means that trustee is not in
706 * charge and we're running on the local cpu w/ rq lock held
707 * and preemption disabled, which in turn means that none else
708 * could be manipulating idle_list, so dereferencing idle_list
709 * without gcwq lock is safe.
710 */
711 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
712 to_wakeup = first_worker(gcwq);
713 return to_wakeup ? to_wakeup->task : NULL;
714}
715
716/**
717 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200718 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200719 * @flags: flags to set
720 * @wakeup: wakeup an idle worker if necessary
721 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200722 * Set @flags in @worker->flags and adjust nr_running accordingly. If
723 * nr_running becomes zero and @wakeup is %true, an idle worker is
724 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200725 *
Tejun Heocb444762010-07-02 10:03:50 +0200726 * CONTEXT:
727 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200728 */
729static inline void worker_set_flags(struct worker *worker, unsigned int flags,
730 bool wakeup)
731{
Tejun Heoe22bee72010-06-29 10:07:14 +0200732 struct global_cwq *gcwq = worker->gcwq;
733
Tejun Heocb444762010-07-02 10:03:50 +0200734 WARN_ON_ONCE(worker->task != current);
735
Tejun Heoe22bee72010-06-29 10:07:14 +0200736 /*
737 * If transitioning into NOT_RUNNING, adjust nr_running and
738 * wake up an idle worker as necessary if requested by
739 * @wakeup.
740 */
741 if ((flags & WORKER_NOT_RUNNING) &&
742 !(worker->flags & WORKER_NOT_RUNNING)) {
743 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
744
745 if (wakeup) {
746 if (atomic_dec_and_test(nr_running) &&
747 !list_empty(&gcwq->worklist))
748 wake_up_worker(gcwq);
749 } else
750 atomic_dec(nr_running);
751 }
752
Tejun Heod302f012010-06-29 10:07:13 +0200753 worker->flags |= flags;
754}
755
756/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200757 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200758 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200759 * @flags: flags to clear
760 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200761 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200762 *
Tejun Heocb444762010-07-02 10:03:50 +0200763 * CONTEXT:
764 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200765 */
766static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
767{
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 struct global_cwq *gcwq = worker->gcwq;
769 unsigned int oflags = worker->flags;
770
Tejun Heocb444762010-07-02 10:03:50 +0200771 WARN_ON_ONCE(worker->task != current);
772
Tejun Heod302f012010-06-29 10:07:13 +0200773 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200774
Tejun Heo42c025f2011-01-11 15:58:49 +0100775 /*
776 * If transitioning out of NOT_RUNNING, increment nr_running. Note
777 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
778 * of multiple flags, not a single flag.
779 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200780 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
781 if (!(worker->flags & WORKER_NOT_RUNNING))
782 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200783}
784
785/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200786 * busy_worker_head - return the busy hash head for a work
787 * @gcwq: gcwq of interest
788 * @work: work to be hashed
789 *
790 * Return hash head of @gcwq for @work.
791 *
792 * CONTEXT:
793 * spin_lock_irq(gcwq->lock).
794 *
795 * RETURNS:
796 * Pointer to the hash head.
797 */
798static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
799 struct work_struct *work)
800{
801 const int base_shift = ilog2(sizeof(struct work_struct));
802 unsigned long v = (unsigned long)work;
803
804 /* simple shift and fold hash, do we need something better? */
805 v >>= base_shift;
806 v += v >> BUSY_WORKER_HASH_ORDER;
807 v &= BUSY_WORKER_HASH_MASK;
808
809 return &gcwq->busy_hash[v];
810}
811
812/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200813 * __find_worker_executing_work - find worker which is executing a work
814 * @gcwq: gcwq of interest
815 * @bwh: hash head as returned by busy_worker_head()
816 * @work: work to find worker for
817 *
818 * Find a worker which is executing @work on @gcwq. @bwh should be
819 * the hash head obtained by calling busy_worker_head() with the same
820 * work.
821 *
822 * CONTEXT:
823 * spin_lock_irq(gcwq->lock).
824 *
825 * RETURNS:
826 * Pointer to worker which is executing @work if found, NULL
827 * otherwise.
828 */
829static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
830 struct hlist_head *bwh,
831 struct work_struct *work)
832{
833 struct worker *worker;
834 struct hlist_node *tmp;
835
836 hlist_for_each_entry(worker, tmp, bwh, hentry)
837 if (worker->current_work == work)
838 return worker;
839 return NULL;
840}
841
842/**
843 * find_worker_executing_work - find worker which is executing a work
844 * @gcwq: gcwq of interest
845 * @work: work to find worker for
846 *
847 * Find a worker which is executing @work on @gcwq. This function is
848 * identical to __find_worker_executing_work() except that this
849 * function calculates @bwh itself.
850 *
851 * CONTEXT:
852 * spin_lock_irq(gcwq->lock).
853 *
854 * RETURNS:
855 * Pointer to worker which is executing @work if found, NULL
856 * otherwise.
857 */
858static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
859 struct work_struct *work)
860{
861 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
862 work);
863}
864
865/**
Tejun Heo649027d2010-06-29 10:07:14 +0200866 * gcwq_determine_ins_pos - find insertion position
867 * @gcwq: gcwq of interest
868 * @cwq: cwq a work is being queued for
869 *
870 * A work for @cwq is about to be queued on @gcwq, determine insertion
871 * position for the work. If @cwq is for HIGHPRI wq, the work is
872 * queued at the head of the queue but in FIFO order with respect to
873 * other HIGHPRI works; otherwise, at the end of the queue. This
874 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
875 * there are HIGHPRI works pending.
876 *
877 * CONTEXT:
878 * spin_lock_irq(gcwq->lock).
879 *
880 * RETURNS:
881 * Pointer to inserstion position.
882 */
883static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
884 struct cpu_workqueue_struct *cwq)
885{
886 struct work_struct *twork;
887
888 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
889 return &gcwq->worklist;
890
891 list_for_each_entry(twork, &gcwq->worklist, entry) {
892 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
893
894 if (!(tcwq->wq->flags & WQ_HIGHPRI))
895 break;
896 }
897
898 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
899 return &twork->entry;
900}
901
902/**
Tejun Heo7e116292010-06-29 10:07:13 +0200903 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200904 * @cwq: cwq @work belongs to
905 * @work: work to insert
906 * @head: insertion point
907 * @extra_flags: extra WORK_STRUCT_* flags to set
908 *
Tejun Heo7e116292010-06-29 10:07:13 +0200909 * Insert @work which belongs to @cwq into @gcwq after @head.
910 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200911 *
912 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200913 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200914 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700915static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200916 struct work_struct *work, struct list_head *head,
917 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700918{
Tejun Heoe22bee72010-06-29 10:07:14 +0200919 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100920
Tejun Heo4690c4a2010-06-29 10:07:10 +0200921 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200922 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200923
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700924 /*
925 * Ensure that we get the right work->data if we see the
926 * result of list_add() below, see try_to_grab_pending().
927 */
928 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200929
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700930 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200931
932 /*
933 * Ensure either worker_sched_deactivated() sees the above
934 * list_add_tail() or we see zero nr_running to avoid workers
935 * lying around lazily while there are works to be processed.
936 */
937 smp_mb();
938
Tejun Heo649027d2010-06-29 10:07:14 +0200939 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200940 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700941}
942
Tejun Heoc8efcc22010-12-20 19:32:04 +0100943/*
944 * Test whether @work is being queued from another work executing on the
945 * same workqueue. This is rather expensive and should only be used from
946 * cold paths.
947 */
948static bool is_chained_work(struct workqueue_struct *wq)
949{
950 unsigned long flags;
951 unsigned int cpu;
952
953 for_each_gcwq_cpu(cpu) {
954 struct global_cwq *gcwq = get_gcwq(cpu);
955 struct worker *worker;
956 struct hlist_node *pos;
957 int i;
958
959 spin_lock_irqsave(&gcwq->lock, flags);
960 for_each_busy_worker(worker, i, pos, gcwq) {
961 if (worker->task != current)
962 continue;
963 spin_unlock_irqrestore(&gcwq->lock, flags);
964 /*
965 * I'm @worker, no locking necessary. See if @work
966 * is headed to the same workqueue.
967 */
968 return worker->current_cwq->wq == wq;
969 }
970 spin_unlock_irqrestore(&gcwq->lock, flags);
971 }
972 return false;
973}
974
Tejun Heo4690c4a2010-06-29 10:07:10 +0200975static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct work_struct *work)
977{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200978 struct global_cwq *gcwq;
979 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200980 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200981 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned long flags;
983
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900984 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200985
Tejun Heoc8efcc22010-12-20 19:32:04 +0100986 /* if dying, only works from the same workqueue are allowed */
987 if (unlikely(wq->flags & WQ_DYING) &&
988 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200989 return;
990
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200991 /* determine gcwq to use */
992 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200993 struct global_cwq *last_gcwq;
994
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200995 if (unlikely(cpu == WORK_CPU_UNBOUND))
996 cpu = raw_smp_processor_id();
997
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200998 /*
999 * It's multi cpu. If @wq is non-reentrant and @work
1000 * was previously on a different cpu, it might still
1001 * be running there, in which case the work needs to
1002 * be queued on that cpu to guarantee non-reentrance.
1003 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001004 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001005 if (wq->flags & WQ_NON_REENTRANT &&
1006 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1007 struct worker *worker;
1008
1009 spin_lock_irqsave(&last_gcwq->lock, flags);
1010
1011 worker = find_worker_executing_work(last_gcwq, work);
1012
1013 if (worker && worker->current_cwq->wq == wq)
1014 gcwq = last_gcwq;
1015 else {
1016 /* meh... not running there, queue here */
1017 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1018 spin_lock_irqsave(&gcwq->lock, flags);
1019 }
1020 } else
1021 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001022 } else {
1023 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1024 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001025 }
1026
1027 /* gcwq determined, get cwq and queue */
1028 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001029 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001030
Tejun Heo4690c4a2010-06-29 10:07:10 +02001031 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001032
Tejun Heo73f53c42010-06-29 10:07:11 +02001033 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001034 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001035
1036 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001037 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001038 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001039 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001040 } else {
1041 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001042 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001043 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001044
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001045 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001046
Tejun Heo8b03ae32010-06-29 10:07:12 +02001047 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001050/**
1051 * queue_work - queue work on a workqueue
1052 * @wq: workqueue to use
1053 * @work: work to queue
1054 *
Alan Stern057647f2006-10-28 10:38:58 -07001055 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001057 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1058 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001060int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001062 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001064 ret = queue_work_on(get_cpu(), wq, work);
1065 put_cpu();
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return ret;
1068}
Dave Jonesae90dd52006-06-30 01:40:45 -04001069EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Zhang Ruic1a220e2008-07-23 21:28:39 -07001071/**
1072 * queue_work_on - queue work on specific cpu
1073 * @cpu: CPU number to execute work on
1074 * @wq: workqueue to use
1075 * @work: work to queue
1076 *
1077 * Returns 0 if @work was already on a queue, non-zero otherwise.
1078 *
1079 * We queue the work to a specific CPU, the caller must ensure it
1080 * can't go away.
1081 */
1082int
1083queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1084{
1085 int ret = 0;
1086
Tejun Heo22df02b2010-06-29 10:07:10 +02001087 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001088 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001089 ret = 1;
1090 }
1091 return ret;
1092}
1093EXPORT_SYMBOL_GPL(queue_work_on);
1094
Li Zefan6d141c32008-02-08 04:21:09 -08001095static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
David Howells52bad642006-11-22 14:54:01 +00001097 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001098 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Tejun Heo4690c4a2010-06-29 10:07:10 +02001100 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001103/**
1104 * queue_delayed_work - queue work on a workqueue after delay
1105 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001106 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001107 * @delay: number of jiffies to wait before queueing
1108 *
Alan Stern057647f2006-10-28 10:38:58 -07001109 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001110 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001111int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001112 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
David Howells52bad642006-11-22 14:54:01 +00001114 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001115 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001117 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
Dave Jonesae90dd52006-06-30 01:40:45 -04001119EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001121/**
1122 * queue_delayed_work_on - queue work on specific CPU after delay
1123 * @cpu: CPU number to execute work on
1124 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001125 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001126 * @delay: number of jiffies to wait before queueing
1127 *
Alan Stern057647f2006-10-28 10:38:58 -07001128 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001129 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001130int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001131 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001132{
1133 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001134 struct timer_list *timer = &dwork->timer;
1135 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001136
Tejun Heo22df02b2010-06-29 10:07:10 +02001137 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001138 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001139
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001140 BUG_ON(timer_pending(timer));
1141 BUG_ON(!list_empty(&work->entry));
1142
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001143 timer_stats_timer_set_start_info(&dwork->timer);
1144
Tejun Heo7a22ad72010-06-29 10:07:13 +02001145 /*
1146 * This stores cwq for the moment, for the timer_fn.
1147 * Note that the work's gcwq is preserved to allow
1148 * reentrance detection for delayed works.
1149 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001150 if (!(wq->flags & WQ_UNBOUND)) {
1151 struct global_cwq *gcwq = get_work_gcwq(work);
1152
1153 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1154 lcpu = gcwq->cpu;
1155 else
1156 lcpu = raw_smp_processor_id();
1157 } else
1158 lcpu = WORK_CPU_UNBOUND;
1159
Tejun Heo7a22ad72010-06-29 10:07:13 +02001160 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001161
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001162 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001163 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001164 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001165
1166 if (unlikely(cpu >= 0))
1167 add_timer_on(timer, cpu);
1168 else
1169 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001170 ret = 1;
1171 }
1172 return ret;
1173}
Dave Jonesae90dd52006-06-30 01:40:45 -04001174EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Tejun Heoc8e55f32010-06-29 10:07:12 +02001176/**
1177 * worker_enter_idle - enter idle state
1178 * @worker: worker which is entering idle state
1179 *
1180 * @worker is entering idle state. Update stats and idle timer if
1181 * necessary.
1182 *
1183 * LOCKING:
1184 * spin_lock_irq(gcwq->lock).
1185 */
1186static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001188 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Tejun Heoc8e55f32010-06-29 10:07:12 +02001190 BUG_ON(worker->flags & WORKER_IDLE);
1191 BUG_ON(!list_empty(&worker->entry) &&
1192 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Tejun Heocb444762010-07-02 10:03:50 +02001194 /* can't use worker_set_flags(), also called from start_worker() */
1195 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001196 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001197 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001198
Tejun Heoc8e55f32010-06-29 10:07:12 +02001199 /* idle_list is LIFO */
1200 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001201
Tejun Heoe22bee72010-06-29 10:07:14 +02001202 if (likely(!(worker->flags & WORKER_ROGUE))) {
1203 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1204 mod_timer(&gcwq->idle_timer,
1205 jiffies + IDLE_WORKER_TIMEOUT);
1206 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001207 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001208
1209 /* sanity check nr_running */
1210 WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
1211 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Tejun Heoc8e55f32010-06-29 10:07:12 +02001214/**
1215 * worker_leave_idle - leave idle state
1216 * @worker: worker which is leaving idle state
1217 *
1218 * @worker is leaving idle state. Update stats.
1219 *
1220 * LOCKING:
1221 * spin_lock_irq(gcwq->lock).
1222 */
1223static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001225 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Tejun Heoc8e55f32010-06-29 10:07:12 +02001227 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001228 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001229 gcwq->nr_idle--;
1230 list_del_init(&worker->entry);
1231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Tejun Heoe22bee72010-06-29 10:07:14 +02001233/**
1234 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1235 * @worker: self
1236 *
1237 * Works which are scheduled while the cpu is online must at least be
1238 * scheduled to a worker which is bound to the cpu so that if they are
1239 * flushed from cpu callbacks while cpu is going down, they are
1240 * guaranteed to execute on the cpu.
1241 *
1242 * This function is to be used by rogue workers and rescuers to bind
1243 * themselves to the target cpu and may race with cpu going down or
1244 * coming online. kthread_bind() can't be used because it may put the
1245 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1246 * verbatim as it's best effort and blocking and gcwq may be
1247 * [dis]associated in the meantime.
1248 *
1249 * This function tries set_cpus_allowed() and locks gcwq and verifies
1250 * the binding against GCWQ_DISASSOCIATED which is set during
1251 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1252 * idle state or fetches works without dropping lock, it can guarantee
1253 * the scheduling requirement described in the first paragraph.
1254 *
1255 * CONTEXT:
1256 * Might sleep. Called without any lock but returns with gcwq->lock
1257 * held.
1258 *
1259 * RETURNS:
1260 * %true if the associated gcwq is online (@worker is successfully
1261 * bound), %false if offline.
1262 */
1263static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001264__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001265{
1266 struct global_cwq *gcwq = worker->gcwq;
1267 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Tejun Heoe22bee72010-06-29 10:07:14 +02001269 while (true) {
1270 /*
1271 * The following call may fail, succeed or succeed
1272 * without actually migrating the task to the cpu if
1273 * it races with cpu hotunplug operation. Verify
1274 * against GCWQ_DISASSOCIATED.
1275 */
Tejun Heof3421792010-07-02 10:03:51 +02001276 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1277 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001278
Tejun Heoe22bee72010-06-29 10:07:14 +02001279 spin_lock_irq(&gcwq->lock);
1280 if (gcwq->flags & GCWQ_DISASSOCIATED)
1281 return false;
1282 if (task_cpu(task) == gcwq->cpu &&
1283 cpumask_equal(&current->cpus_allowed,
1284 get_cpu_mask(gcwq->cpu)))
1285 return true;
1286 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001287
Tejun Heoe22bee72010-06-29 10:07:14 +02001288 /* CPU has come up inbetween, retry migration */
1289 cpu_relax();
1290 }
1291}
1292
1293/*
1294 * Function for worker->rebind_work used to rebind rogue busy workers
1295 * to the associated cpu which is coming back online. This is
1296 * scheduled by cpu up but can race with other cpu hotplug operations
1297 * and may be executed twice without intervening cpu down.
1298 */
1299static void worker_rebind_fn(struct work_struct *work)
1300{
1301 struct worker *worker = container_of(work, struct worker, rebind_work);
1302 struct global_cwq *gcwq = worker->gcwq;
1303
1304 if (worker_maybe_bind_and_lock(worker))
1305 worker_clr_flags(worker, WORKER_REBIND);
1306
1307 spin_unlock_irq(&gcwq->lock);
1308}
1309
Tejun Heoc34056a2010-06-29 10:07:11 +02001310static struct worker *alloc_worker(void)
1311{
1312 struct worker *worker;
1313
1314 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001315 if (worker) {
1316 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001317 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001318 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1319 /* on creation a worker is in !idle && prep state */
1320 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001321 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001322 return worker;
1323}
1324
1325/**
1326 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001327 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001328 * @bind: whether to set affinity to @cpu or not
1329 *
Tejun Heo7e116292010-06-29 10:07:13 +02001330 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001331 * can be started by calling start_worker() or destroyed using
1332 * destroy_worker().
1333 *
1334 * CONTEXT:
1335 * Might sleep. Does GFP_KERNEL allocations.
1336 *
1337 * RETURNS:
1338 * Pointer to the newly created worker.
1339 */
Tejun Heo7e116292010-06-29 10:07:13 +02001340static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001341{
Tejun Heof3421792010-07-02 10:03:51 +02001342 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001343 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001344 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001345
Tejun Heo8b03ae32010-06-29 10:07:12 +02001346 spin_lock_irq(&gcwq->lock);
1347 while (ida_get_new(&gcwq->worker_ida, &id)) {
1348 spin_unlock_irq(&gcwq->lock);
1349 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001350 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001351 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001352 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001353 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001354
1355 worker = alloc_worker();
1356 if (!worker)
1357 goto fail;
1358
Tejun Heo8b03ae32010-06-29 10:07:12 +02001359 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001360 worker->id = id;
1361
Tejun Heof3421792010-07-02 10:03:51 +02001362 if (!on_unbound_cpu)
1363 worker->task = kthread_create(worker_thread, worker,
1364 "kworker/%u:%d", gcwq->cpu, id);
1365 else
1366 worker->task = kthread_create(worker_thread, worker,
1367 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001368 if (IS_ERR(worker->task))
1369 goto fail;
1370
Tejun Heodb7bccf2010-06-29 10:07:12 +02001371 /*
1372 * A rogue worker will become a regular one if CPU comes
1373 * online later on. Make sure every worker has
1374 * PF_THREAD_BOUND set.
1375 */
Tejun Heof3421792010-07-02 10:03:51 +02001376 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001377 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001378 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001379 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001380 if (on_unbound_cpu)
1381 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001383
Tejun Heoc34056a2010-06-29 10:07:11 +02001384 return worker;
1385fail:
1386 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001387 spin_lock_irq(&gcwq->lock);
1388 ida_remove(&gcwq->worker_ida, id);
1389 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001390 }
1391 kfree(worker);
1392 return NULL;
1393}
1394
1395/**
1396 * start_worker - start a newly created worker
1397 * @worker: worker to start
1398 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001399 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001400 *
1401 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001402 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001403 */
1404static void start_worker(struct worker *worker)
1405{
Tejun Heocb444762010-07-02 10:03:50 +02001406 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001407 worker->gcwq->nr_workers++;
1408 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001409 wake_up_process(worker->task);
1410}
1411
1412/**
1413 * destroy_worker - destroy a workqueue worker
1414 * @worker: worker to be destroyed
1415 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001416 * Destroy @worker and adjust @gcwq stats accordingly.
1417 *
1418 * CONTEXT:
1419 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001420 */
1421static void destroy_worker(struct worker *worker)
1422{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001423 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001424 int id = worker->id;
1425
1426 /* sanity check frenzy */
1427 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001428 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001429
Tejun Heoc8e55f32010-06-29 10:07:12 +02001430 if (worker->flags & WORKER_STARTED)
1431 gcwq->nr_workers--;
1432 if (worker->flags & WORKER_IDLE)
1433 gcwq->nr_idle--;
1434
1435 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001436 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001437
1438 spin_unlock_irq(&gcwq->lock);
1439
Tejun Heoc34056a2010-06-29 10:07:11 +02001440 kthread_stop(worker->task);
1441 kfree(worker);
1442
Tejun Heo8b03ae32010-06-29 10:07:12 +02001443 spin_lock_irq(&gcwq->lock);
1444 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001445}
1446
Tejun Heoe22bee72010-06-29 10:07:14 +02001447static void idle_worker_timeout(unsigned long __gcwq)
1448{
1449 struct global_cwq *gcwq = (void *)__gcwq;
1450
1451 spin_lock_irq(&gcwq->lock);
1452
1453 if (too_many_workers(gcwq)) {
1454 struct worker *worker;
1455 unsigned long expires;
1456
1457 /* idle_list is kept in LIFO order, check the last one */
1458 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1459 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1460
1461 if (time_before(jiffies, expires))
1462 mod_timer(&gcwq->idle_timer, expires);
1463 else {
1464 /* it's been idle for too long, wake up manager */
1465 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1466 wake_up_worker(gcwq);
1467 }
1468 }
1469
1470 spin_unlock_irq(&gcwq->lock);
1471}
1472
1473static bool send_mayday(struct work_struct *work)
1474{
1475 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1476 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001477 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001478
1479 if (!(wq->flags & WQ_RESCUER))
1480 return false;
1481
1482 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001483 cpu = cwq->gcwq->cpu;
1484 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1485 if (cpu == WORK_CPU_UNBOUND)
1486 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001487 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001488 wake_up_process(wq->rescuer->task);
1489 return true;
1490}
1491
1492static void gcwq_mayday_timeout(unsigned long __gcwq)
1493{
1494 struct global_cwq *gcwq = (void *)__gcwq;
1495 struct work_struct *work;
1496
1497 spin_lock_irq(&gcwq->lock);
1498
1499 if (need_to_create_worker(gcwq)) {
1500 /*
1501 * We've been trying to create a new worker but
1502 * haven't been successful. We might be hitting an
1503 * allocation deadlock. Send distress signals to
1504 * rescuers.
1505 */
1506 list_for_each_entry(work, &gcwq->worklist, entry)
1507 send_mayday(work);
1508 }
1509
1510 spin_unlock_irq(&gcwq->lock);
1511
1512 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1513}
1514
1515/**
1516 * maybe_create_worker - create a new worker if necessary
1517 * @gcwq: gcwq to create a new worker for
1518 *
1519 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1520 * have at least one idle worker on return from this function. If
1521 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1522 * sent to all rescuers with works scheduled on @gcwq to resolve
1523 * possible allocation deadlock.
1524 *
1525 * On return, need_to_create_worker() is guaranteed to be false and
1526 * may_start_working() true.
1527 *
1528 * LOCKING:
1529 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1530 * multiple times. Does GFP_KERNEL allocations. Called only from
1531 * manager.
1532 *
1533 * RETURNS:
1534 * false if no action was taken and gcwq->lock stayed locked, true
1535 * otherwise.
1536 */
1537static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001538__releases(&gcwq->lock)
1539__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001540{
1541 if (!need_to_create_worker(gcwq))
1542 return false;
1543restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001544 spin_unlock_irq(&gcwq->lock);
1545
Tejun Heoe22bee72010-06-29 10:07:14 +02001546 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1547 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1548
1549 while (true) {
1550 struct worker *worker;
1551
Tejun Heoe22bee72010-06-29 10:07:14 +02001552 worker = create_worker(gcwq, true);
1553 if (worker) {
1554 del_timer_sync(&gcwq->mayday_timer);
1555 spin_lock_irq(&gcwq->lock);
1556 start_worker(worker);
1557 BUG_ON(need_to_create_worker(gcwq));
1558 return true;
1559 }
1560
1561 if (!need_to_create_worker(gcwq))
1562 break;
1563
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 __set_current_state(TASK_INTERRUPTIBLE);
1565 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001566
Tejun Heoe22bee72010-06-29 10:07:14 +02001567 if (!need_to_create_worker(gcwq))
1568 break;
1569 }
1570
Tejun Heoe22bee72010-06-29 10:07:14 +02001571 del_timer_sync(&gcwq->mayday_timer);
1572 spin_lock_irq(&gcwq->lock);
1573 if (need_to_create_worker(gcwq))
1574 goto restart;
1575 return true;
1576}
1577
1578/**
1579 * maybe_destroy_worker - destroy workers which have been idle for a while
1580 * @gcwq: gcwq to destroy workers for
1581 *
1582 * Destroy @gcwq workers which have been idle for longer than
1583 * IDLE_WORKER_TIMEOUT.
1584 *
1585 * LOCKING:
1586 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1587 * multiple times. Called only from manager.
1588 *
1589 * RETURNS:
1590 * false if no action was taken and gcwq->lock stayed locked, true
1591 * otherwise.
1592 */
1593static bool maybe_destroy_workers(struct global_cwq *gcwq)
1594{
1595 bool ret = false;
1596
1597 while (too_many_workers(gcwq)) {
1598 struct worker *worker;
1599 unsigned long expires;
1600
1601 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1602 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1603
1604 if (time_before(jiffies, expires)) {
1605 mod_timer(&gcwq->idle_timer, expires);
1606 break;
1607 }
1608
1609 destroy_worker(worker);
1610 ret = true;
1611 }
1612
1613 return ret;
1614}
1615
1616/**
1617 * manage_workers - manage worker pool
1618 * @worker: self
1619 *
1620 * Assume the manager role and manage gcwq worker pool @worker belongs
1621 * to. At any given time, there can be only zero or one manager per
1622 * gcwq. The exclusion is handled automatically by this function.
1623 *
1624 * The caller can safely start processing works on false return. On
1625 * true return, it's guaranteed that need_to_create_worker() is false
1626 * and may_start_working() is true.
1627 *
1628 * CONTEXT:
1629 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1630 * multiple times. Does GFP_KERNEL allocations.
1631 *
1632 * RETURNS:
1633 * false if no action was taken and gcwq->lock stayed locked, true if
1634 * some action was taken.
1635 */
1636static bool manage_workers(struct worker *worker)
1637{
1638 struct global_cwq *gcwq = worker->gcwq;
1639 bool ret = false;
1640
1641 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1642 return ret;
1643
1644 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1645 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1646
1647 /*
1648 * Destroy and then create so that may_start_working() is true
1649 * on return.
1650 */
1651 ret |= maybe_destroy_workers(gcwq);
1652 ret |= maybe_create_worker(gcwq);
1653
1654 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1655
1656 /*
1657 * The trustee might be waiting to take over the manager
1658 * position, tell it we're done.
1659 */
1660 if (unlikely(gcwq->trustee))
1661 wake_up_all(&gcwq->trustee_wait);
1662
1663 return ret;
1664}
1665
Tejun Heoa62428c2010-06-29 10:07:10 +02001666/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001667 * move_linked_works - move linked works to a list
1668 * @work: start of series of works to be scheduled
1669 * @head: target list to append @work to
1670 * @nextp: out paramter for nested worklist walking
1671 *
1672 * Schedule linked works starting from @work to @head. Work series to
1673 * be scheduled starts at @work and includes any consecutive work with
1674 * WORK_STRUCT_LINKED set in its predecessor.
1675 *
1676 * If @nextp is not NULL, it's updated to point to the next work of
1677 * the last scheduled work. This allows move_linked_works() to be
1678 * nested inside outer list_for_each_entry_safe().
1679 *
1680 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001681 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001682 */
1683static void move_linked_works(struct work_struct *work, struct list_head *head,
1684 struct work_struct **nextp)
1685{
1686 struct work_struct *n;
1687
1688 /*
1689 * Linked worklist will always end before the end of the list,
1690 * use NULL for list head.
1691 */
1692 list_for_each_entry_safe_from(work, n, NULL, entry) {
1693 list_move_tail(&work->entry, head);
1694 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1695 break;
1696 }
1697
1698 /*
1699 * If we're already inside safe list traversal and have moved
1700 * multiple works to the scheduled queue, the next position
1701 * needs to be updated.
1702 */
1703 if (nextp)
1704 *nextp = n;
1705}
1706
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001707static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1708{
1709 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1710 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001711 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001712
Tejun Heocdadf002010-10-05 10:49:55 +02001713 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001714 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001715 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001716 cwq->nr_active++;
1717}
1718
Tejun Heoaffee4b2010-06-29 10:07:12 +02001719/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001720 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1721 * @cwq: cwq of interest
1722 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001723 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001724 *
1725 * A work either has completed or is removed from pending queue,
1726 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1727 *
1728 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001729 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001730 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001731static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1732 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001733{
1734 /* ignore uncolored works */
1735 if (color == WORK_NO_COLOR)
1736 return;
1737
1738 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001739
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001740 if (!delayed) {
1741 cwq->nr_active--;
1742 if (!list_empty(&cwq->delayed_works)) {
1743 /* one down, submit a delayed one */
1744 if (cwq->nr_active < cwq->max_active)
1745 cwq_activate_first_delayed(cwq);
1746 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001747 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001748
1749 /* is flush in progress and are we at the flushing tip? */
1750 if (likely(cwq->flush_color != color))
1751 return;
1752
1753 /* are there still in-flight works? */
1754 if (cwq->nr_in_flight[color])
1755 return;
1756
1757 /* this cwq is done, clear flush_color */
1758 cwq->flush_color = -1;
1759
1760 /*
1761 * If this was the last cwq, wake up the first flusher. It
1762 * will handle the rest.
1763 */
1764 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1765 complete(&cwq->wq->first_flusher->done);
1766}
1767
1768/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001769 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001770 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001771 * @work: work to process
1772 *
1773 * Process @work. This function contains all the logics necessary to
1774 * process a single work including synchronization against and
1775 * interaction with other workers on the same cpu, queueing and
1776 * flushing. As long as context requirement is met, any worker can
1777 * call this function to process a work.
1778 *
1779 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001780 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001781 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001782static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001783__releases(&gcwq->lock)
1784__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001785{
Tejun Heo7e116292010-06-29 10:07:13 +02001786 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001787 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001788 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001789 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001790 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001791 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001792 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001793#ifdef CONFIG_LOCKDEP
1794 /*
1795 * It is permissible to free the struct work_struct from
1796 * inside the function that is called from it, this we need to
1797 * take into account for lockdep too. To avoid bogus "held
1798 * lock freed" warnings as well as problems when looking into
1799 * work->lockdep_map, make a copy and use that here.
1800 */
1801 struct lockdep_map lockdep_map = work->lockdep_map;
1802#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001803 /*
1804 * A single work shouldn't be executed concurrently by
1805 * multiple workers on a single cpu. Check whether anyone is
1806 * already processing the work. If so, defer the work to the
1807 * currently executing one.
1808 */
1809 collision = __find_worker_executing_work(gcwq, bwh, work);
1810 if (unlikely(collision)) {
1811 move_linked_works(work, &collision->scheduled, NULL);
1812 return;
1813 }
1814
Tejun Heoa62428c2010-06-29 10:07:10 +02001815 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001816 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001817 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001818 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001819 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001820 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001821
Tejun Heo7a22ad72010-06-29 10:07:13 +02001822 /* record the current cpu number in the work data and dequeue */
1823 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001824 list_del_init(&work->entry);
1825
Tejun Heo649027d2010-06-29 10:07:14 +02001826 /*
1827 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1828 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1829 */
1830 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1831 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1832 struct work_struct, entry);
1833
1834 if (!list_empty(&gcwq->worklist) &&
1835 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1836 wake_up_worker(gcwq);
1837 else
1838 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1839 }
1840
Tejun Heofb0e7be2010-06-29 10:07:15 +02001841 /*
1842 * CPU intensive works don't participate in concurrency
1843 * management. They're the scheduler's responsibility.
1844 */
1845 if (unlikely(cpu_intensive))
1846 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1847
Tejun Heo8b03ae32010-06-29 10:07:12 +02001848 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001849
Tejun Heoa62428c2010-06-29 10:07:10 +02001850 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001851 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001852 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001853 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001854 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001855 /*
1856 * While we must be careful to not use "work" after this, the trace
1857 * point will only record its address.
1858 */
1859 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001860 lock_map_release(&lockdep_map);
1861 lock_map_release(&cwq->wq->lockdep_map);
1862
1863 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1864 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1865 "%s/0x%08x/%d\n",
1866 current->comm, preempt_count(), task_pid_nr(current));
1867 printk(KERN_ERR " last function: ");
1868 print_symbol("%s\n", (unsigned long)f);
1869 debug_show_held_locks(current);
1870 dump_stack();
1871 }
1872
Tejun Heo8b03ae32010-06-29 10:07:12 +02001873 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001874
Tejun Heofb0e7be2010-06-29 10:07:15 +02001875 /* clear cpu intensive status */
1876 if (unlikely(cpu_intensive))
1877 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1878
Tejun Heoa62428c2010-06-29 10:07:10 +02001879 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001880 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001881 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001882 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001883 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001884}
1885
Tejun Heoaffee4b2010-06-29 10:07:12 +02001886/**
1887 * process_scheduled_works - process scheduled works
1888 * @worker: self
1889 *
1890 * Process all scheduled works. Please note that the scheduled list
1891 * may change while processing a work, so this function repeatedly
1892 * fetches a work from the top and executes it.
1893 *
1894 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001895 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001896 * multiple times.
1897 */
1898static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001900 while (!list_empty(&worker->scheduled)) {
1901 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001903 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Tejun Heo4690c4a2010-06-29 10:07:10 +02001907/**
1908 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001909 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001910 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 * The gcwq worker thread function. There's a single dynamic pool of
1912 * these per each cpu. These workers process all works regardless of
1913 * their specific target workqueue. The only exception is works which
1914 * belong to workqueues with a rescuer which will be explained in
1915 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001916 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001917static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Tejun Heoc34056a2010-06-29 10:07:11 +02001919 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001920 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 /* tell the scheduler that this is a workqueue worker */
1923 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001924woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001925 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Tejun Heoc8e55f32010-06-29 10:07:12 +02001927 /* DIE can be set only while we're idle, checking here is enough */
1928 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001929 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001930 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
Tejun Heoc8e55f32010-06-29 10:07:12 +02001934 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001935recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 /* no more worker necessary? */
1937 if (!need_more_worker(gcwq))
1938 goto sleep;
1939
1940 /* do we need to manage? */
1941 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1942 goto recheck;
1943
Tejun Heoc8e55f32010-06-29 10:07:12 +02001944 /*
1945 * ->scheduled list can only be filled while a worker is
1946 * preparing to process a work or actually processing it.
1947 * Make sure nobody diddled with it while I was sleeping.
1948 */
1949 BUG_ON(!list_empty(&worker->scheduled));
1950
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 /*
1952 * When control reaches this point, we're guaranteed to have
1953 * at least one idle worker or that someone else has already
1954 * assumed the manager role.
1955 */
1956 worker_clr_flags(worker, WORKER_PREP);
1957
1958 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001959 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02001960 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001961 struct work_struct, entry);
1962
1963 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1964 /* optimization path, not strictly necessary */
1965 process_one_work(worker, work);
1966 if (unlikely(!list_empty(&worker->scheduled)))
1967 process_scheduled_works(worker);
1968 } else {
1969 move_linked_works(work, &worker->scheduled, NULL);
1970 process_scheduled_works(worker);
1971 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001973
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001975sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
1977 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001978
Tejun Heoc8e55f32010-06-29 10:07:12 +02001979 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * gcwq->lock is held and there's no work to process and no
1981 * need to manage, sleep. Workers are woken up only while
1982 * holding gcwq->lock or from local cpu, so setting the
1983 * current state before releasing gcwq->lock is enough to
1984 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001985 */
1986 worker_enter_idle(worker);
1987 __set_current_state(TASK_INTERRUPTIBLE);
1988 spin_unlock_irq(&gcwq->lock);
1989 schedule();
1990 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Tejun Heoe22bee72010-06-29 10:07:14 +02001993/**
1994 * rescuer_thread - the rescuer thread function
1995 * @__wq: the associated workqueue
1996 *
1997 * Workqueue rescuer thread function. There's one rescuer for each
1998 * workqueue which has WQ_RESCUER set.
1999 *
2000 * Regular work processing on a gcwq may block trying to create a new
2001 * worker which uses GFP_KERNEL allocation which has slight chance of
2002 * developing into deadlock if some works currently on the same queue
2003 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2004 * the problem rescuer solves.
2005 *
2006 * When such condition is possible, the gcwq summons rescuers of all
2007 * workqueues which have works queued on the gcwq and let them process
2008 * those works so that forward progress can be guaranteed.
2009 *
2010 * This should happen rarely.
2011 */
2012static int rescuer_thread(void *__wq)
2013{
2014 struct workqueue_struct *wq = __wq;
2015 struct worker *rescuer = wq->rescuer;
2016 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002017 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 unsigned int cpu;
2019
2020 set_user_nice(current, RESCUER_NICE_LEVEL);
2021repeat:
2022 set_current_state(TASK_INTERRUPTIBLE);
2023
2024 if (kthread_should_stop())
2025 return 0;
2026
Tejun Heof3421792010-07-02 10:03:51 +02002027 /*
2028 * See whether any cpu is asking for help. Unbounded
2029 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2030 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002031 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002032 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2033 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002034 struct global_cwq *gcwq = cwq->gcwq;
2035 struct work_struct *work, *n;
2036
2037 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002038 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002039
2040 /* migrate to the target cpu if possible */
2041 rescuer->gcwq = gcwq;
2042 worker_maybe_bind_and_lock(rescuer);
2043
2044 /*
2045 * Slurp in all works issued via this workqueue and
2046 * process'em.
2047 */
2048 BUG_ON(!list_empty(&rescuer->scheduled));
2049 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2050 if (get_work_cwq(work) == cwq)
2051 move_linked_works(work, scheduled, &n);
2052
2053 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002054
2055 /*
2056 * Leave this gcwq. If keep_working() is %true, notify a
2057 * regular worker; otherwise, we end up with 0 concurrency
2058 * and stalling the execution.
2059 */
2060 if (keep_working(gcwq))
2061 wake_up_worker(gcwq);
2062
Tejun Heoe22bee72010-06-29 10:07:14 +02002063 spin_unlock_irq(&gcwq->lock);
2064 }
2065
2066 schedule();
2067 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002070struct wq_barrier {
2071 struct work_struct work;
2072 struct completion done;
2073};
2074
2075static void wq_barrier_func(struct work_struct *work)
2076{
2077 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2078 complete(&barr->done);
2079}
2080
Tejun Heo4690c4a2010-06-29 10:07:10 +02002081/**
2082 * insert_wq_barrier - insert a barrier work
2083 * @cwq: cwq to insert barrier into
2084 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002085 * @target: target work to attach @barr to
2086 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002087 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002088 * @barr is linked to @target such that @barr is completed only after
2089 * @target finishes execution. Please note that the ordering
2090 * guarantee is observed only with respect to @target and on the local
2091 * cpu.
2092 *
2093 * Currently, a queued barrier can't be canceled. This is because
2094 * try_to_grab_pending() can't determine whether the work to be
2095 * grabbed is at the head of the queue and thus can't clear LINKED
2096 * flag of the previous work while there must be a valid next work
2097 * after a work with LINKED flag set.
2098 *
2099 * Note that when @worker is non-NULL, @target may be modified
2100 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002101 *
2102 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002103 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002104 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002105static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002106 struct wq_barrier *barr,
2107 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002108{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002109 struct list_head *head;
2110 unsigned int linked = 0;
2111
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002112 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002113 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002114 * as we know for sure that this will not trigger any of the
2115 * checks and call back into the fixup functions where we
2116 * might deadlock.
2117 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002118 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002119 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002120 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002121
Tejun Heoaffee4b2010-06-29 10:07:12 +02002122 /*
2123 * If @target is currently being executed, schedule the
2124 * barrier to the worker; otherwise, put it after @target.
2125 */
2126 if (worker)
2127 head = worker->scheduled.next;
2128 else {
2129 unsigned long *bits = work_data_bits(target);
2130
2131 head = target->entry.next;
2132 /* there can already be other linked works, inherit and set */
2133 linked = *bits & WORK_STRUCT_LINKED;
2134 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2135 }
2136
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002137 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002138 insert_work(cwq, &barr->work, head,
2139 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002140}
2141
Tejun Heo73f53c42010-06-29 10:07:11 +02002142/**
2143 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2144 * @wq: workqueue being flushed
2145 * @flush_color: new flush color, < 0 for no-op
2146 * @work_color: new work color, < 0 for no-op
2147 *
2148 * Prepare cwqs for workqueue flushing.
2149 *
2150 * If @flush_color is non-negative, flush_color on all cwqs should be
2151 * -1. If no cwq has in-flight commands at the specified color, all
2152 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2153 * has in flight commands, its cwq->flush_color is set to
2154 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2155 * wakeup logic is armed and %true is returned.
2156 *
2157 * The caller should have initialized @wq->first_flusher prior to
2158 * calling this function with non-negative @flush_color. If
2159 * @flush_color is negative, no flush color update is done and %false
2160 * is returned.
2161 *
2162 * If @work_color is non-negative, all cwqs should have the same
2163 * work_color which is previous to @work_color and all will be
2164 * advanced to @work_color.
2165 *
2166 * CONTEXT:
2167 * mutex_lock(wq->flush_mutex).
2168 *
2169 * RETURNS:
2170 * %true if @flush_color >= 0 and there's something to flush. %false
2171 * otherwise.
2172 */
2173static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2174 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Tejun Heo73f53c42010-06-29 10:07:11 +02002176 bool wait = false;
2177 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002178
Tejun Heo73f53c42010-06-29 10:07:11 +02002179 if (flush_color >= 0) {
2180 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2181 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002182 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002183
Tejun Heof3421792010-07-02 10:03:51 +02002184 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002185 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002186 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002187
Tejun Heo8b03ae32010-06-29 10:07:12 +02002188 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002189
2190 if (flush_color >= 0) {
2191 BUG_ON(cwq->flush_color != -1);
2192
2193 if (cwq->nr_in_flight[flush_color]) {
2194 cwq->flush_color = flush_color;
2195 atomic_inc(&wq->nr_cwqs_to_flush);
2196 wait = true;
2197 }
2198 }
2199
2200 if (work_color >= 0) {
2201 BUG_ON(work_color != work_next_color(cwq->work_color));
2202 cwq->work_color = work_color;
2203 }
2204
Tejun Heo8b03ae32010-06-29 10:07:12 +02002205 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002206 }
2207
2208 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2209 complete(&wq->first_flusher->done);
2210
2211 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002214/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002216 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 *
2218 * Forces execution of the workqueue and blocks until its completion.
2219 * This is typically used in driver shutdown handlers.
2220 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002221 * We sleep until all works which were queued on entry have been handled,
2222 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002224void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Tejun Heo73f53c42010-06-29 10:07:11 +02002226 struct wq_flusher this_flusher = {
2227 .list = LIST_HEAD_INIT(this_flusher.list),
2228 .flush_color = -1,
2229 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2230 };
2231 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002232
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002233 lock_map_acquire(&wq->lockdep_map);
2234 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002235
2236 mutex_lock(&wq->flush_mutex);
2237
2238 /*
2239 * Start-to-wait phase
2240 */
2241 next_color = work_next_color(wq->work_color);
2242
2243 if (next_color != wq->flush_color) {
2244 /*
2245 * Color space is not full. The current work_color
2246 * becomes our flush_color and work_color is advanced
2247 * by one.
2248 */
2249 BUG_ON(!list_empty(&wq->flusher_overflow));
2250 this_flusher.flush_color = wq->work_color;
2251 wq->work_color = next_color;
2252
2253 if (!wq->first_flusher) {
2254 /* no flush in progress, become the first flusher */
2255 BUG_ON(wq->flush_color != this_flusher.flush_color);
2256
2257 wq->first_flusher = &this_flusher;
2258
2259 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2260 wq->work_color)) {
2261 /* nothing to flush, done */
2262 wq->flush_color = next_color;
2263 wq->first_flusher = NULL;
2264 goto out_unlock;
2265 }
2266 } else {
2267 /* wait in queue */
2268 BUG_ON(wq->flush_color == this_flusher.flush_color);
2269 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2270 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2271 }
2272 } else {
2273 /*
2274 * Oops, color space is full, wait on overflow queue.
2275 * The next flush completion will assign us
2276 * flush_color and transfer to flusher_queue.
2277 */
2278 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2279 }
2280
2281 mutex_unlock(&wq->flush_mutex);
2282
2283 wait_for_completion(&this_flusher.done);
2284
2285 /*
2286 * Wake-up-and-cascade phase
2287 *
2288 * First flushers are responsible for cascading flushes and
2289 * handling overflow. Non-first flushers can simply return.
2290 */
2291 if (wq->first_flusher != &this_flusher)
2292 return;
2293
2294 mutex_lock(&wq->flush_mutex);
2295
Tejun Heo4ce48b32010-07-02 10:03:51 +02002296 /* we might have raced, check again with mutex held */
2297 if (wq->first_flusher != &this_flusher)
2298 goto out_unlock;
2299
Tejun Heo73f53c42010-06-29 10:07:11 +02002300 wq->first_flusher = NULL;
2301
2302 BUG_ON(!list_empty(&this_flusher.list));
2303 BUG_ON(wq->flush_color != this_flusher.flush_color);
2304
2305 while (true) {
2306 struct wq_flusher *next, *tmp;
2307
2308 /* complete all the flushers sharing the current flush color */
2309 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2310 if (next->flush_color != wq->flush_color)
2311 break;
2312 list_del_init(&next->list);
2313 complete(&next->done);
2314 }
2315
2316 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2317 wq->flush_color != work_next_color(wq->work_color));
2318
2319 /* this flush_color is finished, advance by one */
2320 wq->flush_color = work_next_color(wq->flush_color);
2321
2322 /* one color has been freed, handle overflow queue */
2323 if (!list_empty(&wq->flusher_overflow)) {
2324 /*
2325 * Assign the same color to all overflowed
2326 * flushers, advance work_color and append to
2327 * flusher_queue. This is the start-to-wait
2328 * phase for these overflowed flushers.
2329 */
2330 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2331 tmp->flush_color = wq->work_color;
2332
2333 wq->work_color = work_next_color(wq->work_color);
2334
2335 list_splice_tail_init(&wq->flusher_overflow,
2336 &wq->flusher_queue);
2337 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2338 }
2339
2340 if (list_empty(&wq->flusher_queue)) {
2341 BUG_ON(wq->flush_color != wq->work_color);
2342 break;
2343 }
2344
2345 /*
2346 * Need to flush more colors. Make the next flusher
2347 * the new first flusher and arm cwqs.
2348 */
2349 BUG_ON(wq->flush_color == wq->work_color);
2350 BUG_ON(wq->flush_color != next->flush_color);
2351
2352 list_del_init(&next->list);
2353 wq->first_flusher = next;
2354
2355 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2356 break;
2357
2358 /*
2359 * Meh... this color is already done, clear first
2360 * flusher and repeat cascading.
2361 */
2362 wq->first_flusher = NULL;
2363 }
2364
2365out_unlock:
2366 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
Dave Jonesae90dd52006-06-30 01:40:45 -04002368EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Tejun Heobaf59022010-09-16 10:42:16 +02002370static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2371 bool wait_executing)
2372{
2373 struct worker *worker = NULL;
2374 struct global_cwq *gcwq;
2375 struct cpu_workqueue_struct *cwq;
2376
2377 might_sleep();
2378 gcwq = get_work_gcwq(work);
2379 if (!gcwq)
2380 return false;
2381
2382 spin_lock_irq(&gcwq->lock);
2383 if (!list_empty(&work->entry)) {
2384 /*
2385 * See the comment near try_to_grab_pending()->smp_rmb().
2386 * If it was re-queued to a different gcwq under us, we
2387 * are not going to wait.
2388 */
2389 smp_rmb();
2390 cwq = get_work_cwq(work);
2391 if (unlikely(!cwq || gcwq != cwq->gcwq))
2392 goto already_gone;
2393 } else if (wait_executing) {
2394 worker = find_worker_executing_work(gcwq, work);
2395 if (!worker)
2396 goto already_gone;
2397 cwq = worker->current_cwq;
2398 } else
2399 goto already_gone;
2400
2401 insert_wq_barrier(cwq, barr, work, worker);
2402 spin_unlock_irq(&gcwq->lock);
2403
Tejun Heoe1594892011-01-09 23:32:15 +01002404 /*
2405 * If @max_active is 1 or rescuer is in use, flushing another work
2406 * item on the same workqueue may lead to deadlock. Make sure the
2407 * flusher is not running on the same workqueue by verifying write
2408 * access.
2409 */
2410 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2411 lock_map_acquire(&cwq->wq->lockdep_map);
2412 else
2413 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002414 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002415
Tejun Heobaf59022010-09-16 10:42:16 +02002416 return true;
2417already_gone:
2418 spin_unlock_irq(&gcwq->lock);
2419 return false;
2420}
2421
Oleg Nesterovdb700892008-07-25 01:47:49 -07002422/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002423 * flush_work - wait for a work to finish executing the last queueing instance
2424 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002425 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002426 * Wait until @work has finished execution. This function considers
2427 * only the last queueing instance of @work. If @work has been
2428 * enqueued across different CPUs on a non-reentrant workqueue or on
2429 * multiple workqueues, @work might still be executing on return on
2430 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002431 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002432 * If @work was queued only on a non-reentrant, ordered or unbound
2433 * workqueue, @work is guaranteed to be idle on return if it hasn't
2434 * been requeued since flush started.
2435 *
2436 * RETURNS:
2437 * %true if flush_work() waited for the work to finish execution,
2438 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002439 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002440bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002441{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002442 struct wq_barrier barr;
2443
Tejun Heobaf59022010-09-16 10:42:16 +02002444 if (start_flush_work(work, &barr, true)) {
2445 wait_for_completion(&barr.done);
2446 destroy_work_on_stack(&barr.work);
2447 return true;
2448 } else
2449 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002450}
2451EXPORT_SYMBOL_GPL(flush_work);
2452
Tejun Heo401a8d02010-09-16 10:36:00 +02002453static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2454{
2455 struct wq_barrier barr;
2456 struct worker *worker;
2457
2458 spin_lock_irq(&gcwq->lock);
2459
2460 worker = find_worker_executing_work(gcwq, work);
2461 if (unlikely(worker))
2462 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2463
2464 spin_unlock_irq(&gcwq->lock);
2465
2466 if (unlikely(worker)) {
2467 wait_for_completion(&barr.done);
2468 destroy_work_on_stack(&barr.work);
2469 return true;
2470 } else
2471 return false;
2472}
2473
2474static bool wait_on_work(struct work_struct *work)
2475{
2476 bool ret = false;
2477 int cpu;
2478
2479 might_sleep();
2480
2481 lock_map_acquire(&work->lockdep_map);
2482 lock_map_release(&work->lockdep_map);
2483
2484 for_each_gcwq_cpu(cpu)
2485 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2486 return ret;
2487}
2488
Tejun Heo09383492010-09-16 10:48:29 +02002489/**
2490 * flush_work_sync - wait until a work has finished execution
2491 * @work: the work to flush
2492 *
2493 * Wait until @work has finished execution. On return, it's
2494 * guaranteed that all queueing instances of @work which happened
2495 * before this function is called are finished. In other words, if
2496 * @work hasn't been requeued since this function was called, @work is
2497 * guaranteed to be idle on return.
2498 *
2499 * RETURNS:
2500 * %true if flush_work_sync() waited for the work to finish execution,
2501 * %false if it was already idle.
2502 */
2503bool flush_work_sync(struct work_struct *work)
2504{
2505 struct wq_barrier barr;
2506 bool pending, waited;
2507
2508 /* we'll wait for executions separately, queue barr only if pending */
2509 pending = start_flush_work(work, &barr, false);
2510
2511 /* wait for executions to finish */
2512 waited = wait_on_work(work);
2513
2514 /* wait for the pending one */
2515 if (pending) {
2516 wait_for_completion(&barr.done);
2517 destroy_work_on_stack(&barr.work);
2518 }
2519
2520 return pending || waited;
2521}
2522EXPORT_SYMBOL_GPL(flush_work_sync);
2523
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002524/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002525 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002526 * so this work can't be re-armed in any way.
2527 */
2528static int try_to_grab_pending(struct work_struct *work)
2529{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002530 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002531 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002532
Tejun Heo22df02b2010-06-29 10:07:10 +02002533 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002534 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002535
2536 /*
2537 * The queueing is in progress, or it is already queued. Try to
2538 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2539 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002540 gcwq = get_work_gcwq(work);
2541 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002542 return ret;
2543
Tejun Heo8b03ae32010-06-29 10:07:12 +02002544 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002545 if (!list_empty(&work->entry)) {
2546 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002547 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002548 * In that case we must see the new value after rmb(), see
2549 * insert_work()->wmb().
2550 */
2551 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002552 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002553 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002554 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002555 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002556 get_work_color(work),
2557 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002558 ret = 1;
2559 }
2560 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002561 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002562
2563 return ret;
2564}
2565
Tejun Heo401a8d02010-09-16 10:36:00 +02002566static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002567 struct timer_list* timer)
2568{
2569 int ret;
2570
2571 do {
2572 ret = (timer && likely(del_timer(timer)));
2573 if (!ret)
2574 ret = try_to_grab_pending(work);
2575 wait_on_work(work);
2576 } while (unlikely(ret < 0));
2577
Tejun Heo7a22ad72010-06-29 10:07:13 +02002578 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002579 return ret;
2580}
2581
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002582/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002583 * cancel_work_sync - cancel a work and wait for it to finish
2584 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002585 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002586 * Cancel @work and wait for its execution to finish. This function
2587 * can be used even if the work re-queues itself or migrates to
2588 * another workqueue. On return from this function, @work is
2589 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002590 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002591 * cancel_work_sync(&delayed_work->work) must not be used for
2592 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002593 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002594 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002595 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002596 *
2597 * RETURNS:
2598 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002599 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002600bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002601{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002602 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002603}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002604EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002605
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002606/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002607 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2608 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002609 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002610 * Delayed timer is cancelled and the pending work is queued for
2611 * immediate execution. Like flush_work(), this function only
2612 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002613 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002614 * RETURNS:
2615 * %true if flush_work() waited for the work to finish execution,
2616 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002617 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002618bool flush_delayed_work(struct delayed_work *dwork)
2619{
2620 if (del_timer_sync(&dwork->timer))
2621 __queue_work(raw_smp_processor_id(),
2622 get_work_cwq(&dwork->work)->wq, &dwork->work);
2623 return flush_work(&dwork->work);
2624}
2625EXPORT_SYMBOL(flush_delayed_work);
2626
2627/**
Tejun Heo09383492010-09-16 10:48:29 +02002628 * flush_delayed_work_sync - wait for a dwork to finish
2629 * @dwork: the delayed work to flush
2630 *
2631 * Delayed timer is cancelled and the pending work is queued for
2632 * execution immediately. Other than timer handling, its behavior
2633 * is identical to flush_work_sync().
2634 *
2635 * RETURNS:
2636 * %true if flush_work_sync() waited for the work to finish execution,
2637 * %false if it was already idle.
2638 */
2639bool flush_delayed_work_sync(struct delayed_work *dwork)
2640{
2641 if (del_timer_sync(&dwork->timer))
2642 __queue_work(raw_smp_processor_id(),
2643 get_work_cwq(&dwork->work)->wq, &dwork->work);
2644 return flush_work_sync(&dwork->work);
2645}
2646EXPORT_SYMBOL(flush_delayed_work_sync);
2647
2648/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002649 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2650 * @dwork: the delayed work cancel
2651 *
2652 * This is cancel_work_sync() for delayed works.
2653 *
2654 * RETURNS:
2655 * %true if @dwork was pending, %false otherwise.
2656 */
2657bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002658{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002659 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002660}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002661EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002663/**
2664 * schedule_work - put work task in global workqueue
2665 * @work: job to be done
2666 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002667 * Returns zero if @work was already on the kernel-global workqueue and
2668 * non-zero otherwise.
2669 *
2670 * This puts a job in the kernel-global workqueue if it was not already
2671 * queued and leaves it in the same position on the kernel-global
2672 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002673 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002674int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Tejun Heod320c032010-06-29 10:07:14 +02002676 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
Dave Jonesae90dd52006-06-30 01:40:45 -04002678EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Zhang Ruic1a220e2008-07-23 21:28:39 -07002680/*
2681 * schedule_work_on - put work task on a specific cpu
2682 * @cpu: cpu to put the work task on
2683 * @work: job to be done
2684 *
2685 * This puts a job on a specific cpu
2686 */
2687int schedule_work_on(int cpu, struct work_struct *work)
2688{
Tejun Heod320c032010-06-29 10:07:14 +02002689 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002690}
2691EXPORT_SYMBOL(schedule_work_on);
2692
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002693/**
2694 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002695 * @dwork: job to be done
2696 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002697 *
2698 * After waiting for a given time this puts a job in the kernel-global
2699 * workqueue.
2700 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002701int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002702 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Tejun Heod320c032010-06-29 10:07:14 +02002704 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
Dave Jonesae90dd52006-06-30 01:40:45 -04002706EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002708/**
2709 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2710 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002711 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002712 * @delay: number of jiffies to wait
2713 *
2714 * After waiting for a given time this puts a job in the kernel-global
2715 * workqueue on the specified CPU.
2716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002718 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Tejun Heod320c032010-06-29 10:07:14 +02002720 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
Dave Jonesae90dd52006-06-30 01:40:45 -04002722EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Andrew Mortonb6136772006-06-25 05:47:49 -07002724/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002725 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002726 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002727 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002728 * schedule_on_each_cpu() executes @func on each online CPU using the
2729 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002730 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002731 *
2732 * RETURNS:
2733 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002734 */
David Howells65f27f32006-11-22 14:55:48 +00002735int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002736{
2737 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002738 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002739
Andrew Mortonb6136772006-06-25 05:47:49 -07002740 works = alloc_percpu(struct work_struct);
2741 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002742 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002743
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002744 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002745
Christoph Lameter15316ba2006-01-08 01:00:43 -08002746 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002747 struct work_struct *work = per_cpu_ptr(works, cpu);
2748
2749 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002750 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002751 }
Tejun Heo93981802009-11-17 14:06:20 -08002752
2753 for_each_online_cpu(cpu)
2754 flush_work(per_cpu_ptr(works, cpu));
2755
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002756 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002757 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002758 return 0;
2759}
2760
Alan Sterneef6a7d2010-02-12 17:39:21 +09002761/**
2762 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2763 *
2764 * Forces execution of the kernel-global workqueue and blocks until its
2765 * completion.
2766 *
2767 * Think twice before calling this function! It's very easy to get into
2768 * trouble if you don't take great care. Either of the following situations
2769 * will lead to deadlock:
2770 *
2771 * One of the work items currently on the workqueue needs to acquire
2772 * a lock held by your code or its caller.
2773 *
2774 * Your code is running in the context of a work routine.
2775 *
2776 * They will be detected by lockdep when they occur, but the first might not
2777 * occur very often. It depends on what work items are on the workqueue and
2778 * what locks they need, which you have no control over.
2779 *
2780 * In most situations flushing the entire workqueue is overkill; you merely
2781 * need to know that a particular work item isn't queued and isn't running.
2782 * In such cases you should use cancel_delayed_work_sync() or
2783 * cancel_work_sync() instead.
2784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785void flush_scheduled_work(void)
2786{
Tejun Heod320c032010-06-29 10:07:14 +02002787 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
Dave Jonesae90dd52006-06-30 01:40:45 -04002789EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002792 * execute_in_process_context - reliably execute the routine with user context
2793 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002794 * @ew: guaranteed storage for the execute work structure (must
2795 * be available when the work executes)
2796 *
2797 * Executes the function immediately if process context is available,
2798 * otherwise schedules the function for delayed execution.
2799 *
2800 * Returns: 0 - function was executed
2801 * 1 - function was scheduled for execution
2802 */
David Howells65f27f32006-11-22 14:55:48 +00002803int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002804{
2805 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002806 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002807 return 0;
2808 }
2809
David Howells65f27f32006-11-22 14:55:48 +00002810 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002811 schedule_work(&ew->work);
2812
2813 return 1;
2814}
2815EXPORT_SYMBOL_GPL(execute_in_process_context);
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817int keventd_up(void)
2818{
Tejun Heod320c032010-06-29 10:07:14 +02002819 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002822static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002824 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002825 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2826 * Make sure that the alignment isn't lower than that of
2827 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002828 */
Tejun Heo0f900042010-06-29 10:07:11 +02002829 const size_t size = sizeof(struct cpu_workqueue_struct);
2830 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2831 __alignof__(unsigned long long));
Tejun Heo931ac772010-07-20 11:07:48 +02002832#ifdef CONFIG_SMP
2833 bool percpu = !(wq->flags & WQ_UNBOUND);
2834#else
2835 bool percpu = false;
2836#endif
Oleg Nesterov3af244332007-05-09 02:34:09 -07002837
Tejun Heo931ac772010-07-20 11:07:48 +02002838 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002839 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002840 else {
Tejun Heof3421792010-07-02 10:03:51 +02002841 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002842
Tejun Heof3421792010-07-02 10:03:51 +02002843 /*
2844 * Allocate enough room to align cwq and put an extra
2845 * pointer at the end pointing back to the originally
2846 * allocated pointer which will be used for free.
2847 */
2848 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2849 if (ptr) {
2850 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2851 *(void **)(wq->cpu_wq.single + 1) = ptr;
2852 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002853 }
Tejun Heof3421792010-07-02 10:03:51 +02002854
David Howells52605622010-10-25 23:41:11 +01002855 /* just in case, make sure it's actually aligned
2856 * - this is affected by PERCPU() alignment in vmlinux.lds.S
2857 */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002858 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2859 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002860}
2861
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002862static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002863{
Tejun Heo931ac772010-07-20 11:07:48 +02002864#ifdef CONFIG_SMP
2865 bool percpu = !(wq->flags & WQ_UNBOUND);
2866#else
2867 bool percpu = false;
2868#endif
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002869
Tejun Heo931ac772010-07-20 11:07:48 +02002870 if (percpu)
Tejun Heof3421792010-07-02 10:03:51 +02002871 free_percpu(wq->cpu_wq.pcpu);
2872 else if (wq->cpu_wq.single) {
2873 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002874 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002875 }
2876}
2877
Tejun Heof3421792010-07-02 10:03:51 +02002878static int wq_clamp_max_active(int max_active, unsigned int flags,
2879 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002880{
Tejun Heof3421792010-07-02 10:03:51 +02002881 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2882
2883 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002884 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2885 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002886 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002887
Tejun Heof3421792010-07-02 10:03:51 +02002888 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002889}
2890
Tejun Heod320c032010-06-29 10:07:14 +02002891struct workqueue_struct *__alloc_workqueue_key(const char *name,
2892 unsigned int flags,
2893 int max_active,
2894 struct lock_class_key *key,
2895 const char *lock_name)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002896{
2897 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002898 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002899
Tejun Heof3421792010-07-02 10:03:51 +02002900 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002901 * Workqueues which may be used during memory reclaim should
2902 * have a rescuer to guarantee forward progress.
2903 */
2904 if (flags & WQ_MEM_RECLAIM)
2905 flags |= WQ_RESCUER;
2906
2907 /*
Tejun Heof3421792010-07-02 10:03:51 +02002908 * Unbound workqueues aren't concurrency managed and should be
2909 * dispatched to workers immediately.
2910 */
2911 if (flags & WQ_UNBOUND)
2912 flags |= WQ_HIGHPRI;
2913
Tejun Heod320c032010-06-29 10:07:14 +02002914 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heof3421792010-07-02 10:03:51 +02002915 max_active = wq_clamp_max_active(max_active, flags, name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002916
2917 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2918 if (!wq)
Tejun Heo4690c4a2010-06-29 10:07:10 +02002919 goto err;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002920
Tejun Heo97e37d72010-06-29 10:07:10 +02002921 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002922 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002923 mutex_init(&wq->flush_mutex);
2924 atomic_set(&wq->nr_cwqs_to_flush, 0);
2925 INIT_LIST_HEAD(&wq->flusher_queue);
2926 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002927
2928 wq->name = name;
Johannes Bergeb13ba82008-01-16 09:51:58 +01002929 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002930 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002931
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002932 if (alloc_cwqs(wq) < 0)
2933 goto err;
2934
Tejun Heof3421792010-07-02 10:03:51 +02002935 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002936 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002937 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02002938
Tejun Heo0f900042010-06-29 10:07:11 +02002939 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002940 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002941 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002942 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002943 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02002944 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002945 }
2946
Tejun Heoe22bee72010-06-29 10:07:14 +02002947 if (flags & WQ_RESCUER) {
2948 struct worker *rescuer;
2949
Tejun Heof2e005a2010-07-20 15:59:09 +02002950 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02002951 goto err;
2952
2953 wq->rescuer = rescuer = alloc_worker();
2954 if (!rescuer)
2955 goto err;
2956
2957 rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
2958 if (IS_ERR(rescuer->task))
2959 goto err;
2960
Tejun Heoe22bee72010-06-29 10:07:14 +02002961 rescuer->task->flags |= PF_THREAD_BOUND;
2962 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002963 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07002964
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002965 /*
2966 * workqueue_lock protects global freeze state and workqueues
2967 * list. Grab it, set max_active accordingly and add the new
2968 * workqueue to workqueues list.
2969 */
Tejun Heo15376632010-06-29 10:07:11 +02002970 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002971
Tejun Heo58a69cb2011-02-16 09:25:31 +01002972 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02002973 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002974 get_cwq(cpu, wq)->max_active = 0;
2975
Tejun Heo15376632010-06-29 10:07:11 +02002976 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002977
Tejun Heo15376632010-06-29 10:07:11 +02002978 spin_unlock(&workqueue_lock);
2979
Oleg Nesterov3af244332007-05-09 02:34:09 -07002980 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02002981err:
2982 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002983 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02002984 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002985 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02002986 kfree(wq);
2987 }
2988 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002989}
Tejun Heod320c032010-06-29 10:07:14 +02002990EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002991
2992/**
2993 * destroy_workqueue - safely terminate a workqueue
2994 * @wq: target workqueue
2995 *
2996 * Safely destroy a workqueue. All work currently pending will be done first.
2997 */
2998void destroy_workqueue(struct workqueue_struct *wq)
2999{
Tejun Heoc8efcc22010-12-20 19:32:04 +01003000 unsigned int flush_cnt = 0;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003001 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003002
Tejun Heoc8efcc22010-12-20 19:32:04 +01003003 /*
3004 * Mark @wq dying and drain all pending works. Once WQ_DYING is
3005 * set, only chain queueing is allowed. IOW, only currently
3006 * pending or running work items on @wq can queue further work
3007 * items on it. @wq is flushed repeatedly until it becomes empty.
3008 * The number of flushing is detemined by the depth of chaining and
3009 * should be relatively short. Whine if it takes too long.
3010 */
Tejun Heoe41e7042010-08-24 14:22:47 +02003011 wq->flags |= WQ_DYING;
Tejun Heoc8efcc22010-12-20 19:32:04 +01003012reflush:
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003013 flush_workqueue(wq);
3014
Tejun Heoc8efcc22010-12-20 19:32:04 +01003015 for_each_cwq_cpu(cpu, wq) {
3016 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3017
3018 if (!cwq->nr_active && list_empty(&cwq->delayed_works))
3019 continue;
3020
3021 if (++flush_cnt == 10 ||
3022 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
3023 printk(KERN_WARNING "workqueue %s: flush on "
3024 "destruction isn't complete after %u tries\n",
3025 wq->name, flush_cnt);
3026 goto reflush;
3027 }
3028
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003029 /*
3030 * wq list is used to freeze wq, remove from list after
3031 * flushing is complete in case freeze races us.
3032 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003033 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003034 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003035 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003036
Tejun Heoe22bee72010-06-29 10:07:14 +02003037 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003038 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003039 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3040 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003041
Tejun Heo73f53c42010-06-29 10:07:11 +02003042 for (i = 0; i < WORK_NR_COLORS; i++)
3043 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003044 BUG_ON(cwq->nr_active);
3045 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003046 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003047
Tejun Heoe22bee72010-06-29 10:07:14 +02003048 if (wq->flags & WQ_RESCUER) {
3049 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003050 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003051 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003052 }
3053
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003054 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003055 kfree(wq);
3056}
3057EXPORT_SYMBOL_GPL(destroy_workqueue);
3058
Tejun Heodcd989c2010-06-29 10:07:14 +02003059/**
3060 * workqueue_set_max_active - adjust max_active of a workqueue
3061 * @wq: target workqueue
3062 * @max_active: new max_active value.
3063 *
3064 * Set max_active of @wq to @max_active.
3065 *
3066 * CONTEXT:
3067 * Don't call from IRQ context.
3068 */
3069void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3070{
3071 unsigned int cpu;
3072
Tejun Heof3421792010-07-02 10:03:51 +02003073 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003074
3075 spin_lock(&workqueue_lock);
3076
3077 wq->saved_max_active = max_active;
3078
Tejun Heof3421792010-07-02 10:03:51 +02003079 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003080 struct global_cwq *gcwq = get_gcwq(cpu);
3081
3082 spin_lock_irq(&gcwq->lock);
3083
Tejun Heo58a69cb2011-02-16 09:25:31 +01003084 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003085 !(gcwq->flags & GCWQ_FREEZING))
3086 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3087
3088 spin_unlock_irq(&gcwq->lock);
3089 }
3090
3091 spin_unlock(&workqueue_lock);
3092}
3093EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3094
3095/**
3096 * workqueue_congested - test whether a workqueue is congested
3097 * @cpu: CPU in question
3098 * @wq: target workqueue
3099 *
3100 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3101 * no synchronization around this function and the test result is
3102 * unreliable and only useful as advisory hints or for debugging.
3103 *
3104 * RETURNS:
3105 * %true if congested, %false otherwise.
3106 */
3107bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3108{
3109 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3110
3111 return !list_empty(&cwq->delayed_works);
3112}
3113EXPORT_SYMBOL_GPL(workqueue_congested);
3114
3115/**
3116 * work_cpu - return the last known associated cpu for @work
3117 * @work: the work of interest
3118 *
3119 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003120 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003121 */
3122unsigned int work_cpu(struct work_struct *work)
3123{
3124 struct global_cwq *gcwq = get_work_gcwq(work);
3125
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003126 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003127}
3128EXPORT_SYMBOL_GPL(work_cpu);
3129
3130/**
3131 * work_busy - test whether a work is currently pending or running
3132 * @work: the work to be tested
3133 *
3134 * Test whether @work is currently pending or running. There is no
3135 * synchronization around this function and the test result is
3136 * unreliable and only useful as advisory hints or for debugging.
3137 * Especially for reentrant wqs, the pending state might hide the
3138 * running state.
3139 *
3140 * RETURNS:
3141 * OR'd bitmask of WORK_BUSY_* bits.
3142 */
3143unsigned int work_busy(struct work_struct *work)
3144{
3145 struct global_cwq *gcwq = get_work_gcwq(work);
3146 unsigned long flags;
3147 unsigned int ret = 0;
3148
3149 if (!gcwq)
3150 return false;
3151
3152 spin_lock_irqsave(&gcwq->lock, flags);
3153
3154 if (work_pending(work))
3155 ret |= WORK_BUSY_PENDING;
3156 if (find_worker_executing_work(gcwq, work))
3157 ret |= WORK_BUSY_RUNNING;
3158
3159 spin_unlock_irqrestore(&gcwq->lock, flags);
3160
3161 return ret;
3162}
3163EXPORT_SYMBOL_GPL(work_busy);
3164
Tejun Heodb7bccf2010-06-29 10:07:12 +02003165/*
3166 * CPU hotplug.
3167 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003168 * There are two challenges in supporting CPU hotplug. Firstly, there
3169 * are a lot of assumptions on strong associations among work, cwq and
3170 * gcwq which make migrating pending and scheduled works very
3171 * difficult to implement without impacting hot paths. Secondly,
3172 * gcwqs serve mix of short, long and very long running works making
3173 * blocked draining impractical.
3174 *
3175 * This is solved by allowing a gcwq to be detached from CPU, running
3176 * it with unbound (rogue) workers and allowing it to be reattached
3177 * later if the cpu comes back online. A separate thread is created
3178 * to govern a gcwq in such state and is called the trustee of the
3179 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003180 *
3181 * Trustee states and their descriptions.
3182 *
3183 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3184 * new trustee is started with this state.
3185 *
3186 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003187 * assuming the manager role and making all existing
3188 * workers rogue. DOWN_PREPARE waits for trustee to
3189 * enter this state. After reaching IN_CHARGE, trustee
3190 * tries to execute the pending worklist until it's empty
3191 * and the state is set to BUTCHER, or the state is set
3192 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003193 *
3194 * BUTCHER Command state which is set by the cpu callback after
3195 * the cpu has went down. Once this state is set trustee
3196 * knows that there will be no new works on the worklist
3197 * and once the worklist is empty it can proceed to
3198 * killing idle workers.
3199 *
3200 * RELEASE Command state which is set by the cpu callback if the
3201 * cpu down has been canceled or it has come online
3202 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003203 * trying to drain or butcher and clears ROGUE, rebinds
3204 * all remaining workers back to the cpu and releases
3205 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003206 *
3207 * DONE Trustee will enter this state after BUTCHER or RELEASE
3208 * is complete.
3209 *
3210 * trustee CPU draining
3211 * took over down complete
3212 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3213 * | | ^
3214 * | CPU is back online v return workers |
3215 * ----------------> RELEASE --------------
3216 */
3217
3218/**
3219 * trustee_wait_event_timeout - timed event wait for trustee
3220 * @cond: condition to wait for
3221 * @timeout: timeout in jiffies
3222 *
3223 * wait_event_timeout() for trustee to use. Handles locking and
3224 * checks for RELEASE request.
3225 *
3226 * CONTEXT:
3227 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3228 * multiple times. To be used by trustee.
3229 *
3230 * RETURNS:
3231 * Positive indicating left time if @cond is satisfied, 0 if timed
3232 * out, -1 if canceled.
3233 */
3234#define trustee_wait_event_timeout(cond, timeout) ({ \
3235 long __ret = (timeout); \
3236 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3237 __ret) { \
3238 spin_unlock_irq(&gcwq->lock); \
3239 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3240 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3241 __ret); \
3242 spin_lock_irq(&gcwq->lock); \
3243 } \
3244 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3245})
3246
3247/**
3248 * trustee_wait_event - event wait for trustee
3249 * @cond: condition to wait for
3250 *
3251 * wait_event() for trustee to use. Automatically handles locking and
3252 * checks for CANCEL request.
3253 *
3254 * CONTEXT:
3255 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3256 * multiple times. To be used by trustee.
3257 *
3258 * RETURNS:
3259 * 0 if @cond is satisfied, -1 if canceled.
3260 */
3261#define trustee_wait_event(cond) ({ \
3262 long __ret1; \
3263 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3264 __ret1 < 0 ? -1 : 0; \
3265})
3266
3267static int __cpuinit trustee_thread(void *__gcwq)
3268{
3269 struct global_cwq *gcwq = __gcwq;
3270 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003271 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003272 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003273 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003274 int i;
3275
3276 BUG_ON(gcwq->cpu != smp_processor_id());
3277
3278 spin_lock_irq(&gcwq->lock);
3279 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003280 * Claim the manager position and make all workers rogue.
3281 * Trustee must be bound to the target cpu and can't be
3282 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003283 */
3284 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003285 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3286 BUG_ON(rc < 0);
3287
3288 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003289
3290 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003291 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003292
3293 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003294 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003295
3296 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003297 * Call schedule() so that we cross rq->lock and thus can
3298 * guarantee sched callbacks see the rogue flag. This is
3299 * necessary as scheduler callbacks may be invoked from other
3300 * cpus.
3301 */
3302 spin_unlock_irq(&gcwq->lock);
3303 schedule();
3304 spin_lock_irq(&gcwq->lock);
3305
3306 /*
Tejun Heocb444762010-07-02 10:03:50 +02003307 * Sched callbacks are disabled now. Zap nr_running. After
3308 * this, nr_running stays zero and need_more_worker() and
3309 * keep_working() are always true as long as the worklist is
3310 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003311 */
Tejun Heocb444762010-07-02 10:03:50 +02003312 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003313
3314 spin_unlock_irq(&gcwq->lock);
3315 del_timer_sync(&gcwq->idle_timer);
3316 spin_lock_irq(&gcwq->lock);
3317
3318 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003319 * We're now in charge. Notify and proceed to drain. We need
3320 * to keep the gcwq running during the whole CPU down
3321 * procedure as other cpu hotunplug callbacks may need to
3322 * flush currently running tasks.
3323 */
3324 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3325 wake_up_all(&gcwq->trustee_wait);
3326
3327 /*
3328 * The original cpu is in the process of dying and may go away
3329 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003330 * be migrated to other cpus. Try draining any left work. We
3331 * want to get it over with ASAP - spam rescuers, wake up as
3332 * many idlers as necessary and create new ones till the
3333 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003334 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003335 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003336 */
3337 while (gcwq->nr_workers != gcwq->nr_idle ||
3338 gcwq->flags & GCWQ_FREEZING ||
3339 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003340 int nr_works = 0;
3341
3342 list_for_each_entry(work, &gcwq->worklist, entry) {
3343 send_mayday(work);
3344 nr_works++;
3345 }
3346
3347 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3348 if (!nr_works--)
3349 break;
3350 wake_up_process(worker->task);
3351 }
3352
3353 if (need_to_create_worker(gcwq)) {
3354 spin_unlock_irq(&gcwq->lock);
3355 worker = create_worker(gcwq, false);
3356 spin_lock_irq(&gcwq->lock);
3357 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003358 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003359 start_worker(worker);
3360 }
3361 }
3362
Tejun Heodb7bccf2010-06-29 10:07:12 +02003363 /* give a breather */
3364 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3365 break;
3366 }
3367
Tejun Heoe22bee72010-06-29 10:07:14 +02003368 /*
3369 * Either all works have been scheduled and cpu is down, or
3370 * cpu down has already been canceled. Wait for and butcher
3371 * all workers till we're canceled.
3372 */
3373 do {
3374 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3375 while (!list_empty(&gcwq->idle_list))
3376 destroy_worker(list_first_entry(&gcwq->idle_list,
3377 struct worker, entry));
3378 } while (gcwq->nr_workers && rc >= 0);
3379
3380 /*
3381 * At this point, either draining has completed and no worker
3382 * is left, or cpu down has been canceled or the cpu is being
3383 * brought back up. There shouldn't be any idle one left.
3384 * Tell the remaining busy ones to rebind once it finishes the
3385 * currently scheduled works by scheduling the rebind_work.
3386 */
3387 WARN_ON(!list_empty(&gcwq->idle_list));
3388
3389 for_each_busy_worker(worker, i, pos, gcwq) {
3390 struct work_struct *rebind_work = &worker->rebind_work;
3391
3392 /*
3393 * Rebind_work may race with future cpu hotplug
3394 * operations. Use a separate flag to mark that
3395 * rebinding is scheduled.
3396 */
Tejun Heocb444762010-07-02 10:03:50 +02003397 worker->flags |= WORKER_REBIND;
3398 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003399
3400 /* queue rebind_work, wq doesn't matter, use the default one */
3401 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3402 work_data_bits(rebind_work)))
3403 continue;
3404
3405 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003406 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003407 worker->scheduled.next,
3408 work_color_to_flags(WORK_NO_COLOR));
3409 }
3410
3411 /* relinquish manager role */
3412 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3413
Tejun Heodb7bccf2010-06-29 10:07:12 +02003414 /* notify completion */
3415 gcwq->trustee = NULL;
3416 gcwq->trustee_state = TRUSTEE_DONE;
3417 wake_up_all(&gcwq->trustee_wait);
3418 spin_unlock_irq(&gcwq->lock);
3419 return 0;
3420}
3421
3422/**
3423 * wait_trustee_state - wait for trustee to enter the specified state
3424 * @gcwq: gcwq the trustee of interest belongs to
3425 * @state: target state to wait for
3426 *
3427 * Wait for the trustee to reach @state. DONE is already matched.
3428 *
3429 * CONTEXT:
3430 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3431 * multiple times. To be used by cpu_callback.
3432 */
3433static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003434__releases(&gcwq->lock)
3435__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003436{
3437 if (!(gcwq->trustee_state == state ||
3438 gcwq->trustee_state == TRUSTEE_DONE)) {
3439 spin_unlock_irq(&gcwq->lock);
3440 __wait_event(gcwq->trustee_wait,
3441 gcwq->trustee_state == state ||
3442 gcwq->trustee_state == TRUSTEE_DONE);
3443 spin_lock_irq(&gcwq->lock);
3444 }
3445}
3446
Oleg Nesterov3af244332007-05-09 02:34:09 -07003447static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3448 unsigned long action,
3449 void *hcpu)
3450{
3451 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003452 struct global_cwq *gcwq = get_gcwq(cpu);
3453 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003454 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003455 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003457 action &= ~CPU_TASKS_FROZEN;
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003460 case CPU_DOWN_PREPARE:
3461 new_trustee = kthread_create(trustee_thread, gcwq,
3462 "workqueue_trustee/%d\n", cpu);
3463 if (IS_ERR(new_trustee))
3464 return notifier_from_errno(PTR_ERR(new_trustee));
3465 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003466 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003468 BUG_ON(gcwq->first_idle);
3469 new_worker = create_worker(gcwq, false);
3470 if (!new_worker) {
3471 if (new_trustee)
3472 kthread_stop(new_trustee);
3473 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476
Tejun Heodb7bccf2010-06-29 10:07:12 +02003477 /* some are called w/ irq disabled, don't disturb irq status */
3478 spin_lock_irqsave(&gcwq->lock, flags);
3479
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003480 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003481 case CPU_DOWN_PREPARE:
3482 /* initialize trustee and tell it to acquire the gcwq */
3483 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3484 gcwq->trustee = new_trustee;
3485 gcwq->trustee_state = TRUSTEE_START;
3486 wake_up_process(gcwq->trustee);
3487 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003488 /* fall through */
3489 case CPU_UP_PREPARE:
3490 BUG_ON(gcwq->first_idle);
3491 gcwq->first_idle = new_worker;
3492 break;
3493
3494 case CPU_DYING:
3495 /*
3496 * Before this, the trustee and all workers except for
3497 * the ones which are still executing works from
3498 * before the last CPU down must be on the cpu. After
3499 * this, they'll all be diasporas.
3500 */
3501 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003502 break;
3503
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003504 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003505 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003506 /* fall through */
3507 case CPU_UP_CANCELED:
3508 destroy_worker(gcwq->first_idle);
3509 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003510 break;
3511
3512 case CPU_DOWN_FAILED:
3513 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003514 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003515 if (gcwq->trustee_state != TRUSTEE_DONE) {
3516 gcwq->trustee_state = TRUSTEE_RELEASE;
3517 wake_up_process(gcwq->trustee);
3518 wait_trustee_state(gcwq, TRUSTEE_DONE);
3519 }
3520
Tejun Heoe22bee72010-06-29 10:07:14 +02003521 /*
3522 * Trustee is done and there might be no worker left.
3523 * Put the first_idle in and request a real manager to
3524 * take a look.
3525 */
3526 spin_unlock_irq(&gcwq->lock);
3527 kthread_bind(gcwq->first_idle->task, cpu);
3528 spin_lock_irq(&gcwq->lock);
3529 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3530 start_worker(gcwq->first_idle);
3531 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003532 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003533 }
3534
Tejun Heodb7bccf2010-06-29 10:07:12 +02003535 spin_unlock_irqrestore(&gcwq->lock, flags);
3536
Tejun Heo15376632010-06-29 10:07:11 +02003537 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Rusty Russell2d3854a2008-11-05 13:39:10 +11003540#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003541
Rusty Russell2d3854a2008-11-05 13:39:10 +11003542struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003543 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003544 long (*fn)(void *);
3545 void *arg;
3546 long ret;
3547};
3548
Andrew Morton6b440032009-04-09 09:50:37 -06003549static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003550{
Andrew Morton6b440032009-04-09 09:50:37 -06003551 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003552 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003553 complete(&wfc->completion);
3554 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003555}
3556
3557/**
3558 * work_on_cpu - run a function in user context on a particular cpu
3559 * @cpu: the cpu to run on
3560 * @fn: the function to run
3561 * @arg: the function arg
3562 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003563 * This will return the value @fn returns.
3564 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003565 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003566 */
3567long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3568{
Andrew Morton6b440032009-04-09 09:50:37 -06003569 struct task_struct *sub_thread;
3570 struct work_for_cpu wfc = {
3571 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3572 .fn = fn,
3573 .arg = arg,
3574 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003575
Andrew Morton6b440032009-04-09 09:50:37 -06003576 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3577 if (IS_ERR(sub_thread))
3578 return PTR_ERR(sub_thread);
3579 kthread_bind(sub_thread, cpu);
3580 wake_up_process(sub_thread);
3581 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003582 return wfc.ret;
3583}
3584EXPORT_SYMBOL_GPL(work_on_cpu);
3585#endif /* CONFIG_SMP */
3586
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003587#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303588
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003589/**
3590 * freeze_workqueues_begin - begin freezing workqueues
3591 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003592 * Start freezing workqueues. After this function returns, all freezable
3593 * workqueues will queue new works to their frozen_works list instead of
3594 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003595 *
3596 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003597 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003598 */
3599void freeze_workqueues_begin(void)
3600{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003601 unsigned int cpu;
3602
3603 spin_lock(&workqueue_lock);
3604
3605 BUG_ON(workqueue_freezing);
3606 workqueue_freezing = true;
3607
Tejun Heof3421792010-07-02 10:03:51 +02003608 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003609 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003610 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003611
3612 spin_lock_irq(&gcwq->lock);
3613
Tejun Heodb7bccf2010-06-29 10:07:12 +02003614 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3615 gcwq->flags |= GCWQ_FREEZING;
3616
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003617 list_for_each_entry(wq, &workqueues, list) {
3618 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3619
Tejun Heo58a69cb2011-02-16 09:25:31 +01003620 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003621 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003622 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003623
3624 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003625 }
3626
3627 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003629
3630/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003631 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003632 *
3633 * Check whether freezing is complete. This function must be called
3634 * between freeze_workqueues_begin() and thaw_workqueues().
3635 *
3636 * CONTEXT:
3637 * Grabs and releases workqueue_lock.
3638 *
3639 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003640 * %true if some freezable workqueues are still busy. %false if freezing
3641 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003642 */
3643bool freeze_workqueues_busy(void)
3644{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003645 unsigned int cpu;
3646 bool busy = false;
3647
3648 spin_lock(&workqueue_lock);
3649
3650 BUG_ON(!workqueue_freezing);
3651
Tejun Heof3421792010-07-02 10:03:51 +02003652 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003653 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003654 /*
3655 * nr_active is monotonically decreasing. It's safe
3656 * to peek without lock.
3657 */
3658 list_for_each_entry(wq, &workqueues, list) {
3659 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3660
Tejun Heo58a69cb2011-02-16 09:25:31 +01003661 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003662 continue;
3663
3664 BUG_ON(cwq->nr_active < 0);
3665 if (cwq->nr_active) {
3666 busy = true;
3667 goto out_unlock;
3668 }
3669 }
3670 }
3671out_unlock:
3672 spin_unlock(&workqueue_lock);
3673 return busy;
3674}
3675
3676/**
3677 * thaw_workqueues - thaw workqueues
3678 *
3679 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003680 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003681 *
3682 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003683 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003684 */
3685void thaw_workqueues(void)
3686{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003687 unsigned int cpu;
3688
3689 spin_lock(&workqueue_lock);
3690
3691 if (!workqueue_freezing)
3692 goto out_unlock;
3693
Tejun Heof3421792010-07-02 10:03:51 +02003694 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003695 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003696 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003697
3698 spin_lock_irq(&gcwq->lock);
3699
Tejun Heodb7bccf2010-06-29 10:07:12 +02003700 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3701 gcwq->flags &= ~GCWQ_FREEZING;
3702
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 list_for_each_entry(wq, &workqueues, list) {
3704 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3705
Tejun Heo58a69cb2011-02-16 09:25:31 +01003706 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003707 continue;
3708
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003709 /* restore max_active and repopulate worklist */
3710 cwq->max_active = wq->saved_max_active;
3711
3712 while (!list_empty(&cwq->delayed_works) &&
3713 cwq->nr_active < cwq->max_active)
3714 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003715 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003716
Tejun Heoe22bee72010-06-29 10:07:14 +02003717 wake_up_worker(gcwq);
3718
Tejun Heo8b03ae32010-06-29 10:07:12 +02003719 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003720 }
3721
3722 workqueue_freezing = false;
3723out_unlock:
3724 spin_unlock(&workqueue_lock);
3725}
3726#endif /* CONFIG_FREEZER */
3727
Suresh Siddha6ee05782010-07-30 14:57:37 -07003728static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Tejun Heoc34056a2010-06-29 10:07:11 +02003730 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003731 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003732
Tejun Heof6500942010-08-09 11:50:34 +02003733 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003734
3735 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003736 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003737 struct global_cwq *gcwq = get_gcwq(cpu);
3738
3739 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003740 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003741 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003742 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003743
Tejun Heoc8e55f32010-06-29 10:07:12 +02003744 INIT_LIST_HEAD(&gcwq->idle_list);
3745 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3746 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3747
Tejun Heoe22bee72010-06-29 10:07:14 +02003748 init_timer_deferrable(&gcwq->idle_timer);
3749 gcwq->idle_timer.function = idle_worker_timeout;
3750 gcwq->idle_timer.data = (unsigned long)gcwq;
3751
3752 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3753 (unsigned long)gcwq);
3754
Tejun Heo8b03ae32010-06-29 10:07:12 +02003755 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003756
3757 gcwq->trustee_state = TRUSTEE_DONE;
3758 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003759 }
3760
Tejun Heoe22bee72010-06-29 10:07:14 +02003761 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003762 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003763 struct global_cwq *gcwq = get_gcwq(cpu);
3764 struct worker *worker;
3765
Tejun Heo477a3c32010-08-31 10:54:35 +02003766 if (cpu != WORK_CPU_UNBOUND)
3767 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003768 worker = create_worker(gcwq, true);
3769 BUG_ON(!worker);
3770 spin_lock_irq(&gcwq->lock);
3771 start_worker(worker);
3772 spin_unlock_irq(&gcwq->lock);
3773 }
3774
Tejun Heod320c032010-06-29 10:07:14 +02003775 system_wq = alloc_workqueue("events", 0, 0);
3776 system_long_wq = alloc_workqueue("events_long", 0, 0);
3777 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003778 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3779 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo4149efb2011-02-08 10:39:03 +01003780 system_freezeable_wq = alloc_workqueue("events_freezeable",
3781 WQ_FREEZEABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003782 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Tejun Heo4149efb2011-02-08 10:39:03 +01003783 !system_unbound_wq || !system_freezeable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003786early_initcall(init_workqueues);