blob: 575d092fa746ef04ab7d4107abd336c2441a3947 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020048 /* global_cwq flags */
Tejun 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 Heo55e3e1f2012-12-18 10:35:02 -0800131 work_func_t current_func; /* L: current_work's fn */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200132 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200133 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200134 struct task_struct *task; /* I: worker task */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200135 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heoe22bee72010-06-29 10:07:14 +0200136 /* 64 bytes boundary on 64bit, 32 on 32bit */
137 unsigned long last_active; /* L: last active timestamp */
138 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200140 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200141};
142
Tejun Heo4690c4a2010-06-29 10:07:10 +0200143/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200144 * Global per-cpu workqueue. There's one and only one for each cpu
145 * and all works are queued and processed here regardless of their
146 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200147 */
148struct global_cwq {
149 spinlock_t lock; /* the gcwq lock */
Tejun Heo7e116292010-06-29 10:07:13 +0200150 struct list_head worklist; /* L: list of pending works */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200151 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200152 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200153
154 int nr_workers; /* L: total number of workers */
155 int nr_idle; /* L: currently idle ones */
156
157 /* workers are chained either in the idle_list or busy_hash */
Tejun Heoe22bee72010-06-29 10:07:14 +0200158 struct list_head idle_list; /* X: list of idle workers */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200159 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
160 /* L: hash of busy workers */
161
Tejun Heoe22bee72010-06-29 10:07:14 +0200162 struct timer_list idle_timer; /* L: worker idle timeout */
163 struct timer_list mayday_timer; /* L: SOS timer for dworkers */
164
Tejun Heo8b03ae32010-06-29 10:07:12 +0200165 struct ida worker_ida; /* L: for worker IDs */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200166
167 struct task_struct *trustee; /* L: for gcwq shutdown */
168 unsigned int trustee_state; /* L: trustee state */
169 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heoe22bee72010-06-29 10:07:14 +0200170 struct worker *first_idle; /* L: first idle worker */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200171} ____cacheline_aligned_in_smp;
172
173/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200174 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200175 * work_struct->data are used for flags and thus cwqs need to be
176 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178struct cpu_workqueue_struct {
Tejun Heo8b03ae32010-06-29 10:07:12 +0200179 struct global_cwq *gcwq; /* I: the associated gcwq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200180 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200181 int work_color; /* L: current color */
182 int flush_color; /* L: flushing color */
183 int nr_in_flight[WORK_NR_COLORS];
184 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200185 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200186 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200187 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200191 * Structure used to wait for workqueue flush.
192 */
193struct wq_flusher {
194 struct list_head list; /* F: list of flushers */
195 int flush_color; /* F: flush color waiting for */
196 struct completion done; /* flush completion */
197};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Tejun Heo73f53c42010-06-29 10:07:11 +0200199/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200200 * All cpumasks are assumed to be always set on UP and thus can't be
201 * used to determine whether there's something to be done.
202 */
203#ifdef CONFIG_SMP
204typedef cpumask_var_t mayday_mask_t;
205#define mayday_test_and_set_cpu(cpu, mask) \
206 cpumask_test_and_set_cpu((cpu), (mask))
207#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
208#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200209#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200210#define free_mayday_mask(mask) free_cpumask_var((mask))
211#else
212typedef unsigned long mayday_mask_t;
213#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
214#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
215#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
216#define alloc_mayday_mask(maskp, gfp) true
217#define free_mayday_mask(mask) do { } while (0)
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/*
221 * The externally visible workqueue abstraction is an array of
222 * per-CPU workqueues:
223 */
224struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200225 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200226 union {
227 struct cpu_workqueue_struct __percpu *pcpu;
228 struct cpu_workqueue_struct *single;
229 unsigned long v;
230 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200231 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200232
233 struct mutex flush_mutex; /* protects wq flushing */
234 int work_color; /* F: current work color */
235 int flush_color; /* F: current flush color */
236 atomic_t nr_cwqs_to_flush; /* flush in progress */
237 struct wq_flusher *first_flusher; /* F: first flusher */
238 struct list_head flusher_queue; /* F: flush waiters */
239 struct list_head flusher_overflow; /* F: flush overflow list */
240
Tejun Heof2e005a2010-07-20 15:59:09 +0200241 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200242 struct worker *rescuer; /* I: rescue worker */
243
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200244 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200245 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700246#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200247 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700248#endif
Tejun Heob196be82012-01-10 15:11:35 -0800249 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Tejun Heod320c032010-06-29 10:07:14 +0200252struct workqueue_struct *system_wq __read_mostly;
253struct workqueue_struct *system_long_wq __read_mostly;
254struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200255struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100256struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100257struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200258EXPORT_SYMBOL_GPL(system_wq);
259EXPORT_SYMBOL_GPL(system_long_wq);
260EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200261EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100262EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100263EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200264
Tejun Heo97bd2342010-10-05 10:41:14 +0200265#define CREATE_TRACE_POINTS
266#include <trace/events/workqueue.h>
267
Tejun Heodb7bccf2010-06-29 10:07:12 +0200268#define for_each_busy_worker(worker, i, pos, gcwq) \
269 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
270 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
271
Tejun Heof3421792010-07-02 10:03:51 +0200272static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
273 unsigned int sw)
274{
275 if (cpu < nr_cpu_ids) {
276 if (sw & 1) {
277 cpu = cpumask_next(cpu, mask);
278 if (cpu < nr_cpu_ids)
279 return cpu;
280 }
281 if (sw & 2)
282 return WORK_CPU_UNBOUND;
283 }
284 return WORK_CPU_NONE;
285}
286
287static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
288 struct workqueue_struct *wq)
289{
290 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
291}
292
Tejun Heo09884952010-08-01 11:50:12 +0200293/*
294 * CPU iterators
295 *
296 * An extra gcwq is defined for an invalid cpu number
297 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
298 * specific CPU. The following iterators are similar to
299 * for_each_*_cpu() iterators but also considers the unbound gcwq.
300 *
301 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
302 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
303 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
304 * WORK_CPU_UNBOUND for unbound workqueues
305 */
Tejun Heof3421792010-07-02 10:03:51 +0200306#define for_each_gcwq_cpu(cpu) \
307 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
308 (cpu) < WORK_CPU_NONE; \
309 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
310
311#define for_each_online_gcwq_cpu(cpu) \
312 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
313 (cpu) < WORK_CPU_NONE; \
314 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
315
316#define for_each_cwq_cpu(cpu, wq) \
317 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
318 (cpu) < WORK_CPU_NONE; \
319 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
320
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900321#ifdef CONFIG_DEBUG_OBJECTS_WORK
322
323static struct debug_obj_descr work_debug_descr;
324
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100325static void *work_debug_hint(void *addr)
326{
327 return ((struct work_struct *) addr)->func;
328}
329
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900330/*
331 * fixup_init is called when:
332 * - an active object is initialized
333 */
334static int work_fixup_init(void *addr, enum debug_obj_state state)
335{
336 struct work_struct *work = addr;
337
338 switch (state) {
339 case ODEBUG_STATE_ACTIVE:
340 cancel_work_sync(work);
341 debug_object_init(work, &work_debug_descr);
342 return 1;
343 default:
344 return 0;
345 }
346}
347
348/*
349 * fixup_activate is called when:
350 * - an active object is activated
351 * - an unknown object is activated (might be a statically initialized object)
352 */
353static int work_fixup_activate(void *addr, enum debug_obj_state state)
354{
355 struct work_struct *work = addr;
356
357 switch (state) {
358
359 case ODEBUG_STATE_NOTAVAILABLE:
360 /*
361 * This is not really a fixup. The work struct was
362 * statically initialized. We just make sure that it
363 * is tracked in the object tracker.
364 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200365 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900366 debug_object_init(work, &work_debug_descr);
367 debug_object_activate(work, &work_debug_descr);
368 return 0;
369 }
370 WARN_ON_ONCE(1);
371 return 0;
372
373 case ODEBUG_STATE_ACTIVE:
374 WARN_ON(1);
375
376 default:
377 return 0;
378 }
379}
380
381/*
382 * fixup_free is called when:
383 * - an active object is freed
384 */
385static int work_fixup_free(void *addr, enum debug_obj_state state)
386{
387 struct work_struct *work = addr;
388
389 switch (state) {
390 case ODEBUG_STATE_ACTIVE:
391 cancel_work_sync(work);
392 debug_object_free(work, &work_debug_descr);
393 return 1;
394 default:
395 return 0;
396 }
397}
398
399static struct debug_obj_descr work_debug_descr = {
400 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100401 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900402 .fixup_init = work_fixup_init,
403 .fixup_activate = work_fixup_activate,
404 .fixup_free = work_fixup_free,
405};
406
407static inline void debug_work_activate(struct work_struct *work)
408{
409 debug_object_activate(work, &work_debug_descr);
410}
411
412static inline void debug_work_deactivate(struct work_struct *work)
413{
414 debug_object_deactivate(work, &work_debug_descr);
415}
416
417void __init_work(struct work_struct *work, int onstack)
418{
419 if (onstack)
420 debug_object_init_on_stack(work, &work_debug_descr);
421 else
422 debug_object_init(work, &work_debug_descr);
423}
424EXPORT_SYMBOL_GPL(__init_work);
425
426void destroy_work_on_stack(struct work_struct *work)
427{
428 debug_object_free(work, &work_debug_descr);
429}
430EXPORT_SYMBOL_GPL(destroy_work_on_stack);
431
432#else
433static inline void debug_work_activate(struct work_struct *work) { }
434static inline void debug_work_deactivate(struct work_struct *work) { }
435#endif
436
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100437/* Serializes the accesses to the list of workqueues. */
438static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200440static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Oleg Nesterov14441962007-05-23 13:57:57 -0700442/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200443 * The almighty global cpu workqueues. nr_running is the only field
444 * which is expected to be used frequently by other cpus via
445 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700446 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200447static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200448static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800449
Tejun Heof3421792010-07-02 10:03:51 +0200450/*
451 * Global cpu workqueue and nr_running counter for unbound gcwq. The
452 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
453 * workers have WORKER_UNBOUND set.
454 */
455static struct global_cwq unbound_global_cwq;
456static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
457
Tejun Heoc34056a2010-06-29 10:07:11 +0200458static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Tejun Heo8b03ae32010-06-29 10:07:12 +0200460static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Tejun Heof3421792010-07-02 10:03:51 +0200462 if (cpu != WORK_CPU_UNBOUND)
463 return &per_cpu(global_cwq, cpu);
464 else
465 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Tejun Heoe22bee72010-06-29 10:07:14 +0200468static atomic_t *get_gcwq_nr_running(unsigned int cpu)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700469{
Tejun Heof3421792010-07-02 10:03:51 +0200470 if (cpu != WORK_CPU_UNBOUND)
471 return &per_cpu(gcwq_nr_running, cpu);
472 else
473 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700474}
475
Tejun Heo4690c4a2010-06-29 10:07:10 +0200476static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
477 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700478{
Tejun Heof3421792010-07-02 10:03:51 +0200479 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800480 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200481 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200482 } else if (likely(cpu == WORK_CPU_UNBOUND))
483 return wq->cpu_wq.single;
484 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700485}
486
Tejun Heo73f53c42010-06-29 10:07:11 +0200487static unsigned int work_color_to_flags(int color)
488{
489 return color << WORK_STRUCT_COLOR_SHIFT;
490}
491
492static int get_work_color(struct work_struct *work)
493{
494 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
495 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
496}
497
498static int work_next_color(int color)
499{
500 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
David Howells4594bf12006-12-07 11:33:26 +0000503/*
Tejun Heoe1201532010-07-22 14:14:25 +0200504 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
505 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
506 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200507 *
508 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
509 * cwq, cpu or clear work->data. These functions should only be
510 * called while the work is owned - ie. while the PENDING bit is set.
511 *
512 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
513 * corresponding to a work. gcwq is available once the work has been
514 * queued anywhere after initialization. cwq is available only from
515 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000516 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200517static inline void set_work_data(struct work_struct *work, unsigned long data,
518 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000519{
David Howells4594bf12006-12-07 11:33:26 +0000520 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200521 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000522}
David Howells365970a2006-11-22 14:54:49 +0000523
Tejun Heo7a22ad72010-06-29 10:07:13 +0200524static void set_work_cwq(struct work_struct *work,
525 struct cpu_workqueue_struct *cwq,
526 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200527{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200528 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200529 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200530}
531
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000533{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200534 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
535}
536
537static void clear_work_data(struct work_struct *work)
538{
539 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
540}
541
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
543{
Tejun Heoe1201532010-07-22 14:14:25 +0200544 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545
Tejun Heoe1201532010-07-22 14:14:25 +0200546 if (data & WORK_STRUCT_CWQ)
547 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
548 else
549 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550}
551
552static struct global_cwq *get_work_gcwq(struct work_struct *work)
553{
Tejun Heoe1201532010-07-22 14:14:25 +0200554 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555 unsigned int cpu;
556
Tejun Heoe1201532010-07-22 14:14:25 +0200557 if (data & WORK_STRUCT_CWQ)
558 return ((struct cpu_workqueue_struct *)
559 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560
561 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200562 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200563 return NULL;
564
Tejun Heof3421792010-07-02 10:03:51 +0200565 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000567}
568
569/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200570 * Policy functions. These define the policies on how the global
571 * worker pool is managed. Unless noted otherwise, these functions
572 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000573 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200574
Tejun Heo649027d2010-06-29 10:07:14 +0200575static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000576{
Tejun Heo649027d2010-06-29 10:07:14 +0200577 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
578 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000579}
580
Tejun Heoe22bee72010-06-29 10:07:14 +0200581/*
582 * Need to wake up a worker? Called from anything but currently
583 * running workers.
584 */
585static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000586{
Tejun Heo649027d2010-06-29 10:07:14 +0200587 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000588}
589
Tejun Heoe22bee72010-06-29 10:07:14 +0200590/* Can I start working? Called from busy but !running workers. */
591static bool may_start_working(struct global_cwq *gcwq)
592{
593 return gcwq->nr_idle;
594}
595
596/* Do I need to keep working? Called from currently running workers. */
597static bool keep_working(struct global_cwq *gcwq)
598{
599 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
600
Tejun Heo30310042010-10-11 11:51:57 +0200601 return !list_empty(&gcwq->worklist) &&
602 (atomic_read(nr_running) <= 1 ||
603 gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200604}
605
606/* Do we need a new worker? Called from manager. */
607static bool need_to_create_worker(struct global_cwq *gcwq)
608{
609 return need_more_worker(gcwq) && !may_start_working(gcwq);
610}
611
612/* Do I need to be the manager? */
613static bool need_to_manage_workers(struct global_cwq *gcwq)
614{
615 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
616}
617
618/* Do we have too many workers and should some go away? */
619static bool too_many_workers(struct global_cwq *gcwq)
620{
621 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
622 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
623 int nr_busy = gcwq->nr_workers - nr_idle;
624
625 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
626}
627
628/*
629 * Wake up functions.
630 */
631
Tejun Heo7e116292010-06-29 10:07:13 +0200632/* Return the first worker. Safe with preemption disabled */
633static struct worker *first_worker(struct global_cwq *gcwq)
634{
635 if (unlikely(list_empty(&gcwq->idle_list)))
636 return NULL;
637
638 return list_first_entry(&gcwq->idle_list, struct worker, entry);
639}
640
641/**
642 * wake_up_worker - wake up an idle worker
643 * @gcwq: gcwq to wake worker for
644 *
645 * Wake up the first idle worker of @gcwq.
646 *
647 * CONTEXT:
648 * spin_lock_irq(gcwq->lock).
649 */
650static void wake_up_worker(struct global_cwq *gcwq)
651{
652 struct worker *worker = first_worker(gcwq);
653
654 if (likely(worker))
655 wake_up_process(worker->task);
656}
657
Tejun Heo4690c4a2010-06-29 10:07:10 +0200658/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200659 * wq_worker_waking_up - a worker is waking up
660 * @task: task waking up
661 * @cpu: CPU @task is waking up to
662 *
663 * This function is called during try_to_wake_up() when a worker is
664 * being awoken.
665 *
666 * CONTEXT:
667 * spin_lock_irq(rq->lock)
668 */
669void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
670{
671 struct worker *worker = kthread_data(task);
672
Steven Rostedt2d646722010-12-03 23:12:33 -0500673 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe22bee72010-06-29 10:07:14 +0200674 atomic_inc(get_gcwq_nr_running(cpu));
675}
676
677/**
678 * wq_worker_sleeping - a worker is going to sleep
679 * @task: task going to sleep
680 * @cpu: CPU in question, must be the current CPU number
681 *
682 * This function is called during schedule() when a busy worker is
683 * going to sleep. Worker on the same cpu can be woken up by
684 * returning pointer to its task.
685 *
686 * CONTEXT:
687 * spin_lock_irq(rq->lock)
688 *
689 * RETURNS:
690 * Worker task on @cpu to wake up, %NULL if none.
691 */
692struct task_struct *wq_worker_sleeping(struct task_struct *task,
693 unsigned int cpu)
694{
695 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
696 struct global_cwq *gcwq = get_gcwq(cpu);
697 atomic_t *nr_running = get_gcwq_nr_running(cpu);
698
Steven Rostedt2d646722010-12-03 23:12:33 -0500699 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200700 return NULL;
701
702 /* this can only happen on the local cpu */
703 BUG_ON(cpu != raw_smp_processor_id());
704
705 /*
706 * The counterpart of the following dec_and_test, implied mb,
707 * worklist not empty test sequence is in insert_work().
708 * Please read comment there.
709 *
710 * NOT_RUNNING is clear. This means that trustee is not in
711 * charge and we're running on the local cpu w/ rq lock held
712 * and preemption disabled, which in turn means that none else
713 * could be manipulating idle_list, so dereferencing idle_list
714 * without gcwq lock is safe.
715 */
716 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
717 to_wakeup = first_worker(gcwq);
718 return to_wakeup ? to_wakeup->task : NULL;
719}
720
721/**
722 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200723 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200724 * @flags: flags to set
725 * @wakeup: wakeup an idle worker if necessary
726 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200727 * Set @flags in @worker->flags and adjust nr_running accordingly. If
728 * nr_running becomes zero and @wakeup is %true, an idle worker is
729 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200730 *
Tejun Heocb444762010-07-02 10:03:50 +0200731 * CONTEXT:
732 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200733 */
734static inline void worker_set_flags(struct worker *worker, unsigned int flags,
735 bool wakeup)
736{
Tejun Heoe22bee72010-06-29 10:07:14 +0200737 struct global_cwq *gcwq = worker->gcwq;
738
Tejun Heocb444762010-07-02 10:03:50 +0200739 WARN_ON_ONCE(worker->task != current);
740
Tejun Heoe22bee72010-06-29 10:07:14 +0200741 /*
742 * If transitioning into NOT_RUNNING, adjust nr_running and
743 * wake up an idle worker as necessary if requested by
744 * @wakeup.
745 */
746 if ((flags & WORKER_NOT_RUNNING) &&
747 !(worker->flags & WORKER_NOT_RUNNING)) {
748 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
749
750 if (wakeup) {
751 if (atomic_dec_and_test(nr_running) &&
752 !list_empty(&gcwq->worklist))
753 wake_up_worker(gcwq);
754 } else
755 atomic_dec(nr_running);
756 }
757
Tejun Heod302f012010-06-29 10:07:13 +0200758 worker->flags |= flags;
759}
760
761/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200762 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200763 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200764 * @flags: flags to clear
765 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200766 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200767 *
Tejun Heocb444762010-07-02 10:03:50 +0200768 * CONTEXT:
769 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200770 */
771static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
772{
Tejun Heoe22bee72010-06-29 10:07:14 +0200773 struct global_cwq *gcwq = worker->gcwq;
774 unsigned int oflags = worker->flags;
775
Tejun Heocb444762010-07-02 10:03:50 +0200776 WARN_ON_ONCE(worker->task != current);
777
Tejun Heod302f012010-06-29 10:07:13 +0200778 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200779
Tejun Heo42c025f2011-01-11 15:58:49 +0100780 /*
781 * If transitioning out of NOT_RUNNING, increment nr_running. Note
782 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
783 * of multiple flags, not a single flag.
784 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200785 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
786 if (!(worker->flags & WORKER_NOT_RUNNING))
787 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200788}
789
790/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200791 * busy_worker_head - return the busy hash head for a work
792 * @gcwq: gcwq of interest
793 * @work: work to be hashed
794 *
795 * Return hash head of @gcwq for @work.
796 *
797 * CONTEXT:
798 * spin_lock_irq(gcwq->lock).
799 *
800 * RETURNS:
801 * Pointer to the hash head.
802 */
803static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
804 struct work_struct *work)
805{
806 const int base_shift = ilog2(sizeof(struct work_struct));
807 unsigned long v = (unsigned long)work;
808
809 /* simple shift and fold hash, do we need something better? */
810 v >>= base_shift;
811 v += v >> BUSY_WORKER_HASH_ORDER;
812 v &= BUSY_WORKER_HASH_MASK;
813
814 return &gcwq->busy_hash[v];
815}
816
817/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200818 * __find_worker_executing_work - find worker which is executing a work
819 * @gcwq: gcwq of interest
820 * @bwh: hash head as returned by busy_worker_head()
821 * @work: work to find worker for
822 *
823 * Find a worker which is executing @work on @gcwq. @bwh should be
824 * the hash head obtained by calling busy_worker_head() with the same
825 * work.
826 *
827 * CONTEXT:
828 * spin_lock_irq(gcwq->lock).
829 *
830 * RETURNS:
831 * Pointer to worker which is executing @work if found, NULL
832 * otherwise.
833 */
834static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
835 struct hlist_head *bwh,
836 struct work_struct *work)
837{
838 struct worker *worker;
839 struct hlist_node *tmp;
840
841 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800842 if (worker->current_work == work &&
843 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200844 return worker;
845 return NULL;
846}
847
848/**
849 * find_worker_executing_work - find worker which is executing a work
850 * @gcwq: gcwq of interest
851 * @work: work to find worker for
852 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800853 * Find a worker which is executing @work on @gcwq by searching
854 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
855 * to match, its current execution should match the address of @work and
856 * its work function. This is to avoid unwanted dependency between
857 * unrelated work executions through a work item being recycled while still
858 * being executed.
859 *
860 * This is a bit tricky. A work item may be freed once its execution
861 * starts and nothing prevents the freed area from being recycled for
862 * another work item. If the same work item address ends up being reused
863 * before the original execution finishes, workqueue will identify the
864 * recycled work item as currently executing and make it wait until the
865 * current execution finishes, introducing an unwanted dependency.
866 *
867 * This function checks the work item address, work function and workqueue
868 * to avoid false positives. Note that this isn't complete as one may
869 * construct a work function which can introduce dependency onto itself
870 * through a recycled work item. Well, if somebody wants to shoot oneself
871 * in the foot that badly, there's only so much we can do, and if such
872 * deadlock actually occurs, it should be easy to locate the culprit work
873 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200874 *
875 * CONTEXT:
876 * spin_lock_irq(gcwq->lock).
877 *
878 * RETURNS:
879 * Pointer to worker which is executing @work if found, NULL
880 * otherwise.
881 */
882static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
883 struct work_struct *work)
884{
885 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
886 work);
887}
888
889/**
Tejun Heo649027d2010-06-29 10:07:14 +0200890 * gcwq_determine_ins_pos - find insertion position
891 * @gcwq: gcwq of interest
892 * @cwq: cwq a work is being queued for
893 *
894 * A work for @cwq is about to be queued on @gcwq, determine insertion
895 * position for the work. If @cwq is for HIGHPRI wq, the work is
896 * queued at the head of the queue but in FIFO order with respect to
897 * other HIGHPRI works; otherwise, at the end of the queue. This
898 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
899 * there are HIGHPRI works pending.
900 *
901 * CONTEXT:
902 * spin_lock_irq(gcwq->lock).
903 *
904 * RETURNS:
905 * Pointer to inserstion position.
906 */
907static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
908 struct cpu_workqueue_struct *cwq)
909{
910 struct work_struct *twork;
911
912 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
913 return &gcwq->worklist;
914
915 list_for_each_entry(twork, &gcwq->worklist, entry) {
916 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
917
918 if (!(tcwq->wq->flags & WQ_HIGHPRI))
919 break;
920 }
921
922 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
923 return &twork->entry;
924}
925
926/**
Tejun Heo7e116292010-06-29 10:07:13 +0200927 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200928 * @cwq: cwq @work belongs to
929 * @work: work to insert
930 * @head: insertion point
931 * @extra_flags: extra WORK_STRUCT_* flags to set
932 *
Tejun Heo7e116292010-06-29 10:07:13 +0200933 * Insert @work which belongs to @cwq into @gcwq after @head.
934 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200935 *
936 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200937 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200938 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700939static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200940 struct work_struct *work, struct list_head *head,
941 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700942{
Tejun Heoe22bee72010-06-29 10:07:14 +0200943 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100944
Tejun Heo4690c4a2010-06-29 10:07:10 +0200945 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200946 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200947
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700948 /*
949 * Ensure that we get the right work->data if we see the
950 * result of list_add() below, see try_to_grab_pending().
951 */
952 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200953
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700954 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200955
956 /*
957 * Ensure either worker_sched_deactivated() sees the above
958 * list_add_tail() or we see zero nr_running to avoid workers
959 * lying around lazily while there are works to be processed.
960 */
961 smp_mb();
962
Tejun Heo649027d2010-06-29 10:07:14 +0200963 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200964 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700965}
966
Tejun Heoc8efcc22010-12-20 19:32:04 +0100967/*
968 * Test whether @work is being queued from another work executing on the
969 * same workqueue. This is rather expensive and should only be used from
970 * cold paths.
971 */
972static bool is_chained_work(struct workqueue_struct *wq)
973{
974 unsigned long flags;
975 unsigned int cpu;
976
977 for_each_gcwq_cpu(cpu) {
978 struct global_cwq *gcwq = get_gcwq(cpu);
979 struct worker *worker;
980 struct hlist_node *pos;
981 int i;
982
983 spin_lock_irqsave(&gcwq->lock, flags);
984 for_each_busy_worker(worker, i, pos, gcwq) {
985 if (worker->task != current)
986 continue;
987 spin_unlock_irqrestore(&gcwq->lock, flags);
988 /*
989 * I'm @worker, no locking necessary. See if @work
990 * is headed to the same workqueue.
991 */
992 return worker->current_cwq->wq == wq;
993 }
994 spin_unlock_irqrestore(&gcwq->lock, flags);
995 }
996 return false;
997}
998
Tejun Heo4690c4a2010-06-29 10:07:10 +0200999static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct work_struct *work)
1001{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001002 struct global_cwq *gcwq;
1003 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001004 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001005 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned long flags;
1007
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001008 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001009
Tejun Heoc8efcc22010-12-20 19:32:04 +01001010 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001011 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001012 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001013 return;
1014
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001015 /* determine gcwq to use */
1016 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001017 struct global_cwq *last_gcwq;
1018
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001019 if (unlikely(cpu == WORK_CPU_UNBOUND))
1020 cpu = raw_smp_processor_id();
1021
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001022 /*
1023 * It's multi cpu. If @wq is non-reentrant and @work
1024 * was previously on a different cpu, it might still
1025 * be running there, in which case the work needs to
1026 * be queued on that cpu to guarantee non-reentrance.
1027 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001028 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001029 if (wq->flags & WQ_NON_REENTRANT &&
1030 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1031 struct worker *worker;
1032
1033 spin_lock_irqsave(&last_gcwq->lock, flags);
1034
1035 worker = find_worker_executing_work(last_gcwq, work);
1036
1037 if (worker && worker->current_cwq->wq == wq)
1038 gcwq = last_gcwq;
1039 else {
1040 /* meh... not running there, queue here */
1041 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1042 spin_lock_irqsave(&gcwq->lock, flags);
1043 }
1044 } else
1045 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001046 } else {
1047 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1048 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001049 }
1050
1051 /* gcwq determined, get cwq and queue */
1052 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001053 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001054
Tejun Heo4690c4a2010-06-29 10:07:10 +02001055 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056
Tejun Heo73f53c42010-06-29 10:07:11 +02001057 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001058 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001059
1060 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001061 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001063 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001064 } else {
1065 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001066 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001067 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001068
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001069 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001070
Tejun Heo8b03ae32010-06-29 10:07:12 +02001071 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001074/**
1075 * queue_work - queue work on a workqueue
1076 * @wq: workqueue to use
1077 * @work: work to queue
1078 *
Alan Stern057647f2006-10-28 10:38:58 -07001079 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001081 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1082 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001084int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001088 ret = queue_work_on(get_cpu(), wq, work);
1089 put_cpu();
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return ret;
1092}
Dave Jonesae90dd52006-06-30 01:40:45 -04001093EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Zhang Ruic1a220e2008-07-23 21:28:39 -07001095/**
1096 * queue_work_on - queue work on specific cpu
1097 * @cpu: CPU number to execute work on
1098 * @wq: workqueue to use
1099 * @work: work to queue
1100 *
1101 * Returns 0 if @work was already on a queue, non-zero otherwise.
1102 *
1103 * We queue the work to a specific CPU, the caller must ensure it
1104 * can't go away.
1105 */
1106int
1107queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1108{
1109 int ret = 0;
1110
Tejun Heo22df02b2010-06-29 10:07:10 +02001111 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001112 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001113 ret = 1;
1114 }
1115 return ret;
1116}
1117EXPORT_SYMBOL_GPL(queue_work_on);
1118
Li Zefan6d141c32008-02-08 04:21:09 -08001119static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
David Howells52bad642006-11-22 14:54:01 +00001121 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001122 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Tejun Heo4690c4a2010-06-29 10:07:10 +02001124 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001127/**
1128 * queue_delayed_work - queue work on a workqueue after delay
1129 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001130 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001131 * @delay: number of jiffies to wait before queueing
1132 *
Alan Stern057647f2006-10-28 10:38:58 -07001133 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001134 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001135int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001136 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
David Howells52bad642006-11-22 14:54:01 +00001138 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001139 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001141 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
Dave Jonesae90dd52006-06-30 01:40:45 -04001143EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001145/**
1146 * queue_delayed_work_on - queue work on specific CPU after delay
1147 * @cpu: CPU number to execute work on
1148 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001149 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001150 * @delay: number of jiffies to wait before queueing
1151 *
Alan Stern057647f2006-10-28 10:38:58 -07001152 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001153 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001154int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001155 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001156{
1157 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001158 struct timer_list *timer = &dwork->timer;
1159 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001160
Tejun Heo22df02b2010-06-29 10:07:10 +02001161 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001162 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001163
Tejun Heo4afca922012-12-04 07:40:39 -08001164 WARN_ON_ONCE(timer_pending(timer));
1165 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001166
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001167 timer_stats_timer_set_start_info(&dwork->timer);
1168
Tejun Heo7a22ad72010-06-29 10:07:13 +02001169 /*
1170 * This stores cwq for the moment, for the timer_fn.
1171 * Note that the work's gcwq is preserved to allow
1172 * reentrance detection for delayed works.
1173 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001174 if (!(wq->flags & WQ_UNBOUND)) {
1175 struct global_cwq *gcwq = get_work_gcwq(work);
1176
1177 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1178 lcpu = gcwq->cpu;
1179 else
1180 lcpu = raw_smp_processor_id();
1181 } else
1182 lcpu = WORK_CPU_UNBOUND;
1183
Tejun Heo7a22ad72010-06-29 10:07:13 +02001184 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001185
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001186 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001187 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001188 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001189
1190 if (unlikely(cpu >= 0))
1191 add_timer_on(timer, cpu);
1192 else
1193 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001194 ret = 1;
1195 }
1196 return ret;
1197}
Dave Jonesae90dd52006-06-30 01:40:45 -04001198EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tejun Heoc8e55f32010-06-29 10:07:12 +02001200/**
1201 * worker_enter_idle - enter idle state
1202 * @worker: worker which is entering idle state
1203 *
1204 * @worker is entering idle state. Update stats and idle timer if
1205 * necessary.
1206 *
1207 * LOCKING:
1208 * spin_lock_irq(gcwq->lock).
1209 */
1210static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001212 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Tejun Heoc8e55f32010-06-29 10:07:12 +02001214 BUG_ON(worker->flags & WORKER_IDLE);
1215 BUG_ON(!list_empty(&worker->entry) &&
1216 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Tejun Heocb444762010-07-02 10:03:50 +02001218 /* can't use worker_set_flags(), also called from start_worker() */
1219 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001220 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001221 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001222
Tejun Heoc8e55f32010-06-29 10:07:12 +02001223 /* idle_list is LIFO */
1224 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001225
Tejun Heoe22bee72010-06-29 10:07:14 +02001226 if (likely(!(worker->flags & WORKER_ROGUE))) {
1227 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1228 mod_timer(&gcwq->idle_timer,
1229 jiffies + IDLE_WORKER_TIMEOUT);
1230 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001231 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001232
Tejun Heo24312d32012-05-14 15:04:50 -07001233 /*
1234 * Sanity check nr_running. Because trustee releases gcwq->lock
1235 * between setting %WORKER_ROGUE and zapping nr_running, the
1236 * warning may trigger spuriously. Check iff trustee is idle.
1237 */
1238 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
1239 gcwq->nr_workers == gcwq->nr_idle &&
Tejun Heocb444762010-07-02 10:03:50 +02001240 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Tejun Heoc8e55f32010-06-29 10:07:12 +02001243/**
1244 * worker_leave_idle - leave idle state
1245 * @worker: worker which is leaving idle state
1246 *
1247 * @worker is leaving idle state. Update stats.
1248 *
1249 * LOCKING:
1250 * spin_lock_irq(gcwq->lock).
1251 */
1252static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001254 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Tejun Heoc8e55f32010-06-29 10:07:12 +02001256 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001257 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001258 gcwq->nr_idle--;
1259 list_del_init(&worker->entry);
1260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Tejun Heoe22bee72010-06-29 10:07:14 +02001262/**
1263 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1264 * @worker: self
1265 *
1266 * Works which are scheduled while the cpu is online must at least be
1267 * scheduled to a worker which is bound to the cpu so that if they are
1268 * flushed from cpu callbacks while cpu is going down, they are
1269 * guaranteed to execute on the cpu.
1270 *
1271 * This function is to be used by rogue workers and rescuers to bind
1272 * themselves to the target cpu and may race with cpu going down or
1273 * coming online. kthread_bind() can't be used because it may put the
1274 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1275 * verbatim as it's best effort and blocking and gcwq may be
1276 * [dis]associated in the meantime.
1277 *
1278 * This function tries set_cpus_allowed() and locks gcwq and verifies
1279 * the binding against GCWQ_DISASSOCIATED which is set during
1280 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1281 * idle state or fetches works without dropping lock, it can guarantee
1282 * the scheduling requirement described in the first paragraph.
1283 *
1284 * CONTEXT:
1285 * Might sleep. Called without any lock but returns with gcwq->lock
1286 * held.
1287 *
1288 * RETURNS:
1289 * %true if the associated gcwq is online (@worker is successfully
1290 * bound), %false if offline.
1291 */
1292static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001293__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001294{
1295 struct global_cwq *gcwq = worker->gcwq;
1296 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Tejun Heoe22bee72010-06-29 10:07:14 +02001298 while (true) {
1299 /*
1300 * The following call may fail, succeed or succeed
1301 * without actually migrating the task to the cpu if
1302 * it races with cpu hotunplug operation. Verify
1303 * against GCWQ_DISASSOCIATED.
1304 */
Tejun Heof3421792010-07-02 10:03:51 +02001305 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1306 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001307
Tejun Heoe22bee72010-06-29 10:07:14 +02001308 spin_lock_irq(&gcwq->lock);
1309 if (gcwq->flags & GCWQ_DISASSOCIATED)
1310 return false;
1311 if (task_cpu(task) == gcwq->cpu &&
1312 cpumask_equal(&current->cpus_allowed,
1313 get_cpu_mask(gcwq->cpu)))
1314 return true;
1315 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001316
Tejun Heo5035b202011-04-29 18:08:37 +02001317 /*
1318 * We've raced with CPU hot[un]plug. Give it a breather
1319 * and retry migration. cond_resched() is required here;
1320 * otherwise, we might deadlock against cpu_stop trying to
1321 * bring down the CPU on non-preemptive kernel.
1322 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001323 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001324 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001325 }
1326}
1327
1328/*
1329 * Function for worker->rebind_work used to rebind rogue busy workers
1330 * to the associated cpu which is coming back online. This is
1331 * scheduled by cpu up but can race with other cpu hotplug operations
1332 * and may be executed twice without intervening cpu down.
1333 */
1334static void worker_rebind_fn(struct work_struct *work)
1335{
1336 struct worker *worker = container_of(work, struct worker, rebind_work);
1337 struct global_cwq *gcwq = worker->gcwq;
1338
1339 if (worker_maybe_bind_and_lock(worker))
1340 worker_clr_flags(worker, WORKER_REBIND);
1341
1342 spin_unlock_irq(&gcwq->lock);
1343}
1344
Tejun Heoc34056a2010-06-29 10:07:11 +02001345static struct worker *alloc_worker(void)
1346{
1347 struct worker *worker;
1348
1349 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001350 if (worker) {
1351 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001352 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001353 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1354 /* on creation a worker is in !idle && prep state */
1355 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001356 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001357 return worker;
1358}
1359
1360/**
1361 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001362 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001363 * @bind: whether to set affinity to @cpu or not
1364 *
Tejun Heo7e116292010-06-29 10:07:13 +02001365 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001366 * can be started by calling start_worker() or destroyed using
1367 * destroy_worker().
1368 *
1369 * CONTEXT:
1370 * Might sleep. Does GFP_KERNEL allocations.
1371 *
1372 * RETURNS:
1373 * Pointer to the newly created worker.
1374 */
Tejun Heo7e116292010-06-29 10:07:13 +02001375static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001376{
Tejun Heof3421792010-07-02 10:03:51 +02001377 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001378 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001379 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001380
Tejun Heo8b03ae32010-06-29 10:07:12 +02001381 spin_lock_irq(&gcwq->lock);
1382 while (ida_get_new(&gcwq->worker_ida, &id)) {
1383 spin_unlock_irq(&gcwq->lock);
1384 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001385 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001386 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001387 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001388 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001389
1390 worker = alloc_worker();
1391 if (!worker)
1392 goto fail;
1393
Tejun Heo8b03ae32010-06-29 10:07:12 +02001394 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001395 worker->id = id;
1396
Tejun Heof3421792010-07-02 10:03:51 +02001397 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001398 worker->task = kthread_create_on_node(worker_thread,
1399 worker,
1400 cpu_to_node(gcwq->cpu),
1401 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001402 else
1403 worker->task = kthread_create(worker_thread, worker,
1404 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001405 if (IS_ERR(worker->task))
1406 goto fail;
1407
Tejun Heodb7bccf2010-06-29 10:07:12 +02001408 /*
1409 * A rogue worker will become a regular one if CPU comes
1410 * online later on. Make sure every worker has
1411 * PF_THREAD_BOUND set.
1412 */
Tejun Heof3421792010-07-02 10:03:51 +02001413 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001414 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001415 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001416 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001417 if (on_unbound_cpu)
1418 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001420
Tejun Heoc34056a2010-06-29 10:07:11 +02001421 return worker;
1422fail:
1423 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001424 spin_lock_irq(&gcwq->lock);
1425 ida_remove(&gcwq->worker_ida, id);
1426 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001427 }
1428 kfree(worker);
1429 return NULL;
1430}
1431
1432/**
1433 * start_worker - start a newly created worker
1434 * @worker: worker to start
1435 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001436 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001437 *
1438 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001439 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001440 */
1441static void start_worker(struct worker *worker)
1442{
Tejun Heocb444762010-07-02 10:03:50 +02001443 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001444 worker->gcwq->nr_workers++;
1445 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001446 wake_up_process(worker->task);
1447}
1448
1449/**
1450 * destroy_worker - destroy a workqueue worker
1451 * @worker: worker to be destroyed
1452 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001453 * Destroy @worker and adjust @gcwq stats accordingly.
1454 *
1455 * CONTEXT:
1456 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001457 */
1458static void destroy_worker(struct worker *worker)
1459{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001460 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001461 int id = worker->id;
1462
1463 /* sanity check frenzy */
1464 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001465 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001466
Tejun Heoc8e55f32010-06-29 10:07:12 +02001467 if (worker->flags & WORKER_STARTED)
1468 gcwq->nr_workers--;
1469 if (worker->flags & WORKER_IDLE)
1470 gcwq->nr_idle--;
1471
1472 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001473 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001474
1475 spin_unlock_irq(&gcwq->lock);
1476
Tejun Heoc34056a2010-06-29 10:07:11 +02001477 kthread_stop(worker->task);
1478 kfree(worker);
1479
Tejun Heo8b03ae32010-06-29 10:07:12 +02001480 spin_lock_irq(&gcwq->lock);
1481 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001482}
1483
Tejun Heoe22bee72010-06-29 10:07:14 +02001484static void idle_worker_timeout(unsigned long __gcwq)
1485{
1486 struct global_cwq *gcwq = (void *)__gcwq;
1487
1488 spin_lock_irq(&gcwq->lock);
1489
1490 if (too_many_workers(gcwq)) {
1491 struct worker *worker;
1492 unsigned long expires;
1493
1494 /* idle_list is kept in LIFO order, check the last one */
1495 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1496 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1497
1498 if (time_before(jiffies, expires))
1499 mod_timer(&gcwq->idle_timer, expires);
1500 else {
1501 /* it's been idle for too long, wake up manager */
1502 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1503 wake_up_worker(gcwq);
1504 }
1505 }
1506
1507 spin_unlock_irq(&gcwq->lock);
1508}
1509
1510static bool send_mayday(struct work_struct *work)
1511{
1512 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1513 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001514 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001515
1516 if (!(wq->flags & WQ_RESCUER))
1517 return false;
1518
1519 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001520 cpu = cwq->gcwq->cpu;
1521 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1522 if (cpu == WORK_CPU_UNBOUND)
1523 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001524 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001525 wake_up_process(wq->rescuer->task);
1526 return true;
1527}
1528
1529static void gcwq_mayday_timeout(unsigned long __gcwq)
1530{
1531 struct global_cwq *gcwq = (void *)__gcwq;
1532 struct work_struct *work;
1533
1534 spin_lock_irq(&gcwq->lock);
1535
1536 if (need_to_create_worker(gcwq)) {
1537 /*
1538 * We've been trying to create a new worker but
1539 * haven't been successful. We might be hitting an
1540 * allocation deadlock. Send distress signals to
1541 * rescuers.
1542 */
1543 list_for_each_entry(work, &gcwq->worklist, entry)
1544 send_mayday(work);
1545 }
1546
1547 spin_unlock_irq(&gcwq->lock);
1548
1549 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1550}
1551
1552/**
1553 * maybe_create_worker - create a new worker if necessary
1554 * @gcwq: gcwq to create a new worker for
1555 *
1556 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1557 * have at least one idle worker on return from this function. If
1558 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1559 * sent to all rescuers with works scheduled on @gcwq to resolve
1560 * possible allocation deadlock.
1561 *
1562 * On return, need_to_create_worker() is guaranteed to be false and
1563 * may_start_working() true.
1564 *
1565 * LOCKING:
1566 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1567 * multiple times. Does GFP_KERNEL allocations. Called only from
1568 * manager.
1569 *
1570 * RETURNS:
1571 * false if no action was taken and gcwq->lock stayed locked, true
1572 * otherwise.
1573 */
1574static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001575__releases(&gcwq->lock)
1576__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001577{
1578 if (!need_to_create_worker(gcwq))
1579 return false;
1580restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001581 spin_unlock_irq(&gcwq->lock);
1582
Tejun Heoe22bee72010-06-29 10:07:14 +02001583 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1584 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1585
1586 while (true) {
1587 struct worker *worker;
1588
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 worker = create_worker(gcwq, true);
1590 if (worker) {
1591 del_timer_sync(&gcwq->mayday_timer);
1592 spin_lock_irq(&gcwq->lock);
1593 start_worker(worker);
1594 BUG_ON(need_to_create_worker(gcwq));
1595 return true;
1596 }
1597
1598 if (!need_to_create_worker(gcwq))
1599 break;
1600
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 __set_current_state(TASK_INTERRUPTIBLE);
1602 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001603
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 if (!need_to_create_worker(gcwq))
1605 break;
1606 }
1607
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 del_timer_sync(&gcwq->mayday_timer);
1609 spin_lock_irq(&gcwq->lock);
1610 if (need_to_create_worker(gcwq))
1611 goto restart;
1612 return true;
1613}
1614
1615/**
1616 * maybe_destroy_worker - destroy workers which have been idle for a while
1617 * @gcwq: gcwq to destroy workers for
1618 *
1619 * Destroy @gcwq workers which have been idle for longer than
1620 * IDLE_WORKER_TIMEOUT.
1621 *
1622 * LOCKING:
1623 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1624 * multiple times. Called only from manager.
1625 *
1626 * RETURNS:
1627 * false if no action was taken and gcwq->lock stayed locked, true
1628 * otherwise.
1629 */
1630static bool maybe_destroy_workers(struct global_cwq *gcwq)
1631{
1632 bool ret = false;
1633
1634 while (too_many_workers(gcwq)) {
1635 struct worker *worker;
1636 unsigned long expires;
1637
1638 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1639 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1640
1641 if (time_before(jiffies, expires)) {
1642 mod_timer(&gcwq->idle_timer, expires);
1643 break;
1644 }
1645
1646 destroy_worker(worker);
1647 ret = true;
1648 }
1649
1650 return ret;
1651}
1652
1653/**
1654 * manage_workers - manage worker pool
1655 * @worker: self
1656 *
1657 * Assume the manager role and manage gcwq worker pool @worker belongs
1658 * to. At any given time, there can be only zero or one manager per
1659 * gcwq. The exclusion is handled automatically by this function.
1660 *
1661 * The caller can safely start processing works on false return. On
1662 * true return, it's guaranteed that need_to_create_worker() is false
1663 * and may_start_working() is true.
1664 *
1665 * CONTEXT:
1666 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1667 * multiple times. Does GFP_KERNEL allocations.
1668 *
1669 * RETURNS:
1670 * false if no action was taken and gcwq->lock stayed locked, true if
1671 * some action was taken.
1672 */
1673static bool manage_workers(struct worker *worker)
1674{
1675 struct global_cwq *gcwq = worker->gcwq;
1676 bool ret = false;
1677
1678 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1679 return ret;
1680
1681 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1682 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1683
1684 /*
1685 * Destroy and then create so that may_start_working() is true
1686 * on return.
1687 */
1688 ret |= maybe_destroy_workers(gcwq);
1689 ret |= maybe_create_worker(gcwq);
1690
1691 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1692
1693 /*
1694 * The trustee might be waiting to take over the manager
1695 * position, tell it we're done.
1696 */
1697 if (unlikely(gcwq->trustee))
1698 wake_up_all(&gcwq->trustee_wait);
1699
1700 return ret;
1701}
1702
Tejun Heoa62428c2010-06-29 10:07:10 +02001703/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001704 * move_linked_works - move linked works to a list
1705 * @work: start of series of works to be scheduled
1706 * @head: target list to append @work to
1707 * @nextp: out paramter for nested worklist walking
1708 *
1709 * Schedule linked works starting from @work to @head. Work series to
1710 * be scheduled starts at @work and includes any consecutive work with
1711 * WORK_STRUCT_LINKED set in its predecessor.
1712 *
1713 * If @nextp is not NULL, it's updated to point to the next work of
1714 * the last scheduled work. This allows move_linked_works() to be
1715 * nested inside outer list_for_each_entry_safe().
1716 *
1717 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001718 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001719 */
1720static void move_linked_works(struct work_struct *work, struct list_head *head,
1721 struct work_struct **nextp)
1722{
1723 struct work_struct *n;
1724
1725 /*
1726 * Linked worklist will always end before the end of the list,
1727 * use NULL for list head.
1728 */
1729 list_for_each_entry_safe_from(work, n, NULL, entry) {
1730 list_move_tail(&work->entry, head);
1731 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1732 break;
1733 }
1734
1735 /*
1736 * If we're already inside safe list traversal and have moved
1737 * multiple works to the scheduled queue, the next position
1738 * needs to be updated.
1739 */
1740 if (nextp)
1741 *nextp = n;
1742}
1743
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001744static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001745{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001746 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001747 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001748
Tejun Heocdadf002010-10-05 10:49:55 +02001749 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001750 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001751 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001752 cwq->nr_active++;
1753}
1754
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001755static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1756{
1757 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1758 struct work_struct, entry);
1759
1760 cwq_activate_delayed_work(work);
1761}
1762
Tejun Heoaffee4b2010-06-29 10:07:12 +02001763/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001764 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1765 * @cwq: cwq of interest
1766 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001767 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001768 *
1769 * A work either has completed or is removed from pending queue,
1770 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1771 *
1772 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001773 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001774 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001775static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1776 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001777{
1778 /* ignore uncolored works */
1779 if (color == WORK_NO_COLOR)
1780 return;
1781
1782 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001783
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001784 if (!delayed) {
1785 cwq->nr_active--;
1786 if (!list_empty(&cwq->delayed_works)) {
1787 /* one down, submit a delayed one */
1788 if (cwq->nr_active < cwq->max_active)
1789 cwq_activate_first_delayed(cwq);
1790 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001791 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001792
1793 /* is flush in progress and are we at the flushing tip? */
1794 if (likely(cwq->flush_color != color))
1795 return;
1796
1797 /* are there still in-flight works? */
1798 if (cwq->nr_in_flight[color])
1799 return;
1800
1801 /* this cwq is done, clear flush_color */
1802 cwq->flush_color = -1;
1803
1804 /*
1805 * If this was the last cwq, wake up the first flusher. It
1806 * will handle the rest.
1807 */
1808 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1809 complete(&cwq->wq->first_flusher->done);
1810}
1811
1812/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001813 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001814 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001815 * @work: work to process
1816 *
1817 * Process @work. This function contains all the logics necessary to
1818 * process a single work including synchronization against and
1819 * interaction with other workers on the same cpu, queueing and
1820 * flushing. As long as context requirement is met, any worker can
1821 * call this function to process a work.
1822 *
1823 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001824 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001825 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001826static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001827__releases(&gcwq->lock)
1828__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001829{
Tejun Heo7e116292010-06-29 10:07:13 +02001830 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001831 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001832 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001833 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001834 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001835 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001836#ifdef CONFIG_LOCKDEP
1837 /*
1838 * It is permissible to free the struct work_struct from
1839 * inside the function that is called from it, this we need to
1840 * take into account for lockdep too. To avoid bogus "held
1841 * lock freed" warnings as well as problems when looking into
1842 * work->lockdep_map, make a copy and use that here.
1843 */
1844 struct lockdep_map lockdep_map = work->lockdep_map;
1845#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001846 /*
1847 * A single work shouldn't be executed concurrently by
1848 * multiple workers on a single cpu. Check whether anyone is
1849 * already processing the work. If so, defer the work to the
1850 * currently executing one.
1851 */
1852 collision = __find_worker_executing_work(gcwq, bwh, work);
1853 if (unlikely(collision)) {
1854 move_linked_works(work, &collision->scheduled, NULL);
1855 return;
1856 }
1857
Tejun Heoa62428c2010-06-29 10:07:10 +02001858 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001859 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001860 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001861 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001862 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001863 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001864 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001865
Tejun Heo7a22ad72010-06-29 10:07:13 +02001866 /* record the current cpu number in the work data and dequeue */
1867 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001868 list_del_init(&work->entry);
1869
Tejun Heo649027d2010-06-29 10:07:14 +02001870 /*
1871 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1872 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1873 */
1874 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1875 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1876 struct work_struct, entry);
1877
1878 if (!list_empty(&gcwq->worklist) &&
1879 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1880 wake_up_worker(gcwq);
1881 else
1882 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1883 }
1884
Tejun Heofb0e7be2010-06-29 10:07:15 +02001885 /*
1886 * CPU intensive works don't participate in concurrency
1887 * management. They're the scheduler's responsibility.
1888 */
1889 if (unlikely(cpu_intensive))
1890 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1891
Tejun Heo8b03ae32010-06-29 10:07:12 +02001892 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001893
Tejun Heo66307ae2012-08-03 10:30:45 -07001894 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001895 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001896
Tejun Heoe1594892011-01-09 23:32:15 +01001897 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001898 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001899 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001900 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001901 /*
1902 * While we must be careful to not use "work" after this, the trace
1903 * point will only record its address.
1904 */
1905 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001906 lock_map_release(&lockdep_map);
1907 lock_map_release(&cwq->wq->lockdep_map);
1908
1909 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001910 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1911 " last function: %pf\n",
1912 current->comm, preempt_count(), task_pid_nr(current),
1913 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001914 debug_show_held_locks(current);
1915 dump_stack();
1916 }
1917
Tejun Heo8b03ae32010-06-29 10:07:12 +02001918 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001919
Tejun Heofb0e7be2010-06-29 10:07:15 +02001920 /* clear cpu intensive status */
1921 if (unlikely(cpu_intensive))
1922 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1923
Tejun Heoa62428c2010-06-29 10:07:10 +02001924 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001925 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001926 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001927 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001928 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001929 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001930}
1931
Tejun Heoaffee4b2010-06-29 10:07:12 +02001932/**
1933 * process_scheduled_works - process scheduled works
1934 * @worker: self
1935 *
1936 * Process all scheduled works. Please note that the scheduled list
1937 * may change while processing a work, so this function repeatedly
1938 * fetches a work from the top and executes it.
1939 *
1940 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001941 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001942 * multiple times.
1943 */
1944static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001946 while (!list_empty(&worker->scheduled)) {
1947 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001949 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Tejun Heo4690c4a2010-06-29 10:07:10 +02001953/**
1954 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001955 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001956 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 * The gcwq worker thread function. There's a single dynamic pool of
1958 * these per each cpu. These workers process all works regardless of
1959 * their specific target workqueue. The only exception is works which
1960 * belong to workqueues with a rescuer which will be explained in
1961 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001962 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001963static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Tejun Heoc34056a2010-06-29 10:07:11 +02001965 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001966 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 /* tell the scheduler that this is a workqueue worker */
1969 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001970woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001971 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Tejun Heoc8e55f32010-06-29 10:07:12 +02001973 /* DIE can be set only while we're idle, checking here is enough */
1974 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001975 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Tejun Heoc8e55f32010-06-29 10:07:12 +02001980 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001981recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 /* no more worker necessary? */
1983 if (!need_more_worker(gcwq))
1984 goto sleep;
1985
1986 /* do we need to manage? */
1987 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1988 goto recheck;
1989
Tejun Heoc8e55f32010-06-29 10:07:12 +02001990 /*
1991 * ->scheduled list can only be filled while a worker is
1992 * preparing to process a work or actually processing it.
1993 * Make sure nobody diddled with it while I was sleeping.
1994 */
1995 BUG_ON(!list_empty(&worker->scheduled));
1996
Tejun Heoe22bee72010-06-29 10:07:14 +02001997 /*
1998 * When control reaches this point, we're guaranteed to have
1999 * at least one idle worker or that someone else has already
2000 * assumed the manager role.
2001 */
2002 worker_clr_flags(worker, WORKER_PREP);
2003
2004 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002005 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02002006 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002007 struct work_struct, entry);
2008
2009 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2010 /* optimization path, not strictly necessary */
2011 process_one_work(worker, work);
2012 if (unlikely(!list_empty(&worker->scheduled)))
2013 process_scheduled_works(worker);
2014 } else {
2015 move_linked_works(work, &worker->scheduled, NULL);
2016 process_scheduled_works(worker);
2017 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002019
Tejun Heoe22bee72010-06-29 10:07:14 +02002020 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002021sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02002022 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
2023 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002024
Tejun Heoc8e55f32010-06-29 10:07:12 +02002025 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 * gcwq->lock is held and there's no work to process and no
2027 * need to manage, sleep. Workers are woken up only while
2028 * holding gcwq->lock or from local cpu, so setting the
2029 * current state before releasing gcwq->lock is enough to
2030 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002031 */
2032 worker_enter_idle(worker);
2033 __set_current_state(TASK_INTERRUPTIBLE);
2034 spin_unlock_irq(&gcwq->lock);
2035 schedule();
2036 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Tejun Heoe22bee72010-06-29 10:07:14 +02002039/**
2040 * rescuer_thread - the rescuer thread function
2041 * @__wq: the associated workqueue
2042 *
2043 * Workqueue rescuer thread function. There's one rescuer for each
2044 * workqueue which has WQ_RESCUER set.
2045 *
2046 * Regular work processing on a gcwq may block trying to create a new
2047 * worker which uses GFP_KERNEL allocation which has slight chance of
2048 * developing into deadlock if some works currently on the same queue
2049 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2050 * the problem rescuer solves.
2051 *
2052 * When such condition is possible, the gcwq summons rescuers of all
2053 * workqueues which have works queued on the gcwq and let them process
2054 * those works so that forward progress can be guaranteed.
2055 *
2056 * This should happen rarely.
2057 */
2058static int rescuer_thread(void *__wq)
2059{
2060 struct workqueue_struct *wq = __wq;
2061 struct worker *rescuer = wq->rescuer;
2062 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002063 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002064 unsigned int cpu;
2065
2066 set_user_nice(current, RESCUER_NICE_LEVEL);
2067repeat:
2068 set_current_state(TASK_INTERRUPTIBLE);
2069
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002070 if (kthread_should_stop()) {
2071 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002072 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002073 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002074
Tejun Heof3421792010-07-02 10:03:51 +02002075 /*
2076 * See whether any cpu is asking for help. Unbounded
2077 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2078 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002079 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002080 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2081 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002082 struct global_cwq *gcwq = cwq->gcwq;
2083 struct work_struct *work, *n;
2084
2085 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002086 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002087
2088 /* migrate to the target cpu if possible */
2089 rescuer->gcwq = gcwq;
2090 worker_maybe_bind_and_lock(rescuer);
2091
2092 /*
2093 * Slurp in all works issued via this workqueue and
2094 * process'em.
2095 */
2096 BUG_ON(!list_empty(&rescuer->scheduled));
2097 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2098 if (get_work_cwq(work) == cwq)
2099 move_linked_works(work, scheduled, &n);
2100
2101 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002102
2103 /*
2104 * Leave this gcwq. If keep_working() is %true, notify a
2105 * regular worker; otherwise, we end up with 0 concurrency
2106 * and stalling the execution.
2107 */
2108 if (keep_working(gcwq))
2109 wake_up_worker(gcwq);
2110
Tejun Heoe22bee72010-06-29 10:07:14 +02002111 spin_unlock_irq(&gcwq->lock);
2112 }
2113
2114 schedule();
2115 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002118struct wq_barrier {
2119 struct work_struct work;
2120 struct completion done;
2121};
2122
2123static void wq_barrier_func(struct work_struct *work)
2124{
2125 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2126 complete(&barr->done);
2127}
2128
Tejun Heo4690c4a2010-06-29 10:07:10 +02002129/**
2130 * insert_wq_barrier - insert a barrier work
2131 * @cwq: cwq to insert barrier into
2132 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002133 * @target: target work to attach @barr to
2134 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002135 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002136 * @barr is linked to @target such that @barr is completed only after
2137 * @target finishes execution. Please note that the ordering
2138 * guarantee is observed only with respect to @target and on the local
2139 * cpu.
2140 *
2141 * Currently, a queued barrier can't be canceled. This is because
2142 * try_to_grab_pending() can't determine whether the work to be
2143 * grabbed is at the head of the queue and thus can't clear LINKED
2144 * flag of the previous work while there must be a valid next work
2145 * after a work with LINKED flag set.
2146 *
2147 * Note that when @worker is non-NULL, @target may be modified
2148 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002149 *
2150 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002151 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002152 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002153static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002154 struct wq_barrier *barr,
2155 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002156{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002157 struct list_head *head;
2158 unsigned int linked = 0;
2159
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002160 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002161 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002162 * as we know for sure that this will not trigger any of the
2163 * checks and call back into the fixup functions where we
2164 * might deadlock.
2165 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002166 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002167 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002168 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002169
Tejun Heoaffee4b2010-06-29 10:07:12 +02002170 /*
2171 * If @target is currently being executed, schedule the
2172 * barrier to the worker; otherwise, put it after @target.
2173 */
2174 if (worker)
2175 head = worker->scheduled.next;
2176 else {
2177 unsigned long *bits = work_data_bits(target);
2178
2179 head = target->entry.next;
2180 /* there can already be other linked works, inherit and set */
2181 linked = *bits & WORK_STRUCT_LINKED;
2182 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2183 }
2184
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002185 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002186 insert_work(cwq, &barr->work, head,
2187 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002188}
2189
Tejun Heo73f53c42010-06-29 10:07:11 +02002190/**
2191 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2192 * @wq: workqueue being flushed
2193 * @flush_color: new flush color, < 0 for no-op
2194 * @work_color: new work color, < 0 for no-op
2195 *
2196 * Prepare cwqs for workqueue flushing.
2197 *
2198 * If @flush_color is non-negative, flush_color on all cwqs should be
2199 * -1. If no cwq has in-flight commands at the specified color, all
2200 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2201 * has in flight commands, its cwq->flush_color is set to
2202 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2203 * wakeup logic is armed and %true is returned.
2204 *
2205 * The caller should have initialized @wq->first_flusher prior to
2206 * calling this function with non-negative @flush_color. If
2207 * @flush_color is negative, no flush color update is done and %false
2208 * is returned.
2209 *
2210 * If @work_color is non-negative, all cwqs should have the same
2211 * work_color which is previous to @work_color and all will be
2212 * advanced to @work_color.
2213 *
2214 * CONTEXT:
2215 * mutex_lock(wq->flush_mutex).
2216 *
2217 * RETURNS:
2218 * %true if @flush_color >= 0 and there's something to flush. %false
2219 * otherwise.
2220 */
2221static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2222 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Tejun Heo73f53c42010-06-29 10:07:11 +02002224 bool wait = false;
2225 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002226
Tejun Heo73f53c42010-06-29 10:07:11 +02002227 if (flush_color >= 0) {
2228 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2229 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002230 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002231
Tejun Heof3421792010-07-02 10:03:51 +02002232 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002233 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002234 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002235
Tejun Heo8b03ae32010-06-29 10:07:12 +02002236 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002237
2238 if (flush_color >= 0) {
2239 BUG_ON(cwq->flush_color != -1);
2240
2241 if (cwq->nr_in_flight[flush_color]) {
2242 cwq->flush_color = flush_color;
2243 atomic_inc(&wq->nr_cwqs_to_flush);
2244 wait = true;
2245 }
2246 }
2247
2248 if (work_color >= 0) {
2249 BUG_ON(work_color != work_next_color(cwq->work_color));
2250 cwq->work_color = work_color;
2251 }
2252
Tejun Heo8b03ae32010-06-29 10:07:12 +02002253 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002254 }
2255
2256 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2257 complete(&wq->first_flusher->done);
2258
2259 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002262/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002264 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
2266 * Forces execution of the workqueue and blocks until its completion.
2267 * This is typically used in driver shutdown handlers.
2268 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002269 * We sleep until all works which were queued on entry have been handled,
2270 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002272void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Tejun Heo73f53c42010-06-29 10:07:11 +02002274 struct wq_flusher this_flusher = {
2275 .list = LIST_HEAD_INIT(this_flusher.list),
2276 .flush_color = -1,
2277 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2278 };
2279 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002280
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002281 lock_map_acquire(&wq->lockdep_map);
2282 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002283
2284 mutex_lock(&wq->flush_mutex);
2285
2286 /*
2287 * Start-to-wait phase
2288 */
2289 next_color = work_next_color(wq->work_color);
2290
2291 if (next_color != wq->flush_color) {
2292 /*
2293 * Color space is not full. The current work_color
2294 * becomes our flush_color and work_color is advanced
2295 * by one.
2296 */
2297 BUG_ON(!list_empty(&wq->flusher_overflow));
2298 this_flusher.flush_color = wq->work_color;
2299 wq->work_color = next_color;
2300
2301 if (!wq->first_flusher) {
2302 /* no flush in progress, become the first flusher */
2303 BUG_ON(wq->flush_color != this_flusher.flush_color);
2304
2305 wq->first_flusher = &this_flusher;
2306
2307 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2308 wq->work_color)) {
2309 /* nothing to flush, done */
2310 wq->flush_color = next_color;
2311 wq->first_flusher = NULL;
2312 goto out_unlock;
2313 }
2314 } else {
2315 /* wait in queue */
2316 BUG_ON(wq->flush_color == this_flusher.flush_color);
2317 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2318 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2319 }
2320 } else {
2321 /*
2322 * Oops, color space is full, wait on overflow queue.
2323 * The next flush completion will assign us
2324 * flush_color and transfer to flusher_queue.
2325 */
2326 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2327 }
2328
2329 mutex_unlock(&wq->flush_mutex);
2330
2331 wait_for_completion(&this_flusher.done);
2332
2333 /*
2334 * Wake-up-and-cascade phase
2335 *
2336 * First flushers are responsible for cascading flushes and
2337 * handling overflow. Non-first flushers can simply return.
2338 */
2339 if (wq->first_flusher != &this_flusher)
2340 return;
2341
2342 mutex_lock(&wq->flush_mutex);
2343
Tejun Heo4ce48b32010-07-02 10:03:51 +02002344 /* we might have raced, check again with mutex held */
2345 if (wq->first_flusher != &this_flusher)
2346 goto out_unlock;
2347
Tejun Heo73f53c42010-06-29 10:07:11 +02002348 wq->first_flusher = NULL;
2349
2350 BUG_ON(!list_empty(&this_flusher.list));
2351 BUG_ON(wq->flush_color != this_flusher.flush_color);
2352
2353 while (true) {
2354 struct wq_flusher *next, *tmp;
2355
2356 /* complete all the flushers sharing the current flush color */
2357 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2358 if (next->flush_color != wq->flush_color)
2359 break;
2360 list_del_init(&next->list);
2361 complete(&next->done);
2362 }
2363
2364 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2365 wq->flush_color != work_next_color(wq->work_color));
2366
2367 /* this flush_color is finished, advance by one */
2368 wq->flush_color = work_next_color(wq->flush_color);
2369
2370 /* one color has been freed, handle overflow queue */
2371 if (!list_empty(&wq->flusher_overflow)) {
2372 /*
2373 * Assign the same color to all overflowed
2374 * flushers, advance work_color and append to
2375 * flusher_queue. This is the start-to-wait
2376 * phase for these overflowed flushers.
2377 */
2378 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2379 tmp->flush_color = wq->work_color;
2380
2381 wq->work_color = work_next_color(wq->work_color);
2382
2383 list_splice_tail_init(&wq->flusher_overflow,
2384 &wq->flusher_queue);
2385 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2386 }
2387
2388 if (list_empty(&wq->flusher_queue)) {
2389 BUG_ON(wq->flush_color != wq->work_color);
2390 break;
2391 }
2392
2393 /*
2394 * Need to flush more colors. Make the next flusher
2395 * the new first flusher and arm cwqs.
2396 */
2397 BUG_ON(wq->flush_color == wq->work_color);
2398 BUG_ON(wq->flush_color != next->flush_color);
2399
2400 list_del_init(&next->list);
2401 wq->first_flusher = next;
2402
2403 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2404 break;
2405
2406 /*
2407 * Meh... this color is already done, clear first
2408 * flusher and repeat cascading.
2409 */
2410 wq->first_flusher = NULL;
2411 }
2412
2413out_unlock:
2414 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
Dave Jonesae90dd52006-06-30 01:40:45 -04002416EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002418/**
2419 * drain_workqueue - drain a workqueue
2420 * @wq: workqueue to drain
2421 *
2422 * Wait until the workqueue becomes empty. While draining is in progress,
2423 * only chain queueing is allowed. IOW, only currently pending or running
2424 * work items on @wq can queue further work items on it. @wq is flushed
2425 * repeatedly until it becomes empty. The number of flushing is detemined
2426 * by the depth of chaining and should be relatively short. Whine if it
2427 * takes too long.
2428 */
2429void drain_workqueue(struct workqueue_struct *wq)
2430{
2431 unsigned int flush_cnt = 0;
2432 unsigned int cpu;
2433
2434 /*
2435 * __queue_work() needs to test whether there are drainers, is much
2436 * hotter than drain_workqueue() and already looks at @wq->flags.
2437 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2438 */
2439 spin_lock(&workqueue_lock);
2440 if (!wq->nr_drainers++)
2441 wq->flags |= WQ_DRAINING;
2442 spin_unlock(&workqueue_lock);
2443reflush:
2444 flush_workqueue(wq);
2445
2446 for_each_cwq_cpu(cpu, wq) {
2447 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002448 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002449
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002450 spin_lock_irq(&cwq->gcwq->lock);
2451 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
2452 spin_unlock_irq(&cwq->gcwq->lock);
2453
2454 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002455 continue;
2456
2457 if (++flush_cnt == 10 ||
2458 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2459 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2460 wq->name, flush_cnt);
2461 goto reflush;
2462 }
2463
2464 spin_lock(&workqueue_lock);
2465 if (!--wq->nr_drainers)
2466 wq->flags &= ~WQ_DRAINING;
2467 spin_unlock(&workqueue_lock);
2468}
2469EXPORT_SYMBOL_GPL(drain_workqueue);
2470
Tejun Heobaf59022010-09-16 10:42:16 +02002471static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2472 bool wait_executing)
2473{
2474 struct worker *worker = NULL;
2475 struct global_cwq *gcwq;
2476 struct cpu_workqueue_struct *cwq;
2477
2478 might_sleep();
2479 gcwq = get_work_gcwq(work);
2480 if (!gcwq)
2481 return false;
2482
2483 spin_lock_irq(&gcwq->lock);
2484 if (!list_empty(&work->entry)) {
2485 /*
2486 * See the comment near try_to_grab_pending()->smp_rmb().
2487 * If it was re-queued to a different gcwq under us, we
2488 * are not going to wait.
2489 */
2490 smp_rmb();
2491 cwq = get_work_cwq(work);
2492 if (unlikely(!cwq || gcwq != cwq->gcwq))
2493 goto already_gone;
2494 } else if (wait_executing) {
2495 worker = find_worker_executing_work(gcwq, work);
2496 if (!worker)
2497 goto already_gone;
2498 cwq = worker->current_cwq;
2499 } else
2500 goto already_gone;
2501
2502 insert_wq_barrier(cwq, barr, work, worker);
2503 spin_unlock_irq(&gcwq->lock);
2504
Tejun Heoe1594892011-01-09 23:32:15 +01002505 /*
2506 * If @max_active is 1 or rescuer is in use, flushing another work
2507 * item on the same workqueue may lead to deadlock. Make sure the
2508 * flusher is not running on the same workqueue by verifying write
2509 * access.
2510 */
2511 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2512 lock_map_acquire(&cwq->wq->lockdep_map);
2513 else
2514 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002515 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002516
Tejun Heobaf59022010-09-16 10:42:16 +02002517 return true;
2518already_gone:
2519 spin_unlock_irq(&gcwq->lock);
2520 return false;
2521}
2522
Oleg Nesterovdb700892008-07-25 01:47:49 -07002523/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002524 * flush_work - wait for a work to finish executing the last queueing instance
2525 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002526 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002527 * Wait until @work has finished execution. This function considers
2528 * only the last queueing instance of @work. If @work has been
2529 * enqueued across different CPUs on a non-reentrant workqueue or on
2530 * multiple workqueues, @work might still be executing on return on
2531 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002532 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002533 * If @work was queued only on a non-reentrant, ordered or unbound
2534 * workqueue, @work is guaranteed to be idle on return if it hasn't
2535 * been requeued since flush started.
2536 *
2537 * RETURNS:
2538 * %true if flush_work() waited for the work to finish execution,
2539 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002540 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002541bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002542{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002543 struct wq_barrier barr;
2544
Tejun Heobaf59022010-09-16 10:42:16 +02002545 if (start_flush_work(work, &barr, true)) {
2546 wait_for_completion(&barr.done);
2547 destroy_work_on_stack(&barr.work);
2548 return true;
2549 } else
2550 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002551}
2552EXPORT_SYMBOL_GPL(flush_work);
2553
Tejun Heo401a8d02010-09-16 10:36:00 +02002554static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2555{
2556 struct wq_barrier barr;
2557 struct worker *worker;
2558
2559 spin_lock_irq(&gcwq->lock);
2560
2561 worker = find_worker_executing_work(gcwq, work);
2562 if (unlikely(worker))
2563 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2564
2565 spin_unlock_irq(&gcwq->lock);
2566
2567 if (unlikely(worker)) {
2568 wait_for_completion(&barr.done);
2569 destroy_work_on_stack(&barr.work);
2570 return true;
2571 } else
2572 return false;
2573}
2574
2575static bool wait_on_work(struct work_struct *work)
2576{
2577 bool ret = false;
2578 int cpu;
2579
2580 might_sleep();
2581
2582 lock_map_acquire(&work->lockdep_map);
2583 lock_map_release(&work->lockdep_map);
2584
2585 for_each_gcwq_cpu(cpu)
2586 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2587 return ret;
2588}
2589
Tejun Heo09383492010-09-16 10:48:29 +02002590/**
2591 * flush_work_sync - wait until a work has finished execution
2592 * @work: the work to flush
2593 *
2594 * Wait until @work has finished execution. On return, it's
2595 * guaranteed that all queueing instances of @work which happened
2596 * before this function is called are finished. In other words, if
2597 * @work hasn't been requeued since this function was called, @work is
2598 * guaranteed to be idle on return.
2599 *
2600 * RETURNS:
2601 * %true if flush_work_sync() waited for the work to finish execution,
2602 * %false if it was already idle.
2603 */
2604bool flush_work_sync(struct work_struct *work)
2605{
2606 struct wq_barrier barr;
2607 bool pending, waited;
2608
2609 /* we'll wait for executions separately, queue barr only if pending */
2610 pending = start_flush_work(work, &barr, false);
2611
2612 /* wait for executions to finish */
2613 waited = wait_on_work(work);
2614
2615 /* wait for the pending one */
2616 if (pending) {
2617 wait_for_completion(&barr.done);
2618 destroy_work_on_stack(&barr.work);
2619 }
2620
2621 return pending || waited;
2622}
2623EXPORT_SYMBOL_GPL(flush_work_sync);
2624
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002625/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002626 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002627 * so this work can't be re-armed in any way.
2628 */
2629static int try_to_grab_pending(struct work_struct *work)
2630{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002631 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002632 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002633
Tejun Heo22df02b2010-06-29 10:07:10 +02002634 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002635 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002636
2637 /*
2638 * The queueing is in progress, or it is already queued. Try to
2639 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2640 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002641 gcwq = get_work_gcwq(work);
2642 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002643 return ret;
2644
Tejun Heo8b03ae32010-06-29 10:07:12 +02002645 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002646 if (!list_empty(&work->entry)) {
2647 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002648 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002649 * In that case we must see the new value after rmb(), see
2650 * insert_work()->wmb().
2651 */
2652 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002653 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002654 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002655
2656 /*
2657 * A delayed work item cannot be grabbed directly
2658 * because it might have linked NO_COLOR work items
2659 * which, if left on the delayed_list, will confuse
2660 * cwq->nr_active management later on and cause
2661 * stall. Make sure the work item is activated
2662 * before grabbing.
2663 */
2664 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2665 cwq_activate_delayed_work(work);
2666
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002668 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002669 get_work_color(work),
2670 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002671 ret = 1;
2672 }
2673 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002674 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675
2676 return ret;
2677}
2678
Tejun Heo401a8d02010-09-16 10:36:00 +02002679static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002680 struct timer_list* timer)
2681{
2682 int ret;
2683
2684 do {
2685 ret = (timer && likely(del_timer(timer)));
2686 if (!ret)
2687 ret = try_to_grab_pending(work);
2688 wait_on_work(work);
2689 } while (unlikely(ret < 0));
2690
Tejun Heo7a22ad72010-06-29 10:07:13 +02002691 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002692 return ret;
2693}
2694
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002695/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002696 * cancel_work_sync - cancel a work and wait for it to finish
2697 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002698 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002699 * Cancel @work and wait for its execution to finish. This function
2700 * can be used even if the work re-queues itself or migrates to
2701 * another workqueue. On return from this function, @work is
2702 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002703 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002704 * cancel_work_sync(&delayed_work->work) must not be used for
2705 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002706 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002707 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002708 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002709 *
2710 * RETURNS:
2711 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002712 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002713bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002714{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002715 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002716}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002717EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002718
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002720 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2721 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002722 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002723 * Delayed timer is cancelled and the pending work is queued for
2724 * immediate execution. Like flush_work(), this function only
2725 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002726 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 * RETURNS:
2728 * %true if flush_work() waited for the work to finish execution,
2729 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002731bool flush_delayed_work(struct delayed_work *dwork)
2732{
2733 if (del_timer_sync(&dwork->timer))
2734 __queue_work(raw_smp_processor_id(),
2735 get_work_cwq(&dwork->work)->wq, &dwork->work);
2736 return flush_work(&dwork->work);
2737}
2738EXPORT_SYMBOL(flush_delayed_work);
2739
2740/**
Tejun Heo09383492010-09-16 10:48:29 +02002741 * flush_delayed_work_sync - wait for a dwork to finish
2742 * @dwork: the delayed work to flush
2743 *
2744 * Delayed timer is cancelled and the pending work is queued for
2745 * execution immediately. Other than timer handling, its behavior
2746 * is identical to flush_work_sync().
2747 *
2748 * RETURNS:
2749 * %true if flush_work_sync() waited for the work to finish execution,
2750 * %false if it was already idle.
2751 */
2752bool flush_delayed_work_sync(struct delayed_work *dwork)
2753{
2754 if (del_timer_sync(&dwork->timer))
2755 __queue_work(raw_smp_processor_id(),
2756 get_work_cwq(&dwork->work)->wq, &dwork->work);
2757 return flush_work_sync(&dwork->work);
2758}
2759EXPORT_SYMBOL(flush_delayed_work_sync);
2760
2761/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002762 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2763 * @dwork: the delayed work cancel
2764 *
2765 * This is cancel_work_sync() for delayed works.
2766 *
2767 * RETURNS:
2768 * %true if @dwork was pending, %false otherwise.
2769 */
2770bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002771{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002772 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002773}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002774EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002776/**
2777 * schedule_work - put work task in global workqueue
2778 * @work: job to be done
2779 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002780 * Returns zero if @work was already on the kernel-global workqueue and
2781 * non-zero otherwise.
2782 *
2783 * This puts a job in the kernel-global workqueue if it was not already
2784 * queued and leaves it in the same position on the kernel-global
2785 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002786 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002787int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Tejun Heod320c032010-06-29 10:07:14 +02002789 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
Dave Jonesae90dd52006-06-30 01:40:45 -04002791EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Zhang Ruic1a220e2008-07-23 21:28:39 -07002793/*
2794 * schedule_work_on - put work task on a specific cpu
2795 * @cpu: cpu to put the work task on
2796 * @work: job to be done
2797 *
2798 * This puts a job on a specific cpu
2799 */
2800int schedule_work_on(int cpu, struct work_struct *work)
2801{
Tejun Heod320c032010-06-29 10:07:14 +02002802 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002803}
2804EXPORT_SYMBOL(schedule_work_on);
2805
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002806/**
2807 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002808 * @dwork: job to be done
2809 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002810 *
2811 * After waiting for a given time this puts a job in the kernel-global
2812 * workqueue.
2813 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002814int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002815 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Tejun Heod320c032010-06-29 10:07:14 +02002817 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
Dave Jonesae90dd52006-06-30 01:40:45 -04002819EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002821/**
2822 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2823 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002824 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002825 * @delay: number of jiffies to wait
2826 *
2827 * After waiting for a given time this puts a job in the kernel-global
2828 * workqueue on the specified CPU.
2829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002831 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Tejun Heod320c032010-06-29 10:07:14 +02002833 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
Dave Jonesae90dd52006-06-30 01:40:45 -04002835EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Andrew Mortonb6136772006-06-25 05:47:49 -07002837/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002838 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002839 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002840 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002841 * schedule_on_each_cpu() executes @func on each online CPU using the
2842 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002843 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002844 *
2845 * RETURNS:
2846 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 */
David Howells65f27f32006-11-22 14:55:48 +00002848int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002849{
2850 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002851 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002852
Andrew Mortonb6136772006-06-25 05:47:49 -07002853 works = alloc_percpu(struct work_struct);
2854 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002855 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002856
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002857 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002858
Christoph Lameter15316ba2006-01-08 01:00:43 -08002859 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002860 struct work_struct *work = per_cpu_ptr(works, cpu);
2861
2862 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002863 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002864 }
Tejun Heo93981802009-11-17 14:06:20 -08002865
2866 for_each_online_cpu(cpu)
2867 flush_work(per_cpu_ptr(works, cpu));
2868
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002869 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002870 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002871 return 0;
2872}
2873
Alan Sterneef6a7d2010-02-12 17:39:21 +09002874/**
2875 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2876 *
2877 * Forces execution of the kernel-global workqueue and blocks until its
2878 * completion.
2879 *
2880 * Think twice before calling this function! It's very easy to get into
2881 * trouble if you don't take great care. Either of the following situations
2882 * will lead to deadlock:
2883 *
2884 * One of the work items currently on the workqueue needs to acquire
2885 * a lock held by your code or its caller.
2886 *
2887 * Your code is running in the context of a work routine.
2888 *
2889 * They will be detected by lockdep when they occur, but the first might not
2890 * occur very often. It depends on what work items are on the workqueue and
2891 * what locks they need, which you have no control over.
2892 *
2893 * In most situations flushing the entire workqueue is overkill; you merely
2894 * need to know that a particular work item isn't queued and isn't running.
2895 * In such cases you should use cancel_delayed_work_sync() or
2896 * cancel_work_sync() instead.
2897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898void flush_scheduled_work(void)
2899{
Tejun Heod320c032010-06-29 10:07:14 +02002900 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
Dave Jonesae90dd52006-06-30 01:40:45 -04002902EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002905 * execute_in_process_context - reliably execute the routine with user context
2906 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002907 * @ew: guaranteed storage for the execute work structure (must
2908 * be available when the work executes)
2909 *
2910 * Executes the function immediately if process context is available,
2911 * otherwise schedules the function for delayed execution.
2912 *
2913 * Returns: 0 - function was executed
2914 * 1 - function was scheduled for execution
2915 */
David Howells65f27f32006-11-22 14:55:48 +00002916int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002917{
2918 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002919 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002920 return 0;
2921 }
2922
David Howells65f27f32006-11-22 14:55:48 +00002923 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002924 schedule_work(&ew->work);
2925
2926 return 1;
2927}
2928EXPORT_SYMBOL_GPL(execute_in_process_context);
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930int keventd_up(void)
2931{
Tejun Heod320c032010-06-29 10:07:14 +02002932 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002935static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002937 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002938 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2939 * Make sure that the alignment isn't lower than that of
2940 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002941 */
Tejun Heo0f900042010-06-29 10:07:11 +02002942 const size_t size = sizeof(struct cpu_workqueue_struct);
2943 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2944 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002945
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002946 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002947 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002948 else {
Tejun Heof3421792010-07-02 10:03:51 +02002949 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002950
Tejun Heof3421792010-07-02 10:03:51 +02002951 /*
2952 * Allocate enough room to align cwq and put an extra
2953 * pointer at the end pointing back to the originally
2954 * allocated pointer which will be used for free.
2955 */
2956 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2957 if (ptr) {
2958 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2959 *(void **)(wq->cpu_wq.single + 1) = ptr;
2960 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002961 }
Tejun Heof3421792010-07-02 10:03:51 +02002962
Tejun Heo0415b002011-03-24 18:50:09 +01002963 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002964 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2965 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002966}
2967
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002968static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002969{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002970 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002971 free_percpu(wq->cpu_wq.pcpu);
2972 else if (wq->cpu_wq.single) {
2973 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002974 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002975 }
2976}
2977
Tejun Heof3421792010-07-02 10:03:51 +02002978static int wq_clamp_max_active(int max_active, unsigned int flags,
2979 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002980{
Tejun Heof3421792010-07-02 10:03:51 +02002981 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2982
2983 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002984 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2985 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002986 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002987
Tejun Heof3421792010-07-02 10:03:51 +02002988 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002989}
2990
Tejun Heob196be82012-01-10 15:11:35 -08002991struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002992 unsigned int flags,
2993 int max_active,
2994 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002995 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002996{
Tejun Heob196be82012-01-10 15:11:35 -08002997 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002998 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002999 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003000 size_t namelen;
3001
3002 /* determine namelen, allocate wq and format name */
3003 va_start(args, lock_name);
3004 va_copy(args1, args);
3005 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3006
3007 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3008 if (!wq)
3009 goto err;
3010
3011 vsnprintf(wq->name, namelen, fmt, args1);
3012 va_end(args);
3013 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003014
Tejun Heof3421792010-07-02 10:03:51 +02003015 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003016 * Workqueues which may be used during memory reclaim should
3017 * have a rescuer to guarantee forward progress.
3018 */
3019 if (flags & WQ_MEM_RECLAIM)
3020 flags |= WQ_RESCUER;
3021
3022 /*
Tejun Heof3421792010-07-02 10:03:51 +02003023 * Unbound workqueues aren't concurrency managed and should be
3024 * dispatched to workers immediately.
3025 */
3026 if (flags & WQ_UNBOUND)
3027 flags |= WQ_HIGHPRI;
3028
Tejun Heod320c032010-06-29 10:07:14 +02003029 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003030 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003031
Tejun Heob196be82012-01-10 15:11:35 -08003032 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003033 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003034 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003035 mutex_init(&wq->flush_mutex);
3036 atomic_set(&wq->nr_cwqs_to_flush, 0);
3037 INIT_LIST_HEAD(&wq->flusher_queue);
3038 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003039
Johannes Bergeb13ba82008-01-16 09:51:58 +01003040 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003041 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003042
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003043 if (alloc_cwqs(wq) < 0)
3044 goto err;
3045
Tejun Heof3421792010-07-02 10:03:51 +02003046 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003047 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003048 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003049
Tejun Heo0f900042010-06-29 10:07:11 +02003050 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003051 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003052 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003053 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003054 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003055 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003056 }
3057
Tejun Heoe22bee72010-06-29 10:07:14 +02003058 if (flags & WQ_RESCUER) {
3059 struct worker *rescuer;
3060
Tejun Heof2e005a2010-07-20 15:59:09 +02003061 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003062 goto err;
3063
3064 wq->rescuer = rescuer = alloc_worker();
3065 if (!rescuer)
3066 goto err;
3067
Tejun Heob196be82012-01-10 15:11:35 -08003068 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3069 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003070 if (IS_ERR(rescuer->task))
3071 goto err;
3072
Tejun Heoe22bee72010-06-29 10:07:14 +02003073 rescuer->task->flags |= PF_THREAD_BOUND;
3074 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003075 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003076
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003077 /*
3078 * workqueue_lock protects global freeze state and workqueues
3079 * list. Grab it, set max_active accordingly and add the new
3080 * workqueue to workqueues list.
3081 */
Tejun Heo15376632010-06-29 10:07:11 +02003082 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003083
Tejun Heo58a69cb2011-02-16 09:25:31 +01003084 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003085 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003086 get_cwq(cpu, wq)->max_active = 0;
3087
Tejun Heo15376632010-06-29 10:07:11 +02003088 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003089
Tejun Heo15376632010-06-29 10:07:11 +02003090 spin_unlock(&workqueue_lock);
3091
Oleg Nesterov3af244332007-05-09 02:34:09 -07003092 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003093err:
3094 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003095 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003096 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003097 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003098 kfree(wq);
3099 }
3100 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003101}
Tejun Heod320c032010-06-29 10:07:14 +02003102EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003103
3104/**
3105 * destroy_workqueue - safely terminate a workqueue
3106 * @wq: target workqueue
3107 *
3108 * Safely destroy a workqueue. All work currently pending will be done first.
3109 */
3110void destroy_workqueue(struct workqueue_struct *wq)
3111{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003112 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003113
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003114 /* drain it before proceeding with destruction */
3115 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003116
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003117 /*
3118 * wq list is used to freeze wq, remove from list after
3119 * flushing is complete in case freeze races us.
3120 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003121 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003122 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003123 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003124
Tejun Heoe22bee72010-06-29 10:07:14 +02003125 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003126 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003127 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3128 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003129
Tejun Heo73f53c42010-06-29 10:07:11 +02003130 for (i = 0; i < WORK_NR_COLORS; i++)
3131 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003132 BUG_ON(cwq->nr_active);
3133 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003134 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003135
Tejun Heoe22bee72010-06-29 10:07:14 +02003136 if (wq->flags & WQ_RESCUER) {
3137 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003138 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003139 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003140 }
3141
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003142 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003143 kfree(wq);
3144}
3145EXPORT_SYMBOL_GPL(destroy_workqueue);
3146
Tejun Heodcd989c2010-06-29 10:07:14 +02003147/**
3148 * workqueue_set_max_active - adjust max_active of a workqueue
3149 * @wq: target workqueue
3150 * @max_active: new max_active value.
3151 *
3152 * Set max_active of @wq to @max_active.
3153 *
3154 * CONTEXT:
3155 * Don't call from IRQ context.
3156 */
3157void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3158{
3159 unsigned int cpu;
3160
Tejun Heof3421792010-07-02 10:03:51 +02003161 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003162
3163 spin_lock(&workqueue_lock);
3164
3165 wq->saved_max_active = max_active;
3166
Tejun Heof3421792010-07-02 10:03:51 +02003167 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003168 struct global_cwq *gcwq = get_gcwq(cpu);
3169
3170 spin_lock_irq(&gcwq->lock);
3171
Tejun Heo58a69cb2011-02-16 09:25:31 +01003172 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003173 !(gcwq->flags & GCWQ_FREEZING))
3174 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3175
3176 spin_unlock_irq(&gcwq->lock);
3177 }
3178
3179 spin_unlock(&workqueue_lock);
3180}
3181EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3182
3183/**
3184 * workqueue_congested - test whether a workqueue is congested
3185 * @cpu: CPU in question
3186 * @wq: target workqueue
3187 *
3188 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3189 * no synchronization around this function and the test result is
3190 * unreliable and only useful as advisory hints or for debugging.
3191 *
3192 * RETURNS:
3193 * %true if congested, %false otherwise.
3194 */
3195bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3196{
3197 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3198
3199 return !list_empty(&cwq->delayed_works);
3200}
3201EXPORT_SYMBOL_GPL(workqueue_congested);
3202
3203/**
3204 * work_cpu - return the last known associated cpu for @work
3205 * @work: the work of interest
3206 *
3207 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003208 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003209 */
3210unsigned int work_cpu(struct work_struct *work)
3211{
3212 struct global_cwq *gcwq = get_work_gcwq(work);
3213
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003214 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003215}
3216EXPORT_SYMBOL_GPL(work_cpu);
3217
3218/**
3219 * work_busy - test whether a work is currently pending or running
3220 * @work: the work to be tested
3221 *
3222 * Test whether @work is currently pending or running. There is no
3223 * synchronization around this function and the test result is
3224 * unreliable and only useful as advisory hints or for debugging.
3225 * Especially for reentrant wqs, the pending state might hide the
3226 * running state.
3227 *
3228 * RETURNS:
3229 * OR'd bitmask of WORK_BUSY_* bits.
3230 */
3231unsigned int work_busy(struct work_struct *work)
3232{
3233 struct global_cwq *gcwq = get_work_gcwq(work);
3234 unsigned long flags;
3235 unsigned int ret = 0;
3236
3237 if (!gcwq)
3238 return false;
3239
3240 spin_lock_irqsave(&gcwq->lock, flags);
3241
3242 if (work_pending(work))
3243 ret |= WORK_BUSY_PENDING;
3244 if (find_worker_executing_work(gcwq, work))
3245 ret |= WORK_BUSY_RUNNING;
3246
3247 spin_unlock_irqrestore(&gcwq->lock, flags);
3248
3249 return ret;
3250}
3251EXPORT_SYMBOL_GPL(work_busy);
3252
Tejun Heodb7bccf2010-06-29 10:07:12 +02003253/*
3254 * CPU hotplug.
3255 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003256 * There are two challenges in supporting CPU hotplug. Firstly, there
3257 * are a lot of assumptions on strong associations among work, cwq and
3258 * gcwq which make migrating pending and scheduled works very
3259 * difficult to implement without impacting hot paths. Secondly,
3260 * gcwqs serve mix of short, long and very long running works making
3261 * blocked draining impractical.
3262 *
3263 * This is solved by allowing a gcwq to be detached from CPU, running
3264 * it with unbound (rogue) workers and allowing it to be reattached
3265 * later if the cpu comes back online. A separate thread is created
3266 * to govern a gcwq in such state and is called the trustee of the
3267 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003268 *
3269 * Trustee states and their descriptions.
3270 *
3271 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3272 * new trustee is started with this state.
3273 *
3274 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003275 * assuming the manager role and making all existing
3276 * workers rogue. DOWN_PREPARE waits for trustee to
3277 * enter this state. After reaching IN_CHARGE, trustee
3278 * tries to execute the pending worklist until it's empty
3279 * and the state is set to BUTCHER, or the state is set
3280 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003281 *
3282 * BUTCHER Command state which is set by the cpu callback after
3283 * the cpu has went down. Once this state is set trustee
3284 * knows that there will be no new works on the worklist
3285 * and once the worklist is empty it can proceed to
3286 * killing idle workers.
3287 *
3288 * RELEASE Command state which is set by the cpu callback if the
3289 * cpu down has been canceled or it has come online
3290 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003291 * trying to drain or butcher and clears ROGUE, rebinds
3292 * all remaining workers back to the cpu and releases
3293 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003294 *
3295 * DONE Trustee will enter this state after BUTCHER or RELEASE
3296 * is complete.
3297 *
3298 * trustee CPU draining
3299 * took over down complete
3300 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3301 * | | ^
3302 * | CPU is back online v return workers |
3303 * ----------------> RELEASE --------------
3304 */
3305
3306/**
3307 * trustee_wait_event_timeout - timed event wait for trustee
3308 * @cond: condition to wait for
3309 * @timeout: timeout in jiffies
3310 *
3311 * wait_event_timeout() for trustee to use. Handles locking and
3312 * checks for RELEASE request.
3313 *
3314 * CONTEXT:
3315 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3316 * multiple times. To be used by trustee.
3317 *
3318 * RETURNS:
3319 * Positive indicating left time if @cond is satisfied, 0 if timed
3320 * out, -1 if canceled.
3321 */
3322#define trustee_wait_event_timeout(cond, timeout) ({ \
3323 long __ret = (timeout); \
3324 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3325 __ret) { \
3326 spin_unlock_irq(&gcwq->lock); \
3327 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3328 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3329 __ret); \
3330 spin_lock_irq(&gcwq->lock); \
3331 } \
3332 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3333})
3334
3335/**
3336 * trustee_wait_event - event wait for trustee
3337 * @cond: condition to wait for
3338 *
3339 * wait_event() for trustee to use. Automatically handles locking and
3340 * checks for CANCEL request.
3341 *
3342 * CONTEXT:
3343 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3344 * multiple times. To be used by trustee.
3345 *
3346 * RETURNS:
3347 * 0 if @cond is satisfied, -1 if canceled.
3348 */
3349#define trustee_wait_event(cond) ({ \
3350 long __ret1; \
3351 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3352 __ret1 < 0 ? -1 : 0; \
3353})
3354
3355static int __cpuinit trustee_thread(void *__gcwq)
3356{
3357 struct global_cwq *gcwq = __gcwq;
3358 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003359 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003360 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003361 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003362 int i;
3363
3364 BUG_ON(gcwq->cpu != smp_processor_id());
3365
3366 spin_lock_irq(&gcwq->lock);
3367 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003368 * Claim the manager position and make all workers rogue.
3369 * Trustee must be bound to the target cpu and can't be
3370 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003371 */
3372 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003373 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3374 BUG_ON(rc < 0);
3375
3376 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003377
3378 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003379 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003380
3381 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003382 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003383
3384 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003385 * Call schedule() so that we cross rq->lock and thus can
3386 * guarantee sched callbacks see the rogue flag. This is
3387 * necessary as scheduler callbacks may be invoked from other
3388 * cpus.
3389 */
3390 spin_unlock_irq(&gcwq->lock);
3391 schedule();
3392 spin_lock_irq(&gcwq->lock);
3393
3394 /*
Tejun Heocb444762010-07-02 10:03:50 +02003395 * Sched callbacks are disabled now. Zap nr_running. After
3396 * this, nr_running stays zero and need_more_worker() and
3397 * keep_working() are always true as long as the worklist is
3398 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003399 */
Tejun Heocb444762010-07-02 10:03:50 +02003400 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003401
3402 spin_unlock_irq(&gcwq->lock);
3403 del_timer_sync(&gcwq->idle_timer);
3404 spin_lock_irq(&gcwq->lock);
3405
3406 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003407 * We're now in charge. Notify and proceed to drain. We need
3408 * to keep the gcwq running during the whole CPU down
3409 * procedure as other cpu hotunplug callbacks may need to
3410 * flush currently running tasks.
3411 */
3412 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3413 wake_up_all(&gcwq->trustee_wait);
3414
3415 /*
3416 * The original cpu is in the process of dying and may go away
3417 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003418 * be migrated to other cpus. Try draining any left work. We
3419 * want to get it over with ASAP - spam rescuers, wake up as
3420 * many idlers as necessary and create new ones till the
3421 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003422 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003423 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003424 */
3425 while (gcwq->nr_workers != gcwq->nr_idle ||
3426 gcwq->flags & GCWQ_FREEZING ||
3427 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003428 int nr_works = 0;
3429
3430 list_for_each_entry(work, &gcwq->worklist, entry) {
3431 send_mayday(work);
3432 nr_works++;
3433 }
3434
3435 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3436 if (!nr_works--)
3437 break;
3438 wake_up_process(worker->task);
3439 }
3440
3441 if (need_to_create_worker(gcwq)) {
3442 spin_unlock_irq(&gcwq->lock);
3443 worker = create_worker(gcwq, false);
3444 spin_lock_irq(&gcwq->lock);
3445 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003446 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003447 start_worker(worker);
3448 }
3449 }
3450
Tejun Heodb7bccf2010-06-29 10:07:12 +02003451 /* give a breather */
3452 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3453 break;
3454 }
3455
Tejun Heoe22bee72010-06-29 10:07:14 +02003456 /*
3457 * Either all works have been scheduled and cpu is down, or
3458 * cpu down has already been canceled. Wait for and butcher
3459 * all workers till we're canceled.
3460 */
3461 do {
3462 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3463 while (!list_empty(&gcwq->idle_list))
3464 destroy_worker(list_first_entry(&gcwq->idle_list,
3465 struct worker, entry));
3466 } while (gcwq->nr_workers && rc >= 0);
3467
3468 /*
3469 * At this point, either draining has completed and no worker
3470 * is left, or cpu down has been canceled or the cpu is being
3471 * brought back up. There shouldn't be any idle one left.
3472 * Tell the remaining busy ones to rebind once it finishes the
3473 * currently scheduled works by scheduling the rebind_work.
3474 */
3475 WARN_ON(!list_empty(&gcwq->idle_list));
3476
3477 for_each_busy_worker(worker, i, pos, gcwq) {
3478 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003479 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003480
3481 /*
3482 * Rebind_work may race with future cpu hotplug
3483 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003484 * rebinding is scheduled. The morphing should
3485 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003486 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003487 worker_flags |= WORKER_REBIND;
3488 worker_flags &= ~WORKER_ROGUE;
3489 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003490
3491 /* queue rebind_work, wq doesn't matter, use the default one */
3492 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3493 work_data_bits(rebind_work)))
3494 continue;
3495
3496 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003497 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003498 worker->scheduled.next,
3499 work_color_to_flags(WORK_NO_COLOR));
3500 }
3501
3502 /* relinquish manager role */
3503 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3504
Tejun Heodb7bccf2010-06-29 10:07:12 +02003505 /* notify completion */
3506 gcwq->trustee = NULL;
3507 gcwq->trustee_state = TRUSTEE_DONE;
3508 wake_up_all(&gcwq->trustee_wait);
3509 spin_unlock_irq(&gcwq->lock);
3510 return 0;
3511}
3512
3513/**
3514 * wait_trustee_state - wait for trustee to enter the specified state
3515 * @gcwq: gcwq the trustee of interest belongs to
3516 * @state: target state to wait for
3517 *
3518 * Wait for the trustee to reach @state. DONE is already matched.
3519 *
3520 * CONTEXT:
3521 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3522 * multiple times. To be used by cpu_callback.
3523 */
3524static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003525__releases(&gcwq->lock)
3526__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003527{
3528 if (!(gcwq->trustee_state == state ||
3529 gcwq->trustee_state == TRUSTEE_DONE)) {
3530 spin_unlock_irq(&gcwq->lock);
3531 __wait_event(gcwq->trustee_wait,
3532 gcwq->trustee_state == state ||
3533 gcwq->trustee_state == TRUSTEE_DONE);
3534 spin_lock_irq(&gcwq->lock);
3535 }
3536}
3537
Oleg Nesterov3af244332007-05-09 02:34:09 -07003538static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3539 unsigned long action,
3540 void *hcpu)
3541{
3542 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003543 struct global_cwq *gcwq = get_gcwq(cpu);
3544 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003545 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003546 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003548 action &= ~CPU_TASKS_FROZEN;
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003551 case CPU_DOWN_PREPARE:
3552 new_trustee = kthread_create(trustee_thread, gcwq,
3553 "workqueue_trustee/%d\n", cpu);
3554 if (IS_ERR(new_trustee))
3555 return notifier_from_errno(PTR_ERR(new_trustee));
3556 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003557 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003559 BUG_ON(gcwq->first_idle);
3560 new_worker = create_worker(gcwq, false);
3561 if (!new_worker) {
3562 if (new_trustee)
3563 kthread_stop(new_trustee);
3564 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
3567
Tejun Heodb7bccf2010-06-29 10:07:12 +02003568 /* some are called w/ irq disabled, don't disturb irq status */
3569 spin_lock_irqsave(&gcwq->lock, flags);
3570
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003571 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003572 case CPU_DOWN_PREPARE:
3573 /* initialize trustee and tell it to acquire the gcwq */
3574 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3575 gcwq->trustee = new_trustee;
3576 gcwq->trustee_state = TRUSTEE_START;
3577 wake_up_process(gcwq->trustee);
3578 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003579 /* fall through */
3580 case CPU_UP_PREPARE:
3581 BUG_ON(gcwq->first_idle);
3582 gcwq->first_idle = new_worker;
3583 break;
3584
3585 case CPU_DYING:
3586 /*
3587 * Before this, the trustee and all workers except for
3588 * the ones which are still executing works from
3589 * before the last CPU down must be on the cpu. After
3590 * this, they'll all be diasporas.
3591 */
3592 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003593 break;
3594
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003595 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003596 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003597 /* fall through */
3598 case CPU_UP_CANCELED:
3599 destroy_worker(gcwq->first_idle);
3600 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003601 break;
3602
3603 case CPU_DOWN_FAILED:
3604 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003605 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003606 if (gcwq->trustee_state != TRUSTEE_DONE) {
3607 gcwq->trustee_state = TRUSTEE_RELEASE;
3608 wake_up_process(gcwq->trustee);
3609 wait_trustee_state(gcwq, TRUSTEE_DONE);
3610 }
3611
Tejun Heoe22bee72010-06-29 10:07:14 +02003612 /*
3613 * Trustee is done and there might be no worker left.
3614 * Put the first_idle in and request a real manager to
3615 * take a look.
3616 */
3617 spin_unlock_irq(&gcwq->lock);
3618 kthread_bind(gcwq->first_idle->task, cpu);
3619 spin_lock_irq(&gcwq->lock);
3620 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3621 start_worker(gcwq->first_idle);
3622 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003623 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003624 }
3625
Tejun Heodb7bccf2010-06-29 10:07:12 +02003626 spin_unlock_irqrestore(&gcwq->lock, flags);
3627
Tejun Heo15376632010-06-29 10:07:11 +02003628 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Tejun Heod3b42542012-07-17 12:39:26 -07003631/*
3632 * Workqueues should be brought up before normal priority CPU notifiers.
3633 * This will be registered high priority CPU notifier.
3634 */
3635static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3636 unsigned long action,
3637 void *hcpu)
3638{
3639 switch (action & ~CPU_TASKS_FROZEN) {
3640 case CPU_UP_PREPARE:
3641 case CPU_UP_CANCELED:
3642 case CPU_DOWN_FAILED:
3643 case CPU_ONLINE:
3644 return workqueue_cpu_callback(nfb, action, hcpu);
3645 }
3646 return NOTIFY_OK;
3647}
3648
3649/*
3650 * Workqueues should be brought down after normal priority CPU notifiers.
3651 * This will be registered as low priority CPU notifier.
3652 */
3653static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3654 unsigned long action,
3655 void *hcpu)
3656{
3657 switch (action & ~CPU_TASKS_FROZEN) {
3658 case CPU_DOWN_PREPARE:
3659 case CPU_DYING:
3660 case CPU_POST_DEAD:
3661 return workqueue_cpu_callback(nfb, action, hcpu);
3662 }
3663 return NOTIFY_OK;
3664}
3665
Rusty Russell2d3854a2008-11-05 13:39:10 +11003666#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003667
Rusty Russell2d3854a2008-11-05 13:39:10 +11003668struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003669 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003670 long (*fn)(void *);
3671 void *arg;
3672 long ret;
3673};
3674
Tejun Heofc7da7e2012-09-18 12:48:43 -07003675static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003676{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003677 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3678
Rusty Russell2d3854a2008-11-05 13:39:10 +11003679 wfc->ret = wfc->fn(wfc->arg);
3680}
3681
3682/**
3683 * work_on_cpu - run a function in user context on a particular cpu
3684 * @cpu: the cpu to run on
3685 * @fn: the function to run
3686 * @arg: the function arg
3687 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003688 * This will return the value @fn returns.
3689 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003690 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003691 */
3692long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3693{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003694 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003695
Tejun Heofc7da7e2012-09-18 12:48:43 -07003696 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3697 schedule_work_on(cpu, &wfc.work);
3698 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003699 return wfc.ret;
3700}
3701EXPORT_SYMBOL_GPL(work_on_cpu);
3702#endif /* CONFIG_SMP */
3703
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003704#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303705
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003706/**
3707 * freeze_workqueues_begin - begin freezing workqueues
3708 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003709 * Start freezing workqueues. After this function returns, all freezable
3710 * workqueues will queue new works to their frozen_works list instead of
3711 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003712 *
3713 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003714 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003715 */
3716void freeze_workqueues_begin(void)
3717{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003718 unsigned int cpu;
3719
3720 spin_lock(&workqueue_lock);
3721
3722 BUG_ON(workqueue_freezing);
3723 workqueue_freezing = true;
3724
Tejun Heof3421792010-07-02 10:03:51 +02003725 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003726 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003727 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003728
3729 spin_lock_irq(&gcwq->lock);
3730
Tejun Heodb7bccf2010-06-29 10:07:12 +02003731 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3732 gcwq->flags |= GCWQ_FREEZING;
3733
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003734 list_for_each_entry(wq, &workqueues, list) {
3735 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3736
Tejun Heo58a69cb2011-02-16 09:25:31 +01003737 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003738 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003739 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003740
3741 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003742 }
3743
3744 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003746
3747/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003748 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003749 *
3750 * Check whether freezing is complete. This function must be called
3751 * between freeze_workqueues_begin() and thaw_workqueues().
3752 *
3753 * CONTEXT:
3754 * Grabs and releases workqueue_lock.
3755 *
3756 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003757 * %true if some freezable workqueues are still busy. %false if freezing
3758 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003759 */
3760bool freeze_workqueues_busy(void)
3761{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003762 unsigned int cpu;
3763 bool busy = false;
3764
3765 spin_lock(&workqueue_lock);
3766
3767 BUG_ON(!workqueue_freezing);
3768
Tejun Heof3421792010-07-02 10:03:51 +02003769 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003770 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003771 /*
3772 * nr_active is monotonically decreasing. It's safe
3773 * to peek without lock.
3774 */
3775 list_for_each_entry(wq, &workqueues, list) {
3776 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3777
Tejun Heo58a69cb2011-02-16 09:25:31 +01003778 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003779 continue;
3780
3781 BUG_ON(cwq->nr_active < 0);
3782 if (cwq->nr_active) {
3783 busy = true;
3784 goto out_unlock;
3785 }
3786 }
3787 }
3788out_unlock:
3789 spin_unlock(&workqueue_lock);
3790 return busy;
3791}
3792
3793/**
3794 * thaw_workqueues - thaw workqueues
3795 *
3796 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003797 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003798 *
3799 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003800 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801 */
3802void thaw_workqueues(void)
3803{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003804 unsigned int cpu;
3805
3806 spin_lock(&workqueue_lock);
3807
3808 if (!workqueue_freezing)
3809 goto out_unlock;
3810
Tejun Heof3421792010-07-02 10:03:51 +02003811 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003812 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003813 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003814
3815 spin_lock_irq(&gcwq->lock);
3816
Tejun Heodb7bccf2010-06-29 10:07:12 +02003817 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3818 gcwq->flags &= ~GCWQ_FREEZING;
3819
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003820 list_for_each_entry(wq, &workqueues, list) {
3821 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3822
Tejun Heo58a69cb2011-02-16 09:25:31 +01003823 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003824 continue;
3825
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003826 /* restore max_active and repopulate worklist */
3827 cwq->max_active = wq->saved_max_active;
3828
3829 while (!list_empty(&cwq->delayed_works) &&
3830 cwq->nr_active < cwq->max_active)
3831 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003832 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003833
Tejun Heoe22bee72010-06-29 10:07:14 +02003834 wake_up_worker(gcwq);
3835
Tejun Heo8b03ae32010-06-29 10:07:12 +02003836 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003837 }
3838
3839 workqueue_freezing = false;
3840out_unlock:
3841 spin_unlock(&workqueue_lock);
3842}
3843#endif /* CONFIG_FREEZER */
3844
Suresh Siddha6ee05782010-07-30 14:57:37 -07003845static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846{
Tejun Heoc34056a2010-06-29 10:07:11 +02003847 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003848 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003849
Tejun Heod3b42542012-07-17 12:39:26 -07003850 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3851 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003852
3853 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003854 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003855 struct global_cwq *gcwq = get_gcwq(cpu);
3856
3857 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003858 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003859 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003860 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003861
Tejun Heoc8e55f32010-06-29 10:07:12 +02003862 INIT_LIST_HEAD(&gcwq->idle_list);
3863 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3864 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3865
Tejun Heoe22bee72010-06-29 10:07:14 +02003866 init_timer_deferrable(&gcwq->idle_timer);
3867 gcwq->idle_timer.function = idle_worker_timeout;
3868 gcwq->idle_timer.data = (unsigned long)gcwq;
3869
3870 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3871 (unsigned long)gcwq);
3872
Tejun Heo8b03ae32010-06-29 10:07:12 +02003873 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003874
3875 gcwq->trustee_state = TRUSTEE_DONE;
3876 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003877 }
3878
Tejun Heoe22bee72010-06-29 10:07:14 +02003879 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003880 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003881 struct global_cwq *gcwq = get_gcwq(cpu);
3882 struct worker *worker;
3883
Tejun Heo477a3c32010-08-31 10:54:35 +02003884 if (cpu != WORK_CPU_UNBOUND)
3885 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003886 worker = create_worker(gcwq, true);
3887 BUG_ON(!worker);
3888 spin_lock_irq(&gcwq->lock);
3889 start_worker(worker);
3890 spin_unlock_irq(&gcwq->lock);
3891 }
3892
Tejun Heod320c032010-06-29 10:07:14 +02003893 system_wq = alloc_workqueue("events", 0, 0);
3894 system_long_wq = alloc_workqueue("events_long", 0, 0);
3895 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003896 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3897 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003898 system_freezable_wq = alloc_workqueue("events_freezable",
3899 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003900 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3901 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003902 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003903 !system_unbound_wq || !system_freezable_wq ||
3904 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003907early_initcall(init_workqueues);