blob: 66db46d67d3f3b217c57dd8530080f2b27f0a216 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020048 /* global_cwq flags */
Tejun Heode4637a2012-07-12 14:46:37 -070049 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
50 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
51
52 /* pool flags */
53 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
54 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020055
Tejun Heoc8e55f32010-06-29 10:07:12 +020056 /* worker flags */
57 WORKER_STARTED = 1 << 0, /* started */
58 WORKER_DIE = 1 << 1, /* die die die */
59 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020060 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020061 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020062 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020063 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020064 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020065
Tejun Heofb0e7be2010-06-29 10:07:15 +020066 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020067 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020068
69 /* gcwq->trustee_state */
70 TRUSTEE_START = 0, /* start */
71 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
72 TRUSTEE_BUTCHER = 2, /* butcher workers */
73 TRUSTEE_RELEASE = 3, /* release workers */
74 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020075
Tejun Heoc45d2ad2012-07-13 22:16:45 -070076 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heoe87d1492012-07-13 22:16:44 -070077
Tejun Heoc8e55f32010-06-29 10:07:12 +020078 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
79 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
80 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heoe22bee72010-06-29 10:07:14 +020082 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
83 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
84
Tejun Heo3233cdb2011-02-16 18:10:19 +010085 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
86 /* call for help after 10ms
87 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020088 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
89 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020090 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020091
92 /*
93 * Rescue workers are used only on emergencies and shared by
94 * all cpus. Give -20.
95 */
96 RESCUER_NICE_LEVEL = -20,
Tejun Heoc45d2ad2012-07-13 22:16:45 -070097 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020098};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200101 * Structure fields follow one of the following exclusion rules.
102 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200103 * I: Modifiable by initialization/destruction paths and read-only for
104 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200105 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200106 * P: Preemption protected. Disabling preemption is enough and should
107 * only be modified and accessed from the local cpu.
108 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200109 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * X: During normal operation, modification requires gcwq->lock and
112 * should be done only from local cpu. Either disabling preemption
113 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200114 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200115 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200116 * F: wq->flush_mutex protected.
117 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 * W: workqueue_lock protected.
119 */
120
Tejun Heo8b03ae32010-06-29 10:07:12 +0200121struct global_cwq;
Tejun Heo32089f12012-07-12 14:46:37 -0700122struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200123
Tejun Heoe22bee72010-06-29 10:07:14 +0200124/*
125 * The poor guys doing the actual heavy lifting. All on-duty workers
126 * are either serving the manager role, on idle list or on busy hash.
127 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200128struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200129 /* on idle list while idle, on busy hash table while busy */
130 union {
131 struct list_head entry; /* L: while idle */
132 struct hlist_node hentry; /* L: while busy */
133 };
134
Tejun Heoc34056a2010-06-29 10:07:11 +0200135 struct work_struct *current_work; /* L: work being processed */
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800136 work_func_t current_func; /* L: current_work's fn */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200137 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200138 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139 struct task_struct *task; /* I: worker task */
Tejun Heo32089f12012-07-12 14:46:37 -0700140 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200141 /* 64 bytes boundary on 64bit, 32 on 32bit */
142 unsigned long last_active; /* L: last active timestamp */
143 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200144 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200145 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200146};
147
Tejun Heo32089f12012-07-12 14:46:37 -0700148struct worker_pool {
149 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heode4637a2012-07-12 14:46:37 -0700150 unsigned int flags; /* X: flags */
Tejun Heo32089f12012-07-12 14:46:37 -0700151
152 struct list_head worklist; /* L: list of pending works */
153 int nr_workers; /* L: total number of workers */
154 int nr_idle; /* L: currently idle ones */
155
156 struct list_head idle_list; /* X: list of idle workers */
157 struct timer_list idle_timer; /* L: worker idle timeout */
158 struct timer_list mayday_timer; /* L: SOS timer for workers */
159
160 struct ida worker_ida; /* L: for worker IDs */
161 struct worker *first_idle; /* L: first idle worker */
162};
163
Tejun Heo4690c4a2010-06-29 10:07:10 +0200164/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200165 * Global per-cpu workqueue. There's one and only one for each cpu
166 * and all works are queued and processed here regardless of their
167 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200168 */
169struct global_cwq {
170 spinlock_t lock; /* the gcwq lock */
171 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200172 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200173
Tejun Heo32089f12012-07-12 14:46:37 -0700174 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200175 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
176 /* L: hash of busy workers */
177
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700178 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200179
180 struct task_struct *trustee; /* L: for gcwq shutdown */
181 unsigned int trustee_state; /* L: trustee state */
182 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200183} ____cacheline_aligned_in_smp;
184
185/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200186 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200187 * work_struct->data are used for flags and thus cwqs need to be
188 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190struct cpu_workqueue_struct {
Tejun Heo32089f12012-07-12 14:46:37 -0700191 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200192 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200193 int work_color; /* L: current color */
194 int flush_color; /* L: flushing color */
195 int nr_in_flight[WORK_NR_COLORS];
196 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200197 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200198 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200199 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200200};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200203 * Structure used to wait for workqueue flush.
204 */
205struct wq_flusher {
206 struct list_head list; /* F: list of flushers */
207 int flush_color; /* F: flush color waiting for */
208 struct completion done; /* flush completion */
209};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Tejun Heo73f53c42010-06-29 10:07:11 +0200211/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200212 * All cpumasks are assumed to be always set on UP and thus can't be
213 * used to determine whether there's something to be done.
214 */
215#ifdef CONFIG_SMP
216typedef cpumask_var_t mayday_mask_t;
217#define mayday_test_and_set_cpu(cpu, mask) \
218 cpumask_test_and_set_cpu((cpu), (mask))
219#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
220#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200221#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200222#define free_mayday_mask(mask) free_cpumask_var((mask))
223#else
224typedef unsigned long mayday_mask_t;
225#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
226#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
227#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
228#define alloc_mayday_mask(maskp, gfp) true
229#define free_mayday_mask(mask) do { } while (0)
230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*
233 * The externally visible workqueue abstraction is an array of
234 * per-CPU workqueues:
235 */
236struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200237 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200238 union {
239 struct cpu_workqueue_struct __percpu *pcpu;
240 struct cpu_workqueue_struct *single;
241 unsigned long v;
242 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200243 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200244
245 struct mutex flush_mutex; /* protects wq flushing */
246 int work_color; /* F: current work color */
247 int flush_color; /* F: current flush color */
248 atomic_t nr_cwqs_to_flush; /* flush in progress */
249 struct wq_flusher *first_flusher; /* F: first flusher */
250 struct list_head flusher_queue; /* F: flush waiters */
251 struct list_head flusher_overflow; /* F: flush overflow list */
252
Tejun Heof2e005a2010-07-20 15:59:09 +0200253 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200254 struct worker *rescuer; /* I: rescue worker */
255
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200256 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200257 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700258#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200259 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700260#endif
Tejun Heob196be82012-01-10 15:11:35 -0800261 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
Tejun Heod320c032010-06-29 10:07:14 +0200264struct workqueue_struct *system_wq __read_mostly;
265struct workqueue_struct *system_long_wq __read_mostly;
266struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200267struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100268struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100269struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200270EXPORT_SYMBOL_GPL(system_wq);
271EXPORT_SYMBOL_GPL(system_long_wq);
272EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200273EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100274EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100275EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200276
Tejun Heo97bd2342010-10-05 10:41:14 +0200277#define CREATE_TRACE_POINTS
278#include <trace/events/workqueue.h>
279
Tejun Heoe87d1492012-07-13 22:16:44 -0700280#define for_each_worker_pool(pool, gcwq) \
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700281 for ((pool) = &(gcwq)->pools[0]; \
282 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heoe87d1492012-07-13 22:16:44 -0700283
Tejun Heodb7bccf2010-06-29 10:07:12 +0200284#define for_each_busy_worker(worker, i, pos, gcwq) \
285 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
286 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
287
Tejun Heof3421792010-07-02 10:03:51 +0200288static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
289 unsigned int sw)
290{
291 if (cpu < nr_cpu_ids) {
292 if (sw & 1) {
293 cpu = cpumask_next(cpu, mask);
294 if (cpu < nr_cpu_ids)
295 return cpu;
296 }
297 if (sw & 2)
298 return WORK_CPU_UNBOUND;
299 }
300 return WORK_CPU_NONE;
301}
302
303static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
304 struct workqueue_struct *wq)
305{
306 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
307}
308
Tejun Heo09884952010-08-01 11:50:12 +0200309/*
310 * CPU iterators
311 *
312 * An extra gcwq is defined for an invalid cpu number
313 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
314 * specific CPU. The following iterators are similar to
315 * for_each_*_cpu() iterators but also considers the unbound gcwq.
316 *
317 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
318 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
319 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
320 * WORK_CPU_UNBOUND for unbound workqueues
321 */
Tejun Heof3421792010-07-02 10:03:51 +0200322#define for_each_gcwq_cpu(cpu) \
323 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
324 (cpu) < WORK_CPU_NONE; \
325 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
326
327#define for_each_online_gcwq_cpu(cpu) \
328 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
329 (cpu) < WORK_CPU_NONE; \
330 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
331
332#define for_each_cwq_cpu(cpu, wq) \
333 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
334 (cpu) < WORK_CPU_NONE; \
335 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
336
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900337#ifdef CONFIG_DEBUG_OBJECTS_WORK
338
339static struct debug_obj_descr work_debug_descr;
340
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100341static void *work_debug_hint(void *addr)
342{
343 return ((struct work_struct *) addr)->func;
344}
345
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900346/*
347 * fixup_init is called when:
348 * - an active object is initialized
349 */
350static int work_fixup_init(void *addr, enum debug_obj_state state)
351{
352 struct work_struct *work = addr;
353
354 switch (state) {
355 case ODEBUG_STATE_ACTIVE:
356 cancel_work_sync(work);
357 debug_object_init(work, &work_debug_descr);
358 return 1;
359 default:
360 return 0;
361 }
362}
363
364/*
365 * fixup_activate is called when:
366 * - an active object is activated
367 * - an unknown object is activated (might be a statically initialized object)
368 */
369static int work_fixup_activate(void *addr, enum debug_obj_state state)
370{
371 struct work_struct *work = addr;
372
373 switch (state) {
374
375 case ODEBUG_STATE_NOTAVAILABLE:
376 /*
377 * This is not really a fixup. The work struct was
378 * statically initialized. We just make sure that it
379 * is tracked in the object tracker.
380 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200381 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900382 debug_object_init(work, &work_debug_descr);
383 debug_object_activate(work, &work_debug_descr);
384 return 0;
385 }
386 WARN_ON_ONCE(1);
387 return 0;
388
389 case ODEBUG_STATE_ACTIVE:
390 WARN_ON(1);
391
392 default:
393 return 0;
394 }
395}
396
397/*
398 * fixup_free is called when:
399 * - an active object is freed
400 */
401static int work_fixup_free(void *addr, enum debug_obj_state state)
402{
403 struct work_struct *work = addr;
404
405 switch (state) {
406 case ODEBUG_STATE_ACTIVE:
407 cancel_work_sync(work);
408 debug_object_free(work, &work_debug_descr);
409 return 1;
410 default:
411 return 0;
412 }
413}
414
415static struct debug_obj_descr work_debug_descr = {
416 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100417 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900418 .fixup_init = work_fixup_init,
419 .fixup_activate = work_fixup_activate,
420 .fixup_free = work_fixup_free,
421};
422
423static inline void debug_work_activate(struct work_struct *work)
424{
425 debug_object_activate(work, &work_debug_descr);
426}
427
428static inline void debug_work_deactivate(struct work_struct *work)
429{
430 debug_object_deactivate(work, &work_debug_descr);
431}
432
433void __init_work(struct work_struct *work, int onstack)
434{
435 if (onstack)
436 debug_object_init_on_stack(work, &work_debug_descr);
437 else
438 debug_object_init(work, &work_debug_descr);
439}
440EXPORT_SYMBOL_GPL(__init_work);
441
442void destroy_work_on_stack(struct work_struct *work)
443{
444 debug_object_free(work, &work_debug_descr);
445}
446EXPORT_SYMBOL_GPL(destroy_work_on_stack);
447
448#else
449static inline void debug_work_activate(struct work_struct *work) { }
450static inline void debug_work_deactivate(struct work_struct *work) { }
451#endif
452
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100453/* Serializes the accesses to the list of workqueues. */
454static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200456static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Oleg Nesterov14441962007-05-23 13:57:57 -0700458/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200459 * The almighty global cpu workqueues. nr_running is the only field
460 * which is expected to be used frequently by other cpus via
461 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700462 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200463static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe87d1492012-07-13 22:16:44 -0700464static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800465
Tejun Heof3421792010-07-02 10:03:51 +0200466/*
467 * Global cpu workqueue and nr_running counter for unbound gcwq. The
468 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
469 * workers have WORKER_UNBOUND set.
470 */
471static struct global_cwq unbound_global_cwq;
Tejun Heoe87d1492012-07-13 22:16:44 -0700472static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
473 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
474};
Tejun Heof3421792010-07-02 10:03:51 +0200475
Tejun Heoc34056a2010-06-29 10:07:11 +0200476static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700478static int worker_pool_pri(struct worker_pool *pool)
479{
480 return pool - pool->gcwq->pools;
481}
482
Tejun Heo8b03ae32010-06-29 10:07:12 +0200483static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Tejun Heof3421792010-07-02 10:03:51 +0200485 if (cpu != WORK_CPU_UNBOUND)
486 return &per_cpu(global_cwq, cpu);
487 else
488 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Tejun Heo43370c62012-07-12 14:46:37 -0700491static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700492{
Tejun Heo43370c62012-07-12 14:46:37 -0700493 int cpu = pool->gcwq->cpu;
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700494 int idx = worker_pool_pri(pool);
Tejun Heo43370c62012-07-12 14:46:37 -0700495
Tejun Heof3421792010-07-02 10:03:51 +0200496 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoe87d1492012-07-13 22:16:44 -0700497 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200498 else
Tejun Heoe87d1492012-07-13 22:16:44 -0700499 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700500}
501
Tejun Heo4690c4a2010-06-29 10:07:10 +0200502static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
503 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700504{
Tejun Heof3421792010-07-02 10:03:51 +0200505 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800506 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200507 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200508 } else if (likely(cpu == WORK_CPU_UNBOUND))
509 return wq->cpu_wq.single;
510 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700511}
512
Tejun Heo73f53c42010-06-29 10:07:11 +0200513static unsigned int work_color_to_flags(int color)
514{
515 return color << WORK_STRUCT_COLOR_SHIFT;
516}
517
518static int get_work_color(struct work_struct *work)
519{
520 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
521 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
522}
523
524static int work_next_color(int color)
525{
526 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
David Howells4594bf12006-12-07 11:33:26 +0000529/*
Tejun Heoe1201532010-07-22 14:14:25 +0200530 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
531 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
532 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200533 *
534 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
535 * cwq, cpu or clear work->data. These functions should only be
536 * called while the work is owned - ie. while the PENDING bit is set.
537 *
538 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
539 * corresponding to a work. gcwq is available once the work has been
540 * queued anywhere after initialization. cwq is available only from
541 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000542 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543static inline void set_work_data(struct work_struct *work, unsigned long data,
544 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000545{
David Howells4594bf12006-12-07 11:33:26 +0000546 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200547 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000548}
David Howells365970a2006-11-22 14:54:49 +0000549
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550static void set_work_cwq(struct work_struct *work,
551 struct cpu_workqueue_struct *cwq,
552 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200553{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200555 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200556}
557
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000559{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
561}
562
563static void clear_work_data(struct work_struct *work)
564{
565 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
566}
567
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
569{
Tejun Heoe1201532010-07-22 14:14:25 +0200570 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571
Tejun Heoe1201532010-07-22 14:14:25 +0200572 if (data & WORK_STRUCT_CWQ)
573 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
574 else
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530575 {
576 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200577 return NULL;
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530578 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579}
580
581static struct global_cwq *get_work_gcwq(struct work_struct *work)
582{
Tejun Heoe1201532010-07-22 14:14:25 +0200583 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200584 unsigned int cpu;
585
Tejun Heoe1201532010-07-22 14:14:25 +0200586 if (data & WORK_STRUCT_CWQ)
587 return ((struct cpu_workqueue_struct *)
Tejun Heo32089f12012-07-12 14:46:37 -0700588 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589
590 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200591 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592 return NULL;
593
Tejun Heof3421792010-07-02 10:03:51 +0200594 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000596}
597
598/*
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700599 * Policy functions. These define the policies on how the global worker
600 * pools are managed. Unless noted otherwise, these functions assume that
601 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000602 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200603
Tejun Heo43370c62012-07-12 14:46:37 -0700604static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000605{
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700606 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000607}
608
Tejun Heoe22bee72010-06-29 10:07:14 +0200609/*
610 * Need to wake up a worker? Called from anything but currently
611 * running workers.
Tejun Heo546b99b2012-07-12 14:46:37 -0700612 *
613 * Note that, because unbound workers never contribute to nr_running, this
614 * function will always return %true for unbound gcwq as long as the
615 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200616 */
Tejun Heo43370c62012-07-12 14:46:37 -0700617static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000618{
Tejun Heo43370c62012-07-12 14:46:37 -0700619 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000620}
621
Tejun Heoe22bee72010-06-29 10:07:14 +0200622/* Can I start working? Called from busy but !running workers. */
Tejun Heo43370c62012-07-12 14:46:37 -0700623static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200624{
Tejun Heo43370c62012-07-12 14:46:37 -0700625 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200626}
627
628/* Do I need to keep working? Called from currently running workers. */
Tejun Heo43370c62012-07-12 14:46:37 -0700629static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200630{
Tejun Heo43370c62012-07-12 14:46:37 -0700631 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200632
Tejun Heoc45d2ad2012-07-13 22:16:45 -0700633 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200634}
635
636/* Do we need a new worker? Called from manager. */
Tejun Heo43370c62012-07-12 14:46:37 -0700637static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200638{
Tejun Heo43370c62012-07-12 14:46:37 -0700639 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200640}
641
642/* Do I need to be the manager? */
Tejun Heo43370c62012-07-12 14:46:37 -0700643static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200644{
Tejun Heo43370c62012-07-12 14:46:37 -0700645 return need_to_create_worker(pool) ||
Tejun Heode4637a2012-07-12 14:46:37 -0700646 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200647}
648
649/* Do we have too many workers and should some go away? */
Tejun Heo43370c62012-07-12 14:46:37 -0700650static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200651{
Tejun Heode4637a2012-07-12 14:46:37 -0700652 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo43370c62012-07-12 14:46:37 -0700653 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
654 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200655
656 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
657}
658
659/*
660 * Wake up functions.
661 */
662
Tejun Heo7e116292010-06-29 10:07:13 +0200663/* Return the first worker. Safe with preemption disabled */
Tejun Heo43370c62012-07-12 14:46:37 -0700664static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200665{
Tejun Heo43370c62012-07-12 14:46:37 -0700666 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200667 return NULL;
668
Tejun Heo43370c62012-07-12 14:46:37 -0700669 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200670}
671
672/**
673 * wake_up_worker - wake up an idle worker
Tejun Heo43370c62012-07-12 14:46:37 -0700674 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200675 *
Tejun Heo43370c62012-07-12 14:46:37 -0700676 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200677 *
678 * CONTEXT:
679 * spin_lock_irq(gcwq->lock).
680 */
Tejun Heo43370c62012-07-12 14:46:37 -0700681static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200682{
Tejun Heo43370c62012-07-12 14:46:37 -0700683 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200684
685 if (likely(worker))
686 wake_up_process(worker->task);
687}
688
Tejun Heo4690c4a2010-06-29 10:07:10 +0200689/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200690 * wq_worker_waking_up - a worker is waking up
691 * @task: task waking up
692 * @cpu: CPU @task is waking up to
693 *
694 * This function is called during try_to_wake_up() when a worker is
695 * being awoken.
696 *
697 * CONTEXT:
698 * spin_lock_irq(rq->lock)
699 */
700void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
701{
702 struct worker *worker = kthread_data(task);
703
Steven Rostedt2d646722010-12-03 23:12:33 -0500704 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo43370c62012-07-12 14:46:37 -0700705 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200706}
707
708/**
709 * wq_worker_sleeping - a worker is going to sleep
710 * @task: task going to sleep
711 * @cpu: CPU in question, must be the current CPU number
712 *
713 * This function is called during schedule() when a busy worker is
714 * going to sleep. Worker on the same cpu can be woken up by
715 * returning pointer to its task.
716 *
717 * CONTEXT:
718 * spin_lock_irq(rq->lock)
719 *
720 * RETURNS:
721 * Worker task on @cpu to wake up, %NULL if none.
722 */
723struct task_struct *wq_worker_sleeping(struct task_struct *task,
724 unsigned int cpu)
725{
726 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo32089f12012-07-12 14:46:37 -0700727 struct worker_pool *pool = worker->pool;
Tejun Heo43370c62012-07-12 14:46:37 -0700728 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200729
Steven Rostedt2d646722010-12-03 23:12:33 -0500730 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200731 return NULL;
732
733 /* this can only happen on the local cpu */
734 BUG_ON(cpu != raw_smp_processor_id());
735
736 /*
737 * The counterpart of the following dec_and_test, implied mb,
738 * worklist not empty test sequence is in insert_work().
739 * Please read comment there.
740 *
741 * NOT_RUNNING is clear. This means that trustee is not in
742 * charge and we're running on the local cpu w/ rq lock held
743 * and preemption disabled, which in turn means that none else
744 * could be manipulating idle_list, so dereferencing idle_list
745 * without gcwq lock is safe.
746 */
Tejun Heo32089f12012-07-12 14:46:37 -0700747 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo43370c62012-07-12 14:46:37 -0700748 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200749 return to_wakeup ? to_wakeup->task : NULL;
750}
751
752/**
753 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200754 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200755 * @flags: flags to set
756 * @wakeup: wakeup an idle worker if necessary
757 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200758 * Set @flags in @worker->flags and adjust nr_running accordingly. If
759 * nr_running becomes zero and @wakeup is %true, an idle worker is
760 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200761 *
Tejun Heocb444762010-07-02 10:03:50 +0200762 * CONTEXT:
763 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200764 */
765static inline void worker_set_flags(struct worker *worker, unsigned int flags,
766 bool wakeup)
767{
Tejun Heo32089f12012-07-12 14:46:37 -0700768 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200769
Tejun Heocb444762010-07-02 10:03:50 +0200770 WARN_ON_ONCE(worker->task != current);
771
Tejun Heoe22bee72010-06-29 10:07:14 +0200772 /*
773 * If transitioning into NOT_RUNNING, adjust nr_running and
774 * wake up an idle worker as necessary if requested by
775 * @wakeup.
776 */
777 if ((flags & WORKER_NOT_RUNNING) &&
778 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo43370c62012-07-12 14:46:37 -0700779 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200780
781 if (wakeup) {
782 if (atomic_dec_and_test(nr_running) &&
Tejun Heo32089f12012-07-12 14:46:37 -0700783 !list_empty(&pool->worklist))
Tejun Heo43370c62012-07-12 14:46:37 -0700784 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200785 } else
786 atomic_dec(nr_running);
787 }
788
Tejun Heod302f012010-06-29 10:07:13 +0200789 worker->flags |= flags;
790}
791
792/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200793 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200794 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200795 * @flags: flags to clear
796 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200797 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200798 *
Tejun Heocb444762010-07-02 10:03:50 +0200799 * CONTEXT:
800 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200801 */
802static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
803{
Tejun Heo43370c62012-07-12 14:46:37 -0700804 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200805 unsigned int oflags = worker->flags;
806
Tejun Heocb444762010-07-02 10:03:50 +0200807 WARN_ON_ONCE(worker->task != current);
808
Tejun Heod302f012010-06-29 10:07:13 +0200809 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200810
Tejun Heo42c025f2011-01-11 15:58:49 +0100811 /*
812 * If transitioning out of NOT_RUNNING, increment nr_running. Note
813 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
814 * of multiple flags, not a single flag.
815 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200816 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
817 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo43370c62012-07-12 14:46:37 -0700818 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200819}
820
821/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200822 * busy_worker_head - return the busy hash head for a work
823 * @gcwq: gcwq of interest
824 * @work: work to be hashed
825 *
826 * Return hash head of @gcwq for @work.
827 *
828 * CONTEXT:
829 * spin_lock_irq(gcwq->lock).
830 *
831 * RETURNS:
832 * Pointer to the hash head.
833 */
834static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
835 struct work_struct *work)
836{
837 const int base_shift = ilog2(sizeof(struct work_struct));
838 unsigned long v = (unsigned long)work;
839
840 /* simple shift and fold hash, do we need something better? */
841 v >>= base_shift;
842 v += v >> BUSY_WORKER_HASH_ORDER;
843 v &= BUSY_WORKER_HASH_MASK;
844
845 return &gcwq->busy_hash[v];
846}
847
848/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200849 * __find_worker_executing_work - find worker which is executing a work
850 * @gcwq: gcwq of interest
851 * @bwh: hash head as returned by busy_worker_head()
852 * @work: work to find worker for
853 *
854 * Find a worker which is executing @work on @gcwq. @bwh should be
855 * the hash head obtained by calling busy_worker_head() with the same
856 * work.
857 *
858 * CONTEXT:
859 * spin_lock_irq(gcwq->lock).
860 *
861 * RETURNS:
862 * Pointer to worker which is executing @work if found, NULL
863 * otherwise.
864 */
865static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
866 struct hlist_head *bwh,
867 struct work_struct *work)
868{
869 struct worker *worker;
870 struct hlist_node *tmp;
871
872 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800873 if (worker->current_work == work &&
874 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200875 return worker;
876 return NULL;
877}
878
879/**
880 * find_worker_executing_work - find worker which is executing a work
881 * @gcwq: gcwq of interest
882 * @work: work to find worker for
883 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800884 * Find a worker which is executing @work on @gcwq by searching
885 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
886 * to match, its current execution should match the address of @work and
887 * its work function. This is to avoid unwanted dependency between
888 * unrelated work executions through a work item being recycled while still
889 * being executed.
890 *
891 * This is a bit tricky. A work item may be freed once its execution
892 * starts and nothing prevents the freed area from being recycled for
893 * another work item. If the same work item address ends up being reused
894 * before the original execution finishes, workqueue will identify the
895 * recycled work item as currently executing and make it wait until the
896 * current execution finishes, introducing an unwanted dependency.
897 *
898 * This function checks the work item address, work function and workqueue
899 * to avoid false positives. Note that this isn't complete as one may
900 * construct a work function which can introduce dependency onto itself
901 * through a recycled work item. Well, if somebody wants to shoot oneself
902 * in the foot that badly, there's only so much we can do, and if such
903 * deadlock actually occurs, it should be easy to locate the culprit work
904 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200905 *
906 * CONTEXT:
907 * spin_lock_irq(gcwq->lock).
908 *
909 * RETURNS:
910 * Pointer to worker which is executing @work if found, NULL
911 * otherwise.
912 */
913static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
914 struct work_struct *work)
915{
916 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
917 work);
918}
919
920/**
Tejun Heo7e116292010-06-29 10:07:13 +0200921 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200922 * @cwq: cwq @work belongs to
923 * @work: work to insert
924 * @head: insertion point
925 * @extra_flags: extra WORK_STRUCT_* flags to set
926 *
Tejun Heo7e116292010-06-29 10:07:13 +0200927 * Insert @work which belongs to @cwq into @gcwq after @head.
928 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200929 *
930 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200931 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200932 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700933static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200934 struct work_struct *work, struct list_head *head,
935 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700936{
Tejun Heo43370c62012-07-12 14:46:37 -0700937 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100938
Tejun Heo4690c4a2010-06-29 10:07:10 +0200939 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200940 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200941
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700942 /*
943 * Ensure that we get the right work->data if we see the
944 * result of list_add() below, see try_to_grab_pending().
945 */
946 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200947
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700948 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200949
950 /*
951 * Ensure either worker_sched_deactivated() sees the above
952 * list_add_tail() or we see zero nr_running to avoid workers
953 * lying around lazily while there are works to be processed.
954 */
955 smp_mb();
956
Tejun Heo43370c62012-07-12 14:46:37 -0700957 if (__need_more_worker(pool))
958 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700959}
960
Tejun Heoc8efcc22010-12-20 19:32:04 +0100961/*
962 * Test whether @work is being queued from another work executing on the
963 * same workqueue. This is rather expensive and should only be used from
964 * cold paths.
965 */
966static bool is_chained_work(struct workqueue_struct *wq)
967{
968 unsigned long flags;
969 unsigned int cpu;
970
971 for_each_gcwq_cpu(cpu) {
972 struct global_cwq *gcwq = get_gcwq(cpu);
973 struct worker *worker;
974 struct hlist_node *pos;
975 int i;
976
977 spin_lock_irqsave(&gcwq->lock, flags);
978 for_each_busy_worker(worker, i, pos, gcwq) {
979 if (worker->task != current)
980 continue;
981 spin_unlock_irqrestore(&gcwq->lock, flags);
982 /*
983 * I'm @worker, no locking necessary. See if @work
984 * is headed to the same workqueue.
985 */
986 return worker->current_cwq->wq == wq;
987 }
988 spin_unlock_irqrestore(&gcwq->lock, flags);
989 }
990 return false;
991}
992
Tejun Heo4690c4a2010-06-29 10:07:10 +0200993static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct work_struct *work)
995{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200996 struct global_cwq *gcwq;
997 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200998 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200999 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned long flags;
1001
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001002 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001003
Tejun Heoc8efcc22010-12-20 19:32:04 +01001004 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001005 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001006 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001007 return;
1008
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001009 /* determine gcwq to use */
1010 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001011 struct global_cwq *last_gcwq;
1012
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001013 if (unlikely(cpu == WORK_CPU_UNBOUND))
1014 cpu = raw_smp_processor_id();
1015
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001016 /*
1017 * It's multi cpu. If @wq is non-reentrant and @work
1018 * was previously on a different cpu, it might still
1019 * be running there, in which case the work needs to
1020 * be queued on that cpu to guarantee non-reentrance.
1021 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001022 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001023 if (wq->flags & WQ_NON_REENTRANT &&
1024 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1025 struct worker *worker;
1026
1027 spin_lock_irqsave(&last_gcwq->lock, flags);
1028
1029 worker = find_worker_executing_work(last_gcwq, work);
1030
1031 if (worker && worker->current_cwq->wq == wq)
1032 gcwq = last_gcwq;
1033 else {
1034 /* meh... not running there, queue here */
1035 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1036 spin_lock_irqsave(&gcwq->lock, flags);
1037 }
1038 } else
1039 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001040 } else {
1041 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1042 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001043 }
1044
1045 /* gcwq determined, get cwq and queue */
1046 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001047 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001048
Tejun Heo4690c4a2010-06-29 10:07:10 +02001049 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001050
Tejun Heo73f53c42010-06-29 10:07:11 +02001051 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001052 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001053
1054 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001055 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056 cwq->nr_active++;
Tejun Heoc45d2ad2012-07-13 22:16:45 -07001057 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001058 } else {
1059 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001060 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001061 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001063 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001064
Tejun Heo8b03ae32010-06-29 10:07:12 +02001065 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001068/**
1069 * queue_work - queue work on a workqueue
1070 * @wq: workqueue to use
1071 * @work: work to queue
1072 *
Alan Stern057647f2006-10-28 10:38:58 -07001073 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001075 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1076 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001078int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001080 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001082 ret = queue_work_on(get_cpu(), wq, work);
1083 put_cpu();
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return ret;
1086}
Dave Jonesae90dd52006-06-30 01:40:45 -04001087EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Zhang Ruic1a220e2008-07-23 21:28:39 -07001089/**
1090 * queue_work_on - queue work on specific cpu
1091 * @cpu: CPU number to execute work on
1092 * @wq: workqueue to use
1093 * @work: work to queue
1094 *
1095 * Returns 0 if @work was already on a queue, non-zero otherwise.
1096 *
1097 * We queue the work to a specific CPU, the caller must ensure it
1098 * can't go away.
1099 */
1100int
1101queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1102{
1103 int ret = 0;
1104
Tejun Heo22df02b2010-06-29 10:07:10 +02001105 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001106 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001107 ret = 1;
1108 }
1109 return ret;
1110}
1111EXPORT_SYMBOL_GPL(queue_work_on);
1112
Li Zefan6d141c32008-02-08 04:21:09 -08001113static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
David Howells52bad642006-11-22 14:54:01 +00001115 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001116 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Srinivasarao Pb6e586c2013-09-18 14:33:45 +05301118 if (cwq != NULL)
1119 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001122/**
1123 * queue_delayed_work - queue work on a workqueue after delay
1124 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001125 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001126 * @delay: number of jiffies to wait before queueing
1127 *
Alan Stern057647f2006-10-28 10:38:58 -07001128 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001129 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001130int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001131 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
David Howells52bad642006-11-22 14:54:01 +00001133 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001134 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001136 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
Dave Jonesae90dd52006-06-30 01:40:45 -04001138EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001140/**
1141 * queue_delayed_work_on - queue work on specific CPU after delay
1142 * @cpu: CPU number to execute work on
1143 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001144 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001145 * @delay: number of jiffies to wait before queueing
1146 *
Alan Stern057647f2006-10-28 10:38:58 -07001147 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001148 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001149int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001150 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001151{
1152 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001153 struct timer_list *timer = &dwork->timer;
1154 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001155
Tejun Heo22df02b2010-06-29 10:07:10 +02001156 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001157 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001158
Tejun Heo4afca922012-12-04 07:40:39 -08001159 WARN_ON_ONCE(timer_pending(timer));
1160 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001161
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001162 timer_stats_timer_set_start_info(&dwork->timer);
1163
Tejun Heo7a22ad72010-06-29 10:07:13 +02001164 /*
1165 * This stores cwq for the moment, for the timer_fn.
1166 * Note that the work's gcwq is preserved to allow
1167 * reentrance detection for delayed works.
1168 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001169 if (!(wq->flags & WQ_UNBOUND)) {
1170 struct global_cwq *gcwq = get_work_gcwq(work);
1171
1172 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1173 lcpu = gcwq->cpu;
1174 else
1175 lcpu = raw_smp_processor_id();
1176 } else
1177 lcpu = WORK_CPU_UNBOUND;
1178
Tejun Heo7a22ad72010-06-29 10:07:13 +02001179 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001180
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001181 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001182 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001183 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001184
1185 if (unlikely(cpu >= 0))
1186 add_timer_on(timer, cpu);
1187 else
1188 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001189 ret = 1;
1190 }
1191 return ret;
1192}
Dave Jonesae90dd52006-06-30 01:40:45 -04001193EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Tejun Heoc8e55f32010-06-29 10:07:12 +02001195/**
1196 * worker_enter_idle - enter idle state
1197 * @worker: worker which is entering idle state
1198 *
1199 * @worker is entering idle state. Update stats and idle timer if
1200 * necessary.
1201 *
1202 * LOCKING:
1203 * spin_lock_irq(gcwq->lock).
1204 */
1205static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Tejun Heo32089f12012-07-12 14:46:37 -07001207 struct worker_pool *pool = worker->pool;
1208 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Tejun Heoc8e55f32010-06-29 10:07:12 +02001210 BUG_ON(worker->flags & WORKER_IDLE);
1211 BUG_ON(!list_empty(&worker->entry) &&
1212 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Tejun Heocb444762010-07-02 10:03:50 +02001214 /* can't use worker_set_flags(), also called from start_worker() */
1215 worker->flags |= WORKER_IDLE;
Tejun Heo32089f12012-07-12 14:46:37 -07001216 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001217 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001218
Tejun Heoc8e55f32010-06-29 10:07:12 +02001219 /* idle_list is LIFO */
Tejun Heo32089f12012-07-12 14:46:37 -07001220 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001221
Tejun Heoe22bee72010-06-29 10:07:14 +02001222 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo43370c62012-07-12 14:46:37 -07001223 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo32089f12012-07-12 14:46:37 -07001224 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001225 jiffies + IDLE_WORKER_TIMEOUT);
1226 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001227 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001228
Tejun Heo24312d32012-05-14 15:04:50 -07001229 /*
1230 * Sanity check nr_running. Because trustee releases gcwq->lock
1231 * between setting %WORKER_ROGUE and zapping nr_running, the
1232 * warning may trigger spuriously. Check iff trustee is idle.
1233 */
1234 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo32089f12012-07-12 14:46:37 -07001235 pool->nr_workers == pool->nr_idle &&
Tejun Heo43370c62012-07-12 14:46:37 -07001236 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Tejun Heoc8e55f32010-06-29 10:07:12 +02001239/**
1240 * worker_leave_idle - leave idle state
1241 * @worker: worker which is leaving idle state
1242 *
1243 * @worker is leaving idle state. Update stats.
1244 *
1245 * LOCKING:
1246 * spin_lock_irq(gcwq->lock).
1247 */
1248static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Tejun Heo32089f12012-07-12 14:46:37 -07001250 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Tejun Heoc8e55f32010-06-29 10:07:12 +02001252 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001253 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo32089f12012-07-12 14:46:37 -07001254 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001255 list_del_init(&worker->entry);
1256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Tejun Heoe22bee72010-06-29 10:07:14 +02001258/**
1259 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1260 * @worker: self
1261 *
1262 * Works which are scheduled while the cpu is online must at least be
1263 * scheduled to a worker which is bound to the cpu so that if they are
1264 * flushed from cpu callbacks while cpu is going down, they are
1265 * guaranteed to execute on the cpu.
1266 *
1267 * This function is to be used by rogue workers and rescuers to bind
1268 * themselves to the target cpu and may race with cpu going down or
1269 * coming online. kthread_bind() can't be used because it may put the
1270 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1271 * verbatim as it's best effort and blocking and gcwq may be
1272 * [dis]associated in the meantime.
1273 *
1274 * This function tries set_cpus_allowed() and locks gcwq and verifies
1275 * the binding against GCWQ_DISASSOCIATED which is set during
1276 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1277 * idle state or fetches works without dropping lock, it can guarantee
1278 * the scheduling requirement described in the first paragraph.
1279 *
1280 * CONTEXT:
1281 * Might sleep. Called without any lock but returns with gcwq->lock
1282 * held.
1283 *
1284 * RETURNS:
1285 * %true if the associated gcwq is online (@worker is successfully
1286 * bound), %false if offline.
1287 */
1288static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001289__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001290{
Tejun Heo32089f12012-07-12 14:46:37 -07001291 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001292 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Tejun Heoe22bee72010-06-29 10:07:14 +02001294 while (true) {
1295 /*
1296 * The following call may fail, succeed or succeed
1297 * without actually migrating the task to the cpu if
1298 * it races with cpu hotunplug operation. Verify
1299 * against GCWQ_DISASSOCIATED.
1300 */
Tejun Heof3421792010-07-02 10:03:51 +02001301 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1302 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001303
Tejun Heoe22bee72010-06-29 10:07:14 +02001304 spin_lock_irq(&gcwq->lock);
1305 if (gcwq->flags & GCWQ_DISASSOCIATED)
1306 return false;
1307 if (task_cpu(task) == gcwq->cpu &&
1308 cpumask_equal(&current->cpus_allowed,
1309 get_cpu_mask(gcwq->cpu)))
1310 return true;
1311 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001312
Tejun Heo5035b202011-04-29 18:08:37 +02001313 /*
1314 * We've raced with CPU hot[un]plug. Give it a breather
1315 * and retry migration. cond_resched() is required here;
1316 * otherwise, we might deadlock against cpu_stop trying to
1317 * bring down the CPU on non-preemptive kernel.
1318 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001319 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001320 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001321 }
1322}
1323
1324/*
1325 * Function for worker->rebind_work used to rebind rogue busy workers
1326 * to the associated cpu which is coming back online. This is
1327 * scheduled by cpu up but can race with other cpu hotplug operations
1328 * and may be executed twice without intervening cpu down.
1329 */
1330static void worker_rebind_fn(struct work_struct *work)
1331{
1332 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo32089f12012-07-12 14:46:37 -07001333 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001334
1335 if (worker_maybe_bind_and_lock(worker))
1336 worker_clr_flags(worker, WORKER_REBIND);
1337
1338 spin_unlock_irq(&gcwq->lock);
1339}
1340
Tejun Heoc34056a2010-06-29 10:07:11 +02001341static struct worker *alloc_worker(void)
1342{
1343 struct worker *worker;
1344
1345 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001346 if (worker) {
1347 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001348 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001349 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1350 /* on creation a worker is in !idle && prep state */
1351 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001352 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001353 return worker;
1354}
1355
1356/**
1357 * create_worker - create a new workqueue worker
Tejun Heo43370c62012-07-12 14:46:37 -07001358 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001359 * @bind: whether to set affinity to @cpu or not
1360 *
Tejun Heo43370c62012-07-12 14:46:37 -07001361 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001362 * can be started by calling start_worker() or destroyed using
1363 * destroy_worker().
1364 *
1365 * CONTEXT:
1366 * Might sleep. Does GFP_KERNEL allocations.
1367 *
1368 * RETURNS:
1369 * Pointer to the newly created worker.
1370 */
Tejun Heo43370c62012-07-12 14:46:37 -07001371static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001372{
Tejun Heo43370c62012-07-12 14:46:37 -07001373 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001374 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc45d2ad2012-07-13 22:16:45 -07001375 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001376 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001377 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001378
Tejun Heo8b03ae32010-06-29 10:07:12 +02001379 spin_lock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001380 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001381 spin_unlock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001382 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001383 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001384 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001385 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001386 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001387
1388 worker = alloc_worker();
1389 if (!worker)
1390 goto fail;
1391
Tejun Heo32089f12012-07-12 14:46:37 -07001392 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001393 worker->id = id;
1394
Tejun Heof3421792010-07-02 10:03:51 +02001395 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001396 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoc45d2ad2012-07-13 22:16:45 -07001397 worker, cpu_to_node(gcwq->cpu),
1398 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001399 else
1400 worker->task = kthread_create(worker_thread, worker,
Tejun Heoc45d2ad2012-07-13 22:16:45 -07001401 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001402 if (IS_ERR(worker->task))
1403 goto fail;
1404
Tejun Heoc45d2ad2012-07-13 22:16:45 -07001405 if (worker_pool_pri(pool))
1406 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1407
Tejun Heodb7bccf2010-06-29 10:07:12 +02001408 /*
1409 * A rogue worker will become a regular one if CPU comes
1410 * online later on. Make sure every worker has
1411 * PF_THREAD_BOUND set.
1412 */
Tejun Heof3421792010-07-02 10:03:51 +02001413 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001414 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001415 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001416 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001417 if (on_unbound_cpu)
1418 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001420
Tejun Heoc34056a2010-06-29 10:07:11 +02001421 return worker;
1422fail:
1423 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001424 spin_lock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001425 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001426 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001427 }
1428 kfree(worker);
1429 return NULL;
1430}
1431
1432/**
1433 * start_worker - start a newly created worker
1434 * @worker: worker to start
1435 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001436 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001437 *
1438 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001439 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001440 */
1441static void start_worker(struct worker *worker)
1442{
Tejun Heocb444762010-07-02 10:03:50 +02001443 worker->flags |= WORKER_STARTED;
Tejun Heo32089f12012-07-12 14:46:37 -07001444 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001445 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001446 wake_up_process(worker->task);
1447}
1448
1449/**
1450 * destroy_worker - destroy a workqueue worker
1451 * @worker: worker to be destroyed
1452 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001453 * Destroy @worker and adjust @gcwq stats accordingly.
1454 *
1455 * CONTEXT:
1456 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001457 */
1458static void destroy_worker(struct worker *worker)
1459{
Tejun Heo32089f12012-07-12 14:46:37 -07001460 struct worker_pool *pool = worker->pool;
1461 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001462 int id = worker->id;
1463
1464 /* sanity check frenzy */
1465 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001466 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001467
Tejun Heoc8e55f32010-06-29 10:07:12 +02001468 if (worker->flags & WORKER_STARTED)
Tejun Heo32089f12012-07-12 14:46:37 -07001469 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001470 if (worker->flags & WORKER_IDLE)
Tejun Heo32089f12012-07-12 14:46:37 -07001471 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001472
Lai Jiangshan23f09132014-02-15 22:02:28 +08001473 /*
1474 * Once WORKER_DIE is set, the kworker may destroy itself at any
1475 * point. Pin to ensure the task stays until we're done with it.
1476 */
1477 get_task_struct(worker->task);
1478
Tejun Heoc8e55f32010-06-29 10:07:12 +02001479 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001480 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001481
1482 spin_unlock_irq(&gcwq->lock);
1483
Tejun Heoc34056a2010-06-29 10:07:11 +02001484 kthread_stop(worker->task);
Lai Jiangshan23f09132014-02-15 22:02:28 +08001485 put_task_struct(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001486 kfree(worker);
1487
Tejun Heo8b03ae32010-06-29 10:07:12 +02001488 spin_lock_irq(&gcwq->lock);
Tejun Heo32089f12012-07-12 14:46:37 -07001489 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001490}
1491
Tejun Heo43370c62012-07-12 14:46:37 -07001492static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001493{
Tejun Heo43370c62012-07-12 14:46:37 -07001494 struct worker_pool *pool = (void *)__pool;
1495 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001496
1497 spin_lock_irq(&gcwq->lock);
1498
Tejun Heo43370c62012-07-12 14:46:37 -07001499 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001500 struct worker *worker;
1501 unsigned long expires;
1502
1503 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo43370c62012-07-12 14:46:37 -07001504 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001505 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1506
1507 if (time_before(jiffies, expires))
Tejun Heo43370c62012-07-12 14:46:37 -07001508 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001509 else {
1510 /* it's been idle for too long, wake up manager */
Tejun Heode4637a2012-07-12 14:46:37 -07001511 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo43370c62012-07-12 14:46:37 -07001512 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001513 }
1514 }
1515
1516 spin_unlock_irq(&gcwq->lock);
1517}
1518
1519static bool send_mayday(struct work_struct *work)
1520{
1521 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1522 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001523 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001524
1525 if (!(wq->flags & WQ_RESCUER))
1526 return false;
1527
1528 /* mayday mayday mayday */
Tejun Heo32089f12012-07-12 14:46:37 -07001529 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001530 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1531 if (cpu == WORK_CPU_UNBOUND)
1532 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001533 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001534 wake_up_process(wq->rescuer->task);
1535 return true;
1536}
1537
Tejun Heo43370c62012-07-12 14:46:37 -07001538static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001539{
Tejun Heo43370c62012-07-12 14:46:37 -07001540 struct worker_pool *pool = (void *)__pool;
1541 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001542 struct work_struct *work;
1543
1544 spin_lock_irq(&gcwq->lock);
1545
Tejun Heo43370c62012-07-12 14:46:37 -07001546 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001547 /*
1548 * We've been trying to create a new worker but
1549 * haven't been successful. We might be hitting an
1550 * allocation deadlock. Send distress signals to
1551 * rescuers.
1552 */
Tejun Heo43370c62012-07-12 14:46:37 -07001553 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 send_mayday(work);
1555 }
1556
1557 spin_unlock_irq(&gcwq->lock);
1558
Tejun Heo43370c62012-07-12 14:46:37 -07001559 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001560}
1561
1562/**
1563 * maybe_create_worker - create a new worker if necessary
Tejun Heo43370c62012-07-12 14:46:37 -07001564 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 *
Tejun Heo43370c62012-07-12 14:46:37 -07001566 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001567 * have at least one idle worker on return from this function. If
1568 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo43370c62012-07-12 14:46:37 -07001569 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001570 * possible allocation deadlock.
1571 *
1572 * On return, need_to_create_worker() is guaranteed to be false and
1573 * may_start_working() true.
1574 *
1575 * LOCKING:
1576 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1577 * multiple times. Does GFP_KERNEL allocations. Called only from
1578 * manager.
1579 *
1580 * RETURNS:
1581 * false if no action was taken and gcwq->lock stayed locked, true
1582 * otherwise.
1583 */
Tejun Heo43370c62012-07-12 14:46:37 -07001584static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001585__releases(&gcwq->lock)
1586__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001587{
Tejun Heo43370c62012-07-12 14:46:37 -07001588 struct global_cwq *gcwq = pool->gcwq;
1589
1590 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 return false;
1592restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001593 spin_unlock_irq(&gcwq->lock);
1594
Tejun Heoe22bee72010-06-29 10:07:14 +02001595 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo43370c62012-07-12 14:46:37 -07001596 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001597
1598 while (true) {
1599 struct worker *worker;
1600
Tejun Heo43370c62012-07-12 14:46:37 -07001601 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001602 if (worker) {
Tejun Heo43370c62012-07-12 14:46:37 -07001603 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 spin_lock_irq(&gcwq->lock);
1605 start_worker(worker);
Tejun Heo43370c62012-07-12 14:46:37 -07001606 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001607 return true;
1608 }
1609
Tejun Heo43370c62012-07-12 14:46:37 -07001610 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 break;
1612
Tejun Heoe22bee72010-06-29 10:07:14 +02001613 __set_current_state(TASK_INTERRUPTIBLE);
1614 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001615
Tejun Heo43370c62012-07-12 14:46:37 -07001616 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 break;
1618 }
1619
Tejun Heo43370c62012-07-12 14:46:37 -07001620 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 spin_lock_irq(&gcwq->lock);
Tejun Heo43370c62012-07-12 14:46:37 -07001622 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 goto restart;
1624 return true;
1625}
1626
1627/**
1628 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo43370c62012-07-12 14:46:37 -07001629 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001630 *
Tejun Heo43370c62012-07-12 14:46:37 -07001631 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001632 * IDLE_WORKER_TIMEOUT.
1633 *
1634 * LOCKING:
1635 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1636 * multiple times. Called only from manager.
1637 *
1638 * RETURNS:
1639 * false if no action was taken and gcwq->lock stayed locked, true
1640 * otherwise.
1641 */
Tejun Heo43370c62012-07-12 14:46:37 -07001642static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001643{
1644 bool ret = false;
1645
Tejun Heo43370c62012-07-12 14:46:37 -07001646 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001647 struct worker *worker;
1648 unsigned long expires;
1649
Tejun Heo43370c62012-07-12 14:46:37 -07001650 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001651 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1652
1653 if (time_before(jiffies, expires)) {
Tejun Heo43370c62012-07-12 14:46:37 -07001654 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001655 break;
1656 }
1657
1658 destroy_worker(worker);
1659 ret = true;
1660 }
1661
1662 return ret;
1663}
1664
1665/**
1666 * manage_workers - manage worker pool
1667 * @worker: self
1668 *
1669 * Assume the manager role and manage gcwq worker pool @worker belongs
1670 * to. At any given time, there can be only zero or one manager per
1671 * gcwq. The exclusion is handled automatically by this function.
1672 *
1673 * The caller can safely start processing works on false return. On
1674 * true return, it's guaranteed that need_to_create_worker() is false
1675 * and may_start_working() is true.
1676 *
1677 * CONTEXT:
1678 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1679 * multiple times. Does GFP_KERNEL allocations.
1680 *
1681 * RETURNS:
1682 * false if no action was taken and gcwq->lock stayed locked, true if
1683 * some action was taken.
1684 */
1685static bool manage_workers(struct worker *worker)
1686{
Tejun Heo43370c62012-07-12 14:46:37 -07001687 struct worker_pool *pool = worker->pool;
1688 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001689 bool ret = false;
1690
Tejun Heode4637a2012-07-12 14:46:37 -07001691 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001692 return ret;
1693
Tejun Heode4637a2012-07-12 14:46:37 -07001694 pool->flags &= ~POOL_MANAGE_WORKERS;
1695 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001696
1697 /*
1698 * Destroy and then create so that may_start_working() is true
1699 * on return.
1700 */
Tejun Heo43370c62012-07-12 14:46:37 -07001701 ret |= maybe_destroy_workers(pool);
1702 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001703
Tejun Heode4637a2012-07-12 14:46:37 -07001704 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001705
1706 /*
1707 * The trustee might be waiting to take over the manager
1708 * position, tell it we're done.
1709 */
1710 if (unlikely(gcwq->trustee))
1711 wake_up_all(&gcwq->trustee_wait);
1712
1713 return ret;
1714}
1715
Tejun Heoa62428c2010-06-29 10:07:10 +02001716/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001717 * move_linked_works - move linked works to a list
1718 * @work: start of series of works to be scheduled
1719 * @head: target list to append @work to
1720 * @nextp: out paramter for nested worklist walking
1721 *
1722 * Schedule linked works starting from @work to @head. Work series to
1723 * be scheduled starts at @work and includes any consecutive work with
1724 * WORK_STRUCT_LINKED set in its predecessor.
1725 *
1726 * If @nextp is not NULL, it's updated to point to the next work of
1727 * the last scheduled work. This allows move_linked_works() to be
1728 * nested inside outer list_for_each_entry_safe().
1729 *
1730 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001731 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001732 */
1733static void move_linked_works(struct work_struct *work, struct list_head *head,
1734 struct work_struct **nextp)
1735{
1736 struct work_struct *n;
1737
1738 /*
1739 * Linked worklist will always end before the end of the list,
1740 * use NULL for list head.
1741 */
1742 list_for_each_entry_safe_from(work, n, NULL, entry) {
1743 list_move_tail(&work->entry, head);
1744 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1745 break;
1746 }
1747
1748 /*
1749 * If we're already inside safe list traversal and have moved
1750 * multiple works to the scheduled queue, the next position
1751 * needs to be updated.
1752 */
1753 if (nextp)
1754 *nextp = n;
1755}
1756
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001757static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001758{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001759 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001760
Tejun Heocdadf002010-10-05 10:49:55 +02001761 trace_workqueue_activate_work(work);
Tejun Heoc45d2ad2012-07-13 22:16:45 -07001762 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001763 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001764 cwq->nr_active++;
1765}
1766
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001767static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1768{
1769 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1770 struct work_struct, entry);
1771
1772 cwq_activate_delayed_work(work);
1773}
1774
Tejun Heoaffee4b2010-06-29 10:07:12 +02001775/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001776 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1777 * @cwq: cwq of interest
1778 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001779 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001780 *
1781 * A work either has completed or is removed from pending queue,
1782 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1783 *
1784 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001785 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001786 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001787static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1788 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001789{
1790 /* ignore uncolored works */
1791 if (color == WORK_NO_COLOR)
1792 return;
1793
1794 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001795
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001796 if (!delayed) {
1797 cwq->nr_active--;
1798 if (!list_empty(&cwq->delayed_works)) {
1799 /* one down, submit a delayed one */
1800 if (cwq->nr_active < cwq->max_active)
1801 cwq_activate_first_delayed(cwq);
1802 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001803 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001804
1805 /* is flush in progress and are we at the flushing tip? */
1806 if (likely(cwq->flush_color != color))
1807 return;
1808
1809 /* are there still in-flight works? */
1810 if (cwq->nr_in_flight[color])
1811 return;
1812
1813 /* this cwq is done, clear flush_color */
1814 cwq->flush_color = -1;
1815
1816 /*
1817 * If this was the last cwq, wake up the first flusher. It
1818 * will handle the rest.
1819 */
1820 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1821 complete(&cwq->wq->first_flusher->done);
1822}
1823
1824/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001825 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001826 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001827 * @work: work to process
1828 *
1829 * Process @work. This function contains all the logics necessary to
1830 * process a single work including synchronization against and
1831 * interaction with other workers on the same cpu, queueing and
1832 * flushing. As long as context requirement is met, any worker can
1833 * call this function to process a work.
1834 *
1835 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001836 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001837 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001838static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001839__releases(&gcwq->lock)
1840__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001841{
Tejun Heo7e116292010-06-29 10:07:13 +02001842 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo32089f12012-07-12 14:46:37 -07001843 struct worker_pool *pool = worker->pool;
1844 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001845 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001846 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001847 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001848 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001849#ifdef CONFIG_LOCKDEP
1850 /*
1851 * It is permissible to free the struct work_struct from
1852 * inside the function that is called from it, this we need to
1853 * take into account for lockdep too. To avoid bogus "held
1854 * lock freed" warnings as well as problems when looking into
1855 * work->lockdep_map, make a copy and use that here.
1856 */
1857 struct lockdep_map lockdep_map = work->lockdep_map;
1858#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001859 /*
1860 * A single work shouldn't be executed concurrently by
1861 * multiple workers on a single cpu. Check whether anyone is
1862 * already processing the work. If so, defer the work to the
1863 * currently executing one.
1864 */
1865 collision = __find_worker_executing_work(gcwq, bwh, work);
1866 if (unlikely(collision)) {
1867 move_linked_works(work, &collision->scheduled, NULL);
1868 return;
1869 }
1870
Tejun Heoa62428c2010-06-29 10:07:10 +02001871 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001872 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001873 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001874 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001875 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001876 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001877 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001878
Tejun Heo7a22ad72010-06-29 10:07:13 +02001879 /* record the current cpu number in the work data and dequeue */
1880 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001881 list_del_init(&work->entry);
1882
Tejun Heo649027d2010-06-29 10:07:14 +02001883 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001884 * CPU intensive works don't participate in concurrency
1885 * management. They're the scheduler's responsibility.
1886 */
1887 if (unlikely(cpu_intensive))
1888 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1889
Tejun Heo546b99b2012-07-12 14:46:37 -07001890 /*
1891 * Unbound gcwq isn't concurrency managed and work items should be
1892 * executed ASAP. Wake up another worker if necessary.
1893 */
Tejun Heo43370c62012-07-12 14:46:37 -07001894 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1895 wake_up_worker(pool);
Tejun Heo546b99b2012-07-12 14:46:37 -07001896
Tejun Heo8b03ae32010-06-29 10:07:12 +02001897 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001898
Tejun Heo66307ae2012-08-03 10:30:45 -07001899 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001900 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001901
Tejun Heoe1594892011-01-09 23:32:15 +01001902 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001903 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001904 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001905 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001906 /*
1907 * While we must be careful to not use "work" after this, the trace
1908 * point will only record its address.
1909 */
1910 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001911 lock_map_release(&lockdep_map);
1912 lock_map_release(&cwq->wq->lockdep_map);
1913
1914 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001915 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1916 " last function: %pf\n",
1917 current->comm, preempt_count(), task_pid_nr(current),
1918 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001919 debug_show_held_locks(current);
1920 dump_stack();
1921 }
1922
Tejun Heo8b03ae32010-06-29 10:07:12 +02001923 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001924
Tejun Heofb0e7be2010-06-29 10:07:15 +02001925 /* clear cpu intensive status */
1926 if (unlikely(cpu_intensive))
1927 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1928
Tejun Heoa62428c2010-06-29 10:07:10 +02001929 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001930 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001931 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001932 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001933 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001934 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001935}
1936
Tejun Heoaffee4b2010-06-29 10:07:12 +02001937/**
1938 * process_scheduled_works - process scheduled works
1939 * @worker: self
1940 *
1941 * Process all scheduled works. Please note that the scheduled list
1942 * may change while processing a work, so this function repeatedly
1943 * fetches a work from the top and executes it.
1944 *
1945 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001946 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001947 * multiple times.
1948 */
1949static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001951 while (!list_empty(&worker->scheduled)) {
1952 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001954 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Tejun Heo4690c4a2010-06-29 10:07:10 +02001958/**
1959 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001960 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001961 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001962 * The gcwq worker thread function. There's a single dynamic pool of
1963 * these per each cpu. These workers process all works regardless of
1964 * their specific target workqueue. The only exception is works which
1965 * belong to workqueues with a rescuer which will be explained in
1966 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001967 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001968static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Tejun Heoc34056a2010-06-29 10:07:11 +02001970 struct worker *worker = __worker;
Tejun Heo32089f12012-07-12 14:46:37 -07001971 struct worker_pool *pool = worker->pool;
1972 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 /* tell the scheduler that this is a workqueue worker */
1975 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001976woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Tejun Heoc8e55f32010-06-29 10:07:12 +02001979 /* DIE can be set only while we're idle, checking here is enough */
1980 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001981 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
Tejun Heoc8e55f32010-06-29 10:07:12 +02001986 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001987recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001988 /* no more worker necessary? */
Tejun Heo43370c62012-07-12 14:46:37 -07001989 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 goto sleep;
1991
1992 /* do we need to manage? */
Tejun Heo43370c62012-07-12 14:46:37 -07001993 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 goto recheck;
1995
Tejun Heoc8e55f32010-06-29 10:07:12 +02001996 /*
1997 * ->scheduled list can only be filled while a worker is
1998 * preparing to process a work or actually processing it.
1999 * Make sure nobody diddled with it while I was sleeping.
2000 */
2001 BUG_ON(!list_empty(&worker->scheduled));
2002
Tejun Heoe22bee72010-06-29 10:07:14 +02002003 /*
2004 * When control reaches this point, we're guaranteed to have
2005 * at least one idle worker or that someone else has already
2006 * assumed the manager role.
2007 */
2008 worker_clr_flags(worker, WORKER_PREP);
2009
2010 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002011 struct work_struct *work =
Tejun Heo32089f12012-07-12 14:46:37 -07002012 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002013 struct work_struct, entry);
2014
2015 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2016 /* optimization path, not strictly necessary */
2017 process_one_work(worker, work);
2018 if (unlikely(!list_empty(&worker->scheduled)))
2019 process_scheduled_works(worker);
2020 } else {
2021 move_linked_works(work, &worker->scheduled, NULL);
2022 process_scheduled_works(worker);
2023 }
Tejun Heo43370c62012-07-12 14:46:37 -07002024 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002025
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002027sleep:
Tejun Heo43370c62012-07-12 14:46:37 -07002028 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002030
Tejun Heoc8e55f32010-06-29 10:07:12 +02002031 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 * gcwq->lock is held and there's no work to process and no
2033 * need to manage, sleep. Workers are woken up only while
2034 * holding gcwq->lock or from local cpu, so setting the
2035 * current state before releasing gcwq->lock is enough to
2036 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002037 */
2038 worker_enter_idle(worker);
2039 __set_current_state(TASK_INTERRUPTIBLE);
2040 spin_unlock_irq(&gcwq->lock);
2041 schedule();
2042 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Tejun Heoe22bee72010-06-29 10:07:14 +02002045/**
2046 * rescuer_thread - the rescuer thread function
2047 * @__wq: the associated workqueue
2048 *
2049 * Workqueue rescuer thread function. There's one rescuer for each
2050 * workqueue which has WQ_RESCUER set.
2051 *
2052 * Regular work processing on a gcwq may block trying to create a new
2053 * worker which uses GFP_KERNEL allocation which has slight chance of
2054 * developing into deadlock if some works currently on the same queue
2055 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2056 * the problem rescuer solves.
2057 *
2058 * When such condition is possible, the gcwq summons rescuers of all
2059 * workqueues which have works queued on the gcwq and let them process
2060 * those works so that forward progress can be guaranteed.
2061 *
2062 * This should happen rarely.
2063 */
2064static int rescuer_thread(void *__wq)
2065{
2066 struct workqueue_struct *wq = __wq;
2067 struct worker *rescuer = wq->rescuer;
2068 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002069 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002070 unsigned int cpu;
2071
2072 set_user_nice(current, RESCUER_NICE_LEVEL);
2073repeat:
2074 set_current_state(TASK_INTERRUPTIBLE);
2075
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002076 if (kthread_should_stop()) {
2077 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002078 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002079 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002080
Tejun Heof3421792010-07-02 10:03:51 +02002081 /*
2082 * See whether any cpu is asking for help. Unbounded
2083 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2084 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002085 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002086 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2087 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo32089f12012-07-12 14:46:37 -07002088 struct worker_pool *pool = cwq->pool;
2089 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002090 struct work_struct *work, *n;
2091
2092 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002093 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002094
2095 /* migrate to the target cpu if possible */
Tejun Heo32089f12012-07-12 14:46:37 -07002096 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002097 worker_maybe_bind_and_lock(rescuer);
2098
2099 /*
2100 * Slurp in all works issued via this workqueue and
2101 * process'em.
2102 */
2103 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo32089f12012-07-12 14:46:37 -07002104 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002105 if (get_work_cwq(work) == cwq)
2106 move_linked_works(work, scheduled, &n);
2107
2108 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002109
2110 /*
2111 * Leave this gcwq. If keep_working() is %true, notify a
2112 * regular worker; otherwise, we end up with 0 concurrency
2113 * and stalling the execution.
2114 */
Tejun Heo43370c62012-07-12 14:46:37 -07002115 if (keep_working(pool))
2116 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002117
Tejun Heoe22bee72010-06-29 10:07:14 +02002118 spin_unlock_irq(&gcwq->lock);
2119 }
2120
2121 schedule();
2122 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002125struct wq_barrier {
2126 struct work_struct work;
2127 struct completion done;
2128};
2129
2130static void wq_barrier_func(struct work_struct *work)
2131{
2132 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2133 complete(&barr->done);
2134}
2135
Tejun Heo4690c4a2010-06-29 10:07:10 +02002136/**
2137 * insert_wq_barrier - insert a barrier work
2138 * @cwq: cwq to insert barrier into
2139 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002140 * @target: target work to attach @barr to
2141 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002142 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002143 * @barr is linked to @target such that @barr is completed only after
2144 * @target finishes execution. Please note that the ordering
2145 * guarantee is observed only with respect to @target and on the local
2146 * cpu.
2147 *
2148 * Currently, a queued barrier can't be canceled. This is because
2149 * try_to_grab_pending() can't determine whether the work to be
2150 * grabbed is at the head of the queue and thus can't clear LINKED
2151 * flag of the previous work while there must be a valid next work
2152 * after a work with LINKED flag set.
2153 *
2154 * Note that when @worker is non-NULL, @target may be modified
2155 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002156 *
2157 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002158 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002159 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002160static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002161 struct wq_barrier *barr,
2162 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002163{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002164 struct list_head *head;
2165 unsigned int linked = 0;
2166
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002167 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002168 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002169 * as we know for sure that this will not trigger any of the
2170 * checks and call back into the fixup functions where we
2171 * might deadlock.
2172 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002173 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002174 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002175 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002176
Tejun Heoaffee4b2010-06-29 10:07:12 +02002177 /*
2178 * If @target is currently being executed, schedule the
2179 * barrier to the worker; otherwise, put it after @target.
2180 */
2181 if (worker)
2182 head = worker->scheduled.next;
2183 else {
2184 unsigned long *bits = work_data_bits(target);
2185
2186 head = target->entry.next;
2187 /* there can already be other linked works, inherit and set */
2188 linked = *bits & WORK_STRUCT_LINKED;
2189 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2190 }
2191
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002192 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002193 insert_work(cwq, &barr->work, head,
2194 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002195}
2196
Tejun Heo73f53c42010-06-29 10:07:11 +02002197/**
2198 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2199 * @wq: workqueue being flushed
2200 * @flush_color: new flush color, < 0 for no-op
2201 * @work_color: new work color, < 0 for no-op
2202 *
2203 * Prepare cwqs for workqueue flushing.
2204 *
2205 * If @flush_color is non-negative, flush_color on all cwqs should be
2206 * -1. If no cwq has in-flight commands at the specified color, all
2207 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2208 * has in flight commands, its cwq->flush_color is set to
2209 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2210 * wakeup logic is armed and %true is returned.
2211 *
2212 * The caller should have initialized @wq->first_flusher prior to
2213 * calling this function with non-negative @flush_color. If
2214 * @flush_color is negative, no flush color update is done and %false
2215 * is returned.
2216 *
2217 * If @work_color is non-negative, all cwqs should have the same
2218 * work_color which is previous to @work_color and all will be
2219 * advanced to @work_color.
2220 *
2221 * CONTEXT:
2222 * mutex_lock(wq->flush_mutex).
2223 *
2224 * RETURNS:
2225 * %true if @flush_color >= 0 and there's something to flush. %false
2226 * otherwise.
2227 */
2228static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2229 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Tejun Heo73f53c42010-06-29 10:07:11 +02002231 bool wait = false;
2232 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002233
Tejun Heo73f53c42010-06-29 10:07:11 +02002234 if (flush_color >= 0) {
2235 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2236 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002237 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002238
Tejun Heof3421792010-07-02 10:03:51 +02002239 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002240 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo32089f12012-07-12 14:46:37 -07002241 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002242
Tejun Heo8b03ae32010-06-29 10:07:12 +02002243 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002244
2245 if (flush_color >= 0) {
2246 BUG_ON(cwq->flush_color != -1);
2247
2248 if (cwq->nr_in_flight[flush_color]) {
2249 cwq->flush_color = flush_color;
2250 atomic_inc(&wq->nr_cwqs_to_flush);
2251 wait = true;
2252 }
2253 }
2254
2255 if (work_color >= 0) {
2256 BUG_ON(work_color != work_next_color(cwq->work_color));
2257 cwq->work_color = work_color;
2258 }
2259
Tejun Heo8b03ae32010-06-29 10:07:12 +02002260 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002261 }
2262
2263 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2264 complete(&wq->first_flusher->done);
2265
2266 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002269/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002271 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 *
2273 * Forces execution of the workqueue and blocks until its completion.
2274 * This is typically used in driver shutdown handlers.
2275 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002276 * We sleep until all works which were queued on entry have been handled,
2277 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002279void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Tejun Heo73f53c42010-06-29 10:07:11 +02002281 struct wq_flusher this_flusher = {
2282 .list = LIST_HEAD_INIT(this_flusher.list),
2283 .flush_color = -1,
2284 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2285 };
2286 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002287
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002288 lock_map_acquire(&wq->lockdep_map);
2289 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002290
2291 mutex_lock(&wq->flush_mutex);
2292
2293 /*
2294 * Start-to-wait phase
2295 */
2296 next_color = work_next_color(wq->work_color);
2297
2298 if (next_color != wq->flush_color) {
2299 /*
2300 * Color space is not full. The current work_color
2301 * becomes our flush_color and work_color is advanced
2302 * by one.
2303 */
2304 BUG_ON(!list_empty(&wq->flusher_overflow));
2305 this_flusher.flush_color = wq->work_color;
2306 wq->work_color = next_color;
2307
2308 if (!wq->first_flusher) {
2309 /* no flush in progress, become the first flusher */
2310 BUG_ON(wq->flush_color != this_flusher.flush_color);
2311
2312 wq->first_flusher = &this_flusher;
2313
2314 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2315 wq->work_color)) {
2316 /* nothing to flush, done */
2317 wq->flush_color = next_color;
2318 wq->first_flusher = NULL;
2319 goto out_unlock;
2320 }
2321 } else {
2322 /* wait in queue */
2323 BUG_ON(wq->flush_color == this_flusher.flush_color);
2324 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2325 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2326 }
2327 } else {
2328 /*
2329 * Oops, color space is full, wait on overflow queue.
2330 * The next flush completion will assign us
2331 * flush_color and transfer to flusher_queue.
2332 */
2333 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2334 }
2335
2336 mutex_unlock(&wq->flush_mutex);
2337
2338 wait_for_completion(&this_flusher.done);
2339
2340 /*
2341 * Wake-up-and-cascade phase
2342 *
2343 * First flushers are responsible for cascading flushes and
2344 * handling overflow. Non-first flushers can simply return.
2345 */
2346 if (wq->first_flusher != &this_flusher)
2347 return;
2348
2349 mutex_lock(&wq->flush_mutex);
2350
Tejun Heo4ce48b32010-07-02 10:03:51 +02002351 /* we might have raced, check again with mutex held */
2352 if (wq->first_flusher != &this_flusher)
2353 goto out_unlock;
2354
Tejun Heo73f53c42010-06-29 10:07:11 +02002355 wq->first_flusher = NULL;
2356
2357 BUG_ON(!list_empty(&this_flusher.list));
2358 BUG_ON(wq->flush_color != this_flusher.flush_color);
2359
2360 while (true) {
2361 struct wq_flusher *next, *tmp;
2362
2363 /* complete all the flushers sharing the current flush color */
2364 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2365 if (next->flush_color != wq->flush_color)
2366 break;
2367 list_del_init(&next->list);
2368 complete(&next->done);
2369 }
2370
2371 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2372 wq->flush_color != work_next_color(wq->work_color));
2373
2374 /* this flush_color is finished, advance by one */
2375 wq->flush_color = work_next_color(wq->flush_color);
2376
2377 /* one color has been freed, handle overflow queue */
2378 if (!list_empty(&wq->flusher_overflow)) {
2379 /*
2380 * Assign the same color to all overflowed
2381 * flushers, advance work_color and append to
2382 * flusher_queue. This is the start-to-wait
2383 * phase for these overflowed flushers.
2384 */
2385 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2386 tmp->flush_color = wq->work_color;
2387
2388 wq->work_color = work_next_color(wq->work_color);
2389
2390 list_splice_tail_init(&wq->flusher_overflow,
2391 &wq->flusher_queue);
2392 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2393 }
2394
2395 if (list_empty(&wq->flusher_queue)) {
2396 BUG_ON(wq->flush_color != wq->work_color);
2397 break;
2398 }
2399
2400 /*
2401 * Need to flush more colors. Make the next flusher
2402 * the new first flusher and arm cwqs.
2403 */
2404 BUG_ON(wq->flush_color == wq->work_color);
2405 BUG_ON(wq->flush_color != next->flush_color);
2406
2407 list_del_init(&next->list);
2408 wq->first_flusher = next;
2409
2410 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2411 break;
2412
2413 /*
2414 * Meh... this color is already done, clear first
2415 * flusher and repeat cascading.
2416 */
2417 wq->first_flusher = NULL;
2418 }
2419
2420out_unlock:
2421 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
Dave Jonesae90dd52006-06-30 01:40:45 -04002423EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002425/**
2426 * drain_workqueue - drain a workqueue
2427 * @wq: workqueue to drain
2428 *
2429 * Wait until the workqueue becomes empty. While draining is in progress,
2430 * only chain queueing is allowed. IOW, only currently pending or running
2431 * work items on @wq can queue further work items on it. @wq is flushed
2432 * repeatedly until it becomes empty. The number of flushing is detemined
2433 * by the depth of chaining and should be relatively short. Whine if it
2434 * takes too long.
2435 */
2436void drain_workqueue(struct workqueue_struct *wq)
2437{
2438 unsigned int flush_cnt = 0;
2439 unsigned int cpu;
2440
2441 /*
2442 * __queue_work() needs to test whether there are drainers, is much
2443 * hotter than drain_workqueue() and already looks at @wq->flags.
2444 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2445 */
2446 spin_lock(&workqueue_lock);
2447 if (!wq->nr_drainers++)
2448 wq->flags |= WQ_DRAINING;
2449 spin_unlock(&workqueue_lock);
2450reflush:
2451 flush_workqueue(wq);
2452
2453 for_each_cwq_cpu(cpu, wq) {
2454 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002455 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002456
Tejun Heo32089f12012-07-12 14:46:37 -07002457 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002458 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo32089f12012-07-12 14:46:37 -07002459 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002460
2461 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002462 continue;
2463
2464 if (++flush_cnt == 10 ||
2465 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2466 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2467 wq->name, flush_cnt);
2468 goto reflush;
2469 }
2470
2471 spin_lock(&workqueue_lock);
2472 if (!--wq->nr_drainers)
2473 wq->flags &= ~WQ_DRAINING;
2474 spin_unlock(&workqueue_lock);
2475}
2476EXPORT_SYMBOL_GPL(drain_workqueue);
2477
Tejun Heobaf59022010-09-16 10:42:16 +02002478static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2479 bool wait_executing)
2480{
2481 struct worker *worker = NULL;
2482 struct global_cwq *gcwq;
2483 struct cpu_workqueue_struct *cwq;
2484
2485 might_sleep();
2486 gcwq = get_work_gcwq(work);
2487 if (!gcwq)
2488 return false;
2489
2490 spin_lock_irq(&gcwq->lock);
2491 if (!list_empty(&work->entry)) {
2492 /*
2493 * See the comment near try_to_grab_pending()->smp_rmb().
2494 * If it was re-queued to a different gcwq under us, we
2495 * are not going to wait.
2496 */
2497 smp_rmb();
2498 cwq = get_work_cwq(work);
Tejun Heo32089f12012-07-12 14:46:37 -07002499 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002500 goto already_gone;
2501 } else if (wait_executing) {
2502 worker = find_worker_executing_work(gcwq, work);
2503 if (!worker)
2504 goto already_gone;
2505 cwq = worker->current_cwq;
2506 } else
2507 goto already_gone;
2508
2509 insert_wq_barrier(cwq, barr, work, worker);
2510 spin_unlock_irq(&gcwq->lock);
2511
Tejun Heoe1594892011-01-09 23:32:15 +01002512 /*
2513 * If @max_active is 1 or rescuer is in use, flushing another work
2514 * item on the same workqueue may lead to deadlock. Make sure the
2515 * flusher is not running on the same workqueue by verifying write
2516 * access.
2517 */
2518 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2519 lock_map_acquire(&cwq->wq->lockdep_map);
2520 else
2521 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002522 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002523
Tejun Heobaf59022010-09-16 10:42:16 +02002524 return true;
2525already_gone:
2526 spin_unlock_irq(&gcwq->lock);
2527 return false;
2528}
2529
Oleg Nesterovdb700892008-07-25 01:47:49 -07002530/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002531 * flush_work - wait for a work to finish executing the last queueing instance
2532 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002533 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002534 * Wait until @work has finished execution. This function considers
2535 * only the last queueing instance of @work. If @work has been
2536 * enqueued across different CPUs on a non-reentrant workqueue or on
2537 * multiple workqueues, @work might still be executing on return on
2538 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002539 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002540 * If @work was queued only on a non-reentrant, ordered or unbound
2541 * workqueue, @work is guaranteed to be idle on return if it hasn't
2542 * been requeued since flush started.
2543 *
2544 * RETURNS:
2545 * %true if flush_work() waited for the work to finish execution,
2546 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002547 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002548bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002549{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002550 struct wq_barrier barr;
2551
Tejun Heobaf59022010-09-16 10:42:16 +02002552 if (start_flush_work(work, &barr, true)) {
2553 wait_for_completion(&barr.done);
2554 destroy_work_on_stack(&barr.work);
2555 return true;
2556 } else
2557 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002558}
2559EXPORT_SYMBOL_GPL(flush_work);
2560
Tejun Heo401a8d02010-09-16 10:36:00 +02002561static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2562{
2563 struct wq_barrier barr;
2564 struct worker *worker;
2565
2566 spin_lock_irq(&gcwq->lock);
2567
2568 worker = find_worker_executing_work(gcwq, work);
2569 if (unlikely(worker))
2570 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2571
2572 spin_unlock_irq(&gcwq->lock);
2573
2574 if (unlikely(worker)) {
2575 wait_for_completion(&barr.done);
2576 destroy_work_on_stack(&barr.work);
2577 return true;
2578 } else
2579 return false;
2580}
2581
2582static bool wait_on_work(struct work_struct *work)
2583{
2584 bool ret = false;
2585 int cpu;
2586
2587 might_sleep();
2588
2589 lock_map_acquire(&work->lockdep_map);
2590 lock_map_release(&work->lockdep_map);
2591
2592 for_each_gcwq_cpu(cpu)
2593 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2594 return ret;
2595}
2596
Tejun Heo09383492010-09-16 10:48:29 +02002597/**
2598 * flush_work_sync - wait until a work has finished execution
2599 * @work: the work to flush
2600 *
2601 * Wait until @work has finished execution. On return, it's
2602 * guaranteed that all queueing instances of @work which happened
2603 * before this function is called are finished. In other words, if
2604 * @work hasn't been requeued since this function was called, @work is
2605 * guaranteed to be idle on return.
2606 *
2607 * RETURNS:
2608 * %true if flush_work_sync() waited for the work to finish execution,
2609 * %false if it was already idle.
2610 */
2611bool flush_work_sync(struct work_struct *work)
2612{
2613 struct wq_barrier barr;
2614 bool pending, waited;
2615
2616 /* we'll wait for executions separately, queue barr only if pending */
2617 pending = start_flush_work(work, &barr, false);
2618
2619 /* wait for executions to finish */
2620 waited = wait_on_work(work);
2621
2622 /* wait for the pending one */
2623 if (pending) {
2624 wait_for_completion(&barr.done);
2625 destroy_work_on_stack(&barr.work);
2626 }
2627
2628 return pending || waited;
2629}
2630EXPORT_SYMBOL_GPL(flush_work_sync);
2631
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002632/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002633 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002634 * so this work can't be re-armed in any way.
2635 */
2636static int try_to_grab_pending(struct work_struct *work)
2637{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002638 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002639 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002640
Tejun Heo22df02b2010-06-29 10:07:10 +02002641 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002642 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002643
2644 /*
2645 * The queueing is in progress, or it is already queued. Try to
2646 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2647 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002648 gcwq = get_work_gcwq(work);
2649 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002650 return ret;
2651
Tejun Heo8b03ae32010-06-29 10:07:12 +02002652 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002653 if (!list_empty(&work->entry)) {
2654 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002655 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002656 * In that case we must see the new value after rmb(), see
2657 * insert_work()->wmb().
2658 */
2659 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002660 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002661 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002662
2663 /*
2664 * A delayed work item cannot be grabbed directly
2665 * because it might have linked NO_COLOR work items
2666 * which, if left on the delayed_list, will confuse
2667 * cwq->nr_active management later on and cause
2668 * stall. Make sure the work item is activated
2669 * before grabbing.
2670 */
2671 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2672 cwq_activate_delayed_work(work);
2673
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002674 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002675 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002676 get_work_color(work),
2677 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002678 ret = 1;
2679 }
2680 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002681 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002682
2683 return ret;
2684}
2685
Tejun Heo401a8d02010-09-16 10:36:00 +02002686static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002687 struct timer_list* timer)
2688{
2689 int ret;
2690
2691 do {
2692 ret = (timer && likely(del_timer(timer)));
2693 if (!ret)
2694 ret = try_to_grab_pending(work);
2695 wait_on_work(work);
2696 } while (unlikely(ret < 0));
2697
Tejun Heo7a22ad72010-06-29 10:07:13 +02002698 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002699 return ret;
2700}
2701
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002702/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002703 * cancel_work_sync - cancel a work and wait for it to finish
2704 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002705 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002706 * Cancel @work and wait for its execution to finish. This function
2707 * can be used even if the work re-queues itself or migrates to
2708 * another workqueue. On return from this function, @work is
2709 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002710 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002711 * cancel_work_sync(&delayed_work->work) must not be used for
2712 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002713 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002714 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002715 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002716 *
2717 * RETURNS:
2718 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002720bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002721{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002722 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002723}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002724EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002725
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002726/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2728 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002729 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002730 * Delayed timer is cancelled and the pending work is queued for
2731 * immediate execution. Like flush_work(), this function only
2732 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002733 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002734 * RETURNS:
2735 * %true if flush_work() waited for the work to finish execution,
2736 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002737 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002738bool flush_delayed_work(struct delayed_work *dwork)
2739{
2740 if (del_timer_sync(&dwork->timer))
2741 __queue_work(raw_smp_processor_id(),
2742 get_work_cwq(&dwork->work)->wq, &dwork->work);
2743 return flush_work(&dwork->work);
2744}
2745EXPORT_SYMBOL(flush_delayed_work);
2746
2747/**
Tejun Heo09383492010-09-16 10:48:29 +02002748 * flush_delayed_work_sync - wait for a dwork to finish
2749 * @dwork: the delayed work to flush
2750 *
2751 * Delayed timer is cancelled and the pending work is queued for
2752 * execution immediately. Other than timer handling, its behavior
2753 * is identical to flush_work_sync().
2754 *
2755 * RETURNS:
2756 * %true if flush_work_sync() waited for the work to finish execution,
2757 * %false if it was already idle.
2758 */
2759bool flush_delayed_work_sync(struct delayed_work *dwork)
2760{
2761 if (del_timer_sync(&dwork->timer))
2762 __queue_work(raw_smp_processor_id(),
2763 get_work_cwq(&dwork->work)->wq, &dwork->work);
2764 return flush_work_sync(&dwork->work);
2765}
2766EXPORT_SYMBOL(flush_delayed_work_sync);
2767
2768/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002769 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2770 * @dwork: the delayed work cancel
2771 *
2772 * This is cancel_work_sync() for delayed works.
2773 *
2774 * RETURNS:
2775 * %true if @dwork was pending, %false otherwise.
2776 */
2777bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002778{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002779 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002780}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002781EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002783/**
2784 * schedule_work - put work task in global workqueue
2785 * @work: job to be done
2786 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002787 * Returns zero if @work was already on the kernel-global workqueue and
2788 * non-zero otherwise.
2789 *
2790 * This puts a job in the kernel-global workqueue if it was not already
2791 * queued and leaves it in the same position on the kernel-global
2792 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002793 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002794int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Tejun Heod320c032010-06-29 10:07:14 +02002796 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
Dave Jonesae90dd52006-06-30 01:40:45 -04002798EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Zhang Ruic1a220e2008-07-23 21:28:39 -07002800/*
2801 * schedule_work_on - put work task on a specific cpu
2802 * @cpu: cpu to put the work task on
2803 * @work: job to be done
2804 *
2805 * This puts a job on a specific cpu
2806 */
2807int schedule_work_on(int cpu, struct work_struct *work)
2808{
Tejun Heod320c032010-06-29 10:07:14 +02002809 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002810}
2811EXPORT_SYMBOL(schedule_work_on);
2812
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002813/**
2814 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002815 * @dwork: job to be done
2816 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002817 *
2818 * After waiting for a given time this puts a job in the kernel-global
2819 * workqueue.
2820 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002821int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002822 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Tejun Heod320c032010-06-29 10:07:14 +02002824 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
Dave Jonesae90dd52006-06-30 01:40:45 -04002826EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002828/**
2829 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2830 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002831 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002832 * @delay: number of jiffies to wait
2833 *
2834 * After waiting for a given time this puts a job in the kernel-global
2835 * workqueue on the specified CPU.
2836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002838 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Tejun Heod320c032010-06-29 10:07:14 +02002840 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
Dave Jonesae90dd52006-06-30 01:40:45 -04002842EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Andrew Mortonb6136772006-06-25 05:47:49 -07002844/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002845 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002846 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002848 * schedule_on_each_cpu() executes @func on each online CPU using the
2849 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002850 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002851 *
2852 * RETURNS:
2853 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002854 */
David Howells65f27f32006-11-22 14:55:48 +00002855int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002856{
2857 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002858 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002859
Andrew Mortonb6136772006-06-25 05:47:49 -07002860 works = alloc_percpu(struct work_struct);
2861 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002862 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002863
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002864 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002865
Christoph Lameter15316ba2006-01-08 01:00:43 -08002866 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002867 struct work_struct *work = per_cpu_ptr(works, cpu);
2868
2869 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002870 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002871 }
Tejun Heo93981802009-11-17 14:06:20 -08002872
2873 for_each_online_cpu(cpu)
2874 flush_work(per_cpu_ptr(works, cpu));
2875
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002876 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002877 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002878 return 0;
2879}
2880
Alan Sterneef6a7d2010-02-12 17:39:21 +09002881/**
2882 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2883 *
2884 * Forces execution of the kernel-global workqueue and blocks until its
2885 * completion.
2886 *
2887 * Think twice before calling this function! It's very easy to get into
2888 * trouble if you don't take great care. Either of the following situations
2889 * will lead to deadlock:
2890 *
2891 * One of the work items currently on the workqueue needs to acquire
2892 * a lock held by your code or its caller.
2893 *
2894 * Your code is running in the context of a work routine.
2895 *
2896 * They will be detected by lockdep when they occur, but the first might not
2897 * occur very often. It depends on what work items are on the workqueue and
2898 * what locks they need, which you have no control over.
2899 *
2900 * In most situations flushing the entire workqueue is overkill; you merely
2901 * need to know that a particular work item isn't queued and isn't running.
2902 * In such cases you should use cancel_delayed_work_sync() or
2903 * cancel_work_sync() instead.
2904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905void flush_scheduled_work(void)
2906{
Tejun Heod320c032010-06-29 10:07:14 +02002907 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
Dave Jonesae90dd52006-06-30 01:40:45 -04002909EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002912 * execute_in_process_context - reliably execute the routine with user context
2913 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002914 * @ew: guaranteed storage for the execute work structure (must
2915 * be available when the work executes)
2916 *
2917 * Executes the function immediately if process context is available,
2918 * otherwise schedules the function for delayed execution.
2919 *
2920 * Returns: 0 - function was executed
2921 * 1 - function was scheduled for execution
2922 */
David Howells65f27f32006-11-22 14:55:48 +00002923int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002924{
2925 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002926 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002927 return 0;
2928 }
2929
David Howells65f27f32006-11-22 14:55:48 +00002930 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002931 schedule_work(&ew->work);
2932
2933 return 1;
2934}
2935EXPORT_SYMBOL_GPL(execute_in_process_context);
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937int keventd_up(void)
2938{
Tejun Heod320c032010-06-29 10:07:14 +02002939 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002942static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002944 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002945 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2946 * Make sure that the alignment isn't lower than that of
2947 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002948 */
Tejun Heo0f900042010-06-29 10:07:11 +02002949 const size_t size = sizeof(struct cpu_workqueue_struct);
2950 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2951 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002952
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002953 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002954 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002955 else {
Tejun Heof3421792010-07-02 10:03:51 +02002956 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002957
Tejun Heof3421792010-07-02 10:03:51 +02002958 /*
2959 * Allocate enough room to align cwq and put an extra
2960 * pointer at the end pointing back to the originally
2961 * allocated pointer which will be used for free.
2962 */
2963 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2964 if (ptr) {
2965 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2966 *(void **)(wq->cpu_wq.single + 1) = ptr;
2967 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002968 }
Tejun Heof3421792010-07-02 10:03:51 +02002969
Tejun Heo0415b002011-03-24 18:50:09 +01002970 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002971 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2972 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002973}
2974
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002975static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002976{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002977 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002978 free_percpu(wq->cpu_wq.pcpu);
2979 else if (wq->cpu_wq.single) {
2980 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002981 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002982 }
2983}
2984
Tejun Heof3421792010-07-02 10:03:51 +02002985static int wq_clamp_max_active(int max_active, unsigned int flags,
2986 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002987{
Tejun Heof3421792010-07-02 10:03:51 +02002988 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2989
2990 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002991 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2992 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002993 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002994
Tejun Heof3421792010-07-02 10:03:51 +02002995 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002996}
2997
Tejun Heob196be82012-01-10 15:11:35 -08002998struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002999 unsigned int flags,
3000 int max_active,
3001 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003002 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003003{
Tejun Heob196be82012-01-10 15:11:35 -08003004 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003005 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003006 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003007 size_t namelen;
3008
3009 /* determine namelen, allocate wq and format name */
3010 va_start(args, lock_name);
3011 va_copy(args1, args);
3012 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3013
3014 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3015 if (!wq)
3016 goto err;
3017
3018 vsnprintf(wq->name, namelen, fmt, args1);
3019 va_end(args);
3020 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003021
Tejun Heof3421792010-07-02 10:03:51 +02003022 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003023 * Workqueues which may be used during memory reclaim should
3024 * have a rescuer to guarantee forward progress.
3025 */
3026 if (flags & WQ_MEM_RECLAIM)
3027 flags |= WQ_RESCUER;
3028
Tejun Heod320c032010-06-29 10:07:14 +02003029 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003030 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003031
Tejun Heob196be82012-01-10 15:11:35 -08003032 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003033 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003034 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003035 mutex_init(&wq->flush_mutex);
3036 atomic_set(&wq->nr_cwqs_to_flush, 0);
3037 INIT_LIST_HEAD(&wq->flusher_queue);
3038 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003039
Johannes Bergeb13ba82008-01-16 09:51:58 +01003040 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003041 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003042
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003043 if (alloc_cwqs(wq) < 0)
3044 goto err;
3045
Tejun Heof3421792010-07-02 10:03:51 +02003046 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003047 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003048 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoc45d2ad2012-07-13 22:16:45 -07003049 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003050
Tejun Heo0f900042010-06-29 10:07:11 +02003051 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heoc45d2ad2012-07-13 22:16:45 -07003052 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003053 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003054 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003055 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003056 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003057 }
3058
Tejun Heoe22bee72010-06-29 10:07:14 +02003059 if (flags & WQ_RESCUER) {
3060 struct worker *rescuer;
3061
Tejun Heof2e005a2010-07-20 15:59:09 +02003062 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003063 goto err;
3064
3065 wq->rescuer = rescuer = alloc_worker();
3066 if (!rescuer)
3067 goto err;
3068
Tejun Heob196be82012-01-10 15:11:35 -08003069 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3070 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003071 if (IS_ERR(rescuer->task))
3072 goto err;
3073
Tejun Heoe22bee72010-06-29 10:07:14 +02003074 rescuer->task->flags |= PF_THREAD_BOUND;
3075 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003076 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003077
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003078 /*
3079 * workqueue_lock protects global freeze state and workqueues
3080 * list. Grab it, set max_active accordingly and add the new
3081 * workqueue to workqueues list.
3082 */
Tejun Heo15376632010-06-29 10:07:11 +02003083 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003084
Tejun Heo58a69cb2011-02-16 09:25:31 +01003085 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003086 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003087 get_cwq(cpu, wq)->max_active = 0;
3088
Tejun Heo15376632010-06-29 10:07:11 +02003089 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003090
Tejun Heo15376632010-06-29 10:07:11 +02003091 spin_unlock(&workqueue_lock);
3092
Oleg Nesterov3af244332007-05-09 02:34:09 -07003093 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003094err:
3095 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003096 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003097 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003098 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003099 kfree(wq);
3100 }
3101 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003102}
Tejun Heod320c032010-06-29 10:07:14 +02003103EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003104
3105/**
3106 * destroy_workqueue - safely terminate a workqueue
3107 * @wq: target workqueue
3108 *
3109 * Safely destroy a workqueue. All work currently pending will be done first.
3110 */
3111void destroy_workqueue(struct workqueue_struct *wq)
3112{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003113 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003114
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003115 /* drain it before proceeding with destruction */
3116 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003117
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003118 /*
3119 * wq list is used to freeze wq, remove from list after
3120 * flushing is complete in case freeze races us.
3121 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003122 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003123 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003124 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125
Tejun Heoe22bee72010-06-29 10:07:14 +02003126 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003127 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003128 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3129 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003130
Tejun Heo73f53c42010-06-29 10:07:11 +02003131 for (i = 0; i < WORK_NR_COLORS; i++)
3132 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003133 BUG_ON(cwq->nr_active);
3134 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003135 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003136
Tejun Heoe22bee72010-06-29 10:07:14 +02003137 if (wq->flags & WQ_RESCUER) {
3138 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003139 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003140 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003141 }
3142
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003143 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003144 kfree(wq);
3145}
3146EXPORT_SYMBOL_GPL(destroy_workqueue);
3147
Tejun Heodcd989c2010-06-29 10:07:14 +02003148/**
3149 * workqueue_set_max_active - adjust max_active of a workqueue
3150 * @wq: target workqueue
3151 * @max_active: new max_active value.
3152 *
3153 * Set max_active of @wq to @max_active.
3154 *
3155 * CONTEXT:
3156 * Don't call from IRQ context.
3157 */
3158void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3159{
3160 unsigned int cpu;
3161
Tejun Heof3421792010-07-02 10:03:51 +02003162 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003163
3164 spin_lock(&workqueue_lock);
3165
3166 wq->saved_max_active = max_active;
3167
Tejun Heof3421792010-07-02 10:03:51 +02003168 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003169 struct global_cwq *gcwq = get_gcwq(cpu);
3170
3171 spin_lock_irq(&gcwq->lock);
3172
Tejun Heo58a69cb2011-02-16 09:25:31 +01003173 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003174 !(gcwq->flags & GCWQ_FREEZING))
3175 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3176
3177 spin_unlock_irq(&gcwq->lock);
3178 }
3179
3180 spin_unlock(&workqueue_lock);
3181}
3182EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3183
3184/**
3185 * workqueue_congested - test whether a workqueue is congested
3186 * @cpu: CPU in question
3187 * @wq: target workqueue
3188 *
3189 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3190 * no synchronization around this function and the test result is
3191 * unreliable and only useful as advisory hints or for debugging.
3192 *
3193 * RETURNS:
3194 * %true if congested, %false otherwise.
3195 */
3196bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3197{
3198 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3199
3200 return !list_empty(&cwq->delayed_works);
3201}
3202EXPORT_SYMBOL_GPL(workqueue_congested);
3203
3204/**
3205 * work_cpu - return the last known associated cpu for @work
3206 * @work: the work of interest
3207 *
3208 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003209 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003210 */
3211unsigned int work_cpu(struct work_struct *work)
3212{
3213 struct global_cwq *gcwq = get_work_gcwq(work);
3214
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003215 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003216}
3217EXPORT_SYMBOL_GPL(work_cpu);
3218
3219/**
3220 * work_busy - test whether a work is currently pending or running
3221 * @work: the work to be tested
3222 *
3223 * Test whether @work is currently pending or running. There is no
3224 * synchronization around this function and the test result is
3225 * unreliable and only useful as advisory hints or for debugging.
3226 * Especially for reentrant wqs, the pending state might hide the
3227 * running state.
3228 *
3229 * RETURNS:
3230 * OR'd bitmask of WORK_BUSY_* bits.
3231 */
3232unsigned int work_busy(struct work_struct *work)
3233{
3234 struct global_cwq *gcwq = get_work_gcwq(work);
3235 unsigned long flags;
3236 unsigned int ret = 0;
3237
3238 if (!gcwq)
3239 return false;
3240
3241 spin_lock_irqsave(&gcwq->lock, flags);
3242
3243 if (work_pending(work))
3244 ret |= WORK_BUSY_PENDING;
3245 if (find_worker_executing_work(gcwq, work))
3246 ret |= WORK_BUSY_RUNNING;
3247
3248 spin_unlock_irqrestore(&gcwq->lock, flags);
3249
3250 return ret;
3251}
3252EXPORT_SYMBOL_GPL(work_busy);
3253
Tejun Heodb7bccf2010-06-29 10:07:12 +02003254/*
3255 * CPU hotplug.
3256 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003257 * There are two challenges in supporting CPU hotplug. Firstly, there
3258 * are a lot of assumptions on strong associations among work, cwq and
3259 * gcwq which make migrating pending and scheduled works very
3260 * difficult to implement without impacting hot paths. Secondly,
3261 * gcwqs serve mix of short, long and very long running works making
3262 * blocked draining impractical.
3263 *
3264 * This is solved by allowing a gcwq to be detached from CPU, running
3265 * it with unbound (rogue) workers and allowing it to be reattached
3266 * later if the cpu comes back online. A separate thread is created
3267 * to govern a gcwq in such state and is called the trustee of the
3268 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003269 *
3270 * Trustee states and their descriptions.
3271 *
3272 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3273 * new trustee is started with this state.
3274 *
3275 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003276 * assuming the manager role and making all existing
3277 * workers rogue. DOWN_PREPARE waits for trustee to
3278 * enter this state. After reaching IN_CHARGE, trustee
3279 * tries to execute the pending worklist until it's empty
3280 * and the state is set to BUTCHER, or the state is set
3281 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003282 *
3283 * BUTCHER Command state which is set by the cpu callback after
3284 * the cpu has went down. Once this state is set trustee
3285 * knows that there will be no new works on the worklist
3286 * and once the worklist is empty it can proceed to
3287 * killing idle workers.
3288 *
3289 * RELEASE Command state which is set by the cpu callback if the
3290 * cpu down has been canceled or it has come online
3291 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003292 * trying to drain or butcher and clears ROGUE, rebinds
3293 * all remaining workers back to the cpu and releases
3294 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003295 *
3296 * DONE Trustee will enter this state after BUTCHER or RELEASE
3297 * is complete.
3298 *
3299 * trustee CPU draining
3300 * took over down complete
3301 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3302 * | | ^
3303 * | CPU is back online v return workers |
3304 * ----------------> RELEASE --------------
3305 */
3306
3307/**
3308 * trustee_wait_event_timeout - timed event wait for trustee
3309 * @cond: condition to wait for
3310 * @timeout: timeout in jiffies
3311 *
3312 * wait_event_timeout() for trustee to use. Handles locking and
3313 * checks for RELEASE request.
3314 *
3315 * CONTEXT:
3316 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3317 * multiple times. To be used by trustee.
3318 *
3319 * RETURNS:
3320 * Positive indicating left time if @cond is satisfied, 0 if timed
3321 * out, -1 if canceled.
3322 */
3323#define trustee_wait_event_timeout(cond, timeout) ({ \
3324 long __ret = (timeout); \
3325 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3326 __ret) { \
3327 spin_unlock_irq(&gcwq->lock); \
3328 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3329 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3330 __ret); \
3331 spin_lock_irq(&gcwq->lock); \
3332 } \
3333 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3334})
3335
3336/**
3337 * trustee_wait_event - event wait for trustee
3338 * @cond: condition to wait for
3339 *
3340 * wait_event() for trustee to use. Automatically handles locking and
3341 * checks for CANCEL request.
3342 *
3343 * CONTEXT:
3344 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3345 * multiple times. To be used by trustee.
3346 *
3347 * RETURNS:
3348 * 0 if @cond is satisfied, -1 if canceled.
3349 */
3350#define trustee_wait_event(cond) ({ \
3351 long __ret1; \
3352 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3353 __ret1 < 0 ? -1 : 0; \
3354})
3355
Tejun Heoe87d1492012-07-13 22:16:44 -07003356static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3357{
3358 struct worker_pool *pool;
3359
3360 for_each_worker_pool(pool, gcwq)
3361 if (pool->flags & POOL_MANAGING_WORKERS)
3362 return true;
3363 return false;
3364}
3365
3366static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3367{
3368 struct worker_pool *pool;
3369
3370 for_each_worker_pool(pool, gcwq)
3371 if (!list_empty(&pool->idle_list))
3372 return true;
3373 return false;
3374}
3375
Tejun Heodb7bccf2010-06-29 10:07:12 +02003376static int __cpuinit trustee_thread(void *__gcwq)
3377{
3378 struct global_cwq *gcwq = __gcwq;
Tejun Heoe87d1492012-07-13 22:16:44 -07003379 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003380 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003381 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003382 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003383 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003384 int i;
3385
3386 BUG_ON(gcwq->cpu != smp_processor_id());
3387
3388 spin_lock_irq(&gcwq->lock);
3389 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003390 * Claim the manager position and make all workers rogue.
3391 * Trustee must be bound to the target cpu and can't be
3392 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003393 */
3394 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heoe87d1492012-07-13 22:16:44 -07003395 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003396 BUG_ON(rc < 0);
3397
Tejun Heoe87d1492012-07-13 22:16:44 -07003398 for_each_worker_pool(pool, gcwq) {
3399 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003400
Tejun Heoe87d1492012-07-13 22:16:44 -07003401 list_for_each_entry(worker, &pool->idle_list, entry)
3402 worker->flags |= WORKER_ROGUE;
3403 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003404
3405 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003406 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003407
3408 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003409 * Call schedule() so that we cross rq->lock and thus can
3410 * guarantee sched callbacks see the rogue flag. This is
3411 * necessary as scheduler callbacks may be invoked from other
3412 * cpus.
3413 */
3414 spin_unlock_irq(&gcwq->lock);
3415 schedule();
3416 spin_lock_irq(&gcwq->lock);
3417
3418 /*
Tejun Heocb444762010-07-02 10:03:50 +02003419 * Sched callbacks are disabled now. Zap nr_running. After
3420 * this, nr_running stays zero and need_more_worker() and
3421 * keep_working() are always true as long as the worklist is
3422 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003423 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003424 for_each_worker_pool(pool, gcwq)
3425 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003426
3427 spin_unlock_irq(&gcwq->lock);
Tejun Heoe87d1492012-07-13 22:16:44 -07003428 for_each_worker_pool(pool, gcwq)
3429 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003430 spin_lock_irq(&gcwq->lock);
3431
3432 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003433 * We're now in charge. Notify and proceed to drain. We need
3434 * to keep the gcwq running during the whole CPU down
3435 * procedure as other cpu hotunplug callbacks may need to
3436 * flush currently running tasks.
3437 */
3438 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3439 wake_up_all(&gcwq->trustee_wait);
3440
3441 /*
3442 * The original cpu is in the process of dying and may go away
3443 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003444 * be migrated to other cpus. Try draining any left work. We
3445 * want to get it over with ASAP - spam rescuers, wake up as
3446 * many idlers as necessary and create new ones till the
3447 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003448 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003450 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003451 while (true) {
3452 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003453
Tejun Heoe87d1492012-07-13 22:16:44 -07003454 for_each_worker_pool(pool, gcwq)
3455 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003456
Tejun Heoe87d1492012-07-13 22:16:44 -07003457 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3458 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3459 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003460
Tejun Heoe87d1492012-07-13 22:16:44 -07003461 for_each_worker_pool(pool, gcwq) {
3462 int nr_works = 0;
3463
3464 list_for_each_entry(work, &pool->worklist, entry) {
3465 send_mayday(work);
3466 nr_works++;
3467 }
3468
3469 list_for_each_entry(worker, &pool->idle_list, entry) {
3470 if (!nr_works--)
3471 break;
3472 wake_up_process(worker->task);
3473 }
3474
3475 if (need_to_create_worker(pool)) {
3476 spin_unlock_irq(&gcwq->lock);
3477 worker = create_worker(pool, false);
3478 spin_lock_irq(&gcwq->lock);
3479 if (worker) {
3480 worker->flags |= WORKER_ROGUE;
3481 start_worker(worker);
3482 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003483 }
3484 }
3485
Tejun Heodb7bccf2010-06-29 10:07:12 +02003486 /* give a breather */
3487 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3488 break;
3489 }
3490
Tejun Heoe22bee72010-06-29 10:07:14 +02003491 /*
3492 * Either all works have been scheduled and cpu is down, or
3493 * cpu down has already been canceled. Wait for and butcher
3494 * all workers till we're canceled.
3495 */
3496 do {
Tejun Heoe87d1492012-07-13 22:16:44 -07003497 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3498
3499 i = 0;
3500 for_each_worker_pool(pool, gcwq) {
3501 while (!list_empty(&pool->idle_list)) {
3502 worker = list_first_entry(&pool->idle_list,
3503 struct worker, entry);
3504 destroy_worker(worker);
3505 }
3506 i |= pool->nr_workers;
3507 }
3508 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003509
3510 /*
3511 * At this point, either draining has completed and no worker
3512 * is left, or cpu down has been canceled or the cpu is being
3513 * brought back up. There shouldn't be any idle one left.
3514 * Tell the remaining busy ones to rebind once it finishes the
3515 * currently scheduled works by scheduling the rebind_work.
3516 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003517 for_each_worker_pool(pool, gcwq)
3518 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003519
3520 for_each_busy_worker(worker, i, pos, gcwq) {
3521 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003522 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003523
3524 /*
3525 * Rebind_work may race with future cpu hotplug
3526 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003527 * rebinding is scheduled. The morphing should
3528 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003529 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003530 worker_flags |= WORKER_REBIND;
3531 worker_flags &= ~WORKER_ROGUE;
3532 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003533
3534 /* queue rebind_work, wq doesn't matter, use the default one */
3535 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3536 work_data_bits(rebind_work)))
3537 continue;
3538
3539 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003540 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003541 worker->scheduled.next,
3542 work_color_to_flags(WORK_NO_COLOR));
3543 }
3544
3545 /* relinquish manager role */
Tejun Heoe87d1492012-07-13 22:16:44 -07003546 for_each_worker_pool(pool, gcwq)
3547 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003548
Tejun Heodb7bccf2010-06-29 10:07:12 +02003549 /* notify completion */
3550 gcwq->trustee = NULL;
3551 gcwq->trustee_state = TRUSTEE_DONE;
3552 wake_up_all(&gcwq->trustee_wait);
3553 spin_unlock_irq(&gcwq->lock);
3554 return 0;
3555}
3556
3557/**
3558 * wait_trustee_state - wait for trustee to enter the specified state
3559 * @gcwq: gcwq the trustee of interest belongs to
3560 * @state: target state to wait for
3561 *
3562 * Wait for the trustee to reach @state. DONE is already matched.
3563 *
3564 * CONTEXT:
3565 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3566 * multiple times. To be used by cpu_callback.
3567 */
3568static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003569__releases(&gcwq->lock)
3570__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003571{
3572 if (!(gcwq->trustee_state == state ||
3573 gcwq->trustee_state == TRUSTEE_DONE)) {
3574 spin_unlock_irq(&gcwq->lock);
3575 __wait_event(gcwq->trustee_wait,
3576 gcwq->trustee_state == state ||
3577 gcwq->trustee_state == TRUSTEE_DONE);
3578 spin_lock_irq(&gcwq->lock);
3579 }
3580}
3581
Oleg Nesterov3af244332007-05-09 02:34:09 -07003582static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3583 unsigned long action,
3584 void *hcpu)
3585{
3586 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003587 struct global_cwq *gcwq = get_gcwq(cpu);
3588 struct task_struct *new_trustee = NULL;
Tejun Heoe87d1492012-07-13 22:16:44 -07003589 struct worker *new_workers[NR_WORKER_POOLS] = { };
3590 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003591 unsigned long flags;
Tejun Heoe87d1492012-07-13 22:16:44 -07003592 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003594 action &= ~CPU_TASKS_FROZEN;
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597 case CPU_DOWN_PREPARE:
3598 new_trustee = kthread_create(trustee_thread, gcwq,
3599 "workqueue_trustee/%d\n", cpu);
3600 if (IS_ERR(new_trustee))
3601 return notifier_from_errno(PTR_ERR(new_trustee));
3602 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003603 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 case CPU_UP_PREPARE:
Tejun Heoe87d1492012-07-13 22:16:44 -07003605 i = 0;
3606 for_each_worker_pool(pool, gcwq) {
3607 BUG_ON(pool->first_idle);
3608 new_workers[i] = create_worker(pool, false);
3609 if (!new_workers[i++])
3610 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
3613
Tejun Heodb7bccf2010-06-29 10:07:12 +02003614 /* some are called w/ irq disabled, don't disturb irq status */
3615 spin_lock_irqsave(&gcwq->lock, flags);
3616
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003617 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003618 case CPU_DOWN_PREPARE:
3619 /* initialize trustee and tell it to acquire the gcwq */
3620 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3621 gcwq->trustee = new_trustee;
3622 gcwq->trustee_state = TRUSTEE_START;
3623 wake_up_process(gcwq->trustee);
3624 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003625 /* fall through */
3626 case CPU_UP_PREPARE:
Tejun Heoe87d1492012-07-13 22:16:44 -07003627 i = 0;
3628 for_each_worker_pool(pool, gcwq) {
3629 BUG_ON(pool->first_idle);
3630 pool->first_idle = new_workers[i++];
3631 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003632 break;
3633
3634 case CPU_DYING:
3635 /*
3636 * Before this, the trustee and all workers except for
3637 * the ones which are still executing works from
3638 * before the last CPU down must be on the cpu. After
3639 * this, they'll all be diasporas.
3640 */
3641 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003642 break;
3643
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003644 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003645 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003646 /* fall through */
3647 case CPU_UP_CANCELED:
Tejun Heoe87d1492012-07-13 22:16:44 -07003648 for_each_worker_pool(pool, gcwq) {
3649 destroy_worker(pool->first_idle);
3650 pool->first_idle = NULL;
3651 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003652 break;
3653
3654 case CPU_DOWN_FAILED:
3655 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003656 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003657 if (gcwq->trustee_state != TRUSTEE_DONE) {
3658 gcwq->trustee_state = TRUSTEE_RELEASE;
3659 wake_up_process(gcwq->trustee);
3660 wait_trustee_state(gcwq, TRUSTEE_DONE);
3661 }
3662
Tejun Heoe22bee72010-06-29 10:07:14 +02003663 /*
3664 * Trustee is done and there might be no worker left.
3665 * Put the first_idle in and request a real manager to
3666 * take a look.
3667 */
Tejun Heoe87d1492012-07-13 22:16:44 -07003668 for_each_worker_pool(pool, gcwq) {
3669 spin_unlock_irq(&gcwq->lock);
3670 kthread_bind(pool->first_idle->task, cpu);
3671 spin_lock_irq(&gcwq->lock);
3672 pool->flags |= POOL_MANAGE_WORKERS;
3673 start_worker(pool->first_idle);
3674 pool->first_idle = NULL;
3675 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003676 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003677 }
3678
Tejun Heodb7bccf2010-06-29 10:07:12 +02003679 spin_unlock_irqrestore(&gcwq->lock, flags);
3680
Tejun Heo15376632010-06-29 10:07:11 +02003681 return notifier_from_errno(0);
Tejun Heoe87d1492012-07-13 22:16:44 -07003682
3683err_destroy:
3684 if (new_trustee)
3685 kthread_stop(new_trustee);
3686
3687 spin_lock_irqsave(&gcwq->lock, flags);
3688 for (i = 0; i < NR_WORKER_POOLS; i++)
3689 if (new_workers[i])
3690 destroy_worker(new_workers[i]);
3691 spin_unlock_irqrestore(&gcwq->lock, flags);
3692
3693 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Tejun Heod3b42542012-07-17 12:39:26 -07003696/*
3697 * Workqueues should be brought up before normal priority CPU notifiers.
3698 * This will be registered high priority CPU notifier.
3699 */
3700static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3701 unsigned long action,
3702 void *hcpu)
3703{
3704 switch (action & ~CPU_TASKS_FROZEN) {
3705 case CPU_UP_PREPARE:
3706 case CPU_UP_CANCELED:
3707 case CPU_DOWN_FAILED:
3708 case CPU_ONLINE:
3709 return workqueue_cpu_callback(nfb, action, hcpu);
3710 }
3711 return NOTIFY_OK;
3712}
3713
3714/*
3715 * Workqueues should be brought down after normal priority CPU notifiers.
3716 * This will be registered as low priority CPU notifier.
3717 */
3718static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3719 unsigned long action,
3720 void *hcpu)
3721{
3722 switch (action & ~CPU_TASKS_FROZEN) {
3723 case CPU_DOWN_PREPARE:
3724 case CPU_DYING:
3725 case CPU_POST_DEAD:
3726 return workqueue_cpu_callback(nfb, action, hcpu);
3727 }
3728 return NOTIFY_OK;
3729}
3730
Rusty Russell2d3854a2008-11-05 13:39:10 +11003731#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003732
Rusty Russell2d3854a2008-11-05 13:39:10 +11003733struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003734 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003735 long (*fn)(void *);
3736 void *arg;
3737 long ret;
3738};
3739
Tejun Heofc7da7e2012-09-18 12:48:43 -07003740static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003741{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003742 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3743
Rusty Russell2d3854a2008-11-05 13:39:10 +11003744 wfc->ret = wfc->fn(wfc->arg);
3745}
3746
3747/**
3748 * work_on_cpu - run a function in user context on a particular cpu
3749 * @cpu: the cpu to run on
3750 * @fn: the function to run
3751 * @arg: the function arg
3752 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003753 * This will return the value @fn returns.
3754 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003755 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003756 */
3757long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3758{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003759 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003760
Tejun Heofc7da7e2012-09-18 12:48:43 -07003761 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3762 schedule_work_on(cpu, &wfc.work);
3763 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003764 return wfc.ret;
3765}
3766EXPORT_SYMBOL_GPL(work_on_cpu);
3767#endif /* CONFIG_SMP */
3768
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003769#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303770
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003771/**
3772 * freeze_workqueues_begin - begin freezing workqueues
3773 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003774 * Start freezing workqueues. After this function returns, all freezable
3775 * workqueues will queue new works to their frozen_works list instead of
3776 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003777 *
3778 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003779 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003780 */
3781void freeze_workqueues_begin(void)
3782{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003783 unsigned int cpu;
3784
3785 spin_lock(&workqueue_lock);
3786
3787 BUG_ON(workqueue_freezing);
3788 workqueue_freezing = true;
3789
Tejun Heof3421792010-07-02 10:03:51 +02003790 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003791 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003792 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003793
3794 spin_lock_irq(&gcwq->lock);
3795
Tejun Heodb7bccf2010-06-29 10:07:12 +02003796 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3797 gcwq->flags |= GCWQ_FREEZING;
3798
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003799 list_for_each_entry(wq, &workqueues, list) {
3800 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3801
Tejun Heo58a69cb2011-02-16 09:25:31 +01003802 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003803 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003804 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003805
3806 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003807 }
3808
3809 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003811
3812/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003813 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003814 *
3815 * Check whether freezing is complete. This function must be called
3816 * between freeze_workqueues_begin() and thaw_workqueues().
3817 *
3818 * CONTEXT:
3819 * Grabs and releases workqueue_lock.
3820 *
3821 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003822 * %true if some freezable workqueues are still busy. %false if freezing
3823 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003824 */
3825bool freeze_workqueues_busy(void)
3826{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003827 unsigned int cpu;
3828 bool busy = false;
3829
3830 spin_lock(&workqueue_lock);
3831
3832 BUG_ON(!workqueue_freezing);
3833
Tejun Heof3421792010-07-02 10:03:51 +02003834 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003835 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003836 /*
3837 * nr_active is monotonically decreasing. It's safe
3838 * to peek without lock.
3839 */
3840 list_for_each_entry(wq, &workqueues, list) {
3841 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3842
Tejun Heo58a69cb2011-02-16 09:25:31 +01003843 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003844 continue;
3845
3846 BUG_ON(cwq->nr_active < 0);
3847 if (cwq->nr_active) {
3848 busy = true;
3849 goto out_unlock;
3850 }
3851 }
3852 }
3853out_unlock:
3854 spin_unlock(&workqueue_lock);
3855 return busy;
3856}
3857
3858/**
3859 * thaw_workqueues - thaw workqueues
3860 *
3861 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003862 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003863 *
3864 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003865 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003866 */
3867void thaw_workqueues(void)
3868{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003869 unsigned int cpu;
3870
3871 spin_lock(&workqueue_lock);
3872
3873 if (!workqueue_freezing)
3874 goto out_unlock;
3875
Tejun Heof3421792010-07-02 10:03:51 +02003876 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003877 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoe87d1492012-07-13 22:16:44 -07003878 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003879 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003880
3881 spin_lock_irq(&gcwq->lock);
3882
Tejun Heodb7bccf2010-06-29 10:07:12 +02003883 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3884 gcwq->flags &= ~GCWQ_FREEZING;
3885
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003886 list_for_each_entry(wq, &workqueues, list) {
3887 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3888
Tejun Heo58a69cb2011-02-16 09:25:31 +01003889 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003890 continue;
3891
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003892 /* restore max_active and repopulate worklist */
3893 cwq->max_active = wq->saved_max_active;
3894
3895 while (!list_empty(&cwq->delayed_works) &&
3896 cwq->nr_active < cwq->max_active)
3897 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003898 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003899
Tejun Heoe87d1492012-07-13 22:16:44 -07003900 for_each_worker_pool(pool, gcwq)
3901 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003902
Tejun Heo8b03ae32010-06-29 10:07:12 +02003903 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003904 }
3905
3906 workqueue_freezing = false;
3907out_unlock:
3908 spin_unlock(&workqueue_lock);
3909}
3910#endif /* CONFIG_FREEZER */
3911
Suresh Siddha6ee05782010-07-30 14:57:37 -07003912static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913{
Tejun Heoc34056a2010-06-29 10:07:11 +02003914 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003915 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003916
Tejun Heod3b42542012-07-17 12:39:26 -07003917 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3918 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003919
3920 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003921 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003922 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoe87d1492012-07-13 22:16:44 -07003923 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003924
3925 spin_lock_init(&gcwq->lock);
3926 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003927 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003928
Tejun Heoc8e55f32010-06-29 10:07:12 +02003929 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3930 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3931
Tejun Heoe87d1492012-07-13 22:16:44 -07003932 for_each_worker_pool(pool, gcwq) {
3933 pool->gcwq = gcwq;
3934 INIT_LIST_HEAD(&pool->worklist);
3935 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003936
Tejun Heoe87d1492012-07-13 22:16:44 -07003937 init_timer_deferrable(&pool->idle_timer);
3938 pool->idle_timer.function = idle_worker_timeout;
3939 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003940
Tejun Heoe87d1492012-07-13 22:16:44 -07003941 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3942 (unsigned long)pool);
3943
3944 ida_init(&pool->worker_ida);
3945 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003946
3947 gcwq->trustee_state = TRUSTEE_DONE;
3948 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003949 }
3950
Tejun Heoe22bee72010-06-29 10:07:14 +02003951 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003952 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003953 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heoe87d1492012-07-13 22:16:44 -07003954 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003955
Tejun Heo477a3c32010-08-31 10:54:35 +02003956 if (cpu != WORK_CPU_UNBOUND)
3957 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heoe87d1492012-07-13 22:16:44 -07003958
3959 for_each_worker_pool(pool, gcwq) {
3960 struct worker *worker;
3961
3962 worker = create_worker(pool, true);
3963 BUG_ON(!worker);
3964 spin_lock_irq(&gcwq->lock);
3965 start_worker(worker);
3966 spin_unlock_irq(&gcwq->lock);
3967 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003968 }
3969
Tejun Heod320c032010-06-29 10:07:14 +02003970 system_wq = alloc_workqueue("events", 0, 0);
3971 system_long_wq = alloc_workqueue("events_long", 0, 0);
3972 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003973 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3974 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003975 system_freezable_wq = alloc_workqueue("events_freezable",
3976 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003977 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3978 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003979 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003980 !system_unbound_wq || !system_freezable_wq ||
3981 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003984early_initcall(init_workqueues);