blob: b5779df579f0581c16d4a60a11a5d78d81f37cf4 [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>
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -070044#include <linux/bug.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
46#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020049 /* global_cwq flags */
Tejun Heo22ad5642012-07-12 14:46:37 -070050 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
51 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
52
53 /* pool flags */
54 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
55 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020056
Tejun Heoc8e55f32010-06-29 10:07:12 +020057 /* worker flags */
58 WORKER_STARTED = 1 << 0, /* started */
59 WORKER_DIE = 1 << 1, /* die die die */
60 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020062 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020063 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020064 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020065 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020066
Tejun Heofb0e7be2010-06-29 10:07:15 +020067 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020068 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
70 /* gcwq->trustee_state */
71 TRUSTEE_START = 0, /* start */
72 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
73 TRUSTEE_BUTCHER = 2, /* butcher workers */
74 TRUSTEE_RELEASE = 3, /* release workers */
75 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020076
Tejun Heodcb32ee2012-07-13 22:16:45 -070077 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo9c6bae02012-07-13 22:16:44 -070078
Tejun Heoc8e55f32010-06-29 10:07:12 +020079 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
80 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
81 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heoe22bee72010-06-29 10:07:14 +020083 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
84 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
85
Tejun Heo3233cdb2011-02-16 18:10:19 +010086 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
87 /* call for help after 10ms
88 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
90 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020091 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020092
93 /*
94 * Rescue workers are used only on emergencies and shared by
95 * all cpus. Give -20.
96 */
97 RESCUER_NICE_LEVEL = -20,
Tejun Heodcb32ee2012-07-13 22:16:45 -070098 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020099};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200102 * Structure fields follow one of the following exclusion rules.
103 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200104 * I: Modifiable by initialization/destruction paths and read-only for
105 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200107 * P: Preemption protected. Disabling preemption is enough and should
108 * only be modified and accessed from the local cpu.
109 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200110 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200111 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200112 * X: During normal operation, modification requires gcwq->lock and
113 * should be done only from local cpu. Either disabling preemption
114 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200115 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200116 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200117 * F: wq->flush_mutex protected.
118 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200119 * W: workqueue_lock protected.
120 */
121
Tejun Heo8b03ae32010-06-29 10:07:12 +0200122struct global_cwq;
Tejun Heo58658882012-07-12 14:46:37 -0700123struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heoe22bee72010-06-29 10:07:14 +0200125/*
126 * The poor guys doing the actual heavy lifting. All on-duty workers
127 * are either serving the manager role, on idle list or on busy hash.
128 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200129struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200130 /* on idle list while idle, on busy hash table while busy */
131 union {
132 struct list_head entry; /* L: while idle */
133 struct hlist_node hentry; /* L: while busy */
134 };
135
Tejun Heoc34056a2010-06-29 10:07:11 +0200136 struct work_struct *current_work; /* L: work being processed */
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800137 work_func_t current_func; /* L: current_work's fn */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200138 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200139 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200140 struct task_struct *task; /* I: worker task */
Tejun Heo58658882012-07-12 14:46:37 -0700141 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200142 /* 64 bytes boundary on 64bit, 32 on 32bit */
143 unsigned long last_active; /* L: last active timestamp */
144 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200145 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200146 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200147};
148
Tejun Heo58658882012-07-12 14:46:37 -0700149struct worker_pool {
150 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo22ad5642012-07-12 14:46:37 -0700151 unsigned int flags; /* X: flags */
Tejun Heo58658882012-07-12 14:46:37 -0700152
153 struct list_head worklist; /* L: list of pending works */
154 int nr_workers; /* L: total number of workers */
155 int nr_idle; /* L: currently idle ones */
156
157 struct list_head idle_list; /* X: list of idle workers */
158 struct timer_list idle_timer; /* L: worker idle timeout */
159 struct timer_list mayday_timer; /* L: SOS timer for workers */
160
161 struct ida worker_ida; /* L: for worker IDs */
162 struct worker *first_idle; /* L: first idle worker */
163};
164
Tejun Heo4690c4a2010-06-29 10:07:10 +0200165/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200166 * Global per-cpu workqueue. There's one and only one for each cpu
167 * and all works are queued and processed here regardless of their
168 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200169 */
170struct global_cwq {
171 spinlock_t lock; /* the gcwq lock */
172 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200173 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200174
Tejun Heo58658882012-07-12 14:46:37 -0700175 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200176 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
177 /* L: hash of busy workers */
178
Tejun Heodcb32ee2012-07-13 22:16:45 -0700179 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200180
181 struct task_struct *trustee; /* L: for gcwq shutdown */
182 unsigned int trustee_state; /* L: trustee state */
183 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200184} ____cacheline_aligned_in_smp;
185
186/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200187 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200188 * work_struct->data are used for flags and thus cwqs need to be
189 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191struct cpu_workqueue_struct {
Tejun Heo58658882012-07-12 14:46:37 -0700192 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200193 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194 int work_color; /* L: current color */
195 int flush_color; /* L: flushing color */
196 int nr_in_flight[WORK_NR_COLORS];
197 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200199 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200200 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200201};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200204 * Structure used to wait for workqueue flush.
205 */
206struct wq_flusher {
207 struct list_head list; /* F: list of flushers */
208 int flush_color; /* F: flush color waiting for */
209 struct completion done; /* flush completion */
210};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Tejun Heo73f53c42010-06-29 10:07:11 +0200212/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200213 * All cpumasks are assumed to be always set on UP and thus can't be
214 * used to determine whether there's something to be done.
215 */
216#ifdef CONFIG_SMP
217typedef cpumask_var_t mayday_mask_t;
218#define mayday_test_and_set_cpu(cpu, mask) \
219 cpumask_test_and_set_cpu((cpu), (mask))
220#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
221#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200222#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200223#define free_mayday_mask(mask) free_cpumask_var((mask))
224#else
225typedef unsigned long mayday_mask_t;
226#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
227#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
228#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
229#define alloc_mayday_mask(maskp, gfp) true
230#define free_mayday_mask(mask) do { } while (0)
231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * The externally visible workqueue abstraction is an array of
235 * per-CPU workqueues:
236 */
237struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200238 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200239 union {
240 struct cpu_workqueue_struct __percpu *pcpu;
241 struct cpu_workqueue_struct *single;
242 unsigned long v;
243 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200245
246 struct mutex flush_mutex; /* protects wq flushing */
247 int work_color; /* F: current work color */
248 int flush_color; /* F: current flush color */
249 atomic_t nr_cwqs_to_flush; /* flush in progress */
250 struct wq_flusher *first_flusher; /* F: first flusher */
251 struct list_head flusher_queue; /* F: flush waiters */
252 struct list_head flusher_overflow; /* F: flush overflow list */
253
Tejun Heof2e005a2010-07-20 15:59:09 +0200254 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200255 struct worker *rescuer; /* I: rescue worker */
256
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200257 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200258 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700259#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200260 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700261#endif
Tejun Heob196be82012-01-10 15:11:35 -0800262 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
Tejun Heod320c032010-06-29 10:07:14 +0200265struct workqueue_struct *system_wq __read_mostly;
266struct workqueue_struct *system_long_wq __read_mostly;
267struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200268struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100269struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100270struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200271EXPORT_SYMBOL_GPL(system_wq);
272EXPORT_SYMBOL_GPL(system_long_wq);
273EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200274EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100275EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100276EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200277
Tejun Heo97bd2342010-10-05 10:41:14 +0200278#define CREATE_TRACE_POINTS
279#include <trace/events/workqueue.h>
280
Tejun Heo9c6bae02012-07-13 22:16:44 -0700281#define for_each_worker_pool(pool, gcwq) \
Tejun Heodcb32ee2012-07-13 22:16:45 -0700282 for ((pool) = &(gcwq)->pools[0]; \
283 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700284
Tejun Heodb7bccf2010-06-29 10:07:12 +0200285#define for_each_busy_worker(worker, i, pos, gcwq) \
286 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
287 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
288
Tejun Heof3421792010-07-02 10:03:51 +0200289static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
290 unsigned int sw)
291{
292 if (cpu < nr_cpu_ids) {
293 if (sw & 1) {
294 cpu = cpumask_next(cpu, mask);
295 if (cpu < nr_cpu_ids)
296 return cpu;
297 }
298 if (sw & 2)
299 return WORK_CPU_UNBOUND;
300 }
301 return WORK_CPU_NONE;
302}
303
304static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
305 struct workqueue_struct *wq)
306{
307 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
308}
309
Tejun Heo09884952010-08-01 11:50:12 +0200310/*
311 * CPU iterators
312 *
313 * An extra gcwq is defined for an invalid cpu number
314 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
315 * specific CPU. The following iterators are similar to
316 * for_each_*_cpu() iterators but also considers the unbound gcwq.
317 *
318 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
319 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
320 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
321 * WORK_CPU_UNBOUND for unbound workqueues
322 */
Tejun Heof3421792010-07-02 10:03:51 +0200323#define for_each_gcwq_cpu(cpu) \
324 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
325 (cpu) < WORK_CPU_NONE; \
326 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
327
328#define for_each_online_gcwq_cpu(cpu) \
329 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
330 (cpu) < WORK_CPU_NONE; \
331 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
332
333#define for_each_cwq_cpu(cpu, wq) \
334 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
335 (cpu) < WORK_CPU_NONE; \
336 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
337
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900338#ifdef CONFIG_DEBUG_OBJECTS_WORK
339
340static struct debug_obj_descr work_debug_descr;
341
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100342static void *work_debug_hint(void *addr)
343{
344 return ((struct work_struct *) addr)->func;
345}
346
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900347/*
348 * fixup_init is called when:
349 * - an active object is initialized
350 */
351static int work_fixup_init(void *addr, enum debug_obj_state state)
352{
353 struct work_struct *work = addr;
354
355 switch (state) {
356 case ODEBUG_STATE_ACTIVE:
357 cancel_work_sync(work);
358 debug_object_init(work, &work_debug_descr);
359 return 1;
360 default:
361 return 0;
362 }
363}
364
365/*
366 * fixup_activate is called when:
367 * - an active object is activated
368 * - an unknown object is activated (might be a statically initialized object)
369 */
370static int work_fixup_activate(void *addr, enum debug_obj_state state)
371{
372 struct work_struct *work = addr;
373
374 switch (state) {
375
376 case ODEBUG_STATE_NOTAVAILABLE:
377 /*
378 * This is not really a fixup. The work struct was
379 * statically initialized. We just make sure that it
380 * is tracked in the object tracker.
381 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200382 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900383 debug_object_init(work, &work_debug_descr);
384 debug_object_activate(work, &work_debug_descr);
385 return 0;
386 }
387 WARN_ON_ONCE(1);
388 return 0;
389
390 case ODEBUG_STATE_ACTIVE:
391 WARN_ON(1);
392
393 default:
394 return 0;
395 }
396}
397
398/*
399 * fixup_free is called when:
400 * - an active object is freed
401 */
402static int work_fixup_free(void *addr, enum debug_obj_state state)
403{
404 struct work_struct *work = addr;
405
406 switch (state) {
407 case ODEBUG_STATE_ACTIVE:
408 cancel_work_sync(work);
409 debug_object_free(work, &work_debug_descr);
410 return 1;
411 default:
412 return 0;
413 }
414}
415
416static struct debug_obj_descr work_debug_descr = {
417 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100418 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900419 .fixup_init = work_fixup_init,
420 .fixup_activate = work_fixup_activate,
421 .fixup_free = work_fixup_free,
422};
423
424static inline void debug_work_activate(struct work_struct *work)
425{
426 debug_object_activate(work, &work_debug_descr);
427}
428
429static inline void debug_work_deactivate(struct work_struct *work)
430{
431 debug_object_deactivate(work, &work_debug_descr);
432}
433
434void __init_work(struct work_struct *work, int onstack)
435{
436 if (onstack)
437 debug_object_init_on_stack(work, &work_debug_descr);
438 else
439 debug_object_init(work, &work_debug_descr);
440}
441EXPORT_SYMBOL_GPL(__init_work);
442
443void destroy_work_on_stack(struct work_struct *work)
444{
445 debug_object_free(work, &work_debug_descr);
446}
447EXPORT_SYMBOL_GPL(destroy_work_on_stack);
448
449#else
450static inline void debug_work_activate(struct work_struct *work) { }
451static inline void debug_work_deactivate(struct work_struct *work) { }
452#endif
453
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100454/* Serializes the accesses to the list of workqueues. */
455static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200457static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Oleg Nesterov14441962007-05-23 13:57:57 -0700459/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200460 * The almighty global cpu workqueues. nr_running is the only field
461 * which is expected to be used frequently by other cpus via
462 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700463 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200464static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo9c6bae02012-07-13 22:16:44 -0700465static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800466
Tejun Heof3421792010-07-02 10:03:51 +0200467/*
468 * Global cpu workqueue and nr_running counter for unbound gcwq. The
469 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
470 * workers have WORKER_UNBOUND set.
471 */
472static struct global_cwq unbound_global_cwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -0700473static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
474 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
475};
Tejun Heof3421792010-07-02 10:03:51 +0200476
Tejun Heoc34056a2010-06-29 10:07:11 +0200477static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Tejun Heodcb32ee2012-07-13 22:16:45 -0700479static int worker_pool_pri(struct worker_pool *pool)
480{
481 return pool - pool->gcwq->pools;
482}
483
Tejun Heo8b03ae32010-06-29 10:07:12 +0200484static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Tejun Heof3421792010-07-02 10:03:51 +0200486 if (cpu != WORK_CPU_UNBOUND)
487 return &per_cpu(global_cwq, cpu);
488 else
489 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Tejun Heo7ef6a932012-07-12 14:46:37 -0700492static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700493{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700494 int cpu = pool->gcwq->cpu;
Tejun Heodcb32ee2012-07-13 22:16:45 -0700495 int idx = worker_pool_pri(pool);
Tejun Heo7ef6a932012-07-12 14:46:37 -0700496
Tejun Heof3421792010-07-02 10:03:51 +0200497 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700498 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200499 else
Tejun Heo9c6bae02012-07-13 22:16:44 -0700500 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700501}
502
Tejun Heo4690c4a2010-06-29 10:07:10 +0200503static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
504 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700505{
Tejun Heof3421792010-07-02 10:03:51 +0200506 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800507 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200508 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200509 } else if (likely(cpu == WORK_CPU_UNBOUND))
510 return wq->cpu_wq.single;
511 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700512}
513
Tejun Heo73f53c42010-06-29 10:07:11 +0200514static unsigned int work_color_to_flags(int color)
515{
516 return color << WORK_STRUCT_COLOR_SHIFT;
517}
518
519static int get_work_color(struct work_struct *work)
520{
521 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
522 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
523}
524
525static int work_next_color(int color)
526{
527 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
David Howells4594bf12006-12-07 11:33:26 +0000530/*
Tejun Heoe1201532010-07-22 14:14:25 +0200531 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
532 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
533 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200534 *
535 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
536 * cwq, cpu or clear work->data. These functions should only be
537 * called while the work is owned - ie. while the PENDING bit is set.
538 *
539 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
540 * corresponding to a work. gcwq is available once the work has been
541 * queued anywhere after initialization. cwq is available only from
542 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000543 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200544static inline void set_work_data(struct work_struct *work, unsigned long data,
545 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000546{
David Howells4594bf12006-12-07 11:33:26 +0000547 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000549}
David Howells365970a2006-11-22 14:54:49 +0000550
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551static void set_work_cwq(struct work_struct *work,
552 struct cpu_workqueue_struct *cwq,
553 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200554{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200556 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200557}
558
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000560{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
562}
563
564static void clear_work_data(struct work_struct *work)
565{
566 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
567}
568
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
570{
Tejun Heoe1201532010-07-22 14:14:25 +0200571 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572
Tejun Heoe1201532010-07-22 14:14:25 +0200573 if (data & WORK_STRUCT_CWQ)
574 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
575 else
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530576 {
577 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200578 return NULL;
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530579 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580}
581
582static struct global_cwq *get_work_gcwq(struct work_struct *work)
583{
Tejun Heoe1201532010-07-22 14:14:25 +0200584 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585 unsigned int cpu;
586
Tejun Heoe1201532010-07-22 14:14:25 +0200587 if (data & WORK_STRUCT_CWQ)
588 return ((struct cpu_workqueue_struct *)
Tejun Heo58658882012-07-12 14:46:37 -0700589 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590
591 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200592 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200593 return NULL;
594
Tejun Heof3421792010-07-02 10:03:51 +0200595 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000597}
598
599/*
Tejun Heodcb32ee2012-07-13 22:16:45 -0700600 * Policy functions. These define the policies on how the global worker
601 * pools are managed. Unless noted otherwise, these functions assume that
602 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000603 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200604
Tejun Heo7ef6a932012-07-12 14:46:37 -0700605static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000606{
Tejun Heodcb32ee2012-07-13 22:16:45 -0700607 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000608}
609
Tejun Heoe22bee72010-06-29 10:07:14 +0200610/*
611 * Need to wake up a worker? Called from anything but currently
612 * running workers.
Tejun Heob7b5c682012-07-12 14:46:37 -0700613 *
614 * Note that, because unbound workers never contribute to nr_running, this
615 * function will always return %true for unbound gcwq as long as the
616 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200617 */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700618static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000619{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700620 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000621}
622
Tejun Heoe22bee72010-06-29 10:07:14 +0200623/* Can I start working? Called from busy but !running workers. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700624static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200625{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700626 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200627}
628
629/* Do I need to keep working? Called from currently running workers. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700630static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200631{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700632 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200633
Tejun Heodcb32ee2012-07-13 22:16:45 -0700634 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200635}
636
637/* Do we need a new worker? Called from manager. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700638static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200639{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700640 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200641}
642
643/* Do I need to be the manager? */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700644static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200645{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700646 return need_to_create_worker(pool) ||
Tejun Heo22ad5642012-07-12 14:46:37 -0700647 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200648}
649
650/* Do we have too many workers and should some go away? */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700651static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200652{
Tejun Heo22ad5642012-07-12 14:46:37 -0700653 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -0700654 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
655 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200656
657 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
658}
659
660/*
661 * Wake up functions.
662 */
663
Tejun Heo7e116292010-06-29 10:07:13 +0200664/* Return the first worker. Safe with preemption disabled */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700665static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200666{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700667 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200668 return NULL;
669
Tejun Heo7ef6a932012-07-12 14:46:37 -0700670 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200671}
672
673/**
674 * wake_up_worker - wake up an idle worker
Tejun Heo7ef6a932012-07-12 14:46:37 -0700675 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200676 *
Tejun Heo7ef6a932012-07-12 14:46:37 -0700677 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200678 *
679 * CONTEXT:
680 * spin_lock_irq(gcwq->lock).
681 */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700682static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200683{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700684 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200685
686 if (likely(worker))
687 wake_up_process(worker->task);
688}
689
Tejun Heo4690c4a2010-06-29 10:07:10 +0200690/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200691 * wq_worker_waking_up - a worker is waking up
692 * @task: task waking up
693 * @cpu: CPU @task is waking up to
694 *
695 * This function is called during try_to_wake_up() when a worker is
696 * being awoken.
697 *
698 * CONTEXT:
699 * spin_lock_irq(rq->lock)
700 */
701void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
702{
703 struct worker *worker = kthread_data(task);
704
Steven Rostedt2d646722010-12-03 23:12:33 -0500705 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700706 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200707}
708
709/**
710 * wq_worker_sleeping - a worker is going to sleep
711 * @task: task going to sleep
712 * @cpu: CPU in question, must be the current CPU number
713 *
714 * This function is called during schedule() when a busy worker is
715 * going to sleep. Worker on the same cpu can be woken up by
716 * returning pointer to its task.
717 *
718 * CONTEXT:
719 * spin_lock_irq(rq->lock)
720 *
721 * RETURNS:
722 * Worker task on @cpu to wake up, %NULL if none.
723 */
724struct task_struct *wq_worker_sleeping(struct task_struct *task,
725 unsigned int cpu)
726{
727 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo58658882012-07-12 14:46:37 -0700728 struct worker_pool *pool = worker->pool;
Tejun Heo7ef6a932012-07-12 14:46:37 -0700729 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200730
Steven Rostedt2d646722010-12-03 23:12:33 -0500731 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200732 return NULL;
733
734 /* this can only happen on the local cpu */
735 BUG_ON(cpu != raw_smp_processor_id());
736
737 /*
738 * The counterpart of the following dec_and_test, implied mb,
739 * worklist not empty test sequence is in insert_work().
740 * Please read comment there.
741 *
742 * NOT_RUNNING is clear. This means that trustee is not in
743 * charge and we're running on the local cpu w/ rq lock held
744 * and preemption disabled, which in turn means that none else
745 * could be manipulating idle_list, so dereferencing idle_list
746 * without gcwq lock is safe.
747 */
Tejun Heo58658882012-07-12 14:46:37 -0700748 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700749 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200750 return to_wakeup ? to_wakeup->task : NULL;
751}
752
753/**
754 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200755 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200756 * @flags: flags to set
757 * @wakeup: wakeup an idle worker if necessary
758 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200759 * Set @flags in @worker->flags and adjust nr_running accordingly. If
760 * nr_running becomes zero and @wakeup is %true, an idle worker is
761 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200762 *
Tejun Heocb444762010-07-02 10:03:50 +0200763 * CONTEXT:
764 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200765 */
766static inline void worker_set_flags(struct worker *worker, unsigned int flags,
767 bool wakeup)
768{
Tejun Heo58658882012-07-12 14:46:37 -0700769 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200770
Tejun Heocb444762010-07-02 10:03:50 +0200771 WARN_ON_ONCE(worker->task != current);
772
Tejun Heoe22bee72010-06-29 10:07:14 +0200773 /*
774 * If transitioning into NOT_RUNNING, adjust nr_running and
775 * wake up an idle worker as necessary if requested by
776 * @wakeup.
777 */
778 if ((flags & WORKER_NOT_RUNNING) &&
779 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -0700780 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200781
782 if (wakeup) {
783 if (atomic_dec_and_test(nr_running) &&
Tejun Heo58658882012-07-12 14:46:37 -0700784 !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700785 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200786 } else
787 atomic_dec(nr_running);
788 }
789
Tejun Heod302f012010-06-29 10:07:13 +0200790 worker->flags |= flags;
791}
792
793/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200794 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200795 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200796 * @flags: flags to clear
797 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200798 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200799 *
Tejun Heocb444762010-07-02 10:03:50 +0200800 * CONTEXT:
801 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200802 */
803static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
804{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700805 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200806 unsigned int oflags = worker->flags;
807
Tejun Heocb444762010-07-02 10:03:50 +0200808 WARN_ON_ONCE(worker->task != current);
809
Tejun Heod302f012010-06-29 10:07:13 +0200810 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200811
Tejun Heo42c025f2011-01-11 15:58:49 +0100812 /*
813 * If transitioning out of NOT_RUNNING, increment nr_running. Note
814 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
815 * of multiple flags, not a single flag.
816 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
818 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700819 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200820}
821
822/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200823 * busy_worker_head - return the busy hash head for a work
824 * @gcwq: gcwq of interest
825 * @work: work to be hashed
826 *
827 * Return hash head of @gcwq for @work.
828 *
829 * CONTEXT:
830 * spin_lock_irq(gcwq->lock).
831 *
832 * RETURNS:
833 * Pointer to the hash head.
834 */
835static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
836 struct work_struct *work)
837{
838 const int base_shift = ilog2(sizeof(struct work_struct));
839 unsigned long v = (unsigned long)work;
840
841 /* simple shift and fold hash, do we need something better? */
842 v >>= base_shift;
843 v += v >> BUSY_WORKER_HASH_ORDER;
844 v &= BUSY_WORKER_HASH_MASK;
845
846 return &gcwq->busy_hash[v];
847}
848
849/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200850 * __find_worker_executing_work - find worker which is executing a work
851 * @gcwq: gcwq of interest
852 * @bwh: hash head as returned by busy_worker_head()
853 * @work: work to find worker for
854 *
855 * Find a worker which is executing @work on @gcwq. @bwh should be
856 * the hash head obtained by calling busy_worker_head() with the same
857 * work.
858 *
859 * CONTEXT:
860 * spin_lock_irq(gcwq->lock).
861 *
862 * RETURNS:
863 * Pointer to worker which is executing @work if found, NULL
864 * otherwise.
865 */
866static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
867 struct hlist_head *bwh,
868 struct work_struct *work)
869{
870 struct worker *worker;
871 struct hlist_node *tmp;
872
873 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800874 if (worker->current_work == work &&
875 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200876 return worker;
877 return NULL;
878}
879
880/**
881 * find_worker_executing_work - find worker which is executing a work
882 * @gcwq: gcwq of interest
883 * @work: work to find worker for
884 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800885 * Find a worker which is executing @work on @gcwq by searching
886 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
887 * to match, its current execution should match the address of @work and
888 * its work function. This is to avoid unwanted dependency between
889 * unrelated work executions through a work item being recycled while still
890 * being executed.
891 *
892 * This is a bit tricky. A work item may be freed once its execution
893 * starts and nothing prevents the freed area from being recycled for
894 * another work item. If the same work item address ends up being reused
895 * before the original execution finishes, workqueue will identify the
896 * recycled work item as currently executing and make it wait until the
897 * current execution finishes, introducing an unwanted dependency.
898 *
899 * This function checks the work item address, work function and workqueue
900 * to avoid false positives. Note that this isn't complete as one may
901 * construct a work function which can introduce dependency onto itself
902 * through a recycled work item. Well, if somebody wants to shoot oneself
903 * in the foot that badly, there's only so much we can do, and if such
904 * deadlock actually occurs, it should be easy to locate the culprit work
905 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200906 *
907 * CONTEXT:
908 * spin_lock_irq(gcwq->lock).
909 *
910 * RETURNS:
911 * Pointer to worker which is executing @work if found, NULL
912 * otherwise.
913 */
914static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
915 struct work_struct *work)
916{
917 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
918 work);
919}
920
921/**
Tejun Heo7e116292010-06-29 10:07:13 +0200922 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200923 * @cwq: cwq @work belongs to
924 * @work: work to insert
925 * @head: insertion point
926 * @extra_flags: extra WORK_STRUCT_* flags to set
927 *
Tejun Heo7e116292010-06-29 10:07:13 +0200928 * Insert @work which belongs to @cwq into @gcwq after @head.
929 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200930 *
931 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200932 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200933 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700934static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200935 struct work_struct *work, struct list_head *head,
936 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700937{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700938 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100939
Tejun Heo4690c4a2010-06-29 10:07:10 +0200940 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200941 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200942
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700943 /*
944 * Ensure that we get the right work->data if we see the
945 * result of list_add() below, see try_to_grab_pending().
946 */
947 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200948
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700949 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200950
951 /*
952 * Ensure either worker_sched_deactivated() sees the above
953 * list_add_tail() or we see zero nr_running to avoid workers
954 * lying around lazily while there are works to be processed.
955 */
956 smp_mb();
957
Tejun Heo7ef6a932012-07-12 14:46:37 -0700958 if (__need_more_worker(pool))
959 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700960}
961
Tejun Heoc8efcc22010-12-20 19:32:04 +0100962/*
963 * Test whether @work is being queued from another work executing on the
964 * same workqueue. This is rather expensive and should only be used from
965 * cold paths.
966 */
967static bool is_chained_work(struct workqueue_struct *wq)
968{
969 unsigned long flags;
970 unsigned int cpu;
971
972 for_each_gcwq_cpu(cpu) {
973 struct global_cwq *gcwq = get_gcwq(cpu);
974 struct worker *worker;
975 struct hlist_node *pos;
976 int i;
977
978 spin_lock_irqsave(&gcwq->lock, flags);
979 for_each_busy_worker(worker, i, pos, gcwq) {
980 if (worker->task != current)
981 continue;
982 spin_unlock_irqrestore(&gcwq->lock, flags);
983 /*
984 * I'm @worker, no locking necessary. See if @work
985 * is headed to the same workqueue.
986 */
987 return worker->current_cwq->wq == wq;
988 }
989 spin_unlock_irqrestore(&gcwq->lock, flags);
990 }
991 return false;
992}
993
Tejun Heo4690c4a2010-06-29 10:07:10 +0200994static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct work_struct *work)
996{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200997 struct global_cwq *gcwq;
998 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200999 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001000 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 unsigned long flags;
1002
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001003 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001004
Tejun Heoc8efcc22010-12-20 19:32:04 +01001005 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001006 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001007 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001008 return;
1009
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001010 /* determine gcwq to use */
1011 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001012 struct global_cwq *last_gcwq;
1013
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001014 if (unlikely(cpu == WORK_CPU_UNBOUND))
1015 cpu = raw_smp_processor_id();
1016
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001017 /*
1018 * It's multi cpu. If @wq is non-reentrant and @work
1019 * was previously on a different cpu, it might still
1020 * be running there, in which case the work needs to
1021 * be queued on that cpu to guarantee non-reentrance.
1022 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001023 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001024 if (wq->flags & WQ_NON_REENTRANT &&
1025 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1026 struct worker *worker;
1027
1028 spin_lock_irqsave(&last_gcwq->lock, flags);
1029
1030 worker = find_worker_executing_work(last_gcwq, work);
1031
1032 if (worker && worker->current_cwq->wq == wq)
1033 gcwq = last_gcwq;
1034 else {
1035 /* meh... not running there, queue here */
1036 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1037 spin_lock_irqsave(&gcwq->lock, flags);
1038 }
1039 } else
1040 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001041 } else {
1042 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1043 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001044 }
1045
1046 /* gcwq determined, get cwq and queue */
1047 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001048 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001049
Tejun Heo4690c4a2010-06-29 10:07:10 +02001050 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001051
Tejun Heo73f53c42010-06-29 10:07:11 +02001052 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001053 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001054
1055 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001056 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001057 cwq->nr_active++;
Tejun Heodcb32ee2012-07-13 22:16:45 -07001058 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001059 } else {
1060 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001061 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001062 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001063
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001064 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001065
Tejun Heo8b03ae32010-06-29 10:07:12 +02001066 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001069/**
1070 * queue_work - queue work on a workqueue
1071 * @wq: workqueue to use
1072 * @work: work to queue
1073 *
Alan Stern057647f2006-10-28 10:38:58 -07001074 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001076 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1077 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001079int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001081 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001083 ret = queue_work_on(get_cpu(), wq, work);
1084 put_cpu();
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return ret;
1087}
Dave Jonesae90dd52006-06-30 01:40:45 -04001088EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Zhang Ruic1a220e2008-07-23 21:28:39 -07001090/**
1091 * queue_work_on - queue work on specific cpu
1092 * @cpu: CPU number to execute work on
1093 * @wq: workqueue to use
1094 * @work: work to queue
1095 *
1096 * Returns 0 if @work was already on a queue, non-zero otherwise.
1097 *
1098 * We queue the work to a specific CPU, the caller must ensure it
1099 * can't go away.
1100 */
1101int
1102queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1103{
1104 int ret = 0;
1105
Tejun Heo22df02b2010-06-29 10:07:10 +02001106 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001107 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001108 ret = 1;
1109 }
1110 return ret;
1111}
1112EXPORT_SYMBOL_GPL(queue_work_on);
1113
Li Zefan6d141c32008-02-08 04:21:09 -08001114static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
David Howells52bad642006-11-22 14:54:01 +00001116 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001117 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Srinivasarao Pb6e586c2013-09-18 14:33:45 +05301119 if (cwq != NULL)
1120 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001123/**
1124 * queue_delayed_work - queue work on a workqueue after delay
1125 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001126 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001127 * @delay: number of jiffies to wait before queueing
1128 *
Alan Stern057647f2006-10-28 10:38:58 -07001129 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001130 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001131int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001132 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
David Howells52bad642006-11-22 14:54:01 +00001134 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001135 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001137 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Dave Jonesae90dd52006-06-30 01:40:45 -04001139EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001141/**
1142 * queue_delayed_work_on - queue work on specific CPU after delay
1143 * @cpu: CPU number to execute work on
1144 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001145 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001146 * @delay: number of jiffies to wait before queueing
1147 *
Alan Stern057647f2006-10-28 10:38:58 -07001148 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001149 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001150int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001151 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001152{
1153 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001154 struct timer_list *timer = &dwork->timer;
1155 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001156
Tejun Heo22df02b2010-06-29 10:07:10 +02001157 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001158 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001159
Tejun Heo4afca922012-12-04 07:40:39 -08001160 WARN_ON_ONCE(timer_pending(timer));
1161 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001162
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001163 timer_stats_timer_set_start_info(&dwork->timer);
1164
Tejun Heo7a22ad72010-06-29 10:07:13 +02001165 /*
1166 * This stores cwq for the moment, for the timer_fn.
1167 * Note that the work's gcwq is preserved to allow
1168 * reentrance detection for delayed works.
1169 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001170 if (!(wq->flags & WQ_UNBOUND)) {
1171 struct global_cwq *gcwq = get_work_gcwq(work);
1172
1173 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1174 lcpu = gcwq->cpu;
1175 else
1176 lcpu = raw_smp_processor_id();
1177 } else
1178 lcpu = WORK_CPU_UNBOUND;
1179
Tejun Heo7a22ad72010-06-29 10:07:13 +02001180 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001181
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001182 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001183 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001184 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001185
1186 if (unlikely(cpu >= 0))
1187 add_timer_on(timer, cpu);
1188 else
1189 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001190 ret = 1;
1191 }
1192 return ret;
1193}
Dave Jonesae90dd52006-06-30 01:40:45 -04001194EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Tejun Heoc8e55f32010-06-29 10:07:12 +02001196/**
1197 * worker_enter_idle - enter idle state
1198 * @worker: worker which is entering idle state
1199 *
1200 * @worker is entering idle state. Update stats and idle timer if
1201 * necessary.
1202 *
1203 * LOCKING:
1204 * spin_lock_irq(gcwq->lock).
1205 */
1206static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Tejun Heo58658882012-07-12 14:46:37 -07001208 struct worker_pool *pool = worker->pool;
1209 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Tejun Heoc8e55f32010-06-29 10:07:12 +02001211 BUG_ON(worker->flags & WORKER_IDLE);
1212 BUG_ON(!list_empty(&worker->entry) &&
1213 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Tejun Heocb444762010-07-02 10:03:50 +02001215 /* can't use worker_set_flags(), also called from start_worker() */
1216 worker->flags |= WORKER_IDLE;
Tejun Heo58658882012-07-12 14:46:37 -07001217 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001218 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001219
Tejun Heoc8e55f32010-06-29 10:07:12 +02001220 /* idle_list is LIFO */
Tejun Heo58658882012-07-12 14:46:37 -07001221 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001222
Tejun Heoe22bee72010-06-29 10:07:14 +02001223 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001224 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo58658882012-07-12 14:46:37 -07001225 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001226 jiffies + IDLE_WORKER_TIMEOUT);
1227 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001228 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001229
Tejun Heo24312d32012-05-14 15:04:50 -07001230 /*
1231 * Sanity check nr_running. Because trustee releases gcwq->lock
1232 * between setting %WORKER_ROGUE and zapping nr_running, the
1233 * warning may trigger spuriously. Check iff trustee is idle.
1234 */
1235 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo58658882012-07-12 14:46:37 -07001236 pool->nr_workers == pool->nr_idle &&
Tejun Heo7ef6a932012-07-12 14:46:37 -07001237 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Tejun Heoc8e55f32010-06-29 10:07:12 +02001240/**
1241 * worker_leave_idle - leave idle state
1242 * @worker: worker which is leaving idle state
1243 *
1244 * @worker is leaving idle state. Update stats.
1245 *
1246 * LOCKING:
1247 * spin_lock_irq(gcwq->lock).
1248 */
1249static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Tejun Heo58658882012-07-12 14:46:37 -07001251 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Tejun Heoc8e55f32010-06-29 10:07:12 +02001253 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001254 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo58658882012-07-12 14:46:37 -07001255 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001256 list_del_init(&worker->entry);
1257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Tejun Heoe22bee72010-06-29 10:07:14 +02001259/**
1260 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1261 * @worker: self
1262 *
1263 * Works which are scheduled while the cpu is online must at least be
1264 * scheduled to a worker which is bound to the cpu so that if they are
1265 * flushed from cpu callbacks while cpu is going down, they are
1266 * guaranteed to execute on the cpu.
1267 *
1268 * This function is to be used by rogue workers and rescuers to bind
1269 * themselves to the target cpu and may race with cpu going down or
1270 * coming online. kthread_bind() can't be used because it may put the
1271 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1272 * verbatim as it's best effort and blocking and gcwq may be
1273 * [dis]associated in the meantime.
1274 *
1275 * This function tries set_cpus_allowed() and locks gcwq and verifies
1276 * the binding against GCWQ_DISASSOCIATED which is set during
1277 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1278 * idle state or fetches works without dropping lock, it can guarantee
1279 * the scheduling requirement described in the first paragraph.
1280 *
1281 * CONTEXT:
1282 * Might sleep. Called without any lock but returns with gcwq->lock
1283 * held.
1284 *
1285 * RETURNS:
1286 * %true if the associated gcwq is online (@worker is successfully
1287 * bound), %false if offline.
1288 */
1289static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001290__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001291{
Tejun Heo58658882012-07-12 14:46:37 -07001292 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001293 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Tejun Heoe22bee72010-06-29 10:07:14 +02001295 while (true) {
1296 /*
1297 * The following call may fail, succeed or succeed
1298 * without actually migrating the task to the cpu if
1299 * it races with cpu hotunplug operation. Verify
1300 * against GCWQ_DISASSOCIATED.
1301 */
Tejun Heof3421792010-07-02 10:03:51 +02001302 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1303 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001304
Tejun Heoe22bee72010-06-29 10:07:14 +02001305 spin_lock_irq(&gcwq->lock);
1306 if (gcwq->flags & GCWQ_DISASSOCIATED)
1307 return false;
1308 if (task_cpu(task) == gcwq->cpu &&
1309 cpumask_equal(&current->cpus_allowed,
1310 get_cpu_mask(gcwq->cpu)))
1311 return true;
1312 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001313
Tejun Heo5035b202011-04-29 18:08:37 +02001314 /*
1315 * We've raced with CPU hot[un]plug. Give it a breather
1316 * and retry migration. cond_resched() is required here;
1317 * otherwise, we might deadlock against cpu_stop trying to
1318 * bring down the CPU on non-preemptive kernel.
1319 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001320 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001321 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001322 }
1323}
1324
1325/*
1326 * Function for worker->rebind_work used to rebind rogue busy workers
1327 * to the associated cpu which is coming back online. This is
1328 * scheduled by cpu up but can race with other cpu hotplug operations
1329 * and may be executed twice without intervening cpu down.
1330 */
1331static void worker_rebind_fn(struct work_struct *work)
1332{
1333 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo58658882012-07-12 14:46:37 -07001334 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001335
1336 if (worker_maybe_bind_and_lock(worker))
1337 worker_clr_flags(worker, WORKER_REBIND);
1338
1339 spin_unlock_irq(&gcwq->lock);
1340}
1341
Tejun Heoc34056a2010-06-29 10:07:11 +02001342static struct worker *alloc_worker(void)
1343{
1344 struct worker *worker;
1345
1346 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001347 if (worker) {
1348 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001349 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001350 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1351 /* on creation a worker is in !idle && prep state */
1352 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001353 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001354 return worker;
1355}
1356
1357/**
1358 * create_worker - create a new workqueue worker
Tejun Heo7ef6a932012-07-12 14:46:37 -07001359 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001360 * @bind: whether to set affinity to @cpu or not
1361 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001362 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001363 * can be started by calling start_worker() or destroyed using
1364 * destroy_worker().
1365 *
1366 * CONTEXT:
1367 * Might sleep. Does GFP_KERNEL allocations.
1368 *
1369 * RETURNS:
1370 * Pointer to the newly created worker.
1371 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001372static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001373{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001374 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001375 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heodcb32ee2012-07-13 22:16:45 -07001376 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001377 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001378 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001379
Tejun Heo8b03ae32010-06-29 10:07:12 +02001380 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001381 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001382 spin_unlock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001383 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001384 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001385 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001386 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001387 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001388
1389 worker = alloc_worker();
1390 if (!worker)
1391 goto fail;
1392
Tejun Heo58658882012-07-12 14:46:37 -07001393 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001394 worker->id = id;
1395
Tejun Heof3421792010-07-02 10:03:51 +02001396 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001397 worker->task = kthread_create_on_node(worker_thread,
Tejun Heodcb32ee2012-07-13 22:16:45 -07001398 worker, cpu_to_node(gcwq->cpu),
1399 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001400 else
1401 worker->task = kthread_create(worker_thread, worker,
Tejun Heodcb32ee2012-07-13 22:16:45 -07001402 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001403 if (IS_ERR(worker->task))
1404 goto fail;
1405
Tejun Heodcb32ee2012-07-13 22:16:45 -07001406 if (worker_pool_pri(pool))
1407 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1408
Tejun Heodb7bccf2010-06-29 10:07:12 +02001409 /*
1410 * A rogue worker will become a regular one if CPU comes
1411 * online later on. Make sure every worker has
1412 * PF_THREAD_BOUND set.
1413 */
Tejun Heof3421792010-07-02 10:03:51 +02001414 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001415 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001416 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001417 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001418 if (on_unbound_cpu)
1419 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001421
Tejun Heoc34056a2010-06-29 10:07:11 +02001422 return worker;
1423fail:
1424 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001425 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001426 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001427 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001428 }
1429 kfree(worker);
1430 return NULL;
1431}
1432
1433/**
1434 * start_worker - start a newly created worker
1435 * @worker: worker to start
1436 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001437 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001438 *
1439 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001440 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001441 */
1442static void start_worker(struct worker *worker)
1443{
Tejun Heocb444762010-07-02 10:03:50 +02001444 worker->flags |= WORKER_STARTED;
Tejun Heo58658882012-07-12 14:46:37 -07001445 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001446 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001447 wake_up_process(worker->task);
1448}
1449
1450/**
1451 * destroy_worker - destroy a workqueue worker
1452 * @worker: worker to be destroyed
1453 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001454 * Destroy @worker and adjust @gcwq stats accordingly.
1455 *
1456 * CONTEXT:
1457 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001458 */
1459static void destroy_worker(struct worker *worker)
1460{
Tejun Heo58658882012-07-12 14:46:37 -07001461 struct worker_pool *pool = worker->pool;
1462 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001463 int id = worker->id;
1464
1465 /* sanity check frenzy */
1466 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001467 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001468
Tejun Heoc8e55f32010-06-29 10:07:12 +02001469 if (worker->flags & WORKER_STARTED)
Tejun Heo58658882012-07-12 14:46:37 -07001470 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001471 if (worker->flags & WORKER_IDLE)
Tejun Heo58658882012-07-12 14:46:37 -07001472 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001473
1474 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001475 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001476
1477 spin_unlock_irq(&gcwq->lock);
1478
Tejun Heoc34056a2010-06-29 10:07:11 +02001479 kthread_stop(worker->task);
1480 kfree(worker);
1481
Tejun Heo8b03ae32010-06-29 10:07:12 +02001482 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001483 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001484}
1485
Tejun Heo7ef6a932012-07-12 14:46:37 -07001486static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001487{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001488 struct worker_pool *pool = (void *)__pool;
1489 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001490
1491 spin_lock_irq(&gcwq->lock);
1492
Tejun Heo7ef6a932012-07-12 14:46:37 -07001493 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001494 struct worker *worker;
1495 unsigned long expires;
1496
1497 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001498 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001499 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1500
1501 if (time_before(jiffies, expires))
Tejun Heo7ef6a932012-07-12 14:46:37 -07001502 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001503 else {
1504 /* it's been idle for too long, wake up manager */
Tejun Heo22ad5642012-07-12 14:46:37 -07001505 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -07001506 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001507 }
1508 }
1509
1510 spin_unlock_irq(&gcwq->lock);
1511}
1512
1513static bool send_mayday(struct work_struct *work)
1514{
1515 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1516 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001517 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001518
1519 if (!(wq->flags & WQ_RESCUER))
1520 return false;
1521
1522 /* mayday mayday mayday */
Tejun Heo58658882012-07-12 14:46:37 -07001523 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001524 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1525 if (cpu == WORK_CPU_UNBOUND)
1526 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001527 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001528 wake_up_process(wq->rescuer->task);
1529 return true;
1530}
1531
Tejun Heo7ef6a932012-07-12 14:46:37 -07001532static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001533{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001534 struct worker_pool *pool = (void *)__pool;
1535 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 struct work_struct *work;
1537
1538 spin_lock_irq(&gcwq->lock);
1539
Tejun Heo7ef6a932012-07-12 14:46:37 -07001540 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001541 /*
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 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001547 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001548 send_mayday(work);
1549 }
1550
1551 spin_unlock_irq(&gcwq->lock);
1552
Tejun Heo7ef6a932012-07-12 14:46:37 -07001553 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001554}
1555
1556/**
1557 * maybe_create_worker - create a new worker if necessary
Tejun Heo7ef6a932012-07-12 14:46:37 -07001558 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001560 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 * have at least one idle worker on return from this function. If
1562 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo7ef6a932012-07-12 14:46:37 -07001563 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 * 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 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001578static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001579__releases(&gcwq->lock)
1580__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001581{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001582 struct global_cwq *gcwq = pool->gcwq;
1583
1584 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 return false;
1586restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001587 spin_unlock_irq(&gcwq->lock);
1588
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001590 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001591
1592 while (true) {
1593 struct worker *worker;
1594
Tejun Heo7ef6a932012-07-12 14:46:37 -07001595 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 if (worker) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001597 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 spin_lock_irq(&gcwq->lock);
1599 start_worker(worker);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001600 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 return true;
1602 }
1603
Tejun Heo7ef6a932012-07-12 14:46:37 -07001604 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 break;
1606
Tejun Heoe22bee72010-06-29 10:07:14 +02001607 __set_current_state(TASK_INTERRUPTIBLE);
1608 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001609
Tejun Heo7ef6a932012-07-12 14:46:37 -07001610 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 break;
1612 }
1613
Tejun Heo7ef6a932012-07-12 14:46:37 -07001614 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001615 spin_lock_irq(&gcwq->lock);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001616 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 goto restart;
1618 return true;
1619}
1620
1621/**
1622 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo7ef6a932012-07-12 14:46:37 -07001623 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001624 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001625 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001626 * IDLE_WORKER_TIMEOUT.
1627 *
1628 * LOCKING:
1629 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1630 * multiple times. Called only from manager.
1631 *
1632 * RETURNS:
1633 * false if no action was taken and gcwq->lock stayed locked, true
1634 * otherwise.
1635 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001636static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001637{
1638 bool ret = false;
1639
Tejun Heo7ef6a932012-07-12 14:46:37 -07001640 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001641 struct worker *worker;
1642 unsigned long expires;
1643
Tejun Heo7ef6a932012-07-12 14:46:37 -07001644 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001645 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1646
1647 if (time_before(jiffies, expires)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001648 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001649 break;
1650 }
1651
1652 destroy_worker(worker);
1653 ret = true;
1654 }
1655
1656 return ret;
1657}
1658
1659/**
1660 * manage_workers - manage worker pool
1661 * @worker: self
1662 *
1663 * Assume the manager role and manage gcwq worker pool @worker belongs
1664 * to. At any given time, there can be only zero or one manager per
1665 * gcwq. The exclusion is handled automatically by this function.
1666 *
1667 * The caller can safely start processing works on false return. On
1668 * true return, it's guaranteed that need_to_create_worker() is false
1669 * and may_start_working() is true.
1670 *
1671 * CONTEXT:
1672 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1673 * multiple times. Does GFP_KERNEL allocations.
1674 *
1675 * RETURNS:
1676 * false if no action was taken and gcwq->lock stayed locked, true if
1677 * some action was taken.
1678 */
1679static bool manage_workers(struct worker *worker)
1680{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001681 struct worker_pool *pool = worker->pool;
1682 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001683 bool ret = false;
1684
Tejun Heo22ad5642012-07-12 14:46:37 -07001685 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001686 return ret;
1687
Tejun Heo22ad5642012-07-12 14:46:37 -07001688 pool->flags &= ~POOL_MANAGE_WORKERS;
1689 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001690
1691 /*
1692 * Destroy and then create so that may_start_working() is true
1693 * on return.
1694 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001695 ret |= maybe_destroy_workers(pool);
1696 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001697
Tejun Heo22ad5642012-07-12 14:46:37 -07001698 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001699
1700 /*
1701 * The trustee might be waiting to take over the manager
1702 * position, tell it we're done.
1703 */
1704 if (unlikely(gcwq->trustee))
1705 wake_up_all(&gcwq->trustee_wait);
1706
1707 return ret;
1708}
1709
Tejun Heoa62428c2010-06-29 10:07:10 +02001710/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001711 * move_linked_works - move linked works to a list
1712 * @work: start of series of works to be scheduled
1713 * @head: target list to append @work to
1714 * @nextp: out paramter for nested worklist walking
1715 *
1716 * Schedule linked works starting from @work to @head. Work series to
1717 * be scheduled starts at @work and includes any consecutive work with
1718 * WORK_STRUCT_LINKED set in its predecessor.
1719 *
1720 * If @nextp is not NULL, it's updated to point to the next work of
1721 * the last scheduled work. This allows move_linked_works() to be
1722 * nested inside outer list_for_each_entry_safe().
1723 *
1724 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001725 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001726 */
1727static void move_linked_works(struct work_struct *work, struct list_head *head,
1728 struct work_struct **nextp)
1729{
1730 struct work_struct *n;
1731
1732 /*
1733 * Linked worklist will always end before the end of the list,
1734 * use NULL for list head.
1735 */
1736 list_for_each_entry_safe_from(work, n, NULL, entry) {
1737 list_move_tail(&work->entry, head);
1738 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1739 break;
1740 }
1741
1742 /*
1743 * If we're already inside safe list traversal and have moved
1744 * multiple works to the scheduled queue, the next position
1745 * needs to be updated.
1746 */
1747 if (nextp)
1748 *nextp = n;
1749}
1750
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001751static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001752{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001753 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001754
Tejun Heocdadf002010-10-05 10:49:55 +02001755 trace_workqueue_activate_work(work);
Tejun Heodcb32ee2012-07-13 22:16:45 -07001756 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001757 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001758 cwq->nr_active++;
1759}
1760
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001761static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1762{
1763 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1764 struct work_struct, entry);
1765
1766 cwq_activate_delayed_work(work);
1767}
1768
Tejun Heoaffee4b2010-06-29 10:07:12 +02001769/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001770 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1771 * @cwq: cwq of interest
1772 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001773 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001774 *
1775 * A work either has completed or is removed from pending queue,
1776 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1777 *
1778 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001779 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001780 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001781static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1782 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001783{
1784 /* ignore uncolored works */
1785 if (color == WORK_NO_COLOR)
1786 return;
1787
1788 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001789
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001790 if (!delayed) {
1791 cwq->nr_active--;
1792 if (!list_empty(&cwq->delayed_works)) {
1793 /* one down, submit a delayed one */
1794 if (cwq->nr_active < cwq->max_active)
1795 cwq_activate_first_delayed(cwq);
1796 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001797 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001798
1799 /* is flush in progress and are we at the flushing tip? */
1800 if (likely(cwq->flush_color != color))
1801 return;
1802
1803 /* are there still in-flight works? */
1804 if (cwq->nr_in_flight[color])
1805 return;
1806
1807 /* this cwq is done, clear flush_color */
1808 cwq->flush_color = -1;
1809
1810 /*
1811 * If this was the last cwq, wake up the first flusher. It
1812 * will handle the rest.
1813 */
1814 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1815 complete(&cwq->wq->first_flusher->done);
1816}
1817
1818/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001819 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001820 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001821 * @work: work to process
1822 *
1823 * Process @work. This function contains all the logics necessary to
1824 * process a single work including synchronization against and
1825 * interaction with other workers on the same cpu, queueing and
1826 * flushing. As long as context requirement is met, any worker can
1827 * call this function to process a work.
1828 *
1829 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001830 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001831 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001832static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001833__releases(&gcwq->lock)
1834__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001835{
Tejun Heo7e116292010-06-29 10:07:13 +02001836 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07001837 struct worker_pool *pool = worker->pool;
1838 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001839 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001840 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001841 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001842 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001843#ifdef CONFIG_LOCKDEP
1844 /*
1845 * It is permissible to free the struct work_struct from
1846 * inside the function that is called from it, this we need to
1847 * take into account for lockdep too. To avoid bogus "held
1848 * lock freed" warnings as well as problems when looking into
1849 * work->lockdep_map, make a copy and use that here.
1850 */
1851 struct lockdep_map lockdep_map = work->lockdep_map;
1852#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001853 /*
1854 * A single work shouldn't be executed concurrently by
1855 * multiple workers on a single cpu. Check whether anyone is
1856 * already processing the work. If so, defer the work to the
1857 * currently executing one.
1858 */
1859 collision = __find_worker_executing_work(gcwq, bwh, work);
1860 if (unlikely(collision)) {
1861 move_linked_works(work, &collision->scheduled, NULL);
1862 return;
1863 }
1864
Tejun Heoa62428c2010-06-29 10:07:10 +02001865 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001866 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001867 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001868 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001869 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001870 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001871 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001872
Tejun Heo7a22ad72010-06-29 10:07:13 +02001873 /* record the current cpu number in the work data and dequeue */
1874 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001875 list_del_init(&work->entry);
1876
Tejun Heo649027d2010-06-29 10:07:14 +02001877 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001878 * CPU intensive works don't participate in concurrency
1879 * management. They're the scheduler's responsibility.
1880 */
1881 if (unlikely(cpu_intensive))
1882 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1883
Tejun Heob7b5c682012-07-12 14:46:37 -07001884 /*
1885 * Unbound gcwq isn't concurrency managed and work items should be
1886 * executed ASAP. Wake up another worker if necessary.
1887 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001888 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1889 wake_up_worker(pool);
Tejun Heob7b5c682012-07-12 14:46:37 -07001890
Tejun Heo8b03ae32010-06-29 10:07:12 +02001891 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001892
Tejun Heo66307ae2012-08-03 10:30:45 -07001893 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001894 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001895
Tejun Heoe1594892011-01-09 23:32:15 +01001896 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001897 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001898 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001899 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001900 /*
1901 * While we must be careful to not use "work" after this, the trace
1902 * point will only record its address.
1903 */
1904 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001905 lock_map_release(&lockdep_map);
1906 lock_map_release(&cwq->wq->lockdep_map);
1907
1908 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001909 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1910 " last function: %pf\n",
1911 current->comm, preempt_count(), task_pid_nr(current),
1912 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001913 debug_show_held_locks(current);
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -07001914 BUG_ON(PANIC_CORRUPTION);
Tejun Heoa62428c2010-06-29 10:07:10 +02001915 dump_stack();
1916 }
1917
Tejun Heo8b03ae32010-06-29 10:07:12 +02001918 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001919
Tejun Heofb0e7be2010-06-29 10:07:15 +02001920 /* clear cpu intensive status */
1921 if (unlikely(cpu_intensive))
1922 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1923
Tejun Heoa62428c2010-06-29 10:07:10 +02001924 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001925 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001926 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001927 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001928 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001929 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001930}
1931
Tejun Heoaffee4b2010-06-29 10:07:12 +02001932/**
1933 * process_scheduled_works - process scheduled works
1934 * @worker: self
1935 *
1936 * Process all scheduled works. Please note that the scheduled list
1937 * may change while processing a work, so this function repeatedly
1938 * fetches a work from the top and executes it.
1939 *
1940 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001941 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001942 * multiple times.
1943 */
1944static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001946 while (!list_empty(&worker->scheduled)) {
1947 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001949 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Tejun Heo4690c4a2010-06-29 10:07:10 +02001953/**
1954 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001955 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001956 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 * The gcwq worker thread function. There's a single dynamic pool of
1958 * these per each cpu. These workers process all works regardless of
1959 * their specific target workqueue. The only exception is works which
1960 * belong to workqueues with a rescuer which will be explained in
1961 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001962 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001963static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Tejun Heoc34056a2010-06-29 10:07:11 +02001965 struct worker *worker = __worker;
Tejun Heo58658882012-07-12 14:46:37 -07001966 struct worker_pool *pool = worker->pool;
1967 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Tejun Heoe22bee72010-06-29 10:07:14 +02001969 /* tell the scheduler that this is a workqueue worker */
1970 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001971woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001972 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Tejun Heoc8e55f32010-06-29 10:07:12 +02001974 /* DIE can be set only while we're idle, checking here is enough */
1975 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001976 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980
Tejun Heoc8e55f32010-06-29 10:07:12 +02001981 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001982recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 /* no more worker necessary? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001984 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 goto sleep;
1986
1987 /* do we need to manage? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001988 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 goto recheck;
1990
Tejun Heoc8e55f32010-06-29 10:07:12 +02001991 /*
1992 * ->scheduled list can only be filled while a worker is
1993 * preparing to process a work or actually processing it.
1994 * Make sure nobody diddled with it while I was sleeping.
1995 */
1996 BUG_ON(!list_empty(&worker->scheduled));
1997
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 /*
1999 * When control reaches this point, we're guaranteed to have
2000 * at least one idle worker or that someone else has already
2001 * assumed the manager role.
2002 */
2003 worker_clr_flags(worker, WORKER_PREP);
2004
2005 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002006 struct work_struct *work =
Tejun Heo58658882012-07-12 14:46:37 -07002007 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002008 struct work_struct, entry);
2009
2010 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2011 /* optimization path, not strictly necessary */
2012 process_one_work(worker, work);
2013 if (unlikely(!list_empty(&worker->scheduled)))
2014 process_scheduled_works(worker);
2015 } else {
2016 move_linked_works(work, &worker->scheduled, NULL);
2017 process_scheduled_works(worker);
2018 }
Tejun Heo7ef6a932012-07-12 14:46:37 -07002019 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002020
Tejun Heoe22bee72010-06-29 10:07:14 +02002021 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002022sleep:
Tejun Heo7ef6a932012-07-12 14:46:37 -07002023 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002024 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002025
Tejun Heoc8e55f32010-06-29 10:07:12 +02002026 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 * gcwq->lock is held and there's no work to process and no
2028 * need to manage, sleep. Workers are woken up only while
2029 * holding gcwq->lock or from local cpu, so setting the
2030 * current state before releasing gcwq->lock is enough to
2031 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002032 */
2033 worker_enter_idle(worker);
2034 __set_current_state(TASK_INTERRUPTIBLE);
2035 spin_unlock_irq(&gcwq->lock);
2036 schedule();
2037 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Tejun Heoe22bee72010-06-29 10:07:14 +02002040/**
2041 * rescuer_thread - the rescuer thread function
2042 * @__wq: the associated workqueue
2043 *
2044 * Workqueue rescuer thread function. There's one rescuer for each
2045 * workqueue which has WQ_RESCUER set.
2046 *
2047 * Regular work processing on a gcwq may block trying to create a new
2048 * worker which uses GFP_KERNEL allocation which has slight chance of
2049 * developing into deadlock if some works currently on the same queue
2050 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2051 * the problem rescuer solves.
2052 *
2053 * When such condition is possible, the gcwq summons rescuers of all
2054 * workqueues which have works queued on the gcwq and let them process
2055 * those works so that forward progress can be guaranteed.
2056 *
2057 * This should happen rarely.
2058 */
2059static int rescuer_thread(void *__wq)
2060{
2061 struct workqueue_struct *wq = __wq;
2062 struct worker *rescuer = wq->rescuer;
2063 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002064 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002065 unsigned int cpu;
2066
2067 set_user_nice(current, RESCUER_NICE_LEVEL);
2068repeat:
2069 set_current_state(TASK_INTERRUPTIBLE);
2070
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002071 if (kthread_should_stop()) {
2072 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002073 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002074 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002075
Tejun Heof3421792010-07-02 10:03:51 +02002076 /*
2077 * See whether any cpu is asking for help. Unbounded
2078 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2079 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002080 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002081 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2082 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002083 struct worker_pool *pool = cwq->pool;
2084 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002085 struct work_struct *work, *n;
2086
2087 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002088 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002089
2090 /* migrate to the target cpu if possible */
Tejun Heo58658882012-07-12 14:46:37 -07002091 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002092 worker_maybe_bind_and_lock(rescuer);
2093
2094 /*
2095 * Slurp in all works issued via this workqueue and
2096 * process'em.
2097 */
2098 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo58658882012-07-12 14:46:37 -07002099 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002100 if (get_work_cwq(work) == cwq)
2101 move_linked_works(work, scheduled, &n);
2102
2103 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002104
2105 /*
2106 * Leave this gcwq. If keep_working() is %true, notify a
2107 * regular worker; otherwise, we end up with 0 concurrency
2108 * and stalling the execution.
2109 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002110 if (keep_working(pool))
2111 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002112
Tejun Heoe22bee72010-06-29 10:07:14 +02002113 spin_unlock_irq(&gcwq->lock);
2114 }
2115
2116 schedule();
2117 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002120struct wq_barrier {
2121 struct work_struct work;
2122 struct completion done;
2123};
2124
2125static void wq_barrier_func(struct work_struct *work)
2126{
2127 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2128 complete(&barr->done);
2129}
2130
Tejun Heo4690c4a2010-06-29 10:07:10 +02002131/**
2132 * insert_wq_barrier - insert a barrier work
2133 * @cwq: cwq to insert barrier into
2134 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002135 * @target: target work to attach @barr to
2136 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002137 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002138 * @barr is linked to @target such that @barr is completed only after
2139 * @target finishes execution. Please note that the ordering
2140 * guarantee is observed only with respect to @target and on the local
2141 * cpu.
2142 *
2143 * Currently, a queued barrier can't be canceled. This is because
2144 * try_to_grab_pending() can't determine whether the work to be
2145 * grabbed is at the head of the queue and thus can't clear LINKED
2146 * flag of the previous work while there must be a valid next work
2147 * after a work with LINKED flag set.
2148 *
2149 * Note that when @worker is non-NULL, @target may be modified
2150 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002151 *
2152 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002153 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002154 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002155static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002156 struct wq_barrier *barr,
2157 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002158{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002159 struct list_head *head;
2160 unsigned int linked = 0;
2161
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002162 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002163 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002164 * as we know for sure that this will not trigger any of the
2165 * checks and call back into the fixup functions where we
2166 * might deadlock.
2167 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002168 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002169 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002170 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002171
Tejun Heoaffee4b2010-06-29 10:07:12 +02002172 /*
2173 * If @target is currently being executed, schedule the
2174 * barrier to the worker; otherwise, put it after @target.
2175 */
2176 if (worker)
2177 head = worker->scheduled.next;
2178 else {
2179 unsigned long *bits = work_data_bits(target);
2180
2181 head = target->entry.next;
2182 /* there can already be other linked works, inherit and set */
2183 linked = *bits & WORK_STRUCT_LINKED;
2184 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2185 }
2186
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002187 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002188 insert_work(cwq, &barr->work, head,
2189 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002190}
2191
Tejun Heo73f53c42010-06-29 10:07:11 +02002192/**
2193 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2194 * @wq: workqueue being flushed
2195 * @flush_color: new flush color, < 0 for no-op
2196 * @work_color: new work color, < 0 for no-op
2197 *
2198 * Prepare cwqs for workqueue flushing.
2199 *
2200 * If @flush_color is non-negative, flush_color on all cwqs should be
2201 * -1. If no cwq has in-flight commands at the specified color, all
2202 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2203 * has in flight commands, its cwq->flush_color is set to
2204 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2205 * wakeup logic is armed and %true is returned.
2206 *
2207 * The caller should have initialized @wq->first_flusher prior to
2208 * calling this function with non-negative @flush_color. If
2209 * @flush_color is negative, no flush color update is done and %false
2210 * is returned.
2211 *
2212 * If @work_color is non-negative, all cwqs should have the same
2213 * work_color which is previous to @work_color and all will be
2214 * advanced to @work_color.
2215 *
2216 * CONTEXT:
2217 * mutex_lock(wq->flush_mutex).
2218 *
2219 * RETURNS:
2220 * %true if @flush_color >= 0 and there's something to flush. %false
2221 * otherwise.
2222 */
2223static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2224 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Tejun Heo73f53c42010-06-29 10:07:11 +02002226 bool wait = false;
2227 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002228
Tejun Heo73f53c42010-06-29 10:07:11 +02002229 if (flush_color >= 0) {
2230 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2231 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002232 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002233
Tejun Heof3421792010-07-02 10:03:51 +02002234 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002235 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002236 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002237
Tejun Heo8b03ae32010-06-29 10:07:12 +02002238 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002239
2240 if (flush_color >= 0) {
2241 BUG_ON(cwq->flush_color != -1);
2242
2243 if (cwq->nr_in_flight[flush_color]) {
2244 cwq->flush_color = flush_color;
2245 atomic_inc(&wq->nr_cwqs_to_flush);
2246 wait = true;
2247 }
2248 }
2249
2250 if (work_color >= 0) {
2251 BUG_ON(work_color != work_next_color(cwq->work_color));
2252 cwq->work_color = work_color;
2253 }
2254
Tejun Heo8b03ae32010-06-29 10:07:12 +02002255 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002256 }
2257
2258 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2259 complete(&wq->first_flusher->done);
2260
2261 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002264/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002266 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 *
2268 * Forces execution of the workqueue and blocks until its completion.
2269 * This is typically used in driver shutdown handlers.
2270 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002271 * We sleep until all works which were queued on entry have been handled,
2272 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002274void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Tejun Heo73f53c42010-06-29 10:07:11 +02002276 struct wq_flusher this_flusher = {
2277 .list = LIST_HEAD_INIT(this_flusher.list),
2278 .flush_color = -1,
2279 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2280 };
2281 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002282
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002283 lock_map_acquire(&wq->lockdep_map);
2284 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002285
2286 mutex_lock(&wq->flush_mutex);
2287
2288 /*
2289 * Start-to-wait phase
2290 */
2291 next_color = work_next_color(wq->work_color);
2292
2293 if (next_color != wq->flush_color) {
2294 /*
2295 * Color space is not full. The current work_color
2296 * becomes our flush_color and work_color is advanced
2297 * by one.
2298 */
2299 BUG_ON(!list_empty(&wq->flusher_overflow));
2300 this_flusher.flush_color = wq->work_color;
2301 wq->work_color = next_color;
2302
2303 if (!wq->first_flusher) {
2304 /* no flush in progress, become the first flusher */
2305 BUG_ON(wq->flush_color != this_flusher.flush_color);
2306
2307 wq->first_flusher = &this_flusher;
2308
2309 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2310 wq->work_color)) {
2311 /* nothing to flush, done */
2312 wq->flush_color = next_color;
2313 wq->first_flusher = NULL;
2314 goto out_unlock;
2315 }
2316 } else {
2317 /* wait in queue */
2318 BUG_ON(wq->flush_color == this_flusher.flush_color);
2319 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2320 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2321 }
2322 } else {
2323 /*
2324 * Oops, color space is full, wait on overflow queue.
2325 * The next flush completion will assign us
2326 * flush_color and transfer to flusher_queue.
2327 */
2328 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2329 }
2330
2331 mutex_unlock(&wq->flush_mutex);
2332
2333 wait_for_completion(&this_flusher.done);
2334
2335 /*
2336 * Wake-up-and-cascade phase
2337 *
2338 * First flushers are responsible for cascading flushes and
2339 * handling overflow. Non-first flushers can simply return.
2340 */
2341 if (wq->first_flusher != &this_flusher)
2342 return;
2343
2344 mutex_lock(&wq->flush_mutex);
2345
Tejun Heo4ce48b32010-07-02 10:03:51 +02002346 /* we might have raced, check again with mutex held */
2347 if (wq->first_flusher != &this_flusher)
2348 goto out_unlock;
2349
Tejun Heo73f53c42010-06-29 10:07:11 +02002350 wq->first_flusher = NULL;
2351
2352 BUG_ON(!list_empty(&this_flusher.list));
2353 BUG_ON(wq->flush_color != this_flusher.flush_color);
2354
2355 while (true) {
2356 struct wq_flusher *next, *tmp;
2357
2358 /* complete all the flushers sharing the current flush color */
2359 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2360 if (next->flush_color != wq->flush_color)
2361 break;
2362 list_del_init(&next->list);
2363 complete(&next->done);
2364 }
2365
2366 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2367 wq->flush_color != work_next_color(wq->work_color));
2368
2369 /* this flush_color is finished, advance by one */
2370 wq->flush_color = work_next_color(wq->flush_color);
2371
2372 /* one color has been freed, handle overflow queue */
2373 if (!list_empty(&wq->flusher_overflow)) {
2374 /*
2375 * Assign the same color to all overflowed
2376 * flushers, advance work_color and append to
2377 * flusher_queue. This is the start-to-wait
2378 * phase for these overflowed flushers.
2379 */
2380 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2381 tmp->flush_color = wq->work_color;
2382
2383 wq->work_color = work_next_color(wq->work_color);
2384
2385 list_splice_tail_init(&wq->flusher_overflow,
2386 &wq->flusher_queue);
2387 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2388 }
2389
2390 if (list_empty(&wq->flusher_queue)) {
2391 BUG_ON(wq->flush_color != wq->work_color);
2392 break;
2393 }
2394
2395 /*
2396 * Need to flush more colors. Make the next flusher
2397 * the new first flusher and arm cwqs.
2398 */
2399 BUG_ON(wq->flush_color == wq->work_color);
2400 BUG_ON(wq->flush_color != next->flush_color);
2401
2402 list_del_init(&next->list);
2403 wq->first_flusher = next;
2404
2405 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2406 break;
2407
2408 /*
2409 * Meh... this color is already done, clear first
2410 * flusher and repeat cascading.
2411 */
2412 wq->first_flusher = NULL;
2413 }
2414
2415out_unlock:
2416 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
Dave Jonesae90dd52006-06-30 01:40:45 -04002418EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002420/**
2421 * drain_workqueue - drain a workqueue
2422 * @wq: workqueue to drain
2423 *
2424 * Wait until the workqueue becomes empty. While draining is in progress,
2425 * only chain queueing is allowed. IOW, only currently pending or running
2426 * work items on @wq can queue further work items on it. @wq is flushed
2427 * repeatedly until it becomes empty. The number of flushing is detemined
2428 * by the depth of chaining and should be relatively short. Whine if it
2429 * takes too long.
2430 */
2431void drain_workqueue(struct workqueue_struct *wq)
2432{
2433 unsigned int flush_cnt = 0;
2434 unsigned int cpu;
2435
2436 /*
2437 * __queue_work() needs to test whether there are drainers, is much
2438 * hotter than drain_workqueue() and already looks at @wq->flags.
2439 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2440 */
2441 spin_lock(&workqueue_lock);
2442 if (!wq->nr_drainers++)
2443 wq->flags |= WQ_DRAINING;
2444 spin_unlock(&workqueue_lock);
2445reflush:
2446 flush_workqueue(wq);
2447
2448 for_each_cwq_cpu(cpu, wq) {
2449 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002450 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002451
Tejun Heo58658882012-07-12 14:46:37 -07002452 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002453 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo58658882012-07-12 14:46:37 -07002454 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002455
2456 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002457 continue;
2458
2459 if (++flush_cnt == 10 ||
2460 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2461 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2462 wq->name, flush_cnt);
2463 goto reflush;
2464 }
2465
2466 spin_lock(&workqueue_lock);
2467 if (!--wq->nr_drainers)
2468 wq->flags &= ~WQ_DRAINING;
2469 spin_unlock(&workqueue_lock);
2470}
2471EXPORT_SYMBOL_GPL(drain_workqueue);
2472
Tejun Heobaf59022010-09-16 10:42:16 +02002473static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2474 bool wait_executing)
2475{
2476 struct worker *worker = NULL;
2477 struct global_cwq *gcwq;
2478 struct cpu_workqueue_struct *cwq;
2479
2480 might_sleep();
2481 gcwq = get_work_gcwq(work);
2482 if (!gcwq)
2483 return false;
2484
2485 spin_lock_irq(&gcwq->lock);
2486 if (!list_empty(&work->entry)) {
2487 /*
2488 * See the comment near try_to_grab_pending()->smp_rmb().
2489 * If it was re-queued to a different gcwq under us, we
2490 * are not going to wait.
2491 */
2492 smp_rmb();
2493 cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07002494 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002495 goto already_gone;
2496 } else if (wait_executing) {
2497 worker = find_worker_executing_work(gcwq, work);
2498 if (!worker)
2499 goto already_gone;
2500 cwq = worker->current_cwq;
2501 } else
2502 goto already_gone;
2503
2504 insert_wq_barrier(cwq, barr, work, worker);
2505 spin_unlock_irq(&gcwq->lock);
2506
Tejun Heoe1594892011-01-09 23:32:15 +01002507 /*
2508 * If @max_active is 1 or rescuer is in use, flushing another work
2509 * item on the same workqueue may lead to deadlock. Make sure the
2510 * flusher is not running on the same workqueue by verifying write
2511 * access.
2512 */
2513 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2514 lock_map_acquire(&cwq->wq->lockdep_map);
2515 else
2516 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002517 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002518
Tejun Heobaf59022010-09-16 10:42:16 +02002519 return true;
2520already_gone:
2521 spin_unlock_irq(&gcwq->lock);
2522 return false;
2523}
2524
Oleg Nesterovdb700892008-07-25 01:47:49 -07002525/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002526 * flush_work - wait for a work to finish executing the last queueing instance
2527 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002528 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002529 * Wait until @work has finished execution. This function considers
2530 * only the last queueing instance of @work. If @work has been
2531 * enqueued across different CPUs on a non-reentrant workqueue or on
2532 * multiple workqueues, @work might still be executing on return on
2533 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002534 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002535 * If @work was queued only on a non-reentrant, ordered or unbound
2536 * workqueue, @work is guaranteed to be idle on return if it hasn't
2537 * been requeued since flush started.
2538 *
2539 * RETURNS:
2540 * %true if flush_work() waited for the work to finish execution,
2541 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002542 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002543bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002544{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002545 struct wq_barrier barr;
2546
Tejun Heobaf59022010-09-16 10:42:16 +02002547 if (start_flush_work(work, &barr, true)) {
2548 wait_for_completion(&barr.done);
2549 destroy_work_on_stack(&barr.work);
2550 return true;
2551 } else
2552 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002553}
2554EXPORT_SYMBOL_GPL(flush_work);
2555
Tejun Heo401a8d02010-09-16 10:36:00 +02002556static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2557{
2558 struct wq_barrier barr;
2559 struct worker *worker;
2560
2561 spin_lock_irq(&gcwq->lock);
2562
2563 worker = find_worker_executing_work(gcwq, work);
2564 if (unlikely(worker))
2565 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2566
2567 spin_unlock_irq(&gcwq->lock);
2568
2569 if (unlikely(worker)) {
2570 wait_for_completion(&barr.done);
2571 destroy_work_on_stack(&barr.work);
2572 return true;
2573 } else
2574 return false;
2575}
2576
2577static bool wait_on_work(struct work_struct *work)
2578{
2579 bool ret = false;
2580 int cpu;
2581
2582 might_sleep();
2583
2584 lock_map_acquire(&work->lockdep_map);
2585 lock_map_release(&work->lockdep_map);
2586
2587 for_each_gcwq_cpu(cpu)
2588 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2589 return ret;
2590}
2591
Tejun Heo09383492010-09-16 10:48:29 +02002592/**
2593 * flush_work_sync - wait until a work has finished execution
2594 * @work: the work to flush
2595 *
2596 * Wait until @work has finished execution. On return, it's
2597 * guaranteed that all queueing instances of @work which happened
2598 * before this function is called are finished. In other words, if
2599 * @work hasn't been requeued since this function was called, @work is
2600 * guaranteed to be idle on return.
2601 *
2602 * RETURNS:
2603 * %true if flush_work_sync() waited for the work to finish execution,
2604 * %false if it was already idle.
2605 */
2606bool flush_work_sync(struct work_struct *work)
2607{
2608 struct wq_barrier barr;
2609 bool pending, waited;
2610
2611 /* we'll wait for executions separately, queue barr only if pending */
2612 pending = start_flush_work(work, &barr, false);
2613
2614 /* wait for executions to finish */
2615 waited = wait_on_work(work);
2616
2617 /* wait for the pending one */
2618 if (pending) {
2619 wait_for_completion(&barr.done);
2620 destroy_work_on_stack(&barr.work);
2621 }
2622
2623 return pending || waited;
2624}
2625EXPORT_SYMBOL_GPL(flush_work_sync);
2626
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002627/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002628 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002629 * so this work can't be re-armed in any way.
2630 */
2631static int try_to_grab_pending(struct work_struct *work)
2632{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002633 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002634 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002635
Tejun Heo22df02b2010-06-29 10:07:10 +02002636 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002637 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002638
2639 /*
2640 * The queueing is in progress, or it is already queued. Try to
2641 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2642 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002643 gcwq = get_work_gcwq(work);
2644 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002645 return ret;
2646
Tejun Heo8b03ae32010-06-29 10:07:12 +02002647 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002648 if (!list_empty(&work->entry)) {
2649 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002650 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002651 * In that case we must see the new value after rmb(), see
2652 * insert_work()->wmb().
2653 */
2654 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002655 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002656 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002657
2658 /*
2659 * A delayed work item cannot be grabbed directly
2660 * because it might have linked NO_COLOR work items
2661 * which, if left on the delayed_list, will confuse
2662 * cwq->nr_active management later on and cause
2663 * stall. Make sure the work item is activated
2664 * before grabbing.
2665 */
2666 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2667 cwq_activate_delayed_work(work);
2668
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002669 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002670 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002671 get_work_color(work),
2672 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002673 ret = 1;
2674 }
2675 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002676 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002677
2678 return ret;
2679}
2680
Tejun Heo401a8d02010-09-16 10:36:00 +02002681static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002682 struct timer_list* timer)
2683{
2684 int ret;
2685
2686 do {
2687 ret = (timer && likely(del_timer(timer)));
2688 if (!ret)
2689 ret = try_to_grab_pending(work);
2690 wait_on_work(work);
2691 } while (unlikely(ret < 0));
2692
Tejun Heo7a22ad72010-06-29 10:07:13 +02002693 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002694 return ret;
2695}
2696
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002697/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002698 * cancel_work_sync - cancel a work and wait for it to finish
2699 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002700 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002701 * Cancel @work and wait for its execution to finish. This function
2702 * can be used even if the work re-queues itself or migrates to
2703 * another workqueue. On return from this function, @work is
2704 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002705 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002706 * cancel_work_sync(&delayed_work->work) must not be used for
2707 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002708 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002709 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002710 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002711 *
2712 * RETURNS:
2713 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002714 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002715bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002716{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002717 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002718}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002719EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002720
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002721/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002722 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2723 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002724 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002725 * Delayed timer is cancelled and the pending work is queued for
2726 * immediate execution. Like flush_work(), this function only
2727 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002728 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002729 * RETURNS:
2730 * %true if flush_work() waited for the work to finish execution,
2731 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002732 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002733bool flush_delayed_work(struct delayed_work *dwork)
2734{
2735 if (del_timer_sync(&dwork->timer))
2736 __queue_work(raw_smp_processor_id(),
2737 get_work_cwq(&dwork->work)->wq, &dwork->work);
2738 return flush_work(&dwork->work);
2739}
2740EXPORT_SYMBOL(flush_delayed_work);
2741
2742/**
Tejun Heo09383492010-09-16 10:48:29 +02002743 * flush_delayed_work_sync - wait for a dwork to finish
2744 * @dwork: the delayed work to flush
2745 *
2746 * Delayed timer is cancelled and the pending work is queued for
2747 * execution immediately. Other than timer handling, its behavior
2748 * is identical to flush_work_sync().
2749 *
2750 * RETURNS:
2751 * %true if flush_work_sync() waited for the work to finish execution,
2752 * %false if it was already idle.
2753 */
2754bool flush_delayed_work_sync(struct delayed_work *dwork)
2755{
2756 if (del_timer_sync(&dwork->timer))
2757 __queue_work(raw_smp_processor_id(),
2758 get_work_cwq(&dwork->work)->wq, &dwork->work);
2759 return flush_work_sync(&dwork->work);
2760}
2761EXPORT_SYMBOL(flush_delayed_work_sync);
2762
2763/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002764 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2765 * @dwork: the delayed work cancel
2766 *
2767 * This is cancel_work_sync() for delayed works.
2768 *
2769 * RETURNS:
2770 * %true if @dwork was pending, %false otherwise.
2771 */
2772bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002773{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002774 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002775}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002776EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002778/**
2779 * schedule_work - put work task in global workqueue
2780 * @work: job to be done
2781 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002782 * Returns zero if @work was already on the kernel-global workqueue and
2783 * non-zero otherwise.
2784 *
2785 * This puts a job in the kernel-global workqueue if it was not already
2786 * queued and leaves it in the same position on the kernel-global
2787 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002788 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002789int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Tejun Heod320c032010-06-29 10:07:14 +02002791 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
Dave Jonesae90dd52006-06-30 01:40:45 -04002793EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Zhang Ruic1a220e2008-07-23 21:28:39 -07002795/*
2796 * schedule_work_on - put work task on a specific cpu
2797 * @cpu: cpu to put the work task on
2798 * @work: job to be done
2799 *
2800 * This puts a job on a specific cpu
2801 */
2802int schedule_work_on(int cpu, struct work_struct *work)
2803{
Tejun Heod320c032010-06-29 10:07:14 +02002804 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002805}
2806EXPORT_SYMBOL(schedule_work_on);
2807
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002808/**
2809 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002810 * @dwork: job to be done
2811 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002812 *
2813 * After waiting for a given time this puts a job in the kernel-global
2814 * workqueue.
2815 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002816int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002817 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Tejun Heod320c032010-06-29 10:07:14 +02002819 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
Dave Jonesae90dd52006-06-30 01:40:45 -04002821EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002823/**
2824 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2825 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002826 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002827 * @delay: number of jiffies to wait
2828 *
2829 * After waiting for a given time this puts a job in the kernel-global
2830 * workqueue on the specified CPU.
2831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002833 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Tejun Heod320c032010-06-29 10:07:14 +02002835 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
Dave Jonesae90dd52006-06-30 01:40:45 -04002837EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Andrew Mortonb6136772006-06-25 05:47:49 -07002839/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002840 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002841 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002842 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002843 * schedule_on_each_cpu() executes @func on each online CPU using the
2844 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002845 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002846 *
2847 * RETURNS:
2848 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002849 */
David Howells65f27f32006-11-22 14:55:48 +00002850int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002851{
2852 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002853 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002854
Andrew Mortonb6136772006-06-25 05:47:49 -07002855 works = alloc_percpu(struct work_struct);
2856 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002857 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002858
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002859 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002860
Christoph Lameter15316ba2006-01-08 01:00:43 -08002861 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002862 struct work_struct *work = per_cpu_ptr(works, cpu);
2863
2864 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002865 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002866 }
Tejun Heo93981802009-11-17 14:06:20 -08002867
2868 for_each_online_cpu(cpu)
2869 flush_work(per_cpu_ptr(works, cpu));
2870
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002871 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002872 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002873 return 0;
2874}
2875
Alan Sterneef6a7d2010-02-12 17:39:21 +09002876/**
2877 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2878 *
2879 * Forces execution of the kernel-global workqueue and blocks until its
2880 * completion.
2881 *
2882 * Think twice before calling this function! It's very easy to get into
2883 * trouble if you don't take great care. Either of the following situations
2884 * will lead to deadlock:
2885 *
2886 * One of the work items currently on the workqueue needs to acquire
2887 * a lock held by your code or its caller.
2888 *
2889 * Your code is running in the context of a work routine.
2890 *
2891 * They will be detected by lockdep when they occur, but the first might not
2892 * occur very often. It depends on what work items are on the workqueue and
2893 * what locks they need, which you have no control over.
2894 *
2895 * In most situations flushing the entire workqueue is overkill; you merely
2896 * need to know that a particular work item isn't queued and isn't running.
2897 * In such cases you should use cancel_delayed_work_sync() or
2898 * cancel_work_sync() instead.
2899 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900void flush_scheduled_work(void)
2901{
Tejun Heod320c032010-06-29 10:07:14 +02002902 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
Dave Jonesae90dd52006-06-30 01:40:45 -04002904EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002907 * execute_in_process_context - reliably execute the routine with user context
2908 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002909 * @ew: guaranteed storage for the execute work structure (must
2910 * be available when the work executes)
2911 *
2912 * Executes the function immediately if process context is available,
2913 * otherwise schedules the function for delayed execution.
2914 *
2915 * Returns: 0 - function was executed
2916 * 1 - function was scheduled for execution
2917 */
David Howells65f27f32006-11-22 14:55:48 +00002918int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002919{
2920 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002921 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002922 return 0;
2923 }
2924
David Howells65f27f32006-11-22 14:55:48 +00002925 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002926 schedule_work(&ew->work);
2927
2928 return 1;
2929}
2930EXPORT_SYMBOL_GPL(execute_in_process_context);
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932int keventd_up(void)
2933{
Tejun Heod320c032010-06-29 10:07:14 +02002934 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002937static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002939 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002940 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2941 * Make sure that the alignment isn't lower than that of
2942 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002943 */
Tejun Heo0f900042010-06-29 10:07:11 +02002944 const size_t size = sizeof(struct cpu_workqueue_struct);
2945 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2946 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002947
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002948 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002949 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002950 else {
Tejun Heof3421792010-07-02 10:03:51 +02002951 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002952
Tejun Heof3421792010-07-02 10:03:51 +02002953 /*
2954 * Allocate enough room to align cwq and put an extra
2955 * pointer at the end pointing back to the originally
2956 * allocated pointer which will be used for free.
2957 */
2958 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2959 if (ptr) {
2960 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2961 *(void **)(wq->cpu_wq.single + 1) = ptr;
2962 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002963 }
Tejun Heof3421792010-07-02 10:03:51 +02002964
Tejun Heo0415b002011-03-24 18:50:09 +01002965 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002966 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2967 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002968}
2969
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002970static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002971{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002972 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002973 free_percpu(wq->cpu_wq.pcpu);
2974 else if (wq->cpu_wq.single) {
2975 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002976 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002977 }
2978}
2979
Tejun Heof3421792010-07-02 10:03:51 +02002980static int wq_clamp_max_active(int max_active, unsigned int flags,
2981 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002982{
Tejun Heof3421792010-07-02 10:03:51 +02002983 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2984
2985 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002986 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2987 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002988 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002989
Tejun Heof3421792010-07-02 10:03:51 +02002990 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002991}
2992
Tejun Heob196be82012-01-10 15:11:35 -08002993struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002994 unsigned int flags,
2995 int max_active,
2996 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002997 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002998{
Tejun Heob196be82012-01-10 15:11:35 -08002999 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003000 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003001 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003002 size_t namelen;
3003
3004 /* determine namelen, allocate wq and format name */
3005 va_start(args, lock_name);
3006 va_copy(args1, args);
3007 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3008
3009 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3010 if (!wq)
3011 goto err;
3012
3013 vsnprintf(wq->name, namelen, fmt, args1);
3014 va_end(args);
3015 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003016
Tejun Heof3421792010-07-02 10:03:51 +02003017 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003018 * Workqueues which may be used during memory reclaim should
3019 * have a rescuer to guarantee forward progress.
3020 */
3021 if (flags & WQ_MEM_RECLAIM)
3022 flags |= WQ_RESCUER;
3023
Tejun Heod320c032010-06-29 10:07:14 +02003024 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003025 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003026
Tejun Heob196be82012-01-10 15:11:35 -08003027 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003028 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003029 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003030 mutex_init(&wq->flush_mutex);
3031 atomic_set(&wq->nr_cwqs_to_flush, 0);
3032 INIT_LIST_HEAD(&wq->flusher_queue);
3033 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003034
Johannes Bergeb13ba82008-01-16 09:51:58 +01003035 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003036 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003037
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003038 if (alloc_cwqs(wq) < 0)
3039 goto err;
3040
Tejun Heof3421792010-07-02 10:03:51 +02003041 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003042 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003043 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003044 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003045
Tejun Heo0f900042010-06-29 10:07:11 +02003046 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003047 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003048 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003049 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003050 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003051 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003052 }
3053
Tejun Heoe22bee72010-06-29 10:07:14 +02003054 if (flags & WQ_RESCUER) {
3055 struct worker *rescuer;
3056
Tejun Heof2e005a2010-07-20 15:59:09 +02003057 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003058 goto err;
3059
3060 wq->rescuer = rescuer = alloc_worker();
3061 if (!rescuer)
3062 goto err;
3063
Tejun Heob196be82012-01-10 15:11:35 -08003064 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3065 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003066 if (IS_ERR(rescuer->task))
3067 goto err;
3068
Tejun Heoe22bee72010-06-29 10:07:14 +02003069 rescuer->task->flags |= PF_THREAD_BOUND;
3070 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003071 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003072
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003073 /*
3074 * workqueue_lock protects global freeze state and workqueues
3075 * list. Grab it, set max_active accordingly and add the new
3076 * workqueue to workqueues list.
3077 */
Tejun Heo15376632010-06-29 10:07:11 +02003078 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003079
Tejun Heo58a69cb2011-02-16 09:25:31 +01003080 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003081 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003082 get_cwq(cpu, wq)->max_active = 0;
3083
Tejun Heo15376632010-06-29 10:07:11 +02003084 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003085
Tejun Heo15376632010-06-29 10:07:11 +02003086 spin_unlock(&workqueue_lock);
3087
Oleg Nesterov3af244332007-05-09 02:34:09 -07003088 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003089err:
3090 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003091 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003092 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003093 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003094 kfree(wq);
3095 }
3096 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003097}
Tejun Heod320c032010-06-29 10:07:14 +02003098EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003099
3100/**
3101 * destroy_workqueue - safely terminate a workqueue
3102 * @wq: target workqueue
3103 *
3104 * Safely destroy a workqueue. All work currently pending will be done first.
3105 */
3106void destroy_workqueue(struct workqueue_struct *wq)
3107{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003108 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003109
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003110 /* drain it before proceeding with destruction */
3111 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003112
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003113 /*
3114 * wq list is used to freeze wq, remove from list after
3115 * flushing is complete in case freeze races us.
3116 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003117 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003118 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003119 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003120
Tejun Heoe22bee72010-06-29 10:07:14 +02003121 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003122 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003123 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3124 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125
Tejun Heo73f53c42010-06-29 10:07:11 +02003126 for (i = 0; i < WORK_NR_COLORS; i++)
3127 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003128 BUG_ON(cwq->nr_active);
3129 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003130 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003131
Tejun Heoe22bee72010-06-29 10:07:14 +02003132 if (wq->flags & WQ_RESCUER) {
3133 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003134 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003135 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003136 }
3137
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003138 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003139 kfree(wq);
3140}
3141EXPORT_SYMBOL_GPL(destroy_workqueue);
3142
Tejun Heodcd989c2010-06-29 10:07:14 +02003143/**
3144 * workqueue_set_max_active - adjust max_active of a workqueue
3145 * @wq: target workqueue
3146 * @max_active: new max_active value.
3147 *
3148 * Set max_active of @wq to @max_active.
3149 *
3150 * CONTEXT:
3151 * Don't call from IRQ context.
3152 */
3153void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3154{
3155 unsigned int cpu;
3156
Tejun Heof3421792010-07-02 10:03:51 +02003157 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003158
3159 spin_lock(&workqueue_lock);
3160
3161 wq->saved_max_active = max_active;
3162
Tejun Heof3421792010-07-02 10:03:51 +02003163 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003164 struct global_cwq *gcwq = get_gcwq(cpu);
3165
3166 spin_lock_irq(&gcwq->lock);
3167
Tejun Heo58a69cb2011-02-16 09:25:31 +01003168 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003169 !(gcwq->flags & GCWQ_FREEZING))
3170 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3171
3172 spin_unlock_irq(&gcwq->lock);
3173 }
3174
3175 spin_unlock(&workqueue_lock);
3176}
3177EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3178
3179/**
3180 * workqueue_congested - test whether a workqueue is congested
3181 * @cpu: CPU in question
3182 * @wq: target workqueue
3183 *
3184 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3185 * no synchronization around this function and the test result is
3186 * unreliable and only useful as advisory hints or for debugging.
3187 *
3188 * RETURNS:
3189 * %true if congested, %false otherwise.
3190 */
3191bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3192{
3193 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3194
3195 return !list_empty(&cwq->delayed_works);
3196}
3197EXPORT_SYMBOL_GPL(workqueue_congested);
3198
3199/**
3200 * work_cpu - return the last known associated cpu for @work
3201 * @work: the work of interest
3202 *
3203 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003204 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003205 */
3206unsigned int work_cpu(struct work_struct *work)
3207{
3208 struct global_cwq *gcwq = get_work_gcwq(work);
3209
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003210 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003211}
3212EXPORT_SYMBOL_GPL(work_cpu);
3213
3214/**
3215 * work_busy - test whether a work is currently pending or running
3216 * @work: the work to be tested
3217 *
3218 * Test whether @work is currently pending or running. There is no
3219 * synchronization around this function and the test result is
3220 * unreliable and only useful as advisory hints or for debugging.
3221 * Especially for reentrant wqs, the pending state might hide the
3222 * running state.
3223 *
3224 * RETURNS:
3225 * OR'd bitmask of WORK_BUSY_* bits.
3226 */
3227unsigned int work_busy(struct work_struct *work)
3228{
3229 struct global_cwq *gcwq = get_work_gcwq(work);
3230 unsigned long flags;
3231 unsigned int ret = 0;
3232
3233 if (!gcwq)
3234 return false;
3235
3236 spin_lock_irqsave(&gcwq->lock, flags);
3237
3238 if (work_pending(work))
3239 ret |= WORK_BUSY_PENDING;
3240 if (find_worker_executing_work(gcwq, work))
3241 ret |= WORK_BUSY_RUNNING;
3242
3243 spin_unlock_irqrestore(&gcwq->lock, flags);
3244
3245 return ret;
3246}
3247EXPORT_SYMBOL_GPL(work_busy);
3248
Tejun Heodb7bccf2010-06-29 10:07:12 +02003249/*
3250 * CPU hotplug.
3251 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003252 * There are two challenges in supporting CPU hotplug. Firstly, there
3253 * are a lot of assumptions on strong associations among work, cwq and
3254 * gcwq which make migrating pending and scheduled works very
3255 * difficult to implement without impacting hot paths. Secondly,
3256 * gcwqs serve mix of short, long and very long running works making
3257 * blocked draining impractical.
3258 *
3259 * This is solved by allowing a gcwq to be detached from CPU, running
3260 * it with unbound (rogue) workers and allowing it to be reattached
3261 * later if the cpu comes back online. A separate thread is created
3262 * to govern a gcwq in such state and is called the trustee of the
3263 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003264 *
3265 * Trustee states and their descriptions.
3266 *
3267 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3268 * new trustee is started with this state.
3269 *
3270 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003271 * assuming the manager role and making all existing
3272 * workers rogue. DOWN_PREPARE waits for trustee to
3273 * enter this state. After reaching IN_CHARGE, trustee
3274 * tries to execute the pending worklist until it's empty
3275 * and the state is set to BUTCHER, or the state is set
3276 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003277 *
3278 * BUTCHER Command state which is set by the cpu callback after
3279 * the cpu has went down. Once this state is set trustee
3280 * knows that there will be no new works on the worklist
3281 * and once the worklist is empty it can proceed to
3282 * killing idle workers.
3283 *
3284 * RELEASE Command state which is set by the cpu callback if the
3285 * cpu down has been canceled or it has come online
3286 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003287 * trying to drain or butcher and clears ROGUE, rebinds
3288 * all remaining workers back to the cpu and releases
3289 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003290 *
3291 * DONE Trustee will enter this state after BUTCHER or RELEASE
3292 * is complete.
3293 *
3294 * trustee CPU draining
3295 * took over down complete
3296 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3297 * | | ^
3298 * | CPU is back online v return workers |
3299 * ----------------> RELEASE --------------
3300 */
3301
3302/**
3303 * trustee_wait_event_timeout - timed event wait for trustee
3304 * @cond: condition to wait for
3305 * @timeout: timeout in jiffies
3306 *
3307 * wait_event_timeout() for trustee to use. Handles locking and
3308 * checks for RELEASE request.
3309 *
3310 * CONTEXT:
3311 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3312 * multiple times. To be used by trustee.
3313 *
3314 * RETURNS:
3315 * Positive indicating left time if @cond is satisfied, 0 if timed
3316 * out, -1 if canceled.
3317 */
3318#define trustee_wait_event_timeout(cond, timeout) ({ \
3319 long __ret = (timeout); \
3320 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3321 __ret) { \
3322 spin_unlock_irq(&gcwq->lock); \
3323 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3324 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3325 __ret); \
3326 spin_lock_irq(&gcwq->lock); \
3327 } \
3328 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3329})
3330
3331/**
3332 * trustee_wait_event - event wait for trustee
3333 * @cond: condition to wait for
3334 *
3335 * wait_event() for trustee to use. Automatically handles locking and
3336 * checks for CANCEL request.
3337 *
3338 * CONTEXT:
3339 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3340 * multiple times. To be used by trustee.
3341 *
3342 * RETURNS:
3343 * 0 if @cond is satisfied, -1 if canceled.
3344 */
3345#define trustee_wait_event(cond) ({ \
3346 long __ret1; \
3347 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3348 __ret1 < 0 ? -1 : 0; \
3349})
3350
Tejun Heo9c6bae02012-07-13 22:16:44 -07003351static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3352{
3353 struct worker_pool *pool;
3354
3355 for_each_worker_pool(pool, gcwq)
3356 if (pool->flags & POOL_MANAGING_WORKERS)
3357 return true;
3358 return false;
3359}
3360
3361static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3362{
3363 struct worker_pool *pool;
3364
3365 for_each_worker_pool(pool, gcwq)
3366 if (!list_empty(&pool->idle_list))
3367 return true;
3368 return false;
3369}
3370
Tejun Heodb7bccf2010-06-29 10:07:12 +02003371static int __cpuinit trustee_thread(void *__gcwq)
3372{
3373 struct global_cwq *gcwq = __gcwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003374 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003375 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003376 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003377 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003378 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003379 int i;
3380
3381 BUG_ON(gcwq->cpu != smp_processor_id());
3382
3383 spin_lock_irq(&gcwq->lock);
3384 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003385 * Claim the manager position and make all workers rogue.
3386 * Trustee must be bound to the target cpu and can't be
3387 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003388 */
3389 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo9c6bae02012-07-13 22:16:44 -07003390 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003391 BUG_ON(rc < 0);
3392
Tejun Heo9c6bae02012-07-13 22:16:44 -07003393 for_each_worker_pool(pool, gcwq) {
3394 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003395
Tejun Heo9c6bae02012-07-13 22:16:44 -07003396 list_for_each_entry(worker, &pool->idle_list, entry)
3397 worker->flags |= WORKER_ROGUE;
3398 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003399
3400 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003401 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003402
3403 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003404 * Call schedule() so that we cross rq->lock and thus can
3405 * guarantee sched callbacks see the rogue flag. This is
3406 * necessary as scheduler callbacks may be invoked from other
3407 * cpus.
3408 */
3409 spin_unlock_irq(&gcwq->lock);
3410 schedule();
3411 spin_lock_irq(&gcwq->lock);
3412
3413 /*
Tejun Heocb444762010-07-02 10:03:50 +02003414 * Sched callbacks are disabled now. Zap nr_running. After
3415 * this, nr_running stays zero and need_more_worker() and
3416 * keep_working() are always true as long as the worklist is
3417 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003418 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003419 for_each_worker_pool(pool, gcwq)
3420 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003421
3422 spin_unlock_irq(&gcwq->lock);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003423 for_each_worker_pool(pool, gcwq)
3424 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003425 spin_lock_irq(&gcwq->lock);
3426
3427 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003428 * We're now in charge. Notify and proceed to drain. We need
3429 * to keep the gcwq running during the whole CPU down
3430 * procedure as other cpu hotunplug callbacks may need to
3431 * flush currently running tasks.
3432 */
3433 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3434 wake_up_all(&gcwq->trustee_wait);
3435
3436 /*
3437 * The original cpu is in the process of dying and may go away
3438 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003439 * be migrated to other cpus. Try draining any left work. We
3440 * want to get it over with ASAP - spam rescuers, wake up as
3441 * many idlers as necessary and create new ones till the
3442 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003443 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003444 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003445 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003446 while (true) {
3447 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003448
Tejun Heo9c6bae02012-07-13 22:16:44 -07003449 for_each_worker_pool(pool, gcwq)
3450 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003451
Tejun Heo9c6bae02012-07-13 22:16:44 -07003452 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3453 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3454 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003455
Tejun Heo9c6bae02012-07-13 22:16:44 -07003456 for_each_worker_pool(pool, gcwq) {
3457 int nr_works = 0;
3458
3459 list_for_each_entry(work, &pool->worklist, entry) {
3460 send_mayday(work);
3461 nr_works++;
3462 }
3463
3464 list_for_each_entry(worker, &pool->idle_list, entry) {
3465 if (!nr_works--)
3466 break;
3467 wake_up_process(worker->task);
3468 }
3469
3470 if (need_to_create_worker(pool)) {
3471 spin_unlock_irq(&gcwq->lock);
3472 worker = create_worker(pool, false);
3473 spin_lock_irq(&gcwq->lock);
3474 if (worker) {
3475 worker->flags |= WORKER_ROGUE;
3476 start_worker(worker);
3477 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003478 }
3479 }
3480
Tejun Heodb7bccf2010-06-29 10:07:12 +02003481 /* give a breather */
3482 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3483 break;
3484 }
3485
Tejun Heoe22bee72010-06-29 10:07:14 +02003486 /*
3487 * Either all works have been scheduled and cpu is down, or
3488 * cpu down has already been canceled. Wait for and butcher
3489 * all workers till we're canceled.
3490 */
3491 do {
Tejun Heo9c6bae02012-07-13 22:16:44 -07003492 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3493
3494 i = 0;
3495 for_each_worker_pool(pool, gcwq) {
3496 while (!list_empty(&pool->idle_list)) {
3497 worker = list_first_entry(&pool->idle_list,
3498 struct worker, entry);
3499 destroy_worker(worker);
3500 }
3501 i |= pool->nr_workers;
3502 }
3503 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003504
3505 /*
3506 * At this point, either draining has completed and no worker
3507 * is left, or cpu down has been canceled or the cpu is being
3508 * brought back up. There shouldn't be any idle one left.
3509 * Tell the remaining busy ones to rebind once it finishes the
3510 * currently scheduled works by scheduling the rebind_work.
3511 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003512 for_each_worker_pool(pool, gcwq)
3513 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003514
3515 for_each_busy_worker(worker, i, pos, gcwq) {
3516 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003517 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003518
3519 /*
3520 * Rebind_work may race with future cpu hotplug
3521 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003522 * rebinding is scheduled. The morphing should
3523 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003524 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003525 worker_flags |= WORKER_REBIND;
3526 worker_flags &= ~WORKER_ROGUE;
3527 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003528
3529 /* queue rebind_work, wq doesn't matter, use the default one */
3530 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3531 work_data_bits(rebind_work)))
3532 continue;
3533
3534 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003535 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003536 worker->scheduled.next,
3537 work_color_to_flags(WORK_NO_COLOR));
3538 }
3539
3540 /* relinquish manager role */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003541 for_each_worker_pool(pool, gcwq)
3542 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003543
Tejun Heodb7bccf2010-06-29 10:07:12 +02003544 /* notify completion */
3545 gcwq->trustee = NULL;
3546 gcwq->trustee_state = TRUSTEE_DONE;
3547 wake_up_all(&gcwq->trustee_wait);
3548 spin_unlock_irq(&gcwq->lock);
3549 return 0;
3550}
3551
3552/**
3553 * wait_trustee_state - wait for trustee to enter the specified state
3554 * @gcwq: gcwq the trustee of interest belongs to
3555 * @state: target state to wait for
3556 *
3557 * Wait for the trustee to reach @state. DONE is already matched.
3558 *
3559 * CONTEXT:
3560 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3561 * multiple times. To be used by cpu_callback.
3562 */
3563static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003564__releases(&gcwq->lock)
3565__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003566{
3567 if (!(gcwq->trustee_state == state ||
3568 gcwq->trustee_state == TRUSTEE_DONE)) {
3569 spin_unlock_irq(&gcwq->lock);
3570 __wait_event(gcwq->trustee_wait,
3571 gcwq->trustee_state == state ||
3572 gcwq->trustee_state == TRUSTEE_DONE);
3573 spin_lock_irq(&gcwq->lock);
3574 }
3575}
3576
Oleg Nesterov3af244332007-05-09 02:34:09 -07003577static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3578 unsigned long action,
3579 void *hcpu)
3580{
3581 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003582 struct global_cwq *gcwq = get_gcwq(cpu);
3583 struct task_struct *new_trustee = NULL;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003584 struct worker *new_workers[NR_WORKER_POOLS] = { };
3585 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003586 unsigned long flags;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003587 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003589 action &= ~CPU_TASKS_FROZEN;
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003592 case CPU_DOWN_PREPARE:
3593 new_trustee = kthread_create(trustee_thread, gcwq,
3594 "workqueue_trustee/%d\n", cpu);
3595 if (IS_ERR(new_trustee))
3596 return notifier_from_errno(PTR_ERR(new_trustee));
3597 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003598 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003600 i = 0;
3601 for_each_worker_pool(pool, gcwq) {
3602 BUG_ON(pool->first_idle);
3603 new_workers[i] = create_worker(pool, false);
3604 if (!new_workers[i++])
3605 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 }
3608
Tejun Heodb7bccf2010-06-29 10:07:12 +02003609 /* some are called w/ irq disabled, don't disturb irq status */
3610 spin_lock_irqsave(&gcwq->lock, flags);
3611
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003612 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003613 case CPU_DOWN_PREPARE:
3614 /* initialize trustee and tell it to acquire the gcwq */
3615 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3616 gcwq->trustee = new_trustee;
3617 gcwq->trustee_state = TRUSTEE_START;
3618 wake_up_process(gcwq->trustee);
3619 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003620 /* fall through */
3621 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003622 i = 0;
3623 for_each_worker_pool(pool, gcwq) {
3624 BUG_ON(pool->first_idle);
3625 pool->first_idle = new_workers[i++];
3626 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003627 break;
3628
3629 case CPU_DYING:
3630 /*
3631 * Before this, the trustee and all workers except for
3632 * the ones which are still executing works from
3633 * before the last CPU down must be on the cpu. After
3634 * this, they'll all be diasporas.
3635 */
3636 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003637 break;
3638
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003639 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003640 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003641 /* fall through */
3642 case CPU_UP_CANCELED:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003643 for_each_worker_pool(pool, gcwq) {
3644 destroy_worker(pool->first_idle);
3645 pool->first_idle = NULL;
3646 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003647 break;
3648
3649 case CPU_DOWN_FAILED:
3650 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003651 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003652 if (gcwq->trustee_state != TRUSTEE_DONE) {
3653 gcwq->trustee_state = TRUSTEE_RELEASE;
3654 wake_up_process(gcwq->trustee);
3655 wait_trustee_state(gcwq, TRUSTEE_DONE);
3656 }
3657
Tejun Heoe22bee72010-06-29 10:07:14 +02003658 /*
3659 * Trustee is done and there might be no worker left.
3660 * Put the first_idle in and request a real manager to
3661 * take a look.
3662 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003663 for_each_worker_pool(pool, gcwq) {
3664 spin_unlock_irq(&gcwq->lock);
3665 kthread_bind(pool->first_idle->task, cpu);
3666 spin_lock_irq(&gcwq->lock);
3667 pool->flags |= POOL_MANAGE_WORKERS;
3668 start_worker(pool->first_idle);
3669 pool->first_idle = NULL;
3670 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003671 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003672 }
3673
Tejun Heodb7bccf2010-06-29 10:07:12 +02003674 spin_unlock_irqrestore(&gcwq->lock, flags);
3675
Tejun Heo15376632010-06-29 10:07:11 +02003676 return notifier_from_errno(0);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003677
3678err_destroy:
3679 if (new_trustee)
3680 kthread_stop(new_trustee);
3681
3682 spin_lock_irqsave(&gcwq->lock, flags);
3683 for (i = 0; i < NR_WORKER_POOLS; i++)
3684 if (new_workers[i])
3685 destroy_worker(new_workers[i]);
3686 spin_unlock_irqrestore(&gcwq->lock, flags);
3687
3688 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Tejun Heod3b42542012-07-17 12:39:26 -07003691/*
3692 * Workqueues should be brought up before normal priority CPU notifiers.
3693 * This will be registered high priority CPU notifier.
3694 */
3695static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3696 unsigned long action,
3697 void *hcpu)
3698{
3699 switch (action & ~CPU_TASKS_FROZEN) {
3700 case CPU_UP_PREPARE:
3701 case CPU_UP_CANCELED:
3702 case CPU_DOWN_FAILED:
3703 case CPU_ONLINE:
3704 return workqueue_cpu_callback(nfb, action, hcpu);
3705 }
3706 return NOTIFY_OK;
3707}
3708
3709/*
3710 * Workqueues should be brought down after normal priority CPU notifiers.
3711 * This will be registered as low priority CPU notifier.
3712 */
3713static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3714 unsigned long action,
3715 void *hcpu)
3716{
3717 switch (action & ~CPU_TASKS_FROZEN) {
3718 case CPU_DOWN_PREPARE:
3719 case CPU_DYING:
3720 case CPU_POST_DEAD:
3721 return workqueue_cpu_callback(nfb, action, hcpu);
3722 }
3723 return NOTIFY_OK;
3724}
3725
Rusty Russell2d3854a2008-11-05 13:39:10 +11003726#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003727
Rusty Russell2d3854a2008-11-05 13:39:10 +11003728struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003729 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003730 long (*fn)(void *);
3731 void *arg;
3732 long ret;
3733};
3734
Tejun Heofc7da7e2012-09-18 12:48:43 -07003735static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003736{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003737 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3738
Rusty Russell2d3854a2008-11-05 13:39:10 +11003739 wfc->ret = wfc->fn(wfc->arg);
3740}
3741
3742/**
3743 * work_on_cpu - run a function in user context on a particular cpu
3744 * @cpu: the cpu to run on
3745 * @fn: the function to run
3746 * @arg: the function arg
3747 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003748 * This will return the value @fn returns.
3749 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003750 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003751 */
3752long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3753{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003754 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003755
Tejun Heofc7da7e2012-09-18 12:48:43 -07003756 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3757 schedule_work_on(cpu, &wfc.work);
3758 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003759 return wfc.ret;
3760}
3761EXPORT_SYMBOL_GPL(work_on_cpu);
3762#endif /* CONFIG_SMP */
3763
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303765
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003766/**
3767 * freeze_workqueues_begin - begin freezing workqueues
3768 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003769 * Start freezing workqueues. After this function returns, all freezable
3770 * workqueues will queue new works to their frozen_works list instead of
3771 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003772 *
3773 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003774 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003775 */
3776void freeze_workqueues_begin(void)
3777{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003778 unsigned int cpu;
3779
3780 spin_lock(&workqueue_lock);
3781
3782 BUG_ON(workqueue_freezing);
3783 workqueue_freezing = true;
3784
Tejun Heof3421792010-07-02 10:03:51 +02003785 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003786 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003787 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003788
3789 spin_lock_irq(&gcwq->lock);
3790
Tejun Heodb7bccf2010-06-29 10:07:12 +02003791 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3792 gcwq->flags |= GCWQ_FREEZING;
3793
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 list_for_each_entry(wq, &workqueues, list) {
3795 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3796
Tejun Heo58a69cb2011-02-16 09:25:31 +01003797 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003798 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003799 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003800
3801 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003802 }
3803
3804 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003806
3807/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003808 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003809 *
3810 * Check whether freezing is complete. This function must be called
3811 * between freeze_workqueues_begin() and thaw_workqueues().
3812 *
3813 * CONTEXT:
3814 * Grabs and releases workqueue_lock.
3815 *
3816 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003817 * %true if some freezable workqueues are still busy. %false if freezing
3818 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003819 */
3820bool freeze_workqueues_busy(void)
3821{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003822 unsigned int cpu;
3823 bool busy = false;
3824
3825 spin_lock(&workqueue_lock);
3826
3827 BUG_ON(!workqueue_freezing);
3828
Tejun Heof3421792010-07-02 10:03:51 +02003829 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003830 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003831 /*
3832 * nr_active is monotonically decreasing. It's safe
3833 * to peek without lock.
3834 */
3835 list_for_each_entry(wq, &workqueues, list) {
3836 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3837
Tejun Heo58a69cb2011-02-16 09:25:31 +01003838 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003839 continue;
3840
3841 BUG_ON(cwq->nr_active < 0);
3842 if (cwq->nr_active) {
3843 busy = true;
3844 goto out_unlock;
3845 }
3846 }
3847 }
3848out_unlock:
3849 spin_unlock(&workqueue_lock);
3850 return busy;
3851}
3852
3853/**
3854 * thaw_workqueues - thaw workqueues
3855 *
3856 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003857 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003858 *
3859 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003860 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003861 */
3862void thaw_workqueues(void)
3863{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003864 unsigned int cpu;
3865
3866 spin_lock(&workqueue_lock);
3867
3868 if (!workqueue_freezing)
3869 goto out_unlock;
3870
Tejun Heof3421792010-07-02 10:03:51 +02003871 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003872 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003873 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003874 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003875
3876 spin_lock_irq(&gcwq->lock);
3877
Tejun Heodb7bccf2010-06-29 10:07:12 +02003878 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3879 gcwq->flags &= ~GCWQ_FREEZING;
3880
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003881 list_for_each_entry(wq, &workqueues, list) {
3882 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3883
Tejun Heo58a69cb2011-02-16 09:25:31 +01003884 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003885 continue;
3886
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003887 /* restore max_active and repopulate worklist */
3888 cwq->max_active = wq->saved_max_active;
3889
3890 while (!list_empty(&cwq->delayed_works) &&
3891 cwq->nr_active < cwq->max_active)
3892 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003893 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003894
Tejun Heo9c6bae02012-07-13 22:16:44 -07003895 for_each_worker_pool(pool, gcwq)
3896 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003897
Tejun Heo8b03ae32010-06-29 10:07:12 +02003898 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003899 }
3900
3901 workqueue_freezing = false;
3902out_unlock:
3903 spin_unlock(&workqueue_lock);
3904}
3905#endif /* CONFIG_FREEZER */
3906
Suresh Siddha6ee05782010-07-30 14:57:37 -07003907static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
Tejun Heoc34056a2010-06-29 10:07:11 +02003909 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003910 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003911
Tejun Heod3b42542012-07-17 12:39:26 -07003912 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3913 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003914
3915 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003916 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003917 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003918 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003919
3920 spin_lock_init(&gcwq->lock);
3921 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003922 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003923
Tejun Heoc8e55f32010-06-29 10:07:12 +02003924 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3925 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3926
Tejun Heo9c6bae02012-07-13 22:16:44 -07003927 for_each_worker_pool(pool, gcwq) {
3928 pool->gcwq = gcwq;
3929 INIT_LIST_HEAD(&pool->worklist);
3930 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003931
Tejun Heo9c6bae02012-07-13 22:16:44 -07003932 init_timer_deferrable(&pool->idle_timer);
3933 pool->idle_timer.function = idle_worker_timeout;
3934 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003935
Tejun Heo9c6bae02012-07-13 22:16:44 -07003936 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3937 (unsigned long)pool);
3938
3939 ida_init(&pool->worker_ida);
3940 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003941
3942 gcwq->trustee_state = TRUSTEE_DONE;
3943 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003944 }
3945
Tejun Heoe22bee72010-06-29 10:07:14 +02003946 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003947 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003948 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003949 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003950
Tejun Heo477a3c32010-08-31 10:54:35 +02003951 if (cpu != WORK_CPU_UNBOUND)
3952 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003953
3954 for_each_worker_pool(pool, gcwq) {
3955 struct worker *worker;
3956
3957 worker = create_worker(pool, true);
3958 BUG_ON(!worker);
3959 spin_lock_irq(&gcwq->lock);
3960 start_worker(worker);
3961 spin_unlock_irq(&gcwq->lock);
3962 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003963 }
3964
Tejun Heod320c032010-06-29 10:07:14 +02003965 system_wq = alloc_workqueue("events", 0, 0);
3966 system_long_wq = alloc_workqueue("events_long", 0, 0);
3967 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003968 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3969 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003970 system_freezable_wq = alloc_workqueue("events_freezable",
3971 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003972 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3973 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003974 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003975 !system_unbound_wq || !system_freezable_wq ||
3976 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003979early_initcall(init_workqueues);