blob: 98644ae1b020899b7e8622dc23df72cbda9db526 [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 Heo22ad5642012-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 Heodcb32ee2012-07-13 22:16:45 -070076 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo9c6bae02012-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 Heodcb32ee2012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-07-12 14:46:37 -0700148struct worker_pool {
149 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo22ad5642012-07-12 14:46:37 -0700150 unsigned int flags; /* X: flags */
Tejun Heo58658882012-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 Heo58658882012-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 Heodcb32ee2012-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 Heo58658882012-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 Heo9c6bae02012-07-13 22:16:44 -0700280#define for_each_worker_pool(pool, gcwq) \
Tejun Heodcb32ee2012-07-13 22:16:45 -0700281 for ((pool) = &(gcwq)->pools[0]; \
282 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo9c6bae02012-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 Heo9c6bae02012-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 Heo9c6bae02012-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 Heodcb32ee2012-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 Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700493 int cpu = pool->gcwq->cpu;
Tejun Heodcb32ee2012-07-13 22:16:45 -0700494 int idx = worker_pool_pri(pool);
Tejun Heo7ef6a932012-07-12 14:46:37 -0700495
Tejun Heof3421792010-07-02 10:03:51 +0200496 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700497 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200498 else
Tejun Heo9c6bae02012-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 Heo58658882012-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 Heodcb32ee2012-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 Heo7ef6a932012-07-12 14:46:37 -0700604static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000605{
Tejun Heodcb32ee2012-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 Heob7b5c682012-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 Heo7ef6a932012-07-12 14:46:37 -0700617static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000618{
Tejun Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700623static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200624{
Tejun Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700629static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200630{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700631 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200632
Tejun Heodcb32ee2012-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 Heo7ef6a932012-07-12 14:46:37 -0700637static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200638{
Tejun Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700643static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200644{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700645 return need_to_create_worker(pool) ||
Tejun Heo22ad5642012-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 Heo7ef6a932012-07-12 14:46:37 -0700650static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200651{
Tejun Heo22ad5642012-07-12 14:46:37 -0700652 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700664static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200665{
Tejun Heo7ef6a932012-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 Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700674 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200675 *
Tejun Heo7ef6a932012-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 Heo7ef6a932012-07-12 14:46:37 -0700681static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200682{
Tejun Heo7ef6a932012-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 Heo7ef6a932012-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 Heo58658882012-07-12 14:46:37 -0700727 struct worker_pool *pool = worker->pool;
Tejun Heo7ef6a932012-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 Heo58658882012-07-12 14:46:37 -0700747 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-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 Heo58658882012-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 Heo7ef6a932012-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 Heo58658882012-07-12 14:46:37 -0700783 !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-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 Heo7ef6a932012-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 Heo7ef6a932012-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 Heo7ef6a932012-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 Heo7ef6a932012-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 Heodcb32ee2012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-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 Heo7ef6a932012-07-12 14:46:37 -07001223 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo58658882012-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 Heo58658882012-07-12 14:46:37 -07001235 pool->nr_workers == pool->nr_idle &&
Tejun Heo7ef6a932012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-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 Heo7ef6a932012-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 Heo7ef6a932012-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 Heo7ef6a932012-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 Heo7ef6a932012-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 Heodcb32ee2012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-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 Heodcb32ee2012-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 Heodcb32ee2012-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 Heodcb32ee2012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-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 Heo58658882012-07-12 14:46:37 -07001469 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001470 if (worker->flags & WORKER_IDLE)
Tejun Heo58658882012-07-12 14:46:37 -07001471 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001472
1473 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001474 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001475
1476 spin_unlock_irq(&gcwq->lock);
1477
Tejun Heoc34056a2010-06-29 10:07:11 +02001478 kthread_stop(worker->task);
1479 kfree(worker);
1480
Tejun Heo8b03ae32010-06-29 10:07:12 +02001481 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001482 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001483}
1484
Tejun Heo7ef6a932012-07-12 14:46:37 -07001485static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001486{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001487 struct worker_pool *pool = (void *)__pool;
1488 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001489
1490 spin_lock_irq(&gcwq->lock);
1491
Tejun Heo7ef6a932012-07-12 14:46:37 -07001492 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001493 struct worker *worker;
1494 unsigned long expires;
1495
1496 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001497 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001498 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1499
1500 if (time_before(jiffies, expires))
Tejun Heo7ef6a932012-07-12 14:46:37 -07001501 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001502 else {
1503 /* it's been idle for too long, wake up manager */
Tejun Heo22ad5642012-07-12 14:46:37 -07001504 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -07001505 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001506 }
1507 }
1508
1509 spin_unlock_irq(&gcwq->lock);
1510}
1511
1512static bool send_mayday(struct work_struct *work)
1513{
1514 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1515 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001516 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001517
1518 if (!(wq->flags & WQ_RESCUER))
1519 return false;
1520
1521 /* mayday mayday mayday */
Tejun Heo58658882012-07-12 14:46:37 -07001522 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001523 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1524 if (cpu == WORK_CPU_UNBOUND)
1525 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001526 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001527 wake_up_process(wq->rescuer->task);
1528 return true;
1529}
1530
Tejun Heo7ef6a932012-07-12 14:46:37 -07001531static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001532{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001533 struct worker_pool *pool = (void *)__pool;
1534 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001535 struct work_struct *work;
1536
1537 spin_lock_irq(&gcwq->lock);
1538
Tejun Heo7ef6a932012-07-12 14:46:37 -07001539 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001540 /*
1541 * We've been trying to create a new worker but
1542 * haven't been successful. We might be hitting an
1543 * allocation deadlock. Send distress signals to
1544 * rescuers.
1545 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001546 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001547 send_mayday(work);
1548 }
1549
1550 spin_unlock_irq(&gcwq->lock);
1551
Tejun Heo7ef6a932012-07-12 14:46:37 -07001552 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001553}
1554
1555/**
1556 * maybe_create_worker - create a new worker if necessary
Tejun Heo7ef6a932012-07-12 14:46:37 -07001557 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001558 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001559 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001560 * have at least one idle worker on return from this function. If
1561 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo7ef6a932012-07-12 14:46:37 -07001562 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001563 * possible allocation deadlock.
1564 *
1565 * On return, need_to_create_worker() is guaranteed to be false and
1566 * may_start_working() true.
1567 *
1568 * LOCKING:
1569 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1570 * multiple times. Does GFP_KERNEL allocations. Called only from
1571 * manager.
1572 *
1573 * RETURNS:
1574 * false if no action was taken and gcwq->lock stayed locked, true
1575 * otherwise.
1576 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001577static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001578__releases(&gcwq->lock)
1579__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001580{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001581 struct global_cwq *gcwq = pool->gcwq;
1582
1583 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001584 return false;
1585restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001586 spin_unlock_irq(&gcwq->lock);
1587
Tejun Heoe22bee72010-06-29 10:07:14 +02001588 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001589 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001590
1591 while (true) {
1592 struct worker *worker;
1593
Tejun Heo7ef6a932012-07-12 14:46:37 -07001594 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001595 if (worker) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001596 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 spin_lock_irq(&gcwq->lock);
1598 start_worker(worker);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001599 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 return true;
1601 }
1602
Tejun Heo7ef6a932012-07-12 14:46:37 -07001603 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 break;
1605
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 __set_current_state(TASK_INTERRUPTIBLE);
1607 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001608
Tejun Heo7ef6a932012-07-12 14:46:37 -07001609 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 break;
1611 }
1612
Tejun Heo7ef6a932012-07-12 14:46:37 -07001613 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 spin_lock_irq(&gcwq->lock);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001615 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 goto restart;
1617 return true;
1618}
1619
1620/**
1621 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo7ef6a932012-07-12 14:46:37 -07001622 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001624 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 * IDLE_WORKER_TIMEOUT.
1626 *
1627 * LOCKING:
1628 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1629 * multiple times. Called only from manager.
1630 *
1631 * RETURNS:
1632 * false if no action was taken and gcwq->lock stayed locked, true
1633 * otherwise.
1634 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001635static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001636{
1637 bool ret = false;
1638
Tejun Heo7ef6a932012-07-12 14:46:37 -07001639 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001640 struct worker *worker;
1641 unsigned long expires;
1642
Tejun Heo7ef6a932012-07-12 14:46:37 -07001643 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001644 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1645
1646 if (time_before(jiffies, expires)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001647 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001648 break;
1649 }
1650
1651 destroy_worker(worker);
1652 ret = true;
1653 }
1654
1655 return ret;
1656}
1657
1658/**
1659 * manage_workers - manage worker pool
1660 * @worker: self
1661 *
1662 * Assume the manager role and manage gcwq worker pool @worker belongs
1663 * to. At any given time, there can be only zero or one manager per
1664 * gcwq. The exclusion is handled automatically by this function.
1665 *
1666 * The caller can safely start processing works on false return. On
1667 * true return, it's guaranteed that need_to_create_worker() is false
1668 * and may_start_working() is true.
1669 *
1670 * CONTEXT:
1671 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1672 * multiple times. Does GFP_KERNEL allocations.
1673 *
1674 * RETURNS:
1675 * false if no action was taken and gcwq->lock stayed locked, true if
1676 * some action was taken.
1677 */
1678static bool manage_workers(struct worker *worker)
1679{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001680 struct worker_pool *pool = worker->pool;
1681 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001682 bool ret = false;
1683
Tejun Heo22ad5642012-07-12 14:46:37 -07001684 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001685 return ret;
1686
Tejun Heo22ad5642012-07-12 14:46:37 -07001687 pool->flags &= ~POOL_MANAGE_WORKERS;
1688 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001689
1690 /*
1691 * Destroy and then create so that may_start_working() is true
1692 * on return.
1693 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001694 ret |= maybe_destroy_workers(pool);
1695 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001696
Tejun Heo22ad5642012-07-12 14:46:37 -07001697 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001698
1699 /*
1700 * The trustee might be waiting to take over the manager
1701 * position, tell it we're done.
1702 */
1703 if (unlikely(gcwq->trustee))
1704 wake_up_all(&gcwq->trustee_wait);
1705
1706 return ret;
1707}
1708
Tejun Heoa62428c2010-06-29 10:07:10 +02001709/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001710 * move_linked_works - move linked works to a list
1711 * @work: start of series of works to be scheduled
1712 * @head: target list to append @work to
1713 * @nextp: out paramter for nested worklist walking
1714 *
1715 * Schedule linked works starting from @work to @head. Work series to
1716 * be scheduled starts at @work and includes any consecutive work with
1717 * WORK_STRUCT_LINKED set in its predecessor.
1718 *
1719 * If @nextp is not NULL, it's updated to point to the next work of
1720 * the last scheduled work. This allows move_linked_works() to be
1721 * nested inside outer list_for_each_entry_safe().
1722 *
1723 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001724 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001725 */
1726static void move_linked_works(struct work_struct *work, struct list_head *head,
1727 struct work_struct **nextp)
1728{
1729 struct work_struct *n;
1730
1731 /*
1732 * Linked worklist will always end before the end of the list,
1733 * use NULL for list head.
1734 */
1735 list_for_each_entry_safe_from(work, n, NULL, entry) {
1736 list_move_tail(&work->entry, head);
1737 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1738 break;
1739 }
1740
1741 /*
1742 * If we're already inside safe list traversal and have moved
1743 * multiple works to the scheduled queue, the next position
1744 * needs to be updated.
1745 */
1746 if (nextp)
1747 *nextp = n;
1748}
1749
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001750static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001751{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001752 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001753
Tejun Heocdadf002010-10-05 10:49:55 +02001754 trace_workqueue_activate_work(work);
Tejun Heodcb32ee2012-07-13 22:16:45 -07001755 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001756 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001757 cwq->nr_active++;
1758}
1759
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001760static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1761{
1762 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1763 struct work_struct, entry);
1764
1765 cwq_activate_delayed_work(work);
1766}
1767
Tejun Heoaffee4b2010-06-29 10:07:12 +02001768/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001769 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1770 * @cwq: cwq of interest
1771 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001772 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001773 *
1774 * A work either has completed or is removed from pending queue,
1775 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1776 *
1777 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001778 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001779 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001780static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1781 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001782{
1783 /* ignore uncolored works */
1784 if (color == WORK_NO_COLOR)
1785 return;
1786
1787 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001788
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001789 if (!delayed) {
1790 cwq->nr_active--;
1791 if (!list_empty(&cwq->delayed_works)) {
1792 /* one down, submit a delayed one */
1793 if (cwq->nr_active < cwq->max_active)
1794 cwq_activate_first_delayed(cwq);
1795 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001796 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001797
1798 /* is flush in progress and are we at the flushing tip? */
1799 if (likely(cwq->flush_color != color))
1800 return;
1801
1802 /* are there still in-flight works? */
1803 if (cwq->nr_in_flight[color])
1804 return;
1805
1806 /* this cwq is done, clear flush_color */
1807 cwq->flush_color = -1;
1808
1809 /*
1810 * If this was the last cwq, wake up the first flusher. It
1811 * will handle the rest.
1812 */
1813 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1814 complete(&cwq->wq->first_flusher->done);
1815}
1816
1817/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001818 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001819 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001820 * @work: work to process
1821 *
1822 * Process @work. This function contains all the logics necessary to
1823 * process a single work including synchronization against and
1824 * interaction with other workers on the same cpu, queueing and
1825 * flushing. As long as context requirement is met, any worker can
1826 * call this function to process a work.
1827 *
1828 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001829 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001830 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001831static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001832__releases(&gcwq->lock)
1833__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001834{
Tejun Heo7e116292010-06-29 10:07:13 +02001835 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07001836 struct worker_pool *pool = worker->pool;
1837 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001838 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001839 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001840 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001841 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001842#ifdef CONFIG_LOCKDEP
1843 /*
1844 * It is permissible to free the struct work_struct from
1845 * inside the function that is called from it, this we need to
1846 * take into account for lockdep too. To avoid bogus "held
1847 * lock freed" warnings as well as problems when looking into
1848 * work->lockdep_map, make a copy and use that here.
1849 */
1850 struct lockdep_map lockdep_map = work->lockdep_map;
1851#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001852 /*
1853 * A single work shouldn't be executed concurrently by
1854 * multiple workers on a single cpu. Check whether anyone is
1855 * already processing the work. If so, defer the work to the
1856 * currently executing one.
1857 */
1858 collision = __find_worker_executing_work(gcwq, bwh, work);
1859 if (unlikely(collision)) {
1860 move_linked_works(work, &collision->scheduled, NULL);
1861 return;
1862 }
1863
Tejun Heoa62428c2010-06-29 10:07:10 +02001864 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001865 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001866 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001867 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001868 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001869 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001870 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001871
Tejun Heo7a22ad72010-06-29 10:07:13 +02001872 /* record the current cpu number in the work data and dequeue */
1873 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001874 list_del_init(&work->entry);
1875
Tejun Heo649027d2010-06-29 10:07:14 +02001876 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001877 * CPU intensive works don't participate in concurrency
1878 * management. They're the scheduler's responsibility.
1879 */
1880 if (unlikely(cpu_intensive))
1881 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1882
Tejun Heob7b5c682012-07-12 14:46:37 -07001883 /*
1884 * Unbound gcwq isn't concurrency managed and work items should be
1885 * executed ASAP. Wake up another worker if necessary.
1886 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001887 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1888 wake_up_worker(pool);
Tejun Heob7b5c682012-07-12 14:46:37 -07001889
Tejun Heo8b03ae32010-06-29 10:07:12 +02001890 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001891
Tejun Heo66307ae2012-08-03 10:30:45 -07001892 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001893 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001894
Tejun Heoe1594892011-01-09 23:32:15 +01001895 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001896 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001897 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001898 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001899 /*
1900 * While we must be careful to not use "work" after this, the trace
1901 * point will only record its address.
1902 */
1903 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001904 lock_map_release(&lockdep_map);
1905 lock_map_release(&cwq->wq->lockdep_map);
1906
1907 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001908 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1909 " last function: %pf\n",
1910 current->comm, preempt_count(), task_pid_nr(current),
1911 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001912 debug_show_held_locks(current);
1913 dump_stack();
1914 }
1915
Tejun Heo8b03ae32010-06-29 10:07:12 +02001916 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001917
Tejun Heofb0e7be2010-06-29 10:07:15 +02001918 /* clear cpu intensive status */
1919 if (unlikely(cpu_intensive))
1920 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1921
Tejun Heoa62428c2010-06-29 10:07:10 +02001922 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001923 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001924 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001925 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001926 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001927 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001928}
1929
Tejun Heoaffee4b2010-06-29 10:07:12 +02001930/**
1931 * process_scheduled_works - process scheduled works
1932 * @worker: self
1933 *
1934 * Process all scheduled works. Please note that the scheduled list
1935 * may change while processing a work, so this function repeatedly
1936 * fetches a work from the top and executes it.
1937 *
1938 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001939 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001940 * multiple times.
1941 */
1942static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001944 while (!list_empty(&worker->scheduled)) {
1945 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001947 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Tejun Heo4690c4a2010-06-29 10:07:10 +02001951/**
1952 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001953 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001954 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 * The gcwq worker thread function. There's a single dynamic pool of
1956 * these per each cpu. These workers process all works regardless of
1957 * their specific target workqueue. The only exception is works which
1958 * belong to workqueues with a rescuer which will be explained in
1959 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001960 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001961static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Tejun Heoc34056a2010-06-29 10:07:11 +02001963 struct worker *worker = __worker;
Tejun Heo58658882012-07-12 14:46:37 -07001964 struct worker_pool *pool = worker->pool;
1965 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 /* tell the scheduler that this is a workqueue worker */
1968 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001969woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001970 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Tejun Heoc8e55f32010-06-29 10:07:12 +02001972 /* DIE can be set only while we're idle, checking here is enough */
1973 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001974 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978
Tejun Heoc8e55f32010-06-29 10:07:12 +02001979 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001980recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001981 /* no more worker necessary? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001982 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 goto sleep;
1984
1985 /* do we need to manage? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001986 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 goto recheck;
1988
Tejun Heoc8e55f32010-06-29 10:07:12 +02001989 /*
1990 * ->scheduled list can only be filled while a worker is
1991 * preparing to process a work or actually processing it.
1992 * Make sure nobody diddled with it while I was sleeping.
1993 */
1994 BUG_ON(!list_empty(&worker->scheduled));
1995
Tejun Heoe22bee72010-06-29 10:07:14 +02001996 /*
1997 * When control reaches this point, we're guaranteed to have
1998 * at least one idle worker or that someone else has already
1999 * assumed the manager role.
2000 */
2001 worker_clr_flags(worker, WORKER_PREP);
2002
2003 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002004 struct work_struct *work =
Tejun Heo58658882012-07-12 14:46:37 -07002005 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002006 struct work_struct, entry);
2007
2008 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2009 /* optimization path, not strictly necessary */
2010 process_one_work(worker, work);
2011 if (unlikely(!list_empty(&worker->scheduled)))
2012 process_scheduled_works(worker);
2013 } else {
2014 move_linked_works(work, &worker->scheduled, NULL);
2015 process_scheduled_works(worker);
2016 }
Tejun Heo7ef6a932012-07-12 14:46:37 -07002017 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002018
Tejun Heoe22bee72010-06-29 10:07:14 +02002019 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002020sleep:
Tejun Heo7ef6a932012-07-12 14:46:37 -07002021 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002022 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002023
Tejun Heoc8e55f32010-06-29 10:07:12 +02002024 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 * gcwq->lock is held and there's no work to process and no
2026 * need to manage, sleep. Workers are woken up only while
2027 * holding gcwq->lock or from local cpu, so setting the
2028 * current state before releasing gcwq->lock is enough to
2029 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002030 */
2031 worker_enter_idle(worker);
2032 __set_current_state(TASK_INTERRUPTIBLE);
2033 spin_unlock_irq(&gcwq->lock);
2034 schedule();
2035 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Tejun Heoe22bee72010-06-29 10:07:14 +02002038/**
2039 * rescuer_thread - the rescuer thread function
2040 * @__wq: the associated workqueue
2041 *
2042 * Workqueue rescuer thread function. There's one rescuer for each
2043 * workqueue which has WQ_RESCUER set.
2044 *
2045 * Regular work processing on a gcwq may block trying to create a new
2046 * worker which uses GFP_KERNEL allocation which has slight chance of
2047 * developing into deadlock if some works currently on the same queue
2048 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2049 * the problem rescuer solves.
2050 *
2051 * When such condition is possible, the gcwq summons rescuers of all
2052 * workqueues which have works queued on the gcwq and let them process
2053 * those works so that forward progress can be guaranteed.
2054 *
2055 * This should happen rarely.
2056 */
2057static int rescuer_thread(void *__wq)
2058{
2059 struct workqueue_struct *wq = __wq;
2060 struct worker *rescuer = wq->rescuer;
2061 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002062 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002063 unsigned int cpu;
2064
2065 set_user_nice(current, RESCUER_NICE_LEVEL);
2066repeat:
2067 set_current_state(TASK_INTERRUPTIBLE);
2068
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002069 if (kthread_should_stop()) {
2070 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002071 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002072 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002073
Tejun Heof3421792010-07-02 10:03:51 +02002074 /*
2075 * See whether any cpu is asking for help. Unbounded
2076 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2077 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002078 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002079 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2080 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002081 struct worker_pool *pool = cwq->pool;
2082 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002083 struct work_struct *work, *n;
2084
2085 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002086 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002087
2088 /* migrate to the target cpu if possible */
Tejun Heo58658882012-07-12 14:46:37 -07002089 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002090 worker_maybe_bind_and_lock(rescuer);
2091
2092 /*
2093 * Slurp in all works issued via this workqueue and
2094 * process'em.
2095 */
2096 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo58658882012-07-12 14:46:37 -07002097 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002098 if (get_work_cwq(work) == cwq)
2099 move_linked_works(work, scheduled, &n);
2100
2101 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002102
2103 /*
2104 * Leave this gcwq. If keep_working() is %true, notify a
2105 * regular worker; otherwise, we end up with 0 concurrency
2106 * and stalling the execution.
2107 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002108 if (keep_working(pool))
2109 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002110
Tejun Heoe22bee72010-06-29 10:07:14 +02002111 spin_unlock_irq(&gcwq->lock);
2112 }
2113
2114 schedule();
2115 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002118struct wq_barrier {
2119 struct work_struct work;
2120 struct completion done;
2121};
2122
2123static void wq_barrier_func(struct work_struct *work)
2124{
2125 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2126 complete(&barr->done);
2127}
2128
Tejun Heo4690c4a2010-06-29 10:07:10 +02002129/**
2130 * insert_wq_barrier - insert a barrier work
2131 * @cwq: cwq to insert barrier into
2132 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002133 * @target: target work to attach @barr to
2134 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002135 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002136 * @barr is linked to @target such that @barr is completed only after
2137 * @target finishes execution. Please note that the ordering
2138 * guarantee is observed only with respect to @target and on the local
2139 * cpu.
2140 *
2141 * Currently, a queued barrier can't be canceled. This is because
2142 * try_to_grab_pending() can't determine whether the work to be
2143 * grabbed is at the head of the queue and thus can't clear LINKED
2144 * flag of the previous work while there must be a valid next work
2145 * after a work with LINKED flag set.
2146 *
2147 * Note that when @worker is non-NULL, @target may be modified
2148 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002149 *
2150 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002151 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002152 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002153static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002154 struct wq_barrier *barr,
2155 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002156{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002157 struct list_head *head;
2158 unsigned int linked = 0;
2159
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002160 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002161 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002162 * as we know for sure that this will not trigger any of the
2163 * checks and call back into the fixup functions where we
2164 * might deadlock.
2165 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002166 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002167 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002168 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002169
Tejun Heoaffee4b2010-06-29 10:07:12 +02002170 /*
2171 * If @target is currently being executed, schedule the
2172 * barrier to the worker; otherwise, put it after @target.
2173 */
2174 if (worker)
2175 head = worker->scheduled.next;
2176 else {
2177 unsigned long *bits = work_data_bits(target);
2178
2179 head = target->entry.next;
2180 /* there can already be other linked works, inherit and set */
2181 linked = *bits & WORK_STRUCT_LINKED;
2182 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2183 }
2184
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002185 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002186 insert_work(cwq, &barr->work, head,
2187 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002188}
2189
Tejun Heo73f53c42010-06-29 10:07:11 +02002190/**
2191 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2192 * @wq: workqueue being flushed
2193 * @flush_color: new flush color, < 0 for no-op
2194 * @work_color: new work color, < 0 for no-op
2195 *
2196 * Prepare cwqs for workqueue flushing.
2197 *
2198 * If @flush_color is non-negative, flush_color on all cwqs should be
2199 * -1. If no cwq has in-flight commands at the specified color, all
2200 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2201 * has in flight commands, its cwq->flush_color is set to
2202 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2203 * wakeup logic is armed and %true is returned.
2204 *
2205 * The caller should have initialized @wq->first_flusher prior to
2206 * calling this function with non-negative @flush_color. If
2207 * @flush_color is negative, no flush color update is done and %false
2208 * is returned.
2209 *
2210 * If @work_color is non-negative, all cwqs should have the same
2211 * work_color which is previous to @work_color and all will be
2212 * advanced to @work_color.
2213 *
2214 * CONTEXT:
2215 * mutex_lock(wq->flush_mutex).
2216 *
2217 * RETURNS:
2218 * %true if @flush_color >= 0 and there's something to flush. %false
2219 * otherwise.
2220 */
2221static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2222 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Tejun Heo73f53c42010-06-29 10:07:11 +02002224 bool wait = false;
2225 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002226
Tejun Heo73f53c42010-06-29 10:07:11 +02002227 if (flush_color >= 0) {
2228 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2229 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002230 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002231
Tejun Heof3421792010-07-02 10:03:51 +02002232 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002233 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002234 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002235
Tejun Heo8b03ae32010-06-29 10:07:12 +02002236 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002237
2238 if (flush_color >= 0) {
2239 BUG_ON(cwq->flush_color != -1);
2240
2241 if (cwq->nr_in_flight[flush_color]) {
2242 cwq->flush_color = flush_color;
2243 atomic_inc(&wq->nr_cwqs_to_flush);
2244 wait = true;
2245 }
2246 }
2247
2248 if (work_color >= 0) {
2249 BUG_ON(work_color != work_next_color(cwq->work_color));
2250 cwq->work_color = work_color;
2251 }
2252
Tejun Heo8b03ae32010-06-29 10:07:12 +02002253 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002254 }
2255
2256 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2257 complete(&wq->first_flusher->done);
2258
2259 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002262/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002264 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
2266 * Forces execution of the workqueue and blocks until its completion.
2267 * This is typically used in driver shutdown handlers.
2268 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002269 * We sleep until all works which were queued on entry have been handled,
2270 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002272void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Tejun Heo73f53c42010-06-29 10:07:11 +02002274 struct wq_flusher this_flusher = {
2275 .list = LIST_HEAD_INIT(this_flusher.list),
2276 .flush_color = -1,
2277 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2278 };
2279 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002280
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002281 lock_map_acquire(&wq->lockdep_map);
2282 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002283
2284 mutex_lock(&wq->flush_mutex);
2285
2286 /*
2287 * Start-to-wait phase
2288 */
2289 next_color = work_next_color(wq->work_color);
2290
2291 if (next_color != wq->flush_color) {
2292 /*
2293 * Color space is not full. The current work_color
2294 * becomes our flush_color and work_color is advanced
2295 * by one.
2296 */
2297 BUG_ON(!list_empty(&wq->flusher_overflow));
2298 this_flusher.flush_color = wq->work_color;
2299 wq->work_color = next_color;
2300
2301 if (!wq->first_flusher) {
2302 /* no flush in progress, become the first flusher */
2303 BUG_ON(wq->flush_color != this_flusher.flush_color);
2304
2305 wq->first_flusher = &this_flusher;
2306
2307 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2308 wq->work_color)) {
2309 /* nothing to flush, done */
2310 wq->flush_color = next_color;
2311 wq->first_flusher = NULL;
2312 goto out_unlock;
2313 }
2314 } else {
2315 /* wait in queue */
2316 BUG_ON(wq->flush_color == this_flusher.flush_color);
2317 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2318 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2319 }
2320 } else {
2321 /*
2322 * Oops, color space is full, wait on overflow queue.
2323 * The next flush completion will assign us
2324 * flush_color and transfer to flusher_queue.
2325 */
2326 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2327 }
2328
2329 mutex_unlock(&wq->flush_mutex);
2330
2331 wait_for_completion(&this_flusher.done);
2332
2333 /*
2334 * Wake-up-and-cascade phase
2335 *
2336 * First flushers are responsible for cascading flushes and
2337 * handling overflow. Non-first flushers can simply return.
2338 */
2339 if (wq->first_flusher != &this_flusher)
2340 return;
2341
2342 mutex_lock(&wq->flush_mutex);
2343
Tejun Heo4ce48b32010-07-02 10:03:51 +02002344 /* we might have raced, check again with mutex held */
2345 if (wq->first_flusher != &this_flusher)
2346 goto out_unlock;
2347
Tejun Heo73f53c42010-06-29 10:07:11 +02002348 wq->first_flusher = NULL;
2349
2350 BUG_ON(!list_empty(&this_flusher.list));
2351 BUG_ON(wq->flush_color != this_flusher.flush_color);
2352
2353 while (true) {
2354 struct wq_flusher *next, *tmp;
2355
2356 /* complete all the flushers sharing the current flush color */
2357 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2358 if (next->flush_color != wq->flush_color)
2359 break;
2360 list_del_init(&next->list);
2361 complete(&next->done);
2362 }
2363
2364 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2365 wq->flush_color != work_next_color(wq->work_color));
2366
2367 /* this flush_color is finished, advance by one */
2368 wq->flush_color = work_next_color(wq->flush_color);
2369
2370 /* one color has been freed, handle overflow queue */
2371 if (!list_empty(&wq->flusher_overflow)) {
2372 /*
2373 * Assign the same color to all overflowed
2374 * flushers, advance work_color and append to
2375 * flusher_queue. This is the start-to-wait
2376 * phase for these overflowed flushers.
2377 */
2378 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2379 tmp->flush_color = wq->work_color;
2380
2381 wq->work_color = work_next_color(wq->work_color);
2382
2383 list_splice_tail_init(&wq->flusher_overflow,
2384 &wq->flusher_queue);
2385 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2386 }
2387
2388 if (list_empty(&wq->flusher_queue)) {
2389 BUG_ON(wq->flush_color != wq->work_color);
2390 break;
2391 }
2392
2393 /*
2394 * Need to flush more colors. Make the next flusher
2395 * the new first flusher and arm cwqs.
2396 */
2397 BUG_ON(wq->flush_color == wq->work_color);
2398 BUG_ON(wq->flush_color != next->flush_color);
2399
2400 list_del_init(&next->list);
2401 wq->first_flusher = next;
2402
2403 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2404 break;
2405
2406 /*
2407 * Meh... this color is already done, clear first
2408 * flusher and repeat cascading.
2409 */
2410 wq->first_flusher = NULL;
2411 }
2412
2413out_unlock:
2414 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
Dave Jonesae90dd52006-06-30 01:40:45 -04002416EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002418/**
2419 * drain_workqueue - drain a workqueue
2420 * @wq: workqueue to drain
2421 *
2422 * Wait until the workqueue becomes empty. While draining is in progress,
2423 * only chain queueing is allowed. IOW, only currently pending or running
2424 * work items on @wq can queue further work items on it. @wq is flushed
2425 * repeatedly until it becomes empty. The number of flushing is detemined
2426 * by the depth of chaining and should be relatively short. Whine if it
2427 * takes too long.
2428 */
2429void drain_workqueue(struct workqueue_struct *wq)
2430{
2431 unsigned int flush_cnt = 0;
2432 unsigned int cpu;
2433
2434 /*
2435 * __queue_work() needs to test whether there are drainers, is much
2436 * hotter than drain_workqueue() and already looks at @wq->flags.
2437 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2438 */
2439 spin_lock(&workqueue_lock);
2440 if (!wq->nr_drainers++)
2441 wq->flags |= WQ_DRAINING;
2442 spin_unlock(&workqueue_lock);
2443reflush:
2444 flush_workqueue(wq);
2445
2446 for_each_cwq_cpu(cpu, wq) {
2447 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002448 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002449
Tejun Heo58658882012-07-12 14:46:37 -07002450 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002451 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo58658882012-07-12 14:46:37 -07002452 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002453
2454 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002455 continue;
2456
2457 if (++flush_cnt == 10 ||
2458 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2459 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2460 wq->name, flush_cnt);
2461 goto reflush;
2462 }
2463
2464 spin_lock(&workqueue_lock);
2465 if (!--wq->nr_drainers)
2466 wq->flags &= ~WQ_DRAINING;
2467 spin_unlock(&workqueue_lock);
2468}
2469EXPORT_SYMBOL_GPL(drain_workqueue);
2470
Tejun Heobaf59022010-09-16 10:42:16 +02002471static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2472 bool wait_executing)
2473{
2474 struct worker *worker = NULL;
2475 struct global_cwq *gcwq;
2476 struct cpu_workqueue_struct *cwq;
2477
2478 might_sleep();
2479 gcwq = get_work_gcwq(work);
2480 if (!gcwq)
2481 return false;
2482
2483 spin_lock_irq(&gcwq->lock);
2484 if (!list_empty(&work->entry)) {
2485 /*
2486 * See the comment near try_to_grab_pending()->smp_rmb().
2487 * If it was re-queued to a different gcwq under us, we
2488 * are not going to wait.
2489 */
2490 smp_rmb();
2491 cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07002492 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002493 goto already_gone;
2494 } else if (wait_executing) {
2495 worker = find_worker_executing_work(gcwq, work);
2496 if (!worker)
2497 goto already_gone;
2498 cwq = worker->current_cwq;
2499 } else
2500 goto already_gone;
2501
2502 insert_wq_barrier(cwq, barr, work, worker);
2503 spin_unlock_irq(&gcwq->lock);
2504
Tejun Heoe1594892011-01-09 23:32:15 +01002505 /*
2506 * If @max_active is 1 or rescuer is in use, flushing another work
2507 * item on the same workqueue may lead to deadlock. Make sure the
2508 * flusher is not running on the same workqueue by verifying write
2509 * access.
2510 */
2511 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2512 lock_map_acquire(&cwq->wq->lockdep_map);
2513 else
2514 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002515 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002516
Tejun Heobaf59022010-09-16 10:42:16 +02002517 return true;
2518already_gone:
2519 spin_unlock_irq(&gcwq->lock);
2520 return false;
2521}
2522
Oleg Nesterovdb700892008-07-25 01:47:49 -07002523/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002524 * flush_work - wait for a work to finish executing the last queueing instance
2525 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002526 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002527 * Wait until @work has finished execution. This function considers
2528 * only the last queueing instance of @work. If @work has been
2529 * enqueued across different CPUs on a non-reentrant workqueue or on
2530 * multiple workqueues, @work might still be executing on return on
2531 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002532 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002533 * If @work was queued only on a non-reentrant, ordered or unbound
2534 * workqueue, @work is guaranteed to be idle on return if it hasn't
2535 * been requeued since flush started.
2536 *
2537 * RETURNS:
2538 * %true if flush_work() waited for the work to finish execution,
2539 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002540 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002541bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002542{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002543 struct wq_barrier barr;
2544
Tejun Heobaf59022010-09-16 10:42:16 +02002545 if (start_flush_work(work, &barr, true)) {
2546 wait_for_completion(&barr.done);
2547 destroy_work_on_stack(&barr.work);
2548 return true;
2549 } else
2550 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002551}
2552EXPORT_SYMBOL_GPL(flush_work);
2553
Tejun Heo401a8d02010-09-16 10:36:00 +02002554static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2555{
2556 struct wq_barrier barr;
2557 struct worker *worker;
2558
2559 spin_lock_irq(&gcwq->lock);
2560
2561 worker = find_worker_executing_work(gcwq, work);
2562 if (unlikely(worker))
2563 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2564
2565 spin_unlock_irq(&gcwq->lock);
2566
2567 if (unlikely(worker)) {
2568 wait_for_completion(&barr.done);
2569 destroy_work_on_stack(&barr.work);
2570 return true;
2571 } else
2572 return false;
2573}
2574
2575static bool wait_on_work(struct work_struct *work)
2576{
2577 bool ret = false;
2578 int cpu;
2579
2580 might_sleep();
2581
2582 lock_map_acquire(&work->lockdep_map);
2583 lock_map_release(&work->lockdep_map);
2584
2585 for_each_gcwq_cpu(cpu)
2586 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2587 return ret;
2588}
2589
Tejun Heo09383492010-09-16 10:48:29 +02002590/**
2591 * flush_work_sync - wait until a work has finished execution
2592 * @work: the work to flush
2593 *
2594 * Wait until @work has finished execution. On return, it's
2595 * guaranteed that all queueing instances of @work which happened
2596 * before this function is called are finished. In other words, if
2597 * @work hasn't been requeued since this function was called, @work is
2598 * guaranteed to be idle on return.
2599 *
2600 * RETURNS:
2601 * %true if flush_work_sync() waited for the work to finish execution,
2602 * %false if it was already idle.
2603 */
2604bool flush_work_sync(struct work_struct *work)
2605{
2606 struct wq_barrier barr;
2607 bool pending, waited;
2608
2609 /* we'll wait for executions separately, queue barr only if pending */
2610 pending = start_flush_work(work, &barr, false);
2611
2612 /* wait for executions to finish */
2613 waited = wait_on_work(work);
2614
2615 /* wait for the pending one */
2616 if (pending) {
2617 wait_for_completion(&barr.done);
2618 destroy_work_on_stack(&barr.work);
2619 }
2620
2621 return pending || waited;
2622}
2623EXPORT_SYMBOL_GPL(flush_work_sync);
2624
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002625/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002626 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002627 * so this work can't be re-armed in any way.
2628 */
2629static int try_to_grab_pending(struct work_struct *work)
2630{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002631 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002632 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002633
Tejun Heo22df02b2010-06-29 10:07:10 +02002634 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002635 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002636
2637 /*
2638 * The queueing is in progress, or it is already queued. Try to
2639 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2640 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002641 gcwq = get_work_gcwq(work);
2642 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002643 return ret;
2644
Tejun Heo8b03ae32010-06-29 10:07:12 +02002645 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002646 if (!list_empty(&work->entry)) {
2647 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002648 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002649 * In that case we must see the new value after rmb(), see
2650 * insert_work()->wmb().
2651 */
2652 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002653 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002654 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002655
2656 /*
2657 * A delayed work item cannot be grabbed directly
2658 * because it might have linked NO_COLOR work items
2659 * which, if left on the delayed_list, will confuse
2660 * cwq->nr_active management later on and cause
2661 * stall. Make sure the work item is activated
2662 * before grabbing.
2663 */
2664 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2665 cwq_activate_delayed_work(work);
2666
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002668 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002669 get_work_color(work),
2670 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002671 ret = 1;
2672 }
2673 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002674 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675
2676 return ret;
2677}
2678
Tejun Heo401a8d02010-09-16 10:36:00 +02002679static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002680 struct timer_list* timer)
2681{
2682 int ret;
2683
2684 do {
2685 ret = (timer && likely(del_timer(timer)));
2686 if (!ret)
2687 ret = try_to_grab_pending(work);
2688 wait_on_work(work);
2689 } while (unlikely(ret < 0));
2690
Tejun Heo7a22ad72010-06-29 10:07:13 +02002691 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002692 return ret;
2693}
2694
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002695/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002696 * cancel_work_sync - cancel a work and wait for it to finish
2697 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002698 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002699 * Cancel @work and wait for its execution to finish. This function
2700 * can be used even if the work re-queues itself or migrates to
2701 * another workqueue. On return from this function, @work is
2702 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002703 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002704 * cancel_work_sync(&delayed_work->work) must not be used for
2705 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002706 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002707 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002708 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002709 *
2710 * RETURNS:
2711 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002712 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002713bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002714{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002715 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002716}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002717EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002718
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002720 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2721 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002722 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002723 * Delayed timer is cancelled and the pending work is queued for
2724 * immediate execution. Like flush_work(), this function only
2725 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002726 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 * RETURNS:
2728 * %true if flush_work() waited for the work to finish execution,
2729 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002731bool flush_delayed_work(struct delayed_work *dwork)
2732{
2733 if (del_timer_sync(&dwork->timer))
2734 __queue_work(raw_smp_processor_id(),
2735 get_work_cwq(&dwork->work)->wq, &dwork->work);
2736 return flush_work(&dwork->work);
2737}
2738EXPORT_SYMBOL(flush_delayed_work);
2739
2740/**
Tejun Heo09383492010-09-16 10:48:29 +02002741 * flush_delayed_work_sync - wait for a dwork to finish
2742 * @dwork: the delayed work to flush
2743 *
2744 * Delayed timer is cancelled and the pending work is queued for
2745 * execution immediately. Other than timer handling, its behavior
2746 * is identical to flush_work_sync().
2747 *
2748 * RETURNS:
2749 * %true if flush_work_sync() waited for the work to finish execution,
2750 * %false if it was already idle.
2751 */
2752bool flush_delayed_work_sync(struct delayed_work *dwork)
2753{
2754 if (del_timer_sync(&dwork->timer))
2755 __queue_work(raw_smp_processor_id(),
2756 get_work_cwq(&dwork->work)->wq, &dwork->work);
2757 return flush_work_sync(&dwork->work);
2758}
2759EXPORT_SYMBOL(flush_delayed_work_sync);
2760
2761/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002762 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2763 * @dwork: the delayed work cancel
2764 *
2765 * This is cancel_work_sync() for delayed works.
2766 *
2767 * RETURNS:
2768 * %true if @dwork was pending, %false otherwise.
2769 */
2770bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002771{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002772 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002773}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002774EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002776/**
2777 * schedule_work - put work task in global workqueue
2778 * @work: job to be done
2779 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002780 * Returns zero if @work was already on the kernel-global workqueue and
2781 * non-zero otherwise.
2782 *
2783 * This puts a job in the kernel-global workqueue if it was not already
2784 * queued and leaves it in the same position on the kernel-global
2785 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002786 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002787int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Tejun Heod320c032010-06-29 10:07:14 +02002789 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
Dave Jonesae90dd52006-06-30 01:40:45 -04002791EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Zhang Ruic1a220e2008-07-23 21:28:39 -07002793/*
2794 * schedule_work_on - put work task on a specific cpu
2795 * @cpu: cpu to put the work task on
2796 * @work: job to be done
2797 *
2798 * This puts a job on a specific cpu
2799 */
2800int schedule_work_on(int cpu, struct work_struct *work)
2801{
Tejun Heod320c032010-06-29 10:07:14 +02002802 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002803}
2804EXPORT_SYMBOL(schedule_work_on);
2805
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002806/**
2807 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002808 * @dwork: job to be done
2809 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002810 *
2811 * After waiting for a given time this puts a job in the kernel-global
2812 * workqueue.
2813 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002814int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002815 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Tejun Heod320c032010-06-29 10:07:14 +02002817 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
Dave Jonesae90dd52006-06-30 01:40:45 -04002819EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002821/**
2822 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2823 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002824 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002825 * @delay: number of jiffies to wait
2826 *
2827 * After waiting for a given time this puts a job in the kernel-global
2828 * workqueue on the specified CPU.
2829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002831 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Tejun Heod320c032010-06-29 10:07:14 +02002833 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
Dave Jonesae90dd52006-06-30 01:40:45 -04002835EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Andrew Mortonb6136772006-06-25 05:47:49 -07002837/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002838 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002839 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002840 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002841 * schedule_on_each_cpu() executes @func on each online CPU using the
2842 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002843 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002844 *
2845 * RETURNS:
2846 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 */
David Howells65f27f32006-11-22 14:55:48 +00002848int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002849{
2850 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002851 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002852
Andrew Mortonb6136772006-06-25 05:47:49 -07002853 works = alloc_percpu(struct work_struct);
2854 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002855 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002856
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002857 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002858
Christoph Lameter15316ba2006-01-08 01:00:43 -08002859 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002860 struct work_struct *work = per_cpu_ptr(works, cpu);
2861
2862 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002863 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002864 }
Tejun Heo93981802009-11-17 14:06:20 -08002865
2866 for_each_online_cpu(cpu)
2867 flush_work(per_cpu_ptr(works, cpu));
2868
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002869 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002870 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002871 return 0;
2872}
2873
Alan Sterneef6a7d2010-02-12 17:39:21 +09002874/**
2875 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2876 *
2877 * Forces execution of the kernel-global workqueue and blocks until its
2878 * completion.
2879 *
2880 * Think twice before calling this function! It's very easy to get into
2881 * trouble if you don't take great care. Either of the following situations
2882 * will lead to deadlock:
2883 *
2884 * One of the work items currently on the workqueue needs to acquire
2885 * a lock held by your code or its caller.
2886 *
2887 * Your code is running in the context of a work routine.
2888 *
2889 * They will be detected by lockdep when they occur, but the first might not
2890 * occur very often. It depends on what work items are on the workqueue and
2891 * what locks they need, which you have no control over.
2892 *
2893 * In most situations flushing the entire workqueue is overkill; you merely
2894 * need to know that a particular work item isn't queued and isn't running.
2895 * In such cases you should use cancel_delayed_work_sync() or
2896 * cancel_work_sync() instead.
2897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898void flush_scheduled_work(void)
2899{
Tejun Heod320c032010-06-29 10:07:14 +02002900 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
Dave Jonesae90dd52006-06-30 01:40:45 -04002902EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002905 * execute_in_process_context - reliably execute the routine with user context
2906 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002907 * @ew: guaranteed storage for the execute work structure (must
2908 * be available when the work executes)
2909 *
2910 * Executes the function immediately if process context is available,
2911 * otherwise schedules the function for delayed execution.
2912 *
2913 * Returns: 0 - function was executed
2914 * 1 - function was scheduled for execution
2915 */
David Howells65f27f32006-11-22 14:55:48 +00002916int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002917{
2918 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002919 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002920 return 0;
2921 }
2922
David Howells65f27f32006-11-22 14:55:48 +00002923 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002924 schedule_work(&ew->work);
2925
2926 return 1;
2927}
2928EXPORT_SYMBOL_GPL(execute_in_process_context);
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930int keventd_up(void)
2931{
Tejun Heod320c032010-06-29 10:07:14 +02002932 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002935static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002937 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002938 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2939 * Make sure that the alignment isn't lower than that of
2940 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002941 */
Tejun Heo0f900042010-06-29 10:07:11 +02002942 const size_t size = sizeof(struct cpu_workqueue_struct);
2943 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2944 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002945
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002946 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002947 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002948 else {
Tejun Heof3421792010-07-02 10:03:51 +02002949 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002950
Tejun Heof3421792010-07-02 10:03:51 +02002951 /*
2952 * Allocate enough room to align cwq and put an extra
2953 * pointer at the end pointing back to the originally
2954 * allocated pointer which will be used for free.
2955 */
2956 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2957 if (ptr) {
2958 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2959 *(void **)(wq->cpu_wq.single + 1) = ptr;
2960 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002961 }
Tejun Heof3421792010-07-02 10:03:51 +02002962
Tejun Heo0415b002011-03-24 18:50:09 +01002963 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002964 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2965 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002966}
2967
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002968static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002969{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002970 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002971 free_percpu(wq->cpu_wq.pcpu);
2972 else if (wq->cpu_wq.single) {
2973 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002974 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002975 }
2976}
2977
Tejun Heof3421792010-07-02 10:03:51 +02002978static int wq_clamp_max_active(int max_active, unsigned int flags,
2979 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002980{
Tejun Heof3421792010-07-02 10:03:51 +02002981 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2982
2983 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002984 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2985 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002986 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002987
Tejun Heof3421792010-07-02 10:03:51 +02002988 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002989}
2990
Tejun Heob196be82012-01-10 15:11:35 -08002991struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002992 unsigned int flags,
2993 int max_active,
2994 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002995 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002996{
Tejun Heob196be82012-01-10 15:11:35 -08002997 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002998 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002999 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003000 size_t namelen;
3001
3002 /* determine namelen, allocate wq and format name */
3003 va_start(args, lock_name);
3004 va_copy(args1, args);
3005 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3006
3007 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3008 if (!wq)
3009 goto err;
3010
3011 vsnprintf(wq->name, namelen, fmt, args1);
3012 va_end(args);
3013 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003014
Tejun Heof3421792010-07-02 10:03:51 +02003015 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003016 * Workqueues which may be used during memory reclaim should
3017 * have a rescuer to guarantee forward progress.
3018 */
3019 if (flags & WQ_MEM_RECLAIM)
3020 flags |= WQ_RESCUER;
3021
Tejun Heod320c032010-06-29 10:07:14 +02003022 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003023 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003024
Tejun Heob196be82012-01-10 15:11:35 -08003025 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003026 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003027 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003028 mutex_init(&wq->flush_mutex);
3029 atomic_set(&wq->nr_cwqs_to_flush, 0);
3030 INIT_LIST_HEAD(&wq->flusher_queue);
3031 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003032
Johannes Bergeb13ba82008-01-16 09:51:58 +01003033 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003034 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003035
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003036 if (alloc_cwqs(wq) < 0)
3037 goto err;
3038
Tejun Heof3421792010-07-02 10:03:51 +02003039 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003040 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003041 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003042 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003043
Tejun Heo0f900042010-06-29 10:07:11 +02003044 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003045 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003046 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003047 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003048 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003049 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003050 }
3051
Tejun Heoe22bee72010-06-29 10:07:14 +02003052 if (flags & WQ_RESCUER) {
3053 struct worker *rescuer;
3054
Tejun Heof2e005a2010-07-20 15:59:09 +02003055 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003056 goto err;
3057
3058 wq->rescuer = rescuer = alloc_worker();
3059 if (!rescuer)
3060 goto err;
3061
Tejun Heob196be82012-01-10 15:11:35 -08003062 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3063 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003064 if (IS_ERR(rescuer->task))
3065 goto err;
3066
Tejun Heoe22bee72010-06-29 10:07:14 +02003067 rescuer->task->flags |= PF_THREAD_BOUND;
3068 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003069 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003070
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003071 /*
3072 * workqueue_lock protects global freeze state and workqueues
3073 * list. Grab it, set max_active accordingly and add the new
3074 * workqueue to workqueues list.
3075 */
Tejun Heo15376632010-06-29 10:07:11 +02003076 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003077
Tejun Heo58a69cb2011-02-16 09:25:31 +01003078 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003079 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003080 get_cwq(cpu, wq)->max_active = 0;
3081
Tejun Heo15376632010-06-29 10:07:11 +02003082 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003083
Tejun Heo15376632010-06-29 10:07:11 +02003084 spin_unlock(&workqueue_lock);
3085
Oleg Nesterov3af244332007-05-09 02:34:09 -07003086 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003087err:
3088 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003089 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003090 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003091 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003092 kfree(wq);
3093 }
3094 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003095}
Tejun Heod320c032010-06-29 10:07:14 +02003096EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003097
3098/**
3099 * destroy_workqueue - safely terminate a workqueue
3100 * @wq: target workqueue
3101 *
3102 * Safely destroy a workqueue. All work currently pending will be done first.
3103 */
3104void destroy_workqueue(struct workqueue_struct *wq)
3105{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003106 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003107
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003108 /* drain it before proceeding with destruction */
3109 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003110
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003111 /*
3112 * wq list is used to freeze wq, remove from list after
3113 * flushing is complete in case freeze races us.
3114 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003115 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003116 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003117 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003118
Tejun Heoe22bee72010-06-29 10:07:14 +02003119 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003120 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003121 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3122 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003123
Tejun Heo73f53c42010-06-29 10:07:11 +02003124 for (i = 0; i < WORK_NR_COLORS; i++)
3125 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003126 BUG_ON(cwq->nr_active);
3127 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003128 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003129
Tejun Heoe22bee72010-06-29 10:07:14 +02003130 if (wq->flags & WQ_RESCUER) {
3131 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003132 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003133 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003134 }
3135
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003136 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003137 kfree(wq);
3138}
3139EXPORT_SYMBOL_GPL(destroy_workqueue);
3140
Tejun Heodcd989c2010-06-29 10:07:14 +02003141/**
3142 * workqueue_set_max_active - adjust max_active of a workqueue
3143 * @wq: target workqueue
3144 * @max_active: new max_active value.
3145 *
3146 * Set max_active of @wq to @max_active.
3147 *
3148 * CONTEXT:
3149 * Don't call from IRQ context.
3150 */
3151void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3152{
3153 unsigned int cpu;
3154
Tejun Heof3421792010-07-02 10:03:51 +02003155 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003156
3157 spin_lock(&workqueue_lock);
3158
3159 wq->saved_max_active = max_active;
3160
Tejun Heof3421792010-07-02 10:03:51 +02003161 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003162 struct global_cwq *gcwq = get_gcwq(cpu);
3163
3164 spin_lock_irq(&gcwq->lock);
3165
Tejun Heo58a69cb2011-02-16 09:25:31 +01003166 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003167 !(gcwq->flags & GCWQ_FREEZING))
3168 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3169
3170 spin_unlock_irq(&gcwq->lock);
3171 }
3172
3173 spin_unlock(&workqueue_lock);
3174}
3175EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3176
3177/**
3178 * workqueue_congested - test whether a workqueue is congested
3179 * @cpu: CPU in question
3180 * @wq: target workqueue
3181 *
3182 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3183 * no synchronization around this function and the test result is
3184 * unreliable and only useful as advisory hints or for debugging.
3185 *
3186 * RETURNS:
3187 * %true if congested, %false otherwise.
3188 */
3189bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3190{
3191 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3192
3193 return !list_empty(&cwq->delayed_works);
3194}
3195EXPORT_SYMBOL_GPL(workqueue_congested);
3196
3197/**
3198 * work_cpu - return the last known associated cpu for @work
3199 * @work: the work of interest
3200 *
3201 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003202 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003203 */
3204unsigned int work_cpu(struct work_struct *work)
3205{
3206 struct global_cwq *gcwq = get_work_gcwq(work);
3207
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003208 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003209}
3210EXPORT_SYMBOL_GPL(work_cpu);
3211
3212/**
3213 * work_busy - test whether a work is currently pending or running
3214 * @work: the work to be tested
3215 *
3216 * Test whether @work is currently pending or running. There is no
3217 * synchronization around this function and the test result is
3218 * unreliable and only useful as advisory hints or for debugging.
3219 * Especially for reentrant wqs, the pending state might hide the
3220 * running state.
3221 *
3222 * RETURNS:
3223 * OR'd bitmask of WORK_BUSY_* bits.
3224 */
3225unsigned int work_busy(struct work_struct *work)
3226{
3227 struct global_cwq *gcwq = get_work_gcwq(work);
3228 unsigned long flags;
3229 unsigned int ret = 0;
3230
3231 if (!gcwq)
3232 return false;
3233
3234 spin_lock_irqsave(&gcwq->lock, flags);
3235
3236 if (work_pending(work))
3237 ret |= WORK_BUSY_PENDING;
3238 if (find_worker_executing_work(gcwq, work))
3239 ret |= WORK_BUSY_RUNNING;
3240
3241 spin_unlock_irqrestore(&gcwq->lock, flags);
3242
3243 return ret;
3244}
3245EXPORT_SYMBOL_GPL(work_busy);
3246
Tejun Heodb7bccf2010-06-29 10:07:12 +02003247/*
3248 * CPU hotplug.
3249 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003250 * There are two challenges in supporting CPU hotplug. Firstly, there
3251 * are a lot of assumptions on strong associations among work, cwq and
3252 * gcwq which make migrating pending and scheduled works very
3253 * difficult to implement without impacting hot paths. Secondly,
3254 * gcwqs serve mix of short, long and very long running works making
3255 * blocked draining impractical.
3256 *
3257 * This is solved by allowing a gcwq to be detached from CPU, running
3258 * it with unbound (rogue) workers and allowing it to be reattached
3259 * later if the cpu comes back online. A separate thread is created
3260 * to govern a gcwq in such state and is called the trustee of the
3261 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003262 *
3263 * Trustee states and their descriptions.
3264 *
3265 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3266 * new trustee is started with this state.
3267 *
3268 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003269 * assuming the manager role and making all existing
3270 * workers rogue. DOWN_PREPARE waits for trustee to
3271 * enter this state. After reaching IN_CHARGE, trustee
3272 * tries to execute the pending worklist until it's empty
3273 * and the state is set to BUTCHER, or the state is set
3274 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003275 *
3276 * BUTCHER Command state which is set by the cpu callback after
3277 * the cpu has went down. Once this state is set trustee
3278 * knows that there will be no new works on the worklist
3279 * and once the worklist is empty it can proceed to
3280 * killing idle workers.
3281 *
3282 * RELEASE Command state which is set by the cpu callback if the
3283 * cpu down has been canceled or it has come online
3284 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003285 * trying to drain or butcher and clears ROGUE, rebinds
3286 * all remaining workers back to the cpu and releases
3287 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003288 *
3289 * DONE Trustee will enter this state after BUTCHER or RELEASE
3290 * is complete.
3291 *
3292 * trustee CPU draining
3293 * took over down complete
3294 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3295 * | | ^
3296 * | CPU is back online v return workers |
3297 * ----------------> RELEASE --------------
3298 */
3299
3300/**
3301 * trustee_wait_event_timeout - timed event wait for trustee
3302 * @cond: condition to wait for
3303 * @timeout: timeout in jiffies
3304 *
3305 * wait_event_timeout() for trustee to use. Handles locking and
3306 * checks for RELEASE request.
3307 *
3308 * CONTEXT:
3309 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3310 * multiple times. To be used by trustee.
3311 *
3312 * RETURNS:
3313 * Positive indicating left time if @cond is satisfied, 0 if timed
3314 * out, -1 if canceled.
3315 */
3316#define trustee_wait_event_timeout(cond, timeout) ({ \
3317 long __ret = (timeout); \
3318 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3319 __ret) { \
3320 spin_unlock_irq(&gcwq->lock); \
3321 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3322 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3323 __ret); \
3324 spin_lock_irq(&gcwq->lock); \
3325 } \
3326 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3327})
3328
3329/**
3330 * trustee_wait_event - event wait for trustee
3331 * @cond: condition to wait for
3332 *
3333 * wait_event() for trustee to use. Automatically handles locking and
3334 * checks for CANCEL request.
3335 *
3336 * CONTEXT:
3337 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3338 * multiple times. To be used by trustee.
3339 *
3340 * RETURNS:
3341 * 0 if @cond is satisfied, -1 if canceled.
3342 */
3343#define trustee_wait_event(cond) ({ \
3344 long __ret1; \
3345 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3346 __ret1 < 0 ? -1 : 0; \
3347})
3348
Tejun Heo9c6bae02012-07-13 22:16:44 -07003349static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3350{
3351 struct worker_pool *pool;
3352
3353 for_each_worker_pool(pool, gcwq)
3354 if (pool->flags & POOL_MANAGING_WORKERS)
3355 return true;
3356 return false;
3357}
3358
3359static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3360{
3361 struct worker_pool *pool;
3362
3363 for_each_worker_pool(pool, gcwq)
3364 if (!list_empty(&pool->idle_list))
3365 return true;
3366 return false;
3367}
3368
Tejun Heodb7bccf2010-06-29 10:07:12 +02003369static int __cpuinit trustee_thread(void *__gcwq)
3370{
3371 struct global_cwq *gcwq = __gcwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003372 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003373 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003374 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003375 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003376 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003377 int i;
3378
3379 BUG_ON(gcwq->cpu != smp_processor_id());
3380
3381 spin_lock_irq(&gcwq->lock);
3382 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003383 * Claim the manager position and make all workers rogue.
3384 * Trustee must be bound to the target cpu and can't be
3385 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003386 */
3387 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo9c6bae02012-07-13 22:16:44 -07003388 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003389 BUG_ON(rc < 0);
3390
Tejun Heo9c6bae02012-07-13 22:16:44 -07003391 for_each_worker_pool(pool, gcwq) {
3392 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003393
Tejun Heo9c6bae02012-07-13 22:16:44 -07003394 list_for_each_entry(worker, &pool->idle_list, entry)
3395 worker->flags |= WORKER_ROGUE;
3396 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003397
3398 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003399 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003400
3401 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003402 * Call schedule() so that we cross rq->lock and thus can
3403 * guarantee sched callbacks see the rogue flag. This is
3404 * necessary as scheduler callbacks may be invoked from other
3405 * cpus.
3406 */
3407 spin_unlock_irq(&gcwq->lock);
3408 schedule();
3409 spin_lock_irq(&gcwq->lock);
3410
3411 /*
Tejun Heocb444762010-07-02 10:03:50 +02003412 * Sched callbacks are disabled now. Zap nr_running. After
3413 * this, nr_running stays zero and need_more_worker() and
3414 * keep_working() are always true as long as the worklist is
3415 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003416 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003417 for_each_worker_pool(pool, gcwq)
3418 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003419
3420 spin_unlock_irq(&gcwq->lock);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003421 for_each_worker_pool(pool, gcwq)
3422 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003423 spin_lock_irq(&gcwq->lock);
3424
3425 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003426 * We're now in charge. Notify and proceed to drain. We need
3427 * to keep the gcwq running during the whole CPU down
3428 * procedure as other cpu hotunplug callbacks may need to
3429 * flush currently running tasks.
3430 */
3431 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3432 wake_up_all(&gcwq->trustee_wait);
3433
3434 /*
3435 * The original cpu is in the process of dying and may go away
3436 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003437 * be migrated to other cpus. Try draining any left work. We
3438 * want to get it over with ASAP - spam rescuers, wake up as
3439 * many idlers as necessary and create new ones till the
3440 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003441 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003442 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003443 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003444 while (true) {
3445 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003446
Tejun Heo9c6bae02012-07-13 22:16:44 -07003447 for_each_worker_pool(pool, gcwq)
3448 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003449
Tejun Heo9c6bae02012-07-13 22:16:44 -07003450 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3451 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3452 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003453
Tejun Heo9c6bae02012-07-13 22:16:44 -07003454 for_each_worker_pool(pool, gcwq) {
3455 int nr_works = 0;
3456
3457 list_for_each_entry(work, &pool->worklist, entry) {
3458 send_mayday(work);
3459 nr_works++;
3460 }
3461
3462 list_for_each_entry(worker, &pool->idle_list, entry) {
3463 if (!nr_works--)
3464 break;
3465 wake_up_process(worker->task);
3466 }
3467
3468 if (need_to_create_worker(pool)) {
3469 spin_unlock_irq(&gcwq->lock);
3470 worker = create_worker(pool, false);
3471 spin_lock_irq(&gcwq->lock);
3472 if (worker) {
3473 worker->flags |= WORKER_ROGUE;
3474 start_worker(worker);
3475 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003476 }
3477 }
3478
Tejun Heodb7bccf2010-06-29 10:07:12 +02003479 /* give a breather */
3480 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3481 break;
3482 }
3483
Tejun Heoe22bee72010-06-29 10:07:14 +02003484 /*
3485 * Either all works have been scheduled and cpu is down, or
3486 * cpu down has already been canceled. Wait for and butcher
3487 * all workers till we're canceled.
3488 */
3489 do {
Tejun Heo9c6bae02012-07-13 22:16:44 -07003490 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3491
3492 i = 0;
3493 for_each_worker_pool(pool, gcwq) {
3494 while (!list_empty(&pool->idle_list)) {
3495 worker = list_first_entry(&pool->idle_list,
3496 struct worker, entry);
3497 destroy_worker(worker);
3498 }
3499 i |= pool->nr_workers;
3500 }
3501 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003502
3503 /*
3504 * At this point, either draining has completed and no worker
3505 * is left, or cpu down has been canceled or the cpu is being
3506 * brought back up. There shouldn't be any idle one left.
3507 * Tell the remaining busy ones to rebind once it finishes the
3508 * currently scheduled works by scheduling the rebind_work.
3509 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003510 for_each_worker_pool(pool, gcwq)
3511 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003512
3513 for_each_busy_worker(worker, i, pos, gcwq) {
3514 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003515 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003516
3517 /*
3518 * Rebind_work may race with future cpu hotplug
3519 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003520 * rebinding is scheduled. The morphing should
3521 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003522 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003523 worker_flags |= WORKER_REBIND;
3524 worker_flags &= ~WORKER_ROGUE;
3525 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003526
3527 /* queue rebind_work, wq doesn't matter, use the default one */
3528 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3529 work_data_bits(rebind_work)))
3530 continue;
3531
3532 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003533 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003534 worker->scheduled.next,
3535 work_color_to_flags(WORK_NO_COLOR));
3536 }
3537
3538 /* relinquish manager role */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003539 for_each_worker_pool(pool, gcwq)
3540 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003541
Tejun Heodb7bccf2010-06-29 10:07:12 +02003542 /* notify completion */
3543 gcwq->trustee = NULL;
3544 gcwq->trustee_state = TRUSTEE_DONE;
3545 wake_up_all(&gcwq->trustee_wait);
3546 spin_unlock_irq(&gcwq->lock);
3547 return 0;
3548}
3549
3550/**
3551 * wait_trustee_state - wait for trustee to enter the specified state
3552 * @gcwq: gcwq the trustee of interest belongs to
3553 * @state: target state to wait for
3554 *
3555 * Wait for the trustee to reach @state. DONE is already matched.
3556 *
3557 * CONTEXT:
3558 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3559 * multiple times. To be used by cpu_callback.
3560 */
3561static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003562__releases(&gcwq->lock)
3563__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003564{
3565 if (!(gcwq->trustee_state == state ||
3566 gcwq->trustee_state == TRUSTEE_DONE)) {
3567 spin_unlock_irq(&gcwq->lock);
3568 __wait_event(gcwq->trustee_wait,
3569 gcwq->trustee_state == state ||
3570 gcwq->trustee_state == TRUSTEE_DONE);
3571 spin_lock_irq(&gcwq->lock);
3572 }
3573}
3574
Oleg Nesterov3af244332007-05-09 02:34:09 -07003575static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3576 unsigned long action,
3577 void *hcpu)
3578{
3579 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003580 struct global_cwq *gcwq = get_gcwq(cpu);
3581 struct task_struct *new_trustee = NULL;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003582 struct worker *new_workers[NR_WORKER_POOLS] = { };
3583 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003584 unsigned long flags;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003585 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003587 action &= ~CPU_TASKS_FROZEN;
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003590 case CPU_DOWN_PREPARE:
3591 new_trustee = kthread_create(trustee_thread, gcwq,
3592 "workqueue_trustee/%d\n", cpu);
3593 if (IS_ERR(new_trustee))
3594 return notifier_from_errno(PTR_ERR(new_trustee));
3595 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003596 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003598 i = 0;
3599 for_each_worker_pool(pool, gcwq) {
3600 BUG_ON(pool->first_idle);
3601 new_workers[i] = create_worker(pool, false);
3602 if (!new_workers[i++])
3603 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606
Tejun Heodb7bccf2010-06-29 10:07:12 +02003607 /* some are called w/ irq disabled, don't disturb irq status */
3608 spin_lock_irqsave(&gcwq->lock, flags);
3609
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003610 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003611 case CPU_DOWN_PREPARE:
3612 /* initialize trustee and tell it to acquire the gcwq */
3613 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3614 gcwq->trustee = new_trustee;
3615 gcwq->trustee_state = TRUSTEE_START;
3616 wake_up_process(gcwq->trustee);
3617 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003618 /* fall through */
3619 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003620 i = 0;
3621 for_each_worker_pool(pool, gcwq) {
3622 BUG_ON(pool->first_idle);
3623 pool->first_idle = new_workers[i++];
3624 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003625 break;
3626
3627 case CPU_DYING:
3628 /*
3629 * Before this, the trustee and all workers except for
3630 * the ones which are still executing works from
3631 * before the last CPU down must be on the cpu. After
3632 * this, they'll all be diasporas.
3633 */
3634 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003635 break;
3636
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003637 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003638 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003639 /* fall through */
3640 case CPU_UP_CANCELED:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003641 for_each_worker_pool(pool, gcwq) {
3642 destroy_worker(pool->first_idle);
3643 pool->first_idle = NULL;
3644 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003645 break;
3646
3647 case CPU_DOWN_FAILED:
3648 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003649 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003650 if (gcwq->trustee_state != TRUSTEE_DONE) {
3651 gcwq->trustee_state = TRUSTEE_RELEASE;
3652 wake_up_process(gcwq->trustee);
3653 wait_trustee_state(gcwq, TRUSTEE_DONE);
3654 }
3655
Tejun Heoe22bee72010-06-29 10:07:14 +02003656 /*
3657 * Trustee is done and there might be no worker left.
3658 * Put the first_idle in and request a real manager to
3659 * take a look.
3660 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003661 for_each_worker_pool(pool, gcwq) {
3662 spin_unlock_irq(&gcwq->lock);
3663 kthread_bind(pool->first_idle->task, cpu);
3664 spin_lock_irq(&gcwq->lock);
3665 pool->flags |= POOL_MANAGE_WORKERS;
3666 start_worker(pool->first_idle);
3667 pool->first_idle = NULL;
3668 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003669 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003670 }
3671
Tejun Heodb7bccf2010-06-29 10:07:12 +02003672 spin_unlock_irqrestore(&gcwq->lock, flags);
3673
Tejun Heo15376632010-06-29 10:07:11 +02003674 return notifier_from_errno(0);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003675
3676err_destroy:
3677 if (new_trustee)
3678 kthread_stop(new_trustee);
3679
3680 spin_lock_irqsave(&gcwq->lock, flags);
3681 for (i = 0; i < NR_WORKER_POOLS; i++)
3682 if (new_workers[i])
3683 destroy_worker(new_workers[i]);
3684 spin_unlock_irqrestore(&gcwq->lock, flags);
3685
3686 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Tejun Heod3b42542012-07-17 12:39:26 -07003689/*
3690 * Workqueues should be brought up before normal priority CPU notifiers.
3691 * This will be registered high priority CPU notifier.
3692 */
3693static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3694 unsigned long action,
3695 void *hcpu)
3696{
3697 switch (action & ~CPU_TASKS_FROZEN) {
3698 case CPU_UP_PREPARE:
3699 case CPU_UP_CANCELED:
3700 case CPU_DOWN_FAILED:
3701 case CPU_ONLINE:
3702 return workqueue_cpu_callback(nfb, action, hcpu);
3703 }
3704 return NOTIFY_OK;
3705}
3706
3707/*
3708 * Workqueues should be brought down after normal priority CPU notifiers.
3709 * This will be registered as low priority CPU notifier.
3710 */
3711static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3712 unsigned long action,
3713 void *hcpu)
3714{
3715 switch (action & ~CPU_TASKS_FROZEN) {
3716 case CPU_DOWN_PREPARE:
3717 case CPU_DYING:
3718 case CPU_POST_DEAD:
3719 return workqueue_cpu_callback(nfb, action, hcpu);
3720 }
3721 return NOTIFY_OK;
3722}
3723
Rusty Russell2d3854a2008-11-05 13:39:10 +11003724#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003725
Rusty Russell2d3854a2008-11-05 13:39:10 +11003726struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003727 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003728 long (*fn)(void *);
3729 void *arg;
3730 long ret;
3731};
3732
Tejun Heofc7da7e2012-09-18 12:48:43 -07003733static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003734{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003735 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3736
Rusty Russell2d3854a2008-11-05 13:39:10 +11003737 wfc->ret = wfc->fn(wfc->arg);
3738}
3739
3740/**
3741 * work_on_cpu - run a function in user context on a particular cpu
3742 * @cpu: the cpu to run on
3743 * @fn: the function to run
3744 * @arg: the function arg
3745 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003746 * This will return the value @fn returns.
3747 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003748 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003749 */
3750long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3751{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003752 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003753
Tejun Heofc7da7e2012-09-18 12:48:43 -07003754 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3755 schedule_work_on(cpu, &wfc.work);
3756 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003757 return wfc.ret;
3758}
3759EXPORT_SYMBOL_GPL(work_on_cpu);
3760#endif /* CONFIG_SMP */
3761
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003762#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303763
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764/**
3765 * freeze_workqueues_begin - begin freezing workqueues
3766 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003767 * Start freezing workqueues. After this function returns, all freezable
3768 * workqueues will queue new works to their frozen_works list instead of
3769 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003770 *
3771 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003772 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003773 */
3774void freeze_workqueues_begin(void)
3775{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003776 unsigned int cpu;
3777
3778 spin_lock(&workqueue_lock);
3779
3780 BUG_ON(workqueue_freezing);
3781 workqueue_freezing = true;
3782
Tejun Heof3421792010-07-02 10:03:51 +02003783 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003784 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003785 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003786
3787 spin_lock_irq(&gcwq->lock);
3788
Tejun Heodb7bccf2010-06-29 10:07:12 +02003789 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3790 gcwq->flags |= GCWQ_FREEZING;
3791
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003792 list_for_each_entry(wq, &workqueues, list) {
3793 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3794
Tejun Heo58a69cb2011-02-16 09:25:31 +01003795 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003796 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003797 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003798
3799 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003800 }
3801
3802 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003804
3805/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003806 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003807 *
3808 * Check whether freezing is complete. This function must be called
3809 * between freeze_workqueues_begin() and thaw_workqueues().
3810 *
3811 * CONTEXT:
3812 * Grabs and releases workqueue_lock.
3813 *
3814 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003815 * %true if some freezable workqueues are still busy. %false if freezing
3816 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003817 */
3818bool freeze_workqueues_busy(void)
3819{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003820 unsigned int cpu;
3821 bool busy = false;
3822
3823 spin_lock(&workqueue_lock);
3824
3825 BUG_ON(!workqueue_freezing);
3826
Tejun Heof3421792010-07-02 10:03:51 +02003827 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003828 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003829 /*
3830 * nr_active is monotonically decreasing. It's safe
3831 * to peek without lock.
3832 */
3833 list_for_each_entry(wq, &workqueues, list) {
3834 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3835
Tejun Heo58a69cb2011-02-16 09:25:31 +01003836 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003837 continue;
3838
3839 BUG_ON(cwq->nr_active < 0);
3840 if (cwq->nr_active) {
3841 busy = true;
3842 goto out_unlock;
3843 }
3844 }
3845 }
3846out_unlock:
3847 spin_unlock(&workqueue_lock);
3848 return busy;
3849}
3850
3851/**
3852 * thaw_workqueues - thaw workqueues
3853 *
3854 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003855 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003856 *
3857 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003858 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003859 */
3860void thaw_workqueues(void)
3861{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003862 unsigned int cpu;
3863
3864 spin_lock(&workqueue_lock);
3865
3866 if (!workqueue_freezing)
3867 goto out_unlock;
3868
Tejun Heof3421792010-07-02 10:03:51 +02003869 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003870 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003871 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003872 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003873
3874 spin_lock_irq(&gcwq->lock);
3875
Tejun Heodb7bccf2010-06-29 10:07:12 +02003876 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3877 gcwq->flags &= ~GCWQ_FREEZING;
3878
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003879 list_for_each_entry(wq, &workqueues, list) {
3880 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3881
Tejun Heo58a69cb2011-02-16 09:25:31 +01003882 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003883 continue;
3884
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003885 /* restore max_active and repopulate worklist */
3886 cwq->max_active = wq->saved_max_active;
3887
3888 while (!list_empty(&cwq->delayed_works) &&
3889 cwq->nr_active < cwq->max_active)
3890 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003891 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003892
Tejun Heo9c6bae02012-07-13 22:16:44 -07003893 for_each_worker_pool(pool, gcwq)
3894 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003895
Tejun Heo8b03ae32010-06-29 10:07:12 +02003896 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003897 }
3898
3899 workqueue_freezing = false;
3900out_unlock:
3901 spin_unlock(&workqueue_lock);
3902}
3903#endif /* CONFIG_FREEZER */
3904
Suresh Siddha6ee05782010-07-30 14:57:37 -07003905static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
Tejun Heoc34056a2010-06-29 10:07:11 +02003907 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003908 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003909
Tejun Heod3b42542012-07-17 12:39:26 -07003910 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3911 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003912
3913 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003914 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003915 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003916 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003917
3918 spin_lock_init(&gcwq->lock);
3919 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003920 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003921
Tejun Heoc8e55f32010-06-29 10:07:12 +02003922 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3923 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3924
Tejun Heo9c6bae02012-07-13 22:16:44 -07003925 for_each_worker_pool(pool, gcwq) {
3926 pool->gcwq = gcwq;
3927 INIT_LIST_HEAD(&pool->worklist);
3928 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003929
Tejun Heo9c6bae02012-07-13 22:16:44 -07003930 init_timer_deferrable(&pool->idle_timer);
3931 pool->idle_timer.function = idle_worker_timeout;
3932 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003933
Tejun Heo9c6bae02012-07-13 22:16:44 -07003934 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3935 (unsigned long)pool);
3936
3937 ida_init(&pool->worker_ida);
3938 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003939
3940 gcwq->trustee_state = TRUSTEE_DONE;
3941 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003942 }
3943
Tejun Heoe22bee72010-06-29 10:07:14 +02003944 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003945 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003946 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003947 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003948
Tejun Heo477a3c32010-08-31 10:54:35 +02003949 if (cpu != WORK_CPU_UNBOUND)
3950 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003951
3952 for_each_worker_pool(pool, gcwq) {
3953 struct worker *worker;
3954
3955 worker = create_worker(pool, true);
3956 BUG_ON(!worker);
3957 spin_lock_irq(&gcwq->lock);
3958 start_worker(worker);
3959 spin_unlock_irq(&gcwq->lock);
3960 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003961 }
3962
Tejun Heod320c032010-06-29 10:07:14 +02003963 system_wq = alloc_workqueue("events", 0, 0);
3964 system_long_wq = alloc_workqueue("events_long", 0, 0);
3965 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003966 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3967 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003968 system_freezable_wq = alloc_workqueue("events_freezable",
3969 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003970 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3971 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003972 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003973 !system_unbound_wq || !system_freezable_wq ||
3974 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003977early_initcall(init_workqueues);