blob: 5561d85508ecd03cde1311bab211753bf4f245c0 [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
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530549 {
550 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200551 return NULL;
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530552 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553}
554
555static struct global_cwq *get_work_gcwq(struct work_struct *work)
556{
Tejun Heoe1201532010-07-22 14:14:25 +0200557 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 unsigned int cpu;
559
Tejun Heoe1201532010-07-22 14:14:25 +0200560 if (data & WORK_STRUCT_CWQ)
561 return ((struct cpu_workqueue_struct *)
562 (data & WORK_STRUCT_WQ_DATA_MASK))->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200563
564 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200565 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566 return NULL;
567
Tejun Heof3421792010-07-02 10:03:51 +0200568 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000570}
571
572/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200573 * Policy functions. These define the policies on how the global
574 * worker pool is managed. Unless noted otherwise, these functions
575 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000576 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200577
Tejun Heo649027d2010-06-29 10:07:14 +0200578static bool __need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000579{
Tejun Heo649027d2010-06-29 10:07:14 +0200580 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
581 gcwq->flags & GCWQ_HIGHPRI_PENDING;
David Howells365970a2006-11-22 14:54:49 +0000582}
583
Tejun Heoe22bee72010-06-29 10:07:14 +0200584/*
585 * Need to wake up a worker? Called from anything but currently
586 * running workers.
587 */
588static bool need_more_worker(struct global_cwq *gcwq)
David Howells365970a2006-11-22 14:54:49 +0000589{
Tejun Heo649027d2010-06-29 10:07:14 +0200590 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
David Howells365970a2006-11-22 14:54:49 +0000591}
592
Tejun Heoe22bee72010-06-29 10:07:14 +0200593/* Can I start working? Called from busy but !running workers. */
594static bool may_start_working(struct global_cwq *gcwq)
595{
596 return gcwq->nr_idle;
597}
598
599/* Do I need to keep working? Called from currently running workers. */
600static bool keep_working(struct global_cwq *gcwq)
601{
602 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
603
Tejun Heo30310042010-10-11 11:51:57 +0200604 return !list_empty(&gcwq->worklist) &&
605 (atomic_read(nr_running) <= 1 ||
606 gcwq->flags & GCWQ_HIGHPRI_PENDING);
Tejun Heoe22bee72010-06-29 10:07:14 +0200607}
608
609/* Do we need a new worker? Called from manager. */
610static bool need_to_create_worker(struct global_cwq *gcwq)
611{
612 return need_more_worker(gcwq) && !may_start_working(gcwq);
613}
614
615/* Do I need to be the manager? */
616static bool need_to_manage_workers(struct global_cwq *gcwq)
617{
618 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
619}
620
621/* Do we have too many workers and should some go away? */
622static bool too_many_workers(struct global_cwq *gcwq)
623{
624 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
625 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
626 int nr_busy = gcwq->nr_workers - nr_idle;
627
628 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
629}
630
631/*
632 * Wake up functions.
633 */
634
Tejun Heo7e116292010-06-29 10:07:13 +0200635/* Return the first worker. Safe with preemption disabled */
636static struct worker *first_worker(struct global_cwq *gcwq)
637{
638 if (unlikely(list_empty(&gcwq->idle_list)))
639 return NULL;
640
641 return list_first_entry(&gcwq->idle_list, struct worker, entry);
642}
643
644/**
645 * wake_up_worker - wake up an idle worker
646 * @gcwq: gcwq to wake worker for
647 *
648 * Wake up the first idle worker of @gcwq.
649 *
650 * CONTEXT:
651 * spin_lock_irq(gcwq->lock).
652 */
653static void wake_up_worker(struct global_cwq *gcwq)
654{
655 struct worker *worker = first_worker(gcwq);
656
657 if (likely(worker))
658 wake_up_process(worker->task);
659}
660
Tejun Heo4690c4a2010-06-29 10:07:10 +0200661/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200662 * wq_worker_waking_up - a worker is waking up
663 * @task: task waking up
664 * @cpu: CPU @task is waking up to
665 *
666 * This function is called during try_to_wake_up() when a worker is
667 * being awoken.
668 *
669 * CONTEXT:
670 * spin_lock_irq(rq->lock)
671 */
672void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
673{
674 struct worker *worker = kthread_data(task);
675
Steven Rostedt2d646722010-12-03 23:12:33 -0500676 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe22bee72010-06-29 10:07:14 +0200677 atomic_inc(get_gcwq_nr_running(cpu));
678}
679
680/**
681 * wq_worker_sleeping - a worker is going to sleep
682 * @task: task going to sleep
683 * @cpu: CPU in question, must be the current CPU number
684 *
685 * This function is called during schedule() when a busy worker is
686 * going to sleep. Worker on the same cpu can be woken up by
687 * returning pointer to its task.
688 *
689 * CONTEXT:
690 * spin_lock_irq(rq->lock)
691 *
692 * RETURNS:
693 * Worker task on @cpu to wake up, %NULL if none.
694 */
695struct task_struct *wq_worker_sleeping(struct task_struct *task,
696 unsigned int cpu)
697{
698 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
699 struct global_cwq *gcwq = get_gcwq(cpu);
700 atomic_t *nr_running = get_gcwq_nr_running(cpu);
701
Steven Rostedt2d646722010-12-03 23:12:33 -0500702 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200703 return NULL;
704
705 /* this can only happen on the local cpu */
706 BUG_ON(cpu != raw_smp_processor_id());
707
708 /*
709 * The counterpart of the following dec_and_test, implied mb,
710 * worklist not empty test sequence is in insert_work().
711 * Please read comment there.
712 *
713 * NOT_RUNNING is clear. This means that trustee is not in
714 * charge and we're running on the local cpu w/ rq lock held
715 * and preemption disabled, which in turn means that none else
716 * could be manipulating idle_list, so dereferencing idle_list
717 * without gcwq lock is safe.
718 */
719 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
720 to_wakeup = first_worker(gcwq);
721 return to_wakeup ? to_wakeup->task : NULL;
722}
723
724/**
725 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200726 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200727 * @flags: flags to set
728 * @wakeup: wakeup an idle worker if necessary
729 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200730 * Set @flags in @worker->flags and adjust nr_running accordingly. If
731 * nr_running becomes zero and @wakeup is %true, an idle worker is
732 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200733 *
Tejun Heocb444762010-07-02 10:03:50 +0200734 * CONTEXT:
735 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200736 */
737static inline void worker_set_flags(struct worker *worker, unsigned int flags,
738 bool wakeup)
739{
Tejun Heoe22bee72010-06-29 10:07:14 +0200740 struct global_cwq *gcwq = worker->gcwq;
741
Tejun Heocb444762010-07-02 10:03:50 +0200742 WARN_ON_ONCE(worker->task != current);
743
Tejun Heoe22bee72010-06-29 10:07:14 +0200744 /*
745 * If transitioning into NOT_RUNNING, adjust nr_running and
746 * wake up an idle worker as necessary if requested by
747 * @wakeup.
748 */
749 if ((flags & WORKER_NOT_RUNNING) &&
750 !(worker->flags & WORKER_NOT_RUNNING)) {
751 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
752
753 if (wakeup) {
754 if (atomic_dec_and_test(nr_running) &&
755 !list_empty(&gcwq->worklist))
756 wake_up_worker(gcwq);
757 } else
758 atomic_dec(nr_running);
759 }
760
Tejun Heod302f012010-06-29 10:07:13 +0200761 worker->flags |= flags;
762}
763
764/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200765 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200766 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200767 * @flags: flags to clear
768 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200769 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200770 *
Tejun Heocb444762010-07-02 10:03:50 +0200771 * CONTEXT:
772 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200773 */
774static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
775{
Tejun Heoe22bee72010-06-29 10:07:14 +0200776 struct global_cwq *gcwq = worker->gcwq;
777 unsigned int oflags = worker->flags;
778
Tejun Heocb444762010-07-02 10:03:50 +0200779 WARN_ON_ONCE(worker->task != current);
780
Tejun Heod302f012010-06-29 10:07:13 +0200781 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200782
Tejun Heo42c025f2011-01-11 15:58:49 +0100783 /*
784 * If transitioning out of NOT_RUNNING, increment nr_running. Note
785 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
786 * of multiple flags, not a single flag.
787 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200788 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
789 if (!(worker->flags & WORKER_NOT_RUNNING))
790 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
Tejun Heod302f012010-06-29 10:07:13 +0200791}
792
793/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200794 * busy_worker_head - return the busy hash head for a work
795 * @gcwq: gcwq of interest
796 * @work: work to be hashed
797 *
798 * Return hash head of @gcwq for @work.
799 *
800 * CONTEXT:
801 * spin_lock_irq(gcwq->lock).
802 *
803 * RETURNS:
804 * Pointer to the hash head.
805 */
806static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
807 struct work_struct *work)
808{
809 const int base_shift = ilog2(sizeof(struct work_struct));
810 unsigned long v = (unsigned long)work;
811
812 /* simple shift and fold hash, do we need something better? */
813 v >>= base_shift;
814 v += v >> BUSY_WORKER_HASH_ORDER;
815 v &= BUSY_WORKER_HASH_MASK;
816
817 return &gcwq->busy_hash[v];
818}
819
820/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200821 * __find_worker_executing_work - find worker which is executing a work
822 * @gcwq: gcwq of interest
823 * @bwh: hash head as returned by busy_worker_head()
824 * @work: work to find worker for
825 *
826 * Find a worker which is executing @work on @gcwq. @bwh should be
827 * the hash head obtained by calling busy_worker_head() with the same
828 * work.
829 *
830 * CONTEXT:
831 * spin_lock_irq(gcwq->lock).
832 *
833 * RETURNS:
834 * Pointer to worker which is executing @work if found, NULL
835 * otherwise.
836 */
837static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
838 struct hlist_head *bwh,
839 struct work_struct *work)
840{
841 struct worker *worker;
842 struct hlist_node *tmp;
843
844 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800845 if (worker->current_work == work &&
846 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200847 return worker;
848 return NULL;
849}
850
851/**
852 * find_worker_executing_work - find worker which is executing a work
853 * @gcwq: gcwq of interest
854 * @work: work to find worker for
855 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800856 * Find a worker which is executing @work on @gcwq by searching
857 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
858 * to match, its current execution should match the address of @work and
859 * its work function. This is to avoid unwanted dependency between
860 * unrelated work executions through a work item being recycled while still
861 * being executed.
862 *
863 * This is a bit tricky. A work item may be freed once its execution
864 * starts and nothing prevents the freed area from being recycled for
865 * another work item. If the same work item address ends up being reused
866 * before the original execution finishes, workqueue will identify the
867 * recycled work item as currently executing and make it wait until the
868 * current execution finishes, introducing an unwanted dependency.
869 *
870 * This function checks the work item address, work function and workqueue
871 * to avoid false positives. Note that this isn't complete as one may
872 * construct a work function which can introduce dependency onto itself
873 * through a recycled work item. Well, if somebody wants to shoot oneself
874 * in the foot that badly, there's only so much we can do, and if such
875 * deadlock actually occurs, it should be easy to locate the culprit work
876 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200877 *
878 * CONTEXT:
879 * spin_lock_irq(gcwq->lock).
880 *
881 * RETURNS:
882 * Pointer to worker which is executing @work if found, NULL
883 * otherwise.
884 */
885static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
886 struct work_struct *work)
887{
888 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
889 work);
890}
891
892/**
Tejun Heo649027d2010-06-29 10:07:14 +0200893 * gcwq_determine_ins_pos - find insertion position
894 * @gcwq: gcwq of interest
895 * @cwq: cwq a work is being queued for
896 *
897 * A work for @cwq is about to be queued on @gcwq, determine insertion
898 * position for the work. If @cwq is for HIGHPRI wq, the work is
899 * queued at the head of the queue but in FIFO order with respect to
900 * other HIGHPRI works; otherwise, at the end of the queue. This
901 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
902 * there are HIGHPRI works pending.
903 *
904 * CONTEXT:
905 * spin_lock_irq(gcwq->lock).
906 *
907 * RETURNS:
908 * Pointer to inserstion position.
909 */
910static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
911 struct cpu_workqueue_struct *cwq)
912{
913 struct work_struct *twork;
914
915 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
916 return &gcwq->worklist;
917
918 list_for_each_entry(twork, &gcwq->worklist, entry) {
919 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
920
921 if (!(tcwq->wq->flags & WQ_HIGHPRI))
922 break;
923 }
924
925 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
926 return &twork->entry;
927}
928
929/**
Tejun Heo7e116292010-06-29 10:07:13 +0200930 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200931 * @cwq: cwq @work belongs to
932 * @work: work to insert
933 * @head: insertion point
934 * @extra_flags: extra WORK_STRUCT_* flags to set
935 *
Tejun Heo7e116292010-06-29 10:07:13 +0200936 * Insert @work which belongs to @cwq into @gcwq after @head.
937 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200938 *
939 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200940 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200941 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700942static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200943 struct work_struct *work, struct list_head *head,
944 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700945{
Tejun Heoe22bee72010-06-29 10:07:14 +0200946 struct global_cwq *gcwq = cwq->gcwq;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100947
Tejun Heo4690c4a2010-06-29 10:07:10 +0200948 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200949 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200950
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700951 /*
952 * Ensure that we get the right work->data if we see the
953 * result of list_add() below, see try_to_grab_pending().
954 */
955 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200956
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700957 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200958
959 /*
960 * Ensure either worker_sched_deactivated() sees the above
961 * list_add_tail() or we see zero nr_running to avoid workers
962 * lying around lazily while there are works to be processed.
963 */
964 smp_mb();
965
Tejun Heo649027d2010-06-29 10:07:14 +0200966 if (__need_more_worker(gcwq))
Tejun Heoe22bee72010-06-29 10:07:14 +0200967 wake_up_worker(gcwq);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700968}
969
Tejun Heoc8efcc22010-12-20 19:32:04 +0100970/*
971 * Test whether @work is being queued from another work executing on the
972 * same workqueue. This is rather expensive and should only be used from
973 * cold paths.
974 */
975static bool is_chained_work(struct workqueue_struct *wq)
976{
977 unsigned long flags;
978 unsigned int cpu;
979
980 for_each_gcwq_cpu(cpu) {
981 struct global_cwq *gcwq = get_gcwq(cpu);
982 struct worker *worker;
983 struct hlist_node *pos;
984 int i;
985
986 spin_lock_irqsave(&gcwq->lock, flags);
987 for_each_busy_worker(worker, i, pos, gcwq) {
988 if (worker->task != current)
989 continue;
990 spin_unlock_irqrestore(&gcwq->lock, flags);
991 /*
992 * I'm @worker, no locking necessary. See if @work
993 * is headed to the same workqueue.
994 */
995 return worker->current_cwq->wq == wq;
996 }
997 spin_unlock_irqrestore(&gcwq->lock, flags);
998 }
999 return false;
1000}
1001
Tejun Heo4690c4a2010-06-29 10:07:10 +02001002static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct work_struct *work)
1004{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001005 struct global_cwq *gcwq;
1006 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001007 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001008 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned long flags;
1010
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001011 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001012
Tejun Heoc8efcc22010-12-20 19:32:04 +01001013 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001014 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001015 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001016 return;
1017
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001018 /* determine gcwq to use */
1019 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001020 struct global_cwq *last_gcwq;
1021
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001022 if (unlikely(cpu == WORK_CPU_UNBOUND))
1023 cpu = raw_smp_processor_id();
1024
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001025 /*
1026 * It's multi cpu. If @wq is non-reentrant and @work
1027 * was previously on a different cpu, it might still
1028 * be running there, in which case the work needs to
1029 * be queued on that cpu to guarantee non-reentrance.
1030 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001031 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001032 if (wq->flags & WQ_NON_REENTRANT &&
1033 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1034 struct worker *worker;
1035
1036 spin_lock_irqsave(&last_gcwq->lock, flags);
1037
1038 worker = find_worker_executing_work(last_gcwq, work);
1039
1040 if (worker && worker->current_cwq->wq == wq)
1041 gcwq = last_gcwq;
1042 else {
1043 /* meh... not running there, queue here */
1044 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1045 spin_lock_irqsave(&gcwq->lock, flags);
1046 }
1047 } else
1048 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001049 } else {
1050 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1051 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001052 }
1053
1054 /* gcwq determined, get cwq and queue */
1055 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001056 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001057
Tejun Heo4690c4a2010-06-29 10:07:10 +02001058 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001059
Tejun Heo73f53c42010-06-29 10:07:11 +02001060 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001061 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062
1063 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001064 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001065 cwq->nr_active++;
Tejun Heo649027d2010-06-29 10:07:14 +02001066 worklist = gcwq_determine_ins_pos(gcwq, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001067 } else {
1068 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001069 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001070 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001071
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001072 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001073
Tejun Heo8b03ae32010-06-29 10:07:12 +02001074 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001077/**
1078 * queue_work - queue work on a workqueue
1079 * @wq: workqueue to use
1080 * @work: work to queue
1081 *
Alan Stern057647f2006-10-28 10:38:58 -07001082 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001084 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1085 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001087int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001089 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001091 ret = queue_work_on(get_cpu(), wq, work);
1092 put_cpu();
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return ret;
1095}
Dave Jonesae90dd52006-06-30 01:40:45 -04001096EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Zhang Ruic1a220e2008-07-23 21:28:39 -07001098/**
1099 * queue_work_on - queue work on specific cpu
1100 * @cpu: CPU number to execute work on
1101 * @wq: workqueue to use
1102 * @work: work to queue
1103 *
1104 * Returns 0 if @work was already on a queue, non-zero otherwise.
1105 *
1106 * We queue the work to a specific CPU, the caller must ensure it
1107 * can't go away.
1108 */
1109int
1110queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1111{
1112 int ret = 0;
1113
Tejun Heo22df02b2010-06-29 10:07:10 +02001114 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001115 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001116 ret = 1;
1117 }
1118 return ret;
1119}
1120EXPORT_SYMBOL_GPL(queue_work_on);
1121
Li Zefan6d141c32008-02-08 04:21:09 -08001122static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
David Howells52bad642006-11-22 14:54:01 +00001124 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001125 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Srinivasarao Pb6e586c2013-09-18 14:33:45 +05301127 if (cwq != NULL)
1128 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001131/**
1132 * queue_delayed_work - queue work on a workqueue after delay
1133 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001134 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001135 * @delay: number of jiffies to wait before queueing
1136 *
Alan Stern057647f2006-10-28 10:38:58 -07001137 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001138 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001139int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001140 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
David Howells52bad642006-11-22 14:54:01 +00001142 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001143 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001145 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
Dave Jonesae90dd52006-06-30 01:40:45 -04001147EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001149/**
1150 * queue_delayed_work_on - queue work on specific CPU after delay
1151 * @cpu: CPU number to execute work on
1152 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001153 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001154 * @delay: number of jiffies to wait before queueing
1155 *
Alan Stern057647f2006-10-28 10:38:58 -07001156 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001157 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001158int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001159 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001160{
1161 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001162 struct timer_list *timer = &dwork->timer;
1163 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001164
Tejun Heo22df02b2010-06-29 10:07:10 +02001165 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001166 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001167
Tejun Heo4afca922012-12-04 07:40:39 -08001168 WARN_ON_ONCE(timer_pending(timer));
1169 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001170
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001171 timer_stats_timer_set_start_info(&dwork->timer);
1172
Tejun Heo7a22ad72010-06-29 10:07:13 +02001173 /*
1174 * This stores cwq for the moment, for the timer_fn.
1175 * Note that the work's gcwq is preserved to allow
1176 * reentrance detection for delayed works.
1177 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001178 if (!(wq->flags & WQ_UNBOUND)) {
1179 struct global_cwq *gcwq = get_work_gcwq(work);
1180
1181 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1182 lcpu = gcwq->cpu;
1183 else
1184 lcpu = raw_smp_processor_id();
1185 } else
1186 lcpu = WORK_CPU_UNBOUND;
1187
Tejun Heo7a22ad72010-06-29 10:07:13 +02001188 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001189
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001190 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001191 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001192 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001193
1194 if (unlikely(cpu >= 0))
1195 add_timer_on(timer, cpu);
1196 else
1197 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001198 ret = 1;
1199 }
1200 return ret;
1201}
Dave Jonesae90dd52006-06-30 01:40:45 -04001202EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tejun Heoc8e55f32010-06-29 10:07:12 +02001204/**
1205 * worker_enter_idle - enter idle state
1206 * @worker: worker which is entering idle state
1207 *
1208 * @worker is entering idle state. Update stats and idle timer if
1209 * necessary.
1210 *
1211 * LOCKING:
1212 * spin_lock_irq(gcwq->lock).
1213 */
1214static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001216 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Tejun Heoc8e55f32010-06-29 10:07:12 +02001218 BUG_ON(worker->flags & WORKER_IDLE);
1219 BUG_ON(!list_empty(&worker->entry) &&
1220 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Tejun Heocb444762010-07-02 10:03:50 +02001222 /* can't use worker_set_flags(), also called from start_worker() */
1223 worker->flags |= WORKER_IDLE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001224 gcwq->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001225 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001226
Tejun Heoc8e55f32010-06-29 10:07:12 +02001227 /* idle_list is LIFO */
1228 list_add(&worker->entry, &gcwq->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001229
Tejun Heoe22bee72010-06-29 10:07:14 +02001230 if (likely(!(worker->flags & WORKER_ROGUE))) {
1231 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1232 mod_timer(&gcwq->idle_timer,
1233 jiffies + IDLE_WORKER_TIMEOUT);
1234 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001235 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001236
Tejun Heo24312d32012-05-14 15:04:50 -07001237 /*
1238 * Sanity check nr_running. Because trustee releases gcwq->lock
1239 * between setting %WORKER_ROGUE and zapping nr_running, the
1240 * warning may trigger spuriously. Check iff trustee is idle.
1241 */
1242 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
1243 gcwq->nr_workers == gcwq->nr_idle &&
Tejun Heocb444762010-07-02 10:03:50 +02001244 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Tejun Heoc8e55f32010-06-29 10:07:12 +02001247/**
1248 * worker_leave_idle - leave idle state
1249 * @worker: worker which is leaving idle state
1250 *
1251 * @worker is leaving idle state. Update stats.
1252 *
1253 * LOCKING:
1254 * spin_lock_irq(gcwq->lock).
1255 */
1256static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Tejun Heoc8e55f32010-06-29 10:07:12 +02001258 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Tejun Heoc8e55f32010-06-29 10:07:12 +02001260 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001261 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001262 gcwq->nr_idle--;
1263 list_del_init(&worker->entry);
1264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Tejun Heoe22bee72010-06-29 10:07:14 +02001266/**
1267 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1268 * @worker: self
1269 *
1270 * Works which are scheduled while the cpu is online must at least be
1271 * scheduled to a worker which is bound to the cpu so that if they are
1272 * flushed from cpu callbacks while cpu is going down, they are
1273 * guaranteed to execute on the cpu.
1274 *
1275 * This function is to be used by rogue workers and rescuers to bind
1276 * themselves to the target cpu and may race with cpu going down or
1277 * coming online. kthread_bind() can't be used because it may put the
1278 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1279 * verbatim as it's best effort and blocking and gcwq may be
1280 * [dis]associated in the meantime.
1281 *
1282 * This function tries set_cpus_allowed() and locks gcwq and verifies
1283 * the binding against GCWQ_DISASSOCIATED which is set during
1284 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1285 * idle state or fetches works without dropping lock, it can guarantee
1286 * the scheduling requirement described in the first paragraph.
1287 *
1288 * CONTEXT:
1289 * Might sleep. Called without any lock but returns with gcwq->lock
1290 * held.
1291 *
1292 * RETURNS:
1293 * %true if the associated gcwq is online (@worker is successfully
1294 * bound), %false if offline.
1295 */
1296static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001297__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001298{
1299 struct global_cwq *gcwq = worker->gcwq;
1300 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Tejun Heoe22bee72010-06-29 10:07:14 +02001302 while (true) {
1303 /*
1304 * The following call may fail, succeed or succeed
1305 * without actually migrating the task to the cpu if
1306 * it races with cpu hotunplug operation. Verify
1307 * against GCWQ_DISASSOCIATED.
1308 */
Tejun Heof3421792010-07-02 10:03:51 +02001309 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1310 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001311
Tejun Heoe22bee72010-06-29 10:07:14 +02001312 spin_lock_irq(&gcwq->lock);
1313 if (gcwq->flags & GCWQ_DISASSOCIATED)
1314 return false;
1315 if (task_cpu(task) == gcwq->cpu &&
1316 cpumask_equal(&current->cpus_allowed,
1317 get_cpu_mask(gcwq->cpu)))
1318 return true;
1319 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001320
Tejun Heo5035b202011-04-29 18:08:37 +02001321 /*
1322 * We've raced with CPU hot[un]plug. Give it a breather
1323 * and retry migration. cond_resched() is required here;
1324 * otherwise, we might deadlock against cpu_stop trying to
1325 * bring down the CPU on non-preemptive kernel.
1326 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001327 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001328 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001329 }
1330}
1331
1332/*
1333 * Function for worker->rebind_work used to rebind rogue busy workers
1334 * to the associated cpu which is coming back online. This is
1335 * scheduled by cpu up but can race with other cpu hotplug operations
1336 * and may be executed twice without intervening cpu down.
1337 */
1338static void worker_rebind_fn(struct work_struct *work)
1339{
1340 struct worker *worker = container_of(work, struct worker, rebind_work);
1341 struct global_cwq *gcwq = worker->gcwq;
1342
1343 if (worker_maybe_bind_and_lock(worker))
1344 worker_clr_flags(worker, WORKER_REBIND);
1345
1346 spin_unlock_irq(&gcwq->lock);
1347}
1348
Tejun Heoc34056a2010-06-29 10:07:11 +02001349static struct worker *alloc_worker(void)
1350{
1351 struct worker *worker;
1352
1353 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001354 if (worker) {
1355 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001356 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001357 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1358 /* on creation a worker is in !idle && prep state */
1359 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001360 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001361 return worker;
1362}
1363
1364/**
1365 * create_worker - create a new workqueue worker
Tejun Heo7e116292010-06-29 10:07:13 +02001366 * @gcwq: gcwq the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001367 * @bind: whether to set affinity to @cpu or not
1368 *
Tejun Heo7e116292010-06-29 10:07:13 +02001369 * Create a new worker which is bound to @gcwq. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001370 * can be started by calling start_worker() or destroyed using
1371 * destroy_worker().
1372 *
1373 * CONTEXT:
1374 * Might sleep. Does GFP_KERNEL allocations.
1375 *
1376 * RETURNS:
1377 * Pointer to the newly created worker.
1378 */
Tejun Heo7e116292010-06-29 10:07:13 +02001379static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001380{
Tejun Heof3421792010-07-02 10:03:51 +02001381 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001382 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001383 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001384
Tejun Heo8b03ae32010-06-29 10:07:12 +02001385 spin_lock_irq(&gcwq->lock);
1386 while (ida_get_new(&gcwq->worker_ida, &id)) {
1387 spin_unlock_irq(&gcwq->lock);
1388 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001389 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001390 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001391 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001392 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001393
1394 worker = alloc_worker();
1395 if (!worker)
1396 goto fail;
1397
Tejun Heo8b03ae32010-06-29 10:07:12 +02001398 worker->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001399 worker->id = id;
1400
Tejun Heof3421792010-07-02 10:03:51 +02001401 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001402 worker->task = kthread_create_on_node(worker_thread,
1403 worker,
1404 cpu_to_node(gcwq->cpu),
1405 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001406 else
1407 worker->task = kthread_create(worker_thread, worker,
1408 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001409 if (IS_ERR(worker->task))
1410 goto fail;
1411
Tejun Heodb7bccf2010-06-29 10:07:12 +02001412 /*
1413 * A rogue worker will become a regular one if CPU comes
1414 * online later on. Make sure every worker has
1415 * PF_THREAD_BOUND set.
1416 */
Tejun Heof3421792010-07-02 10:03:51 +02001417 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001418 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001419 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001420 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001421 if (on_unbound_cpu)
1422 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001424
Tejun Heoc34056a2010-06-29 10:07:11 +02001425 return worker;
1426fail:
1427 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001428 spin_lock_irq(&gcwq->lock);
1429 ida_remove(&gcwq->worker_ida, id);
1430 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001431 }
1432 kfree(worker);
1433 return NULL;
1434}
1435
1436/**
1437 * start_worker - start a newly created worker
1438 * @worker: worker to start
1439 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001440 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001441 *
1442 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001443 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001444 */
1445static void start_worker(struct worker *worker)
1446{
Tejun Heocb444762010-07-02 10:03:50 +02001447 worker->flags |= WORKER_STARTED;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001448 worker->gcwq->nr_workers++;
1449 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001450 wake_up_process(worker->task);
1451}
1452
1453/**
1454 * destroy_worker - destroy a workqueue worker
1455 * @worker: worker to be destroyed
1456 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001457 * Destroy @worker and adjust @gcwq stats accordingly.
1458 *
1459 * CONTEXT:
1460 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001461 */
1462static void destroy_worker(struct worker *worker)
1463{
Tejun Heo8b03ae32010-06-29 10:07:12 +02001464 struct global_cwq *gcwq = worker->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001465 int id = worker->id;
1466
1467 /* sanity check frenzy */
1468 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001469 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001470
Tejun Heoc8e55f32010-06-29 10:07:12 +02001471 if (worker->flags & WORKER_STARTED)
1472 gcwq->nr_workers--;
1473 if (worker->flags & WORKER_IDLE)
1474 gcwq->nr_idle--;
1475
1476 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001477 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001478
1479 spin_unlock_irq(&gcwq->lock);
1480
Tejun Heoc34056a2010-06-29 10:07:11 +02001481 kthread_stop(worker->task);
1482 kfree(worker);
1483
Tejun Heo8b03ae32010-06-29 10:07:12 +02001484 spin_lock_irq(&gcwq->lock);
1485 ida_remove(&gcwq->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001486}
1487
Tejun Heoe22bee72010-06-29 10:07:14 +02001488static void idle_worker_timeout(unsigned long __gcwq)
1489{
1490 struct global_cwq *gcwq = (void *)__gcwq;
1491
1492 spin_lock_irq(&gcwq->lock);
1493
1494 if (too_many_workers(gcwq)) {
1495 struct worker *worker;
1496 unsigned long expires;
1497
1498 /* idle_list is kept in LIFO order, check the last one */
1499 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1500 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1501
1502 if (time_before(jiffies, expires))
1503 mod_timer(&gcwq->idle_timer, expires);
1504 else {
1505 /* it's been idle for too long, wake up manager */
1506 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1507 wake_up_worker(gcwq);
1508 }
1509 }
1510
1511 spin_unlock_irq(&gcwq->lock);
1512}
1513
1514static bool send_mayday(struct work_struct *work)
1515{
1516 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1517 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001518 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001519
1520 if (!(wq->flags & WQ_RESCUER))
1521 return false;
1522
1523 /* mayday mayday mayday */
Tejun Heof3421792010-07-02 10:03:51 +02001524 cpu = cwq->gcwq->cpu;
1525 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1526 if (cpu == WORK_CPU_UNBOUND)
1527 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001528 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001529 wake_up_process(wq->rescuer->task);
1530 return true;
1531}
1532
1533static void gcwq_mayday_timeout(unsigned long __gcwq)
1534{
1535 struct global_cwq *gcwq = (void *)__gcwq;
1536 struct work_struct *work;
1537
1538 spin_lock_irq(&gcwq->lock);
1539
1540 if (need_to_create_worker(gcwq)) {
1541 /*
1542 * We've been trying to create a new worker but
1543 * haven't been successful. We might be hitting an
1544 * allocation deadlock. Send distress signals to
1545 * rescuers.
1546 */
1547 list_for_each_entry(work, &gcwq->worklist, entry)
1548 send_mayday(work);
1549 }
1550
1551 spin_unlock_irq(&gcwq->lock);
1552
1553 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1554}
1555
1556/**
1557 * maybe_create_worker - create a new worker if necessary
1558 * @gcwq: gcwq to create a new worker for
1559 *
1560 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1561 * have at least one idle worker on return from this function. If
1562 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1563 * sent to all rescuers with works scheduled on @gcwq to resolve
1564 * possible allocation deadlock.
1565 *
1566 * On return, need_to_create_worker() is guaranteed to be false and
1567 * may_start_working() true.
1568 *
1569 * LOCKING:
1570 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1571 * multiple times. Does GFP_KERNEL allocations. Called only from
1572 * manager.
1573 *
1574 * RETURNS:
1575 * false if no action was taken and gcwq->lock stayed locked, true
1576 * otherwise.
1577 */
1578static bool maybe_create_worker(struct global_cwq *gcwq)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001579__releases(&gcwq->lock)
1580__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001581{
1582 if (!need_to_create_worker(gcwq))
1583 return false;
1584restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001585 spin_unlock_irq(&gcwq->lock);
1586
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1588 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1589
1590 while (true) {
1591 struct worker *worker;
1592
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 worker = create_worker(gcwq, true);
1594 if (worker) {
1595 del_timer_sync(&gcwq->mayday_timer);
1596 spin_lock_irq(&gcwq->lock);
1597 start_worker(worker);
1598 BUG_ON(need_to_create_worker(gcwq));
1599 return true;
1600 }
1601
1602 if (!need_to_create_worker(gcwq))
1603 break;
1604
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 __set_current_state(TASK_INTERRUPTIBLE);
1606 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001607
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 if (!need_to_create_worker(gcwq))
1609 break;
1610 }
1611
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 del_timer_sync(&gcwq->mayday_timer);
1613 spin_lock_irq(&gcwq->lock);
1614 if (need_to_create_worker(gcwq))
1615 goto restart;
1616 return true;
1617}
1618
1619/**
1620 * maybe_destroy_worker - destroy workers which have been idle for a while
1621 * @gcwq: gcwq to destroy workers for
1622 *
1623 * Destroy @gcwq workers which have been idle for longer than
1624 * IDLE_WORKER_TIMEOUT.
1625 *
1626 * LOCKING:
1627 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1628 * multiple times. Called only from manager.
1629 *
1630 * RETURNS:
1631 * false if no action was taken and gcwq->lock stayed locked, true
1632 * otherwise.
1633 */
1634static bool maybe_destroy_workers(struct global_cwq *gcwq)
1635{
1636 bool ret = false;
1637
1638 while (too_many_workers(gcwq)) {
1639 struct worker *worker;
1640 unsigned long expires;
1641
1642 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1643 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1644
1645 if (time_before(jiffies, expires)) {
1646 mod_timer(&gcwq->idle_timer, expires);
1647 break;
1648 }
1649
1650 destroy_worker(worker);
1651 ret = true;
1652 }
1653
1654 return ret;
1655}
1656
1657/**
1658 * manage_workers - manage worker pool
1659 * @worker: self
1660 *
1661 * Assume the manager role and manage gcwq worker pool @worker belongs
1662 * to. At any given time, there can be only zero or one manager per
1663 * gcwq. The exclusion is handled automatically by this function.
1664 *
1665 * The caller can safely start processing works on false return. On
1666 * true return, it's guaranteed that need_to_create_worker() is false
1667 * and may_start_working() is true.
1668 *
1669 * CONTEXT:
1670 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1671 * multiple times. Does GFP_KERNEL allocations.
1672 *
1673 * RETURNS:
1674 * false if no action was taken and gcwq->lock stayed locked, true if
1675 * some action was taken.
1676 */
1677static bool manage_workers(struct worker *worker)
1678{
1679 struct global_cwq *gcwq = worker->gcwq;
1680 bool ret = false;
1681
1682 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1683 return ret;
1684
1685 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1686 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1687
1688 /*
1689 * Destroy and then create so that may_start_working() is true
1690 * on return.
1691 */
1692 ret |= maybe_destroy_workers(gcwq);
1693 ret |= maybe_create_worker(gcwq);
1694
1695 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1696
1697 /*
1698 * The trustee might be waiting to take over the manager
1699 * position, tell it we're done.
1700 */
1701 if (unlikely(gcwq->trustee))
1702 wake_up_all(&gcwq->trustee_wait);
1703
1704 return ret;
1705}
1706
Tejun Heoa62428c2010-06-29 10:07:10 +02001707/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001708 * move_linked_works - move linked works to a list
1709 * @work: start of series of works to be scheduled
1710 * @head: target list to append @work to
1711 * @nextp: out paramter for nested worklist walking
1712 *
1713 * Schedule linked works starting from @work to @head. Work series to
1714 * be scheduled starts at @work and includes any consecutive work with
1715 * WORK_STRUCT_LINKED set in its predecessor.
1716 *
1717 * If @nextp is not NULL, it's updated to point to the next work of
1718 * the last scheduled work. This allows move_linked_works() to be
1719 * nested inside outer list_for_each_entry_safe().
1720 *
1721 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001722 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001723 */
1724static void move_linked_works(struct work_struct *work, struct list_head *head,
1725 struct work_struct **nextp)
1726{
1727 struct work_struct *n;
1728
1729 /*
1730 * Linked worklist will always end before the end of the list,
1731 * use NULL for list head.
1732 */
1733 list_for_each_entry_safe_from(work, n, NULL, entry) {
1734 list_move_tail(&work->entry, head);
1735 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1736 break;
1737 }
1738
1739 /*
1740 * If we're already inside safe list traversal and have moved
1741 * multiple works to the scheduled queue, the next position
1742 * needs to be updated.
1743 */
1744 if (nextp)
1745 *nextp = n;
1746}
1747
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001748static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001749{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001750 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001751 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001752
Tejun Heocdadf002010-10-05 10:49:55 +02001753 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001754 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001755 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001756 cwq->nr_active++;
1757}
1758
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001759static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1760{
1761 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1762 struct work_struct, entry);
1763
1764 cwq_activate_delayed_work(work);
1765}
1766
Tejun Heoaffee4b2010-06-29 10:07:12 +02001767/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001768 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1769 * @cwq: cwq of interest
1770 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001771 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001772 *
1773 * A work either has completed or is removed from pending queue,
1774 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1775 *
1776 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001777 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001778 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001779static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1780 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001781{
1782 /* ignore uncolored works */
1783 if (color == WORK_NO_COLOR)
1784 return;
1785
1786 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001787
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001788 if (!delayed) {
1789 cwq->nr_active--;
1790 if (!list_empty(&cwq->delayed_works)) {
1791 /* one down, submit a delayed one */
1792 if (cwq->nr_active < cwq->max_active)
1793 cwq_activate_first_delayed(cwq);
1794 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001795 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001796
1797 /* is flush in progress and are we at the flushing tip? */
1798 if (likely(cwq->flush_color != color))
1799 return;
1800
1801 /* are there still in-flight works? */
1802 if (cwq->nr_in_flight[color])
1803 return;
1804
1805 /* this cwq is done, clear flush_color */
1806 cwq->flush_color = -1;
1807
1808 /*
1809 * If this was the last cwq, wake up the first flusher. It
1810 * will handle the rest.
1811 */
1812 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1813 complete(&cwq->wq->first_flusher->done);
1814}
1815
1816/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001817 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001818 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001819 * @work: work to process
1820 *
1821 * Process @work. This function contains all the logics necessary to
1822 * process a single work including synchronization against and
1823 * interaction with other workers on the same cpu, queueing and
1824 * flushing. As long as context requirement is met, any worker can
1825 * call this function to process a work.
1826 *
1827 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001828 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001829 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001830static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001831__releases(&gcwq->lock)
1832__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001833{
Tejun Heo7e116292010-06-29 10:07:13 +02001834 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001835 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001836 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001837 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001838 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001839 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001840#ifdef CONFIG_LOCKDEP
1841 /*
1842 * It is permissible to free the struct work_struct from
1843 * inside the function that is called from it, this we need to
1844 * take into account for lockdep too. To avoid bogus "held
1845 * lock freed" warnings as well as problems when looking into
1846 * work->lockdep_map, make a copy and use that here.
1847 */
1848 struct lockdep_map lockdep_map = work->lockdep_map;
1849#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001850 /*
1851 * A single work shouldn't be executed concurrently by
1852 * multiple workers on a single cpu. Check whether anyone is
1853 * already processing the work. If so, defer the work to the
1854 * currently executing one.
1855 */
1856 collision = __find_worker_executing_work(gcwq, bwh, work);
1857 if (unlikely(collision)) {
1858 move_linked_works(work, &collision->scheduled, NULL);
1859 return;
1860 }
1861
Tejun Heoa62428c2010-06-29 10:07:10 +02001862 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001863 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001864 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001865 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001866 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001867 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001868 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001869
Tejun Heo7a22ad72010-06-29 10:07:13 +02001870 /* record the current cpu number in the work data and dequeue */
1871 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001872 list_del_init(&work->entry);
1873
Tejun Heo649027d2010-06-29 10:07:14 +02001874 /*
1875 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1876 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1877 */
1878 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1879 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1880 struct work_struct, entry);
1881
1882 if (!list_empty(&gcwq->worklist) &&
1883 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1884 wake_up_worker(gcwq);
1885 else
1886 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1887 }
1888
Tejun Heofb0e7be2010-06-29 10:07:15 +02001889 /*
1890 * CPU intensive works don't participate in concurrency
1891 * management. They're the scheduler's responsibility.
1892 */
1893 if (unlikely(cpu_intensive))
1894 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1895
Tejun Heo8b03ae32010-06-29 10:07:12 +02001896 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001897
Tejun Heo66307ae2012-08-03 10:30:45 -07001898 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001899 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001900
Tejun Heoe1594892011-01-09 23:32:15 +01001901 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001902 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001903 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001904 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001905 /*
1906 * While we must be careful to not use "work" after this, the trace
1907 * point will only record its address.
1908 */
1909 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001910 lock_map_release(&lockdep_map);
1911 lock_map_release(&cwq->wq->lockdep_map);
1912
1913 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001914 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1915 " last function: %pf\n",
1916 current->comm, preempt_count(), task_pid_nr(current),
1917 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001918 debug_show_held_locks(current);
1919 dump_stack();
1920 }
1921
Tejun Heo8b03ae32010-06-29 10:07:12 +02001922 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001923
Tejun Heofb0e7be2010-06-29 10:07:15 +02001924 /* clear cpu intensive status */
1925 if (unlikely(cpu_intensive))
1926 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1927
Tejun Heoa62428c2010-06-29 10:07:10 +02001928 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001929 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001930 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001931 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001932 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001933 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001934}
1935
Tejun Heoaffee4b2010-06-29 10:07:12 +02001936/**
1937 * process_scheduled_works - process scheduled works
1938 * @worker: self
1939 *
1940 * Process all scheduled works. Please note that the scheduled list
1941 * may change while processing a work, so this function repeatedly
1942 * fetches a work from the top and executes it.
1943 *
1944 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001945 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001946 * multiple times.
1947 */
1948static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001950 while (!list_empty(&worker->scheduled)) {
1951 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001953 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Tejun Heo4690c4a2010-06-29 10:07:10 +02001957/**
1958 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001959 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001960 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 * The gcwq worker thread function. There's a single dynamic pool of
1962 * these per each cpu. These workers process all works regardless of
1963 * their specific target workqueue. The only exception is works which
1964 * belong to workqueues with a rescuer which will be explained in
1965 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001966 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001967static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Tejun Heoc34056a2010-06-29 10:07:11 +02001969 struct worker *worker = __worker;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001970 struct global_cwq *gcwq = worker->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 /* tell the scheduler that this is a workqueue worker */
1973 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001974woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001975 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977 /* DIE can be set only while we're idle, checking here is enough */
1978 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001979 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
Tejun Heoc8e55f32010-06-29 10:07:12 +02001984 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001985recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 /* no more worker necessary? */
1987 if (!need_more_worker(gcwq))
1988 goto sleep;
1989
1990 /* do we need to manage? */
1991 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1992 goto recheck;
1993
Tejun Heoc8e55f32010-06-29 10:07:12 +02001994 /*
1995 * ->scheduled list can only be filled while a worker is
1996 * preparing to process a work or actually processing it.
1997 * Make sure nobody diddled with it while I was sleeping.
1998 */
1999 BUG_ON(!list_empty(&worker->scheduled));
2000
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 /*
2002 * When control reaches this point, we're guaranteed to have
2003 * at least one idle worker or that someone else has already
2004 * assumed the manager role.
2005 */
2006 worker_clr_flags(worker, WORKER_PREP);
2007
2008 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002009 struct work_struct *work =
Tejun Heo7e116292010-06-29 10:07:13 +02002010 list_first_entry(&gcwq->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002011 struct work_struct, entry);
2012
2013 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2014 /* optimization path, not strictly necessary */
2015 process_one_work(worker, work);
2016 if (unlikely(!list_empty(&worker->scheduled)))
2017 process_scheduled_works(worker);
2018 } else {
2019 move_linked_works(work, &worker->scheduled, NULL);
2020 process_scheduled_works(worker);
2021 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002022 } while (keep_working(gcwq));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002023
Tejun Heoe22bee72010-06-29 10:07:14 +02002024 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002025sleep:
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
2027 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002028
Tejun Heoc8e55f32010-06-29 10:07:12 +02002029 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 * gcwq->lock is held and there's no work to process and no
2031 * need to manage, sleep. Workers are woken up only while
2032 * holding gcwq->lock or from local cpu, so setting the
2033 * current state before releasing gcwq->lock is enough to
2034 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002035 */
2036 worker_enter_idle(worker);
2037 __set_current_state(TASK_INTERRUPTIBLE);
2038 spin_unlock_irq(&gcwq->lock);
2039 schedule();
2040 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Tejun Heoe22bee72010-06-29 10:07:14 +02002043/**
2044 * rescuer_thread - the rescuer thread function
2045 * @__wq: the associated workqueue
2046 *
2047 * Workqueue rescuer thread function. There's one rescuer for each
2048 * workqueue which has WQ_RESCUER set.
2049 *
2050 * Regular work processing on a gcwq may block trying to create a new
2051 * worker which uses GFP_KERNEL allocation which has slight chance of
2052 * developing into deadlock if some works currently on the same queue
2053 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2054 * the problem rescuer solves.
2055 *
2056 * When such condition is possible, the gcwq summons rescuers of all
2057 * workqueues which have works queued on the gcwq and let them process
2058 * those works so that forward progress can be guaranteed.
2059 *
2060 * This should happen rarely.
2061 */
2062static int rescuer_thread(void *__wq)
2063{
2064 struct workqueue_struct *wq = __wq;
2065 struct worker *rescuer = wq->rescuer;
2066 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002067 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002068 unsigned int cpu;
2069
2070 set_user_nice(current, RESCUER_NICE_LEVEL);
2071repeat:
2072 set_current_state(TASK_INTERRUPTIBLE);
2073
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002074 if (kthread_should_stop()) {
2075 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002076 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002077 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002078
Tejun Heof3421792010-07-02 10:03:51 +02002079 /*
2080 * See whether any cpu is asking for help. Unbounded
2081 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2082 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002083 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002084 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2085 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02002086 struct global_cwq *gcwq = cwq->gcwq;
2087 struct work_struct *work, *n;
2088
2089 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002090 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002091
2092 /* migrate to the target cpu if possible */
2093 rescuer->gcwq = gcwq;
2094 worker_maybe_bind_and_lock(rescuer);
2095
2096 /*
2097 * Slurp in all works issued via this workqueue and
2098 * process'em.
2099 */
2100 BUG_ON(!list_empty(&rescuer->scheduled));
2101 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
2102 if (get_work_cwq(work) == cwq)
2103 move_linked_works(work, scheduled, &n);
2104
2105 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002106
2107 /*
2108 * Leave this gcwq. If keep_working() is %true, notify a
2109 * regular worker; otherwise, we end up with 0 concurrency
2110 * and stalling the execution.
2111 */
2112 if (keep_working(gcwq))
2113 wake_up_worker(gcwq);
2114
Tejun Heoe22bee72010-06-29 10:07:14 +02002115 spin_unlock_irq(&gcwq->lock);
2116 }
2117
2118 schedule();
2119 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002122struct wq_barrier {
2123 struct work_struct work;
2124 struct completion done;
2125};
2126
2127static void wq_barrier_func(struct work_struct *work)
2128{
2129 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2130 complete(&barr->done);
2131}
2132
Tejun Heo4690c4a2010-06-29 10:07:10 +02002133/**
2134 * insert_wq_barrier - insert a barrier work
2135 * @cwq: cwq to insert barrier into
2136 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002137 * @target: target work to attach @barr to
2138 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002139 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002140 * @barr is linked to @target such that @barr is completed only after
2141 * @target finishes execution. Please note that the ordering
2142 * guarantee is observed only with respect to @target and on the local
2143 * cpu.
2144 *
2145 * Currently, a queued barrier can't be canceled. This is because
2146 * try_to_grab_pending() can't determine whether the work to be
2147 * grabbed is at the head of the queue and thus can't clear LINKED
2148 * flag of the previous work while there must be a valid next work
2149 * after a work with LINKED flag set.
2150 *
2151 * Note that when @worker is non-NULL, @target may be modified
2152 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002153 *
2154 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002155 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002156 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002157static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002158 struct wq_barrier *barr,
2159 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002160{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002161 struct list_head *head;
2162 unsigned int linked = 0;
2163
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002164 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002165 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002166 * as we know for sure that this will not trigger any of the
2167 * checks and call back into the fixup functions where we
2168 * might deadlock.
2169 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002170 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002171 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002172 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002173
Tejun Heoaffee4b2010-06-29 10:07:12 +02002174 /*
2175 * If @target is currently being executed, schedule the
2176 * barrier to the worker; otherwise, put it after @target.
2177 */
2178 if (worker)
2179 head = worker->scheduled.next;
2180 else {
2181 unsigned long *bits = work_data_bits(target);
2182
2183 head = target->entry.next;
2184 /* there can already be other linked works, inherit and set */
2185 linked = *bits & WORK_STRUCT_LINKED;
2186 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2187 }
2188
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002189 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002190 insert_work(cwq, &barr->work, head,
2191 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002192}
2193
Tejun Heo73f53c42010-06-29 10:07:11 +02002194/**
2195 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2196 * @wq: workqueue being flushed
2197 * @flush_color: new flush color, < 0 for no-op
2198 * @work_color: new work color, < 0 for no-op
2199 *
2200 * Prepare cwqs for workqueue flushing.
2201 *
2202 * If @flush_color is non-negative, flush_color on all cwqs should be
2203 * -1. If no cwq has in-flight commands at the specified color, all
2204 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2205 * has in flight commands, its cwq->flush_color is set to
2206 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2207 * wakeup logic is armed and %true is returned.
2208 *
2209 * The caller should have initialized @wq->first_flusher prior to
2210 * calling this function with non-negative @flush_color. If
2211 * @flush_color is negative, no flush color update is done and %false
2212 * is returned.
2213 *
2214 * If @work_color is non-negative, all cwqs should have the same
2215 * work_color which is previous to @work_color and all will be
2216 * advanced to @work_color.
2217 *
2218 * CONTEXT:
2219 * mutex_lock(wq->flush_mutex).
2220 *
2221 * RETURNS:
2222 * %true if @flush_color >= 0 and there's something to flush. %false
2223 * otherwise.
2224 */
2225static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2226 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Tejun Heo73f53c42010-06-29 10:07:11 +02002228 bool wait = false;
2229 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002230
Tejun Heo73f53c42010-06-29 10:07:11 +02002231 if (flush_color >= 0) {
2232 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2233 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002234 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002235
Tejun Heof3421792010-07-02 10:03:51 +02002236 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002237 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02002238 struct global_cwq *gcwq = cwq->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002239
Tejun Heo8b03ae32010-06-29 10:07:12 +02002240 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002241
2242 if (flush_color >= 0) {
2243 BUG_ON(cwq->flush_color != -1);
2244
2245 if (cwq->nr_in_flight[flush_color]) {
2246 cwq->flush_color = flush_color;
2247 atomic_inc(&wq->nr_cwqs_to_flush);
2248 wait = true;
2249 }
2250 }
2251
2252 if (work_color >= 0) {
2253 BUG_ON(work_color != work_next_color(cwq->work_color));
2254 cwq->work_color = work_color;
2255 }
2256
Tejun Heo8b03ae32010-06-29 10:07:12 +02002257 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002258 }
2259
2260 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2261 complete(&wq->first_flusher->done);
2262
2263 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002268 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 *
2270 * Forces execution of the workqueue and blocks until its completion.
2271 * This is typically used in driver shutdown handlers.
2272 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002273 * We sleep until all works which were queued on entry have been handled,
2274 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002276void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Tejun Heo73f53c42010-06-29 10:07:11 +02002278 struct wq_flusher this_flusher = {
2279 .list = LIST_HEAD_INIT(this_flusher.list),
2280 .flush_color = -1,
2281 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2282 };
2283 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002284
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002285 lock_map_acquire(&wq->lockdep_map);
2286 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002287
2288 mutex_lock(&wq->flush_mutex);
2289
2290 /*
2291 * Start-to-wait phase
2292 */
2293 next_color = work_next_color(wq->work_color);
2294
2295 if (next_color != wq->flush_color) {
2296 /*
2297 * Color space is not full. The current work_color
2298 * becomes our flush_color and work_color is advanced
2299 * by one.
2300 */
2301 BUG_ON(!list_empty(&wq->flusher_overflow));
2302 this_flusher.flush_color = wq->work_color;
2303 wq->work_color = next_color;
2304
2305 if (!wq->first_flusher) {
2306 /* no flush in progress, become the first flusher */
2307 BUG_ON(wq->flush_color != this_flusher.flush_color);
2308
2309 wq->first_flusher = &this_flusher;
2310
2311 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2312 wq->work_color)) {
2313 /* nothing to flush, done */
2314 wq->flush_color = next_color;
2315 wq->first_flusher = NULL;
2316 goto out_unlock;
2317 }
2318 } else {
2319 /* wait in queue */
2320 BUG_ON(wq->flush_color == this_flusher.flush_color);
2321 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2322 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2323 }
2324 } else {
2325 /*
2326 * Oops, color space is full, wait on overflow queue.
2327 * The next flush completion will assign us
2328 * flush_color and transfer to flusher_queue.
2329 */
2330 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2331 }
2332
2333 mutex_unlock(&wq->flush_mutex);
2334
2335 wait_for_completion(&this_flusher.done);
2336
2337 /*
2338 * Wake-up-and-cascade phase
2339 *
2340 * First flushers are responsible for cascading flushes and
2341 * handling overflow. Non-first flushers can simply return.
2342 */
2343 if (wq->first_flusher != &this_flusher)
2344 return;
2345
2346 mutex_lock(&wq->flush_mutex);
2347
Tejun Heo4ce48b32010-07-02 10:03:51 +02002348 /* we might have raced, check again with mutex held */
2349 if (wq->first_flusher != &this_flusher)
2350 goto out_unlock;
2351
Tejun Heo73f53c42010-06-29 10:07:11 +02002352 wq->first_flusher = NULL;
2353
2354 BUG_ON(!list_empty(&this_flusher.list));
2355 BUG_ON(wq->flush_color != this_flusher.flush_color);
2356
2357 while (true) {
2358 struct wq_flusher *next, *tmp;
2359
2360 /* complete all the flushers sharing the current flush color */
2361 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2362 if (next->flush_color != wq->flush_color)
2363 break;
2364 list_del_init(&next->list);
2365 complete(&next->done);
2366 }
2367
2368 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2369 wq->flush_color != work_next_color(wq->work_color));
2370
2371 /* this flush_color is finished, advance by one */
2372 wq->flush_color = work_next_color(wq->flush_color);
2373
2374 /* one color has been freed, handle overflow queue */
2375 if (!list_empty(&wq->flusher_overflow)) {
2376 /*
2377 * Assign the same color to all overflowed
2378 * flushers, advance work_color and append to
2379 * flusher_queue. This is the start-to-wait
2380 * phase for these overflowed flushers.
2381 */
2382 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2383 tmp->flush_color = wq->work_color;
2384
2385 wq->work_color = work_next_color(wq->work_color);
2386
2387 list_splice_tail_init(&wq->flusher_overflow,
2388 &wq->flusher_queue);
2389 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2390 }
2391
2392 if (list_empty(&wq->flusher_queue)) {
2393 BUG_ON(wq->flush_color != wq->work_color);
2394 break;
2395 }
2396
2397 /*
2398 * Need to flush more colors. Make the next flusher
2399 * the new first flusher and arm cwqs.
2400 */
2401 BUG_ON(wq->flush_color == wq->work_color);
2402 BUG_ON(wq->flush_color != next->flush_color);
2403
2404 list_del_init(&next->list);
2405 wq->first_flusher = next;
2406
2407 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2408 break;
2409
2410 /*
2411 * Meh... this color is already done, clear first
2412 * flusher and repeat cascading.
2413 */
2414 wq->first_flusher = NULL;
2415 }
2416
2417out_unlock:
2418 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
Dave Jonesae90dd52006-06-30 01:40:45 -04002420EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002422/**
2423 * drain_workqueue - drain a workqueue
2424 * @wq: workqueue to drain
2425 *
2426 * Wait until the workqueue becomes empty. While draining is in progress,
2427 * only chain queueing is allowed. IOW, only currently pending or running
2428 * work items on @wq can queue further work items on it. @wq is flushed
2429 * repeatedly until it becomes empty. The number of flushing is detemined
2430 * by the depth of chaining and should be relatively short. Whine if it
2431 * takes too long.
2432 */
2433void drain_workqueue(struct workqueue_struct *wq)
2434{
2435 unsigned int flush_cnt = 0;
2436 unsigned int cpu;
2437
2438 /*
2439 * __queue_work() needs to test whether there are drainers, is much
2440 * hotter than drain_workqueue() and already looks at @wq->flags.
2441 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2442 */
2443 spin_lock(&workqueue_lock);
2444 if (!wq->nr_drainers++)
2445 wq->flags |= WQ_DRAINING;
2446 spin_unlock(&workqueue_lock);
2447reflush:
2448 flush_workqueue(wq);
2449
2450 for_each_cwq_cpu(cpu, wq) {
2451 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002452 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002453
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002454 spin_lock_irq(&cwq->gcwq->lock);
2455 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
2456 spin_unlock_irq(&cwq->gcwq->lock);
2457
2458 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002459 continue;
2460
2461 if (++flush_cnt == 10 ||
2462 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2463 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2464 wq->name, flush_cnt);
2465 goto reflush;
2466 }
2467
2468 spin_lock(&workqueue_lock);
2469 if (!--wq->nr_drainers)
2470 wq->flags &= ~WQ_DRAINING;
2471 spin_unlock(&workqueue_lock);
2472}
2473EXPORT_SYMBOL_GPL(drain_workqueue);
2474
Tejun Heobaf59022010-09-16 10:42:16 +02002475static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2476 bool wait_executing)
2477{
2478 struct worker *worker = NULL;
2479 struct global_cwq *gcwq;
2480 struct cpu_workqueue_struct *cwq;
2481
2482 might_sleep();
2483 gcwq = get_work_gcwq(work);
2484 if (!gcwq)
2485 return false;
2486
2487 spin_lock_irq(&gcwq->lock);
2488 if (!list_empty(&work->entry)) {
2489 /*
2490 * See the comment near try_to_grab_pending()->smp_rmb().
2491 * If it was re-queued to a different gcwq under us, we
2492 * are not going to wait.
2493 */
2494 smp_rmb();
2495 cwq = get_work_cwq(work);
2496 if (unlikely(!cwq || gcwq != cwq->gcwq))
2497 goto already_gone;
2498 } else if (wait_executing) {
2499 worker = find_worker_executing_work(gcwq, work);
2500 if (!worker)
2501 goto already_gone;
2502 cwq = worker->current_cwq;
2503 } else
2504 goto already_gone;
2505
2506 insert_wq_barrier(cwq, barr, work, worker);
2507 spin_unlock_irq(&gcwq->lock);
2508
Tejun Heoe1594892011-01-09 23:32:15 +01002509 /*
2510 * If @max_active is 1 or rescuer is in use, flushing another work
2511 * item on the same workqueue may lead to deadlock. Make sure the
2512 * flusher is not running on the same workqueue by verifying write
2513 * access.
2514 */
2515 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2516 lock_map_acquire(&cwq->wq->lockdep_map);
2517 else
2518 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002519 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002520
Tejun Heobaf59022010-09-16 10:42:16 +02002521 return true;
2522already_gone:
2523 spin_unlock_irq(&gcwq->lock);
2524 return false;
2525}
2526
Oleg Nesterovdb700892008-07-25 01:47:49 -07002527/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002528 * flush_work - wait for a work to finish executing the last queueing instance
2529 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002530 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002531 * Wait until @work has finished execution. This function considers
2532 * only the last queueing instance of @work. If @work has been
2533 * enqueued across different CPUs on a non-reentrant workqueue or on
2534 * multiple workqueues, @work might still be executing on return on
2535 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002536 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002537 * If @work was queued only on a non-reentrant, ordered or unbound
2538 * workqueue, @work is guaranteed to be idle on return if it hasn't
2539 * been requeued since flush started.
2540 *
2541 * RETURNS:
2542 * %true if flush_work() waited for the work to finish execution,
2543 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002544 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002545bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002546{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002547 struct wq_barrier barr;
2548
Tejun Heobaf59022010-09-16 10:42:16 +02002549 if (start_flush_work(work, &barr, true)) {
2550 wait_for_completion(&barr.done);
2551 destroy_work_on_stack(&barr.work);
2552 return true;
2553 } else
2554 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002555}
2556EXPORT_SYMBOL_GPL(flush_work);
2557
Tejun Heo401a8d02010-09-16 10:36:00 +02002558static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2559{
2560 struct wq_barrier barr;
2561 struct worker *worker;
2562
2563 spin_lock_irq(&gcwq->lock);
2564
2565 worker = find_worker_executing_work(gcwq, work);
2566 if (unlikely(worker))
2567 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2568
2569 spin_unlock_irq(&gcwq->lock);
2570
2571 if (unlikely(worker)) {
2572 wait_for_completion(&barr.done);
2573 destroy_work_on_stack(&barr.work);
2574 return true;
2575 } else
2576 return false;
2577}
2578
2579static bool wait_on_work(struct work_struct *work)
2580{
2581 bool ret = false;
2582 int cpu;
2583
2584 might_sleep();
2585
2586 lock_map_acquire(&work->lockdep_map);
2587 lock_map_release(&work->lockdep_map);
2588
2589 for_each_gcwq_cpu(cpu)
2590 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2591 return ret;
2592}
2593
Tejun Heo09383492010-09-16 10:48:29 +02002594/**
2595 * flush_work_sync - wait until a work has finished execution
2596 * @work: the work to flush
2597 *
2598 * Wait until @work has finished execution. On return, it's
2599 * guaranteed that all queueing instances of @work which happened
2600 * before this function is called are finished. In other words, if
2601 * @work hasn't been requeued since this function was called, @work is
2602 * guaranteed to be idle on return.
2603 *
2604 * RETURNS:
2605 * %true if flush_work_sync() waited for the work to finish execution,
2606 * %false if it was already idle.
2607 */
2608bool flush_work_sync(struct work_struct *work)
2609{
2610 struct wq_barrier barr;
2611 bool pending, waited;
2612
2613 /* we'll wait for executions separately, queue barr only if pending */
2614 pending = start_flush_work(work, &barr, false);
2615
2616 /* wait for executions to finish */
2617 waited = wait_on_work(work);
2618
2619 /* wait for the pending one */
2620 if (pending) {
2621 wait_for_completion(&barr.done);
2622 destroy_work_on_stack(&barr.work);
2623 }
2624
2625 return pending || waited;
2626}
2627EXPORT_SYMBOL_GPL(flush_work_sync);
2628
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002629/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002630 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002631 * so this work can't be re-armed in any way.
2632 */
2633static int try_to_grab_pending(struct work_struct *work)
2634{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002635 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002636 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002637
Tejun Heo22df02b2010-06-29 10:07:10 +02002638 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002639 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002640
2641 /*
2642 * The queueing is in progress, or it is already queued. Try to
2643 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2644 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002645 gcwq = get_work_gcwq(work);
2646 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002647 return ret;
2648
Tejun Heo8b03ae32010-06-29 10:07:12 +02002649 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002650 if (!list_empty(&work->entry)) {
2651 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002652 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002653 * In that case we must see the new value after rmb(), see
2654 * insert_work()->wmb().
2655 */
2656 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002657 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002658 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002659
2660 /*
2661 * A delayed work item cannot be grabbed directly
2662 * because it might have linked NO_COLOR work items
2663 * which, if left on the delayed_list, will confuse
2664 * cwq->nr_active management later on and cause
2665 * stall. Make sure the work item is activated
2666 * before grabbing.
2667 */
2668 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2669 cwq_activate_delayed_work(work);
2670
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002671 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002672 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002673 get_work_color(work),
2674 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675 ret = 1;
2676 }
2677 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002678 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002679
2680 return ret;
2681}
2682
Tejun Heo401a8d02010-09-16 10:36:00 +02002683static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002684 struct timer_list* timer)
2685{
2686 int ret;
2687
2688 do {
2689 ret = (timer && likely(del_timer(timer)));
2690 if (!ret)
2691 ret = try_to_grab_pending(work);
2692 wait_on_work(work);
2693 } while (unlikely(ret < 0));
2694
Tejun Heo7a22ad72010-06-29 10:07:13 +02002695 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002696 return ret;
2697}
2698
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002699/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002700 * cancel_work_sync - cancel a work and wait for it to finish
2701 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002702 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002703 * Cancel @work and wait for its execution to finish. This function
2704 * can be used even if the work re-queues itself or migrates to
2705 * another workqueue. On return from this function, @work is
2706 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002707 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002708 * cancel_work_sync(&delayed_work->work) must not be used for
2709 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002710 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002711 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002712 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002713 *
2714 * RETURNS:
2715 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002716 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002717bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002718{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002719 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002720}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002721EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002722
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002723/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002724 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2725 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002726 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 * Delayed timer is cancelled and the pending work is queued for
2728 * immediate execution. Like flush_work(), this function only
2729 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002730 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002731 * RETURNS:
2732 * %true if flush_work() waited for the work to finish execution,
2733 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002734 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002735bool flush_delayed_work(struct delayed_work *dwork)
2736{
2737 if (del_timer_sync(&dwork->timer))
2738 __queue_work(raw_smp_processor_id(),
2739 get_work_cwq(&dwork->work)->wq, &dwork->work);
2740 return flush_work(&dwork->work);
2741}
2742EXPORT_SYMBOL(flush_delayed_work);
2743
2744/**
Tejun Heo09383492010-09-16 10:48:29 +02002745 * flush_delayed_work_sync - wait for a dwork to finish
2746 * @dwork: the delayed work to flush
2747 *
2748 * Delayed timer is cancelled and the pending work is queued for
2749 * execution immediately. Other than timer handling, its behavior
2750 * is identical to flush_work_sync().
2751 *
2752 * RETURNS:
2753 * %true if flush_work_sync() waited for the work to finish execution,
2754 * %false if it was already idle.
2755 */
2756bool flush_delayed_work_sync(struct delayed_work *dwork)
2757{
2758 if (del_timer_sync(&dwork->timer))
2759 __queue_work(raw_smp_processor_id(),
2760 get_work_cwq(&dwork->work)->wq, &dwork->work);
2761 return flush_work_sync(&dwork->work);
2762}
2763EXPORT_SYMBOL(flush_delayed_work_sync);
2764
2765/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002766 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2767 * @dwork: the delayed work cancel
2768 *
2769 * This is cancel_work_sync() for delayed works.
2770 *
2771 * RETURNS:
2772 * %true if @dwork was pending, %false otherwise.
2773 */
2774bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002775{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002776 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002777}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002778EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002780/**
2781 * schedule_work - put work task in global workqueue
2782 * @work: job to be done
2783 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002784 * Returns zero if @work was already on the kernel-global workqueue and
2785 * non-zero otherwise.
2786 *
2787 * This puts a job in the kernel-global workqueue if it was not already
2788 * queued and leaves it in the same position on the kernel-global
2789 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002790 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002791int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Tejun Heod320c032010-06-29 10:07:14 +02002793 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
Dave Jonesae90dd52006-06-30 01:40:45 -04002795EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Zhang Ruic1a220e2008-07-23 21:28:39 -07002797/*
2798 * schedule_work_on - put work task on a specific cpu
2799 * @cpu: cpu to put the work task on
2800 * @work: job to be done
2801 *
2802 * This puts a job on a specific cpu
2803 */
2804int schedule_work_on(int cpu, struct work_struct *work)
2805{
Tejun Heod320c032010-06-29 10:07:14 +02002806 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002807}
2808EXPORT_SYMBOL(schedule_work_on);
2809
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002810/**
2811 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002812 * @dwork: job to be done
2813 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002814 *
2815 * After waiting for a given time this puts a job in the kernel-global
2816 * workqueue.
2817 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002818int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002819 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Tejun Heod320c032010-06-29 10:07:14 +02002821 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
Dave Jonesae90dd52006-06-30 01:40:45 -04002823EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002825/**
2826 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2827 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002828 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002829 * @delay: number of jiffies to wait
2830 *
2831 * After waiting for a given time this puts a job in the kernel-global
2832 * workqueue on the specified CPU.
2833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002835 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Tejun Heod320c032010-06-29 10:07:14 +02002837 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
Dave Jonesae90dd52006-06-30 01:40:45 -04002839EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Andrew Mortonb6136772006-06-25 05:47:49 -07002841/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002842 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002843 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002844 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002845 * schedule_on_each_cpu() executes @func on each online CPU using the
2846 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002848 *
2849 * RETURNS:
2850 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002851 */
David Howells65f27f32006-11-22 14:55:48 +00002852int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002853{
2854 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002855 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002856
Andrew Mortonb6136772006-06-25 05:47:49 -07002857 works = alloc_percpu(struct work_struct);
2858 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002859 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002860
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002861 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002862
Christoph Lameter15316ba2006-01-08 01:00:43 -08002863 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002864 struct work_struct *work = per_cpu_ptr(works, cpu);
2865
2866 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002867 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002868 }
Tejun Heo93981802009-11-17 14:06:20 -08002869
2870 for_each_online_cpu(cpu)
2871 flush_work(per_cpu_ptr(works, cpu));
2872
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002873 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002874 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002875 return 0;
2876}
2877
Alan Sterneef6a7d2010-02-12 17:39:21 +09002878/**
2879 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2880 *
2881 * Forces execution of the kernel-global workqueue and blocks until its
2882 * completion.
2883 *
2884 * Think twice before calling this function! It's very easy to get into
2885 * trouble if you don't take great care. Either of the following situations
2886 * will lead to deadlock:
2887 *
2888 * One of the work items currently on the workqueue needs to acquire
2889 * a lock held by your code or its caller.
2890 *
2891 * Your code is running in the context of a work routine.
2892 *
2893 * They will be detected by lockdep when they occur, but the first might not
2894 * occur very often. It depends on what work items are on the workqueue and
2895 * what locks they need, which you have no control over.
2896 *
2897 * In most situations flushing the entire workqueue is overkill; you merely
2898 * need to know that a particular work item isn't queued and isn't running.
2899 * In such cases you should use cancel_delayed_work_sync() or
2900 * cancel_work_sync() instead.
2901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902void flush_scheduled_work(void)
2903{
Tejun Heod320c032010-06-29 10:07:14 +02002904 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
Dave Jonesae90dd52006-06-30 01:40:45 -04002906EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002909 * execute_in_process_context - reliably execute the routine with user context
2910 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002911 * @ew: guaranteed storage for the execute work structure (must
2912 * be available when the work executes)
2913 *
2914 * Executes the function immediately if process context is available,
2915 * otherwise schedules the function for delayed execution.
2916 *
2917 * Returns: 0 - function was executed
2918 * 1 - function was scheduled for execution
2919 */
David Howells65f27f32006-11-22 14:55:48 +00002920int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002921{
2922 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002923 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002924 return 0;
2925 }
2926
David Howells65f27f32006-11-22 14:55:48 +00002927 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002928 schedule_work(&ew->work);
2929
2930 return 1;
2931}
2932EXPORT_SYMBOL_GPL(execute_in_process_context);
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934int keventd_up(void)
2935{
Tejun Heod320c032010-06-29 10:07:14 +02002936 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002939static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002941 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002942 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2943 * Make sure that the alignment isn't lower than that of
2944 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002945 */
Tejun Heo0f900042010-06-29 10:07:11 +02002946 const size_t size = sizeof(struct cpu_workqueue_struct);
2947 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2948 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002949
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002950 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002951 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002952 else {
Tejun Heof3421792010-07-02 10:03:51 +02002953 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002954
Tejun Heof3421792010-07-02 10:03:51 +02002955 /*
2956 * Allocate enough room to align cwq and put an extra
2957 * pointer at the end pointing back to the originally
2958 * allocated pointer which will be used for free.
2959 */
2960 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2961 if (ptr) {
2962 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2963 *(void **)(wq->cpu_wq.single + 1) = ptr;
2964 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002965 }
Tejun Heof3421792010-07-02 10:03:51 +02002966
Tejun Heo0415b002011-03-24 18:50:09 +01002967 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002968 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2969 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002970}
2971
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002972static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002973{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002974 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002975 free_percpu(wq->cpu_wq.pcpu);
2976 else if (wq->cpu_wq.single) {
2977 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002978 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002979 }
2980}
2981
Tejun Heof3421792010-07-02 10:03:51 +02002982static int wq_clamp_max_active(int max_active, unsigned int flags,
2983 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002984{
Tejun Heof3421792010-07-02 10:03:51 +02002985 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2986
2987 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002988 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2989 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002990 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002991
Tejun Heof3421792010-07-02 10:03:51 +02002992 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002993}
2994
Tejun Heob196be82012-01-10 15:11:35 -08002995struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002996 unsigned int flags,
2997 int max_active,
2998 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002999 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003000{
Tejun Heob196be82012-01-10 15:11:35 -08003001 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003002 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003003 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003004 size_t namelen;
3005
3006 /* determine namelen, allocate wq and format name */
3007 va_start(args, lock_name);
3008 va_copy(args1, args);
3009 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3010
3011 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3012 if (!wq)
3013 goto err;
3014
3015 vsnprintf(wq->name, namelen, fmt, args1);
3016 va_end(args);
3017 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003018
Tejun Heof3421792010-07-02 10:03:51 +02003019 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003020 * Workqueues which may be used during memory reclaim should
3021 * have a rescuer to guarantee forward progress.
3022 */
3023 if (flags & WQ_MEM_RECLAIM)
3024 flags |= WQ_RESCUER;
3025
3026 /*
Tejun Heof3421792010-07-02 10:03:51 +02003027 * Unbound workqueues aren't concurrency managed and should be
3028 * dispatched to workers immediately.
3029 */
3030 if (flags & WQ_UNBOUND)
3031 flags |= WQ_HIGHPRI;
3032
Tejun Heod320c032010-06-29 10:07:14 +02003033 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003034 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003035
Tejun Heob196be82012-01-10 15:11:35 -08003036 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003037 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003038 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003039 mutex_init(&wq->flush_mutex);
3040 atomic_set(&wq->nr_cwqs_to_flush, 0);
3041 INIT_LIST_HEAD(&wq->flusher_queue);
3042 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003043
Johannes Bergeb13ba82008-01-16 09:51:58 +01003044 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003045 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003046
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003047 if (alloc_cwqs(wq) < 0)
3048 goto err;
3049
Tejun Heof3421792010-07-02 10:03:51 +02003050 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003051 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003052 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003053
Tejun Heo0f900042010-06-29 10:07:11 +02003054 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003055 cwq->gcwq = gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003056 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003057 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003058 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003059 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003060 }
3061
Tejun Heoe22bee72010-06-29 10:07:14 +02003062 if (flags & WQ_RESCUER) {
3063 struct worker *rescuer;
3064
Tejun Heof2e005a2010-07-20 15:59:09 +02003065 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003066 goto err;
3067
3068 wq->rescuer = rescuer = alloc_worker();
3069 if (!rescuer)
3070 goto err;
3071
Tejun Heob196be82012-01-10 15:11:35 -08003072 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3073 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003074 if (IS_ERR(rescuer->task))
3075 goto err;
3076
Tejun Heoe22bee72010-06-29 10:07:14 +02003077 rescuer->task->flags |= PF_THREAD_BOUND;
3078 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003079 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003080
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003081 /*
3082 * workqueue_lock protects global freeze state and workqueues
3083 * list. Grab it, set max_active accordingly and add the new
3084 * workqueue to workqueues list.
3085 */
Tejun Heo15376632010-06-29 10:07:11 +02003086 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003087
Tejun Heo58a69cb2011-02-16 09:25:31 +01003088 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003089 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003090 get_cwq(cpu, wq)->max_active = 0;
3091
Tejun Heo15376632010-06-29 10:07:11 +02003092 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003093
Tejun Heo15376632010-06-29 10:07:11 +02003094 spin_unlock(&workqueue_lock);
3095
Oleg Nesterov3af244332007-05-09 02:34:09 -07003096 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003097err:
3098 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003099 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003100 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003101 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003102 kfree(wq);
3103 }
3104 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003105}
Tejun Heod320c032010-06-29 10:07:14 +02003106EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003107
3108/**
3109 * destroy_workqueue - safely terminate a workqueue
3110 * @wq: target workqueue
3111 *
3112 * Safely destroy a workqueue. All work currently pending will be done first.
3113 */
3114void destroy_workqueue(struct workqueue_struct *wq)
3115{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003116 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003117
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003118 /* drain it before proceeding with destruction */
3119 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003120
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003121 /*
3122 * wq list is used to freeze wq, remove from list after
3123 * flushing is complete in case freeze races us.
3124 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003125 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003126 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003127 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003128
Tejun Heoe22bee72010-06-29 10:07:14 +02003129 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003130 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003131 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3132 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003133
Tejun Heo73f53c42010-06-29 10:07:11 +02003134 for (i = 0; i < WORK_NR_COLORS; i++)
3135 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003136 BUG_ON(cwq->nr_active);
3137 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003138 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003139
Tejun Heoe22bee72010-06-29 10:07:14 +02003140 if (wq->flags & WQ_RESCUER) {
3141 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003142 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003143 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003144 }
3145
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003146 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003147 kfree(wq);
3148}
3149EXPORT_SYMBOL_GPL(destroy_workqueue);
3150
Tejun Heodcd989c2010-06-29 10:07:14 +02003151/**
3152 * workqueue_set_max_active - adjust max_active of a workqueue
3153 * @wq: target workqueue
3154 * @max_active: new max_active value.
3155 *
3156 * Set max_active of @wq to @max_active.
3157 *
3158 * CONTEXT:
3159 * Don't call from IRQ context.
3160 */
3161void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3162{
3163 unsigned int cpu;
3164
Tejun Heof3421792010-07-02 10:03:51 +02003165 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003166
3167 spin_lock(&workqueue_lock);
3168
3169 wq->saved_max_active = max_active;
3170
Tejun Heof3421792010-07-02 10:03:51 +02003171 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003172 struct global_cwq *gcwq = get_gcwq(cpu);
3173
3174 spin_lock_irq(&gcwq->lock);
3175
Tejun Heo58a69cb2011-02-16 09:25:31 +01003176 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003177 !(gcwq->flags & GCWQ_FREEZING))
3178 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3179
3180 spin_unlock_irq(&gcwq->lock);
3181 }
3182
3183 spin_unlock(&workqueue_lock);
3184}
3185EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3186
3187/**
3188 * workqueue_congested - test whether a workqueue is congested
3189 * @cpu: CPU in question
3190 * @wq: target workqueue
3191 *
3192 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3193 * no synchronization around this function and the test result is
3194 * unreliable and only useful as advisory hints or for debugging.
3195 *
3196 * RETURNS:
3197 * %true if congested, %false otherwise.
3198 */
3199bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3200{
3201 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3202
3203 return !list_empty(&cwq->delayed_works);
3204}
3205EXPORT_SYMBOL_GPL(workqueue_congested);
3206
3207/**
3208 * work_cpu - return the last known associated cpu for @work
3209 * @work: the work of interest
3210 *
3211 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003212 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003213 */
3214unsigned int work_cpu(struct work_struct *work)
3215{
3216 struct global_cwq *gcwq = get_work_gcwq(work);
3217
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003218 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003219}
3220EXPORT_SYMBOL_GPL(work_cpu);
3221
3222/**
3223 * work_busy - test whether a work is currently pending or running
3224 * @work: the work to be tested
3225 *
3226 * Test whether @work is currently pending or running. There is no
3227 * synchronization around this function and the test result is
3228 * unreliable and only useful as advisory hints or for debugging.
3229 * Especially for reentrant wqs, the pending state might hide the
3230 * running state.
3231 *
3232 * RETURNS:
3233 * OR'd bitmask of WORK_BUSY_* bits.
3234 */
3235unsigned int work_busy(struct work_struct *work)
3236{
3237 struct global_cwq *gcwq = get_work_gcwq(work);
3238 unsigned long flags;
3239 unsigned int ret = 0;
3240
3241 if (!gcwq)
3242 return false;
3243
3244 spin_lock_irqsave(&gcwq->lock, flags);
3245
3246 if (work_pending(work))
3247 ret |= WORK_BUSY_PENDING;
3248 if (find_worker_executing_work(gcwq, work))
3249 ret |= WORK_BUSY_RUNNING;
3250
3251 spin_unlock_irqrestore(&gcwq->lock, flags);
3252
3253 return ret;
3254}
3255EXPORT_SYMBOL_GPL(work_busy);
3256
Tejun Heodb7bccf2010-06-29 10:07:12 +02003257/*
3258 * CPU hotplug.
3259 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003260 * There are two challenges in supporting CPU hotplug. Firstly, there
3261 * are a lot of assumptions on strong associations among work, cwq and
3262 * gcwq which make migrating pending and scheduled works very
3263 * difficult to implement without impacting hot paths. Secondly,
3264 * gcwqs serve mix of short, long and very long running works making
3265 * blocked draining impractical.
3266 *
3267 * This is solved by allowing a gcwq to be detached from CPU, running
3268 * it with unbound (rogue) workers and allowing it to be reattached
3269 * later if the cpu comes back online. A separate thread is created
3270 * to govern a gcwq in such state and is called the trustee of the
3271 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003272 *
3273 * Trustee states and their descriptions.
3274 *
3275 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3276 * new trustee is started with this state.
3277 *
3278 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003279 * assuming the manager role and making all existing
3280 * workers rogue. DOWN_PREPARE waits for trustee to
3281 * enter this state. After reaching IN_CHARGE, trustee
3282 * tries to execute the pending worklist until it's empty
3283 * and the state is set to BUTCHER, or the state is set
3284 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003285 *
3286 * BUTCHER Command state which is set by the cpu callback after
3287 * the cpu has went down. Once this state is set trustee
3288 * knows that there will be no new works on the worklist
3289 * and once the worklist is empty it can proceed to
3290 * killing idle workers.
3291 *
3292 * RELEASE Command state which is set by the cpu callback if the
3293 * cpu down has been canceled or it has come online
3294 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003295 * trying to drain or butcher and clears ROGUE, rebinds
3296 * all remaining workers back to the cpu and releases
3297 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003298 *
3299 * DONE Trustee will enter this state after BUTCHER or RELEASE
3300 * is complete.
3301 *
3302 * trustee CPU draining
3303 * took over down complete
3304 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3305 * | | ^
3306 * | CPU is back online v return workers |
3307 * ----------------> RELEASE --------------
3308 */
3309
3310/**
3311 * trustee_wait_event_timeout - timed event wait for trustee
3312 * @cond: condition to wait for
3313 * @timeout: timeout in jiffies
3314 *
3315 * wait_event_timeout() for trustee to use. Handles locking and
3316 * checks for RELEASE request.
3317 *
3318 * CONTEXT:
3319 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3320 * multiple times. To be used by trustee.
3321 *
3322 * RETURNS:
3323 * Positive indicating left time if @cond is satisfied, 0 if timed
3324 * out, -1 if canceled.
3325 */
3326#define trustee_wait_event_timeout(cond, timeout) ({ \
3327 long __ret = (timeout); \
3328 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3329 __ret) { \
3330 spin_unlock_irq(&gcwq->lock); \
3331 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3332 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3333 __ret); \
3334 spin_lock_irq(&gcwq->lock); \
3335 } \
3336 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3337})
3338
3339/**
3340 * trustee_wait_event - event wait for trustee
3341 * @cond: condition to wait for
3342 *
3343 * wait_event() for trustee to use. Automatically handles locking and
3344 * checks for CANCEL request.
3345 *
3346 * CONTEXT:
3347 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3348 * multiple times. To be used by trustee.
3349 *
3350 * RETURNS:
3351 * 0 if @cond is satisfied, -1 if canceled.
3352 */
3353#define trustee_wait_event(cond) ({ \
3354 long __ret1; \
3355 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3356 __ret1 < 0 ? -1 : 0; \
3357})
3358
3359static int __cpuinit trustee_thread(void *__gcwq)
3360{
3361 struct global_cwq *gcwq = __gcwq;
3362 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003363 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003364 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003365 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003366 int i;
3367
3368 BUG_ON(gcwq->cpu != smp_processor_id());
3369
3370 spin_lock_irq(&gcwq->lock);
3371 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003372 * Claim the manager position and make all workers rogue.
3373 * Trustee must be bound to the target cpu and can't be
3374 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003375 */
3376 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe22bee72010-06-29 10:07:14 +02003377 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3378 BUG_ON(rc < 0);
3379
3380 gcwq->flags |= GCWQ_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003381
3382 list_for_each_entry(worker, &gcwq->idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003383 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003384
3385 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003386 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003387
3388 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003389 * Call schedule() so that we cross rq->lock and thus can
3390 * guarantee sched callbacks see the rogue flag. This is
3391 * necessary as scheduler callbacks may be invoked from other
3392 * cpus.
3393 */
3394 spin_unlock_irq(&gcwq->lock);
3395 schedule();
3396 spin_lock_irq(&gcwq->lock);
3397
3398 /*
Tejun Heocb444762010-07-02 10:03:50 +02003399 * Sched callbacks are disabled now. Zap nr_running. After
3400 * this, nr_running stays zero and need_more_worker() and
3401 * keep_working() are always true as long as the worklist is
3402 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003403 */
Tejun Heocb444762010-07-02 10:03:50 +02003404 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003405
3406 spin_unlock_irq(&gcwq->lock);
3407 del_timer_sync(&gcwq->idle_timer);
3408 spin_lock_irq(&gcwq->lock);
3409
3410 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003411 * We're now in charge. Notify and proceed to drain. We need
3412 * to keep the gcwq running during the whole CPU down
3413 * procedure as other cpu hotunplug callbacks may need to
3414 * flush currently running tasks.
3415 */
3416 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3417 wake_up_all(&gcwq->trustee_wait);
3418
3419 /*
3420 * The original cpu is in the process of dying and may go away
3421 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003422 * be migrated to other cpus. Try draining any left work. We
3423 * want to get it over with ASAP - spam rescuers, wake up as
3424 * many idlers as necessary and create new ones till the
3425 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003426 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003427 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003428 */
3429 while (gcwq->nr_workers != gcwq->nr_idle ||
3430 gcwq->flags & GCWQ_FREEZING ||
3431 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003432 int nr_works = 0;
3433
3434 list_for_each_entry(work, &gcwq->worklist, entry) {
3435 send_mayday(work);
3436 nr_works++;
3437 }
3438
3439 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3440 if (!nr_works--)
3441 break;
3442 wake_up_process(worker->task);
3443 }
3444
3445 if (need_to_create_worker(gcwq)) {
3446 spin_unlock_irq(&gcwq->lock);
3447 worker = create_worker(gcwq, false);
3448 spin_lock_irq(&gcwq->lock);
3449 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003450 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003451 start_worker(worker);
3452 }
3453 }
3454
Tejun Heodb7bccf2010-06-29 10:07:12 +02003455 /* give a breather */
3456 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3457 break;
3458 }
3459
Tejun Heoe22bee72010-06-29 10:07:14 +02003460 /*
3461 * Either all works have been scheduled and cpu is down, or
3462 * cpu down has already been canceled. Wait for and butcher
3463 * all workers till we're canceled.
3464 */
3465 do {
3466 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3467 while (!list_empty(&gcwq->idle_list))
3468 destroy_worker(list_first_entry(&gcwq->idle_list,
3469 struct worker, entry));
3470 } while (gcwq->nr_workers && rc >= 0);
3471
3472 /*
3473 * At this point, either draining has completed and no worker
3474 * is left, or cpu down has been canceled or the cpu is being
3475 * brought back up. There shouldn't be any idle one left.
3476 * Tell the remaining busy ones to rebind once it finishes the
3477 * currently scheduled works by scheduling the rebind_work.
3478 */
3479 WARN_ON(!list_empty(&gcwq->idle_list));
3480
3481 for_each_busy_worker(worker, i, pos, gcwq) {
3482 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003483 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003484
3485 /*
3486 * Rebind_work may race with future cpu hotplug
3487 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003488 * rebinding is scheduled. The morphing should
3489 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003490 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003491 worker_flags |= WORKER_REBIND;
3492 worker_flags &= ~WORKER_ROGUE;
3493 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003494
3495 /* queue rebind_work, wq doesn't matter, use the default one */
3496 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3497 work_data_bits(rebind_work)))
3498 continue;
3499
3500 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003501 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003502 worker->scheduled.next,
3503 work_color_to_flags(WORK_NO_COLOR));
3504 }
3505
3506 /* relinquish manager role */
3507 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3508
Tejun Heodb7bccf2010-06-29 10:07:12 +02003509 /* notify completion */
3510 gcwq->trustee = NULL;
3511 gcwq->trustee_state = TRUSTEE_DONE;
3512 wake_up_all(&gcwq->trustee_wait);
3513 spin_unlock_irq(&gcwq->lock);
3514 return 0;
3515}
3516
3517/**
3518 * wait_trustee_state - wait for trustee to enter the specified state
3519 * @gcwq: gcwq the trustee of interest belongs to
3520 * @state: target state to wait for
3521 *
3522 * Wait for the trustee to reach @state. DONE is already matched.
3523 *
3524 * CONTEXT:
3525 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3526 * multiple times. To be used by cpu_callback.
3527 */
3528static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003529__releases(&gcwq->lock)
3530__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003531{
3532 if (!(gcwq->trustee_state == state ||
3533 gcwq->trustee_state == TRUSTEE_DONE)) {
3534 spin_unlock_irq(&gcwq->lock);
3535 __wait_event(gcwq->trustee_wait,
3536 gcwq->trustee_state == state ||
3537 gcwq->trustee_state == TRUSTEE_DONE);
3538 spin_lock_irq(&gcwq->lock);
3539 }
3540}
3541
Oleg Nesterov3af244332007-05-09 02:34:09 -07003542static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3543 unsigned long action,
3544 void *hcpu)
3545{
3546 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003547 struct global_cwq *gcwq = get_gcwq(cpu);
3548 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003549 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003550 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003552 action &= ~CPU_TASKS_FROZEN;
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003555 case CPU_DOWN_PREPARE:
3556 new_trustee = kthread_create(trustee_thread, gcwq,
3557 "workqueue_trustee/%d\n", cpu);
3558 if (IS_ERR(new_trustee))
3559 return notifier_from_errno(PTR_ERR(new_trustee));
3560 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003561 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 case CPU_UP_PREPARE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003563 BUG_ON(gcwq->first_idle);
3564 new_worker = create_worker(gcwq, false);
3565 if (!new_worker) {
3566 if (new_trustee)
3567 kthread_stop(new_trustee);
3568 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571
Tejun Heodb7bccf2010-06-29 10:07:12 +02003572 /* some are called w/ irq disabled, don't disturb irq status */
3573 spin_lock_irqsave(&gcwq->lock, flags);
3574
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003575 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003576 case CPU_DOWN_PREPARE:
3577 /* initialize trustee and tell it to acquire the gcwq */
3578 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3579 gcwq->trustee = new_trustee;
3580 gcwq->trustee_state = TRUSTEE_START;
3581 wake_up_process(gcwq->trustee);
3582 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003583 /* fall through */
3584 case CPU_UP_PREPARE:
3585 BUG_ON(gcwq->first_idle);
3586 gcwq->first_idle = new_worker;
3587 break;
3588
3589 case CPU_DYING:
3590 /*
3591 * Before this, the trustee and all workers except for
3592 * the ones which are still executing works from
3593 * before the last CPU down must be on the cpu. After
3594 * this, they'll all be diasporas.
3595 */
3596 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597 break;
3598
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003599 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003600 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003601 /* fall through */
3602 case CPU_UP_CANCELED:
3603 destroy_worker(gcwq->first_idle);
3604 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003605 break;
3606
3607 case CPU_DOWN_FAILED:
3608 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003609 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003610 if (gcwq->trustee_state != TRUSTEE_DONE) {
3611 gcwq->trustee_state = TRUSTEE_RELEASE;
3612 wake_up_process(gcwq->trustee);
3613 wait_trustee_state(gcwq, TRUSTEE_DONE);
3614 }
3615
Tejun Heoe22bee72010-06-29 10:07:14 +02003616 /*
3617 * Trustee is done and there might be no worker left.
3618 * Put the first_idle in and request a real manager to
3619 * take a look.
3620 */
3621 spin_unlock_irq(&gcwq->lock);
3622 kthread_bind(gcwq->first_idle->task, cpu);
3623 spin_lock_irq(&gcwq->lock);
3624 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3625 start_worker(gcwq->first_idle);
3626 gcwq->first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003627 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003628 }
3629
Tejun Heodb7bccf2010-06-29 10:07:12 +02003630 spin_unlock_irqrestore(&gcwq->lock, flags);
3631
Tejun Heo15376632010-06-29 10:07:11 +02003632 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Tejun Heod3b42542012-07-17 12:39:26 -07003635/*
3636 * Workqueues should be brought up before normal priority CPU notifiers.
3637 * This will be registered high priority CPU notifier.
3638 */
3639static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3640 unsigned long action,
3641 void *hcpu)
3642{
3643 switch (action & ~CPU_TASKS_FROZEN) {
3644 case CPU_UP_PREPARE:
3645 case CPU_UP_CANCELED:
3646 case CPU_DOWN_FAILED:
3647 case CPU_ONLINE:
3648 return workqueue_cpu_callback(nfb, action, hcpu);
3649 }
3650 return NOTIFY_OK;
3651}
3652
3653/*
3654 * Workqueues should be brought down after normal priority CPU notifiers.
3655 * This will be registered as low priority CPU notifier.
3656 */
3657static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3658 unsigned long action,
3659 void *hcpu)
3660{
3661 switch (action & ~CPU_TASKS_FROZEN) {
3662 case CPU_DOWN_PREPARE:
3663 case CPU_DYING:
3664 case CPU_POST_DEAD:
3665 return workqueue_cpu_callback(nfb, action, hcpu);
3666 }
3667 return NOTIFY_OK;
3668}
3669
Rusty Russell2d3854a2008-11-05 13:39:10 +11003670#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003671
Rusty Russell2d3854a2008-11-05 13:39:10 +11003672struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003673 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003674 long (*fn)(void *);
3675 void *arg;
3676 long ret;
3677};
3678
Tejun Heofc7da7e2012-09-18 12:48:43 -07003679static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003680{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003681 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3682
Rusty Russell2d3854a2008-11-05 13:39:10 +11003683 wfc->ret = wfc->fn(wfc->arg);
3684}
3685
3686/**
3687 * work_on_cpu - run a function in user context on a particular cpu
3688 * @cpu: the cpu to run on
3689 * @fn: the function to run
3690 * @arg: the function arg
3691 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003692 * This will return the value @fn returns.
3693 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003694 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003695 */
3696long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3697{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003698 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003699
Tejun Heofc7da7e2012-09-18 12:48:43 -07003700 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3701 schedule_work_on(cpu, &wfc.work);
3702 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003703 return wfc.ret;
3704}
3705EXPORT_SYMBOL_GPL(work_on_cpu);
3706#endif /* CONFIG_SMP */
3707
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003708#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303709
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003710/**
3711 * freeze_workqueues_begin - begin freezing workqueues
3712 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003713 * Start freezing workqueues. After this function returns, all freezable
3714 * workqueues will queue new works to their frozen_works list instead of
3715 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716 *
3717 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003718 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003719 */
3720void freeze_workqueues_begin(void)
3721{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003722 unsigned int cpu;
3723
3724 spin_lock(&workqueue_lock);
3725
3726 BUG_ON(workqueue_freezing);
3727 workqueue_freezing = true;
3728
Tejun Heof3421792010-07-02 10:03:51 +02003729 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003730 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003731 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003732
3733 spin_lock_irq(&gcwq->lock);
3734
Tejun Heodb7bccf2010-06-29 10:07:12 +02003735 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3736 gcwq->flags |= GCWQ_FREEZING;
3737
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003738 list_for_each_entry(wq, &workqueues, list) {
3739 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3740
Tejun Heo58a69cb2011-02-16 09:25:31 +01003741 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003742 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003743 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003744
3745 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003746 }
3747
3748 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003750
3751/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003752 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003753 *
3754 * Check whether freezing is complete. This function must be called
3755 * between freeze_workqueues_begin() and thaw_workqueues().
3756 *
3757 * CONTEXT:
3758 * Grabs and releases workqueue_lock.
3759 *
3760 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003761 * %true if some freezable workqueues are still busy. %false if freezing
3762 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003763 */
3764bool freeze_workqueues_busy(void)
3765{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003766 unsigned int cpu;
3767 bool busy = false;
3768
3769 spin_lock(&workqueue_lock);
3770
3771 BUG_ON(!workqueue_freezing);
3772
Tejun Heof3421792010-07-02 10:03:51 +02003773 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003774 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003775 /*
3776 * nr_active is monotonically decreasing. It's safe
3777 * to peek without lock.
3778 */
3779 list_for_each_entry(wq, &workqueues, list) {
3780 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3781
Tejun Heo58a69cb2011-02-16 09:25:31 +01003782 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003783 continue;
3784
3785 BUG_ON(cwq->nr_active < 0);
3786 if (cwq->nr_active) {
3787 busy = true;
3788 goto out_unlock;
3789 }
3790 }
3791 }
3792out_unlock:
3793 spin_unlock(&workqueue_lock);
3794 return busy;
3795}
3796
3797/**
3798 * thaw_workqueues - thaw workqueues
3799 *
3800 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003801 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003802 *
3803 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003804 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003805 */
3806void thaw_workqueues(void)
3807{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003808 unsigned int cpu;
3809
3810 spin_lock(&workqueue_lock);
3811
3812 if (!workqueue_freezing)
3813 goto out_unlock;
3814
Tejun Heof3421792010-07-02 10:03:51 +02003815 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003816 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003817 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003818
3819 spin_lock_irq(&gcwq->lock);
3820
Tejun Heodb7bccf2010-06-29 10:07:12 +02003821 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3822 gcwq->flags &= ~GCWQ_FREEZING;
3823
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003824 list_for_each_entry(wq, &workqueues, list) {
3825 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3826
Tejun Heo58a69cb2011-02-16 09:25:31 +01003827 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003828 continue;
3829
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003830 /* restore max_active and repopulate worklist */
3831 cwq->max_active = wq->saved_max_active;
3832
3833 while (!list_empty(&cwq->delayed_works) &&
3834 cwq->nr_active < cwq->max_active)
3835 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003836 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003837
Tejun Heoe22bee72010-06-29 10:07:14 +02003838 wake_up_worker(gcwq);
3839
Tejun Heo8b03ae32010-06-29 10:07:12 +02003840 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003841 }
3842
3843 workqueue_freezing = false;
3844out_unlock:
3845 spin_unlock(&workqueue_lock);
3846}
3847#endif /* CONFIG_FREEZER */
3848
Suresh Siddha6ee05782010-07-30 14:57:37 -07003849static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Tejun Heoc34056a2010-06-29 10:07:11 +02003851 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003852 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003853
Tejun Heod3b42542012-07-17 12:39:26 -07003854 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3855 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003856
3857 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003858 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003859 struct global_cwq *gcwq = get_gcwq(cpu);
3860
3861 spin_lock_init(&gcwq->lock);
Tejun Heo7e116292010-06-29 10:07:13 +02003862 INIT_LIST_HEAD(&gcwq->worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003863 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003864 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003865
Tejun Heoc8e55f32010-06-29 10:07:12 +02003866 INIT_LIST_HEAD(&gcwq->idle_list);
3867 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3868 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3869
Tejun Heoe22bee72010-06-29 10:07:14 +02003870 init_timer_deferrable(&gcwq->idle_timer);
3871 gcwq->idle_timer.function = idle_worker_timeout;
3872 gcwq->idle_timer.data = (unsigned long)gcwq;
3873
3874 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3875 (unsigned long)gcwq);
3876
Tejun Heo8b03ae32010-06-29 10:07:12 +02003877 ida_init(&gcwq->worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003878
3879 gcwq->trustee_state = TRUSTEE_DONE;
3880 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003881 }
3882
Tejun Heoe22bee72010-06-29 10:07:14 +02003883 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003884 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003885 struct global_cwq *gcwq = get_gcwq(cpu);
3886 struct worker *worker;
3887
Tejun Heo477a3c32010-08-31 10:54:35 +02003888 if (cpu != WORK_CPU_UNBOUND)
3889 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe22bee72010-06-29 10:07:14 +02003890 worker = create_worker(gcwq, true);
3891 BUG_ON(!worker);
3892 spin_lock_irq(&gcwq->lock);
3893 start_worker(worker);
3894 spin_unlock_irq(&gcwq->lock);
3895 }
3896
Tejun Heod320c032010-06-29 10:07:14 +02003897 system_wq = alloc_workqueue("events", 0, 0);
3898 system_long_wq = alloc_workqueue("events_long", 0, 0);
3899 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003900 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3901 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003902 system_freezable_wq = alloc_workqueue("events_freezable",
3903 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003904 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3905 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003906 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003907 !system_unbound_wq || !system_freezable_wq ||
3908 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003911early_initcall(init_workqueues);