blob: 1bf4b0e65253e296663d8e90326ae0f892d54a7c [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
Lai Jiangshan23f09132014-02-15 22:02:28 +08001474 /*
1475 * Once WORKER_DIE is set, the kworker may destroy itself at any
1476 * point. Pin to ensure the task stays until we're done with it.
1477 */
1478 get_task_struct(worker->task);
1479
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001481 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001482
1483 spin_unlock_irq(&gcwq->lock);
1484
Tejun Heoc34056a2010-06-29 10:07:11 +02001485 kthread_stop(worker->task);
Lai Jiangshan23f09132014-02-15 22:02:28 +08001486 put_task_struct(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001487 kfree(worker);
1488
Tejun Heo8b03ae32010-06-29 10:07:12 +02001489 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001490 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001491}
1492
Tejun Heo7ef6a932012-07-12 14:46:37 -07001493static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001494{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001495 struct worker_pool *pool = (void *)__pool;
1496 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001497
1498 spin_lock_irq(&gcwq->lock);
1499
Tejun Heo7ef6a932012-07-12 14:46:37 -07001500 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001501 struct worker *worker;
1502 unsigned long expires;
1503
1504 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001505 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001506 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1507
1508 if (time_before(jiffies, expires))
Tejun Heo7ef6a932012-07-12 14:46:37 -07001509 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001510 else {
1511 /* it's been idle for too long, wake up manager */
Tejun Heo22ad5642012-07-12 14:46:37 -07001512 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -07001513 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001514 }
1515 }
1516
1517 spin_unlock_irq(&gcwq->lock);
1518}
1519
1520static bool send_mayday(struct work_struct *work)
1521{
1522 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1523 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001524 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001525
1526 if (!(wq->flags & WQ_RESCUER))
1527 return false;
1528
1529 /* mayday mayday mayday */
Tejun Heo58658882012-07-12 14:46:37 -07001530 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001531 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1532 if (cpu == WORK_CPU_UNBOUND)
1533 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001534 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001535 wake_up_process(wq->rescuer->task);
1536 return true;
1537}
1538
Tejun Heo7ef6a932012-07-12 14:46:37 -07001539static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001540{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001541 struct worker_pool *pool = (void *)__pool;
1542 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001543 struct work_struct *work;
1544
1545 spin_lock_irq(&gcwq->lock);
1546
Tejun Heo7ef6a932012-07-12 14:46:37 -07001547 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001548 /*
1549 * We've been trying to create a new worker but
1550 * haven't been successful. We might be hitting an
1551 * allocation deadlock. Send distress signals to
1552 * rescuers.
1553 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001554 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001555 send_mayday(work);
1556 }
1557
1558 spin_unlock_irq(&gcwq->lock);
1559
Tejun Heo7ef6a932012-07-12 14:46:37 -07001560 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001561}
1562
1563/**
1564 * maybe_create_worker - create a new worker if necessary
Tejun Heo7ef6a932012-07-12 14:46:37 -07001565 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001566 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001567 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001568 * have at least one idle worker on return from this function. If
1569 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo7ef6a932012-07-12 14:46:37 -07001570 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001571 * possible allocation deadlock.
1572 *
1573 * On return, need_to_create_worker() is guaranteed to be false and
1574 * may_start_working() true.
1575 *
1576 * LOCKING:
1577 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1578 * multiple times. Does GFP_KERNEL allocations. Called only from
1579 * manager.
1580 *
1581 * RETURNS:
1582 * false if no action was taken and gcwq->lock stayed locked, true
1583 * otherwise.
1584 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001585static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001586__releases(&gcwq->lock)
1587__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001588{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001589 struct global_cwq *gcwq = pool->gcwq;
1590
1591 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001592 return false;
1593restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001594 spin_unlock_irq(&gcwq->lock);
1595
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001597 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001598
1599 while (true) {
1600 struct worker *worker;
1601
Tejun Heo7ef6a932012-07-12 14:46:37 -07001602 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001603 if (worker) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001604 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 spin_lock_irq(&gcwq->lock);
1606 start_worker(worker);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001607 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 return true;
1609 }
1610
Tejun Heo7ef6a932012-07-12 14:46:37 -07001611 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 break;
1613
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 __set_current_state(TASK_INTERRUPTIBLE);
1615 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001616
Tejun Heo7ef6a932012-07-12 14:46:37 -07001617 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001618 break;
1619 }
1620
Tejun Heo7ef6a932012-07-12 14:46:37 -07001621 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 spin_lock_irq(&gcwq->lock);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001623 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001624 goto restart;
1625 return true;
1626}
1627
1628/**
1629 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo7ef6a932012-07-12 14:46:37 -07001630 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001631 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001632 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 * IDLE_WORKER_TIMEOUT.
1634 *
1635 * LOCKING:
1636 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1637 * multiple times. Called only from manager.
1638 *
1639 * RETURNS:
1640 * false if no action was taken and gcwq->lock stayed locked, true
1641 * otherwise.
1642 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001643static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001644{
1645 bool ret = false;
1646
Tejun Heo7ef6a932012-07-12 14:46:37 -07001647 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001648 struct worker *worker;
1649 unsigned long expires;
1650
Tejun Heo7ef6a932012-07-12 14:46:37 -07001651 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001652 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1653
1654 if (time_before(jiffies, expires)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001655 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001656 break;
1657 }
1658
1659 destroy_worker(worker);
1660 ret = true;
1661 }
1662
1663 return ret;
1664}
1665
1666/**
1667 * manage_workers - manage worker pool
1668 * @worker: self
1669 *
1670 * Assume the manager role and manage gcwq worker pool @worker belongs
1671 * to. At any given time, there can be only zero or one manager per
1672 * gcwq. The exclusion is handled automatically by this function.
1673 *
1674 * The caller can safely start processing works on false return. On
1675 * true return, it's guaranteed that need_to_create_worker() is false
1676 * and may_start_working() is true.
1677 *
1678 * CONTEXT:
1679 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1680 * multiple times. Does GFP_KERNEL allocations.
1681 *
1682 * RETURNS:
1683 * false if no action was taken and gcwq->lock stayed locked, true if
1684 * some action was taken.
1685 */
1686static bool manage_workers(struct worker *worker)
1687{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001688 struct worker_pool *pool = worker->pool;
1689 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001690 bool ret = false;
1691
Tejun Heo22ad5642012-07-12 14:46:37 -07001692 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001693 return ret;
1694
Tejun Heo22ad5642012-07-12 14:46:37 -07001695 pool->flags &= ~POOL_MANAGE_WORKERS;
1696 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001697
1698 /*
1699 * Destroy and then create so that may_start_working() is true
1700 * on return.
1701 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001702 ret |= maybe_destroy_workers(pool);
1703 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001704
Tejun Heo22ad5642012-07-12 14:46:37 -07001705 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001706
1707 /*
1708 * The trustee might be waiting to take over the manager
1709 * position, tell it we're done.
1710 */
1711 if (unlikely(gcwq->trustee))
1712 wake_up_all(&gcwq->trustee_wait);
1713
1714 return ret;
1715}
1716
Tejun Heoa62428c2010-06-29 10:07:10 +02001717/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001718 * move_linked_works - move linked works to a list
1719 * @work: start of series of works to be scheduled
1720 * @head: target list to append @work to
1721 * @nextp: out paramter for nested worklist walking
1722 *
1723 * Schedule linked works starting from @work to @head. Work series to
1724 * be scheduled starts at @work and includes any consecutive work with
1725 * WORK_STRUCT_LINKED set in its predecessor.
1726 *
1727 * If @nextp is not NULL, it's updated to point to the next work of
1728 * the last scheduled work. This allows move_linked_works() to be
1729 * nested inside outer list_for_each_entry_safe().
1730 *
1731 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001732 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001733 */
1734static void move_linked_works(struct work_struct *work, struct list_head *head,
1735 struct work_struct **nextp)
1736{
1737 struct work_struct *n;
1738
1739 /*
1740 * Linked worklist will always end before the end of the list,
1741 * use NULL for list head.
1742 */
1743 list_for_each_entry_safe_from(work, n, NULL, entry) {
1744 list_move_tail(&work->entry, head);
1745 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1746 break;
1747 }
1748
1749 /*
1750 * If we're already inside safe list traversal and have moved
1751 * multiple works to the scheduled queue, the next position
1752 * needs to be updated.
1753 */
1754 if (nextp)
1755 *nextp = n;
1756}
1757
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001758static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001759{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001760 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001761
Tejun Heocdadf002010-10-05 10:49:55 +02001762 trace_workqueue_activate_work(work);
Tejun Heodcb32ee2012-07-13 22:16:45 -07001763 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001764 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001765 cwq->nr_active++;
1766}
1767
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001768static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1769{
1770 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1771 struct work_struct, entry);
1772
1773 cwq_activate_delayed_work(work);
1774}
1775
Tejun Heoaffee4b2010-06-29 10:07:12 +02001776/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001777 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1778 * @cwq: cwq of interest
1779 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001780 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001781 *
1782 * A work either has completed or is removed from pending queue,
1783 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1784 *
1785 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001786 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001787 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001788static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1789 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001790{
1791 /* ignore uncolored works */
1792 if (color == WORK_NO_COLOR)
1793 return;
1794
1795 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001796
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001797 if (!delayed) {
1798 cwq->nr_active--;
1799 if (!list_empty(&cwq->delayed_works)) {
1800 /* one down, submit a delayed one */
1801 if (cwq->nr_active < cwq->max_active)
1802 cwq_activate_first_delayed(cwq);
1803 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001804 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001805
1806 /* is flush in progress and are we at the flushing tip? */
1807 if (likely(cwq->flush_color != color))
1808 return;
1809
1810 /* are there still in-flight works? */
1811 if (cwq->nr_in_flight[color])
1812 return;
1813
1814 /* this cwq is done, clear flush_color */
1815 cwq->flush_color = -1;
1816
1817 /*
1818 * If this was the last cwq, wake up the first flusher. It
1819 * will handle the rest.
1820 */
1821 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1822 complete(&cwq->wq->first_flusher->done);
1823}
1824
1825/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001826 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001827 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001828 * @work: work to process
1829 *
1830 * Process @work. This function contains all the logics necessary to
1831 * process a single work including synchronization against and
1832 * interaction with other workers on the same cpu, queueing and
1833 * flushing. As long as context requirement is met, any worker can
1834 * call this function to process a work.
1835 *
1836 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001837 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001838 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001839static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001840__releases(&gcwq->lock)
1841__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001842{
Tejun Heo7e116292010-06-29 10:07:13 +02001843 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07001844 struct worker_pool *pool = worker->pool;
1845 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001846 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001847 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001848 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001849 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001850#ifdef CONFIG_LOCKDEP
1851 /*
1852 * It is permissible to free the struct work_struct from
1853 * inside the function that is called from it, this we need to
1854 * take into account for lockdep too. To avoid bogus "held
1855 * lock freed" warnings as well as problems when looking into
1856 * work->lockdep_map, make a copy and use that here.
1857 */
1858 struct lockdep_map lockdep_map = work->lockdep_map;
1859#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001860 /*
1861 * A single work shouldn't be executed concurrently by
1862 * multiple workers on a single cpu. Check whether anyone is
1863 * already processing the work. If so, defer the work to the
1864 * currently executing one.
1865 */
1866 collision = __find_worker_executing_work(gcwq, bwh, work);
1867 if (unlikely(collision)) {
1868 move_linked_works(work, &collision->scheduled, NULL);
1869 return;
1870 }
1871
Tejun Heoa62428c2010-06-29 10:07:10 +02001872 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001873 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001874 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001875 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001876 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001877 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001878 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001879
Tejun Heo7a22ad72010-06-29 10:07:13 +02001880 /* record the current cpu number in the work data and dequeue */
1881 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001882 list_del_init(&work->entry);
1883
Tejun Heo649027d2010-06-29 10:07:14 +02001884 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001885 * CPU intensive works don't participate in concurrency
1886 * management. They're the scheduler's responsibility.
1887 */
1888 if (unlikely(cpu_intensive))
1889 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1890
Tejun Heob7b5c682012-07-12 14:46:37 -07001891 /*
1892 * Unbound gcwq isn't concurrency managed and work items should be
1893 * executed ASAP. Wake up another worker if necessary.
1894 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001895 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1896 wake_up_worker(pool);
Tejun Heob7b5c682012-07-12 14:46:37 -07001897
Tejun Heo8b03ae32010-06-29 10:07:12 +02001898 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001899
Tejun Heo66307ae2012-08-03 10:30:45 -07001900 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001901 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001902
Tejun Heoe1594892011-01-09 23:32:15 +01001903 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001904 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001905 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001906 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001907 /*
1908 * While we must be careful to not use "work" after this, the trace
1909 * point will only record its address.
1910 */
1911 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001912 lock_map_release(&lockdep_map);
1913 lock_map_release(&cwq->wq->lockdep_map);
1914
1915 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001916 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1917 " last function: %pf\n",
1918 current->comm, preempt_count(), task_pid_nr(current),
1919 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001920 debug_show_held_locks(current);
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -07001921 BUG_ON(PANIC_CORRUPTION);
Tejun Heoa62428c2010-06-29 10:07:10 +02001922 dump_stack();
1923 }
1924
Tejun Heo00cef7a2013-08-28 17:33:37 -04001925 /*
1926 * The following prevents a kworker from hogging CPU on !PREEMPT
1927 * kernels, where a requeueing work item waiting for something to
1928 * happen could deadlock with stop_machine as such work item could
1929 * indefinitely requeue itself while all other CPUs are trapped in
1930 * stop_machine.
1931 */
1932 cond_resched();
1933
Tejun Heo8b03ae32010-06-29 10:07:12 +02001934 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001935
Tejun Heofb0e7be2010-06-29 10:07:15 +02001936 /* clear cpu intensive status */
1937 if (unlikely(cpu_intensive))
1938 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1939
Tejun Heoa62428c2010-06-29 10:07:10 +02001940 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001941 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001942 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001943 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001944 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001945 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001946}
1947
Tejun Heoaffee4b2010-06-29 10:07:12 +02001948/**
1949 * process_scheduled_works - process scheduled works
1950 * @worker: self
1951 *
1952 * Process all scheduled works. Please note that the scheduled list
1953 * may change while processing a work, so this function repeatedly
1954 * fetches a work from the top and executes it.
1955 *
1956 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001957 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001958 * multiple times.
1959 */
1960static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001962 while (!list_empty(&worker->scheduled)) {
1963 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001965 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Tejun Heo4690c4a2010-06-29 10:07:10 +02001969/**
1970 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001971 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001972 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 * The gcwq worker thread function. There's a single dynamic pool of
1974 * these per each cpu. These workers process all works regardless of
1975 * their specific target workqueue. The only exception is works which
1976 * belong to workqueues with a rescuer which will be explained in
1977 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001978 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001979static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Tejun Heoc34056a2010-06-29 10:07:11 +02001981 struct worker *worker = __worker;
Tejun Heo58658882012-07-12 14:46:37 -07001982 struct worker_pool *pool = worker->pool;
1983 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 /* tell the scheduler that this is a workqueue worker */
1986 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001987woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001988 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Tejun Heoc8e55f32010-06-29 10:07:12 +02001990 /* DIE can be set only while we're idle, checking here is enough */
1991 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001992 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996
Tejun Heoc8e55f32010-06-29 10:07:12 +02001997 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001998recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001999 /* no more worker necessary? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002000 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 goto sleep;
2002
2003 /* do we need to manage? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002004 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002005 goto recheck;
2006
Tejun Heoc8e55f32010-06-29 10:07:12 +02002007 /*
2008 * ->scheduled list can only be filled while a worker is
2009 * preparing to process a work or actually processing it.
2010 * Make sure nobody diddled with it while I was sleeping.
2011 */
2012 BUG_ON(!list_empty(&worker->scheduled));
2013
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 /*
2015 * When control reaches this point, we're guaranteed to have
2016 * at least one idle worker or that someone else has already
2017 * assumed the manager role.
2018 */
2019 worker_clr_flags(worker, WORKER_PREP);
2020
2021 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002022 struct work_struct *work =
Tejun Heo58658882012-07-12 14:46:37 -07002023 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002024 struct work_struct, entry);
2025
2026 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2027 /* optimization path, not strictly necessary */
2028 process_one_work(worker, work);
2029 if (unlikely(!list_empty(&worker->scheduled)))
2030 process_scheduled_works(worker);
2031 } else {
2032 move_linked_works(work, &worker->scheduled, NULL);
2033 process_scheduled_works(worker);
2034 }
Tejun Heo7ef6a932012-07-12 14:46:37 -07002035 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002036
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002038sleep:
Tejun Heo7ef6a932012-07-12 14:46:37 -07002039 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002040 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002041
Tejun Heoc8e55f32010-06-29 10:07:12 +02002042 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002043 * gcwq->lock is held and there's no work to process and no
2044 * need to manage, sleep. Workers are woken up only while
2045 * holding gcwq->lock or from local cpu, so setting the
2046 * current state before releasing gcwq->lock is enough to
2047 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002048 */
2049 worker_enter_idle(worker);
2050 __set_current_state(TASK_INTERRUPTIBLE);
2051 spin_unlock_irq(&gcwq->lock);
2052 schedule();
2053 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Tejun Heoe22bee72010-06-29 10:07:14 +02002056/**
2057 * rescuer_thread - the rescuer thread function
2058 * @__wq: the associated workqueue
2059 *
2060 * Workqueue rescuer thread function. There's one rescuer for each
2061 * workqueue which has WQ_RESCUER set.
2062 *
2063 * Regular work processing on a gcwq may block trying to create a new
2064 * worker which uses GFP_KERNEL allocation which has slight chance of
2065 * developing into deadlock if some works currently on the same queue
2066 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2067 * the problem rescuer solves.
2068 *
2069 * When such condition is possible, the gcwq summons rescuers of all
2070 * workqueues which have works queued on the gcwq and let them process
2071 * those works so that forward progress can be guaranteed.
2072 *
2073 * This should happen rarely.
2074 */
2075static int rescuer_thread(void *__wq)
2076{
2077 struct workqueue_struct *wq = __wq;
2078 struct worker *rescuer = wq->rescuer;
2079 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002080 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002081 unsigned int cpu;
2082
2083 set_user_nice(current, RESCUER_NICE_LEVEL);
2084repeat:
2085 set_current_state(TASK_INTERRUPTIBLE);
2086
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002087 if (kthread_should_stop()) {
2088 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002089 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002090 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002091
Tejun Heof3421792010-07-02 10:03:51 +02002092 /*
2093 * See whether any cpu is asking for help. Unbounded
2094 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2095 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002096 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002097 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2098 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002099 struct worker_pool *pool = cwq->pool;
2100 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002101 struct work_struct *work, *n;
2102
2103 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002104 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002105
2106 /* migrate to the target cpu if possible */
Tejun Heo58658882012-07-12 14:46:37 -07002107 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002108 worker_maybe_bind_and_lock(rescuer);
2109
2110 /*
2111 * Slurp in all works issued via this workqueue and
2112 * process'em.
2113 */
2114 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo58658882012-07-12 14:46:37 -07002115 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002116 if (get_work_cwq(work) == cwq)
2117 move_linked_works(work, scheduled, &n);
2118
2119 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002120
2121 /*
2122 * Leave this gcwq. If keep_working() is %true, notify a
2123 * regular worker; otherwise, we end up with 0 concurrency
2124 * and stalling the execution.
2125 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002126 if (keep_working(pool))
2127 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002128
Tejun Heoe22bee72010-06-29 10:07:14 +02002129 spin_unlock_irq(&gcwq->lock);
2130 }
2131
2132 schedule();
2133 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002136struct wq_barrier {
2137 struct work_struct work;
2138 struct completion done;
2139};
2140
2141static void wq_barrier_func(struct work_struct *work)
2142{
2143 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2144 complete(&barr->done);
2145}
2146
Tejun Heo4690c4a2010-06-29 10:07:10 +02002147/**
2148 * insert_wq_barrier - insert a barrier work
2149 * @cwq: cwq to insert barrier into
2150 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002151 * @target: target work to attach @barr to
2152 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002153 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002154 * @barr is linked to @target such that @barr is completed only after
2155 * @target finishes execution. Please note that the ordering
2156 * guarantee is observed only with respect to @target and on the local
2157 * cpu.
2158 *
2159 * Currently, a queued barrier can't be canceled. This is because
2160 * try_to_grab_pending() can't determine whether the work to be
2161 * grabbed is at the head of the queue and thus can't clear LINKED
2162 * flag of the previous work while there must be a valid next work
2163 * after a work with LINKED flag set.
2164 *
2165 * Note that when @worker is non-NULL, @target may be modified
2166 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002167 *
2168 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002169 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002170 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002171static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002172 struct wq_barrier *barr,
2173 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002174{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002175 struct list_head *head;
2176 unsigned int linked = 0;
2177
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002178 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002179 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002180 * as we know for sure that this will not trigger any of the
2181 * checks and call back into the fixup functions where we
2182 * might deadlock.
2183 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002184 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002185 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002186 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002187
Tejun Heoaffee4b2010-06-29 10:07:12 +02002188 /*
2189 * If @target is currently being executed, schedule the
2190 * barrier to the worker; otherwise, put it after @target.
2191 */
2192 if (worker)
2193 head = worker->scheduled.next;
2194 else {
2195 unsigned long *bits = work_data_bits(target);
2196
2197 head = target->entry.next;
2198 /* there can already be other linked works, inherit and set */
2199 linked = *bits & WORK_STRUCT_LINKED;
2200 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2201 }
2202
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002203 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002204 insert_work(cwq, &barr->work, head,
2205 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002206}
2207
Tejun Heo73f53c42010-06-29 10:07:11 +02002208/**
2209 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2210 * @wq: workqueue being flushed
2211 * @flush_color: new flush color, < 0 for no-op
2212 * @work_color: new work color, < 0 for no-op
2213 *
2214 * Prepare cwqs for workqueue flushing.
2215 *
2216 * If @flush_color is non-negative, flush_color on all cwqs should be
2217 * -1. If no cwq has in-flight commands at the specified color, all
2218 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2219 * has in flight commands, its cwq->flush_color is set to
2220 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2221 * wakeup logic is armed and %true is returned.
2222 *
2223 * The caller should have initialized @wq->first_flusher prior to
2224 * calling this function with non-negative @flush_color. If
2225 * @flush_color is negative, no flush color update is done and %false
2226 * is returned.
2227 *
2228 * If @work_color is non-negative, all cwqs should have the same
2229 * work_color which is previous to @work_color and all will be
2230 * advanced to @work_color.
2231 *
2232 * CONTEXT:
2233 * mutex_lock(wq->flush_mutex).
2234 *
2235 * RETURNS:
2236 * %true if @flush_color >= 0 and there's something to flush. %false
2237 * otherwise.
2238 */
2239static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2240 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Tejun Heo73f53c42010-06-29 10:07:11 +02002242 bool wait = false;
2243 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002244
Tejun Heo73f53c42010-06-29 10:07:11 +02002245 if (flush_color >= 0) {
2246 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2247 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002248 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002249
Tejun Heof3421792010-07-02 10:03:51 +02002250 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002251 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002252 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002253
Tejun Heo8b03ae32010-06-29 10:07:12 +02002254 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002255
2256 if (flush_color >= 0) {
2257 BUG_ON(cwq->flush_color != -1);
2258
2259 if (cwq->nr_in_flight[flush_color]) {
2260 cwq->flush_color = flush_color;
2261 atomic_inc(&wq->nr_cwqs_to_flush);
2262 wait = true;
2263 }
2264 }
2265
2266 if (work_color >= 0) {
2267 BUG_ON(work_color != work_next_color(cwq->work_color));
2268 cwq->work_color = work_color;
2269 }
2270
Tejun Heo8b03ae32010-06-29 10:07:12 +02002271 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002272 }
2273
2274 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2275 complete(&wq->first_flusher->done);
2276
2277 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002282 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 *
2284 * Forces execution of the workqueue and blocks until its completion.
2285 * This is typically used in driver shutdown handlers.
2286 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002287 * We sleep until all works which were queued on entry have been handled,
2288 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002290void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Tejun Heo73f53c42010-06-29 10:07:11 +02002292 struct wq_flusher this_flusher = {
2293 .list = LIST_HEAD_INIT(this_flusher.list),
2294 .flush_color = -1,
2295 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2296 };
2297 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002298
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002299 lock_map_acquire(&wq->lockdep_map);
2300 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002301
2302 mutex_lock(&wq->flush_mutex);
2303
2304 /*
2305 * Start-to-wait phase
2306 */
2307 next_color = work_next_color(wq->work_color);
2308
2309 if (next_color != wq->flush_color) {
2310 /*
2311 * Color space is not full. The current work_color
2312 * becomes our flush_color and work_color is advanced
2313 * by one.
2314 */
2315 BUG_ON(!list_empty(&wq->flusher_overflow));
2316 this_flusher.flush_color = wq->work_color;
2317 wq->work_color = next_color;
2318
2319 if (!wq->first_flusher) {
2320 /* no flush in progress, become the first flusher */
2321 BUG_ON(wq->flush_color != this_flusher.flush_color);
2322
2323 wq->first_flusher = &this_flusher;
2324
2325 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2326 wq->work_color)) {
2327 /* nothing to flush, done */
2328 wq->flush_color = next_color;
2329 wq->first_flusher = NULL;
2330 goto out_unlock;
2331 }
2332 } else {
2333 /* wait in queue */
2334 BUG_ON(wq->flush_color == this_flusher.flush_color);
2335 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2336 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2337 }
2338 } else {
2339 /*
2340 * Oops, color space is full, wait on overflow queue.
2341 * The next flush completion will assign us
2342 * flush_color and transfer to flusher_queue.
2343 */
2344 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2345 }
2346
2347 mutex_unlock(&wq->flush_mutex);
2348
2349 wait_for_completion(&this_flusher.done);
2350
2351 /*
2352 * Wake-up-and-cascade phase
2353 *
2354 * First flushers are responsible for cascading flushes and
2355 * handling overflow. Non-first flushers can simply return.
2356 */
2357 if (wq->first_flusher != &this_flusher)
2358 return;
2359
2360 mutex_lock(&wq->flush_mutex);
2361
Tejun Heo4ce48b32010-07-02 10:03:51 +02002362 /* we might have raced, check again with mutex held */
2363 if (wq->first_flusher != &this_flusher)
2364 goto out_unlock;
2365
Tejun Heo73f53c42010-06-29 10:07:11 +02002366 wq->first_flusher = NULL;
2367
2368 BUG_ON(!list_empty(&this_flusher.list));
2369 BUG_ON(wq->flush_color != this_flusher.flush_color);
2370
2371 while (true) {
2372 struct wq_flusher *next, *tmp;
2373
2374 /* complete all the flushers sharing the current flush color */
2375 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2376 if (next->flush_color != wq->flush_color)
2377 break;
2378 list_del_init(&next->list);
2379 complete(&next->done);
2380 }
2381
2382 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2383 wq->flush_color != work_next_color(wq->work_color));
2384
2385 /* this flush_color is finished, advance by one */
2386 wq->flush_color = work_next_color(wq->flush_color);
2387
2388 /* one color has been freed, handle overflow queue */
2389 if (!list_empty(&wq->flusher_overflow)) {
2390 /*
2391 * Assign the same color to all overflowed
2392 * flushers, advance work_color and append to
2393 * flusher_queue. This is the start-to-wait
2394 * phase for these overflowed flushers.
2395 */
2396 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2397 tmp->flush_color = wq->work_color;
2398
2399 wq->work_color = work_next_color(wq->work_color);
2400
2401 list_splice_tail_init(&wq->flusher_overflow,
2402 &wq->flusher_queue);
2403 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2404 }
2405
2406 if (list_empty(&wq->flusher_queue)) {
2407 BUG_ON(wq->flush_color != wq->work_color);
2408 break;
2409 }
2410
2411 /*
2412 * Need to flush more colors. Make the next flusher
2413 * the new first flusher and arm cwqs.
2414 */
2415 BUG_ON(wq->flush_color == wq->work_color);
2416 BUG_ON(wq->flush_color != next->flush_color);
2417
2418 list_del_init(&next->list);
2419 wq->first_flusher = next;
2420
2421 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2422 break;
2423
2424 /*
2425 * Meh... this color is already done, clear first
2426 * flusher and repeat cascading.
2427 */
2428 wq->first_flusher = NULL;
2429 }
2430
2431out_unlock:
2432 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
Dave Jonesae90dd52006-06-30 01:40:45 -04002434EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002436/**
2437 * drain_workqueue - drain a workqueue
2438 * @wq: workqueue to drain
2439 *
2440 * Wait until the workqueue becomes empty. While draining is in progress,
2441 * only chain queueing is allowed. IOW, only currently pending or running
2442 * work items on @wq can queue further work items on it. @wq is flushed
2443 * repeatedly until it becomes empty. The number of flushing is detemined
2444 * by the depth of chaining and should be relatively short. Whine if it
2445 * takes too long.
2446 */
2447void drain_workqueue(struct workqueue_struct *wq)
2448{
2449 unsigned int flush_cnt = 0;
2450 unsigned int cpu;
2451
2452 /*
2453 * __queue_work() needs to test whether there are drainers, is much
2454 * hotter than drain_workqueue() and already looks at @wq->flags.
2455 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2456 */
2457 spin_lock(&workqueue_lock);
2458 if (!wq->nr_drainers++)
2459 wq->flags |= WQ_DRAINING;
2460 spin_unlock(&workqueue_lock);
2461reflush:
2462 flush_workqueue(wq);
2463
2464 for_each_cwq_cpu(cpu, wq) {
2465 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002466 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002467
Tejun Heo58658882012-07-12 14:46:37 -07002468 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002469 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo58658882012-07-12 14:46:37 -07002470 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002471
2472 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002473 continue;
2474
2475 if (++flush_cnt == 10 ||
2476 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2477 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2478 wq->name, flush_cnt);
2479 goto reflush;
2480 }
2481
2482 spin_lock(&workqueue_lock);
2483 if (!--wq->nr_drainers)
2484 wq->flags &= ~WQ_DRAINING;
2485 spin_unlock(&workqueue_lock);
2486}
2487EXPORT_SYMBOL_GPL(drain_workqueue);
2488
Tejun Heobaf59022010-09-16 10:42:16 +02002489static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2490 bool wait_executing)
2491{
2492 struct worker *worker = NULL;
2493 struct global_cwq *gcwq;
2494 struct cpu_workqueue_struct *cwq;
2495
2496 might_sleep();
2497 gcwq = get_work_gcwq(work);
2498 if (!gcwq)
2499 return false;
2500
2501 spin_lock_irq(&gcwq->lock);
2502 if (!list_empty(&work->entry)) {
2503 /*
2504 * See the comment near try_to_grab_pending()->smp_rmb().
2505 * If it was re-queued to a different gcwq under us, we
2506 * are not going to wait.
2507 */
2508 smp_rmb();
2509 cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07002510 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002511 goto already_gone;
2512 } else if (wait_executing) {
2513 worker = find_worker_executing_work(gcwq, work);
2514 if (!worker)
2515 goto already_gone;
2516 cwq = worker->current_cwq;
2517 } else
2518 goto already_gone;
2519
2520 insert_wq_barrier(cwq, barr, work, worker);
2521 spin_unlock_irq(&gcwq->lock);
2522
Tejun Heoe1594892011-01-09 23:32:15 +01002523 /*
2524 * If @max_active is 1 or rescuer is in use, flushing another work
2525 * item on the same workqueue may lead to deadlock. Make sure the
2526 * flusher is not running on the same workqueue by verifying write
2527 * access.
2528 */
2529 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2530 lock_map_acquire(&cwq->wq->lockdep_map);
2531 else
2532 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002533 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002534
Tejun Heobaf59022010-09-16 10:42:16 +02002535 return true;
2536already_gone:
2537 spin_unlock_irq(&gcwq->lock);
2538 return false;
2539}
2540
Oleg Nesterovdb700892008-07-25 01:47:49 -07002541/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002542 * flush_work - wait for a work to finish executing the last queueing instance
2543 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002544 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002545 * Wait until @work has finished execution. This function considers
2546 * only the last queueing instance of @work. If @work has been
2547 * enqueued across different CPUs on a non-reentrant workqueue or on
2548 * multiple workqueues, @work might still be executing on return on
2549 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002550 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002551 * If @work was queued only on a non-reentrant, ordered or unbound
2552 * workqueue, @work is guaranteed to be idle on return if it hasn't
2553 * been requeued since flush started.
2554 *
2555 * RETURNS:
2556 * %true if flush_work() waited for the work to finish execution,
2557 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002558 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002559bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002560{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002561 struct wq_barrier barr;
2562
Tejun Heobaf59022010-09-16 10:42:16 +02002563 if (start_flush_work(work, &barr, true)) {
2564 wait_for_completion(&barr.done);
2565 destroy_work_on_stack(&barr.work);
2566 return true;
2567 } else
2568 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002569}
2570EXPORT_SYMBOL_GPL(flush_work);
2571
Tejun Heo401a8d02010-09-16 10:36:00 +02002572static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2573{
2574 struct wq_barrier barr;
2575 struct worker *worker;
2576
2577 spin_lock_irq(&gcwq->lock);
2578
2579 worker = find_worker_executing_work(gcwq, work);
2580 if (unlikely(worker))
2581 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2582
2583 spin_unlock_irq(&gcwq->lock);
2584
2585 if (unlikely(worker)) {
2586 wait_for_completion(&barr.done);
2587 destroy_work_on_stack(&barr.work);
2588 return true;
2589 } else
2590 return false;
2591}
2592
2593static bool wait_on_work(struct work_struct *work)
2594{
2595 bool ret = false;
2596 int cpu;
2597
2598 might_sleep();
2599
2600 lock_map_acquire(&work->lockdep_map);
2601 lock_map_release(&work->lockdep_map);
2602
2603 for_each_gcwq_cpu(cpu)
2604 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2605 return ret;
2606}
2607
Tejun Heo09383492010-09-16 10:48:29 +02002608/**
2609 * flush_work_sync - wait until a work has finished execution
2610 * @work: the work to flush
2611 *
2612 * Wait until @work has finished execution. On return, it's
2613 * guaranteed that all queueing instances of @work which happened
2614 * before this function is called are finished. In other words, if
2615 * @work hasn't been requeued since this function was called, @work is
2616 * guaranteed to be idle on return.
2617 *
2618 * RETURNS:
2619 * %true if flush_work_sync() waited for the work to finish execution,
2620 * %false if it was already idle.
2621 */
2622bool flush_work_sync(struct work_struct *work)
2623{
2624 struct wq_barrier barr;
2625 bool pending, waited;
2626
2627 /* we'll wait for executions separately, queue barr only if pending */
2628 pending = start_flush_work(work, &barr, false);
2629
2630 /* wait for executions to finish */
2631 waited = wait_on_work(work);
2632
2633 /* wait for the pending one */
2634 if (pending) {
2635 wait_for_completion(&barr.done);
2636 destroy_work_on_stack(&barr.work);
2637 }
2638
2639 return pending || waited;
2640}
2641EXPORT_SYMBOL_GPL(flush_work_sync);
2642
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002643/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002644 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002645 * so this work can't be re-armed in any way.
2646 */
2647static int try_to_grab_pending(struct work_struct *work)
2648{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002649 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002650 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002651
Tejun Heo22df02b2010-06-29 10:07:10 +02002652 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002653 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654
2655 /*
2656 * The queueing is in progress, or it is already queued. Try to
2657 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2658 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002659 gcwq = get_work_gcwq(work);
2660 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002661 return ret;
2662
Tejun Heo8b03ae32010-06-29 10:07:12 +02002663 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002664 if (!list_empty(&work->entry)) {
2665 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002666 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667 * In that case we must see the new value after rmb(), see
2668 * insert_work()->wmb().
2669 */
2670 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002671 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002672 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002673
2674 /*
2675 * A delayed work item cannot be grabbed directly
2676 * because it might have linked NO_COLOR work items
2677 * which, if left on the delayed_list, will confuse
2678 * cwq->nr_active management later on and cause
2679 * stall. Make sure the work item is activated
2680 * before grabbing.
2681 */
2682 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2683 cwq_activate_delayed_work(work);
2684
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002685 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002686 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002687 get_work_color(work),
2688 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002689 ret = 1;
2690 }
2691 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002692 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002693
2694 return ret;
2695}
2696
Tejun Heo401a8d02010-09-16 10:36:00 +02002697static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002698 struct timer_list* timer)
2699{
2700 int ret;
2701
2702 do {
2703 ret = (timer && likely(del_timer(timer)));
2704 if (!ret)
2705 ret = try_to_grab_pending(work);
2706 wait_on_work(work);
2707 } while (unlikely(ret < 0));
2708
Tejun Heo7a22ad72010-06-29 10:07:13 +02002709 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002710 return ret;
2711}
2712
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002713/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002714 * cancel_work_sync - cancel a work and wait for it to finish
2715 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002716 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002717 * Cancel @work and wait for its execution to finish. This function
2718 * can be used even if the work re-queues itself or migrates to
2719 * another workqueue. On return from this function, @work is
2720 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002721 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002722 * cancel_work_sync(&delayed_work->work) must not be used for
2723 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002724 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002725 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002726 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 *
2728 * RETURNS:
2729 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002731bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002732{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002733 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002734}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002735EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002736
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002737/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002738 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2739 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002740 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002741 * Delayed timer is cancelled and the pending work is queued for
2742 * immediate execution. Like flush_work(), this function only
2743 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002744 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002745 * RETURNS:
2746 * %true if flush_work() waited for the work to finish execution,
2747 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002748 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002749bool flush_delayed_work(struct delayed_work *dwork)
2750{
2751 if (del_timer_sync(&dwork->timer))
2752 __queue_work(raw_smp_processor_id(),
2753 get_work_cwq(&dwork->work)->wq, &dwork->work);
2754 return flush_work(&dwork->work);
2755}
2756EXPORT_SYMBOL(flush_delayed_work);
2757
2758/**
Tejun Heo09383492010-09-16 10:48:29 +02002759 * flush_delayed_work_sync - wait for a dwork to finish
2760 * @dwork: the delayed work to flush
2761 *
2762 * Delayed timer is cancelled and the pending work is queued for
2763 * execution immediately. Other than timer handling, its behavior
2764 * is identical to flush_work_sync().
2765 *
2766 * RETURNS:
2767 * %true if flush_work_sync() waited for the work to finish execution,
2768 * %false if it was already idle.
2769 */
2770bool flush_delayed_work_sync(struct delayed_work *dwork)
2771{
2772 if (del_timer_sync(&dwork->timer))
2773 __queue_work(raw_smp_processor_id(),
2774 get_work_cwq(&dwork->work)->wq, &dwork->work);
2775 return flush_work_sync(&dwork->work);
2776}
2777EXPORT_SYMBOL(flush_delayed_work_sync);
2778
2779/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002780 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2781 * @dwork: the delayed work cancel
2782 *
2783 * This is cancel_work_sync() for delayed works.
2784 *
2785 * RETURNS:
2786 * %true if @dwork was pending, %false otherwise.
2787 */
2788bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002789{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002790 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002791}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002792EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002794/**
2795 * schedule_work - put work task in global workqueue
2796 * @work: job to be done
2797 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002798 * Returns zero if @work was already on the kernel-global workqueue and
2799 * non-zero otherwise.
2800 *
2801 * This puts a job in the kernel-global workqueue if it was not already
2802 * queued and leaves it in the same position on the kernel-global
2803 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002804 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002805int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Tejun Heod320c032010-06-29 10:07:14 +02002807 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
Dave Jonesae90dd52006-06-30 01:40:45 -04002809EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Zhang Ruic1a220e2008-07-23 21:28:39 -07002811/*
2812 * schedule_work_on - put work task on a specific cpu
2813 * @cpu: cpu to put the work task on
2814 * @work: job to be done
2815 *
2816 * This puts a job on a specific cpu
2817 */
2818int schedule_work_on(int cpu, struct work_struct *work)
2819{
Tejun Heod320c032010-06-29 10:07:14 +02002820 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002821}
2822EXPORT_SYMBOL(schedule_work_on);
2823
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002824/**
2825 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002826 * @dwork: job to be done
2827 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002828 *
2829 * After waiting for a given time this puts a job in the kernel-global
2830 * workqueue.
2831 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002832int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002833 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Tejun Heod320c032010-06-29 10:07:14 +02002835 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
Dave Jonesae90dd52006-06-30 01:40:45 -04002837EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002839/**
2840 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2841 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002842 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002843 * @delay: number of jiffies to wait
2844 *
2845 * After waiting for a given time this puts a job in the kernel-global
2846 * workqueue on the specified CPU.
2847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002849 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Tejun Heod320c032010-06-29 10:07:14 +02002851 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
Dave Jonesae90dd52006-06-30 01:40:45 -04002853EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Andrew Mortonb6136772006-06-25 05:47:49 -07002855/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002856 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002857 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002858 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002859 * schedule_on_each_cpu() executes @func on each online CPU using the
2860 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002861 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002862 *
2863 * RETURNS:
2864 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002865 */
David Howells65f27f32006-11-22 14:55:48 +00002866int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002867{
2868 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002869 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002870
Andrew Mortonb6136772006-06-25 05:47:49 -07002871 works = alloc_percpu(struct work_struct);
2872 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002873 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002874
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002875 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002876
Christoph Lameter15316ba2006-01-08 01:00:43 -08002877 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002878 struct work_struct *work = per_cpu_ptr(works, cpu);
2879
2880 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002881 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002882 }
Tejun Heo93981802009-11-17 14:06:20 -08002883
2884 for_each_online_cpu(cpu)
2885 flush_work(per_cpu_ptr(works, cpu));
2886
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002887 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002888 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002889 return 0;
2890}
2891
Alan Sterneef6a7d2010-02-12 17:39:21 +09002892/**
2893 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2894 *
2895 * Forces execution of the kernel-global workqueue and blocks until its
2896 * completion.
2897 *
2898 * Think twice before calling this function! It's very easy to get into
2899 * trouble if you don't take great care. Either of the following situations
2900 * will lead to deadlock:
2901 *
2902 * One of the work items currently on the workqueue needs to acquire
2903 * a lock held by your code or its caller.
2904 *
2905 * Your code is running in the context of a work routine.
2906 *
2907 * They will be detected by lockdep when they occur, but the first might not
2908 * occur very often. It depends on what work items are on the workqueue and
2909 * what locks they need, which you have no control over.
2910 *
2911 * In most situations flushing the entire workqueue is overkill; you merely
2912 * need to know that a particular work item isn't queued and isn't running.
2913 * In such cases you should use cancel_delayed_work_sync() or
2914 * cancel_work_sync() instead.
2915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916void flush_scheduled_work(void)
2917{
Tejun Heod320c032010-06-29 10:07:14 +02002918 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
Dave Jonesae90dd52006-06-30 01:40:45 -04002920EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002923 * execute_in_process_context - reliably execute the routine with user context
2924 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002925 * @ew: guaranteed storage for the execute work structure (must
2926 * be available when the work executes)
2927 *
2928 * Executes the function immediately if process context is available,
2929 * otherwise schedules the function for delayed execution.
2930 *
2931 * Returns: 0 - function was executed
2932 * 1 - function was scheduled for execution
2933 */
David Howells65f27f32006-11-22 14:55:48 +00002934int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002935{
2936 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002937 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002938 return 0;
2939 }
2940
David Howells65f27f32006-11-22 14:55:48 +00002941 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002942 schedule_work(&ew->work);
2943
2944 return 1;
2945}
2946EXPORT_SYMBOL_GPL(execute_in_process_context);
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948int keventd_up(void)
2949{
Tejun Heod320c032010-06-29 10:07:14 +02002950 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002953static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002955 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002956 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2957 * Make sure that the alignment isn't lower than that of
2958 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002959 */
Tejun Heo0f900042010-06-29 10:07:11 +02002960 const size_t size = sizeof(struct cpu_workqueue_struct);
2961 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2962 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002963
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002964 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002965 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002966 else {
Tejun Heof3421792010-07-02 10:03:51 +02002967 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002968
Tejun Heof3421792010-07-02 10:03:51 +02002969 /*
2970 * Allocate enough room to align cwq and put an extra
2971 * pointer at the end pointing back to the originally
2972 * allocated pointer which will be used for free.
2973 */
2974 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2975 if (ptr) {
2976 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2977 *(void **)(wq->cpu_wq.single + 1) = ptr;
2978 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002979 }
Tejun Heof3421792010-07-02 10:03:51 +02002980
Tejun Heo0415b002011-03-24 18:50:09 +01002981 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002982 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2983 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002984}
2985
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002986static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002987{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002988 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002989 free_percpu(wq->cpu_wq.pcpu);
2990 else if (wq->cpu_wq.single) {
2991 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002992 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002993 }
2994}
2995
Tejun Heof3421792010-07-02 10:03:51 +02002996static int wq_clamp_max_active(int max_active, unsigned int flags,
2997 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002998{
Tejun Heof3421792010-07-02 10:03:51 +02002999 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3000
3001 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003002 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3003 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003004 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003005
Tejun Heof3421792010-07-02 10:03:51 +02003006 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003007}
3008
Tejun Heob196be82012-01-10 15:11:35 -08003009struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003010 unsigned int flags,
3011 int max_active,
3012 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003013 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003014{
Tejun Heob196be82012-01-10 15:11:35 -08003015 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003016 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003017 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003018 size_t namelen;
3019
3020 /* determine namelen, allocate wq and format name */
3021 va_start(args, lock_name);
3022 va_copy(args1, args);
3023 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3024
3025 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3026 if (!wq)
3027 goto err;
3028
3029 vsnprintf(wq->name, namelen, fmt, args1);
3030 va_end(args);
3031 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003032
Tejun Heof3421792010-07-02 10:03:51 +02003033 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003034 * Workqueues which may be used during memory reclaim should
3035 * have a rescuer to guarantee forward progress.
3036 */
3037 if (flags & WQ_MEM_RECLAIM)
3038 flags |= WQ_RESCUER;
3039
Tejun Heod320c032010-06-29 10:07:14 +02003040 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003041 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003042
Tejun Heob196be82012-01-10 15:11:35 -08003043 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003044 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003045 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003046 mutex_init(&wq->flush_mutex);
3047 atomic_set(&wq->nr_cwqs_to_flush, 0);
3048 INIT_LIST_HEAD(&wq->flusher_queue);
3049 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003050
Johannes Bergeb13ba82008-01-16 09:51:58 +01003051 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003052 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003053
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003054 if (alloc_cwqs(wq) < 0)
3055 goto err;
3056
Tejun Heof3421792010-07-02 10:03:51 +02003057 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003058 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003059 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003060 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003061
Tejun Heo0f900042010-06-29 10:07:11 +02003062 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003063 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003064 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003065 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003066 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003067 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003068 }
3069
Tejun Heoe22bee72010-06-29 10:07:14 +02003070 if (flags & WQ_RESCUER) {
3071 struct worker *rescuer;
3072
Tejun Heof2e005a2010-07-20 15:59:09 +02003073 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003074 goto err;
3075
3076 wq->rescuer = rescuer = alloc_worker();
3077 if (!rescuer)
3078 goto err;
3079
Tejun Heob196be82012-01-10 15:11:35 -08003080 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3081 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003082 if (IS_ERR(rescuer->task))
3083 goto err;
3084
Tejun Heoe22bee72010-06-29 10:07:14 +02003085 rescuer->task->flags |= PF_THREAD_BOUND;
3086 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003087 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003088
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003089 /*
3090 * workqueue_lock protects global freeze state and workqueues
3091 * list. Grab it, set max_active accordingly and add the new
3092 * workqueue to workqueues list.
3093 */
Tejun Heo15376632010-06-29 10:07:11 +02003094 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003095
Tejun Heo58a69cb2011-02-16 09:25:31 +01003096 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003097 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003098 get_cwq(cpu, wq)->max_active = 0;
3099
Tejun Heo15376632010-06-29 10:07:11 +02003100 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003101
Tejun Heo15376632010-06-29 10:07:11 +02003102 spin_unlock(&workqueue_lock);
3103
Oleg Nesterov3af244332007-05-09 02:34:09 -07003104 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003105err:
3106 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003107 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003108 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003109 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003110 kfree(wq);
3111 }
3112 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003113}
Tejun Heod320c032010-06-29 10:07:14 +02003114EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003115
3116/**
3117 * destroy_workqueue - safely terminate a workqueue
3118 * @wq: target workqueue
3119 *
3120 * Safely destroy a workqueue. All work currently pending will be done first.
3121 */
3122void destroy_workqueue(struct workqueue_struct *wq)
3123{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003124 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003126 /* drain it before proceeding with destruction */
3127 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003128
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003129 /*
3130 * wq list is used to freeze wq, remove from list after
3131 * flushing is complete in case freeze races us.
3132 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003133 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003134 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003135 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003136
Tejun Heoe22bee72010-06-29 10:07:14 +02003137 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003138 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003139 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3140 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003141
Tejun Heo73f53c42010-06-29 10:07:11 +02003142 for (i = 0; i < WORK_NR_COLORS; i++)
3143 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003144 BUG_ON(cwq->nr_active);
3145 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003146 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003147
Tejun Heoe22bee72010-06-29 10:07:14 +02003148 if (wq->flags & WQ_RESCUER) {
3149 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003150 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003151 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003152 }
3153
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003154 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003155 kfree(wq);
3156}
3157EXPORT_SYMBOL_GPL(destroy_workqueue);
3158
Tejun Heodcd989c2010-06-29 10:07:14 +02003159/**
3160 * workqueue_set_max_active - adjust max_active of a workqueue
3161 * @wq: target workqueue
3162 * @max_active: new max_active value.
3163 *
3164 * Set max_active of @wq to @max_active.
3165 *
3166 * CONTEXT:
3167 * Don't call from IRQ context.
3168 */
3169void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3170{
3171 unsigned int cpu;
3172
Tejun Heof3421792010-07-02 10:03:51 +02003173 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003174
3175 spin_lock(&workqueue_lock);
3176
3177 wq->saved_max_active = max_active;
3178
Tejun Heof3421792010-07-02 10:03:51 +02003179 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003180 struct global_cwq *gcwq = get_gcwq(cpu);
3181
3182 spin_lock_irq(&gcwq->lock);
3183
Tejun Heo58a69cb2011-02-16 09:25:31 +01003184 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003185 !(gcwq->flags & GCWQ_FREEZING))
3186 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3187
3188 spin_unlock_irq(&gcwq->lock);
3189 }
3190
3191 spin_unlock(&workqueue_lock);
3192}
3193EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3194
3195/**
3196 * workqueue_congested - test whether a workqueue is congested
3197 * @cpu: CPU in question
3198 * @wq: target workqueue
3199 *
3200 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3201 * no synchronization around this function and the test result is
3202 * unreliable and only useful as advisory hints or for debugging.
3203 *
3204 * RETURNS:
3205 * %true if congested, %false otherwise.
3206 */
3207bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3208{
3209 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3210
3211 return !list_empty(&cwq->delayed_works);
3212}
3213EXPORT_SYMBOL_GPL(workqueue_congested);
3214
3215/**
3216 * work_cpu - return the last known associated cpu for @work
3217 * @work: the work of interest
3218 *
3219 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003220 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003221 */
3222unsigned int work_cpu(struct work_struct *work)
3223{
3224 struct global_cwq *gcwq = get_work_gcwq(work);
3225
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003226 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003227}
3228EXPORT_SYMBOL_GPL(work_cpu);
3229
3230/**
3231 * work_busy - test whether a work is currently pending or running
3232 * @work: the work to be tested
3233 *
3234 * Test whether @work is currently pending or running. There is no
3235 * synchronization around this function and the test result is
3236 * unreliable and only useful as advisory hints or for debugging.
3237 * Especially for reentrant wqs, the pending state might hide the
3238 * running state.
3239 *
3240 * RETURNS:
3241 * OR'd bitmask of WORK_BUSY_* bits.
3242 */
3243unsigned int work_busy(struct work_struct *work)
3244{
3245 struct global_cwq *gcwq = get_work_gcwq(work);
3246 unsigned long flags;
3247 unsigned int ret = 0;
3248
3249 if (!gcwq)
3250 return false;
3251
3252 spin_lock_irqsave(&gcwq->lock, flags);
3253
3254 if (work_pending(work))
3255 ret |= WORK_BUSY_PENDING;
3256 if (find_worker_executing_work(gcwq, work))
3257 ret |= WORK_BUSY_RUNNING;
3258
3259 spin_unlock_irqrestore(&gcwq->lock, flags);
3260
3261 return ret;
3262}
3263EXPORT_SYMBOL_GPL(work_busy);
3264
Tejun Heodb7bccf2010-06-29 10:07:12 +02003265/*
3266 * CPU hotplug.
3267 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003268 * There are two challenges in supporting CPU hotplug. Firstly, there
3269 * are a lot of assumptions on strong associations among work, cwq and
3270 * gcwq which make migrating pending and scheduled works very
3271 * difficult to implement without impacting hot paths. Secondly,
3272 * gcwqs serve mix of short, long and very long running works making
3273 * blocked draining impractical.
3274 *
3275 * This is solved by allowing a gcwq to be detached from CPU, running
3276 * it with unbound (rogue) workers and allowing it to be reattached
3277 * later if the cpu comes back online. A separate thread is created
3278 * to govern a gcwq in such state and is called the trustee of the
3279 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003280 *
3281 * Trustee states and their descriptions.
3282 *
3283 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3284 * new trustee is started with this state.
3285 *
3286 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003287 * assuming the manager role and making all existing
3288 * workers rogue. DOWN_PREPARE waits for trustee to
3289 * enter this state. After reaching IN_CHARGE, trustee
3290 * tries to execute the pending worklist until it's empty
3291 * and the state is set to BUTCHER, or the state is set
3292 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003293 *
3294 * BUTCHER Command state which is set by the cpu callback after
3295 * the cpu has went down. Once this state is set trustee
3296 * knows that there will be no new works on the worklist
3297 * and once the worklist is empty it can proceed to
3298 * killing idle workers.
3299 *
3300 * RELEASE Command state which is set by the cpu callback if the
3301 * cpu down has been canceled or it has come online
3302 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003303 * trying to drain or butcher and clears ROGUE, rebinds
3304 * all remaining workers back to the cpu and releases
3305 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003306 *
3307 * DONE Trustee will enter this state after BUTCHER or RELEASE
3308 * is complete.
3309 *
3310 * trustee CPU draining
3311 * took over down complete
3312 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3313 * | | ^
3314 * | CPU is back online v return workers |
3315 * ----------------> RELEASE --------------
3316 */
3317
3318/**
3319 * trustee_wait_event_timeout - timed event wait for trustee
3320 * @cond: condition to wait for
3321 * @timeout: timeout in jiffies
3322 *
3323 * wait_event_timeout() for trustee to use. Handles locking and
3324 * checks for RELEASE request.
3325 *
3326 * CONTEXT:
3327 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3328 * multiple times. To be used by trustee.
3329 *
3330 * RETURNS:
3331 * Positive indicating left time if @cond is satisfied, 0 if timed
3332 * out, -1 if canceled.
3333 */
3334#define trustee_wait_event_timeout(cond, timeout) ({ \
3335 long __ret = (timeout); \
3336 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3337 __ret) { \
3338 spin_unlock_irq(&gcwq->lock); \
3339 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3340 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3341 __ret); \
3342 spin_lock_irq(&gcwq->lock); \
3343 } \
3344 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3345})
3346
3347/**
3348 * trustee_wait_event - event wait for trustee
3349 * @cond: condition to wait for
3350 *
3351 * wait_event() for trustee to use. Automatically handles locking and
3352 * checks for CANCEL request.
3353 *
3354 * CONTEXT:
3355 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3356 * multiple times. To be used by trustee.
3357 *
3358 * RETURNS:
3359 * 0 if @cond is satisfied, -1 if canceled.
3360 */
3361#define trustee_wait_event(cond) ({ \
3362 long __ret1; \
3363 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3364 __ret1 < 0 ? -1 : 0; \
3365})
3366
Tejun Heo9c6bae02012-07-13 22:16:44 -07003367static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3368{
3369 struct worker_pool *pool;
3370
3371 for_each_worker_pool(pool, gcwq)
3372 if (pool->flags & POOL_MANAGING_WORKERS)
3373 return true;
3374 return false;
3375}
3376
3377static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3378{
3379 struct worker_pool *pool;
3380
3381 for_each_worker_pool(pool, gcwq)
3382 if (!list_empty(&pool->idle_list))
3383 return true;
3384 return false;
3385}
3386
Tejun Heodb7bccf2010-06-29 10:07:12 +02003387static int __cpuinit trustee_thread(void *__gcwq)
3388{
3389 struct global_cwq *gcwq = __gcwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003390 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003391 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003392 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003393 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003394 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003395 int i;
3396
3397 BUG_ON(gcwq->cpu != smp_processor_id());
3398
3399 spin_lock_irq(&gcwq->lock);
3400 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003401 * Claim the manager position and make all workers rogue.
3402 * Trustee must be bound to the target cpu and can't be
3403 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003404 */
3405 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo9c6bae02012-07-13 22:16:44 -07003406 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003407 BUG_ON(rc < 0);
3408
Tejun Heo9c6bae02012-07-13 22:16:44 -07003409 for_each_worker_pool(pool, gcwq) {
3410 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003411
Tejun Heo9c6bae02012-07-13 22:16:44 -07003412 list_for_each_entry(worker, &pool->idle_list, entry)
3413 worker->flags |= WORKER_ROGUE;
3414 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003415
3416 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003417 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003418
3419 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003420 * Call schedule() so that we cross rq->lock and thus can
3421 * guarantee sched callbacks see the rogue flag. This is
3422 * necessary as scheduler callbacks may be invoked from other
3423 * cpus.
3424 */
3425 spin_unlock_irq(&gcwq->lock);
3426 schedule();
3427 spin_lock_irq(&gcwq->lock);
3428
3429 /*
Tejun Heocb444762010-07-02 10:03:50 +02003430 * Sched callbacks are disabled now. Zap nr_running. After
3431 * this, nr_running stays zero and need_more_worker() and
3432 * keep_working() are always true as long as the worklist is
3433 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003434 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003435 for_each_worker_pool(pool, gcwq)
3436 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003437
3438 spin_unlock_irq(&gcwq->lock);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003439 for_each_worker_pool(pool, gcwq)
3440 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003441 spin_lock_irq(&gcwq->lock);
3442
3443 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003444 * We're now in charge. Notify and proceed to drain. We need
3445 * to keep the gcwq running during the whole CPU down
3446 * procedure as other cpu hotunplug callbacks may need to
3447 * flush currently running tasks.
3448 */
3449 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3450 wake_up_all(&gcwq->trustee_wait);
3451
3452 /*
3453 * The original cpu is in the process of dying and may go away
3454 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003455 * be migrated to other cpus. Try draining any left work. We
3456 * want to get it over with ASAP - spam rescuers, wake up as
3457 * many idlers as necessary and create new ones till the
3458 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003459 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003460 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003461 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003462 while (true) {
3463 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003464
Tejun Heo9c6bae02012-07-13 22:16:44 -07003465 for_each_worker_pool(pool, gcwq)
3466 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003467
Tejun Heo9c6bae02012-07-13 22:16:44 -07003468 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3469 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3470 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003471
Tejun Heo9c6bae02012-07-13 22:16:44 -07003472 for_each_worker_pool(pool, gcwq) {
3473 int nr_works = 0;
3474
3475 list_for_each_entry(work, &pool->worklist, entry) {
3476 send_mayday(work);
3477 nr_works++;
3478 }
3479
3480 list_for_each_entry(worker, &pool->idle_list, entry) {
3481 if (!nr_works--)
3482 break;
3483 wake_up_process(worker->task);
3484 }
3485
3486 if (need_to_create_worker(pool)) {
3487 spin_unlock_irq(&gcwq->lock);
3488 worker = create_worker(pool, false);
3489 spin_lock_irq(&gcwq->lock);
3490 if (worker) {
3491 worker->flags |= WORKER_ROGUE;
3492 start_worker(worker);
3493 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003494 }
3495 }
3496
Tejun Heodb7bccf2010-06-29 10:07:12 +02003497 /* give a breather */
3498 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3499 break;
3500 }
3501
Tejun Heoe22bee72010-06-29 10:07:14 +02003502 /*
3503 * Either all works have been scheduled and cpu is down, or
3504 * cpu down has already been canceled. Wait for and butcher
3505 * all workers till we're canceled.
3506 */
3507 do {
Tejun Heo9c6bae02012-07-13 22:16:44 -07003508 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3509
3510 i = 0;
3511 for_each_worker_pool(pool, gcwq) {
3512 while (!list_empty(&pool->idle_list)) {
3513 worker = list_first_entry(&pool->idle_list,
3514 struct worker, entry);
3515 destroy_worker(worker);
3516 }
3517 i |= pool->nr_workers;
3518 }
3519 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003520
3521 /*
3522 * At this point, either draining has completed and no worker
3523 * is left, or cpu down has been canceled or the cpu is being
3524 * brought back up. There shouldn't be any idle one left.
3525 * Tell the remaining busy ones to rebind once it finishes the
3526 * currently scheduled works by scheduling the rebind_work.
3527 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003528 for_each_worker_pool(pool, gcwq)
3529 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003530
3531 for_each_busy_worker(worker, i, pos, gcwq) {
3532 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003533 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003534
3535 /*
3536 * Rebind_work may race with future cpu hotplug
3537 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003538 * rebinding is scheduled. The morphing should
3539 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003540 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003541 worker_flags |= WORKER_REBIND;
3542 worker_flags &= ~WORKER_ROGUE;
3543 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003544
3545 /* queue rebind_work, wq doesn't matter, use the default one */
3546 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3547 work_data_bits(rebind_work)))
3548 continue;
3549
3550 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003551 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003552 worker->scheduled.next,
3553 work_color_to_flags(WORK_NO_COLOR));
3554 }
3555
3556 /* relinquish manager role */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003557 for_each_worker_pool(pool, gcwq)
3558 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003559
Tejun Heodb7bccf2010-06-29 10:07:12 +02003560 /* notify completion */
3561 gcwq->trustee = NULL;
3562 gcwq->trustee_state = TRUSTEE_DONE;
3563 wake_up_all(&gcwq->trustee_wait);
3564 spin_unlock_irq(&gcwq->lock);
3565 return 0;
3566}
3567
3568/**
3569 * wait_trustee_state - wait for trustee to enter the specified state
3570 * @gcwq: gcwq the trustee of interest belongs to
3571 * @state: target state to wait for
3572 *
3573 * Wait for the trustee to reach @state. DONE is already matched.
3574 *
3575 * CONTEXT:
3576 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3577 * multiple times. To be used by cpu_callback.
3578 */
3579static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003580__releases(&gcwq->lock)
3581__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003582{
3583 if (!(gcwq->trustee_state == state ||
3584 gcwq->trustee_state == TRUSTEE_DONE)) {
3585 spin_unlock_irq(&gcwq->lock);
3586 __wait_event(gcwq->trustee_wait,
3587 gcwq->trustee_state == state ||
3588 gcwq->trustee_state == TRUSTEE_DONE);
3589 spin_lock_irq(&gcwq->lock);
3590 }
3591}
3592
Oleg Nesterov3af244332007-05-09 02:34:09 -07003593static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3594 unsigned long action,
3595 void *hcpu)
3596{
3597 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003598 struct global_cwq *gcwq = get_gcwq(cpu);
3599 struct task_struct *new_trustee = NULL;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003600 struct worker *new_workers[NR_WORKER_POOLS] = { };
3601 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003602 unsigned long flags;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003603 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003605 action &= ~CPU_TASKS_FROZEN;
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003608 case CPU_DOWN_PREPARE:
3609 new_trustee = kthread_create(trustee_thread, gcwq,
3610 "workqueue_trustee/%d\n", cpu);
3611 if (IS_ERR(new_trustee))
3612 return notifier_from_errno(PTR_ERR(new_trustee));
3613 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003614 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003616 i = 0;
3617 for_each_worker_pool(pool, gcwq) {
3618 BUG_ON(pool->first_idle);
3619 new_workers[i] = create_worker(pool, false);
3620 if (!new_workers[i++])
3621 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
3624
Tejun Heodb7bccf2010-06-29 10:07:12 +02003625 /* some are called w/ irq disabled, don't disturb irq status */
3626 spin_lock_irqsave(&gcwq->lock, flags);
3627
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003628 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003629 case CPU_DOWN_PREPARE:
3630 /* initialize trustee and tell it to acquire the gcwq */
3631 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3632 gcwq->trustee = new_trustee;
3633 gcwq->trustee_state = TRUSTEE_START;
3634 wake_up_process(gcwq->trustee);
3635 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003636 /* fall through */
3637 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003638 i = 0;
3639 for_each_worker_pool(pool, gcwq) {
3640 BUG_ON(pool->first_idle);
3641 pool->first_idle = new_workers[i++];
3642 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003643 break;
3644
3645 case CPU_DYING:
3646 /*
3647 * Before this, the trustee and all workers except for
3648 * the ones which are still executing works from
3649 * before the last CPU down must be on the cpu. After
3650 * this, they'll all be diasporas.
3651 */
3652 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003653 break;
3654
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003655 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003656 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003657 /* fall through */
3658 case CPU_UP_CANCELED:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003659 for_each_worker_pool(pool, gcwq) {
3660 destroy_worker(pool->first_idle);
3661 pool->first_idle = NULL;
3662 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003663 break;
3664
3665 case CPU_DOWN_FAILED:
3666 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003667 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003668 if (gcwq->trustee_state != TRUSTEE_DONE) {
3669 gcwq->trustee_state = TRUSTEE_RELEASE;
3670 wake_up_process(gcwq->trustee);
3671 wait_trustee_state(gcwq, TRUSTEE_DONE);
3672 }
3673
Tejun Heoe22bee72010-06-29 10:07:14 +02003674 /*
3675 * Trustee is done and there might be no worker left.
3676 * Put the first_idle in and request a real manager to
3677 * take a look.
3678 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003679 for_each_worker_pool(pool, gcwq) {
3680 spin_unlock_irq(&gcwq->lock);
3681 kthread_bind(pool->first_idle->task, cpu);
3682 spin_lock_irq(&gcwq->lock);
3683 pool->flags |= POOL_MANAGE_WORKERS;
3684 start_worker(pool->first_idle);
3685 pool->first_idle = NULL;
3686 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003687 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003688 }
3689
Tejun Heodb7bccf2010-06-29 10:07:12 +02003690 spin_unlock_irqrestore(&gcwq->lock, flags);
3691
Tejun Heo15376632010-06-29 10:07:11 +02003692 return notifier_from_errno(0);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003693
3694err_destroy:
3695 if (new_trustee)
3696 kthread_stop(new_trustee);
3697
3698 spin_lock_irqsave(&gcwq->lock, flags);
3699 for (i = 0; i < NR_WORKER_POOLS; i++)
3700 if (new_workers[i])
3701 destroy_worker(new_workers[i]);
3702 spin_unlock_irqrestore(&gcwq->lock, flags);
3703
3704 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Tejun Heod3b42542012-07-17 12:39:26 -07003707/*
3708 * Workqueues should be brought up before normal priority CPU notifiers.
3709 * This will be registered high priority CPU notifier.
3710 */
3711static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3712 unsigned long action,
3713 void *hcpu)
3714{
3715 switch (action & ~CPU_TASKS_FROZEN) {
3716 case CPU_UP_PREPARE:
3717 case CPU_UP_CANCELED:
3718 case CPU_DOWN_FAILED:
3719 case CPU_ONLINE:
3720 return workqueue_cpu_callback(nfb, action, hcpu);
3721 }
3722 return NOTIFY_OK;
3723}
3724
3725/*
3726 * Workqueues should be brought down after normal priority CPU notifiers.
3727 * This will be registered as low priority CPU notifier.
3728 */
3729static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3730 unsigned long action,
3731 void *hcpu)
3732{
3733 switch (action & ~CPU_TASKS_FROZEN) {
3734 case CPU_DOWN_PREPARE:
3735 case CPU_DYING:
3736 case CPU_POST_DEAD:
3737 return workqueue_cpu_callback(nfb, action, hcpu);
3738 }
3739 return NOTIFY_OK;
3740}
3741
Rusty Russell2d3854a2008-11-05 13:39:10 +11003742#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003743
Rusty Russell2d3854a2008-11-05 13:39:10 +11003744struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003745 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003746 long (*fn)(void *);
3747 void *arg;
3748 long ret;
3749};
3750
Tejun Heofc7da7e2012-09-18 12:48:43 -07003751static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003752{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003753 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3754
Rusty Russell2d3854a2008-11-05 13:39:10 +11003755 wfc->ret = wfc->fn(wfc->arg);
3756}
3757
3758/**
3759 * work_on_cpu - run a function in user context on a particular cpu
3760 * @cpu: the cpu to run on
3761 * @fn: the function to run
3762 * @arg: the function arg
3763 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003764 * This will return the value @fn returns.
3765 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003766 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003767 */
3768long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3769{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003770 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003771
Tejun Heofc7da7e2012-09-18 12:48:43 -07003772 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3773 schedule_work_on(cpu, &wfc.work);
3774 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003775 return wfc.ret;
3776}
3777EXPORT_SYMBOL_GPL(work_on_cpu);
3778#endif /* CONFIG_SMP */
3779
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003780#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303781
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003782/**
3783 * freeze_workqueues_begin - begin freezing workqueues
3784 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003785 * Start freezing workqueues. After this function returns, all freezable
3786 * workqueues will queue new works to their frozen_works list instead of
3787 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003788 *
3789 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003790 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003791 */
3792void freeze_workqueues_begin(void)
3793{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 unsigned int cpu;
3795
3796 spin_lock(&workqueue_lock);
3797
3798 BUG_ON(workqueue_freezing);
3799 workqueue_freezing = true;
3800
Tejun Heof3421792010-07-02 10:03:51 +02003801 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003802 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003803 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003804
3805 spin_lock_irq(&gcwq->lock);
3806
Tejun Heodb7bccf2010-06-29 10:07:12 +02003807 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3808 gcwq->flags |= GCWQ_FREEZING;
3809
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003810 list_for_each_entry(wq, &workqueues, list) {
3811 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3812
Tejun Heo58a69cb2011-02-16 09:25:31 +01003813 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003814 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003815 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003816
3817 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003818 }
3819
3820 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003822
3823/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003824 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003825 *
3826 * Check whether freezing is complete. This function must be called
3827 * between freeze_workqueues_begin() and thaw_workqueues().
3828 *
3829 * CONTEXT:
3830 * Grabs and releases workqueue_lock.
3831 *
3832 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003833 * %true if some freezable workqueues are still busy. %false if freezing
3834 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003835 */
3836bool freeze_workqueues_busy(void)
3837{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003838 unsigned int cpu;
3839 bool busy = false;
3840
3841 spin_lock(&workqueue_lock);
3842
3843 BUG_ON(!workqueue_freezing);
3844
Tejun Heof3421792010-07-02 10:03:51 +02003845 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003846 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003847 /*
3848 * nr_active is monotonically decreasing. It's safe
3849 * to peek without lock.
3850 */
3851 list_for_each_entry(wq, &workqueues, list) {
3852 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3853
Tejun Heo58a69cb2011-02-16 09:25:31 +01003854 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003855 continue;
3856
3857 BUG_ON(cwq->nr_active < 0);
3858 if (cwq->nr_active) {
3859 busy = true;
3860 goto out_unlock;
3861 }
3862 }
3863 }
3864out_unlock:
3865 spin_unlock(&workqueue_lock);
3866 return busy;
3867}
3868
3869/**
3870 * thaw_workqueues - thaw workqueues
3871 *
3872 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003873 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003874 *
3875 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003876 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003877 */
3878void thaw_workqueues(void)
3879{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003880 unsigned int cpu;
3881
3882 spin_lock(&workqueue_lock);
3883
3884 if (!workqueue_freezing)
3885 goto out_unlock;
3886
Tejun Heof3421792010-07-02 10:03:51 +02003887 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003888 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003889 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003890 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003891
3892 spin_lock_irq(&gcwq->lock);
3893
Tejun Heodb7bccf2010-06-29 10:07:12 +02003894 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3895 gcwq->flags &= ~GCWQ_FREEZING;
3896
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003897 list_for_each_entry(wq, &workqueues, list) {
3898 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3899
Tejun Heo58a69cb2011-02-16 09:25:31 +01003900 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003901 continue;
3902
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003903 /* restore max_active and repopulate worklist */
3904 cwq->max_active = wq->saved_max_active;
3905
3906 while (!list_empty(&cwq->delayed_works) &&
3907 cwq->nr_active < cwq->max_active)
3908 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003909 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003910
Tejun Heo9c6bae02012-07-13 22:16:44 -07003911 for_each_worker_pool(pool, gcwq)
3912 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003913
Tejun Heo8b03ae32010-06-29 10:07:12 +02003914 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003915 }
3916
3917 workqueue_freezing = false;
3918out_unlock:
3919 spin_unlock(&workqueue_lock);
3920}
3921#endif /* CONFIG_FREEZER */
3922
Suresh Siddha6ee05782010-07-30 14:57:37 -07003923static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Tejun Heoc34056a2010-06-29 10:07:11 +02003925 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003926 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003927
Tejun Heod3b42542012-07-17 12:39:26 -07003928 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3929 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003930
3931 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003932 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003933 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003934 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003935
3936 spin_lock_init(&gcwq->lock);
3937 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003938 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003939
Tejun Heoc8e55f32010-06-29 10:07:12 +02003940 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3941 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3942
Tejun Heo9c6bae02012-07-13 22:16:44 -07003943 for_each_worker_pool(pool, gcwq) {
3944 pool->gcwq = gcwq;
3945 INIT_LIST_HEAD(&pool->worklist);
3946 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003947
Tejun Heo9c6bae02012-07-13 22:16:44 -07003948 init_timer_deferrable(&pool->idle_timer);
3949 pool->idle_timer.function = idle_worker_timeout;
3950 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003951
Tejun Heo9c6bae02012-07-13 22:16:44 -07003952 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3953 (unsigned long)pool);
3954
3955 ida_init(&pool->worker_ida);
3956 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003957
3958 gcwq->trustee_state = TRUSTEE_DONE;
3959 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003960 }
3961
Tejun Heoe22bee72010-06-29 10:07:14 +02003962 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003963 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003964 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003965 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003966
Tejun Heo477a3c32010-08-31 10:54:35 +02003967 if (cpu != WORK_CPU_UNBOUND)
3968 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003969
3970 for_each_worker_pool(pool, gcwq) {
3971 struct worker *worker;
3972
3973 worker = create_worker(pool, true);
3974 BUG_ON(!worker);
3975 spin_lock_irq(&gcwq->lock);
3976 start_worker(worker);
3977 spin_unlock_irq(&gcwq->lock);
3978 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003979 }
3980
Tejun Heod320c032010-06-29 10:07:14 +02003981 system_wq = alloc_workqueue("events", 0, 0);
3982 system_long_wq = alloc_workqueue("events_long", 0, 0);
3983 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003984 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3985 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003986 system_freezable_wq = alloc_workqueue("events_freezable",
3987 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003988 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3989 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003990 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003991 !system_unbound_wq || !system_freezable_wq ||
3992 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003995early_initcall(init_workqueues);