blob: 13ea688f276182d351e9dc0281f85127381e6b3c [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
Lai Jiangshan23f09132014-02-15 22:02:28 +08001472 /*
1473 * Once WORKER_DIE is set, the kworker may destroy itself at any
1474 * point. Pin to ensure the task stays until we're done with it.
1475 */
1476 get_task_struct(worker->task);
1477
Tejun Heoc8e55f32010-06-29 10:07:12 +02001478 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001479 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480
1481 spin_unlock_irq(&gcwq->lock);
1482
Tejun Heoc34056a2010-06-29 10:07:11 +02001483 kthread_stop(worker->task);
Lai Jiangshan23f09132014-02-15 22:02:28 +08001484 put_task_struct(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001485 kfree(worker);
1486
Tejun Heo8b03ae32010-06-29 10:07:12 +02001487 spin_lock_irq(&gcwq->lock);
1488 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001489}
1490
Tejun Heoe22bee72010-06-29 10:07:14 +02001491static void idle_worker_timeout(unsigned long __gcwq)
1492{
1493 struct global_cwq *gcwq = (void *)__gcwq;
1494
1495 spin_lock_irq(&gcwq->lock);
1496
1497 if (too_many_workers(gcwq)) {
1498 struct worker *worker;
1499 unsigned long expires;
1500
1501 /* idle_list is kept in LIFO order, check the last one */
1502 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1503 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1504
1505 if (time_before(jiffies, expires))
1506 mod_timer(&gcwq->idle_timer, expires);
1507 else {
1508 /* it's been idle for too long, wake up manager */
1509 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1510 wake_up_worker(gcwq);
1511 }
1512 }
1513
1514 spin_unlock_irq(&gcwq->lock);
1515}
1516
1517static bool send_mayday(struct work_struct *work)
1518{
1519 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1520 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001521 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001522
1523 if (!(wq->flags & WQ_RESCUER))
1524 return false;
1525
1526 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001527 cpu = cwq->gcwq->cpu;
1528 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1529 if (cpu == WORK_CPU_UNBOUND)
1530 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001531 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001532 wake_up_process(wq->rescuer->task);
1533 return true;
1534}
1535
1536static void gcwq_mayday_timeout(unsigned long __gcwq)
1537{
1538 struct global_cwq *gcwq = (void *)__gcwq;
1539 struct work_struct *work;
1540
1541 spin_lock_irq(&gcwq->lock);
1542
1543 if (need_to_create_worker(gcwq)) {
1544 /*
1545 * We've been trying to create a new worker but
1546 * haven't been successful. We might be hitting an
1547 * allocation deadlock. Send distress signals to
1548 * rescuers.
1549 */
1550 list_for_each_entry(work, &gcwq->worklist, entry)
1551 send_mayday(work);
1552 }
1553
1554 spin_unlock_irq(&gcwq->lock);
1555
1556 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1557}
1558
1559/**
1560 * maybe_create_worker - create a new worker if necessary
1561 * @gcwq: gcwq to create a new worker for
1562 *
1563 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1564 * have at least one idle worker on return from this function. If
1565 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1566 * sent to all rescuers with works scheduled on @gcwq to resolve
1567 * possible allocation deadlock.
1568 *
1569 * On return, need_to_create_worker() is guaranteed to be false and
1570 * may_start_working() true.
1571 *
1572 * LOCKING:
1573 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1574 * multiple times. Does GFP_KERNEL allocations. Called only from
1575 * manager.
1576 *
1577 * RETURNS:
1578 * false if no action was taken and gcwq->lock stayed locked, true
1579 * otherwise.
1580 */
1581static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001582__releases(&gcwq->lock)
1583__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001584{
1585 if (!need_to_create_worker(gcwq))
1586 return false;
1587restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001588 spin_unlock_irq(&gcwq->lock);
1589
Tejun Heoe22bee72010-06-29 10:07:14 +02001590 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1591 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1592
1593 while (true) {
1594 struct worker *worker;
1595
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 worker = create_worker(gcwq, true);
1597 if (worker) {
1598 del_timer_sync(&gcwq->mayday_timer);
1599 spin_lock_irq(&gcwq->lock);
1600 start_worker(worker);
1601 BUG_ON(need_to_create_worker(gcwq));
1602 return true;
1603 }
1604
1605 if (!need_to_create_worker(gcwq))
1606 break;
1607
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 __set_current_state(TASK_INTERRUPTIBLE);
1609 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001610
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 if (!need_to_create_worker(gcwq))
1612 break;
1613 }
1614
Tejun Heoe22bee72010-06-29 10:07:14 +02001615 del_timer_sync(&gcwq->mayday_timer);
1616 spin_lock_irq(&gcwq->lock);
1617 if (need_to_create_worker(gcwq))
1618 goto restart;
1619 return true;
1620}
1621
1622/**
1623 * maybe_destroy_worker - destroy workers which have been idle for a while
1624 * @gcwq: gcwq to destroy workers for
1625 *
1626 * Destroy @gcwq workers which have been idle for longer than
1627 * IDLE_WORKER_TIMEOUT.
1628 *
1629 * LOCKING:
1630 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1631 * multiple times. Called only from manager.
1632 *
1633 * RETURNS:
1634 * false if no action was taken and gcwq->lock stayed locked, true
1635 * otherwise.
1636 */
1637static bool maybe_destroy_workers(struct global_cwq *gcwq)
1638{
1639 bool ret = false;
1640
1641 while (too_many_workers(gcwq)) {
1642 struct worker *worker;
1643 unsigned long expires;
1644
1645 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1646 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1647
1648 if (time_before(jiffies, expires)) {
1649 mod_timer(&gcwq->idle_timer, expires);
1650 break;
1651 }
1652
1653 destroy_worker(worker);
1654 ret = true;
1655 }
1656
1657 return ret;
1658}
1659
1660/**
1661 * manage_workers - manage worker pool
1662 * @worker: self
1663 *
1664 * Assume the manager role and manage gcwq worker pool @worker belongs
1665 * to. At any given time, there can be only zero or one manager per
1666 * gcwq. The exclusion is handled automatically by this function.
1667 *
1668 * The caller can safely start processing works on false return. On
1669 * true return, it's guaranteed that need_to_create_worker() is false
1670 * and may_start_working() is true.
1671 *
1672 * CONTEXT:
1673 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1674 * multiple times. Does GFP_KERNEL allocations.
1675 *
1676 * RETURNS:
1677 * false if no action was taken and gcwq->lock stayed locked, true if
1678 * some action was taken.
1679 */
1680static bool manage_workers(struct worker *worker)
1681{
1682 struct global_cwq *gcwq = worker->gcwq;
1683 bool ret = false;
1684
1685 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1686 return ret;
1687
1688 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1689 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1690
1691 /*
1692 * Destroy and then create so that may_start_working() is true
1693 * on return.
1694 */
1695 ret |= maybe_destroy_workers(gcwq);
1696 ret |= maybe_create_worker(gcwq);
1697
1698 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1699
1700 /*
1701 * The trustee might be waiting to take over the manager
1702 * position, tell it we're done.
1703 */
1704 if (unlikely(gcwq->trustee))
1705 wake_up_all(&gcwq->trustee_wait);
1706
1707 return ret;
1708}
1709
Tejun Heoa62428c2010-06-29 10:07:10 +02001710/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001711 * move_linked_works - move linked works to a list
1712 * @work: start of series of works to be scheduled
1713 * @head: target list to append @work to
1714 * @nextp: out paramter for nested worklist walking
1715 *
1716 * Schedule linked works starting from @work to @head. Work series to
1717 * be scheduled starts at @work and includes any consecutive work with
1718 * WORK_STRUCT_LINKED set in its predecessor.
1719 *
1720 * If @nextp is not NULL, it's updated to point to the next work of
1721 * the last scheduled work. This allows move_linked_works() to be
1722 * nested inside outer list_for_each_entry_safe().
1723 *
1724 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001725 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001726 */
1727static void move_linked_works(struct work_struct *work, struct list_head *head,
1728 struct work_struct **nextp)
1729{
1730 struct work_struct *n;
1731
1732 /*
1733 * Linked worklist will always end before the end of the list,
1734 * use NULL for list head.
1735 */
1736 list_for_each_entry_safe_from(work, n, NULL, entry) {
1737 list_move_tail(&work->entry, head);
1738 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1739 break;
1740 }
1741
1742 /*
1743 * If we're already inside safe list traversal and have moved
1744 * multiple works to the scheduled queue, the next position
1745 * needs to be updated.
1746 */
1747 if (nextp)
1748 *nextp = n;
1749}
1750
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001751static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001752{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001753 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001754 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001755
Tejun Heocdadf002010-10-05 10:49:55 +02001756 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001757 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001758 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001759 cwq->nr_active++;
1760}
1761
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001762static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1763{
1764 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1765 struct work_struct, entry);
1766
1767 cwq_activate_delayed_work(work);
1768}
1769
Tejun Heoaffee4b2010-06-29 10:07:12 +02001770/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001771 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1772 * @cwq: cwq of interest
1773 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001774 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001775 *
1776 * A work either has completed or is removed from pending queue,
1777 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1778 *
1779 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001780 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001781 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001782static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1783 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001784{
1785 /* ignore uncolored works */
1786 if (color == WORK_NO_COLOR)
1787 return;
1788
1789 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001790
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001791 if (!delayed) {
1792 cwq->nr_active--;
1793 if (!list_empty(&cwq->delayed_works)) {
1794 /* one down, submit a delayed one */
1795 if (cwq->nr_active < cwq->max_active)
1796 cwq_activate_first_delayed(cwq);
1797 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001798 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001799
1800 /* is flush in progress and are we at the flushing tip? */
1801 if (likely(cwq->flush_color != color))
1802 return;
1803
1804 /* are there still in-flight works? */
1805 if (cwq->nr_in_flight[color])
1806 return;
1807
1808 /* this cwq is done, clear flush_color */
1809 cwq->flush_color = -1;
1810
1811 /*
1812 * If this was the last cwq, wake up the first flusher. It
1813 * will handle the rest.
1814 */
1815 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1816 complete(&cwq->wq->first_flusher->done);
1817}
1818
1819/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001820 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001821 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001822 * @work: work to process
1823 *
1824 * Process @work. This function contains all the logics necessary to
1825 * process a single work including synchronization against and
1826 * interaction with other workers on the same cpu, queueing and
1827 * flushing. As long as context requirement is met, any worker can
1828 * call this function to process a work.
1829 *
1830 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001831 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001832 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001833static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001834__releases(&gcwq->lock)
1835__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001836{
Tejun Heo7e116292010-06-29 10:07:13 +02001837 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001838 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001839 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001840 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001841 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001842 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001843#ifdef CONFIG_LOCKDEP
1844 /*
1845 * It is permissible to free the struct work_struct from
1846 * inside the function that is called from it, this we need to
1847 * take into account for lockdep too. To avoid bogus "held
1848 * lock freed" warnings as well as problems when looking into
1849 * work->lockdep_map, make a copy and use that here.
1850 */
1851 struct lockdep_map lockdep_map = work->lockdep_map;
1852#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001853 /*
1854 * A single work shouldn't be executed concurrently by
1855 * multiple workers on a single cpu. Check whether anyone is
1856 * already processing the work. If so, defer the work to the
1857 * currently executing one.
1858 */
1859 collision = __find_worker_executing_work(gcwq, bwh, work);
1860 if (unlikely(collision)) {
1861 move_linked_works(work, &collision->scheduled, NULL);
1862 return;
1863 }
1864
Tejun Heoa62428c2010-06-29 10:07:10 +02001865 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001866 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001867 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001868 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001869 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001870 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001871 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001872
Tejun Heo7a22ad72010-06-29 10:07:13 +02001873 /* record the current cpu number in the work data and dequeue */
1874 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001875 list_del_init(&work->entry);
1876
Tejun Heo649027d2010-06-29 10:07:14 +02001877 /*
1878 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1879 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1880 */
1881 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1882 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1883 struct work_struct, entry);
1884
1885 if (!list_empty(&gcwq->worklist) &&
1886 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1887 wake_up_worker(gcwq);
1888 else
1889 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1890 }
1891
Tejun Heofb0e7be2010-06-29 10:07:15 +02001892 /*
1893 * CPU intensive works don't participate in concurrency
1894 * management. They're the scheduler's responsibility.
1895 */
1896 if (unlikely(cpu_intensive))
1897 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1898
Tejun Heo8b03ae32010-06-29 10:07:12 +02001899 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001900
Tejun Heo66307ae2012-08-03 10:30:45 -07001901 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001902 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001903
Tejun Heoe1594892011-01-09 23:32:15 +01001904 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001905 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001906 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001907 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001908 /*
1909 * While we must be careful to not use "work" after this, the trace
1910 * point will only record its address.
1911 */
1912 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001913 lock_map_release(&lockdep_map);
1914 lock_map_release(&cwq->wq->lockdep_map);
1915
1916 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001917 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1918 " last function: %pf\n",
1919 current->comm, preempt_count(), task_pid_nr(current),
1920 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001921 debug_show_held_locks(current);
1922 dump_stack();
1923 }
1924
Tejun Heo8b03ae32010-06-29 10:07:12 +02001925 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001926
Tejun Heofb0e7be2010-06-29 10:07:15 +02001927 /* clear cpu intensive status */
1928 if (unlikely(cpu_intensive))
1929 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1930
Tejun Heoa62428c2010-06-29 10:07:10 +02001931 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001932 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001933 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001934 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001935 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001936 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001937}
1938
Tejun Heoaffee4b2010-06-29 10:07:12 +02001939/**
1940 * process_scheduled_works - process scheduled works
1941 * @worker: self
1942 *
1943 * Process all scheduled works. Please note that the scheduled list
1944 * may change while processing a work, so this function repeatedly
1945 * fetches a work from the top and executes it.
1946 *
1947 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001948 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001949 * multiple times.
1950 */
1951static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001953 while (!list_empty(&worker->scheduled)) {
1954 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001956 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Tejun Heo4690c4a2010-06-29 10:07:10 +02001960/**
1961 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001962 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001963 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 * The gcwq worker thread function. There's a single dynamic pool of
1965 * these per each cpu. These workers process all works regardless of
1966 * their specific target workqueue. The only exception is works which
1967 * belong to workqueues with a rescuer which will be explained in
1968 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001969 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001970static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Tejun Heoc34056a2010-06-29 10:07:11 +02001972 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001973 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 /* tell the scheduler that this is a workqueue worker */
1976 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001978 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Tejun Heoc8e55f32010-06-29 10:07:12 +02001980 /* DIE can be set only while we're idle, checking here is enough */
1981 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001982 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986
Tejun Heoc8e55f32010-06-29 10:07:12 +02001987 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001988recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 /* no more worker necessary? */
1990 if (!need_more_worker(gcwq))
1991 goto sleep;
1992
1993 /* do we need to manage? */
1994 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1995 goto recheck;
1996
Tejun Heoc8e55f32010-06-29 10:07:12 +02001997 /*
1998 * ->scheduled list can only be filled while a worker is
1999 * preparing to process a work or actually processing it.
2000 * Make sure nobody diddled with it while I was sleeping.
2001 */
2002 BUG_ON(!list_empty(&worker->scheduled));
2003
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 /*
2005 * When control reaches this point, we're guaranteed to have
2006 * at least one idle worker or that someone else has already
2007 * assumed the manager role.
2008 */
2009 worker_clr_flags(worker, WORKER_PREP);
2010
2011 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002012 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02002013 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002014 struct work_struct, entry);
2015
2016 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2017 /* optimization path, not strictly necessary */
2018 process_one_work(worker, work);
2019 if (unlikely(!list_empty(&worker->scheduled)))
2020 process_scheduled_works(worker);
2021 } else {
2022 move_linked_works(work, &worker->scheduled, NULL);
2023 process_scheduled_works(worker);
2024 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002026
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002028sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
2030 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002031
Tejun Heoc8e55f32010-06-29 10:07:12 +02002032 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 * gcwq->lock is held and there's no work to process and no
2034 * need to manage, sleep. Workers are woken up only while
2035 * holding gcwq->lock or from local cpu, so setting the
2036 * current state before releasing gcwq->lock is enough to
2037 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002038 */
2039 worker_enter_idle(worker);
2040 __set_current_state(TASK_INTERRUPTIBLE);
2041 spin_unlock_irq(&gcwq->lock);
2042 schedule();
2043 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Tejun Heoe22bee72010-06-29 10:07:14 +02002046/**
2047 * rescuer_thread - the rescuer thread function
2048 * @__wq: the associated workqueue
2049 *
2050 * Workqueue rescuer thread function. There's one rescuer for each
2051 * workqueue which has WQ_RESCUER set.
2052 *
2053 * Regular work processing on a gcwq may block trying to create a new
2054 * worker which uses GFP_KERNEL allocation which has slight chance of
2055 * developing into deadlock if some works currently on the same queue
2056 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2057 * the problem rescuer solves.
2058 *
2059 * When such condition is possible, the gcwq summons rescuers of all
2060 * workqueues which have works queued on the gcwq and let them process
2061 * those works so that forward progress can be guaranteed.
2062 *
2063 * This should happen rarely.
2064 */
2065static int rescuer_thread(void *__wq)
2066{
2067 struct workqueue_struct *wq = __wq;
2068 struct worker *rescuer = wq->rescuer;
2069 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002070 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002071 unsigned int cpu;
2072
2073 set_user_nice(current, RESCUER_NICE_LEVEL);
2074repeat:
2075 set_current_state(TASK_INTERRUPTIBLE);
2076
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002077 if (kthread_should_stop()) {
2078 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002079 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002080 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002081
Tejun Heof3421792010-07-02 10:03:51 +02002082 /*
2083 * See whether any cpu is asking for help. Unbounded
2084 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2085 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002086 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002087 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2088 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002089 struct global_cwq *gcwq = cwq->gcwq;
2090 struct work_struct *work, *n;
2091
2092 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002093 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002094
2095 /* migrate to the target cpu if possible */
2096 rescuer->gcwq = gcwq;
2097 worker_maybe_bind_and_lock(rescuer);
2098
2099 /*
2100 * Slurp in all works issued via this workqueue and
2101 * process'em.
2102 */
2103 BUG_ON(!list_empty(&rescuer->scheduled));
2104 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2105 if (get_work_cwq(work) == cwq)
2106 move_linked_works(work, scheduled, &n);
2107
2108 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002109
2110 /*
2111 * Leave this gcwq. If keep_working() is %true, notify a
2112 * regular worker; otherwise, we end up with 0 concurrency
2113 * and stalling the execution.
2114 */
2115 if (keep_working(gcwq))
2116 wake_up_worker(gcwq);
2117
Tejun Heoe22bee72010-06-29 10:07:14 +02002118 spin_unlock_irq(&gcwq->lock);
2119 }
2120
2121 schedule();
2122 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002125struct wq_barrier {
2126 struct work_struct work;
2127 struct completion done;
2128};
2129
2130static void wq_barrier_func(struct work_struct *work)
2131{
2132 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2133 complete(&barr->done);
2134}
2135
Tejun Heo4690c4a2010-06-29 10:07:10 +02002136/**
2137 * insert_wq_barrier - insert a barrier work
2138 * @cwq: cwq to insert barrier into
2139 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002140 * @target: target work to attach @barr to
2141 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002142 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002143 * @barr is linked to @target such that @barr is completed only after
2144 * @target finishes execution. Please note that the ordering
2145 * guarantee is observed only with respect to @target and on the local
2146 * cpu.
2147 *
2148 * Currently, a queued barrier can't be canceled. This is because
2149 * try_to_grab_pending() can't determine whether the work to be
2150 * grabbed is at the head of the queue and thus can't clear LINKED
2151 * flag of the previous work while there must be a valid next work
2152 * after a work with LINKED flag set.
2153 *
2154 * Note that when @worker is non-NULL, @target may be modified
2155 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002156 *
2157 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002158 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002159 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002160static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002161 struct wq_barrier *barr,
2162 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002163{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002164 struct list_head *head;
2165 unsigned int linked = 0;
2166
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002167 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002168 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002169 * as we know for sure that this will not trigger any of the
2170 * checks and call back into the fixup functions where we
2171 * might deadlock.
2172 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002173 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002174 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002175 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002176
Tejun Heoaffee4b2010-06-29 10:07:12 +02002177 /*
2178 * If @target is currently being executed, schedule the
2179 * barrier to the worker; otherwise, put it after @target.
2180 */
2181 if (worker)
2182 head = worker->scheduled.next;
2183 else {
2184 unsigned long *bits = work_data_bits(target);
2185
2186 head = target->entry.next;
2187 /* there can already be other linked works, inherit and set */
2188 linked = *bits & WORK_STRUCT_LINKED;
2189 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2190 }
2191
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002192 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002193 insert_work(cwq, &barr->work, head,
2194 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002195}
2196
Tejun Heo73f53c42010-06-29 10:07:11 +02002197/**
2198 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2199 * @wq: workqueue being flushed
2200 * @flush_color: new flush color, < 0 for no-op
2201 * @work_color: new work color, < 0 for no-op
2202 *
2203 * Prepare cwqs for workqueue flushing.
2204 *
2205 * If @flush_color is non-negative, flush_color on all cwqs should be
2206 * -1. If no cwq has in-flight commands at the specified color, all
2207 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2208 * has in flight commands, its cwq->flush_color is set to
2209 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2210 * wakeup logic is armed and %true is returned.
2211 *
2212 * The caller should have initialized @wq->first_flusher prior to
2213 * calling this function with non-negative @flush_color. If
2214 * @flush_color is negative, no flush color update is done and %false
2215 * is returned.
2216 *
2217 * If @work_color is non-negative, all cwqs should have the same
2218 * work_color which is previous to @work_color and all will be
2219 * advanced to @work_color.
2220 *
2221 * CONTEXT:
2222 * mutex_lock(wq->flush_mutex).
2223 *
2224 * RETURNS:
2225 * %true if @flush_color >= 0 and there's something to flush. %false
2226 * otherwise.
2227 */
2228static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2229 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Tejun Heo73f53c42010-06-29 10:07:11 +02002231 bool wait = false;
2232 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002233
Tejun Heo73f53c42010-06-29 10:07:11 +02002234 if (flush_color >= 0) {
2235 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2236 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002237 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002238
Tejun Heof3421792010-07-02 10:03:51 +02002239 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002240 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002241 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002242
Tejun Heo8b03ae32010-06-29 10:07:12 +02002243 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002244
2245 if (flush_color >= 0) {
2246 BUG_ON(cwq->flush_color != -1);
2247
2248 if (cwq->nr_in_flight[flush_color]) {
2249 cwq->flush_color = flush_color;
2250 atomic_inc(&wq->nr_cwqs_to_flush);
2251 wait = true;
2252 }
2253 }
2254
2255 if (work_color >= 0) {
2256 BUG_ON(work_color != work_next_color(cwq->work_color));
2257 cwq->work_color = work_color;
2258 }
2259
Tejun Heo8b03ae32010-06-29 10:07:12 +02002260 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002261 }
2262
2263 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2264 complete(&wq->first_flusher->done);
2265
2266 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002269/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002271 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 *
2273 * Forces execution of the workqueue and blocks until its completion.
2274 * This is typically used in driver shutdown handlers.
2275 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002276 * We sleep until all works which were queued on entry have been handled,
2277 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002279void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Tejun Heo73f53c42010-06-29 10:07:11 +02002281 struct wq_flusher this_flusher = {
2282 .list = LIST_HEAD_INIT(this_flusher.list),
2283 .flush_color = -1,
2284 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2285 };
2286 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002287
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002288 lock_map_acquire(&wq->lockdep_map);
2289 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002290
2291 mutex_lock(&wq->flush_mutex);
2292
2293 /*
2294 * Start-to-wait phase
2295 */
2296 next_color = work_next_color(wq->work_color);
2297
2298 if (next_color != wq->flush_color) {
2299 /*
2300 * Color space is not full. The current work_color
2301 * becomes our flush_color and work_color is advanced
2302 * by one.
2303 */
2304 BUG_ON(!list_empty(&wq->flusher_overflow));
2305 this_flusher.flush_color = wq->work_color;
2306 wq->work_color = next_color;
2307
2308 if (!wq->first_flusher) {
2309 /* no flush in progress, become the first flusher */
2310 BUG_ON(wq->flush_color != this_flusher.flush_color);
2311
2312 wq->first_flusher = &this_flusher;
2313
2314 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2315 wq->work_color)) {
2316 /* nothing to flush, done */
2317 wq->flush_color = next_color;
2318 wq->first_flusher = NULL;
2319 goto out_unlock;
2320 }
2321 } else {
2322 /* wait in queue */
2323 BUG_ON(wq->flush_color == this_flusher.flush_color);
2324 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2325 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2326 }
2327 } else {
2328 /*
2329 * Oops, color space is full, wait on overflow queue.
2330 * The next flush completion will assign us
2331 * flush_color and transfer to flusher_queue.
2332 */
2333 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2334 }
2335
2336 mutex_unlock(&wq->flush_mutex);
2337
2338 wait_for_completion(&this_flusher.done);
2339
2340 /*
2341 * Wake-up-and-cascade phase
2342 *
2343 * First flushers are responsible for cascading flushes and
2344 * handling overflow. Non-first flushers can simply return.
2345 */
2346 if (wq->first_flusher != &this_flusher)
2347 return;
2348
2349 mutex_lock(&wq->flush_mutex);
2350
Tejun Heo4ce48b32010-07-02 10:03:51 +02002351 /* we might have raced, check again with mutex held */
2352 if (wq->first_flusher != &this_flusher)
2353 goto out_unlock;
2354
Tejun Heo73f53c42010-06-29 10:07:11 +02002355 wq->first_flusher = NULL;
2356
2357 BUG_ON(!list_empty(&this_flusher.list));
2358 BUG_ON(wq->flush_color != this_flusher.flush_color);
2359
2360 while (true) {
2361 struct wq_flusher *next, *tmp;
2362
2363 /* complete all the flushers sharing the current flush color */
2364 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2365 if (next->flush_color != wq->flush_color)
2366 break;
2367 list_del_init(&next->list);
2368 complete(&next->done);
2369 }
2370
2371 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2372 wq->flush_color != work_next_color(wq->work_color));
2373
2374 /* this flush_color is finished, advance by one */
2375 wq->flush_color = work_next_color(wq->flush_color);
2376
2377 /* one color has been freed, handle overflow queue */
2378 if (!list_empty(&wq->flusher_overflow)) {
2379 /*
2380 * Assign the same color to all overflowed
2381 * flushers, advance work_color and append to
2382 * flusher_queue. This is the start-to-wait
2383 * phase for these overflowed flushers.
2384 */
2385 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2386 tmp->flush_color = wq->work_color;
2387
2388 wq->work_color = work_next_color(wq->work_color);
2389
2390 list_splice_tail_init(&wq->flusher_overflow,
2391 &wq->flusher_queue);
2392 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2393 }
2394
2395 if (list_empty(&wq->flusher_queue)) {
2396 BUG_ON(wq->flush_color != wq->work_color);
2397 break;
2398 }
2399
2400 /*
2401 * Need to flush more colors. Make the next flusher
2402 * the new first flusher and arm cwqs.
2403 */
2404 BUG_ON(wq->flush_color == wq->work_color);
2405 BUG_ON(wq->flush_color != next->flush_color);
2406
2407 list_del_init(&next->list);
2408 wq->first_flusher = next;
2409
2410 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2411 break;
2412
2413 /*
2414 * Meh... this color is already done, clear first
2415 * flusher and repeat cascading.
2416 */
2417 wq->first_flusher = NULL;
2418 }
2419
2420out_unlock:
2421 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
Dave Jonesae90dd52006-06-30 01:40:45 -04002423EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002425/**
2426 * drain_workqueue - drain a workqueue
2427 * @wq: workqueue to drain
2428 *
2429 * Wait until the workqueue becomes empty. While draining is in progress,
2430 * only chain queueing is allowed. IOW, only currently pending or running
2431 * work items on @wq can queue further work items on it. @wq is flushed
2432 * repeatedly until it becomes empty. The number of flushing is detemined
2433 * by the depth of chaining and should be relatively short. Whine if it
2434 * takes too long.
2435 */
2436void drain_workqueue(struct workqueue_struct *wq)
2437{
2438 unsigned int flush_cnt = 0;
2439 unsigned int cpu;
2440
2441 /*
2442 * __queue_work() needs to test whether there are drainers, is much
2443 * hotter than drain_workqueue() and already looks at @wq->flags.
2444 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2445 */
2446 spin_lock(&workqueue_lock);
2447 if (!wq->nr_drainers++)
2448 wq->flags |= WQ_DRAINING;
2449 spin_unlock(&workqueue_lock);
2450reflush:
2451 flush_workqueue(wq);
2452
2453 for_each_cwq_cpu(cpu, wq) {
2454 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002455 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002456
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002457 spin_lock_irq(&cwq->gcwq->lock);
2458 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
2459 spin_unlock_irq(&cwq->gcwq->lock);
2460
2461 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002462 continue;
2463
2464 if (++flush_cnt == 10 ||
2465 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2466 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2467 wq->name, flush_cnt);
2468 goto reflush;
2469 }
2470
2471 spin_lock(&workqueue_lock);
2472 if (!--wq->nr_drainers)
2473 wq->flags &= ~WQ_DRAINING;
2474 spin_unlock(&workqueue_lock);
2475}
2476EXPORT_SYMBOL_GPL(drain_workqueue);
2477
Tejun Heobaf59022010-09-16 10:42:16 +02002478static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2479 bool wait_executing)
2480{
2481 struct worker *worker = NULL;
2482 struct global_cwq *gcwq;
2483 struct cpu_workqueue_struct *cwq;
2484
2485 might_sleep();
2486 gcwq = get_work_gcwq(work);
2487 if (!gcwq)
2488 return false;
2489
2490 spin_lock_irq(&gcwq->lock);
2491 if (!list_empty(&work->entry)) {
2492 /*
2493 * See the comment near try_to_grab_pending()->smp_rmb().
2494 * If it was re-queued to a different gcwq under us, we
2495 * are not going to wait.
2496 */
2497 smp_rmb();
2498 cwq = get_work_cwq(work);
2499 if (unlikely(!cwq || gcwq != cwq->gcwq))
2500 goto already_gone;
2501 } else if (wait_executing) {
2502 worker = find_worker_executing_work(gcwq, work);
2503 if (!worker)
2504 goto already_gone;
2505 cwq = worker->current_cwq;
2506 } else
2507 goto already_gone;
2508
2509 insert_wq_barrier(cwq, barr, work, worker);
2510 spin_unlock_irq(&gcwq->lock);
2511
Tejun Heoe1594892011-01-09 23:32:15 +01002512 /*
2513 * If @max_active is 1 or rescuer is in use, flushing another work
2514 * item on the same workqueue may lead to deadlock. Make sure the
2515 * flusher is not running on the same workqueue by verifying write
2516 * access.
2517 */
2518 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2519 lock_map_acquire(&cwq->wq->lockdep_map);
2520 else
2521 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002522 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002523
Tejun Heobaf59022010-09-16 10:42:16 +02002524 return true;
2525already_gone:
2526 spin_unlock_irq(&gcwq->lock);
2527 return false;
2528}
2529
Oleg Nesterovdb700892008-07-25 01:47:49 -07002530/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002531 * flush_work - wait for a work to finish executing the last queueing instance
2532 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002533 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002534 * Wait until @work has finished execution. This function considers
2535 * only the last queueing instance of @work. If @work has been
2536 * enqueued across different CPUs on a non-reentrant workqueue or on
2537 * multiple workqueues, @work might still be executing on return on
2538 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002539 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002540 * If @work was queued only on a non-reentrant, ordered or unbound
2541 * workqueue, @work is guaranteed to be idle on return if it hasn't
2542 * been requeued since flush started.
2543 *
2544 * RETURNS:
2545 * %true if flush_work() waited for the work to finish execution,
2546 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002547 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002548bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002549{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002550 struct wq_barrier barr;
2551
Tejun Heobaf59022010-09-16 10:42:16 +02002552 if (start_flush_work(work, &barr, true)) {
2553 wait_for_completion(&barr.done);
2554 destroy_work_on_stack(&barr.work);
2555 return true;
2556 } else
2557 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002558}
2559EXPORT_SYMBOL_GPL(flush_work);
2560
Tejun Heo401a8d02010-09-16 10:36:00 +02002561static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2562{
2563 struct wq_barrier barr;
2564 struct worker *worker;
2565
2566 spin_lock_irq(&gcwq->lock);
2567
2568 worker = find_worker_executing_work(gcwq, work);
2569 if (unlikely(worker))
2570 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2571
2572 spin_unlock_irq(&gcwq->lock);
2573
2574 if (unlikely(worker)) {
2575 wait_for_completion(&barr.done);
2576 destroy_work_on_stack(&barr.work);
2577 return true;
2578 } else
2579 return false;
2580}
2581
2582static bool wait_on_work(struct work_struct *work)
2583{
2584 bool ret = false;
2585 int cpu;
2586
2587 might_sleep();
2588
2589 lock_map_acquire(&work->lockdep_map);
2590 lock_map_release(&work->lockdep_map);
2591
2592 for_each_gcwq_cpu(cpu)
2593 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2594 return ret;
2595}
2596
Tejun Heo09383492010-09-16 10:48:29 +02002597/**
2598 * flush_work_sync - wait until a work has finished execution
2599 * @work: the work to flush
2600 *
2601 * Wait until @work has finished execution. On return, it's
2602 * guaranteed that all queueing instances of @work which happened
2603 * before this function is called are finished. In other words, if
2604 * @work hasn't been requeued since this function was called, @work is
2605 * guaranteed to be idle on return.
2606 *
2607 * RETURNS:
2608 * %true if flush_work_sync() waited for the work to finish execution,
2609 * %false if it was already idle.
2610 */
2611bool flush_work_sync(struct work_struct *work)
2612{
2613 struct wq_barrier barr;
2614 bool pending, waited;
2615
2616 /* we'll wait for executions separately, queue barr only if pending */
2617 pending = start_flush_work(work, &barr, false);
2618
2619 /* wait for executions to finish */
2620 waited = wait_on_work(work);
2621
2622 /* wait for the pending one */
2623 if (pending) {
2624 wait_for_completion(&barr.done);
2625 destroy_work_on_stack(&barr.work);
2626 }
2627
2628 return pending || waited;
2629}
2630EXPORT_SYMBOL_GPL(flush_work_sync);
2631
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002632/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002633 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002634 * so this work can't be re-armed in any way.
2635 */
2636static int try_to_grab_pending(struct work_struct *work)
2637{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002638 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002639 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002640
Tejun Heo22df02b2010-06-29 10:07:10 +02002641 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002642 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002643
2644 /*
2645 * The queueing is in progress, or it is already queued. Try to
2646 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2647 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002648 gcwq = get_work_gcwq(work);
2649 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002650 return ret;
2651
Tejun Heo8b03ae32010-06-29 10:07:12 +02002652 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002653 if (!list_empty(&work->entry)) {
2654 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002655 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002656 * In that case we must see the new value after rmb(), see
2657 * insert_work()->wmb().
2658 */
2659 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002660 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002661 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002662
2663 /*
2664 * A delayed work item cannot be grabbed directly
2665 * because it might have linked NO_COLOR work items
2666 * which, if left on the delayed_list, will confuse
2667 * cwq->nr_active management later on and cause
2668 * stall. Make sure the work item is activated
2669 * before grabbing.
2670 */
2671 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2672 cwq_activate_delayed_work(work);
2673
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002674 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002675 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002676 get_work_color(work),
2677 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002678 ret = 1;
2679 }
2680 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002681 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002682
2683 return ret;
2684}
2685
Tejun Heo401a8d02010-09-16 10:36:00 +02002686static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002687 struct timer_list* timer)
2688{
2689 int ret;
2690
2691 do {
2692 ret = (timer && likely(del_timer(timer)));
2693 if (!ret)
2694 ret = try_to_grab_pending(work);
2695 wait_on_work(work);
2696 } while (unlikely(ret < 0));
2697
Tejun Heo7a22ad72010-06-29 10:07:13 +02002698 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002699 return ret;
2700}
2701
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002702/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002703 * cancel_work_sync - cancel a work and wait for it to finish
2704 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002705 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002706 * Cancel @work and wait for its execution to finish. This function
2707 * can be used even if the work re-queues itself or migrates to
2708 * another workqueue. On return from this function, @work is
2709 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002710 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002711 * cancel_work_sync(&delayed_work->work) must not be used for
2712 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002713 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002714 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002715 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002716 *
2717 * RETURNS:
2718 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002720bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002721{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002722 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002723}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002724EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002725
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002726/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2728 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002729 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002730 * Delayed timer is cancelled and the pending work is queued for
2731 * immediate execution. Like flush_work(), this function only
2732 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002733 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002734 * RETURNS:
2735 * %true if flush_work() waited for the work to finish execution,
2736 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002737 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002738bool flush_delayed_work(struct delayed_work *dwork)
2739{
2740 if (del_timer_sync(&dwork->timer))
2741 __queue_work(raw_smp_processor_id(),
2742 get_work_cwq(&dwork->work)->wq, &dwork->work);
2743 return flush_work(&dwork->work);
2744}
2745EXPORT_SYMBOL(flush_delayed_work);
2746
2747/**
Tejun Heo09383492010-09-16 10:48:29 +02002748 * flush_delayed_work_sync - wait for a dwork to finish
2749 * @dwork: the delayed work to flush
2750 *
2751 * Delayed timer is cancelled and the pending work is queued for
2752 * execution immediately. Other than timer handling, its behavior
2753 * is identical to flush_work_sync().
2754 *
2755 * RETURNS:
2756 * %true if flush_work_sync() waited for the work to finish execution,
2757 * %false if it was already idle.
2758 */
2759bool flush_delayed_work_sync(struct delayed_work *dwork)
2760{
2761 if (del_timer_sync(&dwork->timer))
2762 __queue_work(raw_smp_processor_id(),
2763 get_work_cwq(&dwork->work)->wq, &dwork->work);
2764 return flush_work_sync(&dwork->work);
2765}
2766EXPORT_SYMBOL(flush_delayed_work_sync);
2767
2768/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002769 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2770 * @dwork: the delayed work cancel
2771 *
2772 * This is cancel_work_sync() for delayed works.
2773 *
2774 * RETURNS:
2775 * %true if @dwork was pending, %false otherwise.
2776 */
2777bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002778{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002779 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002780}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002781EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002783/**
2784 * schedule_work - put work task in global workqueue
2785 * @work: job to be done
2786 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002787 * Returns zero if @work was already on the kernel-global workqueue and
2788 * non-zero otherwise.
2789 *
2790 * This puts a job in the kernel-global workqueue if it was not already
2791 * queued and leaves it in the same position on the kernel-global
2792 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002793 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002794int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Tejun Heod320c032010-06-29 10:07:14 +02002796 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
Dave Jonesae90dd52006-06-30 01:40:45 -04002798EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Zhang Ruic1a220e2008-07-23 21:28:39 -07002800/*
2801 * schedule_work_on - put work task on a specific cpu
2802 * @cpu: cpu to put the work task on
2803 * @work: job to be done
2804 *
2805 * This puts a job on a specific cpu
2806 */
2807int schedule_work_on(int cpu, struct work_struct *work)
2808{
Tejun Heod320c032010-06-29 10:07:14 +02002809 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002810}
2811EXPORT_SYMBOL(schedule_work_on);
2812
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002813/**
2814 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002815 * @dwork: job to be done
2816 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002817 *
2818 * After waiting for a given time this puts a job in the kernel-global
2819 * workqueue.
2820 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002821int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002822 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Tejun Heod320c032010-06-29 10:07:14 +02002824 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
Dave Jonesae90dd52006-06-30 01:40:45 -04002826EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002828/**
2829 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2830 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002831 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002832 * @delay: number of jiffies to wait
2833 *
2834 * After waiting for a given time this puts a job in the kernel-global
2835 * workqueue on the specified CPU.
2836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002838 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Tejun Heod320c032010-06-29 10:07:14 +02002840 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
Dave Jonesae90dd52006-06-30 01:40:45 -04002842EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Andrew Mortonb6136772006-06-25 05:47:49 -07002844/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002845 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002846 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002848 * schedule_on_each_cpu() executes @func on each online CPU using the
2849 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002850 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002851 *
2852 * RETURNS:
2853 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002854 */
David Howells65f27f32006-11-22 14:55:48 +00002855int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002856{
2857 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002858 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002859
Andrew Mortonb6136772006-06-25 05:47:49 -07002860 works = alloc_percpu(struct work_struct);
2861 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002862 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002863
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002864 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002865
Christoph Lameter15316ba2006-01-08 01:00:43 -08002866 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002867 struct work_struct *work = per_cpu_ptr(works, cpu);
2868
2869 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002870 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002871 }
Tejun Heo93981802009-11-17 14:06:20 -08002872
2873 for_each_online_cpu(cpu)
2874 flush_work(per_cpu_ptr(works, cpu));
2875
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002876 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002877 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002878 return 0;
2879}
2880
Alan Sterneef6a7d2010-02-12 17:39:21 +09002881/**
2882 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2883 *
2884 * Forces execution of the kernel-global workqueue and blocks until its
2885 * completion.
2886 *
2887 * Think twice before calling this function! It's very easy to get into
2888 * trouble if you don't take great care. Either of the following situations
2889 * will lead to deadlock:
2890 *
2891 * One of the work items currently on the workqueue needs to acquire
2892 * a lock held by your code or its caller.
2893 *
2894 * Your code is running in the context of a work routine.
2895 *
2896 * They will be detected by lockdep when they occur, but the first might not
2897 * occur very often. It depends on what work items are on the workqueue and
2898 * what locks they need, which you have no control over.
2899 *
2900 * In most situations flushing the entire workqueue is overkill; you merely
2901 * need to know that a particular work item isn't queued and isn't running.
2902 * In such cases you should use cancel_delayed_work_sync() or
2903 * cancel_work_sync() instead.
2904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905void flush_scheduled_work(void)
2906{
Tejun Heod320c032010-06-29 10:07:14 +02002907 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
Dave Jonesae90dd52006-06-30 01:40:45 -04002909EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002912 * execute_in_process_context - reliably execute the routine with user context
2913 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002914 * @ew: guaranteed storage for the execute work structure (must
2915 * be available when the work executes)
2916 *
2917 * Executes the function immediately if process context is available,
2918 * otherwise schedules the function for delayed execution.
2919 *
2920 * Returns: 0 - function was executed
2921 * 1 - function was scheduled for execution
2922 */
David Howells65f27f32006-11-22 14:55:48 +00002923int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002924{
2925 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002926 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002927 return 0;
2928 }
2929
David Howells65f27f32006-11-22 14:55:48 +00002930 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002931 schedule_work(&ew->work);
2932
2933 return 1;
2934}
2935EXPORT_SYMBOL_GPL(execute_in_process_context);
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937int keventd_up(void)
2938{
Tejun Heod320c032010-06-29 10:07:14 +02002939 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002942static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002944 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002945 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2946 * Make sure that the alignment isn't lower than that of
2947 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002948 */
Tejun Heo0f900042010-06-29 10:07:11 +02002949 const size_t size = sizeof(struct cpu_workqueue_struct);
2950 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2951 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002952
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002953 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002954 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002955 else {
Tejun Heof3421792010-07-02 10:03:51 +02002956 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002957
Tejun Heof3421792010-07-02 10:03:51 +02002958 /*
2959 * Allocate enough room to align cwq and put an extra
2960 * pointer at the end pointing back to the originally
2961 * allocated pointer which will be used for free.
2962 */
2963 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2964 if (ptr) {
2965 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2966 *(void **)(wq->cpu_wq.single + 1) = ptr;
2967 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002968 }
Tejun Heof3421792010-07-02 10:03:51 +02002969
Tejun Heo0415b002011-03-24 18:50:09 +01002970 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002971 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2972 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002973}
2974
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002975static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002976{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002977 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002978 free_percpu(wq->cpu_wq.pcpu);
2979 else if (wq->cpu_wq.single) {
2980 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002981 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002982 }
2983}
2984
Tejun Heof3421792010-07-02 10:03:51 +02002985static int wq_clamp_max_active(int max_active, unsigned int flags,
2986 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002987{
Tejun Heof3421792010-07-02 10:03:51 +02002988 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2989
2990 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002991 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2992 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002993 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002994
Tejun Heof3421792010-07-02 10:03:51 +02002995 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002996}
2997
Tejun Heob196be82012-01-10 15:11:35 -08002998struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002999 unsigned int flags,
3000 int max_active,
3001 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003002 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003003{
Tejun Heob196be82012-01-10 15:11:35 -08003004 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003005 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003006 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003007 size_t namelen;
3008
3009 /* determine namelen, allocate wq and format name */
3010 va_start(args, lock_name);
3011 va_copy(args1, args);
3012 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3013
3014 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3015 if (!wq)
3016 goto err;
3017
3018 vsnprintf(wq->name, namelen, fmt, args1);
3019 va_end(args);
3020 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003021
Tejun Heof3421792010-07-02 10:03:51 +02003022 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003023 * Workqueues which may be used during memory reclaim should
3024 * have a rescuer to guarantee forward progress.
3025 */
3026 if (flags & WQ_MEM_RECLAIM)
3027 flags |= WQ_RESCUER;
3028
3029 /*
Tejun Heof3421792010-07-02 10:03:51 +02003030 * Unbound workqueues aren't concurrency managed and should be
3031 * dispatched to workers immediately.
3032 */
3033 if (flags & WQ_UNBOUND)
3034 flags |= WQ_HIGHPRI;
3035
Tejun Heod320c032010-06-29 10:07:14 +02003036 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003037 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003038
Tejun Heob196be82012-01-10 15:11:35 -08003039 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003040 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003041 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003042 mutex_init(&wq->flush_mutex);
3043 atomic_set(&wq->nr_cwqs_to_flush, 0);
3044 INIT_LIST_HEAD(&wq->flusher_queue);
3045 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003046
Johannes Bergeb13ba82008-01-16 09:51:58 +01003047 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003048 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003049
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003050 if (alloc_cwqs(wq) < 0)
3051 goto err;
3052
Tejun Heof3421792010-07-02 10:03:51 +02003053 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003054 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003055 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003056
Tejun Heo0f900042010-06-29 10:07:11 +02003057 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003058 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003059 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003060 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003061 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003062 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003063 }
3064
Tejun Heoe22bee72010-06-29 10:07:14 +02003065 if (flags & WQ_RESCUER) {
3066 struct worker *rescuer;
3067
Tejun Heof2e005a2010-07-20 15:59:09 +02003068 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003069 goto err;
3070
3071 wq->rescuer = rescuer = alloc_worker();
3072 if (!rescuer)
3073 goto err;
3074
Tejun Heob196be82012-01-10 15:11:35 -08003075 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3076 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003077 if (IS_ERR(rescuer->task))
3078 goto err;
3079
Tejun Heoe22bee72010-06-29 10:07:14 +02003080 rescuer->task->flags |= PF_THREAD_BOUND;
3081 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003082 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003083
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003084 /*
3085 * workqueue_lock protects global freeze state and workqueues
3086 * list. Grab it, set max_active accordingly and add the new
3087 * workqueue to workqueues list.
3088 */
Tejun Heo15376632010-06-29 10:07:11 +02003089 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003090
Tejun Heo58a69cb2011-02-16 09:25:31 +01003091 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003092 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003093 get_cwq(cpu, wq)->max_active = 0;
3094
Tejun Heo15376632010-06-29 10:07:11 +02003095 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003096
Tejun Heo15376632010-06-29 10:07:11 +02003097 spin_unlock(&workqueue_lock);
3098
Oleg Nesterov3af244332007-05-09 02:34:09 -07003099 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003100err:
3101 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003102 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003103 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003104 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003105 kfree(wq);
3106 }
3107 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003108}
Tejun Heod320c032010-06-29 10:07:14 +02003109EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003110
3111/**
3112 * destroy_workqueue - safely terminate a workqueue
3113 * @wq: target workqueue
3114 *
3115 * Safely destroy a workqueue. All work currently pending will be done first.
3116 */
3117void destroy_workqueue(struct workqueue_struct *wq)
3118{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003119 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003120
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003121 /* drain it before proceeding with destruction */
3122 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003123
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003124 /*
3125 * wq list is used to freeze wq, remove from list after
3126 * flushing is complete in case freeze races us.
3127 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003128 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003129 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003130 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003131
Tejun Heoe22bee72010-06-29 10:07:14 +02003132 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003133 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003134 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3135 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003136
Tejun Heo73f53c42010-06-29 10:07:11 +02003137 for (i = 0; i < WORK_NR_COLORS; i++)
3138 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003139 BUG_ON(cwq->nr_active);
3140 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003141 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003142
Tejun Heoe22bee72010-06-29 10:07:14 +02003143 if (wq->flags & WQ_RESCUER) {
3144 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003145 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003146 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003147 }
3148
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003149 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003150 kfree(wq);
3151}
3152EXPORT_SYMBOL_GPL(destroy_workqueue);
3153
Tejun Heodcd989c2010-06-29 10:07:14 +02003154/**
3155 * workqueue_set_max_active - adjust max_active of a workqueue
3156 * @wq: target workqueue
3157 * @max_active: new max_active value.
3158 *
3159 * Set max_active of @wq to @max_active.
3160 *
3161 * CONTEXT:
3162 * Don't call from IRQ context.
3163 */
3164void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3165{
3166 unsigned int cpu;
3167
Tejun Heof3421792010-07-02 10:03:51 +02003168 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003169
3170 spin_lock(&workqueue_lock);
3171
3172 wq->saved_max_active = max_active;
3173
Tejun Heof3421792010-07-02 10:03:51 +02003174 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003175 struct global_cwq *gcwq = get_gcwq(cpu);
3176
3177 spin_lock_irq(&gcwq->lock);
3178
Tejun Heo58a69cb2011-02-16 09:25:31 +01003179 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003180 !(gcwq->flags & GCWQ_FREEZING))
3181 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3182
3183 spin_unlock_irq(&gcwq->lock);
3184 }
3185
3186 spin_unlock(&workqueue_lock);
3187}
3188EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3189
3190/**
3191 * workqueue_congested - test whether a workqueue is congested
3192 * @cpu: CPU in question
3193 * @wq: target workqueue
3194 *
3195 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3196 * no synchronization around this function and the test result is
3197 * unreliable and only useful as advisory hints or for debugging.
3198 *
3199 * RETURNS:
3200 * %true if congested, %false otherwise.
3201 */
3202bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3203{
3204 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3205
3206 return !list_empty(&cwq->delayed_works);
3207}
3208EXPORT_SYMBOL_GPL(workqueue_congested);
3209
3210/**
3211 * work_cpu - return the last known associated cpu for @work
3212 * @work: the work of interest
3213 *
3214 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003215 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003216 */
3217unsigned int work_cpu(struct work_struct *work)
3218{
3219 struct global_cwq *gcwq = get_work_gcwq(work);
3220
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003221 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003222}
3223EXPORT_SYMBOL_GPL(work_cpu);
3224
3225/**
3226 * work_busy - test whether a work is currently pending or running
3227 * @work: the work to be tested
3228 *
3229 * Test whether @work is currently pending or running. There is no
3230 * synchronization around this function and the test result is
3231 * unreliable and only useful as advisory hints or for debugging.
3232 * Especially for reentrant wqs, the pending state might hide the
3233 * running state.
3234 *
3235 * RETURNS:
3236 * OR'd bitmask of WORK_BUSY_* bits.
3237 */
3238unsigned int work_busy(struct work_struct *work)
3239{
3240 struct global_cwq *gcwq = get_work_gcwq(work);
3241 unsigned long flags;
3242 unsigned int ret = 0;
3243
3244 if (!gcwq)
3245 return false;
3246
3247 spin_lock_irqsave(&gcwq->lock, flags);
3248
3249 if (work_pending(work))
3250 ret |= WORK_BUSY_PENDING;
3251 if (find_worker_executing_work(gcwq, work))
3252 ret |= WORK_BUSY_RUNNING;
3253
3254 spin_unlock_irqrestore(&gcwq->lock, flags);
3255
3256 return ret;
3257}
3258EXPORT_SYMBOL_GPL(work_busy);
3259
Tejun Heodb7bccf2010-06-29 10:07:12 +02003260/*
3261 * CPU hotplug.
3262 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003263 * There are two challenges in supporting CPU hotplug. Firstly, there
3264 * are a lot of assumptions on strong associations among work, cwq and
3265 * gcwq which make migrating pending and scheduled works very
3266 * difficult to implement without impacting hot paths. Secondly,
3267 * gcwqs serve mix of short, long and very long running works making
3268 * blocked draining impractical.
3269 *
3270 * This is solved by allowing a gcwq to be detached from CPU, running
3271 * it with unbound (rogue) workers and allowing it to be reattached
3272 * later if the cpu comes back online. A separate thread is created
3273 * to govern a gcwq in such state and is called the trustee of the
3274 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003275 *
3276 * Trustee states and their descriptions.
3277 *
3278 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3279 * new trustee is started with this state.
3280 *
3281 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003282 * assuming the manager role and making all existing
3283 * workers rogue. DOWN_PREPARE waits for trustee to
3284 * enter this state. After reaching IN_CHARGE, trustee
3285 * tries to execute the pending worklist until it's empty
3286 * and the state is set to BUTCHER, or the state is set
3287 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003288 *
3289 * BUTCHER Command state which is set by the cpu callback after
3290 * the cpu has went down. Once this state is set trustee
3291 * knows that there will be no new works on the worklist
3292 * and once the worklist is empty it can proceed to
3293 * killing idle workers.
3294 *
3295 * RELEASE Command state which is set by the cpu callback if the
3296 * cpu down has been canceled or it has come online
3297 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003298 * trying to drain or butcher and clears ROGUE, rebinds
3299 * all remaining workers back to the cpu and releases
3300 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003301 *
3302 * DONE Trustee will enter this state after BUTCHER or RELEASE
3303 * is complete.
3304 *
3305 * trustee CPU draining
3306 * took over down complete
3307 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3308 * | | ^
3309 * | CPU is back online v return workers |
3310 * ----------------> RELEASE --------------
3311 */
3312
3313/**
3314 * trustee_wait_event_timeout - timed event wait for trustee
3315 * @cond: condition to wait for
3316 * @timeout: timeout in jiffies
3317 *
3318 * wait_event_timeout() for trustee to use. Handles locking and
3319 * checks for RELEASE request.
3320 *
3321 * CONTEXT:
3322 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3323 * multiple times. To be used by trustee.
3324 *
3325 * RETURNS:
3326 * Positive indicating left time if @cond is satisfied, 0 if timed
3327 * out, -1 if canceled.
3328 */
3329#define trustee_wait_event_timeout(cond, timeout) ({ \
3330 long __ret = (timeout); \
3331 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3332 __ret) { \
3333 spin_unlock_irq(&gcwq->lock); \
3334 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3335 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3336 __ret); \
3337 spin_lock_irq(&gcwq->lock); \
3338 } \
3339 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3340})
3341
3342/**
3343 * trustee_wait_event - event wait for trustee
3344 * @cond: condition to wait for
3345 *
3346 * wait_event() for trustee to use. Automatically handles locking and
3347 * checks for CANCEL request.
3348 *
3349 * CONTEXT:
3350 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3351 * multiple times. To be used by trustee.
3352 *
3353 * RETURNS:
3354 * 0 if @cond is satisfied, -1 if canceled.
3355 */
3356#define trustee_wait_event(cond) ({ \
3357 long __ret1; \
3358 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3359 __ret1 < 0 ? -1 : 0; \
3360})
3361
3362static int __cpuinit trustee_thread(void *__gcwq)
3363{
3364 struct global_cwq *gcwq = __gcwq;
3365 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003366 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003367 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003368 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003369 int i;
3370
3371 BUG_ON(gcwq->cpu != smp_processor_id());
3372
3373 spin_lock_irq(&gcwq->lock);
3374 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003375 * Claim the manager position and make all workers rogue.
3376 * Trustee must be bound to the target cpu and can't be
3377 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003378 */
3379 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003380 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3381 BUG_ON(rc < 0);
3382
3383 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003384
3385 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003386 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003387
3388 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003389 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003390
3391 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003392 * Call schedule() so that we cross rq->lock and thus can
3393 * guarantee sched callbacks see the rogue flag. This is
3394 * necessary as scheduler callbacks may be invoked from other
3395 * cpus.
3396 */
3397 spin_unlock_irq(&gcwq->lock);
3398 schedule();
3399 spin_lock_irq(&gcwq->lock);
3400
3401 /*
Tejun Heocb444762010-07-02 10:03:50 +02003402 * Sched callbacks are disabled now. Zap nr_running. After
3403 * this, nr_running stays zero and need_more_worker() and
3404 * keep_working() are always true as long as the worklist is
3405 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003406 */
Tejun Heocb444762010-07-02 10:03:50 +02003407 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003408
3409 spin_unlock_irq(&gcwq->lock);
3410 del_timer_sync(&gcwq->idle_timer);
3411 spin_lock_irq(&gcwq->lock);
3412
3413 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003414 * We're now in charge. Notify and proceed to drain. We need
3415 * to keep the gcwq running during the whole CPU down
3416 * procedure as other cpu hotunplug callbacks may need to
3417 * flush currently running tasks.
3418 */
3419 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3420 wake_up_all(&gcwq->trustee_wait);
3421
3422 /*
3423 * The original cpu is in the process of dying and may go away
3424 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003425 * be migrated to other cpus. Try draining any left work. We
3426 * want to get it over with ASAP - spam rescuers, wake up as
3427 * many idlers as necessary and create new ones till the
3428 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003429 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003430 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003431 */
3432 while (gcwq->nr_workers != gcwq->nr_idle ||
3433 gcwq->flags & GCWQ_FREEZING ||
3434 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003435 int nr_works = 0;
3436
3437 list_for_each_entry(work, &gcwq->worklist, entry) {
3438 send_mayday(work);
3439 nr_works++;
3440 }
3441
3442 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3443 if (!nr_works--)
3444 break;
3445 wake_up_process(worker->task);
3446 }
3447
3448 if (need_to_create_worker(gcwq)) {
3449 spin_unlock_irq(&gcwq->lock);
3450 worker = create_worker(gcwq, false);
3451 spin_lock_irq(&gcwq->lock);
3452 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003453 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003454 start_worker(worker);
3455 }
3456 }
3457
Tejun Heodb7bccf2010-06-29 10:07:12 +02003458 /* give a breather */
3459 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3460 break;
3461 }
3462
Tejun Heoe22bee72010-06-29 10:07:14 +02003463 /*
3464 * Either all works have been scheduled and cpu is down, or
3465 * cpu down has already been canceled. Wait for and butcher
3466 * all workers till we're canceled.
3467 */
3468 do {
3469 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3470 while (!list_empty(&gcwq->idle_list))
3471 destroy_worker(list_first_entry(&gcwq->idle_list,
3472 struct worker, entry));
3473 } while (gcwq->nr_workers && rc >= 0);
3474
3475 /*
3476 * At this point, either draining has completed and no worker
3477 * is left, or cpu down has been canceled or the cpu is being
3478 * brought back up. There shouldn't be any idle one left.
3479 * Tell the remaining busy ones to rebind once it finishes the
3480 * currently scheduled works by scheduling the rebind_work.
3481 */
3482 WARN_ON(!list_empty(&gcwq->idle_list));
3483
3484 for_each_busy_worker(worker, i, pos, gcwq) {
3485 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003486 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003487
3488 /*
3489 * Rebind_work may race with future cpu hotplug
3490 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003491 * rebinding is scheduled. The morphing should
3492 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003493 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003494 worker_flags |= WORKER_REBIND;
3495 worker_flags &= ~WORKER_ROGUE;
3496 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003497
3498 /* queue rebind_work, wq doesn't matter, use the default one */
3499 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3500 work_data_bits(rebind_work)))
3501 continue;
3502
3503 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003504 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003505 worker->scheduled.next,
3506 work_color_to_flags(WORK_NO_COLOR));
3507 }
3508
3509 /* relinquish manager role */
3510 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3511
Tejun Heodb7bccf2010-06-29 10:07:12 +02003512 /* notify completion */
3513 gcwq->trustee = NULL;
3514 gcwq->trustee_state = TRUSTEE_DONE;
3515 wake_up_all(&gcwq->trustee_wait);
3516 spin_unlock_irq(&gcwq->lock);
3517 return 0;
3518}
3519
3520/**
3521 * wait_trustee_state - wait for trustee to enter the specified state
3522 * @gcwq: gcwq the trustee of interest belongs to
3523 * @state: target state to wait for
3524 *
3525 * Wait for the trustee to reach @state. DONE is already matched.
3526 *
3527 * CONTEXT:
3528 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3529 * multiple times. To be used by cpu_callback.
3530 */
3531static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003532__releases(&gcwq->lock)
3533__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003534{
3535 if (!(gcwq->trustee_state == state ||
3536 gcwq->trustee_state == TRUSTEE_DONE)) {
3537 spin_unlock_irq(&gcwq->lock);
3538 __wait_event(gcwq->trustee_wait,
3539 gcwq->trustee_state == state ||
3540 gcwq->trustee_state == TRUSTEE_DONE);
3541 spin_lock_irq(&gcwq->lock);
3542 }
3543}
3544
Oleg Nesterov3af244332007-05-09 02:34:09 -07003545static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3546 unsigned long action,
3547 void *hcpu)
3548{
3549 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003550 struct global_cwq *gcwq = get_gcwq(cpu);
3551 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003552 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003553 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003555 action &= ~CPU_TASKS_FROZEN;
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003558 case CPU_DOWN_PREPARE:
3559 new_trustee = kthread_create(trustee_thread, gcwq,
3560 "workqueue_trustee/%d\n", cpu);
3561 if (IS_ERR(new_trustee))
3562 return notifier_from_errno(PTR_ERR(new_trustee));
3563 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003564 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003566 BUG_ON(gcwq->first_idle);
3567 new_worker = create_worker(gcwq, false);
3568 if (!new_worker) {
3569 if (new_trustee)
3570 kthread_stop(new_trustee);
3571 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
3574
Tejun Heodb7bccf2010-06-29 10:07:12 +02003575 /* some are called w/ irq disabled, don't disturb irq status */
3576 spin_lock_irqsave(&gcwq->lock, flags);
3577
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003578 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003579 case CPU_DOWN_PREPARE:
3580 /* initialize trustee and tell it to acquire the gcwq */
3581 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3582 gcwq->trustee = new_trustee;
3583 gcwq->trustee_state = TRUSTEE_START;
3584 wake_up_process(gcwq->trustee);
3585 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003586 /* fall through */
3587 case CPU_UP_PREPARE:
3588 BUG_ON(gcwq->first_idle);
3589 gcwq->first_idle = new_worker;
3590 break;
3591
3592 case CPU_DYING:
3593 /*
3594 * Before this, the trustee and all workers except for
3595 * the ones which are still executing works from
3596 * before the last CPU down must be on the cpu. After
3597 * this, they'll all be diasporas.
3598 */
3599 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003600 break;
3601
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003602 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003603 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003604 /* fall through */
3605 case CPU_UP_CANCELED:
3606 destroy_worker(gcwq->first_idle);
3607 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003608 break;
3609
3610 case CPU_DOWN_FAILED:
3611 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003612 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003613 if (gcwq->trustee_state != TRUSTEE_DONE) {
3614 gcwq->trustee_state = TRUSTEE_RELEASE;
3615 wake_up_process(gcwq->trustee);
3616 wait_trustee_state(gcwq, TRUSTEE_DONE);
3617 }
3618
Tejun Heoe22bee72010-06-29 10:07:14 +02003619 /*
3620 * Trustee is done and there might be no worker left.
3621 * Put the first_idle in and request a real manager to
3622 * take a look.
3623 */
3624 spin_unlock_irq(&gcwq->lock);
3625 kthread_bind(gcwq->first_idle->task, cpu);
3626 spin_lock_irq(&gcwq->lock);
3627 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3628 start_worker(gcwq->first_idle);
3629 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003630 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003631 }
3632
Tejun Heodb7bccf2010-06-29 10:07:12 +02003633 spin_unlock_irqrestore(&gcwq->lock, flags);
3634
Tejun Heo15376632010-06-29 10:07:11 +02003635 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Tejun Heod3b42542012-07-17 12:39:26 -07003638/*
3639 * Workqueues should be brought up before normal priority CPU notifiers.
3640 * This will be registered high priority CPU notifier.
3641 */
3642static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3643 unsigned long action,
3644 void *hcpu)
3645{
3646 switch (action & ~CPU_TASKS_FROZEN) {
3647 case CPU_UP_PREPARE:
3648 case CPU_UP_CANCELED:
3649 case CPU_DOWN_FAILED:
3650 case CPU_ONLINE:
3651 return workqueue_cpu_callback(nfb, action, hcpu);
3652 }
3653 return NOTIFY_OK;
3654}
3655
3656/*
3657 * Workqueues should be brought down after normal priority CPU notifiers.
3658 * This will be registered as low priority CPU notifier.
3659 */
3660static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3661 unsigned long action,
3662 void *hcpu)
3663{
3664 switch (action & ~CPU_TASKS_FROZEN) {
3665 case CPU_DOWN_PREPARE:
3666 case CPU_DYING:
3667 case CPU_POST_DEAD:
3668 return workqueue_cpu_callback(nfb, action, hcpu);
3669 }
3670 return NOTIFY_OK;
3671}
3672
Rusty Russell2d3854a2008-11-05 13:39:10 +11003673#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003674
Rusty Russell2d3854a2008-11-05 13:39:10 +11003675struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003676 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003677 long (*fn)(void *);
3678 void *arg;
3679 long ret;
3680};
3681
Tejun Heofc7da7e2012-09-18 12:48:43 -07003682static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003683{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003684 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3685
Rusty Russell2d3854a2008-11-05 13:39:10 +11003686 wfc->ret = wfc->fn(wfc->arg);
3687}
3688
3689/**
3690 * work_on_cpu - run a function in user context on a particular cpu
3691 * @cpu: the cpu to run on
3692 * @fn: the function to run
3693 * @arg: the function arg
3694 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003695 * This will return the value @fn returns.
3696 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003697 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003698 */
3699long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3700{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003701 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003702
Tejun Heofc7da7e2012-09-18 12:48:43 -07003703 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3704 schedule_work_on(cpu, &wfc.work);
3705 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003706 return wfc.ret;
3707}
3708EXPORT_SYMBOL_GPL(work_on_cpu);
3709#endif /* CONFIG_SMP */
3710
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003711#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303712
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003713/**
3714 * freeze_workqueues_begin - begin freezing workqueues
3715 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003716 * Start freezing workqueues. After this function returns, all freezable
3717 * workqueues will queue new works to their frozen_works list instead of
3718 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003719 *
3720 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003721 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003722 */
3723void freeze_workqueues_begin(void)
3724{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003725 unsigned int cpu;
3726
3727 spin_lock(&workqueue_lock);
3728
3729 BUG_ON(workqueue_freezing);
3730 workqueue_freezing = true;
3731
Tejun Heof3421792010-07-02 10:03:51 +02003732 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003733 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003734 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003735
3736 spin_lock_irq(&gcwq->lock);
3737
Tejun Heodb7bccf2010-06-29 10:07:12 +02003738 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3739 gcwq->flags |= GCWQ_FREEZING;
3740
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003741 list_for_each_entry(wq, &workqueues, list) {
3742 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3743
Tejun Heo58a69cb2011-02-16 09:25:31 +01003744 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003745 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003746 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003747
3748 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003749 }
3750
3751 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003753
3754/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003755 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003756 *
3757 * Check whether freezing is complete. This function must be called
3758 * between freeze_workqueues_begin() and thaw_workqueues().
3759 *
3760 * CONTEXT:
3761 * Grabs and releases workqueue_lock.
3762 *
3763 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003764 * %true if some freezable workqueues are still busy. %false if freezing
3765 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003766 */
3767bool freeze_workqueues_busy(void)
3768{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003769 unsigned int cpu;
3770 bool busy = false;
3771
3772 spin_lock(&workqueue_lock);
3773
3774 BUG_ON(!workqueue_freezing);
3775
Tejun Heof3421792010-07-02 10:03:51 +02003776 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003777 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003778 /*
3779 * nr_active is monotonically decreasing. It's safe
3780 * to peek without lock.
3781 */
3782 list_for_each_entry(wq, &workqueues, list) {
3783 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3784
Tejun Heo58a69cb2011-02-16 09:25:31 +01003785 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003786 continue;
3787
3788 BUG_ON(cwq->nr_active < 0);
3789 if (cwq->nr_active) {
3790 busy = true;
3791 goto out_unlock;
3792 }
3793 }
3794 }
3795out_unlock:
3796 spin_unlock(&workqueue_lock);
3797 return busy;
3798}
3799
3800/**
3801 * thaw_workqueues - thaw workqueues
3802 *
3803 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003804 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003805 *
3806 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003807 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003808 */
3809void thaw_workqueues(void)
3810{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003811 unsigned int cpu;
3812
3813 spin_lock(&workqueue_lock);
3814
3815 if (!workqueue_freezing)
3816 goto out_unlock;
3817
Tejun Heof3421792010-07-02 10:03:51 +02003818 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003819 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003820 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003821
3822 spin_lock_irq(&gcwq->lock);
3823
Tejun Heodb7bccf2010-06-29 10:07:12 +02003824 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3825 gcwq->flags &= ~GCWQ_FREEZING;
3826
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003827 list_for_each_entry(wq, &workqueues, list) {
3828 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3829
Tejun Heo58a69cb2011-02-16 09:25:31 +01003830 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003831 continue;
3832
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003833 /* restore max_active and repopulate worklist */
3834 cwq->max_active = wq->saved_max_active;
3835
3836 while (!list_empty(&cwq->delayed_works) &&
3837 cwq->nr_active < cwq->max_active)
3838 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003839 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003840
Tejun Heoe22bee72010-06-29 10:07:14 +02003841 wake_up_worker(gcwq);
3842
Tejun Heo8b03ae32010-06-29 10:07:12 +02003843 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003844 }
3845
3846 workqueue_freezing = false;
3847out_unlock:
3848 spin_unlock(&workqueue_lock);
3849}
3850#endif /* CONFIG_FREEZER */
3851
Suresh Siddha6ee05782010-07-30 14:57:37 -07003852static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853{
Tejun Heoc34056a2010-06-29 10:07:11 +02003854 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003855 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003856
Tejun Heod3b42542012-07-17 12:39:26 -07003857 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3858 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003859
3860 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003861 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003862 struct global_cwq *gcwq = get_gcwq(cpu);
3863
3864 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003865 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003866 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003867 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003868
Tejun Heoc8e55f32010-06-29 10:07:12 +02003869 INIT_LIST_HEAD(&gcwq->idle_list);
3870 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3871 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3872
Tejun Heoe22bee72010-06-29 10:07:14 +02003873 init_timer_deferrable(&gcwq->idle_timer);
3874 gcwq->idle_timer.function = idle_worker_timeout;
3875 gcwq->idle_timer.data = (unsigned long)gcwq;
3876
3877 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3878 (unsigned long)gcwq);
3879
Tejun Heo8b03ae32010-06-29 10:07:12 +02003880 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003881
3882 gcwq->trustee_state = TRUSTEE_DONE;
3883 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003884 }
3885
Tejun Heoe22bee72010-06-29 10:07:14 +02003886 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003887 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003888 struct global_cwq *gcwq = get_gcwq(cpu);
3889 struct worker *worker;
3890
Tejun Heo477a3c32010-08-31 10:54:35 +02003891 if (cpu != WORK_CPU_UNBOUND)
3892 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003893 worker = create_worker(gcwq, true);
3894 BUG_ON(!worker);
3895 spin_lock_irq(&gcwq->lock);
3896 start_worker(worker);
3897 spin_unlock_irq(&gcwq->lock);
3898 }
3899
Tejun Heod320c032010-06-29 10:07:14 +02003900 system_wq = alloc_workqueue("events", 0, 0);
3901 system_long_wq = alloc_workqueue("events_long", 0, 0);
3902 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003903 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3904 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003905 system_freezable_wq = alloc_workqueue("events_freezable",
3906 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003907 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3908 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003909 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003910 !system_unbound_wq || !system_freezable_wq ||
3911 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003914early_initcall(init_workqueues);